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>
12 static struct kmem_cache *hashtab_node_cachep __ro_after_init;
15 * Here we simply round the number of elements up to the nearest power of two.
16 * I tried also other options like rounding down or rounding to the closest
17 * power of two (up or down based on which is closer), but I was unable to
18 * find any significant difference in lookup/insert performance that would
19 * justify switching to a different (less intuitive) formula. It could be that
20 * a different formula is actually more optimal, but any future changes here
21 * should be supported with performance/memory usage data.
23 * The total memory used by the htable arrays (only) with Fedora policy loaded
24 * is approximately 163 KB at the time of writing.
26 static u32 hashtab_compute_size(u32 nel)
28 return nel == 0 ? 0 : roundup_pow_of_two(nel);
31 int hashtab_init(struct hashtab *h, u32 nel_hint)
33 h->size = hashtab_compute_size(nel_hint);
38 h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
39 return h->htable ? 0 : -ENOMEM;
42 int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
43 void *key, void *datum)
45 struct hashtab_node *newnode;
47 newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
51 newnode->datum = datum;
59 void hashtab_destroy(struct hashtab *h)
62 struct hashtab_node *cur, *temp;
64 for (i = 0; i < h->size; i++) {
69 kmem_cache_free(hashtab_node_cachep, temp);
78 int hashtab_map(struct hashtab *h,
79 int (*apply)(void *k, void *d, void *args),
84 struct hashtab_node *cur;
86 for (i = 0; i < h->size; i++) {
89 ret = apply(cur->key, cur->datum, args);
99 void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
101 u32 i, chain_len, slots_used, max_chain_len;
102 struct hashtab_node *cur;
106 for (i = 0; i < h->size; i++) {
116 if (chain_len > max_chain_len)
117 max_chain_len = chain_len;
121 info->slots_used = slots_used;
122 info->max_chain_len = max_chain_len;
125 int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
126 int (*copy)(struct hashtab_node *new,
127 struct hashtab_node *orig, void *args),
128 int (*destroy)(void *k, void *d, void *args),
131 struct hashtab_node *cur, *tmp, *tail;
134 memset(new, 0, sizeof(*new));
136 new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
140 new->size = orig->size;
142 for (i = 0; i < orig->size; i++) {
144 for (cur = orig->htable[i]; cur; cur = cur->next) {
145 tmp = kmem_cache_zalloc(hashtab_node_cachep,
149 rc = copy(tmp, cur, args);
151 kmem_cache_free(hashtab_node_cachep, tmp);
156 new->htable[i] = tmp;
167 for (i = 0; i < new->size; i++) {
168 for (cur = new->htable[i]; cur; cur = tmp) {
170 destroy(cur->key, cur->datum, args);
171 kmem_cache_free(hashtab_node_cachep, cur);
174 kmem_cache_free(hashtab_node_cachep, new);
178 void __init hashtab_cache_init(void)
180 hashtab_node_cachep = kmem_cache_create("hashtab_node",
181 sizeof(struct hashtab_node),
182 0, SLAB_PANIC, NULL);