2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
5 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
6 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8 * Code partially derived from nft_hash
9 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
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.
17 #include <linux/atomic.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/log2.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
25 #include <linux/jhash.h>
26 #include <linux/random.h>
27 #include <linux/rhashtable.h>
28 #include <linux/err.h>
29 #include <linux/export.h>
31 #define HASH_DEFAULT_SIZE 64UL
32 #define HASH_MIN_SIZE 4U
33 #define BUCKET_LOCKS_PER_CPU 32UL
36 union nested_table __rcu
*table
;
37 struct rhash_head __rcu
*bucket
;
40 static u32
head_hashfn(struct rhashtable
*ht
,
41 const struct bucket_table
*tbl
,
42 const struct rhash_head
*he
)
44 return rht_head_hashfn(ht
, tbl
, he
, ht
->p
);
47 #ifdef CONFIG_PROVE_LOCKING
48 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
50 int lockdep_rht_mutex_is_held(struct rhashtable
*ht
)
52 return (debug_locks
) ? lockdep_is_held(&ht
->mutex
) : 1;
54 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held
);
56 int lockdep_rht_bucket_is_held(const struct bucket_table
*tbl
, u32 hash
)
58 spinlock_t
*lock
= rht_bucket_lock(tbl
, hash
);
60 return (debug_locks
) ? lockdep_is_held(lock
) : 1;
62 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held
);
64 #define ASSERT_RHT_MUTEX(HT)
68 static int alloc_bucket_locks(struct rhashtable
*ht
, struct bucket_table
*tbl
,
72 #if defined(CONFIG_PROVE_LOCKING)
73 unsigned int nr_pcpus
= 2;
75 unsigned int nr_pcpus
= num_possible_cpus();
78 nr_pcpus
= min_t(unsigned int, nr_pcpus
, 64UL);
79 size
= roundup_pow_of_two(nr_pcpus
* ht
->p
.locks_mul
);
81 /* Never allocate more than 0.5 locks per bucket */
82 size
= min_t(unsigned int, size
, tbl
->size
>> 1);
85 size
= min(size
, 1U << tbl
->nest
);
87 if (sizeof(spinlock_t
) != 0) {
90 if (size
* sizeof(spinlock_t
) > PAGE_SIZE
&&
92 tbl
->locks
= vmalloc(size
* sizeof(spinlock_t
));
94 if (gfp
!= GFP_KERNEL
)
95 gfp
|= __GFP_NOWARN
| __GFP_NORETRY
;
98 tbl
->locks
= kmalloc_array(size
, sizeof(spinlock_t
),
102 for (i
= 0; i
< size
; i
++)
103 spin_lock_init(&tbl
->locks
[i
]);
105 tbl
->locks_mask
= size
- 1;
110 static void nested_table_free(union nested_table
*ntbl
, unsigned int size
)
112 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
113 const unsigned int len
= 1 << shift
;
116 ntbl
= rcu_dereference_raw(ntbl
->table
);
122 for (i
= 0; i
< len
; i
++)
123 nested_table_free(ntbl
+ i
, size
);
129 static void nested_bucket_table_free(const struct bucket_table
*tbl
)
131 unsigned int size
= tbl
->size
>> tbl
->nest
;
132 unsigned int len
= 1 << tbl
->nest
;
133 union nested_table
*ntbl
;
136 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
138 for (i
= 0; i
< len
; i
++)
139 nested_table_free(ntbl
+ i
, size
);
144 static void bucket_table_free(const struct bucket_table
*tbl
)
147 nested_bucket_table_free(tbl
);
153 static void bucket_table_free_rcu(struct rcu_head
*head
)
155 bucket_table_free(container_of(head
, struct bucket_table
, rcu
));
158 static union nested_table
*nested_table_alloc(struct rhashtable
*ht
,
159 union nested_table __rcu
**prev
,
160 unsigned int shifted
,
163 union nested_table
*ntbl
;
166 ntbl
= rcu_dereference(*prev
);
170 ntbl
= kzalloc(PAGE_SIZE
, GFP_ATOMIC
);
172 if (ntbl
&& shifted
) {
173 for (i
= 0; i
< PAGE_SIZE
/ sizeof(ntbl
[0].bucket
); i
++)
174 INIT_RHT_NULLS_HEAD(ntbl
[i
].bucket
, ht
,
175 (i
<< shifted
) | nhash
);
178 rcu_assign_pointer(*prev
, ntbl
);
183 static struct bucket_table
*nested_bucket_table_alloc(struct rhashtable
*ht
,
187 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
188 struct bucket_table
*tbl
;
191 if (nbuckets
< (1 << (shift
+ 1)))
194 size
= sizeof(*tbl
) + sizeof(tbl
->buckets
[0]);
196 tbl
= kzalloc(size
, gfp
);
200 if (!nested_table_alloc(ht
, (union nested_table __rcu
**)tbl
->buckets
,
206 tbl
->nest
= (ilog2(nbuckets
) - 1) % shift
+ 1;
211 static struct bucket_table
*bucket_table_alloc(struct rhashtable
*ht
,
215 struct bucket_table
*tbl
= NULL
;
219 size
= sizeof(*tbl
) + nbuckets
* sizeof(tbl
->buckets
[0]);
220 if (size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
) ||
222 tbl
= kzalloc(size
, gfp
| __GFP_NOWARN
| __GFP_NORETRY
);
223 if (tbl
== NULL
&& gfp
== GFP_KERNEL
)
228 if (tbl
== NULL
&& gfp
!= GFP_KERNEL
) {
229 tbl
= nested_bucket_table_alloc(ht
, nbuckets
, gfp
);
237 if (alloc_bucket_locks(ht
, tbl
, gfp
) < 0) {
238 bucket_table_free(tbl
);
242 INIT_LIST_HEAD(&tbl
->walkers
);
244 get_random_bytes(&tbl
->hash_rnd
, sizeof(tbl
->hash_rnd
));
246 for (i
= 0; i
< nbuckets
; i
++)
247 INIT_RHT_NULLS_HEAD(tbl
->buckets
[i
], ht
, i
);
252 static struct bucket_table
*rhashtable_last_table(struct rhashtable
*ht
,
253 struct bucket_table
*tbl
)
255 struct bucket_table
*new_tbl
;
259 tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
265 static int rhashtable_rehash_one(struct rhashtable
*ht
, unsigned int old_hash
)
267 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
268 struct bucket_table
*new_tbl
= rhashtable_last_table(ht
,
269 rht_dereference_rcu(old_tbl
->future_tbl
, ht
));
270 struct rhash_head __rcu
**pprev
= rht_bucket_var(old_tbl
, old_hash
);
272 struct rhash_head
*head
, *next
, *entry
;
273 spinlock_t
*new_bucket_lock
;
274 unsigned int new_hash
;
281 rht_for_each(entry
, old_tbl
, old_hash
) {
283 next
= rht_dereference_bucket(entry
->next
, old_tbl
, old_hash
);
285 if (rht_is_a_nulls(next
))
288 pprev
= &entry
->next
;
294 new_hash
= head_hashfn(ht
, new_tbl
, entry
);
296 new_bucket_lock
= rht_bucket_lock(new_tbl
, new_hash
);
298 spin_lock_nested(new_bucket_lock
, SINGLE_DEPTH_NESTING
);
299 head
= rht_dereference_bucket(new_tbl
->buckets
[new_hash
],
302 RCU_INIT_POINTER(entry
->next
, head
);
304 rcu_assign_pointer(new_tbl
->buckets
[new_hash
], entry
);
305 spin_unlock(new_bucket_lock
);
307 rcu_assign_pointer(*pprev
, next
);
313 static int rhashtable_rehash_chain(struct rhashtable
*ht
,
314 unsigned int old_hash
)
316 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
317 spinlock_t
*old_bucket_lock
;
320 old_bucket_lock
= rht_bucket_lock(old_tbl
, old_hash
);
322 spin_lock_bh(old_bucket_lock
);
323 while (!(err
= rhashtable_rehash_one(ht
, old_hash
)))
326 if (err
== -ENOENT
) {
330 spin_unlock_bh(old_bucket_lock
);
335 static int rhashtable_rehash_attach(struct rhashtable
*ht
,
336 struct bucket_table
*old_tbl
,
337 struct bucket_table
*new_tbl
)
339 /* Protect future_tbl using the first bucket lock. */
340 spin_lock_bh(old_tbl
->locks
);
342 /* Did somebody beat us to it? */
343 if (rcu_access_pointer(old_tbl
->future_tbl
)) {
344 spin_unlock_bh(old_tbl
->locks
);
348 /* Make insertions go into the new, empty table right away. Deletions
349 * and lookups will be attempted in both tables until we synchronize.
351 rcu_assign_pointer(old_tbl
->future_tbl
, new_tbl
);
353 spin_unlock_bh(old_tbl
->locks
);
358 static int rhashtable_rehash_table(struct rhashtable
*ht
)
360 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
361 struct bucket_table
*new_tbl
;
362 struct rhashtable_walker
*walker
;
363 unsigned int old_hash
;
366 new_tbl
= rht_dereference(old_tbl
->future_tbl
, ht
);
370 for (old_hash
= 0; old_hash
< old_tbl
->size
; old_hash
++) {
371 err
= rhashtable_rehash_chain(ht
, old_hash
);
376 /* Publish the new table pointer. */
377 rcu_assign_pointer(ht
->tbl
, new_tbl
);
379 spin_lock(&ht
->lock
);
380 list_for_each_entry(walker
, &old_tbl
->walkers
, list
)
382 spin_unlock(&ht
->lock
);
384 /* Wait for readers. All new readers will see the new
385 * table, and thus no references to the old table will
388 call_rcu(&old_tbl
->rcu
, bucket_table_free_rcu
);
390 return rht_dereference(new_tbl
->future_tbl
, ht
) ? -EAGAIN
: 0;
393 static int rhashtable_rehash_alloc(struct rhashtable
*ht
,
394 struct bucket_table
*old_tbl
,
397 struct bucket_table
*new_tbl
;
400 ASSERT_RHT_MUTEX(ht
);
402 new_tbl
= bucket_table_alloc(ht
, size
, GFP_KERNEL
);
406 err
= rhashtable_rehash_attach(ht
, old_tbl
, new_tbl
);
408 bucket_table_free(new_tbl
);
414 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
415 * @ht: the hash table to shrink
417 * This function shrinks the hash table to fit, i.e., the smallest
418 * size would not cause it to expand right away automatically.
420 * The caller must ensure that no concurrent resizing occurs by holding
423 * The caller must ensure that no concurrent table mutations take place.
424 * It is however valid to have concurrent lookups if they are RCU protected.
426 * It is valid to have concurrent insertions and deletions protected by per
427 * bucket locks or concurrent RCU protected lookups and traversals.
429 static int rhashtable_shrink(struct rhashtable
*ht
)
431 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
432 unsigned int nelems
= atomic_read(&ht
->nelems
);
433 unsigned int size
= 0;
436 size
= roundup_pow_of_two(nelems
* 3 / 2);
437 if (size
< ht
->p
.min_size
)
438 size
= ht
->p
.min_size
;
440 if (old_tbl
->size
<= size
)
443 if (rht_dereference(old_tbl
->future_tbl
, ht
))
446 return rhashtable_rehash_alloc(ht
, old_tbl
, size
);
449 static void rht_deferred_worker(struct work_struct
*work
)
451 struct rhashtable
*ht
;
452 struct bucket_table
*tbl
;
455 ht
= container_of(work
, struct rhashtable
, run_work
);
456 mutex_lock(&ht
->mutex
);
458 tbl
= rht_dereference(ht
->tbl
, ht
);
459 tbl
= rhashtable_last_table(ht
, tbl
);
461 if (rht_grow_above_75(ht
, tbl
))
462 err
= rhashtable_rehash_alloc(ht
, tbl
, tbl
->size
* 2);
463 else if (ht
->p
.automatic_shrinking
&& rht_shrink_below_30(ht
, tbl
))
464 err
= rhashtable_shrink(ht
);
466 err
= rhashtable_rehash_alloc(ht
, tbl
, tbl
->size
);
469 err
= rhashtable_rehash_table(ht
);
471 mutex_unlock(&ht
->mutex
);
474 schedule_work(&ht
->run_work
);
477 static int rhashtable_insert_rehash(struct rhashtable
*ht
,
478 struct bucket_table
*tbl
)
480 struct bucket_table
*old_tbl
;
481 struct bucket_table
*new_tbl
;
485 old_tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
491 if (rht_grow_above_75(ht
, tbl
))
493 /* Do not schedule more than one rehash */
494 else if (old_tbl
!= tbl
)
499 new_tbl
= bucket_table_alloc(ht
, size
, GFP_ATOMIC
);
503 err
= rhashtable_rehash_attach(ht
, tbl
, new_tbl
);
505 bucket_table_free(new_tbl
);
509 schedule_work(&ht
->run_work
);
514 /* Do not fail the insert if someone else did a rehash. */
515 if (likely(rcu_dereference_raw(tbl
->future_tbl
)))
518 /* Schedule async rehash to retry allocation in process context. */
520 schedule_work(&ht
->run_work
);
525 static void *rhashtable_lookup_one(struct rhashtable
*ht
,
526 struct bucket_table
*tbl
, unsigned int hash
,
527 const void *key
, struct rhash_head
*obj
)
529 struct rhashtable_compare_arg arg
= {
533 struct rhash_head __rcu
**pprev
;
534 struct rhash_head
*head
;
537 elasticity
= ht
->elasticity
;
538 pprev
= rht_bucket_var(tbl
, hash
);
539 rht_for_each_continue(head
, *pprev
, tbl
, hash
) {
540 struct rhlist_head
*list
;
541 struct rhlist_head
*plist
;
546 ht
->p
.obj_cmpfn(&arg
, rht_obj(ht
, head
)) :
547 rhashtable_compare(&arg
, rht_obj(ht
, head
))))
551 return rht_obj(ht
, head
);
553 list
= container_of(obj
, struct rhlist_head
, rhead
);
554 plist
= container_of(head
, struct rhlist_head
, rhead
);
556 RCU_INIT_POINTER(list
->next
, plist
);
557 head
= rht_dereference_bucket(head
->next
, tbl
, hash
);
558 RCU_INIT_POINTER(list
->rhead
.next
, head
);
559 rcu_assign_pointer(*pprev
, obj
);
565 return ERR_PTR(-EAGAIN
);
567 return ERR_PTR(-ENOENT
);
570 static struct bucket_table
*rhashtable_insert_one(struct rhashtable
*ht
,
571 struct bucket_table
*tbl
,
573 struct rhash_head
*obj
,
576 struct rhash_head __rcu
**pprev
;
577 struct bucket_table
*new_tbl
;
578 struct rhash_head
*head
;
580 if (!IS_ERR_OR_NULL(data
))
581 return ERR_PTR(-EEXIST
);
583 if (PTR_ERR(data
) != -EAGAIN
&& PTR_ERR(data
) != -ENOENT
)
584 return ERR_CAST(data
);
586 new_tbl
= rcu_dereference(tbl
->future_tbl
);
590 if (PTR_ERR(data
) != -ENOENT
)
591 return ERR_CAST(data
);
593 if (unlikely(rht_grow_above_max(ht
, tbl
)))
594 return ERR_PTR(-E2BIG
);
596 if (unlikely(rht_grow_above_100(ht
, tbl
)))
597 return ERR_PTR(-EAGAIN
);
599 pprev
= rht_bucket_insert(ht
, tbl
, hash
);
601 return ERR_PTR(-ENOMEM
);
603 head
= rht_dereference_bucket(*pprev
, tbl
, hash
);
605 RCU_INIT_POINTER(obj
->next
, head
);
607 struct rhlist_head
*list
;
609 list
= container_of(obj
, struct rhlist_head
, rhead
);
610 RCU_INIT_POINTER(list
->next
, NULL
);
613 rcu_assign_pointer(*pprev
, obj
);
615 atomic_inc(&ht
->nelems
);
616 if (rht_grow_above_75(ht
, tbl
))
617 schedule_work(&ht
->run_work
);
622 static void *rhashtable_try_insert(struct rhashtable
*ht
, const void *key
,
623 struct rhash_head
*obj
)
625 struct bucket_table
*new_tbl
;
626 struct bucket_table
*tbl
;
631 tbl
= rcu_dereference(ht
->tbl
);
633 /* All insertions must grab the oldest table containing
634 * the hashed bucket that is yet to be rehashed.
637 hash
= rht_head_hashfn(ht
, tbl
, obj
, ht
->p
);
638 lock
= rht_bucket_lock(tbl
, hash
);
641 if (tbl
->rehash
<= hash
)
644 spin_unlock_bh(lock
);
645 tbl
= rcu_dereference(tbl
->future_tbl
);
648 data
= rhashtable_lookup_one(ht
, tbl
, hash
, key
, obj
);
649 new_tbl
= rhashtable_insert_one(ht
, tbl
, hash
, obj
, data
);
650 if (PTR_ERR(new_tbl
) != -EEXIST
)
651 data
= ERR_CAST(new_tbl
);
653 while (!IS_ERR_OR_NULL(new_tbl
)) {
655 hash
= rht_head_hashfn(ht
, tbl
, obj
, ht
->p
);
656 spin_lock_nested(rht_bucket_lock(tbl
, hash
),
657 SINGLE_DEPTH_NESTING
);
659 data
= rhashtable_lookup_one(ht
, tbl
, hash
, key
, obj
);
660 new_tbl
= rhashtable_insert_one(ht
, tbl
, hash
, obj
, data
);
661 if (PTR_ERR(new_tbl
) != -EEXIST
)
662 data
= ERR_CAST(new_tbl
);
664 spin_unlock(rht_bucket_lock(tbl
, hash
));
667 spin_unlock_bh(lock
);
669 if (PTR_ERR(data
) == -EAGAIN
)
670 data
= ERR_PTR(rhashtable_insert_rehash(ht
, tbl
) ?:
676 void *rhashtable_insert_slow(struct rhashtable
*ht
, const void *key
,
677 struct rhash_head
*obj
)
683 data
= rhashtable_try_insert(ht
, key
, obj
);
685 } while (PTR_ERR(data
) == -EAGAIN
);
689 EXPORT_SYMBOL_GPL(rhashtable_insert_slow
);
692 * rhashtable_walk_enter - Initialise an iterator
693 * @ht: Table to walk over
694 * @iter: Hash table Iterator
696 * This function prepares a hash table walk.
698 * Note that if you restart a walk after rhashtable_walk_stop you
699 * may see the same object twice. Also, you may miss objects if
700 * there are removals in between rhashtable_walk_stop and the next
701 * call to rhashtable_walk_start.
703 * For a completely stable walk you should construct your own data
704 * structure outside the hash table.
706 * This function may sleep so you must not call it from interrupt
707 * context or with spin locks held.
709 * You must call rhashtable_walk_exit after this function returns.
711 void rhashtable_walk_enter(struct rhashtable
*ht
, struct rhashtable_iter
*iter
)
718 spin_lock(&ht
->lock
);
720 rcu_dereference_protected(ht
->tbl
, lockdep_is_held(&ht
->lock
));
721 list_add(&iter
->walker
.list
, &iter
->walker
.tbl
->walkers
);
722 spin_unlock(&ht
->lock
);
724 EXPORT_SYMBOL_GPL(rhashtable_walk_enter
);
727 * rhashtable_walk_exit - Free an iterator
728 * @iter: Hash table Iterator
730 * This function frees resources allocated by rhashtable_walk_init.
732 void rhashtable_walk_exit(struct rhashtable_iter
*iter
)
734 spin_lock(&iter
->ht
->lock
);
735 if (iter
->walker
.tbl
)
736 list_del(&iter
->walker
.list
);
737 spin_unlock(&iter
->ht
->lock
);
739 EXPORT_SYMBOL_GPL(rhashtable_walk_exit
);
742 * rhashtable_walk_start - Start a hash table walk
743 * @iter: Hash table iterator
745 * Start a hash table walk. Note that we take the RCU lock in all
746 * cases including when we return an error. So you must always call
747 * rhashtable_walk_stop to clean up.
749 * Returns zero if successful.
751 * Returns -EAGAIN if resize event occured. Note that the iterator
752 * will rewind back to the beginning and you may use it immediately
753 * by calling rhashtable_walk_next.
755 int rhashtable_walk_start(struct rhashtable_iter
*iter
)
758 struct rhashtable
*ht
= iter
->ht
;
762 spin_lock(&ht
->lock
);
763 if (iter
->walker
.tbl
)
764 list_del(&iter
->walker
.list
);
765 spin_unlock(&ht
->lock
);
767 if (!iter
->walker
.tbl
) {
768 iter
->walker
.tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
774 EXPORT_SYMBOL_GPL(rhashtable_walk_start
);
777 * rhashtable_walk_next - Return the next object and advance the iterator
778 * @iter: Hash table iterator
780 * Note that you must call rhashtable_walk_stop when you are finished
783 * Returns the next object or NULL when the end of the table is reached.
785 * Returns -EAGAIN if resize event occured. Note that the iterator
786 * will rewind back to the beginning and you may continue to use it.
788 void *rhashtable_walk_next(struct rhashtable_iter
*iter
)
790 struct bucket_table
*tbl
= iter
->walker
.tbl
;
791 struct rhlist_head
*list
= iter
->list
;
792 struct rhashtable
*ht
= iter
->ht
;
793 struct rhash_head
*p
= iter
->p
;
794 bool rhlist
= ht
->rhlist
;
797 if (!rhlist
|| !(list
= rcu_dereference(list
->next
))) {
798 p
= rcu_dereference(p
->next
);
799 list
= container_of(p
, struct rhlist_head
, rhead
);
804 for (; iter
->slot
< tbl
->size
; iter
->slot
++) {
805 int skip
= iter
->skip
;
807 rht_for_each_rcu(p
, tbl
, iter
->slot
) {
809 list
= container_of(p
, struct rhlist_head
,
815 list
= rcu_dereference(list
->next
);
826 if (!rht_is_a_nulls(p
)) {
830 return rht_obj(ht
, rhlist
? &list
->rhead
: p
);
838 /* Ensure we see any new tables. */
841 iter
->walker
.tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
842 if (iter
->walker
.tbl
) {
845 return ERR_PTR(-EAGAIN
);
850 EXPORT_SYMBOL_GPL(rhashtable_walk_next
);
853 * rhashtable_walk_stop - Finish a hash table walk
854 * @iter: Hash table iterator
856 * Finish a hash table walk.
858 void rhashtable_walk_stop(struct rhashtable_iter
*iter
)
861 struct rhashtable
*ht
;
862 struct bucket_table
*tbl
= iter
->walker
.tbl
;
869 spin_lock(&ht
->lock
);
870 if (tbl
->rehash
< tbl
->size
)
871 list_add(&iter
->walker
.list
, &tbl
->walkers
);
873 iter
->walker
.tbl
= NULL
;
874 spin_unlock(&ht
->lock
);
881 EXPORT_SYMBOL_GPL(rhashtable_walk_stop
);
883 static size_t rounded_hashtable_size(const struct rhashtable_params
*params
)
885 return max(roundup_pow_of_two(params
->nelem_hint
* 4 / 3),
886 (unsigned long)params
->min_size
);
889 static u32
rhashtable_jhash2(const void *key
, u32 length
, u32 seed
)
891 return jhash2(key
, length
, seed
);
895 * rhashtable_init - initialize a new hash table
896 * @ht: hash table to be initialized
897 * @params: configuration parameters
899 * Initializes a new hash table based on the provided configuration
900 * parameters. A table can be configured either with a variable or
903 * Configuration Example 1: Fixed length keys
907 * struct rhash_head node;
910 * struct rhashtable_params params = {
911 * .head_offset = offsetof(struct test_obj, node),
912 * .key_offset = offsetof(struct test_obj, key),
913 * .key_len = sizeof(int),
915 * .nulls_base = (1U << RHT_BASE_SHIFT),
918 * Configuration Example 2: Variable length keys
921 * struct rhash_head node;
924 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
926 * struct test_obj *obj = data;
928 * return [... hash ...];
931 * struct rhashtable_params params = {
932 * .head_offset = offsetof(struct test_obj, node),
934 * .obj_hashfn = my_hash_fn,
937 int rhashtable_init(struct rhashtable
*ht
,
938 const struct rhashtable_params
*params
)
940 struct bucket_table
*tbl
;
943 size
= HASH_DEFAULT_SIZE
;
945 if ((!params
->key_len
&& !params
->obj_hashfn
) ||
946 (params
->obj_hashfn
&& !params
->obj_cmpfn
))
949 if (params
->nulls_base
&& params
->nulls_base
< (1U << RHT_BASE_SHIFT
))
952 memset(ht
, 0, sizeof(*ht
));
953 mutex_init(&ht
->mutex
);
954 spin_lock_init(&ht
->lock
);
955 memcpy(&ht
->p
, params
, sizeof(*params
));
957 if (params
->min_size
)
958 ht
->p
.min_size
= roundup_pow_of_two(params
->min_size
);
960 if (params
->max_size
)
961 ht
->p
.max_size
= rounddown_pow_of_two(params
->max_size
);
963 if (params
->insecure_max_entries
)
964 ht
->p
.insecure_max_entries
=
965 rounddown_pow_of_two(params
->insecure_max_entries
);
967 ht
->p
.insecure_max_entries
= ht
->p
.max_size
* 2;
969 ht
->p
.min_size
= max(ht
->p
.min_size
, HASH_MIN_SIZE
);
971 if (params
->nelem_hint
)
972 size
= rounded_hashtable_size(&ht
->p
);
974 /* The maximum (not average) chain length grows with the
975 * size of the hash table, at a rate of (log N)/(log log N).
976 * The value of 16 is selected so that even if the hash
977 * table grew to 2^32 you would not expect the maximum
978 * chain length to exceed it unless we are under attack
979 * (or extremely unlucky).
981 * As this limit is only to detect attacks, we don't need
982 * to set it to a lower value as you'd need the chain
983 * length to vastly exceed 16 to have any real effect
986 if (!params
->insecure_elasticity
)
989 if (params
->locks_mul
)
990 ht
->p
.locks_mul
= roundup_pow_of_two(params
->locks_mul
);
992 ht
->p
.locks_mul
= BUCKET_LOCKS_PER_CPU
;
994 ht
->key_len
= ht
->p
.key_len
;
995 if (!params
->hashfn
) {
996 ht
->p
.hashfn
= jhash
;
998 if (!(ht
->key_len
& (sizeof(u32
) - 1))) {
999 ht
->key_len
/= sizeof(u32
);
1000 ht
->p
.hashfn
= rhashtable_jhash2
;
1004 tbl
= bucket_table_alloc(ht
, size
, GFP_KERNEL
);
1008 atomic_set(&ht
->nelems
, 0);
1010 RCU_INIT_POINTER(ht
->tbl
, tbl
);
1012 INIT_WORK(&ht
->run_work
, rht_deferred_worker
);
1016 EXPORT_SYMBOL_GPL(rhashtable_init
);
1019 * rhltable_init - initialize a new hash list table
1020 * @hlt: hash list table to be initialized
1021 * @params: configuration parameters
1023 * Initializes a new hash list table.
1025 * See documentation for rhashtable_init.
1027 int rhltable_init(struct rhltable
*hlt
, const struct rhashtable_params
*params
)
1031 /* No rhlist NULLs marking for now. */
1032 if (params
->nulls_base
)
1035 err
= rhashtable_init(&hlt
->ht
, params
);
1036 hlt
->ht
.rhlist
= true;
1039 EXPORT_SYMBOL_GPL(rhltable_init
);
1041 static void rhashtable_free_one(struct rhashtable
*ht
, struct rhash_head
*obj
,
1042 void (*free_fn
)(void *ptr
, void *arg
),
1045 struct rhlist_head
*list
;
1048 free_fn(rht_obj(ht
, obj
), arg
);
1052 list
= container_of(obj
, struct rhlist_head
, rhead
);
1055 list
= rht_dereference(list
->next
, ht
);
1056 free_fn(rht_obj(ht
, obj
), arg
);
1061 * rhashtable_free_and_destroy - free elements and destroy hash table
1062 * @ht: the hash table to destroy
1063 * @free_fn: callback to release resources of element
1064 * @arg: pointer passed to free_fn
1066 * Stops an eventual async resize. If defined, invokes free_fn for each
1067 * element to releasal resources. Please note that RCU protected
1068 * readers may still be accessing the elements. Releasing of resources
1069 * must occur in a compatible manner. Then frees the bucket array.
1071 * This function will eventually sleep to wait for an async resize
1072 * to complete. The caller is responsible that no further write operations
1073 * occurs in parallel.
1075 void rhashtable_free_and_destroy(struct rhashtable
*ht
,
1076 void (*free_fn
)(void *ptr
, void *arg
),
1079 struct bucket_table
*tbl
;
1082 cancel_work_sync(&ht
->run_work
);
1084 mutex_lock(&ht
->mutex
);
1085 tbl
= rht_dereference(ht
->tbl
, ht
);
1087 for (i
= 0; i
< tbl
->size
; i
++) {
1088 struct rhash_head
*pos
, *next
;
1090 for (pos
= rht_dereference(*rht_bucket(tbl
, i
), ht
),
1091 next
= !rht_is_a_nulls(pos
) ?
1092 rht_dereference(pos
->next
, ht
) : NULL
;
1093 !rht_is_a_nulls(pos
);
1095 next
= !rht_is_a_nulls(pos
) ?
1096 rht_dereference(pos
->next
, ht
) : NULL
)
1097 rhashtable_free_one(ht
, pos
, free_fn
, arg
);
1101 bucket_table_free(tbl
);
1102 mutex_unlock(&ht
->mutex
);
1104 EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy
);
1106 void rhashtable_destroy(struct rhashtable
*ht
)
1108 return rhashtable_free_and_destroy(ht
, NULL
, NULL
);
1110 EXPORT_SYMBOL_GPL(rhashtable_destroy
);
1112 struct rhash_head __rcu
**rht_bucket_nested(const struct bucket_table
*tbl
,
1115 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
1116 static struct rhash_head __rcu
*rhnull
=
1117 (struct rhash_head __rcu
*)NULLS_MARKER(0);
1118 unsigned int index
= hash
& ((1 << tbl
->nest
) - 1);
1119 unsigned int size
= tbl
->size
>> tbl
->nest
;
1120 unsigned int subhash
= hash
;
1121 union nested_table
*ntbl
;
1123 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
1124 ntbl
= rht_dereference_bucket_rcu(ntbl
[index
].table
, tbl
, hash
);
1125 subhash
>>= tbl
->nest
;
1127 while (ntbl
&& size
> (1 << shift
)) {
1128 index
= subhash
& ((1 << shift
) - 1);
1129 ntbl
= rht_dereference_bucket_rcu(ntbl
[index
].table
,
1138 return &ntbl
[subhash
].bucket
;
1141 EXPORT_SYMBOL_GPL(rht_bucket_nested
);
1143 struct rhash_head __rcu
**rht_bucket_nested_insert(struct rhashtable
*ht
,
1144 struct bucket_table
*tbl
,
1147 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
1148 unsigned int index
= hash
& ((1 << tbl
->nest
) - 1);
1149 unsigned int size
= tbl
->size
>> tbl
->nest
;
1150 union nested_table
*ntbl
;
1151 unsigned int shifted
;
1154 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
1157 shifted
= tbl
->nest
;
1158 ntbl
= nested_table_alloc(ht
, &ntbl
[index
].table
,
1159 size
<= (1 << shift
) ? shifted
: 0, nhash
);
1161 while (ntbl
&& size
> (1 << shift
)) {
1162 index
= hash
& ((1 << shift
) - 1);
1165 nhash
|= index
<< shifted
;
1167 ntbl
= nested_table_alloc(ht
, &ntbl
[index
].table
,
1168 size
<= (1 << shift
) ? shifted
: 0,
1175 return &ntbl
[hash
].bucket
;
1178 EXPORT_SYMBOL_GPL(rht_bucket_nested_insert
);