Skip to contentSkip to conversation

selinux: reduce the use of hard-coded hash sizes

PATCH Superseded Ondrej Mosnacek 3 participants 5 messages Feb 19, 2020 – Feb 23, 2020 SELinux security module

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selinux: reduce the use of hard-coded hash sizes

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---

Changed in v2:
- guard against h->size == 0 in hashtab_search() and hashtab_insert()

 security/selinux/ss/hashtab.c  | 25 +++++++++++++---
 security/selinux/ss/hashtab.h  |  2 +-
 security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
 security/selinux/ss/policydb.h |  2 --
 4 files changed, 42 insertions(+), 40 deletions(-)
security/selinux/ss/hashtab.c +21 -4
index ebfdaa31ee32..87ad83148cbd 100644
@@ -12,12 +12,26 @@
 
 static struct kmem_cache *hashtab_node_cachep;
 
+static u32 hashtab_compute_size(u32 nel)
+{
+	u32 size;
+
+	if (nel <= 2)
+		return nel;
+
+	/* use the nearest power of two to balance size and access time */
+	size = roundup_pow_of_two(nel);
+	if (size - nel > size / 4)
+		size /= 2;
+	return size;
+}
+
 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
 			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
-			       u32 size)
+			       u32 nel_hint)
 {
 	struct hashtab *p;
-	u32 i;
+	u32 i, size = hashtab_compute_size(nel_hint);
 
 	p = kzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p)
@@ -27,6 +41,9 @@ struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *
 	p->nel = 0;
 	p->hash_value = hash_value;
 	p->keycmp = keycmp;
+	if (!size)
+		return p;
+
 	p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
 	if (!p->htable) {
 		kfree(p);
@@ -46,7 +63,7 @@ int hashtab_insert(struct hashtab *h, void *key, void *datum)
 
 	cond_resched();
 
-	if (!h || h->nel == HASHTAB_MAX_NODES)
+	if (!h || !h->size || h->nel == HASHTAB_MAX_NODES)
 		return -EINVAL;
 
 	hvalue = h->hash_value(h, key);
@@ -82,7 +99,7 @@ void *hashtab_search(struct hashtab *h, const void *key)
 	u32 hvalue;
 	struct hashtab_node *cur;
 
-	if (!h)
+	if (!h || !h->size)
 		return NULL;
 
 	hvalue = h->hash_value(h, key);
security/selinux/ss/hashtab.h +1 -1
index 3e3e42bfd150..dde54d9ff01c 100644
@@ -42,7 +42,7 @@ struct hashtab_info {
  */
 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
 			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
-			       u32 size);
+			       u32 nel_hint);
 
 /*
  * Inserts the specified (key, datum) pair into the specified hash table.
security/selinux/ss/policydb.c +20 -33

113 lines not on this page · raw message

security/selinux/ss/policydb.h +0 -2
index 41ad78a1f17b..72e2932fb12d 100644
@@ -321,8 +321,6 @@ extern int policydb_role_isvalid(struct policydb *p, unsigned int role);
 extern int policydb_read(struct policydb *p, void *fp);
 extern int policydb_write(struct policydb *p, void *fp);
 
-#define PERM_SYMTAB_SIZE 32
-
 #define POLICYDB_CONFIG_MLS    1
 
 /* the config flags related to unknown classes/perms are bits 2 and 3 */
Signature · 1 line
2.24.1
Acked-by on v2 1/1
#1 lore raw
Quoted 12 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

Quoted 5 lines

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

maintainer
#2 lore raw
In reply to Ondrej Mosnacek
Quoted 38 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---

Changed in v2:
- guard against h->size == 0 in hashtab_search() and hashtab_insert()

 security/selinux/ss/hashtab.c  | 25 +++++++++++++---
 security/selinux/ss/hashtab.h  |  2 +-
 security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
 security/selinux/ss/policydb.h |  2 --
 4 files changed, 42 insertions(+), 40 deletions(-)
security/selinux/ss/hashtab.c +1 -0
index ebfdaa31ee32..87ad83148cbd 100644
@@ -12,12 +12,26 @@
 
 static struct kmem_cache *hashtab_node_cachep;
 
+static u32 hashtab_compute_size(u32 nel)

security/selinux/ss/hashtab.c line 25

Quoted 10 lines
security/selinux/ss/hashtab.c +10 -0
+{
+       u32 size;
+
+       if (nel <= 2)
+               return nel;
+
+       /* use the nearest power of two to balance size and access time */
+       size = roundup_pow_of_two(nel);
+       if (size - nel > size / 4)
+               size /= 2;

It would be nice if the commit description (and possibly the code itself via a shorter version in the comments) gave some insight into why you chose this particular adjustment to the hash table size. Was this based on observations with real world policies? Just a gut feeling? Things like this are the sort of thing that one wonders about five years later when modifying the code and by then no one can remember if it is important or not.

Also, considering the adjustment above, why not use rounddown_pow_of_two() instead (perhaps coupled with a minimum size check)?

Quoted 2 lines
security/selinux/ss/hashtab.c +2 -0
+       return size;
+}
Signature · 2 lines
paul moore
www.paul-moore.com
#3 lore raw
Quoted 58 lines · Paul Moore
Quoted 38 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---

Changed in v2:
- guard against h->size == 0 in hashtab_search() and hashtab_insert()

 security/selinux/ss/hashtab.c  | 25 +++++++++++++---
 security/selinux/ss/hashtab.h  |  2 +-
 security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
 security/selinux/ss/policydb.h |  2 --
 4 files changed, 42 insertions(+), 40 deletions(-)
security/selinux/ss/hashtab.c +1 -0
index ebfdaa31ee32..87ad83148cbd 100644
@@ -12,12 +12,26 @@
 
 static struct kmem_cache *hashtab_node_cachep;
 
+static u32 hashtab_compute_size(u32 nel)

security/selinux/ss/hashtab.c line 25

Quoted 10 lines
security/selinux/ss/hashtab.c +10 -0
+{
+       u32 size;
+
+       if (nel <= 2)
+               return nel;
+
+       /* use the nearest power of two to balance size and access time */
+       size = roundup_pow_of_two(nel);
+       if (size - nel > size / 4)
+               size /= 2;

It would be nice if the commit description (and possibly the code itself via a shorter version in the comments) gave some insight into why you chose this particular adjustment to the hash table size. Was this based on observations with real world policies? Just a gut feeling? Things like this are the sort of thing that one wonders about five years later when modifying the code and by then no one can remember if it is important or not.

Quoted 3 lines

Also, considering the adjustment above, why not use rounddown_pow_of_two() instead (perhaps coupled with a minimum size check)?

Good point. I think I was tuning this formula back when I haven't rebased this patch on top of the filename transition rework, so I suspect that the filename_trans hash table was what was causing high memory usage with plain roundup_pow_of_two()... With ~225k rules it should be taking up ~2 MB in such case, which is quite a lot. (In fact, even with the adjusted formula it would still allocate the same table, so my crude method of measuring the memory usage apparently wasn't reliable...) Now that this hash table is only at ~2500 elements even with our imperfect policy, it should only take up ~32 KB when rounding up, which is a pretty small overhead.

I'll re-test the patch with hashtab_compute_size() ~ roundup_pow_of_two(), this time logging the exact amount of bytes taken up by the hash table arrays (which is the only thing that is changing here). I expect it will show only a few 10s of KB difference, in which case I'll respin the patch with this simpler (and likely a bit faster) variant.

Quoted 3 lines
Quoted 3 lines
security/selinux/ss/hashtab.c +2 -0
+       return size;
+}
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.
maintainer
#4 lore raw
Quoted 74 lines · Ondrej Mosnacek
Quoted 58 lines · Paul Moore
Quoted 38 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---

Changed in v2:
- guard against h->size == 0 in hashtab_search() and hashtab_insert()

 security/selinux/ss/hashtab.c  | 25 +++++++++++++---
 security/selinux/ss/hashtab.h  |  2 +-
 security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
 security/selinux/ss/policydb.h |  2 --
 4 files changed, 42 insertions(+), 40 deletions(-)
security/selinux/ss/hashtab.c +1 -0
index ebfdaa31ee32..87ad83148cbd 100644
@@ -12,12 +12,26 @@
 
 static struct kmem_cache *hashtab_node_cachep;
 
+static u32 hashtab_compute_size(u32 nel)

security/selinux/ss/hashtab.c line 25

Quoted 10 lines
security/selinux/ss/hashtab.c +10 -0
+{
+       u32 size;
+
+       if (nel <= 2)
+               return nel;
+
+       /* use the nearest power of two to balance size and access time */
+       size = roundup_pow_of_two(nel);
+       if (size - nel > size / 4)
+               size /= 2;

It would be nice if the commit description (and possibly the code itself via a shorter version in the comments) gave some insight into why you chose this particular adjustment to the hash table size. Was this based on observations with real world policies? Just a gut feeling? Things like this are the sort of thing that one wonders about five years later when modifying the code and by then no one can remember if it is important or not.

Quoted 3 lines

Also, considering the adjustment above, why not use rounddown_pow_of_two() instead (perhaps coupled with a minimum size check)?

Good point. I think I was tuning this formula back when I haven't rebased this patch on top of the filename transition rework, so I suspect that the filename_trans hash table was what was causing high memory usage with plain roundup_pow_of_two()... With ~225k rules it should be taking up ~2 MB in such case, which is quite a lot. (In fact, even with the adjusted formula it would still allocate the same table, so my crude method of measuring the memory usage apparently wasn't reliable...) Now that this hash table is only at ~2500 elements even with our imperfect policy, it should only take up ~32 KB when rounding up, which is a pretty small overhead.

Quoted 6 lines

I'll re-test the patch with hashtab_compute_size() ~ roundup_pow_of_two(), this time logging the exact amount of bytes taken up by the hash table arrays (which is the only thing that is changing here). I expect it will show only a few 10s of KB difference, in which case I'll respin the patch with this simpler (and likely a bit faster) variant.

Thanks. I also want to stress that capturing the logic behind the decision in the patch description / comments is almost as important (maybe more so) than what we end up deciding on for the actual size algorithm. I can almost guarantee the decision made now is going to be brought up for discussion at some point in the future, so let's help the poor future devs as much as we can ;)

Signature · 2 lines
paul moore
www.paul-moore.com