blob: 310e29b5150737eac76503b91baf551da3bc47da [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]);
Davidlohr Bueso93f976b2018-08-21 22:01:45 -0700177 tbl = kvzalloc(size, gfp);
Herbert Xuda204202017-02-11 19:26:47 +0800178
179 size = nbuckets;
180
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700181 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
Herbert Xuda204202017-02-11 19:26:47 +0800182 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
183 nbuckets = 0;
184 }
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700185
Thomas Graf7e1e7762014-08-02 11:47:44 +0200186 if (tbl == NULL)
187 return NULL;
188
Herbert Xuda204202017-02-11 19:26:47 +0800189 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200190
Tom Herbert64e0cd02017-12-04 10:31:45 -0800191 max_locks = size >> 1;
192 if (tbl->nest)
193 max_locks = min_t(size_t, max_locks, 1U << tbl->nest);
194
195 if (alloc_bucket_spinlocks(&tbl->locks, &tbl->locks_mask, max_locks,
196 ht->p.locks_mul, gfp) < 0) {
Thomas Graf97defe12015-01-02 23:00:20 +0100197 bucket_table_free(tbl);
198 return NULL;
199 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200200
Herbert Xueddee5ba2015-03-14 13:57:20 +1100201 INIT_LIST_HEAD(&tbl->walkers);
202
Jason A. Donenfeldd48ad082017-06-07 22:47:13 -0400203 tbl->hash_rnd = get_random_u32();
Herbert Xu5269b532015-03-14 13:57:22 +1100204
Thomas Graff89bd6f2015-01-02 23:00:21 +0100205 for (i = 0; i < nbuckets; i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000206 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100207
Thomas Graf97defe12015-01-02 23:00:20 +0100208 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200209}
210
Herbert Xub8244782015-03-24 00:50:26 +1100211static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
212 struct bucket_table *tbl)
213{
214 struct bucket_table *new_tbl;
215
216 do {
217 new_tbl = tbl;
218 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
219 } while (tbl);
220
221 return new_tbl;
222}
223
Thomas Graf299e5c32015-03-24 14:18:17 +0100224static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100225{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100226 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
NeilBrownc0690012018-06-18 12:52:50 +1000227 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
Herbert Xuda204202017-02-11 19:26:47 +0800228 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
229 int err = -EAGAIN;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100230 struct rhash_head *head, *next, *entry;
231 spinlock_t *new_bucket_lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100232 unsigned int new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100233
Herbert Xuda204202017-02-11 19:26:47 +0800234 if (new_tbl->nest)
235 goto out;
236
237 err = -ENOENT;
238
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100239 rht_for_each(entry, old_tbl, old_hash) {
240 err = 0;
241 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100242
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100243 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200244 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100245
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100246 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200247 }
248
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100249 if (err)
250 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100251
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100252 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100253
Herbert Xu02fd97c2015-03-20 21:57:00 +1100254 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100255
Herbert Xu8f2484b2015-03-14 13:57:21 +1100256 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100257 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
258 new_tbl, new_hash);
259
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200260 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100261
262 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
263 spin_unlock(new_bucket_lock);
264
265 rcu_assign_pointer(*pprev, next);
266
267out:
268 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100269}
270
Herbert Xuda204202017-02-11 19:26:47 +0800271static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100272 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100273{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100274 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
275 spinlock_t *old_bucket_lock;
Herbert Xuda204202017-02-11 19:26:47 +0800276 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100277
Herbert Xu02fd97c2015-03-20 21:57:00 +1100278 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100279
280 spin_lock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800281 while (!(err = rhashtable_rehash_one(ht, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100282 ;
Herbert Xuda204202017-02-11 19:26:47 +0800283
284 if (err == -ENOENT) {
285 old_tbl->rehash++;
286 err = 0;
287 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100288 spin_unlock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800289
290 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100291}
292
Herbert Xub8244782015-03-24 00:50:26 +1100293static int rhashtable_rehash_attach(struct rhashtable *ht,
294 struct bucket_table *old_tbl,
295 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100296{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100297 /* Make insertions go into the new, empty table right away. Deletions
298 * and lookups will be attempted in both tables until we synchronize.
NeilBrown0ad66442018-06-18 12:52:50 +1000299 * As cmpxchg() provides strong barriers, we do not need
300 * rcu_assign_pointer().
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100301 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100302
NeilBrown0ad66442018-06-18 12:52:50 +1000303 if (cmpxchg(&old_tbl->future_tbl, NULL, new_tbl) != NULL)
304 return -EEXIST;
Herbert Xub8244782015-03-24 00:50:26 +1100305
306 return 0;
307}
308
309static int rhashtable_rehash_table(struct rhashtable *ht)
310{
311 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
312 struct bucket_table *new_tbl;
313 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100314 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800315 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100316
317 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
318 if (!new_tbl)
319 return 0;
320
Herbert Xuda204202017-02-11 19:26:47 +0800321 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
322 err = rhashtable_rehash_chain(ht, old_hash);
323 if (err)
324 return err;
Eric Dumazetae6da1f2018-03-31 12:58:48 -0700325 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +0800326 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100327
328 /* Publish the new table pointer. */
329 rcu_assign_pointer(ht->tbl, new_tbl);
330
Herbert Xuba7c95e2015-03-24 09:53:17 +1100331 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100332 list_for_each_entry(walker, &old_tbl->walkers, list)
333 walker->tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100334 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100335
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100336 /* Wait for readers. All new readers will see the new
337 * table, and thus no references to the old table will
338 * remain.
339 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100340 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Herbert Xub8244782015-03-24 00:50:26 +1100341
342 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200343}
344
Herbert Xuda204202017-02-11 19:26:47 +0800345static int rhashtable_rehash_alloc(struct rhashtable *ht,
346 struct bucket_table *old_tbl,
347 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200348{
Herbert Xuda204202017-02-11 19:26:47 +0800349 struct bucket_table *new_tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100350 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200351
352 ASSERT_RHT_MUTEX(ht);
353
Herbert Xuda204202017-02-11 19:26:47 +0800354 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200355 if (new_tbl == NULL)
356 return -ENOMEM;
357
Herbert Xub8244782015-03-24 00:50:26 +1100358 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
359 if (err)
360 bucket_table_free(new_tbl);
361
362 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200363}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200364
365/**
366 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
367 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200368 *
Herbert Xu18093d12015-03-24 00:50:25 +1100369 * This function shrinks the hash table to fit, i.e., the smallest
370 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200371 *
Thomas Graf97defe12015-01-02 23:00:20 +0100372 * The caller must ensure that no concurrent resizing occurs by holding
373 * ht->mutex.
374 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200375 * The caller must ensure that no concurrent table mutations take place.
376 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100377 *
378 * It is valid to have concurrent insertions and deletions protected by per
379 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200380 */
Herbert Xub8244782015-03-24 00:50:26 +1100381static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200382{
Herbert Xuda204202017-02-11 19:26:47 +0800383 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200384 unsigned int nelems = atomic_read(&ht->nelems);
385 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200386
Vegard Nossum12311952016-08-12 20:10:44 +0200387 if (nelems)
388 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100389 if (size < ht->p.min_size)
390 size = ht->p.min_size;
391
392 if (old_tbl->size <= size)
393 return 0;
394
Herbert Xub8244782015-03-24 00:50:26 +1100395 if (rht_dereference(old_tbl->future_tbl, ht))
396 return -EEXIST;
397
Herbert Xuda204202017-02-11 19:26:47 +0800398 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200399}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200400
Thomas Graf97defe12015-01-02 23:00:20 +0100401static void rht_deferred_worker(struct work_struct *work)
402{
403 struct rhashtable *ht;
404 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100405 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100406
Ying Xue57699a42015-01-16 11:13:09 +0800407 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100408 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100409
Thomas Graf97defe12015-01-02 23:00:20 +0100410 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100411 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100412
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100413 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800414 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000415 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800416 err = rhashtable_shrink(ht);
417 else if (tbl->nest)
418 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub8244782015-03-24 00:50:26 +1100419
Herbert Xuda204202017-02-11 19:26:47 +0800420 if (!err)
421 err = rhashtable_rehash_table(ht);
Herbert Xub8244782015-03-24 00:50:26 +1100422
Thomas Graf97defe12015-01-02 23:00:20 +0100423 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100424
425 if (err)
426 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100427}
428
Herbert Xuca268932016-09-19 19:00:09 +0800429static int rhashtable_insert_rehash(struct rhashtable *ht,
430 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100431{
432 struct bucket_table *old_tbl;
433 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100434 unsigned int size;
435 int err;
436
437 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100438
439 size = tbl->size;
440
Herbert Xu3cf92222015-12-03 20:41:29 +0800441 err = -EBUSY;
442
Herbert Xuccd57b12015-03-24 00:50:28 +1100443 if (rht_grow_above_75(ht, tbl))
444 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200445 /* Do not schedule more than one rehash */
446 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800447 goto fail;
448
449 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100450
Davidlohr Bueso93f976b2018-08-21 22:01:45 -0700451 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
Herbert Xu3cf92222015-12-03 20:41:29 +0800452 if (new_tbl == NULL)
453 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100454
455 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
456 if (err) {
457 bucket_table_free(new_tbl);
458 if (err == -EEXIST)
459 err = 0;
460 } else
461 schedule_work(&ht->run_work);
462
463 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800464
465fail:
466 /* Do not fail the insert if someone else did a rehash. */
NeilBrownc0690012018-06-18 12:52:50 +1000467 if (likely(rcu_access_pointer(tbl->future_tbl)))
Herbert Xu3cf92222015-12-03 20:41:29 +0800468 return 0;
469
470 /* Schedule async rehash to retry allocation in process context. */
471 if (err == -ENOMEM)
472 schedule_work(&ht->run_work);
473
474 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100475}
Herbert Xuccd57b12015-03-24 00:50:28 +1100476
Herbert Xuca268932016-09-19 19:00:09 +0800477static void *rhashtable_lookup_one(struct rhashtable *ht,
478 struct bucket_table *tbl, unsigned int hash,
479 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100480{
Herbert Xuca268932016-09-19 19:00:09 +0800481 struct rhashtable_compare_arg arg = {
482 .ht = ht,
483 .key = key,
484 };
485 struct rhash_head __rcu **pprev;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100486 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800487 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100488
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200489 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800490 pprev = rht_bucket_var(tbl, hash);
491 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800492 struct rhlist_head *list;
493 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100494
Herbert Xuca268932016-09-19 19:00:09 +0800495 elasticity--;
496 if (!key ||
497 (ht->p.obj_cmpfn ?
498 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200499 rhashtable_compare(&arg, rht_obj(ht, head)))) {
500 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800501 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200502 }
Herbert Xuca268932016-09-19 19:00:09 +0800503
504 if (!ht->rhlist)
505 return rht_obj(ht, head);
506
507 list = container_of(obj, struct rhlist_head, rhead);
508 plist = container_of(head, struct rhlist_head, rhead);
509
510 RCU_INIT_POINTER(list->next, plist);
511 head = rht_dereference_bucket(head->next, tbl, hash);
512 RCU_INIT_POINTER(list->rhead.next, head);
513 rcu_assign_pointer(*pprev, obj);
514
515 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200516 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100517
Herbert Xuca268932016-09-19 19:00:09 +0800518 if (elasticity <= 0)
519 return ERR_PTR(-EAGAIN);
520
521 return ERR_PTR(-ENOENT);
522}
523
524static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
525 struct bucket_table *tbl,
526 unsigned int hash,
527 struct rhash_head *obj,
528 void *data)
529{
Herbert Xuda204202017-02-11 19:26:47 +0800530 struct rhash_head __rcu **pprev;
Herbert Xuca268932016-09-19 19:00:09 +0800531 struct bucket_table *new_tbl;
532 struct rhash_head *head;
533
534 if (!IS_ERR_OR_NULL(data))
535 return ERR_PTR(-EEXIST);
536
537 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
538 return ERR_CAST(data);
539
NeilBrownc0690012018-06-18 12:52:50 +1000540 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800541 if (new_tbl)
542 return new_tbl;
543
544 if (PTR_ERR(data) != -ENOENT)
545 return ERR_CAST(data);
546
Herbert Xu07ee0722015-05-15 11:30:47 +0800547 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800548 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800549
Herbert Xuca268932016-09-19 19:00:09 +0800550 if (unlikely(rht_grow_above_100(ht, tbl)))
551 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100552
Herbert Xuda204202017-02-11 19:26:47 +0800553 pprev = rht_bucket_insert(ht, tbl, hash);
554 if (!pprev)
555 return ERR_PTR(-ENOMEM);
556
557 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100558
559 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800560 if (ht->rhlist) {
561 struct rhlist_head *list;
562
563 list = container_of(obj, struct rhlist_head, rhead);
564 RCU_INIT_POINTER(list->next, NULL);
565 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100566
Herbert Xuda204202017-02-11 19:26:47 +0800567 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100568
569 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800570 if (rht_grow_above_75(ht, tbl))
571 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100572
Herbert Xuca268932016-09-19 19:00:09 +0800573 return NULL;
574}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100575
Herbert Xuca268932016-09-19 19:00:09 +0800576static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
577 struct rhash_head *obj)
578{
579 struct bucket_table *new_tbl;
580 struct bucket_table *tbl;
581 unsigned int hash;
582 spinlock_t *lock;
583 void *data;
584
585 tbl = rcu_dereference(ht->tbl);
586
587 /* All insertions must grab the oldest table containing
588 * the hashed bucket that is yet to be rehashed.
589 */
590 for (;;) {
591 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
592 lock = rht_bucket_lock(tbl, hash);
593 spin_lock_bh(lock);
594
595 if (tbl->rehash <= hash)
596 break;
597
598 spin_unlock_bh(lock);
NeilBrownc0690012018-06-18 12:52:50 +1000599 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800600 }
601
602 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
603 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
604 if (PTR_ERR(new_tbl) != -EEXIST)
605 data = ERR_CAST(new_tbl);
606
607 while (!IS_ERR_OR_NULL(new_tbl)) {
608 tbl = new_tbl;
609 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
610 spin_lock_nested(rht_bucket_lock(tbl, hash),
611 SINGLE_DEPTH_NESTING);
612
613 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
614 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
615 if (PTR_ERR(new_tbl) != -EEXIST)
616 data = ERR_CAST(new_tbl);
617
618 spin_unlock(rht_bucket_lock(tbl, hash));
619 }
620
621 spin_unlock_bh(lock);
622
623 if (PTR_ERR(data) == -EAGAIN)
624 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
625 -EAGAIN);
626
627 return data;
628}
629
630void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
631 struct rhash_head *obj)
632{
633 void *data;
634
635 do {
636 rcu_read_lock();
637 data = rhashtable_try_insert(ht, key, obj);
638 rcu_read_unlock();
639 } while (PTR_ERR(data) == -EAGAIN);
640
641 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100642}
643EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
644
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100645/**
Herbert Xu246779d2016-08-18 16:50:56 +0800646 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100647 * @ht: Table to walk over
648 * @iter: Hash table Iterator
649 *
650 * This function prepares a hash table walk.
651 *
652 * Note that if you restart a walk after rhashtable_walk_stop you
653 * may see the same object twice. Also, you may miss objects if
654 * there are removals in between rhashtable_walk_stop and the next
655 * call to rhashtable_walk_start.
656 *
657 * For a completely stable walk you should construct your own data
658 * structure outside the hash table.
659 *
NeilBrown82266e92018-04-24 08:29:13 +1000660 * This function may be called from any process context, including
661 * non-preemptable context, but cannot be called from softirq or
662 * hardirq context.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100663 *
Herbert Xu246779d2016-08-18 16:50:56 +0800664 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100665 */
Herbert Xu246779d2016-08-18 16:50:56 +0800666void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100667{
668 iter->ht = ht;
669 iter->p = NULL;
670 iter->slot = 0;
671 iter->skip = 0;
Tom Herbert2db54b42017-12-04 10:31:42 -0800672 iter->end_of_table = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100673
Herbert Xuc6ff5262015-12-16 16:45:54 +0800674 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800675 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800676 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800677 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800678 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100679}
Herbert Xu246779d2016-08-18 16:50:56 +0800680EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100681
682/**
683 * rhashtable_walk_exit - Free an iterator
684 * @iter: Hash table Iterator
685 *
686 * This function frees resources allocated by rhashtable_walk_init.
687 */
688void rhashtable_walk_exit(struct rhashtable_iter *iter)
689{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800690 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800691 if (iter->walker.tbl)
692 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800693 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100694}
695EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
696
697/**
Tom Herbert97a6ec42017-12-04 10:31:41 -0800698 * rhashtable_walk_start_check - Start a hash table walk
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100699 * @iter: Hash table iterator
700 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200701 * Start a hash table walk at the current iterator position. Note that we take
702 * the RCU lock in all cases including when we return an error. So you must
703 * always call rhashtable_walk_stop to clean up.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100704 *
705 * Returns zero if successful.
706 *
707 * Returns -EAGAIN if resize event occured. Note that the iterator
708 * will rewind back to the beginning and you may use it immediately
709 * by calling rhashtable_walk_next.
Tom Herbert97a6ec42017-12-04 10:31:41 -0800710 *
711 * rhashtable_walk_start is defined as an inline variant that returns
712 * void. This is preferred in cases where the caller would ignore
713 * resize events and always continue.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100714 */
Tom Herbert97a6ec42017-12-04 10:31:41 -0800715int rhashtable_walk_start_check(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100716 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100717{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100718 struct rhashtable *ht = iter->ht;
NeilBrown5d240a82018-04-24 08:29:13 +1000719 bool rhlist = ht->rhlist;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100720
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100721 rcu_read_lock();
722
Herbert Xuc6ff5262015-12-16 16:45:54 +0800723 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800724 if (iter->walker.tbl)
725 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800726 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100727
NeilBrown5d240a82018-04-24 08:29:13 +1000728 if (iter->end_of_table)
729 return 0;
730 if (!iter->walker.tbl) {
Herbert Xu246779d2016-08-18 16:50:56 +0800731 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
NeilBrownb41cc042018-04-24 08:29:13 +1000732 iter->slot = 0;
733 iter->skip = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100734 return -EAGAIN;
735 }
736
NeilBrown5d240a82018-04-24 08:29:13 +1000737 if (iter->p && !rhlist) {
738 /*
739 * We need to validate that 'p' is still in the table, and
740 * if so, update 'skip'
741 */
742 struct rhash_head *p;
743 int skip = 0;
744 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
745 skip++;
746 if (p == iter->p) {
747 iter->skip = skip;
748 goto found;
749 }
750 }
751 iter->p = NULL;
752 } else if (iter->p && rhlist) {
753 /* Need to validate that 'list' is still in the table, and
754 * if so, update 'skip' and 'p'.
755 */
756 struct rhash_head *p;
757 struct rhlist_head *list;
758 int skip = 0;
759 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
760 for (list = container_of(p, struct rhlist_head, rhead);
761 list;
762 list = rcu_dereference(list->next)) {
763 skip++;
764 if (list == iter->list) {
765 iter->p = p;
Rishabh Bhatnagarc643ecf2018-07-02 09:35:34 -0700766 iter->skip = skip;
NeilBrown5d240a82018-04-24 08:29:13 +1000767 goto found;
768 }
769 }
770 }
771 iter->p = NULL;
772 }
773found:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100774 return 0;
775}
Tom Herbert97a6ec42017-12-04 10:31:41 -0800776EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100777
778/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800779 * __rhashtable_walk_find_next - Find the next element in a table (or the first
780 * one in case of a new walk).
781 *
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100782 * @iter: Hash table iterator
783 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800784 * Returns the found object or NULL when the end of the table is reached.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100785 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800786 * Returns -EAGAIN if resize event occurred.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100787 */
Tom Herbert2db54b42017-12-04 10:31:42 -0800788static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100789{
Herbert Xu246779d2016-08-18 16:50:56 +0800790 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xuca268932016-09-19 19:00:09 +0800791 struct rhlist_head *list = iter->list;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100792 struct rhashtable *ht = iter->ht;
793 struct rhash_head *p = iter->p;
Herbert Xuca268932016-09-19 19:00:09 +0800794 bool rhlist = ht->rhlist;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100795
Tom Herbert2db54b42017-12-04 10:31:42 -0800796 if (!tbl)
797 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100798
799 for (; iter->slot < tbl->size; iter->slot++) {
800 int skip = iter->skip;
801
802 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800803 if (rhlist) {
804 list = container_of(p, struct rhlist_head,
805 rhead);
806 do {
807 if (!skip)
808 goto next;
809 skip--;
810 list = rcu_dereference(list->next);
811 } while (list);
812
813 continue;
814 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100815 if (!skip)
816 break;
817 skip--;
818 }
819
820next:
821 if (!rht_is_a_nulls(p)) {
822 iter->skip++;
823 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800824 iter->list = list;
825 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100826 }
827
828 iter->skip = 0;
829 }
830
Phil Sutter142b9422015-07-06 15:51:20 +0200831 iter->p = NULL;
832
Herbert Xud88252f2015-03-24 00:50:19 +1100833 /* Ensure we see any new tables. */
834 smp_rmb();
835
Herbert Xu246779d2016-08-18 16:50:56 +0800836 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
837 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100838 iter->slot = 0;
839 iter->skip = 0;
840 return ERR_PTR(-EAGAIN);
Tom Herbert2db54b42017-12-04 10:31:42 -0800841 } else {
842 iter->end_of_table = true;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100843 }
844
Thomas Grafc936a792015-05-05 02:22:53 +0200845 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100846}
Tom Herbert2db54b42017-12-04 10:31:42 -0800847
848/**
849 * rhashtable_walk_next - Return the next object and advance the iterator
850 * @iter: Hash table iterator
851 *
852 * Note that you must call rhashtable_walk_stop when you are finished
853 * with the walk.
854 *
855 * Returns the next object or NULL when the end of the table is reached.
856 *
857 * Returns -EAGAIN if resize event occurred. Note that the iterator
858 * will rewind back to the beginning and you may continue to use it.
859 */
860void *rhashtable_walk_next(struct rhashtable_iter *iter)
861{
862 struct rhlist_head *list = iter->list;
863 struct rhashtable *ht = iter->ht;
864 struct rhash_head *p = iter->p;
865 bool rhlist = ht->rhlist;
866
867 if (p) {
868 if (!rhlist || !(list = rcu_dereference(list->next))) {
869 p = rcu_dereference(p->next);
870 list = container_of(p, struct rhlist_head, rhead);
871 }
872 if (!rht_is_a_nulls(p)) {
873 iter->skip++;
874 iter->p = p;
875 iter->list = list;
876 return rht_obj(ht, rhlist ? &list->rhead : p);
877 }
878
879 /* At the end of this slot, switch to next one and then find
880 * next entry from that point.
881 */
882 iter->skip = 0;
883 iter->slot++;
884 }
885
886 return __rhashtable_walk_find_next(iter);
887}
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100888EXPORT_SYMBOL_GPL(rhashtable_walk_next);
889
890/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800891 * rhashtable_walk_peek - Return the next object but don't advance the iterator
892 * @iter: Hash table iterator
893 *
894 * Returns the next object or NULL when the end of the table is reached.
895 *
896 * Returns -EAGAIN if resize event occurred. Note that the iterator
897 * will rewind back to the beginning and you may continue to use it.
898 */
899void *rhashtable_walk_peek(struct rhashtable_iter *iter)
900{
901 struct rhlist_head *list = iter->list;
902 struct rhashtable *ht = iter->ht;
903 struct rhash_head *p = iter->p;
904
905 if (p)
906 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
907
908 /* No object found in current iter, find next one in the table. */
909
910 if (iter->skip) {
911 /* A nonzero skip value points to the next entry in the table
912 * beyond that last one that was found. Decrement skip so
913 * we find the current value. __rhashtable_walk_find_next
914 * will restore the original value of skip assuming that
915 * the table hasn't changed.
916 */
917 iter->skip--;
918 }
919
920 return __rhashtable_walk_find_next(iter);
921}
922EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
923
924/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100925 * rhashtable_walk_stop - Finish a hash table walk
926 * @iter: Hash table iterator
927 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200928 * Finish a hash table walk. Does not reset the iterator to the start of the
929 * hash table.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100930 */
931void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100932 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100933{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100934 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800935 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100936
Herbert Xueddee5ba2015-03-14 13:57:20 +1100937 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100938 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100939
940 ht = iter->ht;
941
Herbert Xuba7c95e2015-03-24 09:53:17 +1100942 spin_lock(&ht->lock);
Herbert Xuc4db8842015-03-14 13:57:25 +1100943 if (tbl->rehash < tbl->size)
Herbert Xu246779d2016-08-18 16:50:56 +0800944 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100945 else
Herbert Xu246779d2016-08-18 16:50:56 +0800946 iter->walker.tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100947 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100948
Herbert Xu963ecbd2015-03-15 21:12:04 +1100949out:
950 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100951}
952EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
953
Herbert Xu488fb86e2015-03-20 21:56:59 +1100954static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200955{
Davidlohr Bueso107d01f2018-07-16 13:26:13 -0700956 size_t retsize;
957
958 if (params->nelem_hint)
959 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
960 (unsigned long)params->min_size);
961 else
962 retsize = max(HASH_DEFAULT_SIZE,
963 (unsigned long)params->min_size);
964
965 return retsize;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200966}
967
Herbert Xu31ccde22015-03-24 00:50:21 +1100968static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
969{
970 return jhash2(key, length, seed);
971}
972
Thomas Graf7e1e7762014-08-02 11:47:44 +0200973/**
974 * rhashtable_init - initialize a new hash table
975 * @ht: hash table to be initialized
976 * @params: configuration parameters
977 *
978 * Initializes a new hash table based on the provided configuration
979 * parameters. A table can be configured either with a variable or
980 * fixed length key:
981 *
982 * Configuration Example 1: Fixed length keys
983 * struct test_obj {
984 * int key;
985 * void * my_member;
986 * struct rhash_head node;
987 * };
988 *
989 * struct rhashtable_params params = {
990 * .head_offset = offsetof(struct test_obj, node),
991 * .key_offset = offsetof(struct test_obj, key),
992 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100993 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200994 * };
995 *
996 * Configuration Example 2: Variable length keys
997 * struct test_obj {
998 * [...]
999 * struct rhash_head node;
1000 * };
1001 *
Patrick McHardy49f7b332015-03-25 13:07:45 +00001002 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001003 * {
1004 * struct test_obj *obj = data;
1005 *
1006 * return [... hash ...];
1007 * }
1008 *
1009 * struct rhashtable_params params = {
1010 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001011 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001012 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001013 * };
1014 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001015int rhashtable_init(struct rhashtable *ht,
1016 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001017{
1018 struct bucket_table *tbl;
1019 size_t size;
1020
Herbert Xu31ccde22015-03-24 00:50:21 +11001021 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001022 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001023 return -EINVAL;
1024
Thomas Graf97defe12015-01-02 23:00:20 +01001025 memset(ht, 0, sizeof(*ht));
1026 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001027 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001028 memcpy(&ht->p, params, sizeof(*params));
1029
Thomas Grafa998f712015-03-19 22:31:13 +00001030 if (params->min_size)
1031 ht->p.min_size = roundup_pow_of_two(params->min_size);
1032
Herbert Xu6d684e52017-04-27 13:44:51 +08001033 /* Cap total entries at 2^31 to avoid nelems overflow. */
1034 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001035
1036 if (params->max_size) {
1037 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1038 if (ht->p.max_size < ht->max_elems / 2)
1039 ht->max_elems = ht->p.max_size * 2;
1040 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001041
Florian Westphal48e75b432017-05-01 22:18:01 +02001042 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001043
Davidlohr Bueso107d01f2018-07-16 13:26:13 -07001044 size = rounded_hashtable_size(&ht->p);
Herbert Xu3a324602015-12-16 18:13:14 +08001045
Thomas Graf97defe12015-01-02 23:00:20 +01001046 if (params->locks_mul)
1047 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1048 else
1049 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1050
Herbert Xu31ccde22015-03-24 00:50:21 +11001051 ht->key_len = ht->p.key_len;
1052 if (!params->hashfn) {
1053 ht->p.hashfn = jhash;
1054
1055 if (!(ht->key_len & (sizeof(u32) - 1))) {
1056 ht->key_len /= sizeof(u32);
1057 ht->p.hashfn = rhashtable_jhash2;
1058 }
1059 }
1060
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001061 /*
1062 * This is api initialization and thus we need to guarantee the
1063 * initial rhashtable allocation. Upon failure, retry with the
1064 * smallest possible size with __GFP_NOFAIL semantics.
1065 */
Herbert Xub9ecfda2015-03-24 00:50:27 +11001066 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001067 if (unlikely(tbl == NULL)) {
1068 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1069 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1070 }
Thomas Graf7e1e7762014-08-02 11:47:44 +02001071
Ying Xue545a1482015-01-07 13:41:57 +08001072 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001073
Thomas Graf7e1e7762014-08-02 11:47:44 +02001074 RCU_INIT_POINTER(ht->tbl, tbl);
1075
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001076 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001077
Thomas Graf7e1e7762014-08-02 11:47:44 +02001078 return 0;
1079}
1080EXPORT_SYMBOL_GPL(rhashtable_init);
1081
1082/**
Herbert Xuca268932016-09-19 19:00:09 +08001083 * rhltable_init - initialize a new hash list table
1084 * @hlt: hash list table to be initialized
1085 * @params: configuration parameters
1086 *
1087 * Initializes a new hash list table.
1088 *
1089 * See documentation for rhashtable_init.
1090 */
1091int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1092{
1093 int err;
1094
Herbert Xuca268932016-09-19 19:00:09 +08001095 err = rhashtable_init(&hlt->ht, params);
1096 hlt->ht.rhlist = true;
1097 return err;
1098}
1099EXPORT_SYMBOL_GPL(rhltable_init);
1100
1101static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1102 void (*free_fn)(void *ptr, void *arg),
1103 void *arg)
1104{
1105 struct rhlist_head *list;
1106
1107 if (!ht->rhlist) {
1108 free_fn(rht_obj(ht, obj), arg);
1109 return;
1110 }
1111
1112 list = container_of(obj, struct rhlist_head, rhead);
1113 do {
1114 obj = &list->rhead;
1115 list = rht_dereference(list->next, ht);
1116 free_fn(rht_obj(ht, obj), arg);
1117 } while (list);
1118}
1119
1120/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001121 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001122 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001123 * @free_fn: callback to release resources of element
1124 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001125 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001126 * Stops an eventual async resize. If defined, invokes free_fn for each
1127 * element to releasal resources. Please note that RCU protected
1128 * readers may still be accessing the elements. Releasing of resources
1129 * must occur in a compatible manner. Then frees the bucket array.
1130 *
1131 * This function will eventually sleep to wait for an async resize
1132 * to complete. The caller is responsible that no further write operations
1133 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001134 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001135void rhashtable_free_and_destroy(struct rhashtable *ht,
1136 void (*free_fn)(void *ptr, void *arg),
1137 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001138{
Taehee Yoo00261292018-07-08 11:55:51 +09001139 struct bucket_table *tbl, *next_tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001140 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001141
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001142 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001143
Thomas Graf97defe12015-01-02 23:00:20 +01001144 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001145 tbl = rht_dereference(ht->tbl, ht);
Taehee Yoo00261292018-07-08 11:55:51 +09001146restart:
Thomas Graf6b6f3022015-03-24 14:18:20 +01001147 if (free_fn) {
1148 for (i = 0; i < tbl->size; i++) {
1149 struct rhash_head *pos, *next;
1150
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001151 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +08001152 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001153 next = !rht_is_a_nulls(pos) ?
1154 rht_dereference(pos->next, ht) : NULL;
1155 !rht_is_a_nulls(pos);
1156 pos = next,
1157 next = !rht_is_a_nulls(pos) ?
1158 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001159 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001160 }
1161 }
1162
Taehee Yoo00261292018-07-08 11:55:51 +09001163 next_tbl = rht_dereference(tbl->future_tbl, ht);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001164 bucket_table_free(tbl);
Taehee Yoo00261292018-07-08 11:55:51 +09001165 if (next_tbl) {
1166 tbl = next_tbl;
1167 goto restart;
1168 }
Thomas Graf97defe12015-01-02 23:00:20 +01001169 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001170}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001171EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1172
1173void rhashtable_destroy(struct rhashtable *ht)
1174{
1175 return rhashtable_free_and_destroy(ht, NULL, NULL);
1176}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001177EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001178
1179struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1180 unsigned int hash)
1181{
1182 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1183 static struct rhash_head __rcu *rhnull =
1184 (struct rhash_head __rcu *)NULLS_MARKER(0);
1185 unsigned int index = hash & ((1 << tbl->nest) - 1);
1186 unsigned int size = tbl->size >> tbl->nest;
1187 unsigned int subhash = hash;
1188 union nested_table *ntbl;
1189
1190 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
Herbert Xuc4d26032017-02-25 22:39:50 +08001191 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001192 subhash >>= tbl->nest;
1193
1194 while (ntbl && size > (1 << shift)) {
1195 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001196 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1197 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001198 size >>= shift;
1199 subhash >>= shift;
1200 }
1201
1202 if (!ntbl)
1203 return &rhnull;
1204
1205 return &ntbl[subhash].bucket;
1206
1207}
1208EXPORT_SYMBOL_GPL(rht_bucket_nested);
1209
1210struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1211 struct bucket_table *tbl,
1212 unsigned int hash)
1213{
1214 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1215 unsigned int index = hash & ((1 << tbl->nest) - 1);
1216 unsigned int size = tbl->size >> tbl->nest;
1217 union nested_table *ntbl;
Herbert Xuda204202017-02-11 19:26:47 +08001218
1219 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1220 hash >>= tbl->nest;
Herbert Xuda204202017-02-11 19:26:47 +08001221 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001222 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001223
1224 while (ntbl && size > (1 << shift)) {
1225 index = hash & ((1 << shift) - 1);
1226 size >>= shift;
1227 hash >>= shift;
Herbert Xuda204202017-02-11 19:26:47 +08001228 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001229 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001230 }
1231
1232 if (!ntbl)
1233 return NULL;
1234
1235 return &ntbl[hash].bucket;
1236
1237}
1238EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);