2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
5 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
6 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8 * Code partially derived from nft_hash
9 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/atomic.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/log2.h>
21 #include <linux/sched.h>
22 #include <linux/rculist.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
26 #include <linux/jhash.h>
27 #include <linux/random.h>
28 #include <linux/rhashtable.h>
29 #include <linux/err.h>
30 #include <linux/export.h>
32 #define HASH_DEFAULT_SIZE 64UL
33 #define HASH_MIN_SIZE 4U
34 #define BUCKET_LOCKS_PER_CPU 32UL
37 union nested_table __rcu *table;
38 struct rhash_head __rcu *bucket;
41 static u32 head_hashfn(struct rhashtable *ht,
42 const struct bucket_table *tbl,
43 const struct rhash_head *he)
45 return rht_head_hashfn(ht, tbl, he, ht->p);
48 #ifdef CONFIG_PROVE_LOCKING
49 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
51 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
53 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
55 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
57 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
59 spinlock_t *lock = rht_bucket_lock(tbl, hash);
61 return (debug_locks) ? lockdep_is_held(lock) : 1;
63 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
65 #define ASSERT_RHT_MUTEX(HT)
69 static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl,
73 #if defined(CONFIG_PROVE_LOCKING)
74 unsigned int nr_pcpus = 2;
76 unsigned int nr_pcpus = num_possible_cpus();
79 nr_pcpus = min_t(unsigned int, nr_pcpus, 64UL);
80 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
82 /* Never allocate more than 0.5 locks per bucket */
83 size = min_t(unsigned int, size, tbl->size >> 1);
86 size = min(size, 1U << tbl->nest);
88 if (sizeof(spinlock_t) != 0) {
89 if (gfpflags_allow_blocking(gfp))
90 tbl->locks = kvmalloc(size * sizeof(spinlock_t), gfp);
92 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
96 for (i = 0; i < size; i++)
97 spin_lock_init(&tbl->locks[i]);
99 tbl->locks_mask = size - 1;
104 static void nested_table_free(union nested_table *ntbl, unsigned int size)
106 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
107 const unsigned int len = 1 << shift;
110 ntbl = rcu_dereference_raw(ntbl->table);
116 for (i = 0; i < len; i++)
117 nested_table_free(ntbl + i, size);
123 static void nested_bucket_table_free(const struct bucket_table *tbl)
125 unsigned int size = tbl->size >> tbl->nest;
126 unsigned int len = 1 << tbl->nest;
127 union nested_table *ntbl;
130 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
132 for (i = 0; i < len; i++)
133 nested_table_free(ntbl + i, size);
138 static void bucket_table_free(const struct bucket_table *tbl)
141 nested_bucket_table_free(tbl);
147 static void bucket_table_free_rcu(struct rcu_head *head)
149 bucket_table_free(container_of(head, struct bucket_table, rcu));
152 static union nested_table *nested_table_alloc(struct rhashtable *ht,
153 union nested_table __rcu **prev,
154 unsigned int shifted,
157 union nested_table *ntbl;
160 ntbl = rcu_dereference(*prev);
164 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
166 if (ntbl && shifted) {
167 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0].bucket); i++)
168 INIT_RHT_NULLS_HEAD(ntbl[i].bucket, ht,
169 (i << shifted) | nhash);
172 rcu_assign_pointer(*prev, ntbl);
177 static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
181 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
182 struct bucket_table *tbl;
185 if (nbuckets < (1 << (shift + 1)))
188 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
190 tbl = kzalloc(size, gfp);
194 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
200 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
205 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
209 struct bucket_table *tbl = NULL;
213 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
214 if (gfp != GFP_KERNEL)
215 tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
217 tbl = kvzalloc(size, gfp);
221 if (tbl == NULL && gfp != GFP_KERNEL) {
222 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
230 if (alloc_bucket_locks(ht, tbl, gfp) < 0) {
231 bucket_table_free(tbl);
235 INIT_LIST_HEAD(&tbl->walkers);
237 tbl->hash_rnd = get_random_u32();
239 for (i = 0; i < nbuckets; i++)
240 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
245 static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
246 struct bucket_table *tbl)
248 struct bucket_table *new_tbl;
252 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
258 static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
260 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
261 struct bucket_table *new_tbl = rhashtable_last_table(ht,
262 rht_dereference_rcu(old_tbl->future_tbl, ht));
263 struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash);
265 struct rhash_head *head, *next, *entry;
266 spinlock_t *new_bucket_lock;
267 unsigned int new_hash;
274 rht_for_each(entry, old_tbl, old_hash) {
276 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
278 if (rht_is_a_nulls(next))
281 pprev = &entry->next;
287 new_hash = head_hashfn(ht, new_tbl, entry);
289 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
291 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
292 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
295 RCU_INIT_POINTER(entry->next, head);
297 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
298 spin_unlock(new_bucket_lock);
300 rcu_assign_pointer(*pprev, next);
306 static int rhashtable_rehash_chain(struct rhashtable *ht,
307 unsigned int old_hash)
309 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
310 spinlock_t *old_bucket_lock;
313 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
315 spin_lock_bh(old_bucket_lock);
316 while (!(err = rhashtable_rehash_one(ht, old_hash)))
319 if (err == -ENOENT) {
323 spin_unlock_bh(old_bucket_lock);
328 static int rhashtable_rehash_attach(struct rhashtable *ht,
329 struct bucket_table *old_tbl,
330 struct bucket_table *new_tbl)
332 /* Protect future_tbl using the first bucket lock. */
333 spin_lock_bh(old_tbl->locks);
335 /* Did somebody beat us to it? */
336 if (rcu_access_pointer(old_tbl->future_tbl)) {
337 spin_unlock_bh(old_tbl->locks);
341 /* Make insertions go into the new, empty table right away. Deletions
342 * and lookups will be attempted in both tables until we synchronize.
344 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
346 spin_unlock_bh(old_tbl->locks);
351 static int rhashtable_rehash_table(struct rhashtable *ht)
353 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
354 struct bucket_table *new_tbl;
355 struct rhashtable_walker *walker;
356 unsigned int old_hash;
359 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
363 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
364 err = rhashtable_rehash_chain(ht, old_hash);
369 /* Publish the new table pointer. */
370 rcu_assign_pointer(ht->tbl, new_tbl);
372 spin_lock(&ht->lock);
373 list_for_each_entry(walker, &old_tbl->walkers, list)
375 spin_unlock(&ht->lock);
377 /* Wait for readers. All new readers will see the new
378 * table, and thus no references to the old table will
381 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
383 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
386 static int rhashtable_rehash_alloc(struct rhashtable *ht,
387 struct bucket_table *old_tbl,
390 struct bucket_table *new_tbl;
393 ASSERT_RHT_MUTEX(ht);
395 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
399 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
401 bucket_table_free(new_tbl);
407 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
408 * @ht: the hash table to shrink
410 * This function shrinks the hash table to fit, i.e., the smallest
411 * size would not cause it to expand right away automatically.
413 * The caller must ensure that no concurrent resizing occurs by holding
416 * The caller must ensure that no concurrent table mutations take place.
417 * It is however valid to have concurrent lookups if they are RCU protected.
419 * It is valid to have concurrent insertions and deletions protected by per
420 * bucket locks or concurrent RCU protected lookups and traversals.
422 static int rhashtable_shrink(struct rhashtable *ht)
424 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
425 unsigned int nelems = atomic_read(&ht->nelems);
426 unsigned int size = 0;
429 size = roundup_pow_of_two(nelems * 3 / 2);
430 if (size < ht->p.min_size)
431 size = ht->p.min_size;
433 if (old_tbl->size <= size)
436 if (rht_dereference(old_tbl->future_tbl, ht))
439 return rhashtable_rehash_alloc(ht, old_tbl, size);
442 static void rht_deferred_worker(struct work_struct *work)
444 struct rhashtable *ht;
445 struct bucket_table *tbl;
448 ht = container_of(work, struct rhashtable, run_work);
449 mutex_lock(&ht->mutex);
451 tbl = rht_dereference(ht->tbl, ht);
452 tbl = rhashtable_last_table(ht, tbl);
454 if (rht_grow_above_75(ht, tbl))
455 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
456 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
457 err = rhashtable_shrink(ht);
459 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
462 err = rhashtable_rehash_table(ht);
464 mutex_unlock(&ht->mutex);
467 schedule_work(&ht->run_work);
470 static int rhashtable_insert_rehash(struct rhashtable *ht,
471 struct bucket_table *tbl)
473 struct bucket_table *old_tbl;
474 struct bucket_table *new_tbl;
478 old_tbl = rht_dereference_rcu(ht->tbl, ht);
484 if (rht_grow_above_75(ht, tbl))
486 /* Do not schedule more than one rehash */
487 else if (old_tbl != tbl)
492 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
496 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
498 bucket_table_free(new_tbl);
502 schedule_work(&ht->run_work);
507 /* Do not fail the insert if someone else did a rehash. */
508 if (likely(rcu_dereference_raw(tbl->future_tbl)))
511 /* Schedule async rehash to retry allocation in process context. */
513 schedule_work(&ht->run_work);
518 static void *rhashtable_lookup_one(struct rhashtable *ht,
519 struct bucket_table *tbl, unsigned int hash,
520 const void *key, struct rhash_head *obj)
522 struct rhashtable_compare_arg arg = {
526 struct rhash_head __rcu **pprev;
527 struct rhash_head *head;
530 elasticity = RHT_ELASTICITY;
531 pprev = rht_bucket_var(tbl, hash);
532 rht_for_each_continue(head, *pprev, tbl, hash) {
533 struct rhlist_head *list;
534 struct rhlist_head *plist;
539 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
540 rhashtable_compare(&arg, rht_obj(ht, head)))) {
546 return rht_obj(ht, head);
548 list = container_of(obj, struct rhlist_head, rhead);
549 plist = container_of(head, struct rhlist_head, rhead);
551 RCU_INIT_POINTER(list->next, plist);
552 head = rht_dereference_bucket(head->next, tbl, hash);
553 RCU_INIT_POINTER(list->rhead.next, head);
554 rcu_assign_pointer(*pprev, obj);
560 return ERR_PTR(-EAGAIN);
562 return ERR_PTR(-ENOENT);
565 static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
566 struct bucket_table *tbl,
568 struct rhash_head *obj,
571 struct rhash_head __rcu **pprev;
572 struct bucket_table *new_tbl;
573 struct rhash_head *head;
575 if (!IS_ERR_OR_NULL(data))
576 return ERR_PTR(-EEXIST);
578 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
579 return ERR_CAST(data);
581 new_tbl = rcu_dereference(tbl->future_tbl);
585 if (PTR_ERR(data) != -ENOENT)
586 return ERR_CAST(data);
588 if (unlikely(rht_grow_above_max(ht, tbl)))
589 return ERR_PTR(-E2BIG);
591 if (unlikely(rht_grow_above_100(ht, tbl)))
592 return ERR_PTR(-EAGAIN);
594 pprev = rht_bucket_insert(ht, tbl, hash);
596 return ERR_PTR(-ENOMEM);
598 head = rht_dereference_bucket(*pprev, tbl, hash);
600 RCU_INIT_POINTER(obj->next, head);
602 struct rhlist_head *list;
604 list = container_of(obj, struct rhlist_head, rhead);
605 RCU_INIT_POINTER(list->next, NULL);
608 rcu_assign_pointer(*pprev, obj);
610 atomic_inc(&ht->nelems);
611 if (rht_grow_above_75(ht, tbl))
612 schedule_work(&ht->run_work);
617 static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
618 struct rhash_head *obj)
620 struct bucket_table *new_tbl;
621 struct bucket_table *tbl;
626 tbl = rcu_dereference(ht->tbl);
628 /* All insertions must grab the oldest table containing
629 * the hashed bucket that is yet to be rehashed.
632 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
633 lock = rht_bucket_lock(tbl, hash);
636 if (tbl->rehash <= hash)
639 spin_unlock_bh(lock);
640 tbl = rcu_dereference(tbl->future_tbl);
643 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
644 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
645 if (PTR_ERR(new_tbl) != -EEXIST)
646 data = ERR_CAST(new_tbl);
648 while (!IS_ERR_OR_NULL(new_tbl)) {
650 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
651 spin_lock_nested(rht_bucket_lock(tbl, hash),
652 SINGLE_DEPTH_NESTING);
654 data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
655 new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
656 if (PTR_ERR(new_tbl) != -EEXIST)
657 data = ERR_CAST(new_tbl);
659 spin_unlock(rht_bucket_lock(tbl, hash));
662 spin_unlock_bh(lock);
664 if (PTR_ERR(data) == -EAGAIN)
665 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
671 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
672 struct rhash_head *obj)
678 data = rhashtable_try_insert(ht, key, obj);
680 } while (PTR_ERR(data) == -EAGAIN);
684 EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
687 * rhashtable_walk_enter - Initialise an iterator
688 * @ht: Table to walk over
689 * @iter: Hash table Iterator
691 * This function prepares a hash table walk.
693 * Note that if you restart a walk after rhashtable_walk_stop you
694 * may see the same object twice. Also, you may miss objects if
695 * there are removals in between rhashtable_walk_stop and the next
696 * call to rhashtable_walk_start.
698 * For a completely stable walk you should construct your own data
699 * structure outside the hash table.
701 * This function may sleep so you must not call it from interrupt
702 * context or with spin locks held.
704 * You must call rhashtable_walk_exit after this function returns.
706 void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
713 spin_lock(&ht->lock);
715 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
716 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
717 spin_unlock(&ht->lock);
719 EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
722 * rhashtable_walk_exit - Free an iterator
723 * @iter: Hash table Iterator
725 * This function frees resources allocated by rhashtable_walk_init.
727 void rhashtable_walk_exit(struct rhashtable_iter *iter)
729 spin_lock(&iter->ht->lock);
730 if (iter->walker.tbl)
731 list_del(&iter->walker.list);
732 spin_unlock(&iter->ht->lock);
734 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
737 * rhashtable_walk_start - Start a hash table walk
738 * @iter: Hash table iterator
740 * Start a hash table walk at the current iterator position. Note that we take
741 * the RCU lock in all cases including when we return an error. So you must
742 * always call rhashtable_walk_stop to clean up.
744 * Returns zero if successful.
746 * Returns -EAGAIN if resize event occured. Note that the iterator
747 * will rewind back to the beginning and you may use it immediately
748 * by calling rhashtable_walk_next.
750 int rhashtable_walk_start(struct rhashtable_iter *iter)
753 struct rhashtable *ht = iter->ht;
757 spin_lock(&ht->lock);
758 if (iter->walker.tbl)
759 list_del(&iter->walker.list);
760 spin_unlock(&ht->lock);
762 if (!iter->walker.tbl) {
763 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
769 EXPORT_SYMBOL_GPL(rhashtable_walk_start);
772 * rhashtable_walk_next - Return the next object and advance the iterator
773 * @iter: Hash table iterator
775 * Note that you must call rhashtable_walk_stop when you are finished
778 * Returns the next object or NULL when the end of the table is reached.
780 * Returns -EAGAIN if resize event occured. Note that the iterator
781 * will rewind back to the beginning and you may continue to use it.
783 void *rhashtable_walk_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;
792 if (!rhlist || !(list = rcu_dereference(list->next))) {
793 p = rcu_dereference(p->next);
794 list = container_of(p, struct rhlist_head, rhead);
799 for (; iter->slot < tbl->size; iter->slot++) {
800 int skip = iter->skip;
802 rht_for_each_rcu(p, tbl, iter->slot) {
804 list = container_of(p, struct rhlist_head,
810 list = rcu_dereference(list->next);
821 if (!rht_is_a_nulls(p)) {
825 return rht_obj(ht, rhlist ? &list->rhead : p);
833 /* Ensure we see any new tables. */
836 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
837 if (iter->walker.tbl) {
840 return ERR_PTR(-EAGAIN);
845 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
848 * rhashtable_walk_stop - Finish a hash table walk
849 * @iter: Hash table iterator
851 * Finish a hash table walk. Does not reset the iterator to the start of the
854 void rhashtable_walk_stop(struct rhashtable_iter *iter)
857 struct rhashtable *ht;
858 struct bucket_table *tbl = iter->walker.tbl;
865 spin_lock(&ht->lock);
866 if (tbl->rehash < tbl->size)
867 list_add(&iter->walker.list, &tbl->walkers);
869 iter->walker.tbl = NULL;
870 spin_unlock(&ht->lock);
877 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
879 static size_t rounded_hashtable_size(const struct rhashtable_params *params)
881 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
882 (unsigned long)params->min_size);
885 static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
887 return jhash2(key, length, seed);
891 * rhashtable_init - initialize a new hash table
892 * @ht: hash table to be initialized
893 * @params: configuration parameters
895 * Initializes a new hash table based on the provided configuration
896 * parameters. A table can be configured either with a variable or
899 * Configuration Example 1: Fixed length keys
903 * struct rhash_head node;
906 * struct rhashtable_params params = {
907 * .head_offset = offsetof(struct test_obj, node),
908 * .key_offset = offsetof(struct test_obj, key),
909 * .key_len = sizeof(int),
911 * .nulls_base = (1U << RHT_BASE_SHIFT),
914 * Configuration Example 2: Variable length keys
917 * struct rhash_head node;
920 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
922 * struct test_obj *obj = data;
924 * return [... hash ...];
927 * struct rhashtable_params params = {
928 * .head_offset = offsetof(struct test_obj, node),
930 * .obj_hashfn = my_hash_fn,
933 int rhashtable_init(struct rhashtable *ht,
934 const struct rhashtable_params *params)
936 struct bucket_table *tbl;
939 size = HASH_DEFAULT_SIZE;
941 if ((!params->key_len && !params->obj_hashfn) ||
942 (params->obj_hashfn && !params->obj_cmpfn))
945 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
948 memset(ht, 0, sizeof(*ht));
949 mutex_init(&ht->mutex);
950 spin_lock_init(&ht->lock);
951 memcpy(&ht->p, params, sizeof(*params));
953 if (params->min_size)
954 ht->p.min_size = roundup_pow_of_two(params->min_size);
956 /* Cap total entries at 2^31 to avoid nelems overflow. */
957 ht->max_elems = 1u << 31;
959 if (params->max_size) {
960 ht->p.max_size = rounddown_pow_of_two(params->max_size);
961 if (ht->p.max_size < ht->max_elems / 2)
962 ht->max_elems = ht->p.max_size * 2;
965 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
967 if (params->nelem_hint)
968 size = rounded_hashtable_size(&ht->p);
970 if (params->locks_mul)
971 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
973 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
975 ht->key_len = ht->p.key_len;
976 if (!params->hashfn) {
977 ht->p.hashfn = jhash;
979 if (!(ht->key_len & (sizeof(u32) - 1))) {
980 ht->key_len /= sizeof(u32);
981 ht->p.hashfn = rhashtable_jhash2;
985 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
989 atomic_set(&ht->nelems, 0);
991 RCU_INIT_POINTER(ht->tbl, tbl);
993 INIT_WORK(&ht->run_work, rht_deferred_worker);
997 EXPORT_SYMBOL_GPL(rhashtable_init);
1000 * rhltable_init - initialize a new hash list table
1001 * @hlt: hash list table to be initialized
1002 * @params: configuration parameters
1004 * Initializes a new hash list table.
1006 * See documentation for rhashtable_init.
1008 int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1012 /* No rhlist NULLs marking for now. */
1013 if (params->nulls_base)
1016 err = rhashtable_init(&hlt->ht, params);
1017 hlt->ht.rhlist = true;
1020 EXPORT_SYMBOL_GPL(rhltable_init);
1022 static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1023 void (*free_fn)(void *ptr, void *arg),
1026 struct rhlist_head *list;
1029 free_fn(rht_obj(ht, obj), arg);
1033 list = container_of(obj, struct rhlist_head, rhead);
1036 list = rht_dereference(list->next, ht);
1037 free_fn(rht_obj(ht, obj), arg);
1042 * rhashtable_free_and_destroy - free elements and destroy hash table
1043 * @ht: the hash table to destroy
1044 * @free_fn: callback to release resources of element
1045 * @arg: pointer passed to free_fn
1047 * Stops an eventual async resize. If defined, invokes free_fn for each
1048 * element to releasal resources. Please note that RCU protected
1049 * readers may still be accessing the elements. Releasing of resources
1050 * must occur in a compatible manner. Then frees the bucket array.
1052 * This function will eventually sleep to wait for an async resize
1053 * to complete. The caller is responsible that no further write operations
1054 * occurs in parallel.
1056 void rhashtable_free_and_destroy(struct rhashtable *ht,
1057 void (*free_fn)(void *ptr, void *arg),
1060 struct bucket_table *tbl;
1063 cancel_work_sync(&ht->run_work);
1065 mutex_lock(&ht->mutex);
1066 tbl = rht_dereference(ht->tbl, ht);
1068 for (i = 0; i < tbl->size; i++) {
1069 struct rhash_head *pos, *next;
1071 for (pos = rht_dereference(*rht_bucket(tbl, i), ht),
1072 next = !rht_is_a_nulls(pos) ?
1073 rht_dereference(pos->next, ht) : NULL;
1074 !rht_is_a_nulls(pos);
1076 next = !rht_is_a_nulls(pos) ?
1077 rht_dereference(pos->next, ht) : NULL)
1078 rhashtable_free_one(ht, pos, free_fn, arg);
1082 bucket_table_free(tbl);
1083 mutex_unlock(&ht->mutex);
1085 EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1087 void rhashtable_destroy(struct rhashtable *ht)
1089 return rhashtable_free_and_destroy(ht, NULL, NULL);
1091 EXPORT_SYMBOL_GPL(rhashtable_destroy);
1093 struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1096 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1097 static struct rhash_head __rcu *rhnull =
1098 (struct rhash_head __rcu *)NULLS_MARKER(0);
1099 unsigned int index = hash & ((1 << tbl->nest) - 1);
1100 unsigned int size = tbl->size >> tbl->nest;
1101 unsigned int subhash = hash;
1102 union nested_table *ntbl;
1104 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1105 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
1106 subhash >>= tbl->nest;
1108 while (ntbl && size > (1 << shift)) {
1109 index = subhash & ((1 << shift) - 1);
1110 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1119 return &ntbl[subhash].bucket;
1122 EXPORT_SYMBOL_GPL(rht_bucket_nested);
1124 struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
1125 struct bucket_table *tbl,
1128 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1129 unsigned int index = hash & ((1 << tbl->nest) - 1);
1130 unsigned int size = tbl->size >> tbl->nest;
1131 union nested_table *ntbl;
1132 unsigned int shifted;
1135 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1138 shifted = tbl->nest;
1139 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1140 size <= (1 << shift) ? shifted : 0, nhash);
1142 while (ntbl && size > (1 << shift)) {
1143 index = hash & ((1 << shift) - 1);
1146 nhash |= index << shifted;
1148 ntbl = nested_table_alloc(ht, &ntbl[index].table,
1149 size <= (1 << shift) ? shifted : 0,
1156 return &ntbl[hash].bucket;
1159 EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);