]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/rculist.h
Merge tag 'x86-urgent-2020-08-30' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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();
c93773c1
PM
251 ASSERT_EXCLUSIVE_ACCESS(*first);
252 ASSERT_EXCLUSIVE_ACCESS(*last);
82524746
FBH
253
254 /*
255 * Readers are finished with the source list, so perform splice.
256 * The order is important if the new list is global and accessible
257 * to concurrent RCU readers. Note that RCU readers are not
258 * permitted to traverse the prev pointers without excluding
259 * this function.
260 */
261
7d86dccf
PM
262 last->next = next;
263 rcu_assign_pointer(list_next_rcu(prev), first);
264 first->prev = prev;
265 next->prev = last;
266}
267
268/**
269 * list_splice_init_rcu - splice an RCU-protected list into an existing list,
270 * designed for stacks.
271 * @list: the RCU-protected list to splice
272 * @head: the place in the existing list to splice the first list into
aff5f036 273 * @sync: synchronize_rcu, synchronize_rcu_expedited, ...
7d86dccf
PM
274 */
275static inline void list_splice_init_rcu(struct list_head *list,
276 struct list_head *head,
277 void (*sync)(void))
278{
279 if (!list_empty(list))
280 __list_splice_init_rcu(list, head, head->next, sync);
281}
282
283/**
284 * list_splice_tail_init_rcu - splice an RCU-protected list into an existing
285 * list, designed for queues.
286 * @list: the RCU-protected list to splice
287 * @head: the place in the existing list to splice the first list into
aff5f036 288 * @sync: synchronize_rcu, synchronize_rcu_expedited, ...
7d86dccf
PM
289 */
290static inline void list_splice_tail_init_rcu(struct list_head *list,
291 struct list_head *head,
292 void (*sync)(void))
293{
294 if (!list_empty(list))
295 __list_splice_init_rcu(list, head->prev, head, sync);
82524746
FBH
296}
297
72c6a987
JP
298/**
299 * list_entry_rcu - get the struct for this entry
300 * @ptr: the &struct list_head pointer.
301 * @type: the type of the struct this is embedded in.
3943f42c 302 * @member: the name of the list_head within the struct.
72c6a987
JP
303 *
304 * This primitive may safely run concurrently with the _rcu list-mutation
305 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
306 */
307#define list_entry_rcu(ptr, type, member) \
506458ef 308 container_of(READ_ONCE(ptr), type, member)
72c6a987 309
27fdb35f 310/*
f88022a4
MM
311 * Where are list_empty_rcu() and list_first_entry_rcu()?
312 *
313 * Implementing those functions following their counterparts list_empty() and
314 * list_first_entry() is not advisable because they lead to subtle race
315 * conditions as the following snippet shows:
316 *
317 * if (!list_empty_rcu(mylist)) {
318 * struct foo *bar = list_first_entry_rcu(mylist, struct foo, list_member);
319 * do_something(bar);
320 * }
321 *
322 * The list may not be empty when list_empty_rcu checks it, but it may be when
323 * list_first_entry_rcu rereads the ->next pointer.
324 *
325 * Rereading the ->next pointer is not a problem for list_empty() and
326 * list_first_entry() because they would be protected by a lock that blocks
327 * writers.
328 *
329 * See list_first_or_null_rcu for an alternative.
330 */
331
332/**
333 * list_first_or_null_rcu - get the first element from a list
72c6a987
JP
334 * @ptr: the list head to take the element from.
335 * @type: the type of the struct this is embedded in.
3943f42c 336 * @member: the name of the list_head within the struct.
72c6a987 337 *
f88022a4 338 * Note that if the list is empty, it returns NULL.
72c6a987
JP
339 *
340 * This primitive may safely run concurrently with the _rcu list-mutation
341 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
342 */
f88022a4 343#define list_first_or_null_rcu(ptr, type, member) \
0adab9b9
JP
344({ \
345 struct list_head *__ptr = (ptr); \
7d0ae808 346 struct list_head *__next = READ_ONCE(__ptr->next); \
0adab9b9
JP
347 likely(__ptr != __next) ? list_entry_rcu(__next, type, member) : NULL; \
348})
72c6a987 349
ff3c44e6
TH
350/**
351 * list_next_or_null_rcu - get the first element from a list
352 * @head: the head for the list.
353 * @ptr: the list head to take the next element from.
354 * @type: the type of the struct this is embedded in.
355 * @member: the name of the list_head within the struct.
356 *
357 * Note that if the ptr is at the end of the list, NULL is returned.
358 *
359 * This primitive may safely run concurrently with the _rcu list-mutation
360 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
361 */
362#define list_next_or_null_rcu(head, ptr, type, member) \
363({ \
364 struct list_head *__head = (head); \
365 struct list_head *__ptr = (ptr); \
366 struct list_head *__next = READ_ONCE(__ptr->next); \
367 likely(__next != __head) ? list_entry_rcu(__next, type, \
368 member) : NULL; \
369})
370
82524746
FBH
371/**
372 * list_for_each_entry_rcu - iterate over rcu list of given type
373 * @pos: the type * to use as a loop cursor.
374 * @head: the head for your list.
3943f42c 375 * @member: the name of the list_head within the struct.
ddc46593 376 * @cond: optional lockdep expression if called from non-RCU protection.
82524746
FBH
377 *
378 * This list-traversal primitive may safely run concurrently with
379 * the _rcu list-mutation primitives such as list_add_rcu()
380 * as long as the traversal is guarded by rcu_read_lock().
381 */
28875945
JFG
382#define list_for_each_entry_rcu(pos, head, member, cond...) \
383 for (__list_check_rcu(dummy, ## cond, 0), \
384 pos = list_entry_rcu((head)->next, typeof(*pos), member); \
385 &pos->member != (head); \
72c6a987 386 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
82524746 387
69b90729
AK
388/**
389 * list_entry_lockless - get the struct for this entry
390 * @ptr: the &struct list_head pointer.
391 * @type: the type of the struct this is embedded in.
392 * @member: the name of the list_head within the struct.
393 *
aff5f036
PM
394 * This primitive may safely run concurrently with the _rcu
395 * list-mutation primitives such as list_add_rcu(), but requires some
396 * implicit RCU read-side guarding. One example is running within a special
397 * exception-time environment where preemption is disabled and where lockdep
398 * cannot be invoked. Another example is when items are added to the list,
399 * but never deleted.
69b90729
AK
400 */
401#define list_entry_lockless(ptr, type, member) \
506458ef 402 container_of((typeof(ptr))READ_ONCE(ptr), type, member)
69b90729
AK
403
404/**
405 * list_for_each_entry_lockless - iterate over rcu list of given type
406 * @pos: the type * to use as a loop cursor.
407 * @head: the head for your list.
408 * @member: the name of the list_struct within the struct.
409 *
aff5f036
PM
410 * This primitive may safely run concurrently with the _rcu
411 * list-mutation primitives such as list_add_rcu(), but requires some
412 * implicit RCU read-side guarding. One example is running within a special
413 * exception-time environment where preemption is disabled and where lockdep
414 * cannot be invoked. Another example is when items are added to the list,
415 * but never deleted.
69b90729
AK
416 */
417#define list_for_each_entry_lockless(pos, head, member) \
418 for (pos = list_entry_lockless((head)->next, typeof(*pos), member); \
419 &pos->member != (head); \
420 pos = list_entry_lockless(pos->member.next, typeof(*pos), member))
421
254245d2 422/**
423 * list_for_each_entry_continue_rcu - continue iteration over list of given type
424 * @pos: the type * to use as a loop cursor.
425 * @head: the head for your list.
3943f42c 426 * @member: the name of the list_head within the struct.
254245d2 427 *
428 * Continue to iterate over list of given type, continuing after
b7b6f94c
N
429 * the current position which must have been in the list when the RCU read
430 * lock was taken.
431 * This would typically require either that you obtained the node from a
432 * previous walk of the list in the same RCU read-side critical section, or
433 * that you held some sort of non-RCU reference (such as a reference count)
434 * to keep the node alive *and* in the list.
435 *
436 * This iterator is similar to list_for_each_entry_from_rcu() except
437 * this starts after the given position and that one starts at the given
438 * position.
254245d2 439 */
440#define list_for_each_entry_continue_rcu(pos, head, member) \
441 for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \
e66eed65 442 &pos->member != (head); \
254245d2 443 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
444
ead9ad72
N
445/**
446 * list_for_each_entry_from_rcu - iterate over a list from current point
447 * @pos: the type * to use as a loop cursor.
448 * @head: the head for your list.
449 * @member: the name of the list_node within the struct.
450 *
451 * Iterate over the tail of a list starting from a given position,
452 * which must have been in the list when the RCU read lock was taken.
b7b6f94c
N
453 * This would typically require either that you obtained the node from a
454 * previous walk of the list in the same RCU read-side critical section, or
455 * that you held some sort of non-RCU reference (such as a reference count)
456 * to keep the node alive *and* in the list.
457 *
458 * This iterator is similar to list_for_each_entry_continue_rcu() except
459 * this starts from the given position and that one starts from the position
460 * after the given position.
ead9ad72
N
461 */
462#define list_for_each_entry_from_rcu(pos, head, member) \
463 for (; &(pos)->member != (head); \
464 pos = list_entry_rcu(pos->member.next, typeof(*(pos)), member))
465
82524746
FBH
466/**
467 * hlist_del_rcu - deletes entry from hash list without re-initialization
468 * @n: the element to delete from the hash list.
469 *
470 * Note: list_unhashed() on entry does not return true after this,
471 * the entry is in an undefined state. It is useful for RCU based
472 * lockfree traversal.
473 *
474 * In particular, it means that we can not poison the forward
475 * pointers that may still be used for walking the hash list.
476 *
477 * The caller must take whatever precautions are necessary
478 * (such as holding appropriate locks) to avoid racing
479 * with another list-mutation primitive, such as hlist_add_head_rcu()
480 * or hlist_del_rcu(), running on this same list.
481 * However, it is perfectly legal to run concurrently with
482 * the _rcu list-traversal primitives, such as
483 * hlist_for_each_entry().
484 */
485static inline void hlist_del_rcu(struct hlist_node *n)
486{
487 __hlist_del(n);
c54a2744 488 WRITE_ONCE(n->pprev, LIST_POISON2);
82524746
FBH
489}
490
491/**
492 * hlist_replace_rcu - replace old entry by new one
493 * @old : the element to be replaced
494 * @new : the new element to insert
495 *
496 * The @old entry will be replaced with the @new entry atomically.
497 */
498static inline void hlist_replace_rcu(struct hlist_node *old,
499 struct hlist_node *new)
500{
501 struct hlist_node *next = old->next;
502
503 new->next = next;
c54a2744 504 WRITE_ONCE(new->pprev, old->pprev);
67bdbffd 505 rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new);
82524746 506 if (next)
c54a2744
ED
507 WRITE_ONCE(new->next->pprev, &new->next);
508 WRITE_ONCE(old->pprev, LIST_POISON2);
82524746
FBH
509}
510
35fc0e3b
EB
511/**
512 * hlists_swap_heads_rcu - swap the lists the hlist heads point to
513 * @left: The hlist head on the left
514 * @right: The hlist head on the right
515 *
516 * The lists start out as [@left ][node1 ... ] and
24692fa2 517 * [@right ][node2 ... ]
35fc0e3b
EB
518 * The lists end up as [@left ][node2 ... ]
519 * [@right ][node1 ... ]
520 */
521static inline void hlists_swap_heads_rcu(struct hlist_head *left, struct hlist_head *right)
522{
523 struct hlist_node *node1 = left->first;
524 struct hlist_node *node2 = right->first;
525
526 rcu_assign_pointer(left->first, node2);
527 rcu_assign_pointer(right->first, node1);
528 WRITE_ONCE(node2->pprev, &left->first);
529 WRITE_ONCE(node1->pprev, &right->first);
530}
531
67bdbffd
AB
532/*
533 * return the first or the next element in an RCU protected hlist
534 */
535#define hlist_first_rcu(head) (*((struct hlist_node __rcu **)(&(head)->first)))
536#define hlist_next_rcu(node) (*((struct hlist_node __rcu **)(&(node)->next)))
537#define hlist_pprev_rcu(node) (*((struct hlist_node __rcu **)((node)->pprev)))
538
82524746
FBH
539/**
540 * hlist_add_head_rcu
541 * @n: the element to add to the hash list.
542 * @h: the list to add to.
543 *
544 * Description:
545 * Adds the specified element to the specified hlist,
546 * while permitting racing traversals.
547 *
548 * The caller must take whatever precautions are necessary
549 * (such as holding appropriate locks) to avoid racing
550 * with another list-mutation primitive, such as hlist_add_head_rcu()
551 * or hlist_del_rcu(), running on this same list.
552 * However, it is perfectly legal to run concurrently with
553 * the _rcu list-traversal primitives, such as
554 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
555 * problems on Alpha CPUs. Regardless of the type of CPU, the
556 * list-traversal primitive must be guarded by rcu_read_lock().
557 */
558static inline void hlist_add_head_rcu(struct hlist_node *n,
559 struct hlist_head *h)
560{
561 struct hlist_node *first = h->first;
10aa9d2c 562
82524746 563 n->next = first;
c54a2744 564 WRITE_ONCE(n->pprev, &h->first);
67bdbffd 565 rcu_assign_pointer(hlist_first_rcu(h), n);
82524746 566 if (first)
c54a2744 567 WRITE_ONCE(first->pprev, &n->next);
82524746
FBH
568}
569
1602f49b
DM
570/**
571 * hlist_add_tail_rcu
572 * @n: the element to add to the hash list.
573 * @h: the list to add to.
574 *
575 * Description:
576 * Adds the specified element to the specified hlist,
577 * while permitting racing traversals.
578 *
579 * The caller must take whatever precautions are necessary
580 * (such as holding appropriate locks) to avoid racing
581 * with another list-mutation primitive, such as hlist_add_head_rcu()
582 * or hlist_del_rcu(), running on this same list.
583 * However, it is perfectly legal to run concurrently with
584 * the _rcu list-traversal primitives, such as
585 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
586 * problems on Alpha CPUs. Regardless of the type of CPU, the
587 * list-traversal primitive must be guarded by rcu_read_lock().
588 */
589static inline void hlist_add_tail_rcu(struct hlist_node *n,
590 struct hlist_head *h)
591{
592 struct hlist_node *i, *last = NULL;
593
48ac3466
MT
594 /* Note: write side code, so rcu accessors are not needed. */
595 for (i = h->first; i; i = i->next)
1602f49b
DM
596 last = i;
597
598 if (last) {
599 n->next = last->next;
c54a2744 600 WRITE_ONCE(n->pprev, &last->next);
1602f49b
DM
601 rcu_assign_pointer(hlist_next_rcu(last), n);
602 } else {
603 hlist_add_head_rcu(n, h);
604 }
605}
606
82524746
FBH
607/**
608 * hlist_add_before_rcu
609 * @n: the new element to add to the hash list.
610 * @next: the existing element to add the new element before.
611 *
612 * Description:
613 * Adds the specified element to the specified hlist
614 * before the specified node while permitting racing traversals.
615 *
616 * The caller must take whatever precautions are necessary
617 * (such as holding appropriate locks) to avoid racing
618 * with another list-mutation primitive, such as hlist_add_head_rcu()
619 * or hlist_del_rcu(), running on this same list.
620 * However, it is perfectly legal to run concurrently with
621 * the _rcu list-traversal primitives, such as
622 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
623 * problems on Alpha CPUs.
624 */
625static inline void hlist_add_before_rcu(struct hlist_node *n,
626 struct hlist_node *next)
627{
c54a2744 628 WRITE_ONCE(n->pprev, next->pprev);
82524746 629 n->next = next;
67bdbffd 630 rcu_assign_pointer(hlist_pprev_rcu(n), n);
c54a2744 631 WRITE_ONCE(next->pprev, &n->next);
82524746
FBH
632}
633
634/**
1d023284 635 * hlist_add_behind_rcu
82524746 636 * @n: the new element to add to the hash list.
1d023284 637 * @prev: the existing element to add the new element after.
82524746
FBH
638 *
639 * Description:
640 * Adds the specified element to the specified hlist
641 * after the specified node while permitting racing traversals.
642 *
643 * The caller must take whatever precautions are necessary
644 * (such as holding appropriate locks) to avoid racing
645 * with another list-mutation primitive, such as hlist_add_head_rcu()
646 * or hlist_del_rcu(), running on this same list.
647 * However, it is perfectly legal to run concurrently with
648 * the _rcu list-traversal primitives, such as
649 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
650 * problems on Alpha CPUs.
651 */
1d023284
KH
652static inline void hlist_add_behind_rcu(struct hlist_node *n,
653 struct hlist_node *prev)
82524746
FBH
654{
655 n->next = prev->next;
c54a2744 656 WRITE_ONCE(n->pprev, &prev->next);
67bdbffd 657 rcu_assign_pointer(hlist_next_rcu(prev), n);
82524746 658 if (n->next)
c54a2744 659 WRITE_ONCE(n->next->pprev, &n->next);
82524746
FBH
660}
661
67bdbffd
AB
662#define __hlist_for_each_rcu(pos, head) \
663 for (pos = rcu_dereference(hlist_first_rcu(head)); \
75d65a42 664 pos; \
67bdbffd 665 pos = rcu_dereference(hlist_next_rcu(pos)))
1cc52327 666
82524746
FBH
667/**
668 * hlist_for_each_entry_rcu - iterate over rcu list of given type
b67bfe0d 669 * @pos: the type * to use as a loop cursor.
82524746
FBH
670 * @head: the head for your list.
671 * @member: the name of the hlist_node within the struct.
ddc46593 672 * @cond: optional lockdep expression if called from non-RCU protection.
82524746
FBH
673 *
674 * This list-traversal primitive may safely run concurrently with
675 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
676 * as long as the traversal is guarded by rcu_read_lock().
677 */
28875945
JFG
678#define hlist_for_each_entry_rcu(pos, head, member, cond...) \
679 for (__list_check_rcu(dummy, ## cond, 0), \
680 pos = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),\
b67bfe0d
SL
681 typeof(*(pos)), member); \
682 pos; \
683 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(\
684 &(pos)->member)), typeof(*(pos)), member))
82524746 685
12bcbe66
SR
686/**
687 * hlist_for_each_entry_rcu_notrace - iterate over rcu list of given type (for tracing)
688 * @pos: the type * to use as a loop cursor.
689 * @head: the head for your list.
690 * @member: the name of the hlist_node within the struct.
691 *
692 * This list-traversal primitive may safely run concurrently with
693 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
694 * as long as the traversal is guarded by rcu_read_lock().
695 *
696 * This is the same as hlist_for_each_entry_rcu() except that it does
697 * not do any RCU debugging or tracing.
698 */
699#define hlist_for_each_entry_rcu_notrace(pos, head, member) \
0a5b99f5 700 for (pos = hlist_entry_safe(rcu_dereference_raw_check(hlist_first_rcu(head)),\
12bcbe66
SR
701 typeof(*(pos)), member); \
702 pos; \
0a5b99f5 703 pos = hlist_entry_safe(rcu_dereference_raw_check(hlist_next_rcu(\
12bcbe66
SR
704 &(pos)->member)), typeof(*(pos)), member))
705
4f70ecca
ED
706/**
707 * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
b67bfe0d 708 * @pos: the type * to use as a loop cursor.
4f70ecca
ED
709 * @head: the head for your list.
710 * @member: the name of the hlist_node within the struct.
711 *
712 * This list-traversal primitive may safely run concurrently with
713 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
714 * as long as the traversal is guarded by rcu_read_lock().
715 */
b67bfe0d
SL
716#define hlist_for_each_entry_rcu_bh(pos, head, member) \
717 for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_first_rcu(head)),\
718 typeof(*(pos)), member); \
719 pos; \
720 pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu(\
721 &(pos)->member)), typeof(*(pos)), member))
4f70ecca 722
5c578aed 723/**
724 * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point
b67bfe0d 725 * @pos: the type * to use as a loop cursor.
5c578aed 726 * @member: the name of the hlist_node within the struct.
727 */
b67bfe0d 728#define hlist_for_each_entry_continue_rcu(pos, member) \
f520c98e
YX
729 for (pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
730 &(pos)->member)), typeof(*(pos)), member); \
b67bfe0d 731 pos; \
f520c98e
YX
732 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
733 &(pos)->member)), typeof(*(pos)), member))
5c578aed 734
4f70ecca
ED
735/**
736 * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point
b67bfe0d 737 * @pos: the type * to use as a loop cursor.
4f70ecca
ED
738 * @member: the name of the hlist_node within the struct.
739 */
b67bfe0d 740#define hlist_for_each_entry_continue_rcu_bh(pos, member) \
f520c98e
YX
741 for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu( \
742 &(pos)->member)), typeof(*(pos)), member); \
b67bfe0d 743 pos; \
f520c98e
YX
744 pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu( \
745 &(pos)->member)), typeof(*(pos)), member))
4f70ecca 746
97ede29e
YX
747/**
748 * hlist_for_each_entry_from_rcu - iterate over a hlist continuing from current point
749 * @pos: the type * to use as a loop cursor.
750 * @member: the name of the hlist_node within the struct.
751 */
752#define hlist_for_each_entry_from_rcu(pos, member) \
753 for (; pos; \
f517700c
YX
754 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
755 &(pos)->member)), typeof(*(pos)), member))
5c578aed 756
82524746
FBH
757#endif /* __KERNEL__ */
758#endif