]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - lib/rhashtable.c
rhashtable: use cmpxchg() in nested_table_alloc()
[thirdparty/kernel/stable.git] / lib / rhashtable.c
CommitLineData
7e1e7763
TG
1/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
02fd97c3 4 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
a5ec68e3 5 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
7e1e7763
TG
6 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
7e1e7763 8 * Code partially derived from nft_hash
02fd97c3
HX
9 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
7e1e7763
TG
11 *
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
07ee0722 17#include <linux/atomic.h>
7e1e7763
TG
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/log2.h>
5beb5c90 21#include <linux/sched.h>
b2d09103 22#include <linux/rculist.h>
7e1e7763
TG
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <linux/mm.h>
87545899 26#include <linux/jhash.h>
7e1e7763
TG
27#include <linux/random.h>
28#include <linux/rhashtable.h>
61d7b097 29#include <linux/err.h>
6d795413 30#include <linux/export.h>
7e1e7763
TG
31
32#define HASH_DEFAULT_SIZE 64UL
c2e213cf 33#define HASH_MIN_SIZE 4U
4cf0b354 34#define BUCKET_LOCKS_PER_CPU 32UL
97defe1e 35
da20420f
HX
36union nested_table {
37 union nested_table __rcu *table;
38 struct rhash_head __rcu *bucket;
39};
40
988dfbd7 41static u32 head_hashfn(struct rhashtable *ht,
8d24c0b4
TG
42 const struct bucket_table *tbl,
43 const struct rhash_head *he)
7e1e7763 44{
02fd97c3 45 return rht_head_hashfn(ht, tbl, he, ht->p);
7e1e7763
TG
46}
47
a03eaec0 48#ifdef CONFIG_PROVE_LOCKING
a03eaec0 49#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
a03eaec0
TG
50
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{
02fd97c3 59 spinlock_t *lock = rht_bucket_lock(tbl, hash);
a03eaec0
TG
60
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)
a03eaec0
TG
66#endif
67
da20420f
HX
68static 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
97defe1e
TG
102static void bucket_table_free(const struct bucket_table *tbl)
103{
da20420f
HX
104 if (tbl->nest)
105 nested_bucket_table_free(tbl);
106
64e0cd0d 107 free_bucket_spinlocks(tbl->locks);
97defe1e
TG
108 kvfree(tbl);
109}
110
9d901bc0
HX
111static void bucket_table_free_rcu(struct rcu_head *head)
112{
113 bucket_table_free(container_of(head, struct bucket_table, rcu));
114}
115
da20420f
HX
116static union nested_table *nested_table_alloc(struct rhashtable *ht,
117 union nested_table __rcu **prev,
5af68ef7 118 bool leaf)
da20420f
HX
119{
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
5af68ef7
N
129 if (ntbl && leaf) {
130 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
9b4f64a2 131 INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
da20420f
HX
132 }
133
7a41c294
N
134 if (cmpxchg(prev, NULL, ntbl) == NULL)
135 return ntbl;
136 /* Raced with another thread. */
137 kfree(ntbl);
138 return rcu_dereference(*prev);
da20420f
HX
139}
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,
5af68ef7 159 false)) {
da20420f
HX
160 kfree(tbl);
161 return NULL;
162 }
163
164 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
165
166 return tbl;
167}
168
97defe1e 169static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
b9ecfdaa
HX
170 size_t nbuckets,
171 gfp_t gfp)
7e1e7763 172{
eb6d1abf 173 struct bucket_table *tbl = NULL;
64e0cd0d 174 size_t size, max_locks;
f89bd6f8 175 int i;
7e1e7763
TG
176
177 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
93f976b5 178 tbl = kvzalloc(size, gfp);
da20420f
HX
179
180 size = nbuckets;
181
2d22ecf6 182 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
da20420f
HX
183 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
184 nbuckets = 0;
185 }
2d22ecf6 186
7e1e7763
TG
187 if (tbl == NULL)
188 return NULL;
189
da20420f 190 tbl->size = size;
7e1e7763 191
64e0cd0d
TH
192 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) {
97defe1e
TG
198 bucket_table_free(tbl);
199 return NULL;
200 }
7e1e7763 201
4feb7c7a 202 rcu_head_init(&tbl->rcu);
eddee5ba
HX
203 INIT_LIST_HEAD(&tbl->walkers);
204
d48ad080 205 tbl->hash_rnd = get_random_u32();
5269b53d 206
f89bd6f8 207 for (i = 0; i < nbuckets; i++)
9b4f64a2 208 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
f89bd6f8 209
97defe1e 210 return tbl;
7e1e7763
TG
211}
212
b824478b
HX
213static 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
299e5c32 226static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
a5ec68e3 227{
aa34a6cb 228 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
c0690016 229 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
da20420f
HX
230 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
231 int err = -EAGAIN;
aa34a6cb
HX
232 struct rhash_head *head, *next, *entry;
233 spinlock_t *new_bucket_lock;
299e5c32 234 unsigned int new_hash;
aa34a6cb 235
da20420f
HX
236 if (new_tbl->nest)
237 goto out;
238
239 err = -ENOENT;
240
aa34a6cb
HX
241 rht_for_each(entry, old_tbl, old_hash) {
242 err = 0;
243 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
244
245 if (rht_is_a_nulls(next))
246 break;
a5ec68e3 247
aa34a6cb
HX
248 pprev = &entry->next;
249 }
a5ec68e3 250
aa34a6cb
HX
251 if (err)
252 goto out;
97defe1e 253
aa34a6cb 254 new_hash = head_hashfn(ht, new_tbl, entry);
7e1e7763 255
02fd97c3 256 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
7e1e7763 257
8f2484bd 258 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
aa34a6cb
HX
259 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
260 new_tbl, new_hash);
97defe1e 261
7def0f95 262 RCU_INIT_POINTER(entry->next, head);
a5ec68e3 263
aa34a6cb
HX
264 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
265 spin_unlock(new_bucket_lock);
97defe1e 266
aa34a6cb 267 rcu_assign_pointer(*pprev, next);
7e1e7763 268
aa34a6cb
HX
269out:
270 return err;
271}
97defe1e 272
da20420f 273static int rhashtable_rehash_chain(struct rhashtable *ht,
299e5c32 274 unsigned int old_hash)
aa34a6cb
HX
275{
276 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
277 spinlock_t *old_bucket_lock;
da20420f 278 int err;
aa34a6cb 279
02fd97c3 280 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
a5ec68e3 281
aa34a6cb 282 spin_lock_bh(old_bucket_lock);
da20420f 283 while (!(err = rhashtable_rehash_one(ht, old_hash)))
aa34a6cb 284 ;
da20420f 285
4feb7c7a 286 if (err == -ENOENT)
da20420f 287 err = 0;
4feb7c7a 288
aa34a6cb 289 spin_unlock_bh(old_bucket_lock);
da20420f
HX
290
291 return err;
97defe1e
TG
292}
293
b824478b
HX
294static int rhashtable_rehash_attach(struct rhashtable *ht,
295 struct bucket_table *old_tbl,
296 struct bucket_table *new_tbl)
97defe1e 297{
aa34a6cb
HX
298 /* Make insertions go into the new, empty table right away. Deletions
299 * and lookups will be attempted in both tables until we synchronize.
0ad66449
N
300 * As cmpxchg() provides strong barriers, we do not need
301 * rcu_assign_pointer().
aa34a6cb 302 */
aa34a6cb 303
0ad66449
N
304 if (cmpxchg(&old_tbl->future_tbl, NULL, new_tbl) != NULL)
305 return -EEXIST;
b824478b
HX
306
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;
299e5c32 315 unsigned int old_hash;
da20420f 316 int err;
b824478b
HX
317
318 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
319 if (!new_tbl)
320 return 0;
321
da20420f
HX
322 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;
ae6da1f5 326 cond_resched();
da20420f 327 }
aa34a6cb
HX
328
329 /* Publish the new table pointer. */
330 rcu_assign_pointer(ht->tbl, new_tbl);
331
ba7c95ea 332 spin_lock(&ht->lock);
eddee5ba
HX
333 list_for_each_entry(walker, &old_tbl->walkers, list)
334 walker->tbl = NULL;
335
aa34a6cb
HX
336 /* Wait for readers. All new readers will see the new
337 * table, and thus no references to the old table will
338 * remain.
4feb7c7a
N
339 * 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.
aa34a6cb 342 */
9d901bc0 343 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
4feb7c7a 344 spin_unlock(&ht->lock);
b824478b
HX
345
346 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
7e1e7763
TG
347}
348
da20420f
HX
349static int rhashtable_rehash_alloc(struct rhashtable *ht,
350 struct bucket_table *old_tbl,
351 unsigned int size)
7e1e7763 352{
da20420f 353 struct bucket_table *new_tbl;
b824478b 354 int err;
7e1e7763
TG
355
356 ASSERT_RHT_MUTEX(ht);
357
da20420f 358 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
7e1e7763
TG
359 if (new_tbl == NULL)
360 return -ENOMEM;
361
b824478b
HX
362 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
363 if (err)
364 bucket_table_free(new_tbl);
365
366 return err;
7e1e7763 367}
7e1e7763
TG
368
369/**
370 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
371 * @ht: the hash table to shrink
7e1e7763 372 *
18093d1c
HX
373 * This function shrinks the hash table to fit, i.e., the smallest
374 * size would not cause it to expand right away automatically.
7e1e7763 375 *
97defe1e
TG
376 * The caller must ensure that no concurrent resizing occurs by holding
377 * ht->mutex.
378 *
7e1e7763
TG
379 * 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.
97defe1e
TG
381 *
382 * It is valid to have concurrent insertions and deletions protected by per
383 * bucket locks or concurrent RCU protected lookups and traversals.
7e1e7763 384 */
b824478b 385static int rhashtable_shrink(struct rhashtable *ht)
7e1e7763 386{
da20420f 387 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
12311959
VN
388 unsigned int nelems = atomic_read(&ht->nelems);
389 unsigned int size = 0;
7e1e7763 390
12311959
VN
391 if (nelems)
392 size = roundup_pow_of_two(nelems * 3 / 2);
18093d1c
HX
393 if (size < ht->p.min_size)
394 size = ht->p.min_size;
395
396 if (old_tbl->size <= size)
397 return 0;
398
b824478b
HX
399 if (rht_dereference(old_tbl->future_tbl, ht))
400 return -EEXIST;
401
da20420f 402 return rhashtable_rehash_alloc(ht, old_tbl, size);
7e1e7763 403}
7e1e7763 404
97defe1e
TG
405static void rht_deferred_worker(struct work_struct *work)
406{
407 struct rhashtable *ht;
408 struct bucket_table *tbl;
b824478b 409 int err = 0;
97defe1e 410
57699a40 411 ht = container_of(work, struct rhashtable, run_work);
97defe1e 412 mutex_lock(&ht->mutex);
28134a53 413
97defe1e 414 tbl = rht_dereference(ht->tbl, ht);
b824478b 415 tbl = rhashtable_last_table(ht, tbl);
97defe1e 416
a5b6846f 417 if (rht_grow_above_75(ht, tbl))
da20420f 418 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
b5e2c150 419 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
da20420f
HX
420 err = rhashtable_shrink(ht);
421 else if (tbl->nest)
422 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
b824478b 423
408f13ef
HX
424 if (!err || err == -EEXIST) {
425 int nerr;
426
427 nerr = rhashtable_rehash_table(ht);
428 err = err ?: nerr;
429 }
b824478b 430
97defe1e 431 mutex_unlock(&ht->mutex);
b824478b
HX
432
433 if (err)
434 schedule_work(&ht->run_work);
97defe1e
TG
435}
436
ca26893f
HX
437static int rhashtable_insert_rehash(struct rhashtable *ht,
438 struct bucket_table *tbl)
ccd57b1b
HX
439{
440 struct bucket_table *old_tbl;
441 struct bucket_table *new_tbl;
ccd57b1b
HX
442 unsigned int size;
443 int err;
444
445 old_tbl = rht_dereference_rcu(ht->tbl, ht);
ccd57b1b
HX
446
447 size = tbl->size;
448
3cf92222
HX
449 err = -EBUSY;
450
ccd57b1b
HX
451 if (rht_grow_above_75(ht, tbl))
452 size *= 2;
a87b9ebf
TG
453 /* Do not schedule more than one rehash */
454 else if (old_tbl != tbl)
3cf92222
HX
455 goto fail;
456
457 err = -ENOMEM;
ccd57b1b 458
93f976b5 459 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
3cf92222
HX
460 if (new_tbl == NULL)
461 goto fail;
ccd57b1b
HX
462
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;
3cf92222
HX
472
473fail:
474 /* Do not fail the insert if someone else did a rehash. */
c0690016 475 if (likely(rcu_access_pointer(tbl->future_tbl)))
3cf92222
HX
476 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;
ccd57b1b 483}
ccd57b1b 484
ca26893f
HX
485static void *rhashtable_lookup_one(struct rhashtable *ht,
486 struct bucket_table *tbl, unsigned int hash,
487 const void *key, struct rhash_head *obj)
02fd97c3 488{
ca26893f
HX
489 struct rhashtable_compare_arg arg = {
490 .ht = ht,
491 .key = key,
492 };
493 struct rhash_head __rcu **pprev;
02fd97c3 494 struct rhash_head *head;
ca26893f 495 int elasticity;
02fd97c3 496
5f8ddeab 497 elasticity = RHT_ELASTICITY;
da20420f 498 pprev = rht_bucket_var(tbl, hash);
f7ad68bf 499 rht_for_each_from(head, *pprev, tbl, hash) {
ca26893f
HX
500 struct rhlist_head *list;
501 struct rhlist_head *plist;
502
503 elasticity--;
504 if (!key ||
505 (ht->p.obj_cmpfn ?
506 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
d3dcf8eb
PB
507 rhashtable_compare(&arg, rht_obj(ht, head)))) {
508 pprev = &head->next;
ca26893f 509 continue;
d3dcf8eb 510 }
ca26893f
HX
511
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;
5ca8cc5b 524 }
02fd97c3 525
ca26893f
HX
526 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{
da20420f 538 struct rhash_head __rcu **pprev;
ca26893f
HX
539 struct bucket_table *new_tbl;
540 struct rhash_head *head;
541
542 if (!IS_ERR_OR_NULL(data))
543 return ERR_PTR(-EEXIST);
07ee0722 544
ca26893f
HX
545 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
546 return ERR_CAST(data);
ccd57b1b 547
c0690016 548 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
ca26893f
HX
549 if (new_tbl)
550 return new_tbl;
551
552 if (PTR_ERR(data) != -ENOENT)
553 return ERR_CAST(data);
554
555 if (unlikely(rht_grow_above_max(ht, tbl)))
556 return ERR_PTR(-E2BIG);
557
558 if (unlikely(rht_grow_above_100(ht, tbl)))
559 return ERR_PTR(-EAGAIN);
02fd97c3 560
da20420f
HX
561 pprev = rht_bucket_insert(ht, tbl, hash);
562 if (!pprev)
563 return ERR_PTR(-ENOMEM);
564
565 head = rht_dereference_bucket(*pprev, tbl, hash);
02fd97c3
HX
566
567 RCU_INIT_POINTER(obj->next, head);
ca26893f
HX
568 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 }
02fd97c3 574
da20420f 575 rcu_assign_pointer(*pprev, obj);
02fd97c3
HX
576
577 atomic_inc(&ht->nelems);
ca26893f
HX
578 if (rht_grow_above_75(ht, tbl))
579 schedule_work(&ht->run_work);
02fd97c3 580
ca26893f
HX
581 return NULL;
582}
02fd97c3 583
ca26893f
HX
584static 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;
ca26893f
HX
590 void *data;
591
4feb7c7a 592 new_tbl = rcu_dereference(ht->tbl);
ca26893f 593
4feb7c7a 594 do {
ca26893f
HX
595 tbl = new_tbl;
596 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
4feb7c7a 597 spin_lock_bh(rht_bucket_lock(tbl, hash));
ca26893f
HX
598
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
4feb7c7a
N
604 spin_unlock_bh(rht_bucket_lock(tbl, hash));
605 } while (!IS_ERR_OR_NULL(new_tbl));
ca26893f
HX
606
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;
02fd97c3
HX
626}
627EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
628
f2dba9c6 629/**
246779dd 630 * rhashtable_walk_enter - Initialise an iterator
f2dba9c6
HX
631 * @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 *
82266e98
N
644 * This function may be called from any process context, including
645 * non-preemptable context, but cannot be called from softirq or
646 * hardirq context.
f2dba9c6 647 *
246779dd 648 * You must call rhashtable_walk_exit after this function returns.
f2dba9c6 649 */
246779dd 650void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
f2dba9c6
HX
651{
652 iter->ht = ht;
653 iter->p = NULL;
654 iter->slot = 0;
655 iter->skip = 0;
2db54b47 656 iter->end_of_table = 0;
f2dba9c6 657
c6ff5268 658 spin_lock(&ht->lock);
246779dd 659 iter->walker.tbl =
179ccc0a 660 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
246779dd 661 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
c6ff5268 662 spin_unlock(&ht->lock);
f2dba9c6 663}
246779dd 664EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
f2dba9c6
HX
665
666/**
667 * rhashtable_walk_exit - Free an iterator
668 * @iter: Hash table Iterator
669 *
6c4128f6 670 * This function frees resources allocated by rhashtable_walk_enter.
f2dba9c6
HX
671 */
672void rhashtable_walk_exit(struct rhashtable_iter *iter)
673{
c6ff5268 674 spin_lock(&iter->ht->lock);
246779dd
HX
675 if (iter->walker.tbl)
676 list_del(&iter->walker.list);
c6ff5268 677 spin_unlock(&iter->ht->lock);
f2dba9c6
HX
678}
679EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
680
681/**
97a6ec4a 682 * rhashtable_walk_start_check - Start a hash table walk
f2dba9c6
HX
683 * @iter: Hash table iterator
684 *
0647169c
AG
685 * 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.
f2dba9c6
HX
688 *
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.
97a6ec4a
TH
694 *
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.
f2dba9c6 698 */
97a6ec4a 699int rhashtable_walk_start_check(struct rhashtable_iter *iter)
db4374f4 700 __acquires(RCU)
f2dba9c6 701{
eddee5ba 702 struct rhashtable *ht = iter->ht;
5d240a89 703 bool rhlist = ht->rhlist;
eddee5ba 704
c6ff5268 705 rcu_read_lock();
eddee5ba 706
c6ff5268 707 spin_lock(&ht->lock);
246779dd
HX
708 if (iter->walker.tbl)
709 list_del(&iter->walker.list);
c6ff5268 710 spin_unlock(&ht->lock);
eddee5ba 711
5d240a89
N
712 if (iter->end_of_table)
713 return 0;
714 if (!iter->walker.tbl) {
246779dd 715 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
b41cc04b
N
716 iter->slot = 0;
717 iter->skip = 0;
f2dba9c6
HX
718 return -EAGAIN;
719 }
720
5d240a89
N
721 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;
c643ecf3 750 iter->skip = skip;
5d240a89
N
751 goto found;
752 }
753 }
754 }
755 iter->p = NULL;
756 }
757found:
f2dba9c6
HX
758 return 0;
759}
97a6ec4a 760EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
f2dba9c6
HX
761
762/**
2db54b47
TH
763 * __rhashtable_walk_find_next - Find the next element in a table (or the first
764 * one in case of a new walk).
765 *
f2dba9c6
HX
766 * @iter: Hash table iterator
767 *
2db54b47 768 * Returns the found object or NULL when the end of the table is reached.
f2dba9c6 769 *
2db54b47 770 * Returns -EAGAIN if resize event occurred.
f2dba9c6 771 */
2db54b47 772static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
f2dba9c6 773{
246779dd 774 struct bucket_table *tbl = iter->walker.tbl;
ca26893f 775 struct rhlist_head *list = iter->list;
f2dba9c6
HX
776 struct rhashtable *ht = iter->ht;
777 struct rhash_head *p = iter->p;
ca26893f 778 bool rhlist = ht->rhlist;
f2dba9c6 779
2db54b47
TH
780 if (!tbl)
781 return NULL;
f2dba9c6
HX
782
783 for (; iter->slot < tbl->size; iter->slot++) {
784 int skip = iter->skip;
785
786 rht_for_each_rcu(p, tbl, iter->slot) {
ca26893f
HX
787 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 }
f2dba9c6
HX
799 if (!skip)
800 break;
801 skip--;
802 }
803
804next:
805 if (!rht_is_a_nulls(p)) {
806 iter->skip++;
807 iter->p = p;
ca26893f
HX
808 iter->list = list;
809 return rht_obj(ht, rhlist ? &list->rhead : p);
f2dba9c6
HX
810 }
811
812 iter->skip = 0;
813 }
814
142b942a
PS
815 iter->p = NULL;
816
d88252f9
HX
817 /* Ensure we see any new tables. */
818 smp_rmb();
819
246779dd
HX
820 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
821 if (iter->walker.tbl) {
f2dba9c6
HX
822 iter->slot = 0;
823 iter->skip = 0;
f2dba9c6 824 return ERR_PTR(-EAGAIN);
2db54b47
TH
825 } else {
826 iter->end_of_table = true;
f2dba9c6
HX
827 }
828
c936a79f 829 return NULL;
f2dba9c6 830}
2db54b47
TH
831
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}
f2dba9c6
HX
872EXPORT_SYMBOL_GPL(rhashtable_walk_next);
873
2db54b47
TH
874/**
875 * 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
f2dba9c6
HX
908/**
909 * rhashtable_walk_stop - Finish a hash table walk
910 * @iter: Hash table iterator
911 *
0647169c
AG
912 * Finish a hash table walk. Does not reset the iterator to the start of the
913 * hash table.
f2dba9c6
HX
914 */
915void rhashtable_walk_stop(struct rhashtable_iter *iter)
db4374f4 916 __releases(RCU)
f2dba9c6 917{
eddee5ba 918 struct rhashtable *ht;
246779dd 919 struct bucket_table *tbl = iter->walker.tbl;
eddee5ba 920
eddee5ba 921 if (!tbl)
963ecbd4 922 goto out;
eddee5ba
HX
923
924 ht = iter->ht;
925
ba7c95ea 926 spin_lock(&ht->lock);
4feb7c7a
N
927 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
928 /* This bucket table is being freed, don't re-link it. */
246779dd 929 iter->walker.tbl = NULL;
4feb7c7a
N
930 else
931 list_add(&iter->walker.list, &tbl->walkers);
ba7c95ea 932 spin_unlock(&ht->lock);
eddee5ba 933
963ecbd4
HX
934out:
935 rcu_read_unlock();
f2dba9c6
HX
936}
937EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
938
488fb86e 939static size_t rounded_hashtable_size(const struct rhashtable_params *params)
7e1e7763 940{
107d01f5
DB
941 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;
7e1e7763
TG
951}
952
31ccde2d
HX
953static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
954{
955 return jhash2(key, length, seed);
956}
957
7e1e7763
TG
958/**
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),
87545899 978 * .hashfn = jhash,
7e1e7763
TG
979 * };
980 *
981 * Configuration Example 2: Variable length keys
982 * struct test_obj {
983 * [...]
984 * struct rhash_head node;
985 * };
986 *
49f7b33e 987 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
7e1e7763
TG
988 * {
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),
87545899 996 * .hashfn = jhash,
7e1e7763 997 * .obj_hashfn = my_hash_fn,
7e1e7763
TG
998 * };
999 */
488fb86e
HX
1000int rhashtable_init(struct rhashtable *ht,
1001 const struct rhashtable_params *params)
7e1e7763
TG
1002{
1003 struct bucket_table *tbl;
1004 size_t size;
1005
31ccde2d 1006 if ((!params->key_len && !params->obj_hashfn) ||
02fd97c3 1007 (params->obj_hashfn && !params->obj_cmpfn))
7e1e7763
TG
1008 return -EINVAL;
1009
97defe1e
TG
1010 memset(ht, 0, sizeof(*ht));
1011 mutex_init(&ht->mutex);
ba7c95ea 1012 spin_lock_init(&ht->lock);
97defe1e
TG
1013 memcpy(&ht->p, params, sizeof(*params));
1014
a998f712
TG
1015 if (params->min_size)
1016 ht->p.min_size = roundup_pow_of_two(params->min_size);
1017
6d684e54
HX
1018 /* Cap total entries at 2^31 to avoid nelems overflow. */
1019 ht->max_elems = 1u << 31;
2d2ab658
HX
1020
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 }
6d684e54 1026
48e75b43 1027 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
a998f712 1028
107d01f5 1029 size = rounded_hashtable_size(&ht->p);
3a324606 1030
97defe1e
TG
1031 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
31ccde2d
HX
1036 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
2d22ecf6
DB
1046 /*
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 */
b9ecfdaa 1051 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
2d22ecf6
DB
1052 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 }
7e1e7763 1056
545a148e 1057 atomic_set(&ht->nelems, 0);
a5b6846f 1058
7e1e7763
TG
1059 RCU_INIT_POINTER(ht->tbl, tbl);
1060
4c4b52d9 1061 INIT_WORK(&ht->run_work, rht_deferred_worker);
97defe1e 1062
7e1e7763
TG
1063 return 0;
1064}
1065EXPORT_SYMBOL_GPL(rhashtable_init);
1066
ca26893f
HX
1067/**
1068 * 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
ca26893f
HX
1080 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
7e1e7763 1105/**
6b6f302c 1106 * rhashtable_free_and_destroy - free elements and destroy hash table
7e1e7763 1107 * @ht: the hash table to destroy
6b6f302c
TG
1108 * @free_fn: callback to release resources of element
1109 * @arg: pointer passed to free_fn
7e1e7763 1110 *
6b6f302c
TG
1111 * 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.
7e1e7763 1119 */
6b6f302c
TG
1120void rhashtable_free_and_destroy(struct rhashtable *ht,
1121 void (*free_fn)(void *ptr, void *arg),
1122 void *arg)
7e1e7763 1123{
0026129c 1124 struct bucket_table *tbl, *next_tbl;
6b6f302c 1125 unsigned int i;
97defe1e 1126
4c4b52d9 1127 cancel_work_sync(&ht->run_work);
97defe1e 1128
57699a40 1129 mutex_lock(&ht->mutex);
6b6f302c 1130 tbl = rht_dereference(ht->tbl, ht);
0026129c 1131restart:
6b6f302c
TG
1132 if (free_fn) {
1133 for (i = 0; i < tbl->size; i++) {
1134 struct rhash_head *pos, *next;
1135
ae6da1f5 1136 cond_resched();
da20420f 1137 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
6b6f302c
TG
1138 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)
ca26893f 1144 rhashtable_free_one(ht, pos, free_fn, arg);
6b6f302c
TG
1145 }
1146 }
1147
0026129c 1148 next_tbl = rht_dereference(tbl->future_tbl, ht);
6b6f302c 1149 bucket_table_free(tbl);
0026129c
TY
1150 if (next_tbl) {
1151 tbl = next_tbl;
1152 goto restart;
1153 }
97defe1e 1154 mutex_unlock(&ht->mutex);
7e1e7763 1155}
6b6f302c
TG
1156EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1157
1158void rhashtable_destroy(struct rhashtable *ht)
1159{
1160 return rhashtable_free_and_destroy(ht, NULL, NULL);
1161}
7e1e7763 1162EXPORT_SYMBOL_GPL(rhashtable_destroy);
da20420f
HX
1163
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 *));
82208d0d 1168 static struct rhash_head __rcu *rhnull;
da20420f
HX
1169 unsigned int index = hash & ((1 << tbl->nest) - 1);
1170 unsigned int size = tbl->size >> tbl->nest;
1171 unsigned int subhash = hash;
1172 union nested_table *ntbl;
1173
1174 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
c4d2603d 1175 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
da20420f
HX
1176 subhash >>= tbl->nest;
1177
1178 while (ntbl && size > (1 << shift)) {
1179 index = subhash & ((1 << shift) - 1);
c4d2603d
HX
1180 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1181 tbl, hash);
da20420f
HX
1182 size >>= shift;
1183 subhash >>= shift;
1184 }
1185
82208d0d
N
1186 if (!ntbl) {
1187 if (!rhnull)
1188 INIT_RHT_NULLS_HEAD(rhnull);
da20420f 1189 return &rhnull;
82208d0d 1190 }
da20420f
HX
1191
1192 return &ntbl[subhash].bucket;
1193
1194}
1195EXPORT_SYMBOL_GPL(rht_bucket_nested);
1196
1197struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1198 struct bucket_table *tbl,
1199 unsigned int hash)
1200{
1201 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1202 unsigned int index = hash & ((1 << tbl->nest) - 1);
1203 unsigned int size = tbl->size >> tbl->nest;
1204 union nested_table *ntbl;
da20420f
HX
1205
1206 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1207 hash >>= tbl->nest;
da20420f 1208 ntbl = nested_table_alloc(ht, &ntbl[index].table,
5af68ef7 1209 size <= (1 << shift));
da20420f
HX
1210
1211 while (ntbl && size > (1 << shift)) {
1212 index = hash & ((1 << shift) - 1);
1213 size >>= shift;
1214 hash >>= shift;
da20420f 1215 ntbl = nested_table_alloc(ht, &ntbl[index].table,
5af68ef7 1216 size <= (1 << shift));
da20420f
HX
1217 }
1218
1219 if (!ntbl)
1220 return NULL;
1221
1222 return &ntbl[hash].bucket;
1223
1224}
1225EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);