plist.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 Linutronix GmbH, Thomas Gleixner <tglx@kernel.org>
  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, *last, *reverse_iter;
  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. last = reverse_iter = list_entry(first->prio_list.prev, struct plist_node, prio_list);
  75. do {
  76. if (node->prio < iter->prio) {
  77. node_next = &iter->node_list;
  78. break;
  79. } else if (node->prio >= reverse_iter->prio) {
  80. prev = reverse_iter;
  81. iter = list_entry(reverse_iter->prio_list.next,
  82. struct plist_node, prio_list);
  83. if (likely(reverse_iter != last))
  84. node_next = &iter->node_list;
  85. break;
  86. }
  87. prev = iter;
  88. iter = list_entry(iter->prio_list.next,
  89. struct plist_node, prio_list);
  90. reverse_iter = list_entry(reverse_iter->prio_list.prev,
  91. struct plist_node, prio_list);
  92. } while (iter != first);
  93. if (!prev || prev->prio != node->prio)
  94. list_add_tail(&node->prio_list, &iter->prio_list);
  95. ins_node:
  96. list_add_tail(&node->node_list, node_next);
  97. plist_check_head(head);
  98. }
  99. /**
  100. * plist_del - Remove a @node from plist.
  101. *
  102. * @node: &struct plist_node pointer - entry to be removed
  103. * @head: &struct plist_head pointer - list head
  104. */
  105. void plist_del(struct plist_node *node, struct plist_head *head)
  106. {
  107. plist_check_head(head);
  108. if (!list_empty(&node->prio_list)) {
  109. if (node->node_list.next != &head->node_list) {
  110. struct plist_node *next;
  111. next = list_entry(node->node_list.next,
  112. struct plist_node, node_list);
  113. /* add the next plist_node into prio_list */
  114. if (list_empty(&next->prio_list))
  115. list_add(&next->prio_list, &node->prio_list);
  116. }
  117. list_del_init(&node->prio_list);
  118. }
  119. list_del_init(&node->node_list);
  120. plist_check_head(head);
  121. }
  122. /**
  123. * plist_requeue - Requeue @node at end of same-prio entries.
  124. *
  125. * This is essentially an optimized plist_del() followed by
  126. * plist_add(). It moves an entry already in the plist to
  127. * after any other same-priority entries.
  128. *
  129. * @node: &struct plist_node pointer - entry to be moved
  130. * @head: &struct plist_head pointer - list head
  131. */
  132. void plist_requeue(struct plist_node *node, struct plist_head *head)
  133. {
  134. struct plist_node *iter;
  135. struct list_head *node_next = &head->node_list;
  136. plist_check_head(head);
  137. BUG_ON(plist_head_empty(head));
  138. BUG_ON(plist_node_empty(node));
  139. if (node == plist_last(head))
  140. return;
  141. iter = plist_next(node);
  142. if (node->prio != iter->prio)
  143. return;
  144. plist_del(node, head);
  145. /*
  146. * After plist_del(), iter is the replacement of the node. If the node
  147. * was on prio_list, take shortcut to find node_next instead of looping.
  148. */
  149. if (!list_empty(&iter->prio_list)) {
  150. iter = list_entry(iter->prio_list.next, struct plist_node,
  151. prio_list);
  152. node_next = &iter->node_list;
  153. goto queue;
  154. }
  155. plist_for_each_continue(iter, head) {
  156. if (node->prio != iter->prio) {
  157. node_next = &iter->node_list;
  158. break;
  159. }
  160. }
  161. queue:
  162. list_add_tail(&node->node_list, node_next);
  163. plist_check_head(head);
  164. }
  165. #ifdef CONFIG_DEBUG_PLIST
  166. #include <linux/sched.h>
  167. #include <linux/sched/clock.h>
  168. #include <linux/module.h>
  169. #include <linux/init.h>
  170. static struct plist_node __initdata test_node[241];
  171. static void __init plist_test_check(int nr_expect)
  172. {
  173. struct plist_node *first, *prio_pos, *node_pos;
  174. if (plist_head_empty(&test_head)) {
  175. BUG_ON(nr_expect != 0);
  176. return;
  177. }
  178. prio_pos = first = plist_first(&test_head);
  179. plist_for_each(node_pos, &test_head) {
  180. if (nr_expect-- < 0)
  181. break;
  182. if (node_pos == first)
  183. continue;
  184. if (node_pos->prio == prio_pos->prio) {
  185. BUG_ON(!list_empty(&node_pos->prio_list));
  186. continue;
  187. }
  188. BUG_ON(prio_pos->prio > node_pos->prio);
  189. BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
  190. prio_pos = node_pos;
  191. }
  192. BUG_ON(nr_expect != 0);
  193. BUG_ON(prio_pos->prio_list.next != &first->prio_list);
  194. }
  195. static void __init plist_test_requeue(struct plist_node *node)
  196. {
  197. plist_requeue(node, &test_head);
  198. if (node != plist_last(&test_head))
  199. BUG_ON(node->prio == plist_next(node)->prio);
  200. }
  201. static int __init plist_test(void)
  202. {
  203. int nr_expect = 0, i, loop;
  204. unsigned int r = local_clock();
  205. printk(KERN_DEBUG "start plist test\n");
  206. plist_head_init(&test_head);
  207. for (i = 0; i < ARRAY_SIZE(test_node); i++)
  208. plist_node_init(test_node + i, 0);
  209. for (loop = 0; loop < 1000; loop++) {
  210. r = r * 193939 % 47629;
  211. i = r % ARRAY_SIZE(test_node);
  212. if (plist_node_empty(test_node + i)) {
  213. r = r * 193939 % 47629;
  214. test_node[i].prio = r % 99;
  215. plist_add(test_node + i, &test_head);
  216. nr_expect++;
  217. } else {
  218. plist_del(test_node + i, &test_head);
  219. nr_expect--;
  220. }
  221. plist_test_check(nr_expect);
  222. if (!plist_node_empty(test_node + i)) {
  223. plist_test_requeue(test_node + i);
  224. plist_test_check(nr_expect);
  225. }
  226. }
  227. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  228. if (plist_node_empty(test_node + i))
  229. continue;
  230. plist_del(test_node + i, &test_head);
  231. nr_expect--;
  232. plist_test_check(nr_expect);
  233. }
  234. printk(KERN_DEBUG "end plist test\n");
  235. /* Worst case test for plist_add() */
  236. unsigned int test_data[241];
  237. for (i = 0; i < ARRAY_SIZE(test_data); i++)
  238. test_data[i] = i;
  239. ktime_t start, end, time_elapsed = 0;
  240. plist_head_init(&test_head);
  241. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  242. plist_node_init(test_node + i, 0);
  243. test_node[i].prio = test_data[i];
  244. }
  245. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  246. if (plist_node_empty(test_node + i)) {
  247. start = ktime_get();
  248. plist_add(test_node + i, &test_head);
  249. end = ktime_get();
  250. time_elapsed += (end - start);
  251. }
  252. }
  253. pr_debug("plist_add worst case test time elapsed %lld\n", time_elapsed);
  254. return 0;
  255. }
  256. module_init(plist_test);
  257. #endif