xref: /linux/lib/rhashtable.c (revision 88d6ed15acff1cb44b1d1f3c0a393b7f7744957a)
17e1e7763SThomas Graf /*
27e1e7763SThomas Graf  * Resizable, Scalable, Concurrent Hash Table
37e1e7763SThomas Graf  *
47e1e7763SThomas Graf  * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
57e1e7763SThomas Graf  * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
67e1e7763SThomas Graf  *
77e1e7763SThomas Graf  * Based on the following paper:
87e1e7763SThomas Graf  * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
97e1e7763SThomas Graf  *
107e1e7763SThomas Graf  * Code partially derived from nft_hash
117e1e7763SThomas Graf  *
127e1e7763SThomas Graf  * This program is free software; you can redistribute it and/or modify
137e1e7763SThomas Graf  * it under the terms of the GNU General Public License version 2 as
147e1e7763SThomas Graf  * published by the Free Software Foundation.
157e1e7763SThomas Graf  */
167e1e7763SThomas Graf 
177e1e7763SThomas Graf #include <linux/kernel.h>
187e1e7763SThomas Graf #include <linux/init.h>
197e1e7763SThomas Graf #include <linux/log2.h>
207e1e7763SThomas Graf #include <linux/slab.h>
217e1e7763SThomas Graf #include <linux/vmalloc.h>
227e1e7763SThomas Graf #include <linux/mm.h>
2387545899SDaniel Borkmann #include <linux/jhash.h>
247e1e7763SThomas Graf #include <linux/random.h>
257e1e7763SThomas Graf #include <linux/rhashtable.h>
267e1e7763SThomas Graf 
277e1e7763SThomas Graf #define HASH_DEFAULT_SIZE	64UL
287e1e7763SThomas Graf #define HASH_MIN_SIZE		4UL
297e1e7763SThomas Graf 
307e1e7763SThomas Graf #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
317e1e7763SThomas Graf 
327e1e7763SThomas Graf #ifdef CONFIG_PROVE_LOCKING
337e1e7763SThomas Graf int lockdep_rht_mutex_is_held(const struct rhashtable *ht)
347e1e7763SThomas Graf {
357b4ce235SHerbert Xu 	return ht->p.mutex_is_held(ht->p.parent);
367e1e7763SThomas Graf }
377e1e7763SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
38*88d6ed15SThomas Graf 
39*88d6ed15SThomas Graf int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
40*88d6ed15SThomas Graf {
41*88d6ed15SThomas Graf 	return 1;
42*88d6ed15SThomas Graf }
43*88d6ed15SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
447e1e7763SThomas Graf #endif
457e1e7763SThomas Graf 
46c91eee56SThomas Graf static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
477e1e7763SThomas Graf {
487e1e7763SThomas Graf 	return (void *) he - ht->p.head_offset;
497e1e7763SThomas Graf }
507e1e7763SThomas Graf 
518d24c0b4SThomas Graf static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
527e1e7763SThomas Graf {
538d24c0b4SThomas Graf 	return hash & (tbl->size - 1);
547e1e7763SThomas Graf }
557e1e7763SThomas Graf 
568d24c0b4SThomas Graf static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
578d24c0b4SThomas Graf {
588d24c0b4SThomas Graf 	u32 hash;
598d24c0b4SThomas Graf 
608d24c0b4SThomas Graf 	if (unlikely(!ht->p.key_len))
618d24c0b4SThomas Graf 		hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
628d24c0b4SThomas Graf 	else
638d24c0b4SThomas Graf 		hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
648d24c0b4SThomas Graf 				    ht->p.hash_rnd);
658d24c0b4SThomas Graf 
668d24c0b4SThomas Graf 	return hash;
678d24c0b4SThomas Graf }
688d24c0b4SThomas Graf 
698d24c0b4SThomas Graf static u32 key_hashfn(const struct rhashtable *ht, const void *key, u32 len)
707e1e7763SThomas Graf {
717e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
728d24c0b4SThomas Graf 	u32 hash;
737e1e7763SThomas Graf 
748d24c0b4SThomas Graf 	hash = ht->p.hashfn(key, len, ht->p.hash_rnd);
758d24c0b4SThomas Graf 
768d24c0b4SThomas Graf 	return rht_bucket_index(tbl, hash);
777e1e7763SThomas Graf }
787e1e7763SThomas Graf 
797e1e7763SThomas Graf static u32 head_hashfn(const struct rhashtable *ht,
808d24c0b4SThomas Graf 		       const struct bucket_table *tbl,
818d24c0b4SThomas Graf 		       const struct rhash_head *he)
827e1e7763SThomas Graf {
838d24c0b4SThomas Graf 	return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
847e1e7763SThomas Graf }
857e1e7763SThomas Graf 
866eba8224SThomas Graf static struct bucket_table *bucket_table_alloc(size_t nbuckets)
877e1e7763SThomas Graf {
887e1e7763SThomas Graf 	struct bucket_table *tbl;
897e1e7763SThomas Graf 	size_t size;
907e1e7763SThomas Graf 
917e1e7763SThomas Graf 	size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
926eba8224SThomas Graf 	tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
937e1e7763SThomas Graf 	if (tbl == NULL)
947e1e7763SThomas Graf 		tbl = vzalloc(size);
957e1e7763SThomas Graf 
967e1e7763SThomas Graf 	if (tbl == NULL)
977e1e7763SThomas Graf 		return NULL;
987e1e7763SThomas Graf 
997e1e7763SThomas Graf 	tbl->size = nbuckets;
1007e1e7763SThomas Graf 
1017e1e7763SThomas Graf 	return tbl;
1027e1e7763SThomas Graf }
1037e1e7763SThomas Graf 
1047e1e7763SThomas Graf static void bucket_table_free(const struct bucket_table *tbl)
1057e1e7763SThomas Graf {
1067e1e7763SThomas Graf 	kvfree(tbl);
1077e1e7763SThomas Graf }
1087e1e7763SThomas Graf 
1097e1e7763SThomas Graf /**
1107e1e7763SThomas Graf  * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
1117e1e7763SThomas Graf  * @ht:		hash table
1127e1e7763SThomas Graf  * @new_size:	new table size
1137e1e7763SThomas Graf  */
1147e1e7763SThomas Graf bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
1157e1e7763SThomas Graf {
1167e1e7763SThomas Graf 	/* Expand table when exceeding 75% load */
1177e1e7763SThomas Graf 	return ht->nelems > (new_size / 4 * 3);
1187e1e7763SThomas Graf }
1197e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rht_grow_above_75);
1207e1e7763SThomas Graf 
1217e1e7763SThomas Graf /**
1227e1e7763SThomas Graf  * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
1237e1e7763SThomas Graf  * @ht:		hash table
1247e1e7763SThomas Graf  * @new_size:	new table size
1257e1e7763SThomas Graf  */
1267e1e7763SThomas Graf bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
1277e1e7763SThomas Graf {
1287e1e7763SThomas Graf 	/* Shrink table beneath 30% load */
1297e1e7763SThomas Graf 	return ht->nelems < (new_size * 3 / 10);
1307e1e7763SThomas Graf }
1317e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rht_shrink_below_30);
1327e1e7763SThomas Graf 
1337e1e7763SThomas Graf static void hashtable_chain_unzip(const struct rhashtable *ht,
1347e1e7763SThomas Graf 				  const struct bucket_table *new_tbl,
1357e1e7763SThomas Graf 				  struct bucket_table *old_tbl, size_t n)
1367e1e7763SThomas Graf {
1377e1e7763SThomas Graf 	struct rhash_head *he, *p, *next;
1387e1e7763SThomas Graf 	unsigned int h;
1397e1e7763SThomas Graf 
1407e1e7763SThomas Graf 	/* Old bucket empty, no work needed. */
1417e1e7763SThomas Graf 	p = rht_dereference(old_tbl->buckets[n], ht);
1427e1e7763SThomas Graf 	if (!p)
1437e1e7763SThomas Graf 		return;
1447e1e7763SThomas Graf 
1457e1e7763SThomas Graf 	/* Advance the old bucket pointer one or more times until it
1467e1e7763SThomas Graf 	 * reaches a node that doesn't hash to the same bucket as the
1477e1e7763SThomas Graf 	 * previous node p. Call the previous node p;
1487e1e7763SThomas Graf 	 */
1498d24c0b4SThomas Graf 	h = head_hashfn(ht, new_tbl, p);
150*88d6ed15SThomas Graf 	rht_for_each_continue(he, p->next, old_tbl, n) {
1518d24c0b4SThomas Graf 		if (head_hashfn(ht, new_tbl, he) != h)
1527e1e7763SThomas Graf 			break;
1537e1e7763SThomas Graf 		p = he;
1547e1e7763SThomas Graf 	}
1557e1e7763SThomas Graf 	RCU_INIT_POINTER(old_tbl->buckets[n], p->next);
1567e1e7763SThomas Graf 
1577e1e7763SThomas Graf 	/* Find the subsequent node which does hash to the same
1587e1e7763SThomas Graf 	 * bucket as node P, or NULL if no such node exists.
1597e1e7763SThomas Graf 	 */
1607e1e7763SThomas Graf 	next = NULL;
1617e1e7763SThomas Graf 	if (he) {
162*88d6ed15SThomas Graf 		rht_for_each_continue(he, he->next, old_tbl, n) {
1638d24c0b4SThomas Graf 			if (head_hashfn(ht, new_tbl, he) == h) {
1647e1e7763SThomas Graf 				next = he;
1657e1e7763SThomas Graf 				break;
1667e1e7763SThomas Graf 			}
1677e1e7763SThomas Graf 		}
1687e1e7763SThomas Graf 	}
1697e1e7763SThomas Graf 
1707e1e7763SThomas Graf 	/* Set p's next pointer to that subsequent node pointer,
1717e1e7763SThomas Graf 	 * bypassing the nodes which do not hash to p's bucket
1727e1e7763SThomas Graf 	 */
1737e1e7763SThomas Graf 	RCU_INIT_POINTER(p->next, next);
1747e1e7763SThomas Graf }
1757e1e7763SThomas Graf 
1767e1e7763SThomas Graf /**
1777e1e7763SThomas Graf  * rhashtable_expand - Expand hash table while allowing concurrent lookups
1787e1e7763SThomas Graf  * @ht:		the hash table to expand
1797e1e7763SThomas Graf  *
1807e1e7763SThomas Graf  * A secondary bucket array is allocated and the hash entries are migrated
1817e1e7763SThomas Graf  * while keeping them on both lists until the end of the RCU grace period.
1827e1e7763SThomas Graf  *
1837e1e7763SThomas Graf  * This function may only be called in a context where it is safe to call
1847e1e7763SThomas Graf  * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
1857e1e7763SThomas Graf  *
1867e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations take place.
1877e1e7763SThomas Graf  * It is however valid to have concurrent lookups if they are RCU protected.
1887e1e7763SThomas Graf  */
1896eba8224SThomas Graf int rhashtable_expand(struct rhashtable *ht)
1907e1e7763SThomas Graf {
1917e1e7763SThomas Graf 	struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
1927e1e7763SThomas Graf 	struct rhash_head *he;
1937e1e7763SThomas Graf 	unsigned int i, h;
1947e1e7763SThomas Graf 	bool complete;
1957e1e7763SThomas Graf 
1967e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
1977e1e7763SThomas Graf 
1987e1e7763SThomas Graf 	if (ht->p.max_shift && ht->shift >= ht->p.max_shift)
1997e1e7763SThomas Graf 		return 0;
2007e1e7763SThomas Graf 
2016eba8224SThomas Graf 	new_tbl = bucket_table_alloc(old_tbl->size * 2);
2027e1e7763SThomas Graf 	if (new_tbl == NULL)
2037e1e7763SThomas Graf 		return -ENOMEM;
2047e1e7763SThomas Graf 
2057e1e7763SThomas Graf 	ht->shift++;
2067e1e7763SThomas Graf 
2077e1e7763SThomas Graf 	/* For each new bucket, search the corresponding old bucket
2080c828f2fSHerbert Xu 	 * for the first entry that hashes to the new bucket, and
2097e1e7763SThomas Graf 	 * link the new bucket to that entry. Since all the entries
2107e1e7763SThomas Graf 	 * which will end up in the new bucket appear in the same
2117e1e7763SThomas Graf 	 * old bucket, this constructs an entirely valid new hash
2127e1e7763SThomas Graf 	 * table, but with multiple buckets "zipped" together into a
2137e1e7763SThomas Graf 	 * single imprecise chain.
2147e1e7763SThomas Graf 	 */
2157e1e7763SThomas Graf 	for (i = 0; i < new_tbl->size; i++) {
2168d24c0b4SThomas Graf 		h = rht_bucket_index(old_tbl, i);
217*88d6ed15SThomas Graf 		rht_for_each(he, old_tbl, h) {
2188d24c0b4SThomas Graf 			if (head_hashfn(ht, new_tbl, he) == i) {
2197e1e7763SThomas Graf 				RCU_INIT_POINTER(new_tbl->buckets[i], he);
2207e1e7763SThomas Graf 				break;
2217e1e7763SThomas Graf 			}
2227e1e7763SThomas Graf 		}
2237e1e7763SThomas Graf 	}
2247e1e7763SThomas Graf 
2257e1e7763SThomas Graf 	/* Publish the new table pointer. Lookups may now traverse
2260c828f2fSHerbert Xu 	 * the new table, but they will not benefit from any
2270c828f2fSHerbert Xu 	 * additional efficiency until later steps unzip the buckets.
2287e1e7763SThomas Graf 	 */
2297e1e7763SThomas Graf 	rcu_assign_pointer(ht->tbl, new_tbl);
2307e1e7763SThomas Graf 
2317e1e7763SThomas Graf 	/* Unzip interleaved hash chains */
2327e1e7763SThomas Graf 	do {
2337e1e7763SThomas Graf 		/* Wait for readers. All new readers will see the new
2347e1e7763SThomas Graf 		 * table, and thus no references to the old table will
2357e1e7763SThomas Graf 		 * remain.
2367e1e7763SThomas Graf 		 */
2377e1e7763SThomas Graf 		synchronize_rcu();
2387e1e7763SThomas Graf 
2397e1e7763SThomas Graf 		/* For each bucket in the old table (each of which
2407e1e7763SThomas Graf 		 * contains items from multiple buckets of the new
2417e1e7763SThomas Graf 		 * table): ...
2427e1e7763SThomas Graf 		 */
2437e1e7763SThomas Graf 		complete = true;
2447e1e7763SThomas Graf 		for (i = 0; i < old_tbl->size; i++) {
2457e1e7763SThomas Graf 			hashtable_chain_unzip(ht, new_tbl, old_tbl, i);
2467e1e7763SThomas Graf 			if (old_tbl->buckets[i] != NULL)
2477e1e7763SThomas Graf 				complete = false;
2487e1e7763SThomas Graf 		}
2497e1e7763SThomas Graf 	} while (!complete);
2507e1e7763SThomas Graf 
2517e1e7763SThomas Graf 	bucket_table_free(old_tbl);
2527e1e7763SThomas Graf 	return 0;
2537e1e7763SThomas Graf }
2547e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_expand);
2557e1e7763SThomas Graf 
2567e1e7763SThomas Graf /**
2577e1e7763SThomas Graf  * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
2587e1e7763SThomas Graf  * @ht:		the hash table to shrink
2597e1e7763SThomas Graf  *
2607e1e7763SThomas Graf  * This function may only be called in a context where it is safe to call
2617e1e7763SThomas Graf  * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
2627e1e7763SThomas Graf  *
2637e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations take place.
2647e1e7763SThomas Graf  * It is however valid to have concurrent lookups if they are RCU protected.
2657e1e7763SThomas Graf  */
2666eba8224SThomas Graf int rhashtable_shrink(struct rhashtable *ht)
2677e1e7763SThomas Graf {
2687e1e7763SThomas Graf 	struct bucket_table *ntbl, *tbl = rht_dereference(ht->tbl, ht);
2697e1e7763SThomas Graf 	struct rhash_head __rcu **pprev;
2707e1e7763SThomas Graf 	unsigned int i;
2717e1e7763SThomas Graf 
2727e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
2737e1e7763SThomas Graf 
27494000176SYing Xue 	if (ht->shift <= ht->p.min_shift)
2757e1e7763SThomas Graf 		return 0;
2767e1e7763SThomas Graf 
2776eba8224SThomas Graf 	ntbl = bucket_table_alloc(tbl->size / 2);
2787e1e7763SThomas Graf 	if (ntbl == NULL)
2797e1e7763SThomas Graf 		return -ENOMEM;
2807e1e7763SThomas Graf 
2817e1e7763SThomas Graf 	ht->shift--;
2827e1e7763SThomas Graf 
2830c828f2fSHerbert Xu 	/* Link each bucket in the new table to the first bucket
2847e1e7763SThomas Graf 	 * in the old table that contains entries which will hash
2857e1e7763SThomas Graf 	 * to the new bucket.
2867e1e7763SThomas Graf 	 */
2877e1e7763SThomas Graf 	for (i = 0; i < ntbl->size; i++) {
2887e1e7763SThomas Graf 		ntbl->buckets[i] = tbl->buckets[i];
2897e1e7763SThomas Graf 
2900c828f2fSHerbert Xu 		/* Link each bucket in the new table to the first bucket
2917e1e7763SThomas Graf 		 * in the old table that contains entries which will hash
2927e1e7763SThomas Graf 		 * to the new bucket.
2937e1e7763SThomas Graf 		 */
2947e1e7763SThomas Graf 		for (pprev = &ntbl->buckets[i]; *pprev != NULL;
295*88d6ed15SThomas Graf 		     pprev = &rht_dereference_bucket(*pprev, ntbl, i)->next)
2967e1e7763SThomas Graf 			;
2977e1e7763SThomas Graf 		RCU_INIT_POINTER(*pprev, tbl->buckets[i + ntbl->size]);
2987e1e7763SThomas Graf 	}
2997e1e7763SThomas Graf 
3007e1e7763SThomas Graf 	/* Publish the new, valid hash table */
3017e1e7763SThomas Graf 	rcu_assign_pointer(ht->tbl, ntbl);
3027e1e7763SThomas Graf 
3037e1e7763SThomas Graf 	/* Wait for readers. No new readers will have references to the
3047e1e7763SThomas Graf 	 * old hash table.
3057e1e7763SThomas Graf 	 */
3067e1e7763SThomas Graf 	synchronize_rcu();
3077e1e7763SThomas Graf 
3087e1e7763SThomas Graf 	bucket_table_free(tbl);
3097e1e7763SThomas Graf 
3107e1e7763SThomas Graf 	return 0;
3117e1e7763SThomas Graf }
3127e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_shrink);
3137e1e7763SThomas Graf 
3147e1e7763SThomas Graf /**
3157e1e7763SThomas Graf  * rhashtable_insert - insert object into hash hash table
3167e1e7763SThomas Graf  * @ht:		hash table
3177e1e7763SThomas Graf  * @obj:	pointer to hash head inside object
3187e1e7763SThomas Graf  *
3197e1e7763SThomas Graf  * Will automatically grow the table via rhashtable_expand() if the the
3207e1e7763SThomas Graf  * grow_decision function specified at rhashtable_init() returns true.
3217e1e7763SThomas Graf  *
3227e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations occur. It is
3237e1e7763SThomas Graf  * however valid to have concurrent lookups if they are RCU protected.
3247e1e7763SThomas Graf  */
3256eba8224SThomas Graf void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
3267e1e7763SThomas Graf {
3277e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
3287e1e7763SThomas Graf 	u32 hash;
3297e1e7763SThomas Graf 
3307e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
3317e1e7763SThomas Graf 
3328d24c0b4SThomas Graf 	hash = head_hashfn(ht, tbl, obj);
3337e1e7763SThomas Graf 	RCU_INIT_POINTER(obj->next, tbl->buckets[hash]);
3347e1e7763SThomas Graf 	rcu_assign_pointer(tbl->buckets[hash], obj);
3357e1e7763SThomas Graf 	ht->nelems++;
3367e1e7763SThomas Graf 
3377e1e7763SThomas Graf 	if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size))
3386eba8224SThomas Graf 		rhashtable_expand(ht);
3397e1e7763SThomas Graf }
3407e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_insert);
3417e1e7763SThomas Graf 
3427e1e7763SThomas Graf /**
3437e1e7763SThomas Graf  * rhashtable_remove_pprev - remove object from hash table given previous element
3447e1e7763SThomas Graf  * @ht:		hash table
3457e1e7763SThomas Graf  * @obj:	pointer to hash head inside object
3467e1e7763SThomas Graf  * @pprev:	pointer to previous element
3477e1e7763SThomas Graf  *
3487e1e7763SThomas Graf  * Identical to rhashtable_remove() but caller is alreayd aware of the element
3497e1e7763SThomas Graf  * in front of the element to be deleted. This is in particular useful for
3507e1e7763SThomas Graf  * deletion when combined with walking or lookup.
3517e1e7763SThomas Graf  */
3527e1e7763SThomas Graf void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj,
3536eba8224SThomas Graf 			     struct rhash_head __rcu **pprev)
3547e1e7763SThomas Graf {
3557e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
3567e1e7763SThomas Graf 
3577e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
3587e1e7763SThomas Graf 
3597e1e7763SThomas Graf 	RCU_INIT_POINTER(*pprev, obj->next);
3607e1e7763SThomas Graf 	ht->nelems--;
3617e1e7763SThomas Graf 
3627e1e7763SThomas Graf 	if (ht->p.shrink_decision &&
3637e1e7763SThomas Graf 	    ht->p.shrink_decision(ht, tbl->size))
3646eba8224SThomas Graf 		rhashtable_shrink(ht);
3657e1e7763SThomas Graf }
3667e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_remove_pprev);
3677e1e7763SThomas Graf 
3687e1e7763SThomas Graf /**
3697e1e7763SThomas Graf  * rhashtable_remove - remove object from hash table
3707e1e7763SThomas Graf  * @ht:		hash table
3717e1e7763SThomas Graf  * @obj:	pointer to hash head inside object
3727e1e7763SThomas Graf  *
3737e1e7763SThomas Graf  * Since the hash chain is single linked, the removal operation needs to
3747e1e7763SThomas Graf  * walk the bucket chain upon removal. The removal operation is thus
3757e1e7763SThomas Graf  * considerable slow if the hash table is not correctly sized.
3767e1e7763SThomas Graf  *
3777e1e7763SThomas Graf  * Will automatically shrink the table via rhashtable_expand() if the the
3787e1e7763SThomas Graf  * shrink_decision function specified at rhashtable_init() returns true.
3797e1e7763SThomas Graf  *
3807e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations occur. It is
3817e1e7763SThomas Graf  * however valid to have concurrent lookups if they are RCU protected.
3827e1e7763SThomas Graf  */
3836eba8224SThomas Graf bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
3847e1e7763SThomas Graf {
3857e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
3867e1e7763SThomas Graf 	struct rhash_head __rcu **pprev;
3877e1e7763SThomas Graf 	struct rhash_head *he;
3887e1e7763SThomas Graf 	u32 h;
3897e1e7763SThomas Graf 
3907e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
3917e1e7763SThomas Graf 
3928d24c0b4SThomas Graf 	h = head_hashfn(ht, tbl, obj);
3937e1e7763SThomas Graf 
3947e1e7763SThomas Graf 	pprev = &tbl->buckets[h];
395*88d6ed15SThomas Graf 	rht_for_each(he, tbl, h) {
3967e1e7763SThomas Graf 		if (he != obj) {
3977e1e7763SThomas Graf 			pprev = &he->next;
3987e1e7763SThomas Graf 			continue;
3997e1e7763SThomas Graf 		}
4007e1e7763SThomas Graf 
4016eba8224SThomas Graf 		rhashtable_remove_pprev(ht, he, pprev);
4027e1e7763SThomas Graf 		return true;
4037e1e7763SThomas Graf 	}
4047e1e7763SThomas Graf 
4057e1e7763SThomas Graf 	return false;
4067e1e7763SThomas Graf }
4077e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_remove);
4087e1e7763SThomas Graf 
4097e1e7763SThomas Graf /**
4107e1e7763SThomas Graf  * rhashtable_lookup - lookup key in hash table
4117e1e7763SThomas Graf  * @ht:		hash table
4127e1e7763SThomas Graf  * @key:	pointer to key
4137e1e7763SThomas Graf  *
4147e1e7763SThomas Graf  * Computes the hash value for the key and traverses the bucket chain looking
4157e1e7763SThomas Graf  * for a entry with an identical key. The first matching entry is returned.
4167e1e7763SThomas Graf  *
4177e1e7763SThomas Graf  * This lookup function may only be used for fixed key hash table (key_len
4187e1e7763SThomas Graf  * paramter set). It will BUG() if used inappropriately.
4197e1e7763SThomas Graf  *
4207e1e7763SThomas Graf  * Lookups may occur in parallel with hash mutations as long as the lookup is
4217e1e7763SThomas Graf  * guarded by rcu_read_lock(). The caller must take care of this.
4227e1e7763SThomas Graf  */
4237e1e7763SThomas Graf void *rhashtable_lookup(const struct rhashtable *ht, const void *key)
4247e1e7763SThomas Graf {
4257e1e7763SThomas Graf 	const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
4267e1e7763SThomas Graf 	struct rhash_head *he;
4277e1e7763SThomas Graf 	u32 h;
4287e1e7763SThomas Graf 
4297e1e7763SThomas Graf 	BUG_ON(!ht->p.key_len);
4307e1e7763SThomas Graf 
4318d24c0b4SThomas Graf 	h = key_hashfn(ht, key, ht->p.key_len);
432*88d6ed15SThomas Graf 	rht_for_each_rcu(he, tbl, h) {
4337e1e7763SThomas Graf 		if (memcmp(rht_obj(ht, he) + ht->p.key_offset, key,
4347e1e7763SThomas Graf 			   ht->p.key_len))
4357e1e7763SThomas Graf 			continue;
436a4b18cdaSThomas Graf 		return rht_obj(ht, he);
4377e1e7763SThomas Graf 	}
4387e1e7763SThomas Graf 
4397e1e7763SThomas Graf 	return NULL;
4407e1e7763SThomas Graf }
4417e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup);
4427e1e7763SThomas Graf 
4437e1e7763SThomas Graf /**
4447e1e7763SThomas Graf  * rhashtable_lookup_compare - search hash table with compare function
4457e1e7763SThomas Graf  * @ht:		hash table
4468d24c0b4SThomas Graf  * @key:	the pointer to the key
4477e1e7763SThomas Graf  * @compare:	compare function, must return true on match
4487e1e7763SThomas Graf  * @arg:	argument passed on to compare function
4497e1e7763SThomas Graf  *
4507e1e7763SThomas Graf  * Traverses the bucket chain behind the provided hash value and calls the
4517e1e7763SThomas Graf  * specified compare function for each entry.
4527e1e7763SThomas Graf  *
4537e1e7763SThomas Graf  * Lookups may occur in parallel with hash mutations as long as the lookup is
4547e1e7763SThomas Graf  * guarded by rcu_read_lock(). The caller must take care of this.
4557e1e7763SThomas Graf  *
4567e1e7763SThomas Graf  * Returns the first entry on which the compare function returned true.
4577e1e7763SThomas Graf  */
4588d24c0b4SThomas Graf void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key,
4597e1e7763SThomas Graf 				bool (*compare)(void *, void *), void *arg)
4607e1e7763SThomas Graf {
4617e1e7763SThomas Graf 	const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
4627e1e7763SThomas Graf 	struct rhash_head *he;
4638d24c0b4SThomas Graf 	u32 hash;
4647e1e7763SThomas Graf 
4658d24c0b4SThomas Graf 	hash = key_hashfn(ht, key, ht->p.key_len);
466*88d6ed15SThomas Graf 	rht_for_each_rcu(he, tbl, hash) {
4677e1e7763SThomas Graf 		if (!compare(rht_obj(ht, he), arg))
4687e1e7763SThomas Graf 			continue;
469a4b18cdaSThomas Graf 		return rht_obj(ht, he);
4707e1e7763SThomas Graf 	}
4717e1e7763SThomas Graf 
4727e1e7763SThomas Graf 	return NULL;
4737e1e7763SThomas Graf }
4747e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
4757e1e7763SThomas Graf 
47694000176SYing Xue static size_t rounded_hashtable_size(struct rhashtable_params *params)
4777e1e7763SThomas Graf {
47894000176SYing Xue 	return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
47994000176SYing Xue 		   1UL << params->min_shift);
4807e1e7763SThomas Graf }
4817e1e7763SThomas Graf 
4827e1e7763SThomas Graf /**
4837e1e7763SThomas Graf  * rhashtable_init - initialize a new hash table
4847e1e7763SThomas Graf  * @ht:		hash table to be initialized
4857e1e7763SThomas Graf  * @params:	configuration parameters
4867e1e7763SThomas Graf  *
4877e1e7763SThomas Graf  * Initializes a new hash table based on the provided configuration
4887e1e7763SThomas Graf  * parameters. A table can be configured either with a variable or
4897e1e7763SThomas Graf  * fixed length key:
4907e1e7763SThomas Graf  *
4917e1e7763SThomas Graf  * Configuration Example 1: Fixed length keys
4927e1e7763SThomas Graf  * struct test_obj {
4937e1e7763SThomas Graf  *	int			key;
4947e1e7763SThomas Graf  *	void *			my_member;
4957e1e7763SThomas Graf  *	struct rhash_head	node;
4967e1e7763SThomas Graf  * };
4977e1e7763SThomas Graf  *
4987e1e7763SThomas Graf  * struct rhashtable_params params = {
4997e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
5007e1e7763SThomas Graf  *	.key_offset = offsetof(struct test_obj, key),
5017e1e7763SThomas Graf  *	.key_len = sizeof(int),
50287545899SDaniel Borkmann  *	.hashfn = jhash,
5031b2f309dSHerbert Xu  * #ifdef CONFIG_PROVE_LOCKING
5047e1e7763SThomas Graf  *	.mutex_is_held = &my_mutex_is_held,
5051b2f309dSHerbert Xu  * #endif
5067e1e7763SThomas Graf  * };
5077e1e7763SThomas Graf  *
5087e1e7763SThomas Graf  * Configuration Example 2: Variable length keys
5097e1e7763SThomas Graf  * struct test_obj {
5107e1e7763SThomas Graf  *	[...]
5117e1e7763SThomas Graf  *	struct rhash_head	node;
5127e1e7763SThomas Graf  * };
5137e1e7763SThomas Graf  *
5147e1e7763SThomas Graf  * u32 my_hash_fn(const void *data, u32 seed)
5157e1e7763SThomas Graf  * {
5167e1e7763SThomas Graf  *	struct test_obj *obj = data;
5177e1e7763SThomas Graf  *
5187e1e7763SThomas Graf  *	return [... hash ...];
5197e1e7763SThomas Graf  * }
5207e1e7763SThomas Graf  *
5217e1e7763SThomas Graf  * struct rhashtable_params params = {
5227e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
52387545899SDaniel Borkmann  *	.hashfn = jhash,
5247e1e7763SThomas Graf  *	.obj_hashfn = my_hash_fn,
5251b2f309dSHerbert Xu  * #ifdef CONFIG_PROVE_LOCKING
5267e1e7763SThomas Graf  *	.mutex_is_held = &my_mutex_is_held,
5271b2f309dSHerbert Xu  * #endif
5287e1e7763SThomas Graf  * };
5297e1e7763SThomas Graf  */
5307e1e7763SThomas Graf int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
5317e1e7763SThomas Graf {
5327e1e7763SThomas Graf 	struct bucket_table *tbl;
5337e1e7763SThomas Graf 	size_t size;
5347e1e7763SThomas Graf 
5357e1e7763SThomas Graf 	size = HASH_DEFAULT_SIZE;
5367e1e7763SThomas Graf 
5377e1e7763SThomas Graf 	if ((params->key_len && !params->hashfn) ||
5387e1e7763SThomas Graf 	    (!params->key_len && !params->obj_hashfn))
5397e1e7763SThomas Graf 		return -EINVAL;
5407e1e7763SThomas Graf 
54194000176SYing Xue 	params->min_shift = max_t(size_t, params->min_shift,
54294000176SYing Xue 				  ilog2(HASH_MIN_SIZE));
54394000176SYing Xue 
5447e1e7763SThomas Graf 	if (params->nelem_hint)
54594000176SYing Xue 		size = rounded_hashtable_size(params);
5467e1e7763SThomas Graf 
5476eba8224SThomas Graf 	tbl = bucket_table_alloc(size);
5487e1e7763SThomas Graf 	if (tbl == NULL)
5497e1e7763SThomas Graf 		return -ENOMEM;
5507e1e7763SThomas Graf 
5517e1e7763SThomas Graf 	memset(ht, 0, sizeof(*ht));
5527e1e7763SThomas Graf 	ht->shift = ilog2(tbl->size);
5537e1e7763SThomas Graf 	memcpy(&ht->p, params, sizeof(*params));
5547e1e7763SThomas Graf 	RCU_INIT_POINTER(ht->tbl, tbl);
5557e1e7763SThomas Graf 
5567e1e7763SThomas Graf 	if (!ht->p.hash_rnd)
5577e1e7763SThomas Graf 		get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
5587e1e7763SThomas Graf 
5597e1e7763SThomas Graf 	return 0;
5607e1e7763SThomas Graf }
5617e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init);
5627e1e7763SThomas Graf 
5637e1e7763SThomas Graf /**
5647e1e7763SThomas Graf  * rhashtable_destroy - destroy hash table
5657e1e7763SThomas Graf  * @ht:		the hash table to destroy
5667e1e7763SThomas Graf  *
567ae82ddcfSPablo Neira Ayuso  * Frees the bucket array. This function is not rcu safe, therefore the caller
568ae82ddcfSPablo Neira Ayuso  * has to make sure that no resizing may happen by unpublishing the hashtable
569ae82ddcfSPablo Neira Ayuso  * and waiting for the quiescent cycle before releasing the bucket array.
5707e1e7763SThomas Graf  */
5717e1e7763SThomas Graf void rhashtable_destroy(const struct rhashtable *ht)
5727e1e7763SThomas Graf {
573ae82ddcfSPablo Neira Ayuso 	bucket_table_free(ht->tbl);
5747e1e7763SThomas Graf }
5757e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy);
5767e1e7763SThomas Graf 
5777e1e7763SThomas Graf /**************************************************************************
5787e1e7763SThomas Graf  * Self Test
5797e1e7763SThomas Graf  **************************************************************************/
5807e1e7763SThomas Graf 
5817e1e7763SThomas Graf #ifdef CONFIG_TEST_RHASHTABLE
5827e1e7763SThomas Graf 
5837e1e7763SThomas Graf #define TEST_HT_SIZE	8
5847e1e7763SThomas Graf #define TEST_ENTRIES	2048
5857e1e7763SThomas Graf #define TEST_PTR	((void *) 0xdeadbeef)
5867e1e7763SThomas Graf #define TEST_NEXPANDS	4
5877e1e7763SThomas Graf 
5881b2f309dSHerbert Xu #ifdef CONFIG_PROVE_LOCKING
5897b4ce235SHerbert Xu static int test_mutex_is_held(void *parent)
5907e1e7763SThomas Graf {
5917e1e7763SThomas Graf 	return 1;
5927e1e7763SThomas Graf }
5931b2f309dSHerbert Xu #endif
5947e1e7763SThomas Graf 
5957e1e7763SThomas Graf struct test_obj {
5967e1e7763SThomas Graf 	void			*ptr;
5977e1e7763SThomas Graf 	int			value;
5987e1e7763SThomas Graf 	struct rhash_head	node;
5997e1e7763SThomas Graf };
6007e1e7763SThomas Graf 
6017e1e7763SThomas Graf static int __init test_rht_lookup(struct rhashtable *ht)
6027e1e7763SThomas Graf {
6037e1e7763SThomas Graf 	unsigned int i;
6047e1e7763SThomas Graf 
6057e1e7763SThomas Graf 	for (i = 0; i < TEST_ENTRIES * 2; i++) {
6067e1e7763SThomas Graf 		struct test_obj *obj;
6077e1e7763SThomas Graf 		bool expected = !(i % 2);
6087e1e7763SThomas Graf 		u32 key = i;
6097e1e7763SThomas Graf 
6107e1e7763SThomas Graf 		obj = rhashtable_lookup(ht, &key);
6117e1e7763SThomas Graf 
6127e1e7763SThomas Graf 		if (expected && !obj) {
6137e1e7763SThomas Graf 			pr_warn("Test failed: Could not find key %u\n", key);
6147e1e7763SThomas Graf 			return -ENOENT;
6157e1e7763SThomas Graf 		} else if (!expected && obj) {
6167e1e7763SThomas Graf 			pr_warn("Test failed: Unexpected entry found for key %u\n",
6177e1e7763SThomas Graf 				key);
6187e1e7763SThomas Graf 			return -EEXIST;
6197e1e7763SThomas Graf 		} else if (expected && obj) {
6207e1e7763SThomas Graf 			if (obj->ptr != TEST_PTR || obj->value != i) {
6217e1e7763SThomas Graf 				pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n",
6227e1e7763SThomas Graf 					obj->ptr, TEST_PTR, obj->value, i);
6237e1e7763SThomas Graf 				return -EINVAL;
6247e1e7763SThomas Graf 			}
6257e1e7763SThomas Graf 		}
6267e1e7763SThomas Graf 	}
6277e1e7763SThomas Graf 
6287e1e7763SThomas Graf 	return 0;
6297e1e7763SThomas Graf }
6307e1e7763SThomas Graf 
6313e7b2ec4SThomas Graf static void test_bucket_stats(struct rhashtable *ht, bool quiet)
6327e1e7763SThomas Graf {
6333e7b2ec4SThomas Graf 	unsigned int cnt, rcu_cnt, i, total = 0;
634*88d6ed15SThomas Graf 	struct rhash_head *pos;
6357e1e7763SThomas Graf 	struct test_obj *obj;
6363e7b2ec4SThomas Graf 	struct bucket_table *tbl;
6377e1e7763SThomas Graf 
6383e7b2ec4SThomas Graf 	tbl = rht_dereference_rcu(ht->tbl, ht);
6397e1e7763SThomas Graf 	for (i = 0; i < tbl->size; i++) {
6403e7b2ec4SThomas Graf 		rcu_cnt = cnt = 0;
6417e1e7763SThomas Graf 
6427e1e7763SThomas Graf 		if (!quiet)
6437e1e7763SThomas Graf 			pr_info(" [%#4x/%zu]", i, tbl->size);
6447e1e7763SThomas Graf 
645*88d6ed15SThomas Graf 		rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
6467e1e7763SThomas Graf 			cnt++;
6477e1e7763SThomas Graf 			total++;
6487e1e7763SThomas Graf 			if (!quiet)
6497e1e7763SThomas Graf 				pr_cont(" [%p],", obj);
6507e1e7763SThomas Graf 		}
6517e1e7763SThomas Graf 
652*88d6ed15SThomas Graf 		rht_for_each_entry_rcu(obj, pos, tbl, i, node)
6533e7b2ec4SThomas Graf 			rcu_cnt++;
6543e7b2ec4SThomas Graf 
6553e7b2ec4SThomas Graf 		if (rcu_cnt != cnt)
6563e7b2ec4SThomas Graf 			pr_warn("Test failed: Chain count mismach %d != %d",
6573e7b2ec4SThomas Graf 				cnt, rcu_cnt);
6583e7b2ec4SThomas Graf 
6597e1e7763SThomas Graf 		if (!quiet)
6607e1e7763SThomas Graf 			pr_cont("\n  [%#x] first element: %p, chain length: %u\n",
6617e1e7763SThomas Graf 				i, tbl->buckets[i], cnt);
6627e1e7763SThomas Graf 	}
6637e1e7763SThomas Graf 
6647e1e7763SThomas Graf 	pr_info("  Traversal complete: counted=%u, nelems=%zu, entries=%d\n",
6657e1e7763SThomas Graf 		total, ht->nelems, TEST_ENTRIES);
6663e7b2ec4SThomas Graf 
6673e7b2ec4SThomas Graf 	if (total != ht->nelems || total != TEST_ENTRIES)
6683e7b2ec4SThomas Graf 		pr_warn("Test failed: Total count mismatch ^^^");
6697e1e7763SThomas Graf }
6707e1e7763SThomas Graf 
6717e1e7763SThomas Graf static int __init test_rhashtable(struct rhashtable *ht)
6727e1e7763SThomas Graf {
6737e1e7763SThomas Graf 	struct bucket_table *tbl;
674*88d6ed15SThomas Graf 	struct test_obj *obj;
675*88d6ed15SThomas Graf 	struct rhash_head *pos, *next;
6767e1e7763SThomas Graf 	int err;
6777e1e7763SThomas Graf 	unsigned int i;
6787e1e7763SThomas Graf 
6797e1e7763SThomas Graf 	/*
6807e1e7763SThomas Graf 	 * Insertion Test:
6817e1e7763SThomas Graf 	 * Insert TEST_ENTRIES into table with all keys even numbers
6827e1e7763SThomas Graf 	 */
6837e1e7763SThomas Graf 	pr_info("  Adding %d keys\n", TEST_ENTRIES);
6847e1e7763SThomas Graf 	for (i = 0; i < TEST_ENTRIES; i++) {
6857e1e7763SThomas Graf 		struct test_obj *obj;
6867e1e7763SThomas Graf 
6877e1e7763SThomas Graf 		obj = kzalloc(sizeof(*obj), GFP_KERNEL);
6887e1e7763SThomas Graf 		if (!obj) {
6897e1e7763SThomas Graf 			err = -ENOMEM;
6907e1e7763SThomas Graf 			goto error;
6917e1e7763SThomas Graf 		}
6927e1e7763SThomas Graf 
6937e1e7763SThomas Graf 		obj->ptr = TEST_PTR;
6947e1e7763SThomas Graf 		obj->value = i * 2;
6957e1e7763SThomas Graf 
6966eba8224SThomas Graf 		rhashtable_insert(ht, &obj->node);
6977e1e7763SThomas Graf 	}
6987e1e7763SThomas Graf 
6997e1e7763SThomas Graf 	rcu_read_lock();
7003e7b2ec4SThomas Graf 	test_bucket_stats(ht, true);
7017e1e7763SThomas Graf 	test_rht_lookup(ht);
7027e1e7763SThomas Graf 	rcu_read_unlock();
7037e1e7763SThomas Graf 
7047e1e7763SThomas Graf 	for (i = 0; i < TEST_NEXPANDS; i++) {
7057e1e7763SThomas Graf 		pr_info("  Table expansion iteration %u...\n", i);
7066eba8224SThomas Graf 		rhashtable_expand(ht);
7077e1e7763SThomas Graf 
7087e1e7763SThomas Graf 		rcu_read_lock();
7097e1e7763SThomas Graf 		pr_info("  Verifying lookups...\n");
7107e1e7763SThomas Graf 		test_rht_lookup(ht);
7117e1e7763SThomas Graf 		rcu_read_unlock();
7127e1e7763SThomas Graf 	}
7137e1e7763SThomas Graf 
7147e1e7763SThomas Graf 	for (i = 0; i < TEST_NEXPANDS; i++) {
7157e1e7763SThomas Graf 		pr_info("  Table shrinkage iteration %u...\n", i);
7166eba8224SThomas Graf 		rhashtable_shrink(ht);
7177e1e7763SThomas Graf 
7187e1e7763SThomas Graf 		rcu_read_lock();
7197e1e7763SThomas Graf 		pr_info("  Verifying lookups...\n");
7207e1e7763SThomas Graf 		test_rht_lookup(ht);
7217e1e7763SThomas Graf 		rcu_read_unlock();
7227e1e7763SThomas Graf 	}
7237e1e7763SThomas Graf 
7243e7b2ec4SThomas Graf 	rcu_read_lock();
7253e7b2ec4SThomas Graf 	test_bucket_stats(ht, true);
7263e7b2ec4SThomas Graf 	rcu_read_unlock();
7273e7b2ec4SThomas Graf 
7287e1e7763SThomas Graf 	pr_info("  Deleting %d keys\n", TEST_ENTRIES);
7297e1e7763SThomas Graf 	for (i = 0; i < TEST_ENTRIES; i++) {
7307e1e7763SThomas Graf 		u32 key = i * 2;
7317e1e7763SThomas Graf 
7327e1e7763SThomas Graf 		obj = rhashtable_lookup(ht, &key);
7337e1e7763SThomas Graf 		BUG_ON(!obj);
7347e1e7763SThomas Graf 
7356eba8224SThomas Graf 		rhashtable_remove(ht, &obj->node);
7367e1e7763SThomas Graf 		kfree(obj);
7377e1e7763SThomas Graf 	}
7387e1e7763SThomas Graf 
7397e1e7763SThomas Graf 	return 0;
7407e1e7763SThomas Graf 
7417e1e7763SThomas Graf error:
7427e1e7763SThomas Graf 	tbl = rht_dereference_rcu(ht->tbl, ht);
7437e1e7763SThomas Graf 	for (i = 0; i < tbl->size; i++)
744*88d6ed15SThomas Graf 		rht_for_each_entry_safe(obj, pos, next, tbl, i, node)
7457e1e7763SThomas Graf 			kfree(obj);
7467e1e7763SThomas Graf 
7477e1e7763SThomas Graf 	return err;
7487e1e7763SThomas Graf }
7497e1e7763SThomas Graf 
7507e1e7763SThomas Graf static int __init test_rht_init(void)
7517e1e7763SThomas Graf {
7527e1e7763SThomas Graf 	struct rhashtable ht;
7537e1e7763SThomas Graf 	struct rhashtable_params params = {
7547e1e7763SThomas Graf 		.nelem_hint = TEST_HT_SIZE,
7557e1e7763SThomas Graf 		.head_offset = offsetof(struct test_obj, node),
7567e1e7763SThomas Graf 		.key_offset = offsetof(struct test_obj, value),
7577e1e7763SThomas Graf 		.key_len = sizeof(int),
75887545899SDaniel Borkmann 		.hashfn = jhash,
7591b2f309dSHerbert Xu #ifdef CONFIG_PROVE_LOCKING
7607e1e7763SThomas Graf 		.mutex_is_held = &test_mutex_is_held,
7611b2f309dSHerbert Xu #endif
7627e1e7763SThomas Graf 		.grow_decision = rht_grow_above_75,
7637e1e7763SThomas Graf 		.shrink_decision = rht_shrink_below_30,
7647e1e7763SThomas Graf 	};
7657e1e7763SThomas Graf 	int err;
7667e1e7763SThomas Graf 
7677e1e7763SThomas Graf 	pr_info("Running resizable hashtable tests...\n");
7687e1e7763SThomas Graf 
7697e1e7763SThomas Graf 	err = rhashtable_init(&ht, &params);
7707e1e7763SThomas Graf 	if (err < 0) {
7717e1e7763SThomas Graf 		pr_warn("Test failed: Unable to initialize hashtable: %d\n",
7727e1e7763SThomas Graf 			err);
7737e1e7763SThomas Graf 		return err;
7747e1e7763SThomas Graf 	}
7757e1e7763SThomas Graf 
7767e1e7763SThomas Graf 	err = test_rhashtable(&ht);
7777e1e7763SThomas Graf 
7787e1e7763SThomas Graf 	rhashtable_destroy(&ht);
7797e1e7763SThomas Graf 
7807e1e7763SThomas Graf 	return err;
7817e1e7763SThomas Graf }
7827e1e7763SThomas Graf 
7837e1e7763SThomas Graf subsys_initcall(test_rht_init);
7847e1e7763SThomas Graf 
7857e1e7763SThomas Graf #endif /* CONFIG_TEST_RHASHTABLE */
786