1 #ifndef _LINUX_RCULIST_H
2 #define _LINUX_RCULIST_H
7 * RCU-protected list version
9 #include <linux/list.h>
12 * Insert a new entry between two known consecutive entries.
14 * This is only for internal list manipulation where we know
15 * the prev/next entries already!
17 static inline void __list_add_rcu(struct list_head *new,
18 struct list_head *prev, struct list_head *next)
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 * list_replace_rcu - replace old entry by new one
102 * @old : the element to be replaced
103 * @new : the new element to insert
105 * The @old entry will be replaced with the @new entry atomically.
106 * Note: @old should not be empty.
108 static inline void list_replace_rcu(struct list_head *old,
109 struct list_head *new)
111 new->next = old->next;
112 new->prev = old->prev;
114 new->next->prev = new;
115 new->prev->next = new;
116 old->prev = LIST_POISON2;
120 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
121 * @list: the RCU-protected list to splice
122 * @head: the place in the list to splice the first list into
123 * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
125 * @head can be RCU-read traversed concurrently with this function.
127 * Note that this function blocks.
129 * Important note: the caller must take whatever action is necessary to
130 * prevent any other updates to @head. In principle, it is possible
131 * to modify the list as soon as sync() begins execution.
132 * If this sort of thing becomes necessary, an alternative version
133 * based on call_rcu() could be created. But only if -really-
134 * needed -- there is no shortage of RCU API members.
136 static inline void list_splice_init_rcu(struct list_head *list,
137 struct list_head *head,
140 struct list_head *first = list->next;
141 struct list_head *last = list->prev;
142 struct list_head *at = head->next;
144 if (list_empty(head))
147 /* "first" and "last" tracking list, so initialize it. */
149 INIT_LIST_HEAD(list);
152 * At this point, the list body still points to the source list.
153 * Wait for any readers to finish using the list before splicing
154 * the list body into the new list. Any new readers will see
161 * Readers are finished with the source list, so perform splice.
162 * The order is important if the new list is global and accessible
163 * to concurrent RCU readers. Note that RCU readers are not
164 * permitted to traverse the prev pointers without excluding
176 * list_for_each_rcu - iterate over an rcu-protected list
177 * @pos: the &struct list_head to use as a loop cursor.
178 * @head: the head for your list.
180 * This list-traversal primitive may safely run concurrently with
181 * the _rcu list-mutation primitives such as list_add_rcu()
182 * as long as the traversal is guarded by rcu_read_lock().
184 #define list_for_each_rcu(pos, head) \
185 for (pos = (head)->next; \
186 prefetch(rcu_dereference(pos)->next), pos != (head); \
189 #define __list_for_each_rcu(pos, head) \
190 for (pos = (head)->next; \
191 rcu_dereference(pos) != (head); \
195 * list_for_each_safe_rcu
196 * @pos: the &struct list_head to use as a loop cursor.
197 * @n: another &struct list_head to use as temporary storage
198 * @head: the head for your list.
200 * Iterate over an rcu-protected list, safe against removal of list entry.
202 * This list-traversal primitive may safely run concurrently with
203 * the _rcu list-mutation primitives such as list_add_rcu()
204 * as long as the traversal is guarded by rcu_read_lock().
206 #define list_for_each_safe_rcu(pos, n, head) \
207 for (pos = (head)->next; \
208 n = rcu_dereference(pos)->next, pos != (head); \
212 * list_for_each_entry_rcu - iterate over rcu list of given type
213 * @pos: the type * to use as a loop cursor.
214 * @head: the head for your list.
215 * @member: the name of the list_struct within the struct.
217 * This list-traversal primitive may safely run concurrently with
218 * the _rcu list-mutation primitives such as list_add_rcu()
219 * as long as the traversal is guarded by rcu_read_lock().
221 #define list_for_each_entry_rcu(pos, head, member) \
222 for (pos = list_entry((head)->next, typeof(*pos), member); \
223 prefetch(rcu_dereference(pos)->member.next), \
224 &pos->member != (head); \
225 pos = list_entry(pos->member.next, typeof(*pos), member))
229 * list_for_each_continue_rcu
230 * @pos: the &struct list_head to use as a loop cursor.
231 * @head: the head for your list.
233 * Iterate over an rcu-protected list, continuing after current point.
235 * This list-traversal primitive may safely run concurrently with
236 * the _rcu list-mutation primitives such as list_add_rcu()
237 * as long as the traversal is guarded by rcu_read_lock().
239 #define list_for_each_continue_rcu(pos, head) \
240 for ((pos) = (pos)->next; \
241 prefetch(rcu_dereference((pos))->next), (pos) != (head); \
245 * hlist_del_rcu - deletes entry from hash list without re-initialization
246 * @n: the element to delete from the hash list.
248 * Note: list_unhashed() on entry does not return true after this,
249 * the entry is in an undefined state. It is useful for RCU based
250 * lockfree traversal.
252 * In particular, it means that we can not poison the forward
253 * pointers that may still be used for walking the hash list.
255 * The caller must take whatever precautions are necessary
256 * (such as holding appropriate locks) to avoid racing
257 * with another list-mutation primitive, such as hlist_add_head_rcu()
258 * or hlist_del_rcu(), running on this same list.
259 * However, it is perfectly legal to run concurrently with
260 * the _rcu list-traversal primitives, such as
261 * hlist_for_each_entry().
263 static inline void hlist_del_rcu(struct hlist_node *n)
266 n->pprev = LIST_POISON2;
270 * hlist_replace_rcu - replace old entry by new one
271 * @old : the element to be replaced
272 * @new : the new element to insert
274 * The @old entry will be replaced with the @new entry atomically.
276 static inline void hlist_replace_rcu(struct hlist_node *old,
277 struct hlist_node *new)
279 struct hlist_node *next = old->next;
282 new->pprev = old->pprev;
285 new->next->pprev = &new->next;
287 old->pprev = LIST_POISON2;
292 * @n: the element to add to the hash list.
293 * @h: the list to add to.
296 * Adds the specified element to the specified hlist,
297 * while permitting racing traversals.
299 * The caller must take whatever precautions are necessary
300 * (such as holding appropriate locks) to avoid racing
301 * with another list-mutation primitive, such as hlist_add_head_rcu()
302 * or hlist_del_rcu(), running on this same list.
303 * However, it is perfectly legal to run concurrently with
304 * the _rcu list-traversal primitives, such as
305 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
306 * problems on Alpha CPUs. Regardless of the type of CPU, the
307 * list-traversal primitive must be guarded by rcu_read_lock().
309 static inline void hlist_add_head_rcu(struct hlist_node *n,
310 struct hlist_head *h)
312 struct hlist_node *first = h->first;
314 n->pprev = &h->first;
317 first->pprev = &n->next;
322 * hlist_add_before_rcu
323 * @n: the new element to add to the hash list.
324 * @next: the existing element to add the new element before.
327 * Adds the specified element to the specified hlist
328 * before the specified node while permitting racing traversals.
330 * The caller must take whatever precautions are necessary
331 * (such as holding appropriate locks) to avoid racing
332 * with another list-mutation primitive, such as hlist_add_head_rcu()
333 * or hlist_del_rcu(), running on this same list.
334 * However, it is perfectly legal to run concurrently with
335 * the _rcu list-traversal primitives, such as
336 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
337 * problems on Alpha CPUs.
339 static inline void hlist_add_before_rcu(struct hlist_node *n,
340 struct hlist_node *next)
342 n->pprev = next->pprev;
345 next->pprev = &n->next;
350 * hlist_add_after_rcu
351 * @prev: the existing element to add the new element after.
352 * @n: the new element to add to the hash list.
355 * Adds the specified element to the specified hlist
356 * after the specified node while permitting racing traversals.
358 * The caller must take whatever precautions are necessary
359 * (such as holding appropriate locks) to avoid racing
360 * with another list-mutation primitive, such as hlist_add_head_rcu()
361 * or hlist_del_rcu(), running on this same list.
362 * However, it is perfectly legal to run concurrently with
363 * the _rcu list-traversal primitives, such as
364 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
365 * problems on Alpha CPUs.
367 static inline void hlist_add_after_rcu(struct hlist_node *prev,
368 struct hlist_node *n)
370 n->next = prev->next;
371 n->pprev = &prev->next;
375 n->next->pprev = &n->next;
379 * hlist_for_each_entry_rcu - iterate over rcu list of given type
380 * @tpos: the type * to use as a loop cursor.
381 * @pos: the &struct hlist_node to use as a loop cursor.
382 * @head: the head for your list.
383 * @member: the name of the hlist_node within the struct.
385 * This list-traversal primitive may safely run concurrently with
386 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
387 * as long as the traversal is guarded by rcu_read_lock().
389 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
390 for (pos = (head)->first; \
391 rcu_dereference(pos) && ({ prefetch(pos->next); 1; }) && \
392 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
395 #endif /* __KERNEL__ */