![]() |
|
|||
0001 // SPDX-License-Identifier: GPL-2.0 0002 #include <linux/kernel.h> 0003 #include <linux/bug.h> 0004 #include <linux/compiler.h> 0005 #include <linux/export.h> 0006 #include <linux/string.h> 0007 #include <linux/list_sort.h> 0008 #include <linux/list.h> 0009 0010 /* 0011 * Returns a list organized in an intermediate format suited 0012 * to chaining of merge() calls: null-terminated, no reserved or 0013 * sentinel head node, "prev" links not maintained. 0014 */ 0015 __attribute__((nonnull(2,3,4))) 0016 static struct list_head *merge(void *priv, list_cmp_func_t cmp, 0017 struct list_head *a, struct list_head *b) 0018 { 0019 struct list_head *head, **tail = &head; 0020 0021 for (;;) { 0022 /* if equal, take 'a' -- important for sort stability */ 0023 if (cmp(priv, a, b) <= 0) { 0024 *tail = a; 0025 tail = &a->next; 0026 a = a->next; 0027 if (!a) { 0028 *tail = b; 0029 break; 0030 } 0031 } else { 0032 *tail = b; 0033 tail = &b->next; 0034 b = b->next; 0035 if (!b) { 0036 *tail = a; 0037 break; 0038 } 0039 } 0040 } 0041 return head; 0042 } 0043 0044 /* 0045 * Combine final list merge with restoration of standard doubly-linked 0046 * list structure. This approach duplicates code from merge(), but 0047 * runs faster than the tidier alternatives of either a separate final 0048 * prev-link restoration pass, or maintaining the prev links 0049 * throughout. 0050 */ 0051 __attribute__((nonnull(2,3,4,5))) 0052 static void merge_final(void *priv, list_cmp_func_t cmp, struct list_head *head, 0053 struct list_head *a, struct list_head *b) 0054 { 0055 struct list_head *tail = head; 0056 u8 count = 0; 0057 0058 for (;;) { 0059 /* if equal, take 'a' -- important for sort stability */ 0060 if (cmp(priv, a, b) <= 0) { 0061 tail->next = a; 0062 a->prev = tail; 0063 tail = a; 0064 a = a->next; 0065 if (!a) 0066 break; 0067 } else { 0068 tail->next = b; 0069 b->prev = tail; 0070 tail = b; 0071 b = b->next; 0072 if (!b) { 0073 b = a; 0074 break; 0075 } 0076 } 0077 } 0078 0079 /* Finish linking remainder of list b on to tail */ 0080 tail->next = b; 0081 do { 0082 /* 0083 * If the merge is highly unbalanced (e.g. the input is 0084 * already sorted), this loop may run many iterations. 0085 * Continue callbacks to the client even though no 0086 * element comparison is needed, so the client's cmp() 0087 * routine can invoke cond_resched() periodically. 0088 */ 0089 if (unlikely(!++count)) 0090 cmp(priv, b, b); 0091 b->prev = tail; 0092 tail = b; 0093 b = b->next; 0094 } while (b); 0095 0096 /* And the final links to make a circular doubly-linked list */ 0097 tail->next = head; 0098 head->prev = tail; 0099 } 0100 0101 /** 0102 * list_sort - sort a list 0103 * @priv: private data, opaque to list_sort(), passed to @cmp 0104 * @head: the list to sort 0105 * @cmp: the elements comparison function 0106 * 0107 * The comparison function @cmp must return > 0 if @a should sort after 0108 * @b ("@a > @b" if you want an ascending sort), and <= 0 if @a should 0109 * sort before @b *or* their original order should be preserved. It is 0110 * always called with the element that came first in the input in @a, 0111 * and list_sort is a stable sort, so it is not necessary to distinguish 0112 * the @a < @b and @a == @b cases. 0113 * 0114 * This is compatible with two styles of @cmp function: 0115 * - The traditional style which returns <0 / =0 / >0, or 0116 * - Returning a boolean 0/1. 0117 * The latter offers a chance to save a few cycles in the comparison 0118 * (which is used by e.g. plug_ctx_cmp() in block/blk-mq.c). 0119 * 0120 * A good way to write a multi-word comparison is:: 0121 * 0122 * if (a->high != b->high) 0123 * return a->high > b->high; 0124 * if (a->middle != b->middle) 0125 * return a->middle > b->middle; 0126 * return a->low > b->low; 0127 * 0128 * 0129 * This mergesort is as eager as possible while always performing at least 0130 * 2:1 balanced merges. Given two pending sublists of size 2^k, they are 0131 * merged to a size-2^(k+1) list as soon as we have 2^k following elements. 0132 * 0133 * Thus, it will avoid cache thrashing as long as 3*2^k elements can 0134 * fit into the cache. Not quite as good as a fully-eager bottom-up 0135 * mergesort, but it does use 0.2*n fewer comparisons, so is faster in 0136 * the common case that everything fits into L1. 0137 * 0138 * 0139 * The merging is controlled by "count", the number of elements in the 0140 * pending lists. This is beautifully simple code, but rather subtle. 0141 * 0142 * Each time we increment "count", we set one bit (bit k) and clear 0143 * bits k-1 .. 0. Each time this happens (except the very first time 0144 * for each bit, when count increments to 2^k), we merge two lists of 0145 * size 2^k into one list of size 2^(k+1). 0146 * 0147 * This merge happens exactly when the count reaches an odd multiple of 0148 * 2^k, which is when we have 2^k elements pending in smaller lists, 0149 * so it's safe to merge away two lists of size 2^k. 0150 * 0151 * After this happens twice, we have created two lists of size 2^(k+1), 0152 * which will be merged into a list of size 2^(k+2) before we create 0153 * a third list of size 2^(k+1), so there are never more than two pending. 0154 * 0155 * The number of pending lists of size 2^k is determined by the 0156 * state of bit k of "count" plus two extra pieces of information: 0157 * 0158 * - The state of bit k-1 (when k == 0, consider bit -1 always set), and 0159 * - Whether the higher-order bits are zero or non-zero (i.e. 0160 * is count >= 2^(k+1)). 0161 * 0162 * There are six states we distinguish. "x" represents some arbitrary 0163 * bits, and "y" represents some arbitrary non-zero bits: 0164 * 0: 00x: 0 pending of size 2^k; x pending of sizes < 2^k 0165 * 1: 01x: 0 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k 0166 * 2: x10x: 0 pending of size 2^k; 2^k + x pending of sizes < 2^k 0167 * 3: x11x: 1 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k 0168 * 4: y00x: 1 pending of size 2^k; 2^k + x pending of sizes < 2^k 0169 * 5: y01x: 2 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k 0170 * (merge and loop back to state 2) 0171 * 0172 * We gain lists of size 2^k in the 2->3 and 4->5 transitions (because 0173 * bit k-1 is set while the more significant bits are non-zero) and 0174 * merge them away in the 5->2 transition. Note in particular that just 0175 * before the 5->2 transition, all lower-order bits are 11 (state 3), 0176 * so there is one list of each smaller size. 0177 * 0178 * When we reach the end of the input, we merge all the pending 0179 * lists, from smallest to largest. If you work through cases 2 to 0180 * 5 above, you can see that the number of elements we merge with a list 0181 * of size 2^k varies from 2^(k-1) (cases 3 and 5 when x == 0) to 0182 * 2^(k+1) - 1 (second merge of case 5 when x == 2^(k-1) - 1). 0183 */ 0184 __attribute__((nonnull(2,3))) 0185 void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp) 0186 { 0187 struct list_head *list = head->next, *pending = NULL; 0188 size_t count = 0; /* Count of pending */ 0189 0190 if (list == head->prev) /* Zero or one elements */ 0191 return; 0192 0193 /* Convert to a null-terminated singly-linked list. */ 0194 head->prev->next = NULL; 0195 0196 /* 0197 * Data structure invariants: 0198 * - All lists are singly linked and null-terminated; prev 0199 * pointers are not maintained. 0200 * - pending is a prev-linked "list of lists" of sorted 0201 * sublists awaiting further merging. 0202 * - Each of the sorted sublists is power-of-two in size. 0203 * - Sublists are sorted by size and age, smallest & newest at front. 0204 * - There are zero to two sublists of each size. 0205 * - A pair of pending sublists are merged as soon as the number 0206 * of following pending elements equals their size (i.e. 0207 * each time count reaches an odd multiple of that size). 0208 * That ensures each later final merge will be at worst 2:1. 0209 * - Each round consists of: 0210 * - Merging the two sublists selected by the highest bit 0211 * which flips when count is incremented, and 0212 * - Adding an element from the input as a size-1 sublist. 0213 */ 0214 do { 0215 size_t bits; 0216 struct list_head **tail = &pending; 0217 0218 /* Find the least-significant clear bit in count */ 0219 for (bits = count; bits & 1; bits >>= 1) 0220 tail = &(*tail)->prev; 0221 /* Do the indicated merge */ 0222 if (likely(bits)) { 0223 struct list_head *a = *tail, *b = a->prev; 0224 0225 a = merge(priv, cmp, b, a); 0226 /* Install the merged result in place of the inputs */ 0227 a->prev = b->prev; 0228 *tail = a; 0229 } 0230 0231 /* Move one element from input list to pending */ 0232 list->prev = pending; 0233 pending = list; 0234 list = list->next; 0235 pending->next = NULL; 0236 count++; 0237 } while (list); 0238 0239 /* End of input; merge together all the pending lists. */ 0240 list = pending; 0241 pending = pending->prev; 0242 for (;;) { 0243 struct list_head *next = pending->prev; 0244 0245 if (!next) 0246 break; 0247 list = merge(priv, cmp, pending, list); 0248 pending = next; 0249 } 0250 /* The final merge, rebuilding prev links */ 0251 merge_final(priv, cmp, head, pending, list); 0252 } 0253 EXPORT_SYMBOL(list_sort);
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |