1 // SPDX-License-Identifier: GPL-2.0-only
3 * Resizable, Scalable, Concurrent Hash Table
9 * Code partially derived from nft_hash
10 * Rewritten with rehash code from br_multicast plus single list
11 * pointer as suggested by Josh Triplett
14 #include <linux/atomic.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/log2.h>
18 #include <linux/sched.h>
19 #include <linux/rculist.h>
20 #include <linux/slab.h>
21 #include <linux/vmalloc.h>
23 #include <linux/jhash.h>
24 #include <linux/random.h>
25 #include <linux/rhashtable.h>
26 #include <linux/err.h>
27 #include <linux/export.h>
29 #define HASH_DEFAULT_SIZE 64UL
30 #define HASH_MIN_SIZE 4U
33 union nested_table __rcu *table;
34 struct rhash_lock_head *bucket;
37 static u32 head_hashfn(struct rhashtable *ht,
38 const struct bucket_table *tbl,
39 const struct rhash_head *he)
41 return rht_head_hashfn(ht, tbl, he, ht->p);
44 #ifdef CONFIG_PROVE_LOCKING
45 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
47 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
49 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
51 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
53 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
57 if (unlikely(tbl->nest))
59 return bit_spin_is_locked(0, (unsigned long *)&tbl->buckets[hash]);
61 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
63 #define ASSERT_RHT_MUTEX(HT)
66 static inline union nested_table *nested_table_top(
67 const struct bucket_table *tbl)
69 /* The top-level bucket entry does not need RCU protection
70 * because it's set at the same time as tbl->nest.
72 return (void *)rcu_dereference_protected(tbl->buckets[0], 1);
75 static void nested_table_free(union nested_table *ntbl, unsigned int size)
77 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
78 const unsigned int len = 1 << shift;
81 ntbl = rcu_dereference_protected(ntbl->table, 1);
87 for (i = 0; i < len; i++)
88 nested_table_free(ntbl + i, size);
94 static void nested_bucket_table_free(const struct bucket_table *tbl)
96 unsigned int size = tbl->size >> tbl->nest;
97 unsigned int len = 1 << tbl->nest;
98 union nested_table *ntbl;
101 ntbl = nested_table_top(tbl);
103 for (i = 0; i < len; i++)
104 nested_table_free(ntbl + i, size);
109 static void bucket_table_free(const struct bucket_table *tbl)
112 nested_bucket_table_free(tbl);
117 static void bucket_table_free_rcu(struct rcu_head *head)
119 bucket_table_free(container_of(head, struct bucket_table, rcu));
122 static union nested_table *nested_table_alloc(struct rhashtable *ht,
123 union nested_table __rcu **prev,
126 union nested_table *ntbl;
129 ntbl = rcu_dereference(*prev);
133 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
136 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
137 INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
140 if (cmpxchg((union nested_table **)prev, NULL, ntbl) == NULL)
142 /* Raced with another thread. */
144 return rcu_dereference(*prev);
147 static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
151 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
152 struct bucket_table *tbl;
155 if (nbuckets < (1 << (shift + 1)))
158 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
160 tbl = kzalloc(size, gfp);
164 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
170 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
175 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
179 struct bucket_table *tbl = NULL;
182 static struct lock_class_key __key;
184 tbl = kvzalloc(struct_size(tbl, buckets, nbuckets), gfp);
188 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
189 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
196 lockdep_init_map(&tbl->dep_map, "rhashtable_bucket", &__key, 0);
200 rcu_head_init(&tbl->rcu);
201 INIT_LIST_HEAD(&tbl->walkers);
203 tbl->hash_rnd = get_random_u32();
205 for (i = 0; i < nbuckets; i++)
206 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
211 static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
212 struct bucket_table *tbl)
214 struct bucket_table *new_tbl;
218 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
224 static int rhashtable_rehash_one(struct rhashtable *ht,
225 struct rhash_lock_head **bkt,
226 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);
231 struct rhash_head *head, *next, *entry;
232 struct rhash_head __rcu **pprev = NULL;
233 unsigned int new_hash;
240 rht_for_each_from(entry, rht_ptr(bkt, 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 rht_lock_nested(new_tbl, &new_tbl->buckets[new_hash], SINGLE_DEPTH_NESTING);
258 head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
260 RCU_INIT_POINTER(entry->next, head);
262 rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry);
265 rcu_assign_pointer(*pprev, next);
267 /* Need to preserved the bit lock. */
268 rht_assign_locked(bkt, next);
274 static int rhashtable_rehash_chain(struct rhashtable *ht,
275 unsigned int old_hash)
277 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
278 struct rhash_lock_head **bkt = rht_bucket_var(old_tbl, old_hash);
283 rht_lock(old_tbl, bkt);
285 while (!(err = rhashtable_rehash_one(ht, bkt, old_hash)))
290 rht_unlock(old_tbl, bkt);
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((struct bucket_table **)&old_tbl->future_tbl, NULL,
312 static int rhashtable_rehash_table(struct rhashtable *ht)
314 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
315 struct bucket_table *new_tbl;
316 struct rhashtable_walker *walker;
317 unsigned int old_hash;
320 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
324 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
325 err = rhashtable_rehash_chain(ht, old_hash);
331 /* Publish the new table pointer. */
332 rcu_assign_pointer(ht->tbl, new_tbl);
334 spin_lock(&ht->lock);
335 list_for_each_entry(walker, &old_tbl->walkers, list)
338 /* Wait for readers. All new readers will see the new
339 * table, and thus no references to the old table will
341 * We do this inside the locked region so that
342 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
343 * to check if it should not re-link the table.
345 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
346 spin_unlock(&ht->lock);
348 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
351 static int rhashtable_rehash_alloc(struct rhashtable *ht,
352 struct bucket_table *old_tbl,
355 struct bucket_table *new_tbl;
358 ASSERT_RHT_MUTEX(ht);
360 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
364 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
366 bucket_table_free(new_tbl);
372 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
373 * @ht: the hash table to shrink
375 * This function shrinks the hash table to fit, i.e., the smallest
376 * size would not cause it to expand right away automatically.
378 * The caller must ensure that no concurrent resizing occurs by holding
381 * The caller must ensure that no concurrent table mutations take place.
382 * It is however valid to have concurrent lookups if they are RCU protected.
384 * It is valid to have concurrent insertions and deletions protected by per
385 * bucket locks or concurrent RCU protected lookups and traversals.
387 static int rhashtable_shrink(struct rhashtable *ht)
389 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
390 unsigned int nelems = atomic_read(&ht->nelems);
391 unsigned int size = 0;
394 size = roundup_pow_of_two(nelems * 3 / 2);
395 if (size < ht->p.min_size)
396 size = ht->p.min_size;
398 if (old_tbl->size <= size)
401 if (rht_dereference(old_tbl->future_tbl, ht))
404 return rhashtable_rehash_alloc(ht, old_tbl, size);
407 static void rht_deferred_worker(struct work_struct *work)
409 struct rhashtable *ht;
410 struct bucket_table *tbl;
413 ht = container_of(work, struct rhashtable, run_work);
414 mutex_lock(&ht->mutex);
416 tbl = rht_dereference(ht->tbl, ht);
417 tbl = rhashtable_last_table(ht, tbl);
419 if (rht_grow_above_75(ht, tbl))
420 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
421 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
422 err = rhashtable_shrink(ht);
424 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
426 if (!err || err == -EEXIST) {
429 nerr = rhashtable_rehash_table(ht);
433 mutex_unlock(&ht->mutex);
436 schedule_work(&ht->run_work);
439 static int rhashtable_insert_rehash(struct rhashtable *ht,
440 struct bucket_table *tbl)
442 struct bucket_table *old_tbl;
443 struct bucket_table *new_tbl;
447 old_tbl = rht_dereference_rcu(ht->tbl, ht);
453 if (rht_grow_above_75(ht, tbl))
455 /* Do not schedule more than one rehash */
456 else if (old_tbl != tbl)
461 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
465 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
467 bucket_table_free(new_tbl);
471 schedule_work(&ht->run_work);
476 /* Do not fail the insert if someone else did a rehash. */
477 if (likely(rcu_access_pointer(tbl->future_tbl)))
480 /* Schedule async rehash to retry allocation in process context. */
482 schedule_work(&ht->run_work);
487 static void *rhashtable_lookup_one(struct rhashtable *ht,
488 struct rhash_lock_head **bkt,
489 struct bucket_table *tbl, unsigned int hash,
490 const void *key, struct rhash_head *obj)
492 struct rhashtable_compare_arg arg = {
496 struct rhash_head __rcu **pprev = NULL;
497 struct rhash_head *head;
500 elasticity = RHT_ELASTICITY;
501 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
502 struct rhlist_head *list;
503 struct rhlist_head *plist;
508 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
509 rhashtable_compare(&arg, rht_obj(ht, head)))) {
515 return rht_obj(ht, head);
517 list = container_of(obj, struct rhlist_head, rhead);
518 plist = container_of(head, struct rhlist_head, rhead);
520 RCU_INIT_POINTER(list->next, plist);
521 head = rht_dereference_bucket(head->next, tbl, hash);
522 RCU_INIT_POINTER(list->rhead.next, head);
524 rcu_assign_pointer(*pprev, obj);
526 /* Need to preserve the bit lock */
527 rht_assign_locked(bkt, obj);
533 return ERR_PTR(-EAGAIN);
535 return ERR_PTR(-ENOENT);
538 static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
539 struct rhash_lock_head **bkt,
540 struct bucket_table *tbl,
542 struct rhash_head *obj,
545 struct bucket_table *new_tbl;
546 struct rhash_head *head;
548 if (!IS_ERR_OR_NULL(data))
549 return ERR_PTR(-EEXIST);
551 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
552 return ERR_CAST(data);
554 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
558 if (PTR_ERR(data) != -ENOENT)
559 return ERR_CAST(data);
561 if (unlikely(rht_grow_above_max(ht, tbl)))
562 return ERR_PTR(-E2BIG);
564 if (unlikely(rht_grow_above_100(ht, tbl)))
565 return ERR_PTR(-EAGAIN);
567 head = rht_ptr(bkt, tbl, hash);
569 RCU_INIT_POINTER(obj->next, head);
571 struct rhlist_head *list;
573 list = container_of(obj, struct rhlist_head, rhead);
574 RCU_INIT_POINTER(list->next, NULL);
577 /* bkt is always the head of the list, so it holds
578 * the lock, which we need to preserve
580 rht_assign_locked(bkt, obj);
582 atomic_inc(&ht->nelems);
583 if (rht_grow_above_75(ht, tbl))
584 schedule_work(&ht->run_work);
589 static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
590 struct rhash_head *obj)
592 struct bucket_table *new_tbl;
593 struct bucket_table *tbl;
594 struct rhash_lock_head **bkt;
598 new_tbl = rcu_dereference(ht->tbl);
602 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
603 if (rcu_access_pointer(tbl->future_tbl))
605 bkt = rht_bucket_var(tbl, hash);
607 bkt = rht_bucket_insert(ht, tbl, hash);
609 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
610 data = ERR_PTR(-EAGAIN);
613 data = rhashtable_lookup_one(ht, bkt, tbl,
615 new_tbl = rhashtable_insert_one(ht, bkt, tbl,
617 if (PTR_ERR(new_tbl) != -EEXIST)
618 data = ERR_CAST(new_tbl);
620 rht_unlock(tbl, bkt);
622 } while (!IS_ERR_OR_NULL(new_tbl));
624 if (PTR_ERR(data) == -EAGAIN)
625 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
631 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
632 struct rhash_head *obj)
638 data = rhashtable_try_insert(ht, key, obj);
640 } while (PTR_ERR(data) == -EAGAIN);
644 EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
647 * rhashtable_walk_enter - Initialise an iterator
648 * @ht: Table to walk over
649 * @iter: Hash table Iterator
651 * This function prepares a hash table walk.
653 * Note that if you restart a walk after rhashtable_walk_stop you
654 * may see the same object twice. Also, you may miss objects if
655 * there are removals in between rhashtable_walk_stop and the next
656 * call to rhashtable_walk_start.
658 * For a completely stable walk you should construct your own data
659 * structure outside the hash table.
661 * This function may be called from any process context, including
662 * non-preemptable context, but cannot be called from softirq or
665 * You must call rhashtable_walk_exit after this function returns.
667 void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
673 iter->end_of_table = 0;
675 spin_lock(&ht->lock);
677 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
678 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
679 spin_unlock(&ht->lock);
681 EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
684 * rhashtable_walk_exit - Free an iterator
685 * @iter: Hash table Iterator
687 * This function frees resources allocated by rhashtable_walk_enter.
689 void rhashtable_walk_exit(struct rhashtable_iter *iter)
691 spin_lock(&iter->ht->lock);
692 if (iter->walker.tbl)
693 list_del(&iter->walker.list);
694 spin_unlock(&iter->ht->lock);
696 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
699 * rhashtable_walk_start_check - Start a hash table walk
700 * @iter: Hash table iterator
702 * Start a hash table walk at the current iterator position. Note that we take
703 * the RCU lock in all cases including when we return an error. So you must
704 * always call rhashtable_walk_stop to clean up.
706 * Returns zero if successful.
708 * Returns -EAGAIN if resize event occured. Note that the iterator
709 * will rewind back to the beginning and you may use it immediately
710 * by calling rhashtable_walk_next.
712 * rhashtable_walk_start is defined as an inline variant that returns
713 * void. This is preferred in cases where the caller would ignore
714 * resize events and always continue.
716 int rhashtable_walk_start_check(struct rhashtable_iter *iter)
719 struct rhashtable *ht = iter->ht;
720 bool rhlist = ht->rhlist;
724 spin_lock(&ht->lock);
725 if (iter->walker.tbl)
726 list_del(&iter->walker.list);
727 spin_unlock(&ht->lock);
729 if (iter->end_of_table)
731 if (!iter->walker.tbl) {
732 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
738 if (iter->p && !rhlist) {
740 * We need to validate that 'p' is still in the table, and
741 * if so, update 'skip'
743 struct rhash_head *p;
745 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
753 } else if (iter->p && rhlist) {
754 /* Need to validate that 'list' is still in the table, and
755 * if so, update 'skip' and 'p'.
757 struct rhash_head *p;
758 struct rhlist_head *list;
760 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
761 for (list = container_of(p, struct rhlist_head, rhead);
763 list = rcu_dereference(list->next)) {
765 if (list == iter->list) {
777 EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
780 * __rhashtable_walk_find_next - Find the next element in a table (or the first
781 * one in case of a new walk).
783 * @iter: Hash table iterator
785 * Returns the found object or NULL when the end of the table is reached.
787 * Returns -EAGAIN if resize event occurred.
789 static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
791 struct bucket_table *tbl = iter->walker.tbl;
792 struct rhlist_head *list = iter->list;
793 struct rhashtable *ht = iter->ht;
794 struct rhash_head *p = iter->p;
795 bool rhlist = ht->rhlist;
800 for (; iter->slot < tbl->size; iter->slot++) {
801 int skip = iter->skip;
803 rht_for_each_rcu(p, tbl, iter->slot) {
805 list = container_of(p, struct rhlist_head,
811 list = rcu_dereference(list->next);
822 if (!rht_is_a_nulls(p)) {
826 return rht_obj(ht, rhlist ? &list->rhead : p);
834 /* Ensure we see any new tables. */
837 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
838 if (iter->walker.tbl) {
841 return ERR_PTR(-EAGAIN);
843 iter->end_of_table = true;
850 * rhashtable_walk_next - Return the next object and advance the iterator
851 * @iter: Hash table iterator
853 * Note that you must call rhashtable_walk_stop when you are finished
856 * Returns the next object or NULL when the end of the table is reached.
858 * Returns -EAGAIN if resize event occurred. Note that the iterator
859 * will rewind back to the beginning and you may continue to use it.
861 void *rhashtable_walk_next(struct rhashtable_iter *iter)
863 struct rhlist_head *list = iter->list;
864 struct rhashtable *ht = iter->ht;
865 struct rhash_head *p = iter->p;
866 bool rhlist = ht->rhlist;
869 if (!rhlist || !(list = rcu_dereference(list->next))) {
870 p = rcu_dereference(p->next);
871 list = container_of(p, struct rhlist_head, rhead);
873 if (!rht_is_a_nulls(p)) {
877 return rht_obj(ht, rhlist ? &list->rhead : p);
880 /* At the end of this slot, switch to next one and then find
881 * next entry from that point.
887 return __rhashtable_walk_find_next(iter);
889 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
892 * rhashtable_walk_peek - Return the next object but don't advance the iterator
893 * @iter: Hash table iterator
895 * Returns the next object or NULL when the end of the table is reached.
897 * Returns -EAGAIN if resize event occurred. Note that the iterator
898 * will rewind back to the beginning and you may continue to use it.
900 void *rhashtable_walk_peek(struct rhashtable_iter *iter)
902 struct rhlist_head *list = iter->list;
903 struct rhashtable *ht = iter->ht;
904 struct rhash_head *p = iter->p;
907 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
909 /* No object found in current iter, find next one in the table. */
912 /* A nonzero skip value points to the next entry in the table
913 * beyond that last one that was found. Decrement skip so
914 * we find the current value. __rhashtable_walk_find_next
915 * will restore the original value of skip assuming that
916 * the table hasn't changed.
921 return __rhashtable_walk_find_next(iter);
923 EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
926 * rhashtable_walk_stop - Finish a hash table walk
927 * @iter: Hash table iterator
929 * Finish a hash table walk. Does not reset the iterator to the start of the
932 void rhashtable_walk_stop(struct rhashtable_iter *iter)
935 struct rhashtable *ht;
936 struct bucket_table *tbl = iter->walker.tbl;
943 spin_lock(&ht->lock);
944 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
945 /* This bucket table is being freed, don't re-link it. */
946 iter->walker.tbl = NULL;
948 list_add(&iter->walker.list, &tbl->walkers);
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 ht->key_len = ht->p.key_len;
1049 if (!params->hashfn) {
1050 ht->p.hashfn = jhash;
1052 if (!(ht->key_len & (sizeof(u32) - 1))) {
1053 ht->key_len /= sizeof(u32);
1054 ht->p.hashfn = rhashtable_jhash2;
1059 * This is api initialization and thus we need to guarantee the
1060 * initial rhashtable allocation. Upon failure, retry with the
1061 * smallest possible size with __GFP_NOFAIL semantics.
1063 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
1064 if (unlikely(tbl == NULL)) {
1065 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1066 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1069 atomic_set(&ht->nelems, 0);
1071 RCU_INIT_POINTER(ht->tbl, tbl);
1073 INIT_WORK(&ht->run_work, rht_deferred_worker);
1077 EXPORT_SYMBOL_GPL(rhashtable_init);
1080 * rhltable_init - initialize a new hash list table
1081 * @hlt: hash list table to be initialized
1082 * @params: configuration parameters
1084 * Initializes a new hash list table.
1086 * See documentation for rhashtable_init.
1088 int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1092 err = rhashtable_init(&hlt->ht, params);
1093 hlt->ht.rhlist = true;
1096 EXPORT_SYMBOL_GPL(rhltable_init);
1098 static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1099 void (*free_fn)(void *ptr, void *arg),
1102 struct rhlist_head *list;
1105 free_fn(rht_obj(ht, obj), arg);
1109 list = container_of(obj, struct rhlist_head, rhead);
1112 list = rht_dereference(list->next, ht);
1113 free_fn(rht_obj(ht, obj), arg);
1118 * rhashtable_free_and_destroy - free elements and destroy hash table
1119 * @ht: the hash table to destroy
1120 * @free_fn: callback to release resources of element
1121 * @arg: pointer passed to free_fn
1123 * Stops an eventual async resize. If defined, invokes free_fn for each
1124 * element to releasal resources. Please note that RCU protected
1125 * readers may still be accessing the elements. Releasing of resources
1126 * must occur in a compatible manner. Then frees the bucket array.
1128 * This function will eventually sleep to wait for an async resize
1129 * to complete. The caller is responsible that no further write operations
1130 * occurs in parallel.
1132 void rhashtable_free_and_destroy(struct rhashtable *ht,
1133 void (*free_fn)(void *ptr, void *arg),
1136 struct bucket_table *tbl, *next_tbl;
1139 cancel_work_sync(&ht->run_work);
1141 mutex_lock(&ht->mutex);
1142 tbl = rht_dereference(ht->tbl, ht);
1145 for (i = 0; i < tbl->size; i++) {
1146 struct rhash_head *pos, *next;
1149 for (pos = rht_ptr_exclusive(rht_bucket(tbl, i)),
1150 next = !rht_is_a_nulls(pos) ?
1151 rht_dereference(pos->next, ht) : NULL;
1152 !rht_is_a_nulls(pos);
1154 next = !rht_is_a_nulls(pos) ?
1155 rht_dereference(pos->next, ht) : NULL)
1156 rhashtable_free_one(ht, pos, free_fn, arg);
1160 next_tbl = rht_dereference(tbl->future_tbl, ht);
1161 bucket_table_free(tbl);
1166 mutex_unlock(&ht->mutex);
1168 EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1170 void rhashtable_destroy(struct rhashtable *ht)
1172 return rhashtable_free_and_destroy(ht, NULL, NULL);
1174 EXPORT_SYMBOL_GPL(rhashtable_destroy);
1176 struct rhash_lock_head **__rht_bucket_nested(const struct bucket_table *tbl,
1179 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
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 = nested_table_top(tbl);
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_lock_head **rht_bucket_nested(const struct bucket_table *tbl,
1208 static struct rhash_lock_head *rhnull;
1211 INIT_RHT_NULLS_HEAD(rhnull);
1212 return __rht_bucket_nested(tbl, hash) ?: &rhnull;
1214 EXPORT_SYMBOL_GPL(rht_bucket_nested);
1216 struct rhash_lock_head **rht_bucket_nested_insert(struct rhashtable *ht,
1217 struct bucket_table *tbl,
1220 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1221 unsigned int index = hash & ((1 << tbl->nest) - 1);
1222 unsigned int size = tbl->size >> tbl->nest;
1223 union nested_table *ntbl;
1225 ntbl = nested_table_top(tbl);
1227 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1228 size <= (1 << shift));
1230 while (ntbl && size > (1 << shift)) {
1231 index = hash & ((1 << shift) - 1);
1234 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1235 size <= (1 << shift));
1241 return &ntbl[hash].bucket;
1244 EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);