1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the hash table type.
5 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
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);
107 void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
109 u32 i, chain_len, slots_used, max_chain_len;
110 struct hashtab_node *cur;
114 for (i = 0; i < h->size; i++) {
124 if (chain_len > max_chain_len)
125 max_chain_len = chain_len;
129 info->slots_used = slots_used;
130 info->max_chain_len = max_chain_len;
133 int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
134 int (*copy)(struct hashtab_node *new,
135 struct hashtab_node *orig, void *args),
136 int (*destroy)(void *k, void *d, void *args),
139 struct hashtab_node *cur, *tmp, *tail;
142 memset(new, 0, sizeof(*new));
144 new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
148 new->size = orig->size;
150 for (i = 0; i < orig->size; i++) {
152 for (cur = orig->htable[i]; cur; cur = cur->next) {
153 tmp = kmem_cache_zalloc(hashtab_node_cachep,
157 rc = copy(tmp, cur, args);
159 kmem_cache_free(hashtab_node_cachep, tmp);
164 new->htable[i] = tmp;
175 for (i = 0; i < new->size; i++) {
176 for (cur = new->htable[i]; cur; cur = tmp) {
178 destroy(cur->key, cur->datum, args);
179 kmem_cache_free(hashtab_node_cachep, cur);
183 memset(new, 0, sizeof(*new));
187 void __init hashtab_cache_init(void)
189 hashtab_node_cachep = kmem_cache_create("hashtab_node",
190 sizeof(struct hashtab_node),
191 0, SLAB_PANIC, NULL);