blob: c9fafea7dc6e92ff40f2d25bb4733b34a4144870 [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 Graff89bd6f2015-01-02 23:00:21 +0100998 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200999 * };
1000 *
1001 * Configuration Example 2: Variable length keys
1002 * struct test_obj {
1003 * [...]
1004 * struct rhash_head node;
1005 * };
1006 *
Patrick McHardy49f7b332015-03-25 13:07:45 +00001007 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001008 * {
1009 * struct test_obj *obj = data;
1010 *
1011 * return [... hash ...];
1012 * }
1013 *
1014 * struct rhashtable_params params = {
1015 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001016 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001017 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001018 * };
1019 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001020int rhashtable_init(struct rhashtable *ht,
1021 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001022{
1023 struct bucket_table *tbl;
1024 size_t size;
1025
1026 size = HASH_DEFAULT_SIZE;
1027
Herbert Xu31ccde22015-03-24 00:50:21 +11001028 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001029 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001030 return -EINVAL;
1031
Thomas Graff89bd6f2015-01-02 23:00:21 +01001032 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
1033 return -EINVAL;
1034
Thomas Graf97defe12015-01-02 23:00:20 +01001035 memset(ht, 0, sizeof(*ht));
1036 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001037 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001038 memcpy(&ht->p, params, sizeof(*params));
1039
Thomas Grafa998f712015-03-19 22:31:13 +00001040 if (params->min_size)
1041 ht->p.min_size = roundup_pow_of_two(params->min_size);
1042
Herbert Xu6d684e52017-04-27 13:44:51 +08001043 /* Cap total entries at 2^31 to avoid nelems overflow. */
1044 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001045
1046 if (params->max_size) {
1047 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1048 if (ht->p.max_size < ht->max_elems / 2)
1049 ht->max_elems = ht->p.max_size * 2;
1050 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001051
Florian Westphal48e75b432017-05-01 22:18:01 +02001052 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001053
Herbert Xu3a324602015-12-16 18:13:14 +08001054 if (params->nelem_hint)
1055 size = rounded_hashtable_size(&ht->p);
1056
Thomas Graf97defe12015-01-02 23:00:20 +01001057 if (params->locks_mul)
1058 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1059 else
1060 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1061
Herbert Xu31ccde22015-03-24 00:50:21 +11001062 ht->key_len = ht->p.key_len;
1063 if (!params->hashfn) {
1064 ht->p.hashfn = jhash;
1065
1066 if (!(ht->key_len & (sizeof(u32) - 1))) {
1067 ht->key_len /= sizeof(u32);
1068 ht->p.hashfn = rhashtable_jhash2;
1069 }
1070 }
1071
Herbert Xub9ecfda2015-03-24 00:50:27 +11001072 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001073 if (tbl == NULL)
1074 return -ENOMEM;
1075
Ying Xue545a1482015-01-07 13:41:57 +08001076 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001077
Thomas Graf7e1e7762014-08-02 11:47:44 +02001078 RCU_INIT_POINTER(ht->tbl, tbl);
1079
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001080 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001081
Thomas Graf7e1e7762014-08-02 11:47:44 +02001082 return 0;
1083}
1084EXPORT_SYMBOL_GPL(rhashtable_init);
1085
1086/**
Herbert Xuca268932016-09-19 19:00:09 +08001087 * rhltable_init - initialize a new hash list table
1088 * @hlt: hash list table to be initialized
1089 * @params: configuration parameters
1090 *
1091 * Initializes a new hash list table.
1092 *
1093 * See documentation for rhashtable_init.
1094 */
1095int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1096{
1097 int err;
1098
1099 /* No rhlist NULLs marking for now. */
1100 if (params->nulls_base)
1101 return -EINVAL;
1102
1103 err = rhashtable_init(&hlt->ht, params);
1104 hlt->ht.rhlist = true;
1105 return err;
1106}
1107EXPORT_SYMBOL_GPL(rhltable_init);
1108
1109static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1110 void (*free_fn)(void *ptr, void *arg),
1111 void *arg)
1112{
1113 struct rhlist_head *list;
1114
1115 if (!ht->rhlist) {
1116 free_fn(rht_obj(ht, obj), arg);
1117 return;
1118 }
1119
1120 list = container_of(obj, struct rhlist_head, rhead);
1121 do {
1122 obj = &list->rhead;
1123 list = rht_dereference(list->next, ht);
1124 free_fn(rht_obj(ht, obj), arg);
1125 } while (list);
1126}
1127
1128/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001129 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001130 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001131 * @free_fn: callback to release resources of element
1132 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001133 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001134 * Stops an eventual async resize. If defined, invokes free_fn for each
1135 * element to releasal resources. Please note that RCU protected
1136 * readers may still be accessing the elements. Releasing of resources
1137 * must occur in a compatible manner. Then frees the bucket array.
1138 *
1139 * This function will eventually sleep to wait for an async resize
1140 * to complete. The caller is responsible that no further write operations
1141 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001142 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001143void rhashtable_free_and_destroy(struct rhashtable *ht,
1144 void (*free_fn)(void *ptr, void *arg),
1145 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001146{
Herbert Xuda204202017-02-11 19:26:47 +08001147 struct bucket_table *tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001148 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001149
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001150 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001151
Thomas Graf97defe12015-01-02 23:00:20 +01001152 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001153 tbl = rht_dereference(ht->tbl, ht);
1154 if (free_fn) {
1155 for (i = 0; i < tbl->size; i++) {
1156 struct rhash_head *pos, *next;
1157
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001158 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +08001159 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001160 next = !rht_is_a_nulls(pos) ?
1161 rht_dereference(pos->next, ht) : NULL;
1162 !rht_is_a_nulls(pos);
1163 pos = next,
1164 next = !rht_is_a_nulls(pos) ?
1165 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001166 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001167 }
1168 }
1169
1170 bucket_table_free(tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001171 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001172}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001173EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1174
1175void rhashtable_destroy(struct rhashtable *ht)
1176{
1177 return rhashtable_free_and_destroy(ht, NULL, NULL);
1178}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001179EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001180
1181struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1182 unsigned int hash)
1183{
1184 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1185 static struct rhash_head __rcu *rhnull =
1186 (struct rhash_head __rcu *)NULLS_MARKER(0);
1187 unsigned int index = hash & ((1 << tbl->nest) - 1);
1188 unsigned int size = tbl->size >> tbl->nest;
1189 unsigned int subhash = hash;
1190 union nested_table *ntbl;
1191
1192 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
Herbert Xuc4d26032017-02-25 22:39:50 +08001193 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001194 subhash >>= tbl->nest;
1195
1196 while (ntbl && size > (1 << shift)) {
1197 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001198 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1199 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001200 size >>= shift;
1201 subhash >>= shift;
1202 }
1203
1204 if (!ntbl)
1205 return &rhnull;
1206
1207 return &ntbl[subhash].bucket;
1208
1209}
1210EXPORT_SYMBOL_GPL(rht_bucket_nested);
1211
1212struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1213 struct bucket_table *tbl,
1214 unsigned int hash)
1215{
1216 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1217 unsigned int index = hash & ((1 << tbl->nest) - 1);
1218 unsigned int size = tbl->size >> tbl->nest;
1219 union nested_table *ntbl;
1220 unsigned int shifted;
1221 unsigned int nhash;
1222
1223 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1224 hash >>= tbl->nest;
1225 nhash = index;
1226 shifted = tbl->nest;
1227 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1228 size <= (1 << shift) ? shifted : 0, nhash);
1229
1230 while (ntbl && size > (1 << shift)) {
1231 index = hash & ((1 << shift) - 1);
1232 size >>= shift;
1233 hash >>= shift;
1234 nhash |= index << shifted;
1235 shifted += shift;
1236 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1237 size <= (1 << shift) ? shifted : 0,
1238 nhash);
1239 }
1240
1241 if (!ntbl)
1242 return NULL;
1243
1244 return &ntbl[hash].bucket;
1245
1246}
1247EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);