blob: 6ae2ba8e06a2138d77e6b0d5f51c67e17658b1a7 [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 Xub824478b2015-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;
Tejun Heoe47877c2022-12-06 11:36:32 -1000234 unsigned long flags;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100235
Herbert Xuda204202017-02-11 19:26:47 +0800236 if (new_tbl->nest)
237 goto out;
238
239 err = -ENOENT;
240
NeilBrownadc6a3a2019-04-12 11:52:08 +1000241 rht_for_each_from(entry, rht_ptr(bkt, old_tbl, old_hash),
242 old_tbl, old_hash) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100243 err = 0;
244 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100245
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100246 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200247 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100248
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100249 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200250 }
251
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100252 if (err)
253 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100254
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100255 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100256
Tejun Heoe47877c2022-12-06 11:36:32 -1000257 flags = rht_lock_nested(new_tbl, &new_tbl->buckets[new_hash],
258 SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100259
NeilBrownadc6a3a2019-04-12 11:52:08 +1000260 head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100261
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200262 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100263
Tejun Heoe47877c2022-12-06 11:36:32 -1000264 rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry, flags);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100265
NeilBrown8f0db012019-04-02 10:07:45 +1100266 if (pprev)
267 rcu_assign_pointer(*pprev, next);
268 else
269 /* Need to preserved the bit lock. */
NeilBrownf4712b42019-04-12 11:52:08 +1000270 rht_assign_locked(bkt, next);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100271
272out:
273 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100274}
275
Herbert Xuda204202017-02-11 19:26:47 +0800276static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100277 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100278{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100279 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xuce9b3622020-07-24 20:14:34 +1000280 struct rhash_lock_head __rcu **bkt = rht_bucket_var(old_tbl, old_hash);
Tejun Heoe47877c2022-12-06 11:36:32 -1000281 unsigned long flags;
Herbert Xuda204202017-02-11 19:26:47 +0800282 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100283
NeilBrown8f0db012019-04-02 10:07:45 +1100284 if (!bkt)
285 return 0;
Tejun Heoe47877c2022-12-06 11:36:32 -1000286 flags = rht_lock(old_tbl, bkt);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100287
NeilBrown8f0db012019-04-02 10:07:45 +1100288 while (!(err = rhashtable_rehash_one(ht, bkt, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100289 ;
Herbert Xuda204202017-02-11 19:26:47 +0800290
NeilBrown4feb7c72019-03-21 14:42:40 +1100291 if (err == -ENOENT)
Herbert Xuda204202017-02-11 19:26:47 +0800292 err = 0;
Tejun Heoe47877c2022-12-06 11:36:32 -1000293 rht_unlock(old_tbl, bkt, flags);
Herbert Xuda204202017-02-11 19:26:47 +0800294
295 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100296}
297
Herbert Xub824478b2015-03-24 00:50:26 +1100298static int rhashtable_rehash_attach(struct rhashtable *ht,
299 struct bucket_table *old_tbl,
300 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100301{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100302 /* Make insertions go into the new, empty table right away. Deletions
303 * and lookups will be attempted in both tables until we synchronize.
NeilBrown0ad66442018-06-18 12:52:50 +1000304 * As cmpxchg() provides strong barriers, we do not need
305 * rcu_assign_pointer().
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100306 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100307
Herbert Xue9458a42019-05-16 15:19:48 +0800308 if (cmpxchg((struct bucket_table **)&old_tbl->future_tbl, NULL,
309 new_tbl) != NULL)
NeilBrown0ad66442018-06-18 12:52:50 +1000310 return -EEXIST;
Herbert Xub824478b2015-03-24 00:50:26 +1100311
312 return 0;
313}
314
315static int rhashtable_rehash_table(struct rhashtable *ht)
316{
317 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
318 struct bucket_table *new_tbl;
319 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100320 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800321 int err;
Herbert Xub824478b2015-03-24 00:50:26 +1100322
323 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
324 if (!new_tbl)
325 return 0;
326
Herbert Xuda204202017-02-11 19:26:47 +0800327 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
328 err = rhashtable_rehash_chain(ht, old_hash);
329 if (err)
330 return err;
Eric Dumazetae6da1f2018-03-31 12:58:48 -0700331 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +0800332 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100333
334 /* Publish the new table pointer. */
335 rcu_assign_pointer(ht->tbl, new_tbl);
336
Herbert Xuba7c95e2015-03-24 09:53:17 +1100337 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100338 list_for_each_entry(walker, &old_tbl->walkers, list)
339 walker->tbl = NULL;
340
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100341 /* Wait for readers. All new readers will see the new
342 * table, and thus no references to the old table will
343 * remain.
NeilBrown4feb7c72019-03-21 14:42:40 +1100344 * We do this inside the locked region so that
345 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
346 * to check if it should not re-link the table.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100347 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100348 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
NeilBrown4feb7c72019-03-21 14:42:40 +1100349 spin_unlock(&ht->lock);
Herbert Xub824478b2015-03-24 00:50:26 +1100350
351 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200352}
353
Herbert Xuda204202017-02-11 19:26:47 +0800354static int rhashtable_rehash_alloc(struct rhashtable *ht,
355 struct bucket_table *old_tbl,
356 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200357{
Herbert Xuda204202017-02-11 19:26:47 +0800358 struct bucket_table *new_tbl;
Herbert Xub824478b2015-03-24 00:50:26 +1100359 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200360
361 ASSERT_RHT_MUTEX(ht);
362
Herbert Xuda204202017-02-11 19:26:47 +0800363 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200364 if (new_tbl == NULL)
365 return -ENOMEM;
366
Herbert Xub824478b2015-03-24 00:50:26 +1100367 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
368 if (err)
369 bucket_table_free(new_tbl);
370
371 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200372}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200373
374/**
375 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
376 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200377 *
Herbert Xu18093d12015-03-24 00:50:25 +1100378 * This function shrinks the hash table to fit, i.e., the smallest
379 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200380 *
Thomas Graf97defe12015-01-02 23:00:20 +0100381 * The caller must ensure that no concurrent resizing occurs by holding
382 * ht->mutex.
383 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200384 * The caller must ensure that no concurrent table mutations take place.
385 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100386 *
387 * It is valid to have concurrent insertions and deletions protected by per
388 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200389 */
Herbert Xub824478b2015-03-24 00:50:26 +1100390static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200391{
Herbert Xuda204202017-02-11 19:26:47 +0800392 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200393 unsigned int nelems = atomic_read(&ht->nelems);
394 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200395
Vegard Nossum12311952016-08-12 20:10:44 +0200396 if (nelems)
397 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100398 if (size < ht->p.min_size)
399 size = ht->p.min_size;
400
401 if (old_tbl->size <= size)
402 return 0;
403
Herbert Xub824478b2015-03-24 00:50:26 +1100404 if (rht_dereference(old_tbl->future_tbl, ht))
405 return -EEXIST;
406
Herbert Xuda204202017-02-11 19:26:47 +0800407 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200408}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200409
Thomas Graf97defe12015-01-02 23:00:20 +0100410static void rht_deferred_worker(struct work_struct *work)
411{
412 struct rhashtable *ht;
413 struct bucket_table *tbl;
Herbert Xub824478b2015-03-24 00:50:26 +1100414 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100415
Ying Xue57699a42015-01-16 11:13:09 +0800416 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100417 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100418
Thomas Graf97defe12015-01-02 23:00:20 +0100419 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub824478b2015-03-24 00:50:26 +1100420 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100421
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100422 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800423 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000424 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800425 err = rhashtable_shrink(ht);
426 else if (tbl->nest)
427 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub824478b2015-03-24 00:50:26 +1100428
Herbert Xu408f13e2019-03-21 09:39:52 +0800429 if (!err || err == -EEXIST) {
430 int nerr;
431
432 nerr = rhashtable_rehash_table(ht);
433 err = err ?: nerr;
434 }
Herbert Xub824478b2015-03-24 00:50:26 +1100435
Thomas Graf97defe12015-01-02 23:00:20 +0100436 mutex_unlock(&ht->mutex);
Herbert Xub824478b2015-03-24 00:50:26 +1100437
438 if (err)
439 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100440}
441
Herbert Xuca268932016-09-19 19:00:09 +0800442static int rhashtable_insert_rehash(struct rhashtable *ht,
443 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100444{
445 struct bucket_table *old_tbl;
446 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100447 unsigned int size;
448 int err;
449
450 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100451
452 size = tbl->size;
453
Herbert Xu3cf92222015-12-03 20:41:29 +0800454 err = -EBUSY;
455
Herbert Xuccd57b12015-03-24 00:50:28 +1100456 if (rht_grow_above_75(ht, tbl))
457 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200458 /* Do not schedule more than one rehash */
459 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800460 goto fail;
461
462 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100463
Davidlohr Bueso93f976b2018-08-21 22:01:45 -0700464 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
Herbert Xu3cf92222015-12-03 20:41:29 +0800465 if (new_tbl == NULL)
466 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100467
468 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
469 if (err) {
470 bucket_table_free(new_tbl);
471 if (err == -EEXIST)
472 err = 0;
473 } else
474 schedule_work(&ht->run_work);
475
476 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800477
478fail:
479 /* Do not fail the insert if someone else did a rehash. */
NeilBrownc0690012018-06-18 12:52:50 +1000480 if (likely(rcu_access_pointer(tbl->future_tbl)))
Herbert Xu3cf92222015-12-03 20:41:29 +0800481 return 0;
482
483 /* Schedule async rehash to retry allocation in process context. */
484 if (err == -ENOMEM)
485 schedule_work(&ht->run_work);
486
487 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100488}
Herbert Xuccd57b12015-03-24 00:50:28 +1100489
Herbert Xuca268932016-09-19 19:00:09 +0800490static void *rhashtable_lookup_one(struct rhashtable *ht,
Herbert Xuce9b3622020-07-24 20:14:34 +1000491 struct rhash_lock_head __rcu **bkt,
Herbert Xuca268932016-09-19 19:00:09 +0800492 struct bucket_table *tbl, unsigned int hash,
493 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100494{
Herbert Xuca268932016-09-19 19:00:09 +0800495 struct rhashtable_compare_arg arg = {
496 .ht = ht,
497 .key = key,
498 };
NeilBrowne4edbe32019-04-12 11:52:07 +1000499 struct rhash_head __rcu **pprev = NULL;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100500 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800501 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100502
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200503 elasticity = RHT_ELASTICITY;
NeilBrownadc6a3a2019-04-12 11:52:08 +1000504 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800505 struct rhlist_head *list;
506 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100507
Herbert Xuca268932016-09-19 19:00:09 +0800508 elasticity--;
509 if (!key ||
510 (ht->p.obj_cmpfn ?
511 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200512 rhashtable_compare(&arg, rht_obj(ht, head)))) {
513 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800514 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200515 }
Herbert Xuca268932016-09-19 19:00:09 +0800516
517 if (!ht->rhlist)
518 return rht_obj(ht, head);
519
520 list = container_of(obj, struct rhlist_head, rhead);
521 plist = container_of(head, struct rhlist_head, rhead);
522
523 RCU_INIT_POINTER(list->next, plist);
524 head = rht_dereference_bucket(head->next, tbl, hash);
525 RCU_INIT_POINTER(list->rhead.next, head);
NeilBrown8f0db012019-04-02 10:07:45 +1100526 if (pprev)
527 rcu_assign_pointer(*pprev, obj);
528 else
529 /* Need to preserve the bit lock */
NeilBrownf4712b42019-04-12 11:52:08 +1000530 rht_assign_locked(bkt, obj);
Herbert Xuca268932016-09-19 19:00:09 +0800531
532 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200533 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100534
Herbert Xuca268932016-09-19 19:00:09 +0800535 if (elasticity <= 0)
536 return ERR_PTR(-EAGAIN);
537
538 return ERR_PTR(-ENOENT);
539}
540
Herbert Xuce9b3622020-07-24 20:14:34 +1000541static struct bucket_table *rhashtable_insert_one(
542 struct rhashtable *ht, struct rhash_lock_head __rcu **bkt,
543 struct bucket_table *tbl, unsigned int hash, struct rhash_head *obj,
544 void *data)
Herbert Xuca268932016-09-19 19:00:09 +0800545{
546 struct bucket_table *new_tbl;
547 struct rhash_head *head;
548
549 if (!IS_ERR_OR_NULL(data))
550 return ERR_PTR(-EEXIST);
551
552 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
553 return ERR_CAST(data);
554
NeilBrownc0690012018-06-18 12:52:50 +1000555 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800556 if (new_tbl)
557 return new_tbl;
558
559 if (PTR_ERR(data) != -ENOENT)
560 return ERR_CAST(data);
561
Herbert Xu07ee0722015-05-15 11:30:47 +0800562 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800563 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800564
Herbert Xuca268932016-09-19 19:00:09 +0800565 if (unlikely(rht_grow_above_100(ht, tbl)))
566 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100567
NeilBrownadc6a3a2019-04-12 11:52:08 +1000568 head = rht_ptr(bkt, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100569
570 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800571 if (ht->rhlist) {
572 struct rhlist_head *list;
573
574 list = container_of(obj, struct rhlist_head, rhead);
575 RCU_INIT_POINTER(list->next, NULL);
576 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100577
NeilBrown8f0db012019-04-02 10:07:45 +1100578 /* bkt is always the head of the list, so it holds
579 * the lock, which we need to preserve
580 */
NeilBrownf4712b42019-04-12 11:52:08 +1000581 rht_assign_locked(bkt, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100582
583 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800584 if (rht_grow_above_75(ht, tbl))
585 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100586
Herbert Xuca268932016-09-19 19:00:09 +0800587 return NULL;
588}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100589
Herbert Xuca268932016-09-19 19:00:09 +0800590static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
591 struct rhash_head *obj)
592{
593 struct bucket_table *new_tbl;
594 struct bucket_table *tbl;
Herbert Xuce9b3622020-07-24 20:14:34 +1000595 struct rhash_lock_head __rcu **bkt;
Tejun Heoe47877c2022-12-06 11:36:32 -1000596 unsigned long flags;
Herbert Xuca268932016-09-19 19:00:09 +0800597 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800598 void *data;
599
NeilBrown4feb7c72019-03-21 14:42:40 +1100600 new_tbl = rcu_dereference(ht->tbl);
Herbert Xuca268932016-09-19 19:00:09 +0800601
NeilBrown4feb7c72019-03-21 14:42:40 +1100602 do {
Herbert Xuca268932016-09-19 19:00:09 +0800603 tbl = new_tbl;
604 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
NeilBrown8f0db012019-04-02 10:07:45 +1100605 if (rcu_access_pointer(tbl->future_tbl))
606 /* Failure is OK */
607 bkt = rht_bucket_var(tbl, hash);
608 else
609 bkt = rht_bucket_insert(ht, tbl, hash);
610 if (bkt == NULL) {
611 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
612 data = ERR_PTR(-EAGAIN);
613 } else {
Tejun Heoe47877c2022-12-06 11:36:32 -1000614 flags = rht_lock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100615 data = rhashtable_lookup_one(ht, bkt, tbl,
616 hash, key, obj);
617 new_tbl = rhashtable_insert_one(ht, bkt, tbl,
618 hash, obj, data);
619 if (PTR_ERR(new_tbl) != -EEXIST)
620 data = ERR_CAST(new_tbl);
Herbert Xuca268932016-09-19 19:00:09 +0800621
Tejun Heoe47877c2022-12-06 11:36:32 -1000622 rht_unlock(tbl, bkt, flags);
NeilBrown8f0db012019-04-02 10:07:45 +1100623 }
NeilBrown4feb7c72019-03-21 14:42:40 +1100624 } while (!IS_ERR_OR_NULL(new_tbl));
Herbert Xuca268932016-09-19 19:00:09 +0800625
626 if (PTR_ERR(data) == -EAGAIN)
627 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
628 -EAGAIN);
629
630 return data;
631}
632
633void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
634 struct rhash_head *obj)
635{
636 void *data;
637
638 do {
639 rcu_read_lock();
640 data = rhashtable_try_insert(ht, key, obj);
641 rcu_read_unlock();
642 } while (PTR_ERR(data) == -EAGAIN);
643
644 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100645}
646EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
647
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100648/**
Herbert Xu246779d2016-08-18 16:50:56 +0800649 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100650 * @ht: Table to walk over
651 * @iter: Hash table Iterator
652 *
653 * This function prepares a hash table walk.
654 *
655 * Note that if you restart a walk after rhashtable_walk_stop you
656 * may see the same object twice. Also, you may miss objects if
657 * there are removals in between rhashtable_walk_stop and the next
658 * call to rhashtable_walk_start.
659 *
660 * For a completely stable walk you should construct your own data
661 * structure outside the hash table.
662 *
NeilBrown82266e92018-04-24 08:29:13 +1000663 * This function may be called from any process context, including
664 * non-preemptable context, but cannot be called from softirq or
665 * hardirq context.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100666 *
Herbert Xu246779d2016-08-18 16:50:56 +0800667 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100668 */
Herbert Xu246779d2016-08-18 16:50:56 +0800669void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100670{
671 iter->ht = ht;
672 iter->p = NULL;
673 iter->slot = 0;
674 iter->skip = 0;
Tom Herbert2db54b42017-12-04 10:31:42 -0800675 iter->end_of_table = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100676
Herbert Xuc6ff5262015-12-16 16:45:54 +0800677 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800678 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800679 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800680 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800681 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100682}
Herbert Xu246779d2016-08-18 16:50:56 +0800683EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100684
685/**
686 * rhashtable_walk_exit - Free an iterator
687 * @iter: Hash table Iterator
688 *
Herbert Xu6c4128f2019-02-14 22:03:27 +0800689 * This function frees resources allocated by rhashtable_walk_enter.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100690 */
691void rhashtable_walk_exit(struct rhashtable_iter *iter)
692{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800693 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800694 if (iter->walker.tbl)
695 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800696 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100697}
698EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
699
700/**
Tom Herbert97a6ec42017-12-04 10:31:41 -0800701 * rhashtable_walk_start_check - Start a hash table walk
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100702 * @iter: Hash table iterator
703 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200704 * Start a hash table walk at the current iterator position. Note that we take
705 * the RCU lock in all cases including when we return an error. So you must
706 * always call rhashtable_walk_stop to clean up.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100707 *
708 * Returns zero if successful.
709 *
Zhen Lei9dbbc3b2021-07-07 18:07:31 -0700710 * Returns -EAGAIN if resize event occurred. Note that the iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100711 * will rewind back to the beginning and you may use it immediately
712 * by calling rhashtable_walk_next.
Tom Herbert97a6ec42017-12-04 10:31:41 -0800713 *
714 * rhashtable_walk_start is defined as an inline variant that returns
715 * void. This is preferred in cases where the caller would ignore
716 * resize events and always continue.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100717 */
Tom Herbert97a6ec42017-12-04 10:31:41 -0800718int rhashtable_walk_start_check(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100719 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100720{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100721 struct rhashtable *ht = iter->ht;
NeilBrown5d240a82018-04-24 08:29:13 +1000722 bool rhlist = ht->rhlist;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100723
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100724 rcu_read_lock();
725
Herbert Xuc6ff5262015-12-16 16:45:54 +0800726 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800727 if (iter->walker.tbl)
728 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800729 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100730
NeilBrown5d240a82018-04-24 08:29:13 +1000731 if (iter->end_of_table)
732 return 0;
733 if (!iter->walker.tbl) {
Herbert Xu246779d2016-08-18 16:50:56 +0800734 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
NeilBrownb41cc042018-04-24 08:29:13 +1000735 iter->slot = 0;
736 iter->skip = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100737 return -EAGAIN;
738 }
739
NeilBrown5d240a82018-04-24 08:29:13 +1000740 if (iter->p && !rhlist) {
741 /*
742 * We need to validate that 'p' is still in the table, and
743 * if so, update 'skip'
744 */
745 struct rhash_head *p;
746 int skip = 0;
747 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
748 skip++;
749 if (p == iter->p) {
750 iter->skip = skip;
751 goto found;
752 }
753 }
754 iter->p = NULL;
755 } else if (iter->p && rhlist) {
756 /* Need to validate that 'list' is still in the table, and
757 * if so, update 'skip' and 'p'.
758 */
759 struct rhash_head *p;
760 struct rhlist_head *list;
761 int skip = 0;
762 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
763 for (list = container_of(p, struct rhlist_head, rhead);
764 list;
765 list = rcu_dereference(list->next)) {
766 skip++;
767 if (list == iter->list) {
768 iter->p = p;
Rishabh Bhatnagarc643ecf2018-07-02 09:35:34 -0700769 iter->skip = skip;
NeilBrown5d240a82018-04-24 08:29:13 +1000770 goto found;
771 }
772 }
773 }
774 iter->p = NULL;
775 }
776found:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100777 return 0;
778}
Tom Herbert97a6ec42017-12-04 10:31:41 -0800779EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100780
781/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800782 * __rhashtable_walk_find_next - Find the next element in a table (or the first
783 * one in case of a new walk).
784 *
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100785 * @iter: Hash table iterator
786 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800787 * Returns the found object or NULL when the end of the table is reached.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100788 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800789 * Returns -EAGAIN if resize event occurred.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100790 */
Tom Herbert2db54b42017-12-04 10:31:42 -0800791static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100792{
Herbert Xu246779d2016-08-18 16:50:56 +0800793 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xuca268932016-09-19 19:00:09 +0800794 struct rhlist_head *list = iter->list;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100795 struct rhashtable *ht = iter->ht;
796 struct rhash_head *p = iter->p;
Herbert Xuca268932016-09-19 19:00:09 +0800797 bool rhlist = ht->rhlist;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100798
Tom Herbert2db54b42017-12-04 10:31:42 -0800799 if (!tbl)
800 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100801
802 for (; iter->slot < tbl->size; iter->slot++) {
803 int skip = iter->skip;
804
805 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800806 if (rhlist) {
807 list = container_of(p, struct rhlist_head,
808 rhead);
809 do {
810 if (!skip)
811 goto next;
812 skip--;
813 list = rcu_dereference(list->next);
814 } while (list);
815
816 continue;
817 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100818 if (!skip)
819 break;
820 skip--;
821 }
822
823next:
824 if (!rht_is_a_nulls(p)) {
825 iter->skip++;
826 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800827 iter->list = list;
828 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100829 }
830
831 iter->skip = 0;
832 }
833
Phil Sutter142b9422015-07-06 15:51:20 +0200834 iter->p = NULL;
835
Herbert Xud88252f2015-03-24 00:50:19 +1100836 /* Ensure we see any new tables. */
837 smp_rmb();
838
Herbert Xu246779d2016-08-18 16:50:56 +0800839 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
840 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100841 iter->slot = 0;
842 iter->skip = 0;
843 return ERR_PTR(-EAGAIN);
Tom Herbert2db54b42017-12-04 10:31:42 -0800844 } else {
845 iter->end_of_table = true;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100846 }
847
Thomas Grafc936a792015-05-05 02:22:53 +0200848 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100849}
Tom Herbert2db54b42017-12-04 10:31:42 -0800850
851/**
852 * rhashtable_walk_next - Return the next object and advance the iterator
853 * @iter: Hash table iterator
854 *
855 * Note that you must call rhashtable_walk_stop when you are finished
856 * with the walk.
857 *
858 * Returns the next object or NULL when the end of the table is reached.
859 *
860 * Returns -EAGAIN if resize event occurred. Note that the iterator
861 * will rewind back to the beginning and you may continue to use it.
862 */
863void *rhashtable_walk_next(struct rhashtable_iter *iter)
864{
865 struct rhlist_head *list = iter->list;
866 struct rhashtable *ht = iter->ht;
867 struct rhash_head *p = iter->p;
868 bool rhlist = ht->rhlist;
869
870 if (p) {
871 if (!rhlist || !(list = rcu_dereference(list->next))) {
872 p = rcu_dereference(p->next);
873 list = container_of(p, struct rhlist_head, rhead);
874 }
875 if (!rht_is_a_nulls(p)) {
876 iter->skip++;
877 iter->p = p;
878 iter->list = list;
879 return rht_obj(ht, rhlist ? &list->rhead : p);
880 }
881
882 /* At the end of this slot, switch to next one and then find
883 * next entry from that point.
884 */
885 iter->skip = 0;
886 iter->slot++;
887 }
888
889 return __rhashtable_walk_find_next(iter);
890}
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100891EXPORT_SYMBOL_GPL(rhashtable_walk_next);
892
893/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800894 * rhashtable_walk_peek - Return the next object but don't advance the iterator
895 * @iter: Hash table iterator
896 *
897 * Returns the next object or NULL when the end of the table is reached.
898 *
899 * Returns -EAGAIN if resize event occurred. Note that the iterator
900 * will rewind back to the beginning and you may continue to use it.
901 */
902void *rhashtable_walk_peek(struct rhashtable_iter *iter)
903{
904 struct rhlist_head *list = iter->list;
905 struct rhashtable *ht = iter->ht;
906 struct rhash_head *p = iter->p;
907
908 if (p)
909 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
910
911 /* No object found in current iter, find next one in the table. */
912
913 if (iter->skip) {
914 /* A nonzero skip value points to the next entry in the table
915 * beyond that last one that was found. Decrement skip so
916 * we find the current value. __rhashtable_walk_find_next
917 * will restore the original value of skip assuming that
918 * the table hasn't changed.
919 */
920 iter->skip--;
921 }
922
923 return __rhashtable_walk_find_next(iter);
924}
925EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
926
927/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100928 * rhashtable_walk_stop - Finish a hash table walk
929 * @iter: Hash table iterator
930 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200931 * Finish a hash table walk. Does not reset the iterator to the start of the
932 * hash table.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100933 */
934void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100935 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100936{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100937 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800938 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100939
Herbert Xueddee5ba2015-03-14 13:57:20 +1100940 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100941 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100942
943 ht = iter->ht;
944
Herbert Xuba7c95e2015-03-24 09:53:17 +1100945 spin_lock(&ht->lock);
NeilBrown4feb7c72019-03-21 14:42:40 +1100946 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
947 /* This bucket table is being freed, don't re-link it. */
Herbert Xu246779d2016-08-18 16:50:56 +0800948 iter->walker.tbl = NULL;
NeilBrown4feb7c72019-03-21 14:42:40 +1100949 else
950 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xuba7c95e2015-03-24 09:53:17 +1100951 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100952
Herbert Xu963ecbd2015-03-15 21:12:04 +1100953out:
954 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100955}
956EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
957
Herbert Xu488fb86e2015-03-20 21:56:59 +1100958static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200959{
Davidlohr Bueso107d01f2018-07-16 13:26:13 -0700960 size_t retsize;
961
962 if (params->nelem_hint)
963 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
964 (unsigned long)params->min_size);
965 else
966 retsize = max(HASH_DEFAULT_SIZE,
967 (unsigned long)params->min_size);
968
969 return retsize;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200970}
971
Herbert Xu31ccde22015-03-24 00:50:21 +1100972static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
973{
974 return jhash2(key, length, seed);
975}
976
Thomas Graf7e1e7762014-08-02 11:47:44 +0200977/**
978 * rhashtable_init - initialize a new hash table
979 * @ht: hash table to be initialized
980 * @params: configuration parameters
981 *
982 * Initializes a new hash table based on the provided configuration
983 * parameters. A table can be configured either with a variable or
984 * fixed length key:
985 *
986 * Configuration Example 1: Fixed length keys
987 * struct test_obj {
988 * int key;
989 * void * my_member;
990 * struct rhash_head node;
991 * };
992 *
993 * struct rhashtable_params params = {
994 * .head_offset = offsetof(struct test_obj, node),
995 * .key_offset = offsetof(struct test_obj, key),
996 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100997 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200998 * };
999 *
1000 * Configuration Example 2: Variable length keys
1001 * struct test_obj {
1002 * [...]
1003 * struct rhash_head node;
1004 * };
1005 *
Patrick McHardy49f7b332015-03-25 13:07:45 +00001006 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001007 * {
1008 * struct test_obj *obj = data;
1009 *
1010 * return [... hash ...];
1011 * }
1012 *
1013 * struct rhashtable_params params = {
1014 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001015 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001016 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001017 * };
1018 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001019int rhashtable_init(struct rhashtable *ht,
1020 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001021{
1022 struct bucket_table *tbl;
1023 size_t size;
1024
Herbert Xu31ccde22015-03-24 00:50:21 +11001025 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001026 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001027 return -EINVAL;
1028
Thomas Graf97defe12015-01-02 23:00:20 +01001029 memset(ht, 0, sizeof(*ht));
1030 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001031 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001032 memcpy(&ht->p, params, sizeof(*params));
1033
Thomas Grafa998f712015-03-19 22:31:13 +00001034 if (params->min_size)
1035 ht->p.min_size = roundup_pow_of_two(params->min_size);
1036
Herbert Xu6d684e52017-04-27 13:44:51 +08001037 /* Cap total entries at 2^31 to avoid nelems overflow. */
1038 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001039
1040 if (params->max_size) {
1041 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1042 if (ht->p.max_size < ht->max_elems / 2)
1043 ht->max_elems = ht->p.max_size * 2;
1044 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001045
Florian Westphal48e75b432017-05-01 22:18:01 +02001046 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001047
Davidlohr Bueso107d01f2018-07-16 13:26:13 -07001048 size = rounded_hashtable_size(&ht->p);
Herbert Xu3a324602015-12-16 18:13:14 +08001049
Herbert Xu31ccde22015-03-24 00:50:21 +11001050 ht->key_len = ht->p.key_len;
1051 if (!params->hashfn) {
1052 ht->p.hashfn = jhash;
1053
1054 if (!(ht->key_len & (sizeof(u32) - 1))) {
1055 ht->key_len /= sizeof(u32);
1056 ht->p.hashfn = rhashtable_jhash2;
1057 }
1058 }
1059
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001060 /*
1061 * This is api initialization and thus we need to guarantee the
1062 * initial rhashtable allocation. Upon failure, retry with the
1063 * smallest possible size with __GFP_NOFAIL semantics.
1064 */
Herbert Xub9ecfda2015-03-24 00:50:27 +11001065 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001066 if (unlikely(tbl == NULL)) {
1067 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1068 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1069 }
Thomas Graf7e1e7762014-08-02 11:47:44 +02001070
Ying Xue545a1482015-01-07 13:41:57 +08001071 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001072
Thomas Graf7e1e7762014-08-02 11:47:44 +02001073 RCU_INIT_POINTER(ht->tbl, tbl);
1074
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001075 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001076
Thomas Graf7e1e7762014-08-02 11:47:44 +02001077 return 0;
1078}
1079EXPORT_SYMBOL_GPL(rhashtable_init);
1080
1081/**
Herbert Xuca268932016-09-19 19:00:09 +08001082 * rhltable_init - initialize a new hash list table
1083 * @hlt: hash list table to be initialized
1084 * @params: configuration parameters
1085 *
1086 * Initializes a new hash list table.
1087 *
1088 * See documentation for rhashtable_init.
1089 */
1090int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1091{
1092 int err;
1093
Herbert Xuca268932016-09-19 19:00:09 +08001094 err = rhashtable_init(&hlt->ht, params);
1095 hlt->ht.rhlist = true;
1096 return err;
1097}
1098EXPORT_SYMBOL_GPL(rhltable_init);
1099
1100static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1101 void (*free_fn)(void *ptr, void *arg),
1102 void *arg)
1103{
1104 struct rhlist_head *list;
1105
1106 if (!ht->rhlist) {
1107 free_fn(rht_obj(ht, obj), arg);
1108 return;
1109 }
1110
1111 list = container_of(obj, struct rhlist_head, rhead);
1112 do {
1113 obj = &list->rhead;
1114 list = rht_dereference(list->next, ht);
1115 free_fn(rht_obj(ht, obj), arg);
1116 } while (list);
1117}
1118
1119/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001120 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001121 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001122 * @free_fn: callback to release resources of element
1123 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001124 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001125 * Stops an eventual async resize. If defined, invokes free_fn for each
1126 * element to releasal resources. Please note that RCU protected
1127 * readers may still be accessing the elements. Releasing of resources
1128 * must occur in a compatible manner. Then frees the bucket array.
1129 *
1130 * This function will eventually sleep to wait for an async resize
1131 * to complete. The caller is responsible that no further write operations
1132 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001133 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001134void rhashtable_free_and_destroy(struct rhashtable *ht,
1135 void (*free_fn)(void *ptr, void *arg),
1136 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001137{
Taehee Yoo00261292018-07-08 11:55:51 +09001138 struct bucket_table *tbl, *next_tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001139 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001140
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001141 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001142
Thomas Graf97defe12015-01-02 23:00:20 +01001143 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001144 tbl = rht_dereference(ht->tbl, ht);
Taehee Yoo00261292018-07-08 11:55:51 +09001145restart:
Thomas Graf6b6f3022015-03-24 14:18:20 +01001146 if (free_fn) {
1147 for (i = 0; i < tbl->size; i++) {
1148 struct rhash_head *pos, *next;
1149
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001150 cond_resched();
NeilBrownadc6a3a2019-04-12 11:52:08 +10001151 for (pos = rht_ptr_exclusive(rht_bucket(tbl, i)),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001152 next = !rht_is_a_nulls(pos) ?
1153 rht_dereference(pos->next, ht) : NULL;
1154 !rht_is_a_nulls(pos);
1155 pos = next,
1156 next = !rht_is_a_nulls(pos) ?
1157 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001158 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001159 }
1160 }
1161
Taehee Yoo00261292018-07-08 11:55:51 +09001162 next_tbl = rht_dereference(tbl->future_tbl, ht);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001163 bucket_table_free(tbl);
Taehee Yoo00261292018-07-08 11:55:51 +09001164 if (next_tbl) {
1165 tbl = next_tbl;
1166 goto restart;
1167 }
Thomas Graf97defe12015-01-02 23:00:20 +01001168 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001169}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001170EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1171
1172void rhashtable_destroy(struct rhashtable *ht)
1173{
1174 return rhashtable_free_and_destroy(ht, NULL, NULL);
1175}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001176EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001177
Herbert Xuce9b3622020-07-24 20:14:34 +10001178struct rhash_lock_head __rcu **__rht_bucket_nested(
1179 const struct bucket_table *tbl, unsigned int hash)
Herbert Xuda204202017-02-11 19:26:47 +08001180{
1181 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
Herbert Xuda204202017-02-11 19:26:47 +08001182 unsigned int index = hash & ((1 << tbl->nest) - 1);
1183 unsigned int size = tbl->size >> tbl->nest;
1184 unsigned int subhash = hash;
1185 union nested_table *ntbl;
1186
Herbert Xu4a3084a2020-06-03 18:12:43 +10001187 ntbl = nested_table_top(tbl);
Herbert Xuc4d26032017-02-25 22:39:50 +08001188 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001189 subhash >>= tbl->nest;
1190
1191 while (ntbl && size > (1 << shift)) {
1192 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001193 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1194 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001195 size >>= shift;
1196 subhash >>= shift;
1197 }
1198
NeilBrownff302db2019-04-02 10:07:45 +11001199 if (!ntbl)
1200 return NULL;
Herbert Xuda204202017-02-11 19:26:47 +08001201
1202 return &ntbl[subhash].bucket;
1203
1204}
NeilBrownff302db2019-04-02 10:07:45 +11001205EXPORT_SYMBOL_GPL(__rht_bucket_nested);
1206
Herbert Xuce9b3622020-07-24 20:14:34 +10001207struct rhash_lock_head __rcu **rht_bucket_nested(
1208 const struct bucket_table *tbl, unsigned int hash)
NeilBrownff302db2019-04-02 10:07:45 +11001209{
Herbert Xuce9b3622020-07-24 20:14:34 +10001210 static struct rhash_lock_head __rcu *rhnull;
NeilBrownff302db2019-04-02 10:07:45 +11001211
1212 if (!rhnull)
1213 INIT_RHT_NULLS_HEAD(rhnull);
1214 return __rht_bucket_nested(tbl, hash) ?: &rhnull;
1215}
Herbert Xuda204202017-02-11 19:26:47 +08001216EXPORT_SYMBOL_GPL(rht_bucket_nested);
1217
Herbert Xuce9b3622020-07-24 20:14:34 +10001218struct rhash_lock_head __rcu **rht_bucket_nested_insert(
1219 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
Herbert Xuda204202017-02-11 19:26:47 +08001220{
1221 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1222 unsigned int index = hash & ((1 << tbl->nest) - 1);
1223 unsigned int size = tbl->size >> tbl->nest;
1224 union nested_table *ntbl;
Herbert Xuda204202017-02-11 19:26:47 +08001225
Herbert Xu4a3084a2020-06-03 18:12:43 +10001226 ntbl = nested_table_top(tbl);
Herbert Xuda204202017-02-11 19:26:47 +08001227 hash >>= tbl->nest;
Herbert Xuda204202017-02-11 19:26:47 +08001228 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001229 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001230
1231 while (ntbl && size > (1 << shift)) {
1232 index = hash & ((1 << shift) - 1);
1233 size >>= shift;
1234 hash >>= shift;
Herbert Xuda204202017-02-11 19:26:47 +08001235 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001236 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001237 }
1238
1239 if (!ntbl)
1240 return NULL;
1241
1242 return &ntbl[hash].bucket;
1243
1244}
1245EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);