list_sort.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  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);