#include <linux/bit_spinlock.h>
 #include <linux/slab.h>
 #include <linux/sched/mm.h>
-#include <linux/sort.h>
 #include <linux/log2.h>
 #include "ctree.h"
 #include "disk-io.h"
        u32 sample_size;
        /* Buckets store counters for each byte value */
        struct bucket_item *bucket;
+       /* Sorting buffer */
+       struct bucket_item *bucket_b;
        struct list_head list;
 };
 
 
        kvfree(workspace->sample);
        kfree(workspace->bucket);
+       kfree(workspace->bucket_b);
        kfree(workspace);
 }
 
        if (!ws->bucket)
                goto fail;
 
+       ws->bucket_b = kcalloc(BUCKET_SIZE, sizeof(*ws->bucket_b), GFP_KERNEL);
+       if (!ws->bucket_b)
+               goto fail;
+
        INIT_LIST_HEAD(&ws->list);
        return &ws->list;
 fail:
        return entropy_sum * 100 / entropy_max;
 }
 
-/* Compare buckets by size, ascending */
-static int bucket_comp_rev(const void *lv, const void *rv)
+#define RADIX_BASE             4U
+#define COUNTERS_SIZE          (1U << RADIX_BASE)
+
+static u8 get4bits(u64 num, int shift) {
+       u8 low4bits;
+
+       num >>= shift;
+       /* Reverse order */
+       low4bits = (COUNTERS_SIZE - 1) - (num % COUNTERS_SIZE);
+       return low4bits;
+}
+
+static void copy_cell(void *dst, int dest_i, void *src, int src_i)
 {
-       const struct bucket_item *l = (const struct bucket_item *)lv;
-       const struct bucket_item *r = (const struct bucket_item *)rv;
+       struct bucket_item *dstv = (struct bucket_item *)dst;
+       struct bucket_item *srcv = (struct bucket_item *)src;
+       dstv[dest_i] = srcv[src_i];
+}
+
+static u64 get_num(const void *a, int i)
+{
+       struct bucket_item *av = (struct bucket_item *)a;
+       return av[i].count;
+}
 
-       return r->count - l->count;
+/*
+ * Use 4 bits as radix base
+ * Use 16 u32 counters for calculating new possition in buf array
+ *
+ * @array     - array that will be sorted
+ * @array_buf - buffer array to store sorting results
+ *              must be equal in size to @array
+ * @num       - array size
+ * @get_num   - function to extract number from array
+ * @copy_cell - function to copy data from array to array_buf and vice versa
+ * @get4bits  - function to get 4 bits from number at specified offset
+ */
+static void radix_sort(void *array, void *array_buf, int num,
+                      u64 (*get_num)(const void *, int i),
+                      void (*copy_cell)(void *dest, int dest_i,
+                                        void* src, int src_i),
+                      u8 (*get4bits)(u64 num, int shift))
+{
+       u64 max_num;
+       u64 buf_num;
+       u32 counters[COUNTERS_SIZE];
+       u32 new_addr;
+       u32 addr;
+       int bitlen;
+       int shift;
+       int i;
+
+       /*
+        * Try avoid useless loop iterations for small numbers stored in big
+        * counters.  Example: 48 33 4 ... in 64bit array
+        */
+       max_num = get_num(array, 0);
+       for (i = 1; i < num; i++) {
+               buf_num = get_num(array, i);
+               if (buf_num > max_num)
+                       max_num = buf_num;
+       }
+
+       buf_num = ilog2(max_num);
+       bitlen = ALIGN(buf_num, RADIX_BASE * 2);
+
+       shift = 0;
+       while (shift < bitlen) {
+               memset(counters, 0, sizeof(counters));
+
+               for (i = 0; i < num; i++) {
+                       buf_num = get_num(array, i);
+                       addr = get4bits(buf_num, shift);
+                       counters[addr]++;
+               }
+
+               for (i = 1; i < COUNTERS_SIZE; i++)
+                       counters[i] += counters[i - 1];
+
+               for (i = num - 1; i >= 0; i--) {
+                       buf_num = get_num(array, i);
+                       addr = get4bits(buf_num, shift);
+                       counters[addr]--;
+                       new_addr = counters[addr];
+                       copy_cell(array_buf, new_addr, array, i);
+               }
+
+               shift += RADIX_BASE;
+
+               /*
+                * Normal radix expects to move data from a temporary array, to
+                * the main one.  But that requires some CPU time. Avoid that
+                * by doing another sort iteration to original array instead of
+                * memcpy()
+                */
+               memset(counters, 0, sizeof(counters));
+
+               for (i = 0; i < num; i ++) {
+                       buf_num = get_num(array_buf, i);
+                       addr = get4bits(buf_num, shift);
+                       counters[addr]++;
+               }
+
+               for (i = 1; i < COUNTERS_SIZE; i++)
+                       counters[i] += counters[i - 1];
+
+               for (i = num - 1; i >= 0; i--) {
+                       buf_num = get_num(array_buf, i);
+                       addr = get4bits(buf_num, shift);
+                       counters[addr]--;
+                       new_addr = counters[addr];
+                       copy_cell(array, new_addr, array_buf, i);
+               }
+
+               shift += RADIX_BASE;
+       }
 }
 
 /*
        struct bucket_item *bucket = ws->bucket;
 
        /* Sort in reverse order */
-       sort(bucket, BUCKET_SIZE, sizeof(*bucket), &bucket_comp_rev, NULL);
+       radix_sort(ws->bucket, ws->bucket_b, BUCKET_SIZE, get_num, copy_cell,
+                       get4bits);
 
        for (i = 0; i < BYTE_CORE_SET_LOW; i++)
                coreset_sum += bucket[i].count;