xref: /linux/lib/rhashtable.c (revision 9dbeea7f08f3784b152d9fb3b86beb34aad77c72)
17e1e7763SThomas Graf /*
27e1e7763SThomas Graf  * Resizable, Scalable, Concurrent Hash Table
37e1e7763SThomas Graf  *
402fd97c3SHerbert Xu  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
5a5ec68e3SThomas Graf  * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
67e1e7763SThomas Graf  * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
77e1e7763SThomas Graf  *
87e1e7763SThomas Graf  * Code partially derived from nft_hash
902fd97c3SHerbert Xu  * Rewritten with rehash code from br_multicast plus single list
1002fd97c3SHerbert Xu  * pointer as suggested by Josh Triplett
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 
1707ee0722SHerbert Xu #include <linux/atomic.h>
187e1e7763SThomas Graf #include <linux/kernel.h>
197e1e7763SThomas Graf #include <linux/init.h>
207e1e7763SThomas Graf #include <linux/log2.h>
215beb5c90SEric Dumazet #include <linux/sched.h>
227e1e7763SThomas Graf #include <linux/slab.h>
237e1e7763SThomas Graf #include <linux/vmalloc.h>
247e1e7763SThomas Graf #include <linux/mm.h>
2587545899SDaniel Borkmann #include <linux/jhash.h>
267e1e7763SThomas Graf #include <linux/random.h>
277e1e7763SThomas Graf #include <linux/rhashtable.h>
2861d7b097SStephen Rothwell #include <linux/err.h>
296d795413SHauke Mehrtens #include <linux/export.h>
307e1e7763SThomas Graf 
317e1e7763SThomas Graf #define HASH_DEFAULT_SIZE	64UL
32c2e213cfSHerbert Xu #define HASH_MIN_SIZE		4U
334cf0b354SFlorian Westphal #define BUCKET_LOCKS_PER_CPU	32UL
3497defe1eSThomas Graf 
35988dfbd7SHerbert Xu static u32 head_hashfn(struct rhashtable *ht,
368d24c0b4SThomas Graf 		       const struct bucket_table *tbl,
378d24c0b4SThomas Graf 		       const struct rhash_head *he)
387e1e7763SThomas Graf {
3902fd97c3SHerbert Xu 	return rht_head_hashfn(ht, tbl, he, ht->p);
407e1e7763SThomas Graf }
417e1e7763SThomas Graf 
42a03eaec0SThomas Graf #ifdef CONFIG_PROVE_LOCKING
43a03eaec0SThomas Graf #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
44a03eaec0SThomas Graf 
45a03eaec0SThomas Graf int lockdep_rht_mutex_is_held(struct rhashtable *ht)
46a03eaec0SThomas Graf {
47a03eaec0SThomas Graf 	return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
48a03eaec0SThomas Graf }
49a03eaec0SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
50a03eaec0SThomas Graf 
51a03eaec0SThomas Graf int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
52a03eaec0SThomas Graf {
5302fd97c3SHerbert Xu 	spinlock_t *lock = rht_bucket_lock(tbl, hash);
54a03eaec0SThomas Graf 
55a03eaec0SThomas Graf 	return (debug_locks) ? lockdep_is_held(lock) : 1;
56a03eaec0SThomas Graf }
57a03eaec0SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
58a03eaec0SThomas Graf #else
59a03eaec0SThomas Graf #define ASSERT_RHT_MUTEX(HT)
60a03eaec0SThomas Graf #endif
61a03eaec0SThomas Graf 
62a03eaec0SThomas Graf 
63b9ecfdaaSHerbert Xu static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl,
64b9ecfdaaSHerbert Xu 			      gfp_t gfp)
6597defe1eSThomas Graf {
6697defe1eSThomas Graf 	unsigned int i, size;
6797defe1eSThomas Graf #if defined(CONFIG_PROVE_LOCKING)
6897defe1eSThomas Graf 	unsigned int nr_pcpus = 2;
6997defe1eSThomas Graf #else
7097defe1eSThomas Graf 	unsigned int nr_pcpus = num_possible_cpus();
7197defe1eSThomas Graf #endif
7297defe1eSThomas Graf 
734cf0b354SFlorian Westphal 	nr_pcpus = min_t(unsigned int, nr_pcpus, 64UL);
7497defe1eSThomas Graf 	size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
7597defe1eSThomas Graf 
76a5ec68e3SThomas Graf 	/* Never allocate more than 0.5 locks per bucket */
77a5ec68e3SThomas Graf 	size = min_t(unsigned int, size, tbl->size >> 1);
7897defe1eSThomas Graf 
7997defe1eSThomas Graf 	if (sizeof(spinlock_t) != 0) {
80*9dbeea7fSEric Dumazet 		tbl->locks = NULL;
8197defe1eSThomas Graf #ifdef CONFIG_NUMA
82b9ecfdaaSHerbert Xu 		if (size * sizeof(spinlock_t) > PAGE_SIZE &&
83b9ecfdaaSHerbert Xu 		    gfp == GFP_KERNEL)
8497defe1eSThomas Graf 			tbl->locks = vmalloc(size * sizeof(spinlock_t));
8597defe1eSThomas Graf #endif
864cf0b354SFlorian Westphal 		if (gfp != GFP_KERNEL)
874cf0b354SFlorian Westphal 			gfp |= __GFP_NOWARN | __GFP_NORETRY;
884cf0b354SFlorian Westphal 
89*9dbeea7fSEric Dumazet 		if (!tbl->locks)
9097defe1eSThomas Graf 			tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
91b9ecfdaaSHerbert Xu 						   gfp);
9297defe1eSThomas Graf 		if (!tbl->locks)
9397defe1eSThomas Graf 			return -ENOMEM;
9497defe1eSThomas Graf 		for (i = 0; i < size; i++)
9597defe1eSThomas Graf 			spin_lock_init(&tbl->locks[i]);
9697defe1eSThomas Graf 	}
9797defe1eSThomas Graf 	tbl->locks_mask = size - 1;
9897defe1eSThomas Graf 
9997defe1eSThomas Graf 	return 0;
10097defe1eSThomas Graf }
10197defe1eSThomas Graf 
10297defe1eSThomas Graf static void bucket_table_free(const struct bucket_table *tbl)
10397defe1eSThomas Graf {
10497defe1eSThomas Graf 	if (tbl)
10597defe1eSThomas Graf 		kvfree(tbl->locks);
10697defe1eSThomas Graf 
10797defe1eSThomas Graf 	kvfree(tbl);
10897defe1eSThomas Graf }
10997defe1eSThomas Graf 
1109d901bc0SHerbert Xu static void bucket_table_free_rcu(struct rcu_head *head)
1119d901bc0SHerbert Xu {
1129d901bc0SHerbert Xu 	bucket_table_free(container_of(head, struct bucket_table, rcu));
1139d901bc0SHerbert Xu }
1149d901bc0SHerbert Xu 
11597defe1eSThomas Graf static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
116b9ecfdaaSHerbert Xu 					       size_t nbuckets,
117b9ecfdaaSHerbert Xu 					       gfp_t gfp)
1187e1e7763SThomas Graf {
119eb6d1abfSDaniel Borkmann 	struct bucket_table *tbl = NULL;
1207e1e7763SThomas Graf 	size_t size;
121f89bd6f8SThomas Graf 	int i;
1227e1e7763SThomas Graf 
1237e1e7763SThomas Graf 	size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
124b9ecfdaaSHerbert Xu 	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) ||
125b9ecfdaaSHerbert Xu 	    gfp != GFP_KERNEL)
126b9ecfdaaSHerbert Xu 		tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
127b9ecfdaaSHerbert Xu 	if (tbl == NULL && gfp == GFP_KERNEL)
1287e1e7763SThomas Graf 		tbl = vzalloc(size);
1297e1e7763SThomas Graf 	if (tbl == NULL)
1307e1e7763SThomas Graf 		return NULL;
1317e1e7763SThomas Graf 
1327e1e7763SThomas Graf 	tbl->size = nbuckets;
1337e1e7763SThomas Graf 
134b9ecfdaaSHerbert Xu 	if (alloc_bucket_locks(ht, tbl, gfp) < 0) {
13597defe1eSThomas Graf 		bucket_table_free(tbl);
13697defe1eSThomas Graf 		return NULL;
1377e1e7763SThomas Graf 	}
1387e1e7763SThomas Graf 
139eddee5baSHerbert Xu 	INIT_LIST_HEAD(&tbl->walkers);
140eddee5baSHerbert Xu 
1415269b53dSHerbert Xu 	get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
1425269b53dSHerbert Xu 
143f89bd6f8SThomas Graf 	for (i = 0; i < nbuckets; i++)
144f89bd6f8SThomas Graf 		INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
145f89bd6f8SThomas Graf 
14697defe1eSThomas Graf 	return tbl;
1477e1e7763SThomas Graf }
1487e1e7763SThomas Graf 
149b824478bSHerbert Xu static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
150b824478bSHerbert Xu 						  struct bucket_table *tbl)
151b824478bSHerbert Xu {
152b824478bSHerbert Xu 	struct bucket_table *new_tbl;
153b824478bSHerbert Xu 
154b824478bSHerbert Xu 	do {
155b824478bSHerbert Xu 		new_tbl = tbl;
156b824478bSHerbert Xu 		tbl = rht_dereference_rcu(tbl->future_tbl, ht);
157b824478bSHerbert Xu 	} while (tbl);
158b824478bSHerbert Xu 
159b824478bSHerbert Xu 	return new_tbl;
160b824478bSHerbert Xu }
161b824478bSHerbert Xu 
162299e5c32SThomas Graf static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
163a5ec68e3SThomas Graf {
164aa34a6cbSHerbert Xu 	struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
165b824478bSHerbert Xu 	struct bucket_table *new_tbl = rhashtable_last_table(ht,
166b824478bSHerbert Xu 		rht_dereference_rcu(old_tbl->future_tbl, ht));
167aa34a6cbSHerbert Xu 	struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
168aa34a6cbSHerbert Xu 	int err = -ENOENT;
169aa34a6cbSHerbert Xu 	struct rhash_head *head, *next, *entry;
170aa34a6cbSHerbert Xu 	spinlock_t *new_bucket_lock;
171299e5c32SThomas Graf 	unsigned int new_hash;
172a5ec68e3SThomas Graf 
173aa34a6cbSHerbert Xu 	rht_for_each(entry, old_tbl, old_hash) {
174aa34a6cbSHerbert Xu 		err = 0;
175aa34a6cbSHerbert Xu 		next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
176a5ec68e3SThomas Graf 
177aa34a6cbSHerbert Xu 		if (rht_is_a_nulls(next))
1787e1e7763SThomas Graf 			break;
17997defe1eSThomas Graf 
180aa34a6cbSHerbert Xu 		pprev = &entry->next;
1817e1e7763SThomas Graf 	}
1827e1e7763SThomas Graf 
183aa34a6cbSHerbert Xu 	if (err)
184aa34a6cbSHerbert Xu 		goto out;
18597defe1eSThomas Graf 
186aa34a6cbSHerbert Xu 	new_hash = head_hashfn(ht, new_tbl, entry);
187a5ec68e3SThomas Graf 
18802fd97c3SHerbert Xu 	new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
189aa34a6cbSHerbert Xu 
1908f2484bdSHerbert Xu 	spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
191aa34a6cbSHerbert Xu 	head = rht_dereference_bucket(new_tbl->buckets[new_hash],
192aa34a6cbSHerbert Xu 				      new_tbl, new_hash);
193aa34a6cbSHerbert Xu 
194aa34a6cbSHerbert Xu 	RCU_INIT_POINTER(entry->next, head);
195aa34a6cbSHerbert Xu 
196aa34a6cbSHerbert Xu 	rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
197aa34a6cbSHerbert Xu 	spin_unlock(new_bucket_lock);
198aa34a6cbSHerbert Xu 
199aa34a6cbSHerbert Xu 	rcu_assign_pointer(*pprev, next);
200aa34a6cbSHerbert Xu 
201aa34a6cbSHerbert Xu out:
202aa34a6cbSHerbert Xu 	return err;
20397defe1eSThomas Graf }
20497defe1eSThomas Graf 
205299e5c32SThomas Graf static void rhashtable_rehash_chain(struct rhashtable *ht,
206299e5c32SThomas Graf 				    unsigned int old_hash)
20797defe1eSThomas Graf {
208aa34a6cbSHerbert Xu 	struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
209aa34a6cbSHerbert Xu 	spinlock_t *old_bucket_lock;
2107cd10db8SThomas Graf 
21102fd97c3SHerbert Xu 	old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
212aa34a6cbSHerbert Xu 
213aa34a6cbSHerbert Xu 	spin_lock_bh(old_bucket_lock);
214aa34a6cbSHerbert Xu 	while (!rhashtable_rehash_one(ht, old_hash))
215aa34a6cbSHerbert Xu 		;
21663d512d0SHerbert Xu 	old_tbl->rehash++;
217aa34a6cbSHerbert Xu 	spin_unlock_bh(old_bucket_lock);
218aa34a6cbSHerbert Xu }
219aa34a6cbSHerbert Xu 
220b824478bSHerbert Xu static int rhashtable_rehash_attach(struct rhashtable *ht,
221b824478bSHerbert Xu 				    struct bucket_table *old_tbl,
222aa34a6cbSHerbert Xu 				    struct bucket_table *new_tbl)
223aa34a6cbSHerbert Xu {
224b824478bSHerbert Xu 	/* Protect future_tbl using the first bucket lock. */
225b824478bSHerbert Xu 	spin_lock_bh(old_tbl->locks);
226b824478bSHerbert Xu 
227b824478bSHerbert Xu 	/* Did somebody beat us to it? */
228b824478bSHerbert Xu 	if (rcu_access_pointer(old_tbl->future_tbl)) {
229b824478bSHerbert Xu 		spin_unlock_bh(old_tbl->locks);
230b824478bSHerbert Xu 		return -EEXIST;
231b824478bSHerbert Xu 	}
232aa34a6cbSHerbert Xu 
233aa34a6cbSHerbert Xu 	/* Make insertions go into the new, empty table right away. Deletions
234aa34a6cbSHerbert Xu 	 * and lookups will be attempted in both tables until we synchronize.
235aa34a6cbSHerbert Xu 	 */
236c4db8848SHerbert Xu 	rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
237aa34a6cbSHerbert Xu 
238b824478bSHerbert Xu 	spin_unlock_bh(old_tbl->locks);
239b824478bSHerbert Xu 
240b824478bSHerbert Xu 	return 0;
241b824478bSHerbert Xu }
242b824478bSHerbert Xu 
243b824478bSHerbert Xu static int rhashtable_rehash_table(struct rhashtable *ht)
244b824478bSHerbert Xu {
245b824478bSHerbert Xu 	struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
246b824478bSHerbert Xu 	struct bucket_table *new_tbl;
247b824478bSHerbert Xu 	struct rhashtable_walker *walker;
248299e5c32SThomas Graf 	unsigned int old_hash;
249b824478bSHerbert Xu 
250b824478bSHerbert Xu 	new_tbl = rht_dereference(old_tbl->future_tbl, ht);
251b824478bSHerbert Xu 	if (!new_tbl)
252b824478bSHerbert Xu 		return 0;
253b824478bSHerbert Xu 
254aa34a6cbSHerbert Xu 	for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
255aa34a6cbSHerbert Xu 		rhashtable_rehash_chain(ht, old_hash);
256aa34a6cbSHerbert Xu 
257aa34a6cbSHerbert Xu 	/* Publish the new table pointer. */
258aa34a6cbSHerbert Xu 	rcu_assign_pointer(ht->tbl, new_tbl);
259aa34a6cbSHerbert Xu 
260ba7c95eaSHerbert Xu 	spin_lock(&ht->lock);
261eddee5baSHerbert Xu 	list_for_each_entry(walker, &old_tbl->walkers, list)
262eddee5baSHerbert Xu 		walker->tbl = NULL;
263ba7c95eaSHerbert Xu 	spin_unlock(&ht->lock);
264eddee5baSHerbert Xu 
265aa34a6cbSHerbert Xu 	/* Wait for readers. All new readers will see the new
266aa34a6cbSHerbert Xu 	 * table, and thus no references to the old table will
267aa34a6cbSHerbert Xu 	 * remain.
268aa34a6cbSHerbert Xu 	 */
2699d901bc0SHerbert Xu 	call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
270b824478bSHerbert Xu 
271b824478bSHerbert Xu 	return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
2727e1e7763SThomas Graf }
2737e1e7763SThomas Graf 
2747e1e7763SThomas Graf /**
2757e1e7763SThomas Graf  * rhashtable_expand - Expand hash table while allowing concurrent lookups
2767e1e7763SThomas Graf  * @ht:		the hash table to expand
2777e1e7763SThomas Graf  *
278aa34a6cbSHerbert Xu  * A secondary bucket array is allocated and the hash entries are migrated.
2797e1e7763SThomas Graf  *
2807e1e7763SThomas Graf  * This function may only be called in a context where it is safe to call
2817e1e7763SThomas Graf  * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
2827e1e7763SThomas Graf  *
28397defe1eSThomas Graf  * The caller must ensure that no concurrent resizing occurs by holding
28497defe1eSThomas Graf  * ht->mutex.
28597defe1eSThomas Graf  *
28697defe1eSThomas Graf  * It is valid to have concurrent insertions and deletions protected by per
28797defe1eSThomas Graf  * bucket locks or concurrent RCU protected lookups and traversals.
2887e1e7763SThomas Graf  */
289b824478bSHerbert Xu static int rhashtable_expand(struct rhashtable *ht)
2907e1e7763SThomas Graf {
2917e1e7763SThomas Graf 	struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
292b824478bSHerbert Xu 	int err;
2937e1e7763SThomas Graf 
2947e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
2957e1e7763SThomas Graf 
296b824478bSHerbert Xu 	old_tbl = rhashtable_last_table(ht, old_tbl);
297b824478bSHerbert Xu 
298b9ecfdaaSHerbert Xu 	new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, GFP_KERNEL);
2997e1e7763SThomas Graf 	if (new_tbl == NULL)
3007e1e7763SThomas Graf 		return -ENOMEM;
3017e1e7763SThomas Graf 
302b824478bSHerbert Xu 	err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
303b824478bSHerbert Xu 	if (err)
304b824478bSHerbert Xu 		bucket_table_free(new_tbl);
305b824478bSHerbert Xu 
306b824478bSHerbert Xu 	return err;
3077e1e7763SThomas Graf }
3087e1e7763SThomas Graf 
3097e1e7763SThomas Graf /**
3107e1e7763SThomas Graf  * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
3117e1e7763SThomas Graf  * @ht:		the hash table to shrink
3127e1e7763SThomas Graf  *
31318093d1cSHerbert Xu  * This function shrinks the hash table to fit, i.e., the smallest
31418093d1cSHerbert Xu  * size would not cause it to expand right away automatically.
3157e1e7763SThomas Graf  *
31697defe1eSThomas Graf  * The caller must ensure that no concurrent resizing occurs by holding
31797defe1eSThomas Graf  * ht->mutex.
31897defe1eSThomas Graf  *
3197e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations take place.
3207e1e7763SThomas Graf  * It is however valid to have concurrent lookups if they are RCU protected.
32197defe1eSThomas Graf  *
32297defe1eSThomas Graf  * It is valid to have concurrent insertions and deletions protected by per
32397defe1eSThomas Graf  * bucket locks or concurrent RCU protected lookups and traversals.
3247e1e7763SThomas Graf  */
325b824478bSHerbert Xu static int rhashtable_shrink(struct rhashtable *ht)
3267e1e7763SThomas Graf {
327a5b6846fSDaniel Borkmann 	struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
32812311959SVegard Nossum 	unsigned int nelems = atomic_read(&ht->nelems);
32912311959SVegard Nossum 	unsigned int size = 0;
330b824478bSHerbert Xu 	int err;
3317e1e7763SThomas Graf 
3327e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
3337e1e7763SThomas Graf 
33412311959SVegard Nossum 	if (nelems)
33512311959SVegard Nossum 		size = roundup_pow_of_two(nelems * 3 / 2);
33618093d1cSHerbert Xu 	if (size < ht->p.min_size)
33718093d1cSHerbert Xu 		size = ht->p.min_size;
33818093d1cSHerbert Xu 
33918093d1cSHerbert Xu 	if (old_tbl->size <= size)
34018093d1cSHerbert Xu 		return 0;
34118093d1cSHerbert Xu 
342b824478bSHerbert Xu 	if (rht_dereference(old_tbl->future_tbl, ht))
343b824478bSHerbert Xu 		return -EEXIST;
344b824478bSHerbert Xu 
345b9ecfdaaSHerbert Xu 	new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
34697defe1eSThomas Graf 	if (new_tbl == NULL)
3477e1e7763SThomas Graf 		return -ENOMEM;
3487e1e7763SThomas Graf 
349b824478bSHerbert Xu 	err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
350b824478bSHerbert Xu 	if (err)
351b824478bSHerbert Xu 		bucket_table_free(new_tbl);
352b824478bSHerbert Xu 
353b824478bSHerbert Xu 	return err;
3547e1e7763SThomas Graf }
3557e1e7763SThomas Graf 
35697defe1eSThomas Graf static void rht_deferred_worker(struct work_struct *work)
35797defe1eSThomas Graf {
35897defe1eSThomas Graf 	struct rhashtable *ht;
35997defe1eSThomas Graf 	struct bucket_table *tbl;
360b824478bSHerbert Xu 	int err = 0;
36197defe1eSThomas Graf 
36257699a40SYing Xue 	ht = container_of(work, struct rhashtable, run_work);
36397defe1eSThomas Graf 	mutex_lock(&ht->mutex);
36428134a53SHerbert Xu 
36597defe1eSThomas Graf 	tbl = rht_dereference(ht->tbl, ht);
366b824478bSHerbert Xu 	tbl = rhashtable_last_table(ht, tbl);
36797defe1eSThomas Graf 
368a5b6846fSDaniel Borkmann 	if (rht_grow_above_75(ht, tbl))
36997defe1eSThomas Graf 		rhashtable_expand(ht);
370b5e2c150SThomas Graf 	else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
37197defe1eSThomas Graf 		rhashtable_shrink(ht);
372b824478bSHerbert Xu 
373b824478bSHerbert Xu 	err = rhashtable_rehash_table(ht);
374b824478bSHerbert Xu 
37597defe1eSThomas Graf 	mutex_unlock(&ht->mutex);
376b824478bSHerbert Xu 
377b824478bSHerbert Xu 	if (err)
378b824478bSHerbert Xu 		schedule_work(&ht->run_work);
37997defe1eSThomas Graf }
38097defe1eSThomas Graf 
381ccd57b1bSHerbert Xu static bool rhashtable_check_elasticity(struct rhashtable *ht,
382ccd57b1bSHerbert Xu 					struct bucket_table *tbl,
383299e5c32SThomas Graf 					unsigned int hash)
384ccd57b1bSHerbert Xu {
385299e5c32SThomas Graf 	unsigned int elasticity = ht->elasticity;
386ccd57b1bSHerbert Xu 	struct rhash_head *head;
387ccd57b1bSHerbert Xu 
388ccd57b1bSHerbert Xu 	rht_for_each(head, tbl, hash)
389ccd57b1bSHerbert Xu 		if (!--elasticity)
390ccd57b1bSHerbert Xu 			return true;
391ccd57b1bSHerbert Xu 
392ccd57b1bSHerbert Xu 	return false;
393ccd57b1bSHerbert Xu }
394ccd57b1bSHerbert Xu 
3953cf92222SHerbert Xu int rhashtable_insert_rehash(struct rhashtable *ht,
3963cf92222SHerbert Xu 			     struct bucket_table *tbl)
397ccd57b1bSHerbert Xu {
398ccd57b1bSHerbert Xu 	struct bucket_table *old_tbl;
399ccd57b1bSHerbert Xu 	struct bucket_table *new_tbl;
400ccd57b1bSHerbert Xu 	unsigned int size;
401ccd57b1bSHerbert Xu 	int err;
402ccd57b1bSHerbert Xu 
403ccd57b1bSHerbert Xu 	old_tbl = rht_dereference_rcu(ht->tbl, ht);
404ccd57b1bSHerbert Xu 
405ccd57b1bSHerbert Xu 	size = tbl->size;
406ccd57b1bSHerbert Xu 
4073cf92222SHerbert Xu 	err = -EBUSY;
4083cf92222SHerbert Xu 
409ccd57b1bSHerbert Xu 	if (rht_grow_above_75(ht, tbl))
410ccd57b1bSHerbert Xu 		size *= 2;
411a87b9ebfSThomas Graf 	/* Do not schedule more than one rehash */
412a87b9ebfSThomas Graf 	else if (old_tbl != tbl)
4133cf92222SHerbert Xu 		goto fail;
4143cf92222SHerbert Xu 
4153cf92222SHerbert Xu 	err = -ENOMEM;
416ccd57b1bSHerbert Xu 
417ccd57b1bSHerbert Xu 	new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
4183cf92222SHerbert Xu 	if (new_tbl == NULL)
4193cf92222SHerbert Xu 		goto fail;
420ccd57b1bSHerbert Xu 
421ccd57b1bSHerbert Xu 	err = rhashtable_rehash_attach(ht, tbl, new_tbl);
422ccd57b1bSHerbert Xu 	if (err) {
423ccd57b1bSHerbert Xu 		bucket_table_free(new_tbl);
424ccd57b1bSHerbert Xu 		if (err == -EEXIST)
425ccd57b1bSHerbert Xu 			err = 0;
426ccd57b1bSHerbert Xu 	} else
427ccd57b1bSHerbert Xu 		schedule_work(&ht->run_work);
428ccd57b1bSHerbert Xu 
429ccd57b1bSHerbert Xu 	return err;
4303cf92222SHerbert Xu 
4313cf92222SHerbert Xu fail:
4323cf92222SHerbert Xu 	/* Do not fail the insert if someone else did a rehash. */
4333cf92222SHerbert Xu 	if (likely(rcu_dereference_raw(tbl->future_tbl)))
4343cf92222SHerbert Xu 		return 0;
4353cf92222SHerbert Xu 
4363cf92222SHerbert Xu 	/* Schedule async rehash to retry allocation in process context. */
4373cf92222SHerbert Xu 	if (err == -ENOMEM)
4383cf92222SHerbert Xu 		schedule_work(&ht->run_work);
4393cf92222SHerbert Xu 
4403cf92222SHerbert Xu 	return err;
441ccd57b1bSHerbert Xu }
442ccd57b1bSHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_insert_rehash);
443ccd57b1bSHerbert Xu 
4443cf92222SHerbert Xu struct bucket_table *rhashtable_insert_slow(struct rhashtable *ht,
4453cf92222SHerbert Xu 					    const void *key,
44602fd97c3SHerbert Xu 					    struct rhash_head *obj,
44702fd97c3SHerbert Xu 					    struct bucket_table *tbl)
44802fd97c3SHerbert Xu {
44902fd97c3SHerbert Xu 	struct rhash_head *head;
450299e5c32SThomas Graf 	unsigned int hash;
451ccd57b1bSHerbert Xu 	int err;
45202fd97c3SHerbert Xu 
453b824478bSHerbert Xu 	tbl = rhashtable_last_table(ht, tbl);
45402fd97c3SHerbert Xu 	hash = head_hashfn(ht, tbl, obj);
45502fd97c3SHerbert Xu 	spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
45602fd97c3SHerbert Xu 
457ccd57b1bSHerbert Xu 	err = -EEXIST;
45802fd97c3SHerbert Xu 	if (key && rhashtable_lookup_fast(ht, key, ht->p))
45902fd97c3SHerbert Xu 		goto exit;
46002fd97c3SHerbert Xu 
46107ee0722SHerbert Xu 	err = -E2BIG;
46207ee0722SHerbert Xu 	if (unlikely(rht_grow_above_max(ht, tbl)))
46307ee0722SHerbert Xu 		goto exit;
46407ee0722SHerbert Xu 
465ccd57b1bSHerbert Xu 	err = -EAGAIN;
466ccd57b1bSHerbert Xu 	if (rhashtable_check_elasticity(ht, tbl, hash) ||
467ccd57b1bSHerbert Xu 	    rht_grow_above_100(ht, tbl))
468ccd57b1bSHerbert Xu 		goto exit;
469ccd57b1bSHerbert Xu 
47002fd97c3SHerbert Xu 	err = 0;
47102fd97c3SHerbert Xu 
47202fd97c3SHerbert Xu 	head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
47302fd97c3SHerbert Xu 
47402fd97c3SHerbert Xu 	RCU_INIT_POINTER(obj->next, head);
47502fd97c3SHerbert Xu 
47602fd97c3SHerbert Xu 	rcu_assign_pointer(tbl->buckets[hash], obj);
47702fd97c3SHerbert Xu 
47802fd97c3SHerbert Xu 	atomic_inc(&ht->nelems);
47902fd97c3SHerbert Xu 
48002fd97c3SHerbert Xu exit:
48102fd97c3SHerbert Xu 	spin_unlock(rht_bucket_lock(tbl, hash));
48202fd97c3SHerbert Xu 
4833cf92222SHerbert Xu 	if (err == 0)
4843cf92222SHerbert Xu 		return NULL;
4853cf92222SHerbert Xu 	else if (err == -EAGAIN)
4863cf92222SHerbert Xu 		return tbl;
4873cf92222SHerbert Xu 	else
4883cf92222SHerbert Xu 		return ERR_PTR(err);
48902fd97c3SHerbert Xu }
49002fd97c3SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
49102fd97c3SHerbert Xu 
492f2dba9c6SHerbert Xu /**
493f2dba9c6SHerbert Xu  * rhashtable_walk_init - Initialise an iterator
494f2dba9c6SHerbert Xu  * @ht:		Table to walk over
495f2dba9c6SHerbert Xu  * @iter:	Hash table Iterator
4968f6fd83cSBob Copeland  * @gfp:	GFP flags for allocations
497f2dba9c6SHerbert Xu  *
498f2dba9c6SHerbert Xu  * This function prepares a hash table walk.
499f2dba9c6SHerbert Xu  *
500f2dba9c6SHerbert Xu  * Note that if you restart a walk after rhashtable_walk_stop you
501f2dba9c6SHerbert Xu  * may see the same object twice.  Also, you may miss objects if
502f2dba9c6SHerbert Xu  * there are removals in between rhashtable_walk_stop and the next
503f2dba9c6SHerbert Xu  * call to rhashtable_walk_start.
504f2dba9c6SHerbert Xu  *
505f2dba9c6SHerbert Xu  * For a completely stable walk you should construct your own data
506f2dba9c6SHerbert Xu  * structure outside the hash table.
507f2dba9c6SHerbert Xu  *
508f2dba9c6SHerbert Xu  * This function may sleep so you must not call it from interrupt
509f2dba9c6SHerbert Xu  * context or with spin locks held.
510f2dba9c6SHerbert Xu  *
511f2dba9c6SHerbert Xu  * You must call rhashtable_walk_exit if this function returns
512f2dba9c6SHerbert Xu  * successfully.
513f2dba9c6SHerbert Xu  */
5148f6fd83cSBob Copeland int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter,
5158f6fd83cSBob Copeland 			 gfp_t gfp)
516f2dba9c6SHerbert Xu {
517f2dba9c6SHerbert Xu 	iter->ht = ht;
518f2dba9c6SHerbert Xu 	iter->p = NULL;
519f2dba9c6SHerbert Xu 	iter->slot = 0;
520f2dba9c6SHerbert Xu 	iter->skip = 0;
521f2dba9c6SHerbert Xu 
5228f6fd83cSBob Copeland 	iter->walker = kmalloc(sizeof(*iter->walker), gfp);
523f2dba9c6SHerbert Xu 	if (!iter->walker)
524f2dba9c6SHerbert Xu 		return -ENOMEM;
525f2dba9c6SHerbert Xu 
526c6ff5268SHerbert Xu 	spin_lock(&ht->lock);
527179ccc0aSHerbert Xu 	iter->walker->tbl =
528179ccc0aSHerbert Xu 		rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
529eddee5baSHerbert Xu 	list_add(&iter->walker->list, &iter->walker->tbl->walkers);
530c6ff5268SHerbert Xu 	spin_unlock(&ht->lock);
531f2dba9c6SHerbert Xu 
532f2dba9c6SHerbert Xu 	return 0;
533f2dba9c6SHerbert Xu }
534f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_init);
535f2dba9c6SHerbert Xu 
536f2dba9c6SHerbert Xu /**
537f2dba9c6SHerbert Xu  * rhashtable_walk_exit - Free an iterator
538f2dba9c6SHerbert Xu  * @iter:	Hash table Iterator
539f2dba9c6SHerbert Xu  *
540f2dba9c6SHerbert Xu  * This function frees resources allocated by rhashtable_walk_init.
541f2dba9c6SHerbert Xu  */
542f2dba9c6SHerbert Xu void rhashtable_walk_exit(struct rhashtable_iter *iter)
543f2dba9c6SHerbert Xu {
544c6ff5268SHerbert Xu 	spin_lock(&iter->ht->lock);
545eddee5baSHerbert Xu 	if (iter->walker->tbl)
546f2dba9c6SHerbert Xu 		list_del(&iter->walker->list);
547c6ff5268SHerbert Xu 	spin_unlock(&iter->ht->lock);
548f2dba9c6SHerbert Xu 	kfree(iter->walker);
549f2dba9c6SHerbert Xu }
550f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
551f2dba9c6SHerbert Xu 
552f2dba9c6SHerbert Xu /**
553f2dba9c6SHerbert Xu  * rhashtable_walk_start - Start a hash table walk
554f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
555f2dba9c6SHerbert Xu  *
556f2dba9c6SHerbert Xu  * Start a hash table walk.  Note that we take the RCU lock in all
557f2dba9c6SHerbert Xu  * cases including when we return an error.  So you must always call
558f2dba9c6SHerbert Xu  * rhashtable_walk_stop to clean up.
559f2dba9c6SHerbert Xu  *
560f2dba9c6SHerbert Xu  * Returns zero if successful.
561f2dba9c6SHerbert Xu  *
562f2dba9c6SHerbert Xu  * Returns -EAGAIN if resize event occured.  Note that the iterator
563f2dba9c6SHerbert Xu  * will rewind back to the beginning and you may use it immediately
564f2dba9c6SHerbert Xu  * by calling rhashtable_walk_next.
565f2dba9c6SHerbert Xu  */
566f2dba9c6SHerbert Xu int rhashtable_walk_start(struct rhashtable_iter *iter)
567db4374f4SThomas Graf 	__acquires(RCU)
568f2dba9c6SHerbert Xu {
569eddee5baSHerbert Xu 	struct rhashtable *ht = iter->ht;
570eddee5baSHerbert Xu 
571f2dba9c6SHerbert Xu 	rcu_read_lock();
572f2dba9c6SHerbert Xu 
573c6ff5268SHerbert Xu 	spin_lock(&ht->lock);
574c6ff5268SHerbert Xu 	if (iter->walker->tbl)
575c6ff5268SHerbert Xu 		list_del(&iter->walker->list);
576c6ff5268SHerbert Xu 	spin_unlock(&ht->lock);
577eddee5baSHerbert Xu 
578eddee5baSHerbert Xu 	if (!iter->walker->tbl) {
579eddee5baSHerbert Xu 		iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
580f2dba9c6SHerbert Xu 		return -EAGAIN;
581f2dba9c6SHerbert Xu 	}
582f2dba9c6SHerbert Xu 
583f2dba9c6SHerbert Xu 	return 0;
584f2dba9c6SHerbert Xu }
585f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_start);
586f2dba9c6SHerbert Xu 
587f2dba9c6SHerbert Xu /**
588f2dba9c6SHerbert Xu  * rhashtable_walk_next - Return the next object and advance the iterator
589f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
590f2dba9c6SHerbert Xu  *
591f2dba9c6SHerbert Xu  * Note that you must call rhashtable_walk_stop when you are finished
592f2dba9c6SHerbert Xu  * with the walk.
593f2dba9c6SHerbert Xu  *
594f2dba9c6SHerbert Xu  * Returns the next object or NULL when the end of the table is reached.
595f2dba9c6SHerbert Xu  *
596f2dba9c6SHerbert Xu  * Returns -EAGAIN if resize event occured.  Note that the iterator
597f2dba9c6SHerbert Xu  * will rewind back to the beginning and you may continue to use it.
598f2dba9c6SHerbert Xu  */
599f2dba9c6SHerbert Xu void *rhashtable_walk_next(struct rhashtable_iter *iter)
600f2dba9c6SHerbert Xu {
601eddee5baSHerbert Xu 	struct bucket_table *tbl = iter->walker->tbl;
602f2dba9c6SHerbert Xu 	struct rhashtable *ht = iter->ht;
603f2dba9c6SHerbert Xu 	struct rhash_head *p = iter->p;
604f2dba9c6SHerbert Xu 
605f2dba9c6SHerbert Xu 	if (p) {
606f2dba9c6SHerbert Xu 		p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
607f2dba9c6SHerbert Xu 		goto next;
608f2dba9c6SHerbert Xu 	}
609f2dba9c6SHerbert Xu 
610f2dba9c6SHerbert Xu 	for (; iter->slot < tbl->size; iter->slot++) {
611f2dba9c6SHerbert Xu 		int skip = iter->skip;
612f2dba9c6SHerbert Xu 
613f2dba9c6SHerbert Xu 		rht_for_each_rcu(p, tbl, iter->slot) {
614f2dba9c6SHerbert Xu 			if (!skip)
615f2dba9c6SHerbert Xu 				break;
616f2dba9c6SHerbert Xu 			skip--;
617f2dba9c6SHerbert Xu 		}
618f2dba9c6SHerbert Xu 
619f2dba9c6SHerbert Xu next:
620f2dba9c6SHerbert Xu 		if (!rht_is_a_nulls(p)) {
621f2dba9c6SHerbert Xu 			iter->skip++;
622f2dba9c6SHerbert Xu 			iter->p = p;
623c936a79fSThomas Graf 			return rht_obj(ht, p);
624f2dba9c6SHerbert Xu 		}
625f2dba9c6SHerbert Xu 
626f2dba9c6SHerbert Xu 		iter->skip = 0;
627f2dba9c6SHerbert Xu 	}
628f2dba9c6SHerbert Xu 
629142b942aSPhil Sutter 	iter->p = NULL;
630142b942aSPhil Sutter 
631d88252f9SHerbert Xu 	/* Ensure we see any new tables. */
632d88252f9SHerbert Xu 	smp_rmb();
633d88252f9SHerbert Xu 
634c4db8848SHerbert Xu 	iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
635c4db8848SHerbert Xu 	if (iter->walker->tbl) {
636eddee5baSHerbert Xu 		iter->slot = 0;
637eddee5baSHerbert Xu 		iter->skip = 0;
638eddee5baSHerbert Xu 		return ERR_PTR(-EAGAIN);
639eddee5baSHerbert Xu 	}
640eddee5baSHerbert Xu 
641c936a79fSThomas Graf 	return NULL;
642f2dba9c6SHerbert Xu }
643f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_next);
644f2dba9c6SHerbert Xu 
645f2dba9c6SHerbert Xu /**
646f2dba9c6SHerbert Xu  * rhashtable_walk_stop - Finish a hash table walk
647f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
648f2dba9c6SHerbert Xu  *
649f2dba9c6SHerbert Xu  * Finish a hash table walk.
650f2dba9c6SHerbert Xu  */
651f2dba9c6SHerbert Xu void rhashtable_walk_stop(struct rhashtable_iter *iter)
652db4374f4SThomas Graf 	__releases(RCU)
653f2dba9c6SHerbert Xu {
654eddee5baSHerbert Xu 	struct rhashtable *ht;
655eddee5baSHerbert Xu 	struct bucket_table *tbl = iter->walker->tbl;
656eddee5baSHerbert Xu 
657eddee5baSHerbert Xu 	if (!tbl)
658963ecbd4SHerbert Xu 		goto out;
659eddee5baSHerbert Xu 
660eddee5baSHerbert Xu 	ht = iter->ht;
661eddee5baSHerbert Xu 
662ba7c95eaSHerbert Xu 	spin_lock(&ht->lock);
663c4db8848SHerbert Xu 	if (tbl->rehash < tbl->size)
664eddee5baSHerbert Xu 		list_add(&iter->walker->list, &tbl->walkers);
665eddee5baSHerbert Xu 	else
666eddee5baSHerbert Xu 		iter->walker->tbl = NULL;
667ba7c95eaSHerbert Xu 	spin_unlock(&ht->lock);
668eddee5baSHerbert Xu 
669f2dba9c6SHerbert Xu 	iter->p = NULL;
670963ecbd4SHerbert Xu 
671963ecbd4SHerbert Xu out:
672963ecbd4SHerbert Xu 	rcu_read_unlock();
673f2dba9c6SHerbert Xu }
674f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
675f2dba9c6SHerbert Xu 
676488fb86eSHerbert Xu static size_t rounded_hashtable_size(const struct rhashtable_params *params)
6777e1e7763SThomas Graf {
67894000176SYing Xue 	return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
679e2e21c1cSHerbert Xu 		   (unsigned long)params->min_size);
6807e1e7763SThomas Graf }
6817e1e7763SThomas Graf 
68231ccde2dSHerbert Xu static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
68331ccde2dSHerbert Xu {
68431ccde2dSHerbert Xu 	return jhash2(key, length, seed);
68531ccde2dSHerbert Xu }
68631ccde2dSHerbert Xu 
6877e1e7763SThomas Graf /**
6887e1e7763SThomas Graf  * rhashtable_init - initialize a new hash table
6897e1e7763SThomas Graf  * @ht:		hash table to be initialized
6907e1e7763SThomas Graf  * @params:	configuration parameters
6917e1e7763SThomas Graf  *
6927e1e7763SThomas Graf  * Initializes a new hash table based on the provided configuration
6937e1e7763SThomas Graf  * parameters. A table can be configured either with a variable or
6947e1e7763SThomas Graf  * fixed length key:
6957e1e7763SThomas Graf  *
6967e1e7763SThomas Graf  * Configuration Example 1: Fixed length keys
6977e1e7763SThomas Graf  * struct test_obj {
6987e1e7763SThomas Graf  *	int			key;
6997e1e7763SThomas Graf  *	void *			my_member;
7007e1e7763SThomas Graf  *	struct rhash_head	node;
7017e1e7763SThomas Graf  * };
7027e1e7763SThomas Graf  *
7037e1e7763SThomas Graf  * struct rhashtable_params params = {
7047e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
7057e1e7763SThomas Graf  *	.key_offset = offsetof(struct test_obj, key),
7067e1e7763SThomas Graf  *	.key_len = sizeof(int),
70787545899SDaniel Borkmann  *	.hashfn = jhash,
708f89bd6f8SThomas Graf  *	.nulls_base = (1U << RHT_BASE_SHIFT),
7097e1e7763SThomas Graf  * };
7107e1e7763SThomas Graf  *
7117e1e7763SThomas Graf  * Configuration Example 2: Variable length keys
7127e1e7763SThomas Graf  * struct test_obj {
7137e1e7763SThomas Graf  *	[...]
7147e1e7763SThomas Graf  *	struct rhash_head	node;
7157e1e7763SThomas Graf  * };
7167e1e7763SThomas Graf  *
71749f7b33eSPatrick McHardy  * u32 my_hash_fn(const void *data, u32 len, u32 seed)
7187e1e7763SThomas Graf  * {
7197e1e7763SThomas Graf  *	struct test_obj *obj = data;
7207e1e7763SThomas Graf  *
7217e1e7763SThomas Graf  *	return [... hash ...];
7227e1e7763SThomas Graf  * }
7237e1e7763SThomas Graf  *
7247e1e7763SThomas Graf  * struct rhashtable_params params = {
7257e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
72687545899SDaniel Borkmann  *	.hashfn = jhash,
7277e1e7763SThomas Graf  *	.obj_hashfn = my_hash_fn,
7287e1e7763SThomas Graf  * };
7297e1e7763SThomas Graf  */
730488fb86eSHerbert Xu int rhashtable_init(struct rhashtable *ht,
731488fb86eSHerbert Xu 		    const struct rhashtable_params *params)
7327e1e7763SThomas Graf {
7337e1e7763SThomas Graf 	struct bucket_table *tbl;
7347e1e7763SThomas Graf 	size_t size;
7357e1e7763SThomas Graf 
7367e1e7763SThomas Graf 	size = HASH_DEFAULT_SIZE;
7377e1e7763SThomas Graf 
73831ccde2dSHerbert Xu 	if ((!params->key_len && !params->obj_hashfn) ||
73902fd97c3SHerbert Xu 	    (params->obj_hashfn && !params->obj_cmpfn))
7407e1e7763SThomas Graf 		return -EINVAL;
7417e1e7763SThomas Graf 
742f89bd6f8SThomas Graf 	if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
743f89bd6f8SThomas Graf 		return -EINVAL;
744f89bd6f8SThomas Graf 
74597defe1eSThomas Graf 	memset(ht, 0, sizeof(*ht));
74697defe1eSThomas Graf 	mutex_init(&ht->mutex);
747ba7c95eaSHerbert Xu 	spin_lock_init(&ht->lock);
74897defe1eSThomas Graf 	memcpy(&ht->p, params, sizeof(*params));
74997defe1eSThomas Graf 
750a998f712SThomas Graf 	if (params->min_size)
751a998f712SThomas Graf 		ht->p.min_size = roundup_pow_of_two(params->min_size);
752a998f712SThomas Graf 
753a998f712SThomas Graf 	if (params->max_size)
754a998f712SThomas Graf 		ht->p.max_size = rounddown_pow_of_two(params->max_size);
755a998f712SThomas Graf 
75607ee0722SHerbert Xu 	if (params->insecure_max_entries)
75707ee0722SHerbert Xu 		ht->p.insecure_max_entries =
75807ee0722SHerbert Xu 			rounddown_pow_of_two(params->insecure_max_entries);
75907ee0722SHerbert Xu 	else
76007ee0722SHerbert Xu 		ht->p.insecure_max_entries = ht->p.max_size * 2;
76107ee0722SHerbert Xu 
762488fb86eSHerbert Xu 	ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
763a998f712SThomas Graf 
7643a324606SHerbert Xu 	if (params->nelem_hint)
7653a324606SHerbert Xu 		size = rounded_hashtable_size(&ht->p);
7663a324606SHerbert Xu 
76727ed44a5SHerbert Xu 	/* The maximum (not average) chain length grows with the
76827ed44a5SHerbert Xu 	 * size of the hash table, at a rate of (log N)/(log log N).
76927ed44a5SHerbert Xu 	 * The value of 16 is selected so that even if the hash
77027ed44a5SHerbert Xu 	 * table grew to 2^32 you would not expect the maximum
77127ed44a5SHerbert Xu 	 * chain length to exceed it unless we are under attack
77227ed44a5SHerbert Xu 	 * (or extremely unlucky).
77327ed44a5SHerbert Xu 	 *
77427ed44a5SHerbert Xu 	 * As this limit is only to detect attacks, we don't need
77527ed44a5SHerbert Xu 	 * to set it to a lower value as you'd need the chain
77627ed44a5SHerbert Xu 	 * length to vastly exceed 16 to have any real effect
77727ed44a5SHerbert Xu 	 * on the system.
77827ed44a5SHerbert Xu 	 */
779ccd57b1bSHerbert Xu 	if (!params->insecure_elasticity)
780ccd57b1bSHerbert Xu 		ht->elasticity = 16;
781ccd57b1bSHerbert Xu 
78297defe1eSThomas Graf 	if (params->locks_mul)
78397defe1eSThomas Graf 		ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
78497defe1eSThomas Graf 	else
78597defe1eSThomas Graf 		ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
78697defe1eSThomas Graf 
78731ccde2dSHerbert Xu 	ht->key_len = ht->p.key_len;
78831ccde2dSHerbert Xu 	if (!params->hashfn) {
78931ccde2dSHerbert Xu 		ht->p.hashfn = jhash;
79031ccde2dSHerbert Xu 
79131ccde2dSHerbert Xu 		if (!(ht->key_len & (sizeof(u32) - 1))) {
79231ccde2dSHerbert Xu 			ht->key_len /= sizeof(u32);
79331ccde2dSHerbert Xu 			ht->p.hashfn = rhashtable_jhash2;
79431ccde2dSHerbert Xu 		}
79531ccde2dSHerbert Xu 	}
79631ccde2dSHerbert Xu 
797b9ecfdaaSHerbert Xu 	tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
7987e1e7763SThomas Graf 	if (tbl == NULL)
7997e1e7763SThomas Graf 		return -ENOMEM;
8007e1e7763SThomas Graf 
801545a148eSYing Xue 	atomic_set(&ht->nelems, 0);
802a5b6846fSDaniel Borkmann 
8037e1e7763SThomas Graf 	RCU_INIT_POINTER(ht->tbl, tbl);
8047e1e7763SThomas Graf 
80557699a40SYing Xue 	INIT_WORK(&ht->run_work, rht_deferred_worker);
80697defe1eSThomas Graf 
8077e1e7763SThomas Graf 	return 0;
8087e1e7763SThomas Graf }
8097e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init);
8107e1e7763SThomas Graf 
8117e1e7763SThomas Graf /**
8126b6f302cSThomas Graf  * rhashtable_free_and_destroy - free elements and destroy hash table
8137e1e7763SThomas Graf  * @ht:		the hash table to destroy
8146b6f302cSThomas Graf  * @free_fn:	callback to release resources of element
8156b6f302cSThomas Graf  * @arg:	pointer passed to free_fn
8167e1e7763SThomas Graf  *
8176b6f302cSThomas Graf  * Stops an eventual async resize. If defined, invokes free_fn for each
8186b6f302cSThomas Graf  * element to releasal resources. Please note that RCU protected
8196b6f302cSThomas Graf  * readers may still be accessing the elements. Releasing of resources
8206b6f302cSThomas Graf  * must occur in a compatible manner. Then frees the bucket array.
8216b6f302cSThomas Graf  *
8226b6f302cSThomas Graf  * This function will eventually sleep to wait for an async resize
8236b6f302cSThomas Graf  * to complete. The caller is responsible that no further write operations
8246b6f302cSThomas Graf  * occurs in parallel.
8257e1e7763SThomas Graf  */
8266b6f302cSThomas Graf void rhashtable_free_and_destroy(struct rhashtable *ht,
8276b6f302cSThomas Graf 				 void (*free_fn)(void *ptr, void *arg),
8286b6f302cSThomas Graf 				 void *arg)
8297e1e7763SThomas Graf {
8306b6f302cSThomas Graf 	const struct bucket_table *tbl;
8316b6f302cSThomas Graf 	unsigned int i;
83297defe1eSThomas Graf 
83357699a40SYing Xue 	cancel_work_sync(&ht->run_work);
83457699a40SYing Xue 
83597defe1eSThomas Graf 	mutex_lock(&ht->mutex);
8366b6f302cSThomas Graf 	tbl = rht_dereference(ht->tbl, ht);
8376b6f302cSThomas Graf 	if (free_fn) {
8386b6f302cSThomas Graf 		for (i = 0; i < tbl->size; i++) {
8396b6f302cSThomas Graf 			struct rhash_head *pos, *next;
8406b6f302cSThomas Graf 
8416b6f302cSThomas Graf 			for (pos = rht_dereference(tbl->buckets[i], ht),
8426b6f302cSThomas Graf 			     next = !rht_is_a_nulls(pos) ?
8436b6f302cSThomas Graf 					rht_dereference(pos->next, ht) : NULL;
8446b6f302cSThomas Graf 			     !rht_is_a_nulls(pos);
8456b6f302cSThomas Graf 			     pos = next,
8466b6f302cSThomas Graf 			     next = !rht_is_a_nulls(pos) ?
8476b6f302cSThomas Graf 					rht_dereference(pos->next, ht) : NULL)
8486b6f302cSThomas Graf 				free_fn(rht_obj(ht, pos), arg);
8496b6f302cSThomas Graf 		}
8506b6f302cSThomas Graf 	}
8516b6f302cSThomas Graf 
8526b6f302cSThomas Graf 	bucket_table_free(tbl);
85397defe1eSThomas Graf 	mutex_unlock(&ht->mutex);
8547e1e7763SThomas Graf }
8556b6f302cSThomas Graf EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
8566b6f302cSThomas Graf 
8576b6f302cSThomas Graf void rhashtable_destroy(struct rhashtable *ht)
8586b6f302cSThomas Graf {
8596b6f302cSThomas Graf 	return rhashtable_free_and_destroy(ht, NULL, NULL);
8606b6f302cSThomas Graf }
8617e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy);
862