On Sunday 27 February 2005 17:59, Andreas Gruenbacher wrote:> Add a quicksort from glibc as a kernel library function, and switch> xfs over to using it. The implementations are equivalent. The nfsacl> protocol also requires a sort function, so it makes more sense in> the common code.>> The plan is to replace this by Matt Machall's heap sort implementation> after it has been fixed.

My fault; I overlooked the existing fixes in Andrew's tree. So this patch replaces qsort. I have rolled all the fixes into one patch.

A new "[10/16] Infrastructure and server side of nfsacl" patch will follow.

This patch adds a generic array sorting library routine. This is meantto replace qsort, which has two problem areas for kernel use.

The first issue is quadratic worst-case performance. While quicksortworst-case datasets are rarely encountered in normal scenarios, it isin fact quite easy to construct worst cases for almost all quicksortalgorithms given source or access to an element comparison callback.This could allow attackers to cause sorts that would otherwise takeless than a millisecond to take seconds and sorts that should takeless than a second to take weeks or months. Fixing this problemrequires randomizing pivot selection with a secure random numbergenerator, which is rather expensive.

The second is that quicksort's recursion tracking requires eithernontrivial amounts of stack space or dynamic memory allocation and outof memory error handling.

By comparison, heapsort has both O(n log n) average and worst-caseperformance and practically no extra storage requirements. Thisversion runs within 70-90% of the average performance of optimizedquicksort so it should be an acceptable replacement wherever quicksortwould be used in the kernel.

Note that this function has an extra parameter for passing in anoptimized swapping function. This is worth 10% or more over thetypical byte-by-byte exchange functions.