list_sort.c 6.8 KB

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