Skip to contentSkip to conversation

selinux: optimize ebitmap_and()

RFC PATCH Superseded Christian Göttsche 2 participants 2 messages Jul 28, 2023 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 selinux: optimize ebitmap_and()

Iterate on nodes instead of single bits to save node resolution for each single bit.

Similar to userspace patch efcd00814879 ("libsepol: optimize ebitmap_and").

---
 security/selinux/ss/ebitmap.c | 48 ++++++++++++++++++++++++++++++-----
 1 file changed, 41 insertions(+), 7 deletions(-)
security/selinux/ss/ebitmap.c +41 -7
index 77875ad355f7..5ac8acacf873 100644
@@ -81,18 +81,52 @@ int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src)
 
 int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1, const struct ebitmap *e2)
 {
-	struct ebitmap_node *n;
-	int bit, rc;
+	const struct ebitmap_node *n1, *n2;
+	struct ebitmap_node *new = NULL, **prev;
 
 	ebitmap_init(dst);
 
-	ebitmap_for_each_positive_bit(e1, n, bit) {
-		if (ebitmap_get_bit(e2, bit)) {
-			rc = ebitmap_set_bit(dst, bit, 1);
-			if (rc < 0)
-				return rc;
+	prev = &dst->node;
+	n1 = e1->node;
+	n2 = e2->node;
+	while (n1 && n2) {
+		if (n1->startbit == n2->startbit) {
+			unsigned long testmap[EBITMAP_UNIT_NUMS];
+			unsigned int i;
+			bool match = false;
+
+			for (i = 0; i < sizeof(testmap); i++) {
+				testmap[i] = n1->maps[i] & n2->maps[i];
+				if (testmap[i] != 0)
+					match = true;
+			}
+
+			if (match) {
+				new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
+				if (!new) {
+					ebitmap_destroy(dst);
+					return -ENOMEM;
+				}
+				new->startbit = n1->startbit;
+				memcpy(new->maps, testmap, EBITMAP_SIZE / 8);
+				new->next = NULL;
+
+				*prev = new;
+				prev = &(new->next);
+			}
+
+			n1 = n1->next;
+			n2 = n2->next;
+		} else if (n1->startbit > n2->startbit) {
+			n2 = n2->next;
+		} else {
+			n1 = n1->next;
 		}
 	}
+
+	if (new)
+		dst->highbit = new->startbit + EBITMAP_SIZE;
+
 	return 0;
 }
 
Signature · 1 line
2.40.1
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Christian,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on pcmoore-selinux/next]
[also build test WARNING on linus/master v6.5-rc3 next-20230728] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Christian-G-ttsche/selinux-optimize-ebitmap_and/20230729-000029
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git next
patch link:    https://lore.kernel.org/r/20230728155937.41580-1-cgzones%40googlemail.com
patch subject: [RFC PATCH] selinux: optimize ebitmap_and()
config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20230729/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230729/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

security/selinux/ss/ebitmap.c: In function 'ebitmap_and':

>> security/selinux/ss/ebitmap.c:99:54: warning: iteration 6 invokes undefined behavior [-Waggressive-loop-optimizations]
      99 |                                 testmap[i] = n1->maps[i] & n2->maps[i];
         |                                              ~~~~~~~~^~~
   security/selinux/ss/ebitmap.c:98:39: note: within this loop
      98 |                         for (i = 0; i < sizeof(testmap); i++) {
         |                                     ~~^~~~~~~~~~~~~~~~~

vim +99 security/selinux/ss/ebitmap.c

    81	
    82	int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1, const struct ebitmap *e2)
    83	{
    84		const struct ebitmap_node *n1, *n2;
    85		struct ebitmap_node *new = NULL, **prev;
    86	
    87		ebitmap_init(dst);
    88	
    89		prev = &dst->node;
    90		n1 = e1->node;
    91		n2 = e2->node;
    92		while (n1 && n2) {
    93			if (n1->startbit == n2->startbit) {
    94				unsigned long testmap[EBITMAP_UNIT_NUMS];
    95				unsigned int i;
    96				bool match = false;
    97	
    98				for (i = 0; i < sizeof(testmap); i++) {
  > 99					testmap[i] = n1->maps[i] & n2->maps[i];

100 if (testmap[i] != 0)
101 match = true;
102 }
103
104 if (match) {
105 new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
106 if (!new) {
107 ebitmap_destroy(dst);
108 return -ENOMEM;
109 }
110 new->startbit = n1->startbit;
111 memcpy(new->maps, testmap, EBITMAP_SIZE / 8);
112 new->next = NULL;
113
114 *prev = new;
115 prev = &(new->next);
116 }
117
118 n1 = n1->next;
119 n2 = n2->next;
120 } else if (n1->startbit > n2->startbit) {
121 n2 = n2->next;
122 } else {
123 n1 = n1->next;
124 }
125 }
126
127 if (new)
128 dst->highbit = new->startbit + EBITMAP_SIZE;
129
130 return 0;
131 }
132

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki