4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
8 #include <linux/compat.h>
10 #include <linux/printk.h>
12 #include <linux/list.h>
13 #include <linux/list_sort.h>
15 #define MAX_LIST_LENGTH_BITS 20
18 * Returns a list organized in an intermediate format suited
19 * to chaining of merge() calls: null-terminated, no reserved or
20 * sentinel head node, "prev" links not maintained.
22 static struct list_head *merge(void *priv,
23 int (*cmp)(void *priv, struct list_head *a,
25 struct list_head *a, struct list_head *b)
27 struct list_head head, *tail = &head;
30 /* if equal, take 'a' -- important for sort stability */
31 if ((*cmp)(priv, a, b) <= 0) {
45 * Combine final list merge with restoration of standard doubly-linked
46 * list structure. This approach duplicates code from merge(), but
47 * runs faster than the tidier alternatives of either a separate final
48 * prev-link restoration pass, or maintaining the prev links
51 static void merge_and_restore_back_links(void *priv,
52 int (*cmp)(void *priv, struct list_head *a,
54 struct list_head *head,
55 struct list_head *a, struct list_head *b)
57 struct list_head *tail = head;
60 /* if equal, take 'a' -- important for sort stability */
61 if ((*cmp)(priv, a, b) <= 0) {
76 * In worst cases this loop may run many iterations.
77 * Continue callbacks to the client even though no
78 * element comparison is needed, so the client's cmp()
79 * routine can invoke cond_resched() periodically.
81 (*cmp)(priv, tail->next, tail->next);
83 tail->next->prev = tail;
92 * list_sort - sort a list
93 * @priv: private data, opaque to list_sort(), passed to @cmp
94 * @head: the list to sort
95 * @cmp: the elements comparison function
97 * This function implements "merge sort", which has O(nlog(n))
100 * The comparison function @cmp must return a negative value if @a
101 * should sort before @b, and a positive value if @a should sort after
102 * @b. If @a and @b are equivalent, and their original relative
103 * ordering is to be preserved, @cmp must return 0.
105 void list_sort(void *priv, struct list_head *head,
106 int (*cmp)(void *priv, struct list_head *a,
107 struct list_head *b))
109 struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
110 -- last slot is a sentinel */
111 int lev; /* index into part[] */
113 struct list_head *list;
115 if (list_empty(head))
118 memset(part, 0, sizeof(part));
120 head->prev->next = NULL;
124 struct list_head *cur = list;
128 for (lev = 0; part[lev]; lev++) {
129 cur = merge(priv, cmp, part[lev], cur);
133 if (unlikely(lev >= ARRAY_SIZE(part)-1)) {
134 printk_once(KERN_DEBUG "list passed to"
135 " list_sort() too long for"
144 for (lev = 0; lev < max_lev; lev++)
146 list = merge(priv, cmp, part[lev], list);
148 merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
150 EXPORT_SYMBOL(list_sort);
152 #ifdef CONFIG_TEST_LIST_SORT
154 #include <linux/random.h>
157 * The pattern of set bits in the list length determines which cases
158 * are hit in list_sort().
160 #define TEST_LIST_LEN (512+128+2) /* not including head */
162 #define TEST_POISON1 0xDEADBEEF
163 #define TEST_POISON2 0xA324354C
166 unsigned int poison1;
167 struct list_head list;
168 unsigned int poison2;
173 /* Array, containing pointers to all elements in the test list */
174 static struct debug_el **elts __initdata;
176 static int __init check(struct debug_el *ela, struct debug_el *elb)
178 if (ela->serial >= TEST_LIST_LEN) {
179 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
183 if (elb->serial >= TEST_LIST_LEN) {
184 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
188 if (elts[ela->serial] != ela || elts[elb->serial] != elb) {
189 printk(KERN_ERR "list_sort_test: error: phantom element\n");
192 if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) {
193 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
194 ela->poison1, ela->poison2);
197 if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
198 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
199 elb->poison1, elb->poison2);
205 static int __init cmp(void *priv, struct list_head *a, struct list_head *b)
207 struct debug_el *ela, *elb;
209 ela = container_of(a, struct debug_el, list);
210 elb = container_of(b, struct debug_el, list);
213 return ela->value - elb->value;
216 static int __init list_sort_test(void)
218 int i, count = 1, err = -EINVAL;
220 struct list_head *cur, *tmp;
223 printk(KERN_DEBUG "list_sort_test: start testing list_sort()\n");
225 elts = kmalloc(sizeof(void *) * TEST_LIST_LEN, GFP_KERNEL);
227 printk(KERN_ERR "list_sort_test: error: cannot allocate "
232 for (i = 0; i < TEST_LIST_LEN; i++) {
233 el = kmalloc(sizeof(*el), GFP_KERNEL);
235 printk(KERN_ERR "list_sort_test: error: cannot "
236 "allocate memory\n");
239 /* force some equivalencies */
240 el->value = prandom_u32() % (TEST_LIST_LEN / 3);
242 el->poison1 = TEST_POISON1;
243 el->poison2 = TEST_POISON2;
245 list_add_tail(&el->list, &head);
248 list_sort(NULL, &head, cmp);
250 for (cur = head.next; cur->next != &head; cur = cur->next) {
251 struct debug_el *el1;
254 if (cur->next->prev != cur) {
255 printk(KERN_ERR "list_sort_test: error: list is "
260 cmp_result = cmp(NULL, cur, cur->next);
261 if (cmp_result > 0) {
262 printk(KERN_ERR "list_sort_test: error: list is not "
267 el = container_of(cur, struct debug_el, list);
268 el1 = container_of(cur->next, struct debug_el, list);
269 if (cmp_result == 0 && el->serial >= el1->serial) {
270 printk(KERN_ERR "list_sort_test: error: order of "
271 "equivalent elements not preserved\n");
275 if (check(el, el1)) {
276 printk(KERN_ERR "list_sort_test: error: element check "
283 if (count != TEST_LIST_LEN) {
284 printk(KERN_ERR "list_sort_test: error: bad list length %d",
292 list_for_each_safe(cur, tmp, &head) {
294 kfree(container_of(cur, struct debug_el, list));
298 module_init(list_sort_test);
299 #endif /* CONFIG_TEST_LIST_SORT */