blob: 47208bd99f9e1c2ef6753e4b7d56739691531576 [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>
7#include <linux/prefetch.h>
8#include <asm/system.h>
9
10/*
11 * These are non-NULL pointers that will result in page faults
12 * under normal circumstances, used to verify that nobody uses
13 * non-initialized list entries.
14 */
15#define LIST_POISON1 ((void *) 0x00100100)
16#define LIST_POISON2 ((void *) 0x00200200)
17
18/*
19 * Simple doubly linked list implementation.
20 *
21 * Some of the internal functions ("__xxx") are useful when
22 * manipulating whole lists rather than single entries, as
23 * sometimes we already know the next/prev entries and we can
24 * generate better code by using them directly rather than
25 * using the generic single-entry routines.
26 */
27
28struct list_head {
29 struct list_head *next, *prev;
30};
31
32#define LIST_HEAD_INIT(name) { &(name), &(name) }
33
34#define LIST_HEAD(name) \
35 struct list_head name = LIST_HEAD_INIT(name)
36
Zach Brown490d6ab2006-02-03 03:03:56 -080037static inline void INIT_LIST_HEAD(struct list_head *list)
38{
39 list->next = list;
40 list->prev = list;
41}
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Insert a new entry between two known consecutive entries.
45 *
46 * This is only for internal list manipulation where we know
47 * the prev/next entries already!
48 */
49static inline void __list_add(struct list_head *new,
50 struct list_head *prev,
51 struct list_head *next)
52{
53 next->prev = new;
54 new->next = next;
55 new->prev = prev;
56 prev->next = new;
57}
58
59/**
60 * list_add - add a new entry
61 * @new: new entry to be added
62 * @head: list head to add it after
63 *
64 * Insert a new entry after the specified head.
65 * This is good for implementing stacks.
66 */
67static inline void list_add(struct list_head *new, struct list_head *head)
68{
69 __list_add(new, head, head->next);
70}
71
72/**
73 * list_add_tail - add a new entry
74 * @new: new entry to be added
75 * @head: list head to add it before
76 *
77 * Insert a new entry before the specified head.
78 * This is useful for implementing queues.
79 */
80static inline void list_add_tail(struct list_head *new, struct list_head *head)
81{
82 __list_add(new, head->prev, head);
83}
84
85/*
86 * Insert a new entry between two known consecutive entries.
87 *
88 * This is only for internal list manipulation where we know
89 * the prev/next entries already!
90 */
91static inline void __list_add_rcu(struct list_head * new,
92 struct list_head * prev, struct list_head * next)
93{
94 new->next = next;
95 new->prev = prev;
96 smp_wmb();
97 next->prev = new;
98 prev->next = new;
99}
100
101/**
102 * list_add_rcu - add a new entry to rcu-protected list
103 * @new: new entry to be added
104 * @head: list head to add it after
105 *
106 * Insert a new entry after the specified head.
107 * This is good for implementing stacks.
108 *
109 * The caller must take whatever precautions are necessary
110 * (such as holding appropriate locks) to avoid racing
111 * with another list-mutation primitive, such as list_add_rcu()
112 * or list_del_rcu(), running on this same list.
113 * However, it is perfectly legal to run concurrently with
114 * the _rcu list-traversal primitives, such as
115 * list_for_each_entry_rcu().
116 */
117static inline void list_add_rcu(struct list_head *new, struct list_head *head)
118{
119 __list_add_rcu(new, head, head->next);
120}
121
122/**
123 * list_add_tail_rcu - add a new entry to rcu-protected list
124 * @new: new entry to be added
125 * @head: list head to add it before
126 *
127 * Insert a new entry before the specified head.
128 * This is useful for implementing queues.
129 *
130 * The caller must take whatever precautions are necessary
131 * (such as holding appropriate locks) to avoid racing
132 * with another list-mutation primitive, such as list_add_tail_rcu()
133 * or list_del_rcu(), running on this same list.
134 * However, it is perfectly legal to run concurrently with
135 * the _rcu list-traversal primitives, such as
136 * list_for_each_entry_rcu().
137 */
138static inline void list_add_tail_rcu(struct list_head *new,
139 struct list_head *head)
140{
141 __list_add_rcu(new, head->prev, head);
142}
143
144/*
145 * Delete a list entry by making the prev/next entries
146 * point to each other.
147 *
148 * This is only for internal list manipulation where we know
149 * the prev/next entries already!
150 */
151static inline void __list_del(struct list_head * prev, struct list_head * next)
152{
153 next->prev = prev;
154 prev->next = next;
155}
156
157/**
158 * list_del - deletes entry from list.
159 * @entry: the element to delete from the list.
160 * Note: list_empty on entry does not return true after this, the entry is
161 * in an undefined state.
162 */
163static inline void list_del(struct list_head *entry)
164{
165 __list_del(entry->prev, entry->next);
166 entry->next = LIST_POISON1;
167 entry->prev = LIST_POISON2;
168}
169
170/**
171 * list_del_rcu - deletes entry from list without re-initialization
172 * @entry: the element to delete from the list.
173 *
174 * Note: list_empty on entry does not return true after this,
175 * the entry is in an undefined state. It is useful for RCU based
176 * lockfree traversal.
177 *
178 * In particular, it means that we can not poison the forward
179 * pointers that may still be used for walking the list.
180 *
181 * The caller must take whatever precautions are necessary
182 * (such as holding appropriate locks) to avoid racing
183 * with another list-mutation primitive, such as list_del_rcu()
184 * or list_add_rcu(), running on this same list.
185 * However, it is perfectly legal to run concurrently with
186 * the _rcu list-traversal primitives, such as
187 * list_for_each_entry_rcu().
188 *
189 * Note that the caller is not permitted to immediately free
Paul E. McKenneyb2b18662005-06-25 14:55:38 -0700190 * the newly deleted entry. Instead, either synchronize_rcu()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * or call_rcu() must be used to defer freeing until an RCU
192 * grace period has elapsed.
193 */
194static inline void list_del_rcu(struct list_head *entry)
195{
196 __list_del(entry->prev, entry->next);
197 entry->prev = LIST_POISON2;
198}
199
200/*
201 * list_replace_rcu - replace old entry by new one
202 * @old : the element to be replaced
203 * @new : the new element to insert
204 *
205 * The old entry will be replaced with the new entry atomically.
206 */
Ingo Molnarb88cb422005-12-12 00:37:11 -0800207static inline void list_replace_rcu(struct list_head *old,
208 struct list_head *new)
209{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 new->next = old->next;
211 new->prev = old->prev;
212 smp_wmb();
213 new->next->prev = new;
214 new->prev->next = new;
Ingo Molnarb88cb422005-12-12 00:37:11 -0800215 old->prev = LIST_POISON2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/**
219 * list_del_init - deletes entry from list and reinitialize it.
220 * @entry: the element to delete from the list.
221 */
222static inline void list_del_init(struct list_head *entry)
223{
224 __list_del(entry->prev, entry->next);
225 INIT_LIST_HEAD(entry);
226}
227
228/**
229 * list_move - delete from one list and add as another's head
230 * @list: the entry to move
231 * @head: the head that will precede our entry
232 */
233static inline void list_move(struct list_head *list, struct list_head *head)
234{
235 __list_del(list->prev, list->next);
236 list_add(list, head);
237}
238
239/**
240 * list_move_tail - delete from one list and add as another's tail
241 * @list: the entry to move
242 * @head: the head that will follow our entry
243 */
244static inline void list_move_tail(struct list_head *list,
245 struct list_head *head)
246{
247 __list_del(list->prev, list->next);
248 list_add_tail(list, head);
249}
250
251/**
252 * list_empty - tests whether a list is empty
253 * @head: the list to test.
254 */
255static inline int list_empty(const struct list_head *head)
256{
257 return head->next == head;
258}
259
260/**
261 * list_empty_careful - tests whether a list is
262 * empty _and_ checks that no other CPU might be
263 * in the process of still modifying either member
264 *
265 * NOTE: using list_empty_careful() without synchronization
266 * can only be safe if the only activity that can happen
267 * to the list entry is list_del_init(). Eg. it cannot be used
268 * if another CPU could re-list_add() it.
269 *
270 * @head: the list to test.
271 */
272static inline int list_empty_careful(const struct list_head *head)
273{
274 struct list_head *next = head->next;
275 return (next == head) && (next == head->prev);
276}
277
278static inline void __list_splice(struct list_head *list,
279 struct list_head *head)
280{
281 struct list_head *first = list->next;
282 struct list_head *last = list->prev;
283 struct list_head *at = head->next;
284
285 first->prev = head;
286 head->next = first;
287
288 last->next = at;
289 at->prev = last;
290}
291
292/**
293 * list_splice - join two lists
294 * @list: the new list to add.
295 * @head: the place to add it in the first list.
296 */
297static inline void list_splice(struct list_head *list, struct list_head *head)
298{
299 if (!list_empty(list))
300 __list_splice(list, head);
301}
302
303/**
304 * list_splice_init - join two lists and reinitialise the emptied list.
305 * @list: the new list to add.
306 * @head: the place to add it in the first list.
307 *
308 * The list at @list is reinitialised
309 */
310static inline void list_splice_init(struct list_head *list,
311 struct list_head *head)
312{
313 if (!list_empty(list)) {
314 __list_splice(list, head);
315 INIT_LIST_HEAD(list);
316 }
317}
318
319/**
320 * list_entry - get the struct for this entry
321 * @ptr: the &struct list_head pointer.
322 * @type: the type of the struct this is embedded in.
323 * @member: the name of the list_struct within the struct.
324 */
325#define list_entry(ptr, type, member) \
326 container_of(ptr, type, member)
327
328/**
329 * list_for_each - iterate over a list
330 * @pos: the &struct list_head to use as a loop counter.
331 * @head: the head for your list.
332 */
333#define list_for_each(pos, head) \
334 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
335 pos = pos->next)
336
337/**
338 * __list_for_each - iterate over a list
339 * @pos: the &struct list_head to use as a loop counter.
340 * @head: the head for your list.
341 *
342 * This variant differs from list_for_each() in that it's the
343 * simplest possible list iteration code, no prefetching is done.
344 * Use this for code that knows the list to be very short (empty
345 * or 1 entry) most of the time.
346 */
347#define __list_for_each(pos, head) \
348 for (pos = (head)->next; pos != (head); pos = pos->next)
349
350/**
351 * list_for_each_prev - iterate over a list backwards
352 * @pos: the &struct list_head to use as a loop counter.
353 * @head: the head for your list.
354 */
355#define list_for_each_prev(pos, head) \
356 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
357 pos = pos->prev)
358
359/**
360 * list_for_each_safe - iterate over a list safe against removal of list entry
361 * @pos: the &struct list_head to use as a loop counter.
362 * @n: another &struct list_head to use as temporary storage
363 * @head: the head for your list.
364 */
365#define list_for_each_safe(pos, n, head) \
366 for (pos = (head)->next, n = pos->next; pos != (head); \
367 pos = n, n = pos->next)
368
369/**
370 * list_for_each_entry - iterate over list of given type
371 * @pos: the type * to use as a loop counter.
372 * @head: the head for your list.
373 * @member: the name of the list_struct within the struct.
374 */
375#define list_for_each_entry(pos, head, member) \
376 for (pos = list_entry((head)->next, typeof(*pos), member); \
377 prefetch(pos->member.next), &pos->member != (head); \
378 pos = list_entry(pos->member.next, typeof(*pos), member))
379
380/**
381 * list_for_each_entry_reverse - iterate backwards over list of given type.
382 * @pos: the type * to use as a loop counter.
383 * @head: the head for your list.
384 * @member: the name of the list_struct within the struct.
385 */
386#define list_for_each_entry_reverse(pos, head, member) \
387 for (pos = list_entry((head)->prev, typeof(*pos), member); \
388 prefetch(pos->member.prev), &pos->member != (head); \
389 pos = list_entry(pos->member.prev, typeof(*pos), member))
390
391/**
392 * list_prepare_entry - prepare a pos entry for use as a start point in
393 * list_for_each_entry_continue
394 * @pos: the type * to use as a start point
395 * @head: the head of the list
396 * @member: the name of the list_struct within the struct.
397 */
398#define list_prepare_entry(pos, head, member) \
399 ((pos) ? : list_entry(head, typeof(*pos), member))
400
401/**
402 * list_for_each_entry_continue - iterate over list of given type
403 * continuing after existing point
404 * @pos: the type * to use as a loop counter.
405 * @head: the head for your list.
406 * @member: the name of the list_struct within the struct.
407 */
408#define list_for_each_entry_continue(pos, head, member) \
409 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
410 prefetch(pos->member.next), &pos->member != (head); \
411 pos = list_entry(pos->member.next, typeof(*pos), member))
412
413/**
414 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
415 * @pos: the type * to use as a loop counter.
416 * @n: another type * to use as temporary storage
417 * @head: the head for your list.
418 * @member: the name of the list_struct within the struct.
419 */
420#define list_for_each_entry_safe(pos, n, head, member) \
421 for (pos = list_entry((head)->next, typeof(*pos), member), \
422 n = list_entry(pos->member.next, typeof(*pos), member); \
423 &pos->member != (head); \
424 pos = n, n = list_entry(n->member.next, typeof(*n), member))
425
426/**
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700427 * list_for_each_entry_safe_continue - iterate over list of given type
428 * continuing after existing point safe against removal of list entry
429 * @pos: the type * to use as a loop counter.
430 * @n: another type * to use as temporary storage
431 * @head: the head for your list.
432 * @member: the name of the list_struct within the struct.
433 */
434#define list_for_each_entry_safe_continue(pos, n, head, member) \
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300435 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
436 n = list_entry(pos->member.next, typeof(*pos), member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700437 &pos->member != (head); \
438 pos = n, n = list_entry(n->member.next, typeof(*n), member))
439
440/**
David Howells0ad42352006-01-09 20:51:31 -0800441 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
442 * removal of list entry
443 * @pos: the type * to use as a loop counter.
444 * @n: another type * to use as temporary storage
445 * @head: the head for your list.
446 * @member: the name of the list_struct within the struct.
447 */
448#define list_for_each_entry_safe_reverse(pos, n, head, member) \
449 for (pos = list_entry((head)->prev, typeof(*pos), member), \
450 n = list_entry(pos->member.prev, typeof(*pos), member); \
451 &pos->member != (head); \
452 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
453
454/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * list_for_each_rcu - iterate over an rcu-protected list
456 * @pos: the &struct list_head to use as a loop counter.
457 * @head: the head for your list.
458 *
459 * This list-traversal primitive may safely run concurrently with
460 * the _rcu list-mutation primitives such as list_add_rcu()
461 * as long as the traversal is guarded by rcu_read_lock().
462 */
463#define list_for_each_rcu(pos, head) \
Herbert Xub24d18a2005-10-16 20:29:20 -0700464 for (pos = (head)->next; \
465 prefetch(rcu_dereference(pos)->next), pos != (head); \
466 pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468#define __list_for_each_rcu(pos, head) \
Herbert Xub24d18a2005-10-16 20:29:20 -0700469 for (pos = (head)->next; \
470 rcu_dereference(pos) != (head); \
471 pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473/**
474 * list_for_each_safe_rcu - iterate over an rcu-protected list safe
475 * against removal of list entry
476 * @pos: the &struct list_head to use as a loop counter.
477 * @n: another &struct list_head to use as temporary storage
478 * @head: the head for your list.
479 *
480 * This list-traversal primitive may safely run concurrently with
481 * the _rcu list-mutation primitives such as list_add_rcu()
482 * as long as the traversal is guarded by rcu_read_lock().
483 */
484#define list_for_each_safe_rcu(pos, n, head) \
Herbert Xub24d18a2005-10-16 20:29:20 -0700485 for (pos = (head)->next; \
486 n = rcu_dereference(pos)->next, pos != (head); \
487 pos = n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489/**
490 * list_for_each_entry_rcu - iterate over rcu list of given type
491 * @pos: the type * to use as a loop counter.
492 * @head: the head for your list.
493 * @member: the name of the list_struct within the struct.
494 *
495 * This list-traversal primitive may safely run concurrently with
496 * the _rcu list-mutation primitives such as list_add_rcu()
497 * as long as the traversal is guarded by rcu_read_lock().
498 */
Herbert Xub24d18a2005-10-16 20:29:20 -0700499#define list_for_each_entry_rcu(pos, head, member) \
500 for (pos = list_entry((head)->next, typeof(*pos), member); \
501 prefetch(rcu_dereference(pos)->member.next), \
502 &pos->member != (head); \
503 pos = list_entry(pos->member.next, typeof(*pos), member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505
506/**
507 * list_for_each_continue_rcu - iterate over an rcu-protected list
508 * continuing after existing point.
509 * @pos: the &struct list_head to use as a loop counter.
510 * @head: the head for your list.
511 *
512 * This list-traversal primitive may safely run concurrently with
513 * the _rcu list-mutation primitives such as list_add_rcu()
514 * as long as the traversal is guarded by rcu_read_lock().
515 */
516#define list_for_each_continue_rcu(pos, head) \
Herbert Xub24d18a2005-10-16 20:29:20 -0700517 for ((pos) = (pos)->next; \
518 prefetch(rcu_dereference((pos))->next), (pos) != (head); \
519 (pos) = (pos)->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521/*
522 * Double linked lists with a single pointer list head.
523 * Mostly useful for hash tables where the two pointer list head is
524 * too wasteful.
525 * You lose the ability to access the tail in O(1).
526 */
527
528struct hlist_head {
529 struct hlist_node *first;
530};
531
532struct hlist_node {
533 struct hlist_node *next, **pprev;
534};
535
536#define HLIST_HEAD_INIT { .first = NULL }
537#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
538#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800539static inline void INIT_HLIST_NODE(struct hlist_node *h)
540{
541 h->next = NULL;
542 h->pprev = NULL;
543}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545static inline int hlist_unhashed(const struct hlist_node *h)
546{
547 return !h->pprev;
548}
549
550static inline int hlist_empty(const struct hlist_head *h)
551{
552 return !h->first;
553}
554
555static inline void __hlist_del(struct hlist_node *n)
556{
557 struct hlist_node *next = n->next;
558 struct hlist_node **pprev = n->pprev;
559 *pprev = next;
560 if (next)
561 next->pprev = pprev;
562}
563
564static inline void hlist_del(struct hlist_node *n)
565{
566 __hlist_del(n);
567 n->next = LIST_POISON1;
568 n->pprev = LIST_POISON2;
569}
570
571/**
572 * hlist_del_rcu - deletes entry from hash list without re-initialization
573 * @n: the element to delete from the hash list.
574 *
575 * Note: list_unhashed() on entry does not return true after this,
576 * the entry is in an undefined state. It is useful for RCU based
577 * lockfree traversal.
578 *
579 * In particular, it means that we can not poison the forward
580 * pointers that may still be used for walking the hash list.
581 *
582 * The caller must take whatever precautions are necessary
583 * (such as holding appropriate locks) to avoid racing
584 * with another list-mutation primitive, such as hlist_add_head_rcu()
585 * or hlist_del_rcu(), running on this same list.
586 * However, it is perfectly legal to run concurrently with
587 * the _rcu list-traversal primitives, such as
588 * hlist_for_each_entry().
589 */
590static inline void hlist_del_rcu(struct hlist_node *n)
591{
592 __hlist_del(n);
593 n->pprev = LIST_POISON2;
594}
595
596static inline void hlist_del_init(struct hlist_node *n)
597{
598 if (n->pprev) {
599 __hlist_del(n);
600 INIT_HLIST_NODE(n);
601 }
602}
603
Ingo Molnarb88cb422005-12-12 00:37:11 -0800604/*
605 * hlist_replace_rcu - replace old entry by new one
606 * @old : the element to be replaced
607 * @new : the new element to insert
608 *
609 * The old entry will be replaced with the new entry atomically.
610 */
611static inline void hlist_replace_rcu(struct hlist_node *old,
612 struct hlist_node *new)
613{
614 struct hlist_node *next = old->next;
615
616 new->next = next;
617 new->pprev = old->pprev;
618 smp_wmb();
619 if (next)
620 new->next->pprev = &new->next;
621 *new->pprev = new;
622 old->pprev = LIST_POISON2;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
626{
627 struct hlist_node *first = h->first;
628 n->next = first;
629 if (first)
630 first->pprev = &n->next;
631 h->first = n;
632 n->pprev = &h->first;
633}
634
635
636/**
637 * hlist_add_head_rcu - adds the specified element to the specified hlist,
638 * while permitting racing traversals.
639 * @n: the element to add to the hash list.
640 * @h: the list to add to.
641 *
642 * The caller must take whatever precautions are necessary
643 * (such as holding appropriate locks) to avoid racing
644 * with another list-mutation primitive, such as hlist_add_head_rcu()
645 * or hlist_del_rcu(), running on this same list.
646 * However, it is perfectly legal to run concurrently with
647 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800648 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * problems on Alpha CPUs. Regardless of the type of CPU, the
650 * list-traversal primitive must be guarded by rcu_read_lock().
651 */
652static inline void hlist_add_head_rcu(struct hlist_node *n,
653 struct hlist_head *h)
654{
655 struct hlist_node *first = h->first;
656 n->next = first;
657 n->pprev = &h->first;
658 smp_wmb();
659 if (first)
660 first->pprev = &n->next;
661 h->first = n;
662}
663
664/* next must be != NULL */
665static inline void hlist_add_before(struct hlist_node *n,
666 struct hlist_node *next)
667{
668 n->pprev = next->pprev;
669 n->next = next;
670 next->pprev = &n->next;
671 *(n->pprev) = n;
672}
673
674static inline void hlist_add_after(struct hlist_node *n,
675 struct hlist_node *next)
676{
677 next->next = n->next;
678 n->next = next;
679 next->pprev = &n->next;
680
681 if(next->next)
682 next->next->pprev = &next->next;
683}
684
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700685/**
686 * hlist_add_before_rcu - adds the specified element to the specified hlist
687 * before the specified node while permitting racing traversals.
688 * @n: the new element to add to the hash list.
689 * @next: the existing element to add the new element before.
690 *
691 * The caller must take whatever precautions are necessary
692 * (such as holding appropriate locks) to avoid racing
693 * with another list-mutation primitive, such as hlist_add_head_rcu()
694 * or hlist_del_rcu(), running on this same list.
695 * However, it is perfectly legal to run concurrently with
696 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800697 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700698 * problems on Alpha CPUs.
699 */
Robert Olssone5b43762005-08-25 13:01:03 -0700700static inline void hlist_add_before_rcu(struct hlist_node *n,
701 struct hlist_node *next)
702{
703 n->pprev = next->pprev;
704 n->next = next;
705 smp_wmb();
706 next->pprev = &n->next;
707 *(n->pprev) = n;
708}
709
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700710/**
711 * hlist_add_after_rcu - adds the specified element to the specified hlist
712 * after the specified node while permitting racing traversals.
713 * @prev: the existing element to add the new element after.
714 * @n: the new element to add to the hash list.
715 *
716 * The caller must take whatever precautions are necessary
717 * (such as holding appropriate locks) to avoid racing
718 * with another list-mutation primitive, such as hlist_add_head_rcu()
719 * or hlist_del_rcu(), running on this same list.
720 * However, it is perfectly legal to run concurrently with
721 * the _rcu list-traversal primitives, such as
Paul E. McKenney665a7582005-11-07 00:59:17 -0800722 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
Paul E. McKenneycf4ef012005-08-25 16:08:37 -0700723 * problems on Alpha CPUs.
724 */
Robert Olssone5b43762005-08-25 13:01:03 -0700725static inline void hlist_add_after_rcu(struct hlist_node *prev,
726 struct hlist_node *n)
727{
728 n->next = prev->next;
729 n->pprev = &prev->next;
730 smp_wmb();
731 prev->next = n;
732 if (n->next)
733 n->next->pprev = &n->next;
734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
737
738#define hlist_for_each(pos, head) \
739 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
740 pos = pos->next)
741
742#define hlist_for_each_safe(pos, n, head) \
743 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
744 pos = n)
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746/**
747 * hlist_for_each_entry - iterate over list of given type
748 * @tpos: the type * to use as a loop counter.
749 * @pos: the &struct hlist_node to use as a loop counter.
750 * @head: the head for your list.
751 * @member: the name of the hlist_node within the struct.
752 */
753#define hlist_for_each_entry(tpos, pos, head, member) \
754 for (pos = (head)->first; \
755 pos && ({ prefetch(pos->next); 1;}) && \
756 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
757 pos = pos->next)
758
759/**
760 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
761 * @tpos: the type * to use as a loop counter.
762 * @pos: the &struct hlist_node to use as a loop counter.
763 * @member: the name of the hlist_node within the struct.
764 */
765#define hlist_for_each_entry_continue(tpos, pos, member) \
766 for (pos = (pos)->next; \
767 pos && ({ prefetch(pos->next); 1;}) && \
768 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
769 pos = pos->next)
770
771/**
772 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
773 * @tpos: the type * to use as a loop counter.
774 * @pos: the &struct hlist_node to use as a loop counter.
775 * @member: the name of the hlist_node within the struct.
776 */
777#define hlist_for_each_entry_from(tpos, pos, member) \
778 for (; pos && ({ prefetch(pos->next); 1;}) && \
779 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
780 pos = pos->next)
781
782/**
783 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
784 * @tpos: the type * to use as a loop counter.
785 * @pos: the &struct hlist_node to use as a loop counter.
786 * @n: another &struct hlist_node to use as temporary storage
787 * @head: the head for your list.
788 * @member: the name of the hlist_node within the struct.
789 */
790#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
791 for (pos = (head)->first; \
792 pos && ({ n = pos->next; 1; }) && \
793 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
794 pos = n)
795
796/**
797 * hlist_for_each_entry_rcu - iterate over rcu list of given type
Paul E. McKenney665a7582005-11-07 00:59:17 -0800798 * @tpos: the type * to use as a loop counter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * @pos: the &struct hlist_node to use as a loop counter.
800 * @head: the head for your list.
801 * @member: the name of the hlist_node within the struct.
802 *
803 * This list-traversal primitive may safely run concurrently with
Paul E. McKenneye1ba0da2005-04-16 15:25:51 -0700804 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * as long as the traversal is guarded by rcu_read_lock().
806 */
807#define hlist_for_each_entry_rcu(tpos, pos, head, member) \
808 for (pos = (head)->first; \
Herbert Xub24d18a2005-10-16 20:29:20 -0700809 rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Herbert Xub24d18a2005-10-16 20:29:20 -0700811 pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813#else
814#warning "don't include kernel headers in userspace"
815#endif /* __KERNEL__ */
816#endif