plist.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <inaky.perez-gonzalez@intel.com>.
  9. *
  10. * 2001-2005 (c) MontaVista Software, Inc.
  11. * Daniel Walker <dwalker@mvista.com>
  12. *
  13. * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
  14. *
  15. * Simplifications of the original code by
  16. * Oleg Nesterov <oleg@tv-sign.ru>
  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. EXPORT_SYMBOL_GPL(plist_add);
  90. /**
  91. * plist_del - Remove a @node from plist.
  92. *
  93. * @node: &struct plist_node pointer - entry to be removed
  94. * @head: &struct plist_head pointer - list head
  95. */
  96. void plist_del(struct plist_node *node, struct plist_head *head)
  97. {
  98. plist_check_head(head);
  99. if (!list_empty(&node->prio_list)) {
  100. if (node->node_list.next != &head->node_list) {
  101. struct plist_node *next;
  102. next = list_entry(node->node_list.next,
  103. struct plist_node, node_list);
  104. /* add the next plist_node into prio_list */
  105. if (list_empty(&next->prio_list))
  106. list_add(&next->prio_list, &node->prio_list);
  107. }
  108. list_del_init(&node->prio_list);
  109. }
  110. list_del_init(&node->node_list);
  111. plist_check_head(head);
  112. }
  113. EXPORT_SYMBOL_GPL(plist_del);
  114. /**
  115. * plist_requeue - Requeue @node at end of same-prio entries.
  116. *
  117. * This is essentially an optimized plist_del() followed by
  118. * plist_add(). It moves an entry already in the plist to
  119. * after any other same-priority entries.
  120. *
  121. * @node: &struct plist_node pointer - entry to be moved
  122. * @head: &struct plist_head pointer - list head
  123. */
  124. void plist_requeue(struct plist_node *node, struct plist_head *head)
  125. {
  126. struct plist_node *iter;
  127. struct list_head *node_next = &head->node_list;
  128. plist_check_head(head);
  129. BUG_ON(plist_head_empty(head));
  130. BUG_ON(plist_node_empty(node));
  131. if (node == plist_last(head))
  132. return;
  133. iter = plist_next(node);
  134. if (node->prio != iter->prio)
  135. return;
  136. plist_del(node, head);
  137. plist_for_each_continue(iter, head) {
  138. if (node->prio != iter->prio) {
  139. node_next = &iter->node_list;
  140. break;
  141. }
  142. }
  143. list_add_tail(&node->node_list, node_next);
  144. plist_check_head(head);
  145. }
  146. EXPORT_SYMBOL_GPL(plist_requeue);
  147. #ifdef CONFIG_DEBUG_PLIST
  148. #include <linux/sched.h>
  149. #include <linux/sched/clock.h>
  150. #include <linux/module.h>
  151. #include <linux/init.h>
  152. static struct plist_node __initdata test_node[241];
  153. static void __init plist_test_check(int nr_expect)
  154. {
  155. struct plist_node *first, *prio_pos, *node_pos;
  156. if (plist_head_empty(&test_head)) {
  157. BUG_ON(nr_expect != 0);
  158. return;
  159. }
  160. prio_pos = first = plist_first(&test_head);
  161. plist_for_each(node_pos, &test_head) {
  162. if (nr_expect-- < 0)
  163. break;
  164. if (node_pos == first)
  165. continue;
  166. if (node_pos->prio == prio_pos->prio) {
  167. BUG_ON(!list_empty(&node_pos->prio_list));
  168. continue;
  169. }
  170. BUG_ON(prio_pos->prio > node_pos->prio);
  171. BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
  172. prio_pos = node_pos;
  173. }
  174. BUG_ON(nr_expect != 0);
  175. BUG_ON(prio_pos->prio_list.next != &first->prio_list);
  176. }
  177. static void __init plist_test_requeue(struct plist_node *node)
  178. {
  179. plist_requeue(node, &test_head);
  180. if (node != plist_last(&test_head))
  181. BUG_ON(node->prio == plist_next(node)->prio);
  182. }
  183. static int __init plist_test(void)
  184. {
  185. int nr_expect = 0, i, loop;
  186. unsigned int r = local_clock();
  187. printk(KERN_DEBUG "start plist test\n");
  188. plist_head_init(&test_head);
  189. for (i = 0; i < ARRAY_SIZE(test_node); i++)
  190. plist_node_init(test_node + i, 0);
  191. for (loop = 0; loop < 1000; loop++) {
  192. r = r * 193939 % 47629;
  193. i = r % ARRAY_SIZE(test_node);
  194. if (plist_node_empty(test_node + i)) {
  195. r = r * 193939 % 47629;
  196. test_node[i].prio = r % 99;
  197. plist_add(test_node + i, &test_head);
  198. nr_expect++;
  199. } else {
  200. plist_del(test_node + i, &test_head);
  201. nr_expect--;
  202. }
  203. plist_test_check(nr_expect);
  204. if (!plist_node_empty(test_node + i)) {
  205. plist_test_requeue(test_node + i);
  206. plist_test_check(nr_expect);
  207. }
  208. }
  209. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  210. if (plist_node_empty(test_node + i))
  211. continue;
  212. plist_del(test_node + i, &test_head);
  213. nr_expect--;
  214. plist_test_check(nr_expect);
  215. }
  216. printk(KERN_DEBUG "end plist test\n");
  217. return 0;
  218. }
  219. module_init(plist_test);
  220. #endif