list_sort.c 6.7 KB

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