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, const void *ptr)
71 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
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 void *key, u32 len)
85 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
87 return ht->p.hashfn(key, len, tbl->hash_rnd) >> HASH_RESERVED_SPACE;
90 static u32 head_hashfn(struct rhashtable *ht,
91 const struct bucket_table *tbl,
92 const struct rhash_head *he)
94 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
97 #ifdef CONFIG_PROVE_LOCKING
98 static void debug_dump_buckets(struct rhashtable *ht,
99 const struct bucket_table *tbl)
101 struct rhash_head *he;
102 unsigned int i, hash;
104 for (i = 0; i < tbl->size; i++) {
105 pr_warn(" [Bucket %d] ", i);
106 rht_for_each_rcu(he, tbl, i) {
107 hash = head_hashfn(ht, tbl, he);
108 pr_cont("[hash = %#x, lock = %p] ",
109 hash, bucket_lock(tbl, hash));
116 static void debug_dump_table(struct rhashtable *ht,
117 const struct bucket_table *tbl,
120 struct bucket_table *old_tbl, *future_tbl;
122 pr_emerg("BUG: lock for hash %#x in table %p not held\n",
126 future_tbl = rht_dereference_rcu(ht->future_tbl, ht);
127 old_tbl = rht_dereference_rcu(ht->tbl, ht);
128 if (future_tbl != old_tbl) {
129 pr_warn("Future table %p (size: %zd)\n",
130 future_tbl, future_tbl->size);
131 debug_dump_buckets(ht, future_tbl);
134 pr_warn("Table %p (size: %zd)\n", old_tbl, old_tbl->size);
135 debug_dump_buckets(ht, old_tbl);
140 #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
141 #define ASSERT_BUCKET_LOCK(HT, TBL, HASH) \
143 if (unlikely(!lockdep_rht_bucket_is_held(TBL, HASH))) { \
144 debug_dump_table(HT, TBL, HASH); \
149 int lockdep_rht_mutex_is_held(struct rhashtable *ht)
151 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
153 EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
155 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
157 spinlock_t *lock = bucket_lock(tbl, hash);
159 return (debug_locks) ? lockdep_is_held(lock) : 1;
161 EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
163 #define ASSERT_RHT_MUTEX(HT)
164 #define ASSERT_BUCKET_LOCK(HT, TBL, HASH)
168 static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n)
170 struct rhash_head __rcu **pprev;
172 for (pprev = &tbl->buckets[n];
173 !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n));
174 pprev = &rht_dereference_bucket(*pprev, tbl, n)->next)
180 static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
182 unsigned int i, size;
183 #if defined(CONFIG_PROVE_LOCKING)
184 unsigned int nr_pcpus = 2;
186 unsigned int nr_pcpus = num_possible_cpus();
189 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
190 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
192 /* Never allocate more than 0.5 locks per bucket */
193 size = min_t(unsigned int, size, tbl->size >> 1);
195 if (sizeof(spinlock_t) != 0) {
197 if (size * sizeof(spinlock_t) > PAGE_SIZE)
198 tbl->locks = vmalloc(size * sizeof(spinlock_t));
201 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
205 for (i = 0; i < size; i++)
206 spin_lock_init(&tbl->locks[i]);
208 tbl->locks_mask = size - 1;
213 static void bucket_table_free(const struct bucket_table *tbl)
221 static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
224 struct bucket_table *tbl = NULL;
228 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
229 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
230 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
236 tbl->size = nbuckets;
238 if (alloc_bucket_locks(ht, tbl) < 0) {
239 bucket_table_free(tbl);
243 for (i = 0; i < nbuckets; i++)
244 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
250 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
252 * @new_size: new table size
254 static bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
256 /* Expand table when exceeding 75% load */
257 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
258 (!ht->p.max_shift || atomic_read(&ht->shift) < ht->p.max_shift);
262 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
264 * @new_size: new table size
266 static bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
268 /* Shrink table beneath 30% load */
269 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
270 (atomic_read(&ht->shift) > ht->p.min_shift);
273 static void lock_buckets(struct bucket_table *new_tbl,
274 struct bucket_table *old_tbl, unsigned int hash)
275 __acquires(old_bucket_lock)
277 spin_lock_bh(bucket_lock(old_tbl, hash));
278 if (new_tbl != old_tbl)
279 spin_lock_bh_nested(bucket_lock(new_tbl, hash),
283 static void unlock_buckets(struct bucket_table *new_tbl,
284 struct bucket_table *old_tbl, unsigned int hash)
285 __releases(old_bucket_lock)
287 if (new_tbl != old_tbl)
288 spin_unlock_bh(bucket_lock(new_tbl, hash));
289 spin_unlock_bh(bucket_lock(old_tbl, hash));
293 * Unlink entries on bucket which hash to different bucket.
295 * Returns true if no more work needs to be performed on the bucket.
297 static bool hashtable_chain_unzip(struct rhashtable *ht,
298 const struct bucket_table *new_tbl,
299 struct bucket_table *old_tbl,
302 struct rhash_head *he, *p, *next;
303 unsigned int new_hash, new_hash2;
305 ASSERT_BUCKET_LOCK(ht, old_tbl, old_hash);
307 /* Old bucket empty, no work needed. */
308 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
310 if (rht_is_a_nulls(p))
313 new_hash = head_hashfn(ht, new_tbl, p);
314 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
316 /* Advance the old bucket pointer one or more times until it
317 * reaches a node that doesn't hash to the same bucket as the
318 * previous node p. Call the previous node p;
320 rht_for_each_continue(he, p->next, old_tbl, old_hash) {
321 new_hash2 = head_hashfn(ht, new_tbl, he);
322 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash2);
324 if (new_hash != new_hash2)
328 rcu_assign_pointer(old_tbl->buckets[old_hash], p->next);
330 /* Find the subsequent node which does hash to the same
331 * bucket as node P, or NULL if no such node exists.
333 INIT_RHT_NULLS_HEAD(next, ht, old_hash);
334 if (!rht_is_a_nulls(he)) {
335 rht_for_each_continue(he, he->next, old_tbl, old_hash) {
336 if (head_hashfn(ht, new_tbl, he) == new_hash) {
343 /* Set p's next pointer to that subsequent node pointer,
344 * bypassing the nodes which do not hash to p's bucket
346 rcu_assign_pointer(p->next, next);
348 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
351 return !rht_is_a_nulls(p);
354 static void link_old_to_new(struct rhashtable *ht, struct bucket_table *new_tbl,
355 unsigned int new_hash, struct rhash_head *entry)
357 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
359 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry);
363 * rhashtable_expand - Expand hash table while allowing concurrent lookups
364 * @ht: the hash table to expand
366 * A secondary bucket array is allocated and the hash entries are migrated
367 * while keeping them on both lists until the end of the RCU grace period.
369 * This function may only be called in a context where it is safe to call
370 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
372 * The caller must ensure that no concurrent resizing occurs by holding
375 * It is valid to have concurrent insertions and deletions protected by per
376 * bucket locks or concurrent RCU protected lookups and traversals.
378 int rhashtable_expand(struct rhashtable *ht)
380 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
381 struct rhash_head *he;
382 unsigned int new_hash, old_hash;
383 bool complete = false;
385 ASSERT_RHT_MUTEX(ht);
387 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
391 new_tbl->hash_rnd = old_tbl->hash_rnd;
393 atomic_inc(&ht->shift);
395 /* Make insertions go into the new, empty table right away. Deletions
396 * and lookups will be attempted in both tables until we synchronize.
397 * The synchronize_rcu() guarantees for the new table to be picked up
398 * so no new additions go into the old table while we relink.
400 rcu_assign_pointer(ht->future_tbl, new_tbl);
403 /* For each new bucket, search the corresponding old bucket for the
404 * first entry that hashes to the new bucket, and link the end of
405 * newly formed bucket chain (containing entries added to future
406 * table) to that entry. Since all the entries which will end up in
407 * the new bucket appear in the same old bucket, this constructs an
408 * entirely valid new hash table, but with multiple buckets
409 * "zipped" together into a single imprecise chain.
411 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
412 old_hash = rht_bucket_index(old_tbl, new_hash);
413 lock_buckets(new_tbl, old_tbl, new_hash);
414 rht_for_each(he, old_tbl, old_hash) {
415 if (head_hashfn(ht, new_tbl, he) == new_hash) {
416 link_old_to_new(ht, new_tbl, new_hash, he);
420 unlock_buckets(new_tbl, old_tbl, new_hash);
424 /* Unzip interleaved hash chains */
425 while (!complete && !ht->being_destroyed) {
426 /* Wait for readers. All new readers will see the new
427 * table, and thus no references to the old table will
432 /* For each bucket in the old table (each of which
433 * contains items from multiple buckets of the new
437 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
438 lock_buckets(new_tbl, old_tbl, old_hash);
440 if (hashtable_chain_unzip(ht, new_tbl, old_tbl,
444 unlock_buckets(new_tbl, old_tbl, old_hash);
449 rcu_assign_pointer(ht->tbl, new_tbl);
452 bucket_table_free(old_tbl);
455 EXPORT_SYMBOL_GPL(rhashtable_expand);
458 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
459 * @ht: the hash table to shrink
461 * This function may only be called in a context where it is safe to call
462 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
464 * The caller must ensure that no concurrent resizing occurs by holding
467 * The caller must ensure that no concurrent table mutations take place.
468 * It is however valid to have concurrent lookups if they are RCU protected.
470 * It is valid to have concurrent insertions and deletions protected by per
471 * bucket locks or concurrent RCU protected lookups and traversals.
473 int rhashtable_shrink(struct rhashtable *ht)
475 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
476 unsigned int new_hash;
478 ASSERT_RHT_MUTEX(ht);
480 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
484 new_tbl->hash_rnd = tbl->hash_rnd;
486 rcu_assign_pointer(ht->future_tbl, new_tbl);
489 /* Link the first entry in the old bucket to the end of the
490 * bucket in the new table. As entries are concurrently being
491 * added to the new table, lock down the new bucket. As we
492 * always divide the size in half when shrinking, each bucket
493 * in the new table maps to exactly two buckets in the old
496 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
497 lock_buckets(new_tbl, tbl, new_hash);
499 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
500 tbl->buckets[new_hash]);
501 ASSERT_BUCKET_LOCK(ht, tbl, new_hash + new_tbl->size);
502 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
503 tbl->buckets[new_hash + new_tbl->size]);
505 unlock_buckets(new_tbl, tbl, new_hash);
509 /* Publish the new, valid hash table */
510 rcu_assign_pointer(ht->tbl, new_tbl);
511 atomic_dec(&ht->shift);
513 /* Wait for readers. No new readers will have references to the
518 bucket_table_free(tbl);
522 EXPORT_SYMBOL_GPL(rhashtable_shrink);
524 static void rht_deferred_worker(struct work_struct *work)
526 struct rhashtable *ht;
527 struct bucket_table *tbl;
528 struct rhashtable_walker *walker;
530 ht = container_of(work, struct rhashtable, run_work);
531 mutex_lock(&ht->mutex);
532 if (ht->being_destroyed)
535 tbl = rht_dereference(ht->tbl, ht);
537 list_for_each_entry(walker, &ht->walkers, list)
538 walker->resize = true;
540 if (rht_grow_above_75(ht, tbl->size))
541 rhashtable_expand(ht);
542 else if (rht_shrink_below_30(ht, tbl->size))
543 rhashtable_shrink(ht);
545 mutex_unlock(&ht->mutex);
548 static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
549 struct bucket_table *tbl,
550 const struct bucket_table *old_tbl, u32 hash)
552 bool no_resize_running = tbl == old_tbl;
553 struct rhash_head *head;
555 hash = rht_bucket_index(tbl, hash);
556 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
558 ASSERT_BUCKET_LOCK(ht, tbl, hash);
560 if (rht_is_a_nulls(head))
561 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
563 RCU_INIT_POINTER(obj->next, head);
565 rcu_assign_pointer(tbl->buckets[hash], obj);
567 atomic_inc(&ht->nelems);
568 if (no_resize_running && rht_grow_above_75(ht, tbl->size))
569 schedule_work(&ht->run_work);
573 * rhashtable_insert - insert object into hash table
575 * @obj: pointer to hash head inside object
577 * Will take a per bucket spinlock to protect against mutual mutations
578 * on the same bucket. Multiple insertions may occur in parallel unless
579 * they map to the same bucket lock.
581 * It is safe to call this function from atomic context.
583 * Will trigger an automatic deferred table resizing if the size grows
584 * beyond the watermark indicated by grow_decision() which can be passed
585 * to rhashtable_init().
587 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
589 struct bucket_table *tbl, *old_tbl;
594 tbl = rht_dereference_rcu(ht->future_tbl, ht);
595 old_tbl = rht_dereference_rcu(ht->tbl, ht);
596 hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
598 lock_buckets(tbl, old_tbl, hash);
599 __rhashtable_insert(ht, obj, tbl, old_tbl, hash);
600 unlock_buckets(tbl, old_tbl, hash);
604 EXPORT_SYMBOL_GPL(rhashtable_insert);
607 * rhashtable_remove - remove object from hash table
609 * @obj: pointer to hash head inside object
611 * Since the hash chain is single linked, the removal operation needs to
612 * walk the bucket chain upon removal. The removal operation is thus
613 * considerable slow if the hash table is not correctly sized.
615 * Will automatically shrink the table via rhashtable_expand() if the
616 * shrink_decision function specified at rhashtable_init() returns true.
618 * The caller must ensure that no concurrent table mutations occur. It is
619 * however valid to have concurrent lookups if they are RCU protected.
621 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
623 struct bucket_table *tbl, *new_tbl, *old_tbl;
624 struct rhash_head __rcu **pprev;
625 struct rhash_head *he, *he2;
626 unsigned int hash, new_hash;
630 old_tbl = rht_dereference_rcu(ht->tbl, ht);
631 tbl = new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
632 new_hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
634 lock_buckets(new_tbl, old_tbl, new_hash);
636 hash = rht_bucket_index(tbl, new_hash);
637 pprev = &tbl->buckets[hash];
638 rht_for_each(he, tbl, hash) {
644 ASSERT_BUCKET_LOCK(ht, tbl, hash);
646 if (old_tbl->size > new_tbl->size && tbl == old_tbl &&
647 !rht_is_a_nulls(obj->next) &&
648 head_hashfn(ht, tbl, obj->next) != hash) {
649 rcu_assign_pointer(*pprev, (struct rhash_head *) rht_marker(ht, hash));
650 } else if (unlikely(old_tbl->size < new_tbl->size && tbl == new_tbl)) {
651 rht_for_each_continue(he2, obj->next, tbl, hash) {
652 if (head_hashfn(ht, tbl, he2) == hash) {
653 rcu_assign_pointer(*pprev, he2);
658 rcu_assign_pointer(*pprev, (struct rhash_head *) rht_marker(ht, hash));
660 rcu_assign_pointer(*pprev, obj->next);
668 /* The entry may be linked in either 'tbl', 'future_tbl', or both.
669 * 'future_tbl' only exists for a short period of time during
670 * resizing. Thus traversing both is fine and the added cost is
673 if (tbl != old_tbl) {
678 unlock_buckets(new_tbl, old_tbl, new_hash);
681 bool no_resize_running = new_tbl == old_tbl;
683 atomic_dec(&ht->nelems);
684 if (no_resize_running && rht_shrink_below_30(ht, new_tbl->size))
685 schedule_work(&ht->run_work);
692 EXPORT_SYMBOL_GPL(rhashtable_remove);
694 struct rhashtable_compare_arg {
695 struct rhashtable *ht;
699 static bool rhashtable_compare(void *ptr, void *arg)
701 struct rhashtable_compare_arg *x = arg;
702 struct rhashtable *ht = x->ht;
704 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
708 * rhashtable_lookup - lookup key in hash table
710 * @key: pointer to key
712 * Computes the hash value for the key and traverses the bucket chain looking
713 * for a entry with an identical key. The first matching entry is returned.
715 * This lookup function may only be used for fixed key hash table (key_len
716 * parameter set). It will BUG() if used inappropriately.
718 * Lookups may occur in parallel with hashtable mutations and resizing.
720 void *rhashtable_lookup(struct rhashtable *ht, const void *key)
722 struct rhashtable_compare_arg arg = {
727 BUG_ON(!ht->p.key_len);
729 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
731 EXPORT_SYMBOL_GPL(rhashtable_lookup);
734 * rhashtable_lookup_compare - search hash table with compare function
736 * @key: the pointer to the key
737 * @compare: compare function, must return true on match
738 * @arg: argument passed on to compare function
740 * Traverses the bucket chain behind the provided hash value and calls the
741 * specified compare function for each entry.
743 * Lookups may occur in parallel with hashtable mutations and resizing.
745 * Returns the first entry on which the compare function returned true.
747 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
748 bool (*compare)(void *, void *), void *arg)
750 const struct bucket_table *tbl, *old_tbl;
751 struct rhash_head *he;
756 old_tbl = rht_dereference_rcu(ht->tbl, ht);
757 tbl = rht_dereference_rcu(ht->future_tbl, ht);
758 hash = key_hashfn(ht, key, ht->p.key_len);
760 rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) {
761 if (!compare(rht_obj(ht, he), arg))
764 return rht_obj(ht, he);
767 if (unlikely(tbl != old_tbl)) {
775 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
778 * rhashtable_lookup_insert - lookup and insert object into hash table
780 * @obj: pointer to hash head inside object
782 * Locks down the bucket chain in both the old and new table if a resize
783 * is in progress to ensure that writers can't remove from the old table
784 * and can't insert to the new table during the atomic operation of search
785 * and insertion. Searches for duplicates in both the old and new table if
786 * a resize is in progress.
788 * This lookup function may only be used for fixed key hash table (key_len
789 * parameter set). It will BUG() if used inappropriately.
791 * It is safe to call this function from atomic context.
793 * Will trigger an automatic deferred table resizing if the size grows
794 * beyond the watermark indicated by grow_decision() which can be passed
795 * to rhashtable_init().
797 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
799 struct rhashtable_compare_arg arg = {
801 .key = rht_obj(ht, obj) + ht->p.key_offset,
804 BUG_ON(!ht->p.key_len);
806 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
809 EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
812 * rhashtable_lookup_compare_insert - search and insert object to hash table
813 * with compare function
815 * @obj: pointer to hash head inside object
816 * @compare: compare function, must return true on match
817 * @arg: argument passed on to compare function
819 * Locks down the bucket chain in both the old and new table if a resize
820 * is in progress to ensure that writers can't remove from the old table
821 * and can't insert to the new table during the atomic operation of search
822 * and insertion. Searches for duplicates in both the old and new table if
823 * a resize is in progress.
825 * Lookups may occur in parallel with hashtable mutations and resizing.
827 * Will trigger an automatic deferred table resizing if the size grows
828 * beyond the watermark indicated by grow_decision() which can be passed
829 * to rhashtable_init().
831 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
832 struct rhash_head *obj,
833 bool (*compare)(void *, void *),
836 struct bucket_table *new_tbl, *old_tbl;
840 BUG_ON(!ht->p.key_len);
843 old_tbl = rht_dereference_rcu(ht->tbl, ht);
844 new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
845 new_hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
847 lock_buckets(new_tbl, old_tbl, new_hash);
849 if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
855 __rhashtable_insert(ht, obj, new_tbl, old_tbl, new_hash);
858 unlock_buckets(new_tbl, old_tbl, new_hash);
863 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
866 * rhashtable_walk_init - Initialise an iterator
867 * @ht: Table to walk over
868 * @iter: Hash table Iterator
870 * This function prepares a hash table walk.
872 * Note that if you restart a walk after rhashtable_walk_stop you
873 * may see the same object twice. Also, you may miss objects if
874 * there are removals in between rhashtable_walk_stop and the next
875 * call to rhashtable_walk_start.
877 * For a completely stable walk you should construct your own data
878 * structure outside the hash table.
880 * This function may sleep so you must not call it from interrupt
881 * context or with spin locks held.
883 * You must call rhashtable_walk_exit if this function returns
886 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
893 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
897 INIT_LIST_HEAD(&iter->walker->list);
898 iter->walker->resize = false;
900 mutex_lock(&ht->mutex);
901 list_add(&iter->walker->list, &ht->walkers);
902 mutex_unlock(&ht->mutex);
906 EXPORT_SYMBOL_GPL(rhashtable_walk_init);
909 * rhashtable_walk_exit - Free an iterator
910 * @iter: Hash table Iterator
912 * This function frees resources allocated by rhashtable_walk_init.
914 void rhashtable_walk_exit(struct rhashtable_iter *iter)
916 mutex_lock(&iter->ht->mutex);
917 list_del(&iter->walker->list);
918 mutex_unlock(&iter->ht->mutex);
921 EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
924 * rhashtable_walk_start - Start a hash table walk
925 * @iter: Hash table iterator
927 * Start a hash table walk. Note that we take the RCU lock in all
928 * cases including when we return an error. So you must always call
929 * rhashtable_walk_stop to clean up.
931 * Returns zero if successful.
933 * Returns -EAGAIN if resize event occured. Note that the iterator
934 * will rewind back to the beginning and you may use it immediately
935 * by calling rhashtable_walk_next.
937 int rhashtable_walk_start(struct rhashtable_iter *iter)
941 if (iter->walker->resize) {
944 iter->walker->resize = false;
950 EXPORT_SYMBOL_GPL(rhashtable_walk_start);
953 * rhashtable_walk_next - Return the next object and advance the iterator
954 * @iter: Hash table iterator
956 * Note that you must call rhashtable_walk_stop when you are finished
959 * Returns the next object or NULL when the end of the table is reached.
961 * Returns -EAGAIN if resize event occured. Note that the iterator
962 * will rewind back to the beginning and you may continue to use it.
964 void *rhashtable_walk_next(struct rhashtable_iter *iter)
966 const struct bucket_table *tbl;
967 struct rhashtable *ht = iter->ht;
968 struct rhash_head *p = iter->p;
971 tbl = rht_dereference_rcu(ht->tbl, ht);
974 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
978 for (; iter->slot < tbl->size; iter->slot++) {
979 int skip = iter->skip;
981 rht_for_each_rcu(p, tbl, iter->slot) {
988 if (!rht_is_a_nulls(p)) {
991 obj = rht_obj(ht, p);
1001 if (iter->walker->resize) {
1005 iter->walker->resize = false;
1006 return ERR_PTR(-EAGAIN);
1011 EXPORT_SYMBOL_GPL(rhashtable_walk_next);
1014 * rhashtable_walk_stop - Finish a hash table walk
1015 * @iter: Hash table iterator
1017 * Finish a hash table walk.
1019 void rhashtable_walk_stop(struct rhashtable_iter *iter)
1024 EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
1026 static size_t rounded_hashtable_size(struct rhashtable_params *params)
1028 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
1029 1UL << params->min_shift);
1033 * rhashtable_init - initialize a new hash table
1034 * @ht: hash table to be initialized
1035 * @params: configuration parameters
1037 * Initializes a new hash table based on the provided configuration
1038 * parameters. A table can be configured either with a variable or
1041 * Configuration Example 1: Fixed length keys
1045 * struct rhash_head node;
1048 * struct rhashtable_params params = {
1049 * .head_offset = offsetof(struct test_obj, node),
1050 * .key_offset = offsetof(struct test_obj, key),
1051 * .key_len = sizeof(int),
1053 * .nulls_base = (1U << RHT_BASE_SHIFT),
1056 * Configuration Example 2: Variable length keys
1059 * struct rhash_head node;
1062 * u32 my_hash_fn(const void *data, u32 seed)
1064 * struct test_obj *obj = data;
1066 * return [... hash ...];
1069 * struct rhashtable_params params = {
1070 * .head_offset = offsetof(struct test_obj, node),
1072 * .obj_hashfn = my_hash_fn,
1075 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
1077 struct bucket_table *tbl;
1080 size = HASH_DEFAULT_SIZE;
1082 if ((params->key_len && !params->hashfn) ||
1083 (!params->key_len && !params->obj_hashfn))
1086 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
1089 params->min_shift = max_t(size_t, params->min_shift,
1090 ilog2(HASH_MIN_SIZE));
1092 if (params->nelem_hint)
1093 size = rounded_hashtable_size(params);
1095 memset(ht, 0, sizeof(*ht));
1096 mutex_init(&ht->mutex);
1097 memcpy(&ht->p, params, sizeof(*params));
1098 INIT_LIST_HEAD(&ht->walkers);
1100 if (params->locks_mul)
1101 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1103 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1105 tbl = bucket_table_alloc(ht, size);
1109 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
1111 atomic_set(&ht->nelems, 0);
1112 atomic_set(&ht->shift, ilog2(tbl->size));
1113 RCU_INIT_POINTER(ht->tbl, tbl);
1114 RCU_INIT_POINTER(ht->future_tbl, tbl);
1116 INIT_WORK(&ht->run_work, rht_deferred_worker);
1120 EXPORT_SYMBOL_GPL(rhashtable_init);
1123 * rhashtable_destroy - destroy hash table
1124 * @ht: the hash table to destroy
1126 * Frees the bucket array. This function is not rcu safe, therefore the caller
1127 * has to make sure that no resizing may happen by unpublishing the hashtable
1128 * and waiting for the quiescent cycle before releasing the bucket array.
1130 void rhashtable_destroy(struct rhashtable *ht)
1132 ht->being_destroyed = true;
1134 cancel_work_sync(&ht->run_work);
1136 mutex_lock(&ht->mutex);
1137 bucket_table_free(rht_dereference(ht->tbl, ht));
1138 mutex_unlock(&ht->mutex);
1140 EXPORT_SYMBOL_GPL(rhashtable_destroy);