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