1 /******************************************************************************
2 * list.h
3 *
4 * Useful linked-list definitions taken from the Linux kernel (2.6.18).
5 */
6
7 #ifndef __XEN_LIST_H__
8 #define __XEN_LIST_H__
9
10 #include <xen/lib.h>
11 #include <asm/system.h>
12
13 /*
14 * These are non-NULL pointers that will result in faults under normal
15 * circumstances, used to verify that nobody uses non-initialized list
16 * entries. Architectures can override these.
17 */
18 #ifndef LIST_POISON1
19 #define LIST_POISON1 ((void *) 0x00100100)
20 #define LIST_POISON2 ((void *) 0x00200200)
21 #endif
22
23 /*
24 * Simple doubly linked list implementation.
25 *
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.
31 */
32
33 struct list_head {
34 struct list_head *next, *prev;
35 };
36
37 #define LIST_HEAD_INIT(name) { &(name), &(name) }
38
39 #define LIST_HEAD(name) \
40 struct list_head name = LIST_HEAD_INIT(name)
41
42 #define LIST_HEAD_READ_MOSTLY(name) \
43 struct list_head __read_mostly name = LIST_HEAD_INIT(name)
44
INIT_LIST_HEAD(struct list_head * list)45 static inline void INIT_LIST_HEAD(struct list_head *list)
46 {
47 list->next = list;
48 list->prev = list;
49 }
50
list_head_is_null(const struct list_head * list)51 static inline bool list_head_is_null(const struct list_head *list)
52 {
53 return !list->next && !list->prev;
54 }
55
56 /*
57 * Insert a new entry between two known consecutive entries.
58 *
59 * This is only for internal list manipulation where we know
60 * the prev/next entries already!
61 */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)62 static inline void __list_add(struct list_head *new,
63 struct list_head *prev,
64 struct list_head *next)
65 {
66 next->prev = new;
67 new->next = next;
68 new->prev = prev;
69 prev->next = new;
70 }
71
72 /**
73 * list_add - add a new entry
74 * @new: new entry to be added
75 * @head: list head to add it after
76 *
77 * Insert a new entry after the specified head.
78 * This is good for implementing stacks.
79 */
list_add(struct list_head * new,struct list_head * head)80 static inline void list_add(struct list_head *new, struct list_head *head)
81 {
82 __list_add(new, head, head->next);
83 }
84
85 /**
86 * list_add_tail - add a new entry
87 * @new: new entry to be added
88 * @head: list head to add it before
89 *
90 * Insert a new entry before the specified head.
91 * This is useful for implementing queues.
92 */
list_add_tail(struct list_head * new,struct list_head * head)93 static inline void list_add_tail(struct list_head *new, struct list_head *head)
94 {
95 __list_add(new, head->prev, head);
96 }
97
98 /*
99 * Insert a new entry between two known consecutive entries.
100 *
101 * This is only for internal list manipulation where we know
102 * the prev/next entries already!
103 */
__list_add_rcu(struct list_head * new,struct list_head * prev,struct list_head * next)104 static inline void __list_add_rcu(struct list_head *new,
105 struct list_head *prev,
106 struct list_head *next)
107 {
108 new->next = next;
109 new->prev = prev;
110 smp_wmb();
111 next->prev = new;
112 prev->next = new;
113 }
114
115 /**
116 * list_add_rcu - add a new entry to rcu-protected list
117 * @new: new entry to be added
118 * @head: list head to add it after
119 *
120 * Insert a new entry after the specified head.
121 * This is good for implementing stacks.
122 *
123 * The caller must take whatever precautions are necessary
124 * (such as holding appropriate locks) to avoid racing
125 * with another list-mutation primitive, such as list_add_rcu()
126 * or list_del_rcu(), running on this same list.
127 * However, it is perfectly legal to run concurrently with
128 * the _rcu list-traversal primitives, such as
129 * list_for_each_entry_rcu().
130 */
list_add_rcu(struct list_head * new,struct list_head * head)131 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
132 {
133 __list_add_rcu(new, head, head->next);
134 }
135
136 /**
137 * list_add_tail_rcu - add a new entry to rcu-protected list
138 * @new: new entry to be added
139 * @head: list head to add it before
140 *
141 * Insert a new entry before the specified head.
142 * This is useful for implementing queues.
143 *
144 * The caller must take whatever precautions are necessary
145 * (such as holding appropriate locks) to avoid racing
146 * with another list-mutation primitive, such as list_add_tail_rcu()
147 * or list_del_rcu(), running on this same list.
148 * However, it is perfectly legal to run concurrently with
149 * the _rcu list-traversal primitives, such as
150 * list_for_each_entry_rcu().
151 */
list_add_tail_rcu(struct list_head * new,struct list_head * head)152 static inline void list_add_tail_rcu(struct list_head *new,
153 struct list_head *head)
154 {
155 __list_add_rcu(new, head->prev, head);
156 }
157
158 /*
159 * Delete a list entry by making the prev/next entries
160 * point to each other.
161 *
162 * This is only for internal list manipulation where we know
163 * the prev/next entries already!
164 */
__list_del(struct list_head * prev,struct list_head * next)165 static inline void __list_del(struct list_head *prev,
166 struct list_head *next)
167 {
168 next->prev = prev;
169 prev->next = next;
170 }
171
172 /**
173 * list_del - deletes entry from list.
174 * @entry: the element to delete from the list.
175 * Note: list_empty on entry does not return true after this, the entry is
176 * in an undefined state.
177 */
list_del(struct list_head * entry)178 static inline void list_del(struct list_head *entry)
179 {
180 ASSERT(entry->next->prev == entry);
181 ASSERT(entry->prev->next == entry);
182 __list_del(entry->prev, entry->next);
183 entry->next = LIST_POISON1;
184 entry->prev = LIST_POISON2;
185 }
186
187 /**
188 * list_del_rcu - deletes entry from list without re-initialization
189 * @entry: the element to delete from the list.
190 *
191 * Note: list_empty on entry does not return true after this,
192 * the entry is in an undefined state. It is useful for RCU based
193 * lockfree traversal.
194 *
195 * In particular, it means that we can not poison the forward
196 * pointers that may still be used for walking the list.
197 *
198 * The caller must take whatever precautions are necessary
199 * (such as holding appropriate locks) to avoid racing
200 * with another list-mutation primitive, such as list_del_rcu()
201 * or list_add_rcu(), running on this same list.
202 * However, it is perfectly legal to run concurrently with
203 * the _rcu list-traversal primitives, such as
204 * list_for_each_entry_rcu().
205 *
206 * Note that the caller is not permitted to immediately free
207 * the newly deleted entry. Instead, either synchronize_rcu()
208 * or call_rcu() must be used to defer freeing until an RCU
209 * grace period has elapsed.
210 */
list_del_rcu(struct list_head * entry)211 static inline void list_del_rcu(struct list_head *entry)
212 {
213 __list_del(entry->prev, entry->next);
214 entry->prev = LIST_POISON2;
215 }
216
217 /**
218 * list_replace - replace old entry by new one
219 * @old : the element to be replaced
220 * @new : the new element to insert
221 * Note: if 'old' was empty, it will be overwritten.
222 */
list_replace(struct list_head * old,struct list_head * new)223 static inline void list_replace(struct list_head *old,
224 struct list_head *new)
225 {
226 new->next = old->next;
227 new->next->prev = new;
228 new->prev = old->prev;
229 new->prev->next = new;
230 }
231
list_replace_init(struct list_head * old,struct list_head * new)232 static inline void list_replace_init(struct list_head *old,
233 struct list_head *new)
234 {
235 list_replace(old, new);
236 INIT_LIST_HEAD(old);
237 }
238
239 /*
240 * list_replace_rcu - replace old entry by new one
241 * @old : the element to be replaced
242 * @new : the new element to insert
243 *
244 * The old entry will be replaced with the new entry atomically.
245 * Note: 'old' should not be empty.
246 */
list_replace_rcu(struct list_head * old,struct list_head * new)247 static inline void list_replace_rcu(struct list_head *old,
248 struct list_head *new)
249 {
250 new->next = old->next;
251 new->prev = old->prev;
252 smp_wmb();
253 new->next->prev = new;
254 new->prev->next = new;
255 old->prev = LIST_POISON2;
256 }
257
258 /**
259 * list_del_init - deletes entry from list and reinitialize it.
260 * @entry: the element to delete from the list.
261 */
list_del_init(struct list_head * entry)262 static inline void list_del_init(struct list_head *entry)
263 {
264 __list_del(entry->prev, entry->next);
265 INIT_LIST_HEAD(entry);
266 }
267
268 /**
269 * list_move - delete from one list and add as another's head
270 * @list: the entry to move
271 * @head: the head that will precede our entry
272 */
list_move(struct list_head * list,struct list_head * head)273 static inline void list_move(struct list_head *list, struct list_head *head)
274 {
275 __list_del(list->prev, list->next);
276 list_add(list, head);
277 }
278
279 /**
280 * list_move_tail - delete from one list and add as another's tail
281 * @list: the entry to move
282 * @head: the head that will follow our entry
283 */
list_move_tail(struct list_head * list,struct list_head * head)284 static inline void list_move_tail(struct list_head *list,
285 struct list_head *head)
286 {
287 __list_del(list->prev, list->next);
288 list_add_tail(list, head);
289 }
290
291 /**
292 * list_is_last - tests whether @list is the last entry in list @head
293 * @list: the entry to test
294 * @head: the head of the list
295 */
list_is_last(const struct list_head * list,const struct list_head * head)296 static inline int list_is_last(const struct list_head *list,
297 const struct list_head *head)
298 {
299 return list->next == head;
300 }
301
302 /**
303 * list_empty - tests whether a list is empty
304 * @head: the list to test.
305 */
list_empty(const struct list_head * head)306 static inline int list_empty(const struct list_head *head)
307 {
308 return head->next == head;
309 }
310
311 /**
312 * list_is_singular - tests whether a list has exactly one entry
313 * @head: the list to test.
314 */
list_is_singular(const struct list_head * head)315 static inline int list_is_singular(const struct list_head *head)
316 {
317 return !list_empty(head) && (head->next == head->prev);
318 }
319
320 /**
321 * list_empty_careful - tests whether a list is empty and not being modified
322 * @head: the list to test
323 *
324 * Description:
325 * tests whether a list is empty _and_ checks that no other CPU might be
326 * in the process of modifying either member (next or prev)
327 *
328 * NOTE: using list_empty_careful() without synchronization
329 * can only be safe if the only activity that can happen
330 * to the list entry is list_del_init(). Eg. it cannot be used
331 * if another CPU could re-list_add() it.
332 */
list_empty_careful(const struct list_head * head)333 static inline int list_empty_careful(const struct list_head *head)
334 {
335 struct list_head *next = head->next;
336 return (next == head) && (next == head->prev);
337 }
338
__list_splice(struct list_head * list,struct list_head * head)339 static inline void __list_splice(struct list_head *list,
340 struct list_head *head)
341 {
342 struct list_head *first = list->next;
343 struct list_head *last = list->prev;
344 struct list_head *at = head->next;
345
346 first->prev = head;
347 head->next = first;
348
349 last->next = at;
350 at->prev = last;
351 }
352
353 /**
354 * list_splice - join two lists
355 * @list: the new list to add.
356 * @head: the place to add it in the first list.
357 */
list_splice(struct list_head * list,struct list_head * head)358 static inline void list_splice(struct list_head *list, struct list_head *head)
359 {
360 if (!list_empty(list))
361 __list_splice(list, head);
362 }
363
364 /**
365 * list_splice_init - join two lists and reinitialise the emptied list.
366 * @list: the new list to add.
367 * @head: the place to add it in the first list.
368 *
369 * The list at @list is reinitialised
370 */
list_splice_init(struct list_head * list,struct list_head * head)371 static inline void list_splice_init(struct list_head *list,
372 struct list_head *head)
373 {
374 if (!list_empty(list)) {
375 __list_splice(list, head);
376 INIT_LIST_HEAD(list);
377 }
378 }
379
380 /**
381 * list_entry - get the struct for this entry
382 * @ptr: the &struct list_head pointer.
383 * @type: the type of the struct this is embedded in.
384 * @member: the name of the list_struct within the struct.
385 */
386 #define list_entry(ptr, type, member) \
387 container_of(ptr, type, member)
388
389 /**
390 * list_first_entry - get the first element from a list
391 * @ptr: the list head to take the element from.
392 * @type: the type of the struct this is embedded in.
393 * @member: the name of the list_struct within the struct.
394 *
395 * Note, that list is expected to be not empty.
396 */
397 #define list_first_entry(ptr, type, member) \
398 list_entry((ptr)->next, type, member)
399
400 /**
401 * list_last_entry - get the last element from a list
402 * @ptr: the list head to take the element from.
403 * @type: the type of the struct this is embedded in.
404 * @member: the name of the list_struct within the struct.
405 *
406 * Note, that list is expected to be not empty.
407 */
408 #define list_last_entry(ptr, type, member) \
409 list_entry((ptr)->prev, type, member)
410
411 /**
412 * list_first_entry_or_null - get the first element from a list
413 * @ptr: the list head to take the element from.
414 * @type: the type of the struct this is embedded in.
415 * @member: the name of the list_struct within the struct.
416 *
417 * Note that if the list is empty, it returns NULL.
418 */
419 #define list_first_entry_or_null(ptr, type, member) \
420 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
421
422 /**
423 * list_last_entry_or_null - get the last element from a list
424 * @ptr: the list head to take the element from.
425 * @type: the type of the struct this is embedded in.
426 * @member: the name of the list_struct within the struct.
427 *
428 * Note that if the list is empty, it returns NULL.
429 */
430 #define list_last_entry_or_null(ptr, type, member) \
431 (!list_empty(ptr) ? list_last_entry(ptr, type, member) : NULL)
432
433 /**
434 * list_next_entry - get the next element in list
435 * @pos: the type * to cursor
436 * @member: the name of the list_struct within the struct.
437 */
438 #define list_next_entry(pos, member) \
439 list_entry((pos)->member.next, typeof(*(pos)), member)
440
441 /**
442 * list_prev_entry - get the prev element in list
443 * @pos: the type * to cursor
444 * @member: the name of the list_struct within the struct.
445 */
446 #define list_prev_entry(pos, member) \
447 list_entry((pos)->member.prev, typeof(*(pos)), member)
448
449 /**
450 * list_for_each - iterate over a list
451 * @pos: the &struct list_head to use as a loop cursor.
452 * @head: the head for your list.
453 */
454 #define list_for_each(pos, head) \
455 for (pos = (head)->next; pos != (head); pos = pos->next)
456
457 /**
458 * list_for_each_prev - iterate over a list backwards
459 * @pos: the &struct list_head to use as a loop cursor.
460 * @head: the head for your list.
461 */
462 #define list_for_each_prev(pos, head) \
463 for (pos = (head)->prev; pos != (head); pos = pos->prev)
464
465 /**
466 * list_for_each_safe - iterate over a list safe against removal of list entry
467 * @pos: the &struct list_head to use as a loop cursor.
468 * @n: another &struct list_head to use as temporary storage
469 * @head: the head for your list.
470 */
471 #define list_for_each_safe(pos, n, head) \
472 for (pos = (head)->next, n = pos->next; pos != (head); \
473 pos = n, n = pos->next)
474
475 /**
476 * list_for_each_backwards_safe - iterate backwards over a list safe
477 * against removal of list entry
478 * @pos: the &struct list_head to use as a loop counter.
479 * @n: another &struct list_head to use as temporary storage
480 * @head: the head for your list.
481 */
482 #define list_for_each_backwards_safe(pos, n, head) \
483 for ( pos = (head)->prev, n = pos->prev; pos != (head); \
484 pos = n, n = pos->prev )
485
486 /**
487 * list_for_each_entry - iterate over list of given type
488 * @pos: the type * to use as a loop cursor.
489 * @head: the head for your list.
490 * @member: the name of the list_struct within the struct.
491 */
492 #define list_for_each_entry(pos, head, member) \
493 for (pos = list_entry((head)->next, typeof(*pos), member); \
494 &pos->member != (head); \
495 pos = list_entry(pos->member.next, typeof(*pos), member))
496
497 /**
498 * list_for_each_entry_reverse - iterate backwards over list of given type.
499 * @pos: the type * to use as a loop cursor.
500 * @head: the head for your list.
501 * @member: the name of the list_struct within the struct.
502 */
503 #define list_for_each_entry_reverse(pos, head, member) \
504 for (pos = list_entry((head)->prev, typeof(*pos), member); \
505 &pos->member != (head); \
506 pos = list_entry(pos->member.prev, typeof(*pos), member))
507
508 /**
509 * list_prepare_entry - prepare a pos entry for use in
510 * list_for_each_entry_continue
511 * @pos: the type * to use as a start point
512 * @head: the head of the list
513 * @member: the name of the list_struct within the struct.
514 *
515 * Prepares a pos entry for use as a start point in
516 * list_for_each_entry_continue.
517 */
518 #define list_prepare_entry(pos, head, member) \
519 ((pos) ? : list_entry(head, typeof(*pos), member))
520
521 /**
522 * list_for_each_entry_continue - continue iteration over list of given type
523 * @pos: the type * to use as a loop cursor.
524 * @head: the head for your list.
525 * @member: the name of the list_struct within the struct.
526 *
527 * Continue to iterate over list of given type, continuing after
528 * the current position.
529 */
530 #define list_for_each_entry_continue(pos, head, member) \
531 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
532 &pos->member != (head); \
533 pos = list_entry(pos->member.next, typeof(*pos), member))
534
535 /**
536 * list_for_each_entry_from - iterate over list of given type from the
537 * current point
538 * @pos: the type * to use as a loop cursor.
539 * @head: the head for your list.
540 * @member: the name of the list_struct within the struct.
541 *
542 * Iterate over list of given type, continuing from current position.
543 */
544 #define list_for_each_entry_from(pos, head, member) \
545 for (; &pos->member != (head); \
546 pos = list_entry(pos->member.next, typeof(*pos), member))
547
548 /**
549 * list_for_each_entry_safe - iterate over list of given type safe
550 * against removal of list entry
551 * @pos: the type * to use as a loop cursor.
552 * @n: another type * to use as temporary storage
553 * @head: the head for your list.
554 * @member: the name of the list_struct within the struct.
555 */
556 #define list_for_each_entry_safe(pos, n, head, member) \
557 for (pos = list_entry((head)->next, typeof(*pos), member), \
558 n = list_entry(pos->member.next, typeof(*pos), member); \
559 &pos->member != (head); \
560 pos = n, n = list_entry(n->member.next, typeof(*n), member))
561
562 /**
563 * list_for_each_entry_safe_continue
564 * @pos: the type * to use as a loop cursor.
565 * @n: another type * to use as temporary storage
566 * @head: the head for your list.
567 * @member: the name of the list_struct within the struct.
568 *
569 * Iterate over list of given type, continuing after current point,
570 * safe against removal of list entry.
571 */
572 #define list_for_each_entry_safe_continue(pos, n, head, member) \
573 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
574 n = list_entry(pos->member.next, typeof(*pos), member); \
575 &pos->member != (head); \
576 pos = n, n = list_entry(n->member.next, typeof(*n), member))
577
578 /**
579 * list_for_each_entry_safe_from
580 * @pos: the type * to use as a loop cursor.
581 * @n: another type * to use as temporary storage
582 * @head: the head for your list.
583 * @member: the name of the list_struct within the struct.
584 *
585 * Iterate over list of given type from current point, safe against
586 * removal of list entry.
587 */
588 #define list_for_each_entry_safe_from(pos, n, head, member) \
589 for (n = list_entry(pos->member.next, typeof(*pos), member); \
590 &pos->member != (head); \
591 pos = n, n = list_entry(n->member.next, typeof(*n), member))
592
593 /**
594 * list_for_each_entry_safe_reverse
595 * @pos: the type * to use as a loop cursor.
596 * @n: another type * to use as temporary storage
597 * @head: the head for your list.
598 * @member: the name of the list_struct within the struct.
599 *
600 * Iterate backwards over list of given type, safe against removal
601 * of list entry.
602 */
603 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
604 for (pos = list_entry((head)->prev, typeof(*pos), member), \
605 n = list_entry(pos->member.prev, typeof(*pos), member); \
606 &pos->member != (head); \
607 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
608
609 /**
610 * list_for_each_rcu - iterate over an rcu-protected list
611 * @pos: the &struct list_head to use as a loop cursor.
612 * @head: the head for your list.
613 *
614 * This list-traversal primitive may safely run concurrently with
615 * the _rcu list-mutation primitives such as list_add_rcu()
616 * as long as the traversal is guarded by rcu_read_lock().
617 */
618 #define list_for_each_rcu(pos, head) \
619 for (pos = (head)->next; \
620 rcu_dereference(pos) != (head); \
621 pos = pos->next)
622
623 #define __list_for_each_rcu(pos, head) \
624 for (pos = (head)->next; \
625 rcu_dereference(pos) != (head); \
626 pos = pos->next)
627
628 /**
629 * list_for_each_safe_rcu
630 * @pos: the &struct list_head to use as a loop cursor.
631 * @n: another &struct list_head to use as temporary storage
632 * @head: the head for your list.
633 *
634 * Iterate over an rcu-protected list, safe against removal of list entry.
635 *
636 * This list-traversal primitive may safely run concurrently with
637 * the _rcu list-mutation primitives such as list_add_rcu()
638 * as long as the traversal is guarded by rcu_read_lock().
639 */
640 #define list_for_each_safe_rcu(pos, n, head) \
641 for (pos = (head)->next; \
642 n = rcu_dereference(pos)->next, pos != (head); \
643 pos = n)
644
645 /**
646 * list_for_each_entry_rcu - iterate over rcu list of given type
647 * @pos: the type * to use as a loop cursor.
648 * @head: the head for your list.
649 * @member: the name of the list_struct within the struct.
650 *
651 * This list-traversal primitive may safely run concurrently with
652 * the _rcu list-mutation primitives such as list_add_rcu()
653 * as long as the traversal is guarded by rcu_read_lock().
654 */
655 #define list_for_each_entry_rcu(pos, head, member) \
656 for (pos = list_entry((head)->next, typeof(*pos), member); \
657 &rcu_dereference(pos)->member != (head); \
658 pos = list_entry(pos->member.next, typeof(*pos), member))
659
660 /**
661 * list_for_each_continue_rcu
662 * @pos: the &struct list_head to use as a loop cursor.
663 * @head: the head for your list.
664 *
665 * Iterate over an rcu-protected list, continuing after current point.
666 *
667 * This list-traversal primitive may safely run concurrently with
668 * the _rcu list-mutation primitives such as list_add_rcu()
669 * as long as the traversal is guarded by rcu_read_lock().
670 */
671 #define list_for_each_continue_rcu(pos, head) \
672 for ((pos) = (pos)->next; \
673 rcu_dereference(pos) != (head); \
674 (pos) = (pos)->next)
675
676 /*
677 * Double linked lists with a single pointer list head.
678 * Mostly useful for hash tables where the two pointer list head is
679 * too wasteful.
680 * You lose the ability to access the tail in O(1).
681 */
682
683 struct hlist_head {
684 struct hlist_node *first;
685 };
686
687 struct hlist_node {
688 struct hlist_node *next, **pprev;
689 };
690
691 #define HLIST_HEAD_INIT { .first = NULL }
692 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
693 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
INIT_HLIST_NODE(struct hlist_node * h)694 static inline void INIT_HLIST_NODE(struct hlist_node *h)
695 {
696 h->next = NULL;
697 h->pprev = NULL;
698 }
699
hlist_unhashed(const struct hlist_node * h)700 static inline int hlist_unhashed(const struct hlist_node *h)
701 {
702 return !h->pprev;
703 }
704
hlist_empty(const struct hlist_head * h)705 static inline int hlist_empty(const struct hlist_head *h)
706 {
707 return !h->first;
708 }
709
__hlist_del(struct hlist_node * n)710 static inline void __hlist_del(struct hlist_node *n)
711 {
712 struct hlist_node *next = n->next;
713 struct hlist_node **pprev = n->pprev;
714 *pprev = next;
715 if (next)
716 next->pprev = pprev;
717 }
718
hlist_del(struct hlist_node * n)719 static inline void hlist_del(struct hlist_node *n)
720 {
721 __hlist_del(n);
722 n->next = LIST_POISON1;
723 n->pprev = LIST_POISON2;
724 }
725
726 /**
727 * hlist_del_rcu - deletes entry from hash list without re-initialization
728 * @n: the element to delete from the hash list.
729 *
730 * Note: list_unhashed() on entry does not return true after this,
731 * the entry is in an undefined state. It is useful for RCU based
732 * lockfree traversal.
733 *
734 * In particular, it means that we can not poison the forward
735 * pointers that may still be used for walking the hash list.
736 *
737 * The caller must take whatever precautions are necessary
738 * (such as holding appropriate locks) to avoid racing
739 * with another list-mutation primitive, such as hlist_add_head_rcu()
740 * or hlist_del_rcu(), running on this same list.
741 * However, it is perfectly legal to run concurrently with
742 * the _rcu list-traversal primitives, such as
743 * hlist_for_each_entry().
744 */
hlist_del_rcu(struct hlist_node * n)745 static inline void hlist_del_rcu(struct hlist_node *n)
746 {
747 __hlist_del(n);
748 n->pprev = LIST_POISON2;
749 }
750
hlist_del_init(struct hlist_node * n)751 static inline void hlist_del_init(struct hlist_node *n)
752 {
753 if (!hlist_unhashed(n)) {
754 __hlist_del(n);
755 INIT_HLIST_NODE(n);
756 }
757 }
758
759 /*
760 * hlist_replace_rcu - replace old entry by new one
761 * @old : the element to be replaced
762 * @new : the new element to insert
763 *
764 * The old entry will be replaced with the new entry atomically.
765 */
hlist_replace_rcu(struct hlist_node * old,struct hlist_node * new)766 static inline void hlist_replace_rcu(struct hlist_node *old,
767 struct hlist_node *new)
768 {
769 struct hlist_node *next = old->next;
770
771 new->next = next;
772 new->pprev = old->pprev;
773 smp_wmb();
774 if (next)
775 new->next->pprev = &new->next;
776 *new->pprev = new;
777 old->pprev = LIST_POISON2;
778 }
779
hlist_add_head(struct hlist_node * n,struct hlist_head * h)780 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
781 {
782 struct hlist_node *first = h->first;
783 n->next = first;
784 if (first)
785 first->pprev = &n->next;
786 h->first = n;
787 n->pprev = &h->first;
788 }
789
790 /**
791 * hlist_add_head_rcu
792 * @n: the element to add to the hash list.
793 * @h: the list to add to.
794 *
795 * Description:
796 * Adds the specified element to the specified hlist,
797 * while permitting racing traversals.
798 *
799 * The caller must take whatever precautions are necessary
800 * (such as holding appropriate locks) to avoid racing
801 * with another list-mutation primitive, such as hlist_add_head_rcu()
802 * or hlist_del_rcu(), running on this same list.
803 * However, it is perfectly legal to run concurrently with
804 * the _rcu list-traversal primitives, such as
805 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
806 * problems on Alpha CPUs. Regardless of the type of CPU, the
807 * list-traversal primitive must be guarded by rcu_read_lock().
808 */
hlist_add_head_rcu(struct hlist_node * n,struct hlist_head * h)809 static inline void hlist_add_head_rcu(struct hlist_node *n,
810 struct hlist_head *h)
811 {
812 struct hlist_node *first = h->first;
813 n->next = first;
814 n->pprev = &h->first;
815 smp_wmb();
816 if (first)
817 first->pprev = &n->next;
818 h->first = n;
819 }
820
821 /* next must be != NULL */
hlist_add_before(struct hlist_node * n,struct hlist_node * next)822 static inline void hlist_add_before(struct hlist_node *n,
823 struct hlist_node *next)
824 {
825 n->pprev = next->pprev;
826 n->next = next;
827 next->pprev = &n->next;
828 *(n->pprev) = n;
829 }
830
hlist_add_after(struct hlist_node * n,struct hlist_node * next)831 static inline void hlist_add_after(struct hlist_node *n,
832 struct hlist_node *next)
833 {
834 next->next = n->next;
835 n->next = next;
836 next->pprev = &n->next;
837
838 if(next->next)
839 next->next->pprev = &next->next;
840 }
841
842 /**
843 * hlist_add_before_rcu
844 * @n: the new element to add to the hash list.
845 * @next: the existing element to add the new element before.
846 *
847 * Description:
848 * Adds the specified element to the specified hlist
849 * before the specified node while permitting racing traversals.
850 *
851 * The caller must take whatever precautions are necessary
852 * (such as holding appropriate locks) to avoid racing
853 * with another list-mutation primitive, such as hlist_add_head_rcu()
854 * or hlist_del_rcu(), running on this same list.
855 * However, it is perfectly legal to run concurrently with
856 * the _rcu list-traversal primitives, such as
857 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
858 * problems on Alpha CPUs.
859 */
hlist_add_before_rcu(struct hlist_node * n,struct hlist_node * next)860 static inline void hlist_add_before_rcu(struct hlist_node *n,
861 struct hlist_node *next)
862 {
863 n->pprev = next->pprev;
864 n->next = next;
865 smp_wmb();
866 next->pprev = &n->next;
867 *(n->pprev) = n;
868 }
869
870 /**
871 * hlist_add_after_rcu
872 * @prev: the existing element to add the new element after.
873 * @n: the new element to add to the hash list.
874 *
875 * Description:
876 * Adds the specified element to the specified hlist
877 * after the specified node while permitting racing traversals.
878 *
879 * The caller must take whatever precautions are necessary
880 * (such as holding appropriate locks) to avoid racing
881 * with another list-mutation primitive, such as hlist_add_head_rcu()
882 * or hlist_del_rcu(), running on this same list.
883 * However, it is perfectly legal to run concurrently with
884 * the _rcu list-traversal primitives, such as
885 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
886 * problems on Alpha CPUs.
887 */
hlist_add_after_rcu(struct hlist_node * prev,struct hlist_node * n)888 static inline void hlist_add_after_rcu(struct hlist_node *prev,
889 struct hlist_node *n)
890 {
891 n->next = prev->next;
892 n->pprev = &prev->next;
893 smp_wmb();
894 prev->next = n;
895 if (n->next)
896 n->next->pprev = &n->next;
897 }
898
899 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
900
901 #define hlist_for_each(pos, head) \
902 for (pos = (head)->first; pos; pos = pos->next)
903
904 #define hlist_for_each_safe(pos, n, head) \
905 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
906 pos = n)
907
908 /**
909 * hlist_for_each_entry - iterate over list of given type
910 * @tpos: the type * to use as a loop cursor.
911 * @pos: the &struct hlist_node to use as a loop cursor.
912 * @head: the head for your list.
913 * @member: the name of the hlist_node within the struct.
914 */
915 #define hlist_for_each_entry(tpos, pos, head, member) \
916 for (pos = (head)->first; \
917 pos && \
918 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
919 pos = pos->next)
920
921 /**
922 * hlist_for_each_entry_continue - iterate over a hlist continuing
923 * after current point
924 * @tpos: the type * to use as a loop cursor.
925 * @pos: the &struct hlist_node to use as a loop cursor.
926 * @member: the name of the hlist_node within the struct.
927 */
928 #define hlist_for_each_entry_continue(tpos, pos, member) \
929 for (pos = (pos)->next; \
930 pos && \
931 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
932 pos = pos->next)
933
934 /**
935 * hlist_for_each_entry_from - iterate over a hlist continuing from
936 * current point
937 * @tpos: the type * to use as a loop cursor.
938 * @pos: the &struct hlist_node to use as a loop cursor.
939 * @member: the name of the hlist_node within the struct.
940 */
941 #define hlist_for_each_entry_from(tpos, pos, member) \
942 for (; pos && \
943 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
944 pos = pos->next)
945
946 /**
947 * hlist_for_each_entry_safe - iterate over list of given type safe
948 * against removal of list entry
949 * @tpos: the type * to use as a loop cursor.
950 * @pos: the &struct hlist_node to use as a loop cursor.
951 * @n: another &struct hlist_node to use as temporary storage
952 * @head: the head for your list.
953 * @member: the name of the hlist_node within the struct.
954 */
955 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
956 for (pos = (head)->first; \
957 pos && ({ n = pos->next; 1; }) && \
958 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
959 pos = n)
960
961
962 /**
963 * hlist_for_each_entry_rcu - iterate over rcu list of given type
964 * @tpos: the type * to use as a loop cursor.
965 * @pos: the &struct hlist_node to use as a loop cursor.
966 * @head: the head for your list.
967 * @member: the name of the hlist_node within the struct.
968 *
969 * This list-traversal primitive may safely run concurrently with
970 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
971 * as long as the traversal is guarded by rcu_read_lock().
972 */
973 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
974 for (pos = (head)->first; \
975 rcu_dereference(pos) && \
976 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
977 pos = pos->next)
978
979 #endif /* __XEN_LIST_H__ */
980
981