blob: 6fc52d82efe6498b3937e259eb708a3e2034beab [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Herbert Xu02fd97c2015-03-20 21:57:00 +11004 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafa5ec68e2015-02-05 02:03:32 +01005 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02006 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02008 * Code partially derived from nft_hash
Herbert Xu02fd97c2015-03-20 21:57:00 +11009 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Herbert Xu07ee0722015-05-15 11:30:47 +080017#include <linux/atomic.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020018#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/log2.h>
Eric Dumazet5beb5c92015-02-26 07:20:34 -080021#include <linux/sched.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010022#include <linux/rculist.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020023#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010026#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020027#include <linux/random.h>
28#include <linux/rhashtable.h>
Stephen Rothwell61d7b092015-02-09 14:04:03 +110029#include <linux/err.h>
Hauke Mehrtens6d795412015-06-06 22:07:23 +020030#include <linux/export.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020031
32#define HASH_DEFAULT_SIZE 64UL
Herbert Xuc2e213c2015-03-18 20:01:16 +110033#define HASH_MIN_SIZE 4U
Florian Westphal4cf0b352016-08-12 12:03:52 +020034#define BUCKET_LOCKS_PER_CPU 32UL
Thomas Graf97defe12015-01-02 23:00:20 +010035
Herbert Xuda204202017-02-11 19:26:47 +080036union nested_table {
37 union nested_table __rcu *table;
38 struct rhash_head __rcu *bucket;
39};
40
Herbert Xu988dfbd2015-03-10 09:27:55 +110041static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010042 const struct bucket_table *tbl,
43 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020044{
Herbert Xu02fd97c2015-03-20 21:57:00 +110045 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020046}
47
Thomas Grafa03eaec2015-02-05 02:03:34 +010048#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010049#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010050
51int lockdep_rht_mutex_is_held(struct rhashtable *ht)
52{
53 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
54}
55EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
56
57int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
58{
Herbert Xu02fd97c2015-03-20 21:57:00 +110059 spinlock_t *lock = rht_bucket_lock(tbl, hash);
Thomas Grafa03eaec2015-02-05 02:03:34 +010060
61 return (debug_locks) ? lockdep_is_held(lock) : 1;
62}
63EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
64#else
65#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +010066#endif
67
68
Herbert Xub9ecfda2015-03-24 00:50:27 +110069static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl,
70 gfp_t gfp)
Thomas Graf97defe12015-01-02 23:00:20 +010071{
72 unsigned int i, size;
73#if defined(CONFIG_PROVE_LOCKING)
74 unsigned int nr_pcpus = 2;
75#else
76 unsigned int nr_pcpus = num_possible_cpus();
77#endif
78
Florian Westphal4cf0b352016-08-12 12:03:52 +020079 nr_pcpus = min_t(unsigned int, nr_pcpus, 64UL);
Thomas Graf97defe12015-01-02 23:00:20 +010080 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
81
Thomas Grafa5ec68e2015-02-05 02:03:32 +010082 /* Never allocate more than 0.5 locks per bucket */
83 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +010084
Herbert Xuda204202017-02-11 19:26:47 +080085 if (tbl->nest)
86 size = min(size, 1U << tbl->nest);
87
Thomas Graf97defe12015-01-02 23:00:20 +010088 if (sizeof(spinlock_t) != 0) {
Michal Hocko43ca5bc2017-05-08 15:57:18 -070089 if (gfpflags_allow_blocking(gfp))
90 tbl->locks = kvmalloc(size * sizeof(spinlock_t), gfp);
91 else
Eric Dumazet9dbeea72016-08-26 08:51:39 -070092 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
93 gfp);
Thomas Graf97defe12015-01-02 23:00:20 +010094 if (!tbl->locks)
95 return -ENOMEM;
96 for (i = 0; i < size; i++)
97 spin_lock_init(&tbl->locks[i]);
98 }
99 tbl->locks_mask = size - 1;
100
101 return 0;
102}
103
Herbert Xuda204202017-02-11 19:26:47 +0800104static void nested_table_free(union nested_table *ntbl, unsigned int size)
105{
106 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
107 const unsigned int len = 1 << shift;
108 unsigned int i;
109
110 ntbl = rcu_dereference_raw(ntbl->table);
111 if (!ntbl)
112 return;
113
114 if (size > len) {
115 size >>= shift;
116 for (i = 0; i < len; i++)
117 nested_table_free(ntbl + i, size);
118 }
119
120 kfree(ntbl);
121}
122
123static void nested_bucket_table_free(const struct bucket_table *tbl)
124{
125 unsigned int size = tbl->size >> tbl->nest;
126 unsigned int len = 1 << tbl->nest;
127 union nested_table *ntbl;
128 unsigned int i;
129
130 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
131
132 for (i = 0; i < len; i++)
133 nested_table_free(ntbl + i, size);
134
135 kfree(ntbl);
136}
137
Thomas Graf97defe12015-01-02 23:00:20 +0100138static void bucket_table_free(const struct bucket_table *tbl)
139{
Herbert Xuda204202017-02-11 19:26:47 +0800140 if (tbl->nest)
141 nested_bucket_table_free(tbl);
142
Herbert Xuca435402017-02-25 22:38:11 +0800143 kvfree(tbl->locks);
Thomas Graf97defe12015-01-02 23:00:20 +0100144 kvfree(tbl);
145}
146
Herbert Xu9d901bc2015-03-14 13:57:23 +1100147static void bucket_table_free_rcu(struct rcu_head *head)
148{
149 bucket_table_free(container_of(head, struct bucket_table, rcu));
150}
151
Herbert Xuda204202017-02-11 19:26:47 +0800152static union nested_table *nested_table_alloc(struct rhashtable *ht,
153 union nested_table __rcu **prev,
154 unsigned int shifted,
155 unsigned int nhash)
156{
157 union nested_table *ntbl;
158 int i;
159
160 ntbl = rcu_dereference(*prev);
161 if (ntbl)
162 return ntbl;
163
164 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
165
166 if (ntbl && shifted) {
167 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0].bucket); i++)
168 INIT_RHT_NULLS_HEAD(ntbl[i].bucket, ht,
169 (i << shifted) | nhash);
170 }
171
172 rcu_assign_pointer(*prev, ntbl);
173
174 return ntbl;
175}
176
177static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
178 size_t nbuckets,
179 gfp_t gfp)
180{
181 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
182 struct bucket_table *tbl;
183 size_t size;
184
185 if (nbuckets < (1 << (shift + 1)))
186 return NULL;
187
188 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
189
190 tbl = kzalloc(size, gfp);
191 if (!tbl)
192 return NULL;
193
194 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
195 0, 0)) {
196 kfree(tbl);
197 return NULL;
198 }
199
200 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
201
202 return tbl;
203}
204
Thomas Graf97defe12015-01-02 23:00:20 +0100205static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xub9ecfda2015-03-24 00:50:27 +1100206 size_t nbuckets,
207 gfp_t gfp)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200208{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100209 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200210 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100211 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200212
213 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Michal Hocko12e8fd62017-07-10 15:51:55 -0700214 if (gfp != GFP_KERNEL)
Herbert Xub9ecfda2015-03-24 00:50:27 +1100215 tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
Michal Hocko12e8fd62017-07-10 15:51:55 -0700216 else
217 tbl = kvzalloc(size, gfp);
Herbert Xuda204202017-02-11 19:26:47 +0800218
219 size = nbuckets;
220
221 if (tbl == NULL && gfp != GFP_KERNEL) {
222 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
223 nbuckets = 0;
224 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200225 if (tbl == NULL)
226 return NULL;
227
Herbert Xuda204202017-02-11 19:26:47 +0800228 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200229
Herbert Xub9ecfda2015-03-24 00:50:27 +1100230 if (alloc_bucket_locks(ht, tbl, gfp) < 0) {
Thomas Graf97defe12015-01-02 23:00:20 +0100231 bucket_table_free(tbl);
232 return NULL;
233 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200234
Herbert Xueddee5ba2015-03-14 13:57:20 +1100235 INIT_LIST_HEAD(&tbl->walkers);
236
Jason A. Donenfeldd48ad082017-06-07 22:47:13 -0400237 tbl->hash_rnd = get_random_u32();
Herbert Xu5269b532015-03-14 13:57:22 +1100238
Thomas Graff89bd6f2015-01-02 23:00:21 +0100239 for (i = 0; i < nbuckets; i++)
240 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
241
Thomas Graf97defe12015-01-02 23:00:20 +0100242 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200243}
244
Herbert Xub8244782015-03-24 00:50:26 +1100245static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
246 struct bucket_table *tbl)
247{
248 struct bucket_table *new_tbl;
249
250 do {
251 new_tbl = tbl;
252 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
253 } while (tbl);
254
255 return new_tbl;
256}
257
Thomas Graf299e5c32015-03-24 14:18:17 +0100258static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100259{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100260 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100261 struct bucket_table *new_tbl = rhashtable_last_table(ht,
262 rht_dereference_rcu(old_tbl->future_tbl, ht));
Herbert Xuda204202017-02-11 19:26:47 +0800263 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
264 int err = -EAGAIN;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100265 struct rhash_head *head, *next, *entry;
266 spinlock_t *new_bucket_lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100267 unsigned int new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100268
Herbert Xuda204202017-02-11 19:26:47 +0800269 if (new_tbl->nest)
270 goto out;
271
272 err = -ENOENT;
273
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100274 rht_for_each(entry, old_tbl, old_hash) {
275 err = 0;
276 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100277
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100278 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200279 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100280
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100281 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200282 }
283
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100284 if (err)
285 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100286
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100287 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100288
Herbert Xu02fd97c2015-03-20 21:57:00 +1100289 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100290
Herbert Xu8f2484b2015-03-14 13:57:21 +1100291 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100292 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
293 new_tbl, new_hash);
294
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200295 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100296
297 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
298 spin_unlock(new_bucket_lock);
299
300 rcu_assign_pointer(*pprev, next);
301
302out:
303 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100304}
305
Herbert Xuda204202017-02-11 19:26:47 +0800306static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100307 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100308{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100309 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
310 spinlock_t *old_bucket_lock;
Herbert Xuda204202017-02-11 19:26:47 +0800311 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100312
Herbert Xu02fd97c2015-03-20 21:57:00 +1100313 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100314
315 spin_lock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800316 while (!(err = rhashtable_rehash_one(ht, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100317 ;
Herbert Xuda204202017-02-11 19:26:47 +0800318
319 if (err == -ENOENT) {
320 old_tbl->rehash++;
321 err = 0;
322 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100323 spin_unlock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800324
325 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100326}
327
Herbert Xub8244782015-03-24 00:50:26 +1100328static int rhashtable_rehash_attach(struct rhashtable *ht,
329 struct bucket_table *old_tbl,
330 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100331{
Herbert Xub8244782015-03-24 00:50:26 +1100332 /* Protect future_tbl using the first bucket lock. */
333 spin_lock_bh(old_tbl->locks);
334
335 /* Did somebody beat us to it? */
336 if (rcu_access_pointer(old_tbl->future_tbl)) {
337 spin_unlock_bh(old_tbl->locks);
338 return -EEXIST;
339 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100340
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100341 /* Make insertions go into the new, empty table right away. Deletions
342 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100343 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100344 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100345
Herbert Xub8244782015-03-24 00:50:26 +1100346 spin_unlock_bh(old_tbl->locks);
347
348 return 0;
349}
350
351static int rhashtable_rehash_table(struct rhashtable *ht)
352{
353 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
354 struct bucket_table *new_tbl;
355 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100356 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800357 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100358
359 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
360 if (!new_tbl)
361 return 0;
362
Herbert Xuda204202017-02-11 19:26:47 +0800363 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
364 err = rhashtable_rehash_chain(ht, old_hash);
365 if (err)
366 return err;
367 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100368
369 /* Publish the new table pointer. */
370 rcu_assign_pointer(ht->tbl, new_tbl);
371
Herbert Xuba7c95e2015-03-24 09:53:17 +1100372 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100373 list_for_each_entry(walker, &old_tbl->walkers, list)
374 walker->tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100375 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100376
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100377 /* Wait for readers. All new readers will see the new
378 * table, and thus no references to the old table will
379 * remain.
380 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100381 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Herbert Xub8244782015-03-24 00:50:26 +1100382
383 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200384}
385
Herbert Xuda204202017-02-11 19:26:47 +0800386static int rhashtable_rehash_alloc(struct rhashtable *ht,
387 struct bucket_table *old_tbl,
388 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200389{
Herbert Xuda204202017-02-11 19:26:47 +0800390 struct bucket_table *new_tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100391 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200392
393 ASSERT_RHT_MUTEX(ht);
394
Herbert Xuda204202017-02-11 19:26:47 +0800395 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200396 if (new_tbl == NULL)
397 return -ENOMEM;
398
Herbert Xub8244782015-03-24 00:50:26 +1100399 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
400 if (err)
401 bucket_table_free(new_tbl);
402
403 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200404}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200405
406/**
407 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
408 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200409 *
Herbert Xu18093d12015-03-24 00:50:25 +1100410 * This function shrinks the hash table to fit, i.e., the smallest
411 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200412 *
Thomas Graf97defe12015-01-02 23:00:20 +0100413 * The caller must ensure that no concurrent resizing occurs by holding
414 * ht->mutex.
415 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200416 * The caller must ensure that no concurrent table mutations take place.
417 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100418 *
419 * It is valid to have concurrent insertions and deletions protected by per
420 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200421 */
Herbert Xub8244782015-03-24 00:50:26 +1100422static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200423{
Herbert Xuda204202017-02-11 19:26:47 +0800424 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200425 unsigned int nelems = atomic_read(&ht->nelems);
426 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200427
Vegard Nossum12311952016-08-12 20:10:44 +0200428 if (nelems)
429 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100430 if (size < ht->p.min_size)
431 size = ht->p.min_size;
432
433 if (old_tbl->size <= size)
434 return 0;
435
Herbert Xub8244782015-03-24 00:50:26 +1100436 if (rht_dereference(old_tbl->future_tbl, ht))
437 return -EEXIST;
438
Herbert Xuda204202017-02-11 19:26:47 +0800439 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200440}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200441
Thomas Graf97defe12015-01-02 23:00:20 +0100442static void rht_deferred_worker(struct work_struct *work)
443{
444 struct rhashtable *ht;
445 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100446 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100447
Ying Xue57699a42015-01-16 11:13:09 +0800448 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100449 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100450
Thomas Graf97defe12015-01-02 23:00:20 +0100451 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100452 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100453
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100454 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800455 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000456 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800457 err = rhashtable_shrink(ht);
458 else if (tbl->nest)
459 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub8244782015-03-24 00:50:26 +1100460
Herbert Xuda204202017-02-11 19:26:47 +0800461 if (!err)
462 err = rhashtable_rehash_table(ht);
Herbert Xub8244782015-03-24 00:50:26 +1100463
Thomas Graf97defe12015-01-02 23:00:20 +0100464 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100465
466 if (err)
467 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100468}
469
Herbert Xuca268932016-09-19 19:00:09 +0800470static int rhashtable_insert_rehash(struct rhashtable *ht,
471 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100472{
473 struct bucket_table *old_tbl;
474 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100475 unsigned int size;
476 int err;
477
478 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100479
480 size = tbl->size;
481
Herbert Xu3cf92222015-12-03 20:41:29 +0800482 err = -EBUSY;
483
Herbert Xuccd57b12015-03-24 00:50:28 +1100484 if (rht_grow_above_75(ht, tbl))
485 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200486 /* Do not schedule more than one rehash */
487 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800488 goto fail;
489
490 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100491
492 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
Herbert Xu3cf92222015-12-03 20:41:29 +0800493 if (new_tbl == NULL)
494 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100495
496 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
497 if (err) {
498 bucket_table_free(new_tbl);
499 if (err == -EEXIST)
500 err = 0;
501 } else
502 schedule_work(&ht->run_work);
503
504 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800505
506fail:
507 /* Do not fail the insert if someone else did a rehash. */
508 if (likely(rcu_dereference_raw(tbl->future_tbl)))
509 return 0;
510
511 /* Schedule async rehash to retry allocation in process context. */
512 if (err == -ENOMEM)
513 schedule_work(&ht->run_work);
514
515 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100516}
Herbert Xuccd57b12015-03-24 00:50:28 +1100517
Herbert Xuca268932016-09-19 19:00:09 +0800518static void *rhashtable_lookup_one(struct rhashtable *ht,
519 struct bucket_table *tbl, unsigned int hash,
520 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100521{
Herbert Xuca268932016-09-19 19:00:09 +0800522 struct rhashtable_compare_arg arg = {
523 .ht = ht,
524 .key = key,
525 };
526 struct rhash_head __rcu **pprev;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100527 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800528 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100529
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200530 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800531 pprev = rht_bucket_var(tbl, hash);
532 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800533 struct rhlist_head *list;
534 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100535
Herbert Xuca268932016-09-19 19:00:09 +0800536 elasticity--;
537 if (!key ||
538 (ht->p.obj_cmpfn ?
539 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
540 rhashtable_compare(&arg, rht_obj(ht, head))))
541 continue;
542
543 if (!ht->rhlist)
544 return rht_obj(ht, head);
545
546 list = container_of(obj, struct rhlist_head, rhead);
547 plist = container_of(head, struct rhlist_head, rhead);
548
549 RCU_INIT_POINTER(list->next, plist);
550 head = rht_dereference_bucket(head->next, tbl, hash);
551 RCU_INIT_POINTER(list->rhead.next, head);
552 rcu_assign_pointer(*pprev, obj);
553
554 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200555 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100556
Herbert Xuca268932016-09-19 19:00:09 +0800557 if (elasticity <= 0)
558 return ERR_PTR(-EAGAIN);
559
560 return ERR_PTR(-ENOENT);
561}
562
563static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
564 struct bucket_table *tbl,
565 unsigned int hash,
566 struct rhash_head *obj,
567 void *data)
568{
Herbert Xuda204202017-02-11 19:26:47 +0800569 struct rhash_head __rcu **pprev;
Herbert Xuca268932016-09-19 19:00:09 +0800570 struct bucket_table *new_tbl;
571 struct rhash_head *head;
572
573 if (!IS_ERR_OR_NULL(data))
574 return ERR_PTR(-EEXIST);
575
576 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
577 return ERR_CAST(data);
578
579 new_tbl = rcu_dereference(tbl->future_tbl);
580 if (new_tbl)
581 return new_tbl;
582
583 if (PTR_ERR(data) != -ENOENT)
584 return ERR_CAST(data);
585
Herbert Xu07ee0722015-05-15 11:30:47 +0800586 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800587 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800588
Herbert Xuca268932016-09-19 19:00:09 +0800589 if (unlikely(rht_grow_above_100(ht, tbl)))
590 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100591
Herbert Xuda204202017-02-11 19:26:47 +0800592 pprev = rht_bucket_insert(ht, tbl, hash);
593 if (!pprev)
594 return ERR_PTR(-ENOMEM);
595
596 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100597
598 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800599 if (ht->rhlist) {
600 struct rhlist_head *list;
601
602 list = container_of(obj, struct rhlist_head, rhead);
603 RCU_INIT_POINTER(list->next, NULL);
604 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100605
Herbert Xuda204202017-02-11 19:26:47 +0800606 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100607
608 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800609 if (rht_grow_above_75(ht, tbl))
610 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100611
Herbert Xuca268932016-09-19 19:00:09 +0800612 return NULL;
613}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100614
Herbert Xuca268932016-09-19 19:00:09 +0800615static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
616 struct rhash_head *obj)
617{
618 struct bucket_table *new_tbl;
619 struct bucket_table *tbl;
620 unsigned int hash;
621 spinlock_t *lock;
622 void *data;
623
624 tbl = rcu_dereference(ht->tbl);
625
626 /* All insertions must grab the oldest table containing
627 * the hashed bucket that is yet to be rehashed.
628 */
629 for (;;) {
630 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
631 lock = rht_bucket_lock(tbl, hash);
632 spin_lock_bh(lock);
633
634 if (tbl->rehash <= hash)
635 break;
636
637 spin_unlock_bh(lock);
638 tbl = rcu_dereference(tbl->future_tbl);
639 }
640
641 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
642 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
643 if (PTR_ERR(new_tbl) != -EEXIST)
644 data = ERR_CAST(new_tbl);
645
646 while (!IS_ERR_OR_NULL(new_tbl)) {
647 tbl = new_tbl;
648 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
649 spin_lock_nested(rht_bucket_lock(tbl, hash),
650 SINGLE_DEPTH_NESTING);
651
652 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
653 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
654 if (PTR_ERR(new_tbl) != -EEXIST)
655 data = ERR_CAST(new_tbl);
656
657 spin_unlock(rht_bucket_lock(tbl, hash));
658 }
659
660 spin_unlock_bh(lock);
661
662 if (PTR_ERR(data) == -EAGAIN)
663 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
664 -EAGAIN);
665
666 return data;
667}
668
669void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
670 struct rhash_head *obj)
671{
672 void *data;
673
674 do {
675 rcu_read_lock();
676 data = rhashtable_try_insert(ht, key, obj);
677 rcu_read_unlock();
678 } while (PTR_ERR(data) == -EAGAIN);
679
680 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100681}
682EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
683
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100684/**
Herbert Xu246779d2016-08-18 16:50:56 +0800685 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100686 * @ht: Table to walk over
687 * @iter: Hash table Iterator
688 *
689 * This function prepares a hash table walk.
690 *
691 * Note that if you restart a walk after rhashtable_walk_stop you
692 * may see the same object twice. Also, you may miss objects if
693 * there are removals in between rhashtable_walk_stop and the next
694 * call to rhashtable_walk_start.
695 *
696 * For a completely stable walk you should construct your own data
697 * structure outside the hash table.
698 *
699 * This function may sleep so you must not call it from interrupt
700 * context or with spin locks held.
701 *
Herbert Xu246779d2016-08-18 16:50:56 +0800702 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100703 */
Herbert Xu246779d2016-08-18 16:50:56 +0800704void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100705{
706 iter->ht = ht;
707 iter->p = NULL;
708 iter->slot = 0;
709 iter->skip = 0;
Tom Herbert2db54b42017-12-04 10:31:42 -0800710 iter->end_of_table = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100711
Herbert Xuc6ff5262015-12-16 16:45:54 +0800712 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800713 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800714 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800715 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800716 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100717}
Herbert Xu246779d2016-08-18 16:50:56 +0800718EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100719
720/**
721 * rhashtable_walk_exit - Free an iterator
722 * @iter: Hash table Iterator
723 *
724 * This function frees resources allocated by rhashtable_walk_init.
725 */
726void rhashtable_walk_exit(struct rhashtable_iter *iter)
727{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800728 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800729 if (iter->walker.tbl)
730 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800731 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100732}
733EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
734
735/**
Tom Herbert97a6ec42017-12-04 10:31:41 -0800736 * rhashtable_walk_start_check - Start a hash table walk
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100737 * @iter: Hash table iterator
738 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200739 * Start a hash table walk at the current iterator position. Note that we take
740 * the RCU lock in all cases including when we return an error. So you must
741 * always call rhashtable_walk_stop to clean up.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100742 *
743 * Returns zero if successful.
744 *
745 * Returns -EAGAIN if resize event occured. Note that the iterator
746 * will rewind back to the beginning and you may use it immediately
747 * by calling rhashtable_walk_next.
Tom Herbert97a6ec42017-12-04 10:31:41 -0800748 *
749 * rhashtable_walk_start is defined as an inline variant that returns
750 * void. This is preferred in cases where the caller would ignore
751 * resize events and always continue.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100752 */
Tom Herbert97a6ec42017-12-04 10:31:41 -0800753int rhashtable_walk_start_check(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100754 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100755{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100756 struct rhashtable *ht = iter->ht;
757
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100758 rcu_read_lock();
759
Herbert Xuc6ff5262015-12-16 16:45:54 +0800760 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800761 if (iter->walker.tbl)
762 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800763 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100764
Tom Herbert2db54b42017-12-04 10:31:42 -0800765 if (!iter->walker.tbl && !iter->end_of_table) {
Herbert Xu246779d2016-08-18 16:50:56 +0800766 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100767 return -EAGAIN;
768 }
769
770 return 0;
771}
Tom Herbert97a6ec42017-12-04 10:31:41 -0800772EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100773
774/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800775 * __rhashtable_walk_find_next - Find the next element in a table (or the first
776 * one in case of a new walk).
777 *
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100778 * @iter: Hash table iterator
779 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800780 * Returns the found object or NULL when the end of the table is reached.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100781 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800782 * Returns -EAGAIN if resize event occurred.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100783 */
Tom Herbert2db54b42017-12-04 10:31:42 -0800784static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100785{
Herbert Xu246779d2016-08-18 16:50:56 +0800786 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xuca268932016-09-19 19:00:09 +0800787 struct rhlist_head *list = iter->list;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100788 struct rhashtable *ht = iter->ht;
789 struct rhash_head *p = iter->p;
Herbert Xuca268932016-09-19 19:00:09 +0800790 bool rhlist = ht->rhlist;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100791
Tom Herbert2db54b42017-12-04 10:31:42 -0800792 if (!tbl)
793 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100794
795 for (; iter->slot < tbl->size; iter->slot++) {
796 int skip = iter->skip;
797
798 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800799 if (rhlist) {
800 list = container_of(p, struct rhlist_head,
801 rhead);
802 do {
803 if (!skip)
804 goto next;
805 skip--;
806 list = rcu_dereference(list->next);
807 } while (list);
808
809 continue;
810 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100811 if (!skip)
812 break;
813 skip--;
814 }
815
816next:
817 if (!rht_is_a_nulls(p)) {
818 iter->skip++;
819 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800820 iter->list = list;
821 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100822 }
823
824 iter->skip = 0;
825 }
826
Phil Sutter142b9422015-07-06 15:51:20 +0200827 iter->p = NULL;
828
Herbert Xud88252f2015-03-24 00:50:19 +1100829 /* Ensure we see any new tables. */
830 smp_rmb();
831
Herbert Xu246779d2016-08-18 16:50:56 +0800832 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
833 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100834 iter->slot = 0;
835 iter->skip = 0;
836 return ERR_PTR(-EAGAIN);
Tom Herbert2db54b42017-12-04 10:31:42 -0800837 } else {
838 iter->end_of_table = true;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100839 }
840
Thomas Grafc936a792015-05-05 02:22:53 +0200841 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100842}
Tom Herbert2db54b42017-12-04 10:31:42 -0800843
844/**
845 * rhashtable_walk_next - Return the next object and advance the iterator
846 * @iter: Hash table iterator
847 *
848 * Note that you must call rhashtable_walk_stop when you are finished
849 * with the walk.
850 *
851 * Returns the next object or NULL when the end of the table is reached.
852 *
853 * Returns -EAGAIN if resize event occurred. Note that the iterator
854 * will rewind back to the beginning and you may continue to use it.
855 */
856void *rhashtable_walk_next(struct rhashtable_iter *iter)
857{
858 struct rhlist_head *list = iter->list;
859 struct rhashtable *ht = iter->ht;
860 struct rhash_head *p = iter->p;
861 bool rhlist = ht->rhlist;
862
863 if (p) {
864 if (!rhlist || !(list = rcu_dereference(list->next))) {
865 p = rcu_dereference(p->next);
866 list = container_of(p, struct rhlist_head, rhead);
867 }
868 if (!rht_is_a_nulls(p)) {
869 iter->skip++;
870 iter->p = p;
871 iter->list = list;
872 return rht_obj(ht, rhlist ? &list->rhead : p);
873 }
874
875 /* At the end of this slot, switch to next one and then find
876 * next entry from that point.
877 */
878 iter->skip = 0;
879 iter->slot++;
880 }
881
882 return __rhashtable_walk_find_next(iter);
883}
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100884EXPORT_SYMBOL_GPL(rhashtable_walk_next);
885
886/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800887 * rhashtable_walk_peek - Return the next object but don't advance the iterator
888 * @iter: Hash table iterator
889 *
890 * Returns the next object or NULL when the end of the table is reached.
891 *
892 * Returns -EAGAIN if resize event occurred. Note that the iterator
893 * will rewind back to the beginning and you may continue to use it.
894 */
895void *rhashtable_walk_peek(struct rhashtable_iter *iter)
896{
897 struct rhlist_head *list = iter->list;
898 struct rhashtable *ht = iter->ht;
899 struct rhash_head *p = iter->p;
900
901 if (p)
902 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
903
904 /* No object found in current iter, find next one in the table. */
905
906 if (iter->skip) {
907 /* A nonzero skip value points to the next entry in the table
908 * beyond that last one that was found. Decrement skip so
909 * we find the current value. __rhashtable_walk_find_next
910 * will restore the original value of skip assuming that
911 * the table hasn't changed.
912 */
913 iter->skip--;
914 }
915
916 return __rhashtable_walk_find_next(iter);
917}
918EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
919
920/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100921 * rhashtable_walk_stop - Finish a hash table walk
922 * @iter: Hash table iterator
923 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200924 * Finish a hash table walk. Does not reset the iterator to the start of the
925 * hash table.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100926 */
927void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100928 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100929{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100930 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800931 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100932
Herbert Xueddee5ba2015-03-14 13:57:20 +1100933 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100934 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100935
936 ht = iter->ht;
937
Herbert Xuba7c95e2015-03-24 09:53:17 +1100938 spin_lock(&ht->lock);
Herbert Xuc4db8842015-03-14 13:57:25 +1100939 if (tbl->rehash < tbl->size)
Herbert Xu246779d2016-08-18 16:50:56 +0800940 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100941 else
Herbert Xu246779d2016-08-18 16:50:56 +0800942 iter->walker.tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100943 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100944
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100945 iter->p = NULL;
Herbert Xu963ecbd2015-03-15 21:12:04 +1100946
947out:
948 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100949}
950EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
951
Herbert Xu488fb86e2015-03-20 21:56:59 +1100952static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200953{
Ying Xue94000172014-09-03 09:22:36 +0800954 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xue2e21c12015-03-18 20:01:21 +1100955 (unsigned long)params->min_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200956}
957
Herbert Xu31ccde22015-03-24 00:50:21 +1100958static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
959{
960 return jhash2(key, length, seed);
961}
962
Thomas Graf7e1e7762014-08-02 11:47:44 +0200963/**
964 * rhashtable_init - initialize a new hash table
965 * @ht: hash table to be initialized
966 * @params: configuration parameters
967 *
968 * Initializes a new hash table based on the provided configuration
969 * parameters. A table can be configured either with a variable or
970 * fixed length key:
971 *
972 * Configuration Example 1: Fixed length keys
973 * struct test_obj {
974 * int key;
975 * void * my_member;
976 * struct rhash_head node;
977 * };
978 *
979 * struct rhashtable_params params = {
980 * .head_offset = offsetof(struct test_obj, node),
981 * .key_offset = offsetof(struct test_obj, key),
982 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100983 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100984 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200985 * };
986 *
987 * Configuration Example 2: Variable length keys
988 * struct test_obj {
989 * [...]
990 * struct rhash_head node;
991 * };
992 *
Patrick McHardy49f7b332015-03-25 13:07:45 +0000993 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200994 * {
995 * struct test_obj *obj = data;
996 *
997 * return [... hash ...];
998 * }
999 *
1000 * struct rhashtable_params params = {
1001 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001002 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001003 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001004 * };
1005 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001006int rhashtable_init(struct rhashtable *ht,
1007 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001008{
1009 struct bucket_table *tbl;
1010 size_t size;
1011
1012 size = HASH_DEFAULT_SIZE;
1013
Herbert Xu31ccde22015-03-24 00:50:21 +11001014 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001015 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001016 return -EINVAL;
1017
Thomas Graff89bd6f2015-01-02 23:00:21 +01001018 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
1019 return -EINVAL;
1020
Thomas Graf97defe12015-01-02 23:00:20 +01001021 memset(ht, 0, sizeof(*ht));
1022 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001023 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001024 memcpy(&ht->p, params, sizeof(*params));
1025
Thomas Grafa998f712015-03-19 22:31:13 +00001026 if (params->min_size)
1027 ht->p.min_size = roundup_pow_of_two(params->min_size);
1028
Herbert Xu6d684e52017-04-27 13:44:51 +08001029 /* Cap total entries at 2^31 to avoid nelems overflow. */
1030 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001031
1032 if (params->max_size) {
1033 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1034 if (ht->p.max_size < ht->max_elems / 2)
1035 ht->max_elems = ht->p.max_size * 2;
1036 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001037
Florian Westphal48e75b432017-05-01 22:18:01 +02001038 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001039
Herbert Xu3a324602015-12-16 18:13:14 +08001040 if (params->nelem_hint)
1041 size = rounded_hashtable_size(&ht->p);
1042
Thomas Graf97defe12015-01-02 23:00:20 +01001043 if (params->locks_mul)
1044 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1045 else
1046 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1047
Herbert Xu31ccde22015-03-24 00:50:21 +11001048 ht->key_len = ht->p.key_len;
1049 if (!params->hashfn) {
1050 ht->p.hashfn = jhash;
1051
1052 if (!(ht->key_len & (sizeof(u32) - 1))) {
1053 ht->key_len /= sizeof(u32);
1054 ht->p.hashfn = rhashtable_jhash2;
1055 }
1056 }
1057
Herbert Xub9ecfda2015-03-24 00:50:27 +11001058 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001059 if (tbl == NULL)
1060 return -ENOMEM;
1061
Ying Xue545a1482015-01-07 13:41:57 +08001062 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001063
Thomas Graf7e1e7762014-08-02 11:47:44 +02001064 RCU_INIT_POINTER(ht->tbl, tbl);
1065
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001066 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001067
Thomas Graf7e1e7762014-08-02 11:47:44 +02001068 return 0;
1069}
1070EXPORT_SYMBOL_GPL(rhashtable_init);
1071
1072/**
Herbert Xuca268932016-09-19 19:00:09 +08001073 * rhltable_init - initialize a new hash list table
1074 * @hlt: hash list table to be initialized
1075 * @params: configuration parameters
1076 *
1077 * Initializes a new hash list table.
1078 *
1079 * See documentation for rhashtable_init.
1080 */
1081int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1082{
1083 int err;
1084
1085 /* No rhlist NULLs marking for now. */
1086 if (params->nulls_base)
1087 return -EINVAL;
1088
1089 err = rhashtable_init(&hlt->ht, params);
1090 hlt->ht.rhlist = true;
1091 return err;
1092}
1093EXPORT_SYMBOL_GPL(rhltable_init);
1094
1095static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1096 void (*free_fn)(void *ptr, void *arg),
1097 void *arg)
1098{
1099 struct rhlist_head *list;
1100
1101 if (!ht->rhlist) {
1102 free_fn(rht_obj(ht, obj), arg);
1103 return;
1104 }
1105
1106 list = container_of(obj, struct rhlist_head, rhead);
1107 do {
1108 obj = &list->rhead;
1109 list = rht_dereference(list->next, ht);
1110 free_fn(rht_obj(ht, obj), arg);
1111 } while (list);
1112}
1113
1114/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001115 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001116 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001117 * @free_fn: callback to release resources of element
1118 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001119 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001120 * Stops an eventual async resize. If defined, invokes free_fn for each
1121 * element to releasal resources. Please note that RCU protected
1122 * readers may still be accessing the elements. Releasing of resources
1123 * must occur in a compatible manner. Then frees the bucket array.
1124 *
1125 * This function will eventually sleep to wait for an async resize
1126 * to complete. The caller is responsible that no further write operations
1127 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001128 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001129void rhashtable_free_and_destroy(struct rhashtable *ht,
1130 void (*free_fn)(void *ptr, void *arg),
1131 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001132{
Herbert Xuda204202017-02-11 19:26:47 +08001133 struct bucket_table *tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001134 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001135
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001136 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001137
Thomas Graf97defe12015-01-02 23:00:20 +01001138 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001139 tbl = rht_dereference(ht->tbl, ht);
1140 if (free_fn) {
1141 for (i = 0; i < tbl->size; i++) {
1142 struct rhash_head *pos, *next;
1143
Herbert Xuda204202017-02-11 19:26:47 +08001144 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001145 next = !rht_is_a_nulls(pos) ?
1146 rht_dereference(pos->next, ht) : NULL;
1147 !rht_is_a_nulls(pos);
1148 pos = next,
1149 next = !rht_is_a_nulls(pos) ?
1150 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001151 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001152 }
1153 }
1154
1155 bucket_table_free(tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001156 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001157}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001158EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1159
1160void rhashtable_destroy(struct rhashtable *ht)
1161{
1162 return rhashtable_free_and_destroy(ht, NULL, NULL);
1163}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001164EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001165
1166struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1167 unsigned int hash)
1168{
1169 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1170 static struct rhash_head __rcu *rhnull =
1171 (struct rhash_head __rcu *)NULLS_MARKER(0);
1172 unsigned int index = hash & ((1 << tbl->nest) - 1);
1173 unsigned int size = tbl->size >> tbl->nest;
1174 unsigned int subhash = hash;
1175 union nested_table *ntbl;
1176
1177 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
Herbert Xuc4d26032017-02-25 22:39:50 +08001178 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001179 subhash >>= tbl->nest;
1180
1181 while (ntbl && size > (1 << shift)) {
1182 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001183 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1184 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001185 size >>= shift;
1186 subhash >>= shift;
1187 }
1188
1189 if (!ntbl)
1190 return &rhnull;
1191
1192 return &ntbl[subhash].bucket;
1193
1194}
1195EXPORT_SYMBOL_GPL(rht_bucket_nested);
1196
1197struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1198 struct bucket_table *tbl,
1199 unsigned int hash)
1200{
1201 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1202 unsigned int index = hash & ((1 << tbl->nest) - 1);
1203 unsigned int size = tbl->size >> tbl->nest;
1204 union nested_table *ntbl;
1205 unsigned int shifted;
1206 unsigned int nhash;
1207
1208 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1209 hash >>= tbl->nest;
1210 nhash = index;
1211 shifted = tbl->nest;
1212 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1213 size <= (1 << shift) ? shifted : 0, nhash);
1214
1215 while (ntbl && size > (1 << shift)) {
1216 index = hash & ((1 << shift) - 1);
1217 size >>= shift;
1218 hash >>= shift;
1219 nhash |= index << shifted;
1220 shifted += shift;
1221 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1222 size <= (1 << shift) ? shifted : 0,
1223 nhash);
1224 }
1225
1226 if (!ntbl)
1227 return NULL;
1228
1229 return &ntbl[hash].bucket;
1230
1231}
1232EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);