1 #ifndef _LINUX_RCULIST_H
2 #define _LINUX_RCULIST_H
7 * RCU-protected list version
9 #include <linux/list.h>
10 #include <linux/rcupdate.h>
13 * Insert a new entry between two known consecutive entries.
15 * This is only for internal list manipulation where we know
16 * the prev/next entries already!
18 static inline void __list_add_rcu(struct list_head *new,
19 struct list_head *prev, struct list_head *next)
23 rcu_assign_pointer(prev->next, new);
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
32 * Insert a new entry after the specified head.
33 * This is good for implementing stacks.
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().
43 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
45 __list_add_rcu(new, head, head->next);
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
53 * Insert a new entry before the specified head.
54 * This is useful for implementing queues.
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().
64 static inline void list_add_tail_rcu(struct list_head *new,
65 struct list_head *head)
67 __list_add_rcu(new, head->prev, head);
71 * list_del_rcu - deletes entry from list without re-initialization
72 * @entry: the element to delete from the list.
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
78 * In particular, it means that we can not poison the forward
79 * pointers that may still be used for walking the list.
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().
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.
94 static inline void list_del_rcu(struct list_head *entry)
96 __list_del(entry->prev, entry->next);
97 entry->prev = LIST_POISON2;
101 * hlist_del_init_rcu - deletes entry from hash list with re-initialization
102 * @n: the element to delete from the hash list.
104 * Note: list_unhashed() on the node return true after this. It is
105 * useful for RCU based read lockfree traversal if the writer side
106 * must know if the list entry is still hashed or already unhashed.
108 * In particular, it means that we can not poison the forward pointers
109 * that may still be used for walking the hash list and we can only
110 * zero the pprev pointer so list_unhashed() will return true after
113 * The caller must take whatever precautions are necessary (such as
114 * holding appropriate locks) to avoid racing with another
115 * list-mutation primitive, such as hlist_add_head_rcu() or
116 * hlist_del_rcu(), running on this same list. However, it is
117 * perfectly legal to run concurrently with the _rcu list-traversal
118 * primitives, such as hlist_for_each_entry_rcu().
120 static inline void hlist_del_init_rcu(struct hlist_node *n)
122 if (!hlist_unhashed(n)) {
129 * list_replace_rcu - replace old entry by new one
130 * @old : the element to be replaced
131 * @new : the new element to insert
133 * The @old entry will be replaced with the @new entry atomically.
134 * Note: @old should not be empty.
136 static inline void list_replace_rcu(struct list_head *old,
137 struct list_head *new)
139 new->next = old->next;
140 new->prev = old->prev;
141 rcu_assign_pointer(new->prev->next, new);
142 new->next->prev = new;
143 old->prev = LIST_POISON2;
147 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
148 * @list: the RCU-protected list to splice
149 * @head: the place in the list to splice the first list into
150 * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
152 * @head can be RCU-read traversed concurrently with this function.
154 * Note that this function blocks.
156 * Important note: the caller must take whatever action is necessary to
157 * prevent any other updates to @head. In principle, it is possible
158 * to modify the list as soon as sync() begins execution.
159 * If this sort of thing becomes necessary, an alternative version
160 * based on call_rcu() could be created. But only if -really-
161 * needed -- there is no shortage of RCU API members.
163 static inline void list_splice_init_rcu(struct list_head *list,
164 struct list_head *head,
167 struct list_head *first = list->next;
168 struct list_head *last = list->prev;
169 struct list_head *at = head->next;
171 if (list_empty(head))
174 /* "first" and "last" tracking list, so initialize it. */
176 INIT_LIST_HEAD(list);
179 * At this point, the list body still points to the source list.
180 * Wait for any readers to finish using the list before splicing
181 * the list body into the new list. Any new readers will see
188 * Readers are finished with the source list, so perform splice.
189 * The order is important if the new list is global and accessible
190 * to concurrent RCU readers. Note that RCU readers are not
191 * permitted to traverse the prev pointers without excluding
196 rcu_assign_pointer(head->next, first);
201 #define __list_for_each_rcu(pos, head) \
202 for (pos = rcu_dereference((head)->next); \
204 pos = rcu_dereference(pos->next))
207 * list_for_each_entry_rcu - iterate over rcu list of given type
208 * @pos: the type * to use as a loop cursor.
209 * @head: the head for your list.
210 * @member: the name of the list_struct within the struct.
212 * This list-traversal primitive may safely run concurrently with
213 * the _rcu list-mutation primitives such as list_add_rcu()
214 * as long as the traversal is guarded by rcu_read_lock().
216 #define list_for_each_entry_rcu(pos, head, member) \
217 for (pos = list_entry(rcu_dereference((head)->next), typeof(*pos), member); \
218 prefetch(pos->member.next), &pos->member != (head); \
219 pos = list_entry(rcu_dereference(pos->member.next), typeof(*pos), member))
223 * list_for_each_continue_rcu
224 * @pos: the &struct list_head to use as a loop cursor.
225 * @head: the head for your list.
227 * Iterate over an rcu-protected list, continuing after current point.
229 * This list-traversal primitive may safely run concurrently with
230 * the _rcu list-mutation primitives such as list_add_rcu()
231 * as long as the traversal is guarded by rcu_read_lock().
233 #define list_for_each_continue_rcu(pos, head) \
234 for ((pos) = rcu_dereference((pos)->next); \
235 prefetch((pos)->next), (pos) != (head); \
236 (pos) = rcu_dereference((pos)->next))
239 * hlist_del_rcu - deletes entry from hash list without re-initialization
240 * @n: the element to delete from the hash list.
242 * Note: list_unhashed() on entry does not return true after this,
243 * the entry is in an undefined state. It is useful for RCU based
244 * lockfree traversal.
246 * In particular, it means that we can not poison the forward
247 * pointers that may still be used for walking the hash list.
249 * The caller must take whatever precautions are necessary
250 * (such as holding appropriate locks) to avoid racing
251 * with another list-mutation primitive, such as hlist_add_head_rcu()
252 * or hlist_del_rcu(), running on this same list.
253 * However, it is perfectly legal to run concurrently with
254 * the _rcu list-traversal primitives, such as
255 * hlist_for_each_entry().
257 static inline void hlist_del_rcu(struct hlist_node *n)
260 n->pprev = LIST_POISON2;
264 * hlist_replace_rcu - replace old entry by new one
265 * @old : the element to be replaced
266 * @new : the new element to insert
268 * The @old entry will be replaced with the @new entry atomically.
270 static inline void hlist_replace_rcu(struct hlist_node *old,
271 struct hlist_node *new)
273 struct hlist_node *next = old->next;
276 new->pprev = old->pprev;
277 rcu_assign_pointer(*new->pprev, new);
279 new->next->pprev = &new->next;
280 old->pprev = LIST_POISON2;
285 * @n: the element to add to the hash list.
286 * @h: the list to add to.
289 * Adds the specified element to the specified hlist,
290 * while permitting racing traversals.
292 * The caller must take whatever precautions are necessary
293 * (such as holding appropriate locks) to avoid racing
294 * with another list-mutation primitive, such as hlist_add_head_rcu()
295 * or hlist_del_rcu(), running on this same list.
296 * However, it is perfectly legal to run concurrently with
297 * the _rcu list-traversal primitives, such as
298 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
299 * problems on Alpha CPUs. Regardless of the type of CPU, the
300 * list-traversal primitive must be guarded by rcu_read_lock().
302 static inline void hlist_add_head_rcu(struct hlist_node *n,
303 struct hlist_head *h)
305 struct hlist_node *first = h->first;
308 n->pprev = &h->first;
309 rcu_assign_pointer(h->first, n);
311 first->pprev = &n->next;
315 * hlist_add_before_rcu
316 * @n: the new element to add to the hash list.
317 * @next: the existing element to add the new element before.
320 * Adds the specified element to the specified hlist
321 * before the specified node while permitting racing traversals.
323 * The caller must take whatever precautions are necessary
324 * (such as holding appropriate locks) to avoid racing
325 * with another list-mutation primitive, such as hlist_add_head_rcu()
326 * or hlist_del_rcu(), running on this same list.
327 * However, it is perfectly legal to run concurrently with
328 * the _rcu list-traversal primitives, such as
329 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
330 * problems on Alpha CPUs.
332 static inline void hlist_add_before_rcu(struct hlist_node *n,
333 struct hlist_node *next)
335 n->pprev = next->pprev;
337 rcu_assign_pointer(*(n->pprev), n);
338 next->pprev = &n->next;
342 * hlist_add_after_rcu
343 * @prev: the existing element to add the new element after.
344 * @n: the new element to add to the hash list.
347 * Adds the specified element to the specified hlist
348 * after the specified node while permitting racing traversals.
350 * The caller must take whatever precautions are necessary
351 * (such as holding appropriate locks) to avoid racing
352 * with another list-mutation primitive, such as hlist_add_head_rcu()
353 * or hlist_del_rcu(), running on this same list.
354 * However, it is perfectly legal to run concurrently with
355 * the _rcu list-traversal primitives, such as
356 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
357 * problems on Alpha CPUs.
359 static inline void hlist_add_after_rcu(struct hlist_node *prev,
360 struct hlist_node *n)
362 n->next = prev->next;
363 n->pprev = &prev->next;
364 rcu_assign_pointer(prev->next, n);
366 n->next->pprev = &n->next;
370 * hlist_for_each_entry_rcu - iterate over rcu list of given type
371 * @tpos: the type * to use as a loop cursor.
372 * @pos: the &struct hlist_node to use as a loop cursor.
373 * @head: the head for your list.
374 * @member: the name of the hlist_node within the struct.
376 * This list-traversal primitive may safely run concurrently with
377 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
378 * as long as the traversal is guarded by rcu_read_lock().
380 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
381 for (pos = rcu_dereference((head)->first); \
382 pos && ({ prefetch(pos->next); 1; }) && \
383 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
384 pos = rcu_dereference(pos->next))
386 #endif /* __KERNEL__ */