Linux Audio

Check our new training course

Loading...
v4.6
 
  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 <linux/sched.h>
 10#include "hashtab.h"
 
 11
 12struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
 13			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
 14			       u32 size)
 15{
 16	struct hashtab *p;
 17	u32 i;
 18
 19	p = kzalloc(sizeof(*p), GFP_KERNEL);
 20	if (p == NULL)
 21		return p;
 22
 23	p->size = size;
 24	p->nel = 0;
 25	p->hash_value = hash_value;
 26	p->keycmp = keycmp;
 27	p->htable = kmalloc(sizeof(*(p->htable)) * size, GFP_KERNEL);
 28	if (p->htable == NULL) {
 29		kfree(p);
 30		return NULL;
 31	}
 32
 33	for (i = 0; i < size; i++)
 34		p->htable[i] = NULL;
 35
 36	return p;
 37}
 38
 39int hashtab_insert(struct hashtab *h, void *key, void *datum)
 40{
 41	u32 hvalue;
 42	struct hashtab_node *prev, *cur, *newnode;
 43
 44	cond_resched();
 45
 46	if (!h || h->nel == HASHTAB_MAX_NODES)
 47		return -EINVAL;
 48
 49	hvalue = h->hash_value(h, key);
 50	prev = NULL;
 51	cur = h->htable[hvalue];
 52	while (cur && h->keycmp(h, key, cur->key) > 0) {
 53		prev = cur;
 54		cur = cur->next;
 55	}
 
 
 56
 57	if (cur && (h->keycmp(h, key, cur->key) == 0))
 58		return -EEXIST;
 
 
 59
 60	newnode = kzalloc(sizeof(*newnode), GFP_KERNEL);
 61	if (newnode == NULL)
 62		return -ENOMEM;
 63	newnode->key = key;
 64	newnode->datum = datum;
 65	if (prev) {
 66		newnode->next = prev->next;
 67		prev->next = newnode;
 68	} else {
 69		newnode->next = h->htable[hvalue];
 70		h->htable[hvalue] = newnode;
 71	}
 72
 73	h->nel++;
 74	return 0;
 75}
 76
 77void *hashtab_search(struct hashtab *h, const void *key)
 78{
 79	u32 hvalue;
 80	struct hashtab_node *cur;
 81
 82	if (!h)
 83		return NULL;
 84
 85	hvalue = h->hash_value(h, key);
 86	cur = h->htable[hvalue];
 87	while (cur && h->keycmp(h, key, cur->key) > 0)
 88		cur = cur->next;
 89
 90	if (cur == NULL || (h->keycmp(h, key, cur->key) != 0))
 91		return NULL;
 92
 93	return cur->datum;
 94}
 95
 96void hashtab_destroy(struct hashtab *h)
 97{
 98	u32 i;
 99	struct hashtab_node *cur, *temp;
100
101	if (!h)
102		return;
103
104	for (i = 0; i < h->size; i++) {
105		cur = h->htable[i];
106		while (cur) {
107			temp = cur;
108			cur = cur->next;
109			kfree(temp);
110		}
111		h->htable[i] = NULL;
112	}
113
114	kfree(h->htable);
115	h->htable = NULL;
116
117	kfree(h);
118}
119
120int hashtab_map(struct hashtab *h,
121		int (*apply)(void *k, void *d, void *args),
122		void *args)
123{
124	u32 i;
125	int ret;
126	struct hashtab_node *cur;
127
128	if (!h)
129		return 0;
130
131	for (i = 0; i < h->size; i++) {
132		cur = h->htable[i];
133		while (cur) {
134			ret = apply(cur->key, cur->datum, args);
135			if (ret)
136				return ret;
137			cur = cur->next;
138		}
139	}
140	return 0;
141}
142
143
144void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
145{
146	u32 i, chain_len, slots_used, max_chain_len;
 
147	struct hashtab_node *cur;
148
149	slots_used = 0;
150	max_chain_len = 0;
151	for (slots_used = max_chain_len = i = 0; i < h->size; i++) {
 
152		cur = h->htable[i];
153		if (cur) {
154			slots_used++;
155			chain_len = 0;
156			while (cur) {
157				chain_len++;
158				cur = cur->next;
159			}
160
161			if (chain_len > max_chain_len)
162				max_chain_len = chain_len;
 
 
163		}
164	}
165
166	info->slots_used = slots_used;
167	info->max_chain_len = max_chain_len;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168}
v6.8
  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#include <linux/kernel.h>
  8#include <linux/slab.h>
  9#include <linux/errno.h>
 
 10#include "hashtab.h"
 11#include "security.h"
 12
 13static struct kmem_cache *hashtab_node_cachep __ro_after_init;
 
 
 
 
 
 14
 15/*
 16 * Here we simply round the number of elements up to the nearest power of two.
 17 * I tried also other options like rounding down or rounding to the closest
 18 * power of two (up or down based on which is closer), but I was unable to
 19 * find any significant difference in lookup/insert performance that would
 20 * justify switching to a different (less intuitive) formula. It could be that
 21 * a different formula is actually more optimal, but any future changes here
 22 * should be supported with performance/memory usage data.
 23 *
 24 * The total memory used by the htable arrays (only) with Fedora policy loaded
 25 * is approximately 163 KB at the time of writing.
 26 */
 27static u32 hashtab_compute_size(u32 nel)
 28{
 29	return nel == 0 ? 0 : roundup_pow_of_two(nel);
 
 
 
 30}
 31
 32int hashtab_init(struct hashtab *h, u32 nel_hint)
 33{
 34	u32 size = hashtab_compute_size(nel_hint);
 
 35
 36	/* should already be zeroed, but better be safe */
 37	h->nel = 0;
 38	h->size = 0;
 39	h->htable = NULL;
 40
 41	if (size) {
 42		h->htable = kcalloc(size, sizeof(*h->htable), GFP_KERNEL);
 43		if (!h->htable)
 44			return -ENOMEM;
 45		h->size = size;
 
 46	}
 47	return 0;
 48}
 49
 50int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
 51		     void *key, void *datum)
 52{
 53	struct hashtab_node *newnode;
 54
 55	newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
 56	if (!newnode)
 57		return -ENOMEM;
 58	newnode->key = key;
 59	newnode->datum = datum;
 60	newnode->next = *dst;
 61	*dst = newnode;
 
 
 
 
 
 62
 63	h->nel++;
 64	return 0;
 65}
 66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 67void hashtab_destroy(struct hashtab *h)
 68{
 69	u32 i;
 70	struct hashtab_node *cur, *temp;
 71
 
 
 
 72	for (i = 0; i < h->size; i++) {
 73		cur = h->htable[i];
 74		while (cur) {
 75			temp = cur;
 76			cur = cur->next;
 77			kmem_cache_free(hashtab_node_cachep, temp);
 78		}
 79		h->htable[i] = NULL;
 80	}
 81
 82	kfree(h->htable);
 83	h->htable = NULL;
 
 
 84}
 85
 86int hashtab_map(struct hashtab *h,
 87		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),
143		void *args)
144{
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 (cur = orig->htable[i]; cur; cur = cur->next) {
160			tmp = kmem_cache_zalloc(hashtab_node_cachep,
161						GFP_KERNEL);
162			if (!tmp)
163				goto error;
164			rc = copy(tmp, cur, args);
165			if (rc) {
166				kmem_cache_free(hashtab_node_cachep, tmp);
167				goto error;
168			}
169			tmp->next = NULL;
170			if (!tail)
171				new->htable[i] = tmp;
172			else
173				tail->next = tmp;
174			tail = tmp;
175			new->nel++;
176		}
177	}
178
179	return 0;
180
181 error:
182	for (i = 0; i < new->size; i++) {
183		for (cur = new->htable[i]; cur; cur = tmp) {
184			tmp = cur->next;
185			destroy(cur->key, cur->datum, args);
186			kmem_cache_free(hashtab_node_cachep, cur);
187		}
188	}
189	kfree(new->htable);
190	memset(new, 0, sizeof(*new));
191	return -ENOMEM;
192}
193
194void __init hashtab_cache_init(void)
195{
196		hashtab_node_cachep = kmem_cache_create("hashtab_node",
197			sizeof(struct hashtab_node),
198			0, SLAB_PANIC, NULL);
199}