blob: 1c9e0617327046ba33e0f7f5ee9a7df9a5624ddf [file] [log] [blame]
Heiko Schocherc068d442014-06-24 10:10:01 +02001#ifndef __UBOOT__
Simon Glassf7ae49f2020-05-10 11:40:05 -06002#include <log.h>
Simon Glass61b29b82020-02-03 07:36:15 -07003#include <dm/devres.h>
Heiko Schocherc068d442014-06-24 10:10:01 +02004#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/slab.h>
7#else
8#include <linux/compat.h>
9#include <common.h>
10#include <malloc.h>
Simon Glass1e94b462023-09-14 18:21:46 -060011#include <linux/printk.h>
Heiko Schocherc068d442014-06-24 10:10:01 +020012#endif
13#include <linux/list.h>
14#include <linux/list_sort.h>
15
16#define MAX_LIST_LENGTH_BITS 20
17
18/*
19 * Returns a list organized in an intermediate format suited
20 * to chaining of merge() calls: null-terminated, no reserved or
21 * sentinel head node, "prev" links not maintained.
22 */
23static struct list_head *merge(void *priv,
24 int (*cmp)(void *priv, struct list_head *a,
25 struct list_head *b),
26 struct list_head *a, struct list_head *b)
27{
28 struct list_head head, *tail = &head;
29
30 while (a && b) {
31 /* if equal, take 'a' -- important for sort stability */
32 if ((*cmp)(priv, a, b) <= 0) {
33 tail->next = a;
34 a = a->next;
35 } else {
36 tail->next = b;
37 b = b->next;
38 }
39 tail = tail->next;
40 }
41 tail->next = a?:b;
42 return head.next;
43}
44
45/*
46 * Combine final list merge with restoration of standard doubly-linked
47 * list structure. This approach duplicates code from merge(), but
48 * runs faster than the tidier alternatives of either a separate final
49 * prev-link restoration pass, or maintaining the prev links
50 * throughout.
51 */
52static void merge_and_restore_back_links(void *priv,
53 int (*cmp)(void *priv, struct list_head *a,
54 struct list_head *b),
55 struct list_head *head,
56 struct list_head *a, struct list_head *b)
57{
58 struct list_head *tail = head;
59
60 while (a && b) {
61 /* if equal, take 'a' -- important for sort stability */
62 if ((*cmp)(priv, a, b) <= 0) {
63 tail->next = a;
64 a->prev = tail;
65 a = a->next;
66 } else {
67 tail->next = b;
68 b->prev = tail;
69 b = b->next;
70 }
71 tail = tail->next;
72 }
73 tail->next = a ? : b;
74
75 do {
76 /*
77 * In worst cases this loop may run many iterations.
78 * Continue callbacks to the client even though no
79 * element comparison is needed, so the client's cmp()
80 * routine can invoke cond_resched() periodically.
81 */
82 (*cmp)(priv, tail->next, tail->next);
83
84 tail->next->prev = tail;
85 tail = tail->next;
86 } while (tail->next);
87
88 tail->next = head;
89 head->prev = tail;
90}
91
92/**
93 * list_sort - sort a list
94 * @priv: private data, opaque to list_sort(), passed to @cmp
95 * @head: the list to sort
96 * @cmp: the elements comparison function
97 *
98 * This function implements "merge sort", which has O(nlog(n))
99 * complexity.
100 *
101 * The comparison function @cmp must return a negative value if @a
102 * should sort before @b, and a positive value if @a should sort after
103 * @b. If @a and @b are equivalent, and their original relative
104 * ordering is to be preserved, @cmp must return 0.
105 */
106void list_sort(void *priv, struct list_head *head,
107 int (*cmp)(void *priv, struct list_head *a,
108 struct list_head *b))
109{
110 struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
111 -- last slot is a sentinel */
112 int lev; /* index into part[] */
113 int max_lev = 0;
114 struct list_head *list;
115
116 if (list_empty(head))
117 return;
118
119 memset(part, 0, sizeof(part));
120
121 head->prev->next = NULL;
122 list = head->next;
123
124 while (list) {
125 struct list_head *cur = list;
126 list = list->next;
127 cur->next = NULL;
128
129 for (lev = 0; part[lev]; lev++) {
130 cur = merge(priv, cmp, part[lev], cur);
131 part[lev] = NULL;
132 }
133 if (lev > max_lev) {
134 if (unlikely(lev >= ARRAY_SIZE(part)-1)) {
135 printk_once(KERN_DEBUG "list passed to"
136 " list_sort() too long for"
137 " efficiency\n");
138 lev--;
139 }
140 max_lev = lev;
141 }
142 part[lev] = cur;
143 }
144
145 for (lev = 0; lev < max_lev; lev++)
146 if (part[lev])
147 list = merge(priv, cmp, part[lev], list);
148
149 merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
150}
151EXPORT_SYMBOL(list_sort);
152
153#ifdef CONFIG_TEST_LIST_SORT
154
155#include <linux/random.h>
156
157/*
158 * The pattern of set bits in the list length determines which cases
159 * are hit in list_sort().
160 */
161#define TEST_LIST_LEN (512+128+2) /* not including head */
162
163#define TEST_POISON1 0xDEADBEEF
164#define TEST_POISON2 0xA324354C
165
166struct debug_el {
167 unsigned int poison1;
168 struct list_head list;
169 unsigned int poison2;
170 int value;
171 unsigned serial;
172};
173
174/* Array, containing pointers to all elements in the test list */
175static struct debug_el **elts __initdata;
176
177static int __init check(struct debug_el *ela, struct debug_el *elb)
178{
179 if (ela->serial >= TEST_LIST_LEN) {
180 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
181 ela->serial);
182 return -EINVAL;
183 }
184 if (elb->serial >= TEST_LIST_LEN) {
185 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
186 elb->serial);
187 return -EINVAL;
188 }
189 if (elts[ela->serial] != ela || elts[elb->serial] != elb) {
190 printk(KERN_ERR "list_sort_test: error: phantom element\n");
191 return -EINVAL;
192 }
193 if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) {
194 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
195 ela->poison1, ela->poison2);
196 return -EINVAL;
197 }
198 if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
199 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
200 elb->poison1, elb->poison2);
201 return -EINVAL;
202 }
203 return 0;
204}
205
206static int __init cmp(void *priv, struct list_head *a, struct list_head *b)
207{
208 struct debug_el *ela, *elb;
209
210 ela = container_of(a, struct debug_el, list);
211 elb = container_of(b, struct debug_el, list);
212
213 check(ela, elb);
214 return ela->value - elb->value;
215}
216
217static int __init list_sort_test(void)
218{
219 int i, count = 1, err = -EINVAL;
220 struct debug_el *el;
221 struct list_head *cur, *tmp;
222 LIST_HEAD(head);
223
224 printk(KERN_DEBUG "list_sort_test: start testing list_sort()\n");
225
226 elts = kmalloc(sizeof(void *) * TEST_LIST_LEN, GFP_KERNEL);
227 if (!elts) {
228 printk(KERN_ERR "list_sort_test: error: cannot allocate "
229 "memory\n");
230 goto exit;
231 }
232
233 for (i = 0; i < TEST_LIST_LEN; i++) {
234 el = kmalloc(sizeof(*el), GFP_KERNEL);
235 if (!el) {
236 printk(KERN_ERR "list_sort_test: error: cannot "
237 "allocate memory\n");
238 goto exit;
239 }
240 /* force some equivalencies */
241 el->value = prandom_u32() % (TEST_LIST_LEN / 3);
242 el->serial = i;
243 el->poison1 = TEST_POISON1;
244 el->poison2 = TEST_POISON2;
245 elts[i] = el;
246 list_add_tail(&el->list, &head);
247 }
248
249 list_sort(NULL, &head, cmp);
250
251 for (cur = head.next; cur->next != &head; cur = cur->next) {
252 struct debug_el *el1;
253 int cmp_result;
254
255 if (cur->next->prev != cur) {
256 printk(KERN_ERR "list_sort_test: error: list is "
257 "corrupted\n");
258 goto exit;
259 }
260
261 cmp_result = cmp(NULL, cur, cur->next);
262 if (cmp_result > 0) {
263 printk(KERN_ERR "list_sort_test: error: list is not "
264 "sorted\n");
265 goto exit;
266 }
267
268 el = container_of(cur, struct debug_el, list);
269 el1 = container_of(cur->next, struct debug_el, list);
270 if (cmp_result == 0 && el->serial >= el1->serial) {
271 printk(KERN_ERR "list_sort_test: error: order of "
272 "equivalent elements not preserved\n");
273 goto exit;
274 }
275
276 if (check(el, el1)) {
277 printk(KERN_ERR "list_sort_test: error: element check "
278 "failed\n");
279 goto exit;
280 }
281 count++;
282 }
283
284 if (count != TEST_LIST_LEN) {
285 printk(KERN_ERR "list_sort_test: error: bad list length %d",
286 count);
287 goto exit;
288 }
289
290 err = 0;
291exit:
292 kfree(elts);
293 list_for_each_safe(cur, tmp, &head) {
294 list_del(cur);
295 kfree(container_of(cur, struct debug_el, list));
296 }
297 return err;
298}
299module_init(list_sort_test);
300#endif /* CONFIG_TEST_LIST_SORT */