Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * Implementation of the hash table type.
  3 *
  4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5 */
 
  6#include <linux/kernel.h>
  7#include <linux/slab.h>
  8#include <linux/errno.h>
  9#include "hashtab.h"
 
 10
 11struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
 12			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
 13			       u32 size)
 14{
 15	struct hashtab *p;
 16	u32 i;
 17
 18	p = kzalloc(sizeof(*p), GFP_KERNEL);
 19	if (p == NULL)
 20		return p;
 21
 22	p->size = size;
 23	p->nel = 0;
 24	p->hash_value = hash_value;
 25	p->keycmp = keycmp;
 26	p->htable = kmalloc(sizeof(*(p->htable)) * size, GFP_KERNEL);
 27	if (p->htable == NULL) {
 28		kfree(p);
 29		return NULL;
 30	}
 31
 32	for (i = 0; i < size; i++)
 33		p->htable[i] = NULL;
 34
 35	return p;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 36}
 37
 38int hashtab_insert(struct hashtab *h, void *key, void *datum)
 39{
 40	u32 hvalue;
 41	struct hashtab_node *prev, *cur, *newnode;
 42
 43	if (!h || h->nel == HASHTAB_MAX_NODES)
 44		return -EINVAL;
 
 
 45
 46	hvalue = h->hash_value(h, key);
 47	prev = NULL;
 48	cur = h->htable[hvalue];
 49	while (cur && h->keycmp(h, key, cur->key) > 0) {
 50		prev = cur;
 51		cur = cur->next;
 52	}
 
 
 53
 54	if (cur && (h->keycmp(h, key, cur->key) == 0))
 55		return -EEXIST;
 
 
 56
 57	newnode = kzalloc(sizeof(*newnode), GFP_KERNEL);
 58	if (newnode == NULL)
 59		return -ENOMEM;
 60	newnode->key = key;
 61	newnode->datum = datum;
 62	if (prev) {
 63		newnode->next = prev->next;
 64		prev->next = newnode;
 65	} else {
 66		newnode->next = h->htable[hvalue];
 67		h->htable[hvalue] = newnode;
 68	}
 69
 70	h->nel++;
 71	return 0;
 72}
 73
 74void *hashtab_search(struct hashtab *h, const void *key)
 75{
 76	u32 hvalue;
 77	struct hashtab_node *cur;
 78
 79	if (!h)
 80		return NULL;
 81
 82	hvalue = h->hash_value(h, key);
 83	cur = h->htable[hvalue];
 84	while (cur && h->keycmp(h, key, cur->key) > 0)
 85		cur = cur->next;
 86
 87	if (cur == NULL || (h->keycmp(h, key, cur->key) != 0))
 88		return NULL;
 89
 90	return cur->datum;
 91}
 92
 93void hashtab_destroy(struct hashtab *h)
 94{
 95	u32 i;
 96	struct hashtab_node *cur, *temp;
 97
 98	if (!h)
 99		return;
100
101	for (i = 0; i < h->size; i++) {
102		cur = h->htable[i];
103		while (cur) {
104			temp = cur;
105			cur = cur->next;
106			kfree(temp);
107		}
108		h->htable[i] = NULL;
109	}
110
111	kfree(h->htable);
112	h->htable = NULL;
113
114	kfree(h);
115}
116
117int hashtab_map(struct hashtab *h,
118		int (*apply)(void *k, void *d, void *args),
119		void *args)
120{
121	u32 i;
122	int ret;
123	struct hashtab_node *cur;
124
125	if (!h)
126		return 0;
127
128	for (i = 0; i < h->size; i++) {
129		cur = h->htable[i];
130		while (cur) {
131			ret = apply(cur->key, cur->datum, args);
132			if (ret)
133				return ret;
134			cur = cur->next;
135		}
136	}
137	return 0;
138}
139
140
141void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
142{
143	u32 i, chain_len, slots_used, max_chain_len;
 
144	struct hashtab_node *cur;
145
146	slots_used = 0;
147	max_chain_len = 0;
148	for (slots_used = max_chain_len = i = 0; i < h->size; i++) {
 
149		cur = h->htable[i];
150		if (cur) {
151			slots_used++;
152			chain_len = 0;
153			while (cur) {
154				chain_len++;
155				cur = cur->next;
156			}
157
158			if (chain_len > max_chain_len)
159				max_chain_len = chain_len;
 
 
160		}
161	}
162
163	info->slots_used = slots_used;
164	info->max_chain_len = max_chain_len;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165}
v6.9.4
  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, 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)
143{
144	struct hashtab_node *cur, *tmp, *tail;
145	u32 i;
146	int rc;
147
148	memset(new, 0, sizeof(*new));
149
150	new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL);
151	if (!new->htable)
152		return -ENOMEM;
153
154	new->size = orig->size;
155
156	for (i = 0; i < orig->size; i++) {
157		tail = NULL;
158		for (cur = orig->htable[i]; cur; cur = cur->next) {
159			tmp = kmem_cache_zalloc(hashtab_node_cachep,
160						GFP_KERNEL);
161			if (!tmp)
162				goto error;
163			rc = copy(tmp, cur, args);
164			if (rc) {
165				kmem_cache_free(hashtab_node_cachep, tmp);
166				goto error;
167			}
168			tmp->next = NULL;
169			if (!tail)
170				new->htable[i] = tmp;
171			else
172				tail->next = tmp;
173			tail = tmp;
174			new->nel++;
175		}
176	}
177
178	return 0;
179
180error:
181	for (i = 0; i < new->size; i++) {
182		for (cur = new->htable[i]; cur; cur = tmp) {
183			tmp = cur->next;
184			destroy(cur->key, cur->datum, args);
185			kmem_cache_free(hashtab_node_cachep, cur);
186		}
187	}
188	kfree(new->htable);
189	memset(new, 0, sizeof(*new));
190	return -ENOMEM;
191}
192
193void __init hashtab_cache_init(void)
194{
195	hashtab_node_cachep = kmem_cache_create("hashtab_node",
196						sizeof(struct hashtab_node), 0,
197						SLAB_PANIC, NULL);
198}