Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Implementation of the hash table type.
4 *
5 * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
6 */
7
8#include <linux/kernel.h>
9#include <linux/slab.h>
10#include <linux/errno.h>
11#include "hashtab.h"
12#include "security.h"
13
14static struct kmem_cache *hashtab_node_cachep __ro_after_init;
15
16/*
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.
24 *
25 * The total memory used by the htable arrays (only) with Fedora policy loaded
26 * is approximately 163 KB at the time of writing.
27 */
28static u32 hashtab_compute_size(u32 nel)
29{
30 return nel == 0 ? 0 : roundup_pow_of_two(nel);
31}
32
33int hashtab_init(struct hashtab *h, u32 nel_hint)
34{
35 u32 size = hashtab_compute_size(nel_hint);
36
37 /* should already be zeroed, but better be safe */
38 h->nel = 0;
39 h->size = 0;
40 h->htable = NULL;
41
42 if (size) {
43 h->htable = kcalloc(size, sizeof(*h->htable), GFP_KERNEL);
44 if (!h->htable)
45 return -ENOMEM;
46 h->size = size;
47 }
48 return 0;
49}
50
51int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst, void *key,
52 void *datum)
53{
54 struct hashtab_node *newnode;
55
56 newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
57 if (!newnode)
58 return -ENOMEM;
59 newnode->key = key;
60 newnode->datum = datum;
61 newnode->next = *dst;
62 *dst = newnode;
63
64 h->nel++;
65 return 0;
66}
67
68void hashtab_destroy(struct hashtab *h)
69{
70 u32 i;
71 struct hashtab_node *cur, *temp;
72
73 for (i = 0; i < h->size; i++) {
74 cur = h->htable[i];
75 while (cur) {
76 temp = cur;
77 cur = cur->next;
78 kmem_cache_free(hashtab_node_cachep, temp);
79 }
80 h->htable[i] = NULL;
81 }
82
83 kfree(h->htable);
84 h->htable = NULL;
85}
86
87int hashtab_map(struct hashtab *h, int (*apply)(void *k, void *d, void *args),
88 void *args)
89{
90 u32 i;
91 int ret;
92 struct hashtab_node *cur;
93
94 for (i = 0; i < h->size; i++) {
95 cur = h->htable[i];
96 while (cur) {
97 ret = apply(cur->key, cur->datum, args);
98 if (ret)
99 return ret;
100 cur = cur->next;
101 }
102 }
103 return 0;
104}
105
106#ifdef CONFIG_SECURITY_SELINUX_DEBUG
107void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
108{
109 u32 i, chain_len, slots_used, max_chain_len;
110 u64 chain2_len_sum;
111 struct hashtab_node *cur;
112
113 slots_used = 0;
114 max_chain_len = 0;
115 chain2_len_sum = 0;
116 for (i = 0; i < h->size; i++) {
117 cur = h->htable[i];
118 if (cur) {
119 slots_used++;
120 chain_len = 0;
121 while (cur) {
122 chain_len++;
123 cur = cur->next;
124 }
125
126 if (chain_len > max_chain_len)
127 max_chain_len = chain_len;
128
129 chain2_len_sum += (u64)chain_len * chain_len;
130 }
131 }
132
133 info->slots_used = slots_used;
134 info->max_chain_len = max_chain_len;
135 info->chain2_len_sum = chain2_len_sum;
136}
137#endif /* CONFIG_SECURITY_SELINUX_DEBUG */
138
139int hashtab_duplicate(struct hashtab *new, const struct hashtab *orig,
140 int (*copy)(struct hashtab_node *new,
141 const struct hashtab_node *orig, void *args),
142 int (*destroy)(void *k, void *d, void *args), void *args)
143{
144 const struct hashtab_node *orig_cur;
145 struct hashtab_node *cur, *tmp, *tail;
146 u32 i;
147 int rc;
148
149 memset(new, 0, sizeof(*new));
150
151 new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
152 if (!new->htable)
153 return -ENOMEM;
154
155 new->size = orig->size;
156
157 for (i = 0; i < orig->size; i++) {
158 tail = NULL;
159 for (orig_cur = orig->htable[i]; orig_cur;
160 orig_cur = orig_cur->next) {
161 tmp = kmem_cache_zalloc(hashtab_node_cachep,
162 GFP_KERNEL);
163 if (!tmp)
164 goto error;
165 rc = copy(tmp, orig_cur, args);
166 if (rc) {
167 kmem_cache_free(hashtab_node_cachep, tmp);
168 goto error;
169 }
170 tmp->next = NULL;
171 if (!tail)
172 new->htable[i] = tmp;
173 else
174 tail->next = tmp;
175 tail = tmp;
176 new->nel++;
177 }
178 }
179
180 return 0;
181
182error:
183 for (i = 0; i < new->size; i++) {
184 for (cur = new->htable[i]; cur; cur = tmp) {
185 tmp = cur->next;
186 destroy(cur->key, cur->datum, args);
187 kmem_cache_free(hashtab_node_cachep, cur);
188 }
189 }
190 kfree(new->htable);
191 memset(new, 0, sizeof(*new));
192 return -ENOMEM;
193}
194
195void __init hashtab_cache_init(void)
196{
197 hashtab_node_cachep = KMEM_CACHE(hashtab_node, SLAB_PANIC);
198}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Implementation of the hash table type.
4 *
5 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
6 */
7#include <linux/kernel.h>
8#include <linux/slab.h>
9#include <linux/errno.h>
10#include "hashtab.h"
11
12static struct kmem_cache *hashtab_node_cachep __ro_after_init;
13
14/*
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.
22 *
23 * The total memory used by the htable arrays (only) with Fedora policy loaded
24 * is approximately 163 KB at the time of writing.
25 */
26static u32 hashtab_compute_size(u32 nel)
27{
28 return nel == 0 ? 0 : roundup_pow_of_two(nel);
29}
30
31int hashtab_init(struct hashtab *h, u32 nel_hint)
32{
33 h->size = hashtab_compute_size(nel_hint);
34 h->nel = 0;
35 if (!h->size)
36 return 0;
37
38 h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
39 return h->htable ? 0 : -ENOMEM;
40}
41
42int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
43 void *key, void *datum)
44{
45 struct hashtab_node *newnode;
46
47 newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
48 if (!newnode)
49 return -ENOMEM;
50 newnode->key = key;
51 newnode->datum = datum;
52 newnode->next = *dst;
53 *dst = newnode;
54
55 h->nel++;
56 return 0;
57}
58
59void hashtab_destroy(struct hashtab *h)
60{
61 u32 i;
62 struct hashtab_node *cur, *temp;
63
64 for (i = 0; i < h->size; i++) {
65 cur = h->htable[i];
66 while (cur) {
67 temp = cur;
68 cur = cur->next;
69 kmem_cache_free(hashtab_node_cachep, temp);
70 }
71 h->htable[i] = NULL;
72 }
73
74 kfree(h->htable);
75 h->htable = NULL;
76}
77
78int hashtab_map(struct hashtab *h,
79 int (*apply)(void *k, void *d, void *args),
80 void *args)
81{
82 u32 i;
83 int ret;
84 struct hashtab_node *cur;
85
86 for (i = 0; i < h->size; i++) {
87 cur = h->htable[i];
88 while (cur) {
89 ret = apply(cur->key, cur->datum, args);
90 if (ret)
91 return ret;
92 cur = cur->next;
93 }
94 }
95 return 0;
96}
97
98
99void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
100{
101 u32 i, chain_len, slots_used, max_chain_len;
102 struct hashtab_node *cur;
103
104 slots_used = 0;
105 max_chain_len = 0;
106 for (i = 0; i < h->size; i++) {
107 cur = h->htable[i];
108 if (cur) {
109 slots_used++;
110 chain_len = 0;
111 while (cur) {
112 chain_len++;
113 cur = cur->next;
114 }
115
116 if (chain_len > max_chain_len)
117 max_chain_len = chain_len;
118 }
119 }
120
121 info->slots_used = slots_used;
122 info->max_chain_len = max_chain_len;
123}
124
125int 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),
129 void *args)
130{
131 struct hashtab_node *cur, *tmp, *tail;
132 int i, rc;
133
134 memset(new, 0, sizeof(*new));
135
136 new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
137 if (!new->htable)
138 return -ENOMEM;
139
140 new->size = orig->size;
141
142 for (i = 0; i < orig->size; i++) {
143 tail = NULL;
144 for (cur = orig->htable[i]; cur; cur = cur->next) {
145 tmp = kmem_cache_zalloc(hashtab_node_cachep,
146 GFP_KERNEL);
147 if (!tmp)
148 goto error;
149 rc = copy(tmp, cur, args);
150 if (rc) {
151 kmem_cache_free(hashtab_node_cachep, tmp);
152 goto error;
153 }
154 tmp->next = NULL;
155 if (!tail)
156 new->htable[i] = tmp;
157 else
158 tail->next = tmp;
159 tail = tmp;
160 new->nel++;
161 }
162 }
163
164 return 0;
165
166 error:
167 for (i = 0; i < new->size; i++) {
168 for (cur = new->htable[i]; cur; cur = tmp) {
169 tmp = cur->next;
170 destroy(cur->key, cur->datum, args);
171 kmem_cache_free(hashtab_node_cachep, cur);
172 }
173 }
174 kmem_cache_free(hashtab_node_cachep, new);
175 return -ENOMEM;
176}
177
178void __init hashtab_cache_init(void)
179{
180 hashtab_node_cachep = kmem_cache_create("hashtab_node",
181 sizeof(struct hashtab_node),
182 0, SLAB_PANIC, NULL);
183}