17e1e7763SThomas Graf /* 27e1e7763SThomas Graf * Resizable, Scalable, Concurrent Hash Table 37e1e7763SThomas Graf * 47e1e7763SThomas Graf * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch> 57e1e7763SThomas Graf * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> 67e1e7763SThomas Graf * 77e1e7763SThomas Graf * Based on the following paper: 87e1e7763SThomas Graf * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf 97e1e7763SThomas Graf * 107e1e7763SThomas Graf * Code partially derived from nft_hash 117e1e7763SThomas Graf * 127e1e7763SThomas Graf * This program is free software; you can redistribute it and/or modify 137e1e7763SThomas Graf * it under the terms of the GNU General Public License version 2 as 147e1e7763SThomas Graf * published by the Free Software Foundation. 157e1e7763SThomas Graf */ 167e1e7763SThomas Graf 177e1e7763SThomas Graf #include <linux/kernel.h> 187e1e7763SThomas Graf #include <linux/init.h> 197e1e7763SThomas Graf #include <linux/log2.h> 207e1e7763SThomas Graf #include <linux/slab.h> 217e1e7763SThomas Graf #include <linux/vmalloc.h> 227e1e7763SThomas Graf #include <linux/mm.h> 2387545899SDaniel Borkmann #include <linux/jhash.h> 247e1e7763SThomas Graf #include <linux/random.h> 257e1e7763SThomas Graf #include <linux/rhashtable.h> 267e1e7763SThomas Graf 277e1e7763SThomas Graf #define HASH_DEFAULT_SIZE 64UL 287e1e7763SThomas Graf #define HASH_MIN_SIZE 4UL 2997defe1eSThomas Graf #define BUCKET_LOCKS_PER_CPU 128UL 3097defe1eSThomas Graf 31f89bd6f8SThomas Graf /* Base bits plus 1 bit for nulls marker */ 32f89bd6f8SThomas Graf #define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1) 33f89bd6f8SThomas Graf 3497defe1eSThomas Graf enum { 3597defe1eSThomas Graf RHT_LOCK_NORMAL, 3697defe1eSThomas Graf RHT_LOCK_NESTED, 3797defe1eSThomas Graf RHT_LOCK_NESTED2, 3897defe1eSThomas Graf }; 3997defe1eSThomas Graf 4097defe1eSThomas Graf /* The bucket lock is selected based on the hash and protects mutations 4197defe1eSThomas Graf * on a group of hash buckets. 4297defe1eSThomas Graf * 4397defe1eSThomas Graf * IMPORTANT: When holding the bucket lock of both the old and new table 4497defe1eSThomas Graf * during expansions and shrinking, the old bucket lock must always be 4597defe1eSThomas Graf * acquired first. 4697defe1eSThomas Graf */ 4797defe1eSThomas Graf static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash) 4897defe1eSThomas Graf { 4997defe1eSThomas Graf return &tbl->locks[hash & tbl->locks_mask]; 5097defe1eSThomas Graf } 517e1e7763SThomas Graf 527e1e7763SThomas Graf #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) 5397defe1eSThomas Graf #define ASSERT_BUCKET_LOCK(TBL, HASH) \ 5497defe1eSThomas Graf BUG_ON(!lockdep_rht_bucket_is_held(TBL, HASH)) 557e1e7763SThomas Graf 567e1e7763SThomas Graf #ifdef CONFIG_PROVE_LOCKING 5797defe1eSThomas Graf int lockdep_rht_mutex_is_held(struct rhashtable *ht) 587e1e7763SThomas Graf { 5997defe1eSThomas Graf return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; 607e1e7763SThomas Graf } 617e1e7763SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); 6288d6ed15SThomas Graf 6388d6ed15SThomas Graf int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) 6488d6ed15SThomas Graf { 6597defe1eSThomas Graf spinlock_t *lock = bucket_lock(tbl, hash); 6697defe1eSThomas Graf 6797defe1eSThomas Graf return (debug_locks) ? lockdep_is_held(lock) : 1; 6888d6ed15SThomas Graf } 6988d6ed15SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); 707e1e7763SThomas Graf #endif 717e1e7763SThomas Graf 72c91eee56SThomas Graf static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he) 737e1e7763SThomas Graf { 747e1e7763SThomas Graf return (void *) he - ht->p.head_offset; 757e1e7763SThomas Graf } 767e1e7763SThomas Graf 778d24c0b4SThomas Graf static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash) 787e1e7763SThomas Graf { 798d24c0b4SThomas Graf return hash & (tbl->size - 1); 807e1e7763SThomas Graf } 817e1e7763SThomas Graf 828d24c0b4SThomas Graf static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr) 838d24c0b4SThomas Graf { 848d24c0b4SThomas Graf u32 hash; 858d24c0b4SThomas Graf 868d24c0b4SThomas Graf if (unlikely(!ht->p.key_len)) 878d24c0b4SThomas Graf hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd); 888d24c0b4SThomas Graf else 898d24c0b4SThomas Graf hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len, 908d24c0b4SThomas Graf ht->p.hash_rnd); 918d24c0b4SThomas Graf 92f89bd6f8SThomas Graf return hash >> HASH_RESERVED_SPACE; 938d24c0b4SThomas Graf } 948d24c0b4SThomas Graf 9597defe1eSThomas Graf static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len) 967e1e7763SThomas Graf { 977e1e7763SThomas Graf struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); 988d24c0b4SThomas Graf u32 hash; 997e1e7763SThomas Graf 1008d24c0b4SThomas Graf hash = ht->p.hashfn(key, len, ht->p.hash_rnd); 101f89bd6f8SThomas Graf hash >>= HASH_RESERVED_SPACE; 1028d24c0b4SThomas Graf 1038d24c0b4SThomas Graf return rht_bucket_index(tbl, hash); 1047e1e7763SThomas Graf } 1057e1e7763SThomas Graf 1067e1e7763SThomas Graf static u32 head_hashfn(const struct rhashtable *ht, 1078d24c0b4SThomas Graf const struct bucket_table *tbl, 1088d24c0b4SThomas Graf const struct rhash_head *he) 1097e1e7763SThomas Graf { 1108d24c0b4SThomas Graf return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he))); 1117e1e7763SThomas Graf } 1127e1e7763SThomas Graf 113b8e1943eSThomas Graf static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n) 114b8e1943eSThomas Graf { 115b8e1943eSThomas Graf struct rhash_head __rcu **pprev; 116b8e1943eSThomas Graf 117b8e1943eSThomas Graf for (pprev = &tbl->buckets[n]; 118f89bd6f8SThomas Graf !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n)); 119b8e1943eSThomas Graf pprev = &rht_dereference_bucket(*pprev, tbl, n)->next) 120b8e1943eSThomas Graf ; 121b8e1943eSThomas Graf 122b8e1943eSThomas Graf return pprev; 123b8e1943eSThomas Graf } 124b8e1943eSThomas Graf 12597defe1eSThomas Graf static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl) 12697defe1eSThomas Graf { 12797defe1eSThomas Graf unsigned int i, size; 12897defe1eSThomas Graf #if defined(CONFIG_PROVE_LOCKING) 12997defe1eSThomas Graf unsigned int nr_pcpus = 2; 13097defe1eSThomas Graf #else 13197defe1eSThomas Graf unsigned int nr_pcpus = num_possible_cpus(); 13297defe1eSThomas Graf #endif 13397defe1eSThomas Graf 13497defe1eSThomas Graf nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL); 13597defe1eSThomas Graf size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul); 13697defe1eSThomas Graf 13797defe1eSThomas Graf /* Never allocate more than one lock per bucket */ 13897defe1eSThomas Graf size = min_t(unsigned int, size, tbl->size); 13997defe1eSThomas Graf 14097defe1eSThomas Graf if (sizeof(spinlock_t) != 0) { 14197defe1eSThomas Graf #ifdef CONFIG_NUMA 14297defe1eSThomas Graf if (size * sizeof(spinlock_t) > PAGE_SIZE) 14397defe1eSThomas Graf tbl->locks = vmalloc(size * sizeof(spinlock_t)); 14497defe1eSThomas Graf else 14597defe1eSThomas Graf #endif 14697defe1eSThomas Graf tbl->locks = kmalloc_array(size, sizeof(spinlock_t), 14797defe1eSThomas Graf GFP_KERNEL); 14897defe1eSThomas Graf if (!tbl->locks) 14997defe1eSThomas Graf return -ENOMEM; 15097defe1eSThomas Graf for (i = 0; i < size; i++) 15197defe1eSThomas Graf spin_lock_init(&tbl->locks[i]); 15297defe1eSThomas Graf } 15397defe1eSThomas Graf tbl->locks_mask = size - 1; 15497defe1eSThomas Graf 15597defe1eSThomas Graf return 0; 15697defe1eSThomas Graf } 15797defe1eSThomas Graf 15897defe1eSThomas Graf static void bucket_table_free(const struct bucket_table *tbl) 15997defe1eSThomas Graf { 16097defe1eSThomas Graf if (tbl) 16197defe1eSThomas Graf kvfree(tbl->locks); 16297defe1eSThomas Graf 16397defe1eSThomas Graf kvfree(tbl); 16497defe1eSThomas Graf } 16597defe1eSThomas Graf 16697defe1eSThomas Graf static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, 16797defe1eSThomas Graf size_t nbuckets) 1687e1e7763SThomas Graf { 1697e1e7763SThomas Graf struct bucket_table *tbl; 1707e1e7763SThomas Graf size_t size; 171f89bd6f8SThomas Graf int i; 1727e1e7763SThomas Graf 1737e1e7763SThomas Graf size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); 1746eba8224SThomas Graf tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); 1757e1e7763SThomas Graf if (tbl == NULL) 1767e1e7763SThomas Graf tbl = vzalloc(size); 1777e1e7763SThomas Graf 1787e1e7763SThomas Graf if (tbl == NULL) 1797e1e7763SThomas Graf return NULL; 1807e1e7763SThomas Graf 1817e1e7763SThomas Graf tbl->size = nbuckets; 1827e1e7763SThomas Graf 18397defe1eSThomas Graf if (alloc_bucket_locks(ht, tbl) < 0) { 18497defe1eSThomas Graf bucket_table_free(tbl); 18597defe1eSThomas Graf return NULL; 1867e1e7763SThomas Graf } 1877e1e7763SThomas Graf 188f89bd6f8SThomas Graf for (i = 0; i < nbuckets; i++) 189f89bd6f8SThomas Graf INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); 190f89bd6f8SThomas Graf 19197defe1eSThomas Graf return tbl; 1927e1e7763SThomas Graf } 1937e1e7763SThomas Graf 1947e1e7763SThomas Graf /** 1957e1e7763SThomas Graf * rht_grow_above_75 - returns true if nelems > 0.75 * table-size 1967e1e7763SThomas Graf * @ht: hash table 1977e1e7763SThomas Graf * @new_size: new table size 1987e1e7763SThomas Graf */ 1997e1e7763SThomas Graf bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size) 2007e1e7763SThomas Graf { 2017e1e7763SThomas Graf /* Expand table when exceeding 75% load */ 202c0c09bfdSYing Xue return atomic_read(&ht->nelems) > (new_size / 4 * 3) && 203c0c09bfdSYing Xue (ht->p.max_shift && atomic_read(&ht->shift) < ht->p.max_shift); 2047e1e7763SThomas Graf } 2057e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rht_grow_above_75); 2067e1e7763SThomas Graf 2077e1e7763SThomas Graf /** 2087e1e7763SThomas Graf * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size 2097e1e7763SThomas Graf * @ht: hash table 2107e1e7763SThomas Graf * @new_size: new table size 2117e1e7763SThomas Graf */ 2127e1e7763SThomas Graf bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size) 2137e1e7763SThomas Graf { 2147e1e7763SThomas Graf /* Shrink table beneath 30% load */ 215c0c09bfdSYing Xue return atomic_read(&ht->nelems) < (new_size * 3 / 10) && 216c0c09bfdSYing Xue (atomic_read(&ht->shift) > ht->p.min_shift); 2177e1e7763SThomas Graf } 2187e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rht_shrink_below_30); 2197e1e7763SThomas Graf 2207e1e7763SThomas Graf static void hashtable_chain_unzip(const struct rhashtable *ht, 2217e1e7763SThomas Graf const struct bucket_table *new_tbl, 22297defe1eSThomas Graf struct bucket_table *old_tbl, 22397defe1eSThomas Graf size_t old_hash) 2247e1e7763SThomas Graf { 2257e1e7763SThomas Graf struct rhash_head *he, *p, *next; 22697defe1eSThomas Graf spinlock_t *new_bucket_lock, *new_bucket_lock2 = NULL; 22797defe1eSThomas Graf unsigned int new_hash, new_hash2; 22897defe1eSThomas Graf 22997defe1eSThomas Graf ASSERT_BUCKET_LOCK(old_tbl, old_hash); 2307e1e7763SThomas Graf 2317e1e7763SThomas Graf /* Old bucket empty, no work needed. */ 23297defe1eSThomas Graf p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl, 23397defe1eSThomas Graf old_hash); 234f89bd6f8SThomas Graf if (rht_is_a_nulls(p)) 2357e1e7763SThomas Graf return; 2367e1e7763SThomas Graf 23797defe1eSThomas Graf new_hash = new_hash2 = head_hashfn(ht, new_tbl, p); 23897defe1eSThomas Graf new_bucket_lock = bucket_lock(new_tbl, new_hash); 23997defe1eSThomas Graf 2407e1e7763SThomas Graf /* Advance the old bucket pointer one or more times until it 2417e1e7763SThomas Graf * reaches a node that doesn't hash to the same bucket as the 2427e1e7763SThomas Graf * previous node p. Call the previous node p; 2437e1e7763SThomas Graf */ 24497defe1eSThomas Graf rht_for_each_continue(he, p->next, old_tbl, old_hash) { 24597defe1eSThomas Graf new_hash2 = head_hashfn(ht, new_tbl, he); 24697defe1eSThomas Graf if (new_hash != new_hash2) 2477e1e7763SThomas Graf break; 2487e1e7763SThomas Graf p = he; 2497e1e7763SThomas Graf } 25097defe1eSThomas Graf rcu_assign_pointer(old_tbl->buckets[old_hash], p->next); 25197defe1eSThomas Graf 25297defe1eSThomas Graf spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); 25397defe1eSThomas Graf 25497defe1eSThomas Graf /* If we have encountered an entry that maps to a different bucket in 25597defe1eSThomas Graf * the new table, lock down that bucket as well as we might cut off 25697defe1eSThomas Graf * the end of the chain. 25797defe1eSThomas Graf */ 25897defe1eSThomas Graf new_bucket_lock2 = bucket_lock(new_tbl, new_hash); 25997defe1eSThomas Graf if (new_bucket_lock != new_bucket_lock2) 26097defe1eSThomas Graf spin_lock_bh_nested(new_bucket_lock2, RHT_LOCK_NESTED2); 2617e1e7763SThomas Graf 2627e1e7763SThomas Graf /* Find the subsequent node which does hash to the same 2637e1e7763SThomas Graf * bucket as node P, or NULL if no such node exists. 2647e1e7763SThomas Graf */ 265f89bd6f8SThomas Graf INIT_RHT_NULLS_HEAD(next, ht, old_hash); 266f89bd6f8SThomas Graf if (!rht_is_a_nulls(he)) { 26797defe1eSThomas Graf rht_for_each_continue(he, he->next, old_tbl, old_hash) { 26897defe1eSThomas Graf if (head_hashfn(ht, new_tbl, he) == new_hash) { 2697e1e7763SThomas Graf next = he; 2707e1e7763SThomas Graf break; 2717e1e7763SThomas Graf } 2727e1e7763SThomas Graf } 2737e1e7763SThomas Graf } 2747e1e7763SThomas Graf 2757e1e7763SThomas Graf /* Set p's next pointer to that subsequent node pointer, 2767e1e7763SThomas Graf * bypassing the nodes which do not hash to p's bucket 2777e1e7763SThomas Graf */ 27897defe1eSThomas Graf rcu_assign_pointer(p->next, next); 27997defe1eSThomas Graf 28097defe1eSThomas Graf if (new_bucket_lock != new_bucket_lock2) 28197defe1eSThomas Graf spin_unlock_bh(new_bucket_lock2); 28297defe1eSThomas Graf spin_unlock_bh(new_bucket_lock); 28397defe1eSThomas Graf } 28497defe1eSThomas Graf 28597defe1eSThomas Graf static void link_old_to_new(struct bucket_table *new_tbl, 28697defe1eSThomas Graf unsigned int new_hash, struct rhash_head *entry) 28797defe1eSThomas Graf { 28897defe1eSThomas Graf spinlock_t *new_bucket_lock; 28997defe1eSThomas Graf 29097defe1eSThomas Graf new_bucket_lock = bucket_lock(new_tbl, new_hash); 29197defe1eSThomas Graf 29297defe1eSThomas Graf spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); 29397defe1eSThomas Graf rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry); 29497defe1eSThomas Graf spin_unlock_bh(new_bucket_lock); 2957e1e7763SThomas Graf } 2967e1e7763SThomas Graf 2977e1e7763SThomas Graf /** 2987e1e7763SThomas Graf * rhashtable_expand - Expand hash table while allowing concurrent lookups 2997e1e7763SThomas Graf * @ht: the hash table to expand 3007e1e7763SThomas Graf * 3017e1e7763SThomas Graf * A secondary bucket array is allocated and the hash entries are migrated 3027e1e7763SThomas Graf * while keeping them on both lists until the end of the RCU grace period. 3037e1e7763SThomas Graf * 3047e1e7763SThomas Graf * This function may only be called in a context where it is safe to call 3057e1e7763SThomas Graf * synchronize_rcu(), e.g. not within a rcu_read_lock() section. 3067e1e7763SThomas Graf * 30797defe1eSThomas Graf * The caller must ensure that no concurrent resizing occurs by holding 30897defe1eSThomas Graf * ht->mutex. 30997defe1eSThomas Graf * 31097defe1eSThomas Graf * It is valid to have concurrent insertions and deletions protected by per 31197defe1eSThomas Graf * bucket locks or concurrent RCU protected lookups and traversals. 3127e1e7763SThomas Graf */ 3136eba8224SThomas Graf int rhashtable_expand(struct rhashtable *ht) 3147e1e7763SThomas Graf { 3157e1e7763SThomas Graf struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); 3167e1e7763SThomas Graf struct rhash_head *he; 31797defe1eSThomas Graf spinlock_t *old_bucket_lock; 31897defe1eSThomas Graf unsigned int new_hash, old_hash; 31997defe1eSThomas Graf bool complete = false; 3207e1e7763SThomas Graf 3217e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 3227e1e7763SThomas Graf 32397defe1eSThomas Graf new_tbl = bucket_table_alloc(ht, old_tbl->size * 2); 3247e1e7763SThomas Graf if (new_tbl == NULL) 3257e1e7763SThomas Graf return -ENOMEM; 3267e1e7763SThomas Graf 327c0c09bfdSYing Xue atomic_inc(&ht->shift); 3287e1e7763SThomas Graf 32997defe1eSThomas Graf /* Make insertions go into the new, empty table right away. Deletions 33097defe1eSThomas Graf * and lookups will be attempted in both tables until we synchronize. 33197defe1eSThomas Graf * The synchronize_rcu() guarantees for the new table to be picked up 33297defe1eSThomas Graf * so no new additions go into the old table while we relink. 3337e1e7763SThomas Graf */ 33497defe1eSThomas Graf rcu_assign_pointer(ht->future_tbl, new_tbl); 33597defe1eSThomas Graf synchronize_rcu(); 33697defe1eSThomas Graf 33797defe1eSThomas Graf /* For each new bucket, search the corresponding old bucket for the 33897defe1eSThomas Graf * first entry that hashes to the new bucket, and link the end of 33997defe1eSThomas Graf * newly formed bucket chain (containing entries added to future 34097defe1eSThomas Graf * table) to that entry. Since all the entries which will end up in 34197defe1eSThomas Graf * the new bucket appear in the same old bucket, this constructs an 34297defe1eSThomas Graf * entirely valid new hash table, but with multiple buckets 34397defe1eSThomas Graf * "zipped" together into a single imprecise chain. 34497defe1eSThomas Graf */ 34597defe1eSThomas Graf for (new_hash = 0; new_hash < new_tbl->size; new_hash++) { 34697defe1eSThomas Graf old_hash = rht_bucket_index(old_tbl, new_hash); 34797defe1eSThomas Graf old_bucket_lock = bucket_lock(old_tbl, old_hash); 34897defe1eSThomas Graf 34997defe1eSThomas Graf spin_lock_bh(old_bucket_lock); 35097defe1eSThomas Graf rht_for_each(he, old_tbl, old_hash) { 35197defe1eSThomas Graf if (head_hashfn(ht, new_tbl, he) == new_hash) { 35297defe1eSThomas Graf link_old_to_new(new_tbl, new_hash, he); 3537e1e7763SThomas Graf break; 3547e1e7763SThomas Graf } 3557e1e7763SThomas Graf } 35697defe1eSThomas Graf spin_unlock_bh(old_bucket_lock); 3577e1e7763SThomas Graf } 3587e1e7763SThomas Graf 3597e1e7763SThomas Graf /* Publish the new table pointer. Lookups may now traverse 3600c828f2fSHerbert Xu * the new table, but they will not benefit from any 3610c828f2fSHerbert Xu * additional efficiency until later steps unzip the buckets. 3627e1e7763SThomas Graf */ 3637e1e7763SThomas Graf rcu_assign_pointer(ht->tbl, new_tbl); 3647e1e7763SThomas Graf 3657e1e7763SThomas Graf /* Unzip interleaved hash chains */ 36697defe1eSThomas Graf while (!complete && !ht->being_destroyed) { 3677e1e7763SThomas Graf /* Wait for readers. All new readers will see the new 3687e1e7763SThomas Graf * table, and thus no references to the old table will 3697e1e7763SThomas Graf * remain. 3707e1e7763SThomas Graf */ 3717e1e7763SThomas Graf synchronize_rcu(); 3727e1e7763SThomas Graf 3737e1e7763SThomas Graf /* For each bucket in the old table (each of which 3747e1e7763SThomas Graf * contains items from multiple buckets of the new 3757e1e7763SThomas Graf * table): ... 3767e1e7763SThomas Graf */ 3777e1e7763SThomas Graf complete = true; 37897defe1eSThomas Graf for (old_hash = 0; old_hash < old_tbl->size; old_hash++) { 379f89bd6f8SThomas Graf struct rhash_head *head; 380f89bd6f8SThomas Graf 38197defe1eSThomas Graf old_bucket_lock = bucket_lock(old_tbl, old_hash); 38297defe1eSThomas Graf spin_lock_bh(old_bucket_lock); 38397defe1eSThomas Graf 38497defe1eSThomas Graf hashtable_chain_unzip(ht, new_tbl, old_tbl, old_hash); 385f89bd6f8SThomas Graf head = rht_dereference_bucket(old_tbl->buckets[old_hash], 386f89bd6f8SThomas Graf old_tbl, old_hash); 387f89bd6f8SThomas Graf if (!rht_is_a_nulls(head)) 3887e1e7763SThomas Graf complete = false; 38997defe1eSThomas Graf 39097defe1eSThomas Graf spin_unlock_bh(old_bucket_lock); 3917e1e7763SThomas Graf } 39297defe1eSThomas Graf } 3937e1e7763SThomas Graf 3947e1e7763SThomas Graf bucket_table_free(old_tbl); 3957e1e7763SThomas Graf return 0; 3967e1e7763SThomas Graf } 3977e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_expand); 3987e1e7763SThomas Graf 3997e1e7763SThomas Graf /** 4007e1e7763SThomas Graf * rhashtable_shrink - Shrink hash table while allowing concurrent lookups 4017e1e7763SThomas Graf * @ht: the hash table to shrink 4027e1e7763SThomas Graf * 4037e1e7763SThomas Graf * This function may only be called in a context where it is safe to call 4047e1e7763SThomas Graf * synchronize_rcu(), e.g. not within a rcu_read_lock() section. 4057e1e7763SThomas Graf * 40697defe1eSThomas Graf * The caller must ensure that no concurrent resizing occurs by holding 40797defe1eSThomas Graf * ht->mutex. 40897defe1eSThomas Graf * 4097e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations take place. 4107e1e7763SThomas Graf * It is however valid to have concurrent lookups if they are RCU protected. 41197defe1eSThomas Graf * 41297defe1eSThomas Graf * It is valid to have concurrent insertions and deletions protected by per 41397defe1eSThomas Graf * bucket locks or concurrent RCU protected lookups and traversals. 4147e1e7763SThomas Graf */ 4156eba8224SThomas Graf int rhashtable_shrink(struct rhashtable *ht) 4167e1e7763SThomas Graf { 41797defe1eSThomas Graf struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht); 41897defe1eSThomas Graf spinlock_t *new_bucket_lock, *old_bucket_lock1, *old_bucket_lock2; 41997defe1eSThomas Graf unsigned int new_hash; 4207e1e7763SThomas Graf 4217e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 4227e1e7763SThomas Graf 42397defe1eSThomas Graf new_tbl = bucket_table_alloc(ht, tbl->size / 2); 42497defe1eSThomas Graf if (new_tbl == NULL) 4257e1e7763SThomas Graf return -ENOMEM; 4267e1e7763SThomas Graf 42797defe1eSThomas Graf rcu_assign_pointer(ht->future_tbl, new_tbl); 42897defe1eSThomas Graf synchronize_rcu(); 4297e1e7763SThomas Graf 43097defe1eSThomas Graf /* Link the first entry in the old bucket to the end of the 43197defe1eSThomas Graf * bucket in the new table. As entries are concurrently being 43297defe1eSThomas Graf * added to the new table, lock down the new bucket. As we 43397defe1eSThomas Graf * always divide the size in half when shrinking, each bucket 43497defe1eSThomas Graf * in the new table maps to exactly two buckets in the old 43597defe1eSThomas Graf * table. 43697defe1eSThomas Graf * 43797defe1eSThomas Graf * As removals can occur concurrently on the old table, we need 43897defe1eSThomas Graf * to lock down both matching buckets in the old table. 4397e1e7763SThomas Graf */ 44097defe1eSThomas Graf for (new_hash = 0; new_hash < new_tbl->size; new_hash++) { 44197defe1eSThomas Graf old_bucket_lock1 = bucket_lock(tbl, new_hash); 44297defe1eSThomas Graf old_bucket_lock2 = bucket_lock(tbl, new_hash + new_tbl->size); 44397defe1eSThomas Graf new_bucket_lock = bucket_lock(new_tbl, new_hash); 4447e1e7763SThomas Graf 44597defe1eSThomas Graf spin_lock_bh(old_bucket_lock1); 446*80ca8c3aSThomas Graf 447*80ca8c3aSThomas Graf /* Depending on the lock per buckets mapping, the bucket in 448*80ca8c3aSThomas Graf * the lower and upper region may map to the same lock. 449*80ca8c3aSThomas Graf */ 450*80ca8c3aSThomas Graf if (old_bucket_lock1 != old_bucket_lock2) { 45197defe1eSThomas Graf spin_lock_bh_nested(old_bucket_lock2, RHT_LOCK_NESTED); 45297defe1eSThomas Graf spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED2); 453*80ca8c3aSThomas Graf } else { 454*80ca8c3aSThomas Graf spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); 455*80ca8c3aSThomas Graf } 45697defe1eSThomas Graf 45797defe1eSThomas Graf rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), 45897defe1eSThomas Graf tbl->buckets[new_hash]); 45997defe1eSThomas Graf rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), 46097defe1eSThomas Graf tbl->buckets[new_hash + new_tbl->size]); 46197defe1eSThomas Graf 46297defe1eSThomas Graf spin_unlock_bh(new_bucket_lock); 463*80ca8c3aSThomas Graf if (old_bucket_lock1 != old_bucket_lock2) 46497defe1eSThomas Graf spin_unlock_bh(old_bucket_lock2); 46597defe1eSThomas Graf spin_unlock_bh(old_bucket_lock1); 4667e1e7763SThomas Graf } 4677e1e7763SThomas Graf 4687e1e7763SThomas Graf /* Publish the new, valid hash table */ 46997defe1eSThomas Graf rcu_assign_pointer(ht->tbl, new_tbl); 470c0c09bfdSYing Xue atomic_dec(&ht->shift); 4717e1e7763SThomas Graf 4727e1e7763SThomas Graf /* Wait for readers. No new readers will have references to the 4737e1e7763SThomas Graf * old hash table. 4747e1e7763SThomas Graf */ 4757e1e7763SThomas Graf synchronize_rcu(); 4767e1e7763SThomas Graf 4777e1e7763SThomas Graf bucket_table_free(tbl); 4787e1e7763SThomas Graf 4797e1e7763SThomas Graf return 0; 4807e1e7763SThomas Graf } 4817e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_shrink); 4827e1e7763SThomas Graf 48397defe1eSThomas Graf static void rht_deferred_worker(struct work_struct *work) 48497defe1eSThomas Graf { 48597defe1eSThomas Graf struct rhashtable *ht; 48697defe1eSThomas Graf struct bucket_table *tbl; 48797defe1eSThomas Graf 48897defe1eSThomas Graf ht = container_of(work, struct rhashtable, run_work.work); 48997defe1eSThomas Graf mutex_lock(&ht->mutex); 49097defe1eSThomas Graf tbl = rht_dereference(ht->tbl, ht); 49197defe1eSThomas Graf 49297defe1eSThomas Graf if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size)) 49397defe1eSThomas Graf rhashtable_expand(ht); 49497defe1eSThomas Graf else if (ht->p.shrink_decision && ht->p.shrink_decision(ht, tbl->size)) 49597defe1eSThomas Graf rhashtable_shrink(ht); 49697defe1eSThomas Graf 49797defe1eSThomas Graf mutex_unlock(&ht->mutex); 49897defe1eSThomas Graf } 49997defe1eSThomas Graf 50054c5b7d3SYing Xue static void rhashtable_wakeup_worker(struct rhashtable *ht) 50154c5b7d3SYing Xue { 50254c5b7d3SYing Xue struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); 50354c5b7d3SYing Xue struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht); 50454c5b7d3SYing Xue size_t size = tbl->size; 50554c5b7d3SYing Xue 50654c5b7d3SYing Xue /* Only adjust the table if no resizing is currently in progress. */ 50754c5b7d3SYing Xue if (tbl == new_tbl && 50854c5b7d3SYing Xue ((ht->p.grow_decision && ht->p.grow_decision(ht, size)) || 50954c5b7d3SYing Xue (ht->p.shrink_decision && ht->p.shrink_decision(ht, size)))) 51054c5b7d3SYing Xue schedule_delayed_work(&ht->run_work, 0); 51154c5b7d3SYing Xue } 51254c5b7d3SYing Xue 513db304854SYing Xue static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj, 514db304854SYing Xue struct bucket_table *tbl, u32 hash) 515db304854SYing Xue { 516db304854SYing Xue struct rhash_head *head = rht_dereference_bucket(tbl->buckets[hash], 517db304854SYing Xue tbl, hash); 518db304854SYing Xue 519db304854SYing Xue if (rht_is_a_nulls(head)) 520db304854SYing Xue INIT_RHT_NULLS_HEAD(obj->next, ht, hash); 521db304854SYing Xue else 522db304854SYing Xue RCU_INIT_POINTER(obj->next, head); 523db304854SYing Xue 524db304854SYing Xue rcu_assign_pointer(tbl->buckets[hash], obj); 525db304854SYing Xue 526db304854SYing Xue atomic_inc(&ht->nelems); 527db304854SYing Xue 528db304854SYing Xue rhashtable_wakeup_worker(ht); 529db304854SYing Xue } 530db304854SYing Xue 5317e1e7763SThomas Graf /** 532db304854SYing Xue * rhashtable_insert - insert object into hash table 5337e1e7763SThomas Graf * @ht: hash table 5347e1e7763SThomas Graf * @obj: pointer to hash head inside object 5357e1e7763SThomas Graf * 53697defe1eSThomas Graf * Will take a per bucket spinlock to protect against mutual mutations 53797defe1eSThomas Graf * on the same bucket. Multiple insertions may occur in parallel unless 53897defe1eSThomas Graf * they map to the same bucket lock. 5397e1e7763SThomas Graf * 54097defe1eSThomas Graf * It is safe to call this function from atomic context. 54197defe1eSThomas Graf * 54297defe1eSThomas Graf * Will trigger an automatic deferred table resizing if the size grows 54397defe1eSThomas Graf * beyond the watermark indicated by grow_decision() which can be passed 54497defe1eSThomas Graf * to rhashtable_init(). 5457e1e7763SThomas Graf */ 5466eba8224SThomas Graf void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj) 5477e1e7763SThomas Graf { 54897defe1eSThomas Graf struct bucket_table *tbl; 54997defe1eSThomas Graf spinlock_t *lock; 55097defe1eSThomas Graf unsigned hash; 5517e1e7763SThomas Graf 55297defe1eSThomas Graf rcu_read_lock(); 5537e1e7763SThomas Graf 55497defe1eSThomas Graf tbl = rht_dereference_rcu(ht->future_tbl, ht); 5558d24c0b4SThomas Graf hash = head_hashfn(ht, tbl, obj); 55697defe1eSThomas Graf lock = bucket_lock(tbl, hash); 55797defe1eSThomas Graf 55897defe1eSThomas Graf spin_lock_bh(lock); 559db304854SYing Xue __rhashtable_insert(ht, obj, tbl, hash); 56097defe1eSThomas Graf spin_unlock_bh(lock); 5617e1e7763SThomas Graf 56297defe1eSThomas Graf rcu_read_unlock(); 5637e1e7763SThomas Graf } 5647e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_insert); 5657e1e7763SThomas Graf 5667e1e7763SThomas Graf /** 5677e1e7763SThomas Graf * rhashtable_remove - remove object from hash table 5687e1e7763SThomas Graf * @ht: hash table 5697e1e7763SThomas Graf * @obj: pointer to hash head inside object 5707e1e7763SThomas Graf * 5717e1e7763SThomas Graf * Since the hash chain is single linked, the removal operation needs to 5727e1e7763SThomas Graf * walk the bucket chain upon removal. The removal operation is thus 5737e1e7763SThomas Graf * considerable slow if the hash table is not correctly sized. 5747e1e7763SThomas Graf * 575db304854SYing Xue * Will automatically shrink the table via rhashtable_expand() if the 5767e1e7763SThomas Graf * shrink_decision function specified at rhashtable_init() returns true. 5777e1e7763SThomas Graf * 5787e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations occur. It is 5797e1e7763SThomas Graf * however valid to have concurrent lookups if they are RCU protected. 5807e1e7763SThomas Graf */ 5816eba8224SThomas Graf bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) 5827e1e7763SThomas Graf { 58397defe1eSThomas Graf struct bucket_table *tbl; 5847e1e7763SThomas Graf struct rhash_head __rcu **pprev; 5857e1e7763SThomas Graf struct rhash_head *he; 58697defe1eSThomas Graf spinlock_t *lock; 58797defe1eSThomas Graf unsigned int hash; 5887e1e7763SThomas Graf 58997defe1eSThomas Graf rcu_read_lock(); 59097defe1eSThomas Graf tbl = rht_dereference_rcu(ht->tbl, ht); 59197defe1eSThomas Graf hash = head_hashfn(ht, tbl, obj); 5927e1e7763SThomas Graf 59397defe1eSThomas Graf lock = bucket_lock(tbl, hash); 59497defe1eSThomas Graf spin_lock_bh(lock); 5957e1e7763SThomas Graf 59697defe1eSThomas Graf restart: 59797defe1eSThomas Graf pprev = &tbl->buckets[hash]; 59897defe1eSThomas Graf rht_for_each(he, tbl, hash) { 5997e1e7763SThomas Graf if (he != obj) { 6007e1e7763SThomas Graf pprev = &he->next; 6017e1e7763SThomas Graf continue; 6027e1e7763SThomas Graf } 6037e1e7763SThomas Graf 60497defe1eSThomas Graf rcu_assign_pointer(*pprev, obj->next); 60597defe1eSThomas Graf atomic_dec(&ht->nelems); 606897362e4SThomas Graf 60797defe1eSThomas Graf spin_unlock_bh(lock); 60897defe1eSThomas Graf 60954c5b7d3SYing Xue rhashtable_wakeup_worker(ht); 61097defe1eSThomas Graf 61197defe1eSThomas Graf rcu_read_unlock(); 612897362e4SThomas Graf 6137e1e7763SThomas Graf return true; 6147e1e7763SThomas Graf } 6157e1e7763SThomas Graf 616bd6d4db5SYing Xue if (tbl != rht_dereference_rcu(ht->future_tbl, ht)) { 61797defe1eSThomas Graf spin_unlock_bh(lock); 61897defe1eSThomas Graf 619bd6d4db5SYing Xue tbl = rht_dereference_rcu(ht->future_tbl, ht); 62097defe1eSThomas Graf hash = head_hashfn(ht, tbl, obj); 62197defe1eSThomas Graf 62297defe1eSThomas Graf lock = bucket_lock(tbl, hash); 62397defe1eSThomas Graf spin_lock_bh(lock); 62497defe1eSThomas Graf goto restart; 62597defe1eSThomas Graf } 62697defe1eSThomas Graf 62797defe1eSThomas Graf spin_unlock_bh(lock); 62897defe1eSThomas Graf rcu_read_unlock(); 62997defe1eSThomas Graf 6307e1e7763SThomas Graf return false; 6317e1e7763SThomas Graf } 6327e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_remove); 6337e1e7763SThomas Graf 634efb975a6SYing Xue struct rhashtable_compare_arg { 635efb975a6SYing Xue struct rhashtable *ht; 636efb975a6SYing Xue const void *key; 637efb975a6SYing Xue }; 638efb975a6SYing Xue 639efb975a6SYing Xue static bool rhashtable_compare(void *ptr, void *arg) 640efb975a6SYing Xue { 641efb975a6SYing Xue struct rhashtable_compare_arg *x = arg; 642efb975a6SYing Xue struct rhashtable *ht = x->ht; 643efb975a6SYing Xue 644efb975a6SYing Xue return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len); 645efb975a6SYing Xue } 646efb975a6SYing Xue 6477e1e7763SThomas Graf /** 6487e1e7763SThomas Graf * rhashtable_lookup - lookup key in hash table 6497e1e7763SThomas Graf * @ht: hash table 6507e1e7763SThomas Graf * @key: pointer to key 6517e1e7763SThomas Graf * 6527e1e7763SThomas Graf * Computes the hash value for the key and traverses the bucket chain looking 6537e1e7763SThomas Graf * for a entry with an identical key. The first matching entry is returned. 6547e1e7763SThomas Graf * 6557e1e7763SThomas Graf * This lookup function may only be used for fixed key hash table (key_len 656db304854SYing Xue * parameter set). It will BUG() if used inappropriately. 6577e1e7763SThomas Graf * 65897defe1eSThomas Graf * Lookups may occur in parallel with hashtable mutations and resizing. 6597e1e7763SThomas Graf */ 66097defe1eSThomas Graf void *rhashtable_lookup(struct rhashtable *ht, const void *key) 6617e1e7763SThomas Graf { 662efb975a6SYing Xue struct rhashtable_compare_arg arg = { 663efb975a6SYing Xue .ht = ht, 664efb975a6SYing Xue .key = key, 665efb975a6SYing Xue }; 6667e1e7763SThomas Graf 6677e1e7763SThomas Graf BUG_ON(!ht->p.key_len); 6687e1e7763SThomas Graf 669efb975a6SYing Xue return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg); 6707e1e7763SThomas Graf } 6717e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup); 6727e1e7763SThomas Graf 6737e1e7763SThomas Graf /** 6747e1e7763SThomas Graf * rhashtable_lookup_compare - search hash table with compare function 6757e1e7763SThomas Graf * @ht: hash table 6768d24c0b4SThomas Graf * @key: the pointer to the key 6777e1e7763SThomas Graf * @compare: compare function, must return true on match 6787e1e7763SThomas Graf * @arg: argument passed on to compare function 6797e1e7763SThomas Graf * 6807e1e7763SThomas Graf * Traverses the bucket chain behind the provided hash value and calls the 6817e1e7763SThomas Graf * specified compare function for each entry. 6827e1e7763SThomas Graf * 68397defe1eSThomas Graf * Lookups may occur in parallel with hashtable mutations and resizing. 6847e1e7763SThomas Graf * 6857e1e7763SThomas Graf * Returns the first entry on which the compare function returned true. 6867e1e7763SThomas Graf */ 68797defe1eSThomas Graf void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key, 6887e1e7763SThomas Graf bool (*compare)(void *, void *), void *arg) 6897e1e7763SThomas Graf { 69097defe1eSThomas Graf const struct bucket_table *tbl, *old_tbl; 6917e1e7763SThomas Graf struct rhash_head *he; 6928d24c0b4SThomas Graf u32 hash; 6937e1e7763SThomas Graf 69497defe1eSThomas Graf rcu_read_lock(); 69597defe1eSThomas Graf 69697defe1eSThomas Graf old_tbl = rht_dereference_rcu(ht->tbl, ht); 69797defe1eSThomas Graf tbl = rht_dereference_rcu(ht->future_tbl, ht); 6988d24c0b4SThomas Graf hash = key_hashfn(ht, key, ht->p.key_len); 69997defe1eSThomas Graf restart: 70097defe1eSThomas Graf rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) { 7017e1e7763SThomas Graf if (!compare(rht_obj(ht, he), arg)) 7027e1e7763SThomas Graf continue; 70397defe1eSThomas Graf rcu_read_unlock(); 704a4b18cdaSThomas Graf return rht_obj(ht, he); 7057e1e7763SThomas Graf } 7067e1e7763SThomas Graf 70797defe1eSThomas Graf if (unlikely(tbl != old_tbl)) { 70897defe1eSThomas Graf tbl = old_tbl; 70997defe1eSThomas Graf goto restart; 71097defe1eSThomas Graf } 71197defe1eSThomas Graf rcu_read_unlock(); 71297defe1eSThomas Graf 7137e1e7763SThomas Graf return NULL; 7147e1e7763SThomas Graf } 7157e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup_compare); 7167e1e7763SThomas Graf 717db304854SYing Xue /** 718db304854SYing Xue * rhashtable_lookup_insert - lookup and insert object into hash table 719db304854SYing Xue * @ht: hash table 720db304854SYing Xue * @obj: pointer to hash head inside object 721db304854SYing Xue * 722db304854SYing Xue * Locks down the bucket chain in both the old and new table if a resize 723db304854SYing Xue * is in progress to ensure that writers can't remove from the old table 724db304854SYing Xue * and can't insert to the new table during the atomic operation of search 725db304854SYing Xue * and insertion. Searches for duplicates in both the old and new table if 726db304854SYing Xue * a resize is in progress. 727db304854SYing Xue * 728db304854SYing Xue * This lookup function may only be used for fixed key hash table (key_len 729db304854SYing Xue * parameter set). It will BUG() if used inappropriately. 730db304854SYing Xue * 731db304854SYing Xue * It is safe to call this function from atomic context. 732db304854SYing Xue * 733db304854SYing Xue * Will trigger an automatic deferred table resizing if the size grows 734db304854SYing Xue * beyond the watermark indicated by grow_decision() which can be passed 735db304854SYing Xue * to rhashtable_init(). 736db304854SYing Xue */ 737db304854SYing Xue bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj) 738db304854SYing Xue { 7397a868d1eSYing Xue struct rhashtable_compare_arg arg = { 7407a868d1eSYing Xue .ht = ht, 7417a868d1eSYing Xue .key = rht_obj(ht, obj) + ht->p.key_offset, 7427a868d1eSYing Xue }; 7437a868d1eSYing Xue 7447a868d1eSYing Xue BUG_ON(!ht->p.key_len); 7457a868d1eSYing Xue 7467a868d1eSYing Xue return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare, 7477a868d1eSYing Xue &arg); 7487a868d1eSYing Xue } 7497a868d1eSYing Xue EXPORT_SYMBOL_GPL(rhashtable_lookup_insert); 7507a868d1eSYing Xue 7517a868d1eSYing Xue /** 7527a868d1eSYing Xue * rhashtable_lookup_compare_insert - search and insert object to hash table 7537a868d1eSYing Xue * with compare function 7547a868d1eSYing Xue * @ht: hash table 7557a868d1eSYing Xue * @obj: pointer to hash head inside object 7567a868d1eSYing Xue * @compare: compare function, must return true on match 7577a868d1eSYing Xue * @arg: argument passed on to compare function 7587a868d1eSYing Xue * 7597a868d1eSYing Xue * Locks down the bucket chain in both the old and new table if a resize 7607a868d1eSYing Xue * is in progress to ensure that writers can't remove from the old table 7617a868d1eSYing Xue * and can't insert to the new table during the atomic operation of search 7627a868d1eSYing Xue * and insertion. Searches for duplicates in both the old and new table if 7637a868d1eSYing Xue * a resize is in progress. 7647a868d1eSYing Xue * 7657a868d1eSYing Xue * Lookups may occur in parallel with hashtable mutations and resizing. 7667a868d1eSYing Xue * 7677a868d1eSYing Xue * Will trigger an automatic deferred table resizing if the size grows 7687a868d1eSYing Xue * beyond the watermark indicated by grow_decision() which can be passed 7697a868d1eSYing Xue * to rhashtable_init(). 7707a868d1eSYing Xue */ 7717a868d1eSYing Xue bool rhashtable_lookup_compare_insert(struct rhashtable *ht, 7727a868d1eSYing Xue struct rhash_head *obj, 7737a868d1eSYing Xue bool (*compare)(void *, void *), 7747a868d1eSYing Xue void *arg) 7757a868d1eSYing Xue { 776db304854SYing Xue struct bucket_table *new_tbl, *old_tbl; 777db304854SYing Xue spinlock_t *new_bucket_lock, *old_bucket_lock; 778db304854SYing Xue u32 new_hash, old_hash; 779db304854SYing Xue bool success = true; 780db304854SYing Xue 781db304854SYing Xue BUG_ON(!ht->p.key_len); 782db304854SYing Xue 783db304854SYing Xue rcu_read_lock(); 784db304854SYing Xue 785db304854SYing Xue old_tbl = rht_dereference_rcu(ht->tbl, ht); 786db304854SYing Xue old_hash = head_hashfn(ht, old_tbl, obj); 787db304854SYing Xue old_bucket_lock = bucket_lock(old_tbl, old_hash); 788db304854SYing Xue spin_lock_bh(old_bucket_lock); 789db304854SYing Xue 790db304854SYing Xue new_tbl = rht_dereference_rcu(ht->future_tbl, ht); 791db304854SYing Xue new_hash = head_hashfn(ht, new_tbl, obj); 792db304854SYing Xue new_bucket_lock = bucket_lock(new_tbl, new_hash); 793db304854SYing Xue if (unlikely(old_tbl != new_tbl)) 794db304854SYing Xue spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); 795db304854SYing Xue 7967a868d1eSYing Xue if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset, 7977a868d1eSYing Xue compare, arg)) { 798db304854SYing Xue success = false; 799db304854SYing Xue goto exit; 800db304854SYing Xue } 801db304854SYing Xue 802db304854SYing Xue __rhashtable_insert(ht, obj, new_tbl, new_hash); 803db304854SYing Xue 804db304854SYing Xue exit: 805db304854SYing Xue if (unlikely(old_tbl != new_tbl)) 806db304854SYing Xue spin_unlock_bh(new_bucket_lock); 807db304854SYing Xue spin_unlock_bh(old_bucket_lock); 808db304854SYing Xue 809db304854SYing Xue rcu_read_unlock(); 810db304854SYing Xue 811db304854SYing Xue return success; 812db304854SYing Xue } 8137a868d1eSYing Xue EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert); 814db304854SYing Xue 81594000176SYing Xue static size_t rounded_hashtable_size(struct rhashtable_params *params) 8167e1e7763SThomas Graf { 81794000176SYing Xue return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), 81894000176SYing Xue 1UL << params->min_shift); 8197e1e7763SThomas Graf } 8207e1e7763SThomas Graf 8217e1e7763SThomas Graf /** 8227e1e7763SThomas Graf * rhashtable_init - initialize a new hash table 8237e1e7763SThomas Graf * @ht: hash table to be initialized 8247e1e7763SThomas Graf * @params: configuration parameters 8257e1e7763SThomas Graf * 8267e1e7763SThomas Graf * Initializes a new hash table based on the provided configuration 8277e1e7763SThomas Graf * parameters. A table can be configured either with a variable or 8287e1e7763SThomas Graf * fixed length key: 8297e1e7763SThomas Graf * 8307e1e7763SThomas Graf * Configuration Example 1: Fixed length keys 8317e1e7763SThomas Graf * struct test_obj { 8327e1e7763SThomas Graf * int key; 8337e1e7763SThomas Graf * void * my_member; 8347e1e7763SThomas Graf * struct rhash_head node; 8357e1e7763SThomas Graf * }; 8367e1e7763SThomas Graf * 8377e1e7763SThomas Graf * struct rhashtable_params params = { 8387e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 8397e1e7763SThomas Graf * .key_offset = offsetof(struct test_obj, key), 8407e1e7763SThomas Graf * .key_len = sizeof(int), 84187545899SDaniel Borkmann * .hashfn = jhash, 842f89bd6f8SThomas Graf * .nulls_base = (1U << RHT_BASE_SHIFT), 8437e1e7763SThomas Graf * }; 8447e1e7763SThomas Graf * 8457e1e7763SThomas Graf * Configuration Example 2: Variable length keys 8467e1e7763SThomas Graf * struct test_obj { 8477e1e7763SThomas Graf * [...] 8487e1e7763SThomas Graf * struct rhash_head node; 8497e1e7763SThomas Graf * }; 8507e1e7763SThomas Graf * 8517e1e7763SThomas Graf * u32 my_hash_fn(const void *data, u32 seed) 8527e1e7763SThomas Graf * { 8537e1e7763SThomas Graf * struct test_obj *obj = data; 8547e1e7763SThomas Graf * 8557e1e7763SThomas Graf * return [... hash ...]; 8567e1e7763SThomas Graf * } 8577e1e7763SThomas Graf * 8587e1e7763SThomas Graf * struct rhashtable_params params = { 8597e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 86087545899SDaniel Borkmann * .hashfn = jhash, 8617e1e7763SThomas Graf * .obj_hashfn = my_hash_fn, 8627e1e7763SThomas Graf * }; 8637e1e7763SThomas Graf */ 8647e1e7763SThomas Graf int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params) 8657e1e7763SThomas Graf { 8667e1e7763SThomas Graf struct bucket_table *tbl; 8677e1e7763SThomas Graf size_t size; 8687e1e7763SThomas Graf 8697e1e7763SThomas Graf size = HASH_DEFAULT_SIZE; 8707e1e7763SThomas Graf 8717e1e7763SThomas Graf if ((params->key_len && !params->hashfn) || 8727e1e7763SThomas Graf (!params->key_len && !params->obj_hashfn)) 8737e1e7763SThomas Graf return -EINVAL; 8747e1e7763SThomas Graf 875f89bd6f8SThomas Graf if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) 876f89bd6f8SThomas Graf return -EINVAL; 877f89bd6f8SThomas Graf 87894000176SYing Xue params->min_shift = max_t(size_t, params->min_shift, 87994000176SYing Xue ilog2(HASH_MIN_SIZE)); 88094000176SYing Xue 8817e1e7763SThomas Graf if (params->nelem_hint) 88294000176SYing Xue size = rounded_hashtable_size(params); 8837e1e7763SThomas Graf 88497defe1eSThomas Graf memset(ht, 0, sizeof(*ht)); 88597defe1eSThomas Graf mutex_init(&ht->mutex); 88697defe1eSThomas Graf memcpy(&ht->p, params, sizeof(*params)); 88797defe1eSThomas Graf 88897defe1eSThomas Graf if (params->locks_mul) 88997defe1eSThomas Graf ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); 89097defe1eSThomas Graf else 89197defe1eSThomas Graf ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; 89297defe1eSThomas Graf 89397defe1eSThomas Graf tbl = bucket_table_alloc(ht, size); 8947e1e7763SThomas Graf if (tbl == NULL) 8957e1e7763SThomas Graf return -ENOMEM; 8967e1e7763SThomas Graf 897545a148eSYing Xue atomic_set(&ht->nelems, 0); 898c0c09bfdSYing Xue atomic_set(&ht->shift, ilog2(tbl->size)); 8997e1e7763SThomas Graf RCU_INIT_POINTER(ht->tbl, tbl); 90097defe1eSThomas Graf RCU_INIT_POINTER(ht->future_tbl, tbl); 9017e1e7763SThomas Graf 9027e1e7763SThomas Graf if (!ht->p.hash_rnd) 9037e1e7763SThomas Graf get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd)); 9047e1e7763SThomas Graf 90597defe1eSThomas Graf if (ht->p.grow_decision || ht->p.shrink_decision) 90697defe1eSThomas Graf INIT_DEFERRABLE_WORK(&ht->run_work, rht_deferred_worker); 90797defe1eSThomas Graf 9087e1e7763SThomas Graf return 0; 9097e1e7763SThomas Graf } 9107e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init); 9117e1e7763SThomas Graf 9127e1e7763SThomas Graf /** 9137e1e7763SThomas Graf * rhashtable_destroy - destroy hash table 9147e1e7763SThomas Graf * @ht: the hash table to destroy 9157e1e7763SThomas Graf * 916ae82ddcfSPablo Neira Ayuso * Frees the bucket array. This function is not rcu safe, therefore the caller 917ae82ddcfSPablo Neira Ayuso * has to make sure that no resizing may happen by unpublishing the hashtable 918ae82ddcfSPablo Neira Ayuso * and waiting for the quiescent cycle before releasing the bucket array. 9197e1e7763SThomas Graf */ 92097defe1eSThomas Graf void rhashtable_destroy(struct rhashtable *ht) 9217e1e7763SThomas Graf { 92297defe1eSThomas Graf ht->being_destroyed = true; 92397defe1eSThomas Graf 92497defe1eSThomas Graf mutex_lock(&ht->mutex); 92597defe1eSThomas Graf 92697defe1eSThomas Graf cancel_delayed_work(&ht->run_work); 92797defe1eSThomas Graf bucket_table_free(rht_dereference(ht->tbl, ht)); 92897defe1eSThomas Graf 92997defe1eSThomas Graf mutex_unlock(&ht->mutex); 9307e1e7763SThomas Graf } 9317e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy); 9327e1e7763SThomas Graf 9337e1e7763SThomas Graf /************************************************************************** 9347e1e7763SThomas Graf * Self Test 9357e1e7763SThomas Graf **************************************************************************/ 9367e1e7763SThomas Graf 9377e1e7763SThomas Graf #ifdef CONFIG_TEST_RHASHTABLE 9387e1e7763SThomas Graf 9397e1e7763SThomas Graf #define TEST_HT_SIZE 8 9407e1e7763SThomas Graf #define TEST_ENTRIES 2048 9417e1e7763SThomas Graf #define TEST_PTR ((void *) 0xdeadbeef) 9427e1e7763SThomas Graf #define TEST_NEXPANDS 4 9437e1e7763SThomas Graf 9447e1e7763SThomas Graf struct test_obj { 9457e1e7763SThomas Graf void *ptr; 9467e1e7763SThomas Graf int value; 9477e1e7763SThomas Graf struct rhash_head node; 9487e1e7763SThomas Graf }; 9497e1e7763SThomas Graf 9507e1e7763SThomas Graf static int __init test_rht_lookup(struct rhashtable *ht) 9517e1e7763SThomas Graf { 9527e1e7763SThomas Graf unsigned int i; 9537e1e7763SThomas Graf 9547e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES * 2; i++) { 9557e1e7763SThomas Graf struct test_obj *obj; 9567e1e7763SThomas Graf bool expected = !(i % 2); 9577e1e7763SThomas Graf u32 key = i; 9587e1e7763SThomas Graf 9597e1e7763SThomas Graf obj = rhashtable_lookup(ht, &key); 9607e1e7763SThomas Graf 9617e1e7763SThomas Graf if (expected && !obj) { 9627e1e7763SThomas Graf pr_warn("Test failed: Could not find key %u\n", key); 9637e1e7763SThomas Graf return -ENOENT; 9647e1e7763SThomas Graf } else if (!expected && obj) { 9657e1e7763SThomas Graf pr_warn("Test failed: Unexpected entry found for key %u\n", 9667e1e7763SThomas Graf key); 9677e1e7763SThomas Graf return -EEXIST; 9687e1e7763SThomas Graf } else if (expected && obj) { 9697e1e7763SThomas Graf if (obj->ptr != TEST_PTR || obj->value != i) { 9707e1e7763SThomas Graf pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n", 9717e1e7763SThomas Graf obj->ptr, TEST_PTR, obj->value, i); 9727e1e7763SThomas Graf return -EINVAL; 9737e1e7763SThomas Graf } 9747e1e7763SThomas Graf } 9757e1e7763SThomas Graf } 9767e1e7763SThomas Graf 9777e1e7763SThomas Graf return 0; 9787e1e7763SThomas Graf } 9797e1e7763SThomas Graf 9803e7b2ec4SThomas Graf static void test_bucket_stats(struct rhashtable *ht, bool quiet) 9817e1e7763SThomas Graf { 9823e7b2ec4SThomas Graf unsigned int cnt, rcu_cnt, i, total = 0; 98388d6ed15SThomas Graf struct rhash_head *pos; 9847e1e7763SThomas Graf struct test_obj *obj; 9853e7b2ec4SThomas Graf struct bucket_table *tbl; 9867e1e7763SThomas Graf 9873e7b2ec4SThomas Graf tbl = rht_dereference_rcu(ht->tbl, ht); 9887e1e7763SThomas Graf for (i = 0; i < tbl->size; i++) { 9893e7b2ec4SThomas Graf rcu_cnt = cnt = 0; 9907e1e7763SThomas Graf 9917e1e7763SThomas Graf if (!quiet) 9927e1e7763SThomas Graf pr_info(" [%#4x/%zu]", i, tbl->size); 9937e1e7763SThomas Graf 99488d6ed15SThomas Graf rht_for_each_entry_rcu(obj, pos, tbl, i, node) { 9957e1e7763SThomas Graf cnt++; 9967e1e7763SThomas Graf total++; 9977e1e7763SThomas Graf if (!quiet) 9987e1e7763SThomas Graf pr_cont(" [%p],", obj); 9997e1e7763SThomas Graf } 10007e1e7763SThomas Graf 100188d6ed15SThomas Graf rht_for_each_entry_rcu(obj, pos, tbl, i, node) 10023e7b2ec4SThomas Graf rcu_cnt++; 10033e7b2ec4SThomas Graf 10043e7b2ec4SThomas Graf if (rcu_cnt != cnt) 10053e7b2ec4SThomas Graf pr_warn("Test failed: Chain count mismach %d != %d", 10063e7b2ec4SThomas Graf cnt, rcu_cnt); 10073e7b2ec4SThomas Graf 10087e1e7763SThomas Graf if (!quiet) 10097e1e7763SThomas Graf pr_cont("\n [%#x] first element: %p, chain length: %u\n", 10107e1e7763SThomas Graf i, tbl->buckets[i], cnt); 10117e1e7763SThomas Graf } 10127e1e7763SThomas Graf 101397defe1eSThomas Graf pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n", 101497defe1eSThomas Graf total, atomic_read(&ht->nelems), TEST_ENTRIES); 10153e7b2ec4SThomas Graf 101697defe1eSThomas Graf if (total != atomic_read(&ht->nelems) || total != TEST_ENTRIES) 10173e7b2ec4SThomas Graf pr_warn("Test failed: Total count mismatch ^^^"); 10187e1e7763SThomas Graf } 10197e1e7763SThomas Graf 10207e1e7763SThomas Graf static int __init test_rhashtable(struct rhashtable *ht) 10217e1e7763SThomas Graf { 10227e1e7763SThomas Graf struct bucket_table *tbl; 102388d6ed15SThomas Graf struct test_obj *obj; 102488d6ed15SThomas Graf struct rhash_head *pos, *next; 10257e1e7763SThomas Graf int err; 10267e1e7763SThomas Graf unsigned int i; 10277e1e7763SThomas Graf 10287e1e7763SThomas Graf /* 10297e1e7763SThomas Graf * Insertion Test: 10307e1e7763SThomas Graf * Insert TEST_ENTRIES into table with all keys even numbers 10317e1e7763SThomas Graf */ 10327e1e7763SThomas Graf pr_info(" Adding %d keys\n", TEST_ENTRIES); 10337e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES; i++) { 10347e1e7763SThomas Graf struct test_obj *obj; 10357e1e7763SThomas Graf 10367e1e7763SThomas Graf obj = kzalloc(sizeof(*obj), GFP_KERNEL); 10377e1e7763SThomas Graf if (!obj) { 10387e1e7763SThomas Graf err = -ENOMEM; 10397e1e7763SThomas Graf goto error; 10407e1e7763SThomas Graf } 10417e1e7763SThomas Graf 10427e1e7763SThomas Graf obj->ptr = TEST_PTR; 10437e1e7763SThomas Graf obj->value = i * 2; 10447e1e7763SThomas Graf 10456eba8224SThomas Graf rhashtable_insert(ht, &obj->node); 10467e1e7763SThomas Graf } 10477e1e7763SThomas Graf 10487e1e7763SThomas Graf rcu_read_lock(); 10493e7b2ec4SThomas Graf test_bucket_stats(ht, true); 10507e1e7763SThomas Graf test_rht_lookup(ht); 10517e1e7763SThomas Graf rcu_read_unlock(); 10527e1e7763SThomas Graf 10537e1e7763SThomas Graf for (i = 0; i < TEST_NEXPANDS; i++) { 10547e1e7763SThomas Graf pr_info(" Table expansion iteration %u...\n", i); 105597defe1eSThomas Graf mutex_lock(&ht->mutex); 10566eba8224SThomas Graf rhashtable_expand(ht); 105797defe1eSThomas Graf mutex_unlock(&ht->mutex); 10587e1e7763SThomas Graf 10597e1e7763SThomas Graf rcu_read_lock(); 10607e1e7763SThomas Graf pr_info(" Verifying lookups...\n"); 10617e1e7763SThomas Graf test_rht_lookup(ht); 10627e1e7763SThomas Graf rcu_read_unlock(); 10637e1e7763SThomas Graf } 10647e1e7763SThomas Graf 10657e1e7763SThomas Graf for (i = 0; i < TEST_NEXPANDS; i++) { 10667e1e7763SThomas Graf pr_info(" Table shrinkage iteration %u...\n", i); 106797defe1eSThomas Graf mutex_lock(&ht->mutex); 10686eba8224SThomas Graf rhashtable_shrink(ht); 106997defe1eSThomas Graf mutex_unlock(&ht->mutex); 10707e1e7763SThomas Graf 10717e1e7763SThomas Graf rcu_read_lock(); 10727e1e7763SThomas Graf pr_info(" Verifying lookups...\n"); 10737e1e7763SThomas Graf test_rht_lookup(ht); 10747e1e7763SThomas Graf rcu_read_unlock(); 10757e1e7763SThomas Graf } 10767e1e7763SThomas Graf 10773e7b2ec4SThomas Graf rcu_read_lock(); 10783e7b2ec4SThomas Graf test_bucket_stats(ht, true); 10793e7b2ec4SThomas Graf rcu_read_unlock(); 10803e7b2ec4SThomas Graf 10817e1e7763SThomas Graf pr_info(" Deleting %d keys\n", TEST_ENTRIES); 10827e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES; i++) { 10837e1e7763SThomas Graf u32 key = i * 2; 10847e1e7763SThomas Graf 10857e1e7763SThomas Graf obj = rhashtable_lookup(ht, &key); 10867e1e7763SThomas Graf BUG_ON(!obj); 10877e1e7763SThomas Graf 10886eba8224SThomas Graf rhashtable_remove(ht, &obj->node); 10897e1e7763SThomas Graf kfree(obj); 10907e1e7763SThomas Graf } 10917e1e7763SThomas Graf 10927e1e7763SThomas Graf return 0; 10937e1e7763SThomas Graf 10947e1e7763SThomas Graf error: 10957e1e7763SThomas Graf tbl = rht_dereference_rcu(ht->tbl, ht); 10967e1e7763SThomas Graf for (i = 0; i < tbl->size; i++) 109788d6ed15SThomas Graf rht_for_each_entry_safe(obj, pos, next, tbl, i, node) 10987e1e7763SThomas Graf kfree(obj); 10997e1e7763SThomas Graf 11007e1e7763SThomas Graf return err; 11017e1e7763SThomas Graf } 11027e1e7763SThomas Graf 11037e1e7763SThomas Graf static int __init test_rht_init(void) 11047e1e7763SThomas Graf { 11057e1e7763SThomas Graf struct rhashtable ht; 11067e1e7763SThomas Graf struct rhashtable_params params = { 11077e1e7763SThomas Graf .nelem_hint = TEST_HT_SIZE, 11087e1e7763SThomas Graf .head_offset = offsetof(struct test_obj, node), 11097e1e7763SThomas Graf .key_offset = offsetof(struct test_obj, value), 11107e1e7763SThomas Graf .key_len = sizeof(int), 111187545899SDaniel Borkmann .hashfn = jhash, 1112f89bd6f8SThomas Graf .nulls_base = (3U << RHT_BASE_SHIFT), 11137e1e7763SThomas Graf .grow_decision = rht_grow_above_75, 11147e1e7763SThomas Graf .shrink_decision = rht_shrink_below_30, 11157e1e7763SThomas Graf }; 11167e1e7763SThomas Graf int err; 11177e1e7763SThomas Graf 11187e1e7763SThomas Graf pr_info("Running resizable hashtable tests...\n"); 11197e1e7763SThomas Graf 11207e1e7763SThomas Graf err = rhashtable_init(&ht, ¶ms); 11217e1e7763SThomas Graf if (err < 0) { 11227e1e7763SThomas Graf pr_warn("Test failed: Unable to initialize hashtable: %d\n", 11237e1e7763SThomas Graf err); 11247e1e7763SThomas Graf return err; 11257e1e7763SThomas Graf } 11267e1e7763SThomas Graf 11277e1e7763SThomas Graf err = test_rhashtable(&ht); 11287e1e7763SThomas Graf 11297e1e7763SThomas Graf rhashtable_destroy(&ht); 11307e1e7763SThomas Graf 11317e1e7763SThomas Graf return err; 11327e1e7763SThomas Graf } 11337e1e7763SThomas Graf 11347e1e7763SThomas Graf subsys_initcall(test_rht_init); 11357e1e7763SThomas Graf 11367e1e7763SThomas Graf #endif /* CONFIG_TEST_RHASHTABLE */ 1137