Skip to content

Commit 9d14088

Browse files
jsatterfield-linuxpcmoore
authored andcommitted
selinux: hweight optimization in avtab_read_item
avtab_read_item() is a hot function called when reading each rule in a binary policydb. With the current Fedora policy and refpolicy, this function is called nearly 100,000 times per policy load. A single avtab node is only permitted to have a single specifier to describe the data it holds. As such, a check is performed to make sure only one specifier is set. Previously this was done via a for-loop. However, there is already an optimal function for finding the number of bits set (hamming weight) and on some architectures, dedicated instructions (popcount) which can be executed much more efficiently. Even when using -mcpu=generic on a x86-64 Fedora 38 VM, this commit results in a modest 2-4% speedup for policy loading due to a substantial reduction in the number of instructions executed. Signed-off-by: Jacob Satterfield <jsatterfield.linux@gmail.com> Reviewed-by: Stephen Smalley <stephen.smalley.work@gmail.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 37b7ea3 commit 9d14088

1 file changed

Lines changed: 2 additions & 5 deletions

File tree

security/selinux/ss/avtab.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Tuned number of hash slots for avtab to reduce memory usage
1818
*/
1919

20+
#include <linux/bitops.h>
2021
#include <linux/kernel.h>
2122
#include <linux/slab.h>
2223
#include <linux/errno.h>
@@ -471,11 +472,7 @@ int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
471472
return -EINVAL;
472473
}
473474

474-
set = 0;
475-
for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
476-
if (key.specified & spec_order[i])
477-
set++;
478-
}
475+
set = hweight16(key.specified & (AVTAB_XPERMS | AVTAB_TYPE | AVTAB_AV));
479476
if (!set || set > 1) {
480477
pr_err("SELinux: avtab: more than one specifier\n");
481478
return -EINVAL;

0 commit comments

Comments
 (0)