blob: c5b9b9351cec8acdf5fcdafaf88fc58562896391 [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>
Thomas Graf7e1e7762014-08-02 11:47:44 +020022#include <linux/slab.h>
23#include <linux/vmalloc.h>
24#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010025#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020026#include <linux/random.h>
27#include <linux/rhashtable.h>
Stephen Rothwell61d7b092015-02-09 14:04:03 +110028#include <linux/err.h>
Hauke Mehrtens6d795412015-06-06 22:07:23 +020029#include <linux/export.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020030
31#define HASH_DEFAULT_SIZE 64UL
Herbert Xuc2e213c2015-03-18 20:01:16 +110032#define HASH_MIN_SIZE 4U
Florian Westphal4cf0b352016-08-12 12:03:52 +020033#define BUCKET_LOCKS_PER_CPU 32UL
Thomas Graf97defe12015-01-02 23:00:20 +010034
Herbert Xuda204202017-02-11 19:26:47 +080035union nested_table {
36 union nested_table __rcu *table;
37 struct rhash_head __rcu *bucket;
38};
39
Herbert Xu988dfbd2015-03-10 09:27:55 +110040static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010041 const struct bucket_table *tbl,
42 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020043{
Herbert Xu02fd97c2015-03-20 21:57:00 +110044 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020045}
46
Thomas Grafa03eaec2015-02-05 02:03:34 +010047#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010048#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010049
50int lockdep_rht_mutex_is_held(struct rhashtable *ht)
51{
52 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
53}
54EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
55
56int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
57{
Herbert Xu02fd97c2015-03-20 21:57:00 +110058 spinlock_t *lock = rht_bucket_lock(tbl, hash);
Thomas Grafa03eaec2015-02-05 02:03:34 +010059
60 return (debug_locks) ? lockdep_is_held(lock) : 1;
61}
62EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
63#else
64#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +010065#endif
66
67
Herbert Xub9ecfda2015-03-24 00:50:27 +110068static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl,
69 gfp_t gfp)
Thomas Graf97defe12015-01-02 23:00:20 +010070{
71 unsigned int i, size;
72#if defined(CONFIG_PROVE_LOCKING)
73 unsigned int nr_pcpus = 2;
74#else
75 unsigned int nr_pcpus = num_possible_cpus();
76#endif
77
Florian Westphal4cf0b352016-08-12 12:03:52 +020078 nr_pcpus = min_t(unsigned int, nr_pcpus, 64UL);
Thomas Graf97defe12015-01-02 23:00:20 +010079 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
80
Thomas Grafa5ec68e2015-02-05 02:03:32 +010081 /* Never allocate more than 0.5 locks per bucket */
82 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +010083
Herbert Xuda204202017-02-11 19:26:47 +080084 if (tbl->nest)
85 size = min(size, 1U << tbl->nest);
86
Thomas Graf97defe12015-01-02 23:00:20 +010087 if (sizeof(spinlock_t) != 0) {
Eric Dumazet9dbeea72016-08-26 08:51:39 -070088 tbl->locks = NULL;
Thomas Graf97defe12015-01-02 23:00:20 +010089#ifdef CONFIG_NUMA
Herbert Xub9ecfda2015-03-24 00:50:27 +110090 if (size * sizeof(spinlock_t) > PAGE_SIZE &&
91 gfp == GFP_KERNEL)
Thomas Graf97defe12015-01-02 23:00:20 +010092 tbl->locks = vmalloc(size * sizeof(spinlock_t));
Thomas Graf97defe12015-01-02 23:00:20 +010093#endif
Florian Westphal4cf0b352016-08-12 12:03:52 +020094 if (gfp != GFP_KERNEL)
95 gfp |= __GFP_NOWARN | __GFP_NORETRY;
96
Eric Dumazet9dbeea72016-08-26 08:51:39 -070097 if (!tbl->locks)
98 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
99 gfp);
Thomas Graf97defe12015-01-02 23:00:20 +0100100 if (!tbl->locks)
101 return -ENOMEM;
102 for (i = 0; i < size; i++)
103 spin_lock_init(&tbl->locks[i]);
104 }
105 tbl->locks_mask = size - 1;
106
107 return 0;
108}
109
Herbert Xuda204202017-02-11 19:26:47 +0800110static void nested_table_free(union nested_table *ntbl, unsigned int size)
111{
112 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
113 const unsigned int len = 1 << shift;
114 unsigned int i;
115
116 ntbl = rcu_dereference_raw(ntbl->table);
117 if (!ntbl)
118 return;
119
120 if (size > len) {
121 size >>= shift;
122 for (i = 0; i < len; i++)
123 nested_table_free(ntbl + i, size);
124 }
125
126 kfree(ntbl);
127}
128
129static void nested_bucket_table_free(const struct bucket_table *tbl)
130{
131 unsigned int size = tbl->size >> tbl->nest;
132 unsigned int len = 1 << tbl->nest;
133 union nested_table *ntbl;
134 unsigned int i;
135
136 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
137
138 for (i = 0; i < len; i++)
139 nested_table_free(ntbl + i, size);
140
141 kfree(ntbl);
142}
143
Thomas Graf97defe12015-01-02 23:00:20 +0100144static void bucket_table_free(const struct bucket_table *tbl)
145{
Herbert Xuda204202017-02-11 19:26:47 +0800146 if (tbl->nest)
147 nested_bucket_table_free(tbl);
148
Herbert Xuca435402017-02-25 22:38:11 +0800149 kvfree(tbl->locks);
Thomas Graf97defe12015-01-02 23:00:20 +0100150 kvfree(tbl);
151}
152
Herbert Xu9d901bc2015-03-14 13:57:23 +1100153static void bucket_table_free_rcu(struct rcu_head *head)
154{
155 bucket_table_free(container_of(head, struct bucket_table, rcu));
156}
157
Herbert Xuda204202017-02-11 19:26:47 +0800158static union nested_table *nested_table_alloc(struct rhashtable *ht,
159 union nested_table __rcu **prev,
160 unsigned int shifted,
161 unsigned int nhash)
162{
163 union nested_table *ntbl;
164 int i;
165
166 ntbl = rcu_dereference(*prev);
167 if (ntbl)
168 return ntbl;
169
170 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
171
172 if (ntbl && shifted) {
173 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0].bucket); i++)
174 INIT_RHT_NULLS_HEAD(ntbl[i].bucket, ht,
175 (i << shifted) | nhash);
176 }
177
178 rcu_assign_pointer(*prev, ntbl);
179
180 return ntbl;
181}
182
183static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
184 size_t nbuckets,
185 gfp_t gfp)
186{
187 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
188 struct bucket_table *tbl;
189 size_t size;
190
191 if (nbuckets < (1 << (shift + 1)))
192 return NULL;
193
194 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
195
196 tbl = kzalloc(size, gfp);
197 if (!tbl)
198 return NULL;
199
200 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
201 0, 0)) {
202 kfree(tbl);
203 return NULL;
204 }
205
206 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
207
208 return tbl;
209}
210
Thomas Graf97defe12015-01-02 23:00:20 +0100211static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xub9ecfda2015-03-24 00:50:27 +1100212 size_t nbuckets,
213 gfp_t gfp)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200214{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100215 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200216 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100217 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200218
219 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Herbert Xub9ecfda2015-03-24 00:50:27 +1100220 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) ||
221 gfp != GFP_KERNEL)
222 tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
223 if (tbl == NULL && gfp == GFP_KERNEL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200224 tbl = vzalloc(size);
Herbert Xuda204202017-02-11 19:26:47 +0800225
226 size = nbuckets;
227
228 if (tbl == NULL && gfp != GFP_KERNEL) {
229 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
230 nbuckets = 0;
231 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200232 if (tbl == NULL)
233 return NULL;
234
Herbert Xuda204202017-02-11 19:26:47 +0800235 tbl->size = size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200236
Herbert Xub9ecfda2015-03-24 00:50:27 +1100237 if (alloc_bucket_locks(ht, tbl, gfp) < 0) {
Thomas Graf97defe12015-01-02 23:00:20 +0100238 bucket_table_free(tbl);
239 return NULL;
240 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200241
Herbert Xueddee5ba2015-03-14 13:57:20 +1100242 INIT_LIST_HEAD(&tbl->walkers);
243
Herbert Xu5269b532015-03-14 13:57:22 +1100244 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
245
Thomas Graff89bd6f2015-01-02 23:00:21 +0100246 for (i = 0; i < nbuckets; i++)
247 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
248
Thomas Graf97defe12015-01-02 23:00:20 +0100249 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200250}
251
Herbert Xub8244782015-03-24 00:50:26 +1100252static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
253 struct bucket_table *tbl)
254{
255 struct bucket_table *new_tbl;
256
257 do {
258 new_tbl = tbl;
259 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
260 } while (tbl);
261
262 return new_tbl;
263}
264
Thomas Graf299e5c32015-03-24 14:18:17 +0100265static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100266{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100267 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100268 struct bucket_table *new_tbl = rhashtable_last_table(ht,
269 rht_dereference_rcu(old_tbl->future_tbl, ht));
Herbert Xuda204202017-02-11 19:26:47 +0800270 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
271 int err = -EAGAIN;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100272 struct rhash_head *head, *next, *entry;
273 spinlock_t *new_bucket_lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100274 unsigned int new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100275
Herbert Xuda204202017-02-11 19:26:47 +0800276 if (new_tbl->nest)
277 goto out;
278
279 err = -ENOENT;
280
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100281 rht_for_each(entry, old_tbl, old_hash) {
282 err = 0;
283 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100284
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100285 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200286 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100287
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100288 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200289 }
290
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100291 if (err)
292 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100293
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100294 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100295
Herbert Xu02fd97c2015-03-20 21:57:00 +1100296 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100297
Herbert Xu8f2484b2015-03-14 13:57:21 +1100298 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100299 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
300 new_tbl, new_hash);
301
Dmitriy Vyukov7def0f92015-09-22 10:51:52 +0200302 RCU_INIT_POINTER(entry->next, head);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100303
304 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
305 spin_unlock(new_bucket_lock);
306
307 rcu_assign_pointer(*pprev, next);
308
309out:
310 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100311}
312
Herbert Xuda204202017-02-11 19:26:47 +0800313static int rhashtable_rehash_chain(struct rhashtable *ht,
Thomas Graf299e5c32015-03-24 14:18:17 +0100314 unsigned int old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100315{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100316 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
317 spinlock_t *old_bucket_lock;
Herbert Xuda204202017-02-11 19:26:47 +0800318 int err;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100319
Herbert Xu02fd97c2015-03-20 21:57:00 +1100320 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100321
322 spin_lock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800323 while (!(err = rhashtable_rehash_one(ht, old_hash)))
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100324 ;
Herbert Xuda204202017-02-11 19:26:47 +0800325
326 if (err == -ENOENT) {
327 old_tbl->rehash++;
328 err = 0;
329 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100330 spin_unlock_bh(old_bucket_lock);
Herbert Xuda204202017-02-11 19:26:47 +0800331
332 return err;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100333}
334
Herbert Xub8244782015-03-24 00:50:26 +1100335static int rhashtable_rehash_attach(struct rhashtable *ht,
336 struct bucket_table *old_tbl,
337 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100338{
Herbert Xub8244782015-03-24 00:50:26 +1100339 /* Protect future_tbl using the first bucket lock. */
340 spin_lock_bh(old_tbl->locks);
341
342 /* Did somebody beat us to it? */
343 if (rcu_access_pointer(old_tbl->future_tbl)) {
344 spin_unlock_bh(old_tbl->locks);
345 return -EEXIST;
346 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100347
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100348 /* Make insertions go into the new, empty table right away. Deletions
349 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100350 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100351 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100352
Herbert Xub8244782015-03-24 00:50:26 +1100353 spin_unlock_bh(old_tbl->locks);
354
355 return 0;
356}
357
358static int rhashtable_rehash_table(struct rhashtable *ht)
359{
360 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
361 struct bucket_table *new_tbl;
362 struct rhashtable_walker *walker;
Thomas Graf299e5c32015-03-24 14:18:17 +0100363 unsigned int old_hash;
Herbert Xuda204202017-02-11 19:26:47 +0800364 int err;
Herbert Xub8244782015-03-24 00:50:26 +1100365
366 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
367 if (!new_tbl)
368 return 0;
369
Herbert Xuda204202017-02-11 19:26:47 +0800370 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
371 err = rhashtable_rehash_chain(ht, old_hash);
372 if (err)
373 return err;
374 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100375
376 /* Publish the new table pointer. */
377 rcu_assign_pointer(ht->tbl, new_tbl);
378
Herbert Xuba7c95e2015-03-24 09:53:17 +1100379 spin_lock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100380 list_for_each_entry(walker, &old_tbl->walkers, list)
381 walker->tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100382 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100383
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100384 /* Wait for readers. All new readers will see the new
385 * table, and thus no references to the old table will
386 * remain.
387 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100388 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Herbert Xub8244782015-03-24 00:50:26 +1100389
390 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200391}
392
Herbert Xuda204202017-02-11 19:26:47 +0800393static int rhashtable_rehash_alloc(struct rhashtable *ht,
394 struct bucket_table *old_tbl,
395 unsigned int size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200396{
Herbert Xuda204202017-02-11 19:26:47 +0800397 struct bucket_table *new_tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100398 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200399
400 ASSERT_RHT_MUTEX(ht);
401
Herbert Xuda204202017-02-11 19:26:47 +0800402 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200403 if (new_tbl == NULL)
404 return -ENOMEM;
405
Herbert Xub8244782015-03-24 00:50:26 +1100406 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
407 if (err)
408 bucket_table_free(new_tbl);
409
410 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200411}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200412
413/**
414 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
415 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200416 *
Herbert Xu18093d12015-03-24 00:50:25 +1100417 * This function shrinks the hash table to fit, i.e., the smallest
418 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200419 *
Thomas Graf97defe12015-01-02 23:00:20 +0100420 * The caller must ensure that no concurrent resizing occurs by holding
421 * ht->mutex.
422 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200423 * The caller must ensure that no concurrent table mutations take place.
424 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100425 *
426 * It is valid to have concurrent insertions and deletions protected by per
427 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200428 */
Herbert Xub8244782015-03-24 00:50:26 +1100429static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200430{
Herbert Xuda204202017-02-11 19:26:47 +0800431 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Vegard Nossum12311952016-08-12 20:10:44 +0200432 unsigned int nelems = atomic_read(&ht->nelems);
433 unsigned int size = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200434
Vegard Nossum12311952016-08-12 20:10:44 +0200435 if (nelems)
436 size = roundup_pow_of_two(nelems * 3 / 2);
Herbert Xu18093d12015-03-24 00:50:25 +1100437 if (size < ht->p.min_size)
438 size = ht->p.min_size;
439
440 if (old_tbl->size <= size)
441 return 0;
442
Herbert Xub8244782015-03-24 00:50:26 +1100443 if (rht_dereference(old_tbl->future_tbl, ht))
444 return -EEXIST;
445
Herbert Xuda204202017-02-11 19:26:47 +0800446 return rhashtable_rehash_alloc(ht, old_tbl, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200447}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200448
Thomas Graf97defe12015-01-02 23:00:20 +0100449static void rht_deferred_worker(struct work_struct *work)
450{
451 struct rhashtable *ht;
452 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100453 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100454
Ying Xue57699a42015-01-16 11:13:09 +0800455 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100456 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100457
Thomas Graf97defe12015-01-02 23:00:20 +0100458 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100459 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100460
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100461 if (rht_grow_above_75(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800462 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000463 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
Herbert Xuda204202017-02-11 19:26:47 +0800464 err = rhashtable_shrink(ht);
465 else if (tbl->nest)
466 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
Herbert Xub8244782015-03-24 00:50:26 +1100467
Herbert Xuda204202017-02-11 19:26:47 +0800468 if (!err)
469 err = rhashtable_rehash_table(ht);
Herbert Xub8244782015-03-24 00:50:26 +1100470
Thomas Graf97defe12015-01-02 23:00:20 +0100471 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100472
473 if (err)
474 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100475}
476
Herbert Xuca268932016-09-19 19:00:09 +0800477static int rhashtable_insert_rehash(struct rhashtable *ht,
478 struct bucket_table *tbl)
Herbert Xuccd57b12015-03-24 00:50:28 +1100479{
480 struct bucket_table *old_tbl;
481 struct bucket_table *new_tbl;
Herbert Xuccd57b12015-03-24 00:50:28 +1100482 unsigned int size;
483 int err;
484
485 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuccd57b12015-03-24 00:50:28 +1100486
487 size = tbl->size;
488
Herbert Xu3cf92222015-12-03 20:41:29 +0800489 err = -EBUSY;
490
Herbert Xuccd57b12015-03-24 00:50:28 +1100491 if (rht_grow_above_75(ht, tbl))
492 size *= 2;
Thomas Grafa87b9eb2015-04-22 09:41:46 +0200493 /* Do not schedule more than one rehash */
494 else if (old_tbl != tbl)
Herbert Xu3cf92222015-12-03 20:41:29 +0800495 goto fail;
496
497 err = -ENOMEM;
Herbert Xuccd57b12015-03-24 00:50:28 +1100498
499 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
Herbert Xu3cf92222015-12-03 20:41:29 +0800500 if (new_tbl == NULL)
501 goto fail;
Herbert Xuccd57b12015-03-24 00:50:28 +1100502
503 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
504 if (err) {
505 bucket_table_free(new_tbl);
506 if (err == -EEXIST)
507 err = 0;
508 } else
509 schedule_work(&ht->run_work);
510
511 return err;
Herbert Xu3cf92222015-12-03 20:41:29 +0800512
513fail:
514 /* Do not fail the insert if someone else did a rehash. */
515 if (likely(rcu_dereference_raw(tbl->future_tbl)))
516 return 0;
517
518 /* Schedule async rehash to retry allocation in process context. */
519 if (err == -ENOMEM)
520 schedule_work(&ht->run_work);
521
522 return err;
Herbert Xuccd57b12015-03-24 00:50:28 +1100523}
Herbert Xuccd57b12015-03-24 00:50:28 +1100524
Herbert Xuca268932016-09-19 19:00:09 +0800525static void *rhashtable_lookup_one(struct rhashtable *ht,
526 struct bucket_table *tbl, unsigned int hash,
527 const void *key, struct rhash_head *obj)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100528{
Herbert Xuca268932016-09-19 19:00:09 +0800529 struct rhashtable_compare_arg arg = {
530 .ht = ht,
531 .key = key,
532 };
533 struct rhash_head __rcu **pprev;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100534 struct rhash_head *head;
Herbert Xuca268932016-09-19 19:00:09 +0800535 int elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100536
Herbert Xuca268932016-09-19 19:00:09 +0800537 elasticity = ht->elasticity;
Herbert Xuda204202017-02-11 19:26:47 +0800538 pprev = rht_bucket_var(tbl, hash);
539 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800540 struct rhlist_head *list;
541 struct rhlist_head *plist;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100542
Herbert Xuca268932016-09-19 19:00:09 +0800543 elasticity--;
544 if (!key ||
545 (ht->p.obj_cmpfn ?
546 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
547 rhashtable_compare(&arg, rht_obj(ht, head))))
548 continue;
549
550 if (!ht->rhlist)
551 return rht_obj(ht, head);
552
553 list = container_of(obj, struct rhlist_head, rhead);
554 plist = container_of(head, struct rhlist_head, rhead);
555
556 RCU_INIT_POINTER(list->next, plist);
557 head = rht_dereference_bucket(head->next, tbl, hash);
558 RCU_INIT_POINTER(list->rhead.next, head);
559 rcu_assign_pointer(*pprev, obj);
560
561 return NULL;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200562 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100563
Herbert Xuca268932016-09-19 19:00:09 +0800564 if (elasticity <= 0)
565 return ERR_PTR(-EAGAIN);
566
567 return ERR_PTR(-ENOENT);
568}
569
570static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
571 struct bucket_table *tbl,
572 unsigned int hash,
573 struct rhash_head *obj,
574 void *data)
575{
Herbert Xuda204202017-02-11 19:26:47 +0800576 struct rhash_head __rcu **pprev;
Herbert Xuca268932016-09-19 19:00:09 +0800577 struct bucket_table *new_tbl;
578 struct rhash_head *head;
579
580 if (!IS_ERR_OR_NULL(data))
581 return ERR_PTR(-EEXIST);
582
583 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
584 return ERR_CAST(data);
585
586 new_tbl = rcu_dereference(tbl->future_tbl);
587 if (new_tbl)
588 return new_tbl;
589
590 if (PTR_ERR(data) != -ENOENT)
591 return ERR_CAST(data);
592
Herbert Xu07ee0722015-05-15 11:30:47 +0800593 if (unlikely(rht_grow_above_max(ht, tbl)))
Herbert Xuca268932016-09-19 19:00:09 +0800594 return ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800595
Herbert Xuca268932016-09-19 19:00:09 +0800596 if (unlikely(rht_grow_above_100(ht, tbl)))
597 return ERR_PTR(-EAGAIN);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100598
Herbert Xuda204202017-02-11 19:26:47 +0800599 pprev = rht_bucket_insert(ht, tbl, hash);
600 if (!pprev)
601 return ERR_PTR(-ENOMEM);
602
603 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100604
605 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800606 if (ht->rhlist) {
607 struct rhlist_head *list;
608
609 list = container_of(obj, struct rhlist_head, rhead);
610 RCU_INIT_POINTER(list->next, NULL);
611 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100612
Herbert Xuda204202017-02-11 19:26:47 +0800613 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100614
615 atomic_inc(&ht->nelems);
Herbert Xuca268932016-09-19 19:00:09 +0800616 if (rht_grow_above_75(ht, tbl))
617 schedule_work(&ht->run_work);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100618
Herbert Xuca268932016-09-19 19:00:09 +0800619 return NULL;
620}
Herbert Xu02fd97c2015-03-20 21:57:00 +1100621
Herbert Xuca268932016-09-19 19:00:09 +0800622static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
623 struct rhash_head *obj)
624{
625 struct bucket_table *new_tbl;
626 struct bucket_table *tbl;
627 unsigned int hash;
628 spinlock_t *lock;
629 void *data;
630
631 tbl = rcu_dereference(ht->tbl);
632
633 /* All insertions must grab the oldest table containing
634 * the hashed bucket that is yet to be rehashed.
635 */
636 for (;;) {
637 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
638 lock = rht_bucket_lock(tbl, hash);
639 spin_lock_bh(lock);
640
641 if (tbl->rehash <= hash)
642 break;
643
644 spin_unlock_bh(lock);
645 tbl = rcu_dereference(tbl->future_tbl);
646 }
647
648 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
649 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
650 if (PTR_ERR(new_tbl) != -EEXIST)
651 data = ERR_CAST(new_tbl);
652
653 while (!IS_ERR_OR_NULL(new_tbl)) {
654 tbl = new_tbl;
655 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
656 spin_lock_nested(rht_bucket_lock(tbl, hash),
657 SINGLE_DEPTH_NESTING);
658
659 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
660 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
661 if (PTR_ERR(new_tbl) != -EEXIST)
662 data = ERR_CAST(new_tbl);
663
664 spin_unlock(rht_bucket_lock(tbl, hash));
665 }
666
667 spin_unlock_bh(lock);
668
669 if (PTR_ERR(data) == -EAGAIN)
670 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
671 -EAGAIN);
672
673 return data;
674}
675
676void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
677 struct rhash_head *obj)
678{
679 void *data;
680
681 do {
682 rcu_read_lock();
683 data = rhashtable_try_insert(ht, key, obj);
684 rcu_read_unlock();
685 } while (PTR_ERR(data) == -EAGAIN);
686
687 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100688}
689EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
690
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100691/**
Herbert Xu246779d2016-08-18 16:50:56 +0800692 * rhashtable_walk_enter - Initialise an iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100693 * @ht: Table to walk over
694 * @iter: Hash table Iterator
695 *
696 * This function prepares a hash table walk.
697 *
698 * Note that if you restart a walk after rhashtable_walk_stop you
699 * may see the same object twice. Also, you may miss objects if
700 * there are removals in between rhashtable_walk_stop and the next
701 * call to rhashtable_walk_start.
702 *
703 * For a completely stable walk you should construct your own data
704 * structure outside the hash table.
705 *
706 * This function may sleep so you must not call it from interrupt
707 * context or with spin locks held.
708 *
Herbert Xu246779d2016-08-18 16:50:56 +0800709 * You must call rhashtable_walk_exit after this function returns.
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100710 */
Herbert Xu246779d2016-08-18 16:50:56 +0800711void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100712{
713 iter->ht = ht;
714 iter->p = NULL;
715 iter->slot = 0;
716 iter->skip = 0;
717
Herbert Xuc6ff5262015-12-16 16:45:54 +0800718 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800719 iter->walker.tbl =
Herbert Xu179ccc02015-12-19 10:45:28 +0800720 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
Herbert Xu246779d2016-08-18 16:50:56 +0800721 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800722 spin_unlock(&ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100723}
Herbert Xu246779d2016-08-18 16:50:56 +0800724EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100725
726/**
727 * rhashtable_walk_exit - Free an iterator
728 * @iter: Hash table Iterator
729 *
730 * This function frees resources allocated by rhashtable_walk_init.
731 */
732void rhashtable_walk_exit(struct rhashtable_iter *iter)
733{
Herbert Xuc6ff5262015-12-16 16:45:54 +0800734 spin_lock(&iter->ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800735 if (iter->walker.tbl)
736 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800737 spin_unlock(&iter->ht->lock);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100738}
739EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
740
741/**
742 * rhashtable_walk_start - Start a hash table walk
743 * @iter: Hash table iterator
744 *
745 * Start a hash table walk. Note that we take the RCU lock in all
746 * cases including when we return an error. So you must always call
747 * rhashtable_walk_stop to clean up.
748 *
749 * Returns zero if successful.
750 *
751 * Returns -EAGAIN if resize event occured. Note that the iterator
752 * will rewind back to the beginning and you may use it immediately
753 * by calling rhashtable_walk_next.
754 */
755int rhashtable_walk_start(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100756 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100757{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100758 struct rhashtable *ht = iter->ht;
759
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100760 rcu_read_lock();
761
Herbert Xuc6ff5262015-12-16 16:45:54 +0800762 spin_lock(&ht->lock);
Herbert Xu246779d2016-08-18 16:50:56 +0800763 if (iter->walker.tbl)
764 list_del(&iter->walker.list);
Herbert Xuc6ff5262015-12-16 16:45:54 +0800765 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100766
Herbert Xu246779d2016-08-18 16:50:56 +0800767 if (!iter->walker.tbl) {
768 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100769 return -EAGAIN;
770 }
771
772 return 0;
773}
774EXPORT_SYMBOL_GPL(rhashtable_walk_start);
775
776/**
777 * rhashtable_walk_next - Return the next object and advance the iterator
778 * @iter: Hash table iterator
779 *
780 * Note that you must call rhashtable_walk_stop when you are finished
781 * with the walk.
782 *
783 * Returns the next object or NULL when the end of the table is reached.
784 *
785 * Returns -EAGAIN if resize event occured. Note that the iterator
786 * will rewind back to the beginning and you may continue to use it.
787 */
788void *rhashtable_walk_next(struct rhashtable_iter *iter)
789{
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
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100796 if (p) {
Herbert Xuca268932016-09-19 19:00:09 +0800797 if (!rhlist || !(list = rcu_dereference(list->next))) {
798 p = rcu_dereference(p->next);
799 list = container_of(p, struct rhlist_head, rhead);
800 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100801 goto next;
802 }
803
804 for (; iter->slot < tbl->size; iter->slot++) {
805 int skip = iter->skip;
806
807 rht_for_each_rcu(p, tbl, iter->slot) {
Herbert Xuca268932016-09-19 19:00:09 +0800808 if (rhlist) {
809 list = container_of(p, struct rhlist_head,
810 rhead);
811 do {
812 if (!skip)
813 goto next;
814 skip--;
815 list = rcu_dereference(list->next);
816 } while (list);
817
818 continue;
819 }
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100820 if (!skip)
821 break;
822 skip--;
823 }
824
825next:
826 if (!rht_is_a_nulls(p)) {
827 iter->skip++;
828 iter->p = p;
Herbert Xuca268932016-09-19 19:00:09 +0800829 iter->list = list;
830 return rht_obj(ht, rhlist ? &list->rhead : p);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100831 }
832
833 iter->skip = 0;
834 }
835
Phil Sutter142b9422015-07-06 15:51:20 +0200836 iter->p = NULL;
837
Herbert Xud88252f2015-03-24 00:50:19 +1100838 /* Ensure we see any new tables. */
839 smp_rmb();
840
Herbert Xu246779d2016-08-18 16:50:56 +0800841 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
842 if (iter->walker.tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100843 iter->slot = 0;
844 iter->skip = 0;
845 return ERR_PTR(-EAGAIN);
846 }
847
Thomas Grafc936a792015-05-05 02:22:53 +0200848 return NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100849}
850EXPORT_SYMBOL_GPL(rhashtable_walk_next);
851
852/**
853 * rhashtable_walk_stop - Finish a hash table walk
854 * @iter: Hash table iterator
855 *
856 * Finish a hash table walk.
857 */
858void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100859 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100860{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100861 struct rhashtable *ht;
Herbert Xu246779d2016-08-18 16:50:56 +0800862 struct bucket_table *tbl = iter->walker.tbl;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100863
Herbert Xueddee5ba2015-03-14 13:57:20 +1100864 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100865 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100866
867 ht = iter->ht;
868
Herbert Xuba7c95e2015-03-24 09:53:17 +1100869 spin_lock(&ht->lock);
Herbert Xuc4db8842015-03-14 13:57:25 +1100870 if (tbl->rehash < tbl->size)
Herbert Xu246779d2016-08-18 16:50:56 +0800871 list_add(&iter->walker.list, &tbl->walkers);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100872 else
Herbert Xu246779d2016-08-18 16:50:56 +0800873 iter->walker.tbl = NULL;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100874 spin_unlock(&ht->lock);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100875
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100876 iter->p = NULL;
Herbert Xu963ecbd2015-03-15 21:12:04 +1100877
878out:
879 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100880}
881EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
882
Herbert Xu488fb86e2015-03-20 21:56:59 +1100883static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200884{
Ying Xue94000172014-09-03 09:22:36 +0800885 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xue2e21c12015-03-18 20:01:21 +1100886 (unsigned long)params->min_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200887}
888
Herbert Xu31ccde22015-03-24 00:50:21 +1100889static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
890{
891 return jhash2(key, length, seed);
892}
893
Thomas Graf7e1e7762014-08-02 11:47:44 +0200894/**
895 * rhashtable_init - initialize a new hash table
896 * @ht: hash table to be initialized
897 * @params: configuration parameters
898 *
899 * Initializes a new hash table based on the provided configuration
900 * parameters. A table can be configured either with a variable or
901 * fixed length key:
902 *
903 * Configuration Example 1: Fixed length keys
904 * struct test_obj {
905 * int key;
906 * void * my_member;
907 * struct rhash_head node;
908 * };
909 *
910 * struct rhashtable_params params = {
911 * .head_offset = offsetof(struct test_obj, node),
912 * .key_offset = offsetof(struct test_obj, key),
913 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100914 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100915 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200916 * };
917 *
918 * Configuration Example 2: Variable length keys
919 * struct test_obj {
920 * [...]
921 * struct rhash_head node;
922 * };
923 *
Patrick McHardy49f7b332015-03-25 13:07:45 +0000924 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200925 * {
926 * struct test_obj *obj = data;
927 *
928 * return [... hash ...];
929 * }
930 *
931 * struct rhashtable_params params = {
932 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100933 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200934 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200935 * };
936 */
Herbert Xu488fb86e2015-03-20 21:56:59 +1100937int rhashtable_init(struct rhashtable *ht,
938 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200939{
940 struct bucket_table *tbl;
941 size_t size;
942
943 size = HASH_DEFAULT_SIZE;
944
Herbert Xu31ccde22015-03-24 00:50:21 +1100945 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +1100946 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200947 return -EINVAL;
948
Thomas Graff89bd6f2015-01-02 23:00:21 +0100949 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
950 return -EINVAL;
951
Thomas Graf97defe12015-01-02 23:00:20 +0100952 memset(ht, 0, sizeof(*ht));
953 mutex_init(&ht->mutex);
Herbert Xuba7c95e2015-03-24 09:53:17 +1100954 spin_lock_init(&ht->lock);
Thomas Graf97defe12015-01-02 23:00:20 +0100955 memcpy(&ht->p, params, sizeof(*params));
956
Thomas Grafa998f712015-03-19 22:31:13 +0000957 if (params->min_size)
958 ht->p.min_size = roundup_pow_of_two(params->min_size);
959
960 if (params->max_size)
961 ht->p.max_size = rounddown_pow_of_two(params->max_size);
962
Herbert Xu07ee0722015-05-15 11:30:47 +0800963 if (params->insecure_max_entries)
964 ht->p.insecure_max_entries =
965 rounddown_pow_of_two(params->insecure_max_entries);
966 else
967 ht->p.insecure_max_entries = ht->p.max_size * 2;
968
Herbert Xu488fb86e2015-03-20 21:56:59 +1100969 ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +0000970
Herbert Xu3a324602015-12-16 18:13:14 +0800971 if (params->nelem_hint)
972 size = rounded_hashtable_size(&ht->p);
973
Herbert Xu27ed44a2015-03-24 13:37:30 +1100974 /* The maximum (not average) chain length grows with the
975 * size of the hash table, at a rate of (log N)/(log log N).
976 * The value of 16 is selected so that even if the hash
977 * table grew to 2^32 you would not expect the maximum
978 * chain length to exceed it unless we are under attack
979 * (or extremely unlucky).
980 *
981 * As this limit is only to detect attacks, we don't need
982 * to set it to a lower value as you'd need the chain
983 * length to vastly exceed 16 to have any real effect
984 * on the system.
985 */
Herbert Xuccd57b12015-03-24 00:50:28 +1100986 if (!params->insecure_elasticity)
987 ht->elasticity = 16;
988
Thomas Graf97defe12015-01-02 23:00:20 +0100989 if (params->locks_mul)
990 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
991 else
992 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
993
Herbert Xu31ccde22015-03-24 00:50:21 +1100994 ht->key_len = ht->p.key_len;
995 if (!params->hashfn) {
996 ht->p.hashfn = jhash;
997
998 if (!(ht->key_len & (sizeof(u32) - 1))) {
999 ht->key_len /= sizeof(u32);
1000 ht->p.hashfn = rhashtable_jhash2;
1001 }
1002 }
1003
Herbert Xub9ecfda2015-03-24 00:50:27 +11001004 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001005 if (tbl == NULL)
1006 return -ENOMEM;
1007
Ying Xue545a1482015-01-07 13:41:57 +08001008 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +01001009
Thomas Graf7e1e7762014-08-02 11:47:44 +02001010 RCU_INIT_POINTER(ht->tbl, tbl);
1011
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001012 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001013
Thomas Graf7e1e7762014-08-02 11:47:44 +02001014 return 0;
1015}
1016EXPORT_SYMBOL_GPL(rhashtable_init);
1017
1018/**
Herbert Xuca268932016-09-19 19:00:09 +08001019 * rhltable_init - initialize a new hash list table
1020 * @hlt: hash list table to be initialized
1021 * @params: configuration parameters
1022 *
1023 * Initializes a new hash list table.
1024 *
1025 * See documentation for rhashtable_init.
1026 */
1027int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1028{
1029 int err;
1030
1031 /* No rhlist NULLs marking for now. */
1032 if (params->nulls_base)
1033 return -EINVAL;
1034
1035 err = rhashtable_init(&hlt->ht, params);
1036 hlt->ht.rhlist = true;
1037 return err;
1038}
1039EXPORT_SYMBOL_GPL(rhltable_init);
1040
1041static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1042 void (*free_fn)(void *ptr, void *arg),
1043 void *arg)
1044{
1045 struct rhlist_head *list;
1046
1047 if (!ht->rhlist) {
1048 free_fn(rht_obj(ht, obj), arg);
1049 return;
1050 }
1051
1052 list = container_of(obj, struct rhlist_head, rhead);
1053 do {
1054 obj = &list->rhead;
1055 list = rht_dereference(list->next, ht);
1056 free_fn(rht_obj(ht, obj), arg);
1057 } while (list);
1058}
1059
1060/**
Thomas Graf6b6f3022015-03-24 14:18:20 +01001061 * rhashtable_free_and_destroy - free elements and destroy hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +02001062 * @ht: the hash table to destroy
Thomas Graf6b6f3022015-03-24 14:18:20 +01001063 * @free_fn: callback to release resources of element
1064 * @arg: pointer passed to free_fn
Thomas Graf7e1e7762014-08-02 11:47:44 +02001065 *
Thomas Graf6b6f3022015-03-24 14:18:20 +01001066 * Stops an eventual async resize. If defined, invokes free_fn for each
1067 * element to releasal resources. Please note that RCU protected
1068 * readers may still be accessing the elements. Releasing of resources
1069 * must occur in a compatible manner. Then frees the bucket array.
1070 *
1071 * This function will eventually sleep to wait for an async resize
1072 * to complete. The caller is responsible that no further write operations
1073 * occurs in parallel.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001074 */
Thomas Graf6b6f3022015-03-24 14:18:20 +01001075void rhashtable_free_and_destroy(struct rhashtable *ht,
1076 void (*free_fn)(void *ptr, void *arg),
1077 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001078{
Herbert Xuda204202017-02-11 19:26:47 +08001079 struct bucket_table *tbl;
Thomas Graf6b6f3022015-03-24 14:18:20 +01001080 unsigned int i;
Thomas Graf97defe12015-01-02 23:00:20 +01001081
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001082 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001083
Thomas Graf97defe12015-01-02 23:00:20 +01001084 mutex_lock(&ht->mutex);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001085 tbl = rht_dereference(ht->tbl, ht);
1086 if (free_fn) {
1087 for (i = 0; i < tbl->size; i++) {
1088 struct rhash_head *pos, *next;
1089
Herbert Xuda204202017-02-11 19:26:47 +08001090 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
Thomas Graf6b6f3022015-03-24 14:18:20 +01001091 next = !rht_is_a_nulls(pos) ?
1092 rht_dereference(pos->next, ht) : NULL;
1093 !rht_is_a_nulls(pos);
1094 pos = next,
1095 next = !rht_is_a_nulls(pos) ?
1096 rht_dereference(pos->next, ht) : NULL)
Herbert Xuca268932016-09-19 19:00:09 +08001097 rhashtable_free_one(ht, pos, free_fn, arg);
Thomas Graf6b6f3022015-03-24 14:18:20 +01001098 }
1099 }
1100
1101 bucket_table_free(tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001102 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001103}
Thomas Graf6b6f3022015-03-24 14:18:20 +01001104EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1105
1106void rhashtable_destroy(struct rhashtable *ht)
1107{
1108 return rhashtable_free_and_destroy(ht, NULL, NULL);
1109}
Thomas Graf7e1e7762014-08-02 11:47:44 +02001110EXPORT_SYMBOL_GPL(rhashtable_destroy);
Herbert Xuda204202017-02-11 19:26:47 +08001111
1112struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1113 unsigned int hash)
1114{
1115 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1116 static struct rhash_head __rcu *rhnull =
1117 (struct rhash_head __rcu *)NULLS_MARKER(0);
1118 unsigned int index = hash & ((1 << tbl->nest) - 1);
1119 unsigned int size = tbl->size >> tbl->nest;
1120 unsigned int subhash = hash;
1121 union nested_table *ntbl;
1122
1123 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
Herbert Xuc4d26032017-02-25 22:39:50 +08001124 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001125 subhash >>= tbl->nest;
1126
1127 while (ntbl && size > (1 << shift)) {
1128 index = subhash & ((1 << shift) - 1);
Herbert Xuc4d26032017-02-25 22:39:50 +08001129 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1130 tbl, hash);
Herbert Xuda204202017-02-11 19:26:47 +08001131 size >>= shift;
1132 subhash >>= shift;
1133 }
1134
1135 if (!ntbl)
1136 return &rhnull;
1137
1138 return &ntbl[subhash].bucket;
1139
1140}
1141EXPORT_SYMBOL_GPL(rht_bucket_nested);
1142
1143struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1144 struct bucket_table *tbl,
1145 unsigned int hash)
1146{
1147 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1148 unsigned int index = hash & ((1 << tbl->nest) - 1);
1149 unsigned int size = tbl->size >> tbl->nest;
1150 union nested_table *ntbl;
1151 unsigned int shifted;
1152 unsigned int nhash;
1153
1154 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1155 hash >>= tbl->nest;
1156 nhash = index;
1157 shifted = tbl->nest;
1158 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1159 size <= (1 << shift) ? shifted : 0, nhash);
1160
1161 while (ntbl && size > (1 << shift)) {
1162 index = hash & ((1 << shift) - 1);
1163 size >>= shift;
1164 hash >>= shift;
1165 nhash |= index << shifted;
1166 shifted += shift;
1167 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1168 size <= (1 << shift) ? shifted : 0,
1169 nhash);
1170 }
1171
1172 if (!ntbl)
1173 return NULL;
1174
1175 return &ntbl[hash].bucket;
1176
1177}
1178EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);