blob: c10b1050dbe6c617419b0dd89a86f52b96fe3979 [file] [log] [blame]
Franck Bui-Huu82524742008-05-12 21:21:05 +02001#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>
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +020010#include <linux/rcupdate.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020011
12/*
Arnd Bergmann67bdbff2010-02-25 16:55:13 +010013 * return the ->next pointer of a list_head in an rcu safe
14 * way, we must not access it directly
15 */
16#define list_next_rcu(list) (*((struct list_head __rcu **)(&(list)->next)))
17
18/*
Franck Bui-Huu82524742008-05-12 21:21:05 +020019 * Insert a new entry between two known consecutive entries.
20 *
21 * This is only for internal list manipulation where we know
22 * the prev/next entries already!
23 */
24static inline void __list_add_rcu(struct list_head *new,
25 struct list_head *prev, struct list_head *next)
26{
27 new->next = next;
28 new->prev = prev;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +010029 rcu_assign_pointer(list_next_rcu(prev), new);
Franck Bui-Huu82524742008-05-12 21:21:05 +020030 next->prev = new;
Franck Bui-Huu82524742008-05-12 21:21:05 +020031}
32
33/**
34 * list_add_rcu - add a new entry to rcu-protected list
35 * @new: new entry to be added
36 * @head: list head to add it after
37 *
38 * Insert a new entry after the specified head.
39 * This is good for implementing stacks.
40 *
41 * The caller must take whatever precautions are necessary
42 * (such as holding appropriate locks) to avoid racing
43 * with another list-mutation primitive, such as list_add_rcu()
44 * or list_del_rcu(), running on this same list.
45 * However, it is perfectly legal to run concurrently with
46 * the _rcu list-traversal primitives, such as
47 * list_for_each_entry_rcu().
48 */
49static inline void list_add_rcu(struct list_head *new, struct list_head *head)
50{
51 __list_add_rcu(new, head, head->next);
52}
53
54/**
55 * list_add_tail_rcu - add a new entry to rcu-protected list
56 * @new: new entry to be added
57 * @head: list head to add it before
58 *
59 * Insert a new entry before the specified head.
60 * This is useful for implementing queues.
61 *
62 * The caller must take whatever precautions are necessary
63 * (such as holding appropriate locks) to avoid racing
64 * with another list-mutation primitive, such as list_add_tail_rcu()
65 * or list_del_rcu(), running on this same list.
66 * However, it is perfectly legal to run concurrently with
67 * the _rcu list-traversal primitives, such as
68 * list_for_each_entry_rcu().
69 */
70static inline void list_add_tail_rcu(struct list_head *new,
71 struct list_head *head)
72{
73 __list_add_rcu(new, head->prev, head);
74}
75
76/**
77 * list_del_rcu - deletes entry from list without re-initialization
78 * @entry: the element to delete from the list.
79 *
80 * Note: list_empty() on entry does not return true after this,
81 * the entry is in an undefined state. It is useful for RCU based
82 * lockfree traversal.
83 *
84 * In particular, it means that we can not poison the forward
85 * pointers that may still be used for walking the list.
86 *
87 * The caller must take whatever precautions are necessary
88 * (such as holding appropriate locks) to avoid racing
89 * with another list-mutation primitive, such as list_del_rcu()
90 * or list_add_rcu(), running on this same list.
91 * However, it is perfectly legal to run concurrently with
92 * the _rcu list-traversal primitives, such as
93 * list_for_each_entry_rcu().
94 *
95 * Note that the caller is not permitted to immediately free
96 * the newly deleted entry. Instead, either synchronize_rcu()
97 * or call_rcu() must be used to defer freeing until an RCU
98 * grace period has elapsed.
99 */
100static inline void list_del_rcu(struct list_head *entry)
101{
102 __list_del(entry->prev, entry->next);
103 entry->prev = LIST_POISON2;
104}
105
106/**
Andrea Arcangeli6beeac72008-07-28 15:46:22 -0700107 * hlist_del_init_rcu - deletes entry from hash list with re-initialization
108 * @n: the element to delete from the hash list.
109 *
110 * Note: list_unhashed() on the node return true after this. It is
111 * useful for RCU based read lockfree traversal if the writer side
112 * must know if the list entry is still hashed or already unhashed.
113 *
114 * In particular, it means that we can not poison the forward pointers
115 * that may still be used for walking the hash list and we can only
116 * zero the pprev pointer so list_unhashed() will return true after
117 * this.
118 *
119 * The caller must take whatever precautions are necessary (such as
120 * holding appropriate locks) to avoid racing with another
121 * list-mutation primitive, such as hlist_add_head_rcu() or
122 * hlist_del_rcu(), running on this same list. However, it is
123 * perfectly legal to run concurrently with the _rcu list-traversal
124 * primitives, such as hlist_for_each_entry_rcu().
125 */
126static inline void hlist_del_init_rcu(struct hlist_node *n)
127{
128 if (!hlist_unhashed(n)) {
129 __hlist_del(n);
130 n->pprev = NULL;
131 }
132}
133
134/**
Franck Bui-Huu82524742008-05-12 21:21:05 +0200135 * list_replace_rcu - replace old entry by new one
136 * @old : the element to be replaced
137 * @new : the new element to insert
138 *
139 * The @old entry will be replaced with the @new entry atomically.
140 * Note: @old should not be empty.
141 */
142static inline void list_replace_rcu(struct list_head *old,
143 struct list_head *new)
144{
145 new->next = old->next;
146 new->prev = old->prev;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100147 rcu_assign_pointer(list_next_rcu(new->prev), new);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200148 new->next->prev = new;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200149 old->prev = LIST_POISON2;
150}
151
152/**
153 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
154 * @list: the RCU-protected list to splice
155 * @head: the place in the list to splice the first list into
156 * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
157 *
158 * @head can be RCU-read traversed concurrently with this function.
159 *
160 * Note that this function blocks.
161 *
162 * Important note: the caller must take whatever action is necessary to
163 * prevent any other updates to @head. In principle, it is possible
164 * to modify the list as soon as sync() begins execution.
165 * If this sort of thing becomes necessary, an alternative version
166 * based on call_rcu() could be created. But only if -really-
167 * needed -- there is no shortage of RCU API members.
168 */
169static inline void list_splice_init_rcu(struct list_head *list,
170 struct list_head *head,
171 void (*sync)(void))
172{
173 struct list_head *first = list->next;
174 struct list_head *last = list->prev;
175 struct list_head *at = head->next;
176
177 if (list_empty(head))
178 return;
179
180 /* "first" and "last" tracking list, so initialize it. */
181
182 INIT_LIST_HEAD(list);
183
184 /*
185 * At this point, the list body still points to the source list.
186 * Wait for any readers to finish using the list before splicing
187 * the list body into the new list. Any new readers will see
188 * an empty list.
189 */
190
191 sync();
192
193 /*
194 * Readers are finished with the source list, so perform splice.
195 * The order is important if the new list is global and accessible
196 * to concurrent RCU readers. Note that RCU readers are not
197 * permitted to traverse the prev pointers without excluding
198 * this function.
199 */
200
201 last->next = at;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100202 rcu_assign_pointer(list_next_rcu(head), first);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200203 first->prev = head;
204 at->prev = last;
205}
206
Jiri Pirko72c6a982009-04-14 17:33:57 +0200207/**
208 * list_entry_rcu - get the struct for this entry
209 * @ptr: the &struct list_head pointer.
210 * @type: the type of the struct this is embedded in.
211 * @member: the name of the list_struct within the struct.
212 *
213 * This primitive may safely run concurrently with the _rcu list-mutation
214 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
215 */
216#define list_entry_rcu(ptr, type, member) \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100217 ({typeof (*ptr) __rcu *__ptr = (typeof (*ptr) __rcu __force *)ptr; \
218 container_of((typeof(ptr))rcu_dereference_raw(__ptr), type, member); \
219 })
Jiri Pirko72c6a982009-04-14 17:33:57 +0200220
221/**
222 * list_first_entry_rcu - get the first element from a list
223 * @ptr: the list head to take the element from.
224 * @type: the type of the struct this is embedded in.
225 * @member: the name of the list_struct within the struct.
226 *
227 * Note, that list is expected to be not empty.
228 *
229 * This primitive may safely run concurrently with the _rcu list-mutation
230 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
231 */
232#define list_first_entry_rcu(ptr, type, member) \
233 list_entry_rcu((ptr)->next, type, member)
234
Franck Bui-Huu82524742008-05-12 21:21:05 +0200235#define __list_for_each_rcu(pos, head) \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100236 for (pos = rcu_dereference_raw(list_next_rcu(head)); \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200237 pos != (head); \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100238 pos = rcu_dereference_raw(list_next_rcu((pos)))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200239
240/**
241 * list_for_each_entry_rcu - iterate over rcu list of given type
242 * @pos: the type * to use as a loop cursor.
243 * @head: the head for your list.
244 * @member: the name of the list_struct within the struct.
245 *
246 * This list-traversal primitive may safely run concurrently with
247 * the _rcu list-mutation primitives such as list_add_rcu()
248 * as long as the traversal is guarded by rcu_read_lock().
249 */
250#define list_for_each_entry_rcu(pos, head, member) \
Jiri Pirko72c6a982009-04-14 17:33:57 +0200251 for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200252 prefetch(pos->member.next), &pos->member != (head); \
Jiri Pirko72c6a982009-04-14 17:33:57 +0200253 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200254
255
256/**
257 * list_for_each_continue_rcu
258 * @pos: the &struct list_head to use as a loop cursor.
259 * @head: the head for your list.
260 *
261 * Iterate over an rcu-protected list, continuing after current point.
262 *
263 * This list-traversal primitive may safely run concurrently with
264 * the _rcu list-mutation primitives such as list_add_rcu()
265 * as long as the traversal is guarded by rcu_read_lock().
266 */
267#define list_for_each_continue_rcu(pos, head) \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100268 for ((pos) = rcu_dereference_raw(list_next_rcu(pos)); \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200269 prefetch((pos)->next), (pos) != (head); \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100270 (pos) = rcu_dereference_raw(list_next_rcu(pos)))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200271
272/**
stephen hemminger254245d2009-11-10 07:54:47 +0000273 * list_for_each_entry_continue_rcu - continue iteration over list of given type
274 * @pos: the type * to use as a loop cursor.
275 * @head: the head for your list.
276 * @member: the name of the list_struct within the struct.
277 *
278 * Continue to iterate over list of given type, continuing after
279 * the current position.
280 */
281#define list_for_each_entry_continue_rcu(pos, head, member) \
282 for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \
283 prefetch(pos->member.next), &pos->member != (head); \
284 pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
285
286/**
Franck Bui-Huu82524742008-05-12 21:21:05 +0200287 * hlist_del_rcu - deletes entry from hash list without re-initialization
288 * @n: the element to delete from the hash list.
289 *
290 * Note: list_unhashed() on entry does not return true after this,
291 * the entry is in an undefined state. It is useful for RCU based
292 * lockfree traversal.
293 *
294 * In particular, it means that we can not poison the forward
295 * pointers that may still be used for walking the hash list.
296 *
297 * The caller must take whatever precautions are necessary
298 * (such as holding appropriate locks) to avoid racing
299 * with another list-mutation primitive, such as hlist_add_head_rcu()
300 * or hlist_del_rcu(), running on this same list.
301 * However, it is perfectly legal to run concurrently with
302 * the _rcu list-traversal primitives, such as
303 * hlist_for_each_entry().
304 */
305static inline void hlist_del_rcu(struct hlist_node *n)
306{
307 __hlist_del(n);
308 n->pprev = LIST_POISON2;
309}
310
311/**
312 * hlist_replace_rcu - replace old entry by new one
313 * @old : the element to be replaced
314 * @new : the new element to insert
315 *
316 * The @old entry will be replaced with the @new entry atomically.
317 */
318static inline void hlist_replace_rcu(struct hlist_node *old,
319 struct hlist_node *new)
320{
321 struct hlist_node *next = old->next;
322
323 new->next = next;
324 new->pprev = old->pprev;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100325 rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200326 if (next)
327 new->next->pprev = &new->next;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200328 old->pprev = LIST_POISON2;
329}
330
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100331/*
332 * return the first or the next element in an RCU protected hlist
333 */
334#define hlist_first_rcu(head) (*((struct hlist_node __rcu **)(&(head)->first)))
335#define hlist_next_rcu(node) (*((struct hlist_node __rcu **)(&(node)->next)))
336#define hlist_pprev_rcu(node) (*((struct hlist_node __rcu **)((node)->pprev)))
337
Franck Bui-Huu82524742008-05-12 21:21:05 +0200338/**
339 * hlist_add_head_rcu
340 * @n: the element to add to the hash list.
341 * @h: the list to add to.
342 *
343 * Description:
344 * Adds the specified element to the specified hlist,
345 * while permitting racing traversals.
346 *
347 * The caller must take whatever precautions are necessary
348 * (such as holding appropriate locks) to avoid racing
349 * with another list-mutation primitive, such as hlist_add_head_rcu()
350 * or hlist_del_rcu(), running on this same list.
351 * However, it is perfectly legal to run concurrently with
352 * the _rcu list-traversal primitives, such as
353 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
354 * problems on Alpha CPUs. Regardless of the type of CPU, the
355 * list-traversal primitive must be guarded by rcu_read_lock().
356 */
357static inline void hlist_add_head_rcu(struct hlist_node *n,
358 struct hlist_head *h)
359{
360 struct hlist_node *first = h->first;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200361
Franck Bui-Huu82524742008-05-12 21:21:05 +0200362 n->next = first;
363 n->pprev = &h->first;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100364 rcu_assign_pointer(hlist_first_rcu(h), n);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200365 if (first)
366 first->pprev = &n->next;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200367}
368
369/**
370 * hlist_add_before_rcu
371 * @n: the new element to add to the hash list.
372 * @next: the existing element to add the new element before.
373 *
374 * Description:
375 * Adds the specified element to the specified hlist
376 * before the specified node while permitting racing traversals.
377 *
378 * The caller must take whatever precautions are necessary
379 * (such as holding appropriate locks) to avoid racing
380 * with another list-mutation primitive, such as hlist_add_head_rcu()
381 * or hlist_del_rcu(), running on this same list.
382 * However, it is perfectly legal to run concurrently with
383 * the _rcu list-traversal primitives, such as
384 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
385 * problems on Alpha CPUs.
386 */
387static inline void hlist_add_before_rcu(struct hlist_node *n,
388 struct hlist_node *next)
389{
390 n->pprev = next->pprev;
391 n->next = next;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100392 rcu_assign_pointer(hlist_pprev_rcu(n), n);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200393 next->pprev = &n->next;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200394}
395
396/**
397 * hlist_add_after_rcu
398 * @prev: the existing element to add the new element after.
399 * @n: the new element to add to the hash list.
400 *
401 * Description:
402 * Adds the specified element to the specified hlist
403 * after the specified node while permitting racing traversals.
404 *
405 * The caller must take whatever precautions are necessary
406 * (such as holding appropriate locks) to avoid racing
407 * with another list-mutation primitive, such as hlist_add_head_rcu()
408 * or hlist_del_rcu(), running on this same list.
409 * However, it is perfectly legal to run concurrently with
410 * the _rcu list-traversal primitives, such as
411 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
412 * problems on Alpha CPUs.
413 */
414static inline void hlist_add_after_rcu(struct hlist_node *prev,
415 struct hlist_node *n)
416{
417 n->next = prev->next;
418 n->pprev = &prev->next;
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100419 rcu_assign_pointer(hlist_next_rcu(prev), n);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200420 if (n->next)
421 n->next->pprev = &n->next;
422}
423
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100424#define __hlist_for_each_rcu(pos, head) \
425 for (pos = rcu_dereference(hlist_first_rcu(head)); \
426 pos && ({ prefetch(pos->next); 1; }); \
427 pos = rcu_dereference(hlist_next_rcu(pos)))
stephen hemminger1cc52322010-02-22 07:57:17 +0000428
Franck Bui-Huu82524742008-05-12 21:21:05 +0200429/**
430 * hlist_for_each_entry_rcu - iterate over rcu list of given type
431 * @tpos: the type * to use as a loop cursor.
432 * @pos: the &struct hlist_node to use as a loop cursor.
433 * @head: the head for your list.
434 * @member: the name of the hlist_node within the struct.
435 *
436 * This list-traversal primitive may safely run concurrently with
437 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
438 * as long as the traversal is guarded by rcu_read_lock().
439 */
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100440#define hlist_for_each_entry_rcu(tpos, pos, head, member) \
441 for (pos = rcu_dereference_raw(hlist_first_rcu(head)); \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200442 pos && ({ prefetch(pos->next); 1; }) && \
Franck Bui-Huu82524742008-05-12 21:21:05 +0200443 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100444 pos = rcu_dereference_raw(hlist_next_rcu(pos)))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200445
stephen hemminger5c578ae2010-03-17 20:31:11 +0000446/**
Eric Dumazet4f70ecc2010-05-03 10:50:14 +0000447 * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
448 * @tpos: the type * to use as a loop cursor.
449 * @pos: the &struct hlist_node to use as a loop cursor.
450 * @head: the head for your list.
451 * @member: the name of the hlist_node within the struct.
452 *
453 * This list-traversal primitive may safely run concurrently with
454 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
455 * as long as the traversal is guarded by rcu_read_lock().
456 */
457#define hlist_for_each_entry_rcu_bh(tpos, pos, head, member) \
458 for (pos = rcu_dereference_bh((head)->first); \
459 pos && ({ prefetch(pos->next); 1; }) && \
460 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
461 pos = rcu_dereference_bh(pos->next))
462
463/**
stephen hemminger5c578ae2010-03-17 20:31:11 +0000464 * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point
465 * @tpos: the type * to use as a loop cursor.
466 * @pos: the &struct hlist_node to use as a loop cursor.
467 * @member: the name of the hlist_node within the struct.
468 */
469#define hlist_for_each_entry_continue_rcu(tpos, pos, member) \
470 for (pos = rcu_dereference((pos)->next); \
471 pos && ({ prefetch(pos->next); 1; }) && \
472 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
473 pos = rcu_dereference(pos->next))
474
Eric Dumazet4f70ecc2010-05-03 10:50:14 +0000475/**
476 * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point
477 * @tpos: the type * to use as a loop cursor.
478 * @pos: the &struct hlist_node to use as a loop cursor.
479 * @member: the name of the hlist_node within the struct.
480 */
481#define hlist_for_each_entry_continue_rcu_bh(tpos, pos, member) \
482 for (pos = rcu_dereference_bh((pos)->next); \
483 pos && ({ prefetch(pos->next); 1; }) && \
484 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
485 pos = rcu_dereference_bh(pos->next))
486
stephen hemminger5c578ae2010-03-17 20:31:11 +0000487
Franck Bui-Huu82524742008-05-12 21:21:05 +0200488#endif /* __KERNEL__ */
489#endif