blob: e12bbfb240b812bc4945c6f0d4910f35667b0e8e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Thomas Graf7e1e7762014-08-02 11:47:44 +02002/*
3 * Resizable, Scalable, Concurrent Hash Table
4 *
Herbert Xu02fd97c2015-03-20 21:57:00 +11005 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafa5ec68e2015-02-05 02:03:32 +01006 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02007 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02009 * Code partially derived from nft_hash
Herbert Xu02fd97c2015-03-20 21:57:00 +110010 * Rewritten with rehash code from br_multicast plus single list
11 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020012 */
13
Herbert Xu07ee0722015-05-15 11:30:47 +080014#include <linux/atomic.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020015#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/log2.h>
Eric Dumazet5beb5c92015-02-26 07:20:34 -080018#include <linux/sched.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010019#include <linux/rculist.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020020#include <linux/slab.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010023#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020024#include <linux/random.h>
25#include <linux/rhashtable.h>
Stephen Rothwell61d7b092015-02-09 14:04:03 +110026#include <linux/err.h>
Hauke Mehrtens6d795412015-06-06 22:07:23 +020027#include <linux/export.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020028
29#define HASH_DEFAULT_SIZE 64UL
Herbert Xuc2e213c2015-03-18 20:01:16 +110030#define HASH_MIN_SIZE 4U
Thomas Graf97defe12015-01-02 23:00:20 +010031
Herbert Xuda204202017-02-11 19:26:47 +080032union nested_table {
33 union nested_table __rcu *table;
Herbert Xuce9b3622020-07-24 20:14:34 +100034 struct rhash_lock_head __rcu *bucket;
Herbert Xuda204202017-02-11 19:26:47 +080035};
36
Herbert Xu988dfbd2015-03-10 09:27:55 +110037static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010038 const struct bucket_table *tbl,
39 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020040{
Herbert Xu02fd97c2015-03-20 21:57:00 +110041 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020042}
43
Thomas Grafa03eaec2015-02-05 02:03:34 +010044#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010045#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010046
47int lockdep_rht_mutex_is_held(struct rhashtable *ht)
48{
49 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
50}
51EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
52
53int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
54{
NeilBrown8f0db012019-04-02 10:07:45 +110055 if (!debug_locks)
56 return 1;
57 if (unlikely(tbl->nest))
58 return 1;
NeilBrownca0b7092019-04-12 11:52:08 +100059 return bit_spin_is_locked(0, (unsigned long *)&tbl->buckets[hash]);
Thomas Grafa03eaec2015-02-05 02:03:34 +010060}
61EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
62#else
63#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +010064#endif
65
Herbert Xu4a3084a2020-06-03 18:12:43 +100066static inline union nested_table *nested_table_top(
67 const struct bucket_table *tbl)
68{
69 /* The top-level bucket entry does not need RCU protection
70 * because it's set at the same time as tbl->nest.
71 */
72 return (void *)rcu_dereference_protected(tbl->buckets[0], 1);
73}
74
Herbert Xuda204202017-02-11 19:26:47 +080075static void nested_table_free(union nested_table *ntbl, unsigned int size)
76{
77 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
78 const unsigned int len = 1 << shift;
79 unsigned int i;
80
Herbert Xu4a3084a2020-06-03 18:12:43 +100081 ntbl = rcu_dereference_protected(ntbl->table, 1);
Herbert Xuda204202017-02-11 19:26:47 +080082 if (!ntbl)
83 return;
84
85 if (size > len) {
86 size >>= shift;
87 for (i = 0; i < len; i++)
88 nested_table_free(ntbl + i, size);
89 }
90
91 kfree(ntbl);
92}
93
94static void nested_bucket_table_free(const struct bucket_table *tbl)
95{
96 unsigned int size = tbl->size >> tbl->nest;
97 unsigned int len = 1 << tbl->nest;
98 union nested_table *ntbl;
99 unsigned int i;
100
Herbert Xu4a3084a2020-06-03 18:12:43 +1000101 ntbl = nested_table_top(tbl);
Herbert Xuda204202017-02-11 19:26:47 +0800102
103 for (i = 0; i < len; i++)
104 nested_table_free(ntbl + i, size);
105
106 kfree(ntbl);
107}
108
Thomas Graf97defe12015-01-02 23:00:20 +0100109static void bucket_table_free(const struct bucket_table *tbl)
110{
Herbert Xuda204202017-02-11 19:26:47 +0800111 if (tbl->nest)
112 nested_bucket_table_free(tbl);
113
Thomas Graf97defe12015-01-02 23:00:20 +0100114 kvfree(tbl);
115}
116
Herbert Xu9d901bc2015-03-14 13:57:23 +1100117static void bucket_table_free_rcu(struct rcu_head *head)
118{
119 bucket_table_free(container_of(head, struct bucket_table, rcu));
120}
121
Herbert Xuda204202017-02-11 19:26:47 +0800122static union nested_table *nested_table_alloc(struct rhashtable *ht,
123 union nested_table __rcu **prev,
NeilBrown5af68ef2018-06-18 12:52:50 +1000124 bool leaf)
Herbert Xuda204202017-02-11 19:26:47 +0800125{
126 union nested_table *ntbl;
127 int i;
128
129 ntbl = rcu_dereference(*prev);
130 if (ntbl)
131 return ntbl;
132
133 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
134
NeilBrown5af68ef2018-06-18 12:52:50 +1000135 if (ntbl && leaf) {
136 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000137 INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
Herbert Xuda204202017-02-11 19:26:47 +0800138 }
139
Herbert Xue9458a42019-05-16 15:19:48 +0800140 if (cmpxchg((union nested_table **)prev, NULL, ntbl) == NULL)
NeilBrown7a41c292019-04-02 10:07:45 +1100141 return ntbl;
142 /* Raced with another thread. */
143 kfree(ntbl);
144 return rcu_dereference(*prev);
Herbert Xuda204202017-02-11 19:26:47 +0800145}
146
147static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
148 size_t nbuckets,
149 gfp_t gfp)
150{
151 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
152 struct bucket_table *tbl;
153 size_t size;
154
155 if (nbuckets < (1 << (shift + 1)))
156 return NULL;
157
158 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
159
160 tbl = kzalloc(size, gfp);
161 if (!tbl)
162 return NULL;
163
164 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
NeilBrown5af68ef2018-06-18 12:52:50 +1000165 false)) {
Herbert Xuda204202017-02-11 19:26:47 +0800166 kfree(tbl);
167 return NULL;
168 }
169
170 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
171
172 return tbl;
173}
174
Thomas Graf97defe12015-01-02 23:00:20 +0100175static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xub9ecfda2015-03-24 00:50:27 +1100176 size_t nbuckets,
177 gfp_t gfp)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200178{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100179 struct bucket_table *tbl = NULL;
NeilBrown8f0db012019-04-02 10:07:45 +1100180 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100181 int i;
NeilBrown149212f2019-04-02 10:07:45 +1100182 static struct lock_class_key __key;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200183
Gustavo A. R. Silvac252aa32019-04-11 18:43:06 -0500184 tbl = kvzalloc(struct_size(tbl, buckets, nbuckets), gfp);
Herbert Xuda204202017-02-11 19:26:47 +0800185
186 size = nbuckets;
187
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700188 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
Herbert Xuda204202017-02-11 19:26:47 +0800189 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
190 nbuckets = 0;
191 }
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700192
Thomas Graf7e1e7762014-08-02 11:47:44 +0200193 if (tbl == NULL)
194 return NULL;
195
NeilBrown149212f2019-04-02 10:07:45 +1100196 lockdep_init_map(&tbl->dep_map, "rhashtable_bucket", &__key, 0);
197
Herbert Xuda204202017-02-11 19:26:47 +0800198 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200199
NeilBrown4feb7c72019-03-21 14:42:40 +1100200 rcu_head_init(&tbl->rcu);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100201 INIT_LIST_HEAD(&tbl->walkers);
202
Jason A. Donenfeldd48ad082017-06-07 22:47:13 -0400203 tbl->hash_rnd = get_random_u32();
Herbert Xu5269b532015-03-14 13:57:22 +1100204
Thomas Graff89bd6f2015-01-02 23:00:21 +0100205 for (i = 0; i < nbuckets; i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000206 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100207
Thomas Graf97defe12015-01-02 23:00:20 +0100208 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200209}
210
Herbert Xub8244782015-03-24 00:50:26 +1100211static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
212 struct bucket_table *tbl)
213{
214 struct bucket_table *new_tbl;
215
216 do {
217 new_tbl = tbl;
218 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
219 } while (tbl);
220
221 return new_tbl;
222}
223
NeilBrown8f0db012019-04-02 10:07:45 +1100224static int rhashtable_rehash_one(struct rhashtable *ht,
Herbert Xuce9b3622020-07-24 20:14:34 +1000225 struct rhash_lock_head __rcu **bkt,
NeilBrown8f0db012019-04-02 10:07:45 +1100226 unsigned int old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100227{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100228 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
NeilBrownc0690012018-06-18 12:52:50 +1000229 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
Herbert Xuda204202017-02-11 19:26:47 +0800230 int err = -EAGAIN;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100231 struct rhash_head *head, *next, *entry;
NeilBrowne4edbe32019-04-12 11:52:07 +1000232 struct rhash_head __rcu **pprev = NULL;
Thomas Graf299e5c32015-03-24 14:18:17 +0100233 unsigned int new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100234
Herbert Xuda204202017-02-11 19:26:47 +0800235 if (new_tbl->nest)
236 goto out;
237
238 err = -ENOENT;
239
NeilBrownadc6a3a2019-04-12 11:52:08 +1000240 rht_for_each_from(entry, rht_ptr(bkt, old_tbl, old_hash),
241 old_tbl, old_hash) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100242 err = 0;
243 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100244
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100245 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200246 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100247
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100248 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200249 }
250
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100251 if (err)
252 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100253
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100254 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100255
NeilBrown149212f2019-04-02 10:07:45 +1100256 rht_lock_nested(new_tbl, &new_tbl->buckets[new_hash], SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100257
NeilBrownadc6a3a2019-04-12 11:52:08 +1000258 head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100259
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200260 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100261
NeilBrown149212f2019-04-02 10:07:45 +1100262 rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100263
NeilBrown8f0db012019-04-02 10:07:45 +1100264 if (pprev)
265 rcu_assign_pointer(*pprev, next);
266 else
267 /* Need to preserved the bit lock. */
NeilBrownf4712b42019-04-12 11:52:08 +1000268 rht_assign_locked(bkt, next);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100269
270out:
271 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100272}
273
Herbert Xuda204202017-02-11 19:26:47 +0800274static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100275 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100276{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100277 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xuce9b3622020-07-24 20:14:34 +1000278 struct rhash_lock_head __rcu **bkt = rht_bucket_var(old_tbl, old_hash);
Herbert Xuda204202017-02-11 19:26:47 +0800279 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100280
NeilBrown8f0db012019-04-02 10:07:45 +1100281 if (!bkt)
282 return 0;
NeilBrown149212f2019-04-02 10:07:45 +1100283 rht_lock(old_tbl, bkt);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100284
NeilBrown8f0db012019-04-02 10:07:45 +1100285 while (!(err = rhashtable_rehash_one(ht, bkt, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100286 ;
Herbert Xuda204202017-02-11 19:26:47 +0800287
NeilBrown4feb7c72019-03-21 14:42:40 +1100288 if (err == -ENOENT)
Herbert Xuda204202017-02-11 19:26:47 +0800289 err = 0;
NeilBrown149212f2019-04-02 10:07:45 +1100290 rht_unlock(old_tbl, bkt);
Herbert Xuda204202017-02-11 19:26:47 +0800291
292 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100293}
294
Herbert Xub8244782015-03-24 00:50:26 +1100295static int rhashtable_rehash_attach(struct rhashtable *ht,
296 struct bucket_table *old_tbl,
297 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100298{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100299 /* Make insertions go into the new, empty table right away. Deletions
300 * and lookups will be attempted in both tables until we synchronize.
NeilBrown0ad66442018-06-18 12:52:50 +1000301 * As cmpxchg() provides strong barriers, we do not need
302 * rcu_assign_pointer().
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100303 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100304
Herbert Xue9458a42019-05-16 15:19:48 +0800305 if (cmpxchg((struct bucket_table **)&old_tbl->future_tbl, NULL,
306 new_tbl) != NULL)
NeilBrown0ad66442018-06-18 12:52:50 +1000307 return -EEXIST;
Herbert Xub8244782015-03-24 00:50:26 +1100308
309 return 0;
310}
311
312static int rhashtable_rehash_table(struct rhashtable *ht)
313{
314 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
315 struct bucket_table *new_tbl;
316 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100317 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800318 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100319
320 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
321 if (!new_tbl)
322 return 0;
323
Herbert Xuda204202017-02-11 19:26:47 +0800324 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
325 err = rhashtable_rehash_chain(ht, old_hash);
326 if (err)
327 return err;
Eric Dumazetae6da1f2018-03-31 12:58:48 -0700328 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +0800329 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100330
331 /* Publish the new table pointer. */
332 rcu_assign_pointer(ht->tbl, new_tbl);
333
Herbert Xuba7c95e2015-03-24 09:53:17 +1100334 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100335 list_for_each_entry(walker, &old_tbl->walkers, list)
336 walker->tbl = NULL;
337
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100338 /* Wait for readers. All new readers will see the new
339 * table, and thus no references to the old table will
340 * remain.
NeilBrown4feb7c72019-03-21 14:42:40 +1100341 * We do this inside the locked region so that
342 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
343 * to check if it should not re-link the table.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100344 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100345 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
NeilBrown4feb7c72019-03-21 14:42:40 +1100346 spin_unlock(&ht->lock);
Herbert Xub8244782015-03-24 00:50:26 +1100347
348 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200349}
350
Herbert Xuda204202017-02-11 19:26:47 +0800351static int rhashtable_rehash_alloc(struct rhashtable *ht,
352 struct bucket_table *old_tbl,
353 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200354{
Herbert Xuda204202017-02-11 19:26:47 +0800355 struct bucket_table *new_tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100356 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200357
358 ASSERT_RHT_MUTEX(ht);
359
Herbert Xuda204202017-02-11 19:26:47 +0800360 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200361 if (new_tbl == NULL)
362 return -ENOMEM;
363
Herbert Xub8244782015-03-24 00:50:26 +1100364 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
365 if (err)
366 bucket_table_free(new_tbl);
367
368 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200369}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200370
371/**
372 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
373 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200374 *
Herbert Xu18093d12015-03-24 00:50:25 +1100375 * This function shrinks the hash table to fit, i.e., the smallest
376 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200377 *
Thomas Graf97defe12015-01-02 23:00:20 +0100378 * The caller must ensure that no concurrent resizing occurs by holding
379 * ht->mutex.
380 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200381 * The caller must ensure that no concurrent table mutations take place.
382 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100383 *
384 * It is valid to have concurrent insertions and deletions protected by per
385 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200386 */
Herbert Xub8244782015-03-24 00:50:26 +1100387static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200388{
Herbert Xuda204202017-02-11 19:26:47 +0800389 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200390 unsigned int nelems = atomic_read(&ht->nelems);
391 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200392
Vegard Nossum12311952016-08-12 20:10:44 +0200393 if (nelems)
394 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100395 if (size < ht->p.min_size)
396 size = ht->p.min_size;
397
398 if (old_tbl->size <= size)
399 return 0;
400
Herbert Xub8244782015-03-24 00:50:26 +1100401 if (rht_dereference(old_tbl->future_tbl, ht))
402 return -EEXIST;
403
Herbert Xuda204202017-02-11 19:26:47 +0800404 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200405}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200406
Thomas Graf97defe12015-01-02 23:00:20 +0100407static void rht_deferred_worker(struct work_struct *work)
408{
409 struct rhashtable *ht;
410 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100411 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100412
Ying Xue57699a42015-01-16 11:13:09 +0800413 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100414 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100415
Thomas Graf97defe12015-01-02 23:00:20 +0100416 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100417 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100418
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100419 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800420 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000421 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800422 err = rhashtable_shrink(ht);
423 else if (tbl->nest)
424 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub8244782015-03-24 00:50:26 +1100425
Herbert Xu408f13e2019-03-21 09:39:52 +0800426 if (!err || err == -EEXIST) {
427 int nerr;
428
429 nerr = rhashtable_rehash_table(ht);
430 err = err ?: nerr;
431 }
Herbert Xub8244782015-03-24 00:50:26 +1100432
Thomas Graf97defe12015-01-02 23:00:20 +0100433 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100434
435 if (err)
436 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100437}
438
Herbert Xuca268932016-09-19 19:00:09 +0800439static int rhashtable_insert_rehash(struct rhashtable *ht,
440 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100441{
442 struct bucket_table *old_tbl;
443 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100444 unsigned int size;
445 int err;
446
447 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100448
449 size = tbl->size;
450
Herbert Xu3cf92222015-12-03 20:41:29 +0800451 err = -EBUSY;
452
Herbert Xuccd57b12015-03-24 00:50:28 +1100453 if (rht_grow_above_75(ht, tbl))
454 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200455 /* Do not schedule more than one rehash */
456 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800457 goto fail;
458
459 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100460
Davidlohr Bueso93f976b2018-08-21 22:01:45 -0700461 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
Herbert Xu3cf92222015-12-03 20:41:29 +0800462 if (new_tbl == NULL)
463 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100464
465 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
466 if (err) {
467 bucket_table_free(new_tbl);
468 if (err == -EEXIST)
469 err = 0;
470 } else
471 schedule_work(&ht->run_work);
472
473 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800474
475fail:
476 /* Do not fail the insert if someone else did a rehash. */
NeilBrownc0690012018-06-18 12:52:50 +1000477 if (likely(rcu_access_pointer(tbl->future_tbl)))
Herbert Xu3cf92222015-12-03 20:41:29 +0800478 return 0;
479
480 /* Schedule async rehash to retry allocation in process context. */
481 if (err == -ENOMEM)
482 schedule_work(&ht->run_work);
483
484 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100485}
Herbert Xuccd57b12015-03-24 00:50:28 +1100486
Herbert Xuca268932016-09-19 19:00:09 +0800487static void *rhashtable_lookup_one(struct rhashtable *ht,
Herbert Xuce9b3622020-07-24 20:14:34 +1000488 struct rhash_lock_head __rcu **bkt,
Herbert Xuca268932016-09-19 19:00:09 +0800489 struct bucket_table *tbl, unsigned int hash,
490 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100491{
Herbert Xuca268932016-09-19 19:00:09 +0800492 struct rhashtable_compare_arg arg = {
493 .ht = ht,
494 .key = key,
495 };
NeilBrowne4edbe32019-04-12 11:52:07 +1000496 struct rhash_head __rcu **pprev = NULL;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100497 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800498 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100499
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200500 elasticity = RHT_ELASTICITY;
NeilBrownadc6a3a2019-04-12 11:52:08 +1000501 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800502 struct rhlist_head *list;
503 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100504
Herbert Xuca268932016-09-19 19:00:09 +0800505 elasticity--;
506 if (!key ||
507 (ht->p.obj_cmpfn ?
508 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200509 rhashtable_compare(&arg, rht_obj(ht, head)))) {
510 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800511 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200512 }
Herbert Xuca268932016-09-19 19:00:09 +0800513
514 if (!ht->rhlist)
515 return rht_obj(ht, head);
516
517 list = container_of(obj, struct rhlist_head, rhead);
518 plist = container_of(head, struct rhlist_head, rhead);
519
520 RCU_INIT_POINTER(list->next, plist);
521 head = rht_dereference_bucket(head->next, tbl, hash);
522 RCU_INIT_POINTER(list->rhead.next, head);
NeilBrown8f0db012019-04-02 10:07:45 +1100523 if (pprev)
524 rcu_assign_pointer(*pprev, obj);
525 else
526 /* Need to preserve the bit lock */
NeilBrownf4712b42019-04-12 11:52:08 +1000527 rht_assign_locked(bkt, obj);
Herbert Xuca268932016-09-19 19:00:09 +0800528
529 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200530 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100531
Herbert Xuca268932016-09-19 19:00:09 +0800532 if (elasticity <= 0)
533 return ERR_PTR(-EAGAIN);
534
535 return ERR_PTR(-ENOENT);
536}
537
Herbert Xuce9b3622020-07-24 20:14:34 +1000538static struct bucket_table *rhashtable_insert_one(
539 struct rhashtable *ht, struct rhash_lock_head __rcu **bkt,
540 struct bucket_table *tbl, unsigned int hash, struct rhash_head *obj,
541 void *data)
Herbert Xuca268932016-09-19 19:00:09 +0800542{
543 struct bucket_table *new_tbl;
544 struct rhash_head *head;
545
546 if (!IS_ERR_OR_NULL(data))
547 return ERR_PTR(-EEXIST);
548
549 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
550 return ERR_CAST(data);
551
NeilBrownc0690012018-06-18 12:52:50 +1000552 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800553 if (new_tbl)
554 return new_tbl;
555
556 if (PTR_ERR(data) != -ENOENT)
557 return ERR_CAST(data);
558
Herbert Xu07ee0722015-05-15 11:30:47 +0800559 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800560 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800561
Herbert Xuca268932016-09-19 19:00:09 +0800562 if (unlikely(rht_grow_above_100(ht, tbl)))
563 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100564
NeilBrownadc6a3a2019-04-12 11:52:08 +1000565 head = rht_ptr(bkt, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100566
567 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800568 if (ht->rhlist) {
569 struct rhlist_head *list;
570
571 list = container_of(obj, struct rhlist_head, rhead);
572 RCU_INIT_POINTER(list->next, NULL);
573 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100574
NeilBrown8f0db012019-04-02 10:07:45 +1100575 /* bkt is always the head of the list, so it holds
576 * the lock, which we need to preserve
577 */
NeilBrownf4712b42019-04-12 11:52:08 +1000578 rht_assign_locked(bkt, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100579
580 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800581 if (rht_grow_above_75(ht, tbl))
582 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100583
Herbert Xuca268932016-09-19 19:00:09 +0800584 return NULL;
585}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100586
Herbert Xuca268932016-09-19 19:00:09 +0800587static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
588 struct rhash_head *obj)
589{
590 struct bucket_table *new_tbl;
591 struct bucket_table *tbl;
Herbert Xuce9b3622020-07-24 20:14:34 +1000592 struct rhash_lock_head __rcu **bkt;
Herbert Xuca268932016-09-19 19:00:09 +0800593 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800594 void *data;
595
NeilBrown4feb7c72019-03-21 14:42:40 +1100596 new_tbl = rcu_dereference(ht->tbl);
Herbert Xuca268932016-09-19 19:00:09 +0800597
NeilBrown4feb7c72019-03-21 14:42:40 +1100598 do {
Herbert Xuca268932016-09-19 19:00:09 +0800599 tbl = new_tbl;
600 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
NeilBrown8f0db012019-04-02 10:07:45 +1100601 if (rcu_access_pointer(tbl->future_tbl))
602 /* Failure is OK */
603 bkt = rht_bucket_var(tbl, hash);
604 else
605 bkt = rht_bucket_insert(ht, tbl, hash);
606 if (bkt == NULL) {
607 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
608 data = ERR_PTR(-EAGAIN);
609 } else {
NeilBrown149212f2019-04-02 10:07:45 +1100610 rht_lock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100611 data = rhashtable_lookup_one(ht, bkt, tbl,
612 hash, key, obj);
613 new_tbl = rhashtable_insert_one(ht, bkt, tbl,
614 hash, obj, data);
615 if (PTR_ERR(new_tbl) != -EEXIST)
616 data = ERR_CAST(new_tbl);
Herbert Xuca268932016-09-19 19:00:09 +0800617
NeilBrown149212f2019-04-02 10:07:45 +1100618 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100619 }
NeilBrown4feb7c72019-03-21 14:42:40 +1100620 } while (!IS_ERR_OR_NULL(new_tbl));
Herbert Xuca268932016-09-19 19:00:09 +0800621
622 if (PTR_ERR(data) == -EAGAIN)
623 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
624 -EAGAIN);
625
626 return data;
627}
628
629void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
630 struct rhash_head *obj)
631{
632 void *data;
633
634 do {
635 rcu_read_lock();
636 data = rhashtable_try_insert(ht, key, obj);
637 rcu_read_unlock();
638 } while (PTR_ERR(data) == -EAGAIN);
639
640 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100641}
642EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
643
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100644/**
Herbert Xu246779d2016-08-18 16:50:56 +0800645 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100646 * @ht: Table to walk over
647 * @iter: Hash table Iterator
648 *
649 * This function prepares a hash table walk.
650 *
651 * Note that if you restart a walk after rhashtable_walk_stop you
652 * may see the same object twice. Also, you may miss objects if
653 * there are removals in between rhashtable_walk_stop and the next
654 * call to rhashtable_walk_start.
655 *
656 * For a completely stable walk you should construct your own data
657 * structure outside the hash table.
658 *
NeilBrown82266e92018-04-24 08:29:13 +1000659 * This function may be called from any process context, including
660 * non-preemptable context, but cannot be called from softirq or
661 * hardirq context.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100662 *
Herbert Xu246779d2016-08-18 16:50:56 +0800663 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100664 */
Herbert Xu246779d2016-08-18 16:50:56 +0800665void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100666{
667 iter->ht = ht;
668 iter->p = NULL;
669 iter->slot = 0;
670 iter->skip = 0;
Tom Herbert2db54b42017-12-04 10:31:42 -0800671 iter->end_of_table = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100672
Herbert Xuc6ff5262015-12-16 16:45:54 +0800673 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800674 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800675 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800676 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800677 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100678}
Herbert Xu246779d2016-08-18 16:50:56 +0800679EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100680
681/**
682 * rhashtable_walk_exit - Free an iterator
683 * @iter: Hash table Iterator
684 *
Herbert Xu6c4128f2019-02-14 22:03:27 +0800685 * This function frees resources allocated by rhashtable_walk_enter.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100686 */
687void rhashtable_walk_exit(struct rhashtable_iter *iter)
688{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800689 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800690 if (iter->walker.tbl)
691 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800692 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100693}
694EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
695
696/**
Tom Herbert97a6ec42017-12-04 10:31:41 -0800697 * rhashtable_walk_start_check - Start a hash table walk
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100698 * @iter: Hash table iterator
699 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200700 * Start a hash table walk at the current iterator position. Note that we take
701 * the RCU lock in all cases including when we return an error. So you must
702 * always call rhashtable_walk_stop to clean up.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100703 *
704 * Returns zero if successful.
705 *
Zhen Lei9dbbc3b2021-07-07 18:07:31 -0700706 * Returns -EAGAIN if resize event occurred. Note that the iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100707 * will rewind back to the beginning and you may use it immediately
708 * by calling rhashtable_walk_next.
Tom Herbert97a6ec42017-12-04 10:31:41 -0800709 *
710 * rhashtable_walk_start is defined as an inline variant that returns
711 * void. This is preferred in cases where the caller would ignore
712 * resize events and always continue.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100713 */
Tom Herbert97a6ec42017-12-04 10:31:41 -0800714int rhashtable_walk_start_check(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100715 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100716{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100717 struct rhashtable *ht = iter->ht;
NeilBrown5d240a82018-04-24 08:29:13 +1000718 bool rhlist = ht->rhlist;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100719
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100720 rcu_read_lock();
721
Herbert Xuc6ff5262015-12-16 16:45:54 +0800722 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800723 if (iter->walker.tbl)
724 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800725 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100726
NeilBrown5d240a82018-04-24 08:29:13 +1000727 if (iter->end_of_table)
728 return 0;
729 if (!iter->walker.tbl) {
Herbert Xu246779d2016-08-18 16:50:56 +0800730 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
NeilBrownb41cc042018-04-24 08:29:13 +1000731 iter->slot = 0;
732 iter->skip = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100733 return -EAGAIN;
734 }
735
NeilBrown5d240a82018-04-24 08:29:13 +1000736 if (iter->p && !rhlist) {
737 /*
738 * We need to validate that 'p' is still in the table, and
739 * if so, update 'skip'
740 */
741 struct rhash_head *p;
742 int skip = 0;
743 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
744 skip++;
745 if (p == iter->p) {
746 iter->skip = skip;
747 goto found;
748 }
749 }
750 iter->p = NULL;
751 } else if (iter->p && rhlist) {
752 /* Need to validate that 'list' is still in the table, and
753 * if so, update 'skip' and 'p'.
754 */
755 struct rhash_head *p;
756 struct rhlist_head *list;
757 int skip = 0;
758 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
759 for (list = container_of(p, struct rhlist_head, rhead);
760 list;
761 list = rcu_dereference(list->next)) {
762 skip++;
763 if (list == iter->list) {
764 iter->p = p;
Rishabh Bhatnagarc643ecf2018-07-02 09:35:34 -0700765 iter->skip = skip;
NeilBrown5d240a82018-04-24 08:29:13 +1000766 goto found;
767 }
768 }
769 }
770 iter->p = NULL;
771 }
772found:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100773 return 0;
774}
Tom Herbert97a6ec42017-12-04 10:31:41 -0800775EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100776
777/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800778 * __rhashtable_walk_find_next - Find the next element in a table (or the first
779 * one in case of a new walk).
780 *
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100781 * @iter: Hash table iterator
782 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800783 * Returns the found object or NULL when the end of the table is reached.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100784 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800785 * Returns -EAGAIN if resize event occurred.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100786 */
Tom Herbert2db54b42017-12-04 10:31:42 -0800787static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100788{
Herbert Xu246779d2016-08-18 16:50:56 +0800789 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xuca268932016-09-19 19:00:09 +0800790 struct rhlist_head *list = iter->list;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100791 struct rhashtable *ht = iter->ht;
792 struct rhash_head *p = iter->p;
Herbert Xuca268932016-09-19 19:00:09 +0800793 bool rhlist = ht->rhlist;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100794
Tom Herbert2db54b42017-12-04 10:31:42 -0800795 if (!tbl)
796 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100797
798 for (; iter->slot < tbl->size; iter->slot++) {
799 int skip = iter->skip;
800
801 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800802 if (rhlist) {
803 list = container_of(p, struct rhlist_head,
804 rhead);
805 do {
806 if (!skip)
807 goto next;
808 skip--;
809 list = rcu_dereference(list->next);
810 } while (list);
811
812 continue;
813 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100814 if (!skip)
815 break;
816 skip--;
817 }
818
819next:
820 if (!rht_is_a_nulls(p)) {
821 iter->skip++;
822 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800823 iter->list = list;
824 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100825 }
826
827 iter->skip = 0;
828 }
829
Phil Sutter142b9422015-07-06 15:51:20 +0200830 iter->p = NULL;
831
Herbert Xud88252f2015-03-24 00:50:19 +1100832 /* Ensure we see any new tables. */
833 smp_rmb();
834
Herbert Xu246779d2016-08-18 16:50:56 +0800835 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
836 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100837 iter->slot = 0;
838 iter->skip = 0;
839 return ERR_PTR(-EAGAIN);
Tom Herbert2db54b42017-12-04 10:31:42 -0800840 } else {
841 iter->end_of_table = true;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100842 }
843
Thomas Grafc936a792015-05-05 02:22:53 +0200844 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100845}
Tom Herbert2db54b42017-12-04 10:31:42 -0800846
847/**
848 * rhashtable_walk_next - Return the next object and advance the iterator
849 * @iter: Hash table iterator
850 *
851 * Note that you must call rhashtable_walk_stop when you are finished
852 * with the walk.
853 *
854 * Returns the next object or NULL when the end of the table is reached.
855 *
856 * Returns -EAGAIN if resize event occurred. Note that the iterator
857 * will rewind back to the beginning and you may continue to use it.
858 */
859void *rhashtable_walk_next(struct rhashtable_iter *iter)
860{
861 struct rhlist_head *list = iter->list;
862 struct rhashtable *ht = iter->ht;
863 struct rhash_head *p = iter->p;
864 bool rhlist = ht->rhlist;
865
866 if (p) {
867 if (!rhlist || !(list = rcu_dereference(list->next))) {
868 p = rcu_dereference(p->next);
869 list = container_of(p, struct rhlist_head, rhead);
870 }
871 if (!rht_is_a_nulls(p)) {
872 iter->skip++;
873 iter->p = p;
874 iter->list = list;
875 return rht_obj(ht, rhlist ? &list->rhead : p);
876 }
877
878 /* At the end of this slot, switch to next one and then find
879 * next entry from that point.
880 */
881 iter->skip = 0;
882 iter->slot++;
883 }
884
885 return __rhashtable_walk_find_next(iter);
886}
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100887EXPORT_SYMBOL_GPL(rhashtable_walk_next);
888
889/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800890 * rhashtable_walk_peek - Return the next object but don't advance the iterator
891 * @iter: Hash table iterator
892 *
893 * Returns the next object or NULL when the end of the table is reached.
894 *
895 * Returns -EAGAIN if resize event occurred. Note that the iterator
896 * will rewind back to the beginning and you may continue to use it.
897 */
898void *rhashtable_walk_peek(struct rhashtable_iter *iter)
899{
900 struct rhlist_head *list = iter->list;
901 struct rhashtable *ht = iter->ht;
902 struct rhash_head *p = iter->p;
903
904 if (p)
905 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
906
907 /* No object found in current iter, find next one in the table. */
908
909 if (iter->skip) {
910 /* A nonzero skip value points to the next entry in the table
911 * beyond that last one that was found. Decrement skip so
912 * we find the current value. __rhashtable_walk_find_next
913 * will restore the original value of skip assuming that
914 * the table hasn't changed.
915 */
916 iter->skip--;
917 }
918
919 return __rhashtable_walk_find_next(iter);
920}
921EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
922
923/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100924 * rhashtable_walk_stop - Finish a hash table walk
925 * @iter: Hash table iterator
926 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200927 * Finish a hash table walk. Does not reset the iterator to the start of the
928 * hash table.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100929 */
930void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100931 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100932{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100933 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800934 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100935
Herbert Xueddee5ba2015-03-14 13:57:20 +1100936 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100937 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100938
939 ht = iter->ht;
940
Herbert Xuba7c95e2015-03-24 09:53:17 +1100941 spin_lock(&ht->lock);
NeilBrown4feb7c72019-03-21 14:42:40 +1100942 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
943 /* This bucket table is being freed, don't re-link it. */
Herbert Xu246779d2016-08-18 16:50:56 +0800944 iter->walker.tbl = NULL;
NeilBrown4feb7c72019-03-21 14:42:40 +1100945 else
946 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xuba7c95e2015-03-24 09:53:17 +1100947 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100948
Herbert Xu963ecbd2015-03-15 21:12:04 +1100949out:
950 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100951}
952EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
953
Herbert Xu488fb86e2015-03-20 21:56:59 +1100954static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200955{
Davidlohr Bueso107d01f2018-07-16 13:26:13 -0700956 size_t retsize;
957
958 if (params->nelem_hint)
959 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
960 (unsigned long)params->min_size);
961 else
962 retsize = max(HASH_DEFAULT_SIZE,
963 (unsigned long)params->min_size);
964
965 return retsize;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200966}
967
Herbert Xu31ccde22015-03-24 00:50:21 +1100968static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
969{
970 return jhash2(key, length, seed);
971}
972
Thomas Graf7e1e7762014-08-02 11:47:44 +0200973/**
974 * rhashtable_init - initialize a new hash table
975 * @ht: hash table to be initialized
976 * @params: configuration parameters
977 *
978 * Initializes a new hash table based on the provided configuration
979 * parameters. A table can be configured either with a variable or
980 * fixed length key:
981 *
982 * Configuration Example 1: Fixed length keys
983 * struct test_obj {
984 * int key;
985 * void * my_member;
986 * struct rhash_head node;
987 * };
988 *
989 * struct rhashtable_params params = {
990 * .head_offset = offsetof(struct test_obj, node),
991 * .key_offset = offsetof(struct test_obj, key),
992 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100993 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200994 * };
995 *
996 * Configuration Example 2: Variable length keys
997 * struct test_obj {
998 * [...]
999 * struct rhash_head node;
1000 * };
1001 *
Patrick McHardy49f7b332015-03-25 13:07:45 +00001002 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001003 * {
1004 * struct test_obj *obj = data;
1005 *
1006 * return [... hash ...];
1007 * }
1008 *
1009 * struct rhashtable_params params = {
1010 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001011 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001012 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001013 * };
1014 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001015int rhashtable_init(struct rhashtable *ht,
1016 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001017{
1018 struct bucket_table *tbl;
1019 size_t size;
1020
Herbert Xu31ccde22015-03-24 00:50:21 +11001021 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001022 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001023 return -EINVAL;
1024
Thomas Graf97defe12015-01-02 23:00:20 +01001025 memset(ht, 0, sizeof(*ht));
1026 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001027 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001028 memcpy(&ht->p, params, sizeof(*params));
1029
Thomas Grafa998f712015-03-19 22:31:13 +00001030 if (params->min_size)
1031 ht->p.min_size = roundup_pow_of_two(params->min_size);
1032
Herbert Xu6d684e52017-04-27 13:44:51 +08001033 /* Cap total entries at 2^31 to avoid nelems overflow. */
1034 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001035
1036 if (params->max_size) {
1037 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1038 if (ht->p.max_size < ht->max_elems / 2)
1039 ht->max_elems = ht->p.max_size * 2;
1040 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001041
Florian Westphal48e75b432017-05-01 22:18:01 +02001042 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001043
Davidlohr Bueso107d01f2018-07-16 13:26:13 -07001044 size = rounded_hashtable_size(&ht->p);
Herbert Xu3a324602015-12-16 18:13:14 +08001045
Herbert Xu31ccde22015-03-24 00:50:21 +11001046 ht->key_len = ht->p.key_len;
1047 if (!params->hashfn) {
1048 ht->p.hashfn = jhash;
1049
1050 if (!(ht->key_len & (sizeof(u32) - 1))) {
1051 ht->key_len /= sizeof(u32);
1052 ht->p.hashfn = rhashtable_jhash2;
1053 }
1054 }
1055
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001056 /*
1057 * This is api initialization and thus we need to guarantee the
1058 * initial rhashtable allocation. Upon failure, retry with the
1059 * smallest possible size with __GFP_NOFAIL semantics.
1060 */
Herbert Xub9ecfda2015-03-24 00:50:27 +11001061 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001062 if (unlikely(tbl == NULL)) {
1063 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1064 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1065 }
Thomas Graf7e1e7762014-08-02 11:47:44 +02001066
Ying Xue545a1482015-01-07 13:41:57 +08001067 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001068
Thomas Graf7e1e7762014-08-02 11:47:44 +02001069 RCU_INIT_POINTER(ht->tbl, tbl);
1070
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001071 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001072
Thomas Graf7e1e7762014-08-02 11:47:44 +02001073 return 0;
1074}
1075EXPORT_SYMBOL_GPL(rhashtable_init);
1076
1077/**
Herbert Xuca268932016-09-19 19:00:09 +08001078 * rhltable_init - initialize a new hash list table
1079 * @hlt: hash list table to be initialized
1080 * @params: configuration parameters
1081 *
1082 * Initializes a new hash list table.
1083 *
1084 * See documentation for rhashtable_init.
1085 */
1086int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1087{
1088 int err;
1089
Herbert Xuca268932016-09-19 19:00:09 +08001090 err = rhashtable_init(&hlt->ht, params);
1091 hlt->ht.rhlist = true;
1092 return err;
1093}
1094EXPORT_SYMBOL_GPL(rhltable_init);
1095
1096static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1097 void (*free_fn)(void *ptr, void *arg),
1098 void *arg)
1099{
1100 struct rhlist_head *list;
1101
1102 if (!ht->rhlist) {
1103 free_fn(rht_obj(ht, obj), arg);
1104 return;
1105 }
1106
1107 list = container_of(obj, struct rhlist_head, rhead);
1108 do {
1109 obj = &list->rhead;
1110 list = rht_dereference(list->next, ht);
1111 free_fn(rht_obj(ht, obj), arg);
1112 } while (list);
1113}
1114
1115/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001116 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001117 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001118 * @free_fn: callback to release resources of element
1119 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001120 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001121 * Stops an eventual async resize. If defined, invokes free_fn for each
1122 * element to releasal resources. Please note that RCU protected
1123 * readers may still be accessing the elements. Releasing of resources
1124 * must occur in a compatible manner. Then frees the bucket array.
1125 *
1126 * This function will eventually sleep to wait for an async resize
1127 * to complete. The caller is responsible that no further write operations
1128 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001129 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001130void rhashtable_free_and_destroy(struct rhashtable *ht,
1131 void (*free_fn)(void *ptr, void *arg),
1132 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001133{
Taehee Yoo00261292018-07-08 11:55:51 +09001134 struct bucket_table *tbl, *next_tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001135 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001136
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001137 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001138
Thomas Graf97defe12015-01-02 23:00:20 +01001139 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001140 tbl = rht_dereference(ht->tbl, ht);
Taehee Yoo00261292018-07-08 11:55:51 +09001141restart:
Thomas Graf6b6f3022015-03-24 14:18:20 +01001142 if (free_fn) {
1143 for (i = 0; i < tbl->size; i++) {
1144 struct rhash_head *pos, *next;
1145
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001146 cond_resched();
NeilBrownadc6a3a2019-04-12 11:52:08 +10001147 for (pos = rht_ptr_exclusive(rht_bucket(tbl, i)),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001148 next = !rht_is_a_nulls(pos) ?
1149 rht_dereference(pos->next, ht) : NULL;
1150 !rht_is_a_nulls(pos);
1151 pos = next,
1152 next = !rht_is_a_nulls(pos) ?
1153 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001154 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001155 }
1156 }
1157
Taehee Yoo00261292018-07-08 11:55:51 +09001158 next_tbl = rht_dereference(tbl->future_tbl, ht);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001159 bucket_table_free(tbl);
Taehee Yoo00261292018-07-08 11:55:51 +09001160 if (next_tbl) {
1161 tbl = next_tbl;
1162 goto restart;
1163 }
Thomas Graf97defe12015-01-02 23:00:20 +01001164 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001165}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001166EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1167
1168void rhashtable_destroy(struct rhashtable *ht)
1169{
1170 return rhashtable_free_and_destroy(ht, NULL, NULL);
1171}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001172EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001173
Herbert Xuce9b3622020-07-24 20:14:34 +10001174struct rhash_lock_head __rcu **__rht_bucket_nested(
1175 const struct bucket_table *tbl, unsigned int hash)
Herbert Xuda204202017-02-11 19:26:47 +08001176{
1177 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
Herbert Xuda204202017-02-11 19:26:47 +08001178 unsigned int index = hash & ((1 << tbl->nest) - 1);
1179 unsigned int size = tbl->size >> tbl->nest;
1180 unsigned int subhash = hash;
1181 union nested_table *ntbl;
1182
Herbert Xu4a3084a2020-06-03 18:12:43 +10001183 ntbl = nested_table_top(tbl);
Herbert Xuc4d26032017-02-25 22:39:50 +08001184 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001185 subhash >>= tbl->nest;
1186
1187 while (ntbl && size > (1 << shift)) {
1188 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001189 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1190 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001191 size >>= shift;
1192 subhash >>= shift;
1193 }
1194
NeilBrownff302db2019-04-02 10:07:45 +11001195 if (!ntbl)
1196 return NULL;
Herbert Xuda204202017-02-11 19:26:47 +08001197
1198 return &ntbl[subhash].bucket;
1199
1200}
NeilBrownff302db2019-04-02 10:07:45 +11001201EXPORT_SYMBOL_GPL(__rht_bucket_nested);
1202
Herbert Xuce9b3622020-07-24 20:14:34 +10001203struct rhash_lock_head __rcu **rht_bucket_nested(
1204 const struct bucket_table *tbl, unsigned int hash)
NeilBrownff302db2019-04-02 10:07:45 +11001205{
Herbert Xuce9b3622020-07-24 20:14:34 +10001206 static struct rhash_lock_head __rcu *rhnull;
NeilBrownff302db2019-04-02 10:07:45 +11001207
1208 if (!rhnull)
1209 INIT_RHT_NULLS_HEAD(rhnull);
1210 return __rht_bucket_nested(tbl, hash) ?: &rhnull;
1211}
Herbert Xuda204202017-02-11 19:26:47 +08001212EXPORT_SYMBOL_GPL(rht_bucket_nested);
1213
Herbert Xuce9b3622020-07-24 20:14:34 +10001214struct rhash_lock_head __rcu **rht_bucket_nested_insert(
1215 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
Herbert Xuda204202017-02-11 19:26:47 +08001216{
1217 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1218 unsigned int index = hash & ((1 << tbl->nest) - 1);
1219 unsigned int size = tbl->size >> tbl->nest;
1220 union nested_table *ntbl;
Herbert Xuda204202017-02-11 19:26:47 +08001221
Herbert Xu4a3084a2020-06-03 18:12:43 +10001222 ntbl = nested_table_top(tbl);
Herbert Xuda204202017-02-11 19:26:47 +08001223 hash >>= tbl->nest;
Herbert Xuda204202017-02-11 19:26:47 +08001224 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001225 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001226
1227 while (ntbl && size > (1 << shift)) {
1228 index = hash & ((1 << shift) - 1);
1229 size >>= shift;
1230 hash >>= shift;
Herbert Xuda204202017-02-11 19:26:47 +08001231 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001232 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001233 }
1234
1235 if (!ntbl)
1236 return NULL;
1237
1238 return &ntbl[hash].bucket;
1239
1240}
1241EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);