Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * lib/plist.c
0004  *
0005  * Descending-priority-sorted double-linked list
0006  *
0007  * (C) 2002-2003 Intel Corp
0008  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
0009  *
0010  * 2001-2005 (c) MontaVista Software, Inc.
0011  * Daniel Walker <dwalker@mvista.com>
0012  *
0013  * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
0014  *
0015  * Simplifications of the original code by
0016  * Oleg Nesterov <oleg@tv-sign.ru>
0017  *
0018  * Based on simple lists (include/linux/list.h).
0019  *
0020  * This file contains the add / del functions which are considered to
0021  * be too large to inline. See include/linux/plist.h for further
0022  * information.
0023  */
0024 
0025 #include <linux/bug.h>
0026 #include <linux/plist.h>
0027 
0028 #ifdef CONFIG_DEBUG_PLIST
0029 
0030 static struct plist_head test_head;
0031 
0032 static void plist_check_prev_next(struct list_head *t, struct list_head *p,
0033                   struct list_head *n)
0034 {
0035     WARN(n->prev != p || p->next != n,
0036             "top: %p, n: %p, p: %p\n"
0037             "prev: %p, n: %p, p: %p\n"
0038             "next: %p, n: %p, p: %p\n",
0039              t, t->next, t->prev,
0040             p, p->next, p->prev,
0041             n, n->next, n->prev);
0042 }
0043 
0044 static void plist_check_list(struct list_head *top)
0045 {
0046     struct list_head *prev = top, *next = top->next;
0047 
0048     plist_check_prev_next(top, prev, next);
0049     while (next != top) {
0050         prev = next;
0051         next = prev->next;
0052         plist_check_prev_next(top, prev, next);
0053     }
0054 }
0055 
0056 static void plist_check_head(struct plist_head *head)
0057 {
0058     if (!plist_head_empty(head))
0059         plist_check_list(&plist_first(head)->prio_list);
0060     plist_check_list(&head->node_list);
0061 }
0062 
0063 #else
0064 # define plist_check_head(h)    do { } while (0)
0065 #endif
0066 
0067 /**
0068  * plist_add - add @node to @head
0069  *
0070  * @node:   &struct plist_node pointer
0071  * @head:   &struct plist_head pointer
0072  */
0073 void plist_add(struct plist_node *node, struct plist_head *head)
0074 {
0075     struct plist_node *first, *iter, *prev = NULL;
0076     struct list_head *node_next = &head->node_list;
0077 
0078     plist_check_head(head);
0079     WARN_ON(!plist_node_empty(node));
0080     WARN_ON(!list_empty(&node->prio_list));
0081 
0082     if (plist_head_empty(head))
0083         goto ins_node;
0084 
0085     first = iter = plist_first(head);
0086 
0087     do {
0088         if (node->prio < iter->prio) {
0089             node_next = &iter->node_list;
0090             break;
0091         }
0092 
0093         prev = iter;
0094         iter = list_entry(iter->prio_list.next,
0095                 struct plist_node, prio_list);
0096     } while (iter != first);
0097 
0098     if (!prev || prev->prio != node->prio)
0099         list_add_tail(&node->prio_list, &iter->prio_list);
0100 ins_node:
0101     list_add_tail(&node->node_list, node_next);
0102 
0103     plist_check_head(head);
0104 }
0105 
0106 /**
0107  * plist_del - Remove a @node from plist.
0108  *
0109  * @node:   &struct plist_node pointer - entry to be removed
0110  * @head:   &struct plist_head pointer - list head
0111  */
0112 void plist_del(struct plist_node *node, struct plist_head *head)
0113 {
0114     plist_check_head(head);
0115 
0116     if (!list_empty(&node->prio_list)) {
0117         if (node->node_list.next != &head->node_list) {
0118             struct plist_node *next;
0119 
0120             next = list_entry(node->node_list.next,
0121                     struct plist_node, node_list);
0122 
0123             /* add the next plist_node into prio_list */
0124             if (list_empty(&next->prio_list))
0125                 list_add(&next->prio_list, &node->prio_list);
0126         }
0127         list_del_init(&node->prio_list);
0128     }
0129 
0130     list_del_init(&node->node_list);
0131 
0132     plist_check_head(head);
0133 }
0134 
0135 /**
0136  * plist_requeue - Requeue @node at end of same-prio entries.
0137  *
0138  * This is essentially an optimized plist_del() followed by
0139  * plist_add().  It moves an entry already in the plist to
0140  * after any other same-priority entries.
0141  *
0142  * @node:   &struct plist_node pointer - entry to be moved
0143  * @head:   &struct plist_head pointer - list head
0144  */
0145 void plist_requeue(struct plist_node *node, struct plist_head *head)
0146 {
0147     struct plist_node *iter;
0148     struct list_head *node_next = &head->node_list;
0149 
0150     plist_check_head(head);
0151     BUG_ON(plist_head_empty(head));
0152     BUG_ON(plist_node_empty(node));
0153 
0154     if (node == plist_last(head))
0155         return;
0156 
0157     iter = plist_next(node);
0158 
0159     if (node->prio != iter->prio)
0160         return;
0161 
0162     plist_del(node, head);
0163 
0164     plist_for_each_continue(iter, head) {
0165         if (node->prio != iter->prio) {
0166             node_next = &iter->node_list;
0167             break;
0168         }
0169     }
0170     list_add_tail(&node->node_list, node_next);
0171 
0172     plist_check_head(head);
0173 }
0174 
0175 #ifdef CONFIG_DEBUG_PLIST
0176 #include <linux/sched.h>
0177 #include <linux/sched/clock.h>
0178 #include <linux/module.h>
0179 #include <linux/init.h>
0180 
0181 static struct plist_node __initdata test_node[241];
0182 
0183 static void __init plist_test_check(int nr_expect)
0184 {
0185     struct plist_node *first, *prio_pos, *node_pos;
0186 
0187     if (plist_head_empty(&test_head)) {
0188         BUG_ON(nr_expect != 0);
0189         return;
0190     }
0191 
0192     prio_pos = first = plist_first(&test_head);
0193     plist_for_each(node_pos, &test_head) {
0194         if (nr_expect-- < 0)
0195             break;
0196         if (node_pos == first)
0197             continue;
0198         if (node_pos->prio == prio_pos->prio) {
0199             BUG_ON(!list_empty(&node_pos->prio_list));
0200             continue;
0201         }
0202 
0203         BUG_ON(prio_pos->prio > node_pos->prio);
0204         BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
0205         prio_pos = node_pos;
0206     }
0207 
0208     BUG_ON(nr_expect != 0);
0209     BUG_ON(prio_pos->prio_list.next != &first->prio_list);
0210 }
0211 
0212 static void __init plist_test_requeue(struct plist_node *node)
0213 {
0214     plist_requeue(node, &test_head);
0215 
0216     if (node != plist_last(&test_head))
0217         BUG_ON(node->prio == plist_next(node)->prio);
0218 }
0219 
0220 static int  __init plist_test(void)
0221 {
0222     int nr_expect = 0, i, loop;
0223     unsigned int r = local_clock();
0224 
0225     printk(KERN_DEBUG "start plist test\n");
0226     plist_head_init(&test_head);
0227     for (i = 0; i < ARRAY_SIZE(test_node); i++)
0228         plist_node_init(test_node + i, 0);
0229 
0230     for (loop = 0; loop < 1000; loop++) {
0231         r = r * 193939 % 47629;
0232         i = r % ARRAY_SIZE(test_node);
0233         if (plist_node_empty(test_node + i)) {
0234             r = r * 193939 % 47629;
0235             test_node[i].prio = r % 99;
0236             plist_add(test_node + i, &test_head);
0237             nr_expect++;
0238         } else {
0239             plist_del(test_node + i, &test_head);
0240             nr_expect--;
0241         }
0242         plist_test_check(nr_expect);
0243         if (!plist_node_empty(test_node + i)) {
0244             plist_test_requeue(test_node + i);
0245             plist_test_check(nr_expect);
0246         }
0247     }
0248 
0249     for (i = 0; i < ARRAY_SIZE(test_node); i++) {
0250         if (plist_node_empty(test_node + i))
0251             continue;
0252         plist_del(test_node + i, &test_head);
0253         nr_expect--;
0254         plist_test_check(nr_expect);
0255     }
0256 
0257     printk(KERN_DEBUG "end plist test\n");
0258     return 0;
0259 }
0260 
0261 module_init(plist_test);
0262 
0263 #endif