blob: 0e04947b7e0c588fbcdce89128d6fb3140b9c8d3 [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,
NeilBrown5af68ef2018-06-18 12:52:50 +1000119 bool leaf)
Herbert Xuda204202017-02-11 19:26:47 +0800120{
121 union nested_table *ntbl;
122 int i;
123
124 ntbl = rcu_dereference(*prev);
125 if (ntbl)
126 return ntbl;
127
128 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
129
NeilBrown5af68ef2018-06-18 12:52:50 +1000130 if (ntbl && leaf) {
131 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000132 INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
Herbert Xuda204202017-02-11 19:26:47 +0800133 }
134
135 rcu_assign_pointer(*prev, ntbl);
136
137 return ntbl;
138}
139
140static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
141 size_t nbuckets,
142 gfp_t gfp)
143{
144 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
145 struct bucket_table *tbl;
146 size_t size;
147
148 if (nbuckets < (1 << (shift + 1)))
149 return NULL;
150
151 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
152
153 tbl = kzalloc(size, gfp);
154 if (!tbl)
155 return NULL;
156
157 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
NeilBrown5af68ef2018-06-18 12:52:50 +1000158 false)) {
Herbert Xuda204202017-02-11 19:26:47 +0800159 kfree(tbl);
160 return NULL;
161 }
162
163 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
164
165 return tbl;
166}
167
Thomas Graf97defe12015-01-02 23:00:20 +0100168static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xub9ecfda2015-03-24 00:50:27 +1100169 size_t nbuckets,
170 gfp_t gfp)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200171{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100172 struct bucket_table *tbl = NULL;
Tom Herbert64e0cd02017-12-04 10:31:45 -0800173 size_t size, max_locks;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100174 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200175
176 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Michal Hocko12e8fd62017-07-10 15:51:55 -0700177 if (gfp != GFP_KERNEL)
Herbert Xub9ecfda2015-03-24 00:50:27 +1100178 tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
Michal Hocko12e8fd62017-07-10 15:51:55 -0700179 else
180 tbl = kvzalloc(size, gfp);
Herbert Xuda204202017-02-11 19:26:47 +0800181
182 size = nbuckets;
183
184 if (tbl == NULL && gfp != GFP_KERNEL) {
185 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
186 nbuckets = 0;
187 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200188 if (tbl == NULL)
189 return NULL;
190
Herbert Xuda204202017-02-11 19:26:47 +0800191 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200192
Tom Herbert64e0cd02017-12-04 10:31:45 -0800193 max_locks = size >> 1;
194 if (tbl->nest)
195 max_locks = min_t(size_t, max_locks, 1U << tbl->nest);
196
197 if (alloc_bucket_spinlocks(&tbl->locks, &tbl->locks_mask, max_locks,
198 ht->p.locks_mul, gfp) < 0) {
Thomas Graf97defe12015-01-02 23:00:20 +0100199 bucket_table_free(tbl);
200 return NULL;
201 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200202
Herbert Xueddee5ba2015-03-14 13:57:20 +1100203 INIT_LIST_HEAD(&tbl->walkers);
204
Jason A. Donenfeldd48ad082017-06-07 22:47:13 -0400205 tbl->hash_rnd = get_random_u32();
Herbert Xu5269b532015-03-14 13:57:22 +1100206
Thomas Graff89bd6f2015-01-02 23:00:21 +0100207 for (i = 0; i < nbuckets; i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000208 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100209
Thomas Graf97defe12015-01-02 23:00:20 +0100210 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200211}
212
Herbert Xub8244782015-03-24 00:50:26 +1100213static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
214 struct bucket_table *tbl)
215{
216 struct bucket_table *new_tbl;
217
218 do {
219 new_tbl = tbl;
220 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
221 } while (tbl);
222
223 return new_tbl;
224}
225
Thomas Graf299e5c32015-03-24 14:18:17 +0100226static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100227{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100228 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
NeilBrownc0690012018-06-18 12:52:50 +1000229 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
Herbert Xuda204202017-02-11 19:26:47 +0800230 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
231 int err = -EAGAIN;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100232 struct rhash_head *head, *next, *entry;
233 spinlock_t *new_bucket_lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100234 unsigned int new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100235
Herbert Xuda204202017-02-11 19:26:47 +0800236 if (new_tbl->nest)
237 goto out;
238
239 err = -ENOENT;
240
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100241 rht_for_each(entry, old_tbl, old_hash) {
242 err = 0;
243 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100244
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100245 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200246 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100247
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100248 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200249 }
250
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100251 if (err)
252 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100253
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100254 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100255
Herbert Xu02fd97c2015-03-20 21:57:00 +1100256 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100257
Herbert Xu8f2484b2015-03-14 13:57:21 +1100258 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100259 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
260 new_tbl, new_hash);
261
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200262 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100263
264 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
265 spin_unlock(new_bucket_lock);
266
267 rcu_assign_pointer(*pprev, next);
268
269out:
270 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100271}
272
Herbert Xuda204202017-02-11 19:26:47 +0800273static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100274 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100275{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100276 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
277 spinlock_t *old_bucket_lock;
Herbert Xuda204202017-02-11 19:26:47 +0800278 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100279
Herbert Xu02fd97c2015-03-20 21:57:00 +1100280 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100281
282 spin_lock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800283 while (!(err = rhashtable_rehash_one(ht, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100284 ;
Herbert Xuda204202017-02-11 19:26:47 +0800285
286 if (err == -ENOENT) {
287 old_tbl->rehash++;
288 err = 0;
289 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100290 spin_unlock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800291
292 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100293}
294
Herbert Xub8244782015-03-24 00:50:26 +1100295static int rhashtable_rehash_attach(struct rhashtable *ht,
296 struct bucket_table *old_tbl,
297 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100298{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100299 /* Make insertions go into the new, empty table right away. Deletions
300 * and lookups will be attempted in both tables until we synchronize.
NeilBrown0ad66442018-06-18 12:52:50 +1000301 * As cmpxchg() provides strong barriers, we do not need
302 * rcu_assign_pointer().
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100303 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100304
NeilBrown0ad66442018-06-18 12:52:50 +1000305 if (cmpxchg(&old_tbl->future_tbl, NULL, new_tbl) != NULL)
306 return -EEXIST;
Herbert Xub8244782015-03-24 00:50:26 +1100307
308 return 0;
309}
310
311static int rhashtable_rehash_table(struct rhashtable *ht)
312{
313 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
314 struct bucket_table *new_tbl;
315 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100316 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800317 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100318
319 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
320 if (!new_tbl)
321 return 0;
322
Herbert Xuda204202017-02-11 19:26:47 +0800323 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
324 err = rhashtable_rehash_chain(ht, old_hash);
325 if (err)
326 return err;
Eric Dumazetae6da1f2018-03-31 12:58:48 -0700327 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +0800328 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100329
330 /* Publish the new table pointer. */
331 rcu_assign_pointer(ht->tbl, new_tbl);
332
Herbert Xuba7c95e2015-03-24 09:53:17 +1100333 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100334 list_for_each_entry(walker, &old_tbl->walkers, list)
335 walker->tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100336 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100337
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100338 /* Wait for readers. All new readers will see the new
339 * table, and thus no references to the old table will
340 * remain.
341 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100342 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Herbert Xub8244782015-03-24 00:50:26 +1100343
344 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200345}
346
Herbert Xuda204202017-02-11 19:26:47 +0800347static int rhashtable_rehash_alloc(struct rhashtable *ht,
348 struct bucket_table *old_tbl,
349 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200350{
Herbert Xuda204202017-02-11 19:26:47 +0800351 struct bucket_table *new_tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100352 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200353
354 ASSERT_RHT_MUTEX(ht);
355
Herbert Xuda204202017-02-11 19:26:47 +0800356 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200357 if (new_tbl == NULL)
358 return -ENOMEM;
359
Herbert Xub8244782015-03-24 00:50:26 +1100360 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
361 if (err)
362 bucket_table_free(new_tbl);
363
364 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200365}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200366
367/**
368 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
369 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200370 *
Herbert Xu18093d12015-03-24 00:50:25 +1100371 * This function shrinks the hash table to fit, i.e., the smallest
372 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200373 *
Thomas Graf97defe12015-01-02 23:00:20 +0100374 * The caller must ensure that no concurrent resizing occurs by holding
375 * ht->mutex.
376 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200377 * The caller must ensure that no concurrent table mutations take place.
378 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100379 *
380 * It is valid to have concurrent insertions and deletions protected by per
381 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200382 */
Herbert Xub8244782015-03-24 00:50:26 +1100383static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200384{
Herbert Xuda204202017-02-11 19:26:47 +0800385 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200386 unsigned int nelems = atomic_read(&ht->nelems);
387 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200388
Vegard Nossum12311952016-08-12 20:10:44 +0200389 if (nelems)
390 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100391 if (size < ht->p.min_size)
392 size = ht->p.min_size;
393
394 if (old_tbl->size <= size)
395 return 0;
396
Herbert Xub8244782015-03-24 00:50:26 +1100397 if (rht_dereference(old_tbl->future_tbl, ht))
398 return -EEXIST;
399
Herbert Xuda204202017-02-11 19:26:47 +0800400 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200401}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200402
Thomas Graf97defe12015-01-02 23:00:20 +0100403static void rht_deferred_worker(struct work_struct *work)
404{
405 struct rhashtable *ht;
406 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100407 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100408
Ying Xue57699a42015-01-16 11:13:09 +0800409 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100410 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100411
Thomas Graf97defe12015-01-02 23:00:20 +0100412 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100413 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100414
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100415 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800416 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000417 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800418 err = rhashtable_shrink(ht);
419 else if (tbl->nest)
420 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub8244782015-03-24 00:50:26 +1100421
Herbert Xuda204202017-02-11 19:26:47 +0800422 if (!err)
423 err = rhashtable_rehash_table(ht);
Herbert Xub8244782015-03-24 00:50:26 +1100424
Thomas Graf97defe12015-01-02 23:00:20 +0100425 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100426
427 if (err)
428 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100429}
430
Herbert Xuca268932016-09-19 19:00:09 +0800431static int rhashtable_insert_rehash(struct rhashtable *ht,
432 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100433{
434 struct bucket_table *old_tbl;
435 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100436 unsigned int size;
437 int err;
438
439 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100440
441 size = tbl->size;
442
Herbert Xu3cf92222015-12-03 20:41:29 +0800443 err = -EBUSY;
444
Herbert Xuccd57b12015-03-24 00:50:28 +1100445 if (rht_grow_above_75(ht, tbl))
446 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200447 /* Do not schedule more than one rehash */
448 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800449 goto fail;
450
451 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100452
453 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
Herbert Xu3cf92222015-12-03 20:41:29 +0800454 if (new_tbl == NULL)
455 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100456
457 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
458 if (err) {
459 bucket_table_free(new_tbl);
460 if (err == -EEXIST)
461 err = 0;
462 } else
463 schedule_work(&ht->run_work);
464
465 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800466
467fail:
468 /* Do not fail the insert if someone else did a rehash. */
NeilBrownc0690012018-06-18 12:52:50 +1000469 if (likely(rcu_access_pointer(tbl->future_tbl)))
Herbert Xu3cf92222015-12-03 20:41:29 +0800470 return 0;
471
472 /* Schedule async rehash to retry allocation in process context. */
473 if (err == -ENOMEM)
474 schedule_work(&ht->run_work);
475
476 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100477}
Herbert Xuccd57b12015-03-24 00:50:28 +1100478
Herbert Xuca268932016-09-19 19:00:09 +0800479static void *rhashtable_lookup_one(struct rhashtable *ht,
480 struct bucket_table *tbl, unsigned int hash,
481 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100482{
Herbert Xuca268932016-09-19 19:00:09 +0800483 struct rhashtable_compare_arg arg = {
484 .ht = ht,
485 .key = key,
486 };
487 struct rhash_head __rcu **pprev;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100488 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800489 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100490
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200491 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800492 pprev = rht_bucket_var(tbl, hash);
493 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800494 struct rhlist_head *list;
495 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100496
Herbert Xuca268932016-09-19 19:00:09 +0800497 elasticity--;
498 if (!key ||
499 (ht->p.obj_cmpfn ?
500 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200501 rhashtable_compare(&arg, rht_obj(ht, head)))) {
502 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800503 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200504 }
Herbert Xuca268932016-09-19 19:00:09 +0800505
506 if (!ht->rhlist)
507 return rht_obj(ht, head);
508
509 list = container_of(obj, struct rhlist_head, rhead);
510 plist = container_of(head, struct rhlist_head, rhead);
511
512 RCU_INIT_POINTER(list->next, plist);
513 head = rht_dereference_bucket(head->next, tbl, hash);
514 RCU_INIT_POINTER(list->rhead.next, head);
515 rcu_assign_pointer(*pprev, obj);
516
517 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200518 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100519
Herbert Xuca268932016-09-19 19:00:09 +0800520 if (elasticity <= 0)
521 return ERR_PTR(-EAGAIN);
522
523 return ERR_PTR(-ENOENT);
524}
525
526static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
527 struct bucket_table *tbl,
528 unsigned int hash,
529 struct rhash_head *obj,
530 void *data)
531{
Herbert Xuda204202017-02-11 19:26:47 +0800532 struct rhash_head __rcu **pprev;
Herbert Xuca268932016-09-19 19:00:09 +0800533 struct bucket_table *new_tbl;
534 struct rhash_head *head;
535
536 if (!IS_ERR_OR_NULL(data))
537 return ERR_PTR(-EEXIST);
538
539 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
540 return ERR_CAST(data);
541
NeilBrownc0690012018-06-18 12:52:50 +1000542 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800543 if (new_tbl)
544 return new_tbl;
545
546 if (PTR_ERR(data) != -ENOENT)
547 return ERR_CAST(data);
548
Herbert Xu07ee0722015-05-15 11:30:47 +0800549 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800550 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800551
Herbert Xuca268932016-09-19 19:00:09 +0800552 if (unlikely(rht_grow_above_100(ht, tbl)))
553 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100554
Herbert Xuda204202017-02-11 19:26:47 +0800555 pprev = rht_bucket_insert(ht, tbl, hash);
556 if (!pprev)
557 return ERR_PTR(-ENOMEM);
558
559 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100560
561 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800562 if (ht->rhlist) {
563 struct rhlist_head *list;
564
565 list = container_of(obj, struct rhlist_head, rhead);
566 RCU_INIT_POINTER(list->next, NULL);
567 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100568
Herbert Xuda204202017-02-11 19:26:47 +0800569 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100570
571 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800572 if (rht_grow_above_75(ht, tbl))
573 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100574
Herbert Xuca268932016-09-19 19:00:09 +0800575 return NULL;
576}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100577
Herbert Xuca268932016-09-19 19:00:09 +0800578static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
579 struct rhash_head *obj)
580{
581 struct bucket_table *new_tbl;
582 struct bucket_table *tbl;
583 unsigned int hash;
584 spinlock_t *lock;
585 void *data;
586
587 tbl = rcu_dereference(ht->tbl);
588
589 /* All insertions must grab the oldest table containing
590 * the hashed bucket that is yet to be rehashed.
591 */
592 for (;;) {
593 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
594 lock = rht_bucket_lock(tbl, hash);
595 spin_lock_bh(lock);
596
597 if (tbl->rehash <= hash)
598 break;
599
600 spin_unlock_bh(lock);
NeilBrownc0690012018-06-18 12:52:50 +1000601 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800602 }
603
604 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
605 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
606 if (PTR_ERR(new_tbl) != -EEXIST)
607 data = ERR_CAST(new_tbl);
608
609 while (!IS_ERR_OR_NULL(new_tbl)) {
610 tbl = new_tbl;
611 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
612 spin_lock_nested(rht_bucket_lock(tbl, hash),
613 SINGLE_DEPTH_NESTING);
614
615 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
616 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
617 if (PTR_ERR(new_tbl) != -EEXIST)
618 data = ERR_CAST(new_tbl);
619
620 spin_unlock(rht_bucket_lock(tbl, hash));
621 }
622
623 spin_unlock_bh(lock);
624
625 if (PTR_ERR(data) == -EAGAIN)
626 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
627 -EAGAIN);
628
629 return data;
630}
631
632void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
633 struct rhash_head *obj)
634{
635 void *data;
636
637 do {
638 rcu_read_lock();
639 data = rhashtable_try_insert(ht, key, obj);
640 rcu_read_unlock();
641 } while (PTR_ERR(data) == -EAGAIN);
642
643 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100644}
645EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
646
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100647/**
Herbert Xu246779d2016-08-18 16:50:56 +0800648 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100649 * @ht: Table to walk over
650 * @iter: Hash table Iterator
651 *
652 * This function prepares a hash table walk.
653 *
654 * Note that if you restart a walk after rhashtable_walk_stop you
655 * may see the same object twice. Also, you may miss objects if
656 * there are removals in between rhashtable_walk_stop and the next
657 * call to rhashtable_walk_start.
658 *
659 * For a completely stable walk you should construct your own data
660 * structure outside the hash table.
661 *
NeilBrown82266e92018-04-24 08:29:13 +1000662 * This function may be called from any process context, including
663 * non-preemptable context, but cannot be called from softirq or
664 * hardirq context.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100665 *
Herbert Xu246779d2016-08-18 16:50:56 +0800666 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100667 */
Herbert Xu246779d2016-08-18 16:50:56 +0800668void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100669{
670 iter->ht = ht;
671 iter->p = NULL;
672 iter->slot = 0;
673 iter->skip = 0;
Tom Herbert2db54b42017-12-04 10:31:42 -0800674 iter->end_of_table = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100675
Herbert Xuc6ff5262015-12-16 16:45:54 +0800676 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800677 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800678 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800679 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800680 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100681}
Herbert Xu246779d2016-08-18 16:50:56 +0800682EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100683
684/**
685 * rhashtable_walk_exit - Free an iterator
686 * @iter: Hash table Iterator
687 *
688 * This function frees resources allocated by rhashtable_walk_init.
689 */
690void rhashtable_walk_exit(struct rhashtable_iter *iter)
691{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800692 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800693 if (iter->walker.tbl)
694 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800695 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100696}
697EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
698
699/**
Tom Herbert97a6ec42017-12-04 10:31:41 -0800700 * rhashtable_walk_start_check - Start a hash table walk
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100701 * @iter: Hash table iterator
702 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200703 * Start a hash table walk at the current iterator position. Note that we take
704 * the RCU lock in all cases including when we return an error. So you must
705 * always call rhashtable_walk_stop to clean up.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100706 *
707 * Returns zero if successful.
708 *
709 * Returns -EAGAIN if resize event occured. Note that the iterator
710 * will rewind back to the beginning and you may use it immediately
711 * by calling rhashtable_walk_next.
Tom Herbert97a6ec42017-12-04 10:31:41 -0800712 *
713 * rhashtable_walk_start is defined as an inline variant that returns
714 * void. This is preferred in cases where the caller would ignore
715 * resize events and always continue.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100716 */
Tom Herbert97a6ec42017-12-04 10:31:41 -0800717int rhashtable_walk_start_check(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100718 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100719{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100720 struct rhashtable *ht = iter->ht;
NeilBrown5d240a82018-04-24 08:29:13 +1000721 bool rhlist = ht->rhlist;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100722
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100723 rcu_read_lock();
724
Herbert Xuc6ff5262015-12-16 16:45:54 +0800725 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800726 if (iter->walker.tbl)
727 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800728 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100729
NeilBrown5d240a82018-04-24 08:29:13 +1000730 if (iter->end_of_table)
731 return 0;
732 if (!iter->walker.tbl) {
Herbert Xu246779d2016-08-18 16:50:56 +0800733 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
NeilBrownb41cc042018-04-24 08:29:13 +1000734 iter->slot = 0;
735 iter->skip = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100736 return -EAGAIN;
737 }
738
NeilBrown5d240a82018-04-24 08:29:13 +1000739 if (iter->p && !rhlist) {
740 /*
741 * We need to validate that 'p' is still in the table, and
742 * if so, update 'skip'
743 */
744 struct rhash_head *p;
745 int skip = 0;
746 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
747 skip++;
748 if (p == iter->p) {
749 iter->skip = skip;
750 goto found;
751 }
752 }
753 iter->p = NULL;
754 } else if (iter->p && rhlist) {
755 /* Need to validate that 'list' is still in the table, and
756 * if so, update 'skip' and 'p'.
757 */
758 struct rhash_head *p;
759 struct rhlist_head *list;
760 int skip = 0;
761 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
762 for (list = container_of(p, struct rhlist_head, rhead);
763 list;
764 list = rcu_dereference(list->next)) {
765 skip++;
766 if (list == iter->list) {
767 iter->p = p;
768 skip = skip;
769 goto found;
770 }
771 }
772 }
773 iter->p = NULL;
774 }
775found:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100776 return 0;
777}
Tom Herbert97a6ec42017-12-04 10:31:41 -0800778EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100779
780/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800781 * __rhashtable_walk_find_next - Find the next element in a table (or the first
782 * one in case of a new walk).
783 *
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100784 * @iter: Hash table iterator
785 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800786 * Returns the found object or NULL when the end of the table is reached.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100787 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800788 * Returns -EAGAIN if resize event occurred.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100789 */
Tom Herbert2db54b42017-12-04 10:31:42 -0800790static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100791{
Herbert Xu246779d2016-08-18 16:50:56 +0800792 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xuca268932016-09-19 19:00:09 +0800793 struct rhlist_head *list = iter->list;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100794 struct rhashtable *ht = iter->ht;
795 struct rhash_head *p = iter->p;
Herbert Xuca268932016-09-19 19:00:09 +0800796 bool rhlist = ht->rhlist;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100797
Tom Herbert2db54b42017-12-04 10:31:42 -0800798 if (!tbl)
799 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100800
801 for (; iter->slot < tbl->size; iter->slot++) {
802 int skip = iter->skip;
803
804 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800805 if (rhlist) {
806 list = container_of(p, struct rhlist_head,
807 rhead);
808 do {
809 if (!skip)
810 goto next;
811 skip--;
812 list = rcu_dereference(list->next);
813 } while (list);
814
815 continue;
816 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100817 if (!skip)
818 break;
819 skip--;
820 }
821
822next:
823 if (!rht_is_a_nulls(p)) {
824 iter->skip++;
825 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800826 iter->list = list;
827 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100828 }
829
830 iter->skip = 0;
831 }
832
Phil Sutter142b9422015-07-06 15:51:20 +0200833 iter->p = NULL;
834
Herbert Xud88252f2015-03-24 00:50:19 +1100835 /* Ensure we see any new tables. */
836 smp_rmb();
837
Herbert Xu246779d2016-08-18 16:50:56 +0800838 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
839 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100840 iter->slot = 0;
841 iter->skip = 0;
842 return ERR_PTR(-EAGAIN);
Tom Herbert2db54b42017-12-04 10:31:42 -0800843 } else {
844 iter->end_of_table = true;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100845 }
846
Thomas Grafc936a792015-05-05 02:22:53 +0200847 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100848}
Tom Herbert2db54b42017-12-04 10:31:42 -0800849
850/**
851 * rhashtable_walk_next - Return the next object and advance the iterator
852 * @iter: Hash table iterator
853 *
854 * Note that you must call rhashtable_walk_stop when you are finished
855 * with the walk.
856 *
857 * Returns the next object or NULL when the end of the table is reached.
858 *
859 * Returns -EAGAIN if resize event occurred. Note that the iterator
860 * will rewind back to the beginning and you may continue to use it.
861 */
862void *rhashtable_walk_next(struct rhashtable_iter *iter)
863{
864 struct rhlist_head *list = iter->list;
865 struct rhashtable *ht = iter->ht;
866 struct rhash_head *p = iter->p;
867 bool rhlist = ht->rhlist;
868
869 if (p) {
870 if (!rhlist || !(list = rcu_dereference(list->next))) {
871 p = rcu_dereference(p->next);
872 list = container_of(p, struct rhlist_head, rhead);
873 }
874 if (!rht_is_a_nulls(p)) {
875 iter->skip++;
876 iter->p = p;
877 iter->list = list;
878 return rht_obj(ht, rhlist ? &list->rhead : p);
879 }
880
881 /* At the end of this slot, switch to next one and then find
882 * next entry from that point.
883 */
884 iter->skip = 0;
885 iter->slot++;
886 }
887
888 return __rhashtable_walk_find_next(iter);
889}
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100890EXPORT_SYMBOL_GPL(rhashtable_walk_next);
891
892/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800893 * rhashtable_walk_peek - Return the next object but don't advance the iterator
894 * @iter: Hash table iterator
895 *
896 * Returns the next object or NULL when the end of the table is reached.
897 *
898 * Returns -EAGAIN if resize event occurred. Note that the iterator
899 * will rewind back to the beginning and you may continue to use it.
900 */
901void *rhashtable_walk_peek(struct rhashtable_iter *iter)
902{
903 struct rhlist_head *list = iter->list;
904 struct rhashtable *ht = iter->ht;
905 struct rhash_head *p = iter->p;
906
907 if (p)
908 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
909
910 /* No object found in current iter, find next one in the table. */
911
912 if (iter->skip) {
913 /* A nonzero skip value points to the next entry in the table
914 * beyond that last one that was found. Decrement skip so
915 * we find the current value. __rhashtable_walk_find_next
916 * will restore the original value of skip assuming that
917 * the table hasn't changed.
918 */
919 iter->skip--;
920 }
921
922 return __rhashtable_walk_find_next(iter);
923}
924EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
925
926/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100927 * rhashtable_walk_stop - Finish a hash table walk
928 * @iter: Hash table iterator
929 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200930 * Finish a hash table walk. Does not reset the iterator to the start of the
931 * hash table.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100932 */
933void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100934 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100935{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100936 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800937 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100938
Herbert Xueddee5ba2015-03-14 13:57:20 +1100939 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100940 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100941
942 ht = iter->ht;
943
Herbert Xuba7c95e2015-03-24 09:53:17 +1100944 spin_lock(&ht->lock);
Herbert Xuc4db8842015-03-14 13:57:25 +1100945 if (tbl->rehash < tbl->size)
Herbert Xu246779d2016-08-18 16:50:56 +0800946 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100947 else
Herbert Xu246779d2016-08-18 16:50:56 +0800948 iter->walker.tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100949 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100950
Herbert Xu963ecbd2015-03-15 21:12:04 +1100951out:
952 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100953}
954EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
955
Herbert Xu488fb86e2015-03-20 21:56:59 +1100956static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200957{
Ying Xue94000172014-09-03 09:22:36 +0800958 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xue2e21c12015-03-18 20:01:21 +1100959 (unsigned long)params->min_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200960}
961
Herbert Xu31ccde22015-03-24 00:50:21 +1100962static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
963{
964 return jhash2(key, length, seed);
965}
966
Thomas Graf7e1e7762014-08-02 11:47:44 +0200967/**
968 * rhashtable_init - initialize a new hash table
969 * @ht: hash table to be initialized
970 * @params: configuration parameters
971 *
972 * Initializes a new hash table based on the provided configuration
973 * parameters. A table can be configured either with a variable or
974 * fixed length key:
975 *
976 * Configuration Example 1: Fixed length keys
977 * struct test_obj {
978 * int key;
979 * void * my_member;
980 * struct rhash_head node;
981 * };
982 *
983 * struct rhashtable_params params = {
984 * .head_offset = offsetof(struct test_obj, node),
985 * .key_offset = offsetof(struct test_obj, key),
986 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100987 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200988 * };
989 *
990 * Configuration Example 2: Variable length keys
991 * struct test_obj {
992 * [...]
993 * struct rhash_head node;
994 * };
995 *
Patrick McHardy49f7b332015-03-25 13:07:45 +0000996 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200997 * {
998 * struct test_obj *obj = data;
999 *
1000 * return [... hash ...];
1001 * }
1002 *
1003 * struct rhashtable_params params = {
1004 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001005 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001006 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001007 * };
1008 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001009int rhashtable_init(struct rhashtable *ht,
1010 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001011{
1012 struct bucket_table *tbl;
1013 size_t size;
1014
1015 size = HASH_DEFAULT_SIZE;
1016
Herbert Xu31ccde22015-03-24 00:50:21 +11001017 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001018 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001019 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
Herbert Xuca268932016-09-19 19:00:09 +08001085 err = rhashtable_init(&hlt->ht, params);
1086 hlt->ht.rhlist = true;
1087 return err;
1088}
1089EXPORT_SYMBOL_GPL(rhltable_init);
1090
1091static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1092 void (*free_fn)(void *ptr, void *arg),
1093 void *arg)
1094{
1095 struct rhlist_head *list;
1096
1097 if (!ht->rhlist) {
1098 free_fn(rht_obj(ht, obj), arg);
1099 return;
1100 }
1101
1102 list = container_of(obj, struct rhlist_head, rhead);
1103 do {
1104 obj = &list->rhead;
1105 list = rht_dereference(list->next, ht);
1106 free_fn(rht_obj(ht, obj), arg);
1107 } while (list);
1108}
1109
1110/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001111 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001112 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001113 * @free_fn: callback to release resources of element
1114 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001115 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001116 * Stops an eventual async resize. If defined, invokes free_fn for each
1117 * element to releasal resources. Please note that RCU protected
1118 * readers may still be accessing the elements. Releasing of resources
1119 * must occur in a compatible manner. Then frees the bucket array.
1120 *
1121 * This function will eventually sleep to wait for an async resize
1122 * to complete. The caller is responsible that no further write operations
1123 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001124 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001125void rhashtable_free_and_destroy(struct rhashtable *ht,
1126 void (*free_fn)(void *ptr, void *arg),
1127 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001128{
Herbert Xuda204202017-02-11 19:26:47 +08001129 struct bucket_table *tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001130 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001131
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001132 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001133
Thomas Graf97defe12015-01-02 23:00:20 +01001134 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001135 tbl = rht_dereference(ht->tbl, ht);
1136 if (free_fn) {
1137 for (i = 0; i < tbl->size; i++) {
1138 struct rhash_head *pos, *next;
1139
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001140 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +08001141 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001142 next = !rht_is_a_nulls(pos) ?
1143 rht_dereference(pos->next, ht) : NULL;
1144 !rht_is_a_nulls(pos);
1145 pos = next,
1146 next = !rht_is_a_nulls(pos) ?
1147 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001148 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001149 }
1150 }
1151
1152 bucket_table_free(tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001153 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001154}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001155EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1156
1157void rhashtable_destroy(struct rhashtable *ht)
1158{
1159 return rhashtable_free_and_destroy(ht, NULL, NULL);
1160}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001161EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001162
1163struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1164 unsigned int hash)
1165{
1166 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1167 static struct rhash_head __rcu *rhnull =
1168 (struct rhash_head __rcu *)NULLS_MARKER(0);
1169 unsigned int index = hash & ((1 << tbl->nest) - 1);
1170 unsigned int size = tbl->size >> tbl->nest;
1171 unsigned int subhash = hash;
1172 union nested_table *ntbl;
1173
1174 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
Herbert Xuc4d26032017-02-25 22:39:50 +08001175 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001176 subhash >>= tbl->nest;
1177
1178 while (ntbl && size > (1 << shift)) {
1179 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001180 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1181 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001182 size >>= shift;
1183 subhash >>= shift;
1184 }
1185
1186 if (!ntbl)
1187 return &rhnull;
1188
1189 return &ntbl[subhash].bucket;
1190
1191}
1192EXPORT_SYMBOL_GPL(rht_bucket_nested);
1193
1194struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1195 struct bucket_table *tbl,
1196 unsigned int hash)
1197{
1198 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1199 unsigned int index = hash & ((1 << tbl->nest) - 1);
1200 unsigned int size = tbl->size >> tbl->nest;
1201 union nested_table *ntbl;
Herbert Xuda204202017-02-11 19:26:47 +08001202
1203 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1204 hash >>= tbl->nest;
Herbert Xuda204202017-02-11 19:26:47 +08001205 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001206 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001207
1208 while (ntbl && size > (1 << shift)) {
1209 index = hash & ((1 << shift) - 1);
1210 size >>= shift;
1211 hash >>= shift;
Herbert Xuda204202017-02-11 19:26:47 +08001212 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001213 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001214 }
1215
1216 if (!ntbl)
1217 return NULL;
1218
1219 return &ntbl[hash].bucket;
1220
1221}
1222EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);