]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/rculist.h
rcu: List-debug variants of rcu list routines.
[mirror_ubuntu-bionic-kernel.git] / include / linux / rculist.h
CommitLineData
82524746
FBH
1#ifndef _LINUX_RCULIST_H
2#define _LINUX_RCULIST_H
3
4#ifdef __KERNEL__
5
6/*
7 * RCU-protected list version
8 */
9#include <linux/list.h>
10aa9d2c 10#include <linux/rcupdate.h>
82524746 11
65e6bf48
PM
12/*
13 * Why is there no list_empty_rcu()? Because list_empty() serves this
14 * purpose. The list_empty() function fetches the RCU-protected pointer
15 * and compares it to the address of the list head, but neither dereferences
16 * this pointer itself nor provides this pointer to the caller. Therefore,
17 * it is not necessary to use rcu_dereference(), so that list_empty() can
18 * be used anywhere you would want to use a list_empty_rcu().
19 */
20
67bdbffd
AB
21/*
22 * return the ->next pointer of a list_head in an rcu safe
23 * way, we must not access it directly
24 */
25#define list_next_rcu(list) (*((struct list_head __rcu **)(&(list)->next)))
26
82524746
FBH
27/*
28 * Insert a new entry between two known consecutive entries.
29 *
30 * This is only for internal list manipulation where we know
31 * the prev/next entries already!
32 */
559f9bad 33#ifndef CONFIG_DEBUG_LIST
82524746
FBH
34static inline void __list_add_rcu(struct list_head *new,
35 struct list_head *prev, struct list_head *next)
36{
37 new->next = next;
38 new->prev = prev;
67bdbffd 39 rcu_assign_pointer(list_next_rcu(prev), new);
82524746 40 next->prev = new;
82524746 41}
559f9bad
DJ
42#else
43extern void __list_add_rcu(struct list_head *new,
44 struct list_head *prev, struct list_head *next);
45#endif
82524746
FBH
46
47/**
48 * list_add_rcu - add a new entry to rcu-protected list
49 * @new: new entry to be added
50 * @head: list head to add it after
51 *
52 * Insert a new entry after the specified head.
53 * This is good for implementing stacks.
54 *
55 * The caller must take whatever precautions are necessary
56 * (such as holding appropriate locks) to avoid racing
57 * with another list-mutation primitive, such as list_add_rcu()
58 * or list_del_rcu(), running on this same list.
59 * However, it is perfectly legal to run concurrently with
60 * the _rcu list-traversal primitives, such as
61 * list_for_each_entry_rcu().
62 */
63static inline void list_add_rcu(struct list_head *new, struct list_head *head)
64{
65 __list_add_rcu(new, head, head->next);
66}
67
68/**
69 * list_add_tail_rcu - add a new entry to rcu-protected list
70 * @new: new entry to be added
71 * @head: list head to add it before
72 *
73 * Insert a new entry before the specified head.
74 * This is useful for implementing queues.
75 *
76 * The caller must take whatever precautions are necessary
77 * (such as holding appropriate locks) to avoid racing
78 * with another list-mutation primitive, such as list_add_tail_rcu()
79 * or list_del_rcu(), running on this same list.
80 * However, it is perfectly legal to run concurrently with
81 * the _rcu list-traversal primitives, such as
82 * list_for_each_entry_rcu().
83 */
84static inline void list_add_tail_rcu(struct list_head *new,
85 struct list_head *head)
86{
87 __list_add_rcu(new, head->prev, head);
88}
89
90/**
91 * list_del_rcu - deletes entry from list without re-initialization
92 * @entry: the element to delete from the list.
93 *
94 * Note: list_empty() on entry does not return true after this,
95 * the entry is in an undefined state. It is useful for RCU based
96 * lockfree traversal.
97 *
98 * In particular, it means that we can not poison the forward
99 * pointers that may still be used for walking the list.
100 *
101 * The caller must take whatever precautions are necessary
102 * (such as holding appropriate locks) to avoid racing
103 * with another list-mutation primitive, such as list_del_rcu()
104 * or list_add_rcu(), running on this same list.
105 * However, it is perfectly legal to run concurrently with
106 * the _rcu list-traversal primitives, such as
107 * list_for_each_entry_rcu().
108 *
109 * Note that the caller is not permitted to immediately free
110 * the newly deleted entry. Instead, either synchronize_rcu()
111 * or call_rcu() must be used to defer freeing until an RCU
112 * grace period has elapsed.
113 */
114static inline void list_del_rcu(struct list_head *entry)
115{
559f9bad 116 __list_del_entry(entry);
82524746
FBH
117 entry->prev = LIST_POISON2;
118}
119
6beeac76
AA
120/**
121 * hlist_del_init_rcu - deletes entry from hash list with re-initialization
122 * @n: the element to delete from the hash list.
123 *
124 * Note: list_unhashed() on the node return true after this. It is
125 * useful for RCU based read lockfree traversal if the writer side
126 * must know if the list entry is still hashed or already unhashed.
127 *
128 * In particular, it means that we can not poison the forward pointers
129 * that may still be used for walking the hash list and we can only
130 * zero the pprev pointer so list_unhashed() will return true after
131 * this.
132 *
133 * The caller must take whatever precautions are necessary (such as
134 * holding appropriate locks) to avoid racing with another
135 * list-mutation primitive, such as hlist_add_head_rcu() or
136 * hlist_del_rcu(), running on this same list. However, it is
137 * perfectly legal to run concurrently with the _rcu list-traversal
138 * primitives, such as hlist_for_each_entry_rcu().
139 */
140static inline void hlist_del_init_rcu(struct hlist_node *n)
141{
142 if (!hlist_unhashed(n)) {
143 __hlist_del(n);
144 n->pprev = NULL;
145 }
146}
147
82524746
FBH
148/**
149 * list_replace_rcu - replace old entry by new one
150 * @old : the element to be replaced
151 * @new : the new element to insert
152 *
153 * The @old entry will be replaced with the @new entry atomically.
154 * Note: @old should not be empty.
155 */
156static inline void list_replace_rcu(struct list_head *old,
157 struct list_head *new)
158{
159 new->next = old->next;
160 new->prev = old->prev;
67bdbffd 161 rcu_assign_pointer(list_next_rcu(new->prev), new);
82524746 162 new->next->prev = new;
82524746
FBH
163 old->prev = LIST_POISON2;
164}
165
166/**
167 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
168 * @list: the RCU-protected list to splice
169 * @head: the place in the list to splice the first list into
170 * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
171 *
172 * @head can be RCU-read traversed concurrently with this function.
173 *
174 * Note that this function blocks.
175 *
176 * Important note: the caller must take whatever action is necessary to
177 * prevent any other updates to @head. In principle, it is possible
178 * to modify the list as soon as sync() begins execution.
179 * If this sort of thing becomes necessary, an alternative version
180 * based on call_rcu() could be created. But only if -really-
181 * needed -- there is no shortage of RCU API members.
182 */
183static inline void list_splice_init_rcu(struct list_head *list,
184 struct list_head *head,
185 void (*sync)(void))
186{
187 struct list_head *first = list->next;
188 struct list_head *last = list->prev;
189 struct list_head *at = head->next;
190
7f708931 191 if (list_empty(list))
82524746
FBH
192 return;
193
194 /* "first" and "last" tracking list, so initialize it. */
195
196 INIT_LIST_HEAD(list);
197
198 /*
199 * At this point, the list body still points to the source list.
200 * Wait for any readers to finish using the list before splicing
201 * the list body into the new list. Any new readers will see
202 * an empty list.
203 */
204
205 sync();
206
207 /*
208 * Readers are finished with the source list, so perform splice.
209 * The order is important if the new list is global and accessible
210 * to concurrent RCU readers. Note that RCU readers are not
211 * permitted to traverse the prev pointers without excluding
212 * this function.
213 */
214
215 last->next = at;
67bdbffd 216 rcu_assign_pointer(list_next_rcu(head), first);
82524746
FBH
217 first->prev = head;
218 at->prev = last;
219}
220
72c6a987
JP
221/**
222 * list_entry_rcu - get the struct for this entry
223 * @ptr: the &struct list_head pointer.
224 * @type: the type of the struct this is embedded in.
225 * @member: the name of the list_struct within the struct.
226 *
227 * This primitive may safely run concurrently with the _rcu list-mutation
228 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
229 */
230#define list_entry_rcu(ptr, type, member) \
67bdbffd
AB
231 ({typeof (*ptr) __rcu *__ptr = (typeof (*ptr) __rcu __force *)ptr; \
232 container_of((typeof(ptr))rcu_dereference_raw(__ptr), type, member); \
233 })
72c6a987
JP
234
235/**
236 * list_first_entry_rcu - get the first element from a list
237 * @ptr: the list head to take the element from.
238 * @type: the type of the struct this is embedded in.
239 * @member: the name of the list_struct within the struct.
240 *
241 * Note, that list is expected to be not empty.
242 *
243 * This primitive may safely run concurrently with the _rcu list-mutation
244 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
245 */
246#define list_first_entry_rcu(ptr, type, member) \
247 list_entry_rcu((ptr)->next, type, member)
248
82524746
FBH
249/**
250 * list_for_each_entry_rcu - iterate over rcu list of given type
251 * @pos: the type * to use as a loop cursor.
252 * @head: the head for your list.
253 * @member: the name of the list_struct within the struct.
254 *
255 * This list-traversal primitive may safely run concurrently with
256 * the _rcu list-mutation primitives such as list_add_rcu()
257 * as long as the traversal is guarded by rcu_read_lock().
258 */
259#define list_for_each_entry_rcu(pos, head, member) \
72c6a987 260 for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \
e66eed65 261 &pos->member != (head); \
72c6a987 262 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
82524746
FBH
263
264
265/**
266 * list_for_each_continue_rcu
267 * @pos: the &struct list_head to use as a loop cursor.
268 * @head: the head for your list.
269 *
270 * Iterate over an rcu-protected list, continuing after current point.
271 *
272 * This list-traversal primitive may safely run concurrently with
273 * the _rcu list-mutation primitives such as list_add_rcu()
274 * as long as the traversal is guarded by rcu_read_lock().
275 */
276#define list_for_each_continue_rcu(pos, head) \
67bdbffd 277 for ((pos) = rcu_dereference_raw(list_next_rcu(pos)); \
e66eed65 278 (pos) != (head); \
67bdbffd 279 (pos) = rcu_dereference_raw(list_next_rcu(pos)))
82524746 280
254245d2 281/**
282 * list_for_each_entry_continue_rcu - continue iteration over list of given type
283 * @pos: the type * to use as a loop cursor.
284 * @head: the head for your list.
285 * @member: the name of the list_struct within the struct.
286 *
287 * Continue to iterate over list of given type, continuing after
288 * the current position.
289 */
290#define list_for_each_entry_continue_rcu(pos, head, member) \
291 for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \
e66eed65 292 &pos->member != (head); \
254245d2 293 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
294
82524746
FBH
295/**
296 * hlist_del_rcu - deletes entry from hash list without re-initialization
297 * @n: the element to delete from the hash list.
298 *
299 * Note: list_unhashed() on entry does not return true after this,
300 * the entry is in an undefined state. It is useful for RCU based
301 * lockfree traversal.
302 *
303 * In particular, it means that we can not poison the forward
304 * pointers that may still be used for walking the hash list.
305 *
306 * The caller must take whatever precautions are necessary
307 * (such as holding appropriate locks) to avoid racing
308 * with another list-mutation primitive, such as hlist_add_head_rcu()
309 * or hlist_del_rcu(), running on this same list.
310 * However, it is perfectly legal to run concurrently with
311 * the _rcu list-traversal primitives, such as
312 * hlist_for_each_entry().
313 */
314static inline void hlist_del_rcu(struct hlist_node *n)
315{
316 __hlist_del(n);
317 n->pprev = LIST_POISON2;
318}
319
320/**
321 * hlist_replace_rcu - replace old entry by new one
322 * @old : the element to be replaced
323 * @new : the new element to insert
324 *
325 * The @old entry will be replaced with the @new entry atomically.
326 */
327static inline void hlist_replace_rcu(struct hlist_node *old,
328 struct hlist_node *new)
329{
330 struct hlist_node *next = old->next;
331
332 new->next = next;
333 new->pprev = old->pprev;
67bdbffd 334 rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new);
82524746
FBH
335 if (next)
336 new->next->pprev = &new->next;
82524746
FBH
337 old->pprev = LIST_POISON2;
338}
339
67bdbffd
AB
340/*
341 * return the first or the next element in an RCU protected hlist
342 */
343#define hlist_first_rcu(head) (*((struct hlist_node __rcu **)(&(head)->first)))
344#define hlist_next_rcu(node) (*((struct hlist_node __rcu **)(&(node)->next)))
345#define hlist_pprev_rcu(node) (*((struct hlist_node __rcu **)((node)->pprev)))
346
82524746
FBH
347/**
348 * hlist_add_head_rcu
349 * @n: the element to add to the hash list.
350 * @h: the list to add to.
351 *
352 * Description:
353 * Adds the specified element to the specified hlist,
354 * while permitting racing traversals.
355 *
356 * The caller must take whatever precautions are necessary
357 * (such as holding appropriate locks) to avoid racing
358 * with another list-mutation primitive, such as hlist_add_head_rcu()
359 * or hlist_del_rcu(), running on this same list.
360 * However, it is perfectly legal to run concurrently with
361 * the _rcu list-traversal primitives, such as
362 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
363 * problems on Alpha CPUs. Regardless of the type of CPU, the
364 * list-traversal primitive must be guarded by rcu_read_lock().
365 */
366static inline void hlist_add_head_rcu(struct hlist_node *n,
367 struct hlist_head *h)
368{
369 struct hlist_node *first = h->first;
10aa9d2c 370
82524746
FBH
371 n->next = first;
372 n->pprev = &h->first;
67bdbffd 373 rcu_assign_pointer(hlist_first_rcu(h), n);
82524746
FBH
374 if (first)
375 first->pprev = &n->next;
82524746
FBH
376}
377
378/**
379 * hlist_add_before_rcu
380 * @n: the new element to add to the hash list.
381 * @next: the existing element to add the new element before.
382 *
383 * Description:
384 * Adds the specified element to the specified hlist
385 * before the specified node while permitting racing traversals.
386 *
387 * The caller must take whatever precautions are necessary
388 * (such as holding appropriate locks) to avoid racing
389 * with another list-mutation primitive, such as hlist_add_head_rcu()
390 * or hlist_del_rcu(), running on this same list.
391 * However, it is perfectly legal to run concurrently with
392 * the _rcu list-traversal primitives, such as
393 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
394 * problems on Alpha CPUs.
395 */
396static inline void hlist_add_before_rcu(struct hlist_node *n,
397 struct hlist_node *next)
398{
399 n->pprev = next->pprev;
400 n->next = next;
67bdbffd 401 rcu_assign_pointer(hlist_pprev_rcu(n), n);
82524746 402 next->pprev = &n->next;
82524746
FBH
403}
404
405/**
406 * hlist_add_after_rcu
407 * @prev: the existing element to add the new element after.
408 * @n: the new element to add to the hash list.
409 *
410 * Description:
411 * Adds the specified element to the specified hlist
412 * after the specified node while permitting racing traversals.
413 *
414 * The caller must take whatever precautions are necessary
415 * (such as holding appropriate locks) to avoid racing
416 * with another list-mutation primitive, such as hlist_add_head_rcu()
417 * or hlist_del_rcu(), running on this same list.
418 * However, it is perfectly legal to run concurrently with
419 * the _rcu list-traversal primitives, such as
420 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
421 * problems on Alpha CPUs.
422 */
423static inline void hlist_add_after_rcu(struct hlist_node *prev,
424 struct hlist_node *n)
425{
426 n->next = prev->next;
427 n->pprev = &prev->next;
67bdbffd 428 rcu_assign_pointer(hlist_next_rcu(prev), n);
82524746
FBH
429 if (n->next)
430 n->next->pprev = &n->next;
431}
432
67bdbffd
AB
433#define __hlist_for_each_rcu(pos, head) \
434 for (pos = rcu_dereference(hlist_first_rcu(head)); \
75d65a42 435 pos; \
67bdbffd 436 pos = rcu_dereference(hlist_next_rcu(pos)))
1cc52327 437
82524746
FBH
438/**
439 * hlist_for_each_entry_rcu - iterate over rcu list of given type
440 * @tpos: the type * to use as a loop cursor.
441 * @pos: the &struct hlist_node to use as a loop cursor.
442 * @head: the head for your list.
443 * @member: the name of the hlist_node within the struct.
444 *
445 * This list-traversal primitive may safely run concurrently with
446 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
447 * as long as the traversal is guarded by rcu_read_lock().
448 */
67bdbffd
AB
449#define hlist_for_each_entry_rcu(tpos, pos, head, member) \
450 for (pos = rcu_dereference_raw(hlist_first_rcu(head)); \
75d65a42 451 pos && \
82524746 452 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
67bdbffd 453 pos = rcu_dereference_raw(hlist_next_rcu(pos)))
82524746 454
4f70ecca
ED
455/**
456 * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
457 * @tpos: the type * to use as a loop cursor.
458 * @pos: the &struct hlist_node to use as a loop cursor.
459 * @head: the head for your list.
460 * @member: the name of the hlist_node within the struct.
461 *
462 * This list-traversal primitive may safely run concurrently with
463 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
464 * as long as the traversal is guarded by rcu_read_lock().
465 */
466#define hlist_for_each_entry_rcu_bh(tpos, pos, head, member) \
467 for (pos = rcu_dereference_bh((head)->first); \
75d65a42 468 pos && \
4f70ecca
ED
469 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
470 pos = rcu_dereference_bh(pos->next))
471
5c578aed 472/**
473 * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point
474 * @tpos: the type * to use as a loop cursor.
475 * @pos: the &struct hlist_node to use as a loop cursor.
476 * @member: the name of the hlist_node within the struct.
477 */
478#define hlist_for_each_entry_continue_rcu(tpos, pos, member) \
479 for (pos = rcu_dereference((pos)->next); \
75d65a42 480 pos && \
5c578aed 481 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
482 pos = rcu_dereference(pos->next))
483
4f70ecca
ED
484/**
485 * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point
486 * @tpos: the type * to use as a loop cursor.
487 * @pos: the &struct hlist_node to use as a loop cursor.
488 * @member: the name of the hlist_node within the struct.
489 */
490#define hlist_for_each_entry_continue_rcu_bh(tpos, pos, member) \
491 for (pos = rcu_dereference_bh((pos)->next); \
75d65a42 492 pos && \
4f70ecca
ED
493 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
494 pos = rcu_dereference_bh(pos->next))
495
5c578aed 496
82524746
FBH
497#endif /* __KERNEL__ */
498#endif