Thomas Gleixner | aded9cb | 2019-05-19 15:51:40 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 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 | * |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 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 | |
Paul Gortmaker | 50af5ea | 2012-01-20 18:35:53 -0500 | [diff] [blame] | 25 | #include <linux/bug.h> |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 26 | #include <linux/plist.h> |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 27 | |
Davidlohr Bueso | 8e18fae | 2019-05-14 15:42:46 -0700 | [diff] [blame] | 28 | #ifdef CONFIG_DEBUG_PLIST |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 29 | |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 30 | static struct plist_head test_head; |
| 31 | |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 32 | static void plist_check_prev_next(struct list_head *t, struct list_head *p, |
| 33 | struct list_head *n) |
| 34 | { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 35 | WARN(n->prev != p || p->next != n, |
| 36 | "top: %p, n: %p, p: %p\n" |
| 37 | "prev: %p, n: %p, p: %p\n" |
| 38 | "next: %p, n: %p, p: %p\n", |
| 39 | t, t->next, t->prev, |
| 40 | p, p->next, p->prev, |
| 41 | n, n->next, n->prev); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void plist_check_list(struct list_head *top) |
| 45 | { |
| 46 | struct list_head *prev = top, *next = top->next; |
| 47 | |
| 48 | plist_check_prev_next(top, prev, next); |
| 49 | while (next != top) { |
| 50 | prev = next; |
| 51 | next = prev->next; |
| 52 | plist_check_prev_next(top, prev, next); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void plist_check_head(struct plist_head *head) |
| 57 | { |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 58 | if (!plist_head_empty(head)) |
| 59 | plist_check_list(&plist_first(head)->prio_list); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 60 | plist_check_list(&head->node_list); |
| 61 | } |
| 62 | |
| 63 | #else |
| 64 | # define plist_check_head(h) do { } while (0) |
| 65 | #endif |
| 66 | |
| 67 | /** |
| 68 | * plist_add - add @node to @head |
| 69 | * |
| 70 | * @node: &struct plist_node pointer |
| 71 | * @head: &struct plist_head pointer |
| 72 | */ |
| 73 | void plist_add(struct plist_node *node, struct plist_head *head) |
| 74 | { |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 75 | struct plist_node *first, *iter, *prev = NULL; |
| 76 | struct list_head *node_next = &head->node_list; |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 77 | |
| 78 | plist_check_head(head); |
| 79 | WARN_ON(!plist_node_empty(node)); |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 80 | WARN_ON(!list_empty(&node->prio_list)); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 81 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 82 | if (plist_head_empty(head)) |
| 83 | goto ins_node; |
| 84 | |
| 85 | first = iter = plist_first(head); |
| 86 | |
| 87 | do { |
| 88 | if (node->prio < iter->prio) { |
| 89 | node_next = &iter->node_list; |
| 90 | break; |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 91 | } |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 92 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 93 | prev = iter; |
| 94 | iter = list_entry(iter->prio_list.next, |
| 95 | struct plist_node, prio_list); |
| 96 | } while (iter != first); |
| 97 | |
| 98 | if (!prev || prev->prio != node->prio) |
| 99 | list_add_tail(&node->prio_list, &iter->prio_list); |
| 100 | ins_node: |
| 101 | list_add_tail(&node->node_list, node_next); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 102 | |
| 103 | plist_check_head(head); |
| 104 | } |
Choonghoon Park | 7656aa0 | 2021-02-23 14:38:32 +0900 | [diff] [blame] | 105 | EXPORT_SYMBOL_GPL(plist_add); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * plist_del - Remove a @node from plist. |
| 109 | * |
| 110 | * @node: &struct plist_node pointer - entry to be removed |
| 111 | * @head: &struct plist_head pointer - list head |
| 112 | */ |
| 113 | void plist_del(struct plist_node *node, struct plist_head *head) |
| 114 | { |
| 115 | plist_check_head(head); |
| 116 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 117 | if (!list_empty(&node->prio_list)) { |
| 118 | if (node->node_list.next != &head->node_list) { |
| 119 | struct plist_node *next; |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 120 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 121 | next = list_entry(node->node_list.next, |
| 122 | struct plist_node, node_list); |
| 123 | |
| 124 | /* add the next plist_node into prio_list */ |
| 125 | if (list_empty(&next->prio_list)) |
| 126 | list_add(&next->prio_list, &node->prio_list); |
| 127 | } |
| 128 | list_del_init(&node->prio_list); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 131 | list_del_init(&node->node_list); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 132 | |
| 133 | plist_check_head(head); |
| 134 | } |
Choonghoon Park | 7656aa0 | 2021-02-23 14:38:32 +0900 | [diff] [blame] | 135 | EXPORT_SYMBOL_GPL(plist_del); |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 136 | |
Dan Streetman | a75f232 | 2014-06-04 16:09:57 -0700 | [diff] [blame] | 137 | /** |
| 138 | * plist_requeue - Requeue @node at end of same-prio entries. |
| 139 | * |
| 140 | * This is essentially an optimized plist_del() followed by |
| 141 | * plist_add(). It moves an entry already in the plist to |
| 142 | * after any other same-priority entries. |
| 143 | * |
| 144 | * @node: &struct plist_node pointer - entry to be moved |
| 145 | * @head: &struct plist_head pointer - list head |
| 146 | */ |
| 147 | void plist_requeue(struct plist_node *node, struct plist_head *head) |
| 148 | { |
| 149 | struct plist_node *iter; |
| 150 | struct list_head *node_next = &head->node_list; |
| 151 | |
| 152 | plist_check_head(head); |
| 153 | BUG_ON(plist_head_empty(head)); |
| 154 | BUG_ON(plist_node_empty(node)); |
| 155 | |
| 156 | if (node == plist_last(head)) |
| 157 | return; |
| 158 | |
| 159 | iter = plist_next(node); |
| 160 | |
| 161 | if (node->prio != iter->prio) |
| 162 | return; |
| 163 | |
| 164 | plist_del(node, head); |
| 165 | |
| 166 | plist_for_each_continue(iter, head) { |
| 167 | if (node->prio != iter->prio) { |
| 168 | node_next = &iter->node_list; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | list_add_tail(&node->node_list, node_next); |
| 173 | |
| 174 | plist_check_head(head); |
| 175 | } |
Choonghoon Park | 7656aa0 | 2021-02-23 14:38:32 +0900 | [diff] [blame] | 176 | EXPORT_SYMBOL_GPL(plist_requeue); |
Dan Streetman | a75f232 | 2014-06-04 16:09:57 -0700 | [diff] [blame] | 177 | |
Davidlohr Bueso | 8e18fae | 2019-05-14 15:42:46 -0700 | [diff] [blame] | 178 | #ifdef CONFIG_DEBUG_PLIST |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 179 | #include <linux/sched.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 180 | #include <linux/sched/clock.h> |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 181 | #include <linux/module.h> |
| 182 | #include <linux/init.h> |
| 183 | |
| 184 | static struct plist_node __initdata test_node[241]; |
| 185 | |
| 186 | static void __init plist_test_check(int nr_expect) |
| 187 | { |
| 188 | struct plist_node *first, *prio_pos, *node_pos; |
| 189 | |
| 190 | if (plist_head_empty(&test_head)) { |
| 191 | BUG_ON(nr_expect != 0); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | prio_pos = first = plist_first(&test_head); |
| 196 | plist_for_each(node_pos, &test_head) { |
| 197 | if (nr_expect-- < 0) |
| 198 | break; |
| 199 | if (node_pos == first) |
| 200 | continue; |
| 201 | if (node_pos->prio == prio_pos->prio) { |
| 202 | BUG_ON(!list_empty(&node_pos->prio_list)); |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | BUG_ON(prio_pos->prio > node_pos->prio); |
| 207 | BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list); |
| 208 | prio_pos = node_pos; |
| 209 | } |
| 210 | |
| 211 | BUG_ON(nr_expect != 0); |
| 212 | BUG_ON(prio_pos->prio_list.next != &first->prio_list); |
| 213 | } |
| 214 | |
Dan Streetman | a75f232 | 2014-06-04 16:09:57 -0700 | [diff] [blame] | 215 | static void __init plist_test_requeue(struct plist_node *node) |
| 216 | { |
| 217 | plist_requeue(node, &test_head); |
| 218 | |
| 219 | if (node != plist_last(&test_head)) |
| 220 | BUG_ON(node->prio == plist_next(node)->prio); |
| 221 | } |
| 222 | |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 223 | static int __init plist_test(void) |
| 224 | { |
| 225 | int nr_expect = 0, i, loop; |
| 226 | unsigned int r = local_clock(); |
| 227 | |
Dan Streetman | 1812062 | 2014-06-04 16:11:49 -0700 | [diff] [blame] | 228 | printk(KERN_DEBUG "start plist test\n"); |
Dima Zavin | 732375c | 2011-07-07 17:27:59 -0700 | [diff] [blame] | 229 | plist_head_init(&test_head); |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 230 | for (i = 0; i < ARRAY_SIZE(test_node); i++) |
| 231 | plist_node_init(test_node + i, 0); |
| 232 | |
| 233 | for (loop = 0; loop < 1000; loop++) { |
| 234 | r = r * 193939 % 47629; |
| 235 | i = r % ARRAY_SIZE(test_node); |
| 236 | if (plist_node_empty(test_node + i)) { |
| 237 | r = r * 193939 % 47629; |
| 238 | test_node[i].prio = r % 99; |
| 239 | plist_add(test_node + i, &test_head); |
| 240 | nr_expect++; |
| 241 | } else { |
| 242 | plist_del(test_node + i, &test_head); |
| 243 | nr_expect--; |
| 244 | } |
| 245 | plist_test_check(nr_expect); |
Dan Streetman | a75f232 | 2014-06-04 16:09:57 -0700 | [diff] [blame] | 246 | if (!plist_node_empty(test_node + i)) { |
| 247 | plist_test_requeue(test_node + i); |
| 248 | plist_test_check(nr_expect); |
| 249 | } |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | for (i = 0; i < ARRAY_SIZE(test_node); i++) { |
| 253 | if (plist_node_empty(test_node + i)) |
| 254 | continue; |
| 255 | plist_del(test_node + i, &test_head); |
| 256 | nr_expect--; |
| 257 | plist_test_check(nr_expect); |
| 258 | } |
| 259 | |
Dan Streetman | 1812062 | 2014-06-04 16:11:49 -0700 | [diff] [blame] | 260 | printk(KERN_DEBUG "end plist test\n"); |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | module_init(plist_test); |
| 265 | |
| 266 | #endif |