]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/rculist.h
kernel/rcu/tree.c: Fix kernel-doc warnings
[mirror_ubuntu-jammy-kernel.git] / include / linux / rculist.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
82524746
FBH
2#ifndef _LINUX_RCULIST_H
3#define _LINUX_RCULIST_H
4
5#ifdef __KERNEL__
6
7/*
8 * RCU-protected list version
9 */
10#include <linux/list.h>
10aa9d2c 11#include <linux/rcupdate.h>
82524746 12
65e6bf48
PM
13/*
14 * Why is there no list_empty_rcu()? Because list_empty() serves this
15 * purpose. The list_empty() function fetches the RCU-protected pointer
16 * and compares it to the address of the list head, but neither dereferences
17 * this pointer itself nor provides this pointer to the caller. Therefore,
18 * it is not necessary to use rcu_dereference(), so that list_empty() can
19 * be used anywhere you would want to use a list_empty_rcu().
20 */
21
2a855b64
PM
22/*
23 * INIT_LIST_HEAD_RCU - Initialize a list_head visible to RCU readers
24 * @list: list to be initialized
25 *
26 * You should instead use INIT_LIST_HEAD() for normal initialization and
27 * cleanup tasks, when readers have no access to the list being initialized.
28 * However, if the list being initialized is visible to readers, you
29 * need to keep the compiler from being too mischievous.
30 */
31static inline void INIT_LIST_HEAD_RCU(struct list_head *list)
32{
7d0ae808
PM
33 WRITE_ONCE(list->next, list);
34 WRITE_ONCE(list->prev, list);
2a855b64
PM
35}
36
67bdbffd
AB
37/*
38 * return the ->next pointer of a list_head in an rcu safe
39 * way, we must not access it directly
40 */
41#define list_next_rcu(list) (*((struct list_head __rcu **)(&(list)->next)))
42
afa47fdf
MB
43/**
44 * list_tail_rcu - returns the prev pointer of the head of the list
45 * @head: the head of the list
46 *
47 * Note: This should only be used with the list header, and even then
48 * only if list_del() and similar primitives are not also used on the
49 * list header.
50 */
51#define list_tail_rcu(head) (*((struct list_head __rcu **)(&(head)->prev)))
52
28875945
JFG
53/*
54 * Check during list traversal that we are within an RCU reader
55 */
56
57#define check_arg_count_one(dummy)
58
59#ifdef CONFIG_PROVE_RCU_LIST
60#define __list_check_rcu(dummy, cond, extra...) \
61 ({ \
62 check_arg_count_one(extra); \
4dfd5cd8 63 RCU_LOCKDEP_WARN(!(cond) && !rcu_read_lock_any_held(), \
28875945 64 "RCU-list traversed in non-reader section!"); \
4dfd5cd8 65 })
28875945
JFG
66#else
67#define __list_check_rcu(dummy, cond, extra...) \
68 ({ check_arg_count_one(extra); })
69#endif
70
82524746
FBH
71/*
72 * Insert a new entry between two known consecutive entries.
73 *
74 * This is only for internal list manipulation where we know
75 * the prev/next entries already!
76 */
77static inline void __list_add_rcu(struct list_head *new,
78 struct list_head *prev, struct list_head *next)
79{
54acd439
KC
80 if (!__list_add_valid(new, prev, next))
81 return;
82
82524746
FBH
83 new->next = next;
84 new->prev = prev;
67bdbffd 85 rcu_assign_pointer(list_next_rcu(prev), new);
82524746 86 next->prev = new;
82524746
FBH
87}
88
89/**
90 * list_add_rcu - add a new entry to rcu-protected list
91 * @new: new entry to be added
92 * @head: list head to add it after
93 *
94 * Insert a new entry after the specified head.
95 * This is good for implementing stacks.
96 *
97 * The caller must take whatever precautions are necessary
98 * (such as holding appropriate locks) to avoid racing
99 * with another list-mutation primitive, such as list_add_rcu()
100 * or list_del_rcu(), running on this same list.
101 * However, it is perfectly legal to run concurrently with
102 * the _rcu list-traversal primitives, such as
103 * list_for_each_entry_rcu().
104 */
105static inline void list_add_rcu(struct list_head *new, struct list_head *head)
106{
107 __list_add_rcu(new, head, head->next);
108}
109
110/**
111 * list_add_tail_rcu - add a new entry to rcu-protected list
112 * @new: new entry to be added
113 * @head: list head to add it before
114 *
115 * Insert a new entry before the specified head.
116 * This is useful for implementing queues.
117 *
118 * The caller must take whatever precautions are necessary
119 * (such as holding appropriate locks) to avoid racing
120 * with another list-mutation primitive, such as list_add_tail_rcu()
121 * or list_del_rcu(), running on this same list.
122 * However, it is perfectly legal to run concurrently with
123 * the _rcu list-traversal primitives, such as
124 * list_for_each_entry_rcu().
125 */
126static inline void list_add_tail_rcu(struct list_head *new,
127 struct list_head *head)
128{
129 __list_add_rcu(new, head->prev, head);
130}
131
132/**
133 * list_del_rcu - deletes entry from list without re-initialization
134 * @entry: the element to delete from the list.
135 *
136 * Note: list_empty() on entry does not return true after this,
137 * the entry is in an undefined state. It is useful for RCU based
138 * lockfree traversal.
139 *
140 * In particular, it means that we can not poison the forward
141 * pointers that may still be used for walking the list.
142 *
143 * The caller must take whatever precautions are necessary
144 * (such as holding appropriate locks) to avoid racing
145 * with another list-mutation primitive, such as list_del_rcu()
146 * or list_add_rcu(), running on this same list.
147 * However, it is perfectly legal to run concurrently with
148 * the _rcu list-traversal primitives, such as
149 * list_for_each_entry_rcu().
150 *
151 * Note that the caller is not permitted to immediately free
152 * the newly deleted entry. Instead, either synchronize_rcu()
153 * or call_rcu() must be used to defer freeing until an RCU
154 * grace period has elapsed.
155 */
156static inline void list_del_rcu(struct list_head *entry)
157{
559f9bad 158 __list_del_entry(entry);
82524746
FBH
159 entry->prev = LIST_POISON2;
160}
161
6beeac76
AA
162/**
163 * hlist_del_init_rcu - deletes entry from hash list with re-initialization
164 * @n: the element to delete from the hash list.
165 *
166 * Note: list_unhashed() on the node return true after this. It is
167 * useful for RCU based read lockfree traversal if the writer side
168 * must know if the list entry is still hashed or already unhashed.
169 *
170 * In particular, it means that we can not poison the forward pointers
171 * that may still be used for walking the hash list and we can only
172 * zero the pprev pointer so list_unhashed() will return true after
173 * this.
174 *
175 * The caller must take whatever precautions are necessary (such as
176 * holding appropriate locks) to avoid racing with another
177 * list-mutation primitive, such as hlist_add_head_rcu() or
178 * hlist_del_rcu(), running on this same list. However, it is
179 * perfectly legal to run concurrently with the _rcu list-traversal
180 * primitives, such as hlist_for_each_entry_rcu().
181 */
182static inline void hlist_del_init_rcu(struct hlist_node *n)
183{
184 if (!hlist_unhashed(n)) {
185 __hlist_del(n);
c54a2744 186 WRITE_ONCE(n->pprev, NULL);
6beeac76
AA
187 }
188}
189
82524746
FBH
190/**
191 * list_replace_rcu - replace old entry by new one
192 * @old : the element to be replaced
193 * @new : the new element to insert
194 *
195 * The @old entry will be replaced with the @new entry atomically.
196 * Note: @old should not be empty.
197 */
198static inline void list_replace_rcu(struct list_head *old,
199 struct list_head *new)
200{
201 new->next = old->next;
202 new->prev = old->prev;
67bdbffd 203 rcu_assign_pointer(list_next_rcu(new->prev), new);
82524746 204 new->next->prev = new;
82524746
FBH
205 old->prev = LIST_POISON2;
206}
207
208/**
7d86dccf 209 * __list_splice_init_rcu - join an RCU-protected list into an existing list.
82524746 210 * @list: the RCU-protected list to splice
7d86dccf
PM
211 * @prev: points to the last element of the existing list
212 * @next: points to the first element of the existing list
aff5f036 213 * @sync: synchronize_rcu, synchronize_rcu_expedited, ...
82524746 214 *
7d86dccf
PM
215 * The list pointed to by @prev and @next can be RCU-read traversed
216 * concurrently with this function.
82524746
FBH
217 *
218 * Note that this function blocks.
219 *
7d86dccf
PM
220 * Important note: the caller must take whatever action is necessary to prevent
221 * any other updates to the existing list. In principle, it is possible to
222 * modify the list as soon as sync() begins execution. If this sort of thing
223 * becomes necessary, an alternative version based on call_rcu() could be
224 * created. But only if -really- needed -- there is no shortage of RCU API
225 * members.
82524746 226 */
7d86dccf
PM
227static inline void __list_splice_init_rcu(struct list_head *list,
228 struct list_head *prev,
229 struct list_head *next,
230 void (*sync)(void))
82524746
FBH
231{
232 struct list_head *first = list->next;
233 struct list_head *last = list->prev;
82524746 234
2a855b64
PM
235 /*
236 * "first" and "last" tracking list, so initialize it. RCU readers
237 * have access to this list, so we must use INIT_LIST_HEAD_RCU()
238 * instead of INIT_LIST_HEAD().
239 */
82524746 240
2a855b64 241 INIT_LIST_HEAD_RCU(list);
82524746
FBH
242
243 /*
244 * At this point, the list body still points to the source list.
245 * Wait for any readers to finish using the list before splicing
246 * the list body into the new list. Any new readers will see
247 * an empty list.
248 */
249
250 sync();
251
252 /*
253 * Readers are finished with the source list, so perform splice.
254 * The order is important if the new list is global and accessible
255 * to concurrent RCU readers. Note that RCU readers are not
256 * permitted to traverse the prev pointers without excluding
257 * this function.
258 */
259
7d86dccf
PM
260 last->next = next;
261 rcu_assign_pointer(list_next_rcu(prev), first);
262 first->prev = prev;
263 next->prev = last;
264}
265
266/**
267 * list_splice_init_rcu - splice an RCU-protected list into an existing list,
268 * designed for stacks.
269 * @list: the RCU-protected list to splice
270 * @head: the place in the existing list to splice the first list into
aff5f036 271 * @sync: synchronize_rcu, synchronize_rcu_expedited, ...
7d86dccf
PM
272 */
273static inline void list_splice_init_rcu(struct list_head *list,
274 struct list_head *head,
275 void (*sync)(void))
276{
277 if (!list_empty(list))
278 __list_splice_init_rcu(list, head, head->next, sync);
279}
280
281/**
282 * list_splice_tail_init_rcu - splice an RCU-protected list into an existing
283 * list, designed for queues.
284 * @list: the RCU-protected list to splice
285 * @head: the place in the existing list to splice the first list into
aff5f036 286 * @sync: synchronize_rcu, synchronize_rcu_expedited, ...
7d86dccf
PM
287 */
288static inline void list_splice_tail_init_rcu(struct list_head *list,
289 struct list_head *head,
290 void (*sync)(void))
291{
292 if (!list_empty(list))
293 __list_splice_init_rcu(list, head->prev, head, sync);
82524746
FBH
294}
295
72c6a987
JP
296/**
297 * list_entry_rcu - get the struct for this entry
298 * @ptr: the &struct list_head pointer.
299 * @type: the type of the struct this is embedded in.
3943f42c 300 * @member: the name of the list_head within the struct.
72c6a987
JP
301 *
302 * This primitive may safely run concurrently with the _rcu list-mutation
303 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
304 */
305#define list_entry_rcu(ptr, type, member) \
506458ef 306 container_of(READ_ONCE(ptr), type, member)
72c6a987 307
27fdb35f 308/*
f88022a4
MM
309 * Where are list_empty_rcu() and list_first_entry_rcu()?
310 *
311 * Implementing those functions following their counterparts list_empty() and
312 * list_first_entry() is not advisable because they lead to subtle race
313 * conditions as the following snippet shows:
314 *
315 * if (!list_empty_rcu(mylist)) {
316 * struct foo *bar = list_first_entry_rcu(mylist, struct foo, list_member);
317 * do_something(bar);
318 * }
319 *
320 * The list may not be empty when list_empty_rcu checks it, but it may be when
321 * list_first_entry_rcu rereads the ->next pointer.
322 *
323 * Rereading the ->next pointer is not a problem for list_empty() and
324 * list_first_entry() because they would be protected by a lock that blocks
325 * writers.
326 *
327 * See list_first_or_null_rcu for an alternative.
328 */
329
330/**
331 * list_first_or_null_rcu - get the first element from a list
72c6a987
JP
332 * @ptr: the list head to take the element from.
333 * @type: the type of the struct this is embedded in.
3943f42c 334 * @member: the name of the list_head within the struct.
72c6a987 335 *
f88022a4 336 * Note that if the list is empty, it returns NULL.
72c6a987
JP
337 *
338 * This primitive may safely run concurrently with the _rcu list-mutation
339 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
340 */
f88022a4 341#define list_first_or_null_rcu(ptr, type, member) \
0adab9b9
JP
342({ \
343 struct list_head *__ptr = (ptr); \
7d0ae808 344 struct list_head *__next = READ_ONCE(__ptr->next); \
0adab9b9
JP
345 likely(__ptr != __next) ? list_entry_rcu(__next, type, member) : NULL; \
346})
72c6a987 347
ff3c44e6
TH
348/**
349 * list_next_or_null_rcu - get the first element from a list
350 * @head: the head for the list.
351 * @ptr: the list head to take the next element from.
352 * @type: the type of the struct this is embedded in.
353 * @member: the name of the list_head within the struct.
354 *
355 * Note that if the ptr is at the end of the list, NULL is returned.
356 *
357 * This primitive may safely run concurrently with the _rcu list-mutation
358 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
359 */
360#define list_next_or_null_rcu(head, ptr, type, member) \
361({ \
362 struct list_head *__head = (head); \
363 struct list_head *__ptr = (ptr); \
364 struct list_head *__next = READ_ONCE(__ptr->next); \
365 likely(__next != __head) ? list_entry_rcu(__next, type, \
366 member) : NULL; \
367})
368
82524746
FBH
369/**
370 * list_for_each_entry_rcu - iterate over rcu list of given type
371 * @pos: the type * to use as a loop cursor.
372 * @head: the head for your list.
3943f42c 373 * @member: the name of the list_head within the struct.
ddc46593 374 * @cond: optional lockdep expression if called from non-RCU protection.
82524746
FBH
375 *
376 * This list-traversal primitive may safely run concurrently with
377 * the _rcu list-mutation primitives such as list_add_rcu()
378 * as long as the traversal is guarded by rcu_read_lock().
379 */
28875945
JFG
380#define list_for_each_entry_rcu(pos, head, member, cond...) \
381 for (__list_check_rcu(dummy, ## cond, 0), \
382 pos = list_entry_rcu((head)->next, typeof(*pos), member); \
383 &pos->member != (head); \
72c6a987 384 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
82524746 385
69b90729
AK
386/**
387 * list_entry_lockless - get the struct for this entry
388 * @ptr: the &struct list_head pointer.
389 * @type: the type of the struct this is embedded in.
390 * @member: the name of the list_head within the struct.
391 *
aff5f036
PM
392 * This primitive may safely run concurrently with the _rcu
393 * list-mutation primitives such as list_add_rcu(), but requires some
394 * implicit RCU read-side guarding. One example is running within a special
395 * exception-time environment where preemption is disabled and where lockdep
396 * cannot be invoked. Another example is when items are added to the list,
397 * but never deleted.
69b90729
AK
398 */
399#define list_entry_lockless(ptr, type, member) \
506458ef 400 container_of((typeof(ptr))READ_ONCE(ptr), type, member)
69b90729
AK
401
402/**
403 * list_for_each_entry_lockless - iterate over rcu list of given type
404 * @pos: the type * to use as a loop cursor.
405 * @head: the head for your list.
406 * @member: the name of the list_struct within the struct.
407 *
aff5f036
PM
408 * This primitive may safely run concurrently with the _rcu
409 * list-mutation primitives such as list_add_rcu(), but requires some
410 * implicit RCU read-side guarding. One example is running within a special
411 * exception-time environment where preemption is disabled and where lockdep
412 * cannot be invoked. Another example is when items are added to the list,
413 * but never deleted.
69b90729
AK
414 */
415#define list_for_each_entry_lockless(pos, head, member) \
416 for (pos = list_entry_lockless((head)->next, typeof(*pos), member); \
417 &pos->member != (head); \
418 pos = list_entry_lockless(pos->member.next, typeof(*pos), member))
419
254245d2 420/**
421 * list_for_each_entry_continue_rcu - continue iteration over list of given type
422 * @pos: the type * to use as a loop cursor.
423 * @head: the head for your list.
3943f42c 424 * @member: the name of the list_head within the struct.
254245d2 425 *
426 * Continue to iterate over list of given type, continuing after
b7b6f94c
N
427 * the current position which must have been in the list when the RCU read
428 * lock was taken.
429 * This would typically require either that you obtained the node from a
430 * previous walk of the list in the same RCU read-side critical section, or
431 * that you held some sort of non-RCU reference (such as a reference count)
432 * to keep the node alive *and* in the list.
433 *
434 * This iterator is similar to list_for_each_entry_from_rcu() except
435 * this starts after the given position and that one starts at the given
436 * position.
254245d2 437 */
438#define list_for_each_entry_continue_rcu(pos, head, member) \
439 for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \
e66eed65 440 &pos->member != (head); \
254245d2 441 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
442
ead9ad72
N
443/**
444 * list_for_each_entry_from_rcu - iterate over a list from current point
445 * @pos: the type * to use as a loop cursor.
446 * @head: the head for your list.
447 * @member: the name of the list_node within the struct.
448 *
449 * Iterate over the tail of a list starting from a given position,
450 * which must have been in the list when the RCU read lock was taken.
b7b6f94c
N
451 * This would typically require either that you obtained the node from a
452 * previous walk of the list in the same RCU read-side critical section, or
453 * that you held some sort of non-RCU reference (such as a reference count)
454 * to keep the node alive *and* in the list.
455 *
456 * This iterator is similar to list_for_each_entry_continue_rcu() except
457 * this starts from the given position and that one starts from the position
458 * after the given position.
ead9ad72
N
459 */
460#define list_for_each_entry_from_rcu(pos, head, member) \
461 for (; &(pos)->member != (head); \
462 pos = list_entry_rcu(pos->member.next, typeof(*(pos)), member))
463
82524746
FBH
464/**
465 * hlist_del_rcu - deletes entry from hash list without re-initialization
466 * @n: the element to delete from the hash list.
467 *
468 * Note: list_unhashed() on entry does not return true after this,
469 * the entry is in an undefined state. It is useful for RCU based
470 * lockfree traversal.
471 *
472 * In particular, it means that we can not poison the forward
473 * pointers that may still be used for walking the hash list.
474 *
475 * The caller must take whatever precautions are necessary
476 * (such as holding appropriate locks) to avoid racing
477 * with another list-mutation primitive, such as hlist_add_head_rcu()
478 * or hlist_del_rcu(), running on this same list.
479 * However, it is perfectly legal to run concurrently with
480 * the _rcu list-traversal primitives, such as
481 * hlist_for_each_entry().
482 */
483static inline void hlist_del_rcu(struct hlist_node *n)
484{
485 __hlist_del(n);
c54a2744 486 WRITE_ONCE(n->pprev, LIST_POISON2);
82524746
FBH
487}
488
489/**
490 * hlist_replace_rcu - replace old entry by new one
491 * @old : the element to be replaced
492 * @new : the new element to insert
493 *
494 * The @old entry will be replaced with the @new entry atomically.
495 */
496static inline void hlist_replace_rcu(struct hlist_node *old,
497 struct hlist_node *new)
498{
499 struct hlist_node *next = old->next;
500
501 new->next = next;
c54a2744 502 WRITE_ONCE(new->pprev, old->pprev);
67bdbffd 503 rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new);
82524746 504 if (next)
c54a2744
ED
505 WRITE_ONCE(new->next->pprev, &new->next);
506 WRITE_ONCE(old->pprev, LIST_POISON2);
82524746
FBH
507}
508
35fc0e3b
EB
509/**
510 * hlists_swap_heads_rcu - swap the lists the hlist heads point to
511 * @left: The hlist head on the left
512 * @right: The hlist head on the right
513 *
514 * The lists start out as [@left ][node1 ... ] and
515 [@right ][node2 ... ]
516 * The lists end up as [@left ][node2 ... ]
517 * [@right ][node1 ... ]
518 */
519static inline void hlists_swap_heads_rcu(struct hlist_head *left, struct hlist_head *right)
520{
521 struct hlist_node *node1 = left->first;
522 struct hlist_node *node2 = right->first;
523
524 rcu_assign_pointer(left->first, node2);
525 rcu_assign_pointer(right->first, node1);
526 WRITE_ONCE(node2->pprev, &left->first);
527 WRITE_ONCE(node1->pprev, &right->first);
528}
529
67bdbffd
AB
530/*
531 * return the first or the next element in an RCU protected hlist
532 */
533#define hlist_first_rcu(head) (*((struct hlist_node __rcu **)(&(head)->first)))
534#define hlist_next_rcu(node) (*((struct hlist_node __rcu **)(&(node)->next)))
535#define hlist_pprev_rcu(node) (*((struct hlist_node __rcu **)((node)->pprev)))
536
82524746
FBH
537/**
538 * hlist_add_head_rcu
539 * @n: the element to add to the hash list.
540 * @h: the list to add to.
541 *
542 * Description:
543 * Adds the specified element to the specified hlist,
544 * while permitting racing traversals.
545 *
546 * The caller must take whatever precautions are necessary
547 * (such as holding appropriate locks) to avoid racing
548 * with another list-mutation primitive, such as hlist_add_head_rcu()
549 * or hlist_del_rcu(), running on this same list.
550 * However, it is perfectly legal to run concurrently with
551 * the _rcu list-traversal primitives, such as
552 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
553 * problems on Alpha CPUs. Regardless of the type of CPU, the
554 * list-traversal primitive must be guarded by rcu_read_lock().
555 */
556static inline void hlist_add_head_rcu(struct hlist_node *n,
557 struct hlist_head *h)
558{
559 struct hlist_node *first = h->first;
10aa9d2c 560
82524746 561 n->next = first;
c54a2744 562 WRITE_ONCE(n->pprev, &h->first);
67bdbffd 563 rcu_assign_pointer(hlist_first_rcu(h), n);
82524746 564 if (first)
c54a2744 565 WRITE_ONCE(first->pprev, &n->next);
82524746
FBH
566}
567
1602f49b
DM
568/**
569 * hlist_add_tail_rcu
570 * @n: the element to add to the hash list.
571 * @h: the list to add to.
572 *
573 * Description:
574 * Adds the specified element to the specified hlist,
575 * while permitting racing traversals.
576 *
577 * The caller must take whatever precautions are necessary
578 * (such as holding appropriate locks) to avoid racing
579 * with another list-mutation primitive, such as hlist_add_head_rcu()
580 * or hlist_del_rcu(), running on this same list.
581 * However, it is perfectly legal to run concurrently with
582 * the _rcu list-traversal primitives, such as
583 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
584 * problems on Alpha CPUs. Regardless of the type of CPU, the
585 * list-traversal primitive must be guarded by rcu_read_lock().
586 */
587static inline void hlist_add_tail_rcu(struct hlist_node *n,
588 struct hlist_head *h)
589{
590 struct hlist_node *i, *last = NULL;
591
48ac3466
MT
592 /* Note: write side code, so rcu accessors are not needed. */
593 for (i = h->first; i; i = i->next)
1602f49b
DM
594 last = i;
595
596 if (last) {
597 n->next = last->next;
c54a2744 598 WRITE_ONCE(n->pprev, &last->next);
1602f49b
DM
599 rcu_assign_pointer(hlist_next_rcu(last), n);
600 } else {
601 hlist_add_head_rcu(n, h);
602 }
603}
604
82524746
FBH
605/**
606 * hlist_add_before_rcu
607 * @n: the new element to add to the hash list.
608 * @next: the existing element to add the new element before.
609 *
610 * Description:
611 * Adds the specified element to the specified hlist
612 * before the specified node while permitting racing traversals.
613 *
614 * The caller must take whatever precautions are necessary
615 * (such as holding appropriate locks) to avoid racing
616 * with another list-mutation primitive, such as hlist_add_head_rcu()
617 * or hlist_del_rcu(), running on this same list.
618 * However, it is perfectly legal to run concurrently with
619 * the _rcu list-traversal primitives, such as
620 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
621 * problems on Alpha CPUs.
622 */
623static inline void hlist_add_before_rcu(struct hlist_node *n,
624 struct hlist_node *next)
625{
c54a2744 626 WRITE_ONCE(n->pprev, next->pprev);
82524746 627 n->next = next;
67bdbffd 628 rcu_assign_pointer(hlist_pprev_rcu(n), n);
c54a2744 629 WRITE_ONCE(next->pprev, &n->next);
82524746
FBH
630}
631
632/**
1d023284 633 * hlist_add_behind_rcu
82524746 634 * @n: the new element to add to the hash list.
1d023284 635 * @prev: the existing element to add the new element after.
82524746
FBH
636 *
637 * Description:
638 * Adds the specified element to the specified hlist
639 * after the specified node while permitting racing traversals.
640 *
641 * The caller must take whatever precautions are necessary
642 * (such as holding appropriate locks) to avoid racing
643 * with another list-mutation primitive, such as hlist_add_head_rcu()
644 * or hlist_del_rcu(), running on this same list.
645 * However, it is perfectly legal to run concurrently with
646 * the _rcu list-traversal primitives, such as
647 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
648 * problems on Alpha CPUs.
649 */
1d023284
KH
650static inline void hlist_add_behind_rcu(struct hlist_node *n,
651 struct hlist_node *prev)
82524746
FBH
652{
653 n->next = prev->next;
c54a2744 654 WRITE_ONCE(n->pprev, &prev->next);
67bdbffd 655 rcu_assign_pointer(hlist_next_rcu(prev), n);
82524746 656 if (n->next)
c54a2744 657 WRITE_ONCE(n->next->pprev, &n->next);
82524746
FBH
658}
659
67bdbffd
AB
660#define __hlist_for_each_rcu(pos, head) \
661 for (pos = rcu_dereference(hlist_first_rcu(head)); \
75d65a42 662 pos; \
67bdbffd 663 pos = rcu_dereference(hlist_next_rcu(pos)))
1cc52327 664
82524746
FBH
665/**
666 * hlist_for_each_entry_rcu - iterate over rcu list of given type
b67bfe0d 667 * @pos: the type * to use as a loop cursor.
82524746
FBH
668 * @head: the head for your list.
669 * @member: the name of the hlist_node within the struct.
ddc46593 670 * @cond: optional lockdep expression if called from non-RCU protection.
82524746
FBH
671 *
672 * This list-traversal primitive may safely run concurrently with
673 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
674 * as long as the traversal is guarded by rcu_read_lock().
675 */
28875945
JFG
676#define hlist_for_each_entry_rcu(pos, head, member, cond...) \
677 for (__list_check_rcu(dummy, ## cond, 0), \
678 pos = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),\
b67bfe0d
SL
679 typeof(*(pos)), member); \
680 pos; \
681 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(\
682 &(pos)->member)), typeof(*(pos)), member))
82524746 683
12bcbe66
SR
684/**
685 * hlist_for_each_entry_rcu_notrace - iterate over rcu list of given type (for tracing)
686 * @pos: the type * to use as a loop cursor.
687 * @head: the head for your list.
688 * @member: the name of the hlist_node within the struct.
689 *
690 * This list-traversal primitive may safely run concurrently with
691 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
692 * as long as the traversal is guarded by rcu_read_lock().
693 *
694 * This is the same as hlist_for_each_entry_rcu() except that it does
695 * not do any RCU debugging or tracing.
696 */
697#define hlist_for_each_entry_rcu_notrace(pos, head, member) \
0a5b99f5 698 for (pos = hlist_entry_safe(rcu_dereference_raw_check(hlist_first_rcu(head)),\
12bcbe66
SR
699 typeof(*(pos)), member); \
700 pos; \
0a5b99f5 701 pos = hlist_entry_safe(rcu_dereference_raw_check(hlist_next_rcu(\
12bcbe66
SR
702 &(pos)->member)), typeof(*(pos)), member))
703
4f70ecca
ED
704/**
705 * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
b67bfe0d 706 * @pos: the type * to use as a loop cursor.
4f70ecca
ED
707 * @head: the head for your list.
708 * @member: the name of the hlist_node within the struct.
709 *
710 * This list-traversal primitive may safely run concurrently with
711 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
712 * as long as the traversal is guarded by rcu_read_lock().
713 */
b67bfe0d
SL
714#define hlist_for_each_entry_rcu_bh(pos, head, member) \
715 for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_first_rcu(head)),\
716 typeof(*(pos)), member); \
717 pos; \
718 pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu(\
719 &(pos)->member)), typeof(*(pos)), member))
4f70ecca 720
5c578aed 721/**
722 * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point
b67bfe0d 723 * @pos: the type * to use as a loop cursor.
5c578aed 724 * @member: the name of the hlist_node within the struct.
725 */
b67bfe0d 726#define hlist_for_each_entry_continue_rcu(pos, member) \
f520c98e
YX
727 for (pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
728 &(pos)->member)), typeof(*(pos)), member); \
b67bfe0d 729 pos; \
f520c98e
YX
730 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
731 &(pos)->member)), typeof(*(pos)), member))
5c578aed 732
4f70ecca
ED
733/**
734 * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point
b67bfe0d 735 * @pos: the type * to use as a loop cursor.
4f70ecca
ED
736 * @member: the name of the hlist_node within the struct.
737 */
b67bfe0d 738#define hlist_for_each_entry_continue_rcu_bh(pos, member) \
f520c98e
YX
739 for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu( \
740 &(pos)->member)), typeof(*(pos)), member); \
b67bfe0d 741 pos; \
f520c98e
YX
742 pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu( \
743 &(pos)->member)), typeof(*(pos)), member))
4f70ecca 744
97ede29e
YX
745/**
746 * hlist_for_each_entry_from_rcu - iterate over a hlist continuing from current point
747 * @pos: the type * to use as a loop cursor.
748 * @member: the name of the hlist_node within the struct.
749 */
750#define hlist_for_each_entry_from_rcu(pos, member) \
751 for (; pos; \
f517700c
YX
752 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
753 &(pos)->member)), typeof(*(pos)), member))
5c578aed 754
82524746
FBH
755#endif /* __KERNEL__ */
756#endif