6 #include <linux/stddef.h>
7 #include <linux/prefetch.h>
8 #include <asm/system.h>
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.
15 #define LIST_POISON1 ((void *) 0x00100100)
16 #define LIST_POISON2 ((void *) 0x00200200)
19 * Simple doubly linked list implementation.
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.
29 struct list_head
*next
, *prev
;
32 #define LIST_HEAD_INIT(name) { &(name), &(name) }
34 #define LIST_HEAD(name) \
35 struct list_head name = LIST_HEAD_INIT(name)
37 #define INIT_LIST_HEAD(ptr) do { \
38 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
42 * Insert a new entry between two known consecutive entries.
44 * This is only for internal list manipulation where we know
45 * the prev/next entries already!
47 static inline void __list_add(struct list_head
*new,
48 struct list_head
*prev
,
49 struct list_head
*next
)
58 * list_add - add a new entry
59 * @new: new entry to be added
60 * @head: list head to add it after
62 * Insert a new entry after the specified head.
63 * This is good for implementing stacks.
65 static inline void list_add(struct list_head
*new, struct list_head
*head
)
67 __list_add(new, head
, head
->next
);
71 * list_add_tail - add a new entry
72 * @new: new entry to be added
73 * @head: list head to add it before
75 * Insert a new entry before the specified head.
76 * This is useful for implementing queues.
78 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
80 __list_add(new, head
->prev
, head
);
84 * Insert a new entry between two known consecutive entries.
86 * This is only for internal list manipulation where we know
87 * the prev/next entries already!
89 static inline void __list_add_rcu(struct list_head
* new,
90 struct list_head
* prev
, struct list_head
* next
)
100 * list_add_rcu - add a new entry to rcu-protected list
101 * @new: new entry to be added
102 * @head: list head to add it after
104 * Insert a new entry after the specified head.
105 * This is good for implementing stacks.
107 * The caller must take whatever precautions are necessary
108 * (such as holding appropriate locks) to avoid racing
109 * with another list-mutation primitive, such as list_add_rcu()
110 * or list_del_rcu(), running on this same list.
111 * However, it is perfectly legal to run concurrently with
112 * the _rcu list-traversal primitives, such as
113 * list_for_each_entry_rcu().
115 static inline void list_add_rcu(struct list_head
*new, struct list_head
*head
)
117 __list_add_rcu(new, head
, head
->next
);
121 * list_add_tail_rcu - add a new entry to rcu-protected list
122 * @new: new entry to be added
123 * @head: list head to add it before
125 * Insert a new entry before the specified head.
126 * This is useful for implementing queues.
128 * The caller must take whatever precautions are necessary
129 * (such as holding appropriate locks) to avoid racing
130 * with another list-mutation primitive, such as list_add_tail_rcu()
131 * or list_del_rcu(), running on this same list.
132 * However, it is perfectly legal to run concurrently with
133 * the _rcu list-traversal primitives, such as
134 * list_for_each_entry_rcu().
136 static inline void list_add_tail_rcu(struct list_head
*new,
137 struct list_head
*head
)
139 __list_add_rcu(new, head
->prev
, head
);
143 * Delete a list entry by making the prev/next entries
144 * point to each other.
146 * This is only for internal list manipulation where we know
147 * the prev/next entries already!
149 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
156 * list_del - deletes entry from list.
157 * @entry: the element to delete from the list.
158 * Note: list_empty on entry does not return true after this, the entry is
159 * in an undefined state.
161 static inline void list_del(struct list_head
*entry
)
163 __list_del(entry
->prev
, entry
->next
);
164 entry
->next
= LIST_POISON1
;
165 entry
->prev
= LIST_POISON2
;
169 * list_del_rcu - deletes entry from list without re-initialization
170 * @entry: the element to delete from the list.
172 * Note: list_empty on entry does not return true after this,
173 * the entry is in an undefined state. It is useful for RCU based
174 * lockfree traversal.
176 * In particular, it means that we can not poison the forward
177 * pointers that may still be used for walking the list.
179 * The caller must take whatever precautions are necessary
180 * (such as holding appropriate locks) to avoid racing
181 * with another list-mutation primitive, such as list_del_rcu()
182 * or list_add_rcu(), running on this same list.
183 * However, it is perfectly legal to run concurrently with
184 * the _rcu list-traversal primitives, such as
185 * list_for_each_entry_rcu().
187 * Note that the caller is not permitted to immediately free
188 * the newly deleted entry. Instead, either synchronize_rcu()
189 * or call_rcu() must be used to defer freeing until an RCU
190 * grace period has elapsed.
192 static inline void list_del_rcu(struct list_head
*entry
)
194 __list_del(entry
->prev
, entry
->next
);
195 entry
->prev
= LIST_POISON2
;
199 * list_replace_rcu - replace old entry by new one
200 * @old : the element to be replaced
201 * @new : the new element to insert
203 * The old entry will be replaced with the new entry atomically.
205 static inline void list_replace_rcu(struct list_head
*old
,
206 struct list_head
*new)
208 new->next
= old
->next
;
209 new->prev
= old
->prev
;
211 new->next
->prev
= new;
212 new->prev
->next
= new;
213 old
->prev
= LIST_POISON2
;
217 * list_del_init - deletes entry from list and reinitialize it.
218 * @entry: the element to delete from the list.
220 static inline void list_del_init(struct list_head
*entry
)
222 __list_del(entry
->prev
, entry
->next
);
223 INIT_LIST_HEAD(entry
);
227 * list_move - delete from one list and add as another's head
228 * @list: the entry to move
229 * @head: the head that will precede our entry
231 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
233 __list_del(list
->prev
, list
->next
);
234 list_add(list
, head
);
238 * list_move_tail - delete from one list and add as another's tail
239 * @list: the entry to move
240 * @head: the head that will follow our entry
242 static inline void list_move_tail(struct list_head
*list
,
243 struct list_head
*head
)
245 __list_del(list
->prev
, list
->next
);
246 list_add_tail(list
, head
);
250 * list_empty - tests whether a list is empty
251 * @head: the list to test.
253 static inline int list_empty(const struct list_head
*head
)
255 return head
->next
== head
;
259 * list_empty_careful - tests whether a list is
260 * empty _and_ checks that no other CPU might be
261 * in the process of still modifying either member
263 * NOTE: using list_empty_careful() without synchronization
264 * can only be safe if the only activity that can happen
265 * to the list entry is list_del_init(). Eg. it cannot be used
266 * if another CPU could re-list_add() it.
268 * @head: the list to test.
270 static inline int list_empty_careful(const struct list_head
*head
)
272 struct list_head
*next
= head
->next
;
273 return (next
== head
) && (next
== head
->prev
);
276 static inline void __list_splice(struct list_head
*list
,
277 struct list_head
*head
)
279 struct list_head
*first
= list
->next
;
280 struct list_head
*last
= list
->prev
;
281 struct list_head
*at
= head
->next
;
291 * list_splice - join two lists
292 * @list: the new list to add.
293 * @head: the place to add it in the first list.
295 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
297 if (!list_empty(list
))
298 __list_splice(list
, head
);
302 * list_splice_init - join two lists and reinitialise the emptied list.
303 * @list: the new list to add.
304 * @head: the place to add it in the first list.
306 * The list at @list is reinitialised
308 static inline void list_splice_init(struct list_head
*list
,
309 struct list_head
*head
)
311 if (!list_empty(list
)) {
312 __list_splice(list
, head
);
313 INIT_LIST_HEAD(list
);
318 * list_entry - get the struct for this entry
319 * @ptr: the &struct list_head pointer.
320 * @type: the type of the struct this is embedded in.
321 * @member: the name of the list_struct within the struct.
323 #define list_entry(ptr, type, member) \
324 container_of(ptr, type, member)
327 * list_for_each - iterate over a list
328 * @pos: the &struct list_head to use as a loop counter.
329 * @head: the head for your list.
331 #define list_for_each(pos, head) \
332 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
336 * __list_for_each - iterate over a list
337 * @pos: the &struct list_head to use as a loop counter.
338 * @head: the head for your list.
340 * This variant differs from list_for_each() in that it's the
341 * simplest possible list iteration code, no prefetching is done.
342 * Use this for code that knows the list to be very short (empty
343 * or 1 entry) most of the time.
345 #define __list_for_each(pos, head) \
346 for (pos = (head)->next; pos != (head); pos = pos->next)
349 * list_for_each_prev - iterate over a list backwards
350 * @pos: the &struct list_head to use as a loop counter.
351 * @head: the head for your list.
353 #define list_for_each_prev(pos, head) \
354 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
358 * list_for_each_safe - iterate over a list safe against removal of list entry
359 * @pos: the &struct list_head to use as a loop counter.
360 * @n: another &struct list_head to use as temporary storage
361 * @head: the head for your list.
363 #define list_for_each_safe(pos, n, head) \
364 for (pos = (head)->next, n = pos->next; pos != (head); \
365 pos = n, n = pos->next)
368 * list_for_each_entry - iterate over list of given type
369 * @pos: the type * to use as a loop counter.
370 * @head: the head for your list.
371 * @member: the name of the list_struct within the struct.
373 #define list_for_each_entry(pos, head, member) \
374 for (pos = list_entry((head)->next, typeof(*pos), member); \
375 prefetch(pos->member.next), &pos->member != (head); \
376 pos = list_entry(pos->member.next, typeof(*pos), member))
379 * list_for_each_entry_reverse - iterate backwards over list of given type.
380 * @pos: the type * to use as a loop counter.
381 * @head: the head for your list.
382 * @member: the name of the list_struct within the struct.
384 #define list_for_each_entry_reverse(pos, head, member) \
385 for (pos = list_entry((head)->prev, typeof(*pos), member); \
386 prefetch(pos->member.prev), &pos->member != (head); \
387 pos = list_entry(pos->member.prev, typeof(*pos), member))
390 * list_prepare_entry - prepare a pos entry for use as a start point in
391 * list_for_each_entry_continue
392 * @pos: the type * to use as a start point
393 * @head: the head of the list
394 * @member: the name of the list_struct within the struct.
396 #define list_prepare_entry(pos, head, member) \
397 ((pos) ? : list_entry(head, typeof(*pos), member))
400 * list_for_each_entry_continue - iterate over list of given type
401 * continuing after existing point
402 * @pos: the type * to use as a loop counter.
403 * @head: the head for your list.
404 * @member: the name of the list_struct within the struct.
406 #define list_for_each_entry_continue(pos, head, member) \
407 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
408 prefetch(pos->member.next), &pos->member != (head); \
409 pos = list_entry(pos->member.next, typeof(*pos), member))
412 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
413 * @pos: the type * to use as a loop counter.
414 * @n: another type * to use as temporary storage
415 * @head: the head for your list.
416 * @member: the name of the list_struct within the struct.
418 #define list_for_each_entry_safe(pos, n, head, member) \
419 for (pos = list_entry((head)->next, typeof(*pos), member), \
420 n = list_entry(pos->member.next, typeof(*pos), member); \
421 &pos->member != (head); \
422 pos = n, n = list_entry(n->member.next, typeof(*n), member))
425 * list_for_each_entry_safe_continue - iterate over list of given type
426 * continuing after existing point safe against removal of list entry
427 * @pos: the type * to use as a loop counter.
428 * @n: another type * to use as temporary storage
429 * @head: the head for your list.
430 * @member: the name of the list_struct within the struct.
432 #define list_for_each_entry_safe_continue(pos, n, head, member) \
433 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
434 n = list_entry(pos->member.next, typeof(*pos), member); \
435 &pos->member != (head); \
436 pos = n, n = list_entry(n->member.next, typeof(*n), member))
439 * list_for_each_rcu - iterate over an rcu-protected list
440 * @pos: the &struct list_head to use as a loop counter.
441 * @head: the head for your list.
443 * This list-traversal primitive may safely run concurrently with
444 * the _rcu list-mutation primitives such as list_add_rcu()
445 * as long as the traversal is guarded by rcu_read_lock().
447 #define list_for_each_rcu(pos, head) \
448 for (pos = (head)->next; \
449 prefetch(rcu_dereference(pos)->next), pos != (head); \
452 #define __list_for_each_rcu(pos, head) \
453 for (pos = (head)->next; \
454 rcu_dereference(pos) != (head); \
458 * list_for_each_safe_rcu - iterate over an rcu-protected list safe
459 * against removal of list entry
460 * @pos: the &struct list_head to use as a loop counter.
461 * @n: another &struct list_head to use as temporary storage
462 * @head: the head for your list.
464 * This list-traversal primitive may safely run concurrently with
465 * the _rcu list-mutation primitives such as list_add_rcu()
466 * as long as the traversal is guarded by rcu_read_lock().
468 #define list_for_each_safe_rcu(pos, n, head) \
469 for (pos = (head)->next; \
470 n = rcu_dereference(pos)->next, pos != (head); \
474 * list_for_each_entry_rcu - iterate over rcu list of given type
475 * @pos: the type * to use as a loop counter.
476 * @head: the head for your list.
477 * @member: the name of the list_struct within the struct.
479 * This list-traversal primitive may safely run concurrently with
480 * the _rcu list-mutation primitives such as list_add_rcu()
481 * as long as the traversal is guarded by rcu_read_lock().
483 #define list_for_each_entry_rcu(pos, head, member) \
484 for (pos = list_entry((head)->next, typeof(*pos), member); \
485 prefetch(rcu_dereference(pos)->member.next), \
486 &pos->member != (head); \
487 pos = list_entry(pos->member.next, typeof(*pos), member))
491 * list_for_each_continue_rcu - iterate over an rcu-protected list
492 * continuing after existing point.
493 * @pos: the &struct list_head to use as a loop counter.
494 * @head: the head for your list.
496 * This list-traversal primitive may safely run concurrently with
497 * the _rcu list-mutation primitives such as list_add_rcu()
498 * as long as the traversal is guarded by rcu_read_lock().
500 #define list_for_each_continue_rcu(pos, head) \
501 for ((pos) = (pos)->next; \
502 prefetch(rcu_dereference((pos))->next), (pos) != (head); \
506 * Double linked lists with a single pointer list head.
507 * Mostly useful for hash tables where the two pointer list head is
509 * You lose the ability to access the tail in O(1).
513 struct hlist_node
*first
;
517 struct hlist_node
*next
, **pprev
;
520 #define HLIST_HEAD_INIT { .first = NULL }
521 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
522 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
523 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
525 static inline int hlist_unhashed(const struct hlist_node
*h
)
530 static inline int hlist_empty(const struct hlist_head
*h
)
535 static inline void __hlist_del(struct hlist_node
*n
)
537 struct hlist_node
*next
= n
->next
;
538 struct hlist_node
**pprev
= n
->pprev
;
544 static inline void hlist_del(struct hlist_node
*n
)
547 n
->next
= LIST_POISON1
;
548 n
->pprev
= LIST_POISON2
;
552 * hlist_del_rcu - deletes entry from hash list without re-initialization
553 * @n: the element to delete from the hash list.
555 * Note: list_unhashed() on entry does not return true after this,
556 * the entry is in an undefined state. It is useful for RCU based
557 * lockfree traversal.
559 * In particular, it means that we can not poison the forward
560 * pointers that may still be used for walking the hash list.
562 * The caller must take whatever precautions are necessary
563 * (such as holding appropriate locks) to avoid racing
564 * with another list-mutation primitive, such as hlist_add_head_rcu()
565 * or hlist_del_rcu(), running on this same list.
566 * However, it is perfectly legal to run concurrently with
567 * the _rcu list-traversal primitives, such as
568 * hlist_for_each_entry().
570 static inline void hlist_del_rcu(struct hlist_node
*n
)
573 n
->pprev
= LIST_POISON2
;
576 static inline void hlist_del_init(struct hlist_node
*n
)
585 * hlist_replace_rcu - replace old entry by new one
586 * @old : the element to be replaced
587 * @new : the new element to insert
589 * The old entry will be replaced with the new entry atomically.
591 static inline void hlist_replace_rcu(struct hlist_node
*old
,
592 struct hlist_node
*new)
594 struct hlist_node
*next
= old
->next
;
597 new->pprev
= old
->pprev
;
600 new->next
->pprev
= &new->next
;
602 old
->pprev
= LIST_POISON2
;
605 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
607 struct hlist_node
*first
= h
->first
;
610 first
->pprev
= &n
->next
;
612 n
->pprev
= &h
->first
;
617 * hlist_add_head_rcu - adds the specified element to the specified hlist,
618 * while permitting racing traversals.
619 * @n: the element to add to the hash list.
620 * @h: the list to add to.
622 * The caller must take whatever precautions are necessary
623 * (such as holding appropriate locks) to avoid racing
624 * with another list-mutation primitive, such as hlist_add_head_rcu()
625 * or hlist_del_rcu(), running on this same list.
626 * However, it is perfectly legal to run concurrently with
627 * the _rcu list-traversal primitives, such as
628 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
629 * problems on Alpha CPUs. Regardless of the type of CPU, the
630 * list-traversal primitive must be guarded by rcu_read_lock().
632 static inline void hlist_add_head_rcu(struct hlist_node
*n
,
633 struct hlist_head
*h
)
635 struct hlist_node
*first
= h
->first
;
637 n
->pprev
= &h
->first
;
640 first
->pprev
= &n
->next
;
644 /* next must be != NULL */
645 static inline void hlist_add_before(struct hlist_node
*n
,
646 struct hlist_node
*next
)
648 n
->pprev
= next
->pprev
;
650 next
->pprev
= &n
->next
;
654 static inline void hlist_add_after(struct hlist_node
*n
,
655 struct hlist_node
*next
)
657 next
->next
= n
->next
;
659 next
->pprev
= &n
->next
;
662 next
->next
->pprev
= &next
->next
;
666 * hlist_add_before_rcu - adds the specified element to the specified hlist
667 * before the specified node while permitting racing traversals.
668 * @n: the new element to add to the hash list.
669 * @next: the existing element to add the new element before.
671 * The caller must take whatever precautions are necessary
672 * (such as holding appropriate locks) to avoid racing
673 * with another list-mutation primitive, such as hlist_add_head_rcu()
674 * or hlist_del_rcu(), running on this same list.
675 * However, it is perfectly legal to run concurrently with
676 * the _rcu list-traversal primitives, such as
677 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
678 * problems on Alpha CPUs.
680 static inline void hlist_add_before_rcu(struct hlist_node
*n
,
681 struct hlist_node
*next
)
683 n
->pprev
= next
->pprev
;
686 next
->pprev
= &n
->next
;
691 * hlist_add_after_rcu - adds the specified element to the specified hlist
692 * after the specified node while permitting racing traversals.
693 * @prev: the existing element to add the new element after.
694 * @n: the new element to add to the hash list.
696 * The caller must take whatever precautions are necessary
697 * (such as holding appropriate locks) to avoid racing
698 * with another list-mutation primitive, such as hlist_add_head_rcu()
699 * or hlist_del_rcu(), running on this same list.
700 * However, it is perfectly legal to run concurrently with
701 * the _rcu list-traversal primitives, such as
702 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
703 * problems on Alpha CPUs.
705 static inline void hlist_add_after_rcu(struct hlist_node
*prev
,
706 struct hlist_node
*n
)
708 n
->next
= prev
->next
;
709 n
->pprev
= &prev
->next
;
713 n
->next
->pprev
= &n
->next
;
716 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
718 #define hlist_for_each(pos, head) \
719 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
722 #define hlist_for_each_safe(pos, n, head) \
723 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
727 * hlist_for_each_entry - iterate over list of given type
728 * @tpos: the type * to use as a loop counter.
729 * @pos: the &struct hlist_node to use as a loop counter.
730 * @head: the head for your list.
731 * @member: the name of the hlist_node within the struct.
733 #define hlist_for_each_entry(tpos, pos, head, member) \
734 for (pos = (head)->first; \
735 pos && ({ prefetch(pos->next); 1;}) && \
736 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
740 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
741 * @tpos: the type * to use as a loop counter.
742 * @pos: the &struct hlist_node to use as a loop counter.
743 * @member: the name of the hlist_node within the struct.
745 #define hlist_for_each_entry_continue(tpos, pos, member) \
746 for (pos = (pos)->next; \
747 pos && ({ prefetch(pos->next); 1;}) && \
748 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
752 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
753 * @tpos: the type * to use as a loop counter.
754 * @pos: the &struct hlist_node to use as a loop counter.
755 * @member: the name of the hlist_node within the struct.
757 #define hlist_for_each_entry_from(tpos, pos, member) \
758 for (; pos && ({ prefetch(pos->next); 1;}) && \
759 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
763 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
764 * @tpos: the type * to use as a loop counter.
765 * @pos: the &struct hlist_node to use as a loop counter.
766 * @n: another &struct hlist_node to use as temporary storage
767 * @head: the head for your list.
768 * @member: the name of the hlist_node within the struct.
770 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
771 for (pos = (head)->first; \
772 pos && ({ n = pos->next; 1; }) && \
773 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
777 * hlist_for_each_entry_rcu - iterate over rcu list of given type
778 * @tpos: the type * to use as a loop counter.
779 * @pos: the &struct hlist_node to use as a loop counter.
780 * @head: the head for your list.
781 * @member: the name of the hlist_node within the struct.
783 * This list-traversal primitive may safely run concurrently with
784 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
785 * as long as the traversal is guarded by rcu_read_lock().
787 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
788 for (pos = (head)->first; \
789 rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \
790 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
794 #warning "don't include kernel headers in userspace"
795 #endif /* __KERNEL__ */