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;
15 * Here we simply round the number of elements up to the nearest power of two.
16 * I tried also other options like rouding 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 u32 size = hashtab_compute_size(nel_hint);
35 /* should already be zeroed, but better be safe */
41 h->htable = kcalloc(size, sizeof(*h->htable), GFP_KERNEL);
49 int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
50 void *key, void *datum)
52 struct hashtab_node *newnode;
54 newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
58 newnode->datum = datum;
66 void hashtab_destroy(struct hashtab *h)
69 struct hashtab_node *cur, *temp;
71 for (i = 0; i < h->size; i++) {
76 kmem_cache_free(hashtab_node_cachep, temp);
85 int hashtab_map(struct hashtab *h,
86 int (*apply)(void *k, void *d, void *args),
91 struct hashtab_node *cur;
93 for (i = 0; i < h->size; i++) {
96 ret = apply(cur->key, cur->datum, args);
106 void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
108 u32 i, chain_len, slots_used, max_chain_len;
109 struct hashtab_node *cur;
113 for (i = 0; i < h->size; i++) {
123 if (chain_len > max_chain_len)
124 max_chain_len = chain_len;
128 info->slots_used = slots_used;
129 info->max_chain_len = max_chain_len;
132 int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
133 int (*copy)(struct hashtab_node *new,
134 struct hashtab_node *orig, void *args),
135 int (*destroy)(void *k, void *d, void *args),
138 struct hashtab_node *cur, *tmp, *tail;
141 memset(new, 0, sizeof(*new));
143 new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
147 new->size = orig->size;
149 for (i = 0; i < orig->size; i++) {
151 for (cur = orig->htable[i]; cur; cur = cur->next) {
152 tmp = kmem_cache_zalloc(hashtab_node_cachep,
156 rc = copy(tmp, cur, args);
158 kmem_cache_free(hashtab_node_cachep, tmp);
163 new->htable[i] = tmp;
174 for (i = 0; i < new->size; i++) {
175 for (cur = new->htable[i]; cur; cur = tmp) {
177 destroy(cur->key, cur->datum, args);
178 kmem_cache_free(hashtab_node_cachep, cur);
182 memset(new, 0, sizeof(*new));
186 void __init hashtab_cache_init(void)
188 hashtab_node_cachep = kmem_cache_create("hashtab_node",
189 sizeof(struct hashtab_node),
190 0, SLAB_PANIC, NULL);