plist.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lib/plist.c
  4. *
  5. * Descending-priority-sorted double-linked list
  6. *
  7. * (C) 2002-2003 Intel Corp
  8. * Inaky Perez-Gonzalez <[email protected]>.
  9. *
  10. * 2001-2005 (c) MontaVista Software, Inc.
  11. * Daniel Walker <[email protected]>
  12. *
  13. * (C) 2005 Thomas Gleixner <[email protected]>
  14. *
  15. * Simplifications of the original code by
  16. * Oleg Nesterov <[email protected]>
  17. *
  18. * Based on simple lists (include/linux/list.h).
  19. *
  20. * This file contains the add / del functions which are considered to
  21. * be too large to inline. See include/linux/plist.h for further
  22. * information.
  23. */
  24. #include <linux/bug.h>
  25. #include <linux/plist.h>
  26. #ifdef CONFIG_DEBUG_PLIST
  27. static struct plist_head test_head;
  28. static void plist_check_prev_next(struct list_head *t, struct list_head *p,
  29. struct list_head *n)
  30. {
  31. WARN(n->prev != p || p->next != n,
  32. "top: %p, n: %p, p: %p\n"
  33. "prev: %p, n: %p, p: %p\n"
  34. "next: %p, n: %p, p: %p\n",
  35. t, t->next, t->prev,
  36. p, p->next, p->prev,
  37. n, n->next, n->prev);
  38. }
  39. static void plist_check_list(struct list_head *top)
  40. {
  41. struct list_head *prev = top, *next = top->next;
  42. plist_check_prev_next(top, prev, next);
  43. while (next != top) {
  44. prev = next;
  45. next = prev->next;
  46. plist_check_prev_next(top, prev, next);
  47. }
  48. }
  49. static void plist_check_head(struct plist_head *head)
  50. {
  51. if (!plist_head_empty(head))
  52. plist_check_list(&plist_first(head)->prio_list);
  53. plist_check_list(&head->node_list);
  54. }
  55. #else
  56. # define plist_check_head(h) do { } while (0)
  57. #endif
  58. /**
  59. * plist_add - add @node to @head
  60. *
  61. * @node: &struct plist_node pointer
  62. * @head: &struct plist_head pointer
  63. */
  64. void plist_add(struct plist_node *node, struct plist_head *head)
  65. {
  66. struct plist_node *first, *iter, *prev = NULL;
  67. struct list_head *node_next = &head->node_list;
  68. plist_check_head(head);
  69. WARN_ON(!plist_node_empty(node));
  70. WARN_ON(!list_empty(&node->prio_list));
  71. if (plist_head_empty(head))
  72. goto ins_node;
  73. first = iter = plist_first(head);
  74. do {
  75. if (node->prio < iter->prio) {
  76. node_next = &iter->node_list;
  77. break;
  78. }
  79. prev = iter;
  80. iter = list_entry(iter->prio_list.next,
  81. struct plist_node, prio_list);
  82. } while (iter != first);
  83. if (!prev || prev->prio != node->prio)
  84. list_add_tail(&node->prio_list, &iter->prio_list);
  85. ins_node:
  86. list_add_tail(&node->node_list, node_next);
  87. plist_check_head(head);
  88. }
  89. /**
  90. * plist_del - Remove a @node from plist.
  91. *
  92. * @node: &struct plist_node pointer - entry to be removed
  93. * @head: &struct plist_head pointer - list head
  94. */
  95. void plist_del(struct plist_node *node, struct plist_head *head)
  96. {
  97. plist_check_head(head);
  98. if (!list_empty(&node->prio_list)) {
  99. if (node->node_list.next != &head->node_list) {
  100. struct plist_node *next;
  101. next = list_entry(node->node_list.next,
  102. struct plist_node, node_list);
  103. /* add the next plist_node into prio_list */
  104. if (list_empty(&next->prio_list))
  105. list_add(&next->prio_list, &node->prio_list);
  106. }
  107. list_del_init(&node->prio_list);
  108. }
  109. list_del_init(&node->node_list);
  110. plist_check_head(head);
  111. }
  112. /**
  113. * plist_requeue - Requeue @node at end of same-prio entries.
  114. *
  115. * This is essentially an optimized plist_del() followed by
  116. * plist_add(). It moves an entry already in the plist to
  117. * after any other same-priority entries.
  118. *
  119. * @node: &struct plist_node pointer - entry to be moved
  120. * @head: &struct plist_head pointer - list head
  121. */
  122. void plist_requeue(struct plist_node *node, struct plist_head *head)
  123. {
  124. struct plist_node *iter;
  125. struct list_head *node_next = &head->node_list;
  126. plist_check_head(head);
  127. BUG_ON(plist_head_empty(head));
  128. BUG_ON(plist_node_empty(node));
  129. if (node == plist_last(head))
  130. return;
  131. iter = plist_next(node);
  132. if (node->prio != iter->prio)
  133. return;
  134. plist_del(node, head);
  135. plist_for_each_continue(iter, head) {
  136. if (node->prio != iter->prio) {
  137. node_next = &iter->node_list;
  138. break;
  139. }
  140. }
  141. list_add_tail(&node->node_list, node_next);
  142. plist_check_head(head);
  143. }
  144. #ifdef CONFIG_DEBUG_PLIST
  145. #include <linux/sched.h>
  146. #include <linux/sched/clock.h>
  147. #include <linux/module.h>
  148. #include <linux/init.h>
  149. static struct plist_node __initdata test_node[241];
  150. static void __init plist_test_check(int nr_expect)
  151. {
  152. struct plist_node *first, *prio_pos, *node_pos;
  153. if (plist_head_empty(&test_head)) {
  154. BUG_ON(nr_expect != 0);
  155. return;
  156. }
  157. prio_pos = first = plist_first(&test_head);
  158. plist_for_each(node_pos, &test_head) {
  159. if (nr_expect-- < 0)
  160. break;
  161. if (node_pos == first)
  162. continue;
  163. if (node_pos->prio == prio_pos->prio) {
  164. BUG_ON(!list_empty(&node_pos->prio_list));
  165. continue;
  166. }
  167. BUG_ON(prio_pos->prio > node_pos->prio);
  168. BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
  169. prio_pos = node_pos;
  170. }
  171. BUG_ON(nr_expect != 0);
  172. BUG_ON(prio_pos->prio_list.next != &first->prio_list);
  173. }
  174. static void __init plist_test_requeue(struct plist_node *node)
  175. {
  176. plist_requeue(node, &test_head);
  177. if (node != plist_last(&test_head))
  178. BUG_ON(node->prio == plist_next(node)->prio);
  179. }
  180. static int __init plist_test(void)
  181. {
  182. int nr_expect = 0, i, loop;
  183. unsigned int r = local_clock();
  184. printk(KERN_DEBUG "start plist test\n");
  185. plist_head_init(&test_head);
  186. for (i = 0; i < ARRAY_SIZE(test_node); i++)
  187. plist_node_init(test_node + i, 0);
  188. for (loop = 0; loop < 1000; loop++) {
  189. r = r * 193939 % 47629;
  190. i = r % ARRAY_SIZE(test_node);
  191. if (plist_node_empty(test_node + i)) {
  192. r = r * 193939 % 47629;
  193. test_node[i].prio = r % 99;
  194. plist_add(test_node + i, &test_head);
  195. nr_expect++;
  196. } else {
  197. plist_del(test_node + i, &test_head);
  198. nr_expect--;
  199. }
  200. plist_test_check(nr_expect);
  201. if (!plist_node_empty(test_node + i)) {
  202. plist_test_requeue(test_node + i);
  203. plist_test_check(nr_expect);
  204. }
  205. }
  206. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  207. if (plist_node_empty(test_node + i))
  208. continue;
  209. plist_del(test_node + i, &test_head);
  210. nr_expect--;
  211. plist_test_check(nr_expect);
  212. }
  213. printk(KERN_DEBUG "end plist test\n");
  214. return 0;
  215. }
  216. module_init(plist_test);
  217. #endif