xref: /linux-6.15/include/linux/rculist.h (revision 1795cf48)
1 #ifndef _LINUX_RCULIST_H
2 #define _LINUX_RCULIST_H
3 
4 #ifdef __KERNEL__
5 
6 /*
7  * RCU-protected list version
8  */
9 #include <linux/list.h>
10 #include <linux/rcupdate.h>
11 
12 /*
13  * Insert a new entry between two known consecutive entries.
14  *
15  * This is only for internal list manipulation where we know
16  * the prev/next entries already!
17  */
18 static inline void __list_add_rcu(struct list_head *new,
19 		struct list_head *prev, struct list_head *next)
20 {
21 	new->next = next;
22 	new->prev = prev;
23 	rcu_assign_pointer(prev->next, new);
24 	next->prev = new;
25 }
26 
27 /**
28  * list_add_rcu - add a new entry to rcu-protected list
29  * @new: new entry to be added
30  * @head: list head to add it after
31  *
32  * Insert a new entry after the specified head.
33  * This is good for implementing stacks.
34  *
35  * The caller must take whatever precautions are necessary
36  * (such as holding appropriate locks) to avoid racing
37  * with another list-mutation primitive, such as list_add_rcu()
38  * or list_del_rcu(), running on this same list.
39  * However, it is perfectly legal to run concurrently with
40  * the _rcu list-traversal primitives, such as
41  * list_for_each_entry_rcu().
42  */
43 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
44 {
45 	__list_add_rcu(new, head, head->next);
46 }
47 
48 /**
49  * list_add_tail_rcu - add a new entry to rcu-protected list
50  * @new: new entry to be added
51  * @head: list head to add it before
52  *
53  * Insert a new entry before the specified head.
54  * This is useful for implementing queues.
55  *
56  * The caller must take whatever precautions are necessary
57  * (such as holding appropriate locks) to avoid racing
58  * with another list-mutation primitive, such as list_add_tail_rcu()
59  * or list_del_rcu(), running on this same list.
60  * However, it is perfectly legal to run concurrently with
61  * the _rcu list-traversal primitives, such as
62  * list_for_each_entry_rcu().
63  */
64 static inline void list_add_tail_rcu(struct list_head *new,
65 					struct list_head *head)
66 {
67 	__list_add_rcu(new, head->prev, head);
68 }
69 
70 /**
71  * list_del_rcu - deletes entry from list without re-initialization
72  * @entry: the element to delete from the list.
73  *
74  * Note: list_empty() on entry does not return true after this,
75  * the entry is in an undefined state. It is useful for RCU based
76  * lockfree traversal.
77  *
78  * In particular, it means that we can not poison the forward
79  * pointers that may still be used for walking the list.
80  *
81  * The caller must take whatever precautions are necessary
82  * (such as holding appropriate locks) to avoid racing
83  * with another list-mutation primitive, such as list_del_rcu()
84  * or list_add_rcu(), running on this same list.
85  * However, it is perfectly legal to run concurrently with
86  * the _rcu list-traversal primitives, such as
87  * list_for_each_entry_rcu().
88  *
89  * Note that the caller is not permitted to immediately free
90  * the newly deleted entry.  Instead, either synchronize_rcu()
91  * or call_rcu() must be used to defer freeing until an RCU
92  * grace period has elapsed.
93  */
94 static inline void list_del_rcu(struct list_head *entry)
95 {
96 	__list_del(entry->prev, entry->next);
97 	entry->prev = LIST_POISON2;
98 }
99 
100 /**
101  * list_replace_rcu - replace old entry by new one
102  * @old : the element to be replaced
103  * @new : the new element to insert
104  *
105  * The @old entry will be replaced with the @new entry atomically.
106  * Note: @old should not be empty.
107  */
108 static inline void list_replace_rcu(struct list_head *old,
109 				struct list_head *new)
110 {
111 	new->next = old->next;
112 	new->prev = old->prev;
113 	rcu_assign_pointer(new->prev->next, new);
114 	new->next->prev = new;
115 	old->prev = LIST_POISON2;
116 }
117 
118 /**
119  * list_splice_init_rcu - splice an RCU-protected list into an existing list.
120  * @list:	the RCU-protected list to splice
121  * @head:	the place in the list to splice the first list into
122  * @sync:	function to sync: synchronize_rcu(), synchronize_sched(), ...
123  *
124  * @head can be RCU-read traversed concurrently with this function.
125  *
126  * Note that this function blocks.
127  *
128  * Important note: the caller must take whatever action is necessary to
129  *	prevent any other updates to @head.  In principle, it is possible
130  *	to modify the list as soon as sync() begins execution.
131  *	If this sort of thing becomes necessary, an alternative version
132  *	based on call_rcu() could be created.  But only if -really-
133  *	needed -- there is no shortage of RCU API members.
134  */
135 static inline void list_splice_init_rcu(struct list_head *list,
136 					struct list_head *head,
137 					void (*sync)(void))
138 {
139 	struct list_head *first = list->next;
140 	struct list_head *last = list->prev;
141 	struct list_head *at = head->next;
142 
143 	if (list_empty(head))
144 		return;
145 
146 	/* "first" and "last" tracking list, so initialize it. */
147 
148 	INIT_LIST_HEAD(list);
149 
150 	/*
151 	 * At this point, the list body still points to the source list.
152 	 * Wait for any readers to finish using the list before splicing
153 	 * the list body into the new list.  Any new readers will see
154 	 * an empty list.
155 	 */
156 
157 	sync();
158 
159 	/*
160 	 * Readers are finished with the source list, so perform splice.
161 	 * The order is important if the new list is global and accessible
162 	 * to concurrent RCU readers.  Note that RCU readers are not
163 	 * permitted to traverse the prev pointers without excluding
164 	 * this function.
165 	 */
166 
167 	last->next = at;
168 	rcu_assign_pointer(head->next, first);
169 	first->prev = head;
170 	at->prev = last;
171 }
172 
173 /**
174  * list_for_each_rcu	-	iterate over an rcu-protected list
175  * @pos:	the &struct list_head to use as a loop cursor.
176  * @head:	the head for your list.
177  *
178  * This list-traversal primitive may safely run concurrently with
179  * the _rcu list-mutation primitives such as list_add_rcu()
180  * as long as the traversal is guarded by rcu_read_lock().
181  */
182 #define list_for_each_rcu(pos, head) \
183 	for (pos = rcu_dereference((head)->next); \
184 		prefetch(pos->next), pos != (head); \
185 		pos = rcu_dereference(pos->next))
186 
187 #define __list_for_each_rcu(pos, head) \
188 	for (pos = rcu_dereference((head)->next); \
189 		pos != (head); \
190 		pos = rcu_dereference(pos->next))
191 
192 /**
193  * list_for_each_entry_rcu	-	iterate over rcu list of given type
194  * @pos:	the type * to use as a loop cursor.
195  * @head:	the head for your list.
196  * @member:	the name of the list_struct within the struct.
197  *
198  * This list-traversal primitive may safely run concurrently with
199  * the _rcu list-mutation primitives such as list_add_rcu()
200  * as long as the traversal is guarded by rcu_read_lock().
201  */
202 #define list_for_each_entry_rcu(pos, head, member) \
203 	for (pos = list_entry(rcu_dereference((head)->next), typeof(*pos), member); \
204 		prefetch(pos->member.next), &pos->member != (head); \
205 		pos = list_entry(rcu_dereference(pos->member.next), typeof(*pos), member))
206 
207 
208 /**
209  * list_for_each_continue_rcu
210  * @pos:	the &struct list_head to use as a loop cursor.
211  * @head:	the head for your list.
212  *
213  * Iterate over an rcu-protected list, continuing after current point.
214  *
215  * This list-traversal primitive may safely run concurrently with
216  * the _rcu list-mutation primitives such as list_add_rcu()
217  * as long as the traversal is guarded by rcu_read_lock().
218  */
219 #define list_for_each_continue_rcu(pos, head) \
220 	for ((pos) = rcu_dereference((pos)->next); \
221 		prefetch((pos)->next), (pos) != (head); \
222 		(pos) = rcu_dereference((pos)->next))
223 
224 /**
225  * hlist_del_rcu - deletes entry from hash list without re-initialization
226  * @n: the element to delete from the hash list.
227  *
228  * Note: list_unhashed() on entry does not return true after this,
229  * the entry is in an undefined state. It is useful for RCU based
230  * lockfree traversal.
231  *
232  * In particular, it means that we can not poison the forward
233  * pointers that may still be used for walking the hash list.
234  *
235  * The caller must take whatever precautions are necessary
236  * (such as holding appropriate locks) to avoid racing
237  * with another list-mutation primitive, such as hlist_add_head_rcu()
238  * or hlist_del_rcu(), running on this same list.
239  * However, it is perfectly legal to run concurrently with
240  * the _rcu list-traversal primitives, such as
241  * hlist_for_each_entry().
242  */
243 static inline void hlist_del_rcu(struct hlist_node *n)
244 {
245 	__hlist_del(n);
246 	n->pprev = LIST_POISON2;
247 }
248 
249 /**
250  * hlist_replace_rcu - replace old entry by new one
251  * @old : the element to be replaced
252  * @new : the new element to insert
253  *
254  * The @old entry will be replaced with the @new entry atomically.
255  */
256 static inline void hlist_replace_rcu(struct hlist_node *old,
257 					struct hlist_node *new)
258 {
259 	struct hlist_node *next = old->next;
260 
261 	new->next = next;
262 	new->pprev = old->pprev;
263 	rcu_assign_pointer(*new->pprev, new);
264 	if (next)
265 		new->next->pprev = &new->next;
266 	old->pprev = LIST_POISON2;
267 }
268 
269 /**
270  * hlist_add_head_rcu
271  * @n: the element to add to the hash list.
272  * @h: the list to add to.
273  *
274  * Description:
275  * Adds the specified element to the specified hlist,
276  * while permitting racing traversals.
277  *
278  * The caller must take whatever precautions are necessary
279  * (such as holding appropriate locks) to avoid racing
280  * with another list-mutation primitive, such as hlist_add_head_rcu()
281  * or hlist_del_rcu(), running on this same list.
282  * However, it is perfectly legal to run concurrently with
283  * the _rcu list-traversal primitives, such as
284  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
285  * problems on Alpha CPUs.  Regardless of the type of CPU, the
286  * list-traversal primitive must be guarded by rcu_read_lock().
287  */
288 static inline void hlist_add_head_rcu(struct hlist_node *n,
289 					struct hlist_head *h)
290 {
291 	struct hlist_node *first = h->first;
292 
293 	n->next = first;
294 	n->pprev = &h->first;
295 	rcu_assign_pointer(h->first, n);
296 	if (first)
297 		first->pprev = &n->next;
298 }
299 
300 /**
301  * hlist_add_before_rcu
302  * @n: the new element to add to the hash list.
303  * @next: the existing element to add the new element before.
304  *
305  * Description:
306  * Adds the specified element to the specified hlist
307  * before the specified node while permitting racing traversals.
308  *
309  * The caller must take whatever precautions are necessary
310  * (such as holding appropriate locks) to avoid racing
311  * with another list-mutation primitive, such as hlist_add_head_rcu()
312  * or hlist_del_rcu(), running on this same list.
313  * However, it is perfectly legal to run concurrently with
314  * the _rcu list-traversal primitives, such as
315  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
316  * problems on Alpha CPUs.
317  */
318 static inline void hlist_add_before_rcu(struct hlist_node *n,
319 					struct hlist_node *next)
320 {
321 	n->pprev = next->pprev;
322 	n->next = next;
323 	rcu_assign_pointer(*(n->pprev), n);
324 	next->pprev = &n->next;
325 }
326 
327 /**
328  * hlist_add_after_rcu
329  * @prev: the existing element to add the new element after.
330  * @n: the new element to add to the hash list.
331  *
332  * Description:
333  * Adds the specified element to the specified hlist
334  * after the specified node while permitting racing traversals.
335  *
336  * The caller must take whatever precautions are necessary
337  * (such as holding appropriate locks) to avoid racing
338  * with another list-mutation primitive, such as hlist_add_head_rcu()
339  * or hlist_del_rcu(), running on this same list.
340  * However, it is perfectly legal to run concurrently with
341  * the _rcu list-traversal primitives, such as
342  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
343  * problems on Alpha CPUs.
344  */
345 static inline void hlist_add_after_rcu(struct hlist_node *prev,
346 				       struct hlist_node *n)
347 {
348 	n->next = prev->next;
349 	n->pprev = &prev->next;
350 	rcu_assign_pointer(prev->next, n);
351 	if (n->next)
352 		n->next->pprev = &n->next;
353 }
354 
355 /**
356  * hlist_for_each_entry_rcu - iterate over rcu list of given type
357  * @tpos:	the type * to use as a loop cursor.
358  * @pos:	the &struct hlist_node to use as a loop cursor.
359  * @head:	the head for your list.
360  * @member:	the name of the hlist_node within the struct.
361  *
362  * This list-traversal primitive may safely run concurrently with
363  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
364  * as long as the traversal is guarded by rcu_read_lock().
365  */
366 #define hlist_for_each_entry_rcu(tpos, pos, head, member)		 \
367 	for (pos = rcu_dereference((head)->first);			 \
368 		pos && ({ prefetch(pos->next); 1; }) &&			 \
369 		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
370 		pos = rcu_dereference(pos->next))
371 
372 #endif	/* __KERNEL__ */
373 #endif
374