xref: /linux/lib/rhashtable.c (revision ca26893f05e86497a86732768ec53cd38c0819ca)
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) {
809dbeea7fSEric 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 
899dbeea7fSEric 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 
381*ca26893fSHerbert Xu static int rhashtable_insert_rehash(struct rhashtable *ht,
3823cf92222SHerbert Xu 				    struct bucket_table *tbl)
383ccd57b1bSHerbert Xu {
384ccd57b1bSHerbert Xu 	struct bucket_table *old_tbl;
385ccd57b1bSHerbert Xu 	struct bucket_table *new_tbl;
386ccd57b1bSHerbert Xu 	unsigned int size;
387ccd57b1bSHerbert Xu 	int err;
388ccd57b1bSHerbert Xu 
389ccd57b1bSHerbert Xu 	old_tbl = rht_dereference_rcu(ht->tbl, ht);
390ccd57b1bSHerbert Xu 
391ccd57b1bSHerbert Xu 	size = tbl->size;
392ccd57b1bSHerbert Xu 
3933cf92222SHerbert Xu 	err = -EBUSY;
3943cf92222SHerbert Xu 
395ccd57b1bSHerbert Xu 	if (rht_grow_above_75(ht, tbl))
396ccd57b1bSHerbert Xu 		size *= 2;
397a87b9ebfSThomas Graf 	/* Do not schedule more than one rehash */
398a87b9ebfSThomas Graf 	else if (old_tbl != tbl)
3993cf92222SHerbert Xu 		goto fail;
4003cf92222SHerbert Xu 
4013cf92222SHerbert Xu 	err = -ENOMEM;
402ccd57b1bSHerbert Xu 
403ccd57b1bSHerbert Xu 	new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
4043cf92222SHerbert Xu 	if (new_tbl == NULL)
4053cf92222SHerbert Xu 		goto fail;
406ccd57b1bSHerbert Xu 
407ccd57b1bSHerbert Xu 	err = rhashtable_rehash_attach(ht, tbl, new_tbl);
408ccd57b1bSHerbert Xu 	if (err) {
409ccd57b1bSHerbert Xu 		bucket_table_free(new_tbl);
410ccd57b1bSHerbert Xu 		if (err == -EEXIST)
411ccd57b1bSHerbert Xu 			err = 0;
412ccd57b1bSHerbert Xu 	} else
413ccd57b1bSHerbert Xu 		schedule_work(&ht->run_work);
414ccd57b1bSHerbert Xu 
415ccd57b1bSHerbert Xu 	return err;
4163cf92222SHerbert Xu 
4173cf92222SHerbert Xu fail:
4183cf92222SHerbert Xu 	/* Do not fail the insert if someone else did a rehash. */
4193cf92222SHerbert Xu 	if (likely(rcu_dereference_raw(tbl->future_tbl)))
4203cf92222SHerbert Xu 		return 0;
4213cf92222SHerbert Xu 
4223cf92222SHerbert Xu 	/* Schedule async rehash to retry allocation in process context. */
4233cf92222SHerbert Xu 	if (err == -ENOMEM)
4243cf92222SHerbert Xu 		schedule_work(&ht->run_work);
4253cf92222SHerbert Xu 
4263cf92222SHerbert Xu 	return err;
427ccd57b1bSHerbert Xu }
428ccd57b1bSHerbert Xu 
429*ca26893fSHerbert Xu static void *rhashtable_lookup_one(struct rhashtable *ht,
430*ca26893fSHerbert Xu 				   struct bucket_table *tbl, unsigned int hash,
431*ca26893fSHerbert Xu 				   const void *key, struct rhash_head *obj)
43202fd97c3SHerbert Xu {
433*ca26893fSHerbert Xu 	struct rhashtable_compare_arg arg = {
434*ca26893fSHerbert Xu 		.ht = ht,
435*ca26893fSHerbert Xu 		.key = key,
436*ca26893fSHerbert Xu 	};
437*ca26893fSHerbert Xu 	struct rhash_head __rcu **pprev;
43802fd97c3SHerbert Xu 	struct rhash_head *head;
439*ca26893fSHerbert Xu 	int elasticity;
44002fd97c3SHerbert Xu 
441*ca26893fSHerbert Xu 	elasticity = ht->elasticity;
442*ca26893fSHerbert Xu 	pprev = &tbl->buckets[hash];
443*ca26893fSHerbert Xu 	rht_for_each(head, tbl, hash) {
444*ca26893fSHerbert Xu 		struct rhlist_head *list;
445*ca26893fSHerbert Xu 		struct rhlist_head *plist;
44602fd97c3SHerbert Xu 
447*ca26893fSHerbert Xu 		elasticity--;
448*ca26893fSHerbert Xu 		if (!key ||
449*ca26893fSHerbert Xu 		    (ht->p.obj_cmpfn ?
450*ca26893fSHerbert Xu 		     ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
451*ca26893fSHerbert Xu 		     rhashtable_compare(&arg, rht_obj(ht, head))))
452*ca26893fSHerbert Xu 			continue;
453*ca26893fSHerbert Xu 
454*ca26893fSHerbert Xu 		if (!ht->rhlist)
455*ca26893fSHerbert Xu 			return rht_obj(ht, head);
456*ca26893fSHerbert Xu 
457*ca26893fSHerbert Xu 		list = container_of(obj, struct rhlist_head, rhead);
458*ca26893fSHerbert Xu 		plist = container_of(head, struct rhlist_head, rhead);
459*ca26893fSHerbert Xu 
460*ca26893fSHerbert Xu 		RCU_INIT_POINTER(list->next, plist);
461*ca26893fSHerbert Xu 		head = rht_dereference_bucket(head->next, tbl, hash);
462*ca26893fSHerbert Xu 		RCU_INIT_POINTER(list->rhead.next, head);
463*ca26893fSHerbert Xu 		rcu_assign_pointer(*pprev, obj);
464*ca26893fSHerbert Xu 
465*ca26893fSHerbert Xu 		return NULL;
4665ca8cc5bSPablo Neira Ayuso 	}
46702fd97c3SHerbert Xu 
468*ca26893fSHerbert Xu 	if (elasticity <= 0)
469*ca26893fSHerbert Xu 		return ERR_PTR(-EAGAIN);
470*ca26893fSHerbert Xu 
471*ca26893fSHerbert Xu 	return ERR_PTR(-ENOENT);
472*ca26893fSHerbert Xu }
473*ca26893fSHerbert Xu 
474*ca26893fSHerbert Xu static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
475*ca26893fSHerbert Xu 						  struct bucket_table *tbl,
476*ca26893fSHerbert Xu 						  unsigned int hash,
477*ca26893fSHerbert Xu 						  struct rhash_head *obj,
478*ca26893fSHerbert Xu 						  void *data)
479*ca26893fSHerbert Xu {
480*ca26893fSHerbert Xu 	struct bucket_table *new_tbl;
481*ca26893fSHerbert Xu 	struct rhash_head *head;
482*ca26893fSHerbert Xu 
483*ca26893fSHerbert Xu 	if (!IS_ERR_OR_NULL(data))
484*ca26893fSHerbert Xu 		return ERR_PTR(-EEXIST);
485*ca26893fSHerbert Xu 
486*ca26893fSHerbert Xu 	if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
487*ca26893fSHerbert Xu 		return ERR_CAST(data);
488*ca26893fSHerbert Xu 
489*ca26893fSHerbert Xu 	new_tbl = rcu_dereference(tbl->future_tbl);
490*ca26893fSHerbert Xu 	if (new_tbl)
491*ca26893fSHerbert Xu 		return new_tbl;
492*ca26893fSHerbert Xu 
493*ca26893fSHerbert Xu 	if (PTR_ERR(data) != -ENOENT)
494*ca26893fSHerbert Xu 		return ERR_CAST(data);
495*ca26893fSHerbert Xu 
49607ee0722SHerbert Xu 	if (unlikely(rht_grow_above_max(ht, tbl)))
497*ca26893fSHerbert Xu 		return ERR_PTR(-E2BIG);
49807ee0722SHerbert Xu 
499*ca26893fSHerbert Xu 	if (unlikely(rht_grow_above_100(ht, tbl)))
500*ca26893fSHerbert Xu 		return ERR_PTR(-EAGAIN);
50102fd97c3SHerbert Xu 
50202fd97c3SHerbert Xu 	head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
50302fd97c3SHerbert Xu 
50402fd97c3SHerbert Xu 	RCU_INIT_POINTER(obj->next, head);
505*ca26893fSHerbert Xu 	if (ht->rhlist) {
506*ca26893fSHerbert Xu 		struct rhlist_head *list;
507*ca26893fSHerbert Xu 
508*ca26893fSHerbert Xu 		list = container_of(obj, struct rhlist_head, rhead);
509*ca26893fSHerbert Xu 		RCU_INIT_POINTER(list->next, NULL);
510*ca26893fSHerbert Xu 	}
51102fd97c3SHerbert Xu 
51202fd97c3SHerbert Xu 	rcu_assign_pointer(tbl->buckets[hash], obj);
51302fd97c3SHerbert Xu 
51402fd97c3SHerbert Xu 	atomic_inc(&ht->nelems);
515*ca26893fSHerbert Xu 	if (rht_grow_above_75(ht, tbl))
516*ca26893fSHerbert Xu 		schedule_work(&ht->run_work);
51702fd97c3SHerbert Xu 
5183cf92222SHerbert Xu 	return NULL;
519*ca26893fSHerbert Xu }
520*ca26893fSHerbert Xu 
521*ca26893fSHerbert Xu static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
522*ca26893fSHerbert Xu 				   struct rhash_head *obj)
523*ca26893fSHerbert Xu {
524*ca26893fSHerbert Xu 	struct bucket_table *new_tbl;
525*ca26893fSHerbert Xu 	struct bucket_table *tbl;
526*ca26893fSHerbert Xu 	unsigned int hash;
527*ca26893fSHerbert Xu 	spinlock_t *lock;
528*ca26893fSHerbert Xu 	void *data;
529*ca26893fSHerbert Xu 
530*ca26893fSHerbert Xu 	tbl = rcu_dereference(ht->tbl);
531*ca26893fSHerbert Xu 
532*ca26893fSHerbert Xu 	/* All insertions must grab the oldest table containing
533*ca26893fSHerbert Xu 	 * the hashed bucket that is yet to be rehashed.
534*ca26893fSHerbert Xu 	 */
535*ca26893fSHerbert Xu 	for (;;) {
536*ca26893fSHerbert Xu 		hash = rht_head_hashfn(ht, tbl, obj, ht->p);
537*ca26893fSHerbert Xu 		lock = rht_bucket_lock(tbl, hash);
538*ca26893fSHerbert Xu 		spin_lock_bh(lock);
539*ca26893fSHerbert Xu 
540*ca26893fSHerbert Xu 		if (tbl->rehash <= hash)
541*ca26893fSHerbert Xu 			break;
542*ca26893fSHerbert Xu 
543*ca26893fSHerbert Xu 		spin_unlock_bh(lock);
544*ca26893fSHerbert Xu 		tbl = rcu_dereference(tbl->future_tbl);
545*ca26893fSHerbert Xu 	}
546*ca26893fSHerbert Xu 
547*ca26893fSHerbert Xu 	data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
548*ca26893fSHerbert Xu 	new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
549*ca26893fSHerbert Xu 	if (PTR_ERR(new_tbl) != -EEXIST)
550*ca26893fSHerbert Xu 		data = ERR_CAST(new_tbl);
551*ca26893fSHerbert Xu 
552*ca26893fSHerbert Xu 	while (!IS_ERR_OR_NULL(new_tbl)) {
553*ca26893fSHerbert Xu 		tbl = new_tbl;
554*ca26893fSHerbert Xu 		hash = rht_head_hashfn(ht, tbl, obj, ht->p);
555*ca26893fSHerbert Xu 		spin_lock_nested(rht_bucket_lock(tbl, hash),
556*ca26893fSHerbert Xu 				 SINGLE_DEPTH_NESTING);
557*ca26893fSHerbert Xu 
558*ca26893fSHerbert Xu 		data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
559*ca26893fSHerbert Xu 		new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
560*ca26893fSHerbert Xu 		if (PTR_ERR(new_tbl) != -EEXIST)
561*ca26893fSHerbert Xu 			data = ERR_CAST(new_tbl);
562*ca26893fSHerbert Xu 
563*ca26893fSHerbert Xu 		spin_unlock(rht_bucket_lock(tbl, hash));
564*ca26893fSHerbert Xu 	}
565*ca26893fSHerbert Xu 
566*ca26893fSHerbert Xu 	spin_unlock_bh(lock);
567*ca26893fSHerbert Xu 
568*ca26893fSHerbert Xu 	if (PTR_ERR(data) == -EAGAIN)
569*ca26893fSHerbert Xu 		data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
570*ca26893fSHerbert Xu 			       -EAGAIN);
571*ca26893fSHerbert Xu 
572*ca26893fSHerbert Xu 	return data;
573*ca26893fSHerbert Xu }
574*ca26893fSHerbert Xu 
575*ca26893fSHerbert Xu void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
576*ca26893fSHerbert Xu 			     struct rhash_head *obj)
577*ca26893fSHerbert Xu {
578*ca26893fSHerbert Xu 	void *data;
579*ca26893fSHerbert Xu 
580*ca26893fSHerbert Xu 	do {
581*ca26893fSHerbert Xu 		rcu_read_lock();
582*ca26893fSHerbert Xu 		data = rhashtable_try_insert(ht, key, obj);
583*ca26893fSHerbert Xu 		rcu_read_unlock();
584*ca26893fSHerbert Xu 	} while (PTR_ERR(data) == -EAGAIN);
585*ca26893fSHerbert Xu 
586*ca26893fSHerbert Xu 	return data;
58702fd97c3SHerbert Xu }
58802fd97c3SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
58902fd97c3SHerbert Xu 
590f2dba9c6SHerbert Xu /**
591246779ddSHerbert Xu  * rhashtable_walk_enter - Initialise an iterator
592f2dba9c6SHerbert Xu  * @ht:		Table to walk over
593f2dba9c6SHerbert Xu  * @iter:	Hash table Iterator
594f2dba9c6SHerbert Xu  *
595f2dba9c6SHerbert Xu  * This function prepares a hash table walk.
596f2dba9c6SHerbert Xu  *
597f2dba9c6SHerbert Xu  * Note that if you restart a walk after rhashtable_walk_stop you
598f2dba9c6SHerbert Xu  * may see the same object twice.  Also, you may miss objects if
599f2dba9c6SHerbert Xu  * there are removals in between rhashtable_walk_stop and the next
600f2dba9c6SHerbert Xu  * call to rhashtable_walk_start.
601f2dba9c6SHerbert Xu  *
602f2dba9c6SHerbert Xu  * For a completely stable walk you should construct your own data
603f2dba9c6SHerbert Xu  * structure outside the hash table.
604f2dba9c6SHerbert Xu  *
605f2dba9c6SHerbert Xu  * This function may sleep so you must not call it from interrupt
606f2dba9c6SHerbert Xu  * context or with spin locks held.
607f2dba9c6SHerbert Xu  *
608246779ddSHerbert Xu  * You must call rhashtable_walk_exit after this function returns.
609f2dba9c6SHerbert Xu  */
610246779ddSHerbert Xu void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
611f2dba9c6SHerbert Xu {
612f2dba9c6SHerbert Xu 	iter->ht = ht;
613f2dba9c6SHerbert Xu 	iter->p = NULL;
614f2dba9c6SHerbert Xu 	iter->slot = 0;
615f2dba9c6SHerbert Xu 	iter->skip = 0;
616f2dba9c6SHerbert Xu 
617c6ff5268SHerbert Xu 	spin_lock(&ht->lock);
618246779ddSHerbert Xu 	iter->walker.tbl =
619179ccc0aSHerbert Xu 		rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
620246779ddSHerbert Xu 	list_add(&iter->walker.list, &iter->walker.tbl->walkers);
621c6ff5268SHerbert Xu 	spin_unlock(&ht->lock);
622f2dba9c6SHerbert Xu }
623246779ddSHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
624f2dba9c6SHerbert Xu 
625f2dba9c6SHerbert Xu /**
626f2dba9c6SHerbert Xu  * rhashtable_walk_exit - Free an iterator
627f2dba9c6SHerbert Xu  * @iter:	Hash table Iterator
628f2dba9c6SHerbert Xu  *
629f2dba9c6SHerbert Xu  * This function frees resources allocated by rhashtable_walk_init.
630f2dba9c6SHerbert Xu  */
631f2dba9c6SHerbert Xu void rhashtable_walk_exit(struct rhashtable_iter *iter)
632f2dba9c6SHerbert Xu {
633c6ff5268SHerbert Xu 	spin_lock(&iter->ht->lock);
634246779ddSHerbert Xu 	if (iter->walker.tbl)
635246779ddSHerbert Xu 		list_del(&iter->walker.list);
636c6ff5268SHerbert Xu 	spin_unlock(&iter->ht->lock);
637f2dba9c6SHerbert Xu }
638f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
639f2dba9c6SHerbert Xu 
640f2dba9c6SHerbert Xu /**
641f2dba9c6SHerbert Xu  * rhashtable_walk_start - Start a hash table walk
642f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
643f2dba9c6SHerbert Xu  *
644f2dba9c6SHerbert Xu  * Start a hash table walk.  Note that we take the RCU lock in all
645f2dba9c6SHerbert Xu  * cases including when we return an error.  So you must always call
646f2dba9c6SHerbert Xu  * rhashtable_walk_stop to clean up.
647f2dba9c6SHerbert Xu  *
648f2dba9c6SHerbert Xu  * Returns zero if successful.
649f2dba9c6SHerbert Xu  *
650f2dba9c6SHerbert Xu  * Returns -EAGAIN if resize event occured.  Note that the iterator
651f2dba9c6SHerbert Xu  * will rewind back to the beginning and you may use it immediately
652f2dba9c6SHerbert Xu  * by calling rhashtable_walk_next.
653f2dba9c6SHerbert Xu  */
654f2dba9c6SHerbert Xu int rhashtable_walk_start(struct rhashtable_iter *iter)
655db4374f4SThomas Graf 	__acquires(RCU)
656f2dba9c6SHerbert Xu {
657eddee5baSHerbert Xu 	struct rhashtable *ht = iter->ht;
658eddee5baSHerbert Xu 
659f2dba9c6SHerbert Xu 	rcu_read_lock();
660f2dba9c6SHerbert Xu 
661c6ff5268SHerbert Xu 	spin_lock(&ht->lock);
662246779ddSHerbert Xu 	if (iter->walker.tbl)
663246779ddSHerbert Xu 		list_del(&iter->walker.list);
664c6ff5268SHerbert Xu 	spin_unlock(&ht->lock);
665eddee5baSHerbert Xu 
666246779ddSHerbert Xu 	if (!iter->walker.tbl) {
667246779ddSHerbert Xu 		iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
668f2dba9c6SHerbert Xu 		return -EAGAIN;
669f2dba9c6SHerbert Xu 	}
670f2dba9c6SHerbert Xu 
671f2dba9c6SHerbert Xu 	return 0;
672f2dba9c6SHerbert Xu }
673f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_start);
674f2dba9c6SHerbert Xu 
675f2dba9c6SHerbert Xu /**
676f2dba9c6SHerbert Xu  * rhashtable_walk_next - Return the next object and advance the iterator
677f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
678f2dba9c6SHerbert Xu  *
679f2dba9c6SHerbert Xu  * Note that you must call rhashtable_walk_stop when you are finished
680f2dba9c6SHerbert Xu  * with the walk.
681f2dba9c6SHerbert Xu  *
682f2dba9c6SHerbert Xu  * Returns the next object or NULL when the end of the table is reached.
683f2dba9c6SHerbert Xu  *
684f2dba9c6SHerbert Xu  * Returns -EAGAIN if resize event occured.  Note that the iterator
685f2dba9c6SHerbert Xu  * will rewind back to the beginning and you may continue to use it.
686f2dba9c6SHerbert Xu  */
687f2dba9c6SHerbert Xu void *rhashtable_walk_next(struct rhashtable_iter *iter)
688f2dba9c6SHerbert Xu {
689246779ddSHerbert Xu 	struct bucket_table *tbl = iter->walker.tbl;
690*ca26893fSHerbert Xu 	struct rhlist_head *list = iter->list;
691f2dba9c6SHerbert Xu 	struct rhashtable *ht = iter->ht;
692f2dba9c6SHerbert Xu 	struct rhash_head *p = iter->p;
693*ca26893fSHerbert Xu 	bool rhlist = ht->rhlist;
694f2dba9c6SHerbert Xu 
695f2dba9c6SHerbert Xu 	if (p) {
696*ca26893fSHerbert Xu 		if (!rhlist || !(list = rcu_dereference(list->next))) {
697*ca26893fSHerbert Xu 			p = rcu_dereference(p->next);
698*ca26893fSHerbert Xu 			list = container_of(p, struct rhlist_head, rhead);
699*ca26893fSHerbert Xu 		}
700f2dba9c6SHerbert Xu 		goto next;
701f2dba9c6SHerbert Xu 	}
702f2dba9c6SHerbert Xu 
703f2dba9c6SHerbert Xu 	for (; iter->slot < tbl->size; iter->slot++) {
704f2dba9c6SHerbert Xu 		int skip = iter->skip;
705f2dba9c6SHerbert Xu 
706f2dba9c6SHerbert Xu 		rht_for_each_rcu(p, tbl, iter->slot) {
707*ca26893fSHerbert Xu 			if (rhlist) {
708*ca26893fSHerbert Xu 				list = container_of(p, struct rhlist_head,
709*ca26893fSHerbert Xu 						    rhead);
710*ca26893fSHerbert Xu 				do {
711*ca26893fSHerbert Xu 					if (!skip)
712*ca26893fSHerbert Xu 						goto next;
713*ca26893fSHerbert Xu 					skip--;
714*ca26893fSHerbert Xu 					list = rcu_dereference(list->next);
715*ca26893fSHerbert Xu 				} while (list);
716*ca26893fSHerbert Xu 
717*ca26893fSHerbert Xu 				continue;
718*ca26893fSHerbert Xu 			}
719f2dba9c6SHerbert Xu 			if (!skip)
720f2dba9c6SHerbert Xu 				break;
721f2dba9c6SHerbert Xu 			skip--;
722f2dba9c6SHerbert Xu 		}
723f2dba9c6SHerbert Xu 
724f2dba9c6SHerbert Xu next:
725f2dba9c6SHerbert Xu 		if (!rht_is_a_nulls(p)) {
726f2dba9c6SHerbert Xu 			iter->skip++;
727f2dba9c6SHerbert Xu 			iter->p = p;
728*ca26893fSHerbert Xu 			iter->list = list;
729*ca26893fSHerbert Xu 			return rht_obj(ht, rhlist ? &list->rhead : p);
730f2dba9c6SHerbert Xu 		}
731f2dba9c6SHerbert Xu 
732f2dba9c6SHerbert Xu 		iter->skip = 0;
733f2dba9c6SHerbert Xu 	}
734f2dba9c6SHerbert Xu 
735142b942aSPhil Sutter 	iter->p = NULL;
736142b942aSPhil Sutter 
737d88252f9SHerbert Xu 	/* Ensure we see any new tables. */
738d88252f9SHerbert Xu 	smp_rmb();
739d88252f9SHerbert Xu 
740246779ddSHerbert Xu 	iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
741246779ddSHerbert Xu 	if (iter->walker.tbl) {
742eddee5baSHerbert Xu 		iter->slot = 0;
743eddee5baSHerbert Xu 		iter->skip = 0;
744eddee5baSHerbert Xu 		return ERR_PTR(-EAGAIN);
745eddee5baSHerbert Xu 	}
746eddee5baSHerbert Xu 
747c936a79fSThomas Graf 	return NULL;
748f2dba9c6SHerbert Xu }
749f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_next);
750f2dba9c6SHerbert Xu 
751f2dba9c6SHerbert Xu /**
752f2dba9c6SHerbert Xu  * rhashtable_walk_stop - Finish a hash table walk
753f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
754f2dba9c6SHerbert Xu  *
755f2dba9c6SHerbert Xu  * Finish a hash table walk.
756f2dba9c6SHerbert Xu  */
757f2dba9c6SHerbert Xu void rhashtable_walk_stop(struct rhashtable_iter *iter)
758db4374f4SThomas Graf 	__releases(RCU)
759f2dba9c6SHerbert Xu {
760eddee5baSHerbert Xu 	struct rhashtable *ht;
761246779ddSHerbert Xu 	struct bucket_table *tbl = iter->walker.tbl;
762eddee5baSHerbert Xu 
763eddee5baSHerbert Xu 	if (!tbl)
764963ecbd4SHerbert Xu 		goto out;
765eddee5baSHerbert Xu 
766eddee5baSHerbert Xu 	ht = iter->ht;
767eddee5baSHerbert Xu 
768ba7c95eaSHerbert Xu 	spin_lock(&ht->lock);
769c4db8848SHerbert Xu 	if (tbl->rehash < tbl->size)
770246779ddSHerbert Xu 		list_add(&iter->walker.list, &tbl->walkers);
771eddee5baSHerbert Xu 	else
772246779ddSHerbert Xu 		iter->walker.tbl = NULL;
773ba7c95eaSHerbert Xu 	spin_unlock(&ht->lock);
774eddee5baSHerbert Xu 
775f2dba9c6SHerbert Xu 	iter->p = NULL;
776963ecbd4SHerbert Xu 
777963ecbd4SHerbert Xu out:
778963ecbd4SHerbert Xu 	rcu_read_unlock();
779f2dba9c6SHerbert Xu }
780f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
781f2dba9c6SHerbert Xu 
782488fb86eSHerbert Xu static size_t rounded_hashtable_size(const struct rhashtable_params *params)
7837e1e7763SThomas Graf {
78494000176SYing Xue 	return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
785e2e21c1cSHerbert Xu 		   (unsigned long)params->min_size);
7867e1e7763SThomas Graf }
7877e1e7763SThomas Graf 
78831ccde2dSHerbert Xu static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
78931ccde2dSHerbert Xu {
79031ccde2dSHerbert Xu 	return jhash2(key, length, seed);
79131ccde2dSHerbert Xu }
79231ccde2dSHerbert Xu 
7937e1e7763SThomas Graf /**
7947e1e7763SThomas Graf  * rhashtable_init - initialize a new hash table
7957e1e7763SThomas Graf  * @ht:		hash table to be initialized
7967e1e7763SThomas Graf  * @params:	configuration parameters
7977e1e7763SThomas Graf  *
7987e1e7763SThomas Graf  * Initializes a new hash table based on the provided configuration
7997e1e7763SThomas Graf  * parameters. A table can be configured either with a variable or
8007e1e7763SThomas Graf  * fixed length key:
8017e1e7763SThomas Graf  *
8027e1e7763SThomas Graf  * Configuration Example 1: Fixed length keys
8037e1e7763SThomas Graf  * struct test_obj {
8047e1e7763SThomas Graf  *	int			key;
8057e1e7763SThomas Graf  *	void *			my_member;
8067e1e7763SThomas Graf  *	struct rhash_head	node;
8077e1e7763SThomas Graf  * };
8087e1e7763SThomas Graf  *
8097e1e7763SThomas Graf  * struct rhashtable_params params = {
8107e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
8117e1e7763SThomas Graf  *	.key_offset = offsetof(struct test_obj, key),
8127e1e7763SThomas Graf  *	.key_len = sizeof(int),
81387545899SDaniel Borkmann  *	.hashfn = jhash,
814f89bd6f8SThomas Graf  *	.nulls_base = (1U << RHT_BASE_SHIFT),
8157e1e7763SThomas Graf  * };
8167e1e7763SThomas Graf  *
8177e1e7763SThomas Graf  * Configuration Example 2: Variable length keys
8187e1e7763SThomas Graf  * struct test_obj {
8197e1e7763SThomas Graf  *	[...]
8207e1e7763SThomas Graf  *	struct rhash_head	node;
8217e1e7763SThomas Graf  * };
8227e1e7763SThomas Graf  *
82349f7b33eSPatrick McHardy  * u32 my_hash_fn(const void *data, u32 len, u32 seed)
8247e1e7763SThomas Graf  * {
8257e1e7763SThomas Graf  *	struct test_obj *obj = data;
8267e1e7763SThomas Graf  *
8277e1e7763SThomas Graf  *	return [... hash ...];
8287e1e7763SThomas Graf  * }
8297e1e7763SThomas Graf  *
8307e1e7763SThomas Graf  * struct rhashtable_params params = {
8317e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
83287545899SDaniel Borkmann  *	.hashfn = jhash,
8337e1e7763SThomas Graf  *	.obj_hashfn = my_hash_fn,
8347e1e7763SThomas Graf  * };
8357e1e7763SThomas Graf  */
836488fb86eSHerbert Xu int rhashtable_init(struct rhashtable *ht,
837488fb86eSHerbert Xu 		    const struct rhashtable_params *params)
8387e1e7763SThomas Graf {
8397e1e7763SThomas Graf 	struct bucket_table *tbl;
8407e1e7763SThomas Graf 	size_t size;
8417e1e7763SThomas Graf 
8427e1e7763SThomas Graf 	size = HASH_DEFAULT_SIZE;
8437e1e7763SThomas Graf 
84431ccde2dSHerbert Xu 	if ((!params->key_len && !params->obj_hashfn) ||
84502fd97c3SHerbert Xu 	    (params->obj_hashfn && !params->obj_cmpfn))
8467e1e7763SThomas Graf 		return -EINVAL;
8477e1e7763SThomas Graf 
848f89bd6f8SThomas Graf 	if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
849f89bd6f8SThomas Graf 		return -EINVAL;
850f89bd6f8SThomas Graf 
85197defe1eSThomas Graf 	memset(ht, 0, sizeof(*ht));
85297defe1eSThomas Graf 	mutex_init(&ht->mutex);
853ba7c95eaSHerbert Xu 	spin_lock_init(&ht->lock);
85497defe1eSThomas Graf 	memcpy(&ht->p, params, sizeof(*params));
85597defe1eSThomas Graf 
856a998f712SThomas Graf 	if (params->min_size)
857a998f712SThomas Graf 		ht->p.min_size = roundup_pow_of_two(params->min_size);
858a998f712SThomas Graf 
859a998f712SThomas Graf 	if (params->max_size)
860a998f712SThomas Graf 		ht->p.max_size = rounddown_pow_of_two(params->max_size);
861a998f712SThomas Graf 
86207ee0722SHerbert Xu 	if (params->insecure_max_entries)
86307ee0722SHerbert Xu 		ht->p.insecure_max_entries =
86407ee0722SHerbert Xu 			rounddown_pow_of_two(params->insecure_max_entries);
86507ee0722SHerbert Xu 	else
86607ee0722SHerbert Xu 		ht->p.insecure_max_entries = ht->p.max_size * 2;
86707ee0722SHerbert Xu 
868488fb86eSHerbert Xu 	ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
869a998f712SThomas Graf 
8703a324606SHerbert Xu 	if (params->nelem_hint)
8713a324606SHerbert Xu 		size = rounded_hashtable_size(&ht->p);
8723a324606SHerbert Xu 
87327ed44a5SHerbert Xu 	/* The maximum (not average) chain length grows with the
87427ed44a5SHerbert Xu 	 * size of the hash table, at a rate of (log N)/(log log N).
87527ed44a5SHerbert Xu 	 * The value of 16 is selected so that even if the hash
87627ed44a5SHerbert Xu 	 * table grew to 2^32 you would not expect the maximum
87727ed44a5SHerbert Xu 	 * chain length to exceed it unless we are under attack
87827ed44a5SHerbert Xu 	 * (or extremely unlucky).
87927ed44a5SHerbert Xu 	 *
88027ed44a5SHerbert Xu 	 * As this limit is only to detect attacks, we don't need
88127ed44a5SHerbert Xu 	 * to set it to a lower value as you'd need the chain
88227ed44a5SHerbert Xu 	 * length to vastly exceed 16 to have any real effect
88327ed44a5SHerbert Xu 	 * on the system.
88427ed44a5SHerbert Xu 	 */
885ccd57b1bSHerbert Xu 	if (!params->insecure_elasticity)
886ccd57b1bSHerbert Xu 		ht->elasticity = 16;
887ccd57b1bSHerbert Xu 
88897defe1eSThomas Graf 	if (params->locks_mul)
88997defe1eSThomas Graf 		ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
89097defe1eSThomas Graf 	else
89197defe1eSThomas Graf 		ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
89297defe1eSThomas Graf 
89331ccde2dSHerbert Xu 	ht->key_len = ht->p.key_len;
89431ccde2dSHerbert Xu 	if (!params->hashfn) {
89531ccde2dSHerbert Xu 		ht->p.hashfn = jhash;
89631ccde2dSHerbert Xu 
89731ccde2dSHerbert Xu 		if (!(ht->key_len & (sizeof(u32) - 1))) {
89831ccde2dSHerbert Xu 			ht->key_len /= sizeof(u32);
89931ccde2dSHerbert Xu 			ht->p.hashfn = rhashtable_jhash2;
90031ccde2dSHerbert Xu 		}
90131ccde2dSHerbert Xu 	}
90231ccde2dSHerbert Xu 
903b9ecfdaaSHerbert Xu 	tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
9047e1e7763SThomas Graf 	if (tbl == NULL)
9057e1e7763SThomas Graf 		return -ENOMEM;
9067e1e7763SThomas Graf 
907545a148eSYing Xue 	atomic_set(&ht->nelems, 0);
908a5b6846fSDaniel Borkmann 
9097e1e7763SThomas Graf 	RCU_INIT_POINTER(ht->tbl, tbl);
9107e1e7763SThomas Graf 
91157699a40SYing Xue 	INIT_WORK(&ht->run_work, rht_deferred_worker);
91297defe1eSThomas Graf 
9137e1e7763SThomas Graf 	return 0;
9147e1e7763SThomas Graf }
9157e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init);
9167e1e7763SThomas Graf 
9177e1e7763SThomas Graf /**
918*ca26893fSHerbert Xu  * rhltable_init - initialize a new hash list table
919*ca26893fSHerbert Xu  * @hlt:	hash list table to be initialized
920*ca26893fSHerbert Xu  * @params:	configuration parameters
921*ca26893fSHerbert Xu  *
922*ca26893fSHerbert Xu  * Initializes a new hash list table.
923*ca26893fSHerbert Xu  *
924*ca26893fSHerbert Xu  * See documentation for rhashtable_init.
925*ca26893fSHerbert Xu  */
926*ca26893fSHerbert Xu int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
927*ca26893fSHerbert Xu {
928*ca26893fSHerbert Xu 	int err;
929*ca26893fSHerbert Xu 
930*ca26893fSHerbert Xu 	/* No rhlist NULLs marking for now. */
931*ca26893fSHerbert Xu 	if (params->nulls_base)
932*ca26893fSHerbert Xu 		return -EINVAL;
933*ca26893fSHerbert Xu 
934*ca26893fSHerbert Xu 	err = rhashtable_init(&hlt->ht, params);
935*ca26893fSHerbert Xu 	hlt->ht.rhlist = true;
936*ca26893fSHerbert Xu 	return err;
937*ca26893fSHerbert Xu }
938*ca26893fSHerbert Xu EXPORT_SYMBOL_GPL(rhltable_init);
939*ca26893fSHerbert Xu 
940*ca26893fSHerbert Xu static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
941*ca26893fSHerbert Xu 				void (*free_fn)(void *ptr, void *arg),
942*ca26893fSHerbert Xu 				void *arg)
943*ca26893fSHerbert Xu {
944*ca26893fSHerbert Xu 	struct rhlist_head *list;
945*ca26893fSHerbert Xu 
946*ca26893fSHerbert Xu 	if (!ht->rhlist) {
947*ca26893fSHerbert Xu 		free_fn(rht_obj(ht, obj), arg);
948*ca26893fSHerbert Xu 		return;
949*ca26893fSHerbert Xu 	}
950*ca26893fSHerbert Xu 
951*ca26893fSHerbert Xu 	list = container_of(obj, struct rhlist_head, rhead);
952*ca26893fSHerbert Xu 	do {
953*ca26893fSHerbert Xu 		obj = &list->rhead;
954*ca26893fSHerbert Xu 		list = rht_dereference(list->next, ht);
955*ca26893fSHerbert Xu 		free_fn(rht_obj(ht, obj), arg);
956*ca26893fSHerbert Xu 	} while (list);
957*ca26893fSHerbert Xu }
958*ca26893fSHerbert Xu 
959*ca26893fSHerbert Xu /**
9606b6f302cSThomas Graf  * rhashtable_free_and_destroy - free elements and destroy hash table
9617e1e7763SThomas Graf  * @ht:		the hash table to destroy
9626b6f302cSThomas Graf  * @free_fn:	callback to release resources of element
9636b6f302cSThomas Graf  * @arg:	pointer passed to free_fn
9647e1e7763SThomas Graf  *
9656b6f302cSThomas Graf  * Stops an eventual async resize. If defined, invokes free_fn for each
9666b6f302cSThomas Graf  * element to releasal resources. Please note that RCU protected
9676b6f302cSThomas Graf  * readers may still be accessing the elements. Releasing of resources
9686b6f302cSThomas Graf  * must occur in a compatible manner. Then frees the bucket array.
9696b6f302cSThomas Graf  *
9706b6f302cSThomas Graf  * This function will eventually sleep to wait for an async resize
9716b6f302cSThomas Graf  * to complete. The caller is responsible that no further write operations
9726b6f302cSThomas Graf  * occurs in parallel.
9737e1e7763SThomas Graf  */
9746b6f302cSThomas Graf void rhashtable_free_and_destroy(struct rhashtable *ht,
9756b6f302cSThomas Graf 				 void (*free_fn)(void *ptr, void *arg),
9766b6f302cSThomas Graf 				 void *arg)
9777e1e7763SThomas Graf {
9786b6f302cSThomas Graf 	const struct bucket_table *tbl;
9796b6f302cSThomas Graf 	unsigned int i;
98097defe1eSThomas Graf 
98157699a40SYing Xue 	cancel_work_sync(&ht->run_work);
98257699a40SYing Xue 
98397defe1eSThomas Graf 	mutex_lock(&ht->mutex);
9846b6f302cSThomas Graf 	tbl = rht_dereference(ht->tbl, ht);
9856b6f302cSThomas Graf 	if (free_fn) {
9866b6f302cSThomas Graf 		for (i = 0; i < tbl->size; i++) {
9876b6f302cSThomas Graf 			struct rhash_head *pos, *next;
9886b6f302cSThomas Graf 
9896b6f302cSThomas Graf 			for (pos = rht_dereference(tbl->buckets[i], ht),
9906b6f302cSThomas Graf 			     next = !rht_is_a_nulls(pos) ?
9916b6f302cSThomas Graf 					rht_dereference(pos->next, ht) : NULL;
9926b6f302cSThomas Graf 			     !rht_is_a_nulls(pos);
9936b6f302cSThomas Graf 			     pos = next,
9946b6f302cSThomas Graf 			     next = !rht_is_a_nulls(pos) ?
9956b6f302cSThomas Graf 					rht_dereference(pos->next, ht) : NULL)
996*ca26893fSHerbert Xu 				rhashtable_free_one(ht, pos, free_fn, arg);
9976b6f302cSThomas Graf 		}
9986b6f302cSThomas Graf 	}
9996b6f302cSThomas Graf 
10006b6f302cSThomas Graf 	bucket_table_free(tbl);
100197defe1eSThomas Graf 	mutex_unlock(&ht->mutex);
10027e1e7763SThomas Graf }
10036b6f302cSThomas Graf EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
10046b6f302cSThomas Graf 
10056b6f302cSThomas Graf void rhashtable_destroy(struct rhashtable *ht)
10066b6f302cSThomas Graf {
10076b6f302cSThomas Graf 	return rhashtable_free_and_destroy(ht, NULL, NULL);
10086b6f302cSThomas Graf }
10097e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy);
1010