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/rculist.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
26 #include <linux/jhash.h>
27 #include <linux/random.h>
28 #include <linux/rhashtable.h>
29 #include <linux/err.h>
30 #include <linux/export.h>
32 #define HASH_DEFAULT_SIZE 64UL
33 #define HASH_MIN_SIZE 4U
34 #define BUCKET_LOCKS_PER_CPU 32UL
37 union nested_table __rcu
*table
;
38 struct rhash_head __rcu
*bucket
;
41 static u32
head_hashfn(struct rhashtable
*ht
,
42 const struct bucket_table
*tbl
,
43 const struct rhash_head
*he
)
45 return rht_head_hashfn(ht
, tbl
, he
, ht
->p
);
48 #ifdef CONFIG_PROVE_LOCKING
49 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
51 int lockdep_rht_mutex_is_held(struct rhashtable
*ht
)
53 return (debug_locks
) ? lockdep_is_held(&ht
->mutex
) : 1;
55 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held
);
57 int lockdep_rht_bucket_is_held(const struct bucket_table
*tbl
, u32 hash
)
59 spinlock_t
*lock
= rht_bucket_lock(tbl
, hash
);
61 return (debug_locks
) ? lockdep_is_held(lock
) : 1;
63 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held
);
65 #define ASSERT_RHT_MUTEX(HT)
68 static void nested_table_free(union nested_table
*ntbl
, unsigned int size
)
70 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
71 const unsigned int len
= 1 << shift
;
74 ntbl
= rcu_dereference_raw(ntbl
->table
);
80 for (i
= 0; i
< len
; i
++)
81 nested_table_free(ntbl
+ i
, size
);
87 static void nested_bucket_table_free(const struct bucket_table
*tbl
)
89 unsigned int size
= tbl
->size
>> tbl
->nest
;
90 unsigned int len
= 1 << tbl
->nest
;
91 union nested_table
*ntbl
;
94 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
96 for (i
= 0; i
< len
; i
++)
97 nested_table_free(ntbl
+ i
, size
);
102 static void bucket_table_free(const struct bucket_table
*tbl
)
105 nested_bucket_table_free(tbl
);
107 free_bucket_spinlocks(tbl
->locks
);
111 static void bucket_table_free_rcu(struct rcu_head
*head
)
113 bucket_table_free(container_of(head
, struct bucket_table
, rcu
));
116 static union nested_table
*nested_table_alloc(struct rhashtable
*ht
,
117 union nested_table __rcu
**prev
,
120 union nested_table
*ntbl
;
123 ntbl
= rcu_dereference(*prev
);
127 ntbl
= kzalloc(PAGE_SIZE
, GFP_ATOMIC
);
130 for (i
= 0; i
< PAGE_SIZE
/ sizeof(ntbl
[0]); i
++)
131 INIT_RHT_NULLS_HEAD(ntbl
[i
].bucket
);
134 rcu_assign_pointer(*prev
, ntbl
);
139 static struct bucket_table
*nested_bucket_table_alloc(struct rhashtable
*ht
,
143 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
144 struct bucket_table
*tbl
;
147 if (nbuckets
< (1 << (shift
+ 1)))
150 size
= sizeof(*tbl
) + sizeof(tbl
->buckets
[0]);
152 tbl
= kzalloc(size
, gfp
);
156 if (!nested_table_alloc(ht
, (union nested_table __rcu
**)tbl
->buckets
,
162 tbl
->nest
= (ilog2(nbuckets
) - 1) % shift
+ 1;
167 static struct bucket_table
*bucket_table_alloc(struct rhashtable
*ht
,
171 struct bucket_table
*tbl
= NULL
;
172 size_t size
, max_locks
;
175 size
= sizeof(*tbl
) + nbuckets
* sizeof(tbl
->buckets
[0]);
176 tbl
= kvzalloc(size
, gfp
);
180 if (tbl
== NULL
&& (gfp
& ~__GFP_NOFAIL
) != GFP_KERNEL
) {
181 tbl
= nested_bucket_table_alloc(ht
, nbuckets
, gfp
);
190 max_locks
= size
>> 1;
192 max_locks
= min_t(size_t, max_locks
, 1U << tbl
->nest
);
194 if (alloc_bucket_spinlocks(&tbl
->locks
, &tbl
->locks_mask
, max_locks
,
195 ht
->p
.locks_mul
, gfp
) < 0) {
196 bucket_table_free(tbl
);
200 INIT_LIST_HEAD(&tbl
->walkers
);
202 tbl
->hash_rnd
= get_random_u32();
204 for (i
= 0; i
< nbuckets
; i
++)
205 INIT_RHT_NULLS_HEAD(tbl
->buckets
[i
]);
210 static struct bucket_table
*rhashtable_last_table(struct rhashtable
*ht
,
211 struct bucket_table
*tbl
)
213 struct bucket_table
*new_tbl
;
217 tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
223 static int rhashtable_rehash_one(struct rhashtable
*ht
, unsigned int old_hash
)
225 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
226 struct bucket_table
*new_tbl
= rhashtable_last_table(ht
, old_tbl
);
227 struct rhash_head __rcu
**pprev
= rht_bucket_var(old_tbl
, old_hash
);
229 struct rhash_head
*head
, *next
, *entry
;
230 spinlock_t
*new_bucket_lock
;
231 unsigned int new_hash
;
238 rht_for_each(entry
, old_tbl
, old_hash
) {
240 next
= rht_dereference_bucket(entry
->next
, old_tbl
, old_hash
);
242 if (rht_is_a_nulls(next
))
245 pprev
= &entry
->next
;
251 new_hash
= head_hashfn(ht
, new_tbl
, entry
);
253 new_bucket_lock
= rht_bucket_lock(new_tbl
, new_hash
);
255 spin_lock_nested(new_bucket_lock
, SINGLE_DEPTH_NESTING
);
256 head
= rht_dereference_bucket(new_tbl
->buckets
[new_hash
],
259 RCU_INIT_POINTER(entry
->next
, head
);
261 rcu_assign_pointer(new_tbl
->buckets
[new_hash
], entry
);
262 spin_unlock(new_bucket_lock
);
264 rcu_assign_pointer(*pprev
, next
);
270 static int rhashtable_rehash_chain(struct rhashtable
*ht
,
271 unsigned int old_hash
)
273 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
274 spinlock_t
*old_bucket_lock
;
277 old_bucket_lock
= rht_bucket_lock(old_tbl
, old_hash
);
279 spin_lock_bh(old_bucket_lock
);
280 while (!(err
= rhashtable_rehash_one(ht
, old_hash
)))
283 if (err
== -ENOENT
) {
287 spin_unlock_bh(old_bucket_lock
);
292 static int rhashtable_rehash_attach(struct rhashtable
*ht
,
293 struct bucket_table
*old_tbl
,
294 struct bucket_table
*new_tbl
)
296 /* Make insertions go into the new, empty table right away. Deletions
297 * and lookups will be attempted in both tables until we synchronize.
298 * As cmpxchg() provides strong barriers, we do not need
299 * rcu_assign_pointer().
302 if (cmpxchg(&old_tbl
->future_tbl
, NULL
, new_tbl
) != NULL
)
308 static int rhashtable_rehash_table(struct rhashtable
*ht
)
310 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
311 struct bucket_table
*new_tbl
;
312 struct rhashtable_walker
*walker
;
313 unsigned int old_hash
;
316 new_tbl
= rht_dereference(old_tbl
->future_tbl
, ht
);
320 for (old_hash
= 0; old_hash
< old_tbl
->size
; old_hash
++) {
321 err
= rhashtable_rehash_chain(ht
, old_hash
);
327 /* Publish the new table pointer. */
328 rcu_assign_pointer(ht
->tbl
, new_tbl
);
330 spin_lock(&ht
->lock
);
331 list_for_each_entry(walker
, &old_tbl
->walkers
, list
)
333 spin_unlock(&ht
->lock
);
335 /* Wait for readers. All new readers will see the new
336 * table, and thus no references to the old table will
339 call_rcu(&old_tbl
->rcu
, bucket_table_free_rcu
);
341 return rht_dereference(new_tbl
->future_tbl
, ht
) ? -EAGAIN
: 0;
344 static int rhashtable_rehash_alloc(struct rhashtable
*ht
,
345 struct bucket_table
*old_tbl
,
348 struct bucket_table
*new_tbl
;
351 ASSERT_RHT_MUTEX(ht
);
353 new_tbl
= bucket_table_alloc(ht
, size
, GFP_KERNEL
);
357 err
= rhashtable_rehash_attach(ht
, old_tbl
, new_tbl
);
359 bucket_table_free(new_tbl
);
365 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
366 * @ht: the hash table to shrink
368 * This function shrinks the hash table to fit, i.e., the smallest
369 * size would not cause it to expand right away automatically.
371 * The caller must ensure that no concurrent resizing occurs by holding
374 * The caller must ensure that no concurrent table mutations take place.
375 * It is however valid to have concurrent lookups if they are RCU protected.
377 * It is valid to have concurrent insertions and deletions protected by per
378 * bucket locks or concurrent RCU protected lookups and traversals.
380 static int rhashtable_shrink(struct rhashtable
*ht
)
382 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
383 unsigned int nelems
= atomic_read(&ht
->nelems
);
384 unsigned int size
= 0;
387 size
= roundup_pow_of_two(nelems
* 3 / 2);
388 if (size
< ht
->p
.min_size
)
389 size
= ht
->p
.min_size
;
391 if (old_tbl
->size
<= size
)
394 if (rht_dereference(old_tbl
->future_tbl
, ht
))
397 return rhashtable_rehash_alloc(ht
, old_tbl
, size
);
400 static void rht_deferred_worker(struct work_struct
*work
)
402 struct rhashtable
*ht
;
403 struct bucket_table
*tbl
;
406 ht
= container_of(work
, struct rhashtable
, run_work
);
407 mutex_lock(&ht
->mutex
);
409 tbl
= rht_dereference(ht
->tbl
, ht
);
410 tbl
= rhashtable_last_table(ht
, tbl
);
412 if (rht_grow_above_75(ht
, tbl
))
413 err
= rhashtable_rehash_alloc(ht
, tbl
, tbl
->size
* 2);
414 else if (ht
->p
.automatic_shrinking
&& rht_shrink_below_30(ht
, tbl
))
415 err
= rhashtable_shrink(ht
);
417 err
= rhashtable_rehash_alloc(ht
, tbl
, tbl
->size
);
420 err
= rhashtable_rehash_table(ht
);
422 mutex_unlock(&ht
->mutex
);
425 schedule_work(&ht
->run_work
);
428 static int rhashtable_insert_rehash(struct rhashtable
*ht
,
429 struct bucket_table
*tbl
)
431 struct bucket_table
*old_tbl
;
432 struct bucket_table
*new_tbl
;
436 old_tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
442 if (rht_grow_above_75(ht
, tbl
))
444 /* Do not schedule more than one rehash */
445 else if (old_tbl
!= tbl
)
450 new_tbl
= bucket_table_alloc(ht
, size
, GFP_ATOMIC
| __GFP_NOWARN
);
454 err
= rhashtable_rehash_attach(ht
, tbl
, new_tbl
);
456 bucket_table_free(new_tbl
);
460 schedule_work(&ht
->run_work
);
465 /* Do not fail the insert if someone else did a rehash. */
466 if (likely(rcu_access_pointer(tbl
->future_tbl
)))
469 /* Schedule async rehash to retry allocation in process context. */
471 schedule_work(&ht
->run_work
);
476 static void *rhashtable_lookup_one(struct rhashtable
*ht
,
477 struct bucket_table
*tbl
, unsigned int hash
,
478 const void *key
, struct rhash_head
*obj
)
480 struct rhashtable_compare_arg arg
= {
484 struct rhash_head __rcu
**pprev
;
485 struct rhash_head
*head
;
488 elasticity
= RHT_ELASTICITY
;
489 pprev
= rht_bucket_var(tbl
, hash
);
490 rht_for_each_continue(head
, *pprev
, tbl
, hash
) {
491 struct rhlist_head
*list
;
492 struct rhlist_head
*plist
;
497 ht
->p
.obj_cmpfn(&arg
, rht_obj(ht
, head
)) :
498 rhashtable_compare(&arg
, rht_obj(ht
, head
)))) {
504 return rht_obj(ht
, head
);
506 list
= container_of(obj
, struct rhlist_head
, rhead
);
507 plist
= container_of(head
, struct rhlist_head
, rhead
);
509 RCU_INIT_POINTER(list
->next
, plist
);
510 head
= rht_dereference_bucket(head
->next
, tbl
, hash
);
511 RCU_INIT_POINTER(list
->rhead
.next
, head
);
512 rcu_assign_pointer(*pprev
, obj
);
518 return ERR_PTR(-EAGAIN
);
520 return ERR_PTR(-ENOENT
);
523 static struct bucket_table
*rhashtable_insert_one(struct rhashtable
*ht
,
524 struct bucket_table
*tbl
,
526 struct rhash_head
*obj
,
529 struct rhash_head __rcu
**pprev
;
530 struct bucket_table
*new_tbl
;
531 struct rhash_head
*head
;
533 if (!IS_ERR_OR_NULL(data
))
534 return ERR_PTR(-EEXIST
);
536 if (PTR_ERR(data
) != -EAGAIN
&& PTR_ERR(data
) != -ENOENT
)
537 return ERR_CAST(data
);
539 new_tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
543 if (PTR_ERR(data
) != -ENOENT
)
544 return ERR_CAST(data
);
546 if (unlikely(rht_grow_above_max(ht
, tbl
)))
547 return ERR_PTR(-E2BIG
);
549 if (unlikely(rht_grow_above_100(ht
, tbl
)))
550 return ERR_PTR(-EAGAIN
);
552 pprev
= rht_bucket_insert(ht
, tbl
, hash
);
554 return ERR_PTR(-ENOMEM
);
556 head
= rht_dereference_bucket(*pprev
, tbl
, hash
);
558 RCU_INIT_POINTER(obj
->next
, head
);
560 struct rhlist_head
*list
;
562 list
= container_of(obj
, struct rhlist_head
, rhead
);
563 RCU_INIT_POINTER(list
->next
, NULL
);
566 rcu_assign_pointer(*pprev
, obj
);
568 atomic_inc(&ht
->nelems
);
569 if (rht_grow_above_75(ht
, tbl
))
570 schedule_work(&ht
->run_work
);
575 static void *rhashtable_try_insert(struct rhashtable
*ht
, const void *key
,
576 struct rhash_head
*obj
)
578 struct bucket_table
*new_tbl
;
579 struct bucket_table
*tbl
;
584 tbl
= rcu_dereference(ht
->tbl
);
586 /* All insertions must grab the oldest table containing
587 * the hashed bucket that is yet to be rehashed.
590 hash
= rht_head_hashfn(ht
, tbl
, obj
, ht
->p
);
591 lock
= rht_bucket_lock(tbl
, hash
);
594 if (tbl
->rehash
<= hash
)
597 spin_unlock_bh(lock
);
598 tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
601 data
= rhashtable_lookup_one(ht
, tbl
, hash
, key
, obj
);
602 new_tbl
= rhashtable_insert_one(ht
, tbl
, hash
, obj
, data
);
603 if (PTR_ERR(new_tbl
) != -EEXIST
)
604 data
= ERR_CAST(new_tbl
);
606 while (!IS_ERR_OR_NULL(new_tbl
)) {
608 hash
= rht_head_hashfn(ht
, tbl
, obj
, ht
->p
);
609 spin_lock_nested(rht_bucket_lock(tbl
, hash
),
610 SINGLE_DEPTH_NESTING
);
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
);
617 spin_unlock(rht_bucket_lock(tbl
, hash
));
620 spin_unlock_bh(lock
);
622 if (PTR_ERR(data
) == -EAGAIN
)
623 data
= ERR_PTR(rhashtable_insert_rehash(ht
, tbl
) ?:
629 void *rhashtable_insert_slow(struct rhashtable
*ht
, const void *key
,
630 struct rhash_head
*obj
)
636 data
= rhashtable_try_insert(ht
, key
, obj
);
638 } while (PTR_ERR(data
) == -EAGAIN
);
642 EXPORT_SYMBOL_GPL(rhashtable_insert_slow
);
645 * rhashtable_walk_enter - Initialise an iterator
646 * @ht: Table to walk over
647 * @iter: Hash table Iterator
649 * This function prepares a hash table walk.
651 * Note that if you restart a walk after rhashtable_walk_stop you
652 * may see the same object twice. Also, you may miss objects if
653 * there are removals in between rhashtable_walk_stop and the next
654 * call to rhashtable_walk_start.
656 * For a completely stable walk you should construct your own data
657 * structure outside the hash table.
659 * This function may be called from any process context, including
660 * non-preemptable context, but cannot be called from softirq or
663 * You must call rhashtable_walk_exit after this function returns.
665 void rhashtable_walk_enter(struct rhashtable
*ht
, struct rhashtable_iter
*iter
)
671 iter
->end_of_table
= 0;
673 spin_lock(&ht
->lock
);
675 rcu_dereference_protected(ht
->tbl
, lockdep_is_held(&ht
->lock
));
676 list_add(&iter
->walker
.list
, &iter
->walker
.tbl
->walkers
);
677 spin_unlock(&ht
->lock
);
679 EXPORT_SYMBOL_GPL(rhashtable_walk_enter
);
682 * rhashtable_walk_exit - Free an iterator
683 * @iter: Hash table Iterator
685 * This function frees resources allocated by rhashtable_walk_init.
687 void rhashtable_walk_exit(struct rhashtable_iter
*iter
)
689 spin_lock(&iter
->ht
->lock
);
690 if (iter
->walker
.tbl
)
691 list_del(&iter
->walker
.list
);
692 spin_unlock(&iter
->ht
->lock
);
694 EXPORT_SYMBOL_GPL(rhashtable_walk_exit
);
697 * rhashtable_walk_start_check - Start a hash table walk
698 * @iter: Hash table iterator
700 * Start a hash table walk at the current iterator position. Note that we take
701 * the RCU lock in all cases including when we return an error. So you must
702 * always call rhashtable_walk_stop to clean up.
704 * Returns zero if successful.
706 * Returns -EAGAIN if resize event occured. Note that the iterator
707 * will rewind back to the beginning and you may use it immediately
708 * by calling rhashtable_walk_next.
710 * rhashtable_walk_start is defined as an inline variant that returns
711 * void. This is preferred in cases where the caller would ignore
712 * resize events and always continue.
714 int rhashtable_walk_start_check(struct rhashtable_iter
*iter
)
717 struct rhashtable
*ht
= iter
->ht
;
718 bool rhlist
= ht
->rhlist
;
722 spin_lock(&ht
->lock
);
723 if (iter
->walker
.tbl
)
724 list_del(&iter
->walker
.list
);
725 spin_unlock(&ht
->lock
);
727 if (iter
->end_of_table
)
729 if (!iter
->walker
.tbl
) {
730 iter
->walker
.tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
736 if (iter
->p
&& !rhlist
) {
738 * We need to validate that 'p' is still in the table, and
739 * if so, update 'skip'
741 struct rhash_head
*p
;
743 rht_for_each_rcu(p
, iter
->walker
.tbl
, iter
->slot
) {
751 } else if (iter
->p
&& rhlist
) {
752 /* Need to validate that 'list' is still in the table, and
753 * if so, update 'skip' and 'p'.
755 struct rhash_head
*p
;
756 struct rhlist_head
*list
;
758 rht_for_each_rcu(p
, iter
->walker
.tbl
, iter
->slot
) {
759 for (list
= container_of(p
, struct rhlist_head
, rhead
);
761 list
= rcu_dereference(list
->next
)) {
763 if (list
== iter
->list
) {
775 EXPORT_SYMBOL_GPL(rhashtable_walk_start_check
);
778 * __rhashtable_walk_find_next - Find the next element in a table (or the first
779 * one in case of a new walk).
781 * @iter: Hash table iterator
783 * Returns the found object or NULL when the end of the table is reached.
785 * Returns -EAGAIN if resize event occurred.
787 static void *__rhashtable_walk_find_next(struct rhashtable_iter
*iter
)
789 struct bucket_table
*tbl
= iter
->walker
.tbl
;
790 struct rhlist_head
*list
= iter
->list
;
791 struct rhashtable
*ht
= iter
->ht
;
792 struct rhash_head
*p
= iter
->p
;
793 bool rhlist
= ht
->rhlist
;
798 for (; iter
->slot
< tbl
->size
; iter
->slot
++) {
799 int skip
= iter
->skip
;
801 rht_for_each_rcu(p
, tbl
, iter
->slot
) {
803 list
= container_of(p
, struct rhlist_head
,
809 list
= rcu_dereference(list
->next
);
820 if (!rht_is_a_nulls(p
)) {
824 return rht_obj(ht
, rhlist
? &list
->rhead
: p
);
832 /* Ensure we see any new tables. */
835 iter
->walker
.tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
836 if (iter
->walker
.tbl
) {
839 return ERR_PTR(-EAGAIN
);
841 iter
->end_of_table
= true;
848 * rhashtable_walk_next - Return the next object and advance the iterator
849 * @iter: Hash table iterator
851 * Note that you must call rhashtable_walk_stop when you are finished
854 * Returns the next object or NULL when the end of the table is reached.
856 * Returns -EAGAIN if resize event occurred. Note that the iterator
857 * will rewind back to the beginning and you may continue to use it.
859 void *rhashtable_walk_next(struct rhashtable_iter
*iter
)
861 struct rhlist_head
*list
= iter
->list
;
862 struct rhashtable
*ht
= iter
->ht
;
863 struct rhash_head
*p
= iter
->p
;
864 bool rhlist
= ht
->rhlist
;
867 if (!rhlist
|| !(list
= rcu_dereference(list
->next
))) {
868 p
= rcu_dereference(p
->next
);
869 list
= container_of(p
, struct rhlist_head
, rhead
);
871 if (!rht_is_a_nulls(p
)) {
875 return rht_obj(ht
, rhlist
? &list
->rhead
: p
);
878 /* At the end of this slot, switch to next one and then find
879 * next entry from that point.
885 return __rhashtable_walk_find_next(iter
);
887 EXPORT_SYMBOL_GPL(rhashtable_walk_next
);
890 * rhashtable_walk_peek - Return the next object but don't advance the iterator
891 * @iter: Hash table iterator
893 * Returns the next object or NULL when the end of the table is reached.
895 * Returns -EAGAIN if resize event occurred. Note that the iterator
896 * will rewind back to the beginning and you may continue to use it.
898 void *rhashtable_walk_peek(struct rhashtable_iter
*iter
)
900 struct rhlist_head
*list
= iter
->list
;
901 struct rhashtable
*ht
= iter
->ht
;
902 struct rhash_head
*p
= iter
->p
;
905 return rht_obj(ht
, ht
->rhlist
? &list
->rhead
: p
);
907 /* No object found in current iter, find next one in the table. */
910 /* A nonzero skip value points to the next entry in the table
911 * beyond that last one that was found. Decrement skip so
912 * we find the current value. __rhashtable_walk_find_next
913 * will restore the original value of skip assuming that
914 * the table hasn't changed.
919 return __rhashtable_walk_find_next(iter
);
921 EXPORT_SYMBOL_GPL(rhashtable_walk_peek
);
924 * rhashtable_walk_stop - Finish a hash table walk
925 * @iter: Hash table iterator
927 * Finish a hash table walk. Does not reset the iterator to the start of the
930 void rhashtable_walk_stop(struct rhashtable_iter
*iter
)
933 struct rhashtable
*ht
;
934 struct bucket_table
*tbl
= iter
->walker
.tbl
;
941 spin_lock(&ht
->lock
);
942 if (tbl
->rehash
< tbl
->size
)
943 list_add(&iter
->walker
.list
, &tbl
->walkers
);
945 iter
->walker
.tbl
= NULL
;
946 spin_unlock(&ht
->lock
);
951 EXPORT_SYMBOL_GPL(rhashtable_walk_stop
);
953 static size_t rounded_hashtable_size(const struct rhashtable_params
*params
)
957 if (params
->nelem_hint
)
958 retsize
= max(roundup_pow_of_two(params
->nelem_hint
* 4 / 3),
959 (unsigned long)params
->min_size
);
961 retsize
= max(HASH_DEFAULT_SIZE
,
962 (unsigned long)params
->min_size
);
967 static u32
rhashtable_jhash2(const void *key
, u32 length
, u32 seed
)
969 return jhash2(key
, length
, seed
);
973 * rhashtable_init - initialize a new hash table
974 * @ht: hash table to be initialized
975 * @params: configuration parameters
977 * Initializes a new hash table based on the provided configuration
978 * parameters. A table can be configured either with a variable or
981 * Configuration Example 1: Fixed length keys
985 * struct rhash_head node;
988 * struct rhashtable_params params = {
989 * .head_offset = offsetof(struct test_obj, node),
990 * .key_offset = offsetof(struct test_obj, key),
991 * .key_len = sizeof(int),
995 * Configuration Example 2: Variable length keys
998 * struct rhash_head node;
1001 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
1003 * struct test_obj *obj = data;
1005 * return [... hash ...];
1008 * struct rhashtable_params params = {
1009 * .head_offset = offsetof(struct test_obj, node),
1011 * .obj_hashfn = my_hash_fn,
1014 int rhashtable_init(struct rhashtable
*ht
,
1015 const struct rhashtable_params
*params
)
1017 struct bucket_table
*tbl
;
1020 if ((!params
->key_len
&& !params
->obj_hashfn
) ||
1021 (params
->obj_hashfn
&& !params
->obj_cmpfn
))
1024 memset(ht
, 0, sizeof(*ht
));
1025 mutex_init(&ht
->mutex
);
1026 spin_lock_init(&ht
->lock
);
1027 memcpy(&ht
->p
, params
, sizeof(*params
));
1029 if (params
->min_size
)
1030 ht
->p
.min_size
= roundup_pow_of_two(params
->min_size
);
1032 /* Cap total entries at 2^31 to avoid nelems overflow. */
1033 ht
->max_elems
= 1u << 31;
1035 if (params
->max_size
) {
1036 ht
->p
.max_size
= rounddown_pow_of_two(params
->max_size
);
1037 if (ht
->p
.max_size
< ht
->max_elems
/ 2)
1038 ht
->max_elems
= ht
->p
.max_size
* 2;
1041 ht
->p
.min_size
= max_t(u16
, ht
->p
.min_size
, HASH_MIN_SIZE
);
1043 size
= rounded_hashtable_size(&ht
->p
);
1045 if (params
->locks_mul
)
1046 ht
->p
.locks_mul
= roundup_pow_of_two(params
->locks_mul
);
1048 ht
->p
.locks_mul
= BUCKET_LOCKS_PER_CPU
;
1050 ht
->key_len
= ht
->p
.key_len
;
1051 if (!params
->hashfn
) {
1052 ht
->p
.hashfn
= jhash
;
1054 if (!(ht
->key_len
& (sizeof(u32
) - 1))) {
1055 ht
->key_len
/= sizeof(u32
);
1056 ht
->p
.hashfn
= rhashtable_jhash2
;
1061 * This is api initialization and thus we need to guarantee the
1062 * initial rhashtable allocation. Upon failure, retry with the
1063 * smallest possible size with __GFP_NOFAIL semantics.
1065 tbl
= bucket_table_alloc(ht
, size
, GFP_KERNEL
);
1066 if (unlikely(tbl
== NULL
)) {
1067 size
= max_t(u16
, ht
->p
.min_size
, HASH_MIN_SIZE
);
1068 tbl
= bucket_table_alloc(ht
, size
, GFP_KERNEL
| __GFP_NOFAIL
);
1071 atomic_set(&ht
->nelems
, 0);
1073 RCU_INIT_POINTER(ht
->tbl
, tbl
);
1075 INIT_WORK(&ht
->run_work
, rht_deferred_worker
);
1079 EXPORT_SYMBOL_GPL(rhashtable_init
);
1082 * rhltable_init - initialize a new hash list table
1083 * @hlt: hash list table to be initialized
1084 * @params: configuration parameters
1086 * Initializes a new hash list table.
1088 * See documentation for rhashtable_init.
1090 int rhltable_init(struct rhltable
*hlt
, const struct rhashtable_params
*params
)
1094 err
= rhashtable_init(&hlt
->ht
, params
);
1095 hlt
->ht
.rhlist
= true;
1098 EXPORT_SYMBOL_GPL(rhltable_init
);
1100 static void rhashtable_free_one(struct rhashtable
*ht
, struct rhash_head
*obj
,
1101 void (*free_fn
)(void *ptr
, void *arg
),
1104 struct rhlist_head
*list
;
1107 free_fn(rht_obj(ht
, obj
), arg
);
1111 list
= container_of(obj
, struct rhlist_head
, rhead
);
1114 list
= rht_dereference(list
->next
, ht
);
1115 free_fn(rht_obj(ht
, obj
), arg
);
1120 * rhashtable_free_and_destroy - free elements and destroy hash table
1121 * @ht: the hash table to destroy
1122 * @free_fn: callback to release resources of element
1123 * @arg: pointer passed to free_fn
1125 * Stops an eventual async resize. If defined, invokes free_fn for each
1126 * element to releasal resources. Please note that RCU protected
1127 * readers may still be accessing the elements. Releasing of resources
1128 * must occur in a compatible manner. Then frees the bucket array.
1130 * This function will eventually sleep to wait for an async resize
1131 * to complete. The caller is responsible that no further write operations
1132 * occurs in parallel.
1134 void rhashtable_free_and_destroy(struct rhashtable
*ht
,
1135 void (*free_fn
)(void *ptr
, void *arg
),
1138 struct bucket_table
*tbl
, *next_tbl
;
1141 cancel_work_sync(&ht
->run_work
);
1143 mutex_lock(&ht
->mutex
);
1144 tbl
= rht_dereference(ht
->tbl
, ht
);
1147 for (i
= 0; i
< tbl
->size
; i
++) {
1148 struct rhash_head
*pos
, *next
;
1151 for (pos
= rht_dereference(*rht_bucket(tbl
, i
), ht
),
1152 next
= !rht_is_a_nulls(pos
) ?
1153 rht_dereference(pos
->next
, ht
) : NULL
;
1154 !rht_is_a_nulls(pos
);
1156 next
= !rht_is_a_nulls(pos
) ?
1157 rht_dereference(pos
->next
, ht
) : NULL
)
1158 rhashtable_free_one(ht
, pos
, free_fn
, arg
);
1162 next_tbl
= rht_dereference(tbl
->future_tbl
, ht
);
1163 bucket_table_free(tbl
);
1168 mutex_unlock(&ht
->mutex
);
1170 EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy
);
1172 void rhashtable_destroy(struct rhashtable
*ht
)
1174 return rhashtable_free_and_destroy(ht
, NULL
, NULL
);
1176 EXPORT_SYMBOL_GPL(rhashtable_destroy
);
1178 struct rhash_head __rcu
**rht_bucket_nested(const struct bucket_table
*tbl
,
1181 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
1182 static struct rhash_head __rcu
*rhnull
=
1183 (struct rhash_head __rcu
*)NULLS_MARKER(0);
1184 unsigned int index
= hash
& ((1 << tbl
->nest
) - 1);
1185 unsigned int size
= tbl
->size
>> tbl
->nest
;
1186 unsigned int subhash
= hash
;
1187 union nested_table
*ntbl
;
1189 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
1190 ntbl
= rht_dereference_bucket_rcu(ntbl
[index
].table
, tbl
, hash
);
1191 subhash
>>= tbl
->nest
;
1193 while (ntbl
&& size
> (1 << shift
)) {
1194 index
= subhash
& ((1 << shift
) - 1);
1195 ntbl
= rht_dereference_bucket_rcu(ntbl
[index
].table
,
1204 return &ntbl
[subhash
].bucket
;
1207 EXPORT_SYMBOL_GPL(rht_bucket_nested
);
1209 struct rhash_head __rcu
**rht_bucket_nested_insert(struct rhashtable
*ht
,
1210 struct bucket_table
*tbl
,
1213 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
1214 unsigned int index
= hash
& ((1 << tbl
->nest
) - 1);
1215 unsigned int size
= tbl
->size
>> tbl
->nest
;
1216 union nested_table
*ntbl
;
1218 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
1220 ntbl
= nested_table_alloc(ht
, &ntbl
[index
].table
,
1221 size
<= (1 << shift
));
1223 while (ntbl
&& size
> (1 << shift
)) {
1224 index
= hash
& ((1 << shift
) - 1);
1227 ntbl
= nested_table_alloc(ht
, &ntbl
[index
].table
,
1228 size
<= (1 << shift
));
1234 return &ntbl
[hash
].bucket
;
1237 EXPORT_SYMBOL_GPL(rht_bucket_nested_insert
);