2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 * Based on the following paper:
8 * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
10 * Code partially derived from nft_hash
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/kernel.h>
18 #include <linux/init.h>
19 #include <linux/log2.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
24 #include <linux/jhash.h>
25 #include <linux/random.h>
26 #include <linux/rhashtable.h>
27 #include <linux/err.h>
29 #define HASH_DEFAULT_SIZE 64UL
30 #define HASH_MIN_SIZE 4UL
31 #define BUCKET_LOCKS_PER_CPU 128UL
33 /* Base bits plus 1 bit for nulls marker */
34 #define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
41 /* The bucket lock is selected based on the hash and protects mutations
42 * on a group of hash buckets.
44 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
45 * a single lock always covers both buckets which may both contains
46 * entries which link to the same bucket of the old table during resizing.
47 * This allows to simplify the locking as locking the bucket in both
48 * tables during resize always guarantee protection.
50 * IMPORTANT: When holding the bucket lock of both the old and new table
51 * during expansions and shrinking, the old bucket lock must always be
54 static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
56 return &tbl->locks[hash & tbl->locks_mask];
59 static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
61 return (void *) he - ht->p.head_offset;
64 static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
66 return hash & (tbl->size - 1);
69 static u32 obj_raw_hashfn(struct rhashtable *ht,
70 const struct bucket_table *tbl, const void *ptr)
74 if (unlikely(!ht->p.key_len))
75 hash = ht->p.obj_hashfn(ptr, tbl->hash_rnd);
77 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
80 return hash >> HASH_RESERVED_SPACE;
83 static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl,
84 const void *key, u32 len)
86 return rht_bucket_index(tbl, ht->p.hashfn(key, len, tbl->hash_rnd) >>
90 static u32 head_hashfn(struct rhashtable *ht,
91 const struct bucket_table *tbl,
92 const struct rhash_head *he)
94 return rht_bucket_index(tbl, obj_raw_hashfn(ht, tbl, rht_obj(ht, he)));
97 #ifdef CONFIG_PROVE_LOCKING
98 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
100 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
102 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
104 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
106 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
108 spinlock_t *lock = bucket_lock(tbl, hash);
110 return (debug_locks) ? lockdep_is_held(lock) : 1;
112 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
114 #define ASSERT_RHT_MUTEX(HT)
118 static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
120 unsigned int i, size;
121 #if defined(CONFIG_PROVE_LOCKING)
122 unsigned int nr_pcpus = 2;
124 unsigned int nr_pcpus = num_possible_cpus();
127 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
128 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
130 /* Never allocate more than 0.5 locks per bucket */
131 size = min_t(unsigned int, size, tbl->size >> 1);
133 if (sizeof(spinlock_t) != 0) {
135 if (size * sizeof(spinlock_t) > PAGE_SIZE)
136 tbl->locks = vmalloc(size * sizeof(spinlock_t));
139 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
143 for (i = 0; i < size; i++)
144 spin_lock_init(&tbl->locks[i]);
146 tbl->locks_mask = size - 1;
151 static void bucket_table_free(const struct bucket_table *tbl)
159 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
162 struct bucket_table *tbl = NULL;
166 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
167 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
168 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
174 tbl->size = nbuckets;
176 if (alloc_bucket_locks(ht, tbl) < 0) {
177 bucket_table_free(tbl);
181 for (i = 0; i < nbuckets; i++)
182 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
188 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
190 * @new_size: new table size
192 static bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
194 /* Expand table when exceeding 75% load */
195 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
196 (!ht->p.max_shift || atomic_read(&ht->shift) < ht->p.max_shift);
200 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
202 * @new_size: new table size
204 static bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
206 /* Shrink table beneath 30% load */
207 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
208 (atomic_read(&ht->shift) > ht->p.min_shift);
211 static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
213 struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht);
214 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
215 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
217 struct rhash_head *head, *next, *entry;
218 spinlock_t *new_bucket_lock;
221 rht_for_each(entry, old_tbl, old_hash) {
223 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
225 if (rht_is_a_nulls(next))
228 pprev = &entry->next;
234 new_hash = head_hashfn(ht, new_tbl, entry);
236 new_bucket_lock = bucket_lock(new_tbl, new_hash);
238 spin_lock_nested(new_bucket_lock, RHT_LOCK_NESTED);
239 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
242 if (rht_is_a_nulls(head))
243 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
245 RCU_INIT_POINTER(entry->next, head);
247 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
248 spin_unlock(new_bucket_lock);
250 rcu_assign_pointer(*pprev, next);
256 static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
258 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
259 spinlock_t *old_bucket_lock;
261 old_bucket_lock = bucket_lock(old_tbl, old_hash);
263 spin_lock_bh(old_bucket_lock);
264 while (!rhashtable_rehash_one(ht, old_hash))
266 spin_unlock_bh(old_bucket_lock);
269 static void rhashtable_rehash(struct rhashtable *ht,
270 struct bucket_table *new_tbl)
272 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
275 get_random_bytes(&new_tbl->hash_rnd, sizeof(new_tbl->hash_rnd));
277 /* Make insertions go into the new, empty table right away. Deletions
278 * and lookups will be attempted in both tables until we synchronize.
279 * The synchronize_rcu() guarantees for the new table to be picked up
280 * so no new additions go into the old table while we relink.
282 rcu_assign_pointer(ht->future_tbl, new_tbl);
284 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
285 rhashtable_rehash_chain(ht, old_hash);
287 /* Publish the new table pointer. */
288 rcu_assign_pointer(ht->tbl, new_tbl);
290 /* Wait for readers. All new readers will see the new
291 * table, and thus no references to the old table will
296 bucket_table_free(old_tbl);
300 * rhashtable_expand - Expand hash table while allowing concurrent lookups
301 * @ht: the hash table to expand
303 * A secondary bucket array is allocated and the hash entries are migrated.
305 * This function may only be called in a context where it is safe to call
306 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
308 * The caller must ensure that no concurrent resizing occurs by holding
311 * It is valid to have concurrent insertions and deletions protected by per
312 * bucket locks or concurrent RCU protected lookups and traversals.
314 int rhashtable_expand(struct rhashtable *ht)
316 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
318 ASSERT_RHT_MUTEX(ht);
320 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
324 new_tbl->hash_rnd = old_tbl->hash_rnd;
326 atomic_inc(&ht->shift);
328 rhashtable_rehash(ht, new_tbl);
332 EXPORT_SYMBOL_GPL(rhashtable_expand);
335 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
336 * @ht: the hash table to shrink
338 * This function may only be called in a context where it is safe to call
339 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
341 * The caller must ensure that no concurrent resizing occurs by holding
344 * The caller must ensure that no concurrent table mutations take place.
345 * It is however valid to have concurrent lookups if they are RCU protected.
347 * It is valid to have concurrent insertions and deletions protected by per
348 * bucket locks or concurrent RCU protected lookups and traversals.
350 int rhashtable_shrink(struct rhashtable *ht)
352 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
354 ASSERT_RHT_MUTEX(ht);
356 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
360 new_tbl->hash_rnd = tbl->hash_rnd;
362 atomic_dec(&ht->shift);
364 rhashtable_rehash(ht, new_tbl);
368 EXPORT_SYMBOL_GPL(rhashtable_shrink);
370 static void rht_deferred_worker(struct work_struct *work)
372 struct rhashtable *ht;
373 struct bucket_table *tbl;
374 struct rhashtable_walker *walker;
376 ht = container_of(work, struct rhashtable, run_work);
377 mutex_lock(&ht->mutex);
378 if (ht->being_destroyed)
381 tbl = rht_dereference(ht->tbl, ht);
383 list_for_each_entry(walker, &ht->walkers, list)
384 walker->resize = true;
386 if (rht_grow_above_75(ht, tbl->size))
387 rhashtable_expand(ht);
388 else if (rht_shrink_below_30(ht, tbl->size))
389 rhashtable_shrink(ht);
391 mutex_unlock(&ht->mutex);
394 static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
395 bool (*compare)(void *, void *), void *arg)
397 struct bucket_table *tbl, *old_tbl;
398 struct rhash_head *head;
399 bool no_resize_running;
405 old_tbl = rht_dereference_rcu(ht->tbl, ht);
406 hash = obj_raw_hashfn(ht, old_tbl, rht_obj(ht, obj));
408 spin_lock_bh(bucket_lock(old_tbl, hash));
410 /* Because we have already taken the bucket lock in old_tbl,
411 * if we find that future_tbl is not yet visible then that
412 * guarantees all other insertions of the same entry will
413 * also grab the bucket lock in old_tbl because until the
414 * rehash completes ht->tbl won't be changed.
416 tbl = rht_dereference_rcu(ht->future_tbl, ht);
417 if (tbl != old_tbl) {
418 hash = obj_raw_hashfn(ht, tbl, rht_obj(ht, obj));
419 spin_lock_nested(bucket_lock(tbl, hash), RHT_LOCK_NESTED);
423 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
429 no_resize_running = tbl == old_tbl;
431 hash = rht_bucket_index(tbl, hash);
432 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
434 if (rht_is_a_nulls(head))
435 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
437 RCU_INIT_POINTER(obj->next, head);
439 rcu_assign_pointer(tbl->buckets[hash], obj);
441 atomic_inc(&ht->nelems);
442 if (no_resize_running && rht_grow_above_75(ht, tbl->size))
443 schedule_work(&ht->run_work);
446 if (tbl != old_tbl) {
447 hash = obj_raw_hashfn(ht, tbl, rht_obj(ht, obj));
448 spin_unlock(bucket_lock(tbl, hash));
451 hash = obj_raw_hashfn(ht, old_tbl, rht_obj(ht, obj));
452 spin_unlock_bh(bucket_lock(old_tbl, hash));
460 * rhashtable_insert - insert object into hash table
462 * @obj: pointer to hash head inside object
464 * Will take a per bucket spinlock to protect against mutual mutations
465 * on the same bucket. Multiple insertions may occur in parallel unless
466 * they map to the same bucket lock.
468 * It is safe to call this function from atomic context.
470 * Will trigger an automatic deferred table resizing if the size grows
471 * beyond the watermark indicated by grow_decision() which can be passed
472 * to rhashtable_init().
474 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
476 __rhashtable_insert(ht, obj, NULL, NULL);
478 EXPORT_SYMBOL_GPL(rhashtable_insert);
480 static bool __rhashtable_remove(struct rhashtable *ht,
481 struct bucket_table *tbl,
482 struct rhash_head *obj)
484 struct rhash_head __rcu **pprev;
485 struct rhash_head *he;
490 hash = obj_raw_hashfn(ht, tbl, rht_obj(ht, obj));
491 lock = bucket_lock(tbl, hash);
492 hash = rht_bucket_index(tbl, hash);
496 pprev = &tbl->buckets[hash];
497 rht_for_each(he, tbl, hash) {
503 rcu_assign_pointer(*pprev, obj->next);
508 spin_unlock_bh(lock);
514 * rhashtable_remove - remove object from hash table
516 * @obj: pointer to hash head inside object
518 * Since the hash chain is single linked, the removal operation needs to
519 * walk the bucket chain upon removal. The removal operation is thus
520 * considerable slow if the hash table is not correctly sized.
522 * Will automatically shrink the table via rhashtable_expand() if the
523 * shrink_decision function specified at rhashtable_init() returns true.
525 * The caller must ensure that no concurrent table mutations occur. It is
526 * however valid to have concurrent lookups if they are RCU protected.
528 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
530 struct bucket_table *tbl, *old_tbl;
535 old_tbl = rht_dereference_rcu(ht->tbl, ht);
536 ret = __rhashtable_remove(ht, old_tbl, obj);
538 /* Because we have already taken (and released) the bucket
539 * lock in old_tbl, if we find that future_tbl is not yet
540 * visible then that guarantees the entry to still be in
541 * old_tbl if it exists.
543 tbl = rht_dereference_rcu(ht->future_tbl, ht);
544 if (!ret && old_tbl != tbl)
545 ret = __rhashtable_remove(ht, tbl, obj);
548 bool no_resize_running = tbl == old_tbl;
550 atomic_dec(&ht->nelems);
551 if (no_resize_running && rht_shrink_below_30(ht, tbl->size))
552 schedule_work(&ht->run_work);
559 EXPORT_SYMBOL_GPL(rhashtable_remove);
561 struct rhashtable_compare_arg {
562 struct rhashtable *ht;
566 static bool rhashtable_compare(void *ptr, void *arg)
568 struct rhashtable_compare_arg *x = arg;
569 struct rhashtable *ht = x->ht;
571 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
575 * rhashtable_lookup - lookup key in hash table
577 * @key: pointer to key
579 * Computes the hash value for the key and traverses the bucket chain looking
580 * for a entry with an identical key. The first matching entry is returned.
582 * This lookup function may only be used for fixed key hash table (key_len
583 * parameter set). It will BUG() if used inappropriately.
585 * Lookups may occur in parallel with hashtable mutations and resizing.
587 void *rhashtable_lookup(struct rhashtable *ht, const void *key)
589 struct rhashtable_compare_arg arg = {
594 BUG_ON(!ht->p.key_len);
596 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
598 EXPORT_SYMBOL_GPL(rhashtable_lookup);
601 * rhashtable_lookup_compare - search hash table with compare function
603 * @key: the pointer to the key
604 * @compare: compare function, must return true on match
605 * @arg: argument passed on to compare function
607 * Traverses the bucket chain behind the provided hash value and calls the
608 * specified compare function for each entry.
610 * Lookups may occur in parallel with hashtable mutations and resizing.
612 * Returns the first entry on which the compare function returned true.
614 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
615 bool (*compare)(void *, void *), void *arg)
617 const struct bucket_table *tbl, *old_tbl;
618 struct rhash_head *he;
623 tbl = rht_dereference_rcu(ht->tbl, ht);
624 hash = key_hashfn(ht, tbl, key, ht->p.key_len);
626 rht_for_each_rcu(he, tbl, hash) {
627 if (!compare(rht_obj(ht, he), arg))
630 return rht_obj(ht, he);
634 tbl = rht_dereference_rcu(ht->future_tbl, ht);
635 if (unlikely(tbl != old_tbl))
641 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
644 * rhashtable_lookup_insert - lookup and insert object into hash table
646 * @obj: pointer to hash head inside object
648 * Locks down the bucket chain in both the old and new table if a resize
649 * is in progress to ensure that writers can't remove from the old table
650 * and can't insert to the new table during the atomic operation of search
651 * and insertion. Searches for duplicates in both the old and new table if
652 * a resize is in progress.
654 * This lookup function may only be used for fixed key hash table (key_len
655 * parameter set). It will BUG() if used inappropriately.
657 * It is safe to call this function from atomic context.
659 * Will trigger an automatic deferred table resizing if the size grows
660 * beyond the watermark indicated by grow_decision() which can be passed
661 * to rhashtable_init().
663 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
665 struct rhashtable_compare_arg arg = {
667 .key = rht_obj(ht, obj) + ht->p.key_offset,
670 BUG_ON(!ht->p.key_len);
672 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
675 EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
678 * rhashtable_lookup_compare_insert - search and insert object to hash table
679 * with compare function
681 * @obj: pointer to hash head inside object
682 * @compare: compare function, must return true on match
683 * @arg: argument passed on to compare function
685 * Locks down the bucket chain in both the old and new table if a resize
686 * is in progress to ensure that writers can't remove from the old table
687 * and can't insert to the new table during the atomic operation of search
688 * and insertion. Searches for duplicates in both the old and new table if
689 * a resize is in progress.
691 * Lookups may occur in parallel with hashtable mutations and resizing.
693 * Will trigger an automatic deferred table resizing if the size grows
694 * beyond the watermark indicated by grow_decision() which can be passed
695 * to rhashtable_init().
697 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
698 struct rhash_head *obj,
699 bool (*compare)(void *, void *),
702 BUG_ON(!ht->p.key_len);
704 return __rhashtable_insert(ht, obj, compare, arg);
706 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
709 * rhashtable_walk_init - Initialise an iterator
710 * @ht: Table to walk over
711 * @iter: Hash table Iterator
713 * This function prepares a hash table walk.
715 * Note that if you restart a walk after rhashtable_walk_stop you
716 * may see the same object twice. Also, you may miss objects if
717 * there are removals in between rhashtable_walk_stop and the next
718 * call to rhashtable_walk_start.
720 * For a completely stable walk you should construct your own data
721 * structure outside the hash table.
723 * This function may sleep so you must not call it from interrupt
724 * context or with spin locks held.
726 * You must call rhashtable_walk_exit if this function returns
729 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
736 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
740 INIT_LIST_HEAD(&iter->walker->list);
741 iter->walker->resize = false;
743 mutex_lock(&ht->mutex);
744 list_add(&iter->walker->list, &ht->walkers);
745 mutex_unlock(&ht->mutex);
749 EXPORT_SYMBOL_GPL(rhashtable_walk_init);
752 * rhashtable_walk_exit - Free an iterator
753 * @iter: Hash table Iterator
755 * This function frees resources allocated by rhashtable_walk_init.
757 void rhashtable_walk_exit(struct rhashtable_iter *iter)
759 mutex_lock(&iter->ht->mutex);
760 list_del(&iter->walker->list);
761 mutex_unlock(&iter->ht->mutex);
764 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
767 * rhashtable_walk_start - Start a hash table walk
768 * @iter: Hash table iterator
770 * Start a hash table walk. Note that we take the RCU lock in all
771 * cases including when we return an error. So you must always call
772 * rhashtable_walk_stop to clean up.
774 * Returns zero if successful.
776 * Returns -EAGAIN if resize event occured. Note that the iterator
777 * will rewind back to the beginning and you may use it immediately
778 * by calling rhashtable_walk_next.
780 int rhashtable_walk_start(struct rhashtable_iter *iter)
784 if (iter->walker->resize) {
787 iter->walker->resize = false;
793 EXPORT_SYMBOL_GPL(rhashtable_walk_start);
796 * rhashtable_walk_next - Return the next object and advance the iterator
797 * @iter: Hash table iterator
799 * Note that you must call rhashtable_walk_stop when you are finished
802 * Returns the next object or NULL when the end of the table is reached.
804 * Returns -EAGAIN if resize event occured. Note that the iterator
805 * will rewind back to the beginning and you may continue to use it.
807 void *rhashtable_walk_next(struct rhashtable_iter *iter)
809 const struct bucket_table *tbl;
810 struct rhashtable *ht = iter->ht;
811 struct rhash_head *p = iter->p;
814 tbl = rht_dereference_rcu(ht->tbl, ht);
817 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
821 for (; iter->slot < tbl->size; iter->slot++) {
822 int skip = iter->skip;
824 rht_for_each_rcu(p, tbl, iter->slot) {
831 if (!rht_is_a_nulls(p)) {
834 obj = rht_obj(ht, p);
844 if (iter->walker->resize) {
848 iter->walker->resize = false;
849 return ERR_PTR(-EAGAIN);
854 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
857 * rhashtable_walk_stop - Finish a hash table walk
858 * @iter: Hash table iterator
860 * Finish a hash table walk.
862 void rhashtable_walk_stop(struct rhashtable_iter *iter)
867 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
869 static size_t rounded_hashtable_size(struct rhashtable_params *params)
871 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
872 1UL << params->min_shift);
876 * rhashtable_init - initialize a new hash table
877 * @ht: hash table to be initialized
878 * @params: configuration parameters
880 * Initializes a new hash table based on the provided configuration
881 * parameters. A table can be configured either with a variable or
884 * Configuration Example 1: Fixed length keys
888 * struct rhash_head node;
891 * struct rhashtable_params params = {
892 * .head_offset = offsetof(struct test_obj, node),
893 * .key_offset = offsetof(struct test_obj, key),
894 * .key_len = sizeof(int),
896 * .nulls_base = (1U << RHT_BASE_SHIFT),
899 * Configuration Example 2: Variable length keys
902 * struct rhash_head node;
905 * u32 my_hash_fn(const void *data, u32 seed)
907 * struct test_obj *obj = data;
909 * return [... hash ...];
912 * struct rhashtable_params params = {
913 * .head_offset = offsetof(struct test_obj, node),
915 * .obj_hashfn = my_hash_fn,
918 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
920 struct bucket_table *tbl;
923 size = HASH_DEFAULT_SIZE;
925 if ((params->key_len && !params->hashfn) ||
926 (!params->key_len && !params->obj_hashfn))
929 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
932 params->min_shift = max_t(size_t, params->min_shift,
933 ilog2(HASH_MIN_SIZE));
935 if (params->nelem_hint)
936 size = rounded_hashtable_size(params);
938 memset(ht, 0, sizeof(*ht));
939 mutex_init(&ht->mutex);
940 memcpy(&ht->p, params, sizeof(*params));
941 INIT_LIST_HEAD(&ht->walkers);
943 if (params->locks_mul)
944 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
946 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
948 tbl = bucket_table_alloc(ht, size);
952 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
954 atomic_set(&ht->nelems, 0);
955 atomic_set(&ht->shift, ilog2(tbl->size));
956 RCU_INIT_POINTER(ht->tbl, tbl);
957 RCU_INIT_POINTER(ht->future_tbl, tbl);
959 INIT_WORK(&ht->run_work, rht_deferred_worker);
963 EXPORT_SYMBOL_GPL(rhashtable_init);
966 * rhashtable_destroy - destroy hash table
967 * @ht: the hash table to destroy
969 * Frees the bucket array. This function is not rcu safe, therefore the caller
970 * has to make sure that no resizing may happen by unpublishing the hashtable
971 * and waiting for the quiescent cycle before releasing the bucket array.
973 void rhashtable_destroy(struct rhashtable *ht)
975 ht->being_destroyed = true;
977 cancel_work_sync(&ht->run_work);
979 mutex_lock(&ht->mutex);
980 bucket_table_free(rht_dereference(ht->tbl, ht));
981 mutex_unlock(&ht->mutex);
983 EXPORT_SYMBOL_GPL(rhashtable_destroy);