plist.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * lib/plist.c
  3. *
  4. * Descending-priority-sorted double-linked list
  5. *
  6. * (C) 2002-2003 Intel Corp
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
  8. *
  9. * 2001-2005 (c) MontaVista Software, Inc.
  10. * Daniel Walker <dwalker@mvista.com>
  11. *
  12. * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
  13. *
  14. * Simplifications of the original code by
  15. * Oleg Nesterov <oleg@tv-sign.ru>
  16. *
  17. * Licensed under the FSF's GNU Public License v2 or later.
  18. *
  19. * Based on simple lists (include/linux/list.h).
  20. *
  21. * This file contains the add / del functions which are considered to
  22. * be too large to inline. See include/linux/plist.h for further
  23. * information.
  24. */
  25. #include <linux/plist.h>
  26. #include <linux/spinlock.h>
  27. #ifdef CONFIG_DEBUG_PI_LIST
  28. static struct plist_head test_head;
  29. static void plist_check_prev_next(struct list_head *t, struct list_head *p,
  30. struct list_head *n)
  31. {
  32. WARN(n->prev != p || p->next != n,
  33. "top: %p, n: %p, p: %p\n"
  34. "prev: %p, n: %p, p: %p\n"
  35. "next: %p, n: %p, p: %p\n",
  36. t, t->next, t->prev,
  37. p, p->next, p->prev,
  38. n, n->next, n->prev);
  39. }
  40. static void plist_check_list(struct list_head *top)
  41. {
  42. struct list_head *prev = top, *next = top->next;
  43. plist_check_prev_next(top, prev, next);
  44. while (next != top) {
  45. prev = next;
  46. next = prev->next;
  47. plist_check_prev_next(top, prev, next);
  48. }
  49. }
  50. static void plist_check_head(struct plist_head *head)
  51. {
  52. WARN_ON(head != &test_head && !head->rawlock && !head->spinlock);
  53. if (head->rawlock)
  54. WARN_ON_SMP(!raw_spin_is_locked(head->rawlock));
  55. if (head->spinlock)
  56. WARN_ON_SMP(!spin_is_locked(head->spinlock));
  57. if (!plist_head_empty(head))
  58. plist_check_list(&plist_first(head)->prio_list);
  59. plist_check_list(&head->node_list);
  60. }
  61. #else
  62. # define plist_check_head(h) do { } while (0)
  63. #endif
  64. /**
  65. * plist_add - add @node to @head
  66. *
  67. * @node: &struct plist_node pointer
  68. * @head: &struct plist_head pointer
  69. */
  70. void plist_add(struct plist_node *node, struct plist_head *head)
  71. {
  72. struct plist_node *first, *iter, *prev = NULL;
  73. struct list_head *node_next = &head->node_list;
  74. plist_check_head(head);
  75. WARN_ON(!plist_node_empty(node));
  76. WARN_ON(!list_empty(&node->prio_list));
  77. if (plist_head_empty(head))
  78. goto ins_node;
  79. first = iter = plist_first(head);
  80. do {
  81. if (node->prio < iter->prio) {
  82. node_next = &iter->node_list;
  83. break;
  84. }
  85. prev = iter;
  86. iter = list_entry(iter->prio_list.next,
  87. struct plist_node, prio_list);
  88. } while (iter != first);
  89. if (!prev || prev->prio != node->prio)
  90. list_add_tail(&node->prio_list, &iter->prio_list);
  91. ins_node:
  92. list_add_tail(&node->node_list, node_next);
  93. plist_check_head(head);
  94. }
  95. /**
  96. * plist_del - Remove a @node from plist.
  97. *
  98. * @node: &struct plist_node pointer - entry to be removed
  99. * @head: &struct plist_head pointer - list head
  100. */
  101. void plist_del(struct plist_node *node, struct plist_head *head)
  102. {
  103. plist_check_head(head);
  104. if (!list_empty(&node->prio_list)) {
  105. if (node->node_list.next != &head->node_list) {
  106. struct plist_node *next;
  107. next = list_entry(node->node_list.next,
  108. struct plist_node, node_list);
  109. /* add the next plist_node into prio_list */
  110. if (list_empty(&next->prio_list))
  111. list_add(&next->prio_list, &node->prio_list);
  112. }
  113. list_del_init(&node->prio_list);
  114. }
  115. list_del_init(&node->node_list);
  116. plist_check_head(head);
  117. }
  118. #ifdef CONFIG_DEBUG_PI_LIST
  119. #include <linux/sched.h>
  120. #include <linux/module.h>
  121. #include <linux/init.h>
  122. static struct plist_node __initdata test_node[241];
  123. static void __init plist_test_check(int nr_expect)
  124. {
  125. struct plist_node *first, *prio_pos, *node_pos;
  126. if (plist_head_empty(&test_head)) {
  127. BUG_ON(nr_expect != 0);
  128. return;
  129. }
  130. prio_pos = first = plist_first(&test_head);
  131. plist_for_each(node_pos, &test_head) {
  132. if (nr_expect-- < 0)
  133. break;
  134. if (node_pos == first)
  135. continue;
  136. if (node_pos->prio == prio_pos->prio) {
  137. BUG_ON(!list_empty(&node_pos->prio_list));
  138. continue;
  139. }
  140. BUG_ON(prio_pos->prio > node_pos->prio);
  141. BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
  142. prio_pos = node_pos;
  143. }
  144. BUG_ON(nr_expect != 0);
  145. BUG_ON(prio_pos->prio_list.next != &first->prio_list);
  146. }
  147. static int __init plist_test(void)
  148. {
  149. int nr_expect = 0, i, loop;
  150. unsigned int r = local_clock();
  151. printk(KERN_INFO "start plist test\n");
  152. plist_head_init(&test_head, NULL);
  153. for (i = 0; i < ARRAY_SIZE(test_node); i++)
  154. plist_node_init(test_node + i, 0);
  155. for (loop = 0; loop < 1000; loop++) {
  156. r = r * 193939 % 47629;
  157. i = r % ARRAY_SIZE(test_node);
  158. if (plist_node_empty(test_node + i)) {
  159. r = r * 193939 % 47629;
  160. test_node[i].prio = r % 99;
  161. plist_add(test_node + i, &test_head);
  162. nr_expect++;
  163. } else {
  164. plist_del(test_node + i, &test_head);
  165. nr_expect--;
  166. }
  167. plist_test_check(nr_expect);
  168. }
  169. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  170. if (plist_node_empty(test_node + i))
  171. continue;
  172. plist_del(test_node + i, &test_head);
  173. nr_expect--;
  174. plist_test_check(nr_expect);
  175. }
  176. printk(KERN_INFO "end plist test\n");
  177. return 0;
  178. }
  179. module_init(plist_test);
  180. #endif