1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the hash table type.
5 * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/errno.h>
13 static struct kmem_cache *hashtab_node_cachep __ro_after_init;
16 * Here we simply round the number of elements up to the nearest power of two.
17 * I tried also other options like rounding down or rounding to the closest
18 * power of two (up or down based on which is closer), but I was unable to
19 * find any significant difference in lookup/insert performance that would
20 * justify switching to a different (less intuitive) formula. It could be that
21 * a different formula is actually more optimal, but any future changes here
22 * should be supported with performance/memory usage data.
24 * The total memory used by the htable arrays (only) with Fedora policy loaded
25 * is approximately 163 KB at the time of writing.
27 static u32 hashtab_compute_size(u32 nel)
29 return nel == 0 ? 0 : roundup_pow_of_two(nel);
32 int hashtab_init(struct hashtab *h, u32 nel_hint)
34 u32 size = hashtab_compute_size(nel_hint);
36 /* should already be zeroed, but better be safe */
42 h->htable = kcalloc(size, sizeof(*h->htable), GFP_KERNEL);
50 int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
51 void *key, void *datum)
53 struct hashtab_node *newnode;
55 newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
59 newnode->datum = datum;
67 void hashtab_destroy(struct hashtab *h)
70 struct hashtab_node *cur, *temp;
72 for (i = 0; i < h->size; i++) {
77 kmem_cache_free(hashtab_node_cachep, temp);
86 int hashtab_map(struct hashtab *h,
87 int (*apply)(void *k, void *d, void *args),
92 struct hashtab_node *cur;
94 for (i = 0; i < h->size; i++) {
97 ret = apply(cur->key, cur->datum, args);
106 #ifdef CONFIG_SECURITY_SELINUX_DEBUG
107 void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
109 u32 i, chain_len, slots_used, max_chain_len;
111 struct hashtab_node *cur;
116 for (i = 0; i < h->size; i++) {
126 if (chain_len > max_chain_len)
127 max_chain_len = chain_len;
129 chain2_len_sum += (u64)chain_len * chain_len;
133 info->slots_used = slots_used;
134 info->max_chain_len = max_chain_len;
135 info->chain2_len_sum = chain2_len_sum;
137 #endif /* CONFIG_SECURITY_SELINUX_DEBUG */
139 int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
140 int (*copy)(struct hashtab_node *new,
141 struct hashtab_node *orig, void *args),
142 int (*destroy)(void *k, void *d, void *args),
145 struct hashtab_node *cur, *tmp, *tail;
149 memset(new, 0, sizeof(*new));
151 new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
155 new->size = orig->size;
157 for (i = 0; i < orig->size; i++) {
159 for (cur = orig->htable[i]; cur; cur = cur->next) {
160 tmp = kmem_cache_zalloc(hashtab_node_cachep,
164 rc = copy(tmp, cur, args);
166 kmem_cache_free(hashtab_node_cachep, tmp);
171 new->htable[i] = tmp;
182 for (i = 0; i < new->size; i++) {
183 for (cur = new->htable[i]; cur; cur = tmp) {
185 destroy(cur->key, cur->datum, args);
186 kmem_cache_free(hashtab_node_cachep, cur);
190 memset(new, 0, sizeof(*new));
194 void __init hashtab_cache_init(void)
196 hashtab_node_cachep = kmem_cache_create("hashtab_node",
197 sizeof(struct hashtab_node),
198 0, SLAB_PANIC, NULL);