Skip to contentSkip to conversation

kernel/groups.c: use bsearch library function

PATCH In review Thomas Meyer 2 participants 3 messages Oct 7, 2019 – Oct 9, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/groups.c: use bsearch library function

commit b7b2562f7252 ("kernel/groups.c: use sort library function") introduced the sort library function.
also use the bsearch library function instead of open-coding the binary search.

---
 kernel/groups.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
kernel/groups.c +4 -13
index daae2f2dc6d4f..69561a9cb4d39 100644
@@ -2,6 +2,7 @@
 /*
  * Supplementary group IDs
  */
+#include <linux/bsearch.h>
 #include <linux/cred.h>
 #include <linux/export.h>
 #include <linux/slab.h>
@@ -96,22 +97,12 @@ EXPORT_SYMBOL(groups_sort);
 /* a simple bsearch */
 int groups_search(const struct group_info *group_info, kgid_t grp)
 {
-	unsigned int left, right;
-
 	if (!group_info)
 		return 0;
 
-	left = 0;
-	right = group_info->ngroups;
-	while (left < right) {
-		unsigned int mid = (left+right)/2;
-		if (gid_gt(grp, group_info->gid[mid]))
-			left = mid + 1;
-		else if (gid_lt(grp, group_info->gid[mid]))
-			right = mid;
-		else
-			return 1;
-	}
+	if (bsearch(&grp, group_info->gid, group_info->ngroups,
+		    sizeof(*group_info->gid), gid_cmp))
+		return 1;
 	return 0;
 }
 
Signature · 1 line
2.21.0
#1 lore raw
Quoted 4 lines · Thomas Meyer

commit b7b2562f7252 ("kernel/groups.c: use sort library function") introduced the sort library function.
also use the bsearch library function instead of open-coding the binary search.

Yes, but please note the difference between sorting the group_info and searching it: The former is done quite rarely - the setgroups syscall is used roughly once per login-session.

But the searching of that structure is done more or less every time a user accesses a file not owned by that user (e.g., any time a normal user accesses anything in /usr) - at least if I'm reading acl_permission_check() right.

So using a callback-based interface, especially in a post-spectre world, may have a somewhat large performance impact.

Rasmus

#2 lore raw
Quoted 5 lines · Rasmus Villemoes
Quoted 4 lines · Thomas Meyer

commit b7b2562f7252 ("kernel/groups.c: use sort library function") introduced the sort library function.
also use the bsearch library function instead of open-coding the binary search.

Hi,

Quoted 11 lines

Yes, but please note the difference between sorting the group_info and searching it: The former is done quite rarely - the setgroups syscall is used roughly once per login-session.

But the searching of that structure is done more or less every time a user accesses a file not owned by that user (e.g., any time a normal user accesses anything in /usr) - at least if I'm reading acl_permission_check() right.

So using a callback-based interface, especially in a post-spectre world, may have a somewhat large performance impact.

okay, so the code is duplicated for performance reasons? nothing a compiler can inline, I guess.

so what about a comment instead:

kernel/groups.c +1 -1
index daae2f2dc6d4f..46b5d4cd53c2e 100644
@@ -93,7 +93,7 @@ void groups_sort(struct group_info *group_info)
 }
 EXPORT_SYMBOL(groups_sort);
 
-/* a simple bsearch */
+/* duplicate code from lib/bsearch.c for performance reasons */
 int groups_search(const struct group_info *group_info, kgid_t grp)
 {
        unsigned int left, right;

Mfg
thomas