list_sort.c 7.0 KB

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