blob: 688693c919be31ea9c2219898242b6f525c8ced4 [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>
NeilBrown0eb71a92018-06-18 12:52:50 +100031#include <linux/rhashtable.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020032
33#define HASH_DEFAULT_SIZE 64UL
Herbert Xuc2e213c2015-03-18 20:01:16 +110034#define HASH_MIN_SIZE 4U
Florian Westphal4cf0b352016-08-12 12:03:52 +020035#define BUCKET_LOCKS_PER_CPU 32UL
Thomas Graf97defe12015-01-02 23:00:20 +010036
Herbert Xuda204202017-02-11 19:26:47 +080037union nested_table {
38 union nested_table __rcu *table;
39 struct rhash_head __rcu *bucket;
40};
41
Herbert Xu988dfbd2015-03-10 09:27:55 +110042static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010043 const struct bucket_table *tbl,
44 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020045{
Herbert Xu02fd97c2015-03-20 21:57:00 +110046 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020047}
48
Thomas Grafa03eaec2015-02-05 02:03:34 +010049#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010050#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010051
52int lockdep_rht_mutex_is_held(struct rhashtable *ht)
53{
54 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
55}
56EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
57
58int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
59{
Herbert Xu02fd97c2015-03-20 21:57:00 +110060 spinlock_t *lock = rht_bucket_lock(tbl, hash);
Thomas Grafa03eaec2015-02-05 02:03:34 +010061
62 return (debug_locks) ? lockdep_is_held(lock) : 1;
63}
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
Tom Herbert64e0cd02017-12-04 10:31:45 -0800108 free_bucket_spinlocks(tbl->locks);
Thomas Graf97defe12015-01-02 23:00:20 +0100109 kvfree(tbl);
110}
111
Herbert Xu9d901bc2015-03-14 13:57:23 +1100112static void bucket_table_free_rcu(struct rcu_head *head)
113{
114 bucket_table_free(container_of(head, struct bucket_table, rcu));
115}
116
Herbert Xuda204202017-02-11 19:26:47 +0800117static union nested_table *nested_table_alloc(struct rhashtable *ht,
118 union nested_table __rcu **prev,
119 unsigned int shifted,
120 unsigned int nhash)
121{
122 union nested_table *ntbl;
123 int i;
124
125 ntbl = rcu_dereference(*prev);
126 if (ntbl)
127 return ntbl;
128
129 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
130
131 if (ntbl && shifted) {
132 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0].bucket); i++)
133 INIT_RHT_NULLS_HEAD(ntbl[i].bucket, ht,
134 (i << shifted) | nhash);
135 }
136
137 rcu_assign_pointer(*prev, ntbl);
138
139 return ntbl;
140}
141
142static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
143 size_t nbuckets,
144 gfp_t gfp)
145{
146 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
147 struct bucket_table *tbl;
148 size_t size;
149
150 if (nbuckets < (1 << (shift + 1)))
151 return NULL;
152
153 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
154
155 tbl = kzalloc(size, gfp);
156 if (!tbl)
157 return NULL;
158
159 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
160 0, 0)) {
161 kfree(tbl);
162 return NULL;
163 }
164
165 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
166
167 return tbl;
168}
169
Thomas Graf97defe12015-01-02 23:00:20 +0100170static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xub9ecfda2015-03-24 00:50:27 +1100171 size_t nbuckets,
172 gfp_t gfp)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200173{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100174 struct bucket_table *tbl = NULL;
Tom Herbert64e0cd02017-12-04 10:31:45 -0800175 size_t size, max_locks;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100176 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200177
178 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Michal Hocko12e8fd62017-07-10 15:51:55 -0700179 if (gfp != GFP_KERNEL)
Herbert Xub9ecfda2015-03-24 00:50:27 +1100180 tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
Michal Hocko12e8fd62017-07-10 15:51:55 -0700181 else
182 tbl = kvzalloc(size, gfp);
Herbert Xuda204202017-02-11 19:26:47 +0800183
184 size = nbuckets;
185
186 if (tbl == NULL && gfp != GFP_KERNEL) {
187 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
188 nbuckets = 0;
189 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200190 if (tbl == NULL)
191 return NULL;
192
Herbert Xuda204202017-02-11 19:26:47 +0800193 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200194
Tom Herbert64e0cd02017-12-04 10:31:45 -0800195 max_locks = size >> 1;
196 if (tbl->nest)
197 max_locks = min_t(size_t, max_locks, 1U << tbl->nest);
198
199 if (alloc_bucket_spinlocks(&tbl->locks, &tbl->locks_mask, max_locks,
200 ht->p.locks_mul, gfp) < 0) {
Thomas Graf97defe12015-01-02 23:00:20 +0100201 bucket_table_free(tbl);
202 return NULL;
203 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200204
Herbert Xueddee5ba2015-03-14 13:57:20 +1100205 INIT_LIST_HEAD(&tbl->walkers);
206
Jason A. Donenfeldd48ad082017-06-07 22:47:13 -0400207 tbl->hash_rnd = get_random_u32();
Herbert Xu5269b532015-03-14 13:57:22 +1100208
Thomas Graff89bd6f2015-01-02 23:00:21 +0100209 for (i = 0; i < nbuckets; i++)
210 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
211
Thomas Graf97defe12015-01-02 23:00:20 +0100212 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200213}
214
Herbert Xub8244782015-03-24 00:50:26 +1100215static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
216 struct bucket_table *tbl)
217{
218 struct bucket_table *new_tbl;
219
220 do {
221 new_tbl = tbl;
222 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
223 } while (tbl);
224
225 return new_tbl;
226}
227
Thomas Graf299e5c32015-03-24 14:18:17 +0100228static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100229{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100230 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100231 struct bucket_table *new_tbl = rhashtable_last_table(ht,
232 rht_dereference_rcu(old_tbl->future_tbl, ht));
Herbert Xuda204202017-02-11 19:26:47 +0800233 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
234 int err = -EAGAIN;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100235 struct rhash_head *head, *next, *entry;
236 spinlock_t *new_bucket_lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100237 unsigned int new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100238
Herbert Xuda204202017-02-11 19:26:47 +0800239 if (new_tbl->nest)
240 goto out;
241
242 err = -ENOENT;
243
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100244 rht_for_each(entry, old_tbl, old_hash) {
245 err = 0;
246 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100247
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100248 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200249 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100250
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100251 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200252 }
253
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100254 if (err)
255 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100256
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100257 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100258
Herbert Xu02fd97c2015-03-20 21:57:00 +1100259 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100260
Herbert Xu8f2484b2015-03-14 13:57:21 +1100261 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100262 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
263 new_tbl, new_hash);
264
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200265 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100266
267 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
268 spin_unlock(new_bucket_lock);
269
270 rcu_assign_pointer(*pprev, next);
271
272out:
273 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100274}
275
Herbert Xuda204202017-02-11 19:26:47 +0800276static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100277 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100278{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100279 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
280 spinlock_t *old_bucket_lock;
Herbert Xuda204202017-02-11 19:26:47 +0800281 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100282
Herbert Xu02fd97c2015-03-20 21:57:00 +1100283 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100284
285 spin_lock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800286 while (!(err = rhashtable_rehash_one(ht, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100287 ;
Herbert Xuda204202017-02-11 19:26:47 +0800288
289 if (err == -ENOENT) {
290 old_tbl->rehash++;
291 err = 0;
292 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100293 spin_unlock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800294
295 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100296}
297
Herbert Xub8244782015-03-24 00:50:26 +1100298static int rhashtable_rehash_attach(struct rhashtable *ht,
299 struct bucket_table *old_tbl,
300 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100301{
Herbert Xub8244782015-03-24 00:50:26 +1100302 /* Protect future_tbl using the first bucket lock. */
303 spin_lock_bh(old_tbl->locks);
304
305 /* Did somebody beat us to it? */
306 if (rcu_access_pointer(old_tbl->future_tbl)) {
307 spin_unlock_bh(old_tbl->locks);
308 return -EEXIST;
309 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100310
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100311 /* Make insertions go into the new, empty table right away. Deletions
312 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100313 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100314 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100315
Herbert Xub8244782015-03-24 00:50:26 +1100316 spin_unlock_bh(old_tbl->locks);
317
318 return 0;
319}
320
321static int rhashtable_rehash_table(struct rhashtable *ht)
322{
323 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
324 struct bucket_table *new_tbl;
325 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100326 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800327 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100328
329 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
330 if (!new_tbl)
331 return 0;
332
Herbert Xuda204202017-02-11 19:26:47 +0800333 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
334 err = rhashtable_rehash_chain(ht, old_hash);
335 if (err)
336 return err;
Eric Dumazetae6da1f2018-03-31 12:58:48 -0700337 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +0800338 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100339
340 /* Publish the new table pointer. */
341 rcu_assign_pointer(ht->tbl, new_tbl);
342
Herbert Xuba7c95e2015-03-24 09:53:17 +1100343 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100344 list_for_each_entry(walker, &old_tbl->walkers, list)
345 walker->tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100346 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100347
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100348 /* Wait for readers. All new readers will see the new
349 * table, and thus no references to the old table will
350 * remain.
351 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100352 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Herbert Xub8244782015-03-24 00:50:26 +1100353
354 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200355}
356
Herbert Xuda204202017-02-11 19:26:47 +0800357static int rhashtable_rehash_alloc(struct rhashtable *ht,
358 struct bucket_table *old_tbl,
359 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200360{
Herbert Xuda204202017-02-11 19:26:47 +0800361 struct bucket_table *new_tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100362 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200363
364 ASSERT_RHT_MUTEX(ht);
365
Herbert Xuda204202017-02-11 19:26:47 +0800366 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200367 if (new_tbl == NULL)
368 return -ENOMEM;
369
Herbert Xub8244782015-03-24 00:50:26 +1100370 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
371 if (err)
372 bucket_table_free(new_tbl);
373
374 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200375}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200376
377/**
378 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
379 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200380 *
Herbert Xu18093d12015-03-24 00:50:25 +1100381 * This function shrinks the hash table to fit, i.e., the smallest
382 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200383 *
Thomas Graf97defe12015-01-02 23:00:20 +0100384 * The caller must ensure that no concurrent resizing occurs by holding
385 * ht->mutex.
386 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200387 * The caller must ensure that no concurrent table mutations take place.
388 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100389 *
390 * It is valid to have concurrent insertions and deletions protected by per
391 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200392 */
Herbert Xub8244782015-03-24 00:50:26 +1100393static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200394{
Herbert Xuda204202017-02-11 19:26:47 +0800395 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200396 unsigned int nelems = atomic_read(&ht->nelems);
397 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200398
Vegard Nossum12311952016-08-12 20:10:44 +0200399 if (nelems)
400 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100401 if (size < ht->p.min_size)
402 size = ht->p.min_size;
403
404 if (old_tbl->size <= size)
405 return 0;
406
Herbert Xub8244782015-03-24 00:50:26 +1100407 if (rht_dereference(old_tbl->future_tbl, ht))
408 return -EEXIST;
409
Herbert Xuda204202017-02-11 19:26:47 +0800410 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200411}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200412
Thomas Graf97defe12015-01-02 23:00:20 +0100413static void rht_deferred_worker(struct work_struct *work)
414{
415 struct rhashtable *ht;
416 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100417 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100418
Ying Xue57699a42015-01-16 11:13:09 +0800419 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100420 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100421
Thomas Graf97defe12015-01-02 23:00:20 +0100422 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100423 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100424
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100425 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800426 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000427 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800428 err = rhashtable_shrink(ht);
429 else if (tbl->nest)
430 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub8244782015-03-24 00:50:26 +1100431
Herbert Xuda204202017-02-11 19:26:47 +0800432 if (!err)
433 err = rhashtable_rehash_table(ht);
Herbert Xub8244782015-03-24 00:50:26 +1100434
Thomas Graf97defe12015-01-02 23:00:20 +0100435 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100436
437 if (err)
438 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100439}
440
Herbert Xuca268932016-09-19 19:00:09 +0800441static int rhashtable_insert_rehash(struct rhashtable *ht,
442 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100443{
444 struct bucket_table *old_tbl;
445 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100446 unsigned int size;
447 int err;
448
449 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100450
451 size = tbl->size;
452
Herbert Xu3cf92222015-12-03 20:41:29 +0800453 err = -EBUSY;
454
Herbert Xuccd57b12015-03-24 00:50:28 +1100455 if (rht_grow_above_75(ht, tbl))
456 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200457 /* Do not schedule more than one rehash */
458 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800459 goto fail;
460
461 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100462
463 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
Herbert Xu3cf92222015-12-03 20:41:29 +0800464 if (new_tbl == NULL)
465 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100466
467 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
468 if (err) {
469 bucket_table_free(new_tbl);
470 if (err == -EEXIST)
471 err = 0;
472 } else
473 schedule_work(&ht->run_work);
474
475 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800476
477fail:
478 /* Do not fail the insert if someone else did a rehash. */
479 if (likely(rcu_dereference_raw(tbl->future_tbl)))
480 return 0;
481
482 /* Schedule async rehash to retry allocation in process context. */
483 if (err == -ENOMEM)
484 schedule_work(&ht->run_work);
485
486 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100487}
Herbert Xuccd57b12015-03-24 00:50:28 +1100488
Herbert Xuca268932016-09-19 19:00:09 +0800489static void *rhashtable_lookup_one(struct rhashtable *ht,
490 struct bucket_table *tbl, unsigned int hash,
491 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100492{
Herbert Xuca268932016-09-19 19:00:09 +0800493 struct rhashtable_compare_arg arg = {
494 .ht = ht,
495 .key = key,
496 };
497 struct rhash_head __rcu **pprev;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100498 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800499 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100500
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200501 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800502 pprev = rht_bucket_var(tbl, hash);
503 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800504 struct rhlist_head *list;
505 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100506
Herbert Xuca268932016-09-19 19:00:09 +0800507 elasticity--;
508 if (!key ||
509 (ht->p.obj_cmpfn ?
510 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200511 rhashtable_compare(&arg, rht_obj(ht, head)))) {
512 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800513 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200514 }
Herbert Xuca268932016-09-19 19:00:09 +0800515
516 if (!ht->rhlist)
517 return rht_obj(ht, head);
518
519 list = container_of(obj, struct rhlist_head, rhead);
520 plist = container_of(head, struct rhlist_head, rhead);
521
522 RCU_INIT_POINTER(list->next, plist);
523 head = rht_dereference_bucket(head->next, tbl, hash);
524 RCU_INIT_POINTER(list->rhead.next, head);
525 rcu_assign_pointer(*pprev, obj);
526
527 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200528 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100529
Herbert Xuca268932016-09-19 19:00:09 +0800530 if (elasticity <= 0)
531 return ERR_PTR(-EAGAIN);
532
533 return ERR_PTR(-ENOENT);
534}
535
536static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
537 struct bucket_table *tbl,
538 unsigned int hash,
539 struct rhash_head *obj,
540 void *data)
541{
Herbert Xuda204202017-02-11 19:26:47 +0800542 struct rhash_head __rcu **pprev;
Herbert Xuca268932016-09-19 19:00:09 +0800543 struct bucket_table *new_tbl;
544 struct rhash_head *head;
545
546 if (!IS_ERR_OR_NULL(data))
547 return ERR_PTR(-EEXIST);
548
549 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
550 return ERR_CAST(data);
551
552 new_tbl = rcu_dereference(tbl->future_tbl);
553 if (new_tbl)
554 return new_tbl;
555
556 if (PTR_ERR(data) != -ENOENT)
557 return ERR_CAST(data);
558
Herbert Xu07ee0722015-05-15 11:30:47 +0800559 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800560 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800561
Herbert Xuca268932016-09-19 19:00:09 +0800562 if (unlikely(rht_grow_above_100(ht, tbl)))
563 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100564
Herbert Xuda204202017-02-11 19:26:47 +0800565 pprev = rht_bucket_insert(ht, tbl, hash);
566 if (!pprev)
567 return ERR_PTR(-ENOMEM);
568
569 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100570
571 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800572 if (ht->rhlist) {
573 struct rhlist_head *list;
574
575 list = container_of(obj, struct rhlist_head, rhead);
576 RCU_INIT_POINTER(list->next, NULL);
577 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100578
Herbert Xuda204202017-02-11 19:26:47 +0800579 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100580
581 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800582 if (rht_grow_above_75(ht, tbl))
583 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100584
Herbert Xuca268932016-09-19 19:00:09 +0800585 return NULL;
586}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100587
Herbert Xuca268932016-09-19 19:00:09 +0800588static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
589 struct rhash_head *obj)
590{
591 struct bucket_table *new_tbl;
592 struct bucket_table *tbl;
593 unsigned int hash;
594 spinlock_t *lock;
595 void *data;
596
597 tbl = rcu_dereference(ht->tbl);
598
599 /* All insertions must grab the oldest table containing
600 * the hashed bucket that is yet to be rehashed.
601 */
602 for (;;) {
603 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
604 lock = rht_bucket_lock(tbl, hash);
605 spin_lock_bh(lock);
606
607 if (tbl->rehash <= hash)
608 break;
609
610 spin_unlock_bh(lock);
611 tbl = rcu_dereference(tbl->future_tbl);
612 }
613
614 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
615 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
616 if (PTR_ERR(new_tbl) != -EEXIST)
617 data = ERR_CAST(new_tbl);
618
619 while (!IS_ERR_OR_NULL(new_tbl)) {
620 tbl = new_tbl;
621 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
622 spin_lock_nested(rht_bucket_lock(tbl, hash),
623 SINGLE_DEPTH_NESTING);
624
625 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
626 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
627 if (PTR_ERR(new_tbl) != -EEXIST)
628 data = ERR_CAST(new_tbl);
629
630 spin_unlock(rht_bucket_lock(tbl, hash));
631 }
632
633 spin_unlock_bh(lock);
634
635 if (PTR_ERR(data) == -EAGAIN)
636 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
637 -EAGAIN);
638
639 return data;
640}
641
642void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
643 struct rhash_head *obj)
644{
645 void *data;
646
647 do {
648 rcu_read_lock();
649 data = rhashtable_try_insert(ht, key, obj);
650 rcu_read_unlock();
651 } while (PTR_ERR(data) == -EAGAIN);
652
653 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100654}
655EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
656
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100657/**
Herbert Xu246779d2016-08-18 16:50:56 +0800658 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100659 * @ht: Table to walk over
660 * @iter: Hash table Iterator
661 *
662 * This function prepares a hash table walk.
663 *
664 * Note that if you restart a walk after rhashtable_walk_stop you
665 * may see the same object twice. Also, you may miss objects if
666 * there are removals in between rhashtable_walk_stop and the next
667 * call to rhashtable_walk_start.
668 *
669 * For a completely stable walk you should construct your own data
670 * structure outside the hash table.
671 *
NeilBrown82266e92018-04-24 08:29:13 +1000672 * This function may be called from any process context, including
673 * non-preemptable context, but cannot be called from softirq or
674 * hardirq context.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100675 *
Herbert Xu246779d2016-08-18 16:50:56 +0800676 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100677 */
Herbert Xu246779d2016-08-18 16:50:56 +0800678void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100679{
680 iter->ht = ht;
681 iter->p = NULL;
682 iter->slot = 0;
683 iter->skip = 0;
Tom Herbert2db54b42017-12-04 10:31:42 -0800684 iter->end_of_table = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100685
Herbert Xuc6ff5262015-12-16 16:45:54 +0800686 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800687 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800688 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800689 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800690 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100691}
Herbert Xu246779d2016-08-18 16:50:56 +0800692EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100693
694/**
695 * rhashtable_walk_exit - Free an iterator
696 * @iter: Hash table Iterator
697 *
698 * This function frees resources allocated by rhashtable_walk_init.
699 */
700void rhashtable_walk_exit(struct rhashtable_iter *iter)
701{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800702 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800703 if (iter->walker.tbl)
704 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800705 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100706}
707EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
708
709/**
Tom Herbert97a6ec42017-12-04 10:31:41 -0800710 * rhashtable_walk_start_check - Start a hash table walk
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100711 * @iter: Hash table iterator
712 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200713 * Start a hash table walk at the current iterator position. Note that we take
714 * the RCU lock in all cases including when we return an error. So you must
715 * always call rhashtable_walk_stop to clean up.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100716 *
717 * Returns zero if successful.
718 *
719 * Returns -EAGAIN if resize event occured. Note that the iterator
720 * will rewind back to the beginning and you may use it immediately
721 * by calling rhashtable_walk_next.
Tom Herbert97a6ec42017-12-04 10:31:41 -0800722 *
723 * rhashtable_walk_start is defined as an inline variant that returns
724 * void. This is preferred in cases where the caller would ignore
725 * resize events and always continue.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100726 */
Tom Herbert97a6ec42017-12-04 10:31:41 -0800727int rhashtable_walk_start_check(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100728 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100729{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100730 struct rhashtable *ht = iter->ht;
NeilBrown5d240a82018-04-24 08:29:13 +1000731 bool rhlist = ht->rhlist;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100732
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100733 rcu_read_lock();
734
Herbert Xuc6ff5262015-12-16 16:45:54 +0800735 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800736 if (iter->walker.tbl)
737 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800738 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100739
NeilBrown5d240a82018-04-24 08:29:13 +1000740 if (iter->end_of_table)
741 return 0;
742 if (!iter->walker.tbl) {
Herbert Xu246779d2016-08-18 16:50:56 +0800743 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
NeilBrownb41cc042018-04-24 08:29:13 +1000744 iter->slot = 0;
745 iter->skip = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100746 return -EAGAIN;
747 }
748
NeilBrown5d240a82018-04-24 08:29:13 +1000749 if (iter->p && !rhlist) {
750 /*
751 * We need to validate that 'p' is still in the table, and
752 * if so, update 'skip'
753 */
754 struct rhash_head *p;
755 int skip = 0;
756 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
757 skip++;
758 if (p == iter->p) {
759 iter->skip = skip;
760 goto found;
761 }
762 }
763 iter->p = NULL;
764 } else if (iter->p && rhlist) {
765 /* Need to validate that 'list' is still in the table, and
766 * if so, update 'skip' and 'p'.
767 */
768 struct rhash_head *p;
769 struct rhlist_head *list;
770 int skip = 0;
771 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
772 for (list = container_of(p, struct rhlist_head, rhead);
773 list;
774 list = rcu_dereference(list->next)) {
775 skip++;
776 if (list == iter->list) {
777 iter->p = p;
778 skip = skip;
779 goto found;
780 }
781 }
782 }
783 iter->p = NULL;
784 }
785found:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100786 return 0;
787}
Tom Herbert97a6ec42017-12-04 10:31:41 -0800788EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100789
790/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800791 * __rhashtable_walk_find_next - Find the next element in a table (or the first
792 * one in case of a new walk).
793 *
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100794 * @iter: Hash table iterator
795 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800796 * Returns the found object or NULL when the end of the table is reached.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100797 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800798 * Returns -EAGAIN if resize event occurred.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100799 */
Tom Herbert2db54b42017-12-04 10:31:42 -0800800static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100801{
Herbert Xu246779d2016-08-18 16:50:56 +0800802 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xuca268932016-09-19 19:00:09 +0800803 struct rhlist_head *list = iter->list;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100804 struct rhashtable *ht = iter->ht;
805 struct rhash_head *p = iter->p;
Herbert Xuca268932016-09-19 19:00:09 +0800806 bool rhlist = ht->rhlist;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100807
Tom Herbert2db54b42017-12-04 10:31:42 -0800808 if (!tbl)
809 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100810
811 for (; iter->slot < tbl->size; iter->slot++) {
812 int skip = iter->skip;
813
814 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800815 if (rhlist) {
816 list = container_of(p, struct rhlist_head,
817 rhead);
818 do {
819 if (!skip)
820 goto next;
821 skip--;
822 list = rcu_dereference(list->next);
823 } while (list);
824
825 continue;
826 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100827 if (!skip)
828 break;
829 skip--;
830 }
831
832next:
833 if (!rht_is_a_nulls(p)) {
834 iter->skip++;
835 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800836 iter->list = list;
837 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100838 }
839
840 iter->skip = 0;
841 }
842
Phil Sutter142b9422015-07-06 15:51:20 +0200843 iter->p = NULL;
844
Herbert Xud88252f2015-03-24 00:50:19 +1100845 /* Ensure we see any new tables. */
846 smp_rmb();
847
Herbert Xu246779d2016-08-18 16:50:56 +0800848 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
849 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100850 iter->slot = 0;
851 iter->skip = 0;
852 return ERR_PTR(-EAGAIN);
Tom Herbert2db54b42017-12-04 10:31:42 -0800853 } else {
854 iter->end_of_table = true;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100855 }
856
Thomas Grafc936a792015-05-05 02:22:53 +0200857 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100858}
Tom Herbert2db54b42017-12-04 10:31:42 -0800859
860/**
861 * rhashtable_walk_next - Return the next object and advance the iterator
862 * @iter: Hash table iterator
863 *
864 * Note that you must call rhashtable_walk_stop when you are finished
865 * with the walk.
866 *
867 * Returns the next object or NULL when the end of the table is reached.
868 *
869 * Returns -EAGAIN if resize event occurred. Note that the iterator
870 * will rewind back to the beginning and you may continue to use it.
871 */
872void *rhashtable_walk_next(struct rhashtable_iter *iter)
873{
874 struct rhlist_head *list = iter->list;
875 struct rhashtable *ht = iter->ht;
876 struct rhash_head *p = iter->p;
877 bool rhlist = ht->rhlist;
878
879 if (p) {
880 if (!rhlist || !(list = rcu_dereference(list->next))) {
881 p = rcu_dereference(p->next);
882 list = container_of(p, struct rhlist_head, rhead);
883 }
884 if (!rht_is_a_nulls(p)) {
885 iter->skip++;
886 iter->p = p;
887 iter->list = list;
888 return rht_obj(ht, rhlist ? &list->rhead : p);
889 }
890
891 /* At the end of this slot, switch to next one and then find
892 * next entry from that point.
893 */
894 iter->skip = 0;
895 iter->slot++;
896 }
897
898 return __rhashtable_walk_find_next(iter);
899}
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100900EXPORT_SYMBOL_GPL(rhashtable_walk_next);
901
902/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800903 * rhashtable_walk_peek - Return the next object but don't advance the iterator
904 * @iter: Hash table iterator
905 *
906 * Returns the next object or NULL when the end of the table is reached.
907 *
908 * Returns -EAGAIN if resize event occurred. Note that the iterator
909 * will rewind back to the beginning and you may continue to use it.
910 */
911void *rhashtable_walk_peek(struct rhashtable_iter *iter)
912{
913 struct rhlist_head *list = iter->list;
914 struct rhashtable *ht = iter->ht;
915 struct rhash_head *p = iter->p;
916
917 if (p)
918 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
919
920 /* No object found in current iter, find next one in the table. */
921
922 if (iter->skip) {
923 /* A nonzero skip value points to the next entry in the table
924 * beyond that last one that was found. Decrement skip so
925 * we find the current value. __rhashtable_walk_find_next
926 * will restore the original value of skip assuming that
927 * the table hasn't changed.
928 */
929 iter->skip--;
930 }
931
932 return __rhashtable_walk_find_next(iter);
933}
934EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
935
936/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100937 * rhashtable_walk_stop - Finish a hash table walk
938 * @iter: Hash table iterator
939 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200940 * Finish a hash table walk. Does not reset the iterator to the start of the
941 * hash table.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100942 */
943void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100944 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100945{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100946 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800947 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100948
Herbert Xueddee5ba2015-03-14 13:57:20 +1100949 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100950 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100951
952 ht = iter->ht;
953
Herbert Xuba7c95e2015-03-24 09:53:17 +1100954 spin_lock(&ht->lock);
Herbert Xuc4db8842015-03-14 13:57:25 +1100955 if (tbl->rehash < tbl->size)
Herbert Xu246779d2016-08-18 16:50:56 +0800956 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100957 else
Herbert Xu246779d2016-08-18 16:50:56 +0800958 iter->walker.tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100959 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100960
Herbert Xu963ecbd2015-03-15 21:12:04 +1100961out:
962 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100963}
964EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
965
Herbert Xu488fb86e2015-03-20 21:56:59 +1100966static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200967{
Ying Xue94000172014-09-03 09:22:36 +0800968 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xue2e21c12015-03-18 20:01:21 +1100969 (unsigned long)params->min_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200970}
971
Herbert Xu31ccde22015-03-24 00:50:21 +1100972static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
973{
974 return jhash2(key, length, seed);
975}
976
Thomas Graf7e1e7762014-08-02 11:47:44 +0200977/**
978 * rhashtable_init - initialize a new hash table
979 * @ht: hash table to be initialized
980 * @params: configuration parameters
981 *
982 * Initializes a new hash table based on the provided configuration
983 * parameters. A table can be configured either with a variable or
984 * fixed length key:
985 *
986 * Configuration Example 1: Fixed length keys
987 * struct test_obj {
988 * int key;
989 * void * my_member;
990 * struct rhash_head node;
991 * };
992 *
993 * struct rhashtable_params params = {
994 * .head_offset = offsetof(struct test_obj, node),
995 * .key_offset = offsetof(struct test_obj, key),
996 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100997 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200998 * };
999 *
1000 * Configuration Example 2: Variable length keys
1001 * struct test_obj {
1002 * [...]
1003 * struct rhash_head node;
1004 * };
1005 *
Patrick McHardy49f7b332015-03-25 13:07:45 +00001006 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001007 * {
1008 * struct test_obj *obj = data;
1009 *
1010 * return [... hash ...];
1011 * }
1012 *
1013 * struct rhashtable_params params = {
1014 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001015 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001016 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001017 * };
1018 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001019int rhashtable_init(struct rhashtable *ht,
1020 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001021{
1022 struct bucket_table *tbl;
1023 size_t size;
1024
1025 size = HASH_DEFAULT_SIZE;
1026
Herbert Xu31ccde22015-03-24 00:50:21 +11001027 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001028 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001029 return -EINVAL;
1030
Thomas Graf97defe12015-01-02 23:00:20 +01001031 memset(ht, 0, sizeof(*ht));
1032 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001033 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001034 memcpy(&ht->p, params, sizeof(*params));
1035
Thomas Grafa998f712015-03-19 22:31:13 +00001036 if (params->min_size)
1037 ht->p.min_size = roundup_pow_of_two(params->min_size);
1038
Herbert Xu6d684e52017-04-27 13:44:51 +08001039 /* Cap total entries at 2^31 to avoid nelems overflow. */
1040 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001041
1042 if (params->max_size) {
1043 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1044 if (ht->p.max_size < ht->max_elems / 2)
1045 ht->max_elems = ht->p.max_size * 2;
1046 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001047
Florian Westphal48e75b432017-05-01 22:18:01 +02001048 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001049
Herbert Xu3a324602015-12-16 18:13:14 +08001050 if (params->nelem_hint)
1051 size = rounded_hashtable_size(&ht->p);
1052
Thomas Graf97defe12015-01-02 23:00:20 +01001053 if (params->locks_mul)
1054 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1055 else
1056 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1057
Herbert Xu31ccde22015-03-24 00:50:21 +11001058 ht->key_len = ht->p.key_len;
1059 if (!params->hashfn) {
1060 ht->p.hashfn = jhash;
1061
1062 if (!(ht->key_len & (sizeof(u32) - 1))) {
1063 ht->key_len /= sizeof(u32);
1064 ht->p.hashfn = rhashtable_jhash2;
1065 }
1066 }
1067
Herbert Xub9ecfda2015-03-24 00:50:27 +11001068 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001069 if (tbl == NULL)
1070 return -ENOMEM;
1071
Ying Xue545a1482015-01-07 13:41:57 +08001072 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001073
Thomas Graf7e1e7762014-08-02 11:47:44 +02001074 RCU_INIT_POINTER(ht->tbl, tbl);
1075
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001076 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001077
Thomas Graf7e1e7762014-08-02 11:47:44 +02001078 return 0;
1079}
1080EXPORT_SYMBOL_GPL(rhashtable_init);
1081
1082/**
Herbert Xuca268932016-09-19 19:00:09 +08001083 * rhltable_init - initialize a new hash list table
1084 * @hlt: hash list table to be initialized
1085 * @params: configuration parameters
1086 *
1087 * Initializes a new hash list table.
1088 *
1089 * See documentation for rhashtable_init.
1090 */
1091int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1092{
1093 int err;
1094
Herbert Xuca268932016-09-19 19:00:09 +08001095 err = rhashtable_init(&hlt->ht, params);
1096 hlt->ht.rhlist = true;
1097 return err;
1098}
1099EXPORT_SYMBOL_GPL(rhltable_init);
1100
1101static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1102 void (*free_fn)(void *ptr, void *arg),
1103 void *arg)
1104{
1105 struct rhlist_head *list;
1106
1107 if (!ht->rhlist) {
1108 free_fn(rht_obj(ht, obj), arg);
1109 return;
1110 }
1111
1112 list = container_of(obj, struct rhlist_head, rhead);
1113 do {
1114 obj = &list->rhead;
1115 list = rht_dereference(list->next, ht);
1116 free_fn(rht_obj(ht, obj), arg);
1117 } while (list);
1118}
1119
1120/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001121 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001122 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001123 * @free_fn: callback to release resources of element
1124 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001125 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001126 * Stops an eventual async resize. If defined, invokes free_fn for each
1127 * element to releasal resources. Please note that RCU protected
1128 * readers may still be accessing the elements. Releasing of resources
1129 * must occur in a compatible manner. Then frees the bucket array.
1130 *
1131 * This function will eventually sleep to wait for an async resize
1132 * to complete. The caller is responsible that no further write operations
1133 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001134 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001135void rhashtable_free_and_destroy(struct rhashtable *ht,
1136 void (*free_fn)(void *ptr, void *arg),
1137 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001138{
Herbert Xuda204202017-02-11 19:26:47 +08001139 struct bucket_table *tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001140 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001141
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001142 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001143
Thomas Graf97defe12015-01-02 23:00:20 +01001144 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001145 tbl = rht_dereference(ht->tbl, ht);
1146 if (free_fn) {
1147 for (i = 0; i < tbl->size; i++) {
1148 struct rhash_head *pos, *next;
1149
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001150 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +08001151 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001152 next = !rht_is_a_nulls(pos) ?
1153 rht_dereference(pos->next, ht) : NULL;
1154 !rht_is_a_nulls(pos);
1155 pos = next,
1156 next = !rht_is_a_nulls(pos) ?
1157 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001158 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001159 }
1160 }
1161
1162 bucket_table_free(tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001163 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001164}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001165EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1166
1167void rhashtable_destroy(struct rhashtable *ht)
1168{
1169 return rhashtable_free_and_destroy(ht, NULL, NULL);
1170}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001171EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001172
1173struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1174 unsigned int hash)
1175{
1176 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1177 static struct rhash_head __rcu *rhnull =
1178 (struct rhash_head __rcu *)NULLS_MARKER(0);
1179 unsigned int index = hash & ((1 << tbl->nest) - 1);
1180 unsigned int size = tbl->size >> tbl->nest;
1181 unsigned int subhash = hash;
1182 union nested_table *ntbl;
1183
1184 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
Herbert Xuc4d26032017-02-25 22:39:50 +08001185 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001186 subhash >>= tbl->nest;
1187
1188 while (ntbl && size > (1 << shift)) {
1189 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001190 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1191 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001192 size >>= shift;
1193 subhash >>= shift;
1194 }
1195
1196 if (!ntbl)
1197 return &rhnull;
1198
1199 return &ntbl[subhash].bucket;
1200
1201}
1202EXPORT_SYMBOL_GPL(rht_bucket_nested);
1203
1204struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1205 struct bucket_table *tbl,
1206 unsigned int hash)
1207{
1208 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1209 unsigned int index = hash & ((1 << tbl->nest) - 1);
1210 unsigned int size = tbl->size >> tbl->nest;
1211 union nested_table *ntbl;
1212 unsigned int shifted;
1213 unsigned int nhash;
1214
1215 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1216 hash >>= tbl->nest;
1217 nhash = index;
1218 shifted = tbl->nest;
1219 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1220 size <= (1 << shift) ? shifted : 0, nhash);
1221
1222 while (ntbl && size > (1 << shift)) {
1223 index = hash & ((1 << shift) - 1);
1224 size >>= shift;
1225 hash >>= shift;
1226 nhash |= index << shifted;
1227 shifted += shift;
1228 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1229 size <= (1 << shift) ? shifted : 0,
1230 nhash);
1231 }
1232
1233 if (!ntbl)
1234 return NULL;
1235
1236 return &ntbl[hash].bucket;
1237
1238}
1239EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);