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); 44697defe1eSThomas Graf spin_lock_bh_nested(old_bucket_lock2, RHT_LOCK_NESTED); 44797defe1eSThomas Graf spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED2); 44897defe1eSThomas Graf 44997defe1eSThomas Graf rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), 45097defe1eSThomas Graf tbl->buckets[new_hash]); 45197defe1eSThomas Graf rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), 45297defe1eSThomas Graf tbl->buckets[new_hash + new_tbl->size]); 45397defe1eSThomas Graf 45497defe1eSThomas Graf spin_unlock_bh(new_bucket_lock); 45597defe1eSThomas Graf spin_unlock_bh(old_bucket_lock2); 45697defe1eSThomas Graf spin_unlock_bh(old_bucket_lock1); 4577e1e7763SThomas Graf } 4587e1e7763SThomas Graf 4597e1e7763SThomas Graf /* Publish the new, valid hash table */ 46097defe1eSThomas Graf rcu_assign_pointer(ht->tbl, new_tbl); 461c0c09bfdSYing Xue atomic_dec(&ht->shift); 4627e1e7763SThomas Graf 4637e1e7763SThomas Graf /* Wait for readers. No new readers will have references to the 4647e1e7763SThomas Graf * old hash table. 4657e1e7763SThomas Graf */ 4667e1e7763SThomas Graf synchronize_rcu(); 4677e1e7763SThomas Graf 4687e1e7763SThomas Graf bucket_table_free(tbl); 4697e1e7763SThomas Graf 4707e1e7763SThomas Graf return 0; 4717e1e7763SThomas Graf } 4727e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_shrink); 4737e1e7763SThomas Graf 47497defe1eSThomas Graf static void rht_deferred_worker(struct work_struct *work) 47597defe1eSThomas Graf { 47697defe1eSThomas Graf struct rhashtable *ht; 47797defe1eSThomas Graf struct bucket_table *tbl; 47897defe1eSThomas Graf 47997defe1eSThomas Graf ht = container_of(work, struct rhashtable, run_work.work); 48097defe1eSThomas Graf mutex_lock(&ht->mutex); 48197defe1eSThomas Graf tbl = rht_dereference(ht->tbl, ht); 48297defe1eSThomas Graf 48397defe1eSThomas Graf if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size)) 48497defe1eSThomas Graf rhashtable_expand(ht); 48597defe1eSThomas Graf else if (ht->p.shrink_decision && ht->p.shrink_decision(ht, tbl->size)) 48697defe1eSThomas Graf rhashtable_shrink(ht); 48797defe1eSThomas Graf 48897defe1eSThomas Graf mutex_unlock(&ht->mutex); 48997defe1eSThomas Graf } 49097defe1eSThomas Graf 49154c5b7d3SYing Xue static void rhashtable_wakeup_worker(struct rhashtable *ht) 49254c5b7d3SYing Xue { 49354c5b7d3SYing Xue struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); 49454c5b7d3SYing Xue struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht); 49554c5b7d3SYing Xue size_t size = tbl->size; 49654c5b7d3SYing Xue 49754c5b7d3SYing Xue /* Only adjust the table if no resizing is currently in progress. */ 49854c5b7d3SYing Xue if (tbl == new_tbl && 49954c5b7d3SYing Xue ((ht->p.grow_decision && ht->p.grow_decision(ht, size)) || 50054c5b7d3SYing Xue (ht->p.shrink_decision && ht->p.shrink_decision(ht, size)))) 50154c5b7d3SYing Xue schedule_delayed_work(&ht->run_work, 0); 50254c5b7d3SYing Xue } 50354c5b7d3SYing Xue 504db304854SYing Xue static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj, 505db304854SYing Xue struct bucket_table *tbl, u32 hash) 506db304854SYing Xue { 507db304854SYing Xue struct rhash_head *head = rht_dereference_bucket(tbl->buckets[hash], 508db304854SYing Xue tbl, hash); 509db304854SYing Xue 510db304854SYing Xue if (rht_is_a_nulls(head)) 511db304854SYing Xue INIT_RHT_NULLS_HEAD(obj->next, ht, hash); 512db304854SYing Xue else 513db304854SYing Xue RCU_INIT_POINTER(obj->next, head); 514db304854SYing Xue 515db304854SYing Xue rcu_assign_pointer(tbl->buckets[hash], obj); 516db304854SYing Xue 517db304854SYing Xue atomic_inc(&ht->nelems); 518db304854SYing Xue 519db304854SYing Xue rhashtable_wakeup_worker(ht); 520db304854SYing Xue } 521db304854SYing Xue 5227e1e7763SThomas Graf /** 523db304854SYing Xue * rhashtable_insert - insert object into hash table 5247e1e7763SThomas Graf * @ht: hash table 5257e1e7763SThomas Graf * @obj: pointer to hash head inside object 5267e1e7763SThomas Graf * 52797defe1eSThomas Graf * Will take a per bucket spinlock to protect against mutual mutations 52897defe1eSThomas Graf * on the same bucket. Multiple insertions may occur in parallel unless 52997defe1eSThomas Graf * they map to the same bucket lock. 5307e1e7763SThomas Graf * 53197defe1eSThomas Graf * It is safe to call this function from atomic context. 53297defe1eSThomas Graf * 53397defe1eSThomas Graf * Will trigger an automatic deferred table resizing if the size grows 53497defe1eSThomas Graf * beyond the watermark indicated by grow_decision() which can be passed 53597defe1eSThomas Graf * to rhashtable_init(). 5367e1e7763SThomas Graf */ 5376eba8224SThomas Graf void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj) 5387e1e7763SThomas Graf { 53997defe1eSThomas Graf struct bucket_table *tbl; 54097defe1eSThomas Graf spinlock_t *lock; 54197defe1eSThomas Graf unsigned hash; 5427e1e7763SThomas Graf 54397defe1eSThomas Graf rcu_read_lock(); 5447e1e7763SThomas Graf 54597defe1eSThomas Graf tbl = rht_dereference_rcu(ht->future_tbl, ht); 5468d24c0b4SThomas Graf hash = head_hashfn(ht, tbl, obj); 54797defe1eSThomas Graf lock = bucket_lock(tbl, hash); 54897defe1eSThomas Graf 54997defe1eSThomas Graf spin_lock_bh(lock); 550db304854SYing Xue __rhashtable_insert(ht, obj, tbl, hash); 55197defe1eSThomas Graf spin_unlock_bh(lock); 5527e1e7763SThomas Graf 55397defe1eSThomas Graf rcu_read_unlock(); 5547e1e7763SThomas Graf } 5557e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_insert); 5567e1e7763SThomas Graf 5577e1e7763SThomas Graf /** 5587e1e7763SThomas Graf * rhashtable_remove - remove object from hash table 5597e1e7763SThomas Graf * @ht: hash table 5607e1e7763SThomas Graf * @obj: pointer to hash head inside object 5617e1e7763SThomas Graf * 5627e1e7763SThomas Graf * Since the hash chain is single linked, the removal operation needs to 5637e1e7763SThomas Graf * walk the bucket chain upon removal. The removal operation is thus 5647e1e7763SThomas Graf * considerable slow if the hash table is not correctly sized. 5657e1e7763SThomas Graf * 566db304854SYing Xue * Will automatically shrink the table via rhashtable_expand() if the 5677e1e7763SThomas Graf * shrink_decision function specified at rhashtable_init() returns true. 5687e1e7763SThomas Graf * 5697e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations occur. It is 5707e1e7763SThomas Graf * however valid to have concurrent lookups if they are RCU protected. 5717e1e7763SThomas Graf */ 5726eba8224SThomas Graf bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) 5737e1e7763SThomas Graf { 57497defe1eSThomas Graf struct bucket_table *tbl; 5757e1e7763SThomas Graf struct rhash_head __rcu **pprev; 5767e1e7763SThomas Graf struct rhash_head *he; 57797defe1eSThomas Graf spinlock_t *lock; 57897defe1eSThomas Graf unsigned int hash; 5797e1e7763SThomas Graf 58097defe1eSThomas Graf rcu_read_lock(); 58197defe1eSThomas Graf tbl = rht_dereference_rcu(ht->tbl, ht); 58297defe1eSThomas Graf hash = head_hashfn(ht, tbl, obj); 5837e1e7763SThomas Graf 58497defe1eSThomas Graf lock = bucket_lock(tbl, hash); 58597defe1eSThomas Graf spin_lock_bh(lock); 5867e1e7763SThomas Graf 58797defe1eSThomas Graf restart: 58897defe1eSThomas Graf pprev = &tbl->buckets[hash]; 58997defe1eSThomas Graf rht_for_each(he, tbl, hash) { 5907e1e7763SThomas Graf if (he != obj) { 5917e1e7763SThomas Graf pprev = &he->next; 5927e1e7763SThomas Graf continue; 5937e1e7763SThomas Graf } 5947e1e7763SThomas Graf 59597defe1eSThomas Graf rcu_assign_pointer(*pprev, obj->next); 59697defe1eSThomas Graf atomic_dec(&ht->nelems); 597897362e4SThomas Graf 59897defe1eSThomas Graf spin_unlock_bh(lock); 59997defe1eSThomas Graf 60054c5b7d3SYing Xue rhashtable_wakeup_worker(ht); 60197defe1eSThomas Graf 60297defe1eSThomas Graf rcu_read_unlock(); 603897362e4SThomas Graf 6047e1e7763SThomas Graf return true; 6057e1e7763SThomas Graf } 6067e1e7763SThomas Graf 607bd6d4db5SYing Xue if (tbl != rht_dereference_rcu(ht->future_tbl, ht)) { 60897defe1eSThomas Graf spin_unlock_bh(lock); 60997defe1eSThomas Graf 610bd6d4db5SYing Xue tbl = rht_dereference_rcu(ht->future_tbl, ht); 61197defe1eSThomas Graf hash = head_hashfn(ht, tbl, obj); 61297defe1eSThomas Graf 61397defe1eSThomas Graf lock = bucket_lock(tbl, hash); 61497defe1eSThomas Graf spin_lock_bh(lock); 61597defe1eSThomas Graf goto restart; 61697defe1eSThomas Graf } 61797defe1eSThomas Graf 61897defe1eSThomas Graf spin_unlock_bh(lock); 61997defe1eSThomas Graf rcu_read_unlock(); 62097defe1eSThomas Graf 6217e1e7763SThomas Graf return false; 6227e1e7763SThomas Graf } 6237e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_remove); 6247e1e7763SThomas Graf 625efb975a6SYing Xue struct rhashtable_compare_arg { 626efb975a6SYing Xue struct rhashtable *ht; 627efb975a6SYing Xue const void *key; 628efb975a6SYing Xue }; 629efb975a6SYing Xue 630efb975a6SYing Xue static bool rhashtable_compare(void *ptr, void *arg) 631efb975a6SYing Xue { 632efb975a6SYing Xue struct rhashtable_compare_arg *x = arg; 633efb975a6SYing Xue struct rhashtable *ht = x->ht; 634efb975a6SYing Xue 635efb975a6SYing Xue return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len); 636efb975a6SYing Xue } 637efb975a6SYing Xue 6387e1e7763SThomas Graf /** 6397e1e7763SThomas Graf * rhashtable_lookup - lookup key in hash table 6407e1e7763SThomas Graf * @ht: hash table 6417e1e7763SThomas Graf * @key: pointer to key 6427e1e7763SThomas Graf * 6437e1e7763SThomas Graf * Computes the hash value for the key and traverses the bucket chain looking 6447e1e7763SThomas Graf * for a entry with an identical key. The first matching entry is returned. 6457e1e7763SThomas Graf * 6467e1e7763SThomas Graf * This lookup function may only be used for fixed key hash table (key_len 647db304854SYing Xue * parameter set). It will BUG() if used inappropriately. 6487e1e7763SThomas Graf * 64997defe1eSThomas Graf * Lookups may occur in parallel with hashtable mutations and resizing. 6507e1e7763SThomas Graf */ 65197defe1eSThomas Graf void *rhashtable_lookup(struct rhashtable *ht, const void *key) 6527e1e7763SThomas Graf { 653efb975a6SYing Xue struct rhashtable_compare_arg arg = { 654efb975a6SYing Xue .ht = ht, 655efb975a6SYing Xue .key = key, 656efb975a6SYing Xue }; 6577e1e7763SThomas Graf 6587e1e7763SThomas Graf BUG_ON(!ht->p.key_len); 6597e1e7763SThomas Graf 660efb975a6SYing Xue return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg); 6617e1e7763SThomas Graf } 6627e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup); 6637e1e7763SThomas Graf 6647e1e7763SThomas Graf /** 6657e1e7763SThomas Graf * rhashtable_lookup_compare - search hash table with compare function 6667e1e7763SThomas Graf * @ht: hash table 6678d24c0b4SThomas Graf * @key: the pointer to the key 6687e1e7763SThomas Graf * @compare: compare function, must return true on match 6697e1e7763SThomas Graf * @arg: argument passed on to compare function 6707e1e7763SThomas Graf * 6717e1e7763SThomas Graf * Traverses the bucket chain behind the provided hash value and calls the 6727e1e7763SThomas Graf * specified compare function for each entry. 6737e1e7763SThomas Graf * 67497defe1eSThomas Graf * Lookups may occur in parallel with hashtable mutations and resizing. 6757e1e7763SThomas Graf * 6767e1e7763SThomas Graf * Returns the first entry on which the compare function returned true. 6777e1e7763SThomas Graf */ 67897defe1eSThomas Graf void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key, 6797e1e7763SThomas Graf bool (*compare)(void *, void *), void *arg) 6807e1e7763SThomas Graf { 68197defe1eSThomas Graf const struct bucket_table *tbl, *old_tbl; 6827e1e7763SThomas Graf struct rhash_head *he; 6838d24c0b4SThomas Graf u32 hash; 6847e1e7763SThomas Graf 68597defe1eSThomas Graf rcu_read_lock(); 68697defe1eSThomas Graf 68797defe1eSThomas Graf old_tbl = rht_dereference_rcu(ht->tbl, ht); 68897defe1eSThomas Graf tbl = rht_dereference_rcu(ht->future_tbl, ht); 6898d24c0b4SThomas Graf hash = key_hashfn(ht, key, ht->p.key_len); 69097defe1eSThomas Graf restart: 69197defe1eSThomas Graf rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) { 6927e1e7763SThomas Graf if (!compare(rht_obj(ht, he), arg)) 6937e1e7763SThomas Graf continue; 69497defe1eSThomas Graf rcu_read_unlock(); 695a4b18cdaSThomas Graf return rht_obj(ht, he); 6967e1e7763SThomas Graf } 6977e1e7763SThomas Graf 69897defe1eSThomas Graf if (unlikely(tbl != old_tbl)) { 69997defe1eSThomas Graf tbl = old_tbl; 70097defe1eSThomas Graf goto restart; 70197defe1eSThomas Graf } 70297defe1eSThomas Graf rcu_read_unlock(); 70397defe1eSThomas Graf 7047e1e7763SThomas Graf return NULL; 7057e1e7763SThomas Graf } 7067e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup_compare); 7077e1e7763SThomas Graf 708db304854SYing Xue /** 709db304854SYing Xue * rhashtable_lookup_insert - lookup and insert object into hash table 710db304854SYing Xue * @ht: hash table 711db304854SYing Xue * @obj: pointer to hash head inside object 712db304854SYing Xue * 713db304854SYing Xue * Locks down the bucket chain in both the old and new table if a resize 714db304854SYing Xue * is in progress to ensure that writers can't remove from the old table 715db304854SYing Xue * and can't insert to the new table during the atomic operation of search 716db304854SYing Xue * and insertion. Searches for duplicates in both the old and new table if 717db304854SYing Xue * a resize is in progress. 718db304854SYing Xue * 719db304854SYing Xue * This lookup function may only be used for fixed key hash table (key_len 720db304854SYing Xue * parameter set). It will BUG() if used inappropriately. 721db304854SYing Xue * 722db304854SYing Xue * It is safe to call this function from atomic context. 723db304854SYing Xue * 724db304854SYing Xue * Will trigger an automatic deferred table resizing if the size grows 725db304854SYing Xue * beyond the watermark indicated by grow_decision() which can be passed 726db304854SYing Xue * to rhashtable_init(). 727db304854SYing Xue */ 728db304854SYing Xue bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj) 729db304854SYing Xue { 730*7a868d1eSYing Xue struct rhashtable_compare_arg arg = { 731*7a868d1eSYing Xue .ht = ht, 732*7a868d1eSYing Xue .key = rht_obj(ht, obj) + ht->p.key_offset, 733*7a868d1eSYing Xue }; 734*7a868d1eSYing Xue 735*7a868d1eSYing Xue BUG_ON(!ht->p.key_len); 736*7a868d1eSYing Xue 737*7a868d1eSYing Xue return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare, 738*7a868d1eSYing Xue &arg); 739*7a868d1eSYing Xue } 740*7a868d1eSYing Xue EXPORT_SYMBOL_GPL(rhashtable_lookup_insert); 741*7a868d1eSYing Xue 742*7a868d1eSYing Xue /** 743*7a868d1eSYing Xue * rhashtable_lookup_compare_insert - search and insert object to hash table 744*7a868d1eSYing Xue * with compare function 745*7a868d1eSYing Xue * @ht: hash table 746*7a868d1eSYing Xue * @obj: pointer to hash head inside object 747*7a868d1eSYing Xue * @compare: compare function, must return true on match 748*7a868d1eSYing Xue * @arg: argument passed on to compare function 749*7a868d1eSYing Xue * 750*7a868d1eSYing Xue * Locks down the bucket chain in both the old and new table if a resize 751*7a868d1eSYing Xue * is in progress to ensure that writers can't remove from the old table 752*7a868d1eSYing Xue * and can't insert to the new table during the atomic operation of search 753*7a868d1eSYing Xue * and insertion. Searches for duplicates in both the old and new table if 754*7a868d1eSYing Xue * a resize is in progress. 755*7a868d1eSYing Xue * 756*7a868d1eSYing Xue * Lookups may occur in parallel with hashtable mutations and resizing. 757*7a868d1eSYing Xue * 758*7a868d1eSYing Xue * Will trigger an automatic deferred table resizing if the size grows 759*7a868d1eSYing Xue * beyond the watermark indicated by grow_decision() which can be passed 760*7a868d1eSYing Xue * to rhashtable_init(). 761*7a868d1eSYing Xue */ 762*7a868d1eSYing Xue bool rhashtable_lookup_compare_insert(struct rhashtable *ht, 763*7a868d1eSYing Xue struct rhash_head *obj, 764*7a868d1eSYing Xue bool (*compare)(void *, void *), 765*7a868d1eSYing Xue void *arg) 766*7a868d1eSYing Xue { 767db304854SYing Xue struct bucket_table *new_tbl, *old_tbl; 768db304854SYing Xue spinlock_t *new_bucket_lock, *old_bucket_lock; 769db304854SYing Xue u32 new_hash, old_hash; 770db304854SYing Xue bool success = true; 771db304854SYing Xue 772db304854SYing Xue BUG_ON(!ht->p.key_len); 773db304854SYing Xue 774db304854SYing Xue rcu_read_lock(); 775db304854SYing Xue 776db304854SYing Xue old_tbl = rht_dereference_rcu(ht->tbl, ht); 777db304854SYing Xue old_hash = head_hashfn(ht, old_tbl, obj); 778db304854SYing Xue old_bucket_lock = bucket_lock(old_tbl, old_hash); 779db304854SYing Xue spin_lock_bh(old_bucket_lock); 780db304854SYing Xue 781db304854SYing Xue new_tbl = rht_dereference_rcu(ht->future_tbl, ht); 782db304854SYing Xue new_hash = head_hashfn(ht, new_tbl, obj); 783db304854SYing Xue new_bucket_lock = bucket_lock(new_tbl, new_hash); 784db304854SYing Xue if (unlikely(old_tbl != new_tbl)) 785db304854SYing Xue spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); 786db304854SYing Xue 787*7a868d1eSYing Xue if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset, 788*7a868d1eSYing Xue compare, arg)) { 789db304854SYing Xue success = false; 790db304854SYing Xue goto exit; 791db304854SYing Xue } 792db304854SYing Xue 793db304854SYing Xue __rhashtable_insert(ht, obj, new_tbl, new_hash); 794db304854SYing Xue 795db304854SYing Xue exit: 796db304854SYing Xue if (unlikely(old_tbl != new_tbl)) 797db304854SYing Xue spin_unlock_bh(new_bucket_lock); 798db304854SYing Xue spin_unlock_bh(old_bucket_lock); 799db304854SYing Xue 800db304854SYing Xue rcu_read_unlock(); 801db304854SYing Xue 802db304854SYing Xue return success; 803db304854SYing Xue } 804*7a868d1eSYing Xue EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert); 805db304854SYing Xue 80694000176SYing Xue static size_t rounded_hashtable_size(struct rhashtable_params *params) 8077e1e7763SThomas Graf { 80894000176SYing Xue return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), 80994000176SYing Xue 1UL << params->min_shift); 8107e1e7763SThomas Graf } 8117e1e7763SThomas Graf 8127e1e7763SThomas Graf /** 8137e1e7763SThomas Graf * rhashtable_init - initialize a new hash table 8147e1e7763SThomas Graf * @ht: hash table to be initialized 8157e1e7763SThomas Graf * @params: configuration parameters 8167e1e7763SThomas Graf * 8177e1e7763SThomas Graf * Initializes a new hash table based on the provided configuration 8187e1e7763SThomas Graf * parameters. A table can be configured either with a variable or 8197e1e7763SThomas Graf * fixed length key: 8207e1e7763SThomas Graf * 8217e1e7763SThomas Graf * Configuration Example 1: Fixed length keys 8227e1e7763SThomas Graf * struct test_obj { 8237e1e7763SThomas Graf * int key; 8247e1e7763SThomas Graf * void * my_member; 8257e1e7763SThomas Graf * struct rhash_head node; 8267e1e7763SThomas Graf * }; 8277e1e7763SThomas Graf * 8287e1e7763SThomas Graf * struct rhashtable_params params = { 8297e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 8307e1e7763SThomas Graf * .key_offset = offsetof(struct test_obj, key), 8317e1e7763SThomas Graf * .key_len = sizeof(int), 83287545899SDaniel Borkmann * .hashfn = jhash, 833f89bd6f8SThomas Graf * .nulls_base = (1U << RHT_BASE_SHIFT), 8347e1e7763SThomas Graf * }; 8357e1e7763SThomas Graf * 8367e1e7763SThomas Graf * Configuration Example 2: Variable length keys 8377e1e7763SThomas Graf * struct test_obj { 8387e1e7763SThomas Graf * [...] 8397e1e7763SThomas Graf * struct rhash_head node; 8407e1e7763SThomas Graf * }; 8417e1e7763SThomas Graf * 8427e1e7763SThomas Graf * u32 my_hash_fn(const void *data, u32 seed) 8437e1e7763SThomas Graf * { 8447e1e7763SThomas Graf * struct test_obj *obj = data; 8457e1e7763SThomas Graf * 8467e1e7763SThomas Graf * return [... hash ...]; 8477e1e7763SThomas Graf * } 8487e1e7763SThomas Graf * 8497e1e7763SThomas Graf * struct rhashtable_params params = { 8507e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 85187545899SDaniel Borkmann * .hashfn = jhash, 8527e1e7763SThomas Graf * .obj_hashfn = my_hash_fn, 8537e1e7763SThomas Graf * }; 8547e1e7763SThomas Graf */ 8557e1e7763SThomas Graf int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params) 8567e1e7763SThomas Graf { 8577e1e7763SThomas Graf struct bucket_table *tbl; 8587e1e7763SThomas Graf size_t size; 8597e1e7763SThomas Graf 8607e1e7763SThomas Graf size = HASH_DEFAULT_SIZE; 8617e1e7763SThomas Graf 8627e1e7763SThomas Graf if ((params->key_len && !params->hashfn) || 8637e1e7763SThomas Graf (!params->key_len && !params->obj_hashfn)) 8647e1e7763SThomas Graf return -EINVAL; 8657e1e7763SThomas Graf 866f89bd6f8SThomas Graf if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) 867f89bd6f8SThomas Graf return -EINVAL; 868f89bd6f8SThomas Graf 86994000176SYing Xue params->min_shift = max_t(size_t, params->min_shift, 87094000176SYing Xue ilog2(HASH_MIN_SIZE)); 87194000176SYing Xue 8727e1e7763SThomas Graf if (params->nelem_hint) 87394000176SYing Xue size = rounded_hashtable_size(params); 8747e1e7763SThomas Graf 87597defe1eSThomas Graf memset(ht, 0, sizeof(*ht)); 87697defe1eSThomas Graf mutex_init(&ht->mutex); 87797defe1eSThomas Graf memcpy(&ht->p, params, sizeof(*params)); 87897defe1eSThomas Graf 87997defe1eSThomas Graf if (params->locks_mul) 88097defe1eSThomas Graf ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); 88197defe1eSThomas Graf else 88297defe1eSThomas Graf ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; 88397defe1eSThomas Graf 88497defe1eSThomas Graf tbl = bucket_table_alloc(ht, size); 8857e1e7763SThomas Graf if (tbl == NULL) 8867e1e7763SThomas Graf return -ENOMEM; 8877e1e7763SThomas Graf 888545a148eSYing Xue atomic_set(&ht->nelems, 0); 889c0c09bfdSYing Xue atomic_set(&ht->shift, ilog2(tbl->size)); 8907e1e7763SThomas Graf RCU_INIT_POINTER(ht->tbl, tbl); 89197defe1eSThomas Graf RCU_INIT_POINTER(ht->future_tbl, tbl); 8927e1e7763SThomas Graf 8937e1e7763SThomas Graf if (!ht->p.hash_rnd) 8947e1e7763SThomas Graf get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd)); 8957e1e7763SThomas Graf 89697defe1eSThomas Graf if (ht->p.grow_decision || ht->p.shrink_decision) 89797defe1eSThomas Graf INIT_DEFERRABLE_WORK(&ht->run_work, rht_deferred_worker); 89897defe1eSThomas Graf 8997e1e7763SThomas Graf return 0; 9007e1e7763SThomas Graf } 9017e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init); 9027e1e7763SThomas Graf 9037e1e7763SThomas Graf /** 9047e1e7763SThomas Graf * rhashtable_destroy - destroy hash table 9057e1e7763SThomas Graf * @ht: the hash table to destroy 9067e1e7763SThomas Graf * 907ae82ddcfSPablo Neira Ayuso * Frees the bucket array. This function is not rcu safe, therefore the caller 908ae82ddcfSPablo Neira Ayuso * has to make sure that no resizing may happen by unpublishing the hashtable 909ae82ddcfSPablo Neira Ayuso * and waiting for the quiescent cycle before releasing the bucket array. 9107e1e7763SThomas Graf */ 91197defe1eSThomas Graf void rhashtable_destroy(struct rhashtable *ht) 9127e1e7763SThomas Graf { 91397defe1eSThomas Graf ht->being_destroyed = true; 91497defe1eSThomas Graf 91597defe1eSThomas Graf mutex_lock(&ht->mutex); 91697defe1eSThomas Graf 91797defe1eSThomas Graf cancel_delayed_work(&ht->run_work); 91897defe1eSThomas Graf bucket_table_free(rht_dereference(ht->tbl, ht)); 91997defe1eSThomas Graf 92097defe1eSThomas Graf mutex_unlock(&ht->mutex); 9217e1e7763SThomas Graf } 9227e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy); 9237e1e7763SThomas Graf 9247e1e7763SThomas Graf /************************************************************************** 9257e1e7763SThomas Graf * Self Test 9267e1e7763SThomas Graf **************************************************************************/ 9277e1e7763SThomas Graf 9287e1e7763SThomas Graf #ifdef CONFIG_TEST_RHASHTABLE 9297e1e7763SThomas Graf 9307e1e7763SThomas Graf #define TEST_HT_SIZE 8 9317e1e7763SThomas Graf #define TEST_ENTRIES 2048 9327e1e7763SThomas Graf #define TEST_PTR ((void *) 0xdeadbeef) 9337e1e7763SThomas Graf #define TEST_NEXPANDS 4 9347e1e7763SThomas Graf 9357e1e7763SThomas Graf struct test_obj { 9367e1e7763SThomas Graf void *ptr; 9377e1e7763SThomas Graf int value; 9387e1e7763SThomas Graf struct rhash_head node; 9397e1e7763SThomas Graf }; 9407e1e7763SThomas Graf 9417e1e7763SThomas Graf static int __init test_rht_lookup(struct rhashtable *ht) 9427e1e7763SThomas Graf { 9437e1e7763SThomas Graf unsigned int i; 9447e1e7763SThomas Graf 9457e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES * 2; i++) { 9467e1e7763SThomas Graf struct test_obj *obj; 9477e1e7763SThomas Graf bool expected = !(i % 2); 9487e1e7763SThomas Graf u32 key = i; 9497e1e7763SThomas Graf 9507e1e7763SThomas Graf obj = rhashtable_lookup(ht, &key); 9517e1e7763SThomas Graf 9527e1e7763SThomas Graf if (expected && !obj) { 9537e1e7763SThomas Graf pr_warn("Test failed: Could not find key %u\n", key); 9547e1e7763SThomas Graf return -ENOENT; 9557e1e7763SThomas Graf } else if (!expected && obj) { 9567e1e7763SThomas Graf pr_warn("Test failed: Unexpected entry found for key %u\n", 9577e1e7763SThomas Graf key); 9587e1e7763SThomas Graf return -EEXIST; 9597e1e7763SThomas Graf } else if (expected && obj) { 9607e1e7763SThomas Graf if (obj->ptr != TEST_PTR || obj->value != i) { 9617e1e7763SThomas Graf pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n", 9627e1e7763SThomas Graf obj->ptr, TEST_PTR, obj->value, i); 9637e1e7763SThomas Graf return -EINVAL; 9647e1e7763SThomas Graf } 9657e1e7763SThomas Graf } 9667e1e7763SThomas Graf } 9677e1e7763SThomas Graf 9687e1e7763SThomas Graf return 0; 9697e1e7763SThomas Graf } 9707e1e7763SThomas Graf 9713e7b2ec4SThomas Graf static void test_bucket_stats(struct rhashtable *ht, bool quiet) 9727e1e7763SThomas Graf { 9733e7b2ec4SThomas Graf unsigned int cnt, rcu_cnt, i, total = 0; 97488d6ed15SThomas Graf struct rhash_head *pos; 9757e1e7763SThomas Graf struct test_obj *obj; 9763e7b2ec4SThomas Graf struct bucket_table *tbl; 9777e1e7763SThomas Graf 9783e7b2ec4SThomas Graf tbl = rht_dereference_rcu(ht->tbl, ht); 9797e1e7763SThomas Graf for (i = 0; i < tbl->size; i++) { 9803e7b2ec4SThomas Graf rcu_cnt = cnt = 0; 9817e1e7763SThomas Graf 9827e1e7763SThomas Graf if (!quiet) 9837e1e7763SThomas Graf pr_info(" [%#4x/%zu]", i, tbl->size); 9847e1e7763SThomas Graf 98588d6ed15SThomas Graf rht_for_each_entry_rcu(obj, pos, tbl, i, node) { 9867e1e7763SThomas Graf cnt++; 9877e1e7763SThomas Graf total++; 9887e1e7763SThomas Graf if (!quiet) 9897e1e7763SThomas Graf pr_cont(" [%p],", obj); 9907e1e7763SThomas Graf } 9917e1e7763SThomas Graf 99288d6ed15SThomas Graf rht_for_each_entry_rcu(obj, pos, tbl, i, node) 9933e7b2ec4SThomas Graf rcu_cnt++; 9943e7b2ec4SThomas Graf 9953e7b2ec4SThomas Graf if (rcu_cnt != cnt) 9963e7b2ec4SThomas Graf pr_warn("Test failed: Chain count mismach %d != %d", 9973e7b2ec4SThomas Graf cnt, rcu_cnt); 9983e7b2ec4SThomas Graf 9997e1e7763SThomas Graf if (!quiet) 10007e1e7763SThomas Graf pr_cont("\n [%#x] first element: %p, chain length: %u\n", 10017e1e7763SThomas Graf i, tbl->buckets[i], cnt); 10027e1e7763SThomas Graf } 10037e1e7763SThomas Graf 100497defe1eSThomas Graf pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n", 100597defe1eSThomas Graf total, atomic_read(&ht->nelems), TEST_ENTRIES); 10063e7b2ec4SThomas Graf 100797defe1eSThomas Graf if (total != atomic_read(&ht->nelems) || total != TEST_ENTRIES) 10083e7b2ec4SThomas Graf pr_warn("Test failed: Total count mismatch ^^^"); 10097e1e7763SThomas Graf } 10107e1e7763SThomas Graf 10117e1e7763SThomas Graf static int __init test_rhashtable(struct rhashtable *ht) 10127e1e7763SThomas Graf { 10137e1e7763SThomas Graf struct bucket_table *tbl; 101488d6ed15SThomas Graf struct test_obj *obj; 101588d6ed15SThomas Graf struct rhash_head *pos, *next; 10167e1e7763SThomas Graf int err; 10177e1e7763SThomas Graf unsigned int i; 10187e1e7763SThomas Graf 10197e1e7763SThomas Graf /* 10207e1e7763SThomas Graf * Insertion Test: 10217e1e7763SThomas Graf * Insert TEST_ENTRIES into table with all keys even numbers 10227e1e7763SThomas Graf */ 10237e1e7763SThomas Graf pr_info(" Adding %d keys\n", TEST_ENTRIES); 10247e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES; i++) { 10257e1e7763SThomas Graf struct test_obj *obj; 10267e1e7763SThomas Graf 10277e1e7763SThomas Graf obj = kzalloc(sizeof(*obj), GFP_KERNEL); 10287e1e7763SThomas Graf if (!obj) { 10297e1e7763SThomas Graf err = -ENOMEM; 10307e1e7763SThomas Graf goto error; 10317e1e7763SThomas Graf } 10327e1e7763SThomas Graf 10337e1e7763SThomas Graf obj->ptr = TEST_PTR; 10347e1e7763SThomas Graf obj->value = i * 2; 10357e1e7763SThomas Graf 10366eba8224SThomas Graf rhashtable_insert(ht, &obj->node); 10377e1e7763SThomas Graf } 10387e1e7763SThomas Graf 10397e1e7763SThomas Graf rcu_read_lock(); 10403e7b2ec4SThomas Graf test_bucket_stats(ht, true); 10417e1e7763SThomas Graf test_rht_lookup(ht); 10427e1e7763SThomas Graf rcu_read_unlock(); 10437e1e7763SThomas Graf 10447e1e7763SThomas Graf for (i = 0; i < TEST_NEXPANDS; i++) { 10457e1e7763SThomas Graf pr_info(" Table expansion iteration %u...\n", i); 104697defe1eSThomas Graf mutex_lock(&ht->mutex); 10476eba8224SThomas Graf rhashtable_expand(ht); 104897defe1eSThomas Graf mutex_unlock(&ht->mutex); 10497e1e7763SThomas Graf 10507e1e7763SThomas Graf rcu_read_lock(); 10517e1e7763SThomas Graf pr_info(" Verifying lookups...\n"); 10527e1e7763SThomas Graf test_rht_lookup(ht); 10537e1e7763SThomas Graf rcu_read_unlock(); 10547e1e7763SThomas Graf } 10557e1e7763SThomas Graf 10567e1e7763SThomas Graf for (i = 0; i < TEST_NEXPANDS; i++) { 10577e1e7763SThomas Graf pr_info(" Table shrinkage iteration %u...\n", i); 105897defe1eSThomas Graf mutex_lock(&ht->mutex); 10596eba8224SThomas Graf rhashtable_shrink(ht); 106097defe1eSThomas Graf mutex_unlock(&ht->mutex); 10617e1e7763SThomas Graf 10627e1e7763SThomas Graf rcu_read_lock(); 10637e1e7763SThomas Graf pr_info(" Verifying lookups...\n"); 10647e1e7763SThomas Graf test_rht_lookup(ht); 10657e1e7763SThomas Graf rcu_read_unlock(); 10667e1e7763SThomas Graf } 10677e1e7763SThomas Graf 10683e7b2ec4SThomas Graf rcu_read_lock(); 10693e7b2ec4SThomas Graf test_bucket_stats(ht, true); 10703e7b2ec4SThomas Graf rcu_read_unlock(); 10713e7b2ec4SThomas Graf 10727e1e7763SThomas Graf pr_info(" Deleting %d keys\n", TEST_ENTRIES); 10737e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES; i++) { 10747e1e7763SThomas Graf u32 key = i * 2; 10757e1e7763SThomas Graf 10767e1e7763SThomas Graf obj = rhashtable_lookup(ht, &key); 10777e1e7763SThomas Graf BUG_ON(!obj); 10787e1e7763SThomas Graf 10796eba8224SThomas Graf rhashtable_remove(ht, &obj->node); 10807e1e7763SThomas Graf kfree(obj); 10817e1e7763SThomas Graf } 10827e1e7763SThomas Graf 10837e1e7763SThomas Graf return 0; 10847e1e7763SThomas Graf 10857e1e7763SThomas Graf error: 10867e1e7763SThomas Graf tbl = rht_dereference_rcu(ht->tbl, ht); 10877e1e7763SThomas Graf for (i = 0; i < tbl->size; i++) 108888d6ed15SThomas Graf rht_for_each_entry_safe(obj, pos, next, tbl, i, node) 10897e1e7763SThomas Graf kfree(obj); 10907e1e7763SThomas Graf 10917e1e7763SThomas Graf return err; 10927e1e7763SThomas Graf } 10937e1e7763SThomas Graf 10947e1e7763SThomas Graf static int __init test_rht_init(void) 10957e1e7763SThomas Graf { 10967e1e7763SThomas Graf struct rhashtable ht; 10977e1e7763SThomas Graf struct rhashtable_params params = { 10987e1e7763SThomas Graf .nelem_hint = TEST_HT_SIZE, 10997e1e7763SThomas Graf .head_offset = offsetof(struct test_obj, node), 11007e1e7763SThomas Graf .key_offset = offsetof(struct test_obj, value), 11017e1e7763SThomas Graf .key_len = sizeof(int), 110287545899SDaniel Borkmann .hashfn = jhash, 1103f89bd6f8SThomas Graf .nulls_base = (3U << RHT_BASE_SHIFT), 11047e1e7763SThomas Graf .grow_decision = rht_grow_above_75, 11057e1e7763SThomas Graf .shrink_decision = rht_shrink_below_30, 11067e1e7763SThomas Graf }; 11077e1e7763SThomas Graf int err; 11087e1e7763SThomas Graf 11097e1e7763SThomas Graf pr_info("Running resizable hashtable tests...\n"); 11107e1e7763SThomas Graf 11117e1e7763SThomas Graf err = rhashtable_init(&ht, ¶ms); 11127e1e7763SThomas Graf if (err < 0) { 11137e1e7763SThomas Graf pr_warn("Test failed: Unable to initialize hashtable: %d\n", 11147e1e7763SThomas Graf err); 11157e1e7763SThomas Graf return err; 11167e1e7763SThomas Graf } 11177e1e7763SThomas Graf 11187e1e7763SThomas Graf err = test_rhashtable(&ht); 11197e1e7763SThomas Graf 11207e1e7763SThomas Graf rhashtable_destroy(&ht); 11217e1e7763SThomas Graf 11227e1e7763SThomas Graf return err; 11237e1e7763SThomas Graf } 11247e1e7763SThomas Graf 11257e1e7763SThomas Graf subsys_initcall(test_rht_init); 11267e1e7763SThomas Graf 11277e1e7763SThomas Graf #endif /* CONFIG_TEST_RHASHTABLE */ 1128