blob: 6410c857b048d4b44039dade121586e973a8596a [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
Florian Westphal4cf0b352016-08-12 12:03:52 +020034#define BUCKET_LOCKS_PER_CPU 32UL
Thomas Graf97defe12015-01-02 23:00:20 +010035
Herbert Xuda204202017-02-11 19:26:47 +080036union nested_table {
37 union nested_table __rcu *table;
38 struct rhash_head __rcu *bucket;
39};
40
Herbert Xu988dfbd2015-03-10 09:27:55 +110041static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010042 const struct bucket_table *tbl,
43 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020044{
Herbert Xu02fd97c2015-03-20 21:57:00 +110045 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020046}
47
Thomas Grafa03eaec2015-02-05 02:03:34 +010048#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010049#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010050
51int lockdep_rht_mutex_is_held(struct rhashtable *ht)
52{
53 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
54}
55EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
56
57int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
58{
Herbert Xu02fd97c2015-03-20 21:57:00 +110059 spinlock_t *lock = rht_bucket_lock(tbl, hash);
Thomas Grafa03eaec2015-02-05 02:03:34 +010060
61 return (debug_locks) ? lockdep_is_held(lock) : 1;
62}
63EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
64#else
65#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +010066#endif
67
Herbert Xuda204202017-02-11 19:26:47 +080068static void nested_table_free(union nested_table *ntbl, unsigned int size)
69{
70 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
71 const unsigned int len = 1 << shift;
72 unsigned int i;
73
74 ntbl = rcu_dereference_raw(ntbl->table);
75 if (!ntbl)
76 return;
77
78 if (size > len) {
79 size >>= shift;
80 for (i = 0; i < len; i++)
81 nested_table_free(ntbl + i, size);
82 }
83
84 kfree(ntbl);
85}
86
87static void nested_bucket_table_free(const struct bucket_table *tbl)
88{
89 unsigned int size = tbl->size >> tbl->nest;
90 unsigned int len = 1 << tbl->nest;
91 union nested_table *ntbl;
92 unsigned int i;
93
94 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
95
96 for (i = 0; i < len; i++)
97 nested_table_free(ntbl + i, size);
98
99 kfree(ntbl);
100}
101
Thomas Graf97defe12015-01-02 23:00:20 +0100102static void bucket_table_free(const struct bucket_table *tbl)
103{
Herbert Xuda204202017-02-11 19:26:47 +0800104 if (tbl->nest)
105 nested_bucket_table_free(tbl);
106
Tom Herbert64e0cd02017-12-04 10:31:45 -0800107 free_bucket_spinlocks(tbl->locks);
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
134 rcu_assign_pointer(*prev, ntbl);
135
136 return ntbl;
137}
138
139static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
140 size_t nbuckets,
141 gfp_t gfp)
142{
143 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
144 struct bucket_table *tbl;
145 size_t size;
146
147 if (nbuckets < (1 << (shift + 1)))
148 return NULL;
149
150 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
151
152 tbl = kzalloc(size, gfp);
153 if (!tbl)
154 return NULL;
155
156 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
NeilBrown5af68ef2018-06-18 12:52:50 +1000157 false)) {
Herbert Xuda204202017-02-11 19:26:47 +0800158 kfree(tbl);
159 return NULL;
160 }
161
162 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
163
164 return tbl;
165}
166
Thomas Graf97defe12015-01-02 23:00:20 +0100167static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xub9ecfda2015-03-24 00:50:27 +1100168 size_t nbuckets,
169 gfp_t gfp)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200170{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100171 struct bucket_table *tbl = NULL;
Tom Herbert64e0cd02017-12-04 10:31:45 -0800172 size_t size, max_locks;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100173 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200174
175 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Davidlohr Bueso93f976b2018-08-21 22:01:45 -0700176 tbl = kvzalloc(size, gfp);
Herbert Xuda204202017-02-11 19:26:47 +0800177
178 size = nbuckets;
179
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700180 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
Herbert Xuda204202017-02-11 19:26:47 +0800181 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
182 nbuckets = 0;
183 }
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700184
Thomas Graf7e1e7762014-08-02 11:47:44 +0200185 if (tbl == NULL)
186 return NULL;
187
Herbert Xuda204202017-02-11 19:26:47 +0800188 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200189
Tom Herbert64e0cd02017-12-04 10:31:45 -0800190 max_locks = size >> 1;
191 if (tbl->nest)
192 max_locks = min_t(size_t, max_locks, 1U << tbl->nest);
193
194 if (alloc_bucket_spinlocks(&tbl->locks, &tbl->locks_mask, max_locks,
195 ht->p.locks_mul, gfp) < 0) {
Thomas Graf97defe12015-01-02 23:00:20 +0100196 bucket_table_free(tbl);
197 return NULL;
198 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200199
Herbert Xueddee5ba2015-03-14 13:57:20 +1100200 INIT_LIST_HEAD(&tbl->walkers);
201
Jason A. Donenfeldd48ad082017-06-07 22:47:13 -0400202 tbl->hash_rnd = get_random_u32();
Herbert Xu5269b532015-03-14 13:57:22 +1100203
Thomas Graff89bd6f2015-01-02 23:00:21 +0100204 for (i = 0; i < nbuckets; i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000205 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100206
Thomas Graf97defe12015-01-02 23:00:20 +0100207 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200208}
209
Herbert Xub8244782015-03-24 00:50:26 +1100210static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
211 struct bucket_table *tbl)
212{
213 struct bucket_table *new_tbl;
214
215 do {
216 new_tbl = tbl;
217 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
218 } while (tbl);
219
220 return new_tbl;
221}
222
Thomas Graf299e5c32015-03-24 14:18:17 +0100223static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100224{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100225 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
NeilBrownc0690012018-06-18 12:52:50 +1000226 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
Herbert Xuda204202017-02-11 19:26:47 +0800227 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
228 int err = -EAGAIN;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100229 struct rhash_head *head, *next, *entry;
230 spinlock_t *new_bucket_lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100231 unsigned int new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100232
Herbert Xuda204202017-02-11 19:26:47 +0800233 if (new_tbl->nest)
234 goto out;
235
236 err = -ENOENT;
237
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100238 rht_for_each(entry, old_tbl, old_hash) {
239 err = 0;
240 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100241
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100242 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200243 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100244
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100245 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200246 }
247
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100248 if (err)
249 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100250
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100251 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100252
Herbert Xu02fd97c2015-03-20 21:57:00 +1100253 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100254
Herbert Xu8f2484b2015-03-14 13:57:21 +1100255 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100256 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
257 new_tbl, new_hash);
258
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200259 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100260
261 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
262 spin_unlock(new_bucket_lock);
263
264 rcu_assign_pointer(*pprev, next);
265
266out:
267 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100268}
269
Herbert Xuda204202017-02-11 19:26:47 +0800270static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100271 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100272{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100273 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
274 spinlock_t *old_bucket_lock;
Herbert Xuda204202017-02-11 19:26:47 +0800275 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100276
Herbert Xu02fd97c2015-03-20 21:57:00 +1100277 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100278
279 spin_lock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800280 while (!(err = rhashtable_rehash_one(ht, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100281 ;
Herbert Xuda204202017-02-11 19:26:47 +0800282
283 if (err == -ENOENT) {
284 old_tbl->rehash++;
285 err = 0;
286 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100287 spin_unlock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800288
289 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100290}
291
Herbert Xub8244782015-03-24 00:50:26 +1100292static int rhashtable_rehash_attach(struct rhashtable *ht,
293 struct bucket_table *old_tbl,
294 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100295{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100296 /* Make insertions go into the new, empty table right away. Deletions
297 * and lookups will be attempted in both tables until we synchronize.
NeilBrown0ad66442018-06-18 12:52:50 +1000298 * As cmpxchg() provides strong barriers, we do not need
299 * rcu_assign_pointer().
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100300 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100301
NeilBrown0ad66442018-06-18 12:52:50 +1000302 if (cmpxchg(&old_tbl->future_tbl, NULL, new_tbl) != NULL)
303 return -EEXIST;
Herbert Xub8244782015-03-24 00:50:26 +1100304
305 return 0;
306}
307
308static int rhashtable_rehash_table(struct rhashtable *ht)
309{
310 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
311 struct bucket_table *new_tbl;
312 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100313 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800314 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100315
316 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
317 if (!new_tbl)
318 return 0;
319
Herbert Xuda204202017-02-11 19:26:47 +0800320 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
321 err = rhashtable_rehash_chain(ht, old_hash);
322 if (err)
323 return err;
Eric Dumazetae6da1f2018-03-31 12:58:48 -0700324 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +0800325 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100326
327 /* Publish the new table pointer. */
328 rcu_assign_pointer(ht->tbl, new_tbl);
329
Herbert Xuba7c95e2015-03-24 09:53:17 +1100330 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100331 list_for_each_entry(walker, &old_tbl->walkers, list)
332 walker->tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100333 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100334
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100335 /* Wait for readers. All new readers will see the new
336 * table, and thus no references to the old table will
337 * remain.
338 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100339 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
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 Xucf86f7a2019-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,
481 struct bucket_table *tbl, unsigned int hash,
482 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100483{
Herbert Xuca268932016-09-19 19:00:09 +0800484 struct rhashtable_compare_arg arg = {
485 .ht = ht,
486 .key = key,
487 };
488 struct rhash_head __rcu **pprev;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100489 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800490 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100491
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200492 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800493 pprev = rht_bucket_var(tbl, hash);
494 rht_for_each_continue(head, *pprev, 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);
516 rcu_assign_pointer(*pprev, obj);
517
518 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200519 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100520
Herbert Xuca268932016-09-19 19:00:09 +0800521 if (elasticity <= 0)
522 return ERR_PTR(-EAGAIN);
523
524 return ERR_PTR(-ENOENT);
525}
526
527static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
528 struct bucket_table *tbl,
529 unsigned int hash,
530 struct rhash_head *obj,
531 void *data)
532{
Herbert Xuda204202017-02-11 19:26:47 +0800533 struct rhash_head __rcu **pprev;
Herbert Xuca268932016-09-19 19:00:09 +0800534 struct bucket_table *new_tbl;
535 struct rhash_head *head;
536
537 if (!IS_ERR_OR_NULL(data))
538 return ERR_PTR(-EEXIST);
539
540 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
541 return ERR_CAST(data);
542
NeilBrownc0690012018-06-18 12:52:50 +1000543 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800544 if (new_tbl)
545 return new_tbl;
546
547 if (PTR_ERR(data) != -ENOENT)
548 return ERR_CAST(data);
549
Herbert Xu07ee0722015-05-15 11:30:47 +0800550 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800551 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800552
Herbert Xuca268932016-09-19 19:00:09 +0800553 if (unlikely(rht_grow_above_100(ht, tbl)))
554 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100555
Herbert Xuda204202017-02-11 19:26:47 +0800556 pprev = rht_bucket_insert(ht, tbl, hash);
557 if (!pprev)
558 return ERR_PTR(-ENOMEM);
559
560 head = rht_dereference_bucket(*pprev, 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
Herbert Xuda204202017-02-11 19:26:47 +0800570 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100571
572 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800573 if (rht_grow_above_75(ht, tbl))
574 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100575
Herbert Xuca268932016-09-19 19:00:09 +0800576 return NULL;
577}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100578
Herbert Xuca268932016-09-19 19:00:09 +0800579static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
580 struct rhash_head *obj)
581{
582 struct bucket_table *new_tbl;
583 struct bucket_table *tbl;
584 unsigned int hash;
585 spinlock_t *lock;
586 void *data;
587
588 tbl = rcu_dereference(ht->tbl);
589
590 /* All insertions must grab the oldest table containing
591 * the hashed bucket that is yet to be rehashed.
592 */
593 for (;;) {
594 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
595 lock = rht_bucket_lock(tbl, hash);
596 spin_lock_bh(lock);
597
598 if (tbl->rehash <= hash)
599 break;
600
601 spin_unlock_bh(lock);
NeilBrownc0690012018-06-18 12:52:50 +1000602 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800603 }
604
605 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
606 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
607 if (PTR_ERR(new_tbl) != -EEXIST)
608 data = ERR_CAST(new_tbl);
609
610 while (!IS_ERR_OR_NULL(new_tbl)) {
611 tbl = new_tbl;
612 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
613 spin_lock_nested(rht_bucket_lock(tbl, hash),
614 SINGLE_DEPTH_NESTING);
615
616 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
617 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
618 if (PTR_ERR(new_tbl) != -EEXIST)
619 data = ERR_CAST(new_tbl);
620
621 spin_unlock(rht_bucket_lock(tbl, hash));
622 }
623
624 spin_unlock_bh(lock);
625
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 *
689 * This function frees resources allocated by rhashtable_walk_init.
690 */
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 *
710 * Returns -EAGAIN if resize event occured. Note that the iterator
711 * 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);
Herbert Xuc4db8842015-03-14 13:57:25 +1100946 if (tbl->rehash < tbl->size)
Herbert Xu246779d2016-08-18 16:50:56 +0800947 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100948 else
Herbert Xu246779d2016-08-18 16:50:56 +0800949 iter->walker.tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100950 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100951
Herbert Xu963ecbd2015-03-15 21:12:04 +1100952out:
953 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100954}
955EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
956
Herbert Xu488fb86e2015-03-20 21:56:59 +1100957static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200958{
Davidlohr Bueso107d01f2018-07-16 13:26:13 -0700959 size_t retsize;
960
961 if (params->nelem_hint)
962 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
963 (unsigned long)params->min_size);
964 else
965 retsize = max(HASH_DEFAULT_SIZE,
966 (unsigned long)params->min_size);
967
968 return retsize;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200969}
970
Herbert Xu31ccde22015-03-24 00:50:21 +1100971static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
972{
973 return jhash2(key, length, seed);
974}
975
Thomas Graf7e1e7762014-08-02 11:47:44 +0200976/**
977 * rhashtable_init - initialize a new hash table
978 * @ht: hash table to be initialized
979 * @params: configuration parameters
980 *
981 * Initializes a new hash table based on the provided configuration
982 * parameters. A table can be configured either with a variable or
983 * fixed length key:
984 *
985 * Configuration Example 1: Fixed length keys
986 * struct test_obj {
987 * int key;
988 * void * my_member;
989 * struct rhash_head node;
990 * };
991 *
992 * struct rhashtable_params params = {
993 * .head_offset = offsetof(struct test_obj, node),
994 * .key_offset = offsetof(struct test_obj, key),
995 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100996 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200997 * };
998 *
999 * Configuration Example 2: Variable length keys
1000 * struct test_obj {
1001 * [...]
1002 * struct rhash_head node;
1003 * };
1004 *
Patrick McHardy49f7b332015-03-25 13:07:45 +00001005 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001006 * {
1007 * struct test_obj *obj = data;
1008 *
1009 * return [... hash ...];
1010 * }
1011 *
1012 * struct rhashtable_params params = {
1013 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001014 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001015 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001016 * };
1017 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001018int rhashtable_init(struct rhashtable *ht,
1019 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001020{
1021 struct bucket_table *tbl;
1022 size_t size;
1023
Herbert Xu31ccde22015-03-24 00:50:21 +11001024 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001025 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001026 return -EINVAL;
1027
Thomas Graf97defe12015-01-02 23:00:20 +01001028 memset(ht, 0, sizeof(*ht));
1029 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001030 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001031 memcpy(&ht->p, params, sizeof(*params));
1032
Thomas Grafa998f712015-03-19 22:31:13 +00001033 if (params->min_size)
1034 ht->p.min_size = roundup_pow_of_two(params->min_size);
1035
Herbert Xu6d684e52017-04-27 13:44:51 +08001036 /* Cap total entries at 2^31 to avoid nelems overflow. */
1037 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001038
1039 if (params->max_size) {
1040 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1041 if (ht->p.max_size < ht->max_elems / 2)
1042 ht->max_elems = ht->p.max_size * 2;
1043 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001044
Florian Westphal48e75b432017-05-01 22:18:01 +02001045 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001046
Davidlohr Bueso107d01f2018-07-16 13:26:13 -07001047 size = rounded_hashtable_size(&ht->p);
Herbert Xu3a324602015-12-16 18:13:14 +08001048
Thomas Graf97defe12015-01-02 23:00:20 +01001049 if (params->locks_mul)
1050 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1051 else
1052 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1053
Herbert Xu31ccde22015-03-24 00:50:21 +11001054 ht->key_len = ht->p.key_len;
1055 if (!params->hashfn) {
1056 ht->p.hashfn = jhash;
1057
1058 if (!(ht->key_len & (sizeof(u32) - 1))) {
1059 ht->key_len /= sizeof(u32);
1060 ht->p.hashfn = rhashtable_jhash2;
1061 }
1062 }
1063
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001064 /*
1065 * This is api initialization and thus we need to guarantee the
1066 * initial rhashtable allocation. Upon failure, retry with the
1067 * smallest possible size with __GFP_NOFAIL semantics.
1068 */
Herbert Xub9ecfda2015-03-24 00:50:27 +11001069 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001070 if (unlikely(tbl == NULL)) {
1071 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1072 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1073 }
Thomas Graf7e1e7762014-08-02 11:47:44 +02001074
Ying Xue545a1482015-01-07 13:41:57 +08001075 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001076
Thomas Graf7e1e7762014-08-02 11:47:44 +02001077 RCU_INIT_POINTER(ht->tbl, tbl);
1078
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001079 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001080
Thomas Graf7e1e7762014-08-02 11:47:44 +02001081 return 0;
1082}
1083EXPORT_SYMBOL_GPL(rhashtable_init);
1084
1085/**
Herbert Xuca268932016-09-19 19:00:09 +08001086 * rhltable_init - initialize a new hash list table
1087 * @hlt: hash list table to be initialized
1088 * @params: configuration parameters
1089 *
1090 * Initializes a new hash list table.
1091 *
1092 * See documentation for rhashtable_init.
1093 */
1094int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1095{
1096 int err;
1097
Herbert Xuca268932016-09-19 19:00:09 +08001098 err = rhashtable_init(&hlt->ht, params);
1099 hlt->ht.rhlist = true;
1100 return err;
1101}
1102EXPORT_SYMBOL_GPL(rhltable_init);
1103
1104static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1105 void (*free_fn)(void *ptr, void *arg),
1106 void *arg)
1107{
1108 struct rhlist_head *list;
1109
1110 if (!ht->rhlist) {
1111 free_fn(rht_obj(ht, obj), arg);
1112 return;
1113 }
1114
1115 list = container_of(obj, struct rhlist_head, rhead);
1116 do {
1117 obj = &list->rhead;
1118 list = rht_dereference(list->next, ht);
1119 free_fn(rht_obj(ht, obj), arg);
1120 } while (list);
1121}
1122
1123/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001124 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001125 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001126 * @free_fn: callback to release resources of element
1127 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001128 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001129 * Stops an eventual async resize. If defined, invokes free_fn for each
1130 * element to releasal resources. Please note that RCU protected
1131 * readers may still be accessing the elements. Releasing of resources
1132 * must occur in a compatible manner. Then frees the bucket array.
1133 *
1134 * This function will eventually sleep to wait for an async resize
1135 * to complete. The caller is responsible that no further write operations
1136 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001137 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001138void rhashtable_free_and_destroy(struct rhashtable *ht,
1139 void (*free_fn)(void *ptr, void *arg),
1140 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001141{
Taehee Yoo00261292018-07-08 11:55:51 +09001142 struct bucket_table *tbl, *next_tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001143 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001144
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001145 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001146
Thomas Graf97defe12015-01-02 23:00:20 +01001147 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001148 tbl = rht_dereference(ht->tbl, ht);
Taehee Yoo00261292018-07-08 11:55:51 +09001149restart:
Thomas Graf6b6f3022015-03-24 14:18:20 +01001150 if (free_fn) {
1151 for (i = 0; i < tbl->size; i++) {
1152 struct rhash_head *pos, *next;
1153
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001154 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +08001155 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001156 next = !rht_is_a_nulls(pos) ?
1157 rht_dereference(pos->next, ht) : NULL;
1158 !rht_is_a_nulls(pos);
1159 pos = next,
1160 next = !rht_is_a_nulls(pos) ?
1161 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001162 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001163 }
1164 }
1165
Taehee Yoo00261292018-07-08 11:55:51 +09001166 next_tbl = rht_dereference(tbl->future_tbl, ht);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001167 bucket_table_free(tbl);
Taehee Yoo00261292018-07-08 11:55:51 +09001168 if (next_tbl) {
1169 tbl = next_tbl;
1170 goto restart;
1171 }
Thomas Graf97defe12015-01-02 23:00:20 +01001172 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001173}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001174EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1175
1176void rhashtable_destroy(struct rhashtable *ht)
1177{
1178 return rhashtable_free_and_destroy(ht, NULL, NULL);
1179}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001180EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001181
1182struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1183 unsigned int hash)
1184{
1185 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1186 static struct rhash_head __rcu *rhnull =
1187 (struct rhash_head __rcu *)NULLS_MARKER(0);
1188 unsigned int index = hash & ((1 << tbl->nest) - 1);
1189 unsigned int size = tbl->size >> tbl->nest;
1190 unsigned int subhash = hash;
1191 union nested_table *ntbl;
1192
1193 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
Herbert Xuc4d26032017-02-25 22:39:50 +08001194 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001195 subhash >>= tbl->nest;
1196
1197 while (ntbl && size > (1 << shift)) {
1198 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001199 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1200 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001201 size >>= shift;
1202 subhash >>= shift;
1203 }
1204
1205 if (!ntbl)
1206 return &rhnull;
1207
1208 return &ntbl[subhash].bucket;
1209
1210}
1211EXPORT_SYMBOL_GPL(rht_bucket_nested);
1212
1213struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1214 struct bucket_table *tbl,
1215 unsigned int hash)
1216{
1217 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1218 unsigned int index = hash & ((1 << tbl->nest) - 1);
1219 unsigned int size = tbl->size >> tbl->nest;
1220 union nested_table *ntbl;
Herbert Xuda204202017-02-11 19:26:47 +08001221
1222 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1223 hash >>= tbl->nest;
Herbert Xuda204202017-02-11 19:26:47 +08001224 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001225 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001226
1227 while (ntbl && size > (1 << shift)) {
1228 index = hash & ((1 << shift) - 1);
1229 size >>= shift;
1230 hash >>= shift;
Herbert Xuda204202017-02-11 19:26:47 +08001231 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001232 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001233 }
1234
1235 if (!ntbl)
1236 return NULL;
1237
1238 return &ntbl[hash].bucket;
1239
1240}
1241EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);