rhashtable: simplify INIT_RHT_NULLS_HEAD()
[linux-2.6-block.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>
0eb71a9d 31#include <linux/rhashtable.h>
7e1e7763
TG
32
33#define HASH_DEFAULT_SIZE 64UL
c2e213cf 34#define HASH_MIN_SIZE 4U
4cf0b354 35#define BUCKET_LOCKS_PER_CPU 32UL
97defe1e 36
da20420f
HX
37union nested_table {
38 union nested_table __rcu *table;
39 struct rhash_head __rcu *bucket;
40};
41
988dfbd7 42static u32 head_hashfn(struct rhashtable *ht,
8d24c0b4
TG
43 const struct bucket_table *tbl,
44 const struct rhash_head *he)
7e1e7763 45{
02fd97c3 46 return rht_head_hashfn(ht, tbl, he, ht->p);
7e1e7763
TG
47}
48
a03eaec0 49#ifdef CONFIG_PROVE_LOCKING
a03eaec0 50#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
a03eaec0
TG
51
52int lockdep_rht_mutex_is_held(struct rhashtable *ht)
53{
54 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
55}
56EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
57
58int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
59{
02fd97c3 60 spinlock_t *lock = rht_bucket_lock(tbl, hash);
a03eaec0
TG
61
62 return (debug_locks) ? lockdep_is_held(lock) : 1;
63}
64EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
65#else
66#define ASSERT_RHT_MUTEX(HT)
a03eaec0
TG
67#endif
68
da20420f
HX
69static void nested_table_free(union nested_table *ntbl, unsigned int size)
70{
71 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
72 const unsigned int len = 1 << shift;
73 unsigned int i;
74
75 ntbl = rcu_dereference_raw(ntbl->table);
76 if (!ntbl)
77 return;
78
79 if (size > len) {
80 size >>= shift;
81 for (i = 0; i < len; i++)
82 nested_table_free(ntbl + i, size);
83 }
84
85 kfree(ntbl);
86}
87
88static void nested_bucket_table_free(const struct bucket_table *tbl)
89{
90 unsigned int size = tbl->size >> tbl->nest;
91 unsigned int len = 1 << tbl->nest;
92 union nested_table *ntbl;
93 unsigned int i;
94
95 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
96
97 for (i = 0; i < len; i++)
98 nested_table_free(ntbl + i, size);
99
100 kfree(ntbl);
101}
102
97defe1e
TG
103static void bucket_table_free(const struct bucket_table *tbl)
104{
da20420f
HX
105 if (tbl->nest)
106 nested_bucket_table_free(tbl);
107
64e0cd0d 108 free_bucket_spinlocks(tbl->locks);
97defe1e
TG
109 kvfree(tbl);
110}
111
9d901bc0
HX
112static void bucket_table_free_rcu(struct rcu_head *head)
113{
114 bucket_table_free(container_of(head, struct bucket_table, rcu));
115}
116
da20420f
HX
117static union nested_table *nested_table_alloc(struct rhashtable *ht,
118 union nested_table __rcu **prev,
9b4f64a2 119 unsigned int shifted)
da20420f
HX
120{
121 union nested_table *ntbl;
122 int i;
123
124 ntbl = rcu_dereference(*prev);
125 if (ntbl)
126 return ntbl;
127
128 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
129
130 if (ntbl && shifted) {
131 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0].bucket); i++)
9b4f64a2 132 INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
da20420f
HX
133 }
134
135 rcu_assign_pointer(*prev, ntbl);
136
137 return ntbl;
138}
139
140static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
141 size_t nbuckets,
142 gfp_t gfp)
143{
144 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
145 struct bucket_table *tbl;
146 size_t size;
147
148 if (nbuckets < (1 << (shift + 1)))
149 return NULL;
150
151 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
152
153 tbl = kzalloc(size, gfp);
154 if (!tbl)
155 return NULL;
156
157 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
9b4f64a2 158 0)) {
da20420f
HX
159 kfree(tbl);
160 return NULL;
161 }
162
163 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
164
165 return tbl;
166}
167
97defe1e 168static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
b9ecfdaa
HX
169 size_t nbuckets,
170 gfp_t gfp)
7e1e7763 171{
eb6d1abf 172 struct bucket_table *tbl = NULL;
64e0cd0d 173 size_t size, max_locks;
f89bd6f8 174 int i;
7e1e7763
TG
175
176 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
12e8fd6f 177 if (gfp != GFP_KERNEL)
b9ecfdaa 178 tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
12e8fd6f
MH
179 else
180 tbl = kvzalloc(size, gfp);
da20420f
HX
181
182 size = nbuckets;
183
184 if (tbl == NULL && gfp != GFP_KERNEL) {
185 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
186 nbuckets = 0;
187 }
7e1e7763
TG
188 if (tbl == NULL)
189 return NULL;
190
da20420f 191 tbl->size = size;
7e1e7763 192
64e0cd0d
TH
193 max_locks = size >> 1;
194 if (tbl->nest)
195 max_locks = min_t(size_t, max_locks, 1U << tbl->nest);
196
197 if (alloc_bucket_spinlocks(&tbl->locks, &tbl->locks_mask, max_locks,
198 ht->p.locks_mul, gfp) < 0) {
97defe1e
TG
199 bucket_table_free(tbl);
200 return NULL;
201 }
7e1e7763 202
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);
b824478b
HX
229 struct bucket_table *new_tbl = rhashtable_last_table(ht,
230 rht_dereference_rcu(old_tbl->future_tbl, ht));
da20420f
HX
231 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
232 int err = -EAGAIN;
aa34a6cb
HX
233 struct rhash_head *head, *next, *entry;
234 spinlock_t *new_bucket_lock;
299e5c32 235 unsigned int new_hash;
aa34a6cb 236
da20420f
HX
237 if (new_tbl->nest)
238 goto out;
239
240 err = -ENOENT;
241
aa34a6cb
HX
242 rht_for_each(entry, old_tbl, old_hash) {
243 err = 0;
244 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
245
246 if (rht_is_a_nulls(next))
247 break;
a5ec68e3 248
aa34a6cb
HX
249 pprev = &entry->next;
250 }
a5ec68e3 251
aa34a6cb
HX
252 if (err)
253 goto out;
97defe1e 254
aa34a6cb 255 new_hash = head_hashfn(ht, new_tbl, entry);
7e1e7763 256
02fd97c3 257 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
7e1e7763 258
8f2484bd 259 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
aa34a6cb
HX
260 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
261 new_tbl, new_hash);
97defe1e 262
7def0f95 263 RCU_INIT_POINTER(entry->next, head);
a5ec68e3 264
aa34a6cb
HX
265 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
266 spin_unlock(new_bucket_lock);
97defe1e 267
aa34a6cb 268 rcu_assign_pointer(*pprev, next);
7e1e7763 269
aa34a6cb
HX
270out:
271 return err;
272}
97defe1e 273
da20420f 274static int rhashtable_rehash_chain(struct rhashtable *ht,
299e5c32 275 unsigned int old_hash)
aa34a6cb
HX
276{
277 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
278 spinlock_t *old_bucket_lock;
da20420f 279 int err;
aa34a6cb 280
02fd97c3 281 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
a5ec68e3 282
aa34a6cb 283 spin_lock_bh(old_bucket_lock);
da20420f 284 while (!(err = rhashtable_rehash_one(ht, old_hash)))
aa34a6cb 285 ;
da20420f
HX
286
287 if (err == -ENOENT) {
288 old_tbl->rehash++;
289 err = 0;
290 }
aa34a6cb 291 spin_unlock_bh(old_bucket_lock);
da20420f
HX
292
293 return err;
97defe1e
TG
294}
295
b824478b
HX
296static int rhashtable_rehash_attach(struct rhashtable *ht,
297 struct bucket_table *old_tbl,
298 struct bucket_table *new_tbl)
97defe1e 299{
b824478b
HX
300 /* Protect future_tbl using the first bucket lock. */
301 spin_lock_bh(old_tbl->locks);
302
303 /* Did somebody beat us to it? */
304 if (rcu_access_pointer(old_tbl->future_tbl)) {
305 spin_unlock_bh(old_tbl->locks);
306 return -EEXIST;
307 }
7cd10db8 308
aa34a6cb
HX
309 /* Make insertions go into the new, empty table right away. Deletions
310 * and lookups will be attempted in both tables until we synchronize.
aa34a6cb 311 */
c4db8848 312 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
aa34a6cb 313
b824478b
HX
314 spin_unlock_bh(old_tbl->locks);
315
316 return 0;
317}
318
319static int rhashtable_rehash_table(struct rhashtable *ht)
320{
321 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
322 struct bucket_table *new_tbl;
323 struct rhashtable_walker *walker;
299e5c32 324 unsigned int old_hash;
da20420f 325 int err;
b824478b
HX
326
327 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
328 if (!new_tbl)
329 return 0;
330
da20420f
HX
331 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
332 err = rhashtable_rehash_chain(ht, old_hash);
333 if (err)
334 return err;
ae6da1f5 335 cond_resched();
da20420f 336 }
aa34a6cb
HX
337
338 /* Publish the new table pointer. */
339 rcu_assign_pointer(ht->tbl, new_tbl);
340
ba7c95ea 341 spin_lock(&ht->lock);
eddee5ba
HX
342 list_for_each_entry(walker, &old_tbl->walkers, list)
343 walker->tbl = NULL;
ba7c95ea 344 spin_unlock(&ht->lock);
eddee5ba 345
aa34a6cb
HX
346 /* Wait for readers. All new readers will see the new
347 * table, and thus no references to the old table will
348 * remain.
349 */
9d901bc0 350 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
b824478b
HX
351
352 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
7e1e7763
TG
353}
354
da20420f
HX
355static int rhashtable_rehash_alloc(struct rhashtable *ht,
356 struct bucket_table *old_tbl,
357 unsigned int size)
7e1e7763 358{
da20420f 359 struct bucket_table *new_tbl;
b824478b 360 int err;
7e1e7763
TG
361
362 ASSERT_RHT_MUTEX(ht);
363
da20420f 364 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
7e1e7763
TG
365 if (new_tbl == NULL)
366 return -ENOMEM;
367
b824478b
HX
368 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
369 if (err)
370 bucket_table_free(new_tbl);
371
372 return err;
7e1e7763 373}
7e1e7763
TG
374
375/**
376 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
377 * @ht: the hash table to shrink
7e1e7763 378 *
18093d1c
HX
379 * This function shrinks the hash table to fit, i.e., the smallest
380 * size would not cause it to expand right away automatically.
7e1e7763 381 *
97defe1e
TG
382 * The caller must ensure that no concurrent resizing occurs by holding
383 * ht->mutex.
384 *
7e1e7763
TG
385 * The caller must ensure that no concurrent table mutations take place.
386 * It is however valid to have concurrent lookups if they are RCU protected.
97defe1e
TG
387 *
388 * It is valid to have concurrent insertions and deletions protected by per
389 * bucket locks or concurrent RCU protected lookups and traversals.
7e1e7763 390 */
b824478b 391static int rhashtable_shrink(struct rhashtable *ht)
7e1e7763 392{
da20420f 393 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
12311959
VN
394 unsigned int nelems = atomic_read(&ht->nelems);
395 unsigned int size = 0;
7e1e7763 396
12311959
VN
397 if (nelems)
398 size = roundup_pow_of_two(nelems * 3 / 2);
18093d1c
HX
399 if (size < ht->p.min_size)
400 size = ht->p.min_size;
401
402 if (old_tbl->size <= size)
403 return 0;
404
b824478b
HX
405 if (rht_dereference(old_tbl->future_tbl, ht))
406 return -EEXIST;
407
da20420f 408 return rhashtable_rehash_alloc(ht, old_tbl, size);
7e1e7763 409}
7e1e7763 410
97defe1e
TG
411static void rht_deferred_worker(struct work_struct *work)
412{
413 struct rhashtable *ht;
414 struct bucket_table *tbl;
b824478b 415 int err = 0;
97defe1e 416
57699a40 417 ht = container_of(work, struct rhashtable, run_work);
97defe1e 418 mutex_lock(&ht->mutex);
28134a53 419
97defe1e 420 tbl = rht_dereference(ht->tbl, ht);
b824478b 421 tbl = rhashtable_last_table(ht, tbl);
97defe1e 422
a5b6846f 423 if (rht_grow_above_75(ht, tbl))
da20420f 424 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
b5e2c150 425 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
da20420f
HX
426 err = rhashtable_shrink(ht);
427 else if (tbl->nest)
428 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
b824478b 429
da20420f
HX
430 if (!err)
431 err = rhashtable_rehash_table(ht);
b824478b 432
97defe1e 433 mutex_unlock(&ht->mutex);
b824478b
HX
434
435 if (err)
436 schedule_work(&ht->run_work);
97defe1e
TG
437}
438
ca26893f
HX
439static int rhashtable_insert_rehash(struct rhashtable *ht,
440 struct bucket_table *tbl)
ccd57b1b
HX
441{
442 struct bucket_table *old_tbl;
443 struct bucket_table *new_tbl;
ccd57b1b
HX
444 unsigned int size;
445 int err;
446
447 old_tbl = rht_dereference_rcu(ht->tbl, ht);
ccd57b1b
HX
448
449 size = tbl->size;
450
3cf92222
HX
451 err = -EBUSY;
452
ccd57b1b
HX
453 if (rht_grow_above_75(ht, tbl))
454 size *= 2;
a87b9ebf
TG
455 /* Do not schedule more than one rehash */
456 else if (old_tbl != tbl)
3cf92222
HX
457 goto fail;
458
459 err = -ENOMEM;
ccd57b1b
HX
460
461 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
3cf92222
HX
462 if (new_tbl == NULL)
463 goto fail;
ccd57b1b
HX
464
465 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
466 if (err) {
467 bucket_table_free(new_tbl);
468 if (err == -EEXIST)
469 err = 0;
470 } else
471 schedule_work(&ht->run_work);
472
473 return err;
3cf92222
HX
474
475fail:
476 /* Do not fail the insert if someone else did a rehash. */
477 if (likely(rcu_dereference_raw(tbl->future_tbl)))
478 return 0;
479
480 /* Schedule async rehash to retry allocation in process context. */
481 if (err == -ENOMEM)
482 schedule_work(&ht->run_work);
483
484 return err;
ccd57b1b 485}
ccd57b1b 486
ca26893f
HX
487static void *rhashtable_lookup_one(struct rhashtable *ht,
488 struct bucket_table *tbl, unsigned int hash,
489 const void *key, struct rhash_head *obj)
02fd97c3 490{
ca26893f
HX
491 struct rhashtable_compare_arg arg = {
492 .ht = ht,
493 .key = key,
494 };
495 struct rhash_head __rcu **pprev;
02fd97c3 496 struct rhash_head *head;
ca26893f 497 int elasticity;
02fd97c3 498
5f8ddeab 499 elasticity = RHT_ELASTICITY;
da20420f
HX
500 pprev = rht_bucket_var(tbl, hash);
501 rht_for_each_continue(head, *pprev, tbl, hash) {
ca26893f
HX
502 struct rhlist_head *list;
503 struct rhlist_head *plist;
504
505 elasticity--;
506 if (!key ||
507 (ht->p.obj_cmpfn ?
508 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
d3dcf8eb
PB
509 rhashtable_compare(&arg, rht_obj(ht, head)))) {
510 pprev = &head->next;
ca26893f 511 continue;
d3dcf8eb 512 }
ca26893f
HX
513
514 if (!ht->rhlist)
515 return rht_obj(ht, head);
516
517 list = container_of(obj, struct rhlist_head, rhead);
518 plist = container_of(head, struct rhlist_head, rhead);
519
520 RCU_INIT_POINTER(list->next, plist);
521 head = rht_dereference_bucket(head->next, tbl, hash);
522 RCU_INIT_POINTER(list->rhead.next, head);
523 rcu_assign_pointer(*pprev, obj);
524
525 return NULL;
5ca8cc5b 526 }
02fd97c3 527
ca26893f
HX
528 if (elasticity <= 0)
529 return ERR_PTR(-EAGAIN);
530
531 return ERR_PTR(-ENOENT);
532}
533
534static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
535 struct bucket_table *tbl,
536 unsigned int hash,
537 struct rhash_head *obj,
538 void *data)
539{
da20420f 540 struct rhash_head __rcu **pprev;
ca26893f
HX
541 struct bucket_table *new_tbl;
542 struct rhash_head *head;
543
544 if (!IS_ERR_OR_NULL(data))
545 return ERR_PTR(-EEXIST);
07ee0722 546
ca26893f
HX
547 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
548 return ERR_CAST(data);
ccd57b1b 549
ca26893f
HX
550 new_tbl = rcu_dereference(tbl->future_tbl);
551 if (new_tbl)
552 return new_tbl;
553
554 if (PTR_ERR(data) != -ENOENT)
555 return ERR_CAST(data);
556
557 if (unlikely(rht_grow_above_max(ht, tbl)))
558 return ERR_PTR(-E2BIG);
559
560 if (unlikely(rht_grow_above_100(ht, tbl)))
561 return ERR_PTR(-EAGAIN);
02fd97c3 562
da20420f
HX
563 pprev = rht_bucket_insert(ht, tbl, hash);
564 if (!pprev)
565 return ERR_PTR(-ENOMEM);
566
567 head = rht_dereference_bucket(*pprev, tbl, hash);
02fd97c3
HX
568
569 RCU_INIT_POINTER(obj->next, head);
ca26893f
HX
570 if (ht->rhlist) {
571 struct rhlist_head *list;
572
573 list = container_of(obj, struct rhlist_head, rhead);
574 RCU_INIT_POINTER(list->next, NULL);
575 }
02fd97c3 576
da20420f 577 rcu_assign_pointer(*pprev, obj);
02fd97c3
HX
578
579 atomic_inc(&ht->nelems);
ca26893f
HX
580 if (rht_grow_above_75(ht, tbl))
581 schedule_work(&ht->run_work);
02fd97c3 582
ca26893f
HX
583 return NULL;
584}
02fd97c3 585
ca26893f
HX
586static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
587 struct rhash_head *obj)
588{
589 struct bucket_table *new_tbl;
590 struct bucket_table *tbl;
591 unsigned int hash;
592 spinlock_t *lock;
593 void *data;
594
595 tbl = rcu_dereference(ht->tbl);
596
597 /* All insertions must grab the oldest table containing
598 * the hashed bucket that is yet to be rehashed.
599 */
600 for (;;) {
601 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
602 lock = rht_bucket_lock(tbl, hash);
603 spin_lock_bh(lock);
604
605 if (tbl->rehash <= hash)
606 break;
607
608 spin_unlock_bh(lock);
609 tbl = rcu_dereference(tbl->future_tbl);
610 }
611
612 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
613 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
614 if (PTR_ERR(new_tbl) != -EEXIST)
615 data = ERR_CAST(new_tbl);
616
617 while (!IS_ERR_OR_NULL(new_tbl)) {
618 tbl = new_tbl;
619 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
620 spin_lock_nested(rht_bucket_lock(tbl, hash),
621 SINGLE_DEPTH_NESTING);
622
623 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
624 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
625 if (PTR_ERR(new_tbl) != -EEXIST)
626 data = ERR_CAST(new_tbl);
627
628 spin_unlock(rht_bucket_lock(tbl, hash));
629 }
630
631 spin_unlock_bh(lock);
632
633 if (PTR_ERR(data) == -EAGAIN)
634 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
635 -EAGAIN);
636
637 return data;
638}
639
640void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
641 struct rhash_head *obj)
642{
643 void *data;
644
645 do {
646 rcu_read_lock();
647 data = rhashtable_try_insert(ht, key, obj);
648 rcu_read_unlock();
649 } while (PTR_ERR(data) == -EAGAIN);
650
651 return data;
02fd97c3
HX
652}
653EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
654
f2dba9c6 655/**
246779dd 656 * rhashtable_walk_enter - Initialise an iterator
f2dba9c6
HX
657 * @ht: Table to walk over
658 * @iter: Hash table Iterator
659 *
660 * This function prepares a hash table walk.
661 *
662 * Note that if you restart a walk after rhashtable_walk_stop you
663 * may see the same object twice. Also, you may miss objects if
664 * there are removals in between rhashtable_walk_stop and the next
665 * call to rhashtable_walk_start.
666 *
667 * For a completely stable walk you should construct your own data
668 * structure outside the hash table.
669 *
82266e98
N
670 * This function may be called from any process context, including
671 * non-preemptable context, but cannot be called from softirq or
672 * hardirq context.
f2dba9c6 673 *
246779dd 674 * You must call rhashtable_walk_exit after this function returns.
f2dba9c6 675 */
246779dd 676void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
f2dba9c6
HX
677{
678 iter->ht = ht;
679 iter->p = NULL;
680 iter->slot = 0;
681 iter->skip = 0;
2db54b47 682 iter->end_of_table = 0;
f2dba9c6 683
c6ff5268 684 spin_lock(&ht->lock);
246779dd 685 iter->walker.tbl =
179ccc0a 686 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
246779dd 687 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
c6ff5268 688 spin_unlock(&ht->lock);
f2dba9c6 689}
246779dd 690EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
f2dba9c6
HX
691
692/**
693 * rhashtable_walk_exit - Free an iterator
694 * @iter: Hash table Iterator
695 *
696 * This function frees resources allocated by rhashtable_walk_init.
697 */
698void rhashtable_walk_exit(struct rhashtable_iter *iter)
699{
c6ff5268 700 spin_lock(&iter->ht->lock);
246779dd
HX
701 if (iter->walker.tbl)
702 list_del(&iter->walker.list);
c6ff5268 703 spin_unlock(&iter->ht->lock);
f2dba9c6
HX
704}
705EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
706
707/**
97a6ec4a 708 * rhashtable_walk_start_check - Start a hash table walk
f2dba9c6
HX
709 * @iter: Hash table iterator
710 *
0647169c
AG
711 * Start a hash table walk at the current iterator position. Note that we take
712 * the RCU lock in all cases including when we return an error. So you must
713 * always call rhashtable_walk_stop to clean up.
f2dba9c6
HX
714 *
715 * Returns zero if successful.
716 *
717 * Returns -EAGAIN if resize event occured. Note that the iterator
718 * will rewind back to the beginning and you may use it immediately
719 * by calling rhashtable_walk_next.
97a6ec4a
TH
720 *
721 * rhashtable_walk_start is defined as an inline variant that returns
722 * void. This is preferred in cases where the caller would ignore
723 * resize events and always continue.
f2dba9c6 724 */
97a6ec4a 725int rhashtable_walk_start_check(struct rhashtable_iter *iter)
db4374f4 726 __acquires(RCU)
f2dba9c6 727{
eddee5ba 728 struct rhashtable *ht = iter->ht;
5d240a89 729 bool rhlist = ht->rhlist;
eddee5ba 730
c6ff5268 731 rcu_read_lock();
eddee5ba 732
c6ff5268 733 spin_lock(&ht->lock);
246779dd
HX
734 if (iter->walker.tbl)
735 list_del(&iter->walker.list);
c6ff5268 736 spin_unlock(&ht->lock);
eddee5ba 737
5d240a89
N
738 if (iter->end_of_table)
739 return 0;
740 if (!iter->walker.tbl) {
246779dd 741 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
b41cc04b
N
742 iter->slot = 0;
743 iter->skip = 0;
f2dba9c6
HX
744 return -EAGAIN;
745 }
746
5d240a89
N
747 if (iter->p && !rhlist) {
748 /*
749 * We need to validate that 'p' is still in the table, and
750 * if so, update 'skip'
751 */
752 struct rhash_head *p;
753 int skip = 0;
754 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
755 skip++;
756 if (p == iter->p) {
757 iter->skip = skip;
758 goto found;
759 }
760 }
761 iter->p = NULL;
762 } else if (iter->p && rhlist) {
763 /* Need to validate that 'list' is still in the table, and
764 * if so, update 'skip' and 'p'.
765 */
766 struct rhash_head *p;
767 struct rhlist_head *list;
768 int skip = 0;
769 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
770 for (list = container_of(p, struct rhlist_head, rhead);
771 list;
772 list = rcu_dereference(list->next)) {
773 skip++;
774 if (list == iter->list) {
775 iter->p = p;
776 skip = skip;
777 goto found;
778 }
779 }
780 }
781 iter->p = NULL;
782 }
783found:
f2dba9c6
HX
784 return 0;
785}
97a6ec4a 786EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
f2dba9c6
HX
787
788/**
2db54b47
TH
789 * __rhashtable_walk_find_next - Find the next element in a table (or the first
790 * one in case of a new walk).
791 *
f2dba9c6
HX
792 * @iter: Hash table iterator
793 *
2db54b47 794 * Returns the found object or NULL when the end of the table is reached.
f2dba9c6 795 *
2db54b47 796 * Returns -EAGAIN if resize event occurred.
f2dba9c6 797 */
2db54b47 798static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
f2dba9c6 799{
246779dd 800 struct bucket_table *tbl = iter->walker.tbl;
ca26893f 801 struct rhlist_head *list = iter->list;
f2dba9c6
HX
802 struct rhashtable *ht = iter->ht;
803 struct rhash_head *p = iter->p;
ca26893f 804 bool rhlist = ht->rhlist;
f2dba9c6 805
2db54b47
TH
806 if (!tbl)
807 return NULL;
f2dba9c6
HX
808
809 for (; iter->slot < tbl->size; iter->slot++) {
810 int skip = iter->skip;
811
812 rht_for_each_rcu(p, tbl, iter->slot) {
ca26893f
HX
813 if (rhlist) {
814 list = container_of(p, struct rhlist_head,
815 rhead);
816 do {
817 if (!skip)
818 goto next;
819 skip--;
820 list = rcu_dereference(list->next);
821 } while (list);
822
823 continue;
824 }
f2dba9c6
HX
825 if (!skip)
826 break;
827 skip--;
828 }
829
830next:
831 if (!rht_is_a_nulls(p)) {
832 iter->skip++;
833 iter->p = p;
ca26893f
HX
834 iter->list = list;
835 return rht_obj(ht, rhlist ? &list->rhead : p);
f2dba9c6
HX
836 }
837
838 iter->skip = 0;
839 }
840
142b942a
PS
841 iter->p = NULL;
842
d88252f9
HX
843 /* Ensure we see any new tables. */
844 smp_rmb();
845
246779dd
HX
846 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
847 if (iter->walker.tbl) {
f2dba9c6
HX
848 iter->slot = 0;
849 iter->skip = 0;
f2dba9c6 850 return ERR_PTR(-EAGAIN);
2db54b47
TH
851 } else {
852 iter->end_of_table = true;
f2dba9c6
HX
853 }
854
c936a79f 855 return NULL;
f2dba9c6 856}
2db54b47
TH
857
858/**
859 * rhashtable_walk_next - Return the next object and advance the iterator
860 * @iter: Hash table iterator
861 *
862 * Note that you must call rhashtable_walk_stop when you are finished
863 * with the walk.
864 *
865 * Returns the next object or NULL when the end of the table is reached.
866 *
867 * Returns -EAGAIN if resize event occurred. Note that the iterator
868 * will rewind back to the beginning and you may continue to use it.
869 */
870void *rhashtable_walk_next(struct rhashtable_iter *iter)
871{
872 struct rhlist_head *list = iter->list;
873 struct rhashtable *ht = iter->ht;
874 struct rhash_head *p = iter->p;
875 bool rhlist = ht->rhlist;
876
877 if (p) {
878 if (!rhlist || !(list = rcu_dereference(list->next))) {
879 p = rcu_dereference(p->next);
880 list = container_of(p, struct rhlist_head, rhead);
881 }
882 if (!rht_is_a_nulls(p)) {
883 iter->skip++;
884 iter->p = p;
885 iter->list = list;
886 return rht_obj(ht, rhlist ? &list->rhead : p);
887 }
888
889 /* At the end of this slot, switch to next one and then find
890 * next entry from that point.
891 */
892 iter->skip = 0;
893 iter->slot++;
894 }
895
896 return __rhashtable_walk_find_next(iter);
897}
f2dba9c6
HX
898EXPORT_SYMBOL_GPL(rhashtable_walk_next);
899
2db54b47
TH
900/**
901 * rhashtable_walk_peek - Return the next object but don't advance the iterator
902 * @iter: Hash table iterator
903 *
904 * Returns the next object or NULL when the end of the table is reached.
905 *
906 * Returns -EAGAIN if resize event occurred. Note that the iterator
907 * will rewind back to the beginning and you may continue to use it.
908 */
909void *rhashtable_walk_peek(struct rhashtable_iter *iter)
910{
911 struct rhlist_head *list = iter->list;
912 struct rhashtable *ht = iter->ht;
913 struct rhash_head *p = iter->p;
914
915 if (p)
916 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
917
918 /* No object found in current iter, find next one in the table. */
919
920 if (iter->skip) {
921 /* A nonzero skip value points to the next entry in the table
922 * beyond that last one that was found. Decrement skip so
923 * we find the current value. __rhashtable_walk_find_next
924 * will restore the original value of skip assuming that
925 * the table hasn't changed.
926 */
927 iter->skip--;
928 }
929
930 return __rhashtable_walk_find_next(iter);
931}
932EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
933
f2dba9c6
HX
934/**
935 * rhashtable_walk_stop - Finish a hash table walk
936 * @iter: Hash table iterator
937 *
0647169c
AG
938 * Finish a hash table walk. Does not reset the iterator to the start of the
939 * hash table.
f2dba9c6
HX
940 */
941void rhashtable_walk_stop(struct rhashtable_iter *iter)
db4374f4 942 __releases(RCU)
f2dba9c6 943{
eddee5ba 944 struct rhashtable *ht;
246779dd 945 struct bucket_table *tbl = iter->walker.tbl;
eddee5ba 946
eddee5ba 947 if (!tbl)
963ecbd4 948 goto out;
eddee5ba
HX
949
950 ht = iter->ht;
951
ba7c95ea 952 spin_lock(&ht->lock);
c4db8848 953 if (tbl->rehash < tbl->size)
246779dd 954 list_add(&iter->walker.list, &tbl->walkers);
eddee5ba 955 else
246779dd 956 iter->walker.tbl = NULL;
ba7c95ea 957 spin_unlock(&ht->lock);
eddee5ba 958
963ecbd4
HX
959out:
960 rcu_read_unlock();
f2dba9c6
HX
961}
962EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
963
488fb86e 964static size_t rounded_hashtable_size(const struct rhashtable_params *params)
7e1e7763 965{
94000176 966 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
e2e21c1c 967 (unsigned long)params->min_size);
7e1e7763
TG
968}
969
31ccde2d
HX
970static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
971{
972 return jhash2(key, length, seed);
973}
974
7e1e7763
TG
975/**
976 * rhashtable_init - initialize a new hash table
977 * @ht: hash table to be initialized
978 * @params: configuration parameters
979 *
980 * Initializes a new hash table based on the provided configuration
981 * parameters. A table can be configured either with a variable or
982 * fixed length key:
983 *
984 * Configuration Example 1: Fixed length keys
985 * struct test_obj {
986 * int key;
987 * void * my_member;
988 * struct rhash_head node;
989 * };
990 *
991 * struct rhashtable_params params = {
992 * .head_offset = offsetof(struct test_obj, node),
993 * .key_offset = offsetof(struct test_obj, key),
994 * .key_len = sizeof(int),
87545899 995 * .hashfn = jhash,
7e1e7763
TG
996 * };
997 *
998 * Configuration Example 2: Variable length keys
999 * struct test_obj {
1000 * [...]
1001 * struct rhash_head node;
1002 * };
1003 *
49f7b33e 1004 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
7e1e7763
TG
1005 * {
1006 * struct test_obj *obj = data;
1007 *
1008 * return [... hash ...];
1009 * }
1010 *
1011 * struct rhashtable_params params = {
1012 * .head_offset = offsetof(struct test_obj, node),
87545899 1013 * .hashfn = jhash,
7e1e7763 1014 * .obj_hashfn = my_hash_fn,
7e1e7763
TG
1015 * };
1016 */
488fb86e
HX
1017int rhashtable_init(struct rhashtable *ht,
1018 const struct rhashtable_params *params)
7e1e7763
TG
1019{
1020 struct bucket_table *tbl;
1021 size_t size;
1022
1023 size = HASH_DEFAULT_SIZE;
1024
31ccde2d 1025 if ((!params->key_len && !params->obj_hashfn) ||
02fd97c3 1026 (params->obj_hashfn && !params->obj_cmpfn))
7e1e7763
TG
1027 return -EINVAL;
1028
97defe1e
TG
1029 memset(ht, 0, sizeof(*ht));
1030 mutex_init(&ht->mutex);
ba7c95ea 1031 spin_lock_init(&ht->lock);
97defe1e
TG
1032 memcpy(&ht->p, params, sizeof(*params));
1033
a998f712
TG
1034 if (params->min_size)
1035 ht->p.min_size = roundup_pow_of_two(params->min_size);
1036
6d684e54
HX
1037 /* Cap total entries at 2^31 to avoid nelems overflow. */
1038 ht->max_elems = 1u << 31;
2d2ab658
HX
1039
1040 if (params->max_size) {
1041 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1042 if (ht->p.max_size < ht->max_elems / 2)
1043 ht->max_elems = ht->p.max_size * 2;
1044 }
6d684e54 1045
48e75b43 1046 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
a998f712 1047
3a324606
HX
1048 if (params->nelem_hint)
1049 size = rounded_hashtable_size(&ht->p);
1050
97defe1e
TG
1051 if (params->locks_mul)
1052 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1053 else
1054 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1055
31ccde2d
HX
1056 ht->key_len = ht->p.key_len;
1057 if (!params->hashfn) {
1058 ht->p.hashfn = jhash;
1059
1060 if (!(ht->key_len & (sizeof(u32) - 1))) {
1061 ht->key_len /= sizeof(u32);
1062 ht->p.hashfn = rhashtable_jhash2;
1063 }
1064 }
1065
b9ecfdaa 1066 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
7e1e7763
TG
1067 if (tbl == NULL)
1068 return -ENOMEM;
1069
545a148e 1070 atomic_set(&ht->nelems, 0);
a5b6846f 1071
7e1e7763
TG
1072 RCU_INIT_POINTER(ht->tbl, tbl);
1073
4c4b52d9 1074 INIT_WORK(&ht->run_work, rht_deferred_worker);
97defe1e 1075
7e1e7763
TG
1076 return 0;
1077}
1078EXPORT_SYMBOL_GPL(rhashtable_init);
1079
ca26893f
HX
1080/**
1081 * rhltable_init - initialize a new hash list table
1082 * @hlt: hash list table to be initialized
1083 * @params: configuration parameters
1084 *
1085 * Initializes a new hash list table.
1086 *
1087 * See documentation for rhashtable_init.
1088 */
1089int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1090{
1091 int err;
1092
ca26893f
HX
1093 err = rhashtable_init(&hlt->ht, params);
1094 hlt->ht.rhlist = true;
1095 return err;
1096}
1097EXPORT_SYMBOL_GPL(rhltable_init);
1098
1099static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1100 void (*free_fn)(void *ptr, void *arg),
1101 void *arg)
1102{
1103 struct rhlist_head *list;
1104
1105 if (!ht->rhlist) {
1106 free_fn(rht_obj(ht, obj), arg);
1107 return;
1108 }
1109
1110 list = container_of(obj, struct rhlist_head, rhead);
1111 do {
1112 obj = &list->rhead;
1113 list = rht_dereference(list->next, ht);
1114 free_fn(rht_obj(ht, obj), arg);
1115 } while (list);
1116}
1117
7e1e7763 1118/**
6b6f302c 1119 * rhashtable_free_and_destroy - free elements and destroy hash table
7e1e7763 1120 * @ht: the hash table to destroy
6b6f302c
TG
1121 * @free_fn: callback to release resources of element
1122 * @arg: pointer passed to free_fn
7e1e7763 1123 *
6b6f302c
TG
1124 * Stops an eventual async resize. If defined, invokes free_fn for each
1125 * element to releasal resources. Please note that RCU protected
1126 * readers may still be accessing the elements. Releasing of resources
1127 * must occur in a compatible manner. Then frees the bucket array.
1128 *
1129 * This function will eventually sleep to wait for an async resize
1130 * to complete. The caller is responsible that no further write operations
1131 * occurs in parallel.
7e1e7763 1132 */
6b6f302c
TG
1133void rhashtable_free_and_destroy(struct rhashtable *ht,
1134 void (*free_fn)(void *ptr, void *arg),
1135 void *arg)
7e1e7763 1136{
da20420f 1137 struct bucket_table *tbl;
6b6f302c 1138 unsigned int i;
97defe1e 1139
4c4b52d9 1140 cancel_work_sync(&ht->run_work);
97defe1e 1141
57699a40 1142 mutex_lock(&ht->mutex);
6b6f302c
TG
1143 tbl = rht_dereference(ht->tbl, ht);
1144 if (free_fn) {
1145 for (i = 0; i < tbl->size; i++) {
1146 struct rhash_head *pos, *next;
1147
ae6da1f5 1148 cond_resched();
da20420f 1149 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
6b6f302c
TG
1150 next = !rht_is_a_nulls(pos) ?
1151 rht_dereference(pos->next, ht) : NULL;
1152 !rht_is_a_nulls(pos);
1153 pos = next,
1154 next = !rht_is_a_nulls(pos) ?
1155 rht_dereference(pos->next, ht) : NULL)
ca26893f 1156 rhashtable_free_one(ht, pos, free_fn, arg);
6b6f302c
TG
1157 }
1158 }
1159
1160 bucket_table_free(tbl);
97defe1e 1161 mutex_unlock(&ht->mutex);
7e1e7763 1162}
6b6f302c
TG
1163EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1164
1165void rhashtable_destroy(struct rhashtable *ht)
1166{
1167 return rhashtable_free_and_destroy(ht, NULL, NULL);
1168}
7e1e7763 1169EXPORT_SYMBOL_GPL(rhashtable_destroy);
da20420f
HX
1170
1171struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1172 unsigned int hash)
1173{
1174 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1175 static struct rhash_head __rcu *rhnull =
1176 (struct rhash_head __rcu *)NULLS_MARKER(0);
1177 unsigned int index = hash & ((1 << tbl->nest) - 1);
1178 unsigned int size = tbl->size >> tbl->nest;
1179 unsigned int subhash = hash;
1180 union nested_table *ntbl;
1181
1182 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
c4d2603d 1183 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
da20420f
HX
1184 subhash >>= tbl->nest;
1185
1186 while (ntbl && size > (1 << shift)) {
1187 index = subhash & ((1 << shift) - 1);
c4d2603d
HX
1188 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1189 tbl, hash);
da20420f
HX
1190 size >>= shift;
1191 subhash >>= shift;
1192 }
1193
1194 if (!ntbl)
1195 return &rhnull;
1196
1197 return &ntbl[subhash].bucket;
1198
1199}
1200EXPORT_SYMBOL_GPL(rht_bucket_nested);
1201
1202struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1203 struct bucket_table *tbl,
1204 unsigned int hash)
1205{
1206 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1207 unsigned int index = hash & ((1 << tbl->nest) - 1);
1208 unsigned int size = tbl->size >> tbl->nest;
1209 union nested_table *ntbl;
1210 unsigned int shifted;
1211 unsigned int nhash;
1212
1213 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1214 hash >>= tbl->nest;
1215 nhash = index;
1216 shifted = tbl->nest;
1217 ntbl = nested_table_alloc(ht, &ntbl[index].table,
9b4f64a2 1218 size <= (1 << shift) ? shifted : 0);
da20420f
HX
1219
1220 while (ntbl && size > (1 << shift)) {
1221 index = hash & ((1 << shift) - 1);
1222 size >>= shift;
1223 hash >>= shift;
1224 nhash |= index << shifted;
1225 shifted += shift;
1226 ntbl = nested_table_alloc(ht, &ntbl[index].table,
9b4f64a2 1227 size <= (1 << shift) ? shifted : 0);
da20420f
HX
1228 }
1229
1230 if (!ntbl)
1231 return NULL;
1232
1233 return &ntbl[hash].bucket;
1234
1235}
1236EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);