~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/plist.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php