2 * Resizable, Scalable, Concurrent Hash Table
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
36 union nested_table __rcu *table;
37 struct rhash_lock_head *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)
60 if (unlikely(tbl->nest))
62 return bit_spin_is_locked(0, (unsigned long *)&tbl->buckets[hash]);
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);
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 if (cmpxchg((union nested_table **)prev, NULL, ntbl) == NULL)
136 /* Raced with another thread. */
138 return rcu_dereference(*prev);
141 static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
145 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
146 struct bucket_table *tbl;
149 if (nbuckets < (1 << (shift + 1)))
152 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
154 tbl = kzalloc(size, gfp);
158 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
164 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
169 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
173 struct bucket_table *tbl = NULL;
176 static struct lock_class_key __key;
178 tbl = kvzalloc(struct_size(tbl, buckets, nbuckets), gfp);
182 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
183 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
190 lockdep_init_map(&tbl->dep_map, "rhashtable_bucket", &__key, 0);
194 rcu_head_init(&tbl->rcu);
195 INIT_LIST_HEAD(&tbl->walkers);
197 tbl->hash_rnd = get_random_u32();
199 for (i = 0; i < nbuckets; i++)
200 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
205 static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
206 struct bucket_table *tbl)
208 struct bucket_table *new_tbl;
212 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
218 static int rhashtable_rehash_one(struct rhashtable *ht,
219 struct rhash_lock_head **bkt,
220 unsigned int old_hash)
222 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
223 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
225 struct rhash_head *head, *next, *entry;
226 struct rhash_head __rcu **pprev = NULL;
227 unsigned int new_hash;
234 rht_for_each_from(entry, rht_ptr(bkt, old_tbl, old_hash),
237 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
239 if (rht_is_a_nulls(next))
242 pprev = &entry->next;
248 new_hash = head_hashfn(ht, new_tbl, entry);
250 rht_lock_nested(new_tbl, &new_tbl->buckets[new_hash], SINGLE_DEPTH_NESTING);
252 head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
254 RCU_INIT_POINTER(entry->next, head);
256 rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry);
259 rcu_assign_pointer(*pprev, next);
261 /* Need to preserved the bit lock. */
262 rht_assign_locked(bkt, next);
268 static int rhashtable_rehash_chain(struct rhashtable *ht,
269 unsigned int old_hash)
271 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
272 struct rhash_lock_head **bkt = rht_bucket_var(old_tbl, old_hash);
277 rht_lock(old_tbl, bkt);
279 while (!(err = rhashtable_rehash_one(ht, bkt, old_hash)))
284 rht_unlock(old_tbl, bkt);
289 static int rhashtable_rehash_attach(struct rhashtable *ht,
290 struct bucket_table *old_tbl,
291 struct bucket_table *new_tbl)
293 /* Make insertions go into the new, empty table right away. Deletions
294 * and lookups will be attempted in both tables until we synchronize.
295 * As cmpxchg() provides strong barriers, we do not need
296 * rcu_assign_pointer().
299 if (cmpxchg((struct bucket_table **)&old_tbl->future_tbl, NULL,
306 static int rhashtable_rehash_table(struct rhashtable *ht)
308 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
309 struct bucket_table *new_tbl;
310 struct rhashtable_walker *walker;
311 unsigned int old_hash;
314 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
318 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
319 err = rhashtable_rehash_chain(ht, old_hash);
325 /* Publish the new table pointer. */
326 rcu_assign_pointer(ht->tbl, new_tbl);
328 spin_lock(&ht->lock);
329 list_for_each_entry(walker, &old_tbl->walkers, list)
332 /* Wait for readers. All new readers will see the new
333 * table, and thus no references to the old table will
335 * We do this inside the locked region so that
336 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
337 * to check if it should not re-link the table.
339 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
340 spin_unlock(&ht->lock);
342 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
345 static int rhashtable_rehash_alloc(struct rhashtable *ht,
346 struct bucket_table *old_tbl,
349 struct bucket_table *new_tbl;
352 ASSERT_RHT_MUTEX(ht);
354 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
358 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
360 bucket_table_free(new_tbl);
366 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
367 * @ht: the hash table to shrink
369 * This function shrinks the hash table to fit, i.e., the smallest
370 * size would not cause it to expand right away automatically.
372 * The caller must ensure that no concurrent resizing occurs by holding
375 * The caller must ensure that no concurrent table mutations take place.
376 * It is however valid to have concurrent lookups if they are RCU protected.
378 * It is valid to have concurrent insertions and deletions protected by per
379 * bucket locks or concurrent RCU protected lookups and traversals.
381 static int rhashtable_shrink(struct rhashtable *ht)
383 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
384 unsigned int nelems = atomic_read(&ht->nelems);
385 unsigned int size = 0;
388 size = roundup_pow_of_two(nelems * 3 / 2);
389 if (size < ht->p.min_size)
390 size = ht->p.min_size;
392 if (old_tbl->size <= size)
395 if (rht_dereference(old_tbl->future_tbl, ht))
398 return rhashtable_rehash_alloc(ht, old_tbl, size);
401 static void rht_deferred_worker(struct work_struct *work)
403 struct rhashtable *ht;
404 struct bucket_table *tbl;
407 ht = container_of(work, struct rhashtable, run_work);
408 mutex_lock(&ht->mutex);
410 tbl = rht_dereference(ht->tbl, ht);
411 tbl = rhashtable_last_table(ht, tbl);
413 if (rht_grow_above_75(ht, tbl))
414 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
415 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
416 err = rhashtable_shrink(ht);
418 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
420 if (!err || err == -EEXIST) {
423 nerr = rhashtable_rehash_table(ht);
427 mutex_unlock(&ht->mutex);
430 schedule_work(&ht->run_work);
433 static int rhashtable_insert_rehash(struct rhashtable *ht,
434 struct bucket_table *tbl)
436 struct bucket_table *old_tbl;
437 struct bucket_table *new_tbl;
441 old_tbl = rht_dereference_rcu(ht->tbl, ht);
447 if (rht_grow_above_75(ht, tbl))
449 /* Do not schedule more than one rehash */
450 else if (old_tbl != tbl)
455 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
459 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
461 bucket_table_free(new_tbl);
465 schedule_work(&ht->run_work);
470 /* Do not fail the insert if someone else did a rehash. */
471 if (likely(rcu_access_pointer(tbl->future_tbl)))
474 /* Schedule async rehash to retry allocation in process context. */
476 schedule_work(&ht->run_work);
481 static void *rhashtable_lookup_one(struct rhashtable *ht,
482 struct rhash_lock_head **bkt,
483 struct bucket_table *tbl, unsigned int hash,
484 const void *key, struct rhash_head *obj)
486 struct rhashtable_compare_arg arg = {
490 struct rhash_head __rcu **pprev = NULL;
491 struct rhash_head *head;
494 elasticity = RHT_ELASTICITY;
495 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
496 struct rhlist_head *list;
497 struct rhlist_head *plist;
502 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
503 rhashtable_compare(&arg, rht_obj(ht, head)))) {
509 return rht_obj(ht, head);
511 list = container_of(obj, struct rhlist_head, rhead);
512 plist = container_of(head, struct rhlist_head, rhead);
514 RCU_INIT_POINTER(list->next, plist);
515 head = rht_dereference_bucket(head->next, tbl, hash);
516 RCU_INIT_POINTER(list->rhead.next, head);
518 rcu_assign_pointer(*pprev, obj);
520 /* Need to preserve the bit lock */
521 rht_assign_locked(bkt, obj);
527 return ERR_PTR(-EAGAIN);
529 return ERR_PTR(-ENOENT);
532 static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
533 struct rhash_lock_head **bkt,
534 struct bucket_table *tbl,
536 struct rhash_head *obj,
539 struct bucket_table *new_tbl;
540 struct rhash_head *head;
542 if (!IS_ERR_OR_NULL(data))
543 return ERR_PTR(-EEXIST);
545 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
546 return ERR_CAST(data);
548 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
552 if (PTR_ERR(data) != -ENOENT)
553 return ERR_CAST(data);
555 if (unlikely(rht_grow_above_max(ht, tbl)))
556 return ERR_PTR(-E2BIG);
558 if (unlikely(rht_grow_above_100(ht, tbl)))
559 return ERR_PTR(-EAGAIN);
561 head = rht_ptr(bkt, tbl, hash);
563 RCU_INIT_POINTER(obj->next, head);
565 struct rhlist_head *list;
567 list = container_of(obj, struct rhlist_head, rhead);
568 RCU_INIT_POINTER(list->next, NULL);
571 /* bkt is always the head of the list, so it holds
572 * the lock, which we need to preserve
574 rht_assign_locked(bkt, obj);
576 atomic_inc(&ht->nelems);
577 if (rht_grow_above_75(ht, tbl))
578 schedule_work(&ht->run_work);
583 static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
584 struct rhash_head *obj)
586 struct bucket_table *new_tbl;
587 struct bucket_table *tbl;
588 struct rhash_lock_head **bkt;
592 new_tbl = rcu_dereference(ht->tbl);
596 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
597 if (rcu_access_pointer(tbl->future_tbl))
599 bkt = rht_bucket_var(tbl, hash);
601 bkt = rht_bucket_insert(ht, tbl, hash);
603 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
604 data = ERR_PTR(-EAGAIN);
607 data = rhashtable_lookup_one(ht, bkt, tbl,
609 new_tbl = rhashtable_insert_one(ht, bkt, tbl,
611 if (PTR_ERR(new_tbl) != -EEXIST)
612 data = ERR_CAST(new_tbl);
614 rht_unlock(tbl, bkt);
616 } while (!IS_ERR_OR_NULL(new_tbl));
618 if (PTR_ERR(data) == -EAGAIN)
619 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
625 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
626 struct rhash_head *obj)
632 data = rhashtable_try_insert(ht, key, obj);
634 } while (PTR_ERR(data) == -EAGAIN);
638 EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
641 * rhashtable_walk_enter - Initialise an iterator
642 * @ht: Table to walk over
643 * @iter: Hash table Iterator
645 * This function prepares a hash table walk.
647 * Note that if you restart a walk after rhashtable_walk_stop you
648 * may see the same object twice. Also, you may miss objects if
649 * there are removals in between rhashtable_walk_stop and the next
650 * call to rhashtable_walk_start.
652 * For a completely stable walk you should construct your own data
653 * structure outside the hash table.
655 * This function may be called from any process context, including
656 * non-preemptable context, but cannot be called from softirq or
659 * You must call rhashtable_walk_exit after this function returns.
661 void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
667 iter->end_of_table = 0;
669 spin_lock(&ht->lock);
671 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
672 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
673 spin_unlock(&ht->lock);
675 EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
678 * rhashtable_walk_exit - Free an iterator
679 * @iter: Hash table Iterator
681 * This function frees resources allocated by rhashtable_walk_enter.
683 void rhashtable_walk_exit(struct rhashtable_iter *iter)
685 spin_lock(&iter->ht->lock);
686 if (iter->walker.tbl)
687 list_del(&iter->walker.list);
688 spin_unlock(&iter->ht->lock);
690 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
693 * rhashtable_walk_start_check - Start a hash table walk
694 * @iter: Hash table iterator
696 * Start a hash table walk at the current iterator position. Note that we take
697 * the RCU lock in all cases including when we return an error. So you must
698 * always call rhashtable_walk_stop to clean up.
700 * Returns zero if successful.
702 * Returns -EAGAIN if resize event occured. Note that the iterator
703 * will rewind back to the beginning and you may use it immediately
704 * by calling rhashtable_walk_next.
706 * rhashtable_walk_start is defined as an inline variant that returns
707 * void. This is preferred in cases where the caller would ignore
708 * resize events and always continue.
710 int rhashtable_walk_start_check(struct rhashtable_iter *iter)
713 struct rhashtable *ht = iter->ht;
714 bool rhlist = ht->rhlist;
718 spin_lock(&ht->lock);
719 if (iter->walker.tbl)
720 list_del(&iter->walker.list);
721 spin_unlock(&ht->lock);
723 if (iter->end_of_table)
725 if (!iter->walker.tbl) {
726 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
732 if (iter->p && !rhlist) {
734 * We need to validate that 'p' is still in the table, and
735 * if so, update 'skip'
737 struct rhash_head *p;
739 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
747 } else if (iter->p && rhlist) {
748 /* Need to validate that 'list' is still in the table, and
749 * if so, update 'skip' and 'p'.
751 struct rhash_head *p;
752 struct rhlist_head *list;
754 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
755 for (list = container_of(p, struct rhlist_head, rhead);
757 list = rcu_dereference(list->next)) {
759 if (list == iter->list) {
771 EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
774 * __rhashtable_walk_find_next - Find the next element in a table (or the first
775 * one in case of a new walk).
777 * @iter: Hash table iterator
779 * Returns the found object or NULL when the end of the table is reached.
781 * Returns -EAGAIN if resize event occurred.
783 static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
785 struct bucket_table *tbl = iter->walker.tbl;
786 struct rhlist_head *list = iter->list;
787 struct rhashtable *ht = iter->ht;
788 struct rhash_head *p = iter->p;
789 bool rhlist = ht->rhlist;
794 for (; iter->slot < tbl->size; iter->slot++) {
795 int skip = iter->skip;
797 rht_for_each_rcu(p, tbl, iter->slot) {
799 list = container_of(p, struct rhlist_head,
805 list = rcu_dereference(list->next);
816 if (!rht_is_a_nulls(p)) {
820 return rht_obj(ht, rhlist ? &list->rhead : p);
828 /* Ensure we see any new tables. */
831 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
832 if (iter->walker.tbl) {
835 return ERR_PTR(-EAGAIN);
837 iter->end_of_table = true;
844 * rhashtable_walk_next - Return the next object and advance the iterator
845 * @iter: Hash table iterator
847 * Note that you must call rhashtable_walk_stop when you are finished
850 * Returns the next object or NULL when the end of the table is reached.
852 * Returns -EAGAIN if resize event occurred. Note that the iterator
853 * will rewind back to the beginning and you may continue to use it.
855 void *rhashtable_walk_next(struct rhashtable_iter *iter)
857 struct rhlist_head *list = iter->list;
858 struct rhashtable *ht = iter->ht;
859 struct rhash_head *p = iter->p;
860 bool rhlist = ht->rhlist;
863 if (!rhlist || !(list = rcu_dereference(list->next))) {
864 p = rcu_dereference(p->next);
865 list = container_of(p, struct rhlist_head, rhead);
867 if (!rht_is_a_nulls(p)) {
871 return rht_obj(ht, rhlist ? &list->rhead : p);
874 /* At the end of this slot, switch to next one and then find
875 * next entry from that point.
881 return __rhashtable_walk_find_next(iter);
883 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
886 * rhashtable_walk_peek - Return the next object but don't advance the iterator
887 * @iter: Hash table iterator
889 * Returns the next object or NULL when the end of the table is reached.
891 * Returns -EAGAIN if resize event occurred. Note that the iterator
892 * will rewind back to the beginning and you may continue to use it.
894 void *rhashtable_walk_peek(struct rhashtable_iter *iter)
896 struct rhlist_head *list = iter->list;
897 struct rhashtable *ht = iter->ht;
898 struct rhash_head *p = iter->p;
901 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
903 /* No object found in current iter, find next one in the table. */
906 /* A nonzero skip value points to the next entry in the table
907 * beyond that last one that was found. Decrement skip so
908 * we find the current value. __rhashtable_walk_find_next
909 * will restore the original value of skip assuming that
910 * the table hasn't changed.
915 return __rhashtable_walk_find_next(iter);
917 EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
920 * rhashtable_walk_stop - Finish a hash table walk
921 * @iter: Hash table iterator
923 * Finish a hash table walk. Does not reset the iterator to the start of the
926 void rhashtable_walk_stop(struct rhashtable_iter *iter)
929 struct rhashtable *ht;
930 struct bucket_table *tbl = iter->walker.tbl;
937 spin_lock(&ht->lock);
938 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
939 /* This bucket table is being freed, don't re-link it. */
940 iter->walker.tbl = NULL;
942 list_add(&iter->walker.list, &tbl->walkers);
943 spin_unlock(&ht->lock);
948 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
950 static size_t rounded_hashtable_size(const struct rhashtable_params *params)
954 if (params->nelem_hint)
955 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
956 (unsigned long)params->min_size);
958 retsize = max(HASH_DEFAULT_SIZE,
959 (unsigned long)params->min_size);
964 static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
966 return jhash2(key, length, seed);
970 * rhashtable_init - initialize a new hash table
971 * @ht: hash table to be initialized
972 * @params: configuration parameters
974 * Initializes a new hash table based on the provided configuration
975 * parameters. A table can be configured either with a variable or
978 * Configuration Example 1: Fixed length keys
982 * struct rhash_head node;
985 * struct rhashtable_params params = {
986 * .head_offset = offsetof(struct test_obj, node),
987 * .key_offset = offsetof(struct test_obj, key),
988 * .key_len = sizeof(int),
992 * Configuration Example 2: Variable length keys
995 * struct rhash_head node;
998 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
1000 * struct test_obj *obj = data;
1002 * return [... hash ...];
1005 * struct rhashtable_params params = {
1006 * .head_offset = offsetof(struct test_obj, node),
1008 * .obj_hashfn = my_hash_fn,
1011 int rhashtable_init(struct rhashtable *ht,
1012 const struct rhashtable_params *params)
1014 struct bucket_table *tbl;
1017 if ((!params->key_len && !params->obj_hashfn) ||
1018 (params->obj_hashfn && !params->obj_cmpfn))
1021 memset(ht, 0, sizeof(*ht));
1022 mutex_init(&ht->mutex);
1023 spin_lock_init(&ht->lock);
1024 memcpy(&ht->p, params, sizeof(*params));
1026 if (params->min_size)
1027 ht->p.min_size = roundup_pow_of_two(params->min_size);
1029 /* Cap total entries at 2^31 to avoid nelems overflow. */
1030 ht->max_elems = 1u << 31;
1032 if (params->max_size) {
1033 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1034 if (ht->p.max_size < ht->max_elems / 2)
1035 ht->max_elems = ht->p.max_size * 2;
1038 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1040 size = rounded_hashtable_size(&ht->p);
1042 ht->key_len = ht->p.key_len;
1043 if (!params->hashfn) {
1044 ht->p.hashfn = jhash;
1046 if (!(ht->key_len & (sizeof(u32) - 1))) {
1047 ht->key_len /= sizeof(u32);
1048 ht->p.hashfn = rhashtable_jhash2;
1053 * This is api initialization and thus we need to guarantee the
1054 * initial rhashtable allocation. Upon failure, retry with the
1055 * smallest possible size with __GFP_NOFAIL semantics.
1057 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
1058 if (unlikely(tbl == NULL)) {
1059 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1060 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1063 atomic_set(&ht->nelems, 0);
1065 RCU_INIT_POINTER(ht->tbl, tbl);
1067 INIT_WORK(&ht->run_work, rht_deferred_worker);
1071 EXPORT_SYMBOL_GPL(rhashtable_init);
1074 * rhltable_init - initialize a new hash list table
1075 * @hlt: hash list table to be initialized
1076 * @params: configuration parameters
1078 * Initializes a new hash list table.
1080 * See documentation for rhashtable_init.
1082 int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1086 err = rhashtable_init(&hlt->ht, params);
1087 hlt->ht.rhlist = true;
1090 EXPORT_SYMBOL_GPL(rhltable_init);
1092 static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1093 void (*free_fn)(void *ptr, void *arg),
1096 struct rhlist_head *list;
1099 free_fn(rht_obj(ht, obj), arg);
1103 list = container_of(obj, struct rhlist_head, rhead);
1106 list = rht_dereference(list->next, ht);
1107 free_fn(rht_obj(ht, obj), arg);
1112 * rhashtable_free_and_destroy - free elements and destroy hash table
1113 * @ht: the hash table to destroy
1114 * @free_fn: callback to release resources of element
1115 * @arg: pointer passed to free_fn
1117 * Stops an eventual async resize. If defined, invokes free_fn for each
1118 * element to releasal resources. Please note that RCU protected
1119 * readers may still be accessing the elements. Releasing of resources
1120 * must occur in a compatible manner. Then frees the bucket array.
1122 * This function will eventually sleep to wait for an async resize
1123 * to complete. The caller is responsible that no further write operations
1124 * occurs in parallel.
1126 void rhashtable_free_and_destroy(struct rhashtable *ht,
1127 void (*free_fn)(void *ptr, void *arg),
1130 struct bucket_table *tbl, *next_tbl;
1133 cancel_work_sync(&ht->run_work);
1135 mutex_lock(&ht->mutex);
1136 tbl = rht_dereference(ht->tbl, ht);
1139 for (i = 0; i < tbl->size; i++) {
1140 struct rhash_head *pos, *next;
1143 for (pos = rht_ptr_exclusive(rht_bucket(tbl, i)),
1144 next = !rht_is_a_nulls(pos) ?
1145 rht_dereference(pos->next, ht) : NULL;
1146 !rht_is_a_nulls(pos);
1148 next = !rht_is_a_nulls(pos) ?
1149 rht_dereference(pos->next, ht) : NULL)
1150 rhashtable_free_one(ht, pos, free_fn, arg);
1154 next_tbl = rht_dereference(tbl->future_tbl, ht);
1155 bucket_table_free(tbl);
1160 mutex_unlock(&ht->mutex);
1162 EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1164 void rhashtable_destroy(struct rhashtable *ht)
1166 return rhashtable_free_and_destroy(ht, NULL, NULL);
1168 EXPORT_SYMBOL_GPL(rhashtable_destroy);
1170 struct rhash_lock_head **__rht_bucket_nested(const struct bucket_table *tbl,
1173 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1174 unsigned int index = hash & ((1 << tbl->nest) - 1);
1175 unsigned int size = tbl->size >> tbl->nest;
1176 unsigned int subhash = hash;
1177 union nested_table *ntbl;
1179 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1180 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
1181 subhash >>= tbl->nest;
1183 while (ntbl && size > (1 << shift)) {
1184 index = subhash & ((1 << shift) - 1);
1185 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1194 return &ntbl[subhash].bucket;
1197 EXPORT_SYMBOL_GPL(__rht_bucket_nested);
1199 struct rhash_lock_head **rht_bucket_nested(const struct bucket_table *tbl,
1202 static struct rhash_lock_head *rhnull;
1205 INIT_RHT_NULLS_HEAD(rhnull);
1206 return __rht_bucket_nested(tbl, hash) ?: &rhnull;
1208 EXPORT_SYMBOL_GPL(rht_bucket_nested);
1210 struct rhash_lock_head **rht_bucket_nested_insert(struct rhashtable *ht,
1211 struct bucket_table *tbl,
1214 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1215 unsigned int index = hash & ((1 << tbl->nest) - 1);
1216 unsigned int size = tbl->size >> tbl->nest;
1217 union nested_table *ntbl;
1219 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1221 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1222 size <= (1 << shift));
1224 while (ntbl && size > (1 << shift)) {
1225 index = hash & ((1 << shift) - 1);
1228 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1229 size <= (1 << shift));
1235 return &ntbl[hash].bucket;
1238 EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);