xref: /openbmc/linux/security/selinux/ss/hashtab.c (revision e9fd7292)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Implementation of the hash table type.
41da177e4SLinus Torvalds  *
57efbb60bSStephen Smalley  * Author : Stephen Smalley, <sds@tycho.nsa.gov>
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds #include <linux/kernel.h>
81da177e4SLinus Torvalds #include <linux/slab.h>
91da177e4SLinus Torvalds #include <linux/errno.h>
101da177e4SLinus Torvalds #include "hashtab.h"
11*e9fd7292SPaul Moore #include "security.h"
121da177e4SLinus Torvalds 
13cd2bb4cbSOndrej Mosnacek static struct kmem_cache *hashtab_node_cachep __ro_after_init;
147c620eceSKyeongdon Kim 
15e0ac568dSOndrej Mosnacek /*
16e0ac568dSOndrej Mosnacek  * Here we simply round the number of elements up to the nearest power of two.
1763ddf1baSXiong Zhenwu  * I tried also other options like rounding down or rounding to the closest
18e0ac568dSOndrej Mosnacek  * power of two (up or down based on which is closer), but I was unable to
19e0ac568dSOndrej Mosnacek  * find any significant difference in lookup/insert performance that would
20e0ac568dSOndrej Mosnacek  * justify switching to a different (less intuitive) formula. It could be that
21e0ac568dSOndrej Mosnacek  * a different formula is actually more optimal, but any future changes here
22e0ac568dSOndrej Mosnacek  * should be supported with performance/memory usage data.
23e0ac568dSOndrej Mosnacek  *
24e0ac568dSOndrej Mosnacek  * The total memory used by the htable arrays (only) with Fedora policy loaded
25e0ac568dSOndrej Mosnacek  * is approximately 163 KB at the time of writing.
26e0ac568dSOndrej Mosnacek  */
27e0ac568dSOndrej Mosnacek static u32 hashtab_compute_size(u32 nel)
28e0ac568dSOndrej Mosnacek {
29e0ac568dSOndrej Mosnacek 	return nel == 0 ? 0 : roundup_pow_of_two(nel);
30e0ac568dSOndrej Mosnacek }
31e0ac568dSOndrej Mosnacek 
3224def7bbSOndrej Mosnacek int hashtab_init(struct hashtab *h, u32 nel_hint)
331da177e4SLinus Torvalds {
3403414a49SOndrej Mosnacek 	h->size = hashtab_compute_size(nel_hint);
3503414a49SOndrej Mosnacek 	h->nel = 0;
3603414a49SOndrej Mosnacek 	if (!h->size)
3703414a49SOndrej Mosnacek 		return 0;
381da177e4SLinus Torvalds 
3903414a49SOndrej Mosnacek 	h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
4003414a49SOndrej Mosnacek 	return h->htable ? 0 : -ENOMEM;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
4354b27f92SOndrej Mosnacek int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
4454b27f92SOndrej Mosnacek 		     void *key, void *datum)
451da177e4SLinus Torvalds {
4654b27f92SOndrej Mosnacek 	struct hashtab_node *newnode;
471da177e4SLinus Torvalds 
487c620eceSKyeongdon Kim 	newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
49cb8d21e3SMarkus Elfring 	if (!newnode)
501da177e4SLinus Torvalds 		return -ENOMEM;
511da177e4SLinus Torvalds 	newnode->key = key;
521da177e4SLinus Torvalds 	newnode->datum = datum;
5354b27f92SOndrej Mosnacek 	newnode->next = *dst;
5454b27f92SOndrej Mosnacek 	*dst = newnode;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	h->nel++;
571da177e4SLinus Torvalds 	return 0;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds void hashtab_destroy(struct hashtab *h)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	u32 i;
631da177e4SLinus Torvalds 	struct hashtab_node *cur, *temp;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	for (i = 0; i < h->size; i++) {
661da177e4SLinus Torvalds 		cur = h->htable[i];
67dbc74c65SVesa-Matti Kari 		while (cur) {
681da177e4SLinus Torvalds 			temp = cur;
691da177e4SLinus Torvalds 			cur = cur->next;
707c620eceSKyeongdon Kim 			kmem_cache_free(hashtab_node_cachep, temp);
711da177e4SLinus Torvalds 		}
721da177e4SLinus Torvalds 		h->htable[i] = NULL;
731da177e4SLinus Torvalds 	}
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	kfree(h->htable);
761da177e4SLinus Torvalds 	h->htable = NULL;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds int hashtab_map(struct hashtab *h,
801da177e4SLinus Torvalds 		int (*apply)(void *k, void *d, void *args),
811da177e4SLinus Torvalds 		void *args)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	u32 i;
841da177e4SLinus Torvalds 	int ret;
851da177e4SLinus Torvalds 	struct hashtab_node *cur;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	for (i = 0; i < h->size; i++) {
881da177e4SLinus Torvalds 		cur = h->htable[i];
89dbc74c65SVesa-Matti Kari 		while (cur) {
901da177e4SLinus Torvalds 			ret = apply(cur->key, cur->datum, args);
911da177e4SLinus Torvalds 			if (ret)
921da177e4SLinus Torvalds 				return ret;
931da177e4SLinus Torvalds 			cur = cur->next;
941da177e4SLinus Torvalds 		}
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 	return 0;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	u32 i, chain_len, slots_used, max_chain_len;
1031da177e4SLinus Torvalds 	struct hashtab_node *cur;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	slots_used = 0;
1061da177e4SLinus Torvalds 	max_chain_len = 0;
1075794ed76SColin Ian King 	for (i = 0; i < h->size; i++) {
1081da177e4SLinus Torvalds 		cur = h->htable[i];
1091da177e4SLinus Torvalds 		if (cur) {
1101da177e4SLinus Torvalds 			slots_used++;
1111da177e4SLinus Torvalds 			chain_len = 0;
1121da177e4SLinus Torvalds 			while (cur) {
1131da177e4SLinus Torvalds 				chain_len++;
1141da177e4SLinus Torvalds 				cur = cur->next;
1151da177e4SLinus Torvalds 			}
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 			if (chain_len > max_chain_len)
1181da177e4SLinus Torvalds 				max_chain_len = chain_len;
1191da177e4SLinus Torvalds 		}
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	info->slots_used = slots_used;
1231da177e4SLinus Torvalds 	info->max_chain_len = max_chain_len;
1241da177e4SLinus Torvalds }
125aa8e712cSStephen Smalley 
126c7c556f1SStephen Smalley int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
127c7c556f1SStephen Smalley 		int (*copy)(struct hashtab_node *new,
128c7c556f1SStephen Smalley 			struct hashtab_node *orig, void *args),
129c7c556f1SStephen Smalley 		int (*destroy)(void *k, void *d, void *args),
130c7c556f1SStephen Smalley 		void *args)
131c7c556f1SStephen Smalley {
132c7c556f1SStephen Smalley 	struct hashtab_node *cur, *tmp, *tail;
133c7c556f1SStephen Smalley 	int i, rc;
134c7c556f1SStephen Smalley 
135c7c556f1SStephen Smalley 	memset(new, 0, sizeof(*new));
136c7c556f1SStephen Smalley 
137c7c556f1SStephen Smalley 	new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
138c7c556f1SStephen Smalley 	if (!new->htable)
139c7c556f1SStephen Smalley 		return -ENOMEM;
140c7c556f1SStephen Smalley 
141c7c556f1SStephen Smalley 	new->size = orig->size;
142c7c556f1SStephen Smalley 
143c7c556f1SStephen Smalley 	for (i = 0; i < orig->size; i++) {
144c7c556f1SStephen Smalley 		tail = NULL;
145c7c556f1SStephen Smalley 		for (cur = orig->htable[i]; cur; cur = cur->next) {
146c7c556f1SStephen Smalley 			tmp = kmem_cache_zalloc(hashtab_node_cachep,
147c7c556f1SStephen Smalley 						GFP_KERNEL);
148c7c556f1SStephen Smalley 			if (!tmp)
149c7c556f1SStephen Smalley 				goto error;
150c7c556f1SStephen Smalley 			rc = copy(tmp, cur, args);
151c7c556f1SStephen Smalley 			if (rc) {
152c7c556f1SStephen Smalley 				kmem_cache_free(hashtab_node_cachep, tmp);
153c7c556f1SStephen Smalley 				goto error;
154c7c556f1SStephen Smalley 			}
155c7c556f1SStephen Smalley 			tmp->next = NULL;
156c7c556f1SStephen Smalley 			if (!tail)
157c7c556f1SStephen Smalley 				new->htable[i] = tmp;
158c7c556f1SStephen Smalley 			else
159c7c556f1SStephen Smalley 				tail->next = tmp;
160c7c556f1SStephen Smalley 			tail = tmp;
161c7c556f1SStephen Smalley 			new->nel++;
162c7c556f1SStephen Smalley 		}
163c7c556f1SStephen Smalley 	}
164c7c556f1SStephen Smalley 
165c7c556f1SStephen Smalley 	return 0;
166c7c556f1SStephen Smalley 
167c7c556f1SStephen Smalley  error:
168c7c556f1SStephen Smalley 	for (i = 0; i < new->size; i++) {
169c7c556f1SStephen Smalley 		for (cur = new->htable[i]; cur; cur = tmp) {
170c7c556f1SStephen Smalley 			tmp = cur->next;
171c7c556f1SStephen Smalley 			destroy(cur->key, cur->datum, args);
172c7c556f1SStephen Smalley 			kmem_cache_free(hashtab_node_cachep, cur);
173c7c556f1SStephen Smalley 		}
174c7c556f1SStephen Smalley 	}
175c7c556f1SStephen Smalley 	kmem_cache_free(hashtab_node_cachep, new);
176c7c556f1SStephen Smalley 	return -ENOMEM;
177c7c556f1SStephen Smalley }
178c7c556f1SStephen Smalley 
179aa8e712cSStephen Smalley void __init hashtab_cache_init(void)
1807c620eceSKyeongdon Kim {
1817c620eceSKyeongdon Kim 		hashtab_node_cachep = kmem_cache_create("hashtab_node",
1827c620eceSKyeongdon Kim 			sizeof(struct hashtab_node),
1837c620eceSKyeongdon Kim 			0, SLAB_PANIC, NULL);
1847c620eceSKyeongdon Kim }
185