selinux: optimize ebitmap_and()
PATCH v3 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").
---
v3:
apply format style
v2:
fix array size computation
--- security/selinux/ss/ebitmap.c | 50 +++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 8 deletions(-)
security/selinux/ss/ebitmap.c +42 -8
@@ -78,19 +78,53 @@ 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 <ARRAY_SIZE (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.43.0
Paul Moore
maintainerQuoted 8 lines · On Mar 15, 2024 =?UTF-8?q?Christian=20G=C3=B6ttsche?= <[email protected]>…
Iterate on nodes instead of single bits to save node resolution for each single bit.
Similar to userspace patch
efcd00814879("libsepol: optimizeebitmap_and").Signed-off-by Christian Göttsche <[email protected]>
Quoted 8 lines
---
v3:
apply format style
v2:
fix array size computation--- security/selinux/ss/ebitmap.c | 50 +++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 8 deletions(-)
Some minor comments below, but do you have any performance measurements for this change? I realize that ebitmap_and() isn't widely used, but it would be nice to understand the performance difference, and if there isn't much/any difference we might want to stick with the original code as it is much simpler.
Quoted 12 lines
security/selinux/ss/ebitmap.c +2
-2@@ -78,19 +78,53 @@ 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;
security/selinux/ss/ebitmap.c int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src) · line 86
Quoted 10 lines
security/selinux/ss/ebitmap.c +1
-6ebitmap_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;
Later in this function you include parenthesis, that might be nice here too:
prev = &(dst->node);
security/selinux/ss/ebitmap.c int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src) · line 91
Quoted 5 lines
security/selinux/ss/ebitmap.c +5
-0+ n1 = e1->node; + n2 = e2->node; +while (n1 && n2) { +if (n1->startbit == n2->startbit) { +unsigned long testmap[EBITMAP_UNIT_NUMS ];
This is very bikeshed-y, but I much prefer "dstmaps" over "testmap".
security/selinux/ss/ebitmap.c int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src) · line 97
Quoted 6 lines
security/selinux/ss/ebitmap.c +6
-0+unsigned int i; +bool match = false; + +for (i =0 ; i <ARRAY_SIZE (testmap); i++) { + testmap[i] = n1->maps[i] & n2->maps[i]; +if (testmap[i] !=0 )
If I'm going to be nitpicky, I'd probably prefer 'if (!dstmaps[i])'.
security/selinux/ss/ebitmap.c int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src) · line 109
Quoted 12 lines
security/selinux/ss/ebitmap.c +12
-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 );
Why not just use 'sizeof(dstmaps)'?
memcpy(new->maps, dstmaps, sizeof(dstmaps));
security/selinux/ss/ebitmap.c int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src) · line 110
Quoted 1 line
security/selinux/ss/ebitmap.c +1
-0+ new->next =NULL ;
You shouldn't need the line above since you're doing a _zalloc().
Quoted 21 lines
security/selinux/ss/ebitmap.c +14
-0+ *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.43.0
--
paul-moore.com