blob: 7627508f1b741aed30ef52ce76c40d2cc8a0646a [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.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800164 * Note: list_empty() on entry does not return true after this, the entry is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * 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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800182 * Note: list_empty() on entry does not return true after this,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * 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
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800212 *
213 * If @old was empty, it will be overwritten.
Oleg Nesterov54e73772006-06-23 02:05:54 -0700214 */
215static inline void list_replace(struct list_head *old,
216 struct list_head *new)
217{
218 new->next = old->next;
219 new->next->prev = new;
220 new->prev = old->prev;
221 new->prev->next = new;
222}
223
224static inline void list_replace_init(struct list_head *old,
225 struct list_head *new)
226{
227 list_replace(old, new);
228 INIT_LIST_HEAD(old);
229}
230
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800231/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * list_replace_rcu - replace old entry by new one
233 * @old : the element to be replaced
234 * @new : the new element to insert
235 *
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800236 * The @old entry will be replaced with the @new entry atomically.
237 * Note: @old should not be empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Ingo Molnarb88cb422005-12-12 00:37:11 -0800239static inline void list_replace_rcu(struct list_head *old,
240 struct list_head *new)
241{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 new->next = old->next;
243 new->prev = old->prev;
244 smp_wmb();
245 new->next->prev = new;
246 new->prev->next = new;
Ingo Molnarb88cb422005-12-12 00:37:11 -0800247 old->prev = LIST_POISON2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250/**
251 * list_del_init - deletes entry from list and reinitialize it.
252 * @entry: the element to delete from the list.
253 */
254static inline void list_del_init(struct list_head *entry)
255{
256 __list_del(entry->prev, entry->next);
257 INIT_LIST_HEAD(entry);
258}
259
260/**
261 * list_move - delete from one list and add as another's head
262 * @list: the entry to move
263 * @head: the head that will precede our entry
264 */
265static inline void list_move(struct list_head *list, struct list_head *head)
266{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700267 __list_del(list->prev, list->next);
268 list_add(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/**
272 * list_move_tail - delete from one list and add as another's tail
273 * @list: the entry to move
274 * @head: the head that will follow our entry
275 */
276static inline void list_move_tail(struct list_head *list,
277 struct list_head *head)
278{
Daniel Walker78db2ad2007-05-12 16:28:35 -0700279 __list_del(list->prev, list->next);
280 list_add_tail(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/**
Shailabh Nagare8f4d972006-07-14 00:24:35 -0700284 * list_is_last - tests whether @list is the last entry in list @head
285 * @list: the entry to test
286 * @head: the head of the list
287 */
288static inline int list_is_last(const struct list_head *list,
289 const struct list_head *head)
290{
291 return list->next == head;
292}
293
294/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * list_empty - tests whether a list is empty
296 * @head: the list to test.
297 */
298static inline int list_empty(const struct list_head *head)
299{
300 return head->next == head;
301}
302
303/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700304 * list_empty_careful - tests whether a list is empty and not being modified
305 * @head: the list to test
306 *
307 * Description:
308 * tests whether a list is empty _and_ checks that no other CPU might be
309 * in the process of modifying either member (next or prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *
311 * NOTE: using list_empty_careful() without synchronization
312 * can only be safe if the only activity that can happen
313 * to the list entry is list_del_init(). Eg. it cannot be used
314 * if another CPU could re-list_add() it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316static inline int list_empty_careful(const struct list_head *head)
317{
318 struct list_head *next = head->next;
319 return (next == head) && (next == head->prev);
320}
321
Masami Hiramatsu99602572008-04-28 02:14:27 -0700322/**
323 * list_is_singular - tests whether a list has just one entry.
324 * @head: the list to test.
325 */
326static inline int list_is_singular(const struct list_head *head)
327{
328 return !list_empty(head) && (head->next == head->prev);
329}
330
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700331static inline void __list_splice(const struct list_head *list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct list_head *head)
333{
334 struct list_head *first = list->next;
335 struct list_head *last = list->prev;
336 struct list_head *at = head->next;
337
338 first->prev = head;
339 head->next = first;
340
341 last->next = at;
342 at->prev = last;
343}
344
345/**
346 * list_splice - join two lists
347 * @list: the new list to add.
348 * @head: the place to add it in the first list.
349 */
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700350static inline void list_splice(const struct list_head *list,
351 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 if (!list_empty(list))
354 __list_splice(list, head);
355}
356
357/**
358 * list_splice_init - join two lists and reinitialise the emptied list.
359 * @list: the new list to add.
360 * @head: the place to add it in the first list.
361 *
362 * The list at @list is reinitialised
363 */
364static inline void list_splice_init(struct list_head *list,
365 struct list_head *head)
366{
367 if (!list_empty(list)) {
368 __list_splice(list, head);
369 INIT_LIST_HEAD(list);
370 }
371}
372
373/**
Corey Minyard3678d622007-02-10 01:45:42 -0800374 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
375 * @list: the RCU-protected list to splice
376 * @head: the place in the list to splice the first list into
377 * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
378 *
379 * @head can be RCU-read traversed concurrently with this function.
380 *
381 * Note that this function blocks.
382 *
383 * Important note: the caller must take whatever action is necessary to
384 * prevent any other updates to @head. In principle, it is possible
385 * to modify the list as soon as sync() begins execution.
386 * If this sort of thing becomes necessary, an alternative version
387 * based on call_rcu() could be created. But only if -really-
388 * needed -- there is no shortage of RCU API members.
389 */
390static inline void list_splice_init_rcu(struct list_head *list,
391 struct list_head *head,
392 void (*sync)(void))
393{
394 struct list_head *first = list->next;
395 struct list_head *last = list->prev;
396 struct list_head *at = head->next;
397
398 if (list_empty(head))
399 return;
400
401 /* "first" and "last" tracking list, so initialize it. */
402
403 INIT_LIST_HEAD(list);
404
405 /*
406 * At this point, the list body still points to the source list.
407 * Wait for any readers to finish using the list before splicing
408 * the list body into the new list. Any new readers will see
409 * an empty list.
410 */
411
412 sync();
413
414 /*
415 * Readers are finished with the source list, so perform splice.
416 * The order is important if the new list is global and accessible
417 * to concurrent RCU readers. Note that RCU readers are not
418 * permitted to traverse the prev pointers without excluding
419 * this function.
420 */
421
422 last->next = at;
423 smp_wmb();
424 head->next = first;
425 first->prev = head;
426 at->prev = last;
427}
428
429/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * list_entry - get the struct for this entry
431 * @ptr: the &struct list_head pointer.
432 * @type: the type of the struct this is embedded in.
433 * @member: the name of the list_struct within the struct.
434 */
435#define list_entry(ptr, type, member) \
436 container_of(ptr, type, member)
437
438/**
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700439 * list_first_entry - get the first element from a list
440 * @ptr: the list head to take the element from.
441 * @type: the type of the struct this is embedded in.
442 * @member: the name of the list_struct within the struct.
443 *
444 * Note, that list is expected to be not empty.
445 */
446#define list_first_entry(ptr, type, member) \
447 list_entry((ptr)->next, type, member)
448
449/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * list_for_each - iterate over a list
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(pos, head) \
455 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
456 pos = pos->next)
457
458/**
459 * __list_for_each - iterate over a list
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 * @head: the head for your list.
462 *
463 * This variant differs from list_for_each() in that it's the
464 * simplest possible list iteration code, no prefetching is done.
465 * Use this for code that knows the list to be very short (empty
466 * or 1 entry) most of the time.
467 */
468#define __list_for_each(pos, head) \
469 for (pos = (head)->next; pos != (head); pos = pos->next)
470
471/**
472 * list_for_each_prev - iterate over a list backwards
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700473 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * @head: the head for your list.
475 */
476#define list_for_each_prev(pos, head) \
477 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
478 pos = pos->prev)
479
480/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700481 * list_for_each_safe - iterate over a list safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700482 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * @n: another &struct list_head to use as temporary storage
484 * @head: the head for your list.
485 */
486#define list_for_each_safe(pos, n, head) \
487 for (pos = (head)->next, n = pos->next; pos != (head); \
488 pos = n, n = pos->next)
489
490/**
Randy Dunlap8f731f72007-10-18 23:39:28 -0700491 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
Denis V. Lunev37c42522007-10-16 23:29:53 -0700492 * @pos: the &struct list_head to use as a loop cursor.
493 * @n: another &struct list_head to use as temporary storage
494 * @head: the head for your list.
495 */
496#define list_for_each_prev_safe(pos, n, head) \
497 for (pos = (head)->prev, n = pos->prev; \
498 prefetch(pos->prev), pos != (head); \
499 pos = n, n = pos->prev)
500
501/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * list_for_each_entry - iterate 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.
506 */
507#define list_for_each_entry(pos, head, member) \
508 for (pos = list_entry((head)->next, typeof(*pos), member); \
509 prefetch(pos->member.next), &pos->member != (head); \
510 pos = list_entry(pos->member.next, typeof(*pos), member))
511
512/**
513 * list_for_each_entry_reverse - iterate backwards over list of given type.
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700514 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * @head: the head for your list.
516 * @member: the name of the list_struct within the struct.
517 */
518#define list_for_each_entry_reverse(pos, head, member) \
519 for (pos = list_entry((head)->prev, typeof(*pos), member); \
520 prefetch(pos->member.prev), &pos->member != (head); \
521 pos = list_entry(pos->member.prev, typeof(*pos), member))
522
523/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800524 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * @pos: the type * to use as a start point
526 * @head: the head of the list
527 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700528 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800529 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
531#define list_prepare_entry(pos, head, member) \
532 ((pos) ? : list_entry(head, typeof(*pos), member))
533
534/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700535 * list_for_each_entry_continue - continue iteration over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700536 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * @head: the head for your list.
538 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700539 *
540 * Continue to iterate over list of given type, continuing after
541 * the current position.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
543#define list_for_each_entry_continue(pos, head, member) \
544 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
545 prefetch(pos->member.next), &pos->member != (head); \
546 pos = list_entry(pos->member.next, typeof(*pos), member))
547
548/**
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700549 * list_for_each_entry_continue_reverse - iterate backwards from the given point
550 * @pos: the type * to use as a loop cursor.
551 * @head: the head for your list.
552 * @member: the name of the list_struct within the struct.
553 *
554 * Start to iterate over list of given type backwards, continuing after
555 * the current position.
556 */
557#define list_for_each_entry_continue_reverse(pos, head, member) \
558 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
559 prefetch(pos->member.prev), &pos->member != (head); \
560 pos = list_entry(pos->member.prev, typeof(*pos), member))
561
562/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700563 * list_for_each_entry_from - iterate over list of given type from the current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700564 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800565 * @head: the head for your list.
566 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700567 *
568 * Iterate over list of given type, continuing from current position.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800569 */
570#define list_for_each_entry_from(pos, head, member) \
571 for (; prefetch(pos->member.next), &pos->member != (head); \
572 pos = list_entry(pos->member.next, typeof(*pos), member))
573
574/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700576 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * @n: another type * to use as temporary storage
578 * @head: the head for your list.
579 * @member: the name of the list_struct within the struct.
580 */
581#define list_for_each_entry_safe(pos, n, head, member) \
582 for (pos = list_entry((head)->next, typeof(*pos), member), \
583 n = list_entry(pos->member.next, typeof(*pos), member); \
584 &pos->member != (head); \
585 pos = n, n = list_entry(n->member.next, typeof(*n), member))
586
587/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700588 * list_for_each_entry_safe_continue
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700589 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700590 * @n: another type * to use as temporary storage
591 * @head: the head for your list.
592 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700593 *
594 * Iterate over list of given type, continuing after current point,
595 * safe against removal of list entry.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700596 */
597#define list_for_each_entry_safe_continue(pos, n, head, member) \
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300598 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
599 n = list_entry(pos->member.next, typeof(*pos), member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700600 &pos->member != (head); \
601 pos = n, n = list_entry(n->member.next, typeof(*n), member))
602
603/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700604 * list_for_each_entry_safe_from
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700605 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800606 * @n: another type * to use as temporary storage
607 * @head: the head for your list.
608 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700609 *
610 * Iterate over list of given type from current point, safe against
611 * removal of list entry.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800612 */
613#define list_for_each_entry_safe_from(pos, n, head, member) \
614 for (n = list_entry(pos->member.next, typeof(*pos), member); \
615 &pos->member != (head); \
616 pos = n, n = list_entry(n->member.next, typeof(*n), member))
617
618/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700619 * list_for_each_entry_safe_reverse
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700620 * @pos: the type * to use as a loop cursor.
David Howells0ad42352006-01-09 20:51:31 -0800621 * @n: another type * to use as temporary storage
622 * @head: the head for your list.
623 * @member: the name of the list_struct within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700624 *
625 * Iterate backwards over list of given type, safe against removal
626 * of list entry.
David Howells0ad42352006-01-09 20:51:31 -0800627 */
628#define list_for_each_entry_safe_reverse(pos, n, head, member) \
629 for (pos = list_entry((head)->prev, typeof(*pos), member), \
630 n = list_entry(pos->member.prev, typeof(*pos), member); \
631 &pos->member != (head); \
632 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
633
634/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * list_for_each_rcu - iterate over an rcu-protected list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700636 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * @head: the head for your list.
638 *
639 * This list-traversal primitive may safely run concurrently with
640 * the _rcu list-mutation primitives such as list_add_rcu()
641 * as long as the traversal is guarded by rcu_read_lock().
642 */
643#define list_for_each_rcu(pos, head) \
Linus Torvaldscc216c52008-04-20 21:59:13 -0700644 for (pos = rcu_dereference((head)->next); \
645 prefetch(pos->next), pos != (head); \
646 pos = rcu_dereference(pos->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648#define __list_for_each_rcu(pos, head) \
Linus Torvaldscc216c52008-04-20 21:59:13 -0700649 for (pos = rcu_dereference((head)->next); \
650 pos != (head); \
651 pos = rcu_dereference(pos->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653/**
654 * list_for_each_entry_rcu - iterate over rcu list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700655 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * @head: the head for your list.
657 * @member: the name of the list_struct within the struct.
658 *
659 * This list-traversal primitive may safely run concurrently with
660 * the _rcu list-mutation primitives such as list_add_rcu()
661 * as long as the traversal is guarded by rcu_read_lock().
662 */
Herbert Xub24d18a2005-10-16 20:29:20 -0700663#define list_for_each_entry_rcu(pos, head, member) \
Linus Torvaldscc216c52008-04-20 21:59:13 -0700664 for (pos = list_entry(rcu_dereference((head)->next), typeof(*pos), member); \
665 prefetch(pos->member.next), &pos->member != (head); \
666 pos = list_entry(rcu_dereference(pos->member.next), typeof(*pos), member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668
669/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700670 * list_for_each_continue_rcu
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700671 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * @head: the head for your list.
673 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700674 * Iterate over an rcu-protected list, continuing after current point.
675 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * This list-traversal primitive may safely run concurrently with
677 * the _rcu list-mutation primitives such as list_add_rcu()
678 * as long as the traversal is guarded by rcu_read_lock().
679 */
680#define list_for_each_continue_rcu(pos, head) \
Linus Torvaldscc216c52008-04-20 21:59:13 -0700681 for ((pos) = rcu_dereference((pos)->next); \
682 prefetch((pos)->next), (pos) != (head); \
683 (pos) = rcu_dereference((pos)->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685/*
686 * Double linked lists with a single pointer list head.
687 * Mostly useful for hash tables where the two pointer list head is
688 * too wasteful.
689 * You lose the ability to access the tail in O(1).
690 */
691
692struct hlist_head {
693 struct hlist_node *first;
694};
695
696struct hlist_node {
697 struct hlist_node *next, **pprev;
698};
699
700#define HLIST_HEAD_INIT { .first = NULL }
701#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
702#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800703static inline void INIT_HLIST_NODE(struct hlist_node *h)
704{
705 h->next = NULL;
706 h->pprev = NULL;
707}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709static inline int hlist_unhashed(const struct hlist_node *h)
710{
711 return !h->pprev;
712}
713
714static inline int hlist_empty(const struct hlist_head *h)
715{
716 return !h->first;
717}
718
719static inline void __hlist_del(struct hlist_node *n)
720{
721 struct hlist_node *next = n->next;
722 struct hlist_node **pprev = n->pprev;
723 *pprev = next;
724 if (next)
725 next->pprev = pprev;
726}
727
728static inline void hlist_del(struct hlist_node *n)
729{
730 __hlist_del(n);
731 n->next = LIST_POISON1;
732 n->pprev = LIST_POISON2;
733}
734
735/**
736 * hlist_del_rcu - deletes entry from hash list without re-initialization
737 * @n: the element to delete from the hash list.
738 *
739 * Note: list_unhashed() on entry does not return true after this,
740 * the entry is in an undefined state. It is useful for RCU based
741 * lockfree traversal.
742 *
743 * In particular, it means that we can not poison the forward
744 * pointers that may still be used for walking the hash list.
745 *
746 * The caller must take whatever precautions are necessary
747 * (such as holding appropriate locks) to avoid racing
748 * with another list-mutation primitive, such as hlist_add_head_rcu()
749 * or hlist_del_rcu(), running on this same list.
750 * However, it is perfectly legal to run concurrently with
751 * the _rcu list-traversal primitives, such as
752 * hlist_for_each_entry().
753 */
754static inline void hlist_del_rcu(struct hlist_node *n)
755{
756 __hlist_del(n);
757 n->pprev = LIST_POISON2;
758}
759
760static inline void hlist_del_init(struct hlist_node *n)
761{
Akinobu Mitada753be2006-04-28 15:21:23 -0700762 if (!hlist_unhashed(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 __hlist_del(n);
764 INIT_HLIST_NODE(n);
765 }
766}
767
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800768/**
Ingo Molnarb88cb422005-12-12 00:37:11 -0800769 * hlist_replace_rcu - replace old entry by new one
770 * @old : the element to be replaced
771 * @new : the new element to insert
772 *
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800773 * The @old entry will be replaced with the @new entry atomically.
Ingo Molnarb88cb422005-12-12 00:37:11 -0800774 */
775static inline void hlist_replace_rcu(struct hlist_node *old,
776 struct hlist_node *new)
777{
778 struct hlist_node *next = old->next;
779
780 new->next = next;
781 new->pprev = old->pprev;
782 smp_wmb();
783 if (next)
784 new->next->pprev = &new->next;
785 *new->pprev = new;
786 old->pprev = LIST_POISON2;
787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
790{
791 struct hlist_node *first = h->first;
792 n->next = first;
793 if (first)
794 first->pprev = &n->next;
795 h->first = n;
796 n->pprev = &h->first;
797}
798
799
800/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700801 * hlist_add_head_rcu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * @n: the element to add to the hash list.
803 * @h: the list to add to.
804 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700805 * Description:
806 * Adds the specified element to the specified hlist,
807 * while permitting racing traversals.
808 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * The caller must take whatever precautions are necessary
810 * (such as holding appropriate locks) to avoid racing
811 * with another list-mutation primitive, such as hlist_add_head_rcu()
812 * or hlist_del_rcu(), running on this same list.
813 * However, it is perfectly legal to run concurrently with
814 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800815 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * problems on Alpha CPUs. Regardless of the type of CPU, the
817 * list-traversal primitive must be guarded by rcu_read_lock().
818 */
819static inline void hlist_add_head_rcu(struct hlist_node *n,
820 struct hlist_head *h)
821{
822 struct hlist_node *first = h->first;
823 n->next = first;
824 n->pprev = &h->first;
825 smp_wmb();
826 if (first)
827 first->pprev = &n->next;
828 h->first = n;
829}
830
831/* next must be != NULL */
832static inline void hlist_add_before(struct hlist_node *n,
833 struct hlist_node *next)
834{
835 n->pprev = next->pprev;
836 n->next = next;
837 next->pprev = &n->next;
838 *(n->pprev) = n;
839}
840
841static inline void hlist_add_after(struct hlist_node *n,
842 struct hlist_node *next)
843{
844 next->next = n->next;
845 n->next = next;
846 next->pprev = &n->next;
847
848 if(next->next)
849 next->next->pprev = &next->next;
850}
851
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700852/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700853 * hlist_add_before_rcu
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700854 * @n: the new element to add to the hash list.
855 * @next: the existing element to add the new element before.
856 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700857 * Description:
858 * Adds the specified element to the specified hlist
859 * before the specified node while permitting racing traversals.
860 *
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700861 * The caller must take whatever precautions are necessary
862 * (such as holding appropriate locks) to avoid racing
863 * with another list-mutation primitive, such as hlist_add_head_rcu()
864 * or hlist_del_rcu(), running on this same list.
865 * However, it is perfectly legal to run concurrently with
866 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800867 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700868 * problems on Alpha CPUs.
869 */
Robert Olssone5b43762005-08-25 13:01:03 -0700870static inline void hlist_add_before_rcu(struct hlist_node *n,
871 struct hlist_node *next)
872{
873 n->pprev = next->pprev;
874 n->next = next;
875 smp_wmb();
876 next->pprev = &n->next;
877 *(n->pprev) = n;
878}
879
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700880/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700881 * hlist_add_after_rcu
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700882 * @prev: the existing element to add the new element after.
883 * @n: the new element to add to the hash list.
884 *
Randy Dunlapfe96e572006-06-25 05:47:42 -0700885 * Description:
886 * Adds the specified element to the specified hlist
887 * after the specified node while permitting racing traversals.
888 *
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700889 * The caller must take whatever precautions are necessary
890 * (such as holding appropriate locks) to avoid racing
891 * with another list-mutation primitive, such as hlist_add_head_rcu()
892 * or hlist_del_rcu(), running on this same list.
893 * However, it is perfectly legal to run concurrently with
894 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800895 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700896 * problems on Alpha CPUs.
897 */
Robert Olssone5b43762005-08-25 13:01:03 -0700898static inline void hlist_add_after_rcu(struct hlist_node *prev,
899 struct hlist_node *n)
900{
901 n->next = prev->next;
902 n->pprev = &prev->next;
903 smp_wmb();
904 prev->next = n;
905 if (n->next)
906 n->next->pprev = &n->next;
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
910
911#define hlist_for_each(pos, head) \
912 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
913 pos = pos->next)
914
915#define hlist_for_each_safe(pos, n, head) \
916 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
917 pos = n)
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/**
920 * hlist_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700921 * @tpos: the type * to use as a loop cursor.
922 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * @head: the head for your list.
924 * @member: the name of the hlist_node within the struct.
925 */
926#define hlist_for_each_entry(tpos, pos, head, member) \
927 for (pos = (head)->first; \
928 pos && ({ prefetch(pos->next); 1;}) && \
929 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
930 pos = pos->next)
931
932/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700933 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700934 * @tpos: the type * to use as a loop cursor.
935 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * @member: the name of the hlist_node within the struct.
937 */
938#define hlist_for_each_entry_continue(tpos, pos, member) \
939 for (pos = (pos)->next; \
940 pos && ({ prefetch(pos->next); 1;}) && \
941 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
942 pos = pos->next)
943
944/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700945 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700946 * @tpos: the type * to use as a loop cursor.
947 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * @member: the name of the hlist_node within the struct.
949 */
950#define hlist_for_each_entry_from(tpos, pos, member) \
951 for (; pos && ({ prefetch(pos->next); 1;}) && \
952 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
953 pos = pos->next)
954
955/**
956 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700957 * @tpos: the type * to use as a loop cursor.
958 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * @n: another &struct hlist_node to use as temporary storage
960 * @head: the head for your list.
961 * @member: the name of the hlist_node within the struct.
962 */
963#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
964 for (pos = (head)->first; \
965 pos && ({ n = pos->next; 1; }) && \
966 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
967 pos = n)
968
969/**
970 * hlist_for_each_entry_rcu - iterate over rcu list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700971 * @tpos: the type * to use as a loop cursor.
972 * @pos: the &struct hlist_node to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * @head: the head for your list.
974 * @member: the name of the hlist_node within the struct.
975 *
976 * This list-traversal primitive may safely run concurrently with
Paul E. McKenneye1ba0da2005-04-16 15:25:51 -0700977 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * as long as the traversal is guarded by rcu_read_lock().
979 */
980#define hlist_for_each_entry_rcu(tpos, pos, head, member) \
Linus Torvaldscc216c52008-04-20 21:59:13 -0700981 for (pos = rcu_dereference((head)->first); \
982 pos && ({ prefetch(pos->next); 1;}) && \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Linus Torvaldscc216c52008-04-20 21:59:13 -0700984 pos = rcu_dereference(pos->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986#else
987#warning "don't include kernel headers in userspace"
988#endif /* __KERNEL__ */
989#endif