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/slab.h>
21 #include <linux/vmalloc.h>
23 #include <linux/jhash.h>
24 #include <linux/random.h>
25 #include <linux/rhashtable.h>
26 #include <linux/err.h>
28 #define HASH_DEFAULT_SIZE 64UL
29 #define HASH_MIN_SIZE 4UL
30 #define BUCKET_LOCKS_PER_CPU 128UL
32 /* Base bits plus 1 bit for nulls marker */
33 #define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
40 /* The bucket lock is selected based on the hash and protects mutations
41 * on a group of hash buckets.
43 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
44 * a single lock always covers both buckets which may both contains
45 * entries which link to the same bucket of the old table during resizing.
46 * This allows to simplify the locking as locking the bucket in both
47 * tables during resize always guarantee protection.
49 * IMPORTANT: When holding the bucket lock of both the old and new table
50 * during expansions and shrinking, the old bucket lock must always be
53 static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
55 return &tbl->locks[hash & tbl->locks_mask];
58 static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
60 return (void *) he - ht->p.head_offset;
63 static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
65 return hash & (tbl->size - 1);
68 static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
72 if (unlikely(!ht->p.key_len))
73 hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
75 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
78 return hash >> HASH_RESERVED_SPACE;
81 static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len)
83 return ht->p.hashfn(key, len, ht->p.hash_rnd) >> HASH_RESERVED_SPACE;
86 static u32 head_hashfn(const struct rhashtable *ht,
87 const struct bucket_table *tbl,
88 const struct rhash_head *he)
90 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
93 #ifdef CONFIG_PROVE_LOCKING
94 static void debug_dump_buckets(const struct rhashtable *ht,
95 const struct bucket_table *tbl)
97 struct rhash_head *he;
100 for (i = 0; i < tbl->size; i++) {
101 pr_warn(" [Bucket %d] ", i);
102 rht_for_each_rcu(he, tbl, i) {
103 hash = head_hashfn(ht, tbl, he);
104 pr_cont("[hash = %#x, lock = %p] ",
105 hash, bucket_lock(tbl, hash));
112 static void debug_dump_table(struct rhashtable *ht,
113 const struct bucket_table *tbl,
116 struct bucket_table *old_tbl, *future_tbl;
118 pr_emerg("BUG: lock for hash %#x in table %p not held\n",
122 future_tbl = rht_dereference_rcu(ht->future_tbl, ht);
123 old_tbl = rht_dereference_rcu(ht->tbl, ht);
124 if (future_tbl != old_tbl) {
125 pr_warn("Future table %p (size: %zd)\n",
126 future_tbl, future_tbl->size);
127 debug_dump_buckets(ht, future_tbl);
130 pr_warn("Table %p (size: %zd)\n", old_tbl, old_tbl->size);
131 debug_dump_buckets(ht, old_tbl);
136 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
137 #define ASSERT_BUCKET_LOCK(HT, TBL, HASH) \
139 if (unlikely(!lockdep_rht_bucket_is_held(TBL, HASH))) { \
140 debug_dump_table(HT, TBL, HASH); \
145 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
147 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
149 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
151 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
153 spinlock_t *lock = bucket_lock(tbl, hash);
155 return (debug_locks) ? lockdep_is_held(lock) : 1;
157 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
159 #define ASSERT_RHT_MUTEX(HT)
160 #define ASSERT_BUCKET_LOCK(HT, TBL, HASH)
164 static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n)
166 struct rhash_head __rcu **pprev;
168 for (pprev = &tbl->buckets[n];
169 !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n));
170 pprev = &rht_dereference_bucket(*pprev, tbl, n)->next)
176 static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
178 unsigned int i, size;
179 #if defined(CONFIG_PROVE_LOCKING)
180 unsigned int nr_pcpus = 2;
182 unsigned int nr_pcpus = num_possible_cpus();
185 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
186 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
188 /* Never allocate more than 0.5 locks per bucket */
189 size = min_t(unsigned int, size, tbl->size >> 1);
191 if (sizeof(spinlock_t) != 0) {
193 if (size * sizeof(spinlock_t) > PAGE_SIZE)
194 tbl->locks = vmalloc(size * sizeof(spinlock_t));
197 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
201 for (i = 0; i < size; i++)
202 spin_lock_init(&tbl->locks[i]);
204 tbl->locks_mask = size - 1;
209 static void bucket_table_free(const struct bucket_table *tbl)
217 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
220 struct bucket_table *tbl = NULL;
224 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
225 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
226 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
232 tbl->size = nbuckets;
234 if (alloc_bucket_locks(ht, tbl) < 0) {
235 bucket_table_free(tbl);
239 for (i = 0; i < nbuckets; i++)
240 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
246 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
248 * @new_size: new table size
250 static bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
252 /* Expand table when exceeding 75% load */
253 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
254 (!ht->p.max_shift || atomic_read(&ht->shift) < ht->p.max_shift);
258 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
260 * @new_size: new table size
262 static bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
264 /* Shrink table beneath 30% load */
265 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
266 (atomic_read(&ht->shift) > ht->p.min_shift);
269 static void lock_buckets(struct bucket_table *new_tbl,
270 struct bucket_table *old_tbl, unsigned int hash)
271 __acquires(old_bucket_lock)
273 spin_lock_bh(bucket_lock(old_tbl, hash));
274 if (new_tbl != old_tbl)
275 spin_lock_bh_nested(bucket_lock(new_tbl, hash),
279 static void unlock_buckets(struct bucket_table *new_tbl,
280 struct bucket_table *old_tbl, unsigned int hash)
281 __releases(old_bucket_lock)
283 if (new_tbl != old_tbl)
284 spin_unlock_bh(bucket_lock(new_tbl, hash));
285 spin_unlock_bh(bucket_lock(old_tbl, hash));
289 * Unlink entries on bucket which hash to different bucket.
291 * Returns true if no more work needs to be performed on the bucket.
293 static bool hashtable_chain_unzip(struct rhashtable *ht,
294 const struct bucket_table *new_tbl,
295 struct bucket_table *old_tbl,
298 struct rhash_head *he, *p, *next;
299 unsigned int new_hash, new_hash2;
301 ASSERT_BUCKET_LOCK(ht, old_tbl, old_hash);
303 /* Old bucket empty, no work needed. */
304 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
306 if (rht_is_a_nulls(p))
309 new_hash = head_hashfn(ht, new_tbl, p);
310 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
312 /* Advance the old bucket pointer one or more times until it
313 * reaches a node that doesn't hash to the same bucket as the
314 * previous node p. Call the previous node p;
316 rht_for_each_continue(he, p->next, old_tbl, old_hash) {
317 new_hash2 = head_hashfn(ht, new_tbl, he);
318 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash2);
320 if (new_hash != new_hash2)
324 rcu_assign_pointer(old_tbl->buckets[old_hash], p->next);
326 /* Find the subsequent node which does hash to the same
327 * bucket as node P, or NULL if no such node exists.
329 INIT_RHT_NULLS_HEAD(next, ht, old_hash);
330 if (!rht_is_a_nulls(he)) {
331 rht_for_each_continue(he, he->next, old_tbl, old_hash) {
332 if (head_hashfn(ht, new_tbl, he) == new_hash) {
339 /* Set p's next pointer to that subsequent node pointer,
340 * bypassing the nodes which do not hash to p's bucket
342 rcu_assign_pointer(p->next, next);
344 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
347 return !rht_is_a_nulls(p);
350 static void link_old_to_new(struct rhashtable *ht, struct bucket_table *new_tbl,
351 unsigned int new_hash, struct rhash_head *entry)
353 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
355 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry);
359 * rhashtable_expand - Expand hash table while allowing concurrent lookups
360 * @ht: the hash table to expand
362 * A secondary bucket array is allocated and the hash entries are migrated
363 * while keeping them on both lists until the end of the RCU grace period.
365 * This function may only be called in a context where it is safe to call
366 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
368 * The caller must ensure that no concurrent resizing occurs by holding
371 * It is valid to have concurrent insertions and deletions protected by per
372 * bucket locks or concurrent RCU protected lookups and traversals.
374 int rhashtable_expand(struct rhashtable *ht)
376 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
377 struct rhash_head *he;
378 unsigned int new_hash, old_hash;
379 bool complete = false;
381 ASSERT_RHT_MUTEX(ht);
383 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
387 atomic_inc(&ht->shift);
389 /* Make insertions go into the new, empty table right away. Deletions
390 * and lookups will be attempted in both tables until we synchronize.
391 * The synchronize_rcu() guarantees for the new table to be picked up
392 * so no new additions go into the old table while we relink.
394 rcu_assign_pointer(ht->future_tbl, new_tbl);
397 /* For each new bucket, search the corresponding old bucket for the
398 * first entry that hashes to the new bucket, and link the end of
399 * newly formed bucket chain (containing entries added to future
400 * table) to that entry. Since all the entries which will end up in
401 * the new bucket appear in the same old bucket, this constructs an
402 * entirely valid new hash table, but with multiple buckets
403 * "zipped" together into a single imprecise chain.
405 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
406 old_hash = rht_bucket_index(old_tbl, new_hash);
407 lock_buckets(new_tbl, old_tbl, new_hash);
408 rht_for_each(he, old_tbl, old_hash) {
409 if (head_hashfn(ht, new_tbl, he) == new_hash) {
410 link_old_to_new(ht, new_tbl, new_hash, he);
414 unlock_buckets(new_tbl, old_tbl, new_hash);
417 /* Unzip interleaved hash chains */
418 while (!complete && !ht->being_destroyed) {
419 /* Wait for readers. All new readers will see the new
420 * table, and thus no references to the old table will
425 /* For each bucket in the old table (each of which
426 * contains items from multiple buckets of the new
430 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
431 lock_buckets(new_tbl, old_tbl, old_hash);
433 if (hashtable_chain_unzip(ht, new_tbl, old_tbl,
437 unlock_buckets(new_tbl, old_tbl, old_hash);
441 rcu_assign_pointer(ht->tbl, new_tbl);
444 bucket_table_free(old_tbl);
447 EXPORT_SYMBOL_GPL(rhashtable_expand);
450 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
451 * @ht: the hash table to shrink
453 * This function may only be called in a context where it is safe to call
454 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
456 * The caller must ensure that no concurrent resizing occurs by holding
459 * The caller must ensure that no concurrent table mutations take place.
460 * It is however valid to have concurrent lookups if they are RCU protected.
462 * It is valid to have concurrent insertions and deletions protected by per
463 * bucket locks or concurrent RCU protected lookups and traversals.
465 int rhashtable_shrink(struct rhashtable *ht)
467 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
468 unsigned int new_hash;
470 ASSERT_RHT_MUTEX(ht);
472 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
476 rcu_assign_pointer(ht->future_tbl, new_tbl);
479 /* Link the first entry in the old bucket to the end of the
480 * bucket in the new table. As entries are concurrently being
481 * added to the new table, lock down the new bucket. As we
482 * always divide the size in half when shrinking, each bucket
483 * in the new table maps to exactly two buckets in the old
486 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
487 lock_buckets(new_tbl, tbl, new_hash);
489 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
490 tbl->buckets[new_hash]);
491 ASSERT_BUCKET_LOCK(ht, tbl, new_hash + new_tbl->size);
492 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
493 tbl->buckets[new_hash + new_tbl->size]);
495 unlock_buckets(new_tbl, tbl, new_hash);
498 /* Publish the new, valid hash table */
499 rcu_assign_pointer(ht->tbl, new_tbl);
500 atomic_dec(&ht->shift);
502 /* Wait for readers. No new readers will have references to the
507 bucket_table_free(tbl);
511 EXPORT_SYMBOL_GPL(rhashtable_shrink);
513 static void rht_deferred_worker(struct work_struct *work)
515 struct rhashtable *ht;
516 struct bucket_table *tbl;
517 struct rhashtable_walker *walker;
519 ht = container_of(work, struct rhashtable, run_work);
520 mutex_lock(&ht->mutex);
521 if (ht->being_destroyed)
524 tbl = rht_dereference(ht->tbl, ht);
526 list_for_each_entry(walker, &ht->walkers, list)
527 walker->resize = true;
529 if (rht_grow_above_75(ht, tbl->size))
530 rhashtable_expand(ht);
531 else if (rht_shrink_below_30(ht, tbl->size))
532 rhashtable_shrink(ht);
534 mutex_unlock(&ht->mutex);
537 static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
538 struct bucket_table *tbl,
539 const struct bucket_table *old_tbl, u32 hash)
541 bool no_resize_running = tbl == old_tbl;
542 struct rhash_head *head;
544 hash = rht_bucket_index(tbl, hash);
545 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
547 ASSERT_BUCKET_LOCK(ht, tbl, hash);
549 if (rht_is_a_nulls(head))
550 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
552 RCU_INIT_POINTER(obj->next, head);
554 rcu_assign_pointer(tbl->buckets[hash], obj);
556 atomic_inc(&ht->nelems);
557 if (no_resize_running && rht_grow_above_75(ht, tbl->size))
558 schedule_work(&ht->run_work);
562 * rhashtable_insert - insert object into hash table
564 * @obj: pointer to hash head inside object
566 * Will take a per bucket spinlock to protect against mutual mutations
567 * on the same bucket. Multiple insertions may occur in parallel unless
568 * they map to the same bucket lock.
570 * It is safe to call this function from atomic context.
572 * Will trigger an automatic deferred table resizing if the size grows
573 * beyond the watermark indicated by grow_decision() which can be passed
574 * to rhashtable_init().
576 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
578 struct bucket_table *tbl, *old_tbl;
583 tbl = rht_dereference_rcu(ht->future_tbl, ht);
584 old_tbl = rht_dereference_rcu(ht->tbl, ht);
585 hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
587 lock_buckets(tbl, old_tbl, hash);
588 __rhashtable_insert(ht, obj, tbl, old_tbl, hash);
589 unlock_buckets(tbl, old_tbl, hash);
593 EXPORT_SYMBOL_GPL(rhashtable_insert);
596 * rhashtable_remove - remove object from hash table
598 * @obj: pointer to hash head inside object
600 * Since the hash chain is single linked, the removal operation needs to
601 * walk the bucket chain upon removal. The removal operation is thus
602 * considerable slow if the hash table is not correctly sized.
604 * Will automatically shrink the table via rhashtable_expand() if the
605 * shrink_decision function specified at rhashtable_init() returns true.
607 * The caller must ensure that no concurrent table mutations occur. It is
608 * however valid to have concurrent lookups if they are RCU protected.
610 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
612 struct bucket_table *tbl, *new_tbl, *old_tbl;
613 struct rhash_head __rcu **pprev;
614 struct rhash_head *he, *he2;
615 unsigned int hash, new_hash;
619 old_tbl = rht_dereference_rcu(ht->tbl, ht);
620 tbl = new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
621 new_hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
623 lock_buckets(new_tbl, old_tbl, new_hash);
625 hash = rht_bucket_index(tbl, new_hash);
626 pprev = &tbl->buckets[hash];
627 rht_for_each(he, tbl, hash) {
633 ASSERT_BUCKET_LOCK(ht, tbl, hash);
635 if (old_tbl->size > new_tbl->size && tbl == old_tbl &&
636 !rht_is_a_nulls(obj->next) &&
637 head_hashfn(ht, tbl, obj->next) != hash) {
638 rcu_assign_pointer(*pprev, (struct rhash_head *) rht_marker(ht, hash));
639 } else if (unlikely(old_tbl->size < new_tbl->size && tbl == new_tbl)) {
640 rht_for_each_continue(he2, obj->next, tbl, hash) {
641 if (head_hashfn(ht, tbl, he2) == hash) {
642 rcu_assign_pointer(*pprev, he2);
647 rcu_assign_pointer(*pprev, (struct rhash_head *) rht_marker(ht, hash));
649 rcu_assign_pointer(*pprev, obj->next);
657 /* The entry may be linked in either 'tbl', 'future_tbl', or both.
658 * 'future_tbl' only exists for a short period of time during
659 * resizing. Thus traversing both is fine and the added cost is
662 if (tbl != old_tbl) {
667 unlock_buckets(new_tbl, old_tbl, new_hash);
670 bool no_resize_running = new_tbl == old_tbl;
672 atomic_dec(&ht->nelems);
673 if (no_resize_running && rht_shrink_below_30(ht, new_tbl->size))
674 schedule_work(&ht->run_work);
681 EXPORT_SYMBOL_GPL(rhashtable_remove);
683 struct rhashtable_compare_arg {
684 struct rhashtable *ht;
688 static bool rhashtable_compare(void *ptr, void *arg)
690 struct rhashtable_compare_arg *x = arg;
691 struct rhashtable *ht = x->ht;
693 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
697 * rhashtable_lookup - lookup key in hash table
699 * @key: pointer to key
701 * Computes the hash value for the key and traverses the bucket chain looking
702 * for a entry with an identical key. The first matching entry is returned.
704 * This lookup function may only be used for fixed key hash table (key_len
705 * parameter set). It will BUG() if used inappropriately.
707 * Lookups may occur in parallel with hashtable mutations and resizing.
709 void *rhashtable_lookup(struct rhashtable *ht, const void *key)
711 struct rhashtable_compare_arg arg = {
716 BUG_ON(!ht->p.key_len);
718 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
720 EXPORT_SYMBOL_GPL(rhashtable_lookup);
723 * rhashtable_lookup_compare - search hash table with compare function
725 * @key: the pointer to the key
726 * @compare: compare function, must return true on match
727 * @arg: argument passed on to compare function
729 * Traverses the bucket chain behind the provided hash value and calls the
730 * specified compare function for each entry.
732 * Lookups may occur in parallel with hashtable mutations and resizing.
734 * Returns the first entry on which the compare function returned true.
736 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
737 bool (*compare)(void *, void *), void *arg)
739 const struct bucket_table *tbl, *old_tbl;
740 struct rhash_head *he;
745 old_tbl = rht_dereference_rcu(ht->tbl, ht);
746 tbl = rht_dereference_rcu(ht->future_tbl, ht);
747 hash = key_hashfn(ht, key, ht->p.key_len);
749 rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) {
750 if (!compare(rht_obj(ht, he), arg))
753 return rht_obj(ht, he);
756 if (unlikely(tbl != old_tbl)) {
764 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
767 * rhashtable_lookup_insert - lookup and insert object into hash table
769 * @obj: pointer to hash head inside object
771 * Locks down the bucket chain in both the old and new table if a resize
772 * is in progress to ensure that writers can't remove from the old table
773 * and can't insert to the new table during the atomic operation of search
774 * and insertion. Searches for duplicates in both the old and new table if
775 * a resize is in progress.
777 * This lookup function may only be used for fixed key hash table (key_len
778 * parameter set). It will BUG() if used inappropriately.
780 * It is safe to call this function from atomic context.
782 * Will trigger an automatic deferred table resizing if the size grows
783 * beyond the watermark indicated by grow_decision() which can be passed
784 * to rhashtable_init().
786 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
788 struct rhashtable_compare_arg arg = {
790 .key = rht_obj(ht, obj) + ht->p.key_offset,
793 BUG_ON(!ht->p.key_len);
795 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
798 EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
801 * rhashtable_lookup_compare_insert - search and insert object to hash table
802 * with compare function
804 * @obj: pointer to hash head inside object
805 * @compare: compare function, must return true on match
806 * @arg: argument passed on to compare function
808 * Locks down the bucket chain in both the old and new table if a resize
809 * is in progress to ensure that writers can't remove from the old table
810 * and can't insert to the new table during the atomic operation of search
811 * and insertion. Searches for duplicates in both the old and new table if
812 * a resize is in progress.
814 * Lookups may occur in parallel with hashtable mutations and resizing.
816 * Will trigger an automatic deferred table resizing if the size grows
817 * beyond the watermark indicated by grow_decision() which can be passed
818 * to rhashtable_init().
820 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
821 struct rhash_head *obj,
822 bool (*compare)(void *, void *),
825 struct bucket_table *new_tbl, *old_tbl;
829 BUG_ON(!ht->p.key_len);
832 old_tbl = rht_dereference_rcu(ht->tbl, ht);
833 new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
834 new_hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
836 lock_buckets(new_tbl, old_tbl, new_hash);
838 if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
844 __rhashtable_insert(ht, obj, new_tbl, old_tbl, new_hash);
847 unlock_buckets(new_tbl, old_tbl, new_hash);
852 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
855 * rhashtable_walk_init - Initialise an iterator
856 * @ht: Table to walk over
857 * @iter: Hash table Iterator
859 * This function prepares a hash table walk.
861 * Note that if you restart a walk after rhashtable_walk_stop you
862 * may see the same object twice. Also, you may miss objects if
863 * there are removals in between rhashtable_walk_stop and the next
864 * call to rhashtable_walk_start.
866 * For a completely stable walk you should construct your own data
867 * structure outside the hash table.
869 * This function may sleep so you must not call it from interrupt
870 * context or with spin locks held.
872 * You must call rhashtable_walk_exit if this function returns
875 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
882 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
886 INIT_LIST_HEAD(&iter->walker->list);
887 iter->walker->resize = false;
889 mutex_lock(&ht->mutex);
890 list_add(&iter->walker->list, &ht->walkers);
891 mutex_unlock(&ht->mutex);
895 EXPORT_SYMBOL_GPL(rhashtable_walk_init);
898 * rhashtable_walk_exit - Free an iterator
899 * @iter: Hash table Iterator
901 * This function frees resources allocated by rhashtable_walk_init.
903 void rhashtable_walk_exit(struct rhashtable_iter *iter)
905 mutex_lock(&iter->ht->mutex);
906 list_del(&iter->walker->list);
907 mutex_unlock(&iter->ht->mutex);
910 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
913 * rhashtable_walk_start - Start a hash table walk
914 * @iter: Hash table iterator
916 * Start a hash table walk. Note that we take the RCU lock in all
917 * cases including when we return an error. So you must always call
918 * rhashtable_walk_stop to clean up.
920 * Returns zero if successful.
922 * Returns -EAGAIN if resize event occured. Note that the iterator
923 * will rewind back to the beginning and you may use it immediately
924 * by calling rhashtable_walk_next.
926 int rhashtable_walk_start(struct rhashtable_iter *iter)
930 if (iter->walker->resize) {
933 iter->walker->resize = false;
939 EXPORT_SYMBOL_GPL(rhashtable_walk_start);
942 * rhashtable_walk_next - Return the next object and advance the iterator
943 * @iter: Hash table iterator
945 * Note that you must call rhashtable_walk_stop when you are finished
948 * Returns the next object or NULL when the end of the table is reached.
950 * Returns -EAGAIN if resize event occured. Note that the iterator
951 * will rewind back to the beginning and you may continue to use it.
953 void *rhashtable_walk_next(struct rhashtable_iter *iter)
955 const struct bucket_table *tbl;
956 struct rhashtable *ht = iter->ht;
957 struct rhash_head *p = iter->p;
960 tbl = rht_dereference_rcu(ht->tbl, ht);
963 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
967 for (; iter->slot < tbl->size; iter->slot++) {
968 int skip = iter->skip;
970 rht_for_each_rcu(p, tbl, iter->slot) {
977 if (!rht_is_a_nulls(p)) {
980 obj = rht_obj(ht, p);
990 if (iter->walker->resize) {
994 iter->walker->resize = false;
995 return ERR_PTR(-EAGAIN);
1000 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
1003 * rhashtable_walk_stop - Finish a hash table walk
1004 * @iter: Hash table iterator
1006 * Finish a hash table walk.
1008 void rhashtable_walk_stop(struct rhashtable_iter *iter)
1013 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
1015 static size_t rounded_hashtable_size(struct rhashtable_params *params)
1017 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
1018 1UL << params->min_shift);
1022 * rhashtable_init - initialize a new hash table
1023 * @ht: hash table to be initialized
1024 * @params: configuration parameters
1026 * Initializes a new hash table based on the provided configuration
1027 * parameters. A table can be configured either with a variable or
1030 * Configuration Example 1: Fixed length keys
1034 * struct rhash_head node;
1037 * struct rhashtable_params params = {
1038 * .head_offset = offsetof(struct test_obj, node),
1039 * .key_offset = offsetof(struct test_obj, key),
1040 * .key_len = sizeof(int),
1042 * .nulls_base = (1U << RHT_BASE_SHIFT),
1045 * Configuration Example 2: Variable length keys
1048 * struct rhash_head node;
1051 * u32 my_hash_fn(const void *data, u32 seed)
1053 * struct test_obj *obj = data;
1055 * return [... hash ...];
1058 * struct rhashtable_params params = {
1059 * .head_offset = offsetof(struct test_obj, node),
1061 * .obj_hashfn = my_hash_fn,
1064 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
1066 struct bucket_table *tbl;
1069 size = HASH_DEFAULT_SIZE;
1071 if ((params->key_len && !params->hashfn) ||
1072 (!params->key_len && !params->obj_hashfn))
1075 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
1078 params->min_shift = max_t(size_t, params->min_shift,
1079 ilog2(HASH_MIN_SIZE));
1081 if (params->nelem_hint)
1082 size = rounded_hashtable_size(params);
1084 memset(ht, 0, sizeof(*ht));
1085 mutex_init(&ht->mutex);
1086 memcpy(&ht->p, params, sizeof(*params));
1087 INIT_LIST_HEAD(&ht->walkers);
1089 if (params->locks_mul)
1090 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1092 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1094 tbl = bucket_table_alloc(ht, size);
1098 atomic_set(&ht->nelems, 0);
1099 atomic_set(&ht->shift, ilog2(tbl->size));
1100 RCU_INIT_POINTER(ht->tbl, tbl);
1101 RCU_INIT_POINTER(ht->future_tbl, tbl);
1103 if (!ht->p.hash_rnd)
1104 get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
1106 INIT_WORK(&ht->run_work, rht_deferred_worker);
1110 EXPORT_SYMBOL_GPL(rhashtable_init);
1113 * rhashtable_destroy - destroy hash table
1114 * @ht: the hash table to destroy
1116 * Frees the bucket array. This function is not rcu safe, therefore the caller
1117 * has to make sure that no resizing may happen by unpublishing the hashtable
1118 * and waiting for the quiescent cycle before releasing the bucket array.
1120 void rhashtable_destroy(struct rhashtable *ht)
1122 ht->being_destroyed = true;
1124 cancel_work_sync(&ht->run_work);
1126 mutex_lock(&ht->mutex);
1127 bucket_table_free(rht_dereference(ht->tbl, ht));
1128 mutex_unlock(&ht->mutex);
1130 EXPORT_SYMBOL_GPL(rhashtable_destroy);