Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b7b2562f authored by Rasmus Villemoes's avatar Rasmus Villemoes Committed by Linus Torvalds
Browse files

kernel/groups.c: use sort library function

setgroups is not exactly a hot path, so we might as well use the library
function instead of open-coding the sorting.  Saves ~150 bytes.

Link: http://lkml.kernel.org/r/1497301378-22739-1-git-send-email-linux@rasmusvillemoes.dk


Signed-off-by: default avatarRasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Matthew Wilcox <mawilcox@microsoft.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 9dcdcea1
Loading
Loading
Loading
Loading
+11 −24
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
#include <linux/export.h>
#include <linux/slab.h>
#include <linux/security.h>
#include <linux/sort.h>
#include <linux/syscalls.h>
#include <linux/user_namespace.h>
#include <linux/vmalloc.h>
@@ -76,32 +77,18 @@ static int groups_from_user(struct group_info *group_info,
	return 0;
}

/* a simple Shell sort */
static void groups_sort(struct group_info *group_info)
static int gid_cmp(const void *_a, const void *_b)
{
	int base, max, stride;
	int gidsetsize = group_info->ngroups;

	for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
		; /* nothing */
	stride /= 3;
	kgid_t a = *(kgid_t *)_a;
	kgid_t b = *(kgid_t *)_b;

	while (stride) {
		max = gidsetsize - stride;
		for (base = 0; base < max; base++) {
			int left = base;
			int right = left + stride;
			kgid_t tmp = group_info->gid[right];

			while (left >= 0 && gid_gt(group_info->gid[left], tmp)) {
				group_info->gid[right] = group_info->gid[left];
				right = left;
				left -= stride;
			}
			group_info->gid[right] = tmp;
		}
		stride /= 3;
	return gid_gt(a, b) - gid_lt(a, b);
}

static void groups_sort(struct group_info *group_info)
{
	sort(group_info->gid, group_info->ngroups, sizeof(*group_info->gid),
	     gid_cmp, NULL);
}

/* a simple bsearch */