list.h: add list_is_singular()
[pandora-kernel.git] / include / linux / list.h
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 #ifdef __KERNEL__
5
6 #include <linux/stddef.h>
7 #include <linux/poison.h>
8 #include <linux/prefetch.h>
9 #include <asm/system.h>
10
11 /*
12  * 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
21 struct 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
30 static inline void INIT_LIST_HEAD(struct list_head *list)
31 {
32         list->next = list;
33         list->prev = list;
34 }
35
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  */
42 #ifndef CONFIG_DEBUG_LIST
43 static 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 }
52 #else
53 extern void __list_add(struct list_head *new,
54                               struct list_head *prev,
55                               struct list_head *next);
56 #endif
57
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  */
66 #ifndef CONFIG_DEBUG_LIST
67 static inline void list_add(struct list_head *new, struct list_head *head)
68 {
69         __list_add(new, head, head->next);
70 }
71 #else
72 extern void list_add(struct list_head *new, struct list_head *head);
73 #endif
74
75
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  */
84 static 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  */
95 static 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  */
121 static 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  */
142 static 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  */
155 static 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  */
167 #ifndef CONFIG_DEBUG_LIST
168 static 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 }
174 #else
175 extern void list_del(struct list_head *entry);
176 #endif
177
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
198  * the newly deleted entry.  Instead, either synchronize_rcu()
199  * or call_rcu() must be used to defer freeing until an RCU
200  * grace period has elapsed.
201  */
202 static inline void list_del_rcu(struct list_head *entry)
203 {
204         __list_del(entry->prev, entry->next);
205         entry->prev = LIST_POISON2;
206 }
207
208 /**
209  * list_replace - replace old entry by new one
210  * @old : the element to be replaced
211  * @new : the new element to insert
212  *
213  * If @old was empty, it will be overwritten.
214  */
215 static 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
224 static 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
231 /**
232  * list_replace_rcu - replace old entry by new one
233  * @old : the element to be replaced
234  * @new : the new element to insert
235  *
236  * The @old entry will be replaced with the @new entry atomically.
237  * Note: @old should not be empty.
238  */
239 static inline void list_replace_rcu(struct list_head *old,
240                                 struct list_head *new)
241 {
242         new->next = old->next;
243         new->prev = old->prev;
244         smp_wmb();
245         new->next->prev = new;
246         new->prev->next = new;
247         old->prev = LIST_POISON2;
248 }
249
250 /**
251  * list_del_init - deletes entry from list and reinitialize it.
252  * @entry: the element to delete from the list.
253  */
254 static 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  */
265 static inline void list_move(struct list_head *list, struct list_head *head)
266 {
267         __list_del(list->prev, list->next);
268         list_add(list, head);
269 }
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  */
276 static inline void list_move_tail(struct list_head *list,
277                                   struct list_head *head)
278 {
279         __list_del(list->prev, list->next);
280         list_add_tail(list, head);
281 }
282
283 /**
284  * 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  */
288 static 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 /**
295  * list_empty - tests whether a list is empty
296  * @head: the list to test.
297  */
298 static inline int list_empty(const struct list_head *head)
299 {
300         return head->next == head;
301 }
302
303 /**
304  * 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)
310  *
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.
315  */
316 static 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
322 /**
323  * list_is_singular - tests whether a list has just one entry.
324  * @head: the list to test.
325  */
326 static inline int list_is_singular(const struct list_head *head)
327 {
328         return !list_empty(head) && (head->next == head->prev);
329 }
330
331 static inline void __list_splice(struct list_head *list,
332                                  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  */
350 static inline void list_splice(struct list_head *list, struct list_head *head)
351 {
352         if (!list_empty(list))
353                 __list_splice(list, head);
354 }
355
356 /**
357  * list_splice_init - join two lists and reinitialise the emptied list.
358  * @list: the new list to add.
359  * @head: the place to add it in the first list.
360  *
361  * The list at @list is reinitialised
362  */
363 static inline void list_splice_init(struct list_head *list,
364                                     struct list_head *head)
365 {
366         if (!list_empty(list)) {
367                 __list_splice(list, head);
368                 INIT_LIST_HEAD(list);
369         }
370 }
371
372 /**
373  * list_splice_init_rcu - splice an RCU-protected list into an existing list.
374  * @list:       the RCU-protected list to splice
375  * @head:       the place in the list to splice the first list into
376  * @sync:       function to sync: synchronize_rcu(), synchronize_sched(), ...
377  *
378  * @head can be RCU-read traversed concurrently with this function.
379  *
380  * Note that this function blocks.
381  *
382  * Important note: the caller must take whatever action is necessary to
383  *      prevent any other updates to @head.  In principle, it is possible
384  *      to modify the list as soon as sync() begins execution.
385  *      If this sort of thing becomes necessary, an alternative version
386  *      based on call_rcu() could be created.  But only if -really-
387  *      needed -- there is no shortage of RCU API members.
388  */
389 static inline void list_splice_init_rcu(struct list_head *list,
390                                         struct list_head *head,
391                                         void (*sync)(void))
392 {
393         struct list_head *first = list->next;
394         struct list_head *last = list->prev;
395         struct list_head *at = head->next;
396
397         if (list_empty(head))
398                 return;
399
400         /* "first" and "last" tracking list, so initialize it. */
401
402         INIT_LIST_HEAD(list);
403
404         /*
405          * At this point, the list body still points to the source list.
406          * Wait for any readers to finish using the list before splicing
407          * the list body into the new list.  Any new readers will see
408          * an empty list.
409          */
410
411         sync();
412
413         /*
414          * Readers are finished with the source list, so perform splice.
415          * The order is important if the new list is global and accessible
416          * to concurrent RCU readers.  Note that RCU readers are not
417          * permitted to traverse the prev pointers without excluding
418          * this function.
419          */
420
421         last->next = at;
422         smp_wmb();
423         head->next = first;
424         first->prev = head;
425         at->prev = last;
426 }
427
428 /**
429  * list_entry - get the struct for this entry
430  * @ptr:        the &struct list_head pointer.
431  * @type:       the type of the struct this is embedded in.
432  * @member:     the name of the list_struct within the struct.
433  */
434 #define list_entry(ptr, type, member) \
435         container_of(ptr, type, member)
436
437 /**
438  * list_first_entry - get the first element from a list
439  * @ptr:        the list head to take the element from.
440  * @type:       the type of the struct this is embedded in.
441  * @member:     the name of the list_struct within the struct.
442  *
443  * Note, that list is expected to be not empty.
444  */
445 #define list_first_entry(ptr, type, member) \
446         list_entry((ptr)->next, type, member)
447
448 /**
449  * list_for_each        -       iterate over a list
450  * @pos:        the &struct list_head to use as a loop cursor.
451  * @head:       the head for your list.
452  */
453 #define list_for_each(pos, head) \
454         for (pos = (head)->next; prefetch(pos->next), pos != (head); \
455                 pos = pos->next)
456
457 /**
458  * __list_for_each      -       iterate over a list
459  * @pos:        the &struct list_head to use as a loop cursor.
460  * @head:       the head for your list.
461  *
462  * This variant differs from list_for_each() in that it's the
463  * simplest possible list iteration code, no prefetching is done.
464  * Use this for code that knows the list to be very short (empty
465  * or 1 entry) most of the time.
466  */
467 #define __list_for_each(pos, head) \
468         for (pos = (head)->next; pos != (head); pos = pos->next)
469
470 /**
471  * list_for_each_prev   -       iterate over a list backwards
472  * @pos:        the &struct list_head to use as a loop cursor.
473  * @head:       the head for your list.
474  */
475 #define list_for_each_prev(pos, head) \
476         for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
477                 pos = pos->prev)
478
479 /**
480  * list_for_each_safe - iterate over a list safe against removal of list entry
481  * @pos:        the &struct list_head to use as a loop cursor.
482  * @n:          another &struct list_head to use as temporary storage
483  * @head:       the head for your list.
484  */
485 #define list_for_each_safe(pos, n, head) \
486         for (pos = (head)->next, n = pos->next; pos != (head); \
487                 pos = n, n = pos->next)
488
489 /**
490  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
491  * @pos:        the &struct list_head to use as a loop cursor.
492  * @n:          another &struct list_head to use as temporary storage
493  * @head:       the head for your list.
494  */
495 #define list_for_each_prev_safe(pos, n, head) \
496         for (pos = (head)->prev, n = pos->prev; \
497              prefetch(pos->prev), pos != (head); \
498              pos = n, n = pos->prev)
499
500 /**
501  * list_for_each_entry  -       iterate over list of given type
502  * @pos:        the type * to use as a loop cursor.
503  * @head:       the head for your list.
504  * @member:     the name of the list_struct within the struct.
505  */
506 #define list_for_each_entry(pos, head, member)                          \
507         for (pos = list_entry((head)->next, typeof(*pos), member);      \
508              prefetch(pos->member.next), &pos->member != (head);        \
509              pos = list_entry(pos->member.next, typeof(*pos), member))
510
511 /**
512  * list_for_each_entry_reverse - iterate backwards over list of given type.
513  * @pos:        the type * to use as a loop cursor.
514  * @head:       the head for your list.
515  * @member:     the name of the list_struct within the struct.
516  */
517 #define list_for_each_entry_reverse(pos, head, member)                  \
518         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
519              prefetch(pos->member.prev), &pos->member != (head);        \
520              pos = list_entry(pos->member.prev, typeof(*pos), member))
521
522 /**
523  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
524  * @pos:        the type * to use as a start point
525  * @head:       the head of the list
526  * @member:     the name of the list_struct within the struct.
527  *
528  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
529  */
530 #define list_prepare_entry(pos, head, member) \
531         ((pos) ? : list_entry(head, typeof(*pos), member))
532
533 /**
534  * list_for_each_entry_continue - continue iteration over list of given type
535  * @pos:        the type * to use as a loop cursor.
536  * @head:       the head for your list.
537  * @member:     the name of the list_struct within the struct.
538  *
539  * Continue to iterate over list of given type, continuing after
540  * the current position.
541  */
542 #define list_for_each_entry_continue(pos, head, member)                 \
543         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
544              prefetch(pos->member.next), &pos->member != (head);        \
545              pos = list_entry(pos->member.next, typeof(*pos), member))
546
547 /**
548  * list_for_each_entry_continue_reverse - iterate backwards from the given point
549  * @pos:        the type * to use as a loop cursor.
550  * @head:       the head for your list.
551  * @member:     the name of the list_struct within the struct.
552  *
553  * Start to iterate over list of given type backwards, continuing after
554  * the current position.
555  */
556 #define list_for_each_entry_continue_reverse(pos, head, member)         \
557         for (pos = list_entry(pos->member.prev, typeof(*pos), member);  \
558              prefetch(pos->member.prev), &pos->member != (head);        \
559              pos = list_entry(pos->member.prev, typeof(*pos), member))
560
561 /**
562  * list_for_each_entry_from - iterate over list of given type from the current point
563  * @pos:        the type * to use as a loop cursor.
564  * @head:       the head for your list.
565  * @member:     the name of the list_struct within the struct.
566  *
567  * Iterate over list of given type, continuing from current position.
568  */
569 #define list_for_each_entry_from(pos, head, member)                     \
570         for (; prefetch(pos->member.next), &pos->member != (head);      \
571              pos = list_entry(pos->member.next, typeof(*pos), member))
572
573 /**
574  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
575  * @pos:        the type * to use as a loop cursor.
576  * @n:          another type * to use as temporary storage
577  * @head:       the head for your list.
578  * @member:     the name of the list_struct within the struct.
579  */
580 #define list_for_each_entry_safe(pos, n, head, member)                  \
581         for (pos = list_entry((head)->next, typeof(*pos), member),      \
582                 n = list_entry(pos->member.next, typeof(*pos), member); \
583              &pos->member != (head);                                    \
584              pos = n, n = list_entry(n->member.next, typeof(*n), member))
585
586 /**
587  * list_for_each_entry_safe_continue
588  * @pos:        the type * to use as a loop cursor.
589  * @n:          another type * to use as temporary storage
590  * @head:       the head for your list.
591  * @member:     the name of the list_struct within the struct.
592  *
593  * Iterate over list of given type, continuing after current point,
594  * safe against removal of list entry.
595  */
596 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
597         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
598                 n = list_entry(pos->member.next, typeof(*pos), member);         \
599              &pos->member != (head);                                            \
600              pos = n, n = list_entry(n->member.next, typeof(*n), member))
601
602 /**
603  * list_for_each_entry_safe_from
604  * @pos:        the type * to use as a loop cursor.
605  * @n:          another type * to use as temporary storage
606  * @head:       the head for your list.
607  * @member:     the name of the list_struct within the struct.
608  *
609  * Iterate over list of given type from current point, safe against
610  * removal of list entry.
611  */
612 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
613         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
614              &pos->member != (head);                                            \
615              pos = n, n = list_entry(n->member.next, typeof(*n), member))
616
617 /**
618  * list_for_each_entry_safe_reverse
619  * @pos:        the type * to use as a loop cursor.
620  * @n:          another type * to use as temporary storage
621  * @head:       the head for your list.
622  * @member:     the name of the list_struct within the struct.
623  *
624  * Iterate backwards over list of given type, safe against removal
625  * of list entry.
626  */
627 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
628         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
629                 n = list_entry(pos->member.prev, typeof(*pos), member); \
630              &pos->member != (head);                                    \
631              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
632
633 /**
634  * list_for_each_rcu    -       iterate over an rcu-protected list
635  * @pos:        the &struct list_head to use as a loop cursor.
636  * @head:       the head for your list.
637  *
638  * This list-traversal primitive may safely run concurrently with
639  * the _rcu list-mutation primitives such as list_add_rcu()
640  * as long as the traversal is guarded by rcu_read_lock().
641  */
642 #define list_for_each_rcu(pos, head) \
643         for (pos = rcu_dereference((head)->next); \
644                 prefetch(pos->next), pos != (head); \
645                 pos = rcu_dereference(pos->next))
646
647 #define __list_for_each_rcu(pos, head) \
648         for (pos = rcu_dereference((head)->next); \
649                 pos != (head); \
650                 pos = rcu_dereference(pos->next))
651
652 /**
653  * list_for_each_entry_rcu      -       iterate over rcu list of given type
654  * @pos:        the type * to use as a loop cursor.
655  * @head:       the head for your list.
656  * @member:     the name of the list_struct within the struct.
657  *
658  * This list-traversal primitive may safely run concurrently with
659  * the _rcu list-mutation primitives such as list_add_rcu()
660  * as long as the traversal is guarded by rcu_read_lock().
661  */
662 #define list_for_each_entry_rcu(pos, head, member) \
663         for (pos = list_entry(rcu_dereference((head)->next), typeof(*pos), member); \
664                 prefetch(pos->member.next), &pos->member != (head); \
665                 pos = list_entry(rcu_dereference(pos->member.next), typeof(*pos), member))
666
667
668 /**
669  * list_for_each_continue_rcu
670  * @pos:        the &struct list_head to use as a loop cursor.
671  * @head:       the head for your list.
672  *
673  * Iterate over an rcu-protected list, continuing after current point.
674  *
675  * This list-traversal primitive may safely run concurrently with
676  * the _rcu list-mutation primitives such as list_add_rcu()
677  * as long as the traversal is guarded by rcu_read_lock().
678  */
679 #define list_for_each_continue_rcu(pos, head) \
680         for ((pos) = rcu_dereference((pos)->next); \
681                 prefetch((pos)->next), (pos) != (head); \
682                 (pos) = rcu_dereference((pos)->next))
683
684 /*
685  * Double linked lists with a single pointer list head.
686  * Mostly useful for hash tables where the two pointer list head is
687  * too wasteful.
688  * You lose the ability to access the tail in O(1).
689  */
690
691 struct hlist_head {
692         struct hlist_node *first;
693 };
694
695 struct hlist_node {
696         struct hlist_node *next, **pprev;
697 };
698
699 #define HLIST_HEAD_INIT { .first = NULL }
700 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
701 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
702 static inline void INIT_HLIST_NODE(struct hlist_node *h)
703 {
704         h->next = NULL;
705         h->pprev = NULL;
706 }
707
708 static inline int hlist_unhashed(const struct hlist_node *h)
709 {
710         return !h->pprev;
711 }
712
713 static inline int hlist_empty(const struct hlist_head *h)
714 {
715         return !h->first;
716 }
717
718 static inline void __hlist_del(struct hlist_node *n)
719 {
720         struct hlist_node *next = n->next;
721         struct hlist_node **pprev = n->pprev;
722         *pprev = next;
723         if (next)
724                 next->pprev = pprev;
725 }
726
727 static inline void hlist_del(struct hlist_node *n)
728 {
729         __hlist_del(n);
730         n->next = LIST_POISON1;
731         n->pprev = LIST_POISON2;
732 }
733
734 /**
735  * hlist_del_rcu - deletes entry from hash list without re-initialization
736  * @n: the element to delete from the hash list.
737  *
738  * Note: list_unhashed() on entry does not return true after this,
739  * the entry is in an undefined state. It is useful for RCU based
740  * lockfree traversal.
741  *
742  * In particular, it means that we can not poison the forward
743  * pointers that may still be used for walking the hash list.
744  *
745  * The caller must take whatever precautions are necessary
746  * (such as holding appropriate locks) to avoid racing
747  * with another list-mutation primitive, such as hlist_add_head_rcu()
748  * or hlist_del_rcu(), running on this same list.
749  * However, it is perfectly legal to run concurrently with
750  * the _rcu list-traversal primitives, such as
751  * hlist_for_each_entry().
752  */
753 static inline void hlist_del_rcu(struct hlist_node *n)
754 {
755         __hlist_del(n);
756         n->pprev = LIST_POISON2;
757 }
758
759 static inline void hlist_del_init(struct hlist_node *n)
760 {
761         if (!hlist_unhashed(n)) {
762                 __hlist_del(n);
763                 INIT_HLIST_NODE(n);
764         }
765 }
766
767 /**
768  * hlist_replace_rcu - replace old entry by new one
769  * @old : the element to be replaced
770  * @new : the new element to insert
771  *
772  * The @old entry will be replaced with the @new entry atomically.
773  */
774 static inline void hlist_replace_rcu(struct hlist_node *old,
775                                         struct hlist_node *new)
776 {
777         struct hlist_node *next = old->next;
778
779         new->next = next;
780         new->pprev = old->pprev;
781         smp_wmb();
782         if (next)
783                 new->next->pprev = &new->next;
784         *new->pprev = new;
785         old->pprev = LIST_POISON2;
786 }
787
788 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
789 {
790         struct hlist_node *first = h->first;
791         n->next = first;
792         if (first)
793                 first->pprev = &n->next;
794         h->first = n;
795         n->pprev = &h->first;
796 }
797
798
799 /**
800  * hlist_add_head_rcu
801  * @n: the element to add to the hash list.
802  * @h: the list to add to.
803  *
804  * Description:
805  * Adds the specified element to the specified hlist,
806  * while permitting racing traversals.
807  *
808  * The caller must take whatever precautions are necessary
809  * (such as holding appropriate locks) to avoid racing
810  * with another list-mutation primitive, such as hlist_add_head_rcu()
811  * or hlist_del_rcu(), running on this same list.
812  * However, it is perfectly legal to run concurrently with
813  * the _rcu list-traversal primitives, such as
814  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
815  * problems on Alpha CPUs.  Regardless of the type of CPU, the
816  * list-traversal primitive must be guarded by rcu_read_lock().
817  */
818 static inline void hlist_add_head_rcu(struct hlist_node *n,
819                                         struct hlist_head *h)
820 {
821         struct hlist_node *first = h->first;
822         n->next = first;
823         n->pprev = &h->first;
824         smp_wmb();
825         if (first)
826                 first->pprev = &n->next;
827         h->first = n;
828 }
829
830 /* next must be != NULL */
831 static inline void hlist_add_before(struct hlist_node *n,
832                                         struct hlist_node *next)
833 {
834         n->pprev = next->pprev;
835         n->next = next;
836         next->pprev = &n->next;
837         *(n->pprev) = n;
838 }
839
840 static inline void hlist_add_after(struct hlist_node *n,
841                                         struct hlist_node *next)
842 {
843         next->next = n->next;
844         n->next = next;
845         next->pprev = &n->next;
846
847         if(next->next)
848                 next->next->pprev  = &next->next;
849 }
850
851 /**
852  * hlist_add_before_rcu
853  * @n: the new element to add to the hash list.
854  * @next: the existing element to add the new element before.
855  *
856  * Description:
857  * Adds the specified element to the specified hlist
858  * before the specified node while permitting racing traversals.
859  *
860  * 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
866  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
867  * problems on Alpha CPUs.
868  */
869 static inline void hlist_add_before_rcu(struct hlist_node *n,
870                                         struct hlist_node *next)
871 {
872         n->pprev = next->pprev;
873         n->next = next;
874         smp_wmb();
875         next->pprev = &n->next;
876         *(n->pprev) = n;
877 }
878
879 /**
880  * hlist_add_after_rcu
881  * @prev: the existing element to add the new element after.
882  * @n: the new element to add to the hash list.
883  *
884  * Description:
885  * Adds the specified element to the specified hlist
886  * after the specified node while permitting racing traversals.
887  *
888  * The caller must take whatever precautions are necessary
889  * (such as holding appropriate locks) to avoid racing
890  * with another list-mutation primitive, such as hlist_add_head_rcu()
891  * or hlist_del_rcu(), running on this same list.
892  * However, it is perfectly legal to run concurrently with
893  * the _rcu list-traversal primitives, such as
894  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
895  * problems on Alpha CPUs.
896  */
897 static inline void hlist_add_after_rcu(struct hlist_node *prev,
898                                        struct hlist_node *n)
899 {
900         n->next = prev->next;
901         n->pprev = &prev->next;
902         smp_wmb();
903         prev->next = n;
904         if (n->next)
905                 n->next->pprev = &n->next;
906 }
907
908 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
909
910 #define hlist_for_each(pos, head) \
911         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
912              pos = pos->next)
913
914 #define hlist_for_each_safe(pos, n, head) \
915         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
916              pos = n)
917
918 /**
919  * hlist_for_each_entry - iterate over list of given type
920  * @tpos:       the type * to use as a loop cursor.
921  * @pos:        the &struct hlist_node to use as a loop cursor.
922  * @head:       the head for your list.
923  * @member:     the name of the hlist_node within the struct.
924  */
925 #define hlist_for_each_entry(tpos, pos, head, member)                    \
926         for (pos = (head)->first;                                        \
927              pos && ({ prefetch(pos->next); 1;}) &&                      \
928                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
929              pos = pos->next)
930
931 /**
932  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
933  * @tpos:       the type * to use as a loop cursor.
934  * @pos:        the &struct hlist_node to use as a loop cursor.
935  * @member:     the name of the hlist_node within the struct.
936  */
937 #define hlist_for_each_entry_continue(tpos, pos, member)                 \
938         for (pos = (pos)->next;                                          \
939              pos && ({ prefetch(pos->next); 1;}) &&                      \
940                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
941              pos = pos->next)
942
943 /**
944  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
945  * @tpos:       the type * to use as a loop cursor.
946  * @pos:        the &struct hlist_node to use as a loop cursor.
947  * @member:     the name of the hlist_node within the struct.
948  */
949 #define hlist_for_each_entry_from(tpos, pos, member)                     \
950         for (; pos && ({ prefetch(pos->next); 1;}) &&                    \
951                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
952              pos = pos->next)
953
954 /**
955  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
956  * @tpos:       the type * to use as a loop cursor.
957  * @pos:        the &struct hlist_node to use as a loop cursor.
958  * @n:          another &struct hlist_node to use as temporary storage
959  * @head:       the head for your list.
960  * @member:     the name of the hlist_node within the struct.
961  */
962 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
963         for (pos = (head)->first;                                        \
964              pos && ({ n = pos->next; 1; }) &&                           \
965                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
966              pos = n)
967
968 /**
969  * hlist_for_each_entry_rcu - iterate over rcu list of given type
970  * @tpos:       the type * to use as a loop cursor.
971  * @pos:        the &struct hlist_node to use as a loop cursor.
972  * @head:       the head for your list.
973  * @member:     the name of the hlist_node within the struct.
974  *
975  * This list-traversal primitive may safely run concurrently with
976  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
977  * as long as the traversal is guarded by rcu_read_lock().
978  */
979 #define hlist_for_each_entry_rcu(tpos, pos, head, member)                \
980         for (pos = rcu_dereference((head)->first);                       \
981                 pos && ({ prefetch(pos->next); 1;}) &&                   \
982                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
983              pos = rcu_dereference(pos->next))
984
985 #else
986 #warning "don't include kernel headers in userspace"
987 #endif /* __KERNEL__ */
988 #endif