1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the hash table type.
5 * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
14 static struct kmem_cache *hashtab_node_cachep __ro_after_init;
17 * Here we simply round the number of elements up to the nearest power of two.
18 * I tried also other options like rounding down or rounding to the closest
19 * power of two (up or down based on which is closer), but I was unable to
20 * find any significant difference in lookup/insert performance that would
21 * justify switching to a different (less intuitive) formula. It could be that
22 * a different formula is actually more optimal, but any future changes here
23 * should be supported with performance/memory usage data.
25 * The total memory used by the htable arrays (only) with Fedora policy loaded
26 * is approximately 163 KB at the time of writing.
28 static u32 hashtab_compute_size(u32 nel)
30 return nel == 0 ? 0 : roundup_pow_of_two(nel);
33 int hashtab_init(struct hashtab *h, u32 nel_hint)
35 u32 size = hashtab_compute_size(nel_hint);
37 /* should already be zeroed, but better be safe */
43 h->htable = kcalloc(size, sizeof(*h->htable), GFP_KERNEL);
51 int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst, void *key,
54 struct hashtab_node *newnode;
56 newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
60 newnode->datum = datum;
68 void hashtab_destroy(struct hashtab *h)
71 struct hashtab_node *cur, *temp;
73 for (i = 0; i < h->size; i++) {
78 kmem_cache_free(hashtab_node_cachep, temp);
87 int hashtab_map(struct hashtab *h, 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), void *args)
144 struct hashtab_node *cur, *tmp, *tail;
148 memset(new, 0, sizeof(*new));
150 new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
154 new->size = orig->size;
156 for (i = 0; i < orig->size; i++) {
158 for (cur = orig->htable[i]; cur; cur = cur->next) {
159 tmp = kmem_cache_zalloc(hashtab_node_cachep,
163 rc = copy(tmp, cur, args);
165 kmem_cache_free(hashtab_node_cachep, tmp);
170 new->htable[i] = tmp;
181 for (i = 0; i < new->size; i++) {
182 for (cur = new->htable[i]; cur; cur = tmp) {
184 destroy(cur->key, cur->datum, args);
185 kmem_cache_free(hashtab_node_cachep, cur);
189 memset(new, 0, sizeof(*new));
193 void __init hashtab_cache_init(void)
195 hashtab_node_cachep = kmem_cache_create("hashtab_node",
196 sizeof(struct hashtab_node), 0,