2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/slab.h>
6 #include <linux/compat.h>
10 #include <linux/list.h>
11 #include <linux/list_sort.h>
13 #define MAX_LIST_LENGTH_BITS 20
16 * Returns a list organized in an intermediate format suited
17 * to chaining of merge() calls: null-terminated, no reserved or
18 * sentinel head node, "prev" links not maintained.
20 static struct list_head *merge(void *priv,
21 int (*cmp)(void *priv, struct list_head *a,
23 struct list_head *a, struct list_head *b)
25 struct list_head head, *tail = &head;
28 /* if equal, take 'a' -- important for sort stability */
29 if ((*cmp)(priv, a, b) <= 0) {
43 * Combine final list merge with restoration of standard doubly-linked
44 * list structure. This approach duplicates code from merge(), but
45 * runs faster than the tidier alternatives of either a separate final
46 * prev-link restoration pass, or maintaining the prev links
49 static void merge_and_restore_back_links(void *priv,
50 int (*cmp)(void *priv, struct list_head *a,
52 struct list_head *head,
53 struct list_head *a, struct list_head *b)
55 struct list_head *tail = head;
58 /* if equal, take 'a' -- important for sort stability */
59 if ((*cmp)(priv, a, b) <= 0) {
74 * In worst cases this loop may run many iterations.
75 * Continue callbacks to the client even though no
76 * element comparison is needed, so the client's cmp()
77 * routine can invoke cond_resched() periodically.
79 (*cmp)(priv, tail->next, tail->next);
81 tail->next->prev = tail;
90 * list_sort - sort a list
91 * @priv: private data, opaque to list_sort(), passed to @cmp
92 * @head: the list to sort
93 * @cmp: the elements comparison function
95 * This function implements "merge sort", which has O(nlog(n))
98 * The comparison function @cmp must return a negative value if @a
99 * should sort before @b, and a positive value if @a should sort after
100 * @b. If @a and @b are equivalent, and their original relative
101 * ordering is to be preserved, @cmp must return 0.
103 void list_sort(void *priv, struct list_head *head,
104 int (*cmp)(void *priv, struct list_head *a,
105 struct list_head *b))
107 struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
108 -- last slot is a sentinel */
109 int lev; /* index into part[] */
111 struct list_head *list;
113 if (list_empty(head))
116 memset(part, 0, sizeof(part));
118 head->prev->next = NULL;
122 struct list_head *cur = list;
126 for (lev = 0; part[lev]; lev++) {
127 cur = merge(priv, cmp, part[lev], cur);
131 if (unlikely(lev >= ARRAY_SIZE(part)-1)) {
132 printk_once(KERN_DEBUG "list passed to"
133 " list_sort() too long for"
142 for (lev = 0; lev < max_lev; lev++)
144 list = merge(priv, cmp, part[lev], list);
146 merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
148 EXPORT_SYMBOL(list_sort);
150 #ifdef CONFIG_TEST_LIST_SORT
152 #include <linux/random.h>
155 * The pattern of set bits in the list length determines which cases
156 * are hit in list_sort().
158 #define TEST_LIST_LEN (512+128+2) /* not including head */
160 #define TEST_POISON1 0xDEADBEEF
161 #define TEST_POISON2 0xA324354C
164 unsigned int poison1;
165 struct list_head list;
166 unsigned int poison2;
171 /* Array, containing pointers to all elements in the test list */
172 static struct debug_el **elts __initdata;
174 static int __init check(struct debug_el *ela, struct debug_el *elb)
176 if (ela->serial >= TEST_LIST_LEN) {
177 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
181 if (elb->serial >= TEST_LIST_LEN) {
182 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
186 if (elts[ela->serial] != ela || elts[elb->serial] != elb) {
187 printk(KERN_ERR "list_sort_test: error: phantom element\n");
190 if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) {
191 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
192 ela->poison1, ela->poison2);
195 if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
196 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
197 elb->poison1, elb->poison2);
203 static int __init cmp(void *priv, struct list_head *a, struct list_head *b)
205 struct debug_el *ela, *elb;
207 ela = container_of(a, struct debug_el, list);
208 elb = container_of(b, struct debug_el, list);
211 return ela->value - elb->value;
214 static int __init list_sort_test(void)
216 int i, count = 1, err = -EINVAL;
218 struct list_head *cur, *tmp;
221 printk(KERN_DEBUG "list_sort_test: start testing list_sort()\n");
223 elts = kmalloc(sizeof(void *) * TEST_LIST_LEN, GFP_KERNEL);
225 printk(KERN_ERR "list_sort_test: error: cannot allocate "
230 for (i = 0; i < TEST_LIST_LEN; i++) {
231 el = kmalloc(sizeof(*el), GFP_KERNEL);
233 printk(KERN_ERR "list_sort_test: error: cannot "
234 "allocate memory\n");
237 /* force some equivalencies */
238 el->value = prandom_u32() % (TEST_LIST_LEN / 3);
240 el->poison1 = TEST_POISON1;
241 el->poison2 = TEST_POISON2;
243 list_add_tail(&el->list, &head);
246 list_sort(NULL, &head, cmp);
248 for (cur = head.next; cur->next != &head; cur = cur->next) {
249 struct debug_el *el1;
252 if (cur->next->prev != cur) {
253 printk(KERN_ERR "list_sort_test: error: list is "
258 cmp_result = cmp(NULL, cur, cur->next);
259 if (cmp_result > 0) {
260 printk(KERN_ERR "list_sort_test: error: list is not "
265 el = container_of(cur, struct debug_el, list);
266 el1 = container_of(cur->next, struct debug_el, list);
267 if (cmp_result == 0 && el->serial >= el1->serial) {
268 printk(KERN_ERR "list_sort_test: error: order of "
269 "equivalent elements not preserved\n");
273 if (check(el, el1)) {
274 printk(KERN_ERR "list_sort_test: error: element check "
281 if (count != TEST_LIST_LEN) {
282 printk(KERN_ERR "list_sort_test: error: bad list length %d",
290 list_for_each_safe(cur, tmp, &head) {
292 kfree(container_of(cur, struct debug_el, list));
296 module_init(list_sort_test);
297 #endif /* CONFIG_TEST_LIST_SORT */