xref: /linux-6.15/lib/sort.c (revision e2a33a2a)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * A fast, small, non-recursive O(n log n) sort for the Linux kernel
41da177e4SLinus Torvalds  *
522a241ccSGeorge Spelvin  * This performs n*log2(n) + 0.37*n + o(n) comparisons on average,
622a241ccSGeorge Spelvin  * and 1.5*n*log2(n) + O(n) in the (very contrived) worst case.
722a241ccSGeorge Spelvin  *
8f49ac957SKuan-Wei Chiu  * Quicksort manages n*log2(n) - 1.26*n for random inputs (1.63*n
922a241ccSGeorge Spelvin  * better) at the expense of stack usage and much larger code to avoid
1022a241ccSGeorge Spelvin  * quicksort's O(n^2) worst case.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
1342cf8096SRasmus Villemoes #include <linux/types.h>
1442cf8096SRasmus Villemoes #include <linux/export.h>
15ecec4cb7SAdrian Bunk #include <linux/sort.h>
161da177e4SLinus Torvalds 
1737d0ec34SGeorge Spelvin /**
1837d0ec34SGeorge Spelvin  * is_aligned - is this pointer & size okay for word-wide copying?
1937d0ec34SGeorge Spelvin  * @base: pointer to data
2037d0ec34SGeorge Spelvin  * @size: size of each element
2122a241ccSGeorge Spelvin  * @align: required alignment (typically 4 or 8)
2237d0ec34SGeorge Spelvin  *
2337d0ec34SGeorge Spelvin  * Returns true if elements can be copied using word loads and stores.
2437d0ec34SGeorge Spelvin  * The size must be a multiple of the alignment, and the base address must
2537d0ec34SGeorge Spelvin  * be if we do not have CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS.
2637d0ec34SGeorge Spelvin  *
2737d0ec34SGeorge Spelvin  * For some reason, gcc doesn't know to optimize "if (a & mask || b & mask)"
2837d0ec34SGeorge Spelvin  * to "if ((a | b) & mask)", so we do that by hand.
2937d0ec34SGeorge Spelvin  */
3037d0ec34SGeorge Spelvin __attribute_const__ __always_inline
is_aligned(const void * base,size_t size,unsigned char align)3137d0ec34SGeorge Spelvin static bool is_aligned(const void *base, size_t size, unsigned char align)
32ca96ab85SDaniel Wagner {
3337d0ec34SGeorge Spelvin 	unsigned char lsbits = (unsigned char)size;
3437d0ec34SGeorge Spelvin 
3537d0ec34SGeorge Spelvin 	(void)base;
3637d0ec34SGeorge Spelvin #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
3737d0ec34SGeorge Spelvin 	lsbits |= (unsigned char)(uintptr_t)base;
3837d0ec34SGeorge Spelvin #endif
3937d0ec34SGeorge Spelvin 	return (lsbits & (align - 1)) == 0;
40ca96ab85SDaniel Wagner }
41ca96ab85SDaniel Wagner 
4237d0ec34SGeorge Spelvin /**
4337d0ec34SGeorge Spelvin  * swap_words_32 - swap two elements in 32-bit chunks
44aa52619cSRandy Dunlap  * @a: pointer to the first element to swap
45aa52619cSRandy Dunlap  * @b: pointer to the second element to swap
46aa52619cSRandy Dunlap  * @n: element size (must be a multiple of 4)
4737d0ec34SGeorge Spelvin  *
4837d0ec34SGeorge Spelvin  * Exchange the two objects in memory.  This exploits base+index addressing,
4937d0ec34SGeorge Spelvin  * which basically all CPUs have, to minimize loop overhead computations.
5037d0ec34SGeorge Spelvin  *
5137d0ec34SGeorge Spelvin  * For some reason, on x86 gcc 7.3.0 adds a redundant test of n at the
529dbbc3b9SZhen Lei  * bottom of the loop, even though the zero flag is still valid from the
5337d0ec34SGeorge Spelvin  * subtract (since the intervening mov instructions don't alter the flags).
5437d0ec34SGeorge Spelvin  * Gcc 8.1.0 doesn't have that problem.
5537d0ec34SGeorge Spelvin  */
swap_words_32(void * a,void * b,size_t n)568fb583c4SGeorge Spelvin static void swap_words_32(void *a, void *b, size_t n)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	do {
5937d0ec34SGeorge Spelvin 		u32 t = *(u32 *)(a + (n -= 4));
6037d0ec34SGeorge Spelvin 		*(u32 *)(a + n) = *(u32 *)(b + n);
6137d0ec34SGeorge Spelvin 		*(u32 *)(b + n) = t;
6237d0ec34SGeorge Spelvin 	} while (n);
6337d0ec34SGeorge Spelvin }
6437d0ec34SGeorge Spelvin 
6537d0ec34SGeorge Spelvin /**
6637d0ec34SGeorge Spelvin  * swap_words_64 - swap two elements in 64-bit chunks
67aa52619cSRandy Dunlap  * @a: pointer to the first element to swap
68aa52619cSRandy Dunlap  * @b: pointer to the second element to swap
69aa52619cSRandy Dunlap  * @n: element size (must be a multiple of 8)
7037d0ec34SGeorge Spelvin  *
7137d0ec34SGeorge Spelvin  * Exchange the two objects in memory.  This exploits base+index
7237d0ec34SGeorge Spelvin  * addressing, which basically all CPUs have, to minimize loop overhead
7337d0ec34SGeorge Spelvin  * computations.
7437d0ec34SGeorge Spelvin  *
7537d0ec34SGeorge Spelvin  * We'd like to use 64-bit loads if possible.  If they're not, emulating
7637d0ec34SGeorge Spelvin  * one requires base+index+4 addressing which x86 has but most other
7737d0ec34SGeorge Spelvin  * processors do not.  If CONFIG_64BIT, we definitely have 64-bit loads,
7837d0ec34SGeorge Spelvin  * but it's possible to have 64-bit loads without 64-bit pointers (e.g.
7937d0ec34SGeorge Spelvin  * x32 ABI).  Are there any cases the kernel needs to worry about?
8037d0ec34SGeorge Spelvin  */
swap_words_64(void * a,void * b,size_t n)818fb583c4SGeorge Spelvin static void swap_words_64(void *a, void *b, size_t n)
8237d0ec34SGeorge Spelvin {
8337d0ec34SGeorge Spelvin 	do {
8437d0ec34SGeorge Spelvin #ifdef CONFIG_64BIT
8537d0ec34SGeorge Spelvin 		u64 t = *(u64 *)(a + (n -= 8));
8637d0ec34SGeorge Spelvin 		*(u64 *)(a + n) = *(u64 *)(b + n);
8737d0ec34SGeorge Spelvin 		*(u64 *)(b + n) = t;
8837d0ec34SGeorge Spelvin #else
8937d0ec34SGeorge Spelvin 		/* Use two 32-bit transfers to avoid base+index+4 addressing */
9037d0ec34SGeorge Spelvin 		u32 t = *(u32 *)(a + (n -= 4));
9137d0ec34SGeorge Spelvin 		*(u32 *)(a + n) = *(u32 *)(b + n);
9237d0ec34SGeorge Spelvin 		*(u32 *)(b + n) = t;
9337d0ec34SGeorge Spelvin 
9437d0ec34SGeorge Spelvin 		t = *(u32 *)(a + (n -= 4));
9537d0ec34SGeorge Spelvin 		*(u32 *)(a + n) = *(u32 *)(b + n);
9637d0ec34SGeorge Spelvin 		*(u32 *)(b + n) = t;
9737d0ec34SGeorge Spelvin #endif
9837d0ec34SGeorge Spelvin 	} while (n);
9937d0ec34SGeorge Spelvin }
10037d0ec34SGeorge Spelvin 
10137d0ec34SGeorge Spelvin /**
10237d0ec34SGeorge Spelvin  * swap_bytes - swap two elements a byte at a time
103aa52619cSRandy Dunlap  * @a: pointer to the first element to swap
104aa52619cSRandy Dunlap  * @b: pointer to the second element to swap
105aa52619cSRandy Dunlap  * @n: element size
10637d0ec34SGeorge Spelvin  *
10737d0ec34SGeorge Spelvin  * This is the fallback if alignment doesn't allow using larger chunks.
10837d0ec34SGeorge Spelvin  */
swap_bytes(void * a,void * b,size_t n)1098fb583c4SGeorge Spelvin static void swap_bytes(void *a, void *b, size_t n)
11037d0ec34SGeorge Spelvin {
11137d0ec34SGeorge Spelvin 	do {
11237d0ec34SGeorge Spelvin 		char t = ((char *)a)[--n];
11337d0ec34SGeorge Spelvin 		((char *)a)[n] = ((char *)b)[n];
11437d0ec34SGeorge Spelvin 		((char *)b)[n] = t;
11537d0ec34SGeorge Spelvin 	} while (n);
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
1188fb583c4SGeorge Spelvin /*
1198fb583c4SGeorge Spelvin  * The values are arbitrary as long as they can't be confused with
1208fb583c4SGeorge Spelvin  * a pointer, but small integers make for the smallest compare
1218fb583c4SGeorge Spelvin  * instructions.
1228fb583c4SGeorge Spelvin  */
123a0019cd7SJiri Olsa #define SWAP_WORDS_64 (swap_r_func_t)0
124a0019cd7SJiri Olsa #define SWAP_WORDS_32 (swap_r_func_t)1
125a0019cd7SJiri Olsa #define SWAP_BYTES    (swap_r_func_t)2
126a0019cd7SJiri Olsa #define SWAP_WRAPPER  (swap_r_func_t)3
127a0019cd7SJiri Olsa 
128a0019cd7SJiri Olsa struct wrapper {
129a0019cd7SJiri Olsa 	cmp_func_t cmp;
130a0019cd7SJiri Olsa 	swap_func_t swap;
131a0019cd7SJiri Olsa };
1328fb583c4SGeorge Spelvin 
1338fb583c4SGeorge Spelvin /*
1348fb583c4SGeorge Spelvin  * The function pointer is last to make tail calls most efficient if the
1358fb583c4SGeorge Spelvin  * compiler decides not to inline this function.
1368fb583c4SGeorge Spelvin  */
do_swap(void * a,void * b,size_t size,swap_r_func_t swap_func,const void * priv)137a0019cd7SJiri Olsa static void do_swap(void *a, void *b, size_t size, swap_r_func_t swap_func, const void *priv)
1388fb583c4SGeorge Spelvin {
139a0019cd7SJiri Olsa 	if (swap_func == SWAP_WRAPPER) {
140a0019cd7SJiri Olsa 		((const struct wrapper *)priv)->swap(a, b, (int)size);
141a0019cd7SJiri Olsa 		return;
142a0019cd7SJiri Olsa 	}
143a0019cd7SJiri Olsa 
1448fb583c4SGeorge Spelvin 	if (swap_func == SWAP_WORDS_64)
1458fb583c4SGeorge Spelvin 		swap_words_64(a, b, size);
1468fb583c4SGeorge Spelvin 	else if (swap_func == SWAP_WORDS_32)
1478fb583c4SGeorge Spelvin 		swap_words_32(a, b, size);
1488fb583c4SGeorge Spelvin 	else if (swap_func == SWAP_BYTES)
1498fb583c4SGeorge Spelvin 		swap_bytes(a, b, size);
1508fb583c4SGeorge Spelvin 	else
151a0019cd7SJiri Olsa 		swap_func(a, b, (int)size, priv);
1528fb583c4SGeorge Spelvin }
1538fb583c4SGeorge Spelvin 
1544333fb96SRasmus Villemoes #define _CMP_WRAPPER ((cmp_r_func_t)0L)
1554333fb96SRasmus Villemoes 
do_cmp(const void * a,const void * b,cmp_r_func_t cmp,const void * priv)15652ae533bSAndy Shevchenko static int do_cmp(const void *a, const void *b, cmp_r_func_t cmp, const void *priv)
1574333fb96SRasmus Villemoes {
1584333fb96SRasmus Villemoes 	if (cmp == _CMP_WRAPPER)
159a0019cd7SJiri Olsa 		return ((const struct wrapper *)priv)->cmp(a, b);
1604333fb96SRasmus Villemoes 	return cmp(a, b, priv);
1614333fb96SRasmus Villemoes }
1624333fb96SRasmus Villemoes 
16372fd4a35SRobert P. J. Day /**
16422a241ccSGeorge Spelvin  * parent - given the offset of the child, find the offset of the parent.
16522a241ccSGeorge Spelvin  * @i: the offset of the heap element whose parent is sought.  Non-zero.
16622a241ccSGeorge Spelvin  * @lsbit: a precomputed 1-bit mask, equal to "size & -size"
16722a241ccSGeorge Spelvin  * @size: size of each element
16822a241ccSGeorge Spelvin  *
16922a241ccSGeorge Spelvin  * In terms of array indexes, the parent of element j = @i/@size is simply
17022a241ccSGeorge Spelvin  * (j-1)/2.  But when working in byte offsets, we can't use implicit
17122a241ccSGeorge Spelvin  * truncation of integer divides.
17222a241ccSGeorge Spelvin  *
17322a241ccSGeorge Spelvin  * Fortunately, we only need one bit of the quotient, not the full divide.
17422a241ccSGeorge Spelvin  * @size has a least significant bit.  That bit will be clear if @i is
17522a241ccSGeorge Spelvin  * an even multiple of @size, and set if it's an odd multiple.
17622a241ccSGeorge Spelvin  *
17722a241ccSGeorge Spelvin  * Logically, we're doing "if (i & lsbit) i -= size;", but since the
17822a241ccSGeorge Spelvin  * branch is unpredictable, it's done with a bit of clever branch-free
17922a241ccSGeorge Spelvin  * code instead.
18022a241ccSGeorge Spelvin  */
18122a241ccSGeorge Spelvin __attribute_const__ __always_inline
parent(size_t i,unsigned int lsbit,size_t size)18222a241ccSGeorge Spelvin static size_t parent(size_t i, unsigned int lsbit, size_t size)
18322a241ccSGeorge Spelvin {
18422a241ccSGeorge Spelvin 	i -= size;
18522a241ccSGeorge Spelvin 	i -= size & -(i & lsbit);
18622a241ccSGeorge Spelvin 	return i / 2;
18722a241ccSGeorge Spelvin }
18822a241ccSGeorge Spelvin 
189*e2a33a2aSKent Overstreet #include <linux/sched.h>
190*e2a33a2aSKent Overstreet 
__sort_r(void * base,size_t num,size_t size,cmp_r_func_t cmp_func,swap_r_func_t swap_func,const void * priv,bool may_schedule)191*e2a33a2aSKent Overstreet static void __sort_r(void *base, size_t num, size_t size,
19252ae533bSAndy Shevchenko 		     cmp_r_func_t cmp_func,
193a0019cd7SJiri Olsa 		     swap_r_func_t swap_func,
194*e2a33a2aSKent Overstreet 		     const void *priv,
195*e2a33a2aSKent Overstreet 		     bool may_schedule)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	/* pre-scale counters for performance */
19822a241ccSGeorge Spelvin 	size_t n = num * size, a = (num/2) * size;
19922a241ccSGeorge Spelvin 	const unsigned int lsbit = size & -size;  /* Used to find parent */
2000e02ca29SKuan-Wei Chiu 	size_t shift = 0;
20122a241ccSGeorge Spelvin 
20222a241ccSGeorge Spelvin 	if (!a)		/* num < 2 || size == 0 */
20322a241ccSGeorge Spelvin 		return;
2041da177e4SLinus Torvalds 
205a0019cd7SJiri Olsa 	/* called from 'sort' without swap function, let's pick the default */
206a0019cd7SJiri Olsa 	if (swap_func == SWAP_WRAPPER && !((struct wrapper *)priv)->swap)
207a0019cd7SJiri Olsa 		swap_func = NULL;
208a0019cd7SJiri Olsa 
209ca96ab85SDaniel Wagner 	if (!swap_func) {
21037d0ec34SGeorge Spelvin 		if (is_aligned(base, size, 8))
2118fb583c4SGeorge Spelvin 			swap_func = SWAP_WORDS_64;
21237d0ec34SGeorge Spelvin 		else if (is_aligned(base, size, 4))
2138fb583c4SGeorge Spelvin 			swap_func = SWAP_WORDS_32;
214ca96ab85SDaniel Wagner 		else
2158fb583c4SGeorge Spelvin 			swap_func = SWAP_BYTES;
216ca96ab85SDaniel Wagner 	}
2171da177e4SLinus Torvalds 
21822a241ccSGeorge Spelvin 	/*
21922a241ccSGeorge Spelvin 	 * Loop invariants:
22022a241ccSGeorge Spelvin 	 * 1. elements [a,n) satisfy the heap property (compare greater than
22122a241ccSGeorge Spelvin 	 *    all of their children),
22222a241ccSGeorge Spelvin 	 * 2. elements [n,num*size) are sorted, and
22322a241ccSGeorge Spelvin 	 * 3. a <= b <= c <= d <= n (whenever they are valid).
22422a241ccSGeorge Spelvin 	 */
22522a241ccSGeorge Spelvin 	for (;;) {
22622a241ccSGeorge Spelvin 		size_t b, c, d;
2271da177e4SLinus Torvalds 
2280e02ca29SKuan-Wei Chiu 		if (a)			/* Building heap: sift down a */
2290e02ca29SKuan-Wei Chiu 			a -= size << shift;
2300e02ca29SKuan-Wei Chiu 		else if (n > 3 * size) { /* Sorting: Extract two largest elements */
2310e02ca29SKuan-Wei Chiu 			n -= size;
232a0019cd7SJiri Olsa 			do_swap(base, base + n, size, swap_func, priv);
2330e02ca29SKuan-Wei Chiu 			shift = do_cmp(base + size, base + 2 * size, cmp_func, priv) <= 0;
2340e02ca29SKuan-Wei Chiu 			a = size << shift;
2350e02ca29SKuan-Wei Chiu 			n -= size;
2360e02ca29SKuan-Wei Chiu 			do_swap(base + a, base + n, size, swap_func, priv);
2370e02ca29SKuan-Wei Chiu 		} else {		/* Sort complete */
2381da177e4SLinus Torvalds 			break;
2390e02ca29SKuan-Wei Chiu 		}
2401da177e4SLinus Torvalds 
24122a241ccSGeorge Spelvin 		/*
24222a241ccSGeorge Spelvin 		 * Sift element at "a" down into heap.  This is the
24322a241ccSGeorge Spelvin 		 * "bottom-up" variant, which significantly reduces
24422a241ccSGeorge Spelvin 		 * calls to cmp_func(): we find the sift-down path all
24522a241ccSGeorge Spelvin 		 * the way to the leaves (one compare per level), then
24622a241ccSGeorge Spelvin 		 * backtrack to find where to insert the target element.
24722a241ccSGeorge Spelvin 		 *
24822a241ccSGeorge Spelvin 		 * Because elements tend to sift down close to the leaves,
24922a241ccSGeorge Spelvin 		 * this uses fewer compares than doing two per level
25022a241ccSGeorge Spelvin 		 * on the way down.  (A bit more than half as many on
25122a241ccSGeorge Spelvin 		 * average, 3/4 worst-case.)
25222a241ccSGeorge Spelvin 		 */
25322a241ccSGeorge Spelvin 		for (b = a; c = 2*b + size, (d = c + size) < n;)
254db946a42SKuan-Wei Chiu 			b = do_cmp(base + c, base + d, cmp_func, priv) > 0 ? c : d;
25522a241ccSGeorge Spelvin 		if (d == n)	/* Special case last leaf with no sibling */
25622a241ccSGeorge Spelvin 			b = c;
25722a241ccSGeorge Spelvin 
25822a241ccSGeorge Spelvin 		/* Now backtrack from "b" to the correct location for "a" */
2594333fb96SRasmus Villemoes 		while (b != a && do_cmp(base + a, base + b, cmp_func, priv) >= 0)
26022a241ccSGeorge Spelvin 			b = parent(b, lsbit, size);
26122a241ccSGeorge Spelvin 		c = b;			/* Where "a" belongs */
26222a241ccSGeorge Spelvin 		while (b != a) {	/* Shift it into place */
26322a241ccSGeorge Spelvin 			b = parent(b, lsbit, size);
264a0019cd7SJiri Olsa 			do_swap(base + b, base + c, size, swap_func, priv);
26522a241ccSGeorge Spelvin 		}
266*e2a33a2aSKent Overstreet 
267*e2a33a2aSKent Overstreet 		if (may_schedule)
268*e2a33a2aSKent Overstreet 			cond_resched();
26922a241ccSGeorge Spelvin 	}
27041ed7804SKuan-Wei Chiu 
27141ed7804SKuan-Wei Chiu 	n -= size;
27241ed7804SKuan-Wei Chiu 	do_swap(base, base + n, size, swap_func, priv);
27341ed7804SKuan-Wei Chiu 	if (n == size * 2 && do_cmp(base, base + size, cmp_func, priv) > 0)
27441ed7804SKuan-Wei Chiu 		do_swap(base, base + size, size, swap_func, priv);
27522a241ccSGeorge Spelvin }
276*e2a33a2aSKent Overstreet 
277*e2a33a2aSKent Overstreet /**
278*e2a33a2aSKent Overstreet  * sort_r - sort an array of elements
279*e2a33a2aSKent Overstreet  * @base: pointer to data to sort
280*e2a33a2aSKent Overstreet  * @num: number of elements
281*e2a33a2aSKent Overstreet  * @size: size of each element
282*e2a33a2aSKent Overstreet  * @cmp_func: pointer to comparison function
283*e2a33a2aSKent Overstreet  * @swap_func: pointer to swap function or NULL
284*e2a33a2aSKent Overstreet  * @priv: third argument passed to comparison function
285*e2a33a2aSKent Overstreet  *
286*e2a33a2aSKent Overstreet  * This function does a heapsort on the given array.  You may provide
287*e2a33a2aSKent Overstreet  * a swap_func function if you need to do something more than a memory
288*e2a33a2aSKent Overstreet  * copy (e.g. fix up pointers or auxiliary data), but the built-in swap
289*e2a33a2aSKent Overstreet  * avoids a slow retpoline and so is significantly faster.
290*e2a33a2aSKent Overstreet  *
291*e2a33a2aSKent Overstreet  * The comparison function must adhere to specific mathematical
292*e2a33a2aSKent Overstreet  * properties to ensure correct and stable sorting:
293*e2a33a2aSKent Overstreet  * - Antisymmetry: cmp_func(a, b) must return the opposite sign of
294*e2a33a2aSKent Overstreet  * cmp_func(b, a).
295*e2a33a2aSKent Overstreet  * - Transitivity: if cmp_func(a, b) <= 0 and cmp_func(b, c) <= 0, then
296*e2a33a2aSKent Overstreet  * cmp_func(a, c) <= 0.
297*e2a33a2aSKent Overstreet  *
298*e2a33a2aSKent Overstreet  * Sorting time is O(n log n) both on average and worst-case. While
299*e2a33a2aSKent Overstreet  * quicksort is slightly faster on average, it suffers from exploitable
300*e2a33a2aSKent Overstreet  * O(n*n) worst-case behavior and extra memory requirements that make
301*e2a33a2aSKent Overstreet  * it less suitable for kernel use.
302*e2a33a2aSKent Overstreet  */
sort_r(void * base,size_t num,size_t size,cmp_r_func_t cmp_func,swap_r_func_t swap_func,const void * priv)303*e2a33a2aSKent Overstreet void sort_r(void *base, size_t num, size_t size,
304*e2a33a2aSKent Overstreet 	    cmp_r_func_t cmp_func,
305*e2a33a2aSKent Overstreet 	    swap_r_func_t swap_func,
306*e2a33a2aSKent Overstreet 	    const void *priv)
307*e2a33a2aSKent Overstreet {
308*e2a33a2aSKent Overstreet 	__sort_r(base, num, size, cmp_func, swap_func, priv, false);
309*e2a33a2aSKent Overstreet }
3104333fb96SRasmus Villemoes EXPORT_SYMBOL(sort_r);
3114333fb96SRasmus Villemoes 
312*e2a33a2aSKent Overstreet /**
313*e2a33a2aSKent Overstreet  * sort_r_nonatomic - sort an array of elements, with cond_resched
314*e2a33a2aSKent Overstreet  * @base: pointer to data to sort
315*e2a33a2aSKent Overstreet  * @num: number of elements
316*e2a33a2aSKent Overstreet  * @size: size of each element
317*e2a33a2aSKent Overstreet  * @cmp_func: pointer to comparison function
318*e2a33a2aSKent Overstreet  * @swap_func: pointer to swap function or NULL
319*e2a33a2aSKent Overstreet  * @priv: third argument passed to comparison function
320*e2a33a2aSKent Overstreet  *
321*e2a33a2aSKent Overstreet  * Same as sort_r, but preferred for larger arrays as it does a periodic
322*e2a33a2aSKent Overstreet  * cond_resched().
323*e2a33a2aSKent Overstreet  */
sort_r_nonatomic(void * base,size_t num,size_t size,cmp_r_func_t cmp_func,swap_r_func_t swap_func,const void * priv)324*e2a33a2aSKent Overstreet void sort_r_nonatomic(void *base, size_t num, size_t size,
325*e2a33a2aSKent Overstreet 		      cmp_r_func_t cmp_func,
326*e2a33a2aSKent Overstreet 		      swap_r_func_t swap_func,
327*e2a33a2aSKent Overstreet 		      const void *priv)
328*e2a33a2aSKent Overstreet {
329*e2a33a2aSKent Overstreet 	__sort_r(base, num, size, cmp_func, swap_func, priv, true);
330*e2a33a2aSKent Overstreet }
331*e2a33a2aSKent Overstreet EXPORT_SYMBOL(sort_r_nonatomic);
332*e2a33a2aSKent Overstreet 
sort(void * base,size_t num,size_t size,cmp_func_t cmp_func,swap_func_t swap_func)3334333fb96SRasmus Villemoes void sort(void *base, size_t num, size_t size,
33452ae533bSAndy Shevchenko 	  cmp_func_t cmp_func,
33552ae533bSAndy Shevchenko 	  swap_func_t swap_func)
3364333fb96SRasmus Villemoes {
337a0019cd7SJiri Olsa 	struct wrapper w = {
338a0019cd7SJiri Olsa 		.cmp  = cmp_func,
339a0019cd7SJiri Olsa 		.swap = swap_func,
340a0019cd7SJiri Olsa 	};
341a0019cd7SJiri Olsa 
342*e2a33a2aSKent Overstreet 	return __sort_r(base, num, size, _CMP_WRAPPER, SWAP_WRAPPER, &w, false);
3434333fb96SRasmus Villemoes }
3441da177e4SLinus Torvalds EXPORT_SYMBOL(sort);
345*e2a33a2aSKent Overstreet 
sort_nonatomic(void * base,size_t num,size_t size,cmp_func_t cmp_func,swap_func_t swap_func)346*e2a33a2aSKent Overstreet void sort_nonatomic(void *base, size_t num, size_t size,
347*e2a33a2aSKent Overstreet 		    cmp_func_t cmp_func,
348*e2a33a2aSKent Overstreet 		    swap_func_t swap_func)
349*e2a33a2aSKent Overstreet {
350*e2a33a2aSKent Overstreet 	struct wrapper w = {
351*e2a33a2aSKent Overstreet 		.cmp  = cmp_func,
352*e2a33a2aSKent Overstreet 		.swap = swap_func,
353*e2a33a2aSKent Overstreet 	};
354*e2a33a2aSKent Overstreet 
355*e2a33a2aSKent Overstreet 	return __sort_r(base, num, size, _CMP_WRAPPER, SWAP_WRAPPER, &w, true);
356*e2a33a2aSKent Overstreet }
357*e2a33a2aSKent Overstreet EXPORT_SYMBOL(sort_nonatomic);
358