Loading...
1/*
2 * lib/plist.c
3 *
4 * Descending-priority-sorted double-linked list
5 *
6 * (C) 2002-2003 Intel Corp
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
8 *
9 * 2001-2005 (c) MontaVista Software, Inc.
10 * Daniel Walker <dwalker@mvista.com>
11 *
12 * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
13 *
14 * Simplifications of the original code by
15 * Oleg Nesterov <oleg@tv-sign.ru>
16 *
17 * Licensed under the FSF's GNU Public License v2 or later.
18 *
19 * Based on simple lists (include/linux/list.h).
20 *
21 * This file contains the add / del functions which are considered to
22 * be too large to inline. See include/linux/plist.h for further
23 * information.
24 */
25
26#include <linux/bug.h>
27#include <linux/plist.h>
28#include <linux/spinlock.h>
29
30#ifdef CONFIG_DEBUG_PI_LIST
31
32static struct plist_head test_head;
33
34static void plist_check_prev_next(struct list_head *t, struct list_head *p,
35 struct list_head *n)
36{
37 WARN(n->prev != p || p->next != n,
38 "top: %p, n: %p, p: %p\n"
39 "prev: %p, n: %p, p: %p\n"
40 "next: %p, n: %p, p: %p\n",
41 t, t->next, t->prev,
42 p, p->next, p->prev,
43 n, n->next, n->prev);
44}
45
46static void plist_check_list(struct list_head *top)
47{
48 struct list_head *prev = top, *next = top->next;
49
50 plist_check_prev_next(top, prev, next);
51 while (next != top) {
52 prev = next;
53 next = prev->next;
54 plist_check_prev_next(top, prev, next);
55 }
56}
57
58static void plist_check_head(struct plist_head *head)
59{
60 if (!plist_head_empty(head))
61 plist_check_list(&plist_first(head)->prio_list);
62 plist_check_list(&head->node_list);
63}
64
65#else
66# define plist_check_head(h) do { } while (0)
67#endif
68
69/**
70 * plist_add - add @node to @head
71 *
72 * @node: &struct plist_node pointer
73 * @head: &struct plist_head pointer
74 */
75void plist_add(struct plist_node *node, struct plist_head *head)
76{
77 struct plist_node *first, *iter, *prev = NULL;
78 struct list_head *node_next = &head->node_list;
79
80 plist_check_head(head);
81 WARN_ON(!plist_node_empty(node));
82 WARN_ON(!list_empty(&node->prio_list));
83
84 if (plist_head_empty(head))
85 goto ins_node;
86
87 first = iter = plist_first(head);
88
89 do {
90 if (node->prio < iter->prio) {
91 node_next = &iter->node_list;
92 break;
93 }
94
95 prev = iter;
96 iter = list_entry(iter->prio_list.next,
97 struct plist_node, prio_list);
98 } while (iter != first);
99
100 if (!prev || prev->prio != node->prio)
101 list_add_tail(&node->prio_list, &iter->prio_list);
102ins_node:
103 list_add_tail(&node->node_list, node_next);
104
105 plist_check_head(head);
106}
107
108/**
109 * plist_del - Remove a @node from plist.
110 *
111 * @node: &struct plist_node pointer - entry to be removed
112 * @head: &struct plist_head pointer - list head
113 */
114void plist_del(struct plist_node *node, struct plist_head *head)
115{
116 plist_check_head(head);
117
118 if (!list_empty(&node->prio_list)) {
119 if (node->node_list.next != &head->node_list) {
120 struct plist_node *next;
121
122 next = list_entry(node->node_list.next,
123 struct plist_node, node_list);
124
125 /* add the next plist_node into prio_list */
126 if (list_empty(&next->prio_list))
127 list_add(&next->prio_list, &node->prio_list);
128 }
129 list_del_init(&node->prio_list);
130 }
131
132 list_del_init(&node->node_list);
133
134 plist_check_head(head);
135}
136
137#ifdef CONFIG_DEBUG_PI_LIST
138#include <linux/sched.h>
139#include <linux/module.h>
140#include <linux/init.h>
141
142static struct plist_node __initdata test_node[241];
143
144static void __init plist_test_check(int nr_expect)
145{
146 struct plist_node *first, *prio_pos, *node_pos;
147
148 if (plist_head_empty(&test_head)) {
149 BUG_ON(nr_expect != 0);
150 return;
151 }
152
153 prio_pos = first = plist_first(&test_head);
154 plist_for_each(node_pos, &test_head) {
155 if (nr_expect-- < 0)
156 break;
157 if (node_pos == first)
158 continue;
159 if (node_pos->prio == prio_pos->prio) {
160 BUG_ON(!list_empty(&node_pos->prio_list));
161 continue;
162 }
163
164 BUG_ON(prio_pos->prio > node_pos->prio);
165 BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
166 prio_pos = node_pos;
167 }
168
169 BUG_ON(nr_expect != 0);
170 BUG_ON(prio_pos->prio_list.next != &first->prio_list);
171}
172
173static int __init plist_test(void)
174{
175 int nr_expect = 0, i, loop;
176 unsigned int r = local_clock();
177
178 pr_debug("start plist test\n");
179 plist_head_init(&test_head);
180 for (i = 0; i < ARRAY_SIZE(test_node); i++)
181 plist_node_init(test_node + i, 0);
182
183 for (loop = 0; loop < 1000; loop++) {
184 r = r * 193939 % 47629;
185 i = r % ARRAY_SIZE(test_node);
186 if (plist_node_empty(test_node + i)) {
187 r = r * 193939 % 47629;
188 test_node[i].prio = r % 99;
189 plist_add(test_node + i, &test_head);
190 nr_expect++;
191 } else {
192 plist_del(test_node + i, &test_head);
193 nr_expect--;
194 }
195 plist_test_check(nr_expect);
196 }
197
198 for (i = 0; i < ARRAY_SIZE(test_node); i++) {
199 if (plist_node_empty(test_node + i))
200 continue;
201 plist_del(test_node + i, &test_head);
202 nr_expect--;
203 plist_test_check(nr_expect);
204 }
205
206 pr_debug("end plist test\n");
207 return 0;
208}
209
210module_init(plist_test);
211
212#endif
1/*
2 * lib/plist.c
3 *
4 * Descending-priority-sorted double-linked list
5 *
6 * (C) 2002-2003 Intel Corp
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
8 *
9 * 2001-2005 (c) MontaVista Software, Inc.
10 * Daniel Walker <dwalker@mvista.com>
11 *
12 * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
13 *
14 * Simplifications of the original code by
15 * Oleg Nesterov <oleg@tv-sign.ru>
16 *
17 * Licensed under the FSF's GNU Public License v2 or later.
18 *
19 * Based on simple lists (include/linux/list.h).
20 *
21 * This file contains the add / del functions which are considered to
22 * be too large to inline. See include/linux/plist.h for further
23 * information.
24 */
25
26#include <linux/bug.h>
27#include <linux/plist.h>
28
29#ifdef CONFIG_DEBUG_PI_LIST
30
31static struct plist_head test_head;
32
33static void plist_check_prev_next(struct list_head *t, struct list_head *p,
34 struct list_head *n)
35{
36 WARN(n->prev != p || p->next != n,
37 "top: %p, n: %p, p: %p\n"
38 "prev: %p, n: %p, p: %p\n"
39 "next: %p, n: %p, p: %p\n",
40 t, t->next, t->prev,
41 p, p->next, p->prev,
42 n, n->next, n->prev);
43}
44
45static void plist_check_list(struct list_head *top)
46{
47 struct list_head *prev = top, *next = top->next;
48
49 plist_check_prev_next(top, prev, next);
50 while (next != top) {
51 prev = next;
52 next = prev->next;
53 plist_check_prev_next(top, prev, next);
54 }
55}
56
57static void plist_check_head(struct plist_head *head)
58{
59 if (!plist_head_empty(head))
60 plist_check_list(&plist_first(head)->prio_list);
61 plist_check_list(&head->node_list);
62}
63
64#else
65# define plist_check_head(h) do { } while (0)
66#endif
67
68/**
69 * plist_add - add @node to @head
70 *
71 * @node: &struct plist_node pointer
72 * @head: &struct plist_head pointer
73 */
74void plist_add(struct plist_node *node, struct plist_head *head)
75{
76 struct plist_node *first, *iter, *prev = NULL;
77 struct list_head *node_next = &head->node_list;
78
79 plist_check_head(head);
80 WARN_ON(!plist_node_empty(node));
81 WARN_ON(!list_empty(&node->prio_list));
82
83 if (plist_head_empty(head))
84 goto ins_node;
85
86 first = iter = plist_first(head);
87
88 do {
89 if (node->prio < iter->prio) {
90 node_next = &iter->node_list;
91 break;
92 }
93
94 prev = iter;
95 iter = list_entry(iter->prio_list.next,
96 struct plist_node, prio_list);
97 } while (iter != first);
98
99 if (!prev || prev->prio != node->prio)
100 list_add_tail(&node->prio_list, &iter->prio_list);
101ins_node:
102 list_add_tail(&node->node_list, node_next);
103
104 plist_check_head(head);
105}
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 */
113void plist_del(struct plist_node *node, struct plist_head *head)
114{
115 plist_check_head(head);
116
117 if (!list_empty(&node->prio_list)) {
118 if (node->node_list.next != &head->node_list) {
119 struct plist_node *next;
120
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);
129 }
130
131 list_del_init(&node->node_list);
132
133 plist_check_head(head);
134}
135
136/**
137 * plist_requeue - Requeue @node at end of same-prio entries.
138 *
139 * This is essentially an optimized plist_del() followed by
140 * plist_add(). It moves an entry already in the plist to
141 * after any other same-priority entries.
142 *
143 * @node: &struct plist_node pointer - entry to be moved
144 * @head: &struct plist_head pointer - list head
145 */
146void plist_requeue(struct plist_node *node, struct plist_head *head)
147{
148 struct plist_node *iter;
149 struct list_head *node_next = &head->node_list;
150
151 plist_check_head(head);
152 BUG_ON(plist_head_empty(head));
153 BUG_ON(plist_node_empty(node));
154
155 if (node == plist_last(head))
156 return;
157
158 iter = plist_next(node);
159
160 if (node->prio != iter->prio)
161 return;
162
163 plist_del(node, head);
164
165 plist_for_each_continue(iter, head) {
166 if (node->prio != iter->prio) {
167 node_next = &iter->node_list;
168 break;
169 }
170 }
171 list_add_tail(&node->node_list, node_next);
172
173 plist_check_head(head);
174}
175
176#ifdef CONFIG_DEBUG_PI_LIST
177#include <linux/sched.h>
178#include <linux/module.h>
179#include <linux/init.h>
180
181static struct plist_node __initdata test_node[241];
182
183static void __init plist_test_check(int nr_expect)
184{
185 struct plist_node *first, *prio_pos, *node_pos;
186
187 if (plist_head_empty(&test_head)) {
188 BUG_ON(nr_expect != 0);
189 return;
190 }
191
192 prio_pos = first = plist_first(&test_head);
193 plist_for_each(node_pos, &test_head) {
194 if (nr_expect-- < 0)
195 break;
196 if (node_pos == first)
197 continue;
198 if (node_pos->prio == prio_pos->prio) {
199 BUG_ON(!list_empty(&node_pos->prio_list));
200 continue;
201 }
202
203 BUG_ON(prio_pos->prio > node_pos->prio);
204 BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
205 prio_pos = node_pos;
206 }
207
208 BUG_ON(nr_expect != 0);
209 BUG_ON(prio_pos->prio_list.next != &first->prio_list);
210}
211
212static void __init plist_test_requeue(struct plist_node *node)
213{
214 plist_requeue(node, &test_head);
215
216 if (node != plist_last(&test_head))
217 BUG_ON(node->prio == plist_next(node)->prio);
218}
219
220static int __init plist_test(void)
221{
222 int nr_expect = 0, i, loop;
223 unsigned int r = local_clock();
224
225 printk(KERN_DEBUG "start plist test\n");
226 plist_head_init(&test_head);
227 for (i = 0; i < ARRAY_SIZE(test_node); i++)
228 plist_node_init(test_node + i, 0);
229
230 for (loop = 0; loop < 1000; loop++) {
231 r = r * 193939 % 47629;
232 i = r % ARRAY_SIZE(test_node);
233 if (plist_node_empty(test_node + i)) {
234 r = r * 193939 % 47629;
235 test_node[i].prio = r % 99;
236 plist_add(test_node + i, &test_head);
237 nr_expect++;
238 } else {
239 plist_del(test_node + i, &test_head);
240 nr_expect--;
241 }
242 plist_test_check(nr_expect);
243 if (!plist_node_empty(test_node + i)) {
244 plist_test_requeue(test_node + i);
245 plist_test_check(nr_expect);
246 }
247 }
248
249 for (i = 0; i < ARRAY_SIZE(test_node); i++) {
250 if (plist_node_empty(test_node + i))
251 continue;
252 plist_del(test_node + i, &test_head);
253 nr_expect--;
254 plist_test_check(nr_expect);
255 }
256
257 printk(KERN_DEBUG "end plist test\n");
258 return 0;
259}
260
261module_init(plist_test);
262
263#endif