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,
86 return rht_bucket_index(tbl, ht->p.hashfn(key, ht->p.key_len,
91 static u32 head_hashfn(struct rhashtable *ht,
92 const struct bucket_table *tbl,
93 const struct rhash_head *he)
95 return rht_bucket_index(tbl, obj_raw_hashfn(ht, tbl, rht_obj(ht, he)));
98 #ifdef CONFIG_PROVE_LOCKING
99 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
101 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
103 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
105 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
107 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
109 spinlock_t *lock = bucket_lock(tbl, hash);
111 return (debug_locks) ? lockdep_is_held(lock) : 1;
113 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
115 #define ASSERT_RHT_MUTEX(HT)
119 static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
121 unsigned int i, size;
122 #if defined(CONFIG_PROVE_LOCKING)
123 unsigned int nr_pcpus = 2;
125 unsigned int nr_pcpus = num_possible_cpus();
128 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
129 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
131 /* Never allocate more than 0.5 locks per bucket */
132 size = min_t(unsigned int, size, tbl->size >> 1);
134 if (sizeof(spinlock_t) != 0) {
136 if (size * sizeof(spinlock_t) > PAGE_SIZE)
137 tbl->locks = vmalloc(size * sizeof(spinlock_t));
140 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
144 for (i = 0; i < size; i++)
145 spin_lock_init(&tbl->locks[i]);
147 tbl->locks_mask = size - 1;
152 static void bucket_table_free(const struct bucket_table *tbl)
160 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
163 struct bucket_table *tbl = NULL;
167 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
168 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
169 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
175 tbl->size = nbuckets;
177 if (alloc_bucket_locks(ht, tbl) < 0) {
178 bucket_table_free(tbl);
182 for (i = 0; i < nbuckets; i++)
183 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
189 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
191 * @new_size: new table size
193 static bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
195 /* Expand table when exceeding 75% load */
196 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
197 (!ht->p.max_shift || atomic_read(&ht->shift) < ht->p.max_shift);
201 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
203 * @new_size: new table size
205 static bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
207 /* Shrink table beneath 30% load */
208 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
209 (atomic_read(&ht->shift) > ht->p.min_shift);
212 static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
214 struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht);
215 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
216 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
218 struct rhash_head *head, *next, *entry;
219 spinlock_t *new_bucket_lock;
222 rht_for_each(entry, old_tbl, old_hash) {
224 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
226 if (rht_is_a_nulls(next))
229 pprev = &entry->next;
235 new_hash = head_hashfn(ht, new_tbl, entry);
237 new_bucket_lock = bucket_lock(new_tbl, new_hash);
239 spin_lock_nested(new_bucket_lock, RHT_LOCK_NESTED);
240 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
243 if (rht_is_a_nulls(head))
244 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
246 RCU_INIT_POINTER(entry->next, head);
248 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
249 spin_unlock(new_bucket_lock);
251 rcu_assign_pointer(*pprev, next);
257 static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
259 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
260 spinlock_t *old_bucket_lock;
262 old_bucket_lock = bucket_lock(old_tbl, old_hash);
264 spin_lock_bh(old_bucket_lock);
265 while (!rhashtable_rehash_one(ht, old_hash))
267 spin_unlock_bh(old_bucket_lock);
270 static void rhashtable_rehash(struct rhashtable *ht,
271 struct bucket_table *new_tbl)
273 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
276 get_random_bytes(&new_tbl->hash_rnd, sizeof(new_tbl->hash_rnd));
278 /* Make insertions go into the new, empty table right away. Deletions
279 * and lookups will be attempted in both tables until we synchronize.
280 * The synchronize_rcu() guarantees for the new table to be picked up
281 * so no new additions go into the old table while we relink.
283 rcu_assign_pointer(ht->future_tbl, new_tbl);
285 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
286 rhashtable_rehash_chain(ht, old_hash);
288 /* Publish the new table pointer. */
289 rcu_assign_pointer(ht->tbl, new_tbl);
291 /* Wait for readers. All new readers will see the new
292 * table, and thus no references to the old table will
297 bucket_table_free(old_tbl);
301 * rhashtable_expand - Expand hash table while allowing concurrent lookups
302 * @ht: the hash table to expand
304 * A secondary bucket array is allocated and the hash entries are migrated.
306 * This function may only be called in a context where it is safe to call
307 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
309 * The caller must ensure that no concurrent resizing occurs by holding
312 * It is valid to have concurrent insertions and deletions protected by per
313 * bucket locks or concurrent RCU protected lookups and traversals.
315 int rhashtable_expand(struct rhashtable *ht)
317 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
319 ASSERT_RHT_MUTEX(ht);
321 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
325 new_tbl->hash_rnd = old_tbl->hash_rnd;
327 atomic_inc(&ht->shift);
329 rhashtable_rehash(ht, new_tbl);
333 EXPORT_SYMBOL_GPL(rhashtable_expand);
336 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
337 * @ht: the hash table to shrink
339 * This function may only be called in a context where it is safe to call
340 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
342 * The caller must ensure that no concurrent resizing occurs by holding
345 * The caller must ensure that no concurrent table mutations take place.
346 * It is however valid to have concurrent lookups if they are RCU protected.
348 * It is valid to have concurrent insertions and deletions protected by per
349 * bucket locks or concurrent RCU protected lookups and traversals.
351 int rhashtable_shrink(struct rhashtable *ht)
353 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
355 ASSERT_RHT_MUTEX(ht);
357 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
361 new_tbl->hash_rnd = tbl->hash_rnd;
363 atomic_dec(&ht->shift);
365 rhashtable_rehash(ht, new_tbl);
369 EXPORT_SYMBOL_GPL(rhashtable_shrink);
371 static void rht_deferred_worker(struct work_struct *work)
373 struct rhashtable *ht;
374 struct bucket_table *tbl;
375 struct rhashtable_walker *walker;
377 ht = container_of(work, struct rhashtable, run_work);
378 mutex_lock(&ht->mutex);
379 if (ht->being_destroyed)
382 tbl = rht_dereference(ht->tbl, ht);
384 list_for_each_entry(walker, &ht->walkers, list)
385 walker->resize = true;
387 if (rht_grow_above_75(ht, tbl->size))
388 rhashtable_expand(ht);
389 else if (rht_shrink_below_30(ht, tbl->size))
390 rhashtable_shrink(ht);
392 mutex_unlock(&ht->mutex);
395 static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
396 bool (*compare)(void *, void *), void *arg)
398 struct bucket_table *tbl, *old_tbl;
399 struct rhash_head *head;
400 bool no_resize_running;
406 old_tbl = rht_dereference_rcu(ht->tbl, ht);
407 hash = head_hashfn(ht, old_tbl, obj);
409 spin_lock_bh(bucket_lock(old_tbl, hash));
411 /* Because we have already taken the bucket lock in old_tbl,
412 * if we find that future_tbl is not yet visible then that
413 * guarantees all other insertions of the same entry will
414 * also grab the bucket lock in old_tbl because until the
415 * rehash completes ht->tbl won't be changed.
417 tbl = rht_dereference_rcu(ht->future_tbl, ht);
418 if (tbl != old_tbl) {
419 hash = head_hashfn(ht, tbl, obj);
420 spin_lock_nested(bucket_lock(tbl, hash), RHT_LOCK_NESTED);
424 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
430 no_resize_running = tbl == old_tbl;
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 = head_hashfn(ht, tbl, obj);
448 spin_unlock(bucket_lock(tbl, hash));
451 hash = head_hashfn(ht, old_tbl, 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 = head_hashfn(ht, tbl, obj);
491 lock = bucket_lock(tbl, hash);
495 pprev = &tbl->buckets[hash];
496 rht_for_each(he, tbl, hash) {
502 rcu_assign_pointer(*pprev, obj->next);
507 spin_unlock_bh(lock);
513 * rhashtable_remove - remove object from hash table
515 * @obj: pointer to hash head inside object
517 * Since the hash chain is single linked, the removal operation needs to
518 * walk the bucket chain upon removal. The removal operation is thus
519 * considerable slow if the hash table is not correctly sized.
521 * Will automatically shrink the table via rhashtable_expand() if the
522 * shrink_decision function specified at rhashtable_init() returns true.
524 * The caller must ensure that no concurrent table mutations occur. It is
525 * however valid to have concurrent lookups if they are RCU protected.
527 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
529 struct bucket_table *tbl, *old_tbl;
534 old_tbl = rht_dereference_rcu(ht->tbl, ht);
535 ret = __rhashtable_remove(ht, old_tbl, obj);
537 /* Because we have already taken (and released) the bucket
538 * lock in old_tbl, if we find that future_tbl is not yet
539 * visible then that guarantees the entry to still be in
540 * old_tbl if it exists.
542 tbl = rht_dereference_rcu(ht->future_tbl, ht);
543 if (!ret && old_tbl != tbl)
544 ret = __rhashtable_remove(ht, tbl, obj);
547 bool no_resize_running = tbl == old_tbl;
549 atomic_dec(&ht->nelems);
550 if (no_resize_running && rht_shrink_below_30(ht, tbl->size))
551 schedule_work(&ht->run_work);
558 EXPORT_SYMBOL_GPL(rhashtable_remove);
560 struct rhashtable_compare_arg {
561 struct rhashtable *ht;
565 static bool rhashtable_compare(void *ptr, void *arg)
567 struct rhashtable_compare_arg *x = arg;
568 struct rhashtable *ht = x->ht;
570 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
574 * rhashtable_lookup - lookup key in hash table
576 * @key: pointer to key
578 * Computes the hash value for the key and traverses the bucket chain looking
579 * for a entry with an identical key. The first matching entry is returned.
581 * This lookup function may only be used for fixed key hash table (key_len
582 * parameter set). It will BUG() if used inappropriately.
584 * Lookups may occur in parallel with hashtable mutations and resizing.
586 void *rhashtable_lookup(struct rhashtable *ht, const void *key)
588 struct rhashtable_compare_arg arg = {
593 BUG_ON(!ht->p.key_len);
595 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
597 EXPORT_SYMBOL_GPL(rhashtable_lookup);
600 * rhashtable_lookup_compare - search hash table with compare function
602 * @key: the pointer to the key
603 * @compare: compare function, must return true on match
604 * @arg: argument passed on to compare function
606 * Traverses the bucket chain behind the provided hash value and calls the
607 * specified compare function for each entry.
609 * Lookups may occur in parallel with hashtable mutations and resizing.
611 * Returns the first entry on which the compare function returned true.
613 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
614 bool (*compare)(void *, void *), void *arg)
616 const struct bucket_table *tbl, *old_tbl;
617 struct rhash_head *he;
622 tbl = rht_dereference_rcu(ht->tbl, ht);
623 hash = key_hashfn(ht, tbl, key);
625 rht_for_each_rcu(he, tbl, hash) {
626 if (!compare(rht_obj(ht, he), arg))
629 return rht_obj(ht, he);
633 tbl = rht_dereference_rcu(ht->future_tbl, ht);
634 if (unlikely(tbl != old_tbl))
640 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
643 * rhashtable_lookup_insert - lookup and insert object into hash table
645 * @obj: pointer to hash head inside object
647 * Locks down the bucket chain in both the old and new table if a resize
648 * is in progress to ensure that writers can't remove from the old table
649 * and can't insert to the new table during the atomic operation of search
650 * and insertion. Searches for duplicates in both the old and new table if
651 * a resize is in progress.
653 * This lookup function may only be used for fixed key hash table (key_len
654 * parameter set). It will BUG() if used inappropriately.
656 * It is safe to call this function from atomic context.
658 * Will trigger an automatic deferred table resizing if the size grows
659 * beyond the watermark indicated by grow_decision() which can be passed
660 * to rhashtable_init().
662 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
664 struct rhashtable_compare_arg arg = {
666 .key = rht_obj(ht, obj) + ht->p.key_offset,
669 BUG_ON(!ht->p.key_len);
671 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
674 EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
677 * rhashtable_lookup_compare_insert - search and insert object to hash table
678 * with compare function
680 * @obj: pointer to hash head inside object
681 * @compare: compare function, must return true on match
682 * @arg: argument passed on to compare function
684 * Locks down the bucket chain in both the old and new table if a resize
685 * is in progress to ensure that writers can't remove from the old table
686 * and can't insert to the new table during the atomic operation of search
687 * and insertion. Searches for duplicates in both the old and new table if
688 * a resize is in progress.
690 * Lookups may occur in parallel with hashtable mutations and resizing.
692 * Will trigger an automatic deferred table resizing if the size grows
693 * beyond the watermark indicated by grow_decision() which can be passed
694 * to rhashtable_init().
696 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
697 struct rhash_head *obj,
698 bool (*compare)(void *, void *),
701 BUG_ON(!ht->p.key_len);
703 return __rhashtable_insert(ht, obj, compare, arg);
705 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
708 * rhashtable_walk_init - Initialise an iterator
709 * @ht: Table to walk over
710 * @iter: Hash table Iterator
712 * This function prepares a hash table walk.
714 * Note that if you restart a walk after rhashtable_walk_stop you
715 * may see the same object twice. Also, you may miss objects if
716 * there are removals in between rhashtable_walk_stop and the next
717 * call to rhashtable_walk_start.
719 * For a completely stable walk you should construct your own data
720 * structure outside the hash table.
722 * This function may sleep so you must not call it from interrupt
723 * context or with spin locks held.
725 * You must call rhashtable_walk_exit if this function returns
728 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
735 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
739 INIT_LIST_HEAD(&iter->walker->list);
740 iter->walker->resize = false;
742 mutex_lock(&ht->mutex);
743 list_add(&iter->walker->list, &ht->walkers);
744 mutex_unlock(&ht->mutex);
748 EXPORT_SYMBOL_GPL(rhashtable_walk_init);
751 * rhashtable_walk_exit - Free an iterator
752 * @iter: Hash table Iterator
754 * This function frees resources allocated by rhashtable_walk_init.
756 void rhashtable_walk_exit(struct rhashtable_iter *iter)
758 mutex_lock(&iter->ht->mutex);
759 list_del(&iter->walker->list);
760 mutex_unlock(&iter->ht->mutex);
763 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
766 * rhashtable_walk_start - Start a hash table walk
767 * @iter: Hash table iterator
769 * Start a hash table walk. Note that we take the RCU lock in all
770 * cases including when we return an error. So you must always call
771 * rhashtable_walk_stop to clean up.
773 * Returns zero if successful.
775 * Returns -EAGAIN if resize event occured. Note that the iterator
776 * will rewind back to the beginning and you may use it immediately
777 * by calling rhashtable_walk_next.
779 int rhashtable_walk_start(struct rhashtable_iter *iter)
783 if (iter->walker->resize) {
786 iter->walker->resize = false;
792 EXPORT_SYMBOL_GPL(rhashtable_walk_start);
795 * rhashtable_walk_next - Return the next object and advance the iterator
796 * @iter: Hash table iterator
798 * Note that you must call rhashtable_walk_stop when you are finished
801 * Returns the next object or NULL when the end of the table is reached.
803 * Returns -EAGAIN if resize event occured. Note that the iterator
804 * will rewind back to the beginning and you may continue to use it.
806 void *rhashtable_walk_next(struct rhashtable_iter *iter)
808 const struct bucket_table *tbl;
809 struct rhashtable *ht = iter->ht;
810 struct rhash_head *p = iter->p;
813 tbl = rht_dereference_rcu(ht->tbl, ht);
816 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
820 for (; iter->slot < tbl->size; iter->slot++) {
821 int skip = iter->skip;
823 rht_for_each_rcu(p, tbl, iter->slot) {
830 if (!rht_is_a_nulls(p)) {
833 obj = rht_obj(ht, p);
843 if (iter->walker->resize) {
847 iter->walker->resize = false;
848 return ERR_PTR(-EAGAIN);
853 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
856 * rhashtable_walk_stop - Finish a hash table walk
857 * @iter: Hash table iterator
859 * Finish a hash table walk.
861 void rhashtable_walk_stop(struct rhashtable_iter *iter)
866 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
868 static size_t rounded_hashtable_size(struct rhashtable_params *params)
870 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
871 1UL << params->min_shift);
875 * rhashtable_init - initialize a new hash table
876 * @ht: hash table to be initialized
877 * @params: configuration parameters
879 * Initializes a new hash table based on the provided configuration
880 * parameters. A table can be configured either with a variable or
883 * Configuration Example 1: Fixed length keys
887 * struct rhash_head node;
890 * struct rhashtable_params params = {
891 * .head_offset = offsetof(struct test_obj, node),
892 * .key_offset = offsetof(struct test_obj, key),
893 * .key_len = sizeof(int),
895 * .nulls_base = (1U << RHT_BASE_SHIFT),
898 * Configuration Example 2: Variable length keys
901 * struct rhash_head node;
904 * u32 my_hash_fn(const void *data, u32 seed)
906 * struct test_obj *obj = data;
908 * return [... hash ...];
911 * struct rhashtable_params params = {
912 * .head_offset = offsetof(struct test_obj, node),
914 * .obj_hashfn = my_hash_fn,
917 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
919 struct bucket_table *tbl;
922 size = HASH_DEFAULT_SIZE;
924 if ((params->key_len && !params->hashfn) ||
925 (!params->key_len && !params->obj_hashfn))
928 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
931 params->min_shift = max_t(size_t, params->min_shift,
932 ilog2(HASH_MIN_SIZE));
934 if (params->nelem_hint)
935 size = rounded_hashtable_size(params);
937 memset(ht, 0, sizeof(*ht));
938 mutex_init(&ht->mutex);
939 memcpy(&ht->p, params, sizeof(*params));
940 INIT_LIST_HEAD(&ht->walkers);
942 if (params->locks_mul)
943 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
945 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
947 tbl = bucket_table_alloc(ht, size);
951 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
953 atomic_set(&ht->nelems, 0);
954 atomic_set(&ht->shift, ilog2(tbl->size));
955 RCU_INIT_POINTER(ht->tbl, tbl);
956 RCU_INIT_POINTER(ht->future_tbl, tbl);
958 INIT_WORK(&ht->run_work, rht_deferred_worker);
962 EXPORT_SYMBOL_GPL(rhashtable_init);
965 * rhashtable_destroy - destroy hash table
966 * @ht: the hash table to destroy
968 * Frees the bucket array. This function is not rcu safe, therefore the caller
969 * has to make sure that no resizing may happen by unpublishing the hashtable
970 * and waiting for the quiescent cycle before releasing the bucket array.
972 void rhashtable_destroy(struct rhashtable *ht)
974 ht->being_destroyed = true;
976 cancel_work_sync(&ht->run_work);
978 mutex_lock(&ht->mutex);
979 bucket_table_free(rht_dereference(ht->tbl, ht));
980 mutex_unlock(&ht->mutex);
982 EXPORT_SYMBOL_GPL(rhashtable_destroy);