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>
31 #include <linux/rhashtable.h>
33 #define HASH_DEFAULT_SIZE 64UL
34 #define HASH_MIN_SIZE 4U
35 #define BUCKET_LOCKS_PER_CPU 32UL
38 union nested_table __rcu
*table
;
39 struct rhash_head __rcu
*bucket
;
42 static u32
head_hashfn(struct rhashtable
*ht
,
43 const struct bucket_table
*tbl
,
44 const struct rhash_head
*he
)
46 return rht_head_hashfn(ht
, tbl
, he
, ht
->p
);
49 #ifdef CONFIG_PROVE_LOCKING
50 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
52 int lockdep_rht_mutex_is_held(struct rhashtable
*ht
)
54 return (debug_locks
) ? lockdep_is_held(&ht
->mutex
) : 1;
56 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held
);
58 int lockdep_rht_bucket_is_held(const struct bucket_table
*tbl
, u32 hash
)
60 spinlock_t
*lock
= rht_bucket_lock(tbl
, hash
);
62 return (debug_locks
) ? lockdep_is_held(lock
) : 1;
64 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held
);
66 #define ASSERT_RHT_MUTEX(HT)
69 static void nested_table_free(union nested_table
*ntbl
, unsigned int size
)
71 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
72 const unsigned int len
= 1 << shift
;
75 ntbl
= rcu_dereference_raw(ntbl
->table
);
81 for (i
= 0; i
< len
; i
++)
82 nested_table_free(ntbl
+ i
, size
);
88 static void nested_bucket_table_free(const struct bucket_table
*tbl
)
90 unsigned int size
= tbl
->size
>> tbl
->nest
;
91 unsigned int len
= 1 << tbl
->nest
;
92 union nested_table
*ntbl
;
95 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
97 for (i
= 0; i
< len
; i
++)
98 nested_table_free(ntbl
+ i
, size
);
103 static void bucket_table_free(const struct bucket_table
*tbl
)
106 nested_bucket_table_free(tbl
);
108 free_bucket_spinlocks(tbl
->locks
);
112 static void bucket_table_free_rcu(struct rcu_head
*head
)
114 bucket_table_free(container_of(head
, struct bucket_table
, rcu
));
117 static union nested_table
*nested_table_alloc(struct rhashtable
*ht
,
118 union nested_table __rcu
**prev
,
121 union nested_table
*ntbl
;
124 ntbl
= rcu_dereference(*prev
);
128 ntbl
= kzalloc(PAGE_SIZE
, GFP_ATOMIC
);
131 for (i
= 0; i
< PAGE_SIZE
/ sizeof(ntbl
[0]); i
++)
132 INIT_RHT_NULLS_HEAD(ntbl
[i
].bucket
);
135 rcu_assign_pointer(*prev
, ntbl
);
140 static struct bucket_table
*nested_bucket_table_alloc(struct rhashtable
*ht
,
144 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
145 struct bucket_table
*tbl
;
148 if (nbuckets
< (1 << (shift
+ 1)))
151 size
= sizeof(*tbl
) + sizeof(tbl
->buckets
[0]);
153 tbl
= kzalloc(size
, gfp
);
157 if (!nested_table_alloc(ht
, (union nested_table __rcu
**)tbl
->buckets
,
163 tbl
->nest
= (ilog2(nbuckets
) - 1) % shift
+ 1;
168 static struct bucket_table
*bucket_table_alloc(struct rhashtable
*ht
,
172 struct bucket_table
*tbl
= NULL
;
173 size_t size
, max_locks
;
176 size
= sizeof(*tbl
) + nbuckets
* sizeof(tbl
->buckets
[0]);
177 if (gfp
!= GFP_KERNEL
)
178 tbl
= kzalloc(size
, gfp
| __GFP_NOWARN
| __GFP_NORETRY
);
180 tbl
= kvzalloc(size
, gfp
);
184 if (tbl
== NULL
&& gfp
!= GFP_KERNEL
) {
185 tbl
= nested_bucket_table_alloc(ht
, nbuckets
, gfp
);
193 max_locks
= size
>> 1;
195 max_locks
= min_t(size_t, max_locks
, 1U << tbl
->nest
);
197 if (alloc_bucket_spinlocks(&tbl
->locks
, &tbl
->locks_mask
, max_locks
,
198 ht
->p
.locks_mul
, gfp
) < 0) {
199 bucket_table_free(tbl
);
203 INIT_LIST_HEAD(&tbl
->walkers
);
205 tbl
->hash_rnd
= get_random_u32();
207 for (i
= 0; i
< nbuckets
; i
++)
208 INIT_RHT_NULLS_HEAD(tbl
->buckets
[i
]);
213 static struct bucket_table
*rhashtable_last_table(struct rhashtable
*ht
,
214 struct bucket_table
*tbl
)
216 struct bucket_table
*new_tbl
;
220 tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
226 static int rhashtable_rehash_one(struct rhashtable
*ht
, unsigned int old_hash
)
228 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
229 struct bucket_table
*new_tbl
= rhashtable_last_table(ht
, old_tbl
);
230 struct rhash_head __rcu
**pprev
= rht_bucket_var(old_tbl
, old_hash
);
232 struct rhash_head
*head
, *next
, *entry
;
233 spinlock_t
*new_bucket_lock
;
234 unsigned int new_hash
;
241 rht_for_each(entry
, old_tbl
, old_hash
) {
243 next
= rht_dereference_bucket(entry
->next
, old_tbl
, old_hash
);
245 if (rht_is_a_nulls(next
))
248 pprev
= &entry
->next
;
254 new_hash
= head_hashfn(ht
, new_tbl
, entry
);
256 new_bucket_lock
= rht_bucket_lock(new_tbl
, new_hash
);
258 spin_lock_nested(new_bucket_lock
, SINGLE_DEPTH_NESTING
);
259 head
= rht_dereference_bucket(new_tbl
->buckets
[new_hash
],
262 RCU_INIT_POINTER(entry
->next
, head
);
264 rcu_assign_pointer(new_tbl
->buckets
[new_hash
], entry
);
265 spin_unlock(new_bucket_lock
);
267 rcu_assign_pointer(*pprev
, next
);
273 static int rhashtable_rehash_chain(struct rhashtable
*ht
,
274 unsigned int old_hash
)
276 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
277 spinlock_t
*old_bucket_lock
;
280 old_bucket_lock
= rht_bucket_lock(old_tbl
, old_hash
);
282 spin_lock_bh(old_bucket_lock
);
283 while (!(err
= rhashtable_rehash_one(ht
, old_hash
)))
286 if (err
== -ENOENT
) {
290 spin_unlock_bh(old_bucket_lock
);
295 static int rhashtable_rehash_attach(struct rhashtable
*ht
,
296 struct bucket_table
*old_tbl
,
297 struct bucket_table
*new_tbl
)
299 /* Make insertions go into the new, empty table right away. Deletions
300 * and lookups will be attempted in both tables until we synchronize.
301 * As cmpxchg() provides strong barriers, we do not need
302 * rcu_assign_pointer().
305 if (cmpxchg(&old_tbl
->future_tbl
, NULL
, new_tbl
) != NULL
)
311 static int rhashtable_rehash_table(struct rhashtable
*ht
)
313 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
314 struct bucket_table
*new_tbl
;
315 struct rhashtable_walker
*walker
;
316 unsigned int old_hash
;
319 new_tbl
= rht_dereference(old_tbl
->future_tbl
, ht
);
323 for (old_hash
= 0; old_hash
< old_tbl
->size
; old_hash
++) {
324 err
= rhashtable_rehash_chain(ht
, old_hash
);
330 /* Publish the new table pointer. */
331 rcu_assign_pointer(ht
->tbl
, new_tbl
);
333 spin_lock(&ht
->lock
);
334 list_for_each_entry(walker
, &old_tbl
->walkers
, list
)
336 spin_unlock(&ht
->lock
);
338 /* Wait for readers. All new readers will see the new
339 * table, and thus no references to the old table will
342 call_rcu(&old_tbl
->rcu
, bucket_table_free_rcu
);
344 return rht_dereference(new_tbl
->future_tbl
, ht
) ? -EAGAIN
: 0;
347 static int rhashtable_rehash_alloc(struct rhashtable
*ht
,
348 struct bucket_table
*old_tbl
,
351 struct bucket_table
*new_tbl
;
354 ASSERT_RHT_MUTEX(ht
);
356 new_tbl
= bucket_table_alloc(ht
, size
, GFP_KERNEL
);
360 err
= rhashtable_rehash_attach(ht
, old_tbl
, new_tbl
);
362 bucket_table_free(new_tbl
);
368 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
369 * @ht: the hash table to shrink
371 * This function shrinks the hash table to fit, i.e., the smallest
372 * size would not cause it to expand right away automatically.
374 * The caller must ensure that no concurrent resizing occurs by holding
377 * The caller must ensure that no concurrent table mutations take place.
378 * It is however valid to have concurrent lookups if they are RCU protected.
380 * It is valid to have concurrent insertions and deletions protected by per
381 * bucket locks or concurrent RCU protected lookups and traversals.
383 static int rhashtable_shrink(struct rhashtable
*ht
)
385 struct bucket_table
*old_tbl
= rht_dereference(ht
->tbl
, ht
);
386 unsigned int nelems
= atomic_read(&ht
->nelems
);
387 unsigned int size
= 0;
390 size
= roundup_pow_of_two(nelems
* 3 / 2);
391 if (size
< ht
->p
.min_size
)
392 size
= ht
->p
.min_size
;
394 if (old_tbl
->size
<= size
)
397 if (rht_dereference(old_tbl
->future_tbl
, ht
))
400 return rhashtable_rehash_alloc(ht
, old_tbl
, size
);
403 static void rht_deferred_worker(struct work_struct
*work
)
405 struct rhashtable
*ht
;
406 struct bucket_table
*tbl
;
409 ht
= container_of(work
, struct rhashtable
, run_work
);
410 mutex_lock(&ht
->mutex
);
412 tbl
= rht_dereference(ht
->tbl
, ht
);
413 tbl
= rhashtable_last_table(ht
, tbl
);
415 if (rht_grow_above_75(ht
, tbl
))
416 err
= rhashtable_rehash_alloc(ht
, tbl
, tbl
->size
* 2);
417 else if (ht
->p
.automatic_shrinking
&& rht_shrink_below_30(ht
, tbl
))
418 err
= rhashtable_shrink(ht
);
420 err
= rhashtable_rehash_alloc(ht
, tbl
, tbl
->size
);
423 err
= rhashtable_rehash_table(ht
);
425 mutex_unlock(&ht
->mutex
);
428 schedule_work(&ht
->run_work
);
431 static int rhashtable_insert_rehash(struct rhashtable
*ht
,
432 struct bucket_table
*tbl
)
434 struct bucket_table
*old_tbl
;
435 struct bucket_table
*new_tbl
;
439 old_tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
445 if (rht_grow_above_75(ht
, tbl
))
447 /* Do not schedule more than one rehash */
448 else if (old_tbl
!= tbl
)
453 new_tbl
= bucket_table_alloc(ht
, size
, GFP_ATOMIC
);
457 err
= rhashtable_rehash_attach(ht
, tbl
, new_tbl
);
459 bucket_table_free(new_tbl
);
463 schedule_work(&ht
->run_work
);
468 /* Do not fail the insert if someone else did a rehash. */
469 if (likely(rcu_access_pointer(tbl
->future_tbl
)))
472 /* Schedule async rehash to retry allocation in process context. */
474 schedule_work(&ht
->run_work
);
479 static void *rhashtable_lookup_one(struct rhashtable
*ht
,
480 struct bucket_table
*tbl
, unsigned int hash
,
481 const void *key
, struct rhash_head
*obj
)
483 struct rhashtable_compare_arg arg
= {
487 struct rhash_head __rcu
**pprev
;
488 struct rhash_head
*head
;
491 elasticity
= RHT_ELASTICITY
;
492 pprev
= rht_bucket_var(tbl
, hash
);
493 rht_for_each_continue(head
, *pprev
, tbl
, hash
) {
494 struct rhlist_head
*list
;
495 struct rhlist_head
*plist
;
500 ht
->p
.obj_cmpfn(&arg
, rht_obj(ht
, head
)) :
501 rhashtable_compare(&arg
, rht_obj(ht
, head
)))) {
507 return rht_obj(ht
, head
);
509 list
= container_of(obj
, struct rhlist_head
, rhead
);
510 plist
= container_of(head
, struct rhlist_head
, rhead
);
512 RCU_INIT_POINTER(list
->next
, plist
);
513 head
= rht_dereference_bucket(head
->next
, tbl
, hash
);
514 RCU_INIT_POINTER(list
->rhead
.next
, head
);
515 rcu_assign_pointer(*pprev
, obj
);
521 return ERR_PTR(-EAGAIN
);
523 return ERR_PTR(-ENOENT
);
526 static struct bucket_table
*rhashtable_insert_one(struct rhashtable
*ht
,
527 struct bucket_table
*tbl
,
529 struct rhash_head
*obj
,
532 struct rhash_head __rcu
**pprev
;
533 struct bucket_table
*new_tbl
;
534 struct rhash_head
*head
;
536 if (!IS_ERR_OR_NULL(data
))
537 return ERR_PTR(-EEXIST
);
539 if (PTR_ERR(data
) != -EAGAIN
&& PTR_ERR(data
) != -ENOENT
)
540 return ERR_CAST(data
);
542 new_tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
546 if (PTR_ERR(data
) != -ENOENT
)
547 return ERR_CAST(data
);
549 if (unlikely(rht_grow_above_max(ht
, tbl
)))
550 return ERR_PTR(-E2BIG
);
552 if (unlikely(rht_grow_above_100(ht
, tbl
)))
553 return ERR_PTR(-EAGAIN
);
555 pprev
= rht_bucket_insert(ht
, tbl
, hash
);
557 return ERR_PTR(-ENOMEM
);
559 head
= rht_dereference_bucket(*pprev
, tbl
, hash
);
561 RCU_INIT_POINTER(obj
->next
, head
);
563 struct rhlist_head
*list
;
565 list
= container_of(obj
, struct rhlist_head
, rhead
);
566 RCU_INIT_POINTER(list
->next
, NULL
);
569 rcu_assign_pointer(*pprev
, obj
);
571 atomic_inc(&ht
->nelems
);
572 if (rht_grow_above_75(ht
, tbl
))
573 schedule_work(&ht
->run_work
);
578 static void *rhashtable_try_insert(struct rhashtable
*ht
, const void *key
,
579 struct rhash_head
*obj
)
581 struct bucket_table
*new_tbl
;
582 struct bucket_table
*tbl
;
587 tbl
= rcu_dereference(ht
->tbl
);
589 /* All insertions must grab the oldest table containing
590 * the hashed bucket that is yet to be rehashed.
593 hash
= rht_head_hashfn(ht
, tbl
, obj
, ht
->p
);
594 lock
= rht_bucket_lock(tbl
, hash
);
597 if (tbl
->rehash
<= hash
)
600 spin_unlock_bh(lock
);
601 tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
604 data
= rhashtable_lookup_one(ht
, tbl
, hash
, key
, obj
);
605 new_tbl
= rhashtable_insert_one(ht
, tbl
, hash
, obj
, data
);
606 if (PTR_ERR(new_tbl
) != -EEXIST
)
607 data
= ERR_CAST(new_tbl
);
609 while (!IS_ERR_OR_NULL(new_tbl
)) {
611 hash
= rht_head_hashfn(ht
, tbl
, obj
, ht
->p
);
612 spin_lock_nested(rht_bucket_lock(tbl
, hash
),
613 SINGLE_DEPTH_NESTING
);
615 data
= rhashtable_lookup_one(ht
, tbl
, hash
, key
, obj
);
616 new_tbl
= rhashtable_insert_one(ht
, tbl
, hash
, obj
, data
);
617 if (PTR_ERR(new_tbl
) != -EEXIST
)
618 data
= ERR_CAST(new_tbl
);
620 spin_unlock(rht_bucket_lock(tbl
, hash
));
623 spin_unlock_bh(lock
);
625 if (PTR_ERR(data
) == -EAGAIN
)
626 data
= ERR_PTR(rhashtable_insert_rehash(ht
, tbl
) ?:
632 void *rhashtable_insert_slow(struct rhashtable
*ht
, const void *key
,
633 struct rhash_head
*obj
)
639 data
= rhashtable_try_insert(ht
, key
, obj
);
641 } while (PTR_ERR(data
) == -EAGAIN
);
645 EXPORT_SYMBOL_GPL(rhashtable_insert_slow
);
648 * rhashtable_walk_enter - Initialise an iterator
649 * @ht: Table to walk over
650 * @iter: Hash table Iterator
652 * This function prepares a hash table walk.
654 * Note that if you restart a walk after rhashtable_walk_stop you
655 * may see the same object twice. Also, you may miss objects if
656 * there are removals in between rhashtable_walk_stop and the next
657 * call to rhashtable_walk_start.
659 * For a completely stable walk you should construct your own data
660 * structure outside the hash table.
662 * This function may be called from any process context, including
663 * non-preemptable context, but cannot be called from softirq or
666 * You must call rhashtable_walk_exit after this function returns.
668 void rhashtable_walk_enter(struct rhashtable
*ht
, struct rhashtable_iter
*iter
)
674 iter
->end_of_table
= 0;
676 spin_lock(&ht
->lock
);
678 rcu_dereference_protected(ht
->tbl
, lockdep_is_held(&ht
->lock
));
679 list_add(&iter
->walker
.list
, &iter
->walker
.tbl
->walkers
);
680 spin_unlock(&ht
->lock
);
682 EXPORT_SYMBOL_GPL(rhashtable_walk_enter
);
685 * rhashtable_walk_exit - Free an iterator
686 * @iter: Hash table Iterator
688 * This function frees resources allocated by rhashtable_walk_init.
690 void rhashtable_walk_exit(struct rhashtable_iter
*iter
)
692 spin_lock(&iter
->ht
->lock
);
693 if (iter
->walker
.tbl
)
694 list_del(&iter
->walker
.list
);
695 spin_unlock(&iter
->ht
->lock
);
697 EXPORT_SYMBOL_GPL(rhashtable_walk_exit
);
700 * rhashtable_walk_start_check - Start a hash table walk
701 * @iter: Hash table iterator
703 * Start a hash table walk at the current iterator position. Note that we take
704 * the RCU lock in all cases including when we return an error. So you must
705 * always call rhashtable_walk_stop to clean up.
707 * Returns zero if successful.
709 * Returns -EAGAIN if resize event occured. Note that the iterator
710 * will rewind back to the beginning and you may use it immediately
711 * by calling rhashtable_walk_next.
713 * rhashtable_walk_start is defined as an inline variant that returns
714 * void. This is preferred in cases where the caller would ignore
715 * resize events and always continue.
717 int rhashtable_walk_start_check(struct rhashtable_iter
*iter
)
720 struct rhashtable
*ht
= iter
->ht
;
721 bool rhlist
= ht
->rhlist
;
725 spin_lock(&ht
->lock
);
726 if (iter
->walker
.tbl
)
727 list_del(&iter
->walker
.list
);
728 spin_unlock(&ht
->lock
);
730 if (iter
->end_of_table
)
732 if (!iter
->walker
.tbl
) {
733 iter
->walker
.tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
739 if (iter
->p
&& !rhlist
) {
741 * We need to validate that 'p' is still in the table, and
742 * if so, update 'skip'
744 struct rhash_head
*p
;
746 rht_for_each_rcu(p
, iter
->walker
.tbl
, iter
->slot
) {
754 } else if (iter
->p
&& rhlist
) {
755 /* Need to validate that 'list' is still in the table, and
756 * if so, update 'skip' and 'p'.
758 struct rhash_head
*p
;
759 struct rhlist_head
*list
;
761 rht_for_each_rcu(p
, iter
->walker
.tbl
, iter
->slot
) {
762 for (list
= container_of(p
, struct rhlist_head
, rhead
);
764 list
= rcu_dereference(list
->next
)) {
766 if (list
== iter
->list
) {
778 EXPORT_SYMBOL_GPL(rhashtable_walk_start_check
);
781 * __rhashtable_walk_find_next - Find the next element in a table (or the first
782 * one in case of a new walk).
784 * @iter: Hash table iterator
786 * Returns the found object or NULL when the end of the table is reached.
788 * Returns -EAGAIN if resize event occurred.
790 static void *__rhashtable_walk_find_next(struct rhashtable_iter
*iter
)
792 struct bucket_table
*tbl
= iter
->walker
.tbl
;
793 struct rhlist_head
*list
= iter
->list
;
794 struct rhashtable
*ht
= iter
->ht
;
795 struct rhash_head
*p
= iter
->p
;
796 bool rhlist
= ht
->rhlist
;
801 for (; iter
->slot
< tbl
->size
; iter
->slot
++) {
802 int skip
= iter
->skip
;
804 rht_for_each_rcu(p
, tbl
, iter
->slot
) {
806 list
= container_of(p
, struct rhlist_head
,
812 list
= rcu_dereference(list
->next
);
823 if (!rht_is_a_nulls(p
)) {
827 return rht_obj(ht
, rhlist
? &list
->rhead
: p
);
835 /* Ensure we see any new tables. */
838 iter
->walker
.tbl
= rht_dereference_rcu(tbl
->future_tbl
, ht
);
839 if (iter
->walker
.tbl
) {
842 return ERR_PTR(-EAGAIN
);
844 iter
->end_of_table
= true;
851 * rhashtable_walk_next - Return the next object and advance the iterator
852 * @iter: Hash table iterator
854 * Note that you must call rhashtable_walk_stop when you are finished
857 * Returns the next object or NULL when the end of the table is reached.
859 * Returns -EAGAIN if resize event occurred. Note that the iterator
860 * will rewind back to the beginning and you may continue to use it.
862 void *rhashtable_walk_next(struct rhashtable_iter
*iter
)
864 struct rhlist_head
*list
= iter
->list
;
865 struct rhashtable
*ht
= iter
->ht
;
866 struct rhash_head
*p
= iter
->p
;
867 bool rhlist
= ht
->rhlist
;
870 if (!rhlist
|| !(list
= rcu_dereference(list
->next
))) {
871 p
= rcu_dereference(p
->next
);
872 list
= container_of(p
, struct rhlist_head
, rhead
);
874 if (!rht_is_a_nulls(p
)) {
878 return rht_obj(ht
, rhlist
? &list
->rhead
: p
);
881 /* At the end of this slot, switch to next one and then find
882 * next entry from that point.
888 return __rhashtable_walk_find_next(iter
);
890 EXPORT_SYMBOL_GPL(rhashtable_walk_next
);
893 * rhashtable_walk_peek - Return the next object but don't advance the iterator
894 * @iter: Hash table iterator
896 * Returns the next object or NULL when the end of the table is reached.
898 * Returns -EAGAIN if resize event occurred. Note that the iterator
899 * will rewind back to the beginning and you may continue to use it.
901 void *rhashtable_walk_peek(struct rhashtable_iter
*iter
)
903 struct rhlist_head
*list
= iter
->list
;
904 struct rhashtable
*ht
= iter
->ht
;
905 struct rhash_head
*p
= iter
->p
;
908 return rht_obj(ht
, ht
->rhlist
? &list
->rhead
: p
);
910 /* No object found in current iter, find next one in the table. */
913 /* A nonzero skip value points to the next entry in the table
914 * beyond that last one that was found. Decrement skip so
915 * we find the current value. __rhashtable_walk_find_next
916 * will restore the original value of skip assuming that
917 * the table hasn't changed.
922 return __rhashtable_walk_find_next(iter
);
924 EXPORT_SYMBOL_GPL(rhashtable_walk_peek
);
927 * rhashtable_walk_stop - Finish a hash table walk
928 * @iter: Hash table iterator
930 * Finish a hash table walk. Does not reset the iterator to the start of the
933 void rhashtable_walk_stop(struct rhashtable_iter
*iter
)
936 struct rhashtable
*ht
;
937 struct bucket_table
*tbl
= iter
->walker
.tbl
;
944 spin_lock(&ht
->lock
);
945 if (tbl
->rehash
< tbl
->size
)
946 list_add(&iter
->walker
.list
, &tbl
->walkers
);
948 iter
->walker
.tbl
= NULL
;
949 spin_unlock(&ht
->lock
);
954 EXPORT_SYMBOL_GPL(rhashtable_walk_stop
);
956 static size_t rounded_hashtable_size(const struct rhashtable_params
*params
)
960 if (params
->nelem_hint
)
961 retsize
= max(roundup_pow_of_two(params
->nelem_hint
* 4 / 3),
962 (unsigned long)params
->min_size
);
964 retsize
= max(HASH_DEFAULT_SIZE
,
965 (unsigned long)params
->min_size
);
970 static u32
rhashtable_jhash2(const void *key
, u32 length
, u32 seed
)
972 return jhash2(key
, length
, seed
);
976 * rhashtable_init - initialize a new hash table
977 * @ht: hash table to be initialized
978 * @params: configuration parameters
980 * Initializes a new hash table based on the provided configuration
981 * parameters. A table can be configured either with a variable or
984 * Configuration Example 1: Fixed length keys
988 * struct rhash_head node;
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),
998 * Configuration Example 2: Variable length keys
1001 * struct rhash_head node;
1004 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
1006 * struct test_obj *obj = data;
1008 * return [... hash ...];
1011 * struct rhashtable_params params = {
1012 * .head_offset = offsetof(struct test_obj, node),
1014 * .obj_hashfn = my_hash_fn,
1017 int rhashtable_init(struct rhashtable
*ht
,
1018 const struct rhashtable_params
*params
)
1020 struct bucket_table
*tbl
;
1023 if ((!params
->key_len
&& !params
->obj_hashfn
) ||
1024 (params
->obj_hashfn
&& !params
->obj_cmpfn
))
1027 memset(ht
, 0, sizeof(*ht
));
1028 mutex_init(&ht
->mutex
);
1029 spin_lock_init(&ht
->lock
);
1030 memcpy(&ht
->p
, params
, sizeof(*params
));
1032 if (params
->min_size
)
1033 ht
->p
.min_size
= roundup_pow_of_two(params
->min_size
);
1035 /* Cap total entries at 2^31 to avoid nelems overflow. */
1036 ht
->max_elems
= 1u << 31;
1038 if (params
->max_size
) {
1039 ht
->p
.max_size
= rounddown_pow_of_two(params
->max_size
);
1040 if (ht
->p
.max_size
< ht
->max_elems
/ 2)
1041 ht
->max_elems
= ht
->p
.max_size
* 2;
1044 ht
->p
.min_size
= max_t(u16
, ht
->p
.min_size
, HASH_MIN_SIZE
);
1046 size
= rounded_hashtable_size(&ht
->p
);
1048 if (params
->locks_mul
)
1049 ht
->p
.locks_mul
= roundup_pow_of_two(params
->locks_mul
);
1051 ht
->p
.locks_mul
= BUCKET_LOCKS_PER_CPU
;
1053 ht
->key_len
= ht
->p
.key_len
;
1054 if (!params
->hashfn
) {
1055 ht
->p
.hashfn
= jhash
;
1057 if (!(ht
->key_len
& (sizeof(u32
) - 1))) {
1058 ht
->key_len
/= sizeof(u32
);
1059 ht
->p
.hashfn
= rhashtable_jhash2
;
1063 tbl
= bucket_table_alloc(ht
, size
, GFP_KERNEL
);
1067 atomic_set(&ht
->nelems
, 0);
1069 RCU_INIT_POINTER(ht
->tbl
, tbl
);
1071 INIT_WORK(&ht
->run_work
, rht_deferred_worker
);
1075 EXPORT_SYMBOL_GPL(rhashtable_init
);
1078 * rhltable_init - initialize a new hash list table
1079 * @hlt: hash list table to be initialized
1080 * @params: configuration parameters
1082 * Initializes a new hash list table.
1084 * See documentation for rhashtable_init.
1086 int rhltable_init(struct rhltable
*hlt
, const struct rhashtable_params
*params
)
1090 err
= rhashtable_init(&hlt
->ht
, params
);
1091 hlt
->ht
.rhlist
= true;
1094 EXPORT_SYMBOL_GPL(rhltable_init
);
1096 static void rhashtable_free_one(struct rhashtable
*ht
, struct rhash_head
*obj
,
1097 void (*free_fn
)(void *ptr
, void *arg
),
1100 struct rhlist_head
*list
;
1103 free_fn(rht_obj(ht
, obj
), arg
);
1107 list
= container_of(obj
, struct rhlist_head
, rhead
);
1110 list
= rht_dereference(list
->next
, ht
);
1111 free_fn(rht_obj(ht
, obj
), arg
);
1116 * rhashtable_free_and_destroy - free elements and destroy hash table
1117 * @ht: the hash table to destroy
1118 * @free_fn: callback to release resources of element
1119 * @arg: pointer passed to free_fn
1121 * Stops an eventual async resize. If defined, invokes free_fn for each
1122 * element to releasal resources. Please note that RCU protected
1123 * readers may still be accessing the elements. Releasing of resources
1124 * must occur in a compatible manner. Then frees the bucket array.
1126 * This function will eventually sleep to wait for an async resize
1127 * to complete. The caller is responsible that no further write operations
1128 * occurs in parallel.
1130 void rhashtable_free_and_destroy(struct rhashtable
*ht
,
1131 void (*free_fn
)(void *ptr
, void *arg
),
1134 struct bucket_table
*tbl
, *next_tbl
;
1137 cancel_work_sync(&ht
->run_work
);
1139 mutex_lock(&ht
->mutex
);
1140 tbl
= rht_dereference(ht
->tbl
, ht
);
1143 for (i
= 0; i
< tbl
->size
; i
++) {
1144 struct rhash_head
*pos
, *next
;
1147 for (pos
= rht_dereference(*rht_bucket(tbl
, i
), ht
),
1148 next
= !rht_is_a_nulls(pos
) ?
1149 rht_dereference(pos
->next
, ht
) : NULL
;
1150 !rht_is_a_nulls(pos
);
1152 next
= !rht_is_a_nulls(pos
) ?
1153 rht_dereference(pos
->next
, ht
) : NULL
)
1154 rhashtable_free_one(ht
, pos
, free_fn
, arg
);
1158 next_tbl
= rht_dereference(tbl
->future_tbl
, ht
);
1159 bucket_table_free(tbl
);
1164 mutex_unlock(&ht
->mutex
);
1166 EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy
);
1168 void rhashtable_destroy(struct rhashtable
*ht
)
1170 return rhashtable_free_and_destroy(ht
, NULL
, NULL
);
1172 EXPORT_SYMBOL_GPL(rhashtable_destroy
);
1174 struct rhash_head __rcu
**rht_bucket_nested(const struct bucket_table
*tbl
,
1177 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
1178 static struct rhash_head __rcu
*rhnull
=
1179 (struct rhash_head __rcu
*)NULLS_MARKER(0);
1180 unsigned int index
= hash
& ((1 << tbl
->nest
) - 1);
1181 unsigned int size
= tbl
->size
>> tbl
->nest
;
1182 unsigned int subhash
= hash
;
1183 union nested_table
*ntbl
;
1185 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
1186 ntbl
= rht_dereference_bucket_rcu(ntbl
[index
].table
, tbl
, hash
);
1187 subhash
>>= tbl
->nest
;
1189 while (ntbl
&& size
> (1 << shift
)) {
1190 index
= subhash
& ((1 << shift
) - 1);
1191 ntbl
= rht_dereference_bucket_rcu(ntbl
[index
].table
,
1200 return &ntbl
[subhash
].bucket
;
1203 EXPORT_SYMBOL_GPL(rht_bucket_nested
);
1205 struct rhash_head __rcu
**rht_bucket_nested_insert(struct rhashtable
*ht
,
1206 struct bucket_table
*tbl
,
1209 const unsigned int shift
= PAGE_SHIFT
- ilog2(sizeof(void *));
1210 unsigned int index
= hash
& ((1 << tbl
->nest
) - 1);
1211 unsigned int size
= tbl
->size
>> tbl
->nest
;
1212 union nested_table
*ntbl
;
1214 ntbl
= (union nested_table
*)rcu_dereference_raw(tbl
->buckets
[0]);
1216 ntbl
= nested_table_alloc(ht
, &ntbl
[index
].table
,
1217 size
<= (1 << shift
));
1219 while (ntbl
&& size
> (1 << shift
)) {
1220 index
= hash
& ((1 << shift
) - 1);
1223 ntbl
= nested_table_alloc(ht
, &ntbl
[index
].table
,
1224 size
<= (1 << shift
));
1230 return &ntbl
[hash
].bucket
;
1233 EXPORT_SYMBOL_GPL(rht_bucket_nested_insert
);