7 #include <linux/stddef.h>
8 #include <linux/prefetch.h>
9 #include <asm/system.h>
11 #include <linux/kernel.h>
12 #include <linux/prefetch.h>
16 * These are non-NULL pointers that will result in page faults
17 * under normal circumstances, used to verify that nobody uses
18 * non-initialized list entries.
20 #define LIST_POISON1 ((void *) 0x00100100)
21 #define LIST_POISON2 ((void *) 0x00200200)
24 * Simple doubly linked list implementation.
26 * Some of the internal functions ("__xxx") are useful when
27 * manipulating whole lists rather than single entries, as
28 * sometimes we already know the next/prev entries and we can
29 * generate better code by using them directly rather than
30 * using the generic single-entry routines.
34 struct list_head *next, *prev;
37 #define LIST_HEAD_INIT(name) { &(name), &(name) }
39 #define LIST_HEAD(name) \
40 struct list_head name = LIST_HEAD_INIT(name)
42 #define INIT_LIST_HEAD(ptr) do { \
43 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
47 * Insert a new entry between two known consecutive entries.
49 * This is only for internal list manipulation where we know
50 * the prev/next entries already!
52 static inline void __list_add(struct list_head *new,
53 struct list_head *prev,
54 struct list_head *next)
63 * list_add - add a new entry
64 * @new: new entry to be added
65 * @head: list head to add it after
67 * Insert a new entry after the specified head.
68 * This is good for implementing stacks.
70 static inline void list_add(struct list_head *new, struct list_head *head)
72 __list_add(new, head, head->next);
76 * list_add_tail - add a new entry
77 * @new: new entry to be added
78 * @head: list head to add it before
80 * Insert a new entry before the specified head.
81 * This is useful for implementing queues.
83 static inline void list_add_tail(struct list_head *new, struct list_head *head)
85 __list_add(new, head->prev, head);
89 * Insert a new entry between two known consecutive entries.
91 * This is only for internal list manipulation where we know
92 * the prev/next entries already!
94 static __inline__ void __list_add_rcu(struct list_head * new,
95 struct list_head * prev,
96 struct list_head * next)
108 * list_add_rcu - add a new entry to rcu-protected list
109 * @new: new entry to be added
110 * @head: list head to add it after
112 * Insert a new entry after the specified head.
113 * This is good for implementing stacks.
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 static __inline__ void list_add_tail_rcu(struct list_head *new, struct list_head *head)
130 __list_add_rcu(new, head->prev, head);
134 * Delete a list entry by making the prev/next entries
135 * point to each other.
137 * This is only for internal list manipulation where we know
138 * the prev/next entries already!
140 static inline void __list_del(struct list_head * prev, struct list_head * next)
147 * list_del - deletes entry from list.
148 * @entry: the element to delete from the list.
149 * Note: list_empty on entry does not return true after this, the entry is
150 * in an undefined state.
152 static inline void list_del(struct list_head *entry)
154 __list_del(entry->prev, entry->next);
155 entry->next = LIST_POISON1;
156 entry->prev = LIST_POISON2;
160 * list_del_rcu - deletes entry from list without re-initialization
161 * @entry: the element to delete from the list.
163 * Note: list_empty on entry does not return true after this,
164 * the entry is in an undefined state. It is useful for RCU based
165 * lockfree traversal.
167 * In particular, it means that we can not poison the forward
168 * pointers that may still be used for walking the list.
170 static inline void list_del_rcu(struct list_head *entry)
172 __list_del(entry->prev, entry->next);
173 entry->prev = LIST_POISON2;
177 * list_del_init - deletes entry from list and reinitialize it.
178 * @entry: the element to delete from the list.
180 static inline void list_del_init(struct list_head *entry)
182 __list_del(entry->prev, entry->next);
183 INIT_LIST_HEAD(entry);
187 * list_move - delete from one list and add as another's head
188 * @list: the entry to move
189 * @head: the head that will precede our entry
191 static inline void list_move(struct list_head *list, struct list_head *head)
193 __list_del(list->prev, list->next);
194 list_add(list, head);
198 * list_move_tail - delete from one list and add as another's tail
199 * @list: the entry to move
200 * @head: the head that will follow our entry
202 static inline void list_move_tail(struct list_head *list,
203 struct list_head *head)
205 __list_del(list->prev, list->next);
206 list_add_tail(list, head);
210 * list_empty - tests whether a list is empty
211 * @head: the list to test.
213 static inline int list_empty(const struct list_head *head)
215 return head->next == head;
218 static inline void __list_splice(struct list_head *list,
219 struct list_head *head)
221 struct list_head *first = list->next;
222 struct list_head *last = list->prev;
223 struct list_head *at = head->next;
233 * list_splice - join two lists
234 * @list: the new list to add.
235 * @head: the place to add it in the first list.
237 static inline void list_splice(struct list_head *list, struct list_head *head)
239 if (!list_empty(list))
240 __list_splice(list, head);
244 * list_splice_init - join two lists and reinitialise the emptied list.
245 * @list: the new list to add.
246 * @head: the place to add it in the first list.
248 * The list at @list is reinitialised
250 static inline void list_splice_init(struct list_head *list,
251 struct list_head *head)
253 if (!list_empty(list)) {
254 __list_splice(list, head);
255 INIT_LIST_HEAD(list);
260 * list_entry - get the struct for this entry
261 * @ptr: the &struct list_head pointer.
262 * @type: the type of the struct this is embedded in.
263 * @member: the name of the list_struct within the struct.
265 #define list_entry(ptr, type, member) \
266 container_of(ptr, type, member)
269 * list_for_each - iterate over a list
270 * @pos: the &struct list_head to use as a loop counter.
271 * @head: the head for your list.
273 #define list_for_each(pos, head) \
274 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
275 pos = pos->next, prefetch(pos->next))
278 * __list_for_each - iterate over a list
279 * @pos: the &struct list_head to use as a loop counter.
280 * @head: the head for your list.
282 * This variant differs from list_for_each() in that it's the
283 * simplest possible list iteration code, no prefetching is done.
284 * Use this for code that knows the list to be very short (empty
285 * or 1 entry) most of the time.
287 #define __list_for_each(pos, head) \
288 for (pos = (head)->next; pos != (head); pos = pos->next)
291 * list_for_each_prev - iterate over a list backwards
292 * @pos: the &struct list_head to use as a loop counter.
293 * @head: the head for your list.
295 #define list_for_each_prev(pos, head) \
296 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
297 pos = pos->prev, prefetch(pos->prev))
300 * list_for_each_safe - iterate over a list safe against removal of list entry
301 * @pos: the &struct list_head to use as a loop counter.
302 * @n: another &struct list_head to use as temporary storage
303 * @head: the head for your list.
305 #define list_for_each_safe(pos, n, head) \
306 for (pos = (head)->next, n = pos->next; pos != (head); \
307 pos = n, n = pos->next)
310 * list_for_each_entry - iterate over list of given type
311 * @pos: the type * to use as a loop counter.
312 * @head: the head for your list.
313 * @member: the name of the list_struct within the struct.
315 #define list_for_each_entry(pos, head, member) \
316 for (pos = list_entry((head)->next, typeof(*pos), member), \
317 prefetch(pos->member.next); \
318 &pos->member != (head); \
319 pos = list_entry(pos->member.next, typeof(*pos), member), \
320 prefetch(pos->member.next))
323 * list_for_each_entry_reverse - iterate backwards over list of given type.
324 * @pos: the type * to use as a loop counter.
325 * @head: the head for your list.
326 * @member: the name of the list_struct within the struct.
328 #define list_for_each_entry_reverse(pos, head, member) \
329 for (pos = list_entry((head)->prev, typeof(*pos), member), \
330 prefetch(pos->member.prev); \
331 &pos->member != (head); \
332 pos = list_entry(pos->member.prev, typeof(*pos), member), \
333 prefetch(pos->member.prev))
336 * list_for_each_entry_continue - iterate over list of given type
337 * continuing after existing point
338 * @pos: the type * to use as a loop counter.
339 * @head: the head for your list.
340 * @member: the name of the list_struct within the struct.
342 #define list_for_each_entry_continue(pos, head, member) \
343 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
344 prefetch(pos->member.next); \
345 &pos->member != (head); \
346 pos = list_entry(pos->member.next, typeof(*pos), member), \
347 prefetch(pos->member.next))
350 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
351 * @pos: the type * to use as a loop counter.
352 * @n: another type * to use as temporary storage
353 * @head: the head for your list.
354 * @member: the name of the list_struct within the struct.
356 #define list_for_each_entry_safe(pos, n, head, member) \
357 for (pos = list_entry((head)->next, typeof(*pos), member), \
358 n = list_entry(pos->member.next, typeof(*pos), member); \
359 &pos->member != (head); \
360 pos = n, n = list_entry(n->member.next, typeof(*n), member))
363 * list_for_each_rcu - iterate over an rcu-protected list
364 * @pos: the &struct list_head to use as a loop counter.
365 * @head: the head for your list.
367 #define list_for_each_rcu(pos, head) \
368 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
369 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
371 #define __list_for_each_rcu(pos, head) \
372 for (pos = (head)->next; pos != (head); \
373 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
376 * list_for_each_safe_rcu - iterate over an rcu-protected list safe
377 * against removal of list entry
378 * @pos: the &struct list_head to use as a loop counter.
379 * @n: another &struct list_head to use as temporary storage
380 * @head: the head for your list.
382 #define list_for_each_safe_rcu(pos, n, head) \
383 for (pos = (head)->next, n = pos->next; pos != (head); \
384 pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
387 * list_for_each_entry_rcu - iterate over rcu list of given type
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_rcu(pos, head, member) \
393 for (pos = list_entry((head)->next, typeof(*pos), member), \
394 prefetch(pos->member.next); \
395 &pos->member != (head); \
396 pos = list_entry(pos->member.next, typeof(*pos), member), \
397 ({ smp_read_barrier_depends(); 0;}), \
398 prefetch(pos->member.next))
402 * list_for_each_continue_rcu - iterate over an rcu-protected list
403 * continuing after existing point.
404 * @pos: the &struct list_head to use as a loop counter.
405 * @head: the head for your list.
407 #define list_for_each_continue_rcu(pos, head) \
408 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
409 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
412 * Double linked lists with a single pointer list head.
413 * Mostly useful for hash tables where the two pointer list head is
415 * You lose the ability to access the tail in O(1).
419 struct hlist_node *first;
423 struct hlist_node *next, **pprev;
426 #define HLIST_HEAD_INIT { .first = NULL }
427 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
428 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
429 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
431 static __inline__ int hlist_unhashed(const struct hlist_node *h)
436 static __inline__ int hlist_empty(const struct hlist_head *h)
441 static __inline__ void __hlist_del(struct hlist_node *n)
443 struct hlist_node *next = n->next;
444 struct hlist_node **pprev = n->pprev;
450 static __inline__ void hlist_del(struct hlist_node *n)
453 n->next = LIST_POISON1;
454 n->pprev = LIST_POISON2;
458 * hlist_del_rcu - deletes entry from hash list without re-initialization
459 * @n: the element to delete from the hash list.
461 * Note: list_unhashed() on entry does not return true after this,
462 * the entry is in an undefined state. It is useful for RCU based
463 * lockfree traversal.
465 * In particular, it means that we can not poison the forward
466 * pointers that may still be used for walking the hash list.
468 static inline void hlist_del_rcu(struct hlist_node *n)
471 n->pprev = LIST_POISON2;
474 static __inline__ void hlist_del_init(struct hlist_node *n)
482 #define hlist_del_rcu_init hlist_del_init
484 static __inline__ void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
486 struct hlist_node *first = h->first;
489 first->pprev = &n->next;
491 n->pprev = &h->first;
494 static __inline__ void hlist_add_head_rcu(struct hlist_node *n, struct hlist_head *h)
496 struct hlist_node *first = h->first;
498 n->pprev = &h->first;
503 first->pprev = &n->next;
507 /* next must be != NULL */
508 static __inline__ void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
510 n->pprev = next->pprev;
512 next->pprev = &n->next;
516 static __inline__ void hlist_add_after(struct hlist_node *n,
517 struct hlist_node *next)
519 next->next = n->next;
524 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
526 /* Cannot easily do prefetch unfortunately */
527 #define hlist_for_each(pos, head) \
528 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
531 #define hlist_for_each_safe(pos, n, head) \
532 for (pos = (head)->first; n = pos ? pos->next : 0, pos; \
536 * hlist_for_each_entry - iterate over list of given type
537 * @tpos: the type * to use as a loop counter.
538 * @pos: the &struct hlist_node to use as a loop counter.
539 * @head: the head for your list.
540 * @member: the name of the hlist_node within the struct.
542 #define hlist_for_each_entry(tpos, pos, head, member) \
543 for (pos = (head)->first; \
544 pos && ({ prefetch(pos->next); 1;}) && \
545 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
549 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
550 * @tpos: the type * to use as a loop counter.
551 * @pos: the &struct hlist_node to use as a loop counter.
552 * @member: the name of the hlist_node within the struct.
554 #define hlist_for_each_entry_continue(tpos, pos, member) \
555 for (pos = (pos)->next; \
556 pos && ({ prefetch(pos->next); 1;}) && \
557 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
561 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
562 * @tpos: the type * to use as a loop counter.
563 * @pos: the &struct hlist_node to use as a loop counter.
564 * @member: the name of the hlist_node within the struct.
566 #define hlist_for_each_entry_from(tpos, pos, member) \
567 for (; pos && ({ prefetch(pos->next); 1;}) && \
568 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
572 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
573 * @tpos: the type * to use as a loop counter.
574 * @pos: the &struct hlist_node to use as a loop counter.
575 * @n: another &struct hlist_node to use as temporary storage
576 * @head: the head for your list.
577 * @member: the name of the hlist_node within the struct.
579 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
580 for (pos = (head)->first; \
581 pos && ({ n = pos->next; 1; }) && \
582 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
585 #warning "don't include kernel headers in userspace"
586 #endif /* __KERNEL__ */