forked from Minki/linux
54b27f9287
Move (most of) the definitions of hashtab_search() and hashtab_insert() to the header file. In combination with the previous patch, this avoids calling the callbacks indirectly by function pointers and allows for better optimization, leading to a drastic performance improvement of these operations. With this patch, I measured a speed up in the following areas (measured on x86_64 F32 VM with 4 CPUs): 1. Policy load (`load_policy`) - takes ~150 ms instead of ~230 ms. 2. `chcon -R unconfined_u:object_r:user_tmp_t:s0:c381,c519 /tmp/linux-src` where /tmp/linux-src is an extracted linux-5.7 source tarball - takes ~522 ms instead of ~576 ms. This is because of many symtab_search() calls in string_to_context_struct() when there are many categories specified in the context. 3. `stress-ng --msg 1 --msg-ops 10000000` - takes 12.41 s instead of 13.95 s (consumes 18.6 s of kernel CPU time instead of 21.6 s). This is thanks to security_transition_sid() being ~43% faster after this patch. Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com> Acked-by: Stephen Smalley <stephen.smalley.work@gmail.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
131 lines
2.8 KiB
C
131 lines
2.8 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Implementation of the hash table type.
|
|
*
|
|
* Author : Stephen Smalley, <sds@tycho.nsa.gov>
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/errno.h>
|
|
#include "hashtab.h"
|
|
|
|
static struct kmem_cache *hashtab_node_cachep;
|
|
|
|
/*
|
|
* Here we simply round the number of elements up to the nearest power of two.
|
|
* I tried also other options like rouding down or rounding to the closest
|
|
* power of two (up or down based on which is closer), but I was unable to
|
|
* find any significant difference in lookup/insert performance that would
|
|
* justify switching to a different (less intuitive) formula. It could be that
|
|
* a different formula is actually more optimal, but any future changes here
|
|
* should be supported with performance/memory usage data.
|
|
*
|
|
* The total memory used by the htable arrays (only) with Fedora policy loaded
|
|
* is approximately 163 KB at the time of writing.
|
|
*/
|
|
static u32 hashtab_compute_size(u32 nel)
|
|
{
|
|
return nel == 0 ? 0 : roundup_pow_of_two(nel);
|
|
}
|
|
|
|
int hashtab_init(struct hashtab *h, u32 nel_hint)
|
|
{
|
|
h->size = hashtab_compute_size(nel_hint);
|
|
h->nel = 0;
|
|
if (!h->size)
|
|
return 0;
|
|
|
|
h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
|
|
return h->htable ? 0 : -ENOMEM;
|
|
}
|
|
|
|
int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
|
|
void *key, void *datum)
|
|
{
|
|
struct hashtab_node *newnode;
|
|
|
|
newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
|
|
if (!newnode)
|
|
return -ENOMEM;
|
|
newnode->key = key;
|
|
newnode->datum = datum;
|
|
newnode->next = *dst;
|
|
*dst = newnode;
|
|
|
|
h->nel++;
|
|
return 0;
|
|
}
|
|
|
|
void hashtab_destroy(struct hashtab *h)
|
|
{
|
|
u32 i;
|
|
struct hashtab_node *cur, *temp;
|
|
|
|
for (i = 0; i < h->size; i++) {
|
|
cur = h->htable[i];
|
|
while (cur) {
|
|
temp = cur;
|
|
cur = cur->next;
|
|
kmem_cache_free(hashtab_node_cachep, temp);
|
|
}
|
|
h->htable[i] = NULL;
|
|
}
|
|
|
|
kfree(h->htable);
|
|
h->htable = NULL;
|
|
}
|
|
|
|
int hashtab_map(struct hashtab *h,
|
|
int (*apply)(void *k, void *d, void *args),
|
|
void *args)
|
|
{
|
|
u32 i;
|
|
int ret;
|
|
struct hashtab_node *cur;
|
|
|
|
for (i = 0; i < h->size; i++) {
|
|
cur = h->htable[i];
|
|
while (cur) {
|
|
ret = apply(cur->key, cur->datum, args);
|
|
if (ret)
|
|
return ret;
|
|
cur = cur->next;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
|
|
{
|
|
u32 i, chain_len, slots_used, max_chain_len;
|
|
struct hashtab_node *cur;
|
|
|
|
slots_used = 0;
|
|
max_chain_len = 0;
|
|
for (i = 0; i < h->size; i++) {
|
|
cur = h->htable[i];
|
|
if (cur) {
|
|
slots_used++;
|
|
chain_len = 0;
|
|
while (cur) {
|
|
chain_len++;
|
|
cur = cur->next;
|
|
}
|
|
|
|
if (chain_len > max_chain_len)
|
|
max_chain_len = chain_len;
|
|
}
|
|
}
|
|
|
|
info->slots_used = slots_used;
|
|
info->max_chain_len = max_chain_len;
|
|
}
|
|
|
|
void __init hashtab_cache_init(void)
|
|
{
|
|
hashtab_node_cachep = kmem_cache_create("hashtab_node",
|
|
sizeof(struct hashtab_node),
|
|
0, SLAB_PANIC, NULL);
|
|
}
|