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
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 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, unsigned int old_hash)
226 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
227 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
228 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
230 struct rhash_head *head, *next, *entry;
231 spinlock_t *new_bucket_lock;
232 unsigned int new_hash;
239 rht_for_each(entry, old_tbl, old_hash) {
241 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
243 if (rht_is_a_nulls(next))
246 pprev = &entry->next;
252 new_hash = head_hashfn(ht, new_tbl, entry);
254 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
256 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
257 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
260 RCU_INIT_POINTER(entry->next, head);
262 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
263 spin_unlock(new_bucket_lock);
265 rcu_assign_pointer(*pprev, next);
271 static int rhashtable_rehash_chain(struct rhashtable *ht,
272 unsigned int old_hash)
274 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
275 spinlock_t *old_bucket_lock;
278 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
280 spin_lock_bh(old_bucket_lock);
281 while (!(err = rhashtable_rehash_one(ht, old_hash)))
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)
334 /* Wait for readers. All new readers will see the new
335 * table, and thus no references to the old table will
337 * We do this inside the locked region so that
338 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
339 * to check if it should not re-link the table.
341 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
342 spin_unlock(&ht->lock);
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 | __GFP_NOWARN);
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_from(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;
586 new_tbl = rcu_dereference(ht->tbl);
590 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
591 spin_lock_bh(rht_bucket_lock(tbl, hash));
593 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
594 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
595 if (PTR_ERR(new_tbl) != -EEXIST)
596 data = ERR_CAST(new_tbl);
598 spin_unlock_bh(rht_bucket_lock(tbl, hash));
599 } while (!IS_ERR_OR_NULL(new_tbl));
601 if (PTR_ERR(data) == -EAGAIN)
602 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
608 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
609 struct rhash_head *obj)
615 data = rhashtable_try_insert(ht, key, obj);
617 } while (PTR_ERR(data) == -EAGAIN);
621 EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
624 * rhashtable_walk_enter - Initialise an iterator
625 * @ht: Table to walk over
626 * @iter: Hash table Iterator
628 * This function prepares a hash table walk.
630 * Note that if you restart a walk after rhashtable_walk_stop you
631 * may see the same object twice. Also, you may miss objects if
632 * there are removals in between rhashtable_walk_stop and the next
633 * call to rhashtable_walk_start.
635 * For a completely stable walk you should construct your own data
636 * structure outside the hash table.
638 * This function may be called from any process context, including
639 * non-preemptable context, but cannot be called from softirq or
642 * You must call rhashtable_walk_exit after this function returns.
644 void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
650 iter->end_of_table = 0;
652 spin_lock(&ht->lock);
654 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
655 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
656 spin_unlock(&ht->lock);
658 EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
661 * rhashtable_walk_exit - Free an iterator
662 * @iter: Hash table Iterator
664 * This function frees resources allocated by rhashtable_walk_enter.
666 void rhashtable_walk_exit(struct rhashtable_iter *iter)
668 spin_lock(&iter->ht->lock);
669 if (iter->walker.tbl)
670 list_del(&iter->walker.list);
671 spin_unlock(&iter->ht->lock);
673 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
676 * rhashtable_walk_start_check - Start a hash table walk
677 * @iter: Hash table iterator
679 * Start a hash table walk at the current iterator position. Note that we take
680 * the RCU lock in all cases including when we return an error. So you must
681 * always call rhashtable_walk_stop to clean up.
683 * Returns zero if successful.
685 * Returns -EAGAIN if resize event occured. Note that the iterator
686 * will rewind back to the beginning and you may use it immediately
687 * by calling rhashtable_walk_next.
689 * rhashtable_walk_start is defined as an inline variant that returns
690 * void. This is preferred in cases where the caller would ignore
691 * resize events and always continue.
693 int rhashtable_walk_start_check(struct rhashtable_iter *iter)
696 struct rhashtable *ht = iter->ht;
697 bool rhlist = ht->rhlist;
701 spin_lock(&ht->lock);
702 if (iter->walker.tbl)
703 list_del(&iter->walker.list);
704 spin_unlock(&ht->lock);
706 if (iter->end_of_table)
708 if (!iter->walker.tbl) {
709 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
715 if (iter->p && !rhlist) {
717 * We need to validate that 'p' is still in the table, and
718 * if so, update 'skip'
720 struct rhash_head *p;
722 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
730 } else if (iter->p && rhlist) {
731 /* Need to validate that 'list' is still in the table, and
732 * if so, update 'skip' and 'p'.
734 struct rhash_head *p;
735 struct rhlist_head *list;
737 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
738 for (list = container_of(p, struct rhlist_head, rhead);
740 list = rcu_dereference(list->next)) {
742 if (list == iter->list) {
754 EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
757 * __rhashtable_walk_find_next - Find the next element in a table (or the first
758 * one in case of a new walk).
760 * @iter: Hash table iterator
762 * Returns the found object or NULL when the end of the table is reached.
764 * Returns -EAGAIN if resize event occurred.
766 static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
768 struct bucket_table *tbl = iter->walker.tbl;
769 struct rhlist_head *list = iter->list;
770 struct rhashtable *ht = iter->ht;
771 struct rhash_head *p = iter->p;
772 bool rhlist = ht->rhlist;
777 for (; iter->slot < tbl->size; iter->slot++) {
778 int skip = iter->skip;
780 rht_for_each_rcu(p, tbl, iter->slot) {
782 list = container_of(p, struct rhlist_head,
788 list = rcu_dereference(list->next);
799 if (!rht_is_a_nulls(p)) {
803 return rht_obj(ht, rhlist ? &list->rhead : p);
811 /* Ensure we see any new tables. */
814 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
815 if (iter->walker.tbl) {
818 return ERR_PTR(-EAGAIN);
820 iter->end_of_table = true;
827 * rhashtable_walk_next - Return the next object and advance the iterator
828 * @iter: Hash table iterator
830 * Note that you must call rhashtable_walk_stop when you are finished
833 * Returns the next object or NULL when the end of the table is reached.
835 * Returns -EAGAIN if resize event occurred. Note that the iterator
836 * will rewind back to the beginning and you may continue to use it.
838 void *rhashtable_walk_next(struct rhashtable_iter *iter)
840 struct rhlist_head *list = iter->list;
841 struct rhashtable *ht = iter->ht;
842 struct rhash_head *p = iter->p;
843 bool rhlist = ht->rhlist;
846 if (!rhlist || !(list = rcu_dereference(list->next))) {
847 p = rcu_dereference(p->next);
848 list = container_of(p, struct rhlist_head, rhead);
850 if (!rht_is_a_nulls(p)) {
854 return rht_obj(ht, rhlist ? &list->rhead : p);
857 /* At the end of this slot, switch to next one and then find
858 * next entry from that point.
864 return __rhashtable_walk_find_next(iter);
866 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
869 * rhashtable_walk_peek - Return the next object but don't advance the iterator
870 * @iter: Hash table iterator
872 * Returns the next object or NULL when the end of the table is reached.
874 * Returns -EAGAIN if resize event occurred. Note that the iterator
875 * will rewind back to the beginning and you may continue to use it.
877 void *rhashtable_walk_peek(struct rhashtable_iter *iter)
879 struct rhlist_head *list = iter->list;
880 struct rhashtable *ht = iter->ht;
881 struct rhash_head *p = iter->p;
884 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
886 /* No object found in current iter, find next one in the table. */
889 /* A nonzero skip value points to the next entry in the table
890 * beyond that last one that was found. Decrement skip so
891 * we find the current value. __rhashtable_walk_find_next
892 * will restore the original value of skip assuming that
893 * the table hasn't changed.
898 return __rhashtable_walk_find_next(iter);
900 EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
903 * rhashtable_walk_stop - Finish a hash table walk
904 * @iter: Hash table iterator
906 * Finish a hash table walk. Does not reset the iterator to the start of the
909 void rhashtable_walk_stop(struct rhashtable_iter *iter)
912 struct rhashtable *ht;
913 struct bucket_table *tbl = iter->walker.tbl;
920 spin_lock(&ht->lock);
921 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
922 /* This bucket table is being freed, don't re-link it. */
923 iter->walker.tbl = NULL;
925 list_add(&iter->walker.list, &tbl->walkers);
926 spin_unlock(&ht->lock);
931 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
933 static size_t rounded_hashtable_size(const struct rhashtable_params *params)
937 if (params->nelem_hint)
938 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
939 (unsigned long)params->min_size);
941 retsize = max(HASH_DEFAULT_SIZE,
942 (unsigned long)params->min_size);
947 static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
949 return jhash2(key, length, seed);
953 * rhashtable_init - initialize a new hash table
954 * @ht: hash table to be initialized
955 * @params: configuration parameters
957 * Initializes a new hash table based on the provided configuration
958 * parameters. A table can be configured either with a variable or
961 * Configuration Example 1: Fixed length keys
965 * struct rhash_head node;
968 * struct rhashtable_params params = {
969 * .head_offset = offsetof(struct test_obj, node),
970 * .key_offset = offsetof(struct test_obj, key),
971 * .key_len = sizeof(int),
975 * Configuration Example 2: Variable length keys
978 * struct rhash_head node;
981 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
983 * struct test_obj *obj = data;
985 * return [... hash ...];
988 * struct rhashtable_params params = {
989 * .head_offset = offsetof(struct test_obj, node),
991 * .obj_hashfn = my_hash_fn,
994 int rhashtable_init(struct rhashtable *ht,
995 const struct rhashtable_params *params)
997 struct bucket_table *tbl;
1000 if ((!params->key_len && !params->obj_hashfn) ||
1001 (params->obj_hashfn && !params->obj_cmpfn))
1004 memset(ht, 0, sizeof(*ht));
1005 mutex_init(&ht->mutex);
1006 spin_lock_init(&ht->lock);
1007 memcpy(&ht->p, params, sizeof(*params));
1009 if (params->min_size)
1010 ht->p.min_size = roundup_pow_of_two(params->min_size);
1012 /* Cap total entries at 2^31 to avoid nelems overflow. */
1013 ht->max_elems = 1u << 31;
1015 if (params->max_size) {
1016 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1017 if (ht->p.max_size < ht->max_elems / 2)
1018 ht->max_elems = ht->p.max_size * 2;
1021 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1023 size = rounded_hashtable_size(&ht->p);
1025 if (params->locks_mul)
1026 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1028 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1030 ht->key_len = ht->p.key_len;
1031 if (!params->hashfn) {
1032 ht->p.hashfn = jhash;
1034 if (!(ht->key_len & (sizeof(u32) - 1))) {
1035 ht->key_len /= sizeof(u32);
1036 ht->p.hashfn = rhashtable_jhash2;
1041 * This is api initialization and thus we need to guarantee the
1042 * initial rhashtable allocation. Upon failure, retry with the
1043 * smallest possible size with __GFP_NOFAIL semantics.
1045 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
1046 if (unlikely(tbl == NULL)) {
1047 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1048 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1051 atomic_set(&ht->nelems, 0);
1053 RCU_INIT_POINTER(ht->tbl, tbl);
1055 INIT_WORK(&ht->run_work, rht_deferred_worker);
1059 EXPORT_SYMBOL_GPL(rhashtable_init);
1062 * rhltable_init - initialize a new hash list table
1063 * @hlt: hash list table to be initialized
1064 * @params: configuration parameters
1066 * Initializes a new hash list table.
1068 * See documentation for rhashtable_init.
1070 int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1074 err = rhashtable_init(&hlt->ht, params);
1075 hlt->ht.rhlist = true;
1078 EXPORT_SYMBOL_GPL(rhltable_init);
1080 static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1081 void (*free_fn)(void *ptr, void *arg),
1084 struct rhlist_head *list;
1087 free_fn(rht_obj(ht, obj), arg);
1091 list = container_of(obj, struct rhlist_head, rhead);
1094 list = rht_dereference(list->next, ht);
1095 free_fn(rht_obj(ht, obj), arg);
1100 * rhashtable_free_and_destroy - free elements and destroy hash table
1101 * @ht: the hash table to destroy
1102 * @free_fn: callback to release resources of element
1103 * @arg: pointer passed to free_fn
1105 * Stops an eventual async resize. If defined, invokes free_fn for each
1106 * element to releasal resources. Please note that RCU protected
1107 * readers may still be accessing the elements. Releasing of resources
1108 * must occur in a compatible manner. Then frees the bucket array.
1110 * This function will eventually sleep to wait for an async resize
1111 * to complete. The caller is responsible that no further write operations
1112 * occurs in parallel.
1114 void rhashtable_free_and_destroy(struct rhashtable *ht,
1115 void (*free_fn)(void *ptr, void *arg),
1118 struct bucket_table *tbl, *next_tbl;
1121 cancel_work_sync(&ht->run_work);
1123 mutex_lock(&ht->mutex);
1124 tbl = rht_dereference(ht->tbl, ht);
1127 for (i = 0; i < tbl->size; i++) {
1128 struct rhash_head *pos, *next;
1131 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
1132 next = !rht_is_a_nulls(pos) ?
1133 rht_dereference(pos->next, ht) : NULL;
1134 !rht_is_a_nulls(pos);
1136 next = !rht_is_a_nulls(pos) ?
1137 rht_dereference(pos->next, ht) : NULL)
1138 rhashtable_free_one(ht, pos, free_fn, arg);
1142 next_tbl = rht_dereference(tbl->future_tbl, ht);
1143 bucket_table_free(tbl);
1148 mutex_unlock(&ht->mutex);
1150 EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1152 void rhashtable_destroy(struct rhashtable *ht)
1154 return rhashtable_free_and_destroy(ht, NULL, NULL);
1156 EXPORT_SYMBOL_GPL(rhashtable_destroy);
1158 struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1161 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1162 static struct rhash_head __rcu *rhnull;
1163 unsigned int index = hash & ((1 << tbl->nest) - 1);
1164 unsigned int size = tbl->size >> tbl->nest;
1165 unsigned int subhash = hash;
1166 union nested_table *ntbl;
1168 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1169 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
1170 subhash >>= tbl->nest;
1172 while (ntbl && size > (1 << shift)) {
1173 index = subhash & ((1 << shift) - 1);
1174 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1182 INIT_RHT_NULLS_HEAD(rhnull);
1186 return &ntbl[subhash].bucket;
1189 EXPORT_SYMBOL_GPL(rht_bucket_nested);
1191 struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1192 struct bucket_table *tbl,
1195 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1196 unsigned int index = hash & ((1 << tbl->nest) - 1);
1197 unsigned int size = tbl->size >> tbl->nest;
1198 union nested_table *ntbl;
1200 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1202 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1203 size <= (1 << shift));
1205 while (ntbl && size > (1 << shift)) {
1206 index = hash & ((1 << shift) - 1);
1209 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1210 size <= (1 << shift));
1216 return &ntbl[hash].bucket;
1219 EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);