xref: /linux/lib/rhashtable.c (revision adc6a3ab192eb40fb9d8b093c87d9aa785af4513)
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>
22b2d09103SIngo Molnar #include <linux/rculist.h>
237e1e7763SThomas Graf #include <linux/slab.h>
247e1e7763SThomas Graf #include <linux/vmalloc.h>
257e1e7763SThomas Graf #include <linux/mm.h>
2687545899SDaniel Borkmann #include <linux/jhash.h>
277e1e7763SThomas Graf #include <linux/random.h>
287e1e7763SThomas Graf #include <linux/rhashtable.h>
2961d7b097SStephen Rothwell #include <linux/err.h>
306d795413SHauke Mehrtens #include <linux/export.h>
317e1e7763SThomas Graf 
327e1e7763SThomas Graf #define HASH_DEFAULT_SIZE	64UL
33c2e213cfSHerbert Xu #define HASH_MIN_SIZE		4U
3497defe1eSThomas Graf 
35da20420fSHerbert Xu union nested_table {
36da20420fSHerbert Xu 	union nested_table __rcu *table;
378f0db018SNeilBrown 	struct rhash_lock_head __rcu *bucket;
38da20420fSHerbert Xu };
39da20420fSHerbert Xu 
40988dfbd7SHerbert Xu static u32 head_hashfn(struct rhashtable *ht,
418d24c0b4SThomas Graf 		       const struct bucket_table *tbl,
428d24c0b4SThomas Graf 		       const struct rhash_head *he)
437e1e7763SThomas Graf {
4402fd97c3SHerbert Xu 	return rht_head_hashfn(ht, tbl, he, ht->p);
457e1e7763SThomas Graf }
467e1e7763SThomas Graf 
47a03eaec0SThomas Graf #ifdef CONFIG_PROVE_LOCKING
48a03eaec0SThomas Graf #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
49a03eaec0SThomas Graf 
50a03eaec0SThomas Graf int lockdep_rht_mutex_is_held(struct rhashtable *ht)
51a03eaec0SThomas Graf {
52a03eaec0SThomas Graf 	return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
53a03eaec0SThomas Graf }
54a03eaec0SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
55a03eaec0SThomas Graf 
56a03eaec0SThomas Graf int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
57a03eaec0SThomas Graf {
588f0db018SNeilBrown 	if (!debug_locks)
598f0db018SNeilBrown 		return 1;
608f0db018SNeilBrown 	if (unlikely(tbl->nest))
618f0db018SNeilBrown 		return 1;
628f0db018SNeilBrown 	return bit_spin_is_locked(1, (unsigned long *)&tbl->buckets[hash]);
63a03eaec0SThomas Graf }
64a03eaec0SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
65a03eaec0SThomas Graf #else
66a03eaec0SThomas Graf #define ASSERT_RHT_MUTEX(HT)
67a03eaec0SThomas Graf #endif
68a03eaec0SThomas Graf 
69da20420fSHerbert Xu static void nested_table_free(union nested_table *ntbl, unsigned int size)
70da20420fSHerbert Xu {
71da20420fSHerbert Xu 	const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
72da20420fSHerbert Xu 	const unsigned int len = 1 << shift;
73da20420fSHerbert Xu 	unsigned int i;
74da20420fSHerbert Xu 
75da20420fSHerbert Xu 	ntbl = rcu_dereference_raw(ntbl->table);
76da20420fSHerbert Xu 	if (!ntbl)
77da20420fSHerbert Xu 		return;
78da20420fSHerbert Xu 
79da20420fSHerbert Xu 	if (size > len) {
80da20420fSHerbert Xu 		size >>= shift;
81da20420fSHerbert Xu 		for (i = 0; i < len; i++)
82da20420fSHerbert Xu 			nested_table_free(ntbl + i, size);
83da20420fSHerbert Xu 	}
84da20420fSHerbert Xu 
85da20420fSHerbert Xu 	kfree(ntbl);
86da20420fSHerbert Xu }
87da20420fSHerbert Xu 
88da20420fSHerbert Xu static void nested_bucket_table_free(const struct bucket_table *tbl)
89da20420fSHerbert Xu {
90da20420fSHerbert Xu 	unsigned int size = tbl->size >> tbl->nest;
91da20420fSHerbert Xu 	unsigned int len = 1 << tbl->nest;
92da20420fSHerbert Xu 	union nested_table *ntbl;
93da20420fSHerbert Xu 	unsigned int i;
94da20420fSHerbert Xu 
95da20420fSHerbert Xu 	ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
96da20420fSHerbert Xu 
97da20420fSHerbert Xu 	for (i = 0; i < len; i++)
98da20420fSHerbert Xu 		nested_table_free(ntbl + i, size);
99da20420fSHerbert Xu 
100da20420fSHerbert Xu 	kfree(ntbl);
101da20420fSHerbert Xu }
102da20420fSHerbert Xu 
10397defe1eSThomas Graf static void bucket_table_free(const struct bucket_table *tbl)
10497defe1eSThomas Graf {
105da20420fSHerbert Xu 	if (tbl->nest)
106da20420fSHerbert Xu 		nested_bucket_table_free(tbl);
107da20420fSHerbert Xu 
10897defe1eSThomas Graf 	kvfree(tbl);
10997defe1eSThomas Graf }
11097defe1eSThomas Graf 
1119d901bc0SHerbert Xu static void bucket_table_free_rcu(struct rcu_head *head)
1129d901bc0SHerbert Xu {
1139d901bc0SHerbert Xu 	bucket_table_free(container_of(head, struct bucket_table, rcu));
1149d901bc0SHerbert Xu }
1159d901bc0SHerbert Xu 
116da20420fSHerbert Xu static union nested_table *nested_table_alloc(struct rhashtable *ht,
117da20420fSHerbert Xu 					      union nested_table __rcu **prev,
1185af68ef7SNeilBrown 					      bool leaf)
119da20420fSHerbert Xu {
120da20420fSHerbert Xu 	union nested_table *ntbl;
121da20420fSHerbert Xu 	int i;
122da20420fSHerbert Xu 
123da20420fSHerbert Xu 	ntbl = rcu_dereference(*prev);
124da20420fSHerbert Xu 	if (ntbl)
125da20420fSHerbert Xu 		return ntbl;
126da20420fSHerbert Xu 
127da20420fSHerbert Xu 	ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
128da20420fSHerbert Xu 
1295af68ef7SNeilBrown 	if (ntbl && leaf) {
1305af68ef7SNeilBrown 		for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
1319b4f64a2SNeilBrown 			INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
132da20420fSHerbert Xu 	}
133da20420fSHerbert Xu 
1347a41c294SNeilBrown 	if (cmpxchg(prev, NULL, ntbl) == NULL)
135da20420fSHerbert Xu 		return ntbl;
1367a41c294SNeilBrown 	/* Raced with another thread. */
1377a41c294SNeilBrown 	kfree(ntbl);
1387a41c294SNeilBrown 	return rcu_dereference(*prev);
139da20420fSHerbert Xu }
140da20420fSHerbert Xu 
141da20420fSHerbert Xu static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
142da20420fSHerbert Xu 						      size_t nbuckets,
143da20420fSHerbert Xu 						      gfp_t gfp)
144da20420fSHerbert Xu {
145da20420fSHerbert Xu 	const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
146da20420fSHerbert Xu 	struct bucket_table *tbl;
147da20420fSHerbert Xu 	size_t size;
148da20420fSHerbert Xu 
149da20420fSHerbert Xu 	if (nbuckets < (1 << (shift + 1)))
150da20420fSHerbert Xu 		return NULL;
151da20420fSHerbert Xu 
152da20420fSHerbert Xu 	size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
153da20420fSHerbert Xu 
154da20420fSHerbert Xu 	tbl = kzalloc(size, gfp);
155da20420fSHerbert Xu 	if (!tbl)
156da20420fSHerbert Xu 		return NULL;
157da20420fSHerbert Xu 
158da20420fSHerbert Xu 	if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
1595af68ef7SNeilBrown 				false)) {
160da20420fSHerbert Xu 		kfree(tbl);
161da20420fSHerbert Xu 		return NULL;
162da20420fSHerbert Xu 	}
163da20420fSHerbert Xu 
164da20420fSHerbert Xu 	tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
165da20420fSHerbert Xu 
166da20420fSHerbert Xu 	return tbl;
167da20420fSHerbert Xu }
168da20420fSHerbert Xu 
16997defe1eSThomas Graf static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
170b9ecfdaaSHerbert Xu 					       size_t nbuckets,
171b9ecfdaaSHerbert Xu 					       gfp_t gfp)
1727e1e7763SThomas Graf {
173eb6d1abfSDaniel Borkmann 	struct bucket_table *tbl = NULL;
1748f0db018SNeilBrown 	size_t size;
175f89bd6f8SThomas Graf 	int i;
176149212f0SNeilBrown 	static struct lock_class_key __key;
1777e1e7763SThomas Graf 
178c252aa3eSGustavo A. R. Silva 	tbl = kvzalloc(struct_size(tbl, buckets, nbuckets), gfp);
179da20420fSHerbert Xu 
180da20420fSHerbert Xu 	size = nbuckets;
181da20420fSHerbert Xu 
1822d22ecf6SDavidlohr Bueso 	if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
183da20420fSHerbert Xu 		tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
184da20420fSHerbert Xu 		nbuckets = 0;
185da20420fSHerbert Xu 	}
1862d22ecf6SDavidlohr Bueso 
1877e1e7763SThomas Graf 	if (tbl == NULL)
1887e1e7763SThomas Graf 		return NULL;
1897e1e7763SThomas Graf 
190149212f0SNeilBrown 	lockdep_init_map(&tbl->dep_map, "rhashtable_bucket", &__key, 0);
191149212f0SNeilBrown 
192da20420fSHerbert Xu 	tbl->size = size;
1937e1e7763SThomas Graf 
1944feb7c7aSNeilBrown 	rcu_head_init(&tbl->rcu);
195eddee5baSHerbert Xu 	INIT_LIST_HEAD(&tbl->walkers);
196eddee5baSHerbert Xu 
197d48ad080SJason A. Donenfeld 	tbl->hash_rnd = get_random_u32();
1985269b53dSHerbert Xu 
199f89bd6f8SThomas Graf 	for (i = 0; i < nbuckets; i++)
2009b4f64a2SNeilBrown 		INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
201f89bd6f8SThomas Graf 
20297defe1eSThomas Graf 	return tbl;
2037e1e7763SThomas Graf }
2047e1e7763SThomas Graf 
205b824478bSHerbert Xu static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
206b824478bSHerbert Xu 						  struct bucket_table *tbl)
207b824478bSHerbert Xu {
208b824478bSHerbert Xu 	struct bucket_table *new_tbl;
209b824478bSHerbert Xu 
210b824478bSHerbert Xu 	do {
211b824478bSHerbert Xu 		new_tbl = tbl;
212b824478bSHerbert Xu 		tbl = rht_dereference_rcu(tbl->future_tbl, ht);
213b824478bSHerbert Xu 	} while (tbl);
214b824478bSHerbert Xu 
215b824478bSHerbert Xu 	return new_tbl;
216b824478bSHerbert Xu }
217b824478bSHerbert Xu 
2188f0db018SNeilBrown static int rhashtable_rehash_one(struct rhashtable *ht,
2198f0db018SNeilBrown 				 struct rhash_lock_head __rcu **bkt,
2208f0db018SNeilBrown 				 unsigned int old_hash)
221a5ec68e3SThomas Graf {
222aa34a6cbSHerbert Xu 	struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
223c0690016SNeilBrown 	struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
224da20420fSHerbert Xu 	int err = -EAGAIN;
225aa34a6cbSHerbert Xu 	struct rhash_head *head, *next, *entry;
226e4edbe3cSNeilBrown 	struct rhash_head __rcu **pprev = NULL;
227299e5c32SThomas Graf 	unsigned int new_hash;
228a5ec68e3SThomas Graf 
229da20420fSHerbert Xu 	if (new_tbl->nest)
230da20420fSHerbert Xu 		goto out;
231da20420fSHerbert Xu 
232da20420fSHerbert Xu 	err = -ENOENT;
233da20420fSHerbert Xu 
234*adc6a3abSNeilBrown 	rht_for_each_from(entry, rht_ptr(bkt, old_tbl, old_hash),
235*adc6a3abSNeilBrown 			  old_tbl, old_hash) {
236aa34a6cbSHerbert Xu 		err = 0;
237aa34a6cbSHerbert Xu 		next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
238a5ec68e3SThomas Graf 
239aa34a6cbSHerbert Xu 		if (rht_is_a_nulls(next))
2407e1e7763SThomas Graf 			break;
24197defe1eSThomas Graf 
242aa34a6cbSHerbert Xu 		pprev = &entry->next;
2437e1e7763SThomas Graf 	}
2447e1e7763SThomas Graf 
245aa34a6cbSHerbert Xu 	if (err)
246aa34a6cbSHerbert Xu 		goto out;
24797defe1eSThomas Graf 
248aa34a6cbSHerbert Xu 	new_hash = head_hashfn(ht, new_tbl, entry);
249a5ec68e3SThomas Graf 
250149212f0SNeilBrown 	rht_lock_nested(new_tbl, &new_tbl->buckets[new_hash], SINGLE_DEPTH_NESTING);
251aa34a6cbSHerbert Xu 
252*adc6a3abSNeilBrown 	head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
253aa34a6cbSHerbert Xu 
254aa34a6cbSHerbert Xu 	RCU_INIT_POINTER(entry->next, head);
255aa34a6cbSHerbert Xu 
256149212f0SNeilBrown 	rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry);
257aa34a6cbSHerbert Xu 
2588f0db018SNeilBrown 	if (pprev)
259aa34a6cbSHerbert Xu 		rcu_assign_pointer(*pprev, next);
2608f0db018SNeilBrown 	else
2618f0db018SNeilBrown 		/* Need to preserved the bit lock. */
2628f0db018SNeilBrown 		rcu_assign_pointer(*bkt, rht_ptr_locked(next));
263aa34a6cbSHerbert Xu 
264aa34a6cbSHerbert Xu out:
265aa34a6cbSHerbert Xu 	return err;
26697defe1eSThomas Graf }
26797defe1eSThomas Graf 
268da20420fSHerbert Xu static int rhashtable_rehash_chain(struct rhashtable *ht,
269299e5c32SThomas Graf 				    unsigned int old_hash)
27097defe1eSThomas Graf {
271aa34a6cbSHerbert Xu 	struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
2728f0db018SNeilBrown 	struct rhash_lock_head __rcu **bkt = rht_bucket_var(old_tbl, old_hash);
273da20420fSHerbert Xu 	int err;
2747cd10db8SThomas Graf 
2758f0db018SNeilBrown 	if (!bkt)
2768f0db018SNeilBrown 		return 0;
277149212f0SNeilBrown 	rht_lock(old_tbl, bkt);
278aa34a6cbSHerbert Xu 
2798f0db018SNeilBrown 	while (!(err = rhashtable_rehash_one(ht, bkt, old_hash)))
280aa34a6cbSHerbert Xu 		;
281da20420fSHerbert Xu 
2824feb7c7aSNeilBrown 	if (err == -ENOENT)
283da20420fSHerbert Xu 		err = 0;
284149212f0SNeilBrown 	rht_unlock(old_tbl, bkt);
285da20420fSHerbert Xu 
286da20420fSHerbert Xu 	return err;
287aa34a6cbSHerbert Xu }
288aa34a6cbSHerbert Xu 
289b824478bSHerbert Xu static int rhashtable_rehash_attach(struct rhashtable *ht,
290b824478bSHerbert Xu 				    struct bucket_table *old_tbl,
291aa34a6cbSHerbert Xu 				    struct bucket_table *new_tbl)
292aa34a6cbSHerbert Xu {
293aa34a6cbSHerbert Xu 	/* Make insertions go into the new, empty table right away. Deletions
294aa34a6cbSHerbert Xu 	 * and lookups will be attempted in both tables until we synchronize.
2950ad66449SNeilBrown 	 * As cmpxchg() provides strong barriers, we do not need
2960ad66449SNeilBrown 	 * rcu_assign_pointer().
297aa34a6cbSHerbert Xu 	 */
298aa34a6cbSHerbert Xu 
2990ad66449SNeilBrown 	if (cmpxchg(&old_tbl->future_tbl, NULL, new_tbl) != NULL)
3000ad66449SNeilBrown 		return -EEXIST;
301b824478bSHerbert Xu 
302b824478bSHerbert Xu 	return 0;
303b824478bSHerbert Xu }
304b824478bSHerbert Xu 
305b824478bSHerbert Xu static int rhashtable_rehash_table(struct rhashtable *ht)
306b824478bSHerbert Xu {
307b824478bSHerbert Xu 	struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
308b824478bSHerbert Xu 	struct bucket_table *new_tbl;
309b824478bSHerbert Xu 	struct rhashtable_walker *walker;
310299e5c32SThomas Graf 	unsigned int old_hash;
311da20420fSHerbert Xu 	int err;
312b824478bSHerbert Xu 
313b824478bSHerbert Xu 	new_tbl = rht_dereference(old_tbl->future_tbl, ht);
314b824478bSHerbert Xu 	if (!new_tbl)
315b824478bSHerbert Xu 		return 0;
316b824478bSHerbert Xu 
317da20420fSHerbert Xu 	for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
318da20420fSHerbert Xu 		err = rhashtable_rehash_chain(ht, old_hash);
319da20420fSHerbert Xu 		if (err)
320da20420fSHerbert Xu 			return err;
321ae6da1f5SEric Dumazet 		cond_resched();
322da20420fSHerbert Xu 	}
323aa34a6cbSHerbert Xu 
324aa34a6cbSHerbert Xu 	/* Publish the new table pointer. */
325aa34a6cbSHerbert Xu 	rcu_assign_pointer(ht->tbl, new_tbl);
326aa34a6cbSHerbert Xu 
327ba7c95eaSHerbert Xu 	spin_lock(&ht->lock);
328eddee5baSHerbert Xu 	list_for_each_entry(walker, &old_tbl->walkers, list)
329eddee5baSHerbert Xu 		walker->tbl = NULL;
330eddee5baSHerbert Xu 
331aa34a6cbSHerbert Xu 	/* Wait for readers. All new readers will see the new
332aa34a6cbSHerbert Xu 	 * table, and thus no references to the old table will
333aa34a6cbSHerbert Xu 	 * remain.
3344feb7c7aSNeilBrown 	 * We do this inside the locked region so that
3354feb7c7aSNeilBrown 	 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
3364feb7c7aSNeilBrown 	 * to check if it should not re-link the table.
337aa34a6cbSHerbert Xu 	 */
3389d901bc0SHerbert Xu 	call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
3394feb7c7aSNeilBrown 	spin_unlock(&ht->lock);
340b824478bSHerbert Xu 
341b824478bSHerbert Xu 	return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
3427e1e7763SThomas Graf }
3437e1e7763SThomas Graf 
344da20420fSHerbert Xu static int rhashtable_rehash_alloc(struct rhashtable *ht,
345da20420fSHerbert Xu 				   struct bucket_table *old_tbl,
346da20420fSHerbert Xu 				   unsigned int size)
3477e1e7763SThomas Graf {
348da20420fSHerbert Xu 	struct bucket_table *new_tbl;
349b824478bSHerbert Xu 	int err;
3507e1e7763SThomas Graf 
3517e1e7763SThomas Graf 	ASSERT_RHT_MUTEX(ht);
3527e1e7763SThomas Graf 
353da20420fSHerbert Xu 	new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
3547e1e7763SThomas Graf 	if (new_tbl == NULL)
3557e1e7763SThomas Graf 		return -ENOMEM;
3567e1e7763SThomas Graf 
357b824478bSHerbert Xu 	err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
358b824478bSHerbert Xu 	if (err)
359b824478bSHerbert Xu 		bucket_table_free(new_tbl);
360b824478bSHerbert Xu 
361b824478bSHerbert Xu 	return err;
3627e1e7763SThomas Graf }
3637e1e7763SThomas Graf 
3647e1e7763SThomas Graf /**
3657e1e7763SThomas Graf  * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
3667e1e7763SThomas Graf  * @ht:		the hash table to shrink
3677e1e7763SThomas Graf  *
36818093d1cSHerbert Xu  * This function shrinks the hash table to fit, i.e., the smallest
36918093d1cSHerbert Xu  * size would not cause it to expand right away automatically.
3707e1e7763SThomas Graf  *
37197defe1eSThomas Graf  * The caller must ensure that no concurrent resizing occurs by holding
37297defe1eSThomas Graf  * ht->mutex.
37397defe1eSThomas Graf  *
3747e1e7763SThomas Graf  * The caller must ensure that no concurrent table mutations take place.
3757e1e7763SThomas Graf  * It is however valid to have concurrent lookups if they are RCU protected.
37697defe1eSThomas Graf  *
37797defe1eSThomas Graf  * It is valid to have concurrent insertions and deletions protected by per
37897defe1eSThomas Graf  * bucket locks or concurrent RCU protected lookups and traversals.
3797e1e7763SThomas Graf  */
380b824478bSHerbert Xu static int rhashtable_shrink(struct rhashtable *ht)
3817e1e7763SThomas Graf {
382da20420fSHerbert Xu 	struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
38312311959SVegard Nossum 	unsigned int nelems = atomic_read(&ht->nelems);
38412311959SVegard Nossum 	unsigned int size = 0;
3857e1e7763SThomas Graf 
38612311959SVegard Nossum 	if (nelems)
38712311959SVegard Nossum 		size = roundup_pow_of_two(nelems * 3 / 2);
38818093d1cSHerbert Xu 	if (size < ht->p.min_size)
38918093d1cSHerbert Xu 		size = ht->p.min_size;
39018093d1cSHerbert Xu 
39118093d1cSHerbert Xu 	if (old_tbl->size <= size)
39218093d1cSHerbert Xu 		return 0;
39318093d1cSHerbert Xu 
394b824478bSHerbert Xu 	if (rht_dereference(old_tbl->future_tbl, ht))
395b824478bSHerbert Xu 		return -EEXIST;
396b824478bSHerbert Xu 
397da20420fSHerbert Xu 	return rhashtable_rehash_alloc(ht, old_tbl, size);
3987e1e7763SThomas Graf }
3997e1e7763SThomas Graf 
40097defe1eSThomas Graf static void rht_deferred_worker(struct work_struct *work)
40197defe1eSThomas Graf {
40297defe1eSThomas Graf 	struct rhashtable *ht;
40397defe1eSThomas Graf 	struct bucket_table *tbl;
404b824478bSHerbert Xu 	int err = 0;
40597defe1eSThomas Graf 
40657699a40SYing Xue 	ht = container_of(work, struct rhashtable, run_work);
40797defe1eSThomas Graf 	mutex_lock(&ht->mutex);
40828134a53SHerbert Xu 
40997defe1eSThomas Graf 	tbl = rht_dereference(ht->tbl, ht);
410b824478bSHerbert Xu 	tbl = rhashtable_last_table(ht, tbl);
41197defe1eSThomas Graf 
412a5b6846fSDaniel Borkmann 	if (rht_grow_above_75(ht, tbl))
413da20420fSHerbert Xu 		err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
414b5e2c150SThomas Graf 	else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
415da20420fSHerbert Xu 		err = rhashtable_shrink(ht);
416da20420fSHerbert Xu 	else if (tbl->nest)
417da20420fSHerbert Xu 		err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
418b824478bSHerbert Xu 
419408f13efSHerbert Xu 	if (!err || err == -EEXIST) {
420408f13efSHerbert Xu 		int nerr;
421408f13efSHerbert Xu 
422408f13efSHerbert Xu 		nerr = rhashtable_rehash_table(ht);
423408f13efSHerbert Xu 		err = err ?: nerr;
424408f13efSHerbert Xu 	}
425b824478bSHerbert Xu 
42697defe1eSThomas Graf 	mutex_unlock(&ht->mutex);
427b824478bSHerbert Xu 
428b824478bSHerbert Xu 	if (err)
429b824478bSHerbert Xu 		schedule_work(&ht->run_work);
43097defe1eSThomas Graf }
43197defe1eSThomas Graf 
432ca26893fSHerbert Xu static int rhashtable_insert_rehash(struct rhashtable *ht,
4333cf92222SHerbert Xu 				    struct bucket_table *tbl)
434ccd57b1bSHerbert Xu {
435ccd57b1bSHerbert Xu 	struct bucket_table *old_tbl;
436ccd57b1bSHerbert Xu 	struct bucket_table *new_tbl;
437ccd57b1bSHerbert Xu 	unsigned int size;
438ccd57b1bSHerbert Xu 	int err;
439ccd57b1bSHerbert Xu 
440ccd57b1bSHerbert Xu 	old_tbl = rht_dereference_rcu(ht->tbl, ht);
441ccd57b1bSHerbert Xu 
442ccd57b1bSHerbert Xu 	size = tbl->size;
443ccd57b1bSHerbert Xu 
4443cf92222SHerbert Xu 	err = -EBUSY;
4453cf92222SHerbert Xu 
446ccd57b1bSHerbert Xu 	if (rht_grow_above_75(ht, tbl))
447ccd57b1bSHerbert Xu 		size *= 2;
448a87b9ebfSThomas Graf 	/* Do not schedule more than one rehash */
449a87b9ebfSThomas Graf 	else if (old_tbl != tbl)
4503cf92222SHerbert Xu 		goto fail;
4513cf92222SHerbert Xu 
4523cf92222SHerbert Xu 	err = -ENOMEM;
453ccd57b1bSHerbert Xu 
45493f976b5SDavidlohr Bueso 	new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
4553cf92222SHerbert Xu 	if (new_tbl == NULL)
4563cf92222SHerbert Xu 		goto fail;
457ccd57b1bSHerbert Xu 
458ccd57b1bSHerbert Xu 	err = rhashtable_rehash_attach(ht, tbl, new_tbl);
459ccd57b1bSHerbert Xu 	if (err) {
460ccd57b1bSHerbert Xu 		bucket_table_free(new_tbl);
461ccd57b1bSHerbert Xu 		if (err == -EEXIST)
462ccd57b1bSHerbert Xu 			err = 0;
463ccd57b1bSHerbert Xu 	} else
464ccd57b1bSHerbert Xu 		schedule_work(&ht->run_work);
465ccd57b1bSHerbert Xu 
466ccd57b1bSHerbert Xu 	return err;
4673cf92222SHerbert Xu 
4683cf92222SHerbert Xu fail:
4693cf92222SHerbert Xu 	/* Do not fail the insert if someone else did a rehash. */
470c0690016SNeilBrown 	if (likely(rcu_access_pointer(tbl->future_tbl)))
4713cf92222SHerbert Xu 		return 0;
4723cf92222SHerbert Xu 
4733cf92222SHerbert Xu 	/* Schedule async rehash to retry allocation in process context. */
4743cf92222SHerbert Xu 	if (err == -ENOMEM)
4753cf92222SHerbert Xu 		schedule_work(&ht->run_work);
4763cf92222SHerbert Xu 
4773cf92222SHerbert Xu 	return err;
478ccd57b1bSHerbert Xu }
479ccd57b1bSHerbert Xu 
480ca26893fSHerbert Xu static void *rhashtable_lookup_one(struct rhashtable *ht,
4818f0db018SNeilBrown 				   struct rhash_lock_head __rcu **bkt,
482ca26893fSHerbert Xu 				   struct bucket_table *tbl, unsigned int hash,
483ca26893fSHerbert Xu 				   const void *key, struct rhash_head *obj)
48402fd97c3SHerbert Xu {
485ca26893fSHerbert Xu 	struct rhashtable_compare_arg arg = {
486ca26893fSHerbert Xu 		.ht = ht,
487ca26893fSHerbert Xu 		.key = key,
488ca26893fSHerbert Xu 	};
489e4edbe3cSNeilBrown 	struct rhash_head __rcu **pprev = NULL;
49002fd97c3SHerbert Xu 	struct rhash_head *head;
491ca26893fSHerbert Xu 	int elasticity;
49202fd97c3SHerbert Xu 
4935f8ddeabSFlorian Westphal 	elasticity = RHT_ELASTICITY;
494*adc6a3abSNeilBrown 	rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
495ca26893fSHerbert Xu 		struct rhlist_head *list;
496ca26893fSHerbert Xu 		struct rhlist_head *plist;
49702fd97c3SHerbert Xu 
498ca26893fSHerbert Xu 		elasticity--;
499ca26893fSHerbert Xu 		if (!key ||
500ca26893fSHerbert Xu 		    (ht->p.obj_cmpfn ?
501ca26893fSHerbert Xu 		     ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
502d3dcf8ebSPaul Blakey 		     rhashtable_compare(&arg, rht_obj(ht, head)))) {
503d3dcf8ebSPaul Blakey 			pprev = &head->next;
504ca26893fSHerbert Xu 			continue;
505d3dcf8ebSPaul Blakey 		}
506ca26893fSHerbert Xu 
507ca26893fSHerbert Xu 		if (!ht->rhlist)
508ca26893fSHerbert Xu 			return rht_obj(ht, head);
509ca26893fSHerbert Xu 
510ca26893fSHerbert Xu 		list = container_of(obj, struct rhlist_head, rhead);
511ca26893fSHerbert Xu 		plist = container_of(head, struct rhlist_head, rhead);
512ca26893fSHerbert Xu 
513ca26893fSHerbert Xu 		RCU_INIT_POINTER(list->next, plist);
514ca26893fSHerbert Xu 		head = rht_dereference_bucket(head->next, tbl, hash);
515ca26893fSHerbert Xu 		RCU_INIT_POINTER(list->rhead.next, head);
5168f0db018SNeilBrown 		if (pprev)
517ca26893fSHerbert Xu 			rcu_assign_pointer(*pprev, obj);
5188f0db018SNeilBrown 		else
5198f0db018SNeilBrown 			/* Need to preserve the bit lock */
5208f0db018SNeilBrown 			rcu_assign_pointer(*bkt, rht_ptr_locked(obj));
521ca26893fSHerbert Xu 
522ca26893fSHerbert Xu 		return NULL;
5235ca8cc5bSPablo Neira Ayuso 	}
52402fd97c3SHerbert Xu 
525ca26893fSHerbert Xu 	if (elasticity <= 0)
526ca26893fSHerbert Xu 		return ERR_PTR(-EAGAIN);
527ca26893fSHerbert Xu 
528ca26893fSHerbert Xu 	return ERR_PTR(-ENOENT);
529ca26893fSHerbert Xu }
530ca26893fSHerbert Xu 
531ca26893fSHerbert Xu static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
5328f0db018SNeilBrown 						  struct rhash_lock_head __rcu **bkt,
533ca26893fSHerbert Xu 						  struct bucket_table *tbl,
534ca26893fSHerbert Xu 						  unsigned int hash,
535ca26893fSHerbert Xu 						  struct rhash_head *obj,
536ca26893fSHerbert Xu 						  void *data)
537ca26893fSHerbert Xu {
538ca26893fSHerbert Xu 	struct bucket_table *new_tbl;
539ca26893fSHerbert Xu 	struct rhash_head *head;
540ca26893fSHerbert Xu 
541ca26893fSHerbert Xu 	if (!IS_ERR_OR_NULL(data))
542ca26893fSHerbert Xu 		return ERR_PTR(-EEXIST);
543ca26893fSHerbert Xu 
544ca26893fSHerbert Xu 	if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
545ca26893fSHerbert Xu 		return ERR_CAST(data);
546ca26893fSHerbert Xu 
547c0690016SNeilBrown 	new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
548ca26893fSHerbert Xu 	if (new_tbl)
549ca26893fSHerbert Xu 		return new_tbl;
550ca26893fSHerbert Xu 
551ca26893fSHerbert Xu 	if (PTR_ERR(data) != -ENOENT)
552ca26893fSHerbert Xu 		return ERR_CAST(data);
553ca26893fSHerbert Xu 
55407ee0722SHerbert Xu 	if (unlikely(rht_grow_above_max(ht, tbl)))
555ca26893fSHerbert Xu 		return ERR_PTR(-E2BIG);
55607ee0722SHerbert Xu 
557ca26893fSHerbert Xu 	if (unlikely(rht_grow_above_100(ht, tbl)))
558ca26893fSHerbert Xu 		return ERR_PTR(-EAGAIN);
55902fd97c3SHerbert Xu 
560*adc6a3abSNeilBrown 	head = rht_ptr(bkt, tbl, hash);
56102fd97c3SHerbert Xu 
56202fd97c3SHerbert Xu 	RCU_INIT_POINTER(obj->next, head);
563ca26893fSHerbert Xu 	if (ht->rhlist) {
564ca26893fSHerbert Xu 		struct rhlist_head *list;
565ca26893fSHerbert Xu 
566ca26893fSHerbert Xu 		list = container_of(obj, struct rhlist_head, rhead);
567ca26893fSHerbert Xu 		RCU_INIT_POINTER(list->next, NULL);
568ca26893fSHerbert Xu 	}
56902fd97c3SHerbert Xu 
5708f0db018SNeilBrown 	/* bkt is always the head of the list, so it holds
5718f0db018SNeilBrown 	 * the lock, which we need to preserve
5728f0db018SNeilBrown 	 */
5738f0db018SNeilBrown 	rcu_assign_pointer(*bkt, rht_ptr_locked(obj));
57402fd97c3SHerbert Xu 
57502fd97c3SHerbert Xu 	atomic_inc(&ht->nelems);
576ca26893fSHerbert Xu 	if (rht_grow_above_75(ht, tbl))
577ca26893fSHerbert Xu 		schedule_work(&ht->run_work);
57802fd97c3SHerbert Xu 
5793cf92222SHerbert Xu 	return NULL;
580ca26893fSHerbert Xu }
581ca26893fSHerbert Xu 
582ca26893fSHerbert Xu static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
583ca26893fSHerbert Xu 				   struct rhash_head *obj)
584ca26893fSHerbert Xu {
585ca26893fSHerbert Xu 	struct bucket_table *new_tbl;
586ca26893fSHerbert Xu 	struct bucket_table *tbl;
5878f0db018SNeilBrown 	struct rhash_lock_head __rcu **bkt;
588ca26893fSHerbert Xu 	unsigned int hash;
589ca26893fSHerbert Xu 	void *data;
590ca26893fSHerbert Xu 
5914feb7c7aSNeilBrown 	new_tbl = rcu_dereference(ht->tbl);
592ca26893fSHerbert Xu 
5934feb7c7aSNeilBrown 	do {
594ca26893fSHerbert Xu 		tbl = new_tbl;
595ca26893fSHerbert Xu 		hash = rht_head_hashfn(ht, tbl, obj, ht->p);
5968f0db018SNeilBrown 		if (rcu_access_pointer(tbl->future_tbl))
5978f0db018SNeilBrown 			/* Failure is OK */
5988f0db018SNeilBrown 			bkt = rht_bucket_var(tbl, hash);
5998f0db018SNeilBrown 		else
6008f0db018SNeilBrown 			bkt = rht_bucket_insert(ht, tbl, hash);
6018f0db018SNeilBrown 		if (bkt == NULL) {
6028f0db018SNeilBrown 			new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
6038f0db018SNeilBrown 			data = ERR_PTR(-EAGAIN);
6048f0db018SNeilBrown 		} else {
605149212f0SNeilBrown 			rht_lock(tbl, bkt);
6068f0db018SNeilBrown 			data = rhashtable_lookup_one(ht, bkt, tbl,
6078f0db018SNeilBrown 						     hash, key, obj);
6088f0db018SNeilBrown 			new_tbl = rhashtable_insert_one(ht, bkt, tbl,
6098f0db018SNeilBrown 							hash, obj, data);
610ca26893fSHerbert Xu 			if (PTR_ERR(new_tbl) != -EEXIST)
611ca26893fSHerbert Xu 				data = ERR_CAST(new_tbl);
612ca26893fSHerbert Xu 
613149212f0SNeilBrown 			rht_unlock(tbl, bkt);
6148f0db018SNeilBrown 		}
6154feb7c7aSNeilBrown 	} while (!IS_ERR_OR_NULL(new_tbl));
616ca26893fSHerbert Xu 
617ca26893fSHerbert Xu 	if (PTR_ERR(data) == -EAGAIN)
618ca26893fSHerbert Xu 		data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
619ca26893fSHerbert Xu 			       -EAGAIN);
620ca26893fSHerbert Xu 
621ca26893fSHerbert Xu 	return data;
622ca26893fSHerbert Xu }
623ca26893fSHerbert Xu 
624ca26893fSHerbert Xu void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
625ca26893fSHerbert Xu 			     struct rhash_head *obj)
626ca26893fSHerbert Xu {
627ca26893fSHerbert Xu 	void *data;
628ca26893fSHerbert Xu 
629ca26893fSHerbert Xu 	do {
630ca26893fSHerbert Xu 		rcu_read_lock();
631ca26893fSHerbert Xu 		data = rhashtable_try_insert(ht, key, obj);
632ca26893fSHerbert Xu 		rcu_read_unlock();
633ca26893fSHerbert Xu 	} while (PTR_ERR(data) == -EAGAIN);
634ca26893fSHerbert Xu 
635ca26893fSHerbert Xu 	return data;
63602fd97c3SHerbert Xu }
63702fd97c3SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
63802fd97c3SHerbert Xu 
639f2dba9c6SHerbert Xu /**
640246779ddSHerbert Xu  * rhashtable_walk_enter - Initialise an iterator
641f2dba9c6SHerbert Xu  * @ht:		Table to walk over
642f2dba9c6SHerbert Xu  * @iter:	Hash table Iterator
643f2dba9c6SHerbert Xu  *
644f2dba9c6SHerbert Xu  * This function prepares a hash table walk.
645f2dba9c6SHerbert Xu  *
646f2dba9c6SHerbert Xu  * Note that if you restart a walk after rhashtable_walk_stop you
647f2dba9c6SHerbert Xu  * may see the same object twice.  Also, you may miss objects if
648f2dba9c6SHerbert Xu  * there are removals in between rhashtable_walk_stop and the next
649f2dba9c6SHerbert Xu  * call to rhashtable_walk_start.
650f2dba9c6SHerbert Xu  *
651f2dba9c6SHerbert Xu  * For a completely stable walk you should construct your own data
652f2dba9c6SHerbert Xu  * structure outside the hash table.
653f2dba9c6SHerbert Xu  *
65482266e98SNeilBrown  * This function may be called from any process context, including
65582266e98SNeilBrown  * non-preemptable context, but cannot be called from softirq or
65682266e98SNeilBrown  * hardirq context.
657f2dba9c6SHerbert Xu  *
658246779ddSHerbert Xu  * You must call rhashtable_walk_exit after this function returns.
659f2dba9c6SHerbert Xu  */
660246779ddSHerbert Xu void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
661f2dba9c6SHerbert Xu {
662f2dba9c6SHerbert Xu 	iter->ht = ht;
663f2dba9c6SHerbert Xu 	iter->p = NULL;
664f2dba9c6SHerbert Xu 	iter->slot = 0;
665f2dba9c6SHerbert Xu 	iter->skip = 0;
6662db54b47STom Herbert 	iter->end_of_table = 0;
667f2dba9c6SHerbert Xu 
668c6ff5268SHerbert Xu 	spin_lock(&ht->lock);
669246779ddSHerbert Xu 	iter->walker.tbl =
670179ccc0aSHerbert Xu 		rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
671246779ddSHerbert Xu 	list_add(&iter->walker.list, &iter->walker.tbl->walkers);
672c6ff5268SHerbert Xu 	spin_unlock(&ht->lock);
673f2dba9c6SHerbert Xu }
674246779ddSHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
675f2dba9c6SHerbert Xu 
676f2dba9c6SHerbert Xu /**
677f2dba9c6SHerbert Xu  * rhashtable_walk_exit - Free an iterator
678f2dba9c6SHerbert Xu  * @iter:	Hash table Iterator
679f2dba9c6SHerbert Xu  *
6806c4128f6SHerbert Xu  * This function frees resources allocated by rhashtable_walk_enter.
681f2dba9c6SHerbert Xu  */
682f2dba9c6SHerbert Xu void rhashtable_walk_exit(struct rhashtable_iter *iter)
683f2dba9c6SHerbert Xu {
684c6ff5268SHerbert Xu 	spin_lock(&iter->ht->lock);
685246779ddSHerbert Xu 	if (iter->walker.tbl)
686246779ddSHerbert Xu 		list_del(&iter->walker.list);
687c6ff5268SHerbert Xu 	spin_unlock(&iter->ht->lock);
688f2dba9c6SHerbert Xu }
689f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
690f2dba9c6SHerbert Xu 
691f2dba9c6SHerbert Xu /**
69297a6ec4aSTom Herbert  * rhashtable_walk_start_check - Start a hash table walk
693f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
694f2dba9c6SHerbert Xu  *
6950647169cSAndreas Gruenbacher  * Start a hash table walk at the current iterator position.  Note that we take
6960647169cSAndreas Gruenbacher  * the RCU lock in all cases including when we return an error.  So you must
6970647169cSAndreas Gruenbacher  * always call rhashtable_walk_stop to clean up.
698f2dba9c6SHerbert Xu  *
699f2dba9c6SHerbert Xu  * Returns zero if successful.
700f2dba9c6SHerbert Xu  *
701f2dba9c6SHerbert Xu  * Returns -EAGAIN if resize event occured.  Note that the iterator
702f2dba9c6SHerbert Xu  * will rewind back to the beginning and you may use it immediately
703f2dba9c6SHerbert Xu  * by calling rhashtable_walk_next.
70497a6ec4aSTom Herbert  *
70597a6ec4aSTom Herbert  * rhashtable_walk_start is defined as an inline variant that returns
70697a6ec4aSTom Herbert  * void. This is preferred in cases where the caller would ignore
70797a6ec4aSTom Herbert  * resize events and always continue.
708f2dba9c6SHerbert Xu  */
70997a6ec4aSTom Herbert int rhashtable_walk_start_check(struct rhashtable_iter *iter)
710db4374f4SThomas Graf 	__acquires(RCU)
711f2dba9c6SHerbert Xu {
712eddee5baSHerbert Xu 	struct rhashtable *ht = iter->ht;
7135d240a89SNeilBrown 	bool rhlist = ht->rhlist;
714eddee5baSHerbert Xu 
715f2dba9c6SHerbert Xu 	rcu_read_lock();
716f2dba9c6SHerbert Xu 
717c6ff5268SHerbert Xu 	spin_lock(&ht->lock);
718246779ddSHerbert Xu 	if (iter->walker.tbl)
719246779ddSHerbert Xu 		list_del(&iter->walker.list);
720c6ff5268SHerbert Xu 	spin_unlock(&ht->lock);
721eddee5baSHerbert Xu 
7225d240a89SNeilBrown 	if (iter->end_of_table)
7235d240a89SNeilBrown 		return 0;
7245d240a89SNeilBrown 	if (!iter->walker.tbl) {
725246779ddSHerbert Xu 		iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
726b41cc04bSNeilBrown 		iter->slot = 0;
727b41cc04bSNeilBrown 		iter->skip = 0;
728f2dba9c6SHerbert Xu 		return -EAGAIN;
729f2dba9c6SHerbert Xu 	}
730f2dba9c6SHerbert Xu 
7315d240a89SNeilBrown 	if (iter->p && !rhlist) {
7325d240a89SNeilBrown 		/*
7335d240a89SNeilBrown 		 * We need to validate that 'p' is still in the table, and
7345d240a89SNeilBrown 		 * if so, update 'skip'
7355d240a89SNeilBrown 		 */
7365d240a89SNeilBrown 		struct rhash_head *p;
7375d240a89SNeilBrown 		int skip = 0;
7385d240a89SNeilBrown 		rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
7395d240a89SNeilBrown 			skip++;
7405d240a89SNeilBrown 			if (p == iter->p) {
7415d240a89SNeilBrown 				iter->skip = skip;
7425d240a89SNeilBrown 				goto found;
7435d240a89SNeilBrown 			}
7445d240a89SNeilBrown 		}
7455d240a89SNeilBrown 		iter->p = NULL;
7465d240a89SNeilBrown 	} else if (iter->p && rhlist) {
7475d240a89SNeilBrown 		/* Need to validate that 'list' is still in the table, and
7485d240a89SNeilBrown 		 * if so, update 'skip' and 'p'.
7495d240a89SNeilBrown 		 */
7505d240a89SNeilBrown 		struct rhash_head *p;
7515d240a89SNeilBrown 		struct rhlist_head *list;
7525d240a89SNeilBrown 		int skip = 0;
7535d240a89SNeilBrown 		rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
7545d240a89SNeilBrown 			for (list = container_of(p, struct rhlist_head, rhead);
7555d240a89SNeilBrown 			     list;
7565d240a89SNeilBrown 			     list = rcu_dereference(list->next)) {
7575d240a89SNeilBrown 				skip++;
7585d240a89SNeilBrown 				if (list == iter->list) {
7595d240a89SNeilBrown 					iter->p = p;
760c643ecf3SRishabh Bhatnagar 					iter->skip = skip;
7615d240a89SNeilBrown 					goto found;
7625d240a89SNeilBrown 				}
7635d240a89SNeilBrown 			}
7645d240a89SNeilBrown 		}
7655d240a89SNeilBrown 		iter->p = NULL;
7665d240a89SNeilBrown 	}
7675d240a89SNeilBrown found:
768f2dba9c6SHerbert Xu 	return 0;
769f2dba9c6SHerbert Xu }
77097a6ec4aSTom Herbert EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
771f2dba9c6SHerbert Xu 
772f2dba9c6SHerbert Xu /**
7732db54b47STom Herbert  * __rhashtable_walk_find_next - Find the next element in a table (or the first
7742db54b47STom Herbert  * one in case of a new walk).
7752db54b47STom Herbert  *
776f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
777f2dba9c6SHerbert Xu  *
7782db54b47STom Herbert  * Returns the found object or NULL when the end of the table is reached.
779f2dba9c6SHerbert Xu  *
7802db54b47STom Herbert  * Returns -EAGAIN if resize event occurred.
781f2dba9c6SHerbert Xu  */
7822db54b47STom Herbert static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
783f2dba9c6SHerbert Xu {
784246779ddSHerbert Xu 	struct bucket_table *tbl = iter->walker.tbl;
785ca26893fSHerbert Xu 	struct rhlist_head *list = iter->list;
786f2dba9c6SHerbert Xu 	struct rhashtable *ht = iter->ht;
787f2dba9c6SHerbert Xu 	struct rhash_head *p = iter->p;
788ca26893fSHerbert Xu 	bool rhlist = ht->rhlist;
789f2dba9c6SHerbert Xu 
7902db54b47STom Herbert 	if (!tbl)
7912db54b47STom Herbert 		return NULL;
792f2dba9c6SHerbert Xu 
793f2dba9c6SHerbert Xu 	for (; iter->slot < tbl->size; iter->slot++) {
794f2dba9c6SHerbert Xu 		int skip = iter->skip;
795f2dba9c6SHerbert Xu 
796f2dba9c6SHerbert Xu 		rht_for_each_rcu(p, tbl, iter->slot) {
797ca26893fSHerbert Xu 			if (rhlist) {
798ca26893fSHerbert Xu 				list = container_of(p, struct rhlist_head,
799ca26893fSHerbert Xu 						    rhead);
800ca26893fSHerbert Xu 				do {
801ca26893fSHerbert Xu 					if (!skip)
802ca26893fSHerbert Xu 						goto next;
803ca26893fSHerbert Xu 					skip--;
804ca26893fSHerbert Xu 					list = rcu_dereference(list->next);
805ca26893fSHerbert Xu 				} while (list);
806ca26893fSHerbert Xu 
807ca26893fSHerbert Xu 				continue;
808ca26893fSHerbert Xu 			}
809f2dba9c6SHerbert Xu 			if (!skip)
810f2dba9c6SHerbert Xu 				break;
811f2dba9c6SHerbert Xu 			skip--;
812f2dba9c6SHerbert Xu 		}
813f2dba9c6SHerbert Xu 
814f2dba9c6SHerbert Xu next:
815f2dba9c6SHerbert Xu 		if (!rht_is_a_nulls(p)) {
816f2dba9c6SHerbert Xu 			iter->skip++;
817f2dba9c6SHerbert Xu 			iter->p = p;
818ca26893fSHerbert Xu 			iter->list = list;
819ca26893fSHerbert Xu 			return rht_obj(ht, rhlist ? &list->rhead : p);
820f2dba9c6SHerbert Xu 		}
821f2dba9c6SHerbert Xu 
822f2dba9c6SHerbert Xu 		iter->skip = 0;
823f2dba9c6SHerbert Xu 	}
824f2dba9c6SHerbert Xu 
825142b942aSPhil Sutter 	iter->p = NULL;
826142b942aSPhil Sutter 
827d88252f9SHerbert Xu 	/* Ensure we see any new tables. */
828d88252f9SHerbert Xu 	smp_rmb();
829d88252f9SHerbert Xu 
830246779ddSHerbert Xu 	iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
831246779ddSHerbert Xu 	if (iter->walker.tbl) {
832eddee5baSHerbert Xu 		iter->slot = 0;
833eddee5baSHerbert Xu 		iter->skip = 0;
834eddee5baSHerbert Xu 		return ERR_PTR(-EAGAIN);
8352db54b47STom Herbert 	} else {
8362db54b47STom Herbert 		iter->end_of_table = true;
837eddee5baSHerbert Xu 	}
838eddee5baSHerbert Xu 
839c936a79fSThomas Graf 	return NULL;
840f2dba9c6SHerbert Xu }
8412db54b47STom Herbert 
8422db54b47STom Herbert /**
8432db54b47STom Herbert  * rhashtable_walk_next - Return the next object and advance the iterator
8442db54b47STom Herbert  * @iter:	Hash table iterator
8452db54b47STom Herbert  *
8462db54b47STom Herbert  * Note that you must call rhashtable_walk_stop when you are finished
8472db54b47STom Herbert  * with the walk.
8482db54b47STom Herbert  *
8492db54b47STom Herbert  * Returns the next object or NULL when the end of the table is reached.
8502db54b47STom Herbert  *
8512db54b47STom Herbert  * Returns -EAGAIN if resize event occurred.  Note that the iterator
8522db54b47STom Herbert  * will rewind back to the beginning and you may continue to use it.
8532db54b47STom Herbert  */
8542db54b47STom Herbert void *rhashtable_walk_next(struct rhashtable_iter *iter)
8552db54b47STom Herbert {
8562db54b47STom Herbert 	struct rhlist_head *list = iter->list;
8572db54b47STom Herbert 	struct rhashtable *ht = iter->ht;
8582db54b47STom Herbert 	struct rhash_head *p = iter->p;
8592db54b47STom Herbert 	bool rhlist = ht->rhlist;
8602db54b47STom Herbert 
8612db54b47STom Herbert 	if (p) {
8622db54b47STom Herbert 		if (!rhlist || !(list = rcu_dereference(list->next))) {
8632db54b47STom Herbert 			p = rcu_dereference(p->next);
8642db54b47STom Herbert 			list = container_of(p, struct rhlist_head, rhead);
8652db54b47STom Herbert 		}
8662db54b47STom Herbert 		if (!rht_is_a_nulls(p)) {
8672db54b47STom Herbert 			iter->skip++;
8682db54b47STom Herbert 			iter->p = p;
8692db54b47STom Herbert 			iter->list = list;
8702db54b47STom Herbert 			return rht_obj(ht, rhlist ? &list->rhead : p);
8712db54b47STom Herbert 		}
8722db54b47STom Herbert 
8732db54b47STom Herbert 		/* At the end of this slot, switch to next one and then find
8742db54b47STom Herbert 		 * next entry from that point.
8752db54b47STom Herbert 		 */
8762db54b47STom Herbert 		iter->skip = 0;
8772db54b47STom Herbert 		iter->slot++;
8782db54b47STom Herbert 	}
8792db54b47STom Herbert 
8802db54b47STom Herbert 	return __rhashtable_walk_find_next(iter);
8812db54b47STom Herbert }
882f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_next);
883f2dba9c6SHerbert Xu 
884f2dba9c6SHerbert Xu /**
8852db54b47STom Herbert  * rhashtable_walk_peek - Return the next object but don't advance the iterator
8862db54b47STom Herbert  * @iter:	Hash table iterator
8872db54b47STom Herbert  *
8882db54b47STom Herbert  * Returns the next object or NULL when the end of the table is reached.
8892db54b47STom Herbert  *
8902db54b47STom Herbert  * Returns -EAGAIN if resize event occurred.  Note that the iterator
8912db54b47STom Herbert  * will rewind back to the beginning and you may continue to use it.
8922db54b47STom Herbert  */
8932db54b47STom Herbert void *rhashtable_walk_peek(struct rhashtable_iter *iter)
8942db54b47STom Herbert {
8952db54b47STom Herbert 	struct rhlist_head *list = iter->list;
8962db54b47STom Herbert 	struct rhashtable *ht = iter->ht;
8972db54b47STom Herbert 	struct rhash_head *p = iter->p;
8982db54b47STom Herbert 
8992db54b47STom Herbert 	if (p)
9002db54b47STom Herbert 		return rht_obj(ht, ht->rhlist ? &list->rhead : p);
9012db54b47STom Herbert 
9022db54b47STom Herbert 	/* No object found in current iter, find next one in the table. */
9032db54b47STom Herbert 
9042db54b47STom Herbert 	if (iter->skip) {
9052db54b47STom Herbert 		/* A nonzero skip value points to the next entry in the table
9062db54b47STom Herbert 		 * beyond that last one that was found. Decrement skip so
9072db54b47STom Herbert 		 * we find the current value. __rhashtable_walk_find_next
9082db54b47STom Herbert 		 * will restore the original value of skip assuming that
9092db54b47STom Herbert 		 * the table hasn't changed.
9102db54b47STom Herbert 		 */
9112db54b47STom Herbert 		iter->skip--;
9122db54b47STom Herbert 	}
9132db54b47STom Herbert 
9142db54b47STom Herbert 	return __rhashtable_walk_find_next(iter);
9152db54b47STom Herbert }
9162db54b47STom Herbert EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
9172db54b47STom Herbert 
9182db54b47STom Herbert /**
919f2dba9c6SHerbert Xu  * rhashtable_walk_stop - Finish a hash table walk
920f2dba9c6SHerbert Xu  * @iter:	Hash table iterator
921f2dba9c6SHerbert Xu  *
9220647169cSAndreas Gruenbacher  * Finish a hash table walk.  Does not reset the iterator to the start of the
9230647169cSAndreas Gruenbacher  * hash table.
924f2dba9c6SHerbert Xu  */
925f2dba9c6SHerbert Xu void rhashtable_walk_stop(struct rhashtable_iter *iter)
926db4374f4SThomas Graf 	__releases(RCU)
927f2dba9c6SHerbert Xu {
928eddee5baSHerbert Xu 	struct rhashtable *ht;
929246779ddSHerbert Xu 	struct bucket_table *tbl = iter->walker.tbl;
930eddee5baSHerbert Xu 
931eddee5baSHerbert Xu 	if (!tbl)
932963ecbd4SHerbert Xu 		goto out;
933eddee5baSHerbert Xu 
934eddee5baSHerbert Xu 	ht = iter->ht;
935eddee5baSHerbert Xu 
936ba7c95eaSHerbert Xu 	spin_lock(&ht->lock);
9374feb7c7aSNeilBrown 	if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
9384feb7c7aSNeilBrown 		/* This bucket table is being freed, don't re-link it. */
939246779ddSHerbert Xu 		iter->walker.tbl = NULL;
9404feb7c7aSNeilBrown 	else
9414feb7c7aSNeilBrown 		list_add(&iter->walker.list, &tbl->walkers);
942ba7c95eaSHerbert Xu 	spin_unlock(&ht->lock);
943eddee5baSHerbert Xu 
944963ecbd4SHerbert Xu out:
945963ecbd4SHerbert Xu 	rcu_read_unlock();
946f2dba9c6SHerbert Xu }
947f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
948f2dba9c6SHerbert Xu 
949488fb86eSHerbert Xu static size_t rounded_hashtable_size(const struct rhashtable_params *params)
9507e1e7763SThomas Graf {
951107d01f5SDavidlohr Bueso 	size_t retsize;
952107d01f5SDavidlohr Bueso 
953107d01f5SDavidlohr Bueso 	if (params->nelem_hint)
954107d01f5SDavidlohr Bueso 		retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
955e2e21c1cSHerbert Xu 			      (unsigned long)params->min_size);
956107d01f5SDavidlohr Bueso 	else
957107d01f5SDavidlohr Bueso 		retsize = max(HASH_DEFAULT_SIZE,
958107d01f5SDavidlohr Bueso 			      (unsigned long)params->min_size);
959107d01f5SDavidlohr Bueso 
960107d01f5SDavidlohr Bueso 	return retsize;
9617e1e7763SThomas Graf }
9627e1e7763SThomas Graf 
96331ccde2dSHerbert Xu static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
96431ccde2dSHerbert Xu {
96531ccde2dSHerbert Xu 	return jhash2(key, length, seed);
96631ccde2dSHerbert Xu }
96731ccde2dSHerbert Xu 
9687e1e7763SThomas Graf /**
9697e1e7763SThomas Graf  * rhashtable_init - initialize a new hash table
9707e1e7763SThomas Graf  * @ht:		hash table to be initialized
9717e1e7763SThomas Graf  * @params:	configuration parameters
9727e1e7763SThomas Graf  *
9737e1e7763SThomas Graf  * Initializes a new hash table based on the provided configuration
9747e1e7763SThomas Graf  * parameters. A table can be configured either with a variable or
9757e1e7763SThomas Graf  * fixed length key:
9767e1e7763SThomas Graf  *
9777e1e7763SThomas Graf  * Configuration Example 1: Fixed length keys
9787e1e7763SThomas Graf  * struct test_obj {
9797e1e7763SThomas Graf  *	int			key;
9807e1e7763SThomas Graf  *	void *			my_member;
9817e1e7763SThomas Graf  *	struct rhash_head	node;
9827e1e7763SThomas Graf  * };
9837e1e7763SThomas Graf  *
9847e1e7763SThomas Graf  * struct rhashtable_params params = {
9857e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
9867e1e7763SThomas Graf  *	.key_offset = offsetof(struct test_obj, key),
9877e1e7763SThomas Graf  *	.key_len = sizeof(int),
98887545899SDaniel Borkmann  *	.hashfn = jhash,
9897e1e7763SThomas Graf  * };
9907e1e7763SThomas Graf  *
9917e1e7763SThomas Graf  * Configuration Example 2: Variable length keys
9927e1e7763SThomas Graf  * struct test_obj {
9937e1e7763SThomas Graf  *	[...]
9947e1e7763SThomas Graf  *	struct rhash_head	node;
9957e1e7763SThomas Graf  * };
9967e1e7763SThomas Graf  *
99749f7b33eSPatrick McHardy  * u32 my_hash_fn(const void *data, u32 len, u32 seed)
9987e1e7763SThomas Graf  * {
9997e1e7763SThomas Graf  *	struct test_obj *obj = data;
10007e1e7763SThomas Graf  *
10017e1e7763SThomas Graf  *	return [... hash ...];
10027e1e7763SThomas Graf  * }
10037e1e7763SThomas Graf  *
10047e1e7763SThomas Graf  * struct rhashtable_params params = {
10057e1e7763SThomas Graf  *	.head_offset = offsetof(struct test_obj, node),
100687545899SDaniel Borkmann  *	.hashfn = jhash,
10077e1e7763SThomas Graf  *	.obj_hashfn = my_hash_fn,
10087e1e7763SThomas Graf  * };
10097e1e7763SThomas Graf  */
1010488fb86eSHerbert Xu int rhashtable_init(struct rhashtable *ht,
1011488fb86eSHerbert Xu 		    const struct rhashtable_params *params)
10127e1e7763SThomas Graf {
10137e1e7763SThomas Graf 	struct bucket_table *tbl;
10147e1e7763SThomas Graf 	size_t size;
10157e1e7763SThomas Graf 
101631ccde2dSHerbert Xu 	if ((!params->key_len && !params->obj_hashfn) ||
101702fd97c3SHerbert Xu 	    (params->obj_hashfn && !params->obj_cmpfn))
10187e1e7763SThomas Graf 		return -EINVAL;
10197e1e7763SThomas Graf 
102097defe1eSThomas Graf 	memset(ht, 0, sizeof(*ht));
102197defe1eSThomas Graf 	mutex_init(&ht->mutex);
1022ba7c95eaSHerbert Xu 	spin_lock_init(&ht->lock);
102397defe1eSThomas Graf 	memcpy(&ht->p, params, sizeof(*params));
102497defe1eSThomas Graf 
1025a998f712SThomas Graf 	if (params->min_size)
1026a998f712SThomas Graf 		ht->p.min_size = roundup_pow_of_two(params->min_size);
1027a998f712SThomas Graf 
10286d684e54SHerbert Xu 	/* Cap total entries at 2^31 to avoid nelems overflow. */
10296d684e54SHerbert Xu 	ht->max_elems = 1u << 31;
10302d2ab658SHerbert Xu 
10312d2ab658SHerbert Xu 	if (params->max_size) {
10322d2ab658SHerbert Xu 		ht->p.max_size = rounddown_pow_of_two(params->max_size);
10336d684e54SHerbert Xu 		if (ht->p.max_size < ht->max_elems / 2)
10346d684e54SHerbert Xu 			ht->max_elems = ht->p.max_size * 2;
10352d2ab658SHerbert Xu 	}
10366d684e54SHerbert Xu 
103748e75b43SFlorian Westphal 	ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1038a998f712SThomas Graf 
10393a324606SHerbert Xu 	size = rounded_hashtable_size(&ht->p);
10403a324606SHerbert Xu 
104131ccde2dSHerbert Xu 	ht->key_len = ht->p.key_len;
104231ccde2dSHerbert Xu 	if (!params->hashfn) {
104331ccde2dSHerbert Xu 		ht->p.hashfn = jhash;
104431ccde2dSHerbert Xu 
104531ccde2dSHerbert Xu 		if (!(ht->key_len & (sizeof(u32) - 1))) {
104631ccde2dSHerbert Xu 			ht->key_len /= sizeof(u32);
104731ccde2dSHerbert Xu 			ht->p.hashfn = rhashtable_jhash2;
104831ccde2dSHerbert Xu 		}
104931ccde2dSHerbert Xu 	}
105031ccde2dSHerbert Xu 
10512d22ecf6SDavidlohr Bueso 	/*
10522d22ecf6SDavidlohr Bueso 	 * This is api initialization and thus we need to guarantee the
10532d22ecf6SDavidlohr Bueso 	 * initial rhashtable allocation. Upon failure, retry with the
10542d22ecf6SDavidlohr Bueso 	 * smallest possible size with __GFP_NOFAIL semantics.
10552d22ecf6SDavidlohr Bueso 	 */
1056b9ecfdaaSHerbert Xu 	tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
10572d22ecf6SDavidlohr Bueso 	if (unlikely(tbl == NULL)) {
10582d22ecf6SDavidlohr Bueso 		size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
10592d22ecf6SDavidlohr Bueso 		tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
10602d22ecf6SDavidlohr Bueso 	}
10617e1e7763SThomas Graf 
1062545a148eSYing Xue 	atomic_set(&ht->nelems, 0);
1063a5b6846fSDaniel Borkmann 
10647e1e7763SThomas Graf 	RCU_INIT_POINTER(ht->tbl, tbl);
10657e1e7763SThomas Graf 
106657699a40SYing Xue 	INIT_WORK(&ht->run_work, rht_deferred_worker);
106797defe1eSThomas Graf 
10687e1e7763SThomas Graf 	return 0;
10697e1e7763SThomas Graf }
10707e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init);
10717e1e7763SThomas Graf 
10727e1e7763SThomas Graf /**
1073ca26893fSHerbert Xu  * rhltable_init - initialize a new hash list table
1074ca26893fSHerbert Xu  * @hlt:	hash list table to be initialized
1075ca26893fSHerbert Xu  * @params:	configuration parameters
1076ca26893fSHerbert Xu  *
1077ca26893fSHerbert Xu  * Initializes a new hash list table.
1078ca26893fSHerbert Xu  *
1079ca26893fSHerbert Xu  * See documentation for rhashtable_init.
1080ca26893fSHerbert Xu  */
1081ca26893fSHerbert Xu int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1082ca26893fSHerbert Xu {
1083ca26893fSHerbert Xu 	int err;
1084ca26893fSHerbert Xu 
1085ca26893fSHerbert Xu 	err = rhashtable_init(&hlt->ht, params);
1086ca26893fSHerbert Xu 	hlt->ht.rhlist = true;
1087ca26893fSHerbert Xu 	return err;
1088ca26893fSHerbert Xu }
1089ca26893fSHerbert Xu EXPORT_SYMBOL_GPL(rhltable_init);
1090ca26893fSHerbert Xu 
1091ca26893fSHerbert Xu static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1092ca26893fSHerbert Xu 				void (*free_fn)(void *ptr, void *arg),
1093ca26893fSHerbert Xu 				void *arg)
1094ca26893fSHerbert Xu {
1095ca26893fSHerbert Xu 	struct rhlist_head *list;
1096ca26893fSHerbert Xu 
1097ca26893fSHerbert Xu 	if (!ht->rhlist) {
1098ca26893fSHerbert Xu 		free_fn(rht_obj(ht, obj), arg);
1099ca26893fSHerbert Xu 		return;
1100ca26893fSHerbert Xu 	}
1101ca26893fSHerbert Xu 
1102ca26893fSHerbert Xu 	list = container_of(obj, struct rhlist_head, rhead);
1103ca26893fSHerbert Xu 	do {
1104ca26893fSHerbert Xu 		obj = &list->rhead;
1105ca26893fSHerbert Xu 		list = rht_dereference(list->next, ht);
1106ca26893fSHerbert Xu 		free_fn(rht_obj(ht, obj), arg);
1107ca26893fSHerbert Xu 	} while (list);
1108ca26893fSHerbert Xu }
1109ca26893fSHerbert Xu 
1110ca26893fSHerbert Xu /**
11116b6f302cSThomas Graf  * rhashtable_free_and_destroy - free elements and destroy hash table
11127e1e7763SThomas Graf  * @ht:		the hash table to destroy
11136b6f302cSThomas Graf  * @free_fn:	callback to release resources of element
11146b6f302cSThomas Graf  * @arg:	pointer passed to free_fn
11157e1e7763SThomas Graf  *
11166b6f302cSThomas Graf  * Stops an eventual async resize. If defined, invokes free_fn for each
11176b6f302cSThomas Graf  * element to releasal resources. Please note that RCU protected
11186b6f302cSThomas Graf  * readers may still be accessing the elements. Releasing of resources
11196b6f302cSThomas Graf  * must occur in a compatible manner. Then frees the bucket array.
11206b6f302cSThomas Graf  *
11216b6f302cSThomas Graf  * This function will eventually sleep to wait for an async resize
11226b6f302cSThomas Graf  * to complete. The caller is responsible that no further write operations
11236b6f302cSThomas Graf  * occurs in parallel.
11247e1e7763SThomas Graf  */
11256b6f302cSThomas Graf void rhashtable_free_and_destroy(struct rhashtable *ht,
11266b6f302cSThomas Graf 				 void (*free_fn)(void *ptr, void *arg),
11276b6f302cSThomas Graf 				 void *arg)
11287e1e7763SThomas Graf {
11290026129cSTaehee Yoo 	struct bucket_table *tbl, *next_tbl;
11306b6f302cSThomas Graf 	unsigned int i;
113197defe1eSThomas Graf 
113257699a40SYing Xue 	cancel_work_sync(&ht->run_work);
113357699a40SYing Xue 
113497defe1eSThomas Graf 	mutex_lock(&ht->mutex);
11356b6f302cSThomas Graf 	tbl = rht_dereference(ht->tbl, ht);
11360026129cSTaehee Yoo restart:
11376b6f302cSThomas Graf 	if (free_fn) {
11386b6f302cSThomas Graf 		for (i = 0; i < tbl->size; i++) {
11396b6f302cSThomas Graf 			struct rhash_head *pos, *next;
11406b6f302cSThomas Graf 
1141ae6da1f5SEric Dumazet 			cond_resched();
1142*adc6a3abSNeilBrown 			for (pos = rht_ptr_exclusive(rht_bucket(tbl, i)),
11436b6f302cSThomas Graf 			     next = !rht_is_a_nulls(pos) ?
11446b6f302cSThomas Graf 					rht_dereference(pos->next, ht) : NULL;
11456b6f302cSThomas Graf 			     !rht_is_a_nulls(pos);
11466b6f302cSThomas Graf 			     pos = next,
11476b6f302cSThomas Graf 			     next = !rht_is_a_nulls(pos) ?
11486b6f302cSThomas Graf 					rht_dereference(pos->next, ht) : NULL)
1149ca26893fSHerbert Xu 				rhashtable_free_one(ht, pos, free_fn, arg);
11506b6f302cSThomas Graf 		}
11516b6f302cSThomas Graf 	}
11526b6f302cSThomas Graf 
11530026129cSTaehee Yoo 	next_tbl = rht_dereference(tbl->future_tbl, ht);
11546b6f302cSThomas Graf 	bucket_table_free(tbl);
11550026129cSTaehee Yoo 	if (next_tbl) {
11560026129cSTaehee Yoo 		tbl = next_tbl;
11570026129cSTaehee Yoo 		goto restart;
11580026129cSTaehee Yoo 	}
115997defe1eSThomas Graf 	mutex_unlock(&ht->mutex);
11607e1e7763SThomas Graf }
11616b6f302cSThomas Graf EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
11626b6f302cSThomas Graf 
11636b6f302cSThomas Graf void rhashtable_destroy(struct rhashtable *ht)
11646b6f302cSThomas Graf {
11656b6f302cSThomas Graf 	return rhashtable_free_and_destroy(ht, NULL, NULL);
11666b6f302cSThomas Graf }
11677e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy);
1168da20420fSHerbert Xu 
11698f0db018SNeilBrown struct rhash_lock_head __rcu **__rht_bucket_nested(const struct bucket_table *tbl,
1170da20420fSHerbert Xu 						   unsigned int hash)
1171da20420fSHerbert Xu {
1172da20420fSHerbert Xu 	const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1173da20420fSHerbert Xu 	unsigned int index = hash & ((1 << tbl->nest) - 1);
1174da20420fSHerbert Xu 	unsigned int size = tbl->size >> tbl->nest;
1175da20420fSHerbert Xu 	unsigned int subhash = hash;
1176da20420fSHerbert Xu 	union nested_table *ntbl;
1177da20420fSHerbert Xu 
1178da20420fSHerbert Xu 	ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1179c4d2603dSHerbert Xu 	ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
1180da20420fSHerbert Xu 	subhash >>= tbl->nest;
1181da20420fSHerbert Xu 
1182da20420fSHerbert Xu 	while (ntbl && size > (1 << shift)) {
1183da20420fSHerbert Xu 		index = subhash & ((1 << shift) - 1);
1184c4d2603dSHerbert Xu 		ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1185c4d2603dSHerbert Xu 						  tbl, hash);
1186da20420fSHerbert Xu 		size >>= shift;
1187da20420fSHerbert Xu 		subhash >>= shift;
1188da20420fSHerbert Xu 	}
1189da20420fSHerbert Xu 
1190ff302db9SNeilBrown 	if (!ntbl)
1191ff302db9SNeilBrown 		return NULL;
1192da20420fSHerbert Xu 
1193da20420fSHerbert Xu 	return &ntbl[subhash].bucket;
1194da20420fSHerbert Xu 
1195da20420fSHerbert Xu }
1196ff302db9SNeilBrown EXPORT_SYMBOL_GPL(__rht_bucket_nested);
1197ff302db9SNeilBrown 
11988f0db018SNeilBrown struct rhash_lock_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1199ff302db9SNeilBrown 						 unsigned int hash)
1200ff302db9SNeilBrown {
12018f0db018SNeilBrown 	static struct rhash_lock_head __rcu *rhnull;
1202ff302db9SNeilBrown 
1203ff302db9SNeilBrown 	if (!rhnull)
1204ff302db9SNeilBrown 		INIT_RHT_NULLS_HEAD(rhnull);
1205ff302db9SNeilBrown 	return __rht_bucket_nested(tbl, hash) ?: &rhnull;
1206ff302db9SNeilBrown }
1207da20420fSHerbert Xu EXPORT_SYMBOL_GPL(rht_bucket_nested);
1208da20420fSHerbert Xu 
12098f0db018SNeilBrown struct rhash_lock_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1210da20420fSHerbert Xu 							struct bucket_table *tbl,
1211da20420fSHerbert Xu 							unsigned int hash)
1212da20420fSHerbert Xu {
1213da20420fSHerbert Xu 	const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1214da20420fSHerbert Xu 	unsigned int index = hash & ((1 << tbl->nest) - 1);
1215da20420fSHerbert Xu 	unsigned int size = tbl->size >> tbl->nest;
1216da20420fSHerbert Xu 	union nested_table *ntbl;
1217da20420fSHerbert Xu 
1218da20420fSHerbert Xu 	ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1219da20420fSHerbert Xu 	hash >>= tbl->nest;
1220da20420fSHerbert Xu 	ntbl = nested_table_alloc(ht, &ntbl[index].table,
12215af68ef7SNeilBrown 				  size <= (1 << shift));
1222da20420fSHerbert Xu 
1223da20420fSHerbert Xu 	while (ntbl && size > (1 << shift)) {
1224da20420fSHerbert Xu 		index = hash & ((1 << shift) - 1);
1225da20420fSHerbert Xu 		size >>= shift;
1226da20420fSHerbert Xu 		hash >>= shift;
1227da20420fSHerbert Xu 		ntbl = nested_table_alloc(ht, &ntbl[index].table,
12285af68ef7SNeilBrown 					  size <= (1 << shift));
1229da20420fSHerbert Xu 	}
1230da20420fSHerbert Xu 
1231da20420fSHerbert Xu 	if (!ntbl)
1232da20420fSHerbert Xu 		return NULL;
1233da20420fSHerbert Xu 
1234da20420fSHerbert Xu 	return &ntbl[hash].bucket;
1235da20420fSHerbert Xu 
1236da20420fSHerbert Xu }
1237da20420fSHerbert Xu EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);
1238