blob: 6529fe1b45c13421d69665326ba620e7c39d01e0 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Herbert Xu02fd97c2015-03-20 21:57:00 +11004 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafa5ec68e2015-02-05 02:03:32 +01005 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02006 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02008 * Code partially derived from nft_hash
Herbert Xu02fd97c2015-03-20 21:57:00 +11009 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Herbert Xu07ee0722015-05-15 11:30:47 +080017#include <linux/atomic.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020018#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/log2.h>
Eric Dumazet5beb5c92015-02-26 07:20:34 -080021#include <linux/sched.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010022#include <linux/rculist.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020023#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010026#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020027#include <linux/random.h>
28#include <linux/rhashtable.h>
Stephen Rothwell61d7b092015-02-09 14:04:03 +110029#include <linux/err.h>
Hauke Mehrtens6d795412015-06-06 22:07:23 +020030#include <linux/export.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020031
32#define HASH_DEFAULT_SIZE 64UL
Herbert Xuc2e213c2015-03-18 20:01:16 +110033#define HASH_MIN_SIZE 4U
Thomas Graf97defe12015-01-02 23:00:20 +010034
Herbert Xuda204202017-02-11 19:26:47 +080035union nested_table {
36 union nested_table __rcu *table;
NeilBrown8f0db012019-04-02 10:07:45 +110037 struct rhash_lock_head __rcu *bucket;
Herbert Xuda204202017-02-11 19:26:47 +080038};
39
Herbert Xu988dfbd2015-03-10 09:27:55 +110040static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010041 const struct bucket_table *tbl,
42 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020043{
Herbert Xu02fd97c2015-03-20 21:57:00 +110044 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020045}
46
Thomas Grafa03eaec2015-02-05 02:03:34 +010047#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010048#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010049
50int lockdep_rht_mutex_is_held(struct rhashtable *ht)
51{
52 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
53}
54EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
55
56int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
57{
NeilBrown8f0db012019-04-02 10:07:45 +110058 if (!debug_locks)
59 return 1;
60 if (unlikely(tbl->nest))
61 return 1;
NeilBrownca0b7092019-04-12 11:52:08 +100062 return bit_spin_is_locked(0, (unsigned long *)&tbl->buckets[hash]);
Thomas Grafa03eaec2015-02-05 02:03:34 +010063}
64EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
65#else
66#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +010067#endif
68
Herbert Xuda204202017-02-11 19:26:47 +080069static void nested_table_free(union nested_table *ntbl, unsigned int size)
70{
71 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
72 const unsigned int len = 1 << shift;
73 unsigned int i;
74
75 ntbl = rcu_dereference_raw(ntbl->table);
76 if (!ntbl)
77 return;
78
79 if (size > len) {
80 size >>= shift;
81 for (i = 0; i < len; i++)
82 nested_table_free(ntbl + i, size);
83 }
84
85 kfree(ntbl);
86}
87
88static void nested_bucket_table_free(const struct bucket_table *tbl)
89{
90 unsigned int size = tbl->size >> tbl->nest;
91 unsigned int len = 1 << tbl->nest;
92 union nested_table *ntbl;
93 unsigned int i;
94
95 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
96
97 for (i = 0; i < len; i++)
98 nested_table_free(ntbl + i, size);
99
100 kfree(ntbl);
101}
102
Thomas Graf97defe12015-01-02 23:00:20 +0100103static void bucket_table_free(const struct bucket_table *tbl)
104{
Herbert Xuda204202017-02-11 19:26:47 +0800105 if (tbl->nest)
106 nested_bucket_table_free(tbl);
107
Thomas Graf97defe12015-01-02 23:00:20 +0100108 kvfree(tbl);
109}
110
Herbert Xu9d901bc2015-03-14 13:57:23 +1100111static void bucket_table_free_rcu(struct rcu_head *head)
112{
113 bucket_table_free(container_of(head, struct bucket_table, rcu));
114}
115
Herbert Xuda204202017-02-11 19:26:47 +0800116static union nested_table *nested_table_alloc(struct rhashtable *ht,
117 union nested_table __rcu **prev,
NeilBrown5af68ef2018-06-18 12:52:50 +1000118 bool leaf)
Herbert Xuda204202017-02-11 19:26:47 +0800119{
120 union nested_table *ntbl;
121 int i;
122
123 ntbl = rcu_dereference(*prev);
124 if (ntbl)
125 return ntbl;
126
127 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
128
NeilBrown5af68ef2018-06-18 12:52:50 +1000129 if (ntbl && leaf) {
130 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000131 INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
Herbert Xuda204202017-02-11 19:26:47 +0800132 }
133
NeilBrown7a41c292019-04-02 10:07:45 +1100134 if (cmpxchg(prev, NULL, ntbl) == NULL)
135 return ntbl;
136 /* Raced with another thread. */
137 kfree(ntbl);
138 return rcu_dereference(*prev);
Herbert Xuda204202017-02-11 19:26:47 +0800139}
140
141static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
142 size_t nbuckets,
143 gfp_t gfp)
144{
145 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
146 struct bucket_table *tbl;
147 size_t size;
148
149 if (nbuckets < (1 << (shift + 1)))
150 return NULL;
151
152 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
153
154 tbl = kzalloc(size, gfp);
155 if (!tbl)
156 return NULL;
157
158 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
NeilBrown5af68ef2018-06-18 12:52:50 +1000159 false)) {
Herbert Xuda204202017-02-11 19:26:47 +0800160 kfree(tbl);
161 return NULL;
162 }
163
164 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
165
166 return tbl;
167}
168
Thomas Graf97defe12015-01-02 23:00:20 +0100169static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xub9ecfda2015-03-24 00:50:27 +1100170 size_t nbuckets,
171 gfp_t gfp)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200172{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100173 struct bucket_table *tbl = NULL;
NeilBrown8f0db012019-04-02 10:07:45 +1100174 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100175 int i;
NeilBrown149212f2019-04-02 10:07:45 +1100176 static struct lock_class_key __key;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200177
Gustavo A. R. Silvac252aa32019-04-11 18:43:06 -0500178 tbl = kvzalloc(struct_size(tbl, buckets, nbuckets), gfp);
Herbert Xuda204202017-02-11 19:26:47 +0800179
180 size = nbuckets;
181
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700182 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
Herbert Xuda204202017-02-11 19:26:47 +0800183 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
184 nbuckets = 0;
185 }
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700186
Thomas Graf7e1e7762014-08-02 11:47:44 +0200187 if (tbl == NULL)
188 return NULL;
189
NeilBrown149212f2019-04-02 10:07:45 +1100190 lockdep_init_map(&tbl->dep_map, "rhashtable_bucket", &__key, 0);
191
Herbert Xuda204202017-02-11 19:26:47 +0800192 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200193
NeilBrown4feb7c72019-03-21 14:42:40 +1100194 rcu_head_init(&tbl->rcu);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100195 INIT_LIST_HEAD(&tbl->walkers);
196
Jason A. Donenfeldd48ad082017-06-07 22:47:13 -0400197 tbl->hash_rnd = get_random_u32();
Herbert Xu5269b532015-03-14 13:57:22 +1100198
Thomas Graff89bd6f2015-01-02 23:00:21 +0100199 for (i = 0; i < nbuckets; i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000200 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100201
Thomas Graf97defe12015-01-02 23:00:20 +0100202 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200203}
204
Herbert Xub8244782015-03-24 00:50:26 +1100205static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
206 struct bucket_table *tbl)
207{
208 struct bucket_table *new_tbl;
209
210 do {
211 new_tbl = tbl;
212 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
213 } while (tbl);
214
215 return new_tbl;
216}
217
NeilBrown8f0db012019-04-02 10:07:45 +1100218static int rhashtable_rehash_one(struct rhashtable *ht,
219 struct rhash_lock_head __rcu **bkt,
220 unsigned int old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100221{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100222 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
NeilBrownc0690012018-06-18 12:52:50 +1000223 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
Herbert Xuda204202017-02-11 19:26:47 +0800224 int err = -EAGAIN;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100225 struct rhash_head *head, *next, *entry;
NeilBrowne4edbe32019-04-12 11:52:07 +1000226 struct rhash_head __rcu **pprev = NULL;
Thomas Graf299e5c32015-03-24 14:18:17 +0100227 unsigned int new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100228
Herbert Xuda204202017-02-11 19:26:47 +0800229 if (new_tbl->nest)
230 goto out;
231
232 err = -ENOENT;
233
NeilBrownadc6a3a2019-04-12 11:52:08 +1000234 rht_for_each_from(entry, rht_ptr(bkt, old_tbl, old_hash),
235 old_tbl, old_hash) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100236 err = 0;
237 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100238
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100239 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200240 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100241
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100242 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200243 }
244
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100245 if (err)
246 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100247
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100248 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100249
NeilBrown149212f2019-04-02 10:07:45 +1100250 rht_lock_nested(new_tbl, &new_tbl->buckets[new_hash], SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100251
NeilBrownadc6a3a2019-04-12 11:52:08 +1000252 head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100253
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200254 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100255
NeilBrown149212f2019-04-02 10:07:45 +1100256 rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100257
NeilBrown8f0db012019-04-02 10:07:45 +1100258 if (pprev)
259 rcu_assign_pointer(*pprev, next);
260 else
261 /* Need to preserved the bit lock. */
NeilBrownf4712b42019-04-12 11:52:08 +1000262 rht_assign_locked(bkt, next);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100263
264out:
265 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100266}
267
Herbert Xuda204202017-02-11 19:26:47 +0800268static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100269 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100270{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100271 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
NeilBrown8f0db012019-04-02 10:07:45 +1100272 struct rhash_lock_head __rcu **bkt = rht_bucket_var(old_tbl, old_hash);
Herbert Xuda204202017-02-11 19:26:47 +0800273 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100274
NeilBrown8f0db012019-04-02 10:07:45 +1100275 if (!bkt)
276 return 0;
NeilBrown149212f2019-04-02 10:07:45 +1100277 rht_lock(old_tbl, bkt);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100278
NeilBrown8f0db012019-04-02 10:07:45 +1100279 while (!(err = rhashtable_rehash_one(ht, bkt, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100280 ;
Herbert Xuda204202017-02-11 19:26:47 +0800281
NeilBrown4feb7c72019-03-21 14:42:40 +1100282 if (err == -ENOENT)
Herbert Xuda204202017-02-11 19:26:47 +0800283 err = 0;
NeilBrown149212f2019-04-02 10:07:45 +1100284 rht_unlock(old_tbl, bkt);
Herbert Xuda204202017-02-11 19:26:47 +0800285
286 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100287}
288
Herbert Xub8244782015-03-24 00:50:26 +1100289static int rhashtable_rehash_attach(struct rhashtable *ht,
290 struct bucket_table *old_tbl,
291 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100292{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100293 /* Make insertions go into the new, empty table right away. Deletions
294 * and lookups will be attempted in both tables until we synchronize.
NeilBrown0ad66442018-06-18 12:52:50 +1000295 * As cmpxchg() provides strong barriers, we do not need
296 * rcu_assign_pointer().
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100297 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100298
NeilBrown0ad66442018-06-18 12:52:50 +1000299 if (cmpxchg(&old_tbl->future_tbl, NULL, new_tbl) != NULL)
300 return -EEXIST;
Herbert Xub8244782015-03-24 00:50:26 +1100301
302 return 0;
303}
304
305static int rhashtable_rehash_table(struct rhashtable *ht)
306{
307 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
308 struct bucket_table *new_tbl;
309 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100310 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800311 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100312
313 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
314 if (!new_tbl)
315 return 0;
316
Herbert Xuda204202017-02-11 19:26:47 +0800317 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
318 err = rhashtable_rehash_chain(ht, old_hash);
319 if (err)
320 return err;
Eric Dumazetae6da1f2018-03-31 12:58:48 -0700321 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +0800322 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100323
324 /* Publish the new table pointer. */
325 rcu_assign_pointer(ht->tbl, new_tbl);
326
Herbert Xuba7c95e2015-03-24 09:53:17 +1100327 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100328 list_for_each_entry(walker, &old_tbl->walkers, list)
329 walker->tbl = NULL;
330
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100331 /* Wait for readers. All new readers will see the new
332 * table, and thus no references to the old table will
333 * remain.
NeilBrown4feb7c72019-03-21 14:42:40 +1100334 * We do this inside the locked region so that
335 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
336 * to check if it should not re-link the table.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100337 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100338 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
NeilBrown4feb7c72019-03-21 14:42:40 +1100339 spin_unlock(&ht->lock);
Herbert Xub8244782015-03-24 00:50:26 +1100340
341 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200342}
343
Herbert Xuda204202017-02-11 19:26:47 +0800344static int rhashtable_rehash_alloc(struct rhashtable *ht,
345 struct bucket_table *old_tbl,
346 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200347{
Herbert Xuda204202017-02-11 19:26:47 +0800348 struct bucket_table *new_tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100349 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200350
351 ASSERT_RHT_MUTEX(ht);
352
Herbert Xuda204202017-02-11 19:26:47 +0800353 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200354 if (new_tbl == NULL)
355 return -ENOMEM;
356
Herbert Xub8244782015-03-24 00:50:26 +1100357 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
358 if (err)
359 bucket_table_free(new_tbl);
360
361 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200362}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200363
364/**
365 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
366 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200367 *
Herbert Xu18093d12015-03-24 00:50:25 +1100368 * This function shrinks the hash table to fit, i.e., the smallest
369 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200370 *
Thomas Graf97defe12015-01-02 23:00:20 +0100371 * The caller must ensure that no concurrent resizing occurs by holding
372 * ht->mutex.
373 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200374 * The caller must ensure that no concurrent table mutations take place.
375 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100376 *
377 * It is valid to have concurrent insertions and deletions protected by per
378 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200379 */
Herbert Xub8244782015-03-24 00:50:26 +1100380static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200381{
Herbert Xuda204202017-02-11 19:26:47 +0800382 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200383 unsigned int nelems = atomic_read(&ht->nelems);
384 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200385
Vegard Nossum12311952016-08-12 20:10:44 +0200386 if (nelems)
387 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100388 if (size < ht->p.min_size)
389 size = ht->p.min_size;
390
391 if (old_tbl->size <= size)
392 return 0;
393
Herbert Xub8244782015-03-24 00:50:26 +1100394 if (rht_dereference(old_tbl->future_tbl, ht))
395 return -EEXIST;
396
Herbert Xuda204202017-02-11 19:26:47 +0800397 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200398}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200399
Thomas Graf97defe12015-01-02 23:00:20 +0100400static void rht_deferred_worker(struct work_struct *work)
401{
402 struct rhashtable *ht;
403 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100404 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100405
Ying Xue57699a42015-01-16 11:13:09 +0800406 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100407 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100408
Thomas Graf97defe12015-01-02 23:00:20 +0100409 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100410 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100411
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100412 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800413 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000414 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800415 err = rhashtable_shrink(ht);
416 else if (tbl->nest)
417 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub8244782015-03-24 00:50:26 +1100418
Herbert Xu408f13e2019-03-21 09:39:52 +0800419 if (!err || err == -EEXIST) {
420 int nerr;
421
422 nerr = rhashtable_rehash_table(ht);
423 err = err ?: nerr;
424 }
Herbert Xub8244782015-03-24 00:50:26 +1100425
Thomas Graf97defe12015-01-02 23:00:20 +0100426 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100427
428 if (err)
429 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100430}
431
Herbert Xuca268932016-09-19 19:00:09 +0800432static int rhashtable_insert_rehash(struct rhashtable *ht,
433 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100434{
435 struct bucket_table *old_tbl;
436 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100437 unsigned int size;
438 int err;
439
440 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100441
442 size = tbl->size;
443
Herbert Xu3cf92222015-12-03 20:41:29 +0800444 err = -EBUSY;
445
Herbert Xuccd57b12015-03-24 00:50:28 +1100446 if (rht_grow_above_75(ht, tbl))
447 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200448 /* Do not schedule more than one rehash */
449 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800450 goto fail;
451
452 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100453
Davidlohr Bueso93f976b2018-08-21 22:01:45 -0700454 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
Herbert Xu3cf92222015-12-03 20:41:29 +0800455 if (new_tbl == NULL)
456 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100457
458 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
459 if (err) {
460 bucket_table_free(new_tbl);
461 if (err == -EEXIST)
462 err = 0;
463 } else
464 schedule_work(&ht->run_work);
465
466 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800467
468fail:
469 /* Do not fail the insert if someone else did a rehash. */
NeilBrownc0690012018-06-18 12:52:50 +1000470 if (likely(rcu_access_pointer(tbl->future_tbl)))
Herbert Xu3cf92222015-12-03 20:41:29 +0800471 return 0;
472
473 /* Schedule async rehash to retry allocation in process context. */
474 if (err == -ENOMEM)
475 schedule_work(&ht->run_work);
476
477 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100478}
Herbert Xuccd57b12015-03-24 00:50:28 +1100479
Herbert Xuca268932016-09-19 19:00:09 +0800480static void *rhashtable_lookup_one(struct rhashtable *ht,
NeilBrown8f0db012019-04-02 10:07:45 +1100481 struct rhash_lock_head __rcu **bkt,
Herbert Xuca268932016-09-19 19:00:09 +0800482 struct bucket_table *tbl, unsigned int hash,
483 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100484{
Herbert Xuca268932016-09-19 19:00:09 +0800485 struct rhashtable_compare_arg arg = {
486 .ht = ht,
487 .key = key,
488 };
NeilBrowne4edbe32019-04-12 11:52:07 +1000489 struct rhash_head __rcu **pprev = NULL;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100490 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800491 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100492
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200493 elasticity = RHT_ELASTICITY;
NeilBrownadc6a3a2019-04-12 11:52:08 +1000494 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800495 struct rhlist_head *list;
496 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100497
Herbert Xuca268932016-09-19 19:00:09 +0800498 elasticity--;
499 if (!key ||
500 (ht->p.obj_cmpfn ?
501 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200502 rhashtable_compare(&arg, rht_obj(ht, head)))) {
503 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800504 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200505 }
Herbert Xuca268932016-09-19 19:00:09 +0800506
507 if (!ht->rhlist)
508 return rht_obj(ht, head);
509
510 list = container_of(obj, struct rhlist_head, rhead);
511 plist = container_of(head, struct rhlist_head, rhead);
512
513 RCU_INIT_POINTER(list->next, plist);
514 head = rht_dereference_bucket(head->next, tbl, hash);
515 RCU_INIT_POINTER(list->rhead.next, head);
NeilBrown8f0db012019-04-02 10:07:45 +1100516 if (pprev)
517 rcu_assign_pointer(*pprev, obj);
518 else
519 /* Need to preserve the bit lock */
NeilBrownf4712b42019-04-12 11:52:08 +1000520 rht_assign_locked(bkt, obj);
Herbert Xuca268932016-09-19 19:00:09 +0800521
522 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200523 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100524
Herbert Xuca268932016-09-19 19:00:09 +0800525 if (elasticity <= 0)
526 return ERR_PTR(-EAGAIN);
527
528 return ERR_PTR(-ENOENT);
529}
530
531static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
NeilBrown8f0db012019-04-02 10:07:45 +1100532 struct rhash_lock_head __rcu **bkt,
Herbert Xuca268932016-09-19 19:00:09 +0800533 struct bucket_table *tbl,
534 unsigned int hash,
535 struct rhash_head *obj,
536 void *data)
537{
538 struct bucket_table *new_tbl;
539 struct rhash_head *head;
540
541 if (!IS_ERR_OR_NULL(data))
542 return ERR_PTR(-EEXIST);
543
544 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
545 return ERR_CAST(data);
546
NeilBrownc0690012018-06-18 12:52:50 +1000547 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800548 if (new_tbl)
549 return new_tbl;
550
551 if (PTR_ERR(data) != -ENOENT)
552 return ERR_CAST(data);
553
Herbert Xu07ee0722015-05-15 11:30:47 +0800554 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800555 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800556
Herbert Xuca268932016-09-19 19:00:09 +0800557 if (unlikely(rht_grow_above_100(ht, tbl)))
558 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100559
NeilBrownadc6a3a2019-04-12 11:52:08 +1000560 head = rht_ptr(bkt, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100561
562 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800563 if (ht->rhlist) {
564 struct rhlist_head *list;
565
566 list = container_of(obj, struct rhlist_head, rhead);
567 RCU_INIT_POINTER(list->next, NULL);
568 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100569
NeilBrown8f0db012019-04-02 10:07:45 +1100570 /* bkt is always the head of the list, so it holds
571 * the lock, which we need to preserve
572 */
NeilBrownf4712b42019-04-12 11:52:08 +1000573 rht_assign_locked(bkt, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100574
575 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800576 if (rht_grow_above_75(ht, tbl))
577 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100578
Herbert Xuca268932016-09-19 19:00:09 +0800579 return NULL;
580}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100581
Herbert Xuca268932016-09-19 19:00:09 +0800582static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
583 struct rhash_head *obj)
584{
585 struct bucket_table *new_tbl;
586 struct bucket_table *tbl;
NeilBrown8f0db012019-04-02 10:07:45 +1100587 struct rhash_lock_head __rcu **bkt;
Herbert Xuca268932016-09-19 19:00:09 +0800588 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800589 void *data;
590
NeilBrown4feb7c72019-03-21 14:42:40 +1100591 new_tbl = rcu_dereference(ht->tbl);
Herbert Xuca268932016-09-19 19:00:09 +0800592
NeilBrown4feb7c72019-03-21 14:42:40 +1100593 do {
Herbert Xuca268932016-09-19 19:00:09 +0800594 tbl = new_tbl;
595 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
NeilBrown8f0db012019-04-02 10:07:45 +1100596 if (rcu_access_pointer(tbl->future_tbl))
597 /* Failure is OK */
598 bkt = rht_bucket_var(tbl, hash);
599 else
600 bkt = rht_bucket_insert(ht, tbl, hash);
601 if (bkt == NULL) {
602 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
603 data = ERR_PTR(-EAGAIN);
604 } else {
NeilBrown149212f2019-04-02 10:07:45 +1100605 rht_lock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100606 data = rhashtable_lookup_one(ht, bkt, tbl,
607 hash, key, obj);
608 new_tbl = rhashtable_insert_one(ht, bkt, tbl,
609 hash, obj, data);
610 if (PTR_ERR(new_tbl) != -EEXIST)
611 data = ERR_CAST(new_tbl);
Herbert Xuca268932016-09-19 19:00:09 +0800612
NeilBrown149212f2019-04-02 10:07:45 +1100613 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100614 }
NeilBrown4feb7c72019-03-21 14:42:40 +1100615 } while (!IS_ERR_OR_NULL(new_tbl));
Herbert Xuca268932016-09-19 19:00:09 +0800616
617 if (PTR_ERR(data) == -EAGAIN)
618 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
619 -EAGAIN);
620
621 return data;
622}
623
624void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
625 struct rhash_head *obj)
626{
627 void *data;
628
629 do {
630 rcu_read_lock();
631 data = rhashtable_try_insert(ht, key, obj);
632 rcu_read_unlock();
633 } while (PTR_ERR(data) == -EAGAIN);
634
635 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100636}
637EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
638
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100639/**
Herbert Xu246779d2016-08-18 16:50:56 +0800640 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100641 * @ht: Table to walk over
642 * @iter: Hash table Iterator
643 *
644 * This function prepares a hash table walk.
645 *
646 * Note that if you restart a walk after rhashtable_walk_stop you
647 * may see the same object twice. Also, you may miss objects if
648 * there are removals in between rhashtable_walk_stop and the next
649 * call to rhashtable_walk_start.
650 *
651 * For a completely stable walk you should construct your own data
652 * structure outside the hash table.
653 *
NeilBrown82266e92018-04-24 08:29:13 +1000654 * This function may be called from any process context, including
655 * non-preemptable context, but cannot be called from softirq or
656 * hardirq context.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100657 *
Herbert Xu246779d2016-08-18 16:50:56 +0800658 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100659 */
Herbert Xu246779d2016-08-18 16:50:56 +0800660void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100661{
662 iter->ht = ht;
663 iter->p = NULL;
664 iter->slot = 0;
665 iter->skip = 0;
Tom Herbert2db54b42017-12-04 10:31:42 -0800666 iter->end_of_table = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100667
Herbert Xuc6ff5262015-12-16 16:45:54 +0800668 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800669 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800670 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800671 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800672 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100673}
Herbert Xu246779d2016-08-18 16:50:56 +0800674EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100675
676/**
677 * rhashtable_walk_exit - Free an iterator
678 * @iter: Hash table Iterator
679 *
Herbert Xu6c4128f2019-02-14 22:03:27 +0800680 * This function frees resources allocated by rhashtable_walk_enter.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100681 */
682void rhashtable_walk_exit(struct rhashtable_iter *iter)
683{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800684 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800685 if (iter->walker.tbl)
686 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800687 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100688}
689EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
690
691/**
Tom Herbert97a6ec42017-12-04 10:31:41 -0800692 * rhashtable_walk_start_check - Start a hash table walk
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100693 * @iter: Hash table iterator
694 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200695 * Start a hash table walk at the current iterator position. Note that we take
696 * the RCU lock in all cases including when we return an error. So you must
697 * always call rhashtable_walk_stop to clean up.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100698 *
699 * Returns zero if successful.
700 *
701 * Returns -EAGAIN if resize event occured. Note that the iterator
702 * will rewind back to the beginning and you may use it immediately
703 * by calling rhashtable_walk_next.
Tom Herbert97a6ec42017-12-04 10:31:41 -0800704 *
705 * rhashtable_walk_start is defined as an inline variant that returns
706 * void. This is preferred in cases where the caller would ignore
707 * resize events and always continue.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100708 */
Tom Herbert97a6ec42017-12-04 10:31:41 -0800709int rhashtable_walk_start_check(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100710 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100711{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100712 struct rhashtable *ht = iter->ht;
NeilBrown5d240a82018-04-24 08:29:13 +1000713 bool rhlist = ht->rhlist;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100714
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100715 rcu_read_lock();
716
Herbert Xuc6ff5262015-12-16 16:45:54 +0800717 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800718 if (iter->walker.tbl)
719 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800720 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100721
NeilBrown5d240a82018-04-24 08:29:13 +1000722 if (iter->end_of_table)
723 return 0;
724 if (!iter->walker.tbl) {
Herbert Xu246779d2016-08-18 16:50:56 +0800725 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
NeilBrownb41cc042018-04-24 08:29:13 +1000726 iter->slot = 0;
727 iter->skip = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100728 return -EAGAIN;
729 }
730
NeilBrown5d240a82018-04-24 08:29:13 +1000731 if (iter->p && !rhlist) {
732 /*
733 * We need to validate that 'p' is still in the table, and
734 * if so, update 'skip'
735 */
736 struct rhash_head *p;
737 int skip = 0;
738 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
739 skip++;
740 if (p == iter->p) {
741 iter->skip = skip;
742 goto found;
743 }
744 }
745 iter->p = NULL;
746 } else if (iter->p && rhlist) {
747 /* Need to validate that 'list' is still in the table, and
748 * if so, update 'skip' and 'p'.
749 */
750 struct rhash_head *p;
751 struct rhlist_head *list;
752 int skip = 0;
753 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
754 for (list = container_of(p, struct rhlist_head, rhead);
755 list;
756 list = rcu_dereference(list->next)) {
757 skip++;
758 if (list == iter->list) {
759 iter->p = p;
Rishabh Bhatnagarc643ecf2018-07-02 09:35:34 -0700760 iter->skip = skip;
NeilBrown5d240a82018-04-24 08:29:13 +1000761 goto found;
762 }
763 }
764 }
765 iter->p = NULL;
766 }
767found:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100768 return 0;
769}
Tom Herbert97a6ec42017-12-04 10:31:41 -0800770EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100771
772/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800773 * __rhashtable_walk_find_next - Find the next element in a table (or the first
774 * one in case of a new walk).
775 *
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100776 * @iter: Hash table iterator
777 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800778 * Returns the found object or NULL when the end of the table is reached.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100779 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800780 * Returns -EAGAIN if resize event occurred.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100781 */
Tom Herbert2db54b42017-12-04 10:31:42 -0800782static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100783{
Herbert Xu246779d2016-08-18 16:50:56 +0800784 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xuca268932016-09-19 19:00:09 +0800785 struct rhlist_head *list = iter->list;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100786 struct rhashtable *ht = iter->ht;
787 struct rhash_head *p = iter->p;
Herbert Xuca268932016-09-19 19:00:09 +0800788 bool rhlist = ht->rhlist;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100789
Tom Herbert2db54b42017-12-04 10:31:42 -0800790 if (!tbl)
791 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100792
793 for (; iter->slot < tbl->size; iter->slot++) {
794 int skip = iter->skip;
795
796 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800797 if (rhlist) {
798 list = container_of(p, struct rhlist_head,
799 rhead);
800 do {
801 if (!skip)
802 goto next;
803 skip--;
804 list = rcu_dereference(list->next);
805 } while (list);
806
807 continue;
808 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100809 if (!skip)
810 break;
811 skip--;
812 }
813
814next:
815 if (!rht_is_a_nulls(p)) {
816 iter->skip++;
817 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800818 iter->list = list;
819 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100820 }
821
822 iter->skip = 0;
823 }
824
Phil Sutter142b9422015-07-06 15:51:20 +0200825 iter->p = NULL;
826
Herbert Xud88252f2015-03-24 00:50:19 +1100827 /* Ensure we see any new tables. */
828 smp_rmb();
829
Herbert Xu246779d2016-08-18 16:50:56 +0800830 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
831 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100832 iter->slot = 0;
833 iter->skip = 0;
834 return ERR_PTR(-EAGAIN);
Tom Herbert2db54b42017-12-04 10:31:42 -0800835 } else {
836 iter->end_of_table = true;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100837 }
838
Thomas Grafc936a792015-05-05 02:22:53 +0200839 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100840}
Tom Herbert2db54b42017-12-04 10:31:42 -0800841
842/**
843 * rhashtable_walk_next - Return the next object and advance the iterator
844 * @iter: Hash table iterator
845 *
846 * Note that you must call rhashtable_walk_stop when you are finished
847 * with the walk.
848 *
849 * Returns the next object or NULL when the end of the table is reached.
850 *
851 * Returns -EAGAIN if resize event occurred. Note that the iterator
852 * will rewind back to the beginning and you may continue to use it.
853 */
854void *rhashtable_walk_next(struct rhashtable_iter *iter)
855{
856 struct rhlist_head *list = iter->list;
857 struct rhashtable *ht = iter->ht;
858 struct rhash_head *p = iter->p;
859 bool rhlist = ht->rhlist;
860
861 if (p) {
862 if (!rhlist || !(list = rcu_dereference(list->next))) {
863 p = rcu_dereference(p->next);
864 list = container_of(p, struct rhlist_head, rhead);
865 }
866 if (!rht_is_a_nulls(p)) {
867 iter->skip++;
868 iter->p = p;
869 iter->list = list;
870 return rht_obj(ht, rhlist ? &list->rhead : p);
871 }
872
873 /* At the end of this slot, switch to next one and then find
874 * next entry from that point.
875 */
876 iter->skip = 0;
877 iter->slot++;
878 }
879
880 return __rhashtable_walk_find_next(iter);
881}
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100882EXPORT_SYMBOL_GPL(rhashtable_walk_next);
883
884/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800885 * rhashtable_walk_peek - Return the next object but don't advance the iterator
886 * @iter: Hash table iterator
887 *
888 * Returns the next object or NULL when the end of the table is reached.
889 *
890 * Returns -EAGAIN if resize event occurred. Note that the iterator
891 * will rewind back to the beginning and you may continue to use it.
892 */
893void *rhashtable_walk_peek(struct rhashtable_iter *iter)
894{
895 struct rhlist_head *list = iter->list;
896 struct rhashtable *ht = iter->ht;
897 struct rhash_head *p = iter->p;
898
899 if (p)
900 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
901
902 /* No object found in current iter, find next one in the table. */
903
904 if (iter->skip) {
905 /* A nonzero skip value points to the next entry in the table
906 * beyond that last one that was found. Decrement skip so
907 * we find the current value. __rhashtable_walk_find_next
908 * will restore the original value of skip assuming that
909 * the table hasn't changed.
910 */
911 iter->skip--;
912 }
913
914 return __rhashtable_walk_find_next(iter);
915}
916EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
917
918/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100919 * rhashtable_walk_stop - Finish a hash table walk
920 * @iter: Hash table iterator
921 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200922 * Finish a hash table walk. Does not reset the iterator to the start of the
923 * hash table.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100924 */
925void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100926 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100927{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100928 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800929 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100930
Herbert Xueddee5ba2015-03-14 13:57:20 +1100931 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100932 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100933
934 ht = iter->ht;
935
Herbert Xuba7c95e2015-03-24 09:53:17 +1100936 spin_lock(&ht->lock);
NeilBrown4feb7c72019-03-21 14:42:40 +1100937 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
938 /* This bucket table is being freed, don't re-link it. */
Herbert Xu246779d2016-08-18 16:50:56 +0800939 iter->walker.tbl = NULL;
NeilBrown4feb7c72019-03-21 14:42:40 +1100940 else
941 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xuba7c95e2015-03-24 09:53:17 +1100942 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100943
Herbert Xu963ecbd2015-03-15 21:12:04 +1100944out:
945 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100946}
947EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
948
Herbert Xu488fb86e2015-03-20 21:56:59 +1100949static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200950{
Davidlohr Bueso107d01f2018-07-16 13:26:13 -0700951 size_t retsize;
952
953 if (params->nelem_hint)
954 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
955 (unsigned long)params->min_size);
956 else
957 retsize = max(HASH_DEFAULT_SIZE,
958 (unsigned long)params->min_size);
959
960 return retsize;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200961}
962
Herbert Xu31ccde22015-03-24 00:50:21 +1100963static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
964{
965 return jhash2(key, length, seed);
966}
967
Thomas Graf7e1e7762014-08-02 11:47:44 +0200968/**
969 * rhashtable_init - initialize a new hash table
970 * @ht: hash table to be initialized
971 * @params: configuration parameters
972 *
973 * Initializes a new hash table based on the provided configuration
974 * parameters. A table can be configured either with a variable or
975 * fixed length key:
976 *
977 * Configuration Example 1: Fixed length keys
978 * struct test_obj {
979 * int key;
980 * void * my_member;
981 * struct rhash_head node;
982 * };
983 *
984 * struct rhashtable_params params = {
985 * .head_offset = offsetof(struct test_obj, node),
986 * .key_offset = offsetof(struct test_obj, key),
987 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100988 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200989 * };
990 *
991 * Configuration Example 2: Variable length keys
992 * struct test_obj {
993 * [...]
994 * struct rhash_head node;
995 * };
996 *
Patrick McHardy49f7b332015-03-25 13:07:45 +0000997 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200998 * {
999 * struct test_obj *obj = data;
1000 *
1001 * return [... hash ...];
1002 * }
1003 *
1004 * struct rhashtable_params params = {
1005 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001006 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001007 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001008 * };
1009 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001010int rhashtable_init(struct rhashtable *ht,
1011 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001012{
1013 struct bucket_table *tbl;
1014 size_t size;
1015
Herbert Xu31ccde22015-03-24 00:50:21 +11001016 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001017 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001018 return -EINVAL;
1019
Thomas Graf97defe12015-01-02 23:00:20 +01001020 memset(ht, 0, sizeof(*ht));
1021 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001022 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001023 memcpy(&ht->p, params, sizeof(*params));
1024
Thomas Grafa998f712015-03-19 22:31:13 +00001025 if (params->min_size)
1026 ht->p.min_size = roundup_pow_of_two(params->min_size);
1027
Herbert Xu6d684e52017-04-27 13:44:51 +08001028 /* Cap total entries at 2^31 to avoid nelems overflow. */
1029 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001030
1031 if (params->max_size) {
1032 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1033 if (ht->p.max_size < ht->max_elems / 2)
1034 ht->max_elems = ht->p.max_size * 2;
1035 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001036
Florian Westphal48e75b432017-05-01 22:18:01 +02001037 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001038
Davidlohr Bueso107d01f2018-07-16 13:26:13 -07001039 size = rounded_hashtable_size(&ht->p);
Herbert Xu3a324602015-12-16 18:13:14 +08001040
Herbert Xu31ccde22015-03-24 00:50:21 +11001041 ht->key_len = ht->p.key_len;
1042 if (!params->hashfn) {
1043 ht->p.hashfn = jhash;
1044
1045 if (!(ht->key_len & (sizeof(u32) - 1))) {
1046 ht->key_len /= sizeof(u32);
1047 ht->p.hashfn = rhashtable_jhash2;
1048 }
1049 }
1050
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001051 /*
1052 * This is api initialization and thus we need to guarantee the
1053 * initial rhashtable allocation. Upon failure, retry with the
1054 * smallest possible size with __GFP_NOFAIL semantics.
1055 */
Herbert Xub9ecfda2015-03-24 00:50:27 +11001056 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001057 if (unlikely(tbl == NULL)) {
1058 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1059 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1060 }
Thomas Graf7e1e7762014-08-02 11:47:44 +02001061
Ying Xue545a1482015-01-07 13:41:57 +08001062 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001063
Thomas Graf7e1e7762014-08-02 11:47:44 +02001064 RCU_INIT_POINTER(ht->tbl, tbl);
1065
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001066 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001067
Thomas Graf7e1e7762014-08-02 11:47:44 +02001068 return 0;
1069}
1070EXPORT_SYMBOL_GPL(rhashtable_init);
1071
1072/**
Herbert Xuca268932016-09-19 19:00:09 +08001073 * rhltable_init - initialize a new hash list table
1074 * @hlt: hash list table to be initialized
1075 * @params: configuration parameters
1076 *
1077 * Initializes a new hash list table.
1078 *
1079 * See documentation for rhashtable_init.
1080 */
1081int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1082{
1083 int err;
1084
Herbert Xuca268932016-09-19 19:00:09 +08001085 err = rhashtable_init(&hlt->ht, params);
1086 hlt->ht.rhlist = true;
1087 return err;
1088}
1089EXPORT_SYMBOL_GPL(rhltable_init);
1090
1091static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1092 void (*free_fn)(void *ptr, void *arg),
1093 void *arg)
1094{
1095 struct rhlist_head *list;
1096
1097 if (!ht->rhlist) {
1098 free_fn(rht_obj(ht, obj), arg);
1099 return;
1100 }
1101
1102 list = container_of(obj, struct rhlist_head, rhead);
1103 do {
1104 obj = &list->rhead;
1105 list = rht_dereference(list->next, ht);
1106 free_fn(rht_obj(ht, obj), arg);
1107 } while (list);
1108}
1109
1110/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001111 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001112 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001113 * @free_fn: callback to release resources of element
1114 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001115 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001116 * Stops an eventual async resize. If defined, invokes free_fn for each
1117 * element to releasal resources. Please note that RCU protected
1118 * readers may still be accessing the elements. Releasing of resources
1119 * must occur in a compatible manner. Then frees the bucket array.
1120 *
1121 * This function will eventually sleep to wait for an async resize
1122 * to complete. The caller is responsible that no further write operations
1123 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001124 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001125void rhashtable_free_and_destroy(struct rhashtable *ht,
1126 void (*free_fn)(void *ptr, void *arg),
1127 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001128{
Taehee Yoo00261292018-07-08 11:55:51 +09001129 struct bucket_table *tbl, *next_tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001130 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001131
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001132 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001133
Thomas Graf97defe12015-01-02 23:00:20 +01001134 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001135 tbl = rht_dereference(ht->tbl, ht);
Taehee Yoo00261292018-07-08 11:55:51 +09001136restart:
Thomas Graf6b6f3022015-03-24 14:18:20 +01001137 if (free_fn) {
1138 for (i = 0; i < tbl->size; i++) {
1139 struct rhash_head *pos, *next;
1140
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001141 cond_resched();
NeilBrownadc6a3a2019-04-12 11:52:08 +10001142 for (pos = rht_ptr_exclusive(rht_bucket(tbl, i)),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001143 next = !rht_is_a_nulls(pos) ?
1144 rht_dereference(pos->next, ht) : NULL;
1145 !rht_is_a_nulls(pos);
1146 pos = next,
1147 next = !rht_is_a_nulls(pos) ?
1148 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001149 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001150 }
1151 }
1152
Taehee Yoo00261292018-07-08 11:55:51 +09001153 next_tbl = rht_dereference(tbl->future_tbl, ht);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001154 bucket_table_free(tbl);
Taehee Yoo00261292018-07-08 11:55:51 +09001155 if (next_tbl) {
1156 tbl = next_tbl;
1157 goto restart;
1158 }
Thomas Graf97defe12015-01-02 23:00:20 +01001159 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001160}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001161EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1162
1163void rhashtable_destroy(struct rhashtable *ht)
1164{
1165 return rhashtable_free_and_destroy(ht, NULL, NULL);
1166}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001167EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001168
NeilBrown8f0db012019-04-02 10:07:45 +11001169struct rhash_lock_head __rcu **__rht_bucket_nested(const struct bucket_table *tbl,
1170 unsigned int hash)
Herbert Xuda204202017-02-11 19:26:47 +08001171{
1172 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
Herbert Xuda204202017-02-11 19:26:47 +08001173 unsigned int index = hash & ((1 << tbl->nest) - 1);
1174 unsigned int size = tbl->size >> tbl->nest;
1175 unsigned int subhash = hash;
1176 union nested_table *ntbl;
1177
1178 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
Herbert Xuc4d26032017-02-25 22:39:50 +08001179 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001180 subhash >>= tbl->nest;
1181
1182 while (ntbl && size > (1 << shift)) {
1183 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001184 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1185 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001186 size >>= shift;
1187 subhash >>= shift;
1188 }
1189
NeilBrownff302db2019-04-02 10:07:45 +11001190 if (!ntbl)
1191 return NULL;
Herbert Xuda204202017-02-11 19:26:47 +08001192
1193 return &ntbl[subhash].bucket;
1194
1195}
NeilBrownff302db2019-04-02 10:07:45 +11001196EXPORT_SYMBOL_GPL(__rht_bucket_nested);
1197
NeilBrown8f0db012019-04-02 10:07:45 +11001198struct rhash_lock_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1199 unsigned int hash)
NeilBrownff302db2019-04-02 10:07:45 +11001200{
NeilBrown8f0db012019-04-02 10:07:45 +11001201 static struct rhash_lock_head __rcu *rhnull;
NeilBrownff302db2019-04-02 10:07:45 +11001202
1203 if (!rhnull)
1204 INIT_RHT_NULLS_HEAD(rhnull);
1205 return __rht_bucket_nested(tbl, hash) ?: &rhnull;
1206}
Herbert Xuda204202017-02-11 19:26:47 +08001207EXPORT_SYMBOL_GPL(rht_bucket_nested);
1208
NeilBrown8f0db012019-04-02 10:07:45 +11001209struct rhash_lock_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1210 struct bucket_table *tbl,
1211 unsigned int hash)
Herbert Xuda204202017-02-11 19:26:47 +08001212{
1213 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1214 unsigned int index = hash & ((1 << tbl->nest) - 1);
1215 unsigned int size = tbl->size >> tbl->nest;
1216 union nested_table *ntbl;
Herbert Xuda204202017-02-11 19:26:47 +08001217
1218 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1219 hash >>= tbl->nest;
Herbert Xuda204202017-02-11 19:26:47 +08001220 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001221 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001222
1223 while (ntbl && size > (1 << shift)) {
1224 index = hash & ((1 << shift) - 1);
1225 size >>= shift;
1226 hash >>= shift;
Herbert Xuda204202017-02-11 19:26:47 +08001227 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001228 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001229 }
1230
1231 if (!ntbl)
1232 return NULL;
1233
1234 return &ntbl[hash].bucket;
1235
1236}
1237EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);