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 >> HASH_RESERVED_SPACE) & (tbl->size - 1);
69 static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl,
72 return rht_bucket_index(tbl, ht->p.hashfn(key, ht->p.key_len,
76 static u32 head_hashfn(struct rhashtable *ht,
77 const struct bucket_table *tbl,
78 const struct rhash_head *he)
80 const char *ptr = rht_obj(ht, he);
82 return likely(ht->p.key_len) ?
83 key_hashfn(ht, tbl, ptr + ht->p.key_offset) :
84 rht_bucket_index(tbl, ht->p.obj_hashfn(ptr, tbl->hash_rnd));
87 #ifdef CONFIG_PROVE_LOCKING
88 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
90 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
92 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
94 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
96 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
98 spinlock_t *lock = bucket_lock(tbl, hash);
100 return (debug_locks) ? lockdep_is_held(lock) : 1;
102 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
104 #define ASSERT_RHT_MUTEX(HT)
108 static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
110 unsigned int i, size;
111 #if defined(CONFIG_PROVE_LOCKING)
112 unsigned int nr_pcpus = 2;
114 unsigned int nr_pcpus = num_possible_cpus();
117 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
118 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
120 /* Never allocate more than 0.5 locks per bucket */
121 size = min_t(unsigned int, size, tbl->size >> 1);
123 if (sizeof(spinlock_t) != 0) {
125 if (size * sizeof(spinlock_t) > PAGE_SIZE)
126 tbl->locks = vmalloc(size * sizeof(spinlock_t));
129 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
133 for (i = 0; i < size; i++)
134 spin_lock_init(&tbl->locks[i]);
136 tbl->locks_mask = size - 1;
141 static void bucket_table_free(const struct bucket_table *tbl)
149 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
150 size_t nbuckets, u32 hash_rnd)
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;
165 tbl->shift = ilog2(nbuckets);
166 tbl->hash_rnd = hash_rnd;
168 if (alloc_bucket_locks(ht, tbl) < 0) {
169 bucket_table_free(tbl);
173 INIT_LIST_HEAD(&tbl->walkers);
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->shift < 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->shift > ht->p.min_shift;
207 static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
209 struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht);
210 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
211 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
213 struct rhash_head *head, *next, *entry;
214 spinlock_t *new_bucket_lock;
217 rht_for_each(entry, old_tbl, old_hash) {
219 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
221 if (rht_is_a_nulls(next))
224 pprev = &entry->next;
230 new_hash = head_hashfn(ht, new_tbl, entry);
232 new_bucket_lock = bucket_lock(new_tbl, new_hash);
234 spin_lock_nested(new_bucket_lock, RHT_LOCK_NESTED);
235 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
238 if (rht_is_a_nulls(head))
239 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
241 RCU_INIT_POINTER(entry->next, head);
243 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
244 spin_unlock(new_bucket_lock);
246 rcu_assign_pointer(*pprev, next);
252 static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
254 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
255 spinlock_t *old_bucket_lock;
257 old_bucket_lock = bucket_lock(old_tbl, old_hash);
259 spin_lock_bh(old_bucket_lock);
260 while (!rhashtable_rehash_one(ht, old_hash))
262 spin_unlock_bh(old_bucket_lock);
265 static void rhashtable_rehash(struct rhashtable *ht,
266 struct bucket_table *new_tbl)
268 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
269 struct rhashtable_walker *walker;
272 get_random_bytes(&new_tbl->hash_rnd, sizeof(new_tbl->hash_rnd));
274 /* Make insertions go into the new, empty table right away. Deletions
275 * and lookups will be attempted in both tables until we synchronize.
276 * The synchronize_rcu() guarantees for the new table to be picked up
277 * so no new additions go into the old table while we relink.
279 rcu_assign_pointer(ht->future_tbl, new_tbl);
281 /* Ensure the new table is visible to readers. */
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 list_for_each_entry(walker, &old_tbl->walkers, list)
293 /* Wait for readers. All new readers will see the new
294 * table, and thus no references to the old table will
299 bucket_table_free(old_tbl);
303 * rhashtable_expand - Expand hash table while allowing concurrent lookups
304 * @ht: the hash table to expand
306 * A secondary bucket array is allocated and the hash entries are migrated.
308 * This function may only be called in a context where it is safe to call
309 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
311 * The caller must ensure that no concurrent resizing occurs by holding
314 * It is valid to have concurrent insertions and deletions protected by per
315 * bucket locks or concurrent RCU protected lookups and traversals.
317 int rhashtable_expand(struct rhashtable *ht)
319 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
321 ASSERT_RHT_MUTEX(ht);
323 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, old_tbl->hash_rnd);
327 rhashtable_rehash(ht, new_tbl);
330 EXPORT_SYMBOL_GPL(rhashtable_expand);
333 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
334 * @ht: the hash table to shrink
336 * This function may only be called in a context where it is safe to call
337 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
339 * The caller must ensure that no concurrent resizing occurs by holding
342 * The caller must ensure that no concurrent table mutations take place.
343 * It is however valid to have concurrent lookups if they are RCU protected.
345 * It is valid to have concurrent insertions and deletions protected by per
346 * bucket locks or concurrent RCU protected lookups and traversals.
348 int rhashtable_shrink(struct rhashtable *ht)
350 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
352 ASSERT_RHT_MUTEX(ht);
354 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2, old_tbl->hash_rnd);
358 rhashtable_rehash(ht, new_tbl);
361 EXPORT_SYMBOL_GPL(rhashtable_shrink);
363 static void rht_deferred_worker(struct work_struct *work)
365 struct rhashtable *ht;
366 struct bucket_table *tbl;
368 ht = container_of(work, struct rhashtable, run_work);
369 mutex_lock(&ht->mutex);
370 if (ht->being_destroyed)
373 tbl = rht_dereference(ht->tbl, ht);
375 if (rht_grow_above_75(ht, tbl))
376 rhashtable_expand(ht);
377 else if (rht_shrink_below_30(ht, tbl))
378 rhashtable_shrink(ht);
380 mutex_unlock(&ht->mutex);
383 static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
384 bool (*compare)(void *, void *), void *arg)
386 struct bucket_table *tbl, *old_tbl;
387 struct rhash_head *head;
388 bool no_resize_running;
394 old_tbl = rht_dereference_rcu(ht->tbl, ht);
395 hash = head_hashfn(ht, old_tbl, obj);
397 spin_lock_bh(bucket_lock(old_tbl, hash));
399 /* Because we have already taken the bucket lock in old_tbl,
400 * if we find that future_tbl is not yet visible then that
401 * guarantees all other insertions of the same entry will
402 * also grab the bucket lock in old_tbl because until the
403 * rehash completes ht->tbl won't be changed.
405 tbl = rht_dereference_rcu(ht->future_tbl, ht);
406 if (tbl != old_tbl) {
407 hash = head_hashfn(ht, tbl, obj);
408 spin_lock_nested(bucket_lock(tbl, hash), RHT_LOCK_NESTED);
412 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
418 no_resize_running = tbl == old_tbl;
420 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
422 if (rht_is_a_nulls(head))
423 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
425 RCU_INIT_POINTER(obj->next, head);
427 rcu_assign_pointer(tbl->buckets[hash], obj);
429 atomic_inc(&ht->nelems);
430 if (no_resize_running && rht_grow_above_75(ht, tbl))
431 schedule_work(&ht->run_work);
434 if (tbl != old_tbl) {
435 hash = head_hashfn(ht, tbl, obj);
436 spin_unlock(bucket_lock(tbl, hash));
439 hash = head_hashfn(ht, old_tbl, obj);
440 spin_unlock_bh(bucket_lock(old_tbl, hash));
448 * rhashtable_insert - insert object into hash table
450 * @obj: pointer to hash head inside object
452 * Will take a per bucket spinlock to protect against mutual mutations
453 * on the same bucket. Multiple insertions may occur in parallel unless
454 * they map to the same bucket lock.
456 * It is safe to call this function from atomic context.
458 * Will trigger an automatic deferred table resizing if the size grows
459 * beyond the watermark indicated by grow_decision() which can be passed
460 * to rhashtable_init().
462 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
464 __rhashtable_insert(ht, obj, NULL, NULL);
466 EXPORT_SYMBOL_GPL(rhashtable_insert);
468 static bool __rhashtable_remove(struct rhashtable *ht,
469 struct bucket_table *tbl,
470 struct rhash_head *obj)
472 struct rhash_head __rcu **pprev;
473 struct rhash_head *he;
478 hash = head_hashfn(ht, tbl, obj);
479 lock = bucket_lock(tbl, hash);
483 pprev = &tbl->buckets[hash];
484 rht_for_each(he, tbl, hash) {
490 rcu_assign_pointer(*pprev, obj->next);
495 spin_unlock_bh(lock);
501 * rhashtable_remove - remove object from hash table
503 * @obj: pointer to hash head inside object
505 * Since the hash chain is single linked, the removal operation needs to
506 * walk the bucket chain upon removal. The removal operation is thus
507 * considerable slow if the hash table is not correctly sized.
509 * Will automatically shrink the table via rhashtable_expand() if the
510 * shrink_decision function specified at rhashtable_init() returns true.
512 * The caller must ensure that no concurrent table mutations occur. It is
513 * however valid to have concurrent lookups if they are RCU protected.
515 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
517 struct bucket_table *tbl, *old_tbl;
522 old_tbl = rht_dereference_rcu(ht->tbl, ht);
523 ret = __rhashtable_remove(ht, old_tbl, obj);
525 /* Because we have already taken (and released) the bucket
526 * lock in old_tbl, if we find that future_tbl is not yet
527 * visible then that guarantees the entry to still be in
528 * old_tbl if it exists.
530 tbl = rht_dereference_rcu(ht->future_tbl, ht);
531 if (!ret && old_tbl != tbl)
532 ret = __rhashtable_remove(ht, tbl, obj);
535 bool no_resize_running = tbl == old_tbl;
537 atomic_dec(&ht->nelems);
538 if (no_resize_running && rht_shrink_below_30(ht, tbl))
539 schedule_work(&ht->run_work);
546 EXPORT_SYMBOL_GPL(rhashtable_remove);
548 struct rhashtable_compare_arg {
549 struct rhashtable *ht;
553 static bool rhashtable_compare(void *ptr, void *arg)
555 struct rhashtable_compare_arg *x = arg;
556 struct rhashtable *ht = x->ht;
558 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
562 * rhashtable_lookup - lookup key in hash table
564 * @key: pointer to key
566 * Computes the hash value for the key and traverses the bucket chain looking
567 * for a entry with an identical key. The first matching entry is returned.
569 * This lookup function may only be used for fixed key hash table (key_len
570 * parameter set). It will BUG() if used inappropriately.
572 * Lookups may occur in parallel with hashtable mutations and resizing.
574 void *rhashtable_lookup(struct rhashtable *ht, const void *key)
576 struct rhashtable_compare_arg arg = {
581 BUG_ON(!ht->p.key_len);
583 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
585 EXPORT_SYMBOL_GPL(rhashtable_lookup);
588 * rhashtable_lookup_compare - search hash table with compare function
590 * @key: the pointer to the key
591 * @compare: compare function, must return true on match
592 * @arg: argument passed on to compare function
594 * Traverses the bucket chain behind the provided hash value and calls the
595 * specified compare function for each entry.
597 * Lookups may occur in parallel with hashtable mutations and resizing.
599 * Returns the first entry on which the compare function returned true.
601 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
602 bool (*compare)(void *, void *), void *arg)
604 const struct bucket_table *tbl, *old_tbl;
605 struct rhash_head *he;
610 tbl = rht_dereference_rcu(ht->tbl, ht);
612 hash = key_hashfn(ht, tbl, key);
613 rht_for_each_rcu(he, tbl, hash) {
614 if (!compare(rht_obj(ht, he), arg))
617 return rht_obj(ht, he);
620 /* Ensure we see any new tables. */
624 tbl = rht_dereference_rcu(ht->future_tbl, ht);
625 if (unlikely(tbl != old_tbl))
631 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
634 * rhashtable_lookup_insert - lookup and insert object into hash table
636 * @obj: pointer to hash head inside object
638 * Locks down the bucket chain in both the old and new table if a resize
639 * is in progress to ensure that writers can't remove from the old table
640 * and can't insert to the new table during the atomic operation of search
641 * and insertion. Searches for duplicates in both the old and new table if
642 * a resize is in progress.
644 * This lookup function may only be used for fixed key hash table (key_len
645 * parameter set). It will BUG() if used inappropriately.
647 * It is safe to call this function from atomic context.
649 * Will trigger an automatic deferred table resizing if the size grows
650 * beyond the watermark indicated by grow_decision() which can be passed
651 * to rhashtable_init().
653 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
655 struct rhashtable_compare_arg arg = {
657 .key = rht_obj(ht, obj) + ht->p.key_offset,
660 BUG_ON(!ht->p.key_len);
662 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
665 EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
668 * rhashtable_lookup_compare_insert - search and insert object to hash table
669 * with compare function
671 * @obj: pointer to hash head inside object
672 * @compare: compare function, must return true on match
673 * @arg: argument passed on to compare function
675 * Locks down the bucket chain in both the old and new table if a resize
676 * is in progress to ensure that writers can't remove from the old table
677 * and can't insert to the new table during the atomic operation of search
678 * and insertion. Searches for duplicates in both the old and new table if
679 * a resize is in progress.
681 * Lookups may occur in parallel with hashtable mutations and resizing.
683 * Will trigger an automatic deferred table resizing if the size grows
684 * beyond the watermark indicated by grow_decision() which can be passed
685 * to rhashtable_init().
687 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
688 struct rhash_head *obj,
689 bool (*compare)(void *, void *),
692 BUG_ON(!ht->p.key_len);
694 return __rhashtable_insert(ht, obj, compare, arg);
696 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
699 * rhashtable_walk_init - Initialise an iterator
700 * @ht: Table to walk over
701 * @iter: Hash table Iterator
703 * This function prepares a hash table walk.
705 * Note that if you restart a walk after rhashtable_walk_stop you
706 * may see the same object twice. Also, you may miss objects if
707 * there are removals in between rhashtable_walk_stop and the next
708 * call to rhashtable_walk_start.
710 * For a completely stable walk you should construct your own data
711 * structure outside the hash table.
713 * This function may sleep so you must not call it from interrupt
714 * context or with spin locks held.
716 * You must call rhashtable_walk_exit if this function returns
719 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
726 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
730 mutex_lock(&ht->mutex);
731 iter->walker->tbl = rht_dereference(ht->tbl, ht);
732 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
733 mutex_unlock(&ht->mutex);
737 EXPORT_SYMBOL_GPL(rhashtable_walk_init);
740 * rhashtable_walk_exit - Free an iterator
741 * @iter: Hash table Iterator
743 * This function frees resources allocated by rhashtable_walk_init.
745 void rhashtable_walk_exit(struct rhashtable_iter *iter)
747 mutex_lock(&iter->ht->mutex);
748 if (iter->walker->tbl)
749 list_del(&iter->walker->list);
750 mutex_unlock(&iter->ht->mutex);
753 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
756 * rhashtable_walk_start - Start a hash table walk
757 * @iter: Hash table iterator
759 * Start a hash table walk. Note that we take the RCU lock in all
760 * cases including when we return an error. So you must always call
761 * rhashtable_walk_stop to clean up.
763 * Returns zero if successful.
765 * Returns -EAGAIN if resize event occured. Note that the iterator
766 * will rewind back to the beginning and you may use it immediately
767 * by calling rhashtable_walk_next.
769 int rhashtable_walk_start(struct rhashtable_iter *iter)
771 struct rhashtable *ht = iter->ht;
773 mutex_lock(&ht->mutex);
775 if (iter->walker->tbl)
776 list_del(&iter->walker->list);
780 mutex_unlock(&ht->mutex);
782 if (!iter->walker->tbl) {
783 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
789 EXPORT_SYMBOL_GPL(rhashtable_walk_start);
792 * rhashtable_walk_next - Return the next object and advance the iterator
793 * @iter: Hash table iterator
795 * Note that you must call rhashtable_walk_stop when you are finished
798 * Returns the next object or NULL when the end of the table is reached.
800 * Returns -EAGAIN if resize event occured. Note that the iterator
801 * will rewind back to the beginning and you may continue to use it.
803 void *rhashtable_walk_next(struct rhashtable_iter *iter)
805 struct bucket_table *tbl = iter->walker->tbl;
806 struct rhashtable *ht = iter->ht;
807 struct rhash_head *p = iter->p;
811 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
815 for (; iter->slot < tbl->size; iter->slot++) {
816 int skip = iter->skip;
818 rht_for_each_rcu(p, tbl, iter->slot) {
825 if (!rht_is_a_nulls(p)) {
828 obj = rht_obj(ht, p);
835 iter->walker->tbl = rht_dereference_rcu(ht->future_tbl, ht);
836 if (iter->walker->tbl != tbl) {
839 return ERR_PTR(-EAGAIN);
842 iter->walker->tbl = NULL;
849 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
852 * rhashtable_walk_stop - Finish a hash table walk
853 * @iter: Hash table iterator
855 * Finish a hash table walk.
857 void rhashtable_walk_stop(struct rhashtable_iter *iter)
859 struct rhashtable *ht;
860 struct bucket_table *tbl = iter->walker->tbl;
869 mutex_lock(&ht->mutex);
870 if (rht_dereference(ht->tbl, ht) == tbl ||
871 rht_dereference(ht->future_tbl, ht) == tbl)
872 list_add(&iter->walker->list, &tbl->walkers);
874 iter->walker->tbl = NULL;
875 mutex_unlock(&ht->mutex);
879 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
881 static size_t rounded_hashtable_size(struct rhashtable_params *params)
883 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
884 1UL << params->min_shift);
888 * rhashtable_init - initialize a new hash table
889 * @ht: hash table to be initialized
890 * @params: configuration parameters
892 * Initializes a new hash table based on the provided configuration
893 * parameters. A table can be configured either with a variable or
896 * Configuration Example 1: Fixed length keys
900 * struct rhash_head node;
903 * struct rhashtable_params params = {
904 * .head_offset = offsetof(struct test_obj, node),
905 * .key_offset = offsetof(struct test_obj, key),
906 * .key_len = sizeof(int),
908 * .nulls_base = (1U << RHT_BASE_SHIFT),
911 * Configuration Example 2: Variable length keys
914 * struct rhash_head node;
917 * u32 my_hash_fn(const void *data, u32 seed)
919 * struct test_obj *obj = data;
921 * return [... hash ...];
924 * struct rhashtable_params params = {
925 * .head_offset = offsetof(struct test_obj, node),
927 * .obj_hashfn = my_hash_fn,
930 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
932 struct bucket_table *tbl;
936 size = HASH_DEFAULT_SIZE;
938 if ((params->key_len && !params->hashfn) ||
939 (!params->key_len && !params->obj_hashfn))
942 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
945 params->min_shift = max_t(size_t, params->min_shift,
946 ilog2(HASH_MIN_SIZE));
948 if (params->nelem_hint)
949 size = rounded_hashtable_size(params);
951 memset(ht, 0, sizeof(*ht));
952 mutex_init(&ht->mutex);
953 memcpy(&ht->p, params, sizeof(*params));
955 if (params->locks_mul)
956 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
958 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
960 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
962 tbl = bucket_table_alloc(ht, size, hash_rnd);
966 atomic_set(&ht->nelems, 0);
968 RCU_INIT_POINTER(ht->tbl, tbl);
969 RCU_INIT_POINTER(ht->future_tbl, tbl);
971 INIT_WORK(&ht->run_work, rht_deferred_worker);
975 EXPORT_SYMBOL_GPL(rhashtable_init);
978 * rhashtable_destroy - destroy hash table
979 * @ht: the hash table to destroy
981 * Frees the bucket array. This function is not rcu safe, therefore the caller
982 * has to make sure that no resizing may happen by unpublishing the hashtable
983 * and waiting for the quiescent cycle before releasing the bucket array.
985 void rhashtable_destroy(struct rhashtable *ht)
987 ht->being_destroyed = true;
989 cancel_work_sync(&ht->run_work);
991 mutex_lock(&ht->mutex);
992 bucket_table_free(rht_dereference(ht->tbl, ht));
993 mutex_unlock(&ht->mutex);
995 EXPORT_SYMBOL_GPL(rhashtable_destroy);