blob: 6c4f5c8e9baa6ef85bd7e690f0b30c9ea7ad0f4b [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Herbert Xu02fd97c2015-03-20 21:57:00 +11004 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafa5ec68e2015-02-05 02:03:32 +01005 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02006 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02008 * Code partially derived from nft_hash
Herbert Xu02fd97c2015-03-20 21:57:00 +11009 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Herbert Xu07ee0722015-05-15 11:30:47 +080017#include <linux/atomic.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020018#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/log2.h>
Eric Dumazet5beb5c92015-02-26 07:20:34 -080021#include <linux/sched.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010022#include <linux/rculist.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020023#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010026#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020027#include <linux/random.h>
28#include <linux/rhashtable.h>
Stephen Rothwell61d7b092015-02-09 14:04:03 +110029#include <linux/err.h>
Hauke Mehrtens6d795412015-06-06 22:07:23 +020030#include <linux/export.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020031
32#define HASH_DEFAULT_SIZE 64UL
Herbert Xuc2e213c2015-03-18 20:01:16 +110033#define HASH_MIN_SIZE 4U
Florian Westphal4cf0b352016-08-12 12:03:52 +020034#define BUCKET_LOCKS_PER_CPU 32UL
Thomas Graf97defe12015-01-02 23:00:20 +010035
Herbert Xuda204202017-02-11 19:26:47 +080036union nested_table {
37 union nested_table __rcu *table;
38 struct rhash_head __rcu *bucket;
39};
40
Herbert Xu988dfbd2015-03-10 09:27:55 +110041static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010042 const struct bucket_table *tbl,
43 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020044{
Herbert Xu02fd97c2015-03-20 21:57:00 +110045 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020046}
47
Thomas Grafa03eaec2015-02-05 02:03:34 +010048#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010049#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010050
51int lockdep_rht_mutex_is_held(struct rhashtable *ht)
52{
53 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
54}
55EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
56
57int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
58{
Herbert Xu02fd97c2015-03-20 21:57:00 +110059 spinlock_t *lock = rht_bucket_lock(tbl, hash);
Thomas Grafa03eaec2015-02-05 02:03:34 +010060
61 return (debug_locks) ? lockdep_is_held(lock) : 1;
62}
63EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
64#else
65#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +010066#endif
67
Herbert Xuda204202017-02-11 19:26:47 +080068static void nested_table_free(union nested_table *ntbl, unsigned int size)
69{
70 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
71 const unsigned int len = 1 << shift;
72 unsigned int i;
73
74 ntbl = rcu_dereference_raw(ntbl->table);
75 if (!ntbl)
76 return;
77
78 if (size > len) {
79 size >>= shift;
80 for (i = 0; i < len; i++)
81 nested_table_free(ntbl + i, size);
82 }
83
84 kfree(ntbl);
85}
86
87static void nested_bucket_table_free(const struct bucket_table *tbl)
88{
89 unsigned int size = tbl->size >> tbl->nest;
90 unsigned int len = 1 << tbl->nest;
91 union nested_table *ntbl;
92 unsigned int i;
93
94 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
95
96 for (i = 0; i < len; i++)
97 nested_table_free(ntbl + i, size);
98
99 kfree(ntbl);
100}
101
Thomas Graf97defe12015-01-02 23:00:20 +0100102static void bucket_table_free(const struct bucket_table *tbl)
103{
Herbert Xuda204202017-02-11 19:26:47 +0800104 if (tbl->nest)
105 nested_bucket_table_free(tbl);
106
Tom Herbert64e0cd02017-12-04 10:31:45 -0800107 free_bucket_spinlocks(tbl->locks);
Thomas Graf97defe12015-01-02 23:00:20 +0100108 kvfree(tbl);
109}
110
Herbert Xu9d901bc2015-03-14 13:57:23 +1100111static void bucket_table_free_rcu(struct rcu_head *head)
112{
113 bucket_table_free(container_of(head, struct bucket_table, rcu));
114}
115
Herbert Xuda204202017-02-11 19:26:47 +0800116static union nested_table *nested_table_alloc(struct rhashtable *ht,
117 union nested_table __rcu **prev,
NeilBrown5af68ef2018-06-18 12:52:50 +1000118 bool leaf)
Herbert Xuda204202017-02-11 19:26:47 +0800119{
120 union nested_table *ntbl;
121 int i;
122
123 ntbl = rcu_dereference(*prev);
124 if (ntbl)
125 return ntbl;
126
127 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
128
NeilBrown5af68ef2018-06-18 12:52:50 +1000129 if (ntbl && leaf) {
130 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
NeilBrown9b4f64a2018-06-18 12:52:50 +1000131 INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
Herbert Xuda204202017-02-11 19:26:47 +0800132 }
133
NeilBrown7a41c292019-04-02 10:07:45 +1100134 if (cmpxchg(prev, NULL, ntbl) == NULL)
135 return ntbl;
136 /* Raced with another thread. */
137 kfree(ntbl);
138 return rcu_dereference(*prev);
Herbert Xuda204202017-02-11 19:26:47 +0800139}
140
141static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
142 size_t nbuckets,
143 gfp_t gfp)
144{
145 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
146 struct bucket_table *tbl;
147 size_t size;
148
149 if (nbuckets < (1 << (shift + 1)))
150 return NULL;
151
152 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
153
154 tbl = kzalloc(size, gfp);
155 if (!tbl)
156 return NULL;
157
158 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
NeilBrown5af68ef2018-06-18 12:52:50 +1000159 false)) {
Herbert Xuda204202017-02-11 19:26:47 +0800160 kfree(tbl);
161 return NULL;
162 }
163
164 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
165
166 return tbl;
167}
168
Thomas Graf97defe12015-01-02 23:00:20 +0100169static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xub9ecfda2015-03-24 00:50:27 +1100170 size_t nbuckets,
171 gfp_t gfp)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200172{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100173 struct bucket_table *tbl = NULL;
Tom Herbert64e0cd02017-12-04 10:31:45 -0800174 size_t size, max_locks;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100175 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200176
177 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Davidlohr Bueso93f976b2018-08-21 22:01:45 -0700178 tbl = kvzalloc(size, gfp);
Herbert Xuda204202017-02-11 19:26:47 +0800179
180 size = nbuckets;
181
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700182 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
Herbert Xuda204202017-02-11 19:26:47 +0800183 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
184 nbuckets = 0;
185 }
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -0700186
Thomas Graf7e1e7762014-08-02 11:47:44 +0200187 if (tbl == NULL)
188 return NULL;
189
Herbert Xuda204202017-02-11 19:26:47 +0800190 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200191
Tom Herbert64e0cd02017-12-04 10:31:45 -0800192 max_locks = size >> 1;
193 if (tbl->nest)
194 max_locks = min_t(size_t, max_locks, 1U << tbl->nest);
195
196 if (alloc_bucket_spinlocks(&tbl->locks, &tbl->locks_mask, max_locks,
197 ht->p.locks_mul, gfp) < 0) {
Thomas Graf97defe12015-01-02 23:00:20 +0100198 bucket_table_free(tbl);
199 return NULL;
200 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200201
NeilBrown4feb7c72019-03-21 14:42:40 +1100202 rcu_head_init(&tbl->rcu);
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
NeilBrown4feb7c72019-03-21 14:42:40 +1100286 if (err == -ENOENT)
Herbert Xuda204202017-02-11 19:26:47 +0800287 err = 0;
NeilBrown4feb7c72019-03-21 14:42:40 +1100288
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100289 spin_unlock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800290
291 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100292}
293
Herbert Xub8244782015-03-24 00:50:26 +1100294static int rhashtable_rehash_attach(struct rhashtable *ht,
295 struct bucket_table *old_tbl,
296 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100297{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100298 /* Make insertions go into the new, empty table right away. Deletions
299 * and lookups will be attempted in both tables until we synchronize.
NeilBrown0ad66442018-06-18 12:52:50 +1000300 * As cmpxchg() provides strong barriers, we do not need
301 * rcu_assign_pointer().
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100302 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100303
NeilBrown0ad66442018-06-18 12:52:50 +1000304 if (cmpxchg(&old_tbl->future_tbl, NULL, new_tbl) != NULL)
305 return -EEXIST;
Herbert Xub8244782015-03-24 00:50:26 +1100306
307 return 0;
308}
309
310static int rhashtable_rehash_table(struct rhashtable *ht)
311{
312 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
313 struct bucket_table *new_tbl;
314 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100315 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800316 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100317
318 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
319 if (!new_tbl)
320 return 0;
321
Herbert Xuda204202017-02-11 19:26:47 +0800322 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
323 err = rhashtable_rehash_chain(ht, old_hash);
324 if (err)
325 return err;
Eric Dumazetae6da1f2018-03-31 12:58:48 -0700326 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +0800327 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100328
329 /* Publish the new table pointer. */
330 rcu_assign_pointer(ht->tbl, new_tbl);
331
Herbert Xuba7c95e2015-03-24 09:53:17 +1100332 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100333 list_for_each_entry(walker, &old_tbl->walkers, list)
334 walker->tbl = NULL;
335
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.
NeilBrown4feb7c72019-03-21 14:42:40 +1100339 * We do this inside the locked region so that
340 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
341 * to check if it should not re-link the table.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100342 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100343 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
NeilBrown4feb7c72019-03-21 14:42:40 +1100344 spin_unlock(&ht->lock);
Herbert Xub8244782015-03-24 00:50:26 +1100345
346 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200347}
348
Herbert Xuda204202017-02-11 19:26:47 +0800349static int rhashtable_rehash_alloc(struct rhashtable *ht,
350 struct bucket_table *old_tbl,
351 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200352{
Herbert Xuda204202017-02-11 19:26:47 +0800353 struct bucket_table *new_tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100354 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200355
356 ASSERT_RHT_MUTEX(ht);
357
Herbert Xuda204202017-02-11 19:26:47 +0800358 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200359 if (new_tbl == NULL)
360 return -ENOMEM;
361
Herbert Xub8244782015-03-24 00:50:26 +1100362 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
363 if (err)
364 bucket_table_free(new_tbl);
365
366 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200367}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200368
369/**
370 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
371 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200372 *
Herbert Xu18093d12015-03-24 00:50:25 +1100373 * This function shrinks the hash table to fit, i.e., the smallest
374 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200375 *
Thomas Graf97defe12015-01-02 23:00:20 +0100376 * The caller must ensure that no concurrent resizing occurs by holding
377 * ht->mutex.
378 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200379 * The caller must ensure that no concurrent table mutations take place.
380 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100381 *
382 * It is valid to have concurrent insertions and deletions protected by per
383 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200384 */
Herbert Xub8244782015-03-24 00:50:26 +1100385static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200386{
Herbert Xuda204202017-02-11 19:26:47 +0800387 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200388 unsigned int nelems = atomic_read(&ht->nelems);
389 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200390
Vegard Nossum12311952016-08-12 20:10:44 +0200391 if (nelems)
392 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100393 if (size < ht->p.min_size)
394 size = ht->p.min_size;
395
396 if (old_tbl->size <= size)
397 return 0;
398
Herbert Xub8244782015-03-24 00:50:26 +1100399 if (rht_dereference(old_tbl->future_tbl, ht))
400 return -EEXIST;
401
Herbert Xuda204202017-02-11 19:26:47 +0800402 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200403}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200404
Thomas Graf97defe12015-01-02 23:00:20 +0100405static void rht_deferred_worker(struct work_struct *work)
406{
407 struct rhashtable *ht;
408 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100409 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100410
Ying Xue57699a42015-01-16 11:13:09 +0800411 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100412 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100413
Thomas Graf97defe12015-01-02 23:00:20 +0100414 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100415 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100416
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100417 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800418 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000419 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800420 err = rhashtable_shrink(ht);
421 else if (tbl->nest)
422 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub8244782015-03-24 00:50:26 +1100423
Herbert Xu408f13e2019-03-21 09:39:52 +0800424 if (!err || err == -EEXIST) {
425 int nerr;
426
427 nerr = rhashtable_rehash_table(ht);
428 err = err ?: nerr;
429 }
Herbert Xub8244782015-03-24 00:50:26 +1100430
Thomas Graf97defe12015-01-02 23:00:20 +0100431 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100432
433 if (err)
434 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100435}
436
Herbert Xuca268932016-09-19 19:00:09 +0800437static int rhashtable_insert_rehash(struct rhashtable *ht,
438 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100439{
440 struct bucket_table *old_tbl;
441 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100442 unsigned int size;
443 int err;
444
445 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100446
447 size = tbl->size;
448
Herbert Xu3cf92222015-12-03 20:41:29 +0800449 err = -EBUSY;
450
Herbert Xuccd57b12015-03-24 00:50:28 +1100451 if (rht_grow_above_75(ht, tbl))
452 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200453 /* Do not schedule more than one rehash */
454 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800455 goto fail;
456
457 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100458
Davidlohr Bueso93f976b2018-08-21 22:01:45 -0700459 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
Herbert Xu3cf92222015-12-03 20:41:29 +0800460 if (new_tbl == NULL)
461 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100462
463 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
464 if (err) {
465 bucket_table_free(new_tbl);
466 if (err == -EEXIST)
467 err = 0;
468 } else
469 schedule_work(&ht->run_work);
470
471 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800472
473fail:
474 /* Do not fail the insert if someone else did a rehash. */
NeilBrownc0690012018-06-18 12:52:50 +1000475 if (likely(rcu_access_pointer(tbl->future_tbl)))
Herbert Xu3cf92222015-12-03 20:41:29 +0800476 return 0;
477
478 /* Schedule async rehash to retry allocation in process context. */
479 if (err == -ENOMEM)
480 schedule_work(&ht->run_work);
481
482 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100483}
Herbert Xuccd57b12015-03-24 00:50:28 +1100484
Herbert Xuca268932016-09-19 19:00:09 +0800485static void *rhashtable_lookup_one(struct rhashtable *ht,
486 struct bucket_table *tbl, unsigned int hash,
487 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100488{
Herbert Xuca268932016-09-19 19:00:09 +0800489 struct rhashtable_compare_arg arg = {
490 .ht = ht,
491 .key = key,
492 };
493 struct rhash_head __rcu **pprev;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100494 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800495 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100496
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200497 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800498 pprev = rht_bucket_var(tbl, hash);
NeilBrownf7ad68b2019-03-21 14:42:40 +1100499 rht_for_each_from(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800500 struct rhlist_head *list;
501 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100502
Herbert Xuca268932016-09-19 19:00:09 +0800503 elasticity--;
504 if (!key ||
505 (ht->p.obj_cmpfn ?
506 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200507 rhashtable_compare(&arg, rht_obj(ht, head)))) {
508 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800509 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200510 }
Herbert Xuca268932016-09-19 19:00:09 +0800511
512 if (!ht->rhlist)
513 return rht_obj(ht, head);
514
515 list = container_of(obj, struct rhlist_head, rhead);
516 plist = container_of(head, struct rhlist_head, rhead);
517
518 RCU_INIT_POINTER(list->next, plist);
519 head = rht_dereference_bucket(head->next, tbl, hash);
520 RCU_INIT_POINTER(list->rhead.next, head);
521 rcu_assign_pointer(*pprev, obj);
522
523 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200524 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100525
Herbert Xuca268932016-09-19 19:00:09 +0800526 if (elasticity <= 0)
527 return ERR_PTR(-EAGAIN);
528
529 return ERR_PTR(-ENOENT);
530}
531
532static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
533 struct bucket_table *tbl,
534 unsigned int hash,
535 struct rhash_head *obj,
536 void *data)
537{
Herbert Xuda204202017-02-11 19:26:47 +0800538 struct rhash_head __rcu **pprev;
Herbert Xuca268932016-09-19 19:00:09 +0800539 struct bucket_table *new_tbl;
540 struct rhash_head *head;
541
542 if (!IS_ERR_OR_NULL(data))
543 return ERR_PTR(-EEXIST);
544
545 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
546 return ERR_CAST(data);
547
NeilBrownc0690012018-06-18 12:52:50 +1000548 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800549 if (new_tbl)
550 return new_tbl;
551
552 if (PTR_ERR(data) != -ENOENT)
553 return ERR_CAST(data);
554
Herbert Xu07ee0722015-05-15 11:30:47 +0800555 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800556 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800557
Herbert Xuca268932016-09-19 19:00:09 +0800558 if (unlikely(rht_grow_above_100(ht, tbl)))
559 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100560
Herbert Xuda204202017-02-11 19:26:47 +0800561 pprev = rht_bucket_insert(ht, tbl, hash);
562 if (!pprev)
563 return ERR_PTR(-ENOMEM);
564
565 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100566
567 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800568 if (ht->rhlist) {
569 struct rhlist_head *list;
570
571 list = container_of(obj, struct rhlist_head, rhead);
572 RCU_INIT_POINTER(list->next, NULL);
573 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100574
Herbert Xuda204202017-02-11 19:26:47 +0800575 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100576
577 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800578 if (rht_grow_above_75(ht, tbl))
579 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100580
Herbert Xuca268932016-09-19 19:00:09 +0800581 return NULL;
582}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100583
Herbert Xuca268932016-09-19 19:00:09 +0800584static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
585 struct rhash_head *obj)
586{
587 struct bucket_table *new_tbl;
588 struct bucket_table *tbl;
589 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800590 void *data;
591
NeilBrown4feb7c72019-03-21 14:42:40 +1100592 new_tbl = rcu_dereference(ht->tbl);
Herbert Xuca268932016-09-19 19:00:09 +0800593
NeilBrown4feb7c72019-03-21 14:42:40 +1100594 do {
Herbert Xuca268932016-09-19 19:00:09 +0800595 tbl = new_tbl;
596 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
NeilBrown4feb7c72019-03-21 14:42:40 +1100597 spin_lock_bh(rht_bucket_lock(tbl, hash));
Herbert Xuca268932016-09-19 19:00:09 +0800598
599 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
600 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
601 if (PTR_ERR(new_tbl) != -EEXIST)
602 data = ERR_CAST(new_tbl);
603
NeilBrown4feb7c72019-03-21 14:42:40 +1100604 spin_unlock_bh(rht_bucket_lock(tbl, hash));
605 } while (!IS_ERR_OR_NULL(new_tbl));
Herbert Xuca268932016-09-19 19:00:09 +0800606
607 if (PTR_ERR(data) == -EAGAIN)
608 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
609 -EAGAIN);
610
611 return data;
612}
613
614void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
615 struct rhash_head *obj)
616{
617 void *data;
618
619 do {
620 rcu_read_lock();
621 data = rhashtable_try_insert(ht, key, obj);
622 rcu_read_unlock();
623 } while (PTR_ERR(data) == -EAGAIN);
624
625 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100626}
627EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
628
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100629/**
Herbert Xu246779d2016-08-18 16:50:56 +0800630 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100631 * @ht: Table to walk over
632 * @iter: Hash table Iterator
633 *
634 * This function prepares a hash table walk.
635 *
636 * Note that if you restart a walk after rhashtable_walk_stop you
637 * may see the same object twice. Also, you may miss objects if
638 * there are removals in between rhashtable_walk_stop and the next
639 * call to rhashtable_walk_start.
640 *
641 * For a completely stable walk you should construct your own data
642 * structure outside the hash table.
643 *
NeilBrown82266e92018-04-24 08:29:13 +1000644 * This function may be called from any process context, including
645 * non-preemptable context, but cannot be called from softirq or
646 * hardirq context.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100647 *
Herbert Xu246779d2016-08-18 16:50:56 +0800648 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100649 */
Herbert Xu246779d2016-08-18 16:50:56 +0800650void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100651{
652 iter->ht = ht;
653 iter->p = NULL;
654 iter->slot = 0;
655 iter->skip = 0;
Tom Herbert2db54b42017-12-04 10:31:42 -0800656 iter->end_of_table = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100657
Herbert Xuc6ff5262015-12-16 16:45:54 +0800658 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800659 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800660 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800661 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800662 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100663}
Herbert Xu246779d2016-08-18 16:50:56 +0800664EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100665
666/**
667 * rhashtable_walk_exit - Free an iterator
668 * @iter: Hash table Iterator
669 *
Herbert Xu6c4128f2019-02-14 22:03:27 +0800670 * This function frees resources allocated by rhashtable_walk_enter.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100671 */
672void rhashtable_walk_exit(struct rhashtable_iter *iter)
673{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800674 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800675 if (iter->walker.tbl)
676 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800677 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100678}
679EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
680
681/**
Tom Herbert97a6ec42017-12-04 10:31:41 -0800682 * rhashtable_walk_start_check - Start a hash table walk
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100683 * @iter: Hash table iterator
684 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200685 * Start a hash table walk at the current iterator position. Note that we take
686 * the RCU lock in all cases including when we return an error. So you must
687 * always call rhashtable_walk_stop to clean up.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100688 *
689 * Returns zero if successful.
690 *
691 * Returns -EAGAIN if resize event occured. Note that the iterator
692 * will rewind back to the beginning and you may use it immediately
693 * by calling rhashtable_walk_next.
Tom Herbert97a6ec42017-12-04 10:31:41 -0800694 *
695 * rhashtable_walk_start is defined as an inline variant that returns
696 * void. This is preferred in cases where the caller would ignore
697 * resize events and always continue.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100698 */
Tom Herbert97a6ec42017-12-04 10:31:41 -0800699int rhashtable_walk_start_check(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100700 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100701{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100702 struct rhashtable *ht = iter->ht;
NeilBrown5d240a82018-04-24 08:29:13 +1000703 bool rhlist = ht->rhlist;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100704
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100705 rcu_read_lock();
706
Herbert Xuc6ff5262015-12-16 16:45:54 +0800707 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800708 if (iter->walker.tbl)
709 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800710 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100711
NeilBrown5d240a82018-04-24 08:29:13 +1000712 if (iter->end_of_table)
713 return 0;
714 if (!iter->walker.tbl) {
Herbert Xu246779d2016-08-18 16:50:56 +0800715 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
NeilBrownb41cc042018-04-24 08:29:13 +1000716 iter->slot = 0;
717 iter->skip = 0;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100718 return -EAGAIN;
719 }
720
NeilBrown5d240a82018-04-24 08:29:13 +1000721 if (iter->p && !rhlist) {
722 /*
723 * We need to validate that 'p' is still in the table, and
724 * if so, update 'skip'
725 */
726 struct rhash_head *p;
727 int skip = 0;
728 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
729 skip++;
730 if (p == iter->p) {
731 iter->skip = skip;
732 goto found;
733 }
734 }
735 iter->p = NULL;
736 } else if (iter->p && rhlist) {
737 /* Need to validate that 'list' is still in the table, and
738 * if so, update 'skip' and 'p'.
739 */
740 struct rhash_head *p;
741 struct rhlist_head *list;
742 int skip = 0;
743 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
744 for (list = container_of(p, struct rhlist_head, rhead);
745 list;
746 list = rcu_dereference(list->next)) {
747 skip++;
748 if (list == iter->list) {
749 iter->p = p;
Rishabh Bhatnagarc643ecf2018-07-02 09:35:34 -0700750 iter->skip = skip;
NeilBrown5d240a82018-04-24 08:29:13 +1000751 goto found;
752 }
753 }
754 }
755 iter->p = NULL;
756 }
757found:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100758 return 0;
759}
Tom Herbert97a6ec42017-12-04 10:31:41 -0800760EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100761
762/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800763 * __rhashtable_walk_find_next - Find the next element in a table (or the first
764 * one in case of a new walk).
765 *
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100766 * @iter: Hash table iterator
767 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800768 * Returns the found object or NULL when the end of the table is reached.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100769 *
Tom Herbert2db54b42017-12-04 10:31:42 -0800770 * Returns -EAGAIN if resize event occurred.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100771 */
Tom Herbert2db54b42017-12-04 10:31:42 -0800772static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100773{
Herbert Xu246779d2016-08-18 16:50:56 +0800774 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xuca268932016-09-19 19:00:09 +0800775 struct rhlist_head *list = iter->list;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100776 struct rhashtable *ht = iter->ht;
777 struct rhash_head *p = iter->p;
Herbert Xuca268932016-09-19 19:00:09 +0800778 bool rhlist = ht->rhlist;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100779
Tom Herbert2db54b42017-12-04 10:31:42 -0800780 if (!tbl)
781 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100782
783 for (; iter->slot < tbl->size; iter->slot++) {
784 int skip = iter->skip;
785
786 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800787 if (rhlist) {
788 list = container_of(p, struct rhlist_head,
789 rhead);
790 do {
791 if (!skip)
792 goto next;
793 skip--;
794 list = rcu_dereference(list->next);
795 } while (list);
796
797 continue;
798 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100799 if (!skip)
800 break;
801 skip--;
802 }
803
804next:
805 if (!rht_is_a_nulls(p)) {
806 iter->skip++;
807 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800808 iter->list = list;
809 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100810 }
811
812 iter->skip = 0;
813 }
814
Phil Sutter142b9422015-07-06 15:51:20 +0200815 iter->p = NULL;
816
Herbert Xud88252f2015-03-24 00:50:19 +1100817 /* Ensure we see any new tables. */
818 smp_rmb();
819
Herbert Xu246779d2016-08-18 16:50:56 +0800820 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
821 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100822 iter->slot = 0;
823 iter->skip = 0;
824 return ERR_PTR(-EAGAIN);
Tom Herbert2db54b42017-12-04 10:31:42 -0800825 } else {
826 iter->end_of_table = true;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100827 }
828
Thomas Grafc936a792015-05-05 02:22:53 +0200829 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100830}
Tom Herbert2db54b42017-12-04 10:31:42 -0800831
832/**
833 * rhashtable_walk_next - Return the next object and advance the iterator
834 * @iter: Hash table iterator
835 *
836 * Note that you must call rhashtable_walk_stop when you are finished
837 * with the walk.
838 *
839 * Returns the next object or NULL when the end of the table is reached.
840 *
841 * Returns -EAGAIN if resize event occurred. Note that the iterator
842 * will rewind back to the beginning and you may continue to use it.
843 */
844void *rhashtable_walk_next(struct rhashtable_iter *iter)
845{
846 struct rhlist_head *list = iter->list;
847 struct rhashtable *ht = iter->ht;
848 struct rhash_head *p = iter->p;
849 bool rhlist = ht->rhlist;
850
851 if (p) {
852 if (!rhlist || !(list = rcu_dereference(list->next))) {
853 p = rcu_dereference(p->next);
854 list = container_of(p, struct rhlist_head, rhead);
855 }
856 if (!rht_is_a_nulls(p)) {
857 iter->skip++;
858 iter->p = p;
859 iter->list = list;
860 return rht_obj(ht, rhlist ? &list->rhead : p);
861 }
862
863 /* At the end of this slot, switch to next one and then find
864 * next entry from that point.
865 */
866 iter->skip = 0;
867 iter->slot++;
868 }
869
870 return __rhashtable_walk_find_next(iter);
871}
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100872EXPORT_SYMBOL_GPL(rhashtable_walk_next);
873
874/**
Tom Herbert2db54b42017-12-04 10:31:42 -0800875 * rhashtable_walk_peek - Return the next object but don't advance the iterator
876 * @iter: Hash table iterator
877 *
878 * Returns the next object or NULL when the end of the table is reached.
879 *
880 * Returns -EAGAIN if resize event occurred. Note that the iterator
881 * will rewind back to the beginning and you may continue to use it.
882 */
883void *rhashtable_walk_peek(struct rhashtable_iter *iter)
884{
885 struct rhlist_head *list = iter->list;
886 struct rhashtable *ht = iter->ht;
887 struct rhash_head *p = iter->p;
888
889 if (p)
890 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
891
892 /* No object found in current iter, find next one in the table. */
893
894 if (iter->skip) {
895 /* A nonzero skip value points to the next entry in the table
896 * beyond that last one that was found. Decrement skip so
897 * we find the current value. __rhashtable_walk_find_next
898 * will restore the original value of skip assuming that
899 * the table hasn't changed.
900 */
901 iter->skip--;
902 }
903
904 return __rhashtable_walk_find_next(iter);
905}
906EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
907
908/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100909 * rhashtable_walk_stop - Finish a hash table walk
910 * @iter: Hash table iterator
911 *
Andreas Gruenbacher06471692017-09-19 12:41:37 +0200912 * Finish a hash table walk. Does not reset the iterator to the start of the
913 * hash table.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100914 */
915void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100916 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100917{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100918 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800919 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100920
Herbert Xueddee5ba2015-03-14 13:57:20 +1100921 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100922 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100923
924 ht = iter->ht;
925
Herbert Xuba7c95e2015-03-24 09:53:17 +1100926 spin_lock(&ht->lock);
NeilBrown4feb7c72019-03-21 14:42:40 +1100927 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
928 /* This bucket table is being freed, don't re-link it. */
Herbert Xu246779d2016-08-18 16:50:56 +0800929 iter->walker.tbl = NULL;
NeilBrown4feb7c72019-03-21 14:42:40 +1100930 else
931 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xuba7c95e2015-03-24 09:53:17 +1100932 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100933
Herbert Xu963ecbd2015-03-15 21:12:04 +1100934out:
935 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100936}
937EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
938
Herbert Xu488fb86e2015-03-20 21:56:59 +1100939static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200940{
Davidlohr Bueso107d01f2018-07-16 13:26:13 -0700941 size_t retsize;
942
943 if (params->nelem_hint)
944 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
945 (unsigned long)params->min_size);
946 else
947 retsize = max(HASH_DEFAULT_SIZE,
948 (unsigned long)params->min_size);
949
950 return retsize;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200951}
952
Herbert Xu31ccde22015-03-24 00:50:21 +1100953static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
954{
955 return jhash2(key, length, seed);
956}
957
Thomas Graf7e1e7762014-08-02 11:47:44 +0200958/**
959 * rhashtable_init - initialize a new hash table
960 * @ht: hash table to be initialized
961 * @params: configuration parameters
962 *
963 * Initializes a new hash table based on the provided configuration
964 * parameters. A table can be configured either with a variable or
965 * fixed length key:
966 *
967 * Configuration Example 1: Fixed length keys
968 * struct test_obj {
969 * int key;
970 * void * my_member;
971 * struct rhash_head node;
972 * };
973 *
974 * struct rhashtable_params params = {
975 * .head_offset = offsetof(struct test_obj, node),
976 * .key_offset = offsetof(struct test_obj, key),
977 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100978 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200979 * };
980 *
981 * Configuration Example 2: Variable length keys
982 * struct test_obj {
983 * [...]
984 * struct rhash_head node;
985 * };
986 *
Patrick McHardy49f7b332015-03-25 13:07:45 +0000987 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200988 * {
989 * struct test_obj *obj = data;
990 *
991 * return [... hash ...];
992 * }
993 *
994 * struct rhashtable_params params = {
995 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100996 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200997 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200998 * };
999 */
Herbert Xu488fb86e2015-03-20 21:56:59 +11001000int rhashtable_init(struct rhashtable *ht,
1001 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001002{
1003 struct bucket_table *tbl;
1004 size_t size;
1005
Herbert Xu31ccde22015-03-24 00:50:21 +11001006 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +11001007 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +02001008 return -EINVAL;
1009
Thomas Graf97defe12015-01-02 23:00:20 +01001010 memset(ht, 0, sizeof(*ht));
1011 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +11001012 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +01001013 memcpy(&ht->p, params, sizeof(*params));
1014
Thomas Grafa998f712015-03-19 22:31:13 +00001015 if (params->min_size)
1016 ht->p.min_size = roundup_pow_of_two(params->min_size);
1017
Herbert Xu6d684e52017-04-27 13:44:51 +08001018 /* Cap total entries at 2^31 to avoid nelems overflow. */
1019 ht->max_elems = 1u << 31;
Herbert Xu2d2ab652017-04-28 14:10:48 +08001020
1021 if (params->max_size) {
1022 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1023 if (ht->p.max_size < ht->max_elems / 2)
1024 ht->max_elems = ht->p.max_size * 2;
1025 }
Herbert Xu6d684e52017-04-27 13:44:51 +08001026
Florian Westphal48e75b432017-05-01 22:18:01 +02001027 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +00001028
Davidlohr Bueso107d01f2018-07-16 13:26:13 -07001029 size = rounded_hashtable_size(&ht->p);
Herbert Xu3a324602015-12-16 18:13:14 +08001030
Thomas Graf97defe12015-01-02 23:00:20 +01001031 if (params->locks_mul)
1032 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1033 else
1034 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1035
Herbert Xu31ccde22015-03-24 00:50:21 +11001036 ht->key_len = ht->p.key_len;
1037 if (!params->hashfn) {
1038 ht->p.hashfn = jhash;
1039
1040 if (!(ht->key_len & (sizeof(u32) - 1))) {
1041 ht->key_len /= sizeof(u32);
1042 ht->p.hashfn = rhashtable_jhash2;
1043 }
1044 }
1045
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001046 /*
1047 * This is api initialization and thus we need to guarantee the
1048 * initial rhashtable allocation. Upon failure, retry with the
1049 * smallest possible size with __GFP_NOFAIL semantics.
1050 */
Herbert Xub9ecfda2015-03-24 00:50:27 +11001051 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Davidlohr Bueso2d22ecf2018-08-21 22:01:48 -07001052 if (unlikely(tbl == NULL)) {
1053 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1054 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1055 }
Thomas Graf7e1e7762014-08-02 11:47:44 +02001056
Ying Xue545a1482015-01-07 13:41:57 +08001057 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001058
Thomas Graf7e1e7762014-08-02 11:47:44 +02001059 RCU_INIT_POINTER(ht->tbl, tbl);
1060
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001061 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001062
Thomas Graf7e1e7762014-08-02 11:47:44 +02001063 return 0;
1064}
1065EXPORT_SYMBOL_GPL(rhashtable_init);
1066
1067/**
Herbert Xuca268932016-09-19 19:00:09 +08001068 * rhltable_init - initialize a new hash list table
1069 * @hlt: hash list table to be initialized
1070 * @params: configuration parameters
1071 *
1072 * Initializes a new hash list table.
1073 *
1074 * See documentation for rhashtable_init.
1075 */
1076int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1077{
1078 int err;
1079
Herbert Xuca268932016-09-19 19:00:09 +08001080 err = rhashtable_init(&hlt->ht, params);
1081 hlt->ht.rhlist = true;
1082 return err;
1083}
1084EXPORT_SYMBOL_GPL(rhltable_init);
1085
1086static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1087 void (*free_fn)(void *ptr, void *arg),
1088 void *arg)
1089{
1090 struct rhlist_head *list;
1091
1092 if (!ht->rhlist) {
1093 free_fn(rht_obj(ht, obj), arg);
1094 return;
1095 }
1096
1097 list = container_of(obj, struct rhlist_head, rhead);
1098 do {
1099 obj = &list->rhead;
1100 list = rht_dereference(list->next, ht);
1101 free_fn(rht_obj(ht, obj), arg);
1102 } while (list);
1103}
1104
1105/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001106 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001107 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001108 * @free_fn: callback to release resources of element
1109 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001110 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001111 * Stops an eventual async resize. If defined, invokes free_fn for each
1112 * element to releasal resources. Please note that RCU protected
1113 * readers may still be accessing the elements. Releasing of resources
1114 * must occur in a compatible manner. Then frees the bucket array.
1115 *
1116 * This function will eventually sleep to wait for an async resize
1117 * to complete. The caller is responsible that no further write operations
1118 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001119 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001120void rhashtable_free_and_destroy(struct rhashtable *ht,
1121 void (*free_fn)(void *ptr, void *arg),
1122 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001123{
Taehee Yoo00261292018-07-08 11:55:51 +09001124 struct bucket_table *tbl, *next_tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001125 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001126
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001127 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001128
Thomas Graf97defe12015-01-02 23:00:20 +01001129 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001130 tbl = rht_dereference(ht->tbl, ht);
Taehee Yoo00261292018-07-08 11:55:51 +09001131restart:
Thomas Graf6b6f3022015-03-24 14:18:20 +01001132 if (free_fn) {
1133 for (i = 0; i < tbl->size; i++) {
1134 struct rhash_head *pos, *next;
1135
Eric Dumazetae6da1f2018-03-31 12:58:48 -07001136 cond_resched();
Herbert Xuda204202017-02-11 19:26:47 +08001137 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001138 next = !rht_is_a_nulls(pos) ?
1139 rht_dereference(pos->next, ht) : NULL;
1140 !rht_is_a_nulls(pos);
1141 pos = next,
1142 next = !rht_is_a_nulls(pos) ?
1143 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001144 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001145 }
1146 }
1147
Taehee Yoo00261292018-07-08 11:55:51 +09001148 next_tbl = rht_dereference(tbl->future_tbl, ht);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001149 bucket_table_free(tbl);
Taehee Yoo00261292018-07-08 11:55:51 +09001150 if (next_tbl) {
1151 tbl = next_tbl;
1152 goto restart;
1153 }
Thomas Graf97defe12015-01-02 23:00:20 +01001154 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001155}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001156EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1157
1158void rhashtable_destroy(struct rhashtable *ht)
1159{
1160 return rhashtable_free_and_destroy(ht, NULL, NULL);
1161}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001162EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001163
1164struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1165 unsigned int hash)
1166{
1167 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
NeilBrown82208d0d2018-11-30 10:26:50 +11001168 static struct rhash_head __rcu *rhnull;
Herbert Xuda204202017-02-11 19:26:47 +08001169 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
NeilBrown82208d0d2018-11-30 10:26:50 +11001186 if (!ntbl) {
1187 if (!rhnull)
1188 INIT_RHT_NULLS_HEAD(rhnull);
Herbert Xuda204202017-02-11 19:26:47 +08001189 return &rhnull;
NeilBrown82208d0d2018-11-30 10:26:50 +11001190 }
Herbert Xuda204202017-02-11 19:26:47 +08001191
1192 return &ntbl[subhash].bucket;
1193
1194}
1195EXPORT_SYMBOL_GPL(rht_bucket_nested);
1196
1197struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1198 struct bucket_table *tbl,
1199 unsigned int hash)
1200{
1201 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1202 unsigned int index = hash & ((1 << tbl->nest) - 1);
1203 unsigned int size = tbl->size >> tbl->nest;
1204 union nested_table *ntbl;
Herbert Xuda204202017-02-11 19:26:47 +08001205
1206 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1207 hash >>= tbl->nest;
Herbert Xuda204202017-02-11 19:26:47 +08001208 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001209 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001210
1211 while (ntbl && size > (1 << shift)) {
1212 index = hash & ((1 << shift) - 1);
1213 size >>= shift;
1214 hash >>= shift;
Herbert Xuda204202017-02-11 19:26:47 +08001215 ntbl = nested_table_alloc(ht, &ntbl[index].table,
NeilBrown5af68ef2018-06-18 12:52:50 +10001216 size <= (1 << shift));
Herbert Xuda204202017-02-11 19:26:47 +08001217 }
1218
1219 if (!ntbl)
1220 return NULL;
1221
1222 return &ntbl[hash].bucket;
1223
1224}
1225EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);