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)
36 /* The bucket lock is selected based on the hash and protects mutations
37 * on a group of hash buckets.
39 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
40 * a single lock always covers both buckets which may both contains
41 * entries which link to the same bucket of the old table during resizing.
42 * This allows to simplify the locking as locking the bucket in both
43 * tables during resize always guarantee protection.
45 * IMPORTANT: When holding the bucket lock of both the old and new table
46 * during expansions and shrinking, the old bucket lock must always be
49 static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
51 return &tbl->locks[hash & tbl->locks_mask];
54 static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
56 return (void *) he - ht->p.head_offset;
59 static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
61 return (hash >> HASH_RESERVED_SPACE) & (tbl->size - 1);
64 static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl,
67 return rht_bucket_index(tbl, ht->p.hashfn(key, ht->p.key_len,
71 static u32 head_hashfn(struct rhashtable *ht,
72 const struct bucket_table *tbl,
73 const struct rhash_head *he)
75 const char *ptr = rht_obj(ht, he);
77 return likely(ht->p.key_len) ?
78 key_hashfn(ht, tbl, ptr + ht->p.key_offset) :
79 rht_bucket_index(tbl, ht->p.obj_hashfn(ptr, tbl->hash_rnd));
82 #ifdef CONFIG_PROVE_LOCKING
83 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
85 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
87 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
89 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
91 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
93 spinlock_t *lock = bucket_lock(tbl, hash);
95 return (debug_locks) ? lockdep_is_held(lock) : 1;
97 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
99 #define ASSERT_RHT_MUTEX(HT)
103 static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
105 unsigned int i, size;
106 #if defined(CONFIG_PROVE_LOCKING)
107 unsigned int nr_pcpus = 2;
109 unsigned int nr_pcpus = num_possible_cpus();
112 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
113 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
115 /* Never allocate more than 0.5 locks per bucket */
116 size = min_t(unsigned int, size, tbl->size >> 1);
118 if (sizeof(spinlock_t) != 0) {
120 if (size * sizeof(spinlock_t) > PAGE_SIZE)
121 tbl->locks = vmalloc(size * sizeof(spinlock_t));
124 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
128 for (i = 0; i < size; i++)
129 spin_lock_init(&tbl->locks[i]);
131 tbl->locks_mask = size - 1;
136 static void bucket_table_free(const struct bucket_table *tbl)
144 static void bucket_table_free_rcu(struct rcu_head *head)
146 bucket_table_free(container_of(head, struct bucket_table, rcu));
149 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
152 struct bucket_table *tbl = NULL;
156 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
157 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
158 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
164 tbl->size = nbuckets;
166 if (alloc_bucket_locks(ht, tbl) < 0) {
167 bucket_table_free(tbl);
171 INIT_LIST_HEAD(&tbl->walkers);
173 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
175 for (i = 0; i < nbuckets; i++)
176 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
182 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
184 * @tbl: current table
186 static bool rht_grow_above_75(const struct rhashtable *ht,
187 const struct bucket_table *tbl)
189 /* Expand table when exceeding 75% load */
190 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
191 (!ht->p.max_shift || tbl->size < (1 << ht->p.max_shift));
195 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
197 * @tbl: current table
199 static bool rht_shrink_below_30(const struct rhashtable *ht,
200 const struct bucket_table *tbl)
202 /* Shrink table beneath 30% load */
203 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
204 tbl->size > (1 << ht->p.min_shift);
207 static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
209 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
210 struct bucket_table *new_tbl =
211 rht_dereference(old_tbl->future_tbl, ht) ?: old_tbl;
212 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
214 struct rhash_head *head, *next, *entry;
215 spinlock_t *new_bucket_lock;
218 rht_for_each(entry, old_tbl, old_hash) {
220 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
222 if (rht_is_a_nulls(next))
225 pprev = &entry->next;
231 new_hash = head_hashfn(ht, new_tbl, entry);
233 new_bucket_lock = bucket_lock(new_tbl, new_hash);
235 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
236 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
239 if (rht_is_a_nulls(head))
240 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
242 RCU_INIT_POINTER(entry->next, head);
244 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
245 spin_unlock(new_bucket_lock);
247 rcu_assign_pointer(*pprev, next);
253 static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
255 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
256 spinlock_t *old_bucket_lock;
258 old_bucket_lock = bucket_lock(old_tbl, old_hash);
260 spin_lock_bh(old_bucket_lock);
261 while (!rhashtable_rehash_one(ht, old_hash))
264 spin_unlock_bh(old_bucket_lock);
267 static void rhashtable_rehash(struct rhashtable *ht,
268 struct bucket_table *new_tbl)
270 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
271 struct rhashtable_walker *walker;
274 /* Make insertions go into the new, empty table right away. Deletions
275 * and lookups will be attempted in both tables until we synchronize.
277 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
279 /* Ensure the new table is visible to readers. */
282 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
283 rhashtable_rehash_chain(ht, old_hash);
285 /* Publish the new table pointer. */
286 rcu_assign_pointer(ht->tbl, new_tbl);
288 list_for_each_entry(walker, &old_tbl->walkers, list)
291 /* Wait for readers. All new readers will see the new
292 * table, and thus no references to the old table will
295 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
299 * rhashtable_expand - Expand hash table while allowing concurrent lookups
300 * @ht: the hash table to expand
302 * A secondary bucket array is allocated and the hash entries are migrated.
304 * This function may only be called in a context where it is safe to call
305 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
307 * The caller must ensure that no concurrent resizing occurs by holding
310 * It is valid to have concurrent insertions and deletions protected by per
311 * bucket locks or concurrent RCU protected lookups and traversals.
313 int rhashtable_expand(struct rhashtable *ht)
315 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
317 ASSERT_RHT_MUTEX(ht);
319 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
323 rhashtable_rehash(ht, new_tbl);
326 EXPORT_SYMBOL_GPL(rhashtable_expand);
329 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
330 * @ht: the hash table to shrink
332 * This function may only be called in a context where it is safe to call
333 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
335 * The caller must ensure that no concurrent resizing occurs by holding
338 * The caller must ensure that no concurrent table mutations take place.
339 * It is however valid to have concurrent lookups if they are RCU protected.
341 * It is valid to have concurrent insertions and deletions protected by per
342 * bucket locks or concurrent RCU protected lookups and traversals.
344 int rhashtable_shrink(struct rhashtable *ht)
346 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
348 ASSERT_RHT_MUTEX(ht);
350 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2);
354 rhashtable_rehash(ht, new_tbl);
357 EXPORT_SYMBOL_GPL(rhashtable_shrink);
359 static void rht_deferred_worker(struct work_struct *work)
361 struct rhashtable *ht;
362 struct bucket_table *tbl;
364 ht = container_of(work, struct rhashtable, run_work);
365 mutex_lock(&ht->mutex);
366 if (ht->being_destroyed)
369 tbl = rht_dereference(ht->tbl, ht);
371 if (rht_grow_above_75(ht, tbl))
372 rhashtable_expand(ht);
373 else if (rht_shrink_below_30(ht, tbl))
374 rhashtable_shrink(ht);
376 mutex_unlock(&ht->mutex);
379 static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
380 bool (*compare)(void *, void *), void *arg)
382 struct bucket_table *tbl, *old_tbl;
383 struct rhash_head *head;
384 bool no_resize_running;
386 spinlock_t *old_lock;
391 old_tbl = rht_dereference_rcu(ht->tbl, ht);
392 hash = head_hashfn(ht, old_tbl, obj);
393 old_lock = bucket_lock(old_tbl, hash);
395 spin_lock_bh(old_lock);
397 /* Because we have already taken the bucket lock in old_tbl,
398 * if we find that future_tbl is not yet visible then that
399 * guarantees all other insertions of the same entry will
400 * also grab the bucket lock in old_tbl because until the
401 * rehash completes ht->tbl won't be changed.
403 tbl = rht_dereference_rcu(old_tbl->future_tbl, ht) ?: old_tbl;
404 if (tbl != old_tbl) {
405 hash = head_hashfn(ht, tbl, obj);
406 spin_lock_nested(bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
410 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
416 no_resize_running = tbl == old_tbl;
418 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
420 if (rht_is_a_nulls(head))
421 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
423 RCU_INIT_POINTER(obj->next, head);
425 rcu_assign_pointer(tbl->buckets[hash], obj);
427 atomic_inc(&ht->nelems);
428 if (no_resize_running && rht_grow_above_75(ht, tbl))
429 schedule_work(&ht->run_work);
433 spin_unlock(bucket_lock(tbl, hash));
435 spin_unlock_bh(old_lock);
443 * rhashtable_insert - insert object into hash table
445 * @obj: pointer to hash head inside object
447 * Will take a per bucket spinlock to protect against mutual mutations
448 * on the same bucket. Multiple insertions may occur in parallel unless
449 * they map to the same bucket lock.
451 * It is safe to call this function from atomic context.
453 * Will trigger an automatic deferred table resizing if the size grows
454 * beyond the watermark indicated by grow_decision() which can be passed
455 * to rhashtable_init().
457 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
459 __rhashtable_insert(ht, obj, NULL, NULL);
461 EXPORT_SYMBOL_GPL(rhashtable_insert);
463 static bool __rhashtable_remove(struct rhashtable *ht,
464 struct bucket_table *tbl,
465 struct rhash_head *obj)
467 struct rhash_head __rcu **pprev;
468 struct rhash_head *he;
473 hash = head_hashfn(ht, tbl, obj);
474 lock = bucket_lock(tbl, hash);
478 pprev = &tbl->buckets[hash];
479 rht_for_each(he, tbl, hash) {
485 rcu_assign_pointer(*pprev, obj->next);
490 spin_unlock_bh(lock);
496 * rhashtable_remove - remove object from hash table
498 * @obj: pointer to hash head inside object
500 * Since the hash chain is single linked, the removal operation needs to
501 * walk the bucket chain upon removal. The removal operation is thus
502 * considerable slow if the hash table is not correctly sized.
504 * Will automatically shrink the table via rhashtable_expand() if the
505 * shrink_decision function specified at rhashtable_init() returns true.
507 * The caller must ensure that no concurrent table mutations occur. It is
508 * however valid to have concurrent lookups if they are RCU protected.
510 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
512 struct bucket_table *tbl;
517 tbl = rht_dereference_rcu(ht->tbl, ht);
519 /* Because we have already taken (and released) the bucket
520 * lock in old_tbl, if we find that future_tbl is not yet
521 * visible then that guarantees the entry to still be in
522 * the old tbl if it exists.
524 while (!(ret = __rhashtable_remove(ht, tbl, obj)) &&
525 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
529 atomic_dec(&ht->nelems);
530 if (rht_shrink_below_30(ht, tbl))
531 schedule_work(&ht->run_work);
538 EXPORT_SYMBOL_GPL(rhashtable_remove);
540 struct rhashtable_compare_arg {
541 struct rhashtable *ht;
545 static bool rhashtable_compare(void *ptr, void *arg)
547 struct rhashtable_compare_arg *x = arg;
548 struct rhashtable *ht = x->ht;
550 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
554 * rhashtable_lookup - lookup key in hash table
556 * @key: pointer to key
558 * Computes the hash value for the key and traverses the bucket chain looking
559 * for a entry with an identical key. The first matching entry is returned.
561 * This lookup function may only be used for fixed key hash table (key_len
562 * parameter set). It will BUG() if used inappropriately.
564 * Lookups may occur in parallel with hashtable mutations and resizing.
566 void *rhashtable_lookup(struct rhashtable *ht, const void *key)
568 struct rhashtable_compare_arg arg = {
573 BUG_ON(!ht->p.key_len);
575 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
577 EXPORT_SYMBOL_GPL(rhashtable_lookup);
580 * rhashtable_lookup_compare - search hash table with compare function
582 * @key: the pointer to the key
583 * @compare: compare function, must return true on match
584 * @arg: argument passed on to compare function
586 * Traverses the bucket chain behind the provided hash value and calls the
587 * specified compare function for each entry.
589 * Lookups may occur in parallel with hashtable mutations and resizing.
591 * Returns the first entry on which the compare function returned true.
593 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
594 bool (*compare)(void *, void *), void *arg)
596 const struct bucket_table *tbl;
597 struct rhash_head *he;
602 tbl = rht_dereference_rcu(ht->tbl, ht);
604 hash = key_hashfn(ht, tbl, key);
605 rht_for_each_rcu(he, tbl, hash) {
606 if (!compare(rht_obj(ht, he), arg))
609 return rht_obj(ht, he);
612 /* Ensure we see any new tables. */
615 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
622 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
625 * rhashtable_lookup_insert - lookup and insert object into hash table
627 * @obj: pointer to hash head inside object
629 * Locks down the bucket chain in both the old and new table if a resize
630 * is in progress to ensure that writers can't remove from the old table
631 * and can't insert to the new table during the atomic operation of search
632 * and insertion. Searches for duplicates in both the old and new table if
633 * a resize is in progress.
635 * This lookup function may only be used for fixed key hash table (key_len
636 * parameter set). It will BUG() if used inappropriately.
638 * It is safe to call this function from atomic context.
640 * Will trigger an automatic deferred table resizing if the size grows
641 * beyond the watermark indicated by grow_decision() which can be passed
642 * to rhashtable_init().
644 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
646 struct rhashtable_compare_arg arg = {
648 .key = rht_obj(ht, obj) + ht->p.key_offset,
651 BUG_ON(!ht->p.key_len);
653 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
656 EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
659 * rhashtable_lookup_compare_insert - search and insert object to hash table
660 * with compare function
662 * @obj: pointer to hash head inside object
663 * @compare: compare function, must return true on match
664 * @arg: argument passed on to compare function
666 * Locks down the bucket chain in both the old and new table if a resize
667 * is in progress to ensure that writers can't remove from the old table
668 * and can't insert to the new table during the atomic operation of search
669 * and insertion. Searches for duplicates in both the old and new table if
670 * a resize is in progress.
672 * Lookups may occur in parallel with hashtable mutations and resizing.
674 * Will trigger an automatic deferred table resizing if the size grows
675 * beyond the watermark indicated by grow_decision() which can be passed
676 * to rhashtable_init().
678 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
679 struct rhash_head *obj,
680 bool (*compare)(void *, void *),
683 BUG_ON(!ht->p.key_len);
685 return __rhashtable_insert(ht, obj, compare, arg);
687 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
690 * rhashtable_walk_init - Initialise an iterator
691 * @ht: Table to walk over
692 * @iter: Hash table Iterator
694 * This function prepares a hash table walk.
696 * Note that if you restart a walk after rhashtable_walk_stop you
697 * may see the same object twice. Also, you may miss objects if
698 * there are removals in between rhashtable_walk_stop and the next
699 * call to rhashtable_walk_start.
701 * For a completely stable walk you should construct your own data
702 * structure outside the hash table.
704 * This function may sleep so you must not call it from interrupt
705 * context or with spin locks held.
707 * You must call rhashtable_walk_exit if this function returns
710 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
717 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
721 mutex_lock(&ht->mutex);
722 iter->walker->tbl = rht_dereference(ht->tbl, ht);
723 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
724 mutex_unlock(&ht->mutex);
728 EXPORT_SYMBOL_GPL(rhashtable_walk_init);
731 * rhashtable_walk_exit - Free an iterator
732 * @iter: Hash table Iterator
734 * This function frees resources allocated by rhashtable_walk_init.
736 void rhashtable_walk_exit(struct rhashtable_iter *iter)
738 mutex_lock(&iter->ht->mutex);
739 if (iter->walker->tbl)
740 list_del(&iter->walker->list);
741 mutex_unlock(&iter->ht->mutex);
744 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
747 * rhashtable_walk_start - Start a hash table walk
748 * @iter: Hash table iterator
750 * Start a hash table walk. Note that we take the RCU lock in all
751 * cases including when we return an error. So you must always call
752 * rhashtable_walk_stop to clean up.
754 * Returns zero if successful.
756 * Returns -EAGAIN if resize event occured. Note that the iterator
757 * will rewind back to the beginning and you may use it immediately
758 * by calling rhashtable_walk_next.
760 int rhashtable_walk_start(struct rhashtable_iter *iter)
763 struct rhashtable *ht = iter->ht;
765 mutex_lock(&ht->mutex);
767 if (iter->walker->tbl)
768 list_del(&iter->walker->list);
772 mutex_unlock(&ht->mutex);
774 if (!iter->walker->tbl) {
775 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
781 EXPORT_SYMBOL_GPL(rhashtable_walk_start);
784 * rhashtable_walk_next - Return the next object and advance the iterator
785 * @iter: Hash table iterator
787 * Note that you must call rhashtable_walk_stop when you are finished
790 * Returns the next object or NULL when the end of the table is reached.
792 * Returns -EAGAIN if resize event occured. Note that the iterator
793 * will rewind back to the beginning and you may continue to use it.
795 void *rhashtable_walk_next(struct rhashtable_iter *iter)
797 struct bucket_table *tbl = iter->walker->tbl;
798 struct rhashtable *ht = iter->ht;
799 struct rhash_head *p = iter->p;
803 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
807 for (; iter->slot < tbl->size; iter->slot++) {
808 int skip = iter->skip;
810 rht_for_each_rcu(p, tbl, iter->slot) {
817 if (!rht_is_a_nulls(p)) {
820 obj = rht_obj(ht, p);
827 iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
828 if (iter->walker->tbl) {
831 return ERR_PTR(-EAGAIN);
840 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
843 * rhashtable_walk_stop - Finish a hash table walk
844 * @iter: Hash table iterator
846 * Finish a hash table walk.
848 void rhashtable_walk_stop(struct rhashtable_iter *iter)
851 struct rhashtable *ht;
852 struct bucket_table *tbl = iter->walker->tbl;
859 mutex_lock(&ht->mutex);
860 if (tbl->rehash < tbl->size)
861 list_add(&iter->walker->list, &tbl->walkers);
863 iter->walker->tbl = NULL;
864 mutex_unlock(&ht->mutex);
871 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
873 static size_t rounded_hashtable_size(struct rhashtable_params *params)
875 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
876 1UL << params->min_shift);
880 * rhashtable_init - initialize a new hash table
881 * @ht: hash table to be initialized
882 * @params: configuration parameters
884 * Initializes a new hash table based on the provided configuration
885 * parameters. A table can be configured either with a variable or
888 * Configuration Example 1: Fixed length keys
892 * struct rhash_head node;
895 * struct rhashtable_params params = {
896 * .head_offset = offsetof(struct test_obj, node),
897 * .key_offset = offsetof(struct test_obj, key),
898 * .key_len = sizeof(int),
900 * .nulls_base = (1U << RHT_BASE_SHIFT),
903 * Configuration Example 2: Variable length keys
906 * struct rhash_head node;
909 * u32 my_hash_fn(const void *data, u32 seed)
911 * struct test_obj *obj = data;
913 * return [... hash ...];
916 * struct rhashtable_params params = {
917 * .head_offset = offsetof(struct test_obj, node),
919 * .obj_hashfn = my_hash_fn,
922 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
924 struct bucket_table *tbl;
927 size = HASH_DEFAULT_SIZE;
929 if ((params->key_len && !params->hashfn) ||
930 (!params->key_len && !params->obj_hashfn))
933 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
936 params->min_shift = max_t(size_t, params->min_shift,
937 ilog2(HASH_MIN_SIZE));
939 if (params->nelem_hint)
940 size = rounded_hashtable_size(params);
942 memset(ht, 0, sizeof(*ht));
943 mutex_init(&ht->mutex);
944 memcpy(&ht->p, params, sizeof(*params));
946 if (params->locks_mul)
947 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
949 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
951 tbl = bucket_table_alloc(ht, size);
955 atomic_set(&ht->nelems, 0);
957 RCU_INIT_POINTER(ht->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);