selinux: avtab arrays and refactors
v2 cover selinux: avtab arrays and refactors
As the refpolicy and the default Fedora policy continue to grow in size, especially with regard to rules / access vectors, the memory usage of the policydb and runtime to search through it increases. Looking at /proc/slabinfo indicates that the avtab_node_cachep kmem_cache is significantly responsible for overall memory usage and was a good target for optimizations. Running "perf record" on the "load_policy" command shows that a majority of time is spent adding rules into the avtab.
This patch series is an at optimizing these hot spots within the security server implementation to help it scale with additional rules in the future.
All patches are independent of each other.
Patches 1-3 are a series of refactors of the internal avtab.c interfaces and code paths with no logic changes. It removes duplicative code and homogenizes access patterns.
Patch 4 changes avtab to use arrays instead of a kmem_cache for individual nodes of the hastable.
Jacob Satterfield (4):
selinux: simplify avtab_insert_node() prototype
selinux: refactor avtab_node comparisons
selinux: avtab iteration macros
selinux: use arrays for avtab hashtable nodes
security/selinux/ss/avtab.c | 206 ++++++++++++++---------------- security/selinux/ss/avtab.h | 4 +- security/selinux/ss/conditional.c | 37 ++++-- security/selinux/ss/conditional.h | 2 +- 4 files changed, 123 insertions(+), 126 deletions(-)
Signature · 1 line
2.41.0
PATCH v2 1/4 selinux: simplify avtab_insert_node() prototype
__hashtab_insert() in hashtab.h has a cleaner interface that allows the caller to specify the chain node location that the new node is being inserted into so that it can update the node that currently occupies it.
--- security/selinux/ss/avtab.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
security/selinux/ss/avtab.c +7 -13
@@ -67,8 +67,7 @@ static inline u32 avtab_hash(const struct avtab_key *keyp, u32 mask) }static struct avtab_node * -avtab_insert_node (struct avtab *h,u32 hvalue, -struct avtab_node *prev, +avtab_insert_node (struct avtab *h,struct avtab_node **dst,const struct avtab_key *key,const struct avtab_datum *datum) {struct avtab_node *newnode;
@@ -90,15 +89,8 @@ avtab_insert_node(struct avtab *h, u32 hvalue, newnode->datum.u.data = datum->u.data; } -if (prev) { - newnode->next = prev->next; - prev->next = newnode; - }else { -struct avtab_node **n = &h->htable[hvalue]; - - newnode->next = *n; - *n = newnode; - } + newnode->next = *dst; + *dst = newnode; h->nel++;return newnode;
@@ -138,7 +130,8 @@ static int avtab_insert(struct avtab *h, const struct avtab_key *key,break ; } - newnode =avtab_insert_node (h, hvalue, prev, key, datum); + newnode =avtab_insert_node (h, prev ? &prev->next : &h->htable[hvalue], + key, datum);if (!newnode)return -ENOMEM ;
@@ -178,7 +171,8 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h, key->target_class < cur->key.target_class) break; } -return avtab_insert_node (h, hvalue, prev, key, datum); +return avtab_insert_node (h, prev ? &prev->next : &h->htable[hvalue], + key, datum); }/* This search function returns a node pointer, and can be used in
Signature · 1 line
2.41.0
Re: [PATCH 1/4] selinux: simplify avtab_insert_node() prototype
Quoted 6 lines · Jacob Satterfield
__hashtab_insert()inhashtab.hhas a cleaner interface that allows the caller to specify the chain node location that the new node is being inserted into so that it can update the node that currently occupies it.Signed-off-by Jacob Satterfield <[email protected]>
Re: [PATCH 1/4] selinux: simplify avtab_insert_node() prototype
In reply to Jacob SatterfieldQuoted 10 lines · On Sep 29, 2023 Stephen Smalley <[email protected]> wrote:
__hashtab_insert()inhashtab.hhas a cleaner interface that allows the caller to specify the chain node location that the new node is being inserted into so that it can update the node that currently occupies it.Signed-off-by Jacob Satterfield <[email protected]>Reviewed-by Stephen Smalley <[email protected]>--- security/selinux/ss/avtab.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
Merged into selinux/next, thanks!
--
paul-moore.com
PATCH v2 2/4 selinux: refactor avtab_node comparisons
In reply to Jacob SatterfieldIn four separate functions within avtab, the same comparison logic is used. The only difference is how the result is handled or whether there is a unique specifier value to be checked for or used.
Extracting this functionality into the avtab_node_cmp() function unifies the comparison logic between searching and insertion and gets rid of duplicative code so that the implementation is easier to maintain.
--- security/selinux/ss/avtab.c | 101 +++++++++++++++--------------------- 1 file changed, 41 insertions(+), 60 deletions(-)
security/selinux/ss/avtab.c +41 -60
@@ -96,12 +96,34 @@ avtab_insert_node(struct avtab *h, struct avtab_node **dst,return newnode; } +static int avtab_node_cmp (const struct avtab_key *key1, +const struct avtab_key *key2) +{ +u16 specified = key1->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); + +if (key1->source_type == key2->source_type && + key1->target_type == key2->target_type && + key1->target_class == key2->target_class && + (specified & key2->specified)) +return 0 ; +if (key1->source_type < key2->source_type) +return -1 ; +if (key1->source_type == key2->source_type && + key1->target_type < key2->target_type) +return -1 ; +if (key1->source_type == key2->source_type && + key1->target_type == key2->target_type && + key1->target_class < key2->target_class) +return -1 ; +return 1 ; +} +static int avtab_insert (struct avtab *h,const struct avtab_key *key,const struct avtab_datum *datum) {u32 hvalue;struct avtab_node *prev, *cur, *newnode; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot || h->nel ==U32_MAX )return -EINVAL ;
@@ -110,23 +132,11 @@ static int avtab_insert(struct avtab *h, const struct avtab_key *key,for (prev =NULL , cur = h->htable[hvalue]; cur; prev = cur, cur = cur->next) { -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class == cur->key.target_class && - (specified & cur->key.specified)) { -/* extended perms may not be unique */ -if (specified &AVTAB_XPERMS ) -break ; + cmp =avtab_node_cmp (key, &cur->key); +/* extended perms may not be unique */ +if (unlikely (cmp ==0 && !(key->specified &AVTAB_XPERMS )))return -EEXIST ; - } -if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) +if (cmp <=0 )break ; }
@@ -148,7 +158,7 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h, {u32 hvalue;struct avtab_node *prev, *cur; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot || h->nel ==U32_MAX )return NULL ;
@@ -156,19 +166,8 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h,for (prev =NULL , cur = h->htable[hvalue]; cur; prev = cur, cur = cur->next) { -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class == cur->key.target_class && - (specified & cur->key.specified)) -break ; -if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) + cmp =avtab_node_cmp (key, &cur->key); +if (cmp <=0 )break ; } return avtab_insert_node(h, prev ? &prev->next : &h->htable[hvalue],
@@ -183,7 +182,7 @@ struct avtab_node *avtab_search_node(struct avtab *h, {u32 hvalue;struct avtab_node *cur; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot)return NULL ;
@@ -191,20 +190,10 @@ struct avtab_node *avtab_search_node(struct avtab *h, hvalue =avtab_hash (key, h->mask);for (cur = h->htable[hvalue]; cur; cur = cur->next) { -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class == cur->key.target_class && - (specified & cur->key.specified)) + cmp =avtab_node_cmp (key, &cur->key); +if (cmp ==0 )return cur; - -if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) +if (cmp <0 )break ; }return NULL ;
@@ -213,27 +202,19 @@ struct avtab_node *avtab_search_node(struct avtab *h,struct avtab_node *avtab_search_node_next (struct avtab_node *node,u16 specified) { +struct avtab_key tmp_key;struct avtab_node *cur; +int cmp;if (!node)return NULL ; - - specified &= ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); + tmp_key = node->key; + tmp_key.specified = specified;for (cur = node->next; cur; cur = cur->next) { -if (node->key.source_type == cur->key.source_type && - node->key.target_type == cur->key.target_type && - node->key.target_class == cur->key.target_class && - (specified & cur->key.specified)) + cmp =avtab_node_cmp (&tmp_key, &cur->key); +if (cmp ==0 )return cur; - -if (node->key.source_type < cur->key.source_type) -break ; -if (node->key.source_type == cur->key.source_type && - node->key.target_type < cur->key.target_type) -break ; -if (node->key.source_type == cur->key.source_type && - node->key.target_type == cur->key.target_type && - node->key.target_class < cur->key.target_class) +if (cmp <0 )break ; }return NULL ;
Signature · 1 line
2.41.0
Re: [PATCH 2/4] selinux: refactor avtab_node comparisons
Quoted 10 lines · Jacob Satterfield
In four separate functions within avtab, the same comparison logic is used. The only difference is how the result is handled or whether there is a unique specifier value to be checked for or used.
Extracting this functionality into the
avtab_node_cmp()function unifies the comparison logic between searching and insertion and gets rid of duplicative code so that the implementation is easier to maintain.Signed-off-by Jacob Satterfield <[email protected]>
Paul Moore
maintainerRe: [PATCH 2/4] selinux: refactor avtab_node comparisons
In reply to Jacob SatterfieldQuoted 9 lines · On Sep 29, 2023 Jacob Satterfield <[email protected]> wrote:
In four separate functions within avtab, the same comparison logic is used. The only difference is how the result is handled or whether there is a unique specifier value to be checked for or used.
Extracting this functionality into the
avtab_node_cmp()function unifies the comparison logic between searching and insertion and gets rid of duplicative code so that the implementation is easier to maintain.
Quoted 5 lines
Signed-off-by Jacob Satterfield <[email protected]>Reviewed-by Stephen Smalley <[email protected]>--- security/selinux/ss/avtab.c | 101 +++++++++++++++--------------------- 1 file changed, 41 insertions(+), 60 deletions(-)
Thanks for doing this. I've got a few small nits (below), but overall I think this is a nice improvement.
Quoted 8 lines
security/selinux/ss/avtab.c +0
-0@@ -96,12 +96,34 @@ avtab_insert_node(struct avtab *h, struct avtab_node **dst,return newnode; }
security/selinux/ss/avtab.c avtab_insert_node(struct avtab *h, struct avtab_node **dst, · line 108
Quoted 10 lines
security/selinux/ss/avtab.c +10
-0+static int avtab_node_cmp (const struct avtab_key *key1, +const struct avtab_key *key2) +{ +u16 specified = key1->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); + +if (key1->source_type == key2->source_type && + key1->target_type == key2->target_type && + key1->target_class == key2->target_class && + (specified & key2->specified)) +return 0 ;
Normally I'd be a nice guy and fix this up during the merge, but since I'd like a respin for other reasons I'm going to mention this so you can fix it yourself, and learn a bit for next time ;)
Please don't needlessly indent multi-line if-conditionals. As an example, here is what I would have preferred to see for the above if-statement:
if (key1->source_type == key2->source_type &&
key1->target_type == key2->target_type &&
key1->target_class == key2->target_class &&
(specified & key2->specified))
return 0;At some point in the future (hopefully soon, but my review backlog is daunting at the moment) we'll have some automated tools to help identify problems like this, but we're not there quite yet.
Quoted 28 lines
security/selinux/ss/avtab.c +13
-3+if (key1->source_type < key2->source_type) +return -1 ; +if (key1->source_type == key2->source_type && + key1->target_type < key2->target_type) +return -1 ; +if (key1->source_type == key2->source_type && + key1->target_type == key2->target_type && + key1->target_class < key2->target_class) +return -1 ; +return 1 ; +} +static int avtab_insert (struct avtab *h,const struct avtab_key *key,const struct avtab_datum *datum) {u32 hvalue;struct avtab_node *prev, *cur, *newnode; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot || h->nel ==U32_MAX )return -EINVAL ;@@ -110,23 +132,11 @@ static int avtab_insert(struct avtab *h, const struct avtab_key *key,for (prev =NULL , cur = h->htable[hvalue]; cur; prev = cur, cur = cur->next) { - if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type &&
security/selinux/ss/avtab.c static int avtab_insert(struct avtab *h, const struct avtab_key *key, · line 121
Quoted 10 lines
security/selinux/ss/avtab.c +3
-6- key->target_class == cur->key.target_class && - (specified & cur->key.specified)) { -/* extended perms may not be unique */ -if (specified &AVTAB_XPERMS ) -break ; + cmp =avtab_node_cmp (key, &cur->key); +/* extended perms may not be unique */ +if (unlikely (cmp ==0 && !(key->specified &AVTAB_XPERMS )))return -EEXIST ; - }
I'm generally not a big fan of the unlikely()/likely() macros unless it is a rather extreme case, please don't add one here.
security/selinux/ss/avtab.c static int avtab_insert(struct avtab *h, const struct avtab_key *key, · line 140
Quoted 10 lines
security/selinux/ss/avtab.c +1
-8-if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) +if (cmp <=0 )break ;
I wonder if we should put the cmp <= 0 (no match) check first since that will probably the more common case, right? Or am I thinking of this backwards?
Quoted 54 lines
security/selinux/ss/avtab.c +6
-20}@@ -148,7 +158,7 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h, {u32 hvalue;struct avtab_node *prev, *cur; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot || h->nel ==U32_MAX )return NULL ;@@ -156,19 +166,8 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h,for (prev =NULL , cur = h->htable[hvalue]; cur; prev = cur, cur = cur->next) { -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class == cur->key.target_class && - (specified & cur->key.specified)) -break ; -if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) + cmp =avtab_node_cmp (key, &cur->key); +if (cmp <=0 )break ; } return avtab_insert_node(h, prev ? &prev->next : &h->htable[hvalue],@@ -183,7 +182,7 @@ struct avtab_node *avtab_search_node(struct avtab *h, {u32 hvalue;struct avtab_node *cur; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot)return NULL ;@@ -191,20 +190,10 @@ struct avtab_node *avtab_search_node(struct avtab *h, hvalue =avtab_hash (key, h->mask);for (cur = h->htable[hvalue]; cur; cur = cur->next) { -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class == cur->key.target_class && - (specified & cur->key.specified)) + cmp =avtab_node_cmp (key, &cur->key); +if (cmp ==0 )return cur; -
security/selinux/ss/avtab.c struct avtab_node *avtab_search_node(struct avtab *h, · line 197
Quoted 10 lines
security/selinux/ss/avtab.c +1
-8-if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) +if (cmp <0 )break ;
See the ordering comment/question above in avtab_insert(). I'm reasonably confident in the search case there will be more misses than hits.
Quoted 25 lines
security/selinux/ss/avtab.c +6
-7}return NULL ;@@ -213,27 +202,19 @@ struct avtab_node *avtab_search_node(struct avtab *h,struct avtab_node *avtab_search_node_next (struct avtab_node *node,u16 specified) { +struct avtab_key tmp_key;struct avtab_node *cur; +int cmp;if (!node)return NULL ; - - specified &= ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); + tmp_key = node->key; + tmp_key.specified = specified;for (cur = node->next; cur; cur = cur->next) { -if (node->key.source_type == cur->key.source_type && - node->key.target_type == cur->key.target_type && - node->key.target_class == cur->key.target_class && - (specified & cur->key.specified)) + cmp =avtab_node_cmp (&tmp_key, &cur->key); +if (cmp ==0 )return cur; -
security/selinux/ss/avtab.c struct avtab_node *avtab_search_node(struct avtab *h, · line 218
Quoted 10 lines
security/selinux/ss/avtab.c +1
-8-if (node->key.source_type < cur->key.source_type) -break ; -if (node->key.source_type == cur->key.source_type && - node->key.target_type < cur->key.target_type) -break ; -if (node->key.source_type == cur->key.source_type && - node->key.target_type == cur->key.target_type && - node->key.target_class < cur->key.target_class) +if (cmp <0 )break ;
Another ordering spot.
Quoted 4 lines
security/selinux/ss/avtab.c +0
-0}return NULL ;Signature · 1 line
2.41.0
--
paul-moore.com
Re: [PATCH 2/4] selinux: refactor avtab_node comparisons
Quoted 48 lines · Paul Moore
Quoted 9 lines · On Sep 29, 2023 Jacob Satterfield <[email protected]> wrote:
In four separate functions within avtab, the same comparison logic is used. The only difference is how the result is handled or whether there is a unique specifier value to be checked for or used.
Extracting this functionality into the
avtab_node_cmp()function unifies the comparison logic between searching and insertion and gets rid of duplicative code so that the implementation is easier to maintain.Quoted 5 lines
Signed-off-by Jacob Satterfield <[email protected]>Reviewed-by Stephen Smalley <[email protected]>--- security/selinux/ss/avtab.c | 101 +++++++++++++++--------------------- 1 file changed, 41 insertions(+), 60 deletions(-)Thanks for doing this. I've got a few small nits (below), but overall I think this is a nice improvement.
Quoted 8 lines
security/selinux/ss/avtab.c +0
-0@@ -96,12 +96,34 @@ avtab_insert_node(struct avtab *h, struct avtab_node **dst,return newnode; }security/selinux/ss/avtab.c avtab_insert_node(struct avtab *h, struct avtab_node **dst, · line 108
Quoted 10 lines
security/selinux/ss/avtab.c +10
-0+static int avtab_node_cmp (const struct avtab_key *key1, +const struct avtab_key *key2) +{ +u16 specified = key1->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); + +if (key1->source_type == key2->source_type && + key1->target_type == key2->target_type && + key1->target_class == key2->target_class && + (specified & key2->specified)) +return 0 ;Normally I'd be a nice guy and fix this up during the merge, but since I'd like a respin for other reasons I'm going to mention this so you can fix it yourself, and learn a bit for next time ;)
Please don't needlessly indent multi-line if-conditionals. As an example, here is what I would have preferred to see for the above if-statement:
if (
key1->source_type==key2->source_type&&
Quoted 9 lines
key1->target_type == key2->target_type && key1->target_class == key2->target_class && (specified & key2->specified)) return 0;At some point in the future (hopefully soon, but my review backlog is daunting at the moment) we'll have some automated tools to help identify problems like this, but we're not there quite yet.
Fixed, my editor must have inserted them when I moved the code up. I'll be more mindful in the future.
Quoted 33 lines
Quoted 33 lines
security/selinux/ss/avtab.c +13
-8+if (key1->source_type < key2->source_type) +return -1 ; +if (key1->source_type == key2->source_type && + key1->target_type < key2->target_type) +return -1 ; +if (key1->source_type == key2->source_type && + key1->target_type == key2->target_type && + key1->target_class < key2->target_class) +return -1 ; +return 1 ; +} +static int avtab_insert (struct avtab *h,const struct avtab_key *key,const struct avtab_datum *datum) {u32 hvalue;struct avtab_node *prev, *cur, *newnode; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot || h->nel ==U32_MAX )return -EINVAL ;@@ -110,23 +132,11 @@ static int avtab_insert(struct avtab *h, const struct avtab_key *key,for (prev =NULL , cur = h->htable[hvalue]; cur; prev = cur, cur = cur->next) { -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class == cur->key.target_class && - (specified & cur->key.specified)) { -/* extended perms may not be unique */ -if (specified &AVTAB_XPERMS ) -break ;
Quoted 9 lines
security/selinux/ss/avtab.c static int avtab_insert(struct avtab *h, const struct avtab_key *key, · line 121
Quoted 5 lines
security/selinux/ss/avtab.c +3
-1+ cmp =avtab_node_cmp (key, &cur->key); +/* extended perms may not be unique */ +if (unlikely (cmp ==0 && !(key->specified &AVTAB_XPERMS )))return -EEXIST ; - }I'm generally not a big fan of the unlikely()/likely() macros unless it is a rather extreme case, please don't add one here.
Understood. I will not add them unless perf profiling shows a significant branch-predictor improvement; and I'll tag it accordingly in the commit message.
Quoted 11 lines
security/selinux/ss/avtab.c static int avtab_insert(struct avtab *h, const struct avtab_key *key, · line 140
Quoted 10 lines
security/selinux/ss/avtab.c +1
-8-if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) +if (cmp <=0 )break ;
Quoted 4 lines
I wonder if we should put the
cmp <= 0(no match) check first since that will probably the more common case, right? Or am I thinking of this backwards?
In this function, we can't do the <= first because the == branch is an invalid policy check. Only duplicate XPERMs are allowed, but if the <= is moved first it would allow all duplicates.
Quoted 60 lines
Quoted 60 lines
security/selinux/ss/avtab.c +6
-26}@@ -148,7 +158,7 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h, {u32 hvalue;struct avtab_node *prev, *cur; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot || h->nel ==U32_MAX )return NULL ;@@ -156,19 +166,8 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h,for (prev =NULL , cur = h->htable[hvalue]; cur; prev = cur, cur = cur->next) { -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class == cur->key.target_class && - (specified & cur->key.specified)) -break ; -if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) + cmp =avtab_node_cmp (key, &cur->key); +if (cmp <=0 )break ; } return avtab_insert_node(h, prev ? &prev->next : &h->htable[hvalue],@@ -183,7 +182,7 @@ struct avtab_node *avtab_search_node(struct avtab *h, {u32 hvalue;struct avtab_node *cur; -u16 specified = key->specified & ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); +int cmp;if (!h || !h->nslot)return NULL ;@@ -191,20 +190,10 @@ struct avtab_node *avtab_search_node(struct avtab *h, hvalue =avtab_hash (key, h->mask);for (cur = h->htable[hvalue]; cur; cur = cur->next) { -if (key->source_type == cur->key.source_type && - key->target_type == cur->key.target_type && - key->target_class == cur->key.target_class && - (specified & cur->key.specified)) + cmp =avtab_node_cmp (key, &cur->key); +if (cmp ==0 )return cur; - -if (key->source_type < cur->key.source_type) -break ; -if (key->source_type == cur->key.source_type && - key->target_type < cur->key.target_type) -break ; - if (key->source_type == cur->key.source_type &&
Quoted 9 lines
security/selinux/ss/avtab.c struct avtab_node *avtab_search_node(struct avtab *h, · line 197
Quoted 4 lines
security/selinux/ss/avtab.c +1
-2- key->target_type == cur->key.target_type && - key->target_class < cur->key.target_class) +if (cmp <0 )break ;See the ordering comment/question above in
avtab_insert(). I'm reasonably confident in the search case there will be more misses than hits.
I'll admit that I'm not well versed in all of the ways that SELinux is exercised, but my intuition of the call patterns of this particular function leads me to believe that the performance trade-off depends on which mode will be used.
Under permissive mode, a long-running process (like a database or webserver) could cause an indeterminate number of searches which don't have entries in the avtab causing misses to be more prevalent. Under enforcing mode, the same process would only search up to the first missing entry before being denied, making hits the default case.
I did not consider a change to the original ordering since I had assumed this trade-off had already been considered. The current ordering suggests that enforcing would be preferred. However, given that this portion of code has not been touched since it was mainlined, do we see one mode preferred over the other and should that preference be catered to? Should it be a compile time option? And how substantial is the performance difference under the policies of different distributions?
Quoted 29 lines
Quoted 29 lines
security/selinux/ss/avtab.c +6
-11}return NULL ;@@ -213,27 +202,19 @@ struct avtab_node *avtab_search_node(struct avtab *h,struct avtab_node *avtab_search_node_next (struct avtab_node *node,u16 specified) { +struct avtab_key tmp_key;struct avtab_node *cur; +int cmp;if (!node)return NULL ; - - specified &= ~(AVTAB_ENABLED |AVTAB_ENABLED_OLD ); + tmp_key = node->key; + tmp_key.specified = specified;for (cur = node->next; cur; cur = cur->next) { -if (node->key.source_type == cur->key.source_type && - node->key.target_type == cur->key.target_type && - node->key.target_class == cur->key.target_class && - (specified & cur->key.specified)) + cmp =avtab_node_cmp (&tmp_key, &cur->key); +if (cmp ==0 )return cur; - -if (node->key.source_type < cur->key.source_type) -break ; -if (node->key.source_type == cur->key.source_type && - node->key.target_type < cur->key.target_type)
Quoted 9 lines
security/selinux/ss/avtab.c struct avtab_node *avtab_search_node(struct avtab *h, · line 218
Quoted 6 lines
security/selinux/ss/avtab.c +1
-4-break ; -if (node->key.source_type == cur->key.source_type && - node->key.target_type == cur->key.target_type && - node->key.target_class < cur->key.target_class) +if (cmp <0 )break ;Another ordering spot.
My comments above apply to here as well.
Quoted 7 lines
Quoted 4 lines
security/selinux/ss/avtab.c +0
-1}return NULL ; --2.41.0
--
paul-moore.com
Thanks for the review!
-Jacob
PATCH v2 3/4 selinux: avtab iteration macros
In reply to Jacob SatterfieldSimilar to the list_for_each macros in list.h, this patch adds two macros that iterates an avtab_node linked list (avtab_chain_for_each and avtab_chain_for_each_prev). This has two benefits: it reduces the amount of duplicative code for iteration and it makes changes to the underlying hashtable data structure easier as there are fewer places to update.
--- security/selinux/ss/avtab.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
security/selinux/ss/avtab.c +17 -23
@@ -27,6 +27,13 @@static struct kmem_cache *avtab_node_cachep__ro_after_init ;static struct kmem_cache *avtab_xperms_cachep__ro_after_init ; +#define avtab_chain_for_each (pos, tab, slot) \ + for (pos = (tab)->htable[slot]; pos; pos = pos->next) + +#define avtab_chain_for_each_prev (pos, prev, tab, slot) \ + for (prev = NULL, pos = (tab)->htable[slot]; pos; \ + prev = pos, pos = pos->next) +/* Based on MurmurHash3, written by Austin Appleby and placed in the * public domain. */
@@ -129,9 +136,7 @@ static int avtab_insert(struct avtab *h, const struct avtab_key *key,return -EINVAL ; hvalue =avtab_hash (key, h->mask); -for (prev =NULL , cur = h->htable[hvalue]; - cur; - prev = cur, cur = cur->next) { +avtab_chain_for_each_prev (cur, prev, h, hvalue) { cmp =avtab_node_cmp (key, &cur->key);/* extended perms may not be unique */ if (unlikely (cmp ==0 && !(key->specified &AVTAB_XPERMS )))
@@ -163,9 +168,7 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h,if (!h || !h->nslot || h->nel ==U32_MAX )return NULL ; hvalue =avtab_hash (key, h->mask); -for (prev =NULL , cur = h->htable[hvalue]; - cur; - prev = cur, cur = cur->next) { +avtab_chain_for_each_prev (cur, prev, h, hvalue) { cmp =avtab_node_cmp (key, &cur->key);if (cmp <=0 )break ;
@@ -180,16 +183,13 @@ struct avtab_node *avtab_insert_nonunique(struct avtab *h,struct avtab_node *avtab_search_node (struct avtab *h,const struct avtab_key *key) { -u32 hvalue;struct avtab_node *cur;int cmp;if (!h || !h->nslot)return NULL ; - hvalue =avtab_hash (key, h->mask); -for (cur = h->htable[hvalue]; cur; - cur = cur->next) { +avtab_chain_for_each (cur, h,avtab_hash (key, h->mask)) { cmp =avtab_node_cmp (key, &cur->key);if (cmp ==0 )return cur;
@@ -223,20 +223,17 @@ avtab_search_node_next(struct avtab_node *node, u16 specified)void avtab_destroy (struct avtab *h) {u32 i; -struct avtab_node *cur, *temp; +struct avtab_node *cur;if (!h)return ;for (i =0 ; i < h->nslot; i++) { - cur = h->htable[i]; -while (cur) { - temp = cur; - cur = cur->next; -if (temp->key.specified &AVTAB_XPERMS ) +avtab_chain_for_each (cur, h, i) { +if (cur->key.specified &AVTAB_XPERMS )kmem_cache_free (avtab_xperms_cachep, - temp->datum.u.xperms); -kmem_cache_free (avtab_node_cachep, temp); + cur->datum.u.xperms); +kmem_cache_free (avtab_node_cachep, cur); } }kvfree (h->htable);
@@ -307,10 +304,8 @@ void avtab_hash_eval(struct avtab *h, const char *tag)if (cur) { slots_used++; chain_len =0 ; -while (cur) { +avtab_chain_for_each (cur, h, i) chain_len++; - cur = cur->next; - }if (chain_len > max_chain_len) max_chain_len = chain_len;
@@ -593,8 +588,7 @@ int avtab_write(struct policydb *p, struct avtab *a, void *fp)return rc;for (i =0 ; i < a->nslot; i++) { -for (cur = a->htable[i]; cur; - cur = cur->next) { +avtab_chain_for_each (cur, a, i) { rc =avtab_write_item (p, cur, fp);if (rc)return rc;
Signature · 1 line
2.41.0
Stephen Smalley
maintainerRe: [PATCH 3/4] selinux: avtab iteration macros
Quoted 6 lines · Jacob Satterfield
Similar to the
list_for_eachmacros inlist.h, this patch adds two macros that iterates anavtab_nodelinked list (avtab_chain_for_eachandavtab_chain_for_each_prev). This has two benefits: it reduces the amount of duplicative code for iteration and it makes changes to the underlying hashtable data structure easier as there are fewer places to update.
You will need/want an equivalent to list_for_each_safe() or open-code it to handle avtab_destroy() below.
Quoted 26 lines
Signed-off-by Jacob Satterfield <[email protected]>--- security/selinux/ss/avtab.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)security/selinux/ss/avtab.c +1
-6@@ -223,20 +223,17 @@ avtab_search_node_next(struct avtab_node *node, u16 specified)void avtab_destroy (struct avtab *h) {u32 i; -struct avtab_node *cur, *temp; +struct avtab_node *cur;if (!h)return ;for (i =0 ; i < h->nslot; i++) { - cur = h->htable[i]; -while (cur) { - temp = cur; - cur = cur->next; -if (temp->key.specified &AVTAB_XPERMS )
security/selinux/ss/avtab.c avtab_search_node_next(struct avtab_node *node, u16 specified) · line 239
Quoted 10 lines
security/selinux/ss/avtab.c +4
-2+avtab_chain_for_each (cur, h, i) { +if (cur->key.specified &AVTAB_XPERMS )kmem_cache_free (avtab_xperms_cachep, - temp->datum.u.xperms); -kmem_cache_free (avtab_node_cachep, temp); + cur->datum.u.xperms); +kmem_cache_free (avtab_node_cachep, cur); } }kvfree (h->htable);
This requires an avtab_chain_for_each_safe() or similar since it frees the node.
Re: [PATCH 3/4] selinux: avtab iteration macros
Quoted 43 lines · Stephen Smalley
Quoted 6 lines · Jacob Satterfield
Similar to the
list_for_eachmacros inlist.h, this patch adds two macros that iterates anavtab_nodelinked list (avtab_chain_for_eachandavtab_chain_for_each_prev). This has two benefits: it reduces the amount of duplicative code for iteration and it makes changes to the underlying hashtable data structure easier as there are fewer places to update.You will need/want an equivalent to
list_for_each_safe()or open-code it to handleavtab_destroy()below.Quoted 30 lines
Signed-off-by Jacob Satterfield <[email protected]>--- security/selinux/ss/avtab.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)security/selinux/ss/avtab.c +3
-7@@ -223,20 +223,17 @@ avtab_search_node_next(struct avtab_node *node, u16 specified)void avtab_destroy (struct avtab *h) {u32 i; -struct avtab_node *cur, *temp; +struct avtab_node *cur;if (!h)return ;for (i =0 ; i < h->nslot; i++) { - cur = h->htable[i]; -while (cur) { - temp = cur; - cur = cur->next; -if (temp->key.specified &AVTAB_XPERMS ) +avtab_chain_for_each (cur, h, i) { +if (cur->key.specified &AVTAB_XPERMS ) kmem_cache_free(avtab_xperms_cachep, - temp->datum.u.xperms);
Quoted 8 lines
security/selinux/ss/avtab.c avtab_search_node_next(struct avtab_node *node, u16 specified) · line 239
Quoted 6 lines
security/selinux/ss/avtab.c +2
-1-kmem_cache_free (avtab_node_cachep, temp); + cur->datum.u.xperms); +kmem_cache_free (avtab_node_cachep, cur); } }kvfree (h->htable);This requires an
avtab_chain_for_each_safe()or similar since it frees the node.
Great catch! Since this separate macro would only have one invocation, it would make the code less readable without any benefit. I have reverted the changes to avtab_destroy() for the next spin.
Thanks!
-Jacob
PATCH v2 4/4 selinux: use arrays for avtab hashtable nodes
In reply to Jacob SatterfieldThe current avtab hashtable employs a separate chaining collision resolution strategy where each bucket/chain holds an ordered linked list of pointers to kmem_cache allocated avtab_node elements.
On Fedora 38 (x86_64) using the default policy, avtab_node_cachep uses 573 slabs each containing 170 objects totaling 2,337,840 bytes. A call to kmem_cache_zalloc() is required for every single rule, which in the default policy is currently 96,730 and continually rising.
When both sets of avtab_node (regular and cond.) are turned into arrays with the hash table slot indexing into it rather than a pointer, then this results in only 2 additional kvcalloc() calls and the complete removal of the kmem_cache itself.
Due to how conditional rules are written in the binary policy, the code responsible for loading does not know how many conditional rules there are before creating the avtab structure. Instead, it was using the number of elements in the non-conditional avtab as a hint and allocates the hash table based on it. Therefore, a two-pass algorithm is now used to calculate the rule count before allocating the avtab nodes array. With the current refpolicy and default Fedora policy, this causes the number of hash slots for the conditional array to become 4096 instead of 32768. This results in a savings of 224KB of heap memory.
The caching characteristics of iterating a single array are better due to locality of reference. Running "perf stat -r 100 -d load_policy" has shown a runtime reduction of at least 10% on a Fedora 38 x86_64 VM with this single patch. Future patches focused on improving the hash table's collision resolution strategy and array layout (struct-of-arrays vs. array-of-structs) may elicit even more caching and therefore runtime performance improvements.
--- security/selinux/ss/avtab.c | 47 ++++++++++++++++++++----------- security/selinux/ss/avtab.h | 4 ++- security/selinux/ss/conditional.c | 37 ++++++++++++++++-------- security/selinux/ss/conditional.h | 2 +- 4 files changed, 59 insertions(+), 31 deletions(-)
security/selinux/ss/avtab.c +30 -17
@@ -24,7 +24,6 @@#include "avtab.h" #include "policydb.h" -static struct kmem_cache *avtab_node_cachep__ro_after_init ;static struct kmem_cache *avtab_xperms_cachep__ro_after_init ;#define avtab_chain_for_each (pos, tab, slot) \
@@ -79,17 +78,15 @@ avtab_insert_node(struct avtab *h, struct avtab_node **dst, {struct avtab_node *newnode;struct avtab_extended_perms *xperms; - newnode =kmem_cache_zalloc (avtab_node_cachep,GFP_KERNEL ); -if (newnode ==NULL ) +if (h->nel == h->nnodes)return NULL ; + newnode = &h->nodes[h->nel]; newnode->key = *key;if (key->specified &AVTAB_XPERMS ) { xperms =kmem_cache_zalloc (avtab_xperms_cachep,GFP_KERNEL ); -if (xperms ==NULL ) { -kmem_cache_free (avtab_node_cachep, newnode); +if (xperms ==NULL )return NULL ; - } *xperms = *(datum->u.xperms); newnode->datum.u.xperms = xperms; }else {
@@ -233,11 +230,13 @@ void avtab_destroy(struct avtab *h)if (cur->key.specified &AVTAB_XPERMS )kmem_cache_free (avtab_xperms_cachep, cur->datum.u.xperms); -kmem_cache_free (avtab_node_cachep, cur); } }kvfree (h->htable); +kvfree (h->nodes); h->htable =NULL ; + h->nodes =NULL ; + h->nnodes =0 ; h->nel =0 ; h->nslot =0 ; h->mask =0 ;
@@ -246,20 +245,28 @@ void avtab_destroy(struct avtab *h)void avtab_init (struct avtab *h) { h->htable =NULL ; + h->nodes =NULL ; + h->nnodes =0 ; h->nel =0 ; h->nslot =0 ; h->mask =0 ; } -static int avtab_alloc_common (struct avtab *h,u32 nslot) +static int avtab_alloc_common (struct avtab *h,u32 nslot,u32 nrules) {if (!nslot)return 0 ; - h->htable =kvcalloc (nslot,sizeof (void *),GFP_KERNEL ); + h->htable =kvcalloc (nslot,sizeof (*h->htable),GFP_KERNEL );if (!h->htable)return -ENOMEM ; - + h->nodes =kvcalloc (nrules,sizeof (*h->nodes),GFP_KERNEL ); +if (!h->nodes) { +kvfree (h->htable); + h->htable =NULL ; +return -ENOMEM ; + } + h->nnodes = nrules; h->nslot = nslot; h->mask = nslot -1 ;return 0 ;
@@ -275,7 +282,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)if (nslot >MAX_AVTAB_HASH_BUCKETS ) nslot =MAX_AVTAB_HASH_BUCKETS ; - rc =avtab_alloc_common (h, nslot); + rc =avtab_alloc_common (h, nslot, nrules);if (rc)return rc; }
@@ -286,7 +293,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)int avtab_alloc_dup (struct avtab *new,const struct avtab *orig) { -return avtab_alloc_common (new, orig->nslot); +return avtab_alloc_common (new, orig->nslot, orig->nel); }#ifdef CONFIG_SECURITY_SELINUX_DEBUG
@@ -335,7 +342,7 @@ static const uint16_t spec_order[] = {int avtab_read_item (struct avtab *a,void *fp,struct policydb *pol,int (*insertf)(struct avtab *a,const struct avtab_key *k,const struct avtab_datum *d,void *p), -void *p) +void *p,u32 *nrules) {__le16 buf16[4 ];u16 enabled;
@@ -409,6 +416,10 @@ int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,if (val & spec_order[i]) { key.specified = spec_order[i] | enabled; datum.u.data =le32_to_cpu (buf32[items++]); +if (nrules) { + (*nrules)++; +continue ; + } rc =insertf (a, &key, &datum, p);if (rc)return rc;
@@ -487,6 +498,11 @@ int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,pr_err ("SELinux: avtab: invalid type\n" );return -EINVAL ; } + +if (nrules) { + (*nrules)++; +return 0 ; + }return insertf (a, &key, &datum, p); }
@@ -520,7 +536,7 @@ int avtab_read(struct avtab *a, void *fp, struct policydb *pol) goto bad;for (i =0 ; i < nel; i++) { - rc =avtab_read_item (a, fp, pol, avtab_insertf,NULL ); + rc =avtab_read_item (a, fp, pol, avtab_insertf,NULL ,NULL );if (rc) {if (rc == -ENOMEM )pr_err ("SELinux: avtab: out of memory\n" );
@@ -600,9 +616,6 @@ int avtab_write(struct policydb *p, struct avtab *a, void *fp)void __init avtab_cache_init (void ) { - avtab_node_cachep =kmem_cache_create ("avtab_node" , -sizeof (struct avtab_node ), -0 ,SLAB_PANIC ,NULL ); avtab_xperms_cachep =kmem_cache_create ("avtab_extended_perms" ,sizeof (struct avtab_extended_perms ),0 ,SLAB_PANIC ,NULL );
security/selinux/ss/avtab.h +3 -1
@@ -82,6 +82,8 @@ struct avtab_node {struct avtab {struct avtab_node **htable; +struct avtab_node *nodes; +u32 nnodes;/* number of nodes */ u32 nel;/* number of elements */ u32 nslot;/* number of hash slots */ u32 mask;/* mask to compute hash func */
@@ -104,7 +106,7 @@ struct policydb;int avtab_read_item (struct avtab *a,void *fp,struct policydb *pol,int (*insert)(struct avtab *a,const struct avtab_key *k,const struct avtab_datum *d,void *p), -void *p); +void *p,u32 *nrules);int avtab_read (struct avtab *a,void *fp,struct policydb *pol);int avtab_write_item (struct policydb *p,const struct avtab_node *cur,void *fp);
security/selinux/ss/conditional.c +25 -12
@@ -140,7 +140,7 @@ void cond_policydb_init(struct policydb *p)static void cond_node_destroy (struct cond_node *node) {kfree (node->expr.nodes); -/* the avtab_ptr_t nodes are destroyed by the avtab */ +/* the actual nodes were destroyed by avtab_destroy() */ kfree (node->true_list.nodes);kfree (node->false_list.nodes); }
@@ -323,7 +323,8 @@ static int cond_insertf(struct avtab *a, const struct avtab_key *k,static int cond_read_av_list (struct policydb *p,void *fp,struct cond_av_list *list, -struct cond_av_list *other) +struct cond_av_list *other, +u32 *nrules) {int rc;__le32 buf[1 ];
@@ -347,7 +348,7 @@ static int cond_read_av_list(struct policydb *p, void *fp,for (i =0 ; i < len; i++) { data.dst = &list->nodes[i]; rc =avtab_read_item (&p->te_cond_avtab, fp, p, cond_insertf, - &data); + &data, nrules);if (rc) {kfree (list->nodes); list->nodes =NULL ;
@@ -373,7 +374,8 @@ static int expr_node_isvalid(struct policydb *p, struct cond_expr_node *expr)return 1 ; } -static int cond_read_node (struct policydb *p,struct cond_node *node,void *fp) +static int cond_read_node (struct policydb *p,struct cond_node *node, +void *fp,u32 *nrules) {__le32 buf[2 ];u32 i, len;
@@ -407,16 +409,17 @@ static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)return -EINVAL ; } - rc =cond_read_av_list (p, fp, &node->true_list,NULL ); + rc =cond_read_av_list (p, fp, &node->true_list,NULL , nrules);if (rc)return rc; -return cond_read_av_list (p, fp, &node->false_list, &node->true_list); +return cond_read_av_list (p, fp, &node->false_list, &node->true_list, nrules); } -int cond_read_list (struct policydb *p,void *fp) +int cond_read_list (struct policydb *p,struct policy_file *fp) {__le32 buf[1 ]; -u32 i, len; +struct policy_file tmp_fp; +u32 i, len, nrules;int rc; rc =next_entry (buf, fp,sizeof (buf));
@@ -428,15 +431,25 @@ int cond_read_list(struct policydb *p, void *fp) p->cond_list =kcalloc (len,sizeof (*p->cond_list),GFP_KERNEL );if (!p->cond_list)return -ENOMEM ; + p->cond_list_len = len; + +/* first pass to only calculate the avrule count */ + tmp_fp = *fp; + nrules =0 ; +for (i =0 ; i < len; i++) { + rc =cond_read_node (p, &p->cond_list[i], &tmp_fp, &nrules); +if (rc) + goto err; +cond_node_destroy (&p->cond_list[i]); + } - rc =avtab_alloc (&(p->te_cond_avtab), p->te_avtab.nel); + rc =avtab_alloc (&(p->te_cond_avtab), nrules);if (rc) goto err; - p->cond_list_len = len; - +/* second pass to read in the conditional nodes */ for (i =0 ; i < len; i++) { - rc =cond_read_node (p, &p->cond_list[i], fp); + rc =cond_read_node (p, &p->cond_list[i], fp,NULL );if (rc) goto err; }
security/selinux/ss/conditional.h +1 -1
@@ -70,7 +70,7 @@ int cond_destroy_bool(void *key, void *datum, void *p);int cond_index_bool (void *key,void *datum,void *datap);int cond_read_bool (struct policydb *p,struct symtab *s,void *fp); -int cond_read_list (struct policydb *p,void *fp); +int cond_read_list (struct policydb *p,struct policy_file *fp);int cond_write_bool (void *key,void *datum,void *ptr);int cond_write_list (struct policydb *p,void *fp);
Signature · 1 line
2.41.0