xref: /linux/lib/rhashtable.c (revision ae82ddcf8e8239bdd06e6830d450cf9e785b8024)
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>
237e1e7763SThomas Graf #include <linux/hash.h>
247e1e7763SThomas Graf #include <linux/random.h>
257e1e7763SThomas Graf #include <linux/rhashtable.h>
267e1e7763SThomas Graf #include <linux/log2.h>
277e1e7763SThomas Graf 
287e1e7763SThomas Graf #define HASH_DEFAULT_SIZE	64UL
297e1e7763SThomas Graf #define HASH_MIN_SIZE		4UL
307e1e7763SThomas Graf 
317e1e7763SThomas Graf #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
327e1e7763SThomas Graf 
337e1e7763SThomas Graf #ifdef CONFIG_PROVE_LOCKING
347e1e7763SThomas Graf int lockdep_rht_mutex_is_held(const struct rhashtable *ht)
357e1e7763SThomas Graf {
367e1e7763SThomas Graf 	return ht->p.mutex_is_held();
377e1e7763SThomas Graf }
387e1e7763SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
397e1e7763SThomas Graf #endif
407e1e7763SThomas Graf 
41c91eee56SThomas Graf static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
427e1e7763SThomas Graf {
437e1e7763SThomas Graf 	return (void *) he - ht->p.head_offset;
447e1e7763SThomas Graf }
457e1e7763SThomas Graf 
467e1e7763SThomas Graf static u32 __hashfn(const struct rhashtable *ht, const void *key,
477e1e7763SThomas Graf 		      u32 len, u32 hsize)
487e1e7763SThomas Graf {
497e1e7763SThomas Graf 	u32 h;
507e1e7763SThomas Graf 
517e1e7763SThomas Graf 	h = ht->p.hashfn(key, len, ht->p.hash_rnd);
527e1e7763SThomas Graf 
537e1e7763SThomas Graf 	return h & (hsize - 1);
547e1e7763SThomas Graf }
557e1e7763SThomas Graf 
567e1e7763SThomas Graf /**
577e1e7763SThomas Graf  * rhashtable_hashfn - compute hash for key of given length
587e1e7763SThomas Graf  * @ht:		hash table to compuate for
597e1e7763SThomas Graf  * @key:	pointer to key
607e1e7763SThomas Graf  * @len:	length of key
617e1e7763SThomas Graf  *
627e1e7763SThomas Graf  * Computes the hash value using the hash function provided in the 'hashfn'
637e1e7763SThomas Graf  * of struct rhashtable_params. The returned value is guaranteed to be
647e1e7763SThomas Graf  * smaller than the number of buckets in the hash table.
657e1e7763SThomas Graf  */
667e1e7763SThomas Graf u32 rhashtable_hashfn(const struct rhashtable *ht, const void *key, u32 len)
677e1e7763SThomas Graf {
687e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
697e1e7763SThomas Graf 
707e1e7763SThomas Graf 	return __hashfn(ht, key, len, tbl->size);
717e1e7763SThomas Graf }
727e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_hashfn);
737e1e7763SThomas Graf 
747e1e7763SThomas Graf static u32 obj_hashfn(const struct rhashtable *ht, const void *ptr, u32 hsize)
757e1e7763SThomas Graf {
767e1e7763SThomas Graf 	if (unlikely(!ht->p.key_len)) {
777e1e7763SThomas Graf 		u32 h;
787e1e7763SThomas Graf 
797e1e7763SThomas Graf 		h = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
807e1e7763SThomas Graf 
817e1e7763SThomas Graf 		return h & (hsize - 1);
827e1e7763SThomas Graf 	}
837e1e7763SThomas Graf 
847e1e7763SThomas Graf 	return __hashfn(ht, ptr + ht->p.key_offset, ht->p.key_len, hsize);
857e1e7763SThomas Graf }
867e1e7763SThomas Graf 
877e1e7763SThomas Graf /**
887e1e7763SThomas Graf  * rhashtable_obj_hashfn - compute hash for hashed object
897e1e7763SThomas Graf  * @ht:		hash table to compuate for
907e1e7763SThomas Graf  * @ptr:	pointer to hashed object
917e1e7763SThomas Graf  *
927e1e7763SThomas Graf  * Computes the hash value using the hash function `hashfn` respectively
937e1e7763SThomas Graf  * 'obj_hashfn' depending on whether the hash table is set up to work with
947e1e7763SThomas Graf  * a fixed length key. The returned value is guaranteed to be smaller than
957e1e7763SThomas Graf  * the number of buckets in the hash table.
967e1e7763SThomas Graf  */
977e1e7763SThomas Graf u32 rhashtable_obj_hashfn(const struct rhashtable *ht, void *ptr)
987e1e7763SThomas Graf {
997e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
1007e1e7763SThomas Graf 
1017e1e7763SThomas Graf 	return obj_hashfn(ht, ptr, tbl->size);
1027e1e7763SThomas Graf }
1037e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_obj_hashfn);
1047e1e7763SThomas Graf 
1057e1e7763SThomas Graf static u32 head_hashfn(const struct rhashtable *ht,
1067e1e7763SThomas Graf 		       const struct rhash_head *he, u32 hsize)
1077e1e7763SThomas Graf {
1087e1e7763SThomas Graf 	return obj_hashfn(ht, rht_obj(ht, he), hsize);
1097e1e7763SThomas Graf }
1107e1e7763SThomas Graf 
1117e1e7763SThomas Graf static struct bucket_table *bucket_table_alloc(size_t nbuckets, gfp_t flags)
1127e1e7763SThomas Graf {
1137e1e7763SThomas Graf 	struct bucket_table *tbl;
1147e1e7763SThomas Graf 	size_t size;
1157e1e7763SThomas Graf 
1167e1e7763SThomas Graf 	size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
1177e1e7763SThomas Graf 	tbl = kzalloc(size, flags);
1187e1e7763SThomas Graf 	if (tbl == NULL)
1197e1e7763SThomas Graf 		tbl = vzalloc(size);
1207e1e7763SThomas Graf 
1217e1e7763SThomas Graf 	if (tbl == NULL)
1227e1e7763SThomas Graf 		return NULL;
1237e1e7763SThomas Graf 
1247e1e7763SThomas Graf 	tbl->size = nbuckets;
1257e1e7763SThomas Graf 
1267e1e7763SThomas Graf 	return tbl;
1277e1e7763SThomas Graf }
1287e1e7763SThomas Graf 
1297e1e7763SThomas Graf static void bucket_table_free(const struct bucket_table *tbl)
1307e1e7763SThomas Graf {
1317e1e7763SThomas Graf 	kvfree(tbl);
1327e1e7763SThomas Graf }
1337e1e7763SThomas Graf 
1347e1e7763SThomas Graf /**
1357e1e7763SThomas Graf  * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
1367e1e7763SThomas Graf  * @ht:		hash table
1377e1e7763SThomas Graf  * @new_size:	new table size
1387e1e7763SThomas Graf  */
1397e1e7763SThomas Graf bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
1407e1e7763SThomas Graf {
1417e1e7763SThomas Graf 	/* Expand table when exceeding 75% load */
1427e1e7763SThomas Graf 	return ht->nelems > (new_size / 4 * 3);
1437e1e7763SThomas Graf }
1447e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rht_grow_above_75);
1457e1e7763SThomas Graf 
1467e1e7763SThomas Graf /**
1477e1e7763SThomas Graf  * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
1487e1e7763SThomas Graf  * @ht:		hash table
1497e1e7763SThomas Graf  * @new_size:	new table size
1507e1e7763SThomas Graf  */
1517e1e7763SThomas Graf bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
1527e1e7763SThomas Graf {
1537e1e7763SThomas Graf 	/* Shrink table beneath 30% load */
1547e1e7763SThomas Graf 	return ht->nelems < (new_size * 3 / 10);
1557e1e7763SThomas Graf }
1567e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rht_shrink_below_30);
1577e1e7763SThomas Graf 
1587e1e7763SThomas Graf static void hashtable_chain_unzip(const struct rhashtable *ht,
1597e1e7763SThomas Graf 				  const struct bucket_table *new_tbl,
1607e1e7763SThomas Graf 				  struct bucket_table *old_tbl, size_t n)
1617e1e7763SThomas Graf {
1627e1e7763SThomas Graf 	struct rhash_head *he, *p, *next;
1637e1e7763SThomas Graf 	unsigned int h;
1647e1e7763SThomas Graf 
1657e1e7763SThomas Graf 	/* Old bucket empty, no work needed. */
1667e1e7763SThomas Graf 	p = rht_dereference(old_tbl->buckets[n], ht);
1677e1e7763SThomas Graf 	if (!p)
1687e1e7763SThomas Graf 		return;
1697e1e7763SThomas Graf 
1707e1e7763SThomas Graf 	/* Advance the old bucket pointer one or more times until it
1717e1e7763SThomas Graf 	 * reaches a node that doesn't hash to the same bucket as the
1727e1e7763SThomas Graf 	 * previous node p. Call the previous node p;
1737e1e7763SThomas Graf 	 */
1747e1e7763SThomas Graf 	h = head_hashfn(ht, p, new_tbl->size);
1757e1e7763SThomas Graf 	rht_for_each(he, p->next, ht) {
1767e1e7763SThomas Graf 		if (head_hashfn(ht, he, new_tbl->size) != h)
1777e1e7763SThomas Graf 			break;
1787e1e7763SThomas Graf 		p = he;
1797e1e7763SThomas Graf 	}
1807e1e7763SThomas Graf 	RCU_INIT_POINTER(old_tbl->buckets[n], p->next);
1817e1e7763SThomas Graf 
1827e1e7763SThomas Graf 	/* Find the subsequent node which does hash to the same
1837e1e7763SThomas Graf 	 * bucket as node P, or NULL if no such node exists.
1847e1e7763SThomas Graf 	 */
1857e1e7763SThomas Graf 	next = NULL;
1867e1e7763SThomas Graf 	if (he) {
1877e1e7763SThomas Graf 		rht_for_each(he, he->next, ht) {
1887e1e7763SThomas Graf 			if (head_hashfn(ht, he, new_tbl->size) == h) {
1897e1e7763SThomas Graf 				next = he;
1907e1e7763SThomas Graf 				break;
1917e1e7763SThomas Graf 			}
1927e1e7763SThomas Graf 		}
1937e1e7763SThomas Graf 	}
1947e1e7763SThomas Graf 
1957e1e7763SThomas Graf 	/* Set p's next pointer to that subsequent node pointer,
1967e1e7763SThomas Graf 	 * bypassing the nodes which do not hash to p's bucket
1977e1e7763SThomas Graf 	 */
1987e1e7763SThomas Graf 	RCU_INIT_POINTER(p->next, next);
1997e1e7763SThomas Graf }
2007e1e7763SThomas Graf 
2017e1e7763SThomas Graf /**
2027e1e7763SThomas Graf  * rhashtable_expand - Expand hash table while allowing concurrent lookups
2037e1e7763SThomas Graf  * @ht:		the hash table to expand
2047e1e7763SThomas Graf  * @flags:	allocation flags
2057e1e7763SThomas Graf  *
2067e1e7763SThomas Graf  * A secondary bucket array is allocated and the hash entries are migrated
2077e1e7763SThomas Graf  * while keeping them on both lists until the end of the RCU grace period.
2087e1e7763SThomas Graf  *
2097e1e7763SThomas Graf  * This function may only be called in a context where it is safe to call
2107e1e7763SThomas Graf  * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
2117e1e7763SThomas Graf  *
2127e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations take place.
2137e1e7763SThomas Graf  * It is however valid to have concurrent lookups if they are RCU protected.
2147e1e7763SThomas Graf  */
2157e1e7763SThomas Graf int rhashtable_expand(struct rhashtable *ht, gfp_t flags)
2167e1e7763SThomas Graf {
2177e1e7763SThomas Graf 	struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
2187e1e7763SThomas Graf 	struct rhash_head *he;
2197e1e7763SThomas Graf 	unsigned int i, h;
2207e1e7763SThomas Graf 	bool complete;
2217e1e7763SThomas Graf 
2227e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
2237e1e7763SThomas Graf 
2247e1e7763SThomas Graf 	if (ht->p.max_shift && ht->shift >= ht->p.max_shift)
2257e1e7763SThomas Graf 		return 0;
2267e1e7763SThomas Graf 
2277e1e7763SThomas Graf 	new_tbl = bucket_table_alloc(old_tbl->size * 2, flags);
2287e1e7763SThomas Graf 	if (new_tbl == NULL)
2297e1e7763SThomas Graf 		return -ENOMEM;
2307e1e7763SThomas Graf 
2317e1e7763SThomas Graf 	ht->shift++;
2327e1e7763SThomas Graf 
2337e1e7763SThomas Graf 	/* For each new bucket, search the corresponding old bucket
2347e1e7763SThomas Graf 	 * for the first entry that hashes to the new bucket, and
2357e1e7763SThomas Graf 	 * link the new bucket to that entry. Since all the entries
2367e1e7763SThomas Graf 	 * which will end up in the new bucket appear in the same
2377e1e7763SThomas Graf 	 * old bucket, this constructs an entirely valid new hash
2387e1e7763SThomas Graf 	 * table, but with multiple buckets "zipped" together into a
2397e1e7763SThomas Graf 	 * single imprecise chain.
2407e1e7763SThomas Graf 	 */
2417e1e7763SThomas Graf 	for (i = 0; i < new_tbl->size; i++) {
2427e1e7763SThomas Graf 		h = i & (old_tbl->size - 1);
2437e1e7763SThomas Graf 		rht_for_each(he, old_tbl->buckets[h], ht) {
2447e1e7763SThomas Graf 			if (head_hashfn(ht, he, new_tbl->size) == i) {
2457e1e7763SThomas Graf 				RCU_INIT_POINTER(new_tbl->buckets[i], he);
2467e1e7763SThomas Graf 				break;
2477e1e7763SThomas Graf 			}
2487e1e7763SThomas Graf 		}
2497e1e7763SThomas Graf 	}
2507e1e7763SThomas Graf 
2517e1e7763SThomas Graf 	/* Publish the new table pointer. Lookups may now traverse
2527e1e7763SThomas Graf 	 * the new table, but they will not benefit from any
2537e1e7763SThomas Graf 	 * additional efficiency until later steps unzip the buckets.
2547e1e7763SThomas Graf 	 */
2557e1e7763SThomas Graf 	rcu_assign_pointer(ht->tbl, new_tbl);
2567e1e7763SThomas Graf 
2577e1e7763SThomas Graf 	/* Unzip interleaved hash chains */
2587e1e7763SThomas Graf 	do {
2597e1e7763SThomas Graf 		/* Wait for readers. All new readers will see the new
2607e1e7763SThomas Graf 		 * table, and thus no references to the old table will
2617e1e7763SThomas Graf 		 * remain.
2627e1e7763SThomas Graf 		 */
2637e1e7763SThomas Graf 		synchronize_rcu();
2647e1e7763SThomas Graf 
2657e1e7763SThomas Graf 		/* For each bucket in the old table (each of which
2667e1e7763SThomas Graf 		 * contains items from multiple buckets of the new
2677e1e7763SThomas Graf 		 * table): ...
2687e1e7763SThomas Graf 		 */
2697e1e7763SThomas Graf 		complete = true;
2707e1e7763SThomas Graf 		for (i = 0; i < old_tbl->size; i++) {
2717e1e7763SThomas Graf 			hashtable_chain_unzip(ht, new_tbl, old_tbl, i);
2727e1e7763SThomas Graf 			if (old_tbl->buckets[i] != NULL)
2737e1e7763SThomas Graf 				complete = false;
2747e1e7763SThomas Graf 		}
2757e1e7763SThomas Graf 	} while (!complete);
2767e1e7763SThomas Graf 
2777e1e7763SThomas Graf 	bucket_table_free(old_tbl);
2787e1e7763SThomas Graf 	return 0;
2797e1e7763SThomas Graf }
2807e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_expand);
2817e1e7763SThomas Graf 
2827e1e7763SThomas Graf /**
2837e1e7763SThomas Graf  * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
2847e1e7763SThomas Graf  * @ht:		the hash table to shrink
2857e1e7763SThomas Graf  * @flags:	allocation flags
2867e1e7763SThomas Graf  *
2877e1e7763SThomas Graf  * This function may only be called in a context where it is safe to call
2887e1e7763SThomas Graf  * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
2897e1e7763SThomas Graf  *
2907e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations take place.
2917e1e7763SThomas Graf  * It is however valid to have concurrent lookups if they are RCU protected.
2927e1e7763SThomas Graf  */
2937e1e7763SThomas Graf int rhashtable_shrink(struct rhashtable *ht, gfp_t flags)
2947e1e7763SThomas Graf {
2957e1e7763SThomas Graf 	struct bucket_table *ntbl, *tbl = rht_dereference(ht->tbl, ht);
2967e1e7763SThomas Graf 	struct rhash_head __rcu **pprev;
2977e1e7763SThomas Graf 	unsigned int i;
2987e1e7763SThomas Graf 
2997e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
3007e1e7763SThomas Graf 
3017e1e7763SThomas Graf 	if (tbl->size <= HASH_MIN_SIZE)
3027e1e7763SThomas Graf 		return 0;
3037e1e7763SThomas Graf 
3047e1e7763SThomas Graf 	ntbl = bucket_table_alloc(tbl->size / 2, flags);
3057e1e7763SThomas Graf 	if (ntbl == NULL)
3067e1e7763SThomas Graf 		return -ENOMEM;
3077e1e7763SThomas Graf 
3087e1e7763SThomas Graf 	ht->shift--;
3097e1e7763SThomas Graf 
3107e1e7763SThomas Graf 	/* Link each bucket in the new table to the first bucket
3117e1e7763SThomas Graf 	 * in the old table that contains entries which will hash
3127e1e7763SThomas Graf 	 * to the new bucket.
3137e1e7763SThomas Graf 	 */
3147e1e7763SThomas Graf 	for (i = 0; i < ntbl->size; i++) {
3157e1e7763SThomas Graf 		ntbl->buckets[i] = tbl->buckets[i];
3167e1e7763SThomas Graf 
3177e1e7763SThomas Graf 		/* Link each bucket in the new table to the first bucket
3187e1e7763SThomas Graf 		 * in the old table that contains entries which will hash
3197e1e7763SThomas Graf 		 * to the new bucket.
3207e1e7763SThomas Graf 		 */
3217e1e7763SThomas Graf 		for (pprev = &ntbl->buckets[i]; *pprev != NULL;
3227e1e7763SThomas Graf 		     pprev = &rht_dereference(*pprev, ht)->next)
3237e1e7763SThomas Graf 			;
3247e1e7763SThomas Graf 		RCU_INIT_POINTER(*pprev, tbl->buckets[i + ntbl->size]);
3257e1e7763SThomas Graf 	}
3267e1e7763SThomas Graf 
3277e1e7763SThomas Graf 	/* Publish the new, valid hash table */
3287e1e7763SThomas Graf 	rcu_assign_pointer(ht->tbl, ntbl);
3297e1e7763SThomas Graf 
3307e1e7763SThomas Graf 	/* Wait for readers. No new readers will have references to the
3317e1e7763SThomas Graf 	 * old hash table.
3327e1e7763SThomas Graf 	 */
3337e1e7763SThomas Graf 	synchronize_rcu();
3347e1e7763SThomas Graf 
3357e1e7763SThomas Graf 	bucket_table_free(tbl);
3367e1e7763SThomas Graf 
3377e1e7763SThomas Graf 	return 0;
3387e1e7763SThomas Graf }
3397e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_shrink);
3407e1e7763SThomas Graf 
3417e1e7763SThomas Graf /**
3427e1e7763SThomas Graf  * rhashtable_insert - insert object into hash hash table
3437e1e7763SThomas Graf  * @ht:		hash table
3447e1e7763SThomas Graf  * @obj:	pointer to hash head inside object
3457e1e7763SThomas Graf  * @flags:	allocation flags (table expansion)
3467e1e7763SThomas Graf  *
3477e1e7763SThomas Graf  * Will automatically grow the table via rhashtable_expand() if the the
3487e1e7763SThomas Graf  * grow_decision function specified at rhashtable_init() returns true.
3497e1e7763SThomas Graf  *
3507e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations occur. It is
3517e1e7763SThomas Graf  * however valid to have concurrent lookups if they are RCU protected.
3527e1e7763SThomas Graf  */
3537e1e7763SThomas Graf void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
3547e1e7763SThomas Graf 		       gfp_t flags)
3557e1e7763SThomas Graf {
3567e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
3577e1e7763SThomas Graf 	u32 hash;
3587e1e7763SThomas Graf 
3597e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
3607e1e7763SThomas Graf 
3617e1e7763SThomas Graf 	hash = head_hashfn(ht, obj, tbl->size);
3627e1e7763SThomas Graf 	RCU_INIT_POINTER(obj->next, tbl->buckets[hash]);
3637e1e7763SThomas Graf 	rcu_assign_pointer(tbl->buckets[hash], obj);
3647e1e7763SThomas Graf 	ht->nelems++;
3657e1e7763SThomas Graf 
3667e1e7763SThomas Graf 	if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size))
3677e1e7763SThomas Graf 		rhashtable_expand(ht, flags);
3687e1e7763SThomas Graf }
3697e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_insert);
3707e1e7763SThomas Graf 
3717e1e7763SThomas Graf /**
3727e1e7763SThomas Graf  * rhashtable_remove_pprev - remove object from hash table given previous element
3737e1e7763SThomas Graf  * @ht:		hash table
3747e1e7763SThomas Graf  * @obj:	pointer to hash head inside object
3757e1e7763SThomas Graf  * @pprev:	pointer to previous element
3767e1e7763SThomas Graf  * @flags:	allocation flags (table expansion)
3777e1e7763SThomas Graf  *
3787e1e7763SThomas Graf  * Identical to rhashtable_remove() but caller is alreayd aware of the element
3797e1e7763SThomas Graf  * in front of the element to be deleted. This is in particular useful for
3807e1e7763SThomas Graf  * deletion when combined with walking or lookup.
3817e1e7763SThomas Graf  */
3827e1e7763SThomas Graf void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj,
3835300fdcbSThomas Graf 			     struct rhash_head __rcu **pprev, gfp_t flags)
3847e1e7763SThomas Graf {
3857e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
3867e1e7763SThomas Graf 
3877e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
3887e1e7763SThomas Graf 
3897e1e7763SThomas Graf 	RCU_INIT_POINTER(*pprev, obj->next);
3907e1e7763SThomas Graf 	ht->nelems--;
3917e1e7763SThomas Graf 
3927e1e7763SThomas Graf 	if (ht->p.shrink_decision &&
3937e1e7763SThomas Graf 	    ht->p.shrink_decision(ht, tbl->size))
3947e1e7763SThomas Graf 		rhashtable_shrink(ht, flags);
3957e1e7763SThomas Graf }
3967e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_remove_pprev);
3977e1e7763SThomas Graf 
3987e1e7763SThomas Graf /**
3997e1e7763SThomas Graf  * rhashtable_remove - remove object from hash table
4007e1e7763SThomas Graf  * @ht:		hash table
4017e1e7763SThomas Graf  * @obj:	pointer to hash head inside object
4027e1e7763SThomas Graf  * @flags:	allocation flags (table expansion)
4037e1e7763SThomas Graf  *
4047e1e7763SThomas Graf  * Since the hash chain is single linked, the removal operation needs to
4057e1e7763SThomas Graf  * walk the bucket chain upon removal. The removal operation is thus
4067e1e7763SThomas Graf  * considerable slow if the hash table is not correctly sized.
4077e1e7763SThomas Graf  *
4087e1e7763SThomas Graf  * Will automatically shrink the table via rhashtable_expand() if the the
4097e1e7763SThomas Graf  * shrink_decision function specified at rhashtable_init() returns true.
4107e1e7763SThomas Graf  *
4117e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations occur. It is
4127e1e7763SThomas Graf  * however valid to have concurrent lookups if they are RCU protected.
4137e1e7763SThomas Graf  */
4147e1e7763SThomas Graf bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj,
4157e1e7763SThomas Graf 		       gfp_t flags)
4167e1e7763SThomas Graf {
4177e1e7763SThomas Graf 	struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
4187e1e7763SThomas Graf 	struct rhash_head __rcu **pprev;
4197e1e7763SThomas Graf 	struct rhash_head *he;
4207e1e7763SThomas Graf 	u32 h;
4217e1e7763SThomas Graf 
4227e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
4237e1e7763SThomas Graf 
4247e1e7763SThomas Graf 	h = head_hashfn(ht, obj, tbl->size);
4257e1e7763SThomas Graf 
4267e1e7763SThomas Graf 	pprev = &tbl->buckets[h];
4277e1e7763SThomas Graf 	rht_for_each(he, tbl->buckets[h], ht) {
4287e1e7763SThomas Graf 		if (he != obj) {
4297e1e7763SThomas Graf 			pprev = &he->next;
4307e1e7763SThomas Graf 			continue;
4317e1e7763SThomas Graf 		}
4327e1e7763SThomas Graf 
4337e1e7763SThomas Graf 		rhashtable_remove_pprev(ht, he, pprev, flags);
4347e1e7763SThomas Graf 		return true;
4357e1e7763SThomas Graf 	}
4367e1e7763SThomas Graf 
4377e1e7763SThomas Graf 	return false;
4387e1e7763SThomas Graf }
4397e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_remove);
4407e1e7763SThomas Graf 
4417e1e7763SThomas Graf /**
4427e1e7763SThomas Graf  * rhashtable_lookup - lookup key in hash table
4437e1e7763SThomas Graf  * @ht:		hash table
4447e1e7763SThomas Graf  * @key:	pointer to key
4457e1e7763SThomas Graf  *
4467e1e7763SThomas Graf  * Computes the hash value for the key and traverses the bucket chain looking
4477e1e7763SThomas Graf  * for a entry with an identical key. The first matching entry is returned.
4487e1e7763SThomas Graf  *
4497e1e7763SThomas Graf  * This lookup function may only be used for fixed key hash table (key_len
4507e1e7763SThomas Graf  * paramter set). It will BUG() if used inappropriately.
4517e1e7763SThomas Graf  *
4527e1e7763SThomas Graf  * Lookups may occur in parallel with hash mutations as long as the lookup is
4537e1e7763SThomas Graf  * guarded by rcu_read_lock(). The caller must take care of this.
4547e1e7763SThomas Graf  */
4557e1e7763SThomas Graf void *rhashtable_lookup(const struct rhashtable *ht, const void *key)
4567e1e7763SThomas Graf {
4577e1e7763SThomas Graf 	const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
4587e1e7763SThomas Graf 	struct rhash_head *he;
4597e1e7763SThomas Graf 	u32 h;
4607e1e7763SThomas Graf 
4617e1e7763SThomas Graf 	BUG_ON(!ht->p.key_len);
4627e1e7763SThomas Graf 
4637e1e7763SThomas Graf 	h = __hashfn(ht, key, ht->p.key_len, tbl->size);
4647e1e7763SThomas Graf 	rht_for_each_rcu(he, tbl->buckets[h], ht) {
4657e1e7763SThomas Graf 		if (memcmp(rht_obj(ht, he) + ht->p.key_offset, key,
4667e1e7763SThomas Graf 			   ht->p.key_len))
4677e1e7763SThomas Graf 			continue;
4687e1e7763SThomas Graf 		return (void *) he - ht->p.head_offset;
4697e1e7763SThomas Graf 	}
4707e1e7763SThomas Graf 
4717e1e7763SThomas Graf 	return NULL;
4727e1e7763SThomas Graf }
4737e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup);
4747e1e7763SThomas Graf 
4757e1e7763SThomas Graf /**
4767e1e7763SThomas Graf  * rhashtable_lookup_compare - search hash table with compare function
4777e1e7763SThomas Graf  * @ht:		hash table
4787e1e7763SThomas Graf  * @hash:	hash value of desired entry
4797e1e7763SThomas Graf  * @compare:	compare function, must return true on match
4807e1e7763SThomas Graf  * @arg:	argument passed on to compare function
4817e1e7763SThomas Graf  *
4827e1e7763SThomas Graf  * Traverses the bucket chain behind the provided hash value and calls the
4837e1e7763SThomas Graf  * specified compare function for each entry.
4847e1e7763SThomas Graf  *
4857e1e7763SThomas Graf  * Lookups may occur in parallel with hash mutations as long as the lookup is
4867e1e7763SThomas Graf  * guarded by rcu_read_lock(). The caller must take care of this.
4877e1e7763SThomas Graf  *
4887e1e7763SThomas Graf  * Returns the first entry on which the compare function returned true.
4897e1e7763SThomas Graf  */
4907e1e7763SThomas Graf void *rhashtable_lookup_compare(const struct rhashtable *ht, u32 hash,
4917e1e7763SThomas Graf 				bool (*compare)(void *, void *), void *arg)
4927e1e7763SThomas Graf {
4937e1e7763SThomas Graf 	const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
4947e1e7763SThomas Graf 	struct rhash_head *he;
4957e1e7763SThomas Graf 
4967e1e7763SThomas Graf 	if (unlikely(hash >= tbl->size))
4977e1e7763SThomas Graf 		return NULL;
4987e1e7763SThomas Graf 
4997e1e7763SThomas Graf 	rht_for_each_rcu(he, tbl->buckets[hash], ht) {
5007e1e7763SThomas Graf 		if (!compare(rht_obj(ht, he), arg))
5017e1e7763SThomas Graf 			continue;
5027e1e7763SThomas Graf 		return (void *) he - ht->p.head_offset;
5037e1e7763SThomas Graf 	}
5047e1e7763SThomas Graf 
5057e1e7763SThomas Graf 	return NULL;
5067e1e7763SThomas Graf }
5077e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
5087e1e7763SThomas Graf 
5097e1e7763SThomas Graf static size_t rounded_hashtable_size(unsigned int nelem)
5107e1e7763SThomas Graf {
5117e1e7763SThomas Graf 	return max(roundup_pow_of_two(nelem * 4 / 3), HASH_MIN_SIZE);
5127e1e7763SThomas Graf }
5137e1e7763SThomas Graf 
5147e1e7763SThomas Graf /**
5157e1e7763SThomas Graf  * rhashtable_init - initialize a new hash table
5167e1e7763SThomas Graf  * @ht:		hash table to be initialized
5177e1e7763SThomas Graf  * @params:	configuration parameters
5187e1e7763SThomas Graf  *
5197e1e7763SThomas Graf  * Initializes a new hash table based on the provided configuration
5207e1e7763SThomas Graf  * parameters. A table can be configured either with a variable or
5217e1e7763SThomas Graf  * fixed length key:
5227e1e7763SThomas Graf  *
5237e1e7763SThomas Graf  * Configuration Example 1: Fixed length keys
5247e1e7763SThomas Graf  * struct test_obj {
5257e1e7763SThomas Graf  *	int			key;
5267e1e7763SThomas Graf  *	void *			my_member;
5277e1e7763SThomas Graf  *	struct rhash_head	node;
5287e1e7763SThomas Graf  * };
5297e1e7763SThomas Graf  *
5307e1e7763SThomas Graf  * struct rhashtable_params params = {
5317e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
5327e1e7763SThomas Graf  *	.key_offset = offsetof(struct test_obj, key),
5337e1e7763SThomas Graf  *	.key_len = sizeof(int),
5347e1e7763SThomas Graf  *	.hashfn = arch_fast_hash,
5357e1e7763SThomas Graf  *	.mutex_is_held = &my_mutex_is_held,
5367e1e7763SThomas Graf  * };
5377e1e7763SThomas Graf  *
5387e1e7763SThomas Graf  * Configuration Example 2: Variable length keys
5397e1e7763SThomas Graf  * struct test_obj {
5407e1e7763SThomas Graf  *	[...]
5417e1e7763SThomas Graf  *	struct rhash_head	node;
5427e1e7763SThomas Graf  * };
5437e1e7763SThomas Graf  *
5447e1e7763SThomas Graf  * u32 my_hash_fn(const void *data, u32 seed)
5457e1e7763SThomas Graf  * {
5467e1e7763SThomas Graf  *	struct test_obj *obj = data;
5477e1e7763SThomas Graf  *
5487e1e7763SThomas Graf  *	return [... hash ...];
5497e1e7763SThomas Graf  * }
5507e1e7763SThomas Graf  *
5517e1e7763SThomas Graf  * struct rhashtable_params params = {
5527e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
5537e1e7763SThomas Graf  *	.hashfn = arch_fast_hash,
5547e1e7763SThomas Graf  *	.obj_hashfn = my_hash_fn,
5557e1e7763SThomas Graf  *	.mutex_is_held = &my_mutex_is_held,
5567e1e7763SThomas Graf  * };
5577e1e7763SThomas Graf  */
5587e1e7763SThomas Graf int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
5597e1e7763SThomas Graf {
5607e1e7763SThomas Graf 	struct bucket_table *tbl;
5617e1e7763SThomas Graf 	size_t size;
5627e1e7763SThomas Graf 
5637e1e7763SThomas Graf 	size = HASH_DEFAULT_SIZE;
5647e1e7763SThomas Graf 
5657e1e7763SThomas Graf 	if ((params->key_len && !params->hashfn) ||
5667e1e7763SThomas Graf 	    (!params->key_len && !params->obj_hashfn))
5677e1e7763SThomas Graf 		return -EINVAL;
5687e1e7763SThomas Graf 
5697e1e7763SThomas Graf 	if (params->nelem_hint)
5707e1e7763SThomas Graf 		size = rounded_hashtable_size(params->nelem_hint);
5717e1e7763SThomas Graf 
5727e1e7763SThomas Graf 	tbl = bucket_table_alloc(size, GFP_KERNEL);
5737e1e7763SThomas Graf 	if (tbl == NULL)
5747e1e7763SThomas Graf 		return -ENOMEM;
5757e1e7763SThomas Graf 
5767e1e7763SThomas Graf 	memset(ht, 0, sizeof(*ht));
5777e1e7763SThomas Graf 	ht->shift = ilog2(tbl->size);
5787e1e7763SThomas Graf 	memcpy(&ht->p, params, sizeof(*params));
5797e1e7763SThomas Graf 	RCU_INIT_POINTER(ht->tbl, tbl);
5807e1e7763SThomas Graf 
5817e1e7763SThomas Graf 	if (!ht->p.hash_rnd)
5827e1e7763SThomas Graf 		get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
5837e1e7763SThomas Graf 
5847e1e7763SThomas Graf 	return 0;
5857e1e7763SThomas Graf }
5867e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init);
5877e1e7763SThomas Graf 
5887e1e7763SThomas Graf /**
5897e1e7763SThomas Graf  * rhashtable_destroy - destroy hash table
5907e1e7763SThomas Graf  * @ht:		the hash table to destroy
5917e1e7763SThomas Graf  *
592*ae82ddcfSPablo Neira Ayuso  * Frees the bucket array. This function is not rcu safe, therefore the caller
593*ae82ddcfSPablo Neira Ayuso  * has to make sure that no resizing may happen by unpublishing the hashtable
594*ae82ddcfSPablo Neira Ayuso  * and waiting for the quiescent cycle before releasing the bucket array.
5957e1e7763SThomas Graf  */
5967e1e7763SThomas Graf void rhashtable_destroy(const struct rhashtable *ht)
5977e1e7763SThomas Graf {
598*ae82ddcfSPablo Neira Ayuso 	bucket_table_free(ht->tbl);
5997e1e7763SThomas Graf }
6007e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy);
6017e1e7763SThomas Graf 
6027e1e7763SThomas Graf /**************************************************************************
6037e1e7763SThomas Graf  * Self Test
6047e1e7763SThomas Graf  **************************************************************************/
6057e1e7763SThomas Graf 
6067e1e7763SThomas Graf #ifdef CONFIG_TEST_RHASHTABLE
6077e1e7763SThomas Graf 
6087e1e7763SThomas Graf #define TEST_HT_SIZE	8
6097e1e7763SThomas Graf #define TEST_ENTRIES	2048
6107e1e7763SThomas Graf #define TEST_PTR	((void *) 0xdeadbeef)
6117e1e7763SThomas Graf #define TEST_NEXPANDS	4
6127e1e7763SThomas Graf 
6137e1e7763SThomas Graf static int test_mutex_is_held(void)
6147e1e7763SThomas Graf {
6157e1e7763SThomas Graf 	return 1;
6167e1e7763SThomas Graf }
6177e1e7763SThomas Graf 
6187e1e7763SThomas Graf struct test_obj {
6197e1e7763SThomas Graf 	void			*ptr;
6207e1e7763SThomas Graf 	int			value;
6217e1e7763SThomas Graf 	struct rhash_head	node;
6227e1e7763SThomas Graf };
6237e1e7763SThomas Graf 
6247e1e7763SThomas Graf static int __init test_rht_lookup(struct rhashtable *ht)
6257e1e7763SThomas Graf {
6267e1e7763SThomas Graf 	unsigned int i;
6277e1e7763SThomas Graf 
6287e1e7763SThomas Graf 	for (i = 0; i < TEST_ENTRIES * 2; i++) {
6297e1e7763SThomas Graf 		struct test_obj *obj;
6307e1e7763SThomas Graf 		bool expected = !(i % 2);
6317e1e7763SThomas Graf 		u32 key = i;
6327e1e7763SThomas Graf 
6337e1e7763SThomas Graf 		obj = rhashtable_lookup(ht, &key);
6347e1e7763SThomas Graf 
6357e1e7763SThomas Graf 		if (expected && !obj) {
6367e1e7763SThomas Graf 			pr_warn("Test failed: Could not find key %u\n", key);
6377e1e7763SThomas Graf 			return -ENOENT;
6387e1e7763SThomas Graf 		} else if (!expected && obj) {
6397e1e7763SThomas Graf 			pr_warn("Test failed: Unexpected entry found for key %u\n",
6407e1e7763SThomas Graf 				key);
6417e1e7763SThomas Graf 			return -EEXIST;
6427e1e7763SThomas Graf 		} else if (expected && obj) {
6437e1e7763SThomas Graf 			if (obj->ptr != TEST_PTR || obj->value != i) {
6447e1e7763SThomas Graf 				pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n",
6457e1e7763SThomas Graf 					obj->ptr, TEST_PTR, obj->value, i);
6467e1e7763SThomas Graf 				return -EINVAL;
6477e1e7763SThomas Graf 			}
6487e1e7763SThomas Graf 		}
6497e1e7763SThomas Graf 	}
6507e1e7763SThomas Graf 
6517e1e7763SThomas Graf 	return 0;
6527e1e7763SThomas Graf }
6537e1e7763SThomas Graf 
6547e1e7763SThomas Graf static void test_bucket_stats(struct rhashtable *ht,
6557e1e7763SThomas Graf 				     struct bucket_table *tbl,
6567e1e7763SThomas Graf 				     bool quiet)
6577e1e7763SThomas Graf {
6587e1e7763SThomas Graf 	unsigned int cnt, i, total = 0;
6597e1e7763SThomas Graf 	struct test_obj *obj;
6607e1e7763SThomas Graf 
6617e1e7763SThomas Graf 	for (i = 0; i < tbl->size; i++) {
6627e1e7763SThomas Graf 		cnt = 0;
6637e1e7763SThomas Graf 
6647e1e7763SThomas Graf 		if (!quiet)
6657e1e7763SThomas Graf 			pr_info(" [%#4x/%zu]", i, tbl->size);
6667e1e7763SThomas Graf 
6677e1e7763SThomas Graf 		rht_for_each_entry_rcu(obj, tbl->buckets[i], node) {
6687e1e7763SThomas Graf 			cnt++;
6697e1e7763SThomas Graf 			total++;
6707e1e7763SThomas Graf 			if (!quiet)
6717e1e7763SThomas Graf 				pr_cont(" [%p],", obj);
6727e1e7763SThomas Graf 		}
6737e1e7763SThomas Graf 
6747e1e7763SThomas Graf 		if (!quiet)
6757e1e7763SThomas Graf 			pr_cont("\n  [%#x] first element: %p, chain length: %u\n",
6767e1e7763SThomas Graf 				i, tbl->buckets[i], cnt);
6777e1e7763SThomas Graf 	}
6787e1e7763SThomas Graf 
6797e1e7763SThomas Graf 	pr_info("  Traversal complete: counted=%u, nelems=%zu, entries=%d\n",
6807e1e7763SThomas Graf 		total, ht->nelems, TEST_ENTRIES);
6817e1e7763SThomas Graf }
6827e1e7763SThomas Graf 
6837e1e7763SThomas Graf static int __init test_rhashtable(struct rhashtable *ht)
6847e1e7763SThomas Graf {
6857e1e7763SThomas Graf 	struct bucket_table *tbl;
6867e1e7763SThomas Graf 	struct test_obj *obj, *next;
6877e1e7763SThomas Graf 	int err;
6887e1e7763SThomas Graf 	unsigned int i;
6897e1e7763SThomas Graf 
6907e1e7763SThomas Graf 	/*
6917e1e7763SThomas Graf 	 * Insertion Test:
6927e1e7763SThomas Graf 	 * Insert TEST_ENTRIES into table with all keys even numbers
6937e1e7763SThomas Graf 	 */
6947e1e7763SThomas Graf 	pr_info("  Adding %d keys\n", TEST_ENTRIES);
6957e1e7763SThomas Graf 	for (i = 0; i < TEST_ENTRIES; i++) {
6967e1e7763SThomas Graf 		struct test_obj *obj;
6977e1e7763SThomas Graf 
6987e1e7763SThomas Graf 		obj = kzalloc(sizeof(*obj), GFP_KERNEL);
6997e1e7763SThomas Graf 		if (!obj) {
7007e1e7763SThomas Graf 			err = -ENOMEM;
7017e1e7763SThomas Graf 			goto error;
7027e1e7763SThomas Graf 		}
7037e1e7763SThomas Graf 
7047e1e7763SThomas Graf 		obj->ptr = TEST_PTR;
7057e1e7763SThomas Graf 		obj->value = i * 2;
7067e1e7763SThomas Graf 
7077e1e7763SThomas Graf 		rhashtable_insert(ht, &obj->node, GFP_KERNEL);
7087e1e7763SThomas Graf 	}
7097e1e7763SThomas Graf 
7107e1e7763SThomas Graf 	rcu_read_lock();
7117e1e7763SThomas Graf 	tbl = rht_dereference_rcu(ht->tbl, ht);
7127e1e7763SThomas Graf 	test_bucket_stats(ht, tbl, true);
7137e1e7763SThomas Graf 	test_rht_lookup(ht);
7147e1e7763SThomas Graf 	rcu_read_unlock();
7157e1e7763SThomas Graf 
7167e1e7763SThomas Graf 	for (i = 0; i < TEST_NEXPANDS; i++) {
7177e1e7763SThomas Graf 		pr_info("  Table expansion iteration %u...\n", i);
7187e1e7763SThomas Graf 		rhashtable_expand(ht, GFP_KERNEL);
7197e1e7763SThomas Graf 
7207e1e7763SThomas Graf 		rcu_read_lock();
7217e1e7763SThomas Graf 		pr_info("  Verifying lookups...\n");
7227e1e7763SThomas Graf 		test_rht_lookup(ht);
7237e1e7763SThomas Graf 		rcu_read_unlock();
7247e1e7763SThomas Graf 	}
7257e1e7763SThomas Graf 
7267e1e7763SThomas Graf 	for (i = 0; i < TEST_NEXPANDS; i++) {
7277e1e7763SThomas Graf 		pr_info("  Table shrinkage iteration %u...\n", i);
7287e1e7763SThomas Graf 		rhashtable_shrink(ht, GFP_KERNEL);
7297e1e7763SThomas Graf 
7307e1e7763SThomas Graf 		rcu_read_lock();
7317e1e7763SThomas Graf 		pr_info("  Verifying lookups...\n");
7327e1e7763SThomas Graf 		test_rht_lookup(ht);
7337e1e7763SThomas Graf 		rcu_read_unlock();
7347e1e7763SThomas Graf 	}
7357e1e7763SThomas Graf 
7367e1e7763SThomas Graf 	pr_info("  Deleting %d keys\n", TEST_ENTRIES);
7377e1e7763SThomas Graf 	for (i = 0; i < TEST_ENTRIES; i++) {
7387e1e7763SThomas Graf 		u32 key = i * 2;
7397e1e7763SThomas Graf 
7407e1e7763SThomas Graf 		obj = rhashtable_lookup(ht, &key);
7417e1e7763SThomas Graf 		BUG_ON(!obj);
7427e1e7763SThomas Graf 
7437e1e7763SThomas Graf 		rhashtable_remove(ht, &obj->node, GFP_KERNEL);
7447e1e7763SThomas Graf 		kfree(obj);
7457e1e7763SThomas Graf 	}
7467e1e7763SThomas Graf 
7477e1e7763SThomas Graf 	return 0;
7487e1e7763SThomas Graf 
7497e1e7763SThomas Graf error:
7507e1e7763SThomas Graf 	tbl = rht_dereference_rcu(ht->tbl, ht);
7517e1e7763SThomas Graf 	for (i = 0; i < tbl->size; i++)
7527e1e7763SThomas Graf 		rht_for_each_entry_safe(obj, next, tbl->buckets[i], ht, node)
7537e1e7763SThomas Graf 			kfree(obj);
7547e1e7763SThomas Graf 
7557e1e7763SThomas Graf 	return err;
7567e1e7763SThomas Graf }
7577e1e7763SThomas Graf 
7587e1e7763SThomas Graf static int __init test_rht_init(void)
7597e1e7763SThomas Graf {
7607e1e7763SThomas Graf 	struct rhashtable ht;
7617e1e7763SThomas Graf 	struct rhashtable_params params = {
7627e1e7763SThomas Graf 		.nelem_hint = TEST_HT_SIZE,
7637e1e7763SThomas Graf 		.head_offset = offsetof(struct test_obj, node),
7647e1e7763SThomas Graf 		.key_offset = offsetof(struct test_obj, value),
7657e1e7763SThomas Graf 		.key_len = sizeof(int),
7667e1e7763SThomas Graf 		.hashfn = arch_fast_hash,
7677e1e7763SThomas Graf 		.mutex_is_held = &test_mutex_is_held,
7687e1e7763SThomas Graf 		.grow_decision = rht_grow_above_75,
7697e1e7763SThomas Graf 		.shrink_decision = rht_shrink_below_30,
7707e1e7763SThomas Graf 	};
7717e1e7763SThomas Graf 	int err;
7727e1e7763SThomas Graf 
7737e1e7763SThomas Graf 	pr_info("Running resizable hashtable tests...\n");
7747e1e7763SThomas Graf 
7757e1e7763SThomas Graf 	err = rhashtable_init(&ht, &params);
7767e1e7763SThomas Graf 	if (err < 0) {
7777e1e7763SThomas Graf 		pr_warn("Test failed: Unable to initialize hashtable: %d\n",
7787e1e7763SThomas Graf 			err);
7797e1e7763SThomas Graf 		return err;
7807e1e7763SThomas Graf 	}
7817e1e7763SThomas Graf 
7827e1e7763SThomas Graf 	err = test_rhashtable(&ht);
7837e1e7763SThomas Graf 
7847e1e7763SThomas Graf 	rhashtable_destroy(&ht);
7857e1e7763SThomas Graf 
7867e1e7763SThomas Graf 	return err;
7877e1e7763SThomas Graf }
7887e1e7763SThomas Graf 
7897e1e7763SThomas Graf subsys_initcall(test_rht_init);
7907e1e7763SThomas Graf 
7917e1e7763SThomas Graf #endif /* CONFIG_TEST_RHASHTABLE */
792