xref: /linux-6.15/tools/lib/list_sort.c (revision ff1a39c3)
1*92ec3cc9SIan Rogers // SPDX-License-Identifier: GPL-2.0
2*92ec3cc9SIan Rogers #include <linux/compiler.h>
3*92ec3cc9SIan Rogers #include <linux/export.h>
4*92ec3cc9SIan Rogers #include <linux/list_sort.h>
5*92ec3cc9SIan Rogers #include <linux/list.h>
6*92ec3cc9SIan Rogers 
7*92ec3cc9SIan Rogers /*
8*92ec3cc9SIan Rogers  * Returns a list organized in an intermediate format suited
9*92ec3cc9SIan Rogers  * to chaining of merge() calls: null-terminated, no reserved or
10*92ec3cc9SIan Rogers  * sentinel head node, "prev" links not maintained.
11*92ec3cc9SIan Rogers  */
12*92ec3cc9SIan Rogers __attribute__((nonnull(2,3,4)))
merge(void * priv,list_cmp_func_t cmp,struct list_head * a,struct list_head * b)13*92ec3cc9SIan Rogers static struct list_head *merge(void *priv, list_cmp_func_t cmp,
14*92ec3cc9SIan Rogers 				struct list_head *a, struct list_head *b)
15*92ec3cc9SIan Rogers {
16*92ec3cc9SIan Rogers 	struct list_head *head, **tail = &head;
17*92ec3cc9SIan Rogers 
18*92ec3cc9SIan Rogers 	for (;;) {
19*92ec3cc9SIan Rogers 		/* if equal, take 'a' -- important for sort stability */
20*92ec3cc9SIan Rogers 		if (cmp(priv, a, b) <= 0) {
21*92ec3cc9SIan Rogers 			*tail = a;
22*92ec3cc9SIan Rogers 			tail = &a->next;
23*92ec3cc9SIan Rogers 			a = a->next;
24*92ec3cc9SIan Rogers 			if (!a) {
25*92ec3cc9SIan Rogers 				*tail = b;
26*92ec3cc9SIan Rogers 				break;
27*92ec3cc9SIan Rogers 			}
28*92ec3cc9SIan Rogers 		} else {
29*92ec3cc9SIan Rogers 			*tail = b;
30*92ec3cc9SIan Rogers 			tail = &b->next;
31*92ec3cc9SIan Rogers 			b = b->next;
32*92ec3cc9SIan Rogers 			if (!b) {
33*92ec3cc9SIan Rogers 				*tail = a;
34*92ec3cc9SIan Rogers 				break;
35*92ec3cc9SIan Rogers 			}
36*92ec3cc9SIan Rogers 		}
37*92ec3cc9SIan Rogers 	}
38*92ec3cc9SIan Rogers 	return head;
39*92ec3cc9SIan Rogers }
40*92ec3cc9SIan Rogers 
41*92ec3cc9SIan Rogers /*
42*92ec3cc9SIan Rogers  * Combine final list merge with restoration of standard doubly-linked
43*92ec3cc9SIan Rogers  * list structure.  This approach duplicates code from merge(), but
44*92ec3cc9SIan Rogers  * runs faster than the tidier alternatives of either a separate final
45*92ec3cc9SIan Rogers  * prev-link restoration pass, or maintaining the prev links
46*92ec3cc9SIan Rogers  * throughout.
47*92ec3cc9SIan Rogers  */
48*92ec3cc9SIan Rogers __attribute__((nonnull(2,3,4,5)))
merge_final(void * priv,list_cmp_func_t cmp,struct list_head * head,struct list_head * a,struct list_head * b)49*92ec3cc9SIan Rogers static void merge_final(void *priv, list_cmp_func_t cmp, struct list_head *head,
50*92ec3cc9SIan Rogers 			struct list_head *a, struct list_head *b)
51*92ec3cc9SIan Rogers {
52*92ec3cc9SIan Rogers 	struct list_head *tail = head;
53*92ec3cc9SIan Rogers 
54*92ec3cc9SIan Rogers 	for (;;) {
55*92ec3cc9SIan Rogers 		/* if equal, take 'a' -- important for sort stability */
56*92ec3cc9SIan Rogers 		if (cmp(priv, a, b) <= 0) {
57*92ec3cc9SIan Rogers 			tail->next = a;
58*92ec3cc9SIan Rogers 			a->prev = tail;
59*92ec3cc9SIan Rogers 			tail = a;
60*92ec3cc9SIan Rogers 			a = a->next;
61*92ec3cc9SIan Rogers 			if (!a)
62*92ec3cc9SIan Rogers 				break;
63*92ec3cc9SIan Rogers 		} else {
64*92ec3cc9SIan Rogers 			tail->next = b;
65*92ec3cc9SIan Rogers 			b->prev = tail;
66*92ec3cc9SIan Rogers 			tail = b;
67*92ec3cc9SIan Rogers 			b = b->next;
68*92ec3cc9SIan Rogers 			if (!b) {
69*92ec3cc9SIan Rogers 				b = a;
70*92ec3cc9SIan Rogers 				break;
71*92ec3cc9SIan Rogers 			}
72*92ec3cc9SIan Rogers 		}
73*92ec3cc9SIan Rogers 	}
74*92ec3cc9SIan Rogers 
75*92ec3cc9SIan Rogers 	/* Finish linking remainder of list b on to tail */
76*92ec3cc9SIan Rogers 	tail->next = b;
77*92ec3cc9SIan Rogers 	do {
78*92ec3cc9SIan Rogers 		b->prev = tail;
79*92ec3cc9SIan Rogers 		tail = b;
80*92ec3cc9SIan Rogers 		b = b->next;
81*92ec3cc9SIan Rogers 	} while (b);
82*92ec3cc9SIan Rogers 
83*92ec3cc9SIan Rogers 	/* And the final links to make a circular doubly-linked list */
84*92ec3cc9SIan Rogers 	tail->next = head;
85*92ec3cc9SIan Rogers 	head->prev = tail;
86*92ec3cc9SIan Rogers }
87*92ec3cc9SIan Rogers 
88*92ec3cc9SIan Rogers /**
89*92ec3cc9SIan Rogers  * list_sort - sort a list
90*92ec3cc9SIan Rogers  * @priv: private data, opaque to list_sort(), passed to @cmp
91*92ec3cc9SIan Rogers  * @head: the list to sort
92*92ec3cc9SIan Rogers  * @cmp: the elements comparison function
93*92ec3cc9SIan Rogers  *
94*92ec3cc9SIan Rogers  * The comparison function @cmp must return > 0 if @a should sort after
95*92ec3cc9SIan Rogers  * @b ("@a > @b" if you want an ascending sort), and <= 0 if @a should
96*92ec3cc9SIan Rogers  * sort before @b *or* their original order should be preserved.  It is
97*92ec3cc9SIan Rogers  * always called with the element that came first in the input in @a,
98*92ec3cc9SIan Rogers  * and list_sort is a stable sort, so it is not necessary to distinguish
99*92ec3cc9SIan Rogers  * the @a < @b and @a == @b cases.
100*92ec3cc9SIan Rogers  *
101*92ec3cc9SIan Rogers  * This is compatible with two styles of @cmp function:
102*92ec3cc9SIan Rogers  * - The traditional style which returns <0 / =0 / >0, or
103*92ec3cc9SIan Rogers  * - Returning a boolean 0/1.
104*92ec3cc9SIan Rogers  * The latter offers a chance to save a few cycles in the comparison
105*92ec3cc9SIan Rogers  * (which is used by e.g. plug_ctx_cmp() in block/blk-mq.c).
106*92ec3cc9SIan Rogers  *
107*92ec3cc9SIan Rogers  * A good way to write a multi-word comparison is::
108*92ec3cc9SIan Rogers  *
109*92ec3cc9SIan Rogers  *	if (a->high != b->high)
110*92ec3cc9SIan Rogers  *		return a->high > b->high;
111*92ec3cc9SIan Rogers  *	if (a->middle != b->middle)
112*92ec3cc9SIan Rogers  *		return a->middle > b->middle;
113*92ec3cc9SIan Rogers  *	return a->low > b->low;
114*92ec3cc9SIan Rogers  *
115*92ec3cc9SIan Rogers  *
116*92ec3cc9SIan Rogers  * This mergesort is as eager as possible while always performing at least
117*92ec3cc9SIan Rogers  * 2:1 balanced merges.  Given two pending sublists of size 2^k, they are
118*92ec3cc9SIan Rogers  * merged to a size-2^(k+1) list as soon as we have 2^k following elements.
119*92ec3cc9SIan Rogers  *
120*92ec3cc9SIan Rogers  * Thus, it will avoid cache thrashing as long as 3*2^k elements can
121*92ec3cc9SIan Rogers  * fit into the cache.  Not quite as good as a fully-eager bottom-up
122*92ec3cc9SIan Rogers  * mergesort, but it does use 0.2*n fewer comparisons, so is faster in
123*92ec3cc9SIan Rogers  * the common case that everything fits into L1.
124*92ec3cc9SIan Rogers  *
125*92ec3cc9SIan Rogers  *
126*92ec3cc9SIan Rogers  * The merging is controlled by "count", the number of elements in the
127*92ec3cc9SIan Rogers  * pending lists.  This is beautifully simple code, but rather subtle.
128*92ec3cc9SIan Rogers  *
129*92ec3cc9SIan Rogers  * Each time we increment "count", we set one bit (bit k) and clear
130*92ec3cc9SIan Rogers  * bits k-1 .. 0.  Each time this happens (except the very first time
131*92ec3cc9SIan Rogers  * for each bit, when count increments to 2^k), we merge two lists of
132*92ec3cc9SIan Rogers  * size 2^k into one list of size 2^(k+1).
133*92ec3cc9SIan Rogers  *
134*92ec3cc9SIan Rogers  * This merge happens exactly when the count reaches an odd multiple of
135*92ec3cc9SIan Rogers  * 2^k, which is when we have 2^k elements pending in smaller lists,
136*92ec3cc9SIan Rogers  * so it's safe to merge away two lists of size 2^k.
137*92ec3cc9SIan Rogers  *
138*92ec3cc9SIan Rogers  * After this happens twice, we have created two lists of size 2^(k+1),
139*92ec3cc9SIan Rogers  * which will be merged into a list of size 2^(k+2) before we create
140*92ec3cc9SIan Rogers  * a third list of size 2^(k+1), so there are never more than two pending.
141*92ec3cc9SIan Rogers  *
142*92ec3cc9SIan Rogers  * The number of pending lists of size 2^k is determined by the
143*92ec3cc9SIan Rogers  * state of bit k of "count" plus two extra pieces of information:
144*92ec3cc9SIan Rogers  *
145*92ec3cc9SIan Rogers  * - The state of bit k-1 (when k == 0, consider bit -1 always set), and
146*92ec3cc9SIan Rogers  * - Whether the higher-order bits are zero or non-zero (i.e.
147*92ec3cc9SIan Rogers  *   is count >= 2^(k+1)).
148*92ec3cc9SIan Rogers  *
149*92ec3cc9SIan Rogers  * There are six states we distinguish.  "x" represents some arbitrary
150*92ec3cc9SIan Rogers  * bits, and "y" represents some arbitrary non-zero bits:
151*92ec3cc9SIan Rogers  * 0:  00x: 0 pending of size 2^k;           x pending of sizes < 2^k
152*92ec3cc9SIan Rogers  * 1:  01x: 0 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k
153*92ec3cc9SIan Rogers  * 2: x10x: 0 pending of size 2^k; 2^k     + x pending of sizes < 2^k
154*92ec3cc9SIan Rogers  * 3: x11x: 1 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k
155*92ec3cc9SIan Rogers  * 4: y00x: 1 pending of size 2^k; 2^k     + x pending of sizes < 2^k
156*92ec3cc9SIan Rogers  * 5: y01x: 2 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k
157*92ec3cc9SIan Rogers  * (merge and loop back to state 2)
158*92ec3cc9SIan Rogers  *
159*92ec3cc9SIan Rogers  * We gain lists of size 2^k in the 2->3 and 4->5 transitions (because
160*92ec3cc9SIan Rogers  * bit k-1 is set while the more significant bits are non-zero) and
161*92ec3cc9SIan Rogers  * merge them away in the 5->2 transition.  Note in particular that just
162*92ec3cc9SIan Rogers  * before the 5->2 transition, all lower-order bits are 11 (state 3),
163*92ec3cc9SIan Rogers  * so there is one list of each smaller size.
164*92ec3cc9SIan Rogers  *
165*92ec3cc9SIan Rogers  * When we reach the end of the input, we merge all the pending
166*92ec3cc9SIan Rogers  * lists, from smallest to largest.  If you work through cases 2 to
167*92ec3cc9SIan Rogers  * 5 above, you can see that the number of elements we merge with a list
168*92ec3cc9SIan Rogers  * of size 2^k varies from 2^(k-1) (cases 3 and 5 when x == 0) to
169*92ec3cc9SIan Rogers  * 2^(k+1) - 1 (second merge of case 5 when x == 2^(k-1) - 1).
170*92ec3cc9SIan Rogers  */
171*92ec3cc9SIan Rogers __attribute__((nonnull(2,3)))
list_sort(void * priv,struct list_head * head,list_cmp_func_t cmp)172*92ec3cc9SIan Rogers void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp)
173*92ec3cc9SIan Rogers {
174*92ec3cc9SIan Rogers 	struct list_head *list = head->next, *pending = NULL;
175*92ec3cc9SIan Rogers 	size_t count = 0;	/* Count of pending */
176*92ec3cc9SIan Rogers 
177*92ec3cc9SIan Rogers 	if (list == head->prev)	/* Zero or one elements */
178*92ec3cc9SIan Rogers 		return;
179*92ec3cc9SIan Rogers 
180*92ec3cc9SIan Rogers 	/* Convert to a null-terminated singly-linked list. */
181*92ec3cc9SIan Rogers 	head->prev->next = NULL;
182*92ec3cc9SIan Rogers 
183*92ec3cc9SIan Rogers 	/*
184*92ec3cc9SIan Rogers 	 * Data structure invariants:
185*92ec3cc9SIan Rogers 	 * - All lists are singly linked and null-terminated; prev
186*92ec3cc9SIan Rogers 	 *   pointers are not maintained.
187*92ec3cc9SIan Rogers 	 * - pending is a prev-linked "list of lists" of sorted
188*92ec3cc9SIan Rogers 	 *   sublists awaiting further merging.
189*92ec3cc9SIan Rogers 	 * - Each of the sorted sublists is power-of-two in size.
190*92ec3cc9SIan Rogers 	 * - Sublists are sorted by size and age, smallest & newest at front.
191*92ec3cc9SIan Rogers 	 * - There are zero to two sublists of each size.
192*92ec3cc9SIan Rogers 	 * - A pair of pending sublists are merged as soon as the number
193*92ec3cc9SIan Rogers 	 *   of following pending elements equals their size (i.e.
194*92ec3cc9SIan Rogers 	 *   each time count reaches an odd multiple of that size).
195*92ec3cc9SIan Rogers 	 *   That ensures each later final merge will be at worst 2:1.
196*92ec3cc9SIan Rogers 	 * - Each round consists of:
197*92ec3cc9SIan Rogers 	 *   - Merging the two sublists selected by the highest bit
198*92ec3cc9SIan Rogers 	 *     which flips when count is incremented, and
199*92ec3cc9SIan Rogers 	 *   - Adding an element from the input as a size-1 sublist.
200*92ec3cc9SIan Rogers 	 */
201*92ec3cc9SIan Rogers 	do {
202*92ec3cc9SIan Rogers 		size_t bits;
203*92ec3cc9SIan Rogers 		struct list_head **tail = &pending;
204*92ec3cc9SIan Rogers 
205*92ec3cc9SIan Rogers 		/* Find the least-significant clear bit in count */
206*92ec3cc9SIan Rogers 		for (bits = count; bits & 1; bits >>= 1)
207*92ec3cc9SIan Rogers 			tail = &(*tail)->prev;
208*92ec3cc9SIan Rogers 		/* Do the indicated merge */
209*92ec3cc9SIan Rogers 		if (likely(bits)) {
210*92ec3cc9SIan Rogers 			struct list_head *a = *tail, *b = a->prev;
211*92ec3cc9SIan Rogers 
212*92ec3cc9SIan Rogers 			a = merge(priv, cmp, b, a);
213*92ec3cc9SIan Rogers 			/* Install the merged result in place of the inputs */
214*92ec3cc9SIan Rogers 			a->prev = b->prev;
215*92ec3cc9SIan Rogers 			*tail = a;
216*92ec3cc9SIan Rogers 		}
217*92ec3cc9SIan Rogers 
218*92ec3cc9SIan Rogers 		/* Move one element from input list to pending */
219*92ec3cc9SIan Rogers 		list->prev = pending;
220*92ec3cc9SIan Rogers 		pending = list;
221*92ec3cc9SIan Rogers 		list = list->next;
222*92ec3cc9SIan Rogers 		pending->next = NULL;
223*92ec3cc9SIan Rogers 		count++;
224*92ec3cc9SIan Rogers 	} while (list);
225*92ec3cc9SIan Rogers 
226*92ec3cc9SIan Rogers 	/* End of input; merge together all the pending lists. */
227*92ec3cc9SIan Rogers 	list = pending;
228*92ec3cc9SIan Rogers 	pending = pending->prev;
229*92ec3cc9SIan Rogers 	for (;;) {
230*92ec3cc9SIan Rogers 		struct list_head *next = pending->prev;
231*92ec3cc9SIan Rogers 
232*92ec3cc9SIan Rogers 		if (!next)
233*92ec3cc9SIan Rogers 			break;
234*92ec3cc9SIan Rogers 		list = merge(priv, cmp, pending, list);
235*92ec3cc9SIan Rogers 		pending = next;
236*92ec3cc9SIan Rogers 	}
237*92ec3cc9SIan Rogers 	/* The final merge, rebuilding prev links */
238*92ec3cc9SIan Rogers 	merge_final(priv, cmp, head, pending, list);
239*92ec3cc9SIan Rogers }
240*92ec3cc9SIan Rogers EXPORT_SYMBOL(list_sort);
241