blob: f8633d37e35815454eb66df55016e1f0ab2d260d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Franck Bui-Huu82524742008-05-12 21:21:05 +02002#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>
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +020011#include <linux/rcupdate.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020012
13/*
Paul E. McKenney65e6bf42010-08-19 21:43:09 -070014 * 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
22/*
Paul E. McKenney2a855b62013-08-23 09:40:42 -070023 * 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{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -080033 WRITE_ONCE(list->next, list);
34 WRITE_ONCE(list->prev, list);
Paul E. McKenney2a855b62013-08-23 09:40:42 -070035}
36
37/*
Arnd Bergmann67bdbff2010-02-25 16:55:13 +010038 * 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
Madhuparna Bhowmikafa47fd2019-12-09 13:20:43 +053043/**
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
Arnd Bergmann67bdbff2010-02-25 16:55:13 +010053/*
Joel Fernandes (Google)28875942019-07-16 18:12:22 -040054 * 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); \
Amol Grover4dfd5cd2020-01-18 22:24:18 +053063 RCU_LOCKDEP_WARN(!(cond) && !rcu_read_lock_any_held(), \
Joel Fernandes (Google)28875942019-07-16 18:12:22 -040064 "RCU-list traversed in non-reader section!"); \
Amol Grover4dfd5cd2020-01-18 22:24:18 +053065 })
Madhuparna Bhowmikae2212a2020-07-12 18:40:02 +053066
67#define __list_check_srcu(cond) \
68 ({ \
69 RCU_LOCKDEP_WARN(!(cond), \
70 "RCU-list traversed without holding the required lock!");\
71 })
Joel Fernandes (Google)28875942019-07-16 18:12:22 -040072#else
73#define __list_check_rcu(dummy, cond, extra...) \
74 ({ check_arg_count_one(extra); })
Madhuparna Bhowmikae2212a2020-07-12 18:40:02 +053075
76#define __list_check_srcu(cond) ({ })
Joel Fernandes (Google)28875942019-07-16 18:12:22 -040077#endif
78
79/*
Franck Bui-Huu82524742008-05-12 21:21:05 +020080 * Insert a new entry between two known consecutive entries.
81 *
82 * This is only for internal list manipulation where we know
83 * the prev/next entries already!
84 */
85static inline void __list_add_rcu(struct list_head *new,
86 struct list_head *prev, struct list_head *next)
87{
Kees Cook54acd432016-08-17 14:42:09 -070088 if (!__list_add_valid(new, prev, next))
89 return;
90
Franck Bui-Huu82524742008-05-12 21:21:05 +020091 new->next = next;
92 new->prev = prev;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +010093 rcu_assign_pointer(list_next_rcu(prev), new);
Franck Bui-Huu82524742008-05-12 21:21:05 +020094 next->prev = new;
Franck Bui-Huu82524742008-05-12 21:21:05 +020095}
96
97/**
98 * list_add_rcu - add a new entry to rcu-protected list
99 * @new: new entry to be added
100 * @head: list head to add it after
101 *
102 * Insert a new entry after the specified head.
103 * This is good for implementing stacks.
104 *
105 * The caller must take whatever precautions are necessary
106 * (such as holding appropriate locks) to avoid racing
107 * with another list-mutation primitive, such as list_add_rcu()
108 * or list_del_rcu(), running on this same list.
109 * However, it is perfectly legal to run concurrently with
110 * the _rcu list-traversal primitives, such as
111 * list_for_each_entry_rcu().
112 */
113static inline void list_add_rcu(struct list_head *new, struct list_head *head)
114{
115 __list_add_rcu(new, head, head->next);
116}
117
118/**
119 * list_add_tail_rcu - add a new entry to rcu-protected list
120 * @new: new entry to be added
121 * @head: list head to add it before
122 *
123 * Insert a new entry before the specified head.
124 * This is useful for implementing queues.
125 *
126 * The caller must take whatever precautions are necessary
127 * (such as holding appropriate locks) to avoid racing
128 * with another list-mutation primitive, such as list_add_tail_rcu()
129 * or list_del_rcu(), running on this same list.
130 * However, it is perfectly legal to run concurrently with
131 * the _rcu list-traversal primitives, such as
132 * list_for_each_entry_rcu().
133 */
134static inline void list_add_tail_rcu(struct list_head *new,
135 struct list_head *head)
136{
137 __list_add_rcu(new, head->prev, head);
138}
139
140/**
141 * list_del_rcu - deletes entry from list without re-initialization
142 * @entry: the element to delete from the list.
143 *
144 * Note: list_empty() on entry does not return true after this,
145 * the entry is in an undefined state. It is useful for RCU based
146 * lockfree traversal.
147 *
148 * In particular, it means that we can not poison the forward
149 * pointers that may still be used for walking the list.
150 *
151 * The caller must take whatever precautions are necessary
152 * (such as holding appropriate locks) to avoid racing
153 * with another list-mutation primitive, such as list_del_rcu()
154 * or list_add_rcu(), running on this same list.
155 * However, it is perfectly legal to run concurrently with
156 * the _rcu list-traversal primitives, such as
157 * list_for_each_entry_rcu().
158 *
159 * Note that the caller is not permitted to immediately free
160 * the newly deleted entry. Instead, either synchronize_rcu()
161 * or call_rcu() must be used to defer freeing until an RCU
162 * grace period has elapsed.
163 */
164static inline void list_del_rcu(struct list_head *entry)
165{
Dave Jones559f9ba2012-03-14 22:17:39 -0400166 __list_del_entry(entry);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200167 entry->prev = LIST_POISON2;
168}
169
170/**
Andrea Arcangeli6beeac72008-07-28 15:46:22 -0700171 * hlist_del_init_rcu - deletes entry from hash list with re-initialization
172 * @n: the element to delete from the hash list.
173 *
174 * Note: list_unhashed() on the node return true after this. It is
175 * useful for RCU based read lockfree traversal if the writer side
176 * must know if the list entry is still hashed or already unhashed.
177 *
178 * In particular, it means that we can not poison the forward pointers
179 * that may still be used for walking the hash list and we can only
180 * zero the pprev pointer so list_unhashed() will return true after
181 * this.
182 *
183 * The caller must take whatever precautions are necessary (such as
184 * holding appropriate locks) to avoid racing with another
185 * list-mutation primitive, such as hlist_add_head_rcu() or
186 * hlist_del_rcu(), running on this same list. However, it is
187 * perfectly legal to run concurrently with the _rcu list-traversal
188 * primitives, such as hlist_for_each_entry_rcu().
189 */
190static inline void hlist_del_init_rcu(struct hlist_node *n)
191{
192 if (!hlist_unhashed(n)) {
193 __hlist_del(n);
Eric Dumazetc54a2742019-11-07 11:37:37 -0800194 WRITE_ONCE(n->pprev, NULL);
Andrea Arcangeli6beeac72008-07-28 15:46:22 -0700195 }
196}
197
198/**
Franck Bui-Huu82524742008-05-12 21:21:05 +0200199 * list_replace_rcu - replace old entry by new one
200 * @old : the element to be replaced
201 * @new : the new element to insert
202 *
203 * The @old entry will be replaced with the @new entry atomically.
204 * Note: @old should not be empty.
205 */
206static inline void list_replace_rcu(struct list_head *old,
207 struct list_head *new)
208{
209 new->next = old->next;
210 new->prev = old->prev;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100211 rcu_assign_pointer(list_next_rcu(new->prev), new);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200212 new->next->prev = new;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200213 old->prev = LIST_POISON2;
214}
215
216/**
Petko Manolov7d86dcc2015-10-12 18:23:51 +0300217 * __list_splice_init_rcu - join an RCU-protected list into an existing list.
Franck Bui-Huu82524742008-05-12 21:21:05 +0200218 * @list: the RCU-protected list to splice
Petko Manolov7d86dcc2015-10-12 18:23:51 +0300219 * @prev: points to the last element of the existing list
220 * @next: points to the first element of the existing list
Paul E. McKenneyaff5f032018-07-07 18:12:26 -0700221 * @sync: synchronize_rcu, synchronize_rcu_expedited, ...
Franck Bui-Huu82524742008-05-12 21:21:05 +0200222 *
Petko Manolov7d86dcc2015-10-12 18:23:51 +0300223 * The list pointed to by @prev and @next can be RCU-read traversed
224 * concurrently with this function.
Franck Bui-Huu82524742008-05-12 21:21:05 +0200225 *
226 * Note that this function blocks.
227 *
Petko Manolov7d86dcc2015-10-12 18:23:51 +0300228 * Important note: the caller must take whatever action is necessary to prevent
229 * any other updates to the existing list. In principle, it is possible to
230 * modify the list as soon as sync() begins execution. If this sort of thing
231 * becomes necessary, an alternative version based on call_rcu() could be
232 * created. But only if -really- needed -- there is no shortage of RCU API
233 * members.
Franck Bui-Huu82524742008-05-12 21:21:05 +0200234 */
Petko Manolov7d86dcc2015-10-12 18:23:51 +0300235static inline void __list_splice_init_rcu(struct list_head *list,
236 struct list_head *prev,
237 struct list_head *next,
238 void (*sync)(void))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200239{
240 struct list_head *first = list->next;
241 struct list_head *last = list->prev;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200242
Paul E. McKenney2a855b62013-08-23 09:40:42 -0700243 /*
244 * "first" and "last" tracking list, so initialize it. RCU readers
245 * have access to this list, so we must use INIT_LIST_HEAD_RCU()
246 * instead of INIT_LIST_HEAD().
247 */
Franck Bui-Huu82524742008-05-12 21:21:05 +0200248
Paul E. McKenney2a855b62013-08-23 09:40:42 -0700249 INIT_LIST_HEAD_RCU(list);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200250
251 /*
252 * At this point, the list body still points to the source list.
253 * Wait for any readers to finish using the list before splicing
254 * the list body into the new list. Any new readers will see
255 * an empty list.
256 */
257
258 sync();
Paul E. McKenneyc93773c2020-02-12 13:29:15 -0800259 ASSERT_EXCLUSIVE_ACCESS(*first);
260 ASSERT_EXCLUSIVE_ACCESS(*last);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200261
262 /*
263 * Readers are finished with the source list, so perform splice.
264 * The order is important if the new list is global and accessible
265 * to concurrent RCU readers. Note that RCU readers are not
266 * permitted to traverse the prev pointers without excluding
267 * this function.
268 */
269
Petko Manolov7d86dcc2015-10-12 18:23:51 +0300270 last->next = next;
271 rcu_assign_pointer(list_next_rcu(prev), first);
272 first->prev = prev;
273 next->prev = last;
274}
275
276/**
277 * list_splice_init_rcu - splice an RCU-protected list into an existing list,
278 * designed for stacks.
279 * @list: the RCU-protected list to splice
280 * @head: the place in the existing list to splice the first list into
Paul E. McKenneyaff5f032018-07-07 18:12:26 -0700281 * @sync: synchronize_rcu, synchronize_rcu_expedited, ...
Petko Manolov7d86dcc2015-10-12 18:23:51 +0300282 */
283static inline void list_splice_init_rcu(struct list_head *list,
284 struct list_head *head,
285 void (*sync)(void))
286{
287 if (!list_empty(list))
288 __list_splice_init_rcu(list, head, head->next, sync);
289}
290
291/**
292 * list_splice_tail_init_rcu - splice an RCU-protected list into an existing
293 * list, designed for queues.
294 * @list: the RCU-protected list to splice
295 * @head: the place in the existing list to splice the first list into
Paul E. McKenneyaff5f032018-07-07 18:12:26 -0700296 * @sync: synchronize_rcu, synchronize_rcu_expedited, ...
Petko Manolov7d86dcc2015-10-12 18:23:51 +0300297 */
298static inline void list_splice_tail_init_rcu(struct list_head *list,
299 struct list_head *head,
300 void (*sync)(void))
301{
302 if (!list_empty(list))
303 __list_splice_init_rcu(list, head->prev, head, sync);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200304}
305
Jiri Pirko72c6a982009-04-14 17:33:57 +0200306/**
307 * list_entry_rcu - get the struct for this entry
308 * @ptr: the &struct list_head pointer.
309 * @type: the type of the struct this is embedded in.
Andrey Utkin3943f422014-11-14 05:09:55 +0400310 * @member: the name of the list_head within the struct.
Jiri Pirko72c6a982009-04-14 17:33:57 +0200311 *
312 * This primitive may safely run concurrently with the _rcu list-mutation
313 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
314 */
315#define list_entry_rcu(ptr, type, member) \
Will Deacon506458e2017-10-24 11:22:48 +0100316 container_of(READ_ONCE(ptr), type, member)
Jiri Pirko72c6a982009-04-14 17:33:57 +0200317
Paul E. McKenney27fdb352017-10-19 14:26:21 -0700318/*
Michel Machadof88022a2012-04-10 14:07:40 -0400319 * Where are list_empty_rcu() and list_first_entry_rcu()?
320 *
321 * Implementing those functions following their counterparts list_empty() and
322 * list_first_entry() is not advisable because they lead to subtle race
323 * conditions as the following snippet shows:
324 *
325 * if (!list_empty_rcu(mylist)) {
326 * struct foo *bar = list_first_entry_rcu(mylist, struct foo, list_member);
327 * do_something(bar);
328 * }
329 *
330 * The list may not be empty when list_empty_rcu checks it, but it may be when
331 * list_first_entry_rcu rereads the ->next pointer.
332 *
333 * Rereading the ->next pointer is not a problem for list_empty() and
334 * list_first_entry() because they would be protected by a lock that blocks
335 * writers.
336 *
337 * See list_first_or_null_rcu for an alternative.
338 */
339
340/**
341 * list_first_or_null_rcu - get the first element from a list
Jiri Pirko72c6a982009-04-14 17:33:57 +0200342 * @ptr: the list head to take the element from.
343 * @type: the type of the struct this is embedded in.
Andrey Utkin3943f422014-11-14 05:09:55 +0400344 * @member: the name of the list_head within the struct.
Jiri Pirko72c6a982009-04-14 17:33:57 +0200345 *
Michel Machadof88022a2012-04-10 14:07:40 -0400346 * Note that if the list is empty, it returns NULL.
Jiri Pirko72c6a982009-04-14 17:33:57 +0200347 *
348 * This primitive may safely run concurrently with the _rcu list-mutation
349 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
350 */
Michel Machadof88022a2012-04-10 14:07:40 -0400351#define list_first_or_null_rcu(ptr, type, member) \
Joe Perches0adab9b2013-12-05 16:19:15 -0800352({ \
353 struct list_head *__ptr = (ptr); \
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800354 struct list_head *__next = READ_ONCE(__ptr->next); \
Joe Perches0adab9b2013-12-05 16:19:15 -0800355 likely(__ptr != __next) ? list_entry_rcu(__next, type, member) : NULL; \
356})
Jiri Pirko72c6a982009-04-14 17:33:57 +0200357
Franck Bui-Huu82524742008-05-12 21:21:05 +0200358/**
Tom Herbertff3c44e2016-03-07 14:11:00 -0800359 * list_next_or_null_rcu - get the first element from a list
360 * @head: the head for the list.
361 * @ptr: the list head to take the next element from.
362 * @type: the type of the struct this is embedded in.
363 * @member: the name of the list_head within the struct.
364 *
365 * Note that if the ptr is at the end of the list, NULL is returned.
366 *
367 * This primitive may safely run concurrently with the _rcu list-mutation
368 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
369 */
370#define list_next_or_null_rcu(head, ptr, type, member) \
371({ \
372 struct list_head *__head = (head); \
373 struct list_head *__ptr = (ptr); \
374 struct list_head *__next = READ_ONCE(__ptr->next); \
375 likely(__next != __head) ? list_entry_rcu(__next, type, \
376 member) : NULL; \
377})
378
379/**
Franck Bui-Huu82524742008-05-12 21:21:05 +0200380 * list_for_each_entry_rcu - iterate over rcu list of given type
381 * @pos: the type * to use as a loop cursor.
382 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400383 * @member: the name of the list_head within the struct.
Jonathan Neuschäferddc465932020-03-05 23:22:55 +0100384 * @cond: optional lockdep expression if called from non-RCU protection.
Franck Bui-Huu82524742008-05-12 21:21:05 +0200385 *
386 * This list-traversal primitive may safely run concurrently with
387 * the _rcu list-mutation primitives such as list_add_rcu()
388 * as long as the traversal is guarded by rcu_read_lock().
389 */
Joel Fernandes (Google)28875942019-07-16 18:12:22 -0400390#define list_for_each_entry_rcu(pos, head, member, cond...) \
391 for (__list_check_rcu(dummy, ## cond, 0), \
392 pos = list_entry_rcu((head)->next, typeof(*pos), member); \
393 &pos->member != (head); \
Jiri Pirko72c6a982009-04-14 17:33:57 +0200394 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200395
Franck Bui-Huu82524742008-05-12 21:21:05 +0200396/**
Madhuparna Bhowmikae2212a2020-07-12 18:40:02 +0530397 * list_for_each_entry_srcu - iterate over rcu list of given type
398 * @pos: the type * to use as a loop cursor.
399 * @head: the head for your list.
400 * @member: the name of the list_head within the struct.
401 * @cond: lockdep expression for the lock required to traverse the list.
402 *
403 * This list-traversal primitive may safely run concurrently with
404 * the _rcu list-mutation primitives such as list_add_rcu()
405 * as long as the traversal is guarded by srcu_read_lock().
406 * The lockdep expression srcu_read_lock_held() can be passed as the
407 * cond argument from read side.
408 */
409#define list_for_each_entry_srcu(pos, head, member, cond) \
410 for (__list_check_srcu(cond), \
411 pos = list_entry_rcu((head)->next, typeof(*pos), member); \
412 &pos->member != (head); \
413 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
414
415/**
Alexey Kardashevskiy69b90722015-12-05 18:14:19 -0800416 * list_entry_lockless - get the struct for this entry
417 * @ptr: the &struct list_head pointer.
418 * @type: the type of the struct this is embedded in.
419 * @member: the name of the list_head within the struct.
420 *
Paul E. McKenneyaff5f032018-07-07 18:12:26 -0700421 * This primitive may safely run concurrently with the _rcu
422 * list-mutation primitives such as list_add_rcu(), but requires some
423 * implicit RCU read-side guarding. One example is running within a special
424 * exception-time environment where preemption is disabled and where lockdep
425 * cannot be invoked. Another example is when items are added to the list,
426 * but never deleted.
Alexey Kardashevskiy69b90722015-12-05 18:14:19 -0800427 */
428#define list_entry_lockless(ptr, type, member) \
Will Deacon506458e2017-10-24 11:22:48 +0100429 container_of((typeof(ptr))READ_ONCE(ptr), type, member)
Alexey Kardashevskiy69b90722015-12-05 18:14:19 -0800430
431/**
432 * list_for_each_entry_lockless - iterate over rcu list of given type
433 * @pos: the type * to use as a loop cursor.
434 * @head: the head for your list.
435 * @member: the name of the list_struct within the struct.
436 *
Paul E. McKenneyaff5f032018-07-07 18:12:26 -0700437 * This primitive may safely run concurrently with the _rcu
438 * list-mutation primitives such as list_add_rcu(), but requires some
439 * implicit RCU read-side guarding. One example is running within a special
440 * exception-time environment where preemption is disabled and where lockdep
441 * cannot be invoked. Another example is when items are added to the list,
442 * but never deleted.
Alexey Kardashevskiy69b90722015-12-05 18:14:19 -0800443 */
444#define list_for_each_entry_lockless(pos, head, member) \
445 for (pos = list_entry_lockless((head)->next, typeof(*pos), member); \
446 &pos->member != (head); \
447 pos = list_entry_lockless(pos->member.next, typeof(*pos), member))
448
449/**
stephen hemminger254245d2009-11-10 07:54:47 +0000450 * list_for_each_entry_continue_rcu - continue iteration over list of given type
451 * @pos: the type * to use as a loop cursor.
452 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400453 * @member: the name of the list_head within the struct.
stephen hemminger254245d2009-11-10 07:54:47 +0000454 *
455 * Continue to iterate over list of given type, continuing after
NeilBrownb7b6f942018-06-18 14:22:40 +1000456 * the current position which must have been in the list when the RCU read
457 * lock was taken.
458 * This would typically require either that you obtained the node from a
459 * previous walk of the list in the same RCU read-side critical section, or
460 * that you held some sort of non-RCU reference (such as a reference count)
461 * to keep the node alive *and* in the list.
462 *
463 * This iterator is similar to list_for_each_entry_from_rcu() except
464 * this starts after the given position and that one starts at the given
465 * position.
stephen hemminger254245d2009-11-10 07:54:47 +0000466 */
467#define list_for_each_entry_continue_rcu(pos, head, member) \
468 for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \
Linus Torvaldse66eed62011-05-19 14:15:29 -0700469 &pos->member != (head); \
stephen hemminger254245d2009-11-10 07:54:47 +0000470 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
471
472/**
NeilBrownead9ad72018-04-30 14:31:30 +1000473 * list_for_each_entry_from_rcu - iterate over a list from current point
474 * @pos: the type * to use as a loop cursor.
475 * @head: the head for your list.
476 * @member: the name of the list_node within the struct.
477 *
478 * Iterate over the tail of a list starting from a given position,
479 * which must have been in the list when the RCU read lock was taken.
NeilBrownb7b6f942018-06-18 14:22:40 +1000480 * This would typically require either that you obtained the node from a
481 * previous walk of the list in the same RCU read-side critical section, or
482 * that you held some sort of non-RCU reference (such as a reference count)
483 * to keep the node alive *and* in the list.
484 *
485 * This iterator is similar to list_for_each_entry_continue_rcu() except
486 * this starts from the given position and that one starts from the position
487 * after the given position.
NeilBrownead9ad72018-04-30 14:31:30 +1000488 */
489#define list_for_each_entry_from_rcu(pos, head, member) \
490 for (; &(pos)->member != (head); \
491 pos = list_entry_rcu(pos->member.next, typeof(*(pos)), member))
492
493/**
Franck Bui-Huu82524742008-05-12 21:21:05 +0200494 * hlist_del_rcu - deletes entry from hash list without re-initialization
495 * @n: the element to delete from the hash list.
496 *
497 * Note: list_unhashed() on entry does not return true after this,
498 * the entry is in an undefined state. It is useful for RCU based
499 * lockfree traversal.
500 *
501 * In particular, it means that we can not poison the forward
502 * pointers that may still be used for walking the hash list.
503 *
504 * The caller must take whatever precautions are necessary
505 * (such as holding appropriate locks) to avoid racing
506 * with another list-mutation primitive, such as hlist_add_head_rcu()
507 * or hlist_del_rcu(), running on this same list.
508 * However, it is perfectly legal to run concurrently with
509 * the _rcu list-traversal primitives, such as
510 * hlist_for_each_entry().
511 */
512static inline void hlist_del_rcu(struct hlist_node *n)
513{
514 __hlist_del(n);
Eric Dumazetc54a2742019-11-07 11:37:37 -0800515 WRITE_ONCE(n->pprev, LIST_POISON2);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200516}
517
518/**
519 * hlist_replace_rcu - replace old entry by new one
520 * @old : the element to be replaced
521 * @new : the new element to insert
522 *
523 * The @old entry will be replaced with the @new entry atomically.
524 */
525static inline void hlist_replace_rcu(struct hlist_node *old,
526 struct hlist_node *new)
527{
528 struct hlist_node *next = old->next;
529
530 new->next = next;
Eric Dumazetc54a2742019-11-07 11:37:37 -0800531 WRITE_ONCE(new->pprev, old->pprev);
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100532 rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200533 if (next)
Eric Dumazetc54a2742019-11-07 11:37:37 -0800534 WRITE_ONCE(new->next->pprev, &new->next);
535 WRITE_ONCE(old->pprev, LIST_POISON2);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200536}
537
Eric W. Biederman35fc0e32020-04-24 11:19:10 -0500538/**
539 * hlists_swap_heads_rcu - swap the lists the hlist heads point to
540 * @left: The hlist head on the left
541 * @right: The hlist head on the right
542 *
543 * The lists start out as [@left ][node1 ... ] and
Mauro Carvalho Chehab24692fa2020-06-15 08:46:49 +0200544 * [@right ][node2 ... ]
Eric W. Biederman35fc0e32020-04-24 11:19:10 -0500545 * The lists end up as [@left ][node2 ... ]
546 * [@right ][node1 ... ]
547 */
548static inline void hlists_swap_heads_rcu(struct hlist_head *left, struct hlist_head *right)
549{
550 struct hlist_node *node1 = left->first;
551 struct hlist_node *node2 = right->first;
552
553 rcu_assign_pointer(left->first, node2);
554 rcu_assign_pointer(right->first, node1);
555 WRITE_ONCE(node2->pprev, &left->first);
556 WRITE_ONCE(node1->pprev, &right->first);
557}
558
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100559/*
560 * return the first or the next element in an RCU protected hlist
561 */
562#define hlist_first_rcu(head) (*((struct hlist_node __rcu **)(&(head)->first)))
563#define hlist_next_rcu(node) (*((struct hlist_node __rcu **)(&(node)->next)))
564#define hlist_pprev_rcu(node) (*((struct hlist_node __rcu **)((node)->pprev)))
565
Franck Bui-Huu82524742008-05-12 21:21:05 +0200566/**
567 * hlist_add_head_rcu
568 * @n: the element to add to the hash list.
569 * @h: the list to add to.
570 *
571 * Description:
572 * Adds the specified element to the specified hlist,
573 * while permitting racing traversals.
574 *
575 * The caller must take whatever precautions are necessary
576 * (such as holding appropriate locks) to avoid racing
577 * with another list-mutation primitive, such as hlist_add_head_rcu()
578 * or hlist_del_rcu(), running on this same list.
579 * However, it is perfectly legal to run concurrently with
580 * the _rcu list-traversal primitives, such as
581 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
582 * problems on Alpha CPUs. Regardless of the type of CPU, the
583 * list-traversal primitive must be guarded by rcu_read_lock().
584 */
585static inline void hlist_add_head_rcu(struct hlist_node *n,
586 struct hlist_head *h)
587{
588 struct hlist_node *first = h->first;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200589
Franck Bui-Huu82524742008-05-12 21:21:05 +0200590 n->next = first;
Eric Dumazetc54a2742019-11-07 11:37:37 -0800591 WRITE_ONCE(n->pprev, &h->first);
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100592 rcu_assign_pointer(hlist_first_rcu(h), n);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200593 if (first)
Eric Dumazetc54a2742019-11-07 11:37:37 -0800594 WRITE_ONCE(first->pprev, &n->next);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200595}
596
597/**
David S. Miller1602f492016-04-23 18:26:24 -0400598 * hlist_add_tail_rcu
599 * @n: the element to add to the hash list.
600 * @h: the list to add to.
601 *
602 * Description:
603 * Adds the specified element to the specified hlist,
604 * while permitting racing traversals.
605 *
606 * The caller must take whatever precautions are necessary
607 * (such as holding appropriate locks) to avoid racing
608 * with another list-mutation primitive, such as hlist_add_head_rcu()
609 * or hlist_del_rcu(), running on this same list.
610 * However, it is perfectly legal to run concurrently with
611 * the _rcu list-traversal primitives, such as
612 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
613 * problems on Alpha CPUs. Regardless of the type of CPU, the
614 * list-traversal primitive must be guarded by rcu_read_lock().
615 */
616static inline void hlist_add_tail_rcu(struct hlist_node *n,
617 struct hlist_head *h)
618{
619 struct hlist_node *i, *last = NULL;
620
Michael S. Tsirkin48ac3462017-02-27 21:14:19 +0200621 /* Note: write side code, so rcu accessors are not needed. */
622 for (i = h->first; i; i = i->next)
David S. Miller1602f492016-04-23 18:26:24 -0400623 last = i;
624
625 if (last) {
626 n->next = last->next;
Eric Dumazetc54a2742019-11-07 11:37:37 -0800627 WRITE_ONCE(n->pprev, &last->next);
David S. Miller1602f492016-04-23 18:26:24 -0400628 rcu_assign_pointer(hlist_next_rcu(last), n);
629 } else {
630 hlist_add_head_rcu(n, h);
631 }
632}
633
634/**
Franck Bui-Huu82524742008-05-12 21:21:05 +0200635 * hlist_add_before_rcu
636 * @n: the new element to add to the hash list.
637 * @next: the existing element to add the new element before.
638 *
639 * Description:
640 * Adds the specified element to the specified hlist
641 * before 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 */
652static inline void hlist_add_before_rcu(struct hlist_node *n,
653 struct hlist_node *next)
654{
Eric Dumazetc54a2742019-11-07 11:37:37 -0800655 WRITE_ONCE(n->pprev, next->pprev);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200656 n->next = next;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100657 rcu_assign_pointer(hlist_pprev_rcu(n), n);
Eric Dumazetc54a2742019-11-07 11:37:37 -0800658 WRITE_ONCE(next->pprev, &n->next);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200659}
660
661/**
Ken Helias1d023282014-08-06 16:09:16 -0700662 * hlist_add_behind_rcu
Franck Bui-Huu82524742008-05-12 21:21:05 +0200663 * @n: the new element to add to the hash list.
Ken Helias1d023282014-08-06 16:09:16 -0700664 * @prev: the existing element to add the new element after.
Franck Bui-Huu82524742008-05-12 21:21:05 +0200665 *
666 * Description:
667 * Adds the specified element to the specified hlist
668 * after the specified node while permitting racing traversals.
669 *
670 * The caller must take whatever precautions are necessary
671 * (such as holding appropriate locks) to avoid racing
672 * with another list-mutation primitive, such as hlist_add_head_rcu()
673 * or hlist_del_rcu(), running on this same list.
674 * However, it is perfectly legal to run concurrently with
675 * the _rcu list-traversal primitives, such as
676 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
677 * problems on Alpha CPUs.
678 */
Ken Helias1d023282014-08-06 16:09:16 -0700679static inline void hlist_add_behind_rcu(struct hlist_node *n,
680 struct hlist_node *prev)
Franck Bui-Huu82524742008-05-12 21:21:05 +0200681{
682 n->next = prev->next;
Eric Dumazetc54a2742019-11-07 11:37:37 -0800683 WRITE_ONCE(n->pprev, &prev->next);
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100684 rcu_assign_pointer(hlist_next_rcu(prev), n);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200685 if (n->next)
Eric Dumazetc54a2742019-11-07 11:37:37 -0800686 WRITE_ONCE(n->next->pprev, &n->next);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200687}
688
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100689#define __hlist_for_each_rcu(pos, head) \
690 for (pos = rcu_dereference(hlist_first_rcu(head)); \
Linus Torvalds75d65a42011-05-19 13:50:07 -0700691 pos; \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100692 pos = rcu_dereference(hlist_next_rcu(pos)))
stephen hemminger1cc52322010-02-22 07:57:17 +0000693
Franck Bui-Huu82524742008-05-12 21:21:05 +0200694/**
695 * hlist_for_each_entry_rcu - iterate over rcu list of given type
Sasha Levinb67bfe02013-02-27 17:06:00 -0800696 * @pos: the type * to use as a loop cursor.
Franck Bui-Huu82524742008-05-12 21:21:05 +0200697 * @head: the head for your list.
698 * @member: the name of the hlist_node within the struct.
Jonathan Neuschäferddc465932020-03-05 23:22:55 +0100699 * @cond: optional lockdep expression if called from non-RCU protection.
Franck Bui-Huu82524742008-05-12 21:21:05 +0200700 *
701 * This list-traversal primitive may safely run concurrently with
702 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
703 * as long as the traversal is guarded by rcu_read_lock().
704 */
Joel Fernandes (Google)28875942019-07-16 18:12:22 -0400705#define hlist_for_each_entry_rcu(pos, head, member, cond...) \
706 for (__list_check_rcu(dummy, ## cond, 0), \
707 pos = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),\
Sasha Levinb67bfe02013-02-27 17:06:00 -0800708 typeof(*(pos)), member); \
709 pos; \
710 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(\
711 &(pos)->member)), typeof(*(pos)), member))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200712
stephen hemminger5c578aed2010-03-17 20:31:11 +0000713/**
Madhuparna Bhowmikae2212a2020-07-12 18:40:02 +0530714 * hlist_for_each_entry_srcu - iterate over rcu list of given type
715 * @pos: the type * to use as a loop cursor.
716 * @head: the head for your list.
717 * @member: the name of the hlist_node within the struct.
718 * @cond: lockdep expression for the lock required to traverse the list.
719 *
720 * This list-traversal primitive may safely run concurrently with
721 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
722 * as long as the traversal is guarded by srcu_read_lock().
723 * The lockdep expression srcu_read_lock_held() can be passed as the
724 * cond argument from read side.
725 */
726#define hlist_for_each_entry_srcu(pos, head, member, cond) \
727 for (__list_check_srcu(cond), \
728 pos = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),\
729 typeof(*(pos)), member); \
730 pos; \
731 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(\
732 &(pos)->member)), typeof(*(pos)), member))
733
734/**
Steven Rostedt12bcbe62013-05-28 14:38:42 -0400735 * hlist_for_each_entry_rcu_notrace - iterate over rcu list of given type (for tracing)
736 * @pos: the type * to use as a loop cursor.
737 * @head: the head for your list.
738 * @member: the name of the hlist_node within the struct.
739 *
740 * This list-traversal primitive may safely run concurrently with
741 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
742 * as long as the traversal is guarded by rcu_read_lock().
743 *
744 * This is the same as hlist_for_each_entry_rcu() except that it does
745 * not do any RCU debugging or tracing.
746 */
747#define hlist_for_each_entry_rcu_notrace(pos, head, member) \
Joel Fernandes (Google)0a5b99f2019-07-11 16:45:41 -0400748 for (pos = hlist_entry_safe(rcu_dereference_raw_check(hlist_first_rcu(head)),\
Steven Rostedt12bcbe62013-05-28 14:38:42 -0400749 typeof(*(pos)), member); \
750 pos; \
Joel Fernandes (Google)0a5b99f2019-07-11 16:45:41 -0400751 pos = hlist_entry_safe(rcu_dereference_raw_check(hlist_next_rcu(\
Steven Rostedt12bcbe62013-05-28 14:38:42 -0400752 &(pos)->member)), typeof(*(pos)), member))
753
754/**
Eric Dumazet4f70ecc2010-05-03 10:50:14 +0000755 * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
Sasha Levinb67bfe02013-02-27 17:06:00 -0800756 * @pos: the type * to use as a loop cursor.
Eric Dumazet4f70ecc2010-05-03 10:50:14 +0000757 * @head: the head for your list.
758 * @member: the name of the hlist_node within the struct.
759 *
760 * This list-traversal primitive may safely run concurrently with
761 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
762 * as long as the traversal is guarded by rcu_read_lock().
763 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800764#define hlist_for_each_entry_rcu_bh(pos, head, member) \
765 for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_first_rcu(head)),\
766 typeof(*(pos)), member); \
767 pos; \
768 pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu(\
769 &(pos)->member)), typeof(*(pos)), member))
Eric Dumazet4f70ecc2010-05-03 10:50:14 +0000770
771/**
stephen hemminger5c578aed2010-03-17 20:31:11 +0000772 * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point
Sasha Levinb67bfe02013-02-27 17:06:00 -0800773 * @pos: the type * to use as a loop cursor.
stephen hemminger5c578aed2010-03-17 20:31:11 +0000774 * @member: the name of the hlist_node within the struct.
775 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800776#define hlist_for_each_entry_continue_rcu(pos, member) \
Ying Xuef520c982014-12-12 09:36:14 +0800777 for (pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
778 &(pos)->member)), typeof(*(pos)), member); \
Sasha Levinb67bfe02013-02-27 17:06:00 -0800779 pos; \
Ying Xuef520c982014-12-12 09:36:14 +0800780 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
781 &(pos)->member)), typeof(*(pos)), member))
stephen hemminger5c578aed2010-03-17 20:31:11 +0000782
Eric Dumazet4f70ecc2010-05-03 10:50:14 +0000783/**
784 * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point
Sasha Levinb67bfe02013-02-27 17:06:00 -0800785 * @pos: the type * to use as a loop cursor.
Eric Dumazet4f70ecc2010-05-03 10:50:14 +0000786 * @member: the name of the hlist_node within the struct.
787 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800788#define hlist_for_each_entry_continue_rcu_bh(pos, member) \
Ying Xuef520c982014-12-12 09:36:14 +0800789 for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu( \
790 &(pos)->member)), typeof(*(pos)), member); \
Sasha Levinb67bfe02013-02-27 17:06:00 -0800791 pos; \
Ying Xuef520c982014-12-12 09:36:14 +0800792 pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu( \
793 &(pos)->member)), typeof(*(pos)), member))
Eric Dumazet4f70ecc2010-05-03 10:50:14 +0000794
Ying Xue97ede292014-12-02 15:00:30 +0800795/**
796 * hlist_for_each_entry_from_rcu - iterate over a hlist continuing from current point
797 * @pos: the type * to use as a loop cursor.
798 * @member: the name of the hlist_node within the struct.
799 */
800#define hlist_for_each_entry_from_rcu(pos, member) \
801 for (; pos; \
Ying Xuef5177002015-03-26 13:27:08 +0800802 pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
803 &(pos)->member)), typeof(*(pos)), member))
stephen hemminger5c578aed2010-03-17 20:31:11 +0000804
Franck Bui-Huu82524742008-05-12 21:21:05 +0200805#endif /* __KERNEL__ */
806#endif