blob: cdc96559e5aed675a02b1a48df8fa9e1773c89ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
4#ifdef __KERNEL__
5
6#include <linux/stddef.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -07007#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/prefetch.h>
9#include <asm/system.h>
10
11/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Simple doubly linked list implementation.
13 *
14 * Some of the internal functions ("__xxx") are useful when
15 * manipulating whole lists rather than single entries, as
16 * sometimes we already know the next/prev entries and we can
17 * generate better code by using them directly rather than
18 * using the generic single-entry routines.
19 */
20
21struct list_head {
22 struct list_head *next, *prev;
23};
24
25#define LIST_HEAD_INIT(name) { &(name), &(name) }
26
27#define LIST_HEAD(name) \
28 struct list_head name = LIST_HEAD_INIT(name)
29
Zach Brown490d6ab2006-02-03 03:03:56 -080030static inline void INIT_LIST_HEAD(struct list_head *list)
31{
32 list->next = list;
33 list->prev = list;
34}
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
37 * Insert a new entry between two known consecutive entries.
38 *
39 * This is only for internal list manipulation where we know
40 * the prev/next entries already!
41 */
Dave Jones199a9af2006-09-29 01:59:00 -070042#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static inline void __list_add(struct list_head *new,
44 struct list_head *prev,
45 struct list_head *next)
46{
47 next->prev = new;
48 new->next = next;
49 new->prev = prev;
50 prev->next = new;
51}
Dave Jones199a9af2006-09-29 01:59:00 -070052#else
53extern void __list_add(struct list_head *new,
54 struct list_head *prev,
55 struct list_head *next);
56#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/**
59 * list_add - add a new entry
60 * @new: new entry to be added
61 * @head: list head to add it after
62 *
63 * Insert a new entry after the specified head.
64 * This is good for implementing stacks.
65 */
Dave Jones199a9af2006-09-29 01:59:00 -070066#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static inline void list_add(struct list_head *new, struct list_head *head)
68{
69 __list_add(new, head, head->next);
70}
Dave Jones199a9af2006-09-29 01:59:00 -070071#else
72extern void list_add(struct list_head *new, struct list_head *head);
73#endif
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/**
77 * list_add_tail - add a new entry
78 * @new: new entry to be added
79 * @head: list head to add it before
80 *
81 * Insert a new entry before the specified head.
82 * This is useful for implementing queues.
83 */
84static inline void list_add_tail(struct list_head *new, struct list_head *head)
85{
86 __list_add(new, head->prev, head);
87}
88
89/*
90 * Insert a new entry between two known consecutive entries.
91 *
92 * This is only for internal list manipulation where we know
93 * the prev/next entries already!
94 */
95static inline void __list_add_rcu(struct list_head * new,
96 struct list_head * prev, struct list_head * next)
97{
98 new->next = next;
99 new->prev = prev;
100 smp_wmb();
101 next->prev = new;
102 prev->next = new;
103}
104
105/**
106 * list_add_rcu - add a new entry to rcu-protected list
107 * @new: new entry to be added
108 * @head: list head to add it after
109 *
110 * Insert a new entry after the specified head.
111 * This is good for implementing stacks.
112 *
113 * The caller must take whatever precautions are necessary
114 * (such as holding appropriate locks) to avoid racing
115 * with another list-mutation primitive, such as list_add_rcu()
116 * or list_del_rcu(), running on this same list.
117 * However, it is perfectly legal to run concurrently with
118 * the _rcu list-traversal primitives, such as
119 * list_for_each_entry_rcu().
120 */
121static inline void list_add_rcu(struct list_head *new, struct list_head *head)
122{
123 __list_add_rcu(new, head, head->next);
124}
125
126/**
127 * list_add_tail_rcu - add a new entry to rcu-protected list
128 * @new: new entry to be added
129 * @head: list head to add it before
130 *
131 * Insert a new entry before the specified head.
132 * This is useful for implementing queues.
133 *
134 * The caller must take whatever precautions are necessary
135 * (such as holding appropriate locks) to avoid racing
136 * with another list-mutation primitive, such as list_add_tail_rcu()
137 * or list_del_rcu(), running on this same list.
138 * However, it is perfectly legal to run concurrently with
139 * the _rcu list-traversal primitives, such as
140 * list_for_each_entry_rcu().
141 */
142static inline void list_add_tail_rcu(struct list_head *new,
143 struct list_head *head)
144{
145 __list_add_rcu(new, head->prev, head);
146}
147
148/*
149 * Delete a list entry by making the prev/next entries
150 * point to each other.
151 *
152 * This is only for internal list manipulation where we know
153 * the prev/next entries already!
154 */
155static inline void __list_del(struct list_head * prev, struct list_head * next)
156{
157 next->prev = prev;
158 prev->next = next;
159}
160
161/**
162 * list_del - deletes entry from list.
163 * @entry: the element to delete from the list.
164 * Note: list_empty on entry does not return true after this, the entry is
165 * in an undefined state.
166 */
Dave Jones199a9af2006-09-29 01:59:00 -0700167#ifndef CONFIG_DEBUG_LIST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static inline void list_del(struct list_head *entry)
169{
170 __list_del(entry->prev, entry->next);
171 entry->next = LIST_POISON1;
172 entry->prev = LIST_POISON2;
173}
Dave Jones199a9af2006-09-29 01:59:00 -0700174#else
175extern void list_del(struct list_head *entry);
176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/**
179 * list_del_rcu - deletes entry from list without re-initialization
180 * @entry: the element to delete from the list.
181 *
182 * Note: list_empty on entry does not return true after this,
183 * the entry is in an undefined state. It is useful for RCU based
184 * lockfree traversal.
185 *
186 * In particular, it means that we can not poison the forward
187 * pointers that may still be used for walking the list.
188 *
189 * The caller must take whatever precautions are necessary
190 * (such as holding appropriate locks) to avoid racing
191 * with another list-mutation primitive, such as list_del_rcu()
192 * or list_add_rcu(), running on this same list.
193 * However, it is perfectly legal to run concurrently with
194 * the _rcu list-traversal primitives, such as
195 * list_for_each_entry_rcu().
196 *
197 * Note that the caller is not permitted to immediately free
Paul E. McKenneyb2b18662005-06-25 14:55:38 -0700198 * the newly deleted entry. Instead, either synchronize_rcu()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * or call_rcu() must be used to defer freeing until an RCU
200 * grace period has elapsed.
201 */
202static inline void list_del_rcu(struct list_head *entry)
203{
204 __list_del(entry->prev, entry->next);
205 entry->prev = LIST_POISON2;
206}
207
Oleg Nesterov54e73772006-06-23 02:05:54 -0700208/**
209 * list_replace - replace old entry by new one
210 * @old : the element to be replaced
211 * @new : the new element to insert
212 * Note: if 'old' was empty, it will be overwritten.
213 */
214static inline void list_replace(struct list_head *old,
215 struct list_head *new)
216{
217 new->next = old->next;
218 new->next->prev = new;
219 new->prev = old->prev;
220 new->prev->next = new;
221}
222
223static inline void list_replace_init(struct list_head *old,
224 struct list_head *new)
225{
226 list_replace(old, new);
227 INIT_LIST_HEAD(old);
228}
229
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800230/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * list_replace_rcu - replace old entry by new one
232 * @old : the element to be replaced
233 * @new : the new element to insert
234 *
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800235 * The @old entry will be replaced with the @new entry atomically.
236 * Note: @old should not be empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Ingo Molnarb88cb422005-12-12 00:37:11 -0800238static inline void list_replace_rcu(struct list_head *old,
239 struct list_head *new)
240{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 new->next = old->next;
242 new->prev = old->prev;
243 smp_wmb();
244 new->next->prev = new;
245 new->prev->next = new;
Ingo Molnarb88cb422005-12-12 00:37:11 -0800246 old->prev = LIST_POISON2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/**
250 * list_del_init - deletes entry from list and reinitialize it.
251 * @entry: the element to delete from the list.
252 */
253static inline void list_del_init(struct list_head *entry)
254{
255 __list_del(entry->prev, entry->next);
256 INIT_LIST_HEAD(entry);
257}
258
259/**
260 * list_move - delete from one list and add as another's head
261 * @list: the entry to move
262 * @head: the head that will precede our entry
263 */
264static inline void list_move(struct list_head *list, struct list_head *head)
265{
266 __list_del(list->prev, list->next);
267 list_add(list, head);
268}
269
270/**
271 * list_move_tail - delete from one list and add as another's tail
272 * @list: the entry to move
273 * @head: the head that will follow our entry
274 */
275static inline void list_move_tail(struct list_head *list,
276 struct list_head *head)
277{
278 __list_del(list->prev, list->next);
279 list_add_tail(list, head);
280}
281
282/**
Shailabh Nagare8f4d972006-07-14 00:24:35 -0700283 * list_is_last - tests whether @list is the last entry in list @head
284 * @list: the entry to test
285 * @head: the head of the list
286 */
287static inline int list_is_last(const struct list_head *list,
288 const struct list_head *head)
289{
290 return list->next == head;
291}
292
293/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * list_empty - tests whether a list is empty
295 * @head: the list to test.
296 */
297static inline int list_empty(const struct list_head *head)
298{
299 return head->next == head;
300}
301
302/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700303 * list_empty_careful - tests whether a list is empty and not being modified
304 * @head: the list to test
305 *
306 * Description:
307 * tests whether a list is empty _and_ checks that no other CPU might be
308 * in the process of modifying either member (next or prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
310 * NOTE: using list_empty_careful() without synchronization
311 * can only be safe if the only activity that can happen
312 * to the list entry is list_del_init(). Eg. it cannot be used
313 * if another CPU could re-list_add() it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315static inline int list_empty_careful(const struct list_head *head)
316{
317 struct list_head *next = head->next;
318 return (next == head) && (next == head->prev);
319}
320
321static inline void __list_splice(struct list_head *list,
322 struct list_head *head)
323{
324 struct list_head *first = list->next;
325 struct list_head *last = list->prev;
326 struct list_head *at = head->next;
327
328 first->prev = head;
329 head->next = first;
330
331 last->next = at;
332 at->prev = last;
333}
334
335/**
336 * list_splice - join two lists
337 * @list: the new list to add.
338 * @head: the place to add it in the first list.
339 */
340static inline void list_splice(struct list_head *list, struct list_head *head)
341{
342 if (!list_empty(list))
343 __list_splice(list, head);
344}
345
346/**
347 * list_splice_init - join two lists and reinitialise the emptied list.
348 * @list: the new list to add.
349 * @head: the place to add it in the first list.
350 *
351 * The list at @list is reinitialised
352 */
353static inline void list_splice_init(struct list_head *list,
354 struct list_head *head)
355{
356 if (!list_empty(list)) {
357 __list_splice(list, head);
358 INIT_LIST_HEAD(list);
359 }
360}
361
362/**
Corey Minyard3678d622007-02-10 01:45:42 -0800363 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
364 * @list: the RCU-protected list to splice
365 * @head: the place in the list to splice the first list into
366 * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
367 *
368 * @head can be RCU-read traversed concurrently with this function.
369 *
370 * Note that this function blocks.
371 *
372 * Important note: the caller must take whatever action is necessary to
373 * prevent any other updates to @head. In principle, it is possible
374 * to modify the list as soon as sync() begins execution.
375 * If this sort of thing becomes necessary, an alternative version
376 * based on call_rcu() could be created. But only if -really-
377 * needed -- there is no shortage of RCU API members.
378 */
379static inline void list_splice_init_rcu(struct list_head *list,
380 struct list_head *head,
381 void (*sync)(void))
382{
383 struct list_head *first = list->next;
384 struct list_head *last = list->prev;
385 struct list_head *at = head->next;
386
387 if (list_empty(head))
388 return;
389
390 /* "first" and "last" tracking list, so initialize it. */
391
392 INIT_LIST_HEAD(list);
393
394 /*
395 * At this point, the list body still points to the source list.
396 * Wait for any readers to finish using the list before splicing
397 * the list body into the new list. Any new readers will see
398 * an empty list.
399 */
400
401 sync();
402
403 /*
404 * Readers are finished with the source list, so perform splice.
405 * The order is important if the new list is global and accessible
406 * to concurrent RCU readers. Note that RCU readers are not
407 * permitted to traverse the prev pointers without excluding
408 * this function.
409 */
410
411 last->next = at;
412 smp_wmb();
413 head->next = first;
414 first->prev = head;
415 at->prev = last;
416}
417
418/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * list_entry - get the struct for this entry
420 * @ptr: the &struct list_head pointer.
421 * @type: the type of the struct this is embedded in.
422 * @member: the name of the list_struct within the struct.
423 */
424#define list_entry(ptr, type, member) \
425 container_of(ptr, type, member)
426
427/**
428 * list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700429 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * @head: the head for your list.
431 */
432#define list_for_each(pos, head) \
433 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
434 pos = pos->next)
435
436/**
437 * __list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700438 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * @head: the head for your list.
440 *
441 * This variant differs from list_for_each() in that it's the
442 * simplest possible list iteration code, no prefetching is done.
443 * Use this for code that knows the list to be very short (empty
444 * or 1 entry) most of the time.
445 */
446#define __list_for_each(pos, head) \
447 for (pos = (head)->next; pos != (head); pos = pos->next)
448
449/**
450 * list_for_each_prev - iterate over a list backwards
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700451 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @head: the head for your list.
453 */
454#define list_for_each_prev(pos, head) \
455 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
456 pos = pos->prev)
457
458/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700459 * list_for_each_safe - iterate over a list safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700460 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * @n: another &struct list_head to use as temporary storage
462 * @head: the head for your list.
463 */
464#define list_for_each_safe(pos, n, head) \
465 for (pos = (head)->next, n = pos->next; pos != (head); \
466 pos = n, n = pos->next)
467
468/**
469 * list_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700470 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * @head: the head for your list.
472 * @member: the name of the list_struct within the struct.
473 */
474#define list_for_each_entry(pos, head, member) \
475 for (pos = list_entry((head)->next, typeof(*pos), member); \
476 prefetch(pos->member.next), &pos->member != (head); \
477 pos = list_entry(pos->member.next, typeof(*pos), member))
478
479/**
480 * list_for_each_entry_reverse - iterate backwards over list of given type.
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700481 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * @head: the head for your list.
483 * @member: the name of the list_struct within the struct.
484 */
485#define list_for_each_entry_reverse(pos, head, member) \
486 for (pos = list_entry((head)->prev, typeof(*pos), member); \
487 prefetch(pos->member.prev), &pos->member != (head); \
488 pos = list_entry(pos->member.prev, typeof(*pos), member))
489
490/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700491 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * @pos: the type * to use as a start point
493 * @head: the head of the list
494 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700495 *
496 * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
498#define list_prepare_entry(pos, head, member) \
499 ((pos) ? : list_entry(head, typeof(*pos), member))
500
501/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700502 * list_for_each_entry_continue - continue iteration over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700503 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * @head: the head for your list.
505 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700506 *
507 * Continue to iterate over list of given type, continuing after
508 * the current position.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
510#define list_for_each_entry_continue(pos, head, member) \
511 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
512 prefetch(pos->member.next), &pos->member != (head); \
513 pos = list_entry(pos->member.next, typeof(*pos), member))
514
515/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700516 * list_for_each_entry_from - iterate over list of given type from the current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700517 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800518 * @head: the head for your list.
519 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700520 *
521 * Iterate over list of given type, continuing from current position.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800522 */
523#define list_for_each_entry_from(pos, head, member) \
524 for (; prefetch(pos->member.next), &pos->member != (head); \
525 pos = list_entry(pos->member.next, typeof(*pos), member))
526
527/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700529 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * @n: another type * to use as temporary storage
531 * @head: the head for your list.
532 * @member: the name of the list_struct within the struct.
533 */
534#define list_for_each_entry_safe(pos, n, head, member) \
535 for (pos = list_entry((head)->next, typeof(*pos), member), \
536 n = list_entry(pos->member.next, typeof(*pos), member); \
537 &pos->member != (head); \
538 pos = n, n = list_entry(n->member.next, typeof(*n), member))
539
540/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700541 * list_for_each_entry_safe_continue
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700542 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700543 * @n: another type * to use as temporary storage
544 * @head: the head for your list.
545 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700546 *
547 * Iterate over list of given type, continuing after current point,
548 * safe against removal of list entry.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700549 */
550#define list_for_each_entry_safe_continue(pos, n, head, member) \
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300551 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
552 n = list_entry(pos->member.next, typeof(*pos), member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700553 &pos->member != (head); \
554 pos = n, n = list_entry(n->member.next, typeof(*n), member))
555
556/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700557 * list_for_each_entry_safe_from
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700558 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800559 * @n: another type * to use as temporary storage
560 * @head: the head for your list.
561 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700562 *
563 * Iterate over list of given type from current point, safe against
564 * removal of list entry.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800565 */
566#define list_for_each_entry_safe_from(pos, n, head, member) \
567 for (n = list_entry(pos->member.next, typeof(*pos), member); \
568 &pos->member != (head); \
569 pos = n, n = list_entry(n->member.next, typeof(*n), member))
570
571/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700572 * list_for_each_entry_safe_reverse
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700573 * @pos: the type * to use as a loop cursor.
David Howells0ad42352006-01-09 20:51:31 -0800574 * @n: another type * to use as temporary storage
575 * @head: the head for your list.
576 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700577 *
578 * Iterate backwards over list of given type, safe against removal
579 * of list entry.
David Howells0ad42352006-01-09 20:51:31 -0800580 */
581#define list_for_each_entry_safe_reverse(pos, n, head, member) \
582 for (pos = list_entry((head)->prev, typeof(*pos), member), \
583 n = list_entry(pos->member.prev, typeof(*pos), member); \
584 &pos->member != (head); \
585 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
586
587/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * list_for_each_rcu - iterate over an rcu-protected list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700589 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * @head: the head for your list.
591 *
592 * This list-traversal primitive may safely run concurrently with
593 * the _rcu list-mutation primitives such as list_add_rcu()
594 * as long as the traversal is guarded by rcu_read_lock().
595 */
596#define list_for_each_rcu(pos, head) \
Herbert Xub24d18a2005-10-16 20:29:20 -0700597 for (pos = (head)->next; \
598 prefetch(rcu_dereference(pos)->next), pos != (head); \
599 pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601#define __list_for_each_rcu(pos, head) \
Herbert Xub24d18a2005-10-16 20:29:20 -0700602 for (pos = (head)->next; \
603 rcu_dereference(pos) != (head); \
604 pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700607 * list_for_each_safe_rcu
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700608 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * @n: another &struct list_head to use as temporary storage
610 * @head: the head for your list.
611 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700612 * Iterate over an rcu-protected list, safe against removal of list entry.
613 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * This list-traversal primitive may safely run concurrently with
615 * the _rcu list-mutation primitives such as list_add_rcu()
616 * as long as the traversal is guarded by rcu_read_lock().
617 */
618#define list_for_each_safe_rcu(pos, n, head) \
Herbert Xub24d18a2005-10-16 20:29:20 -0700619 for (pos = (head)->next; \
620 n = rcu_dereference(pos)->next, pos != (head); \
621 pos = n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623/**
624 * list_for_each_entry_rcu - iterate over rcu list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700625 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * @head: the head for your list.
627 * @member: the name of the list_struct within the struct.
628 *
629 * This list-traversal primitive may safely run concurrently with
630 * the _rcu list-mutation primitives such as list_add_rcu()
631 * as long as the traversal is guarded by rcu_read_lock().
632 */
Herbert Xub24d18a2005-10-16 20:29:20 -0700633#define list_for_each_entry_rcu(pos, head, member) \
634 for (pos = list_entry((head)->next, typeof(*pos), member); \
635 prefetch(rcu_dereference(pos)->member.next), \
636 &pos->member != (head); \
637 pos = list_entry(pos->member.next, typeof(*pos), member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639
640/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700641 * list_for_each_continue_rcu
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700642 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * @head: the head for your list.
644 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700645 * Iterate over an rcu-protected list, continuing after current point.
646 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * This list-traversal primitive may safely run concurrently with
648 * the _rcu list-mutation primitives such as list_add_rcu()
649 * as long as the traversal is guarded by rcu_read_lock().
650 */
651#define list_for_each_continue_rcu(pos, head) \
Herbert Xub24d18a2005-10-16 20:29:20 -0700652 for ((pos) = (pos)->next; \
653 prefetch(rcu_dereference((pos))->next), (pos) != (head); \
654 (pos) = (pos)->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656/*
657 * Double linked lists with a single pointer list head.
658 * Mostly useful for hash tables where the two pointer list head is
659 * too wasteful.
660 * You lose the ability to access the tail in O(1).
661 */
662
663struct hlist_head {
664 struct hlist_node *first;
665};
666
667struct hlist_node {
668 struct hlist_node *next, **pprev;
669};
670
671#define HLIST_HEAD_INIT { .first = NULL }
672#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
673#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800674static inline void INIT_HLIST_NODE(struct hlist_node *h)
675{
676 h->next = NULL;
677 h->pprev = NULL;
678}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680static inline int hlist_unhashed(const struct hlist_node *h)
681{
682 return !h->pprev;
683}
684
685static inline int hlist_empty(const struct hlist_head *h)
686{
687 return !h->first;
688}
689
690static inline void __hlist_del(struct hlist_node *n)
691{
692 struct hlist_node *next = n->next;
693 struct hlist_node **pprev = n->pprev;
694 *pprev = next;
695 if (next)
696 next->pprev = pprev;
697}
698
699static inline void hlist_del(struct hlist_node *n)
700{
701 __hlist_del(n);
702 n->next = LIST_POISON1;
703 n->pprev = LIST_POISON2;
704}
705
706/**
707 * hlist_del_rcu - deletes entry from hash list without re-initialization
708 * @n: the element to delete from the hash list.
709 *
710 * Note: list_unhashed() on entry does not return true after this,
711 * the entry is in an undefined state. It is useful for RCU based
712 * lockfree traversal.
713 *
714 * In particular, it means that we can not poison the forward
715 * pointers that may still be used for walking the hash list.
716 *
717 * The caller must take whatever precautions are necessary
718 * (such as holding appropriate locks) to avoid racing
719 * with another list-mutation primitive, such as hlist_add_head_rcu()
720 * or hlist_del_rcu(), running on this same list.
721 * However, it is perfectly legal to run concurrently with
722 * the _rcu list-traversal primitives, such as
723 * hlist_for_each_entry().
724 */
725static inline void hlist_del_rcu(struct hlist_node *n)
726{
727 __hlist_del(n);
728 n->pprev = LIST_POISON2;
729}
730
731static inline void hlist_del_init(struct hlist_node *n)
732{
Akinobu Mitada753be2006-04-28 15:21:23 -0700733 if (!hlist_unhashed(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 __hlist_del(n);
735 INIT_HLIST_NODE(n);
736 }
737}
738
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800739/**
Ingo Molnarb88cb422005-12-12 00:37:11 -0800740 * hlist_replace_rcu - replace old entry by new one
741 * @old : the element to be replaced
742 * @new : the new element to insert
743 *
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800744 * The @old entry will be replaced with the @new entry atomically.
Ingo Molnarb88cb422005-12-12 00:37:11 -0800745 */
746static inline void hlist_replace_rcu(struct hlist_node *old,
747 struct hlist_node *new)
748{
749 struct hlist_node *next = old->next;
750
751 new->next = next;
752 new->pprev = old->pprev;
753 smp_wmb();
754 if (next)
755 new->next->pprev = &new->next;
756 *new->pprev = new;
757 old->pprev = LIST_POISON2;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
761{
762 struct hlist_node *first = h->first;
763 n->next = first;
764 if (first)
765 first->pprev = &n->next;
766 h->first = n;
767 n->pprev = &h->first;
768}
769
770
771/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700772 * hlist_add_head_rcu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * @n: the element to add to the hash list.
774 * @h: the list to add to.
775 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700776 * Description:
777 * Adds the specified element to the specified hlist,
778 * while permitting racing traversals.
779 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * The caller must take whatever precautions are necessary
781 * (such as holding appropriate locks) to avoid racing
782 * with another list-mutation primitive, such as hlist_add_head_rcu()
783 * or hlist_del_rcu(), running on this same list.
784 * However, it is perfectly legal to run concurrently with
785 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800786 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * problems on Alpha CPUs. Regardless of the type of CPU, the
788 * list-traversal primitive must be guarded by rcu_read_lock().
789 */
790static inline void hlist_add_head_rcu(struct hlist_node *n,
791 struct hlist_head *h)
792{
793 struct hlist_node *first = h->first;
794 n->next = first;
795 n->pprev = &h->first;
796 smp_wmb();
797 if (first)
798 first->pprev = &n->next;
799 h->first = n;
800}
801
802/* next must be != NULL */
803static inline void hlist_add_before(struct hlist_node *n,
804 struct hlist_node *next)
805{
806 n->pprev = next->pprev;
807 n->next = next;
808 next->pprev = &n->next;
809 *(n->pprev) = n;
810}
811
812static inline void hlist_add_after(struct hlist_node *n,
813 struct hlist_node *next)
814{
815 next->next = n->next;
816 n->next = next;
817 next->pprev = &n->next;
818
819 if(next->next)
820 next->next->pprev = &next->next;
821}
822
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700823/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700824 * hlist_add_before_rcu
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700825 * @n: the new element to add to the hash list.
826 * @next: the existing element to add the new element before.
827 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700828 * Description:
829 * Adds the specified element to the specified hlist
830 * before the specified node while permitting racing traversals.
831 *
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700832 * The caller must take whatever precautions are necessary
833 * (such as holding appropriate locks) to avoid racing
834 * with another list-mutation primitive, such as hlist_add_head_rcu()
835 * or hlist_del_rcu(), running on this same list.
836 * However, it is perfectly legal to run concurrently with
837 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800838 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700839 * problems on Alpha CPUs.
840 */
Robert Olssone5b43762005-08-25 13:01:03 -0700841static inline void hlist_add_before_rcu(struct hlist_node *n,
842 struct hlist_node *next)
843{
844 n->pprev = next->pprev;
845 n->next = next;
846 smp_wmb();
847 next->pprev = &n->next;
848 *(n->pprev) = n;
849}
850
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700851/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700852 * hlist_add_after_rcu
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700853 * @prev: the existing element to add the new element after.
854 * @n: the new element to add to the hash list.
855 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700856 * Description:
857 * Adds the specified element to the specified hlist
858 * after the specified node while permitting racing traversals.
859 *
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700860 * The caller must take whatever precautions are necessary
861 * (such as holding appropriate locks) to avoid racing
862 * with another list-mutation primitive, such as hlist_add_head_rcu()
863 * or hlist_del_rcu(), running on this same list.
864 * However, it is perfectly legal to run concurrently with
865 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800866 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700867 * problems on Alpha CPUs.
868 */
Robert Olssone5b43762005-08-25 13:01:03 -0700869static inline void hlist_add_after_rcu(struct hlist_node *prev,
870 struct hlist_node *n)
871{
872 n->next = prev->next;
873 n->pprev = &prev->next;
874 smp_wmb();
875 prev->next = n;
876 if (n->next)
877 n->next->pprev = &n->next;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
881
882#define hlist_for_each(pos, head) \
883 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
884 pos = pos->next)
885
886#define hlist_for_each_safe(pos, n, head) \
887 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
888 pos = n)
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/**
891 * hlist_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700892 * @tpos: the type * to use as a loop cursor.
893 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * @head: the head for your list.
895 * @member: the name of the hlist_node within the struct.
896 */
897#define hlist_for_each_entry(tpos, pos, head, member) \
898 for (pos = (head)->first; \
899 pos && ({ prefetch(pos->next); 1;}) && \
900 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
901 pos = pos->next)
902
903/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700904 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700905 * @tpos: the type * to use as a loop cursor.
906 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * @member: the name of the hlist_node within the struct.
908 */
909#define hlist_for_each_entry_continue(tpos, pos, member) \
910 for (pos = (pos)->next; \
911 pos && ({ prefetch(pos->next); 1;}) && \
912 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
913 pos = pos->next)
914
915/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700916 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700917 * @tpos: the type * to use as a loop cursor.
918 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * @member: the name of the hlist_node within the struct.
920 */
921#define hlist_for_each_entry_from(tpos, pos, member) \
922 for (; pos && ({ prefetch(pos->next); 1;}) && \
923 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
924 pos = pos->next)
925
926/**
927 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700928 * @tpos: the type * to use as a loop cursor.
929 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * @n: another &struct hlist_node to use as temporary storage
931 * @head: the head for your list.
932 * @member: the name of the hlist_node within the struct.
933 */
934#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
935 for (pos = (head)->first; \
936 pos && ({ n = pos->next; 1; }) && \
937 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
938 pos = n)
939
940/**
941 * hlist_for_each_entry_rcu - iterate over rcu list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700942 * @tpos: the type * to use as a loop cursor.
943 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * @head: the head for your list.
945 * @member: the name of the hlist_node within the struct.
946 *
947 * This list-traversal primitive may safely run concurrently with
Paul E. McKenneye1ba0da2005-04-16 15:25:51 -0700948 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * as long as the traversal is guarded by rcu_read_lock().
950 */
951#define hlist_for_each_entry_rcu(tpos, pos, head, member) \
952 for (pos = (head)->first; \
Herbert Xub24d18a2005-10-16 20:29:20 -0700953 rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Herbert Xub24d18a2005-10-16 20:29:20 -0700955 pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957#else
958#warning "don't include kernel headers in userspace"
959#endif /* __KERNEL__ */
960#endif