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_kernel()
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_del_init - deletes entry from list and reinitialize it.
200 * @entry: the element to delete from the list.
202 static inline void list_del_init(struct list_head *entry)
204 __list_del(entry->prev, entry->next);
205 INIT_LIST_HEAD(entry);
209 * list_move - delete from one list and add as another's head
210 * @list: the entry to move
211 * @head: the head that will precede our entry
213 static inline void list_move(struct list_head *list, struct list_head *head)
215 __list_del(list->prev, list->next);
216 list_add(list, head);
220 * list_move_tail - delete from one list and add as another's tail
221 * @list: the entry to move
222 * @head: the head that will follow our entry
224 static inline void list_move_tail(struct list_head *list,
225 struct list_head *head)
227 __list_del(list->prev, list->next);
228 list_add_tail(list, head);
232 * list_empty - tests whether a list is empty
233 * @head: the list to test.
235 static inline int list_empty(const struct list_head *head)
237 return head->next == head;
241 * list_empty_careful - tests whether a list is
242 * empty _and_ checks that no other CPU might be
243 * in the process of still modifying either member
245 * NOTE: using list_empty_careful() without synchronization
246 * can only be safe if the only activity that can happen
247 * to the list entry is list_del_init(). Eg. it cannot be used
248 * if another CPU could re-list_add() it.
250 * @head: the list to test.
252 static inline int list_empty_careful(const struct list_head *head)
254 struct list_head *next = head->next;
255 return (next == head) && (next == head->prev);
258 static inline void __list_splice(struct list_head *list,
259 struct list_head *head)
261 struct list_head *first = list->next;
262 struct list_head *last = list->prev;
263 struct list_head *at = head->next;
273 * list_splice - join two lists
274 * @list: the new list to add.
275 * @head: the place to add it in the first list.
277 static inline void list_splice(struct list_head *list, struct list_head *head)
279 if (!list_empty(list))
280 __list_splice(list, head);
284 * list_splice_init - join two lists and reinitialise the emptied list.
285 * @list: the new list to add.
286 * @head: the place to add it in the first list.
288 * The list at @list is reinitialised
290 static inline void list_splice_init(struct list_head *list,
291 struct list_head *head)
293 if (!list_empty(list)) {
294 __list_splice(list, head);
295 INIT_LIST_HEAD(list);
300 * list_entry - get the struct for this entry
301 * @ptr: the &struct list_head pointer.
302 * @type: the type of the struct this is embedded in.
303 * @member: the name of the list_struct within the struct.
305 #define list_entry(ptr, type, member) \
306 container_of(ptr, type, member)
309 * list_for_each - iterate over a list
310 * @pos: the &struct list_head to use as a loop counter.
311 * @head: the head for your list.
313 #define list_for_each(pos, head) \
314 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
315 pos = pos->next, prefetch(pos->next))
318 * __list_for_each - iterate over a list
319 * @pos: the &struct list_head to use as a loop counter.
320 * @head: the head for your list.
322 * This variant differs from list_for_each() in that it's the
323 * simplest possible list iteration code, no prefetching is done.
324 * Use this for code that knows the list to be very short (empty
325 * or 1 entry) most of the time.
327 #define __list_for_each(pos, head) \
328 for (pos = (head)->next; pos != (head); pos = pos->next)
331 * list_for_each_prev - iterate over a list backwards
332 * @pos: the &struct list_head to use as a loop counter.
333 * @head: the head for your list.
335 #define list_for_each_prev(pos, head) \
336 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
337 pos = pos->prev, prefetch(pos->prev))
340 * list_for_each_safe - iterate over a list safe against removal of list entry
341 * @pos: the &struct list_head to use as a loop counter.
342 * @n: another &struct list_head to use as temporary storage
343 * @head: the head for your list.
345 #define list_for_each_safe(pos, n, head) \
346 for (pos = (head)->next, n = pos->next; pos != (head); \
347 pos = n, n = pos->next)
350 * list_for_each_entry - iterate over list of given type
351 * @pos: the type * to use as a loop counter.
352 * @head: the head for your list.
353 * @member: the name of the list_struct within the struct.
355 #define list_for_each_entry(pos, head, member) \
356 for (pos = list_entry((head)->next, typeof(*pos), member), \
357 prefetch(pos->member.next); \
358 &pos->member != (head); \
359 pos = list_entry(pos->member.next, typeof(*pos), member), \
360 prefetch(pos->member.next))
363 * list_for_each_entry_reverse - iterate backwards over list of given type.
364 * @pos: the type * to use as a loop counter.
365 * @head: the head for your list.
366 * @member: the name of the list_struct within the struct.
368 #define list_for_each_entry_reverse(pos, head, member) \
369 for (pos = list_entry((head)->prev, typeof(*pos), member), \
370 prefetch(pos->member.prev); \
371 &pos->member != (head); \
372 pos = list_entry(pos->member.prev, typeof(*pos), member), \
373 prefetch(pos->member.prev))
376 * list_prepare_entry - prepare a pos entry for use as a start point in
377 * list_for_each_entry_continue
378 * @pos: the type * to use as a start point
379 * @head: the head of the list
380 * @member: the name of the list_struct within the struct.
382 #define list_prepare_entry(pos, head, member) \
383 ((pos) ? : list_entry(head, typeof(*pos), member))
386 * list_for_each_entry_continue - iterate over list of given type
387 * continuing after existing point
388 * @pos: the type * to use as a loop counter.
389 * @head: the head for your list.
390 * @member: the name of the list_struct within the struct.
392 #define list_for_each_entry_continue(pos, head, member) \
393 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
394 prefetch(pos->member.next); \
395 &pos->member != (head); \
396 pos = list_entry(pos->member.next, typeof(*pos), member), \
397 prefetch(pos->member.next))
400 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
401 * @pos: the type * to use as a loop counter.
402 * @n: another type * to use as temporary storage
403 * @head: the head for your list.
404 * @member: the name of the list_struct within the struct.
406 #define list_for_each_entry_safe(pos, n, head, member) \
407 for (pos = list_entry((head)->next, typeof(*pos), member), \
408 n = list_entry(pos->member.next, typeof(*pos), member); \
409 &pos->member != (head); \
410 pos = n, n = list_entry(n->member.next, typeof(*n), member))
413 * list_for_each_rcu - iterate over an rcu-protected list
414 * @pos: the &struct list_head to use as a loop counter.
415 * @head: the head for your list.
417 * This list-traversal primitive may safely run concurrently with
418 * the _rcu list-mutation primitives such as list_add_rcu()
419 * as long as the traversal is guarded by rcu_read_lock().
421 #define list_for_each_rcu(pos, head) \
422 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
423 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
425 #define __list_for_each_rcu(pos, head) \
426 for (pos = (head)->next; pos != (head); \
427 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
430 * list_for_each_safe_rcu - iterate over an rcu-protected list safe
431 * against removal of list entry
432 * @pos: the &struct list_head to use as a loop counter.
433 * @n: another &struct list_head to use as temporary storage
434 * @head: the head for your list.
436 * This list-traversal primitive may safely run concurrently with
437 * the _rcu list-mutation primitives such as list_add_rcu()
438 * as long as the traversal is guarded by rcu_read_lock().
440 #define list_for_each_safe_rcu(pos, n, head) \
441 for (pos = (head)->next, n = pos->next; pos != (head); \
442 pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
445 * list_for_each_entry_rcu - iterate over rcu list of given type
446 * @pos: the type * to use as a loop counter.
447 * @head: the head for your list.
448 * @member: the name of the list_struct within the struct.
450 * This list-traversal primitive may safely run concurrently with
451 * the _rcu list-mutation primitives such as list_add_rcu()
452 * as long as the traversal is guarded by rcu_read_lock().
454 #define list_for_each_entry_rcu(pos, head, member) \
455 for (pos = list_entry((head)->next, typeof(*pos), member), \
456 prefetch(pos->member.next); \
457 &pos->member != (head); \
458 pos = list_entry(pos->member.next, typeof(*pos), member), \
459 ({ smp_read_barrier_depends(); 0;}), \
460 prefetch(pos->member.next))
464 * list_for_each_continue_rcu - iterate over an rcu-protected list
465 * continuing after existing point.
466 * @pos: the &struct list_head to use as a loop counter.
467 * @head: the head for your list.
469 * This list-traversal primitive may safely run concurrently with
470 * the _rcu list-mutation primitives such as list_add_rcu()
471 * as long as the traversal is guarded by rcu_read_lock().
473 #define list_for_each_continue_rcu(pos, head) \
474 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
475 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
478 * Double linked lists with a single pointer list head.
479 * Mostly useful for hash tables where the two pointer list head is
481 * You lose the ability to access the tail in O(1).
485 struct hlist_node *first;
489 struct hlist_node *next, **pprev;
492 #define HLIST_HEAD_INIT { .first = NULL }
493 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
494 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
495 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
497 static inline int hlist_unhashed(const struct hlist_node *h)
502 static inline int hlist_empty(const struct hlist_head *h)
507 static inline void __hlist_del(struct hlist_node *n)
509 struct hlist_node *next = n->next;
510 struct hlist_node **pprev = n->pprev;
516 static inline void hlist_del(struct hlist_node *n)
519 n->next = LIST_POISON1;
520 n->pprev = LIST_POISON2;
524 * hlist_del_rcu - deletes entry from hash list without re-initialization
525 * @n: the element to delete from the hash list.
527 * Note: list_unhashed() on entry does not return true after this,
528 * the entry is in an undefined state. It is useful for RCU based
529 * lockfree traversal.
531 * In particular, it means that we can not poison the forward
532 * pointers that may still be used for walking the hash list.
534 * The caller must take whatever precautions are necessary
535 * (such as holding appropriate locks) to avoid racing
536 * with another list-mutation primitive, such as hlist_add_head_rcu()
537 * or hlist_del_rcu(), running on this same list.
538 * However, it is perfectly legal to run concurrently with
539 * the _rcu list-traversal primitives, such as
540 * hlist_for_each_entry().
542 static inline void hlist_del_rcu(struct hlist_node *n)
545 n->pprev = LIST_POISON2;
548 static inline void hlist_del_init(struct hlist_node *n)
556 #define hlist_del_rcu_init hlist_del_init
558 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
560 struct hlist_node *first = h->first;
563 first->pprev = &n->next;
565 n->pprev = &h->first;
570 * hlist_add_head_rcu - adds the specified element to the specified hlist,
571 * while permitting racing traversals.
572 * @n: the element to add to the hash list.
573 * @h: the list to add to.
575 * The caller must take whatever precautions are necessary
576 * (such as holding appropriate locks) to avoid racing
577 * with another list-mutation primitive, such as hlist_add_head_rcu()
578 * or hlist_del_rcu(), running on this same list.
579 * However, it is perfectly legal to run concurrently with
580 * the _rcu list-traversal primitives, such as
581 * hlist_for_each_entry(), but only if smp_read_barrier_depends()
582 * is used to prevent memory-consistency problems on Alpha CPUs.
583 * Regardless of the type of CPU, the list-traversal primitive
584 * must be guarded by rcu_read_lock().
586 * OK, so why don't we have an hlist_for_each_entry_rcu()???
588 static inline void hlist_add_head_rcu(struct hlist_node *n,
589 struct hlist_head *h)
591 struct hlist_node *first = h->first;
593 n->pprev = &h->first;
596 first->pprev = &n->next;
600 /* next must be != NULL */
601 static inline void hlist_add_before(struct hlist_node *n,
602 struct hlist_node *next)
604 n->pprev = next->pprev;
606 next->pprev = &n->next;
610 static inline void hlist_add_after(struct hlist_node *n,
611 struct hlist_node *next)
613 next->next = n->next;
615 next->pprev = &n->next;
618 next->next->pprev = &next->next;
621 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
623 /* Cannot easily do prefetch unfortunately */
624 #define hlist_for_each(pos, head) \
625 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
628 #define hlist_for_each_safe(pos, n, head) \
629 for (pos = (head)->first; n = pos ? pos->next : 0, pos; \
633 * hlist_for_each_entry - iterate over list of given type
634 * @tpos: the type * to use as a loop counter.
635 * @pos: the &struct hlist_node to use as a loop counter.
636 * @head: the head for your list.
637 * @member: the name of the hlist_node within the struct.
639 #define hlist_for_each_entry(tpos, pos, head, member) \
640 for (pos = (head)->first; \
641 pos && ({ prefetch(pos->next); 1;}) && \
642 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
646 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
647 * @tpos: the type * to use as a loop counter.
648 * @pos: the &struct hlist_node to use as a loop counter.
649 * @member: the name of the hlist_node within the struct.
651 #define hlist_for_each_entry_continue(tpos, pos, member) \
652 for (pos = (pos)->next; \
653 pos && ({ prefetch(pos->next); 1;}) && \
654 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
658 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
659 * @tpos: the type * to use as a loop counter.
660 * @pos: the &struct hlist_node to use as a loop counter.
661 * @member: the name of the hlist_node within the struct.
663 #define hlist_for_each_entry_from(tpos, pos, member) \
664 for (; pos && ({ prefetch(pos->next); 1;}) && \
665 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
669 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
670 * @tpos: the type * to use as a loop counter.
671 * @pos: the &struct hlist_node to use as a loop counter.
672 * @n: another &struct hlist_node to use as temporary storage
673 * @head: the head for your list.
674 * @member: the name of the hlist_node within the struct.
676 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
677 for (pos = (head)->first; \
678 pos && ({ n = pos->next; 1; }) && \
679 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
682 #warning "don't include kernel headers in userspace"
683 #endif /* __KERNEL__ */