17e1e7763SThomas Graf /* 27e1e7763SThomas Graf * Resizable, Scalable, Concurrent Hash Table 37e1e7763SThomas Graf * 402fd97c3SHerbert Xu * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> 5a5ec68e3SThomas Graf * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch> 67e1e7763SThomas Graf * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> 77e1e7763SThomas Graf * 87e1e7763SThomas Graf * Code partially derived from nft_hash 902fd97c3SHerbert Xu * Rewritten with rehash code from br_multicast plus single list 1002fd97c3SHerbert Xu * pointer as suggested by Josh Triplett 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> 205beb5c90SEric Dumazet #include <linux/sched.h> 217e1e7763SThomas Graf #include <linux/slab.h> 227e1e7763SThomas Graf #include <linux/vmalloc.h> 237e1e7763SThomas Graf #include <linux/mm.h> 2487545899SDaniel Borkmann #include <linux/jhash.h> 257e1e7763SThomas Graf #include <linux/random.h> 267e1e7763SThomas Graf #include <linux/rhashtable.h> 2761d7b097SStephen Rothwell #include <linux/err.h> 287e1e7763SThomas Graf 297e1e7763SThomas Graf #define HASH_DEFAULT_SIZE 64UL 30c2e213cfSHerbert Xu #define HASH_MIN_SIZE 4U 3197defe1eSThomas Graf #define BUCKET_LOCKS_PER_CPU 128UL 3297defe1eSThomas Graf 33988dfbd7SHerbert Xu static u32 head_hashfn(struct rhashtable *ht, 348d24c0b4SThomas Graf const struct bucket_table *tbl, 358d24c0b4SThomas Graf const struct rhash_head *he) 367e1e7763SThomas Graf { 3702fd97c3SHerbert Xu return rht_head_hashfn(ht, tbl, he, ht->p); 387e1e7763SThomas Graf } 397e1e7763SThomas Graf 40a03eaec0SThomas Graf #ifdef CONFIG_PROVE_LOCKING 41a03eaec0SThomas Graf #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) 42a03eaec0SThomas Graf 43a03eaec0SThomas Graf int lockdep_rht_mutex_is_held(struct rhashtable *ht) 44a03eaec0SThomas Graf { 45a03eaec0SThomas Graf return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; 46a03eaec0SThomas Graf } 47a03eaec0SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); 48a03eaec0SThomas Graf 49a03eaec0SThomas Graf int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) 50a03eaec0SThomas Graf { 5102fd97c3SHerbert Xu spinlock_t *lock = rht_bucket_lock(tbl, hash); 52a03eaec0SThomas Graf 53a03eaec0SThomas Graf return (debug_locks) ? lockdep_is_held(lock) : 1; 54a03eaec0SThomas Graf } 55a03eaec0SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); 56a03eaec0SThomas Graf #else 57a03eaec0SThomas Graf #define ASSERT_RHT_MUTEX(HT) 58a03eaec0SThomas Graf #endif 59a03eaec0SThomas Graf 60a03eaec0SThomas Graf 61b9ecfdaaSHerbert Xu static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl, 62b9ecfdaaSHerbert Xu gfp_t gfp) 6397defe1eSThomas Graf { 6497defe1eSThomas Graf unsigned int i, size; 6597defe1eSThomas Graf #if defined(CONFIG_PROVE_LOCKING) 6697defe1eSThomas Graf unsigned int nr_pcpus = 2; 6797defe1eSThomas Graf #else 6897defe1eSThomas Graf unsigned int nr_pcpus = num_possible_cpus(); 6997defe1eSThomas Graf #endif 7097defe1eSThomas Graf 7197defe1eSThomas Graf nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL); 7297defe1eSThomas Graf size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul); 7397defe1eSThomas Graf 74a5ec68e3SThomas Graf /* Never allocate more than 0.5 locks per bucket */ 75a5ec68e3SThomas Graf size = min_t(unsigned int, size, tbl->size >> 1); 7697defe1eSThomas Graf 7797defe1eSThomas Graf if (sizeof(spinlock_t) != 0) { 7897defe1eSThomas Graf #ifdef CONFIG_NUMA 79b9ecfdaaSHerbert Xu if (size * sizeof(spinlock_t) > PAGE_SIZE && 80b9ecfdaaSHerbert Xu gfp == GFP_KERNEL) 8197defe1eSThomas Graf tbl->locks = vmalloc(size * sizeof(spinlock_t)); 8297defe1eSThomas Graf else 8397defe1eSThomas Graf #endif 8497defe1eSThomas Graf tbl->locks = kmalloc_array(size, sizeof(spinlock_t), 85b9ecfdaaSHerbert Xu gfp); 8697defe1eSThomas Graf if (!tbl->locks) 8797defe1eSThomas Graf return -ENOMEM; 8897defe1eSThomas Graf for (i = 0; i < size; i++) 8997defe1eSThomas Graf spin_lock_init(&tbl->locks[i]); 9097defe1eSThomas Graf } 9197defe1eSThomas Graf tbl->locks_mask = size - 1; 9297defe1eSThomas Graf 9397defe1eSThomas Graf return 0; 9497defe1eSThomas Graf } 9597defe1eSThomas Graf 9697defe1eSThomas Graf static void bucket_table_free(const struct bucket_table *tbl) 9797defe1eSThomas Graf { 9897defe1eSThomas Graf if (tbl) 9997defe1eSThomas Graf kvfree(tbl->locks); 10097defe1eSThomas Graf 10197defe1eSThomas Graf kvfree(tbl); 10297defe1eSThomas Graf } 10397defe1eSThomas Graf 1049d901bc0SHerbert Xu static void bucket_table_free_rcu(struct rcu_head *head) 1059d901bc0SHerbert Xu { 1069d901bc0SHerbert Xu bucket_table_free(container_of(head, struct bucket_table, rcu)); 1079d901bc0SHerbert Xu } 1089d901bc0SHerbert Xu 10997defe1eSThomas Graf static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, 110b9ecfdaaSHerbert Xu size_t nbuckets, 111b9ecfdaaSHerbert Xu gfp_t gfp) 1127e1e7763SThomas Graf { 113eb6d1abfSDaniel Borkmann struct bucket_table *tbl = NULL; 1147e1e7763SThomas Graf size_t size; 115f89bd6f8SThomas Graf int i; 1167e1e7763SThomas Graf 1177e1e7763SThomas Graf size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); 118b9ecfdaaSHerbert Xu if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) || 119b9ecfdaaSHerbert Xu gfp != GFP_KERNEL) 120b9ecfdaaSHerbert Xu tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY); 121b9ecfdaaSHerbert Xu if (tbl == NULL && gfp == GFP_KERNEL) 1227e1e7763SThomas Graf tbl = vzalloc(size); 1237e1e7763SThomas Graf if (tbl == NULL) 1247e1e7763SThomas Graf return NULL; 1257e1e7763SThomas Graf 1267e1e7763SThomas Graf tbl->size = nbuckets; 1277e1e7763SThomas Graf 128b9ecfdaaSHerbert Xu if (alloc_bucket_locks(ht, tbl, gfp) < 0) { 12997defe1eSThomas Graf bucket_table_free(tbl); 13097defe1eSThomas Graf return NULL; 1317e1e7763SThomas Graf } 1327e1e7763SThomas Graf 133eddee5baSHerbert Xu INIT_LIST_HEAD(&tbl->walkers); 134eddee5baSHerbert Xu 1355269b53dSHerbert Xu get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); 1365269b53dSHerbert Xu 137f89bd6f8SThomas Graf for (i = 0; i < nbuckets; i++) 138f89bd6f8SThomas Graf INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); 139f89bd6f8SThomas Graf 14097defe1eSThomas Graf return tbl; 1417e1e7763SThomas Graf } 1427e1e7763SThomas Graf 143b824478bSHerbert Xu static struct bucket_table *rhashtable_last_table(struct rhashtable *ht, 144b824478bSHerbert Xu struct bucket_table *tbl) 145b824478bSHerbert Xu { 146b824478bSHerbert Xu struct bucket_table *new_tbl; 147b824478bSHerbert Xu 148b824478bSHerbert Xu do { 149b824478bSHerbert Xu new_tbl = tbl; 150b824478bSHerbert Xu tbl = rht_dereference_rcu(tbl->future_tbl, ht); 151b824478bSHerbert Xu } while (tbl); 152b824478bSHerbert Xu 153b824478bSHerbert Xu return new_tbl; 154b824478bSHerbert Xu } 155b824478bSHerbert Xu 156aa34a6cbSHerbert Xu static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash) 157a5ec68e3SThomas Graf { 158aa34a6cbSHerbert Xu struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); 159b824478bSHerbert Xu struct bucket_table *new_tbl = rhashtable_last_table(ht, 160b824478bSHerbert Xu rht_dereference_rcu(old_tbl->future_tbl, ht)); 161aa34a6cbSHerbert Xu struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash]; 162aa34a6cbSHerbert Xu int err = -ENOENT; 163aa34a6cbSHerbert Xu struct rhash_head *head, *next, *entry; 164aa34a6cbSHerbert Xu spinlock_t *new_bucket_lock; 165aa34a6cbSHerbert Xu unsigned new_hash; 166a5ec68e3SThomas Graf 167aa34a6cbSHerbert Xu rht_for_each(entry, old_tbl, old_hash) { 168aa34a6cbSHerbert Xu err = 0; 169aa34a6cbSHerbert Xu next = rht_dereference_bucket(entry->next, old_tbl, old_hash); 170a5ec68e3SThomas Graf 171aa34a6cbSHerbert Xu if (rht_is_a_nulls(next)) 1727e1e7763SThomas Graf break; 17397defe1eSThomas Graf 174aa34a6cbSHerbert Xu pprev = &entry->next; 1757e1e7763SThomas Graf } 1767e1e7763SThomas Graf 177aa34a6cbSHerbert Xu if (err) 178aa34a6cbSHerbert Xu goto out; 17997defe1eSThomas Graf 180aa34a6cbSHerbert Xu new_hash = head_hashfn(ht, new_tbl, entry); 181a5ec68e3SThomas Graf 18202fd97c3SHerbert Xu new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); 183aa34a6cbSHerbert Xu 1848f2484bdSHerbert Xu spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); 185aa34a6cbSHerbert Xu head = rht_dereference_bucket(new_tbl->buckets[new_hash], 186aa34a6cbSHerbert Xu new_tbl, new_hash); 187aa34a6cbSHerbert Xu 188aa34a6cbSHerbert Xu if (rht_is_a_nulls(head)) 189aa34a6cbSHerbert Xu INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash); 190aa34a6cbSHerbert Xu else 191aa34a6cbSHerbert Xu RCU_INIT_POINTER(entry->next, head); 192aa34a6cbSHerbert Xu 193aa34a6cbSHerbert Xu rcu_assign_pointer(new_tbl->buckets[new_hash], entry); 194aa34a6cbSHerbert Xu spin_unlock(new_bucket_lock); 195aa34a6cbSHerbert Xu 196aa34a6cbSHerbert Xu rcu_assign_pointer(*pprev, next); 197aa34a6cbSHerbert Xu 198aa34a6cbSHerbert Xu out: 199aa34a6cbSHerbert Xu return err; 20097defe1eSThomas Graf } 20197defe1eSThomas Graf 202aa34a6cbSHerbert Xu static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash) 20397defe1eSThomas Graf { 204aa34a6cbSHerbert Xu struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); 205aa34a6cbSHerbert Xu spinlock_t *old_bucket_lock; 2067cd10db8SThomas Graf 20702fd97c3SHerbert Xu old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); 208aa34a6cbSHerbert Xu 209aa34a6cbSHerbert Xu spin_lock_bh(old_bucket_lock); 210aa34a6cbSHerbert Xu while (!rhashtable_rehash_one(ht, old_hash)) 211aa34a6cbSHerbert Xu ; 21263d512d0SHerbert Xu old_tbl->rehash++; 213aa34a6cbSHerbert Xu spin_unlock_bh(old_bucket_lock); 214aa34a6cbSHerbert Xu } 215aa34a6cbSHerbert Xu 216b824478bSHerbert Xu static int rhashtable_rehash_attach(struct rhashtable *ht, 217b824478bSHerbert Xu struct bucket_table *old_tbl, 218aa34a6cbSHerbert Xu struct bucket_table *new_tbl) 219aa34a6cbSHerbert Xu { 220b824478bSHerbert Xu /* Protect future_tbl using the first bucket lock. */ 221b824478bSHerbert Xu spin_lock_bh(old_tbl->locks); 222b824478bSHerbert Xu 223b824478bSHerbert Xu /* Did somebody beat us to it? */ 224b824478bSHerbert Xu if (rcu_access_pointer(old_tbl->future_tbl)) { 225b824478bSHerbert Xu spin_unlock_bh(old_tbl->locks); 226b824478bSHerbert Xu return -EEXIST; 227b824478bSHerbert Xu } 228aa34a6cbSHerbert Xu 229aa34a6cbSHerbert Xu /* Make insertions go into the new, empty table right away. Deletions 230aa34a6cbSHerbert Xu * and lookups will be attempted in both tables until we synchronize. 231aa34a6cbSHerbert Xu */ 232c4db8848SHerbert Xu rcu_assign_pointer(old_tbl->future_tbl, new_tbl); 233aa34a6cbSHerbert Xu 2349497df88SHerbert Xu /* Ensure the new table is visible to readers. */ 2359497df88SHerbert Xu smp_wmb(); 2369497df88SHerbert Xu 237b824478bSHerbert Xu spin_unlock_bh(old_tbl->locks); 238b824478bSHerbert Xu 239b824478bSHerbert Xu return 0; 240b824478bSHerbert Xu } 241b824478bSHerbert Xu 242b824478bSHerbert Xu static int rhashtable_rehash_table(struct rhashtable *ht) 243b824478bSHerbert Xu { 244b824478bSHerbert Xu struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); 245b824478bSHerbert Xu struct bucket_table *new_tbl; 246b824478bSHerbert Xu struct rhashtable_walker *walker; 247b824478bSHerbert Xu unsigned old_hash; 248b824478bSHerbert Xu 249b824478bSHerbert Xu new_tbl = rht_dereference(old_tbl->future_tbl, ht); 250b824478bSHerbert Xu if (!new_tbl) 251b824478bSHerbert Xu return 0; 252b824478bSHerbert Xu 253aa34a6cbSHerbert Xu for (old_hash = 0; old_hash < old_tbl->size; old_hash++) 254aa34a6cbSHerbert Xu rhashtable_rehash_chain(ht, old_hash); 255aa34a6cbSHerbert Xu 256aa34a6cbSHerbert Xu /* Publish the new table pointer. */ 257aa34a6cbSHerbert Xu rcu_assign_pointer(ht->tbl, new_tbl); 258aa34a6cbSHerbert Xu 259ba7c95eaSHerbert Xu spin_lock(&ht->lock); 260eddee5baSHerbert Xu list_for_each_entry(walker, &old_tbl->walkers, list) 261eddee5baSHerbert Xu walker->tbl = NULL; 262ba7c95eaSHerbert Xu spin_unlock(&ht->lock); 263eddee5baSHerbert Xu 264aa34a6cbSHerbert Xu /* Wait for readers. All new readers will see the new 265aa34a6cbSHerbert Xu * table, and thus no references to the old table will 266aa34a6cbSHerbert Xu * remain. 267aa34a6cbSHerbert Xu */ 2689d901bc0SHerbert Xu call_rcu(&old_tbl->rcu, bucket_table_free_rcu); 269b824478bSHerbert Xu 270b824478bSHerbert Xu return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0; 2717e1e7763SThomas Graf } 2727e1e7763SThomas Graf 2737e1e7763SThomas Graf /** 2747e1e7763SThomas Graf * rhashtable_expand - Expand hash table while allowing concurrent lookups 2757e1e7763SThomas Graf * @ht: the hash table to expand 2767e1e7763SThomas Graf * 277aa34a6cbSHerbert Xu * A secondary bucket array is allocated and the hash entries are migrated. 2787e1e7763SThomas Graf * 2797e1e7763SThomas Graf * This function may only be called in a context where it is safe to call 2807e1e7763SThomas Graf * synchronize_rcu(), e.g. not within a rcu_read_lock() section. 2817e1e7763SThomas Graf * 28297defe1eSThomas Graf * The caller must ensure that no concurrent resizing occurs by holding 28397defe1eSThomas Graf * ht->mutex. 28497defe1eSThomas Graf * 28597defe1eSThomas Graf * It is valid to have concurrent insertions and deletions protected by per 28697defe1eSThomas Graf * bucket locks or concurrent RCU protected lookups and traversals. 2877e1e7763SThomas Graf */ 288b824478bSHerbert Xu static int rhashtable_expand(struct rhashtable *ht) 2897e1e7763SThomas Graf { 2907e1e7763SThomas Graf struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); 291b824478bSHerbert Xu int err; 2927e1e7763SThomas Graf 2937e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 2947e1e7763SThomas Graf 295b824478bSHerbert Xu old_tbl = rhashtable_last_table(ht, old_tbl); 296b824478bSHerbert Xu 297b9ecfdaaSHerbert Xu new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, GFP_KERNEL); 2987e1e7763SThomas Graf if (new_tbl == NULL) 2997e1e7763SThomas Graf return -ENOMEM; 3007e1e7763SThomas Graf 301b824478bSHerbert Xu err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); 302b824478bSHerbert Xu if (err) 303b824478bSHerbert Xu bucket_table_free(new_tbl); 304b824478bSHerbert Xu 305b824478bSHerbert Xu return err; 3067e1e7763SThomas Graf } 3077e1e7763SThomas Graf 3087e1e7763SThomas Graf /** 3097e1e7763SThomas Graf * rhashtable_shrink - Shrink hash table while allowing concurrent lookups 3107e1e7763SThomas Graf * @ht: the hash table to shrink 3117e1e7763SThomas Graf * 31218093d1cSHerbert Xu * This function shrinks the hash table to fit, i.e., the smallest 31318093d1cSHerbert Xu * size would not cause it to expand right away automatically. 3147e1e7763SThomas Graf * 31597defe1eSThomas Graf * The caller must ensure that no concurrent resizing occurs by holding 31697defe1eSThomas Graf * ht->mutex. 31797defe1eSThomas Graf * 3187e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations take place. 3197e1e7763SThomas Graf * It is however valid to have concurrent lookups if they are RCU protected. 32097defe1eSThomas Graf * 32197defe1eSThomas Graf * It is valid to have concurrent insertions and deletions protected by per 32297defe1eSThomas Graf * bucket locks or concurrent RCU protected lookups and traversals. 3237e1e7763SThomas Graf */ 324b824478bSHerbert Xu static int rhashtable_shrink(struct rhashtable *ht) 3257e1e7763SThomas Graf { 326a5b6846fSDaniel Borkmann struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); 32718093d1cSHerbert Xu unsigned size = roundup_pow_of_two(atomic_read(&ht->nelems) * 3 / 2); 328b824478bSHerbert Xu int err; 3297e1e7763SThomas Graf 3307e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 3317e1e7763SThomas Graf 33218093d1cSHerbert Xu if (size < ht->p.min_size) 33318093d1cSHerbert Xu size = ht->p.min_size; 33418093d1cSHerbert Xu 33518093d1cSHerbert Xu if (old_tbl->size <= size) 33618093d1cSHerbert Xu return 0; 33718093d1cSHerbert Xu 338b824478bSHerbert Xu if (rht_dereference(old_tbl->future_tbl, ht)) 339b824478bSHerbert Xu return -EEXIST; 340b824478bSHerbert Xu 341b9ecfdaaSHerbert Xu new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL); 34297defe1eSThomas Graf if (new_tbl == NULL) 3437e1e7763SThomas Graf return -ENOMEM; 3447e1e7763SThomas Graf 345b824478bSHerbert Xu err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); 346b824478bSHerbert Xu if (err) 347b824478bSHerbert Xu bucket_table_free(new_tbl); 348b824478bSHerbert Xu 349b824478bSHerbert Xu return err; 3507e1e7763SThomas Graf } 3517e1e7763SThomas Graf 35297defe1eSThomas Graf static void rht_deferred_worker(struct work_struct *work) 35397defe1eSThomas Graf { 35497defe1eSThomas Graf struct rhashtable *ht; 35597defe1eSThomas Graf struct bucket_table *tbl; 356b824478bSHerbert Xu int err = 0; 35797defe1eSThomas Graf 35857699a40SYing Xue ht = container_of(work, struct rhashtable, run_work); 35997defe1eSThomas Graf mutex_lock(&ht->mutex); 36028134a53SHerbert Xu if (ht->being_destroyed) 36128134a53SHerbert Xu goto unlock; 36228134a53SHerbert Xu 36397defe1eSThomas Graf tbl = rht_dereference(ht->tbl, ht); 364b824478bSHerbert Xu tbl = rhashtable_last_table(ht, tbl); 36597defe1eSThomas Graf 366a5b6846fSDaniel Borkmann if (rht_grow_above_75(ht, tbl)) 36797defe1eSThomas Graf rhashtable_expand(ht); 368a5b6846fSDaniel Borkmann else if (rht_shrink_below_30(ht, tbl)) 36997defe1eSThomas Graf rhashtable_shrink(ht); 370b824478bSHerbert Xu 371b824478bSHerbert Xu err = rhashtable_rehash_table(ht); 372b824478bSHerbert Xu 37328134a53SHerbert Xu unlock: 37497defe1eSThomas Graf mutex_unlock(&ht->mutex); 375b824478bSHerbert Xu 376b824478bSHerbert Xu if (err) 377b824478bSHerbert Xu schedule_work(&ht->run_work); 37897defe1eSThomas Graf } 37997defe1eSThomas Graf 380ccd57b1bSHerbert Xu static bool rhashtable_check_elasticity(struct rhashtable *ht, 381ccd57b1bSHerbert Xu struct bucket_table *tbl, 382ccd57b1bSHerbert Xu unsigned hash) 383ccd57b1bSHerbert Xu { 384ccd57b1bSHerbert Xu unsigned elasticity = ht->elasticity; 385ccd57b1bSHerbert Xu struct rhash_head *head; 386ccd57b1bSHerbert Xu 387ccd57b1bSHerbert Xu rht_for_each(head, tbl, hash) 388ccd57b1bSHerbert Xu if (!--elasticity) 389ccd57b1bSHerbert Xu return true; 390ccd57b1bSHerbert Xu 391ccd57b1bSHerbert Xu return false; 392ccd57b1bSHerbert Xu } 393ccd57b1bSHerbert Xu 394ccd57b1bSHerbert Xu int rhashtable_insert_rehash(struct rhashtable *ht) 395ccd57b1bSHerbert Xu { 396ccd57b1bSHerbert Xu struct bucket_table *old_tbl; 397ccd57b1bSHerbert Xu struct bucket_table *new_tbl; 398ccd57b1bSHerbert Xu struct bucket_table *tbl; 399ccd57b1bSHerbert Xu unsigned int size; 400ccd57b1bSHerbert Xu int err; 401ccd57b1bSHerbert Xu 402ccd57b1bSHerbert Xu old_tbl = rht_dereference_rcu(ht->tbl, ht); 403ccd57b1bSHerbert Xu tbl = rhashtable_last_table(ht, old_tbl); 404ccd57b1bSHerbert Xu 405ccd57b1bSHerbert Xu size = tbl->size; 406ccd57b1bSHerbert Xu 407ccd57b1bSHerbert Xu if (rht_grow_above_75(ht, tbl)) 408ccd57b1bSHerbert Xu size *= 2; 409ccd57b1bSHerbert Xu /* More than two rehashes (not resizes) detected. */ 410ccd57b1bSHerbert Xu else if (WARN_ON(old_tbl != tbl && old_tbl->size == size)) 411ccd57b1bSHerbert Xu return -EBUSY; 412ccd57b1bSHerbert Xu 413ccd57b1bSHerbert Xu new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC); 414ccd57b1bSHerbert Xu if (new_tbl == NULL) 415ccd57b1bSHerbert Xu return -ENOMEM; 416ccd57b1bSHerbert Xu 417ccd57b1bSHerbert Xu err = rhashtable_rehash_attach(ht, tbl, new_tbl); 418ccd57b1bSHerbert Xu if (err) { 419ccd57b1bSHerbert Xu bucket_table_free(new_tbl); 420ccd57b1bSHerbert Xu if (err == -EEXIST) 421ccd57b1bSHerbert Xu err = 0; 422ccd57b1bSHerbert Xu } else 423ccd57b1bSHerbert Xu schedule_work(&ht->run_work); 424ccd57b1bSHerbert Xu 425ccd57b1bSHerbert Xu return err; 426ccd57b1bSHerbert Xu } 427ccd57b1bSHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_insert_rehash); 428ccd57b1bSHerbert Xu 42902fd97c3SHerbert Xu int rhashtable_insert_slow(struct rhashtable *ht, const void *key, 43002fd97c3SHerbert Xu struct rhash_head *obj, 43102fd97c3SHerbert Xu struct bucket_table *tbl) 43202fd97c3SHerbert Xu { 43302fd97c3SHerbert Xu struct rhash_head *head; 43402fd97c3SHerbert Xu unsigned hash; 435ccd57b1bSHerbert Xu int err; 43602fd97c3SHerbert Xu 437b824478bSHerbert Xu tbl = rhashtable_last_table(ht, tbl); 43802fd97c3SHerbert Xu hash = head_hashfn(ht, tbl, obj); 43902fd97c3SHerbert Xu spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING); 44002fd97c3SHerbert Xu 441ccd57b1bSHerbert Xu err = -EEXIST; 44202fd97c3SHerbert Xu if (key && rhashtable_lookup_fast(ht, key, ht->p)) 44302fd97c3SHerbert Xu goto exit; 44402fd97c3SHerbert Xu 445ccd57b1bSHerbert Xu err = -EAGAIN; 446ccd57b1bSHerbert Xu if (rhashtable_check_elasticity(ht, tbl, hash) || 447ccd57b1bSHerbert Xu rht_grow_above_100(ht, tbl)) 448ccd57b1bSHerbert Xu goto exit; 449ccd57b1bSHerbert Xu 45002fd97c3SHerbert Xu err = 0; 45102fd97c3SHerbert Xu 45202fd97c3SHerbert Xu head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); 45302fd97c3SHerbert Xu 45402fd97c3SHerbert Xu RCU_INIT_POINTER(obj->next, head); 45502fd97c3SHerbert Xu 45602fd97c3SHerbert Xu rcu_assign_pointer(tbl->buckets[hash], obj); 45702fd97c3SHerbert Xu 45802fd97c3SHerbert Xu atomic_inc(&ht->nelems); 45902fd97c3SHerbert Xu 46002fd97c3SHerbert Xu exit: 46102fd97c3SHerbert Xu spin_unlock(rht_bucket_lock(tbl, hash)); 46202fd97c3SHerbert Xu 46302fd97c3SHerbert Xu return err; 46402fd97c3SHerbert Xu } 46502fd97c3SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_insert_slow); 46602fd97c3SHerbert Xu 467f2dba9c6SHerbert Xu /** 468f2dba9c6SHerbert Xu * rhashtable_walk_init - Initialise an iterator 469f2dba9c6SHerbert Xu * @ht: Table to walk over 470f2dba9c6SHerbert Xu * @iter: Hash table Iterator 471f2dba9c6SHerbert Xu * 472f2dba9c6SHerbert Xu * This function prepares a hash table walk. 473f2dba9c6SHerbert Xu * 474f2dba9c6SHerbert Xu * Note that if you restart a walk after rhashtable_walk_stop you 475f2dba9c6SHerbert Xu * may see the same object twice. Also, you may miss objects if 476f2dba9c6SHerbert Xu * there are removals in between rhashtable_walk_stop and the next 477f2dba9c6SHerbert Xu * call to rhashtable_walk_start. 478f2dba9c6SHerbert Xu * 479f2dba9c6SHerbert Xu * For a completely stable walk you should construct your own data 480f2dba9c6SHerbert Xu * structure outside the hash table. 481f2dba9c6SHerbert Xu * 482f2dba9c6SHerbert Xu * This function may sleep so you must not call it from interrupt 483f2dba9c6SHerbert Xu * context or with spin locks held. 484f2dba9c6SHerbert Xu * 485f2dba9c6SHerbert Xu * You must call rhashtable_walk_exit if this function returns 486f2dba9c6SHerbert Xu * successfully. 487f2dba9c6SHerbert Xu */ 488f2dba9c6SHerbert Xu int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter) 489f2dba9c6SHerbert Xu { 490f2dba9c6SHerbert Xu iter->ht = ht; 491f2dba9c6SHerbert Xu iter->p = NULL; 492f2dba9c6SHerbert Xu iter->slot = 0; 493f2dba9c6SHerbert Xu iter->skip = 0; 494f2dba9c6SHerbert Xu 495f2dba9c6SHerbert Xu iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL); 496f2dba9c6SHerbert Xu if (!iter->walker) 497f2dba9c6SHerbert Xu return -ENOMEM; 498f2dba9c6SHerbert Xu 499f2dba9c6SHerbert Xu mutex_lock(&ht->mutex); 500eddee5baSHerbert Xu iter->walker->tbl = rht_dereference(ht->tbl, ht); 501eddee5baSHerbert Xu list_add(&iter->walker->list, &iter->walker->tbl->walkers); 502f2dba9c6SHerbert Xu mutex_unlock(&ht->mutex); 503f2dba9c6SHerbert Xu 504f2dba9c6SHerbert Xu return 0; 505f2dba9c6SHerbert Xu } 506f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_init); 507f2dba9c6SHerbert Xu 508f2dba9c6SHerbert Xu /** 509f2dba9c6SHerbert Xu * rhashtable_walk_exit - Free an iterator 510f2dba9c6SHerbert Xu * @iter: Hash table Iterator 511f2dba9c6SHerbert Xu * 512f2dba9c6SHerbert Xu * This function frees resources allocated by rhashtable_walk_init. 513f2dba9c6SHerbert Xu */ 514f2dba9c6SHerbert Xu void rhashtable_walk_exit(struct rhashtable_iter *iter) 515f2dba9c6SHerbert Xu { 516f2dba9c6SHerbert Xu mutex_lock(&iter->ht->mutex); 517eddee5baSHerbert Xu if (iter->walker->tbl) 518f2dba9c6SHerbert Xu list_del(&iter->walker->list); 519f2dba9c6SHerbert Xu mutex_unlock(&iter->ht->mutex); 520f2dba9c6SHerbert Xu kfree(iter->walker); 521f2dba9c6SHerbert Xu } 522f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_exit); 523f2dba9c6SHerbert Xu 524f2dba9c6SHerbert Xu /** 525f2dba9c6SHerbert Xu * rhashtable_walk_start - Start a hash table walk 526f2dba9c6SHerbert Xu * @iter: Hash table iterator 527f2dba9c6SHerbert Xu * 528f2dba9c6SHerbert Xu * Start a hash table walk. Note that we take the RCU lock in all 529f2dba9c6SHerbert Xu * cases including when we return an error. So you must always call 530f2dba9c6SHerbert Xu * rhashtable_walk_stop to clean up. 531f2dba9c6SHerbert Xu * 532f2dba9c6SHerbert Xu * Returns zero if successful. 533f2dba9c6SHerbert Xu * 534f2dba9c6SHerbert Xu * Returns -EAGAIN if resize event occured. Note that the iterator 535f2dba9c6SHerbert Xu * will rewind back to the beginning and you may use it immediately 536f2dba9c6SHerbert Xu * by calling rhashtable_walk_next. 537f2dba9c6SHerbert Xu */ 538f2dba9c6SHerbert Xu int rhashtable_walk_start(struct rhashtable_iter *iter) 539db4374f4SThomas Graf __acquires(RCU) 540f2dba9c6SHerbert Xu { 541eddee5baSHerbert Xu struct rhashtable *ht = iter->ht; 542eddee5baSHerbert Xu 543eddee5baSHerbert Xu mutex_lock(&ht->mutex); 544eddee5baSHerbert Xu 545eddee5baSHerbert Xu if (iter->walker->tbl) 546eddee5baSHerbert Xu list_del(&iter->walker->list); 547eddee5baSHerbert Xu 548f2dba9c6SHerbert Xu rcu_read_lock(); 549f2dba9c6SHerbert Xu 550eddee5baSHerbert Xu mutex_unlock(&ht->mutex); 551eddee5baSHerbert Xu 552eddee5baSHerbert Xu if (!iter->walker->tbl) { 553eddee5baSHerbert Xu iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht); 554f2dba9c6SHerbert Xu return -EAGAIN; 555f2dba9c6SHerbert Xu } 556f2dba9c6SHerbert Xu 557f2dba9c6SHerbert Xu return 0; 558f2dba9c6SHerbert Xu } 559f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_start); 560f2dba9c6SHerbert Xu 561f2dba9c6SHerbert Xu /** 562f2dba9c6SHerbert Xu * rhashtable_walk_next - Return the next object and advance the iterator 563f2dba9c6SHerbert Xu * @iter: Hash table iterator 564f2dba9c6SHerbert Xu * 565f2dba9c6SHerbert Xu * Note that you must call rhashtable_walk_stop when you are finished 566f2dba9c6SHerbert Xu * with the walk. 567f2dba9c6SHerbert Xu * 568f2dba9c6SHerbert Xu * Returns the next object or NULL when the end of the table is reached. 569f2dba9c6SHerbert Xu * 570f2dba9c6SHerbert Xu * Returns -EAGAIN if resize event occured. Note that the iterator 571f2dba9c6SHerbert Xu * will rewind back to the beginning and you may continue to use it. 572f2dba9c6SHerbert Xu */ 573f2dba9c6SHerbert Xu void *rhashtable_walk_next(struct rhashtable_iter *iter) 574f2dba9c6SHerbert Xu { 575eddee5baSHerbert Xu struct bucket_table *tbl = iter->walker->tbl; 576f2dba9c6SHerbert Xu struct rhashtable *ht = iter->ht; 577f2dba9c6SHerbert Xu struct rhash_head *p = iter->p; 578f2dba9c6SHerbert Xu void *obj = NULL; 579f2dba9c6SHerbert Xu 580f2dba9c6SHerbert Xu if (p) { 581f2dba9c6SHerbert Xu p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot); 582f2dba9c6SHerbert Xu goto next; 583f2dba9c6SHerbert Xu } 584f2dba9c6SHerbert Xu 585f2dba9c6SHerbert Xu for (; iter->slot < tbl->size; iter->slot++) { 586f2dba9c6SHerbert Xu int skip = iter->skip; 587f2dba9c6SHerbert Xu 588f2dba9c6SHerbert Xu rht_for_each_rcu(p, tbl, iter->slot) { 589f2dba9c6SHerbert Xu if (!skip) 590f2dba9c6SHerbert Xu break; 591f2dba9c6SHerbert Xu skip--; 592f2dba9c6SHerbert Xu } 593f2dba9c6SHerbert Xu 594f2dba9c6SHerbert Xu next: 595f2dba9c6SHerbert Xu if (!rht_is_a_nulls(p)) { 596f2dba9c6SHerbert Xu iter->skip++; 597f2dba9c6SHerbert Xu iter->p = p; 598f2dba9c6SHerbert Xu obj = rht_obj(ht, p); 599f2dba9c6SHerbert Xu goto out; 600f2dba9c6SHerbert Xu } 601f2dba9c6SHerbert Xu 602f2dba9c6SHerbert Xu iter->skip = 0; 603f2dba9c6SHerbert Xu } 604f2dba9c6SHerbert Xu 605d88252f9SHerbert Xu /* Ensure we see any new tables. */ 606d88252f9SHerbert Xu smp_rmb(); 607d88252f9SHerbert Xu 608c4db8848SHerbert Xu iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht); 609c4db8848SHerbert Xu if (iter->walker->tbl) { 610eddee5baSHerbert Xu iter->slot = 0; 611eddee5baSHerbert Xu iter->skip = 0; 612eddee5baSHerbert Xu return ERR_PTR(-EAGAIN); 613eddee5baSHerbert Xu } 614eddee5baSHerbert Xu 615f2dba9c6SHerbert Xu iter->p = NULL; 616f2dba9c6SHerbert Xu 617f2dba9c6SHerbert Xu out: 618f2dba9c6SHerbert Xu 619f2dba9c6SHerbert Xu return obj; 620f2dba9c6SHerbert Xu } 621f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_next); 622f2dba9c6SHerbert Xu 623f2dba9c6SHerbert Xu /** 624f2dba9c6SHerbert Xu * rhashtable_walk_stop - Finish a hash table walk 625f2dba9c6SHerbert Xu * @iter: Hash table iterator 626f2dba9c6SHerbert Xu * 627f2dba9c6SHerbert Xu * Finish a hash table walk. 628f2dba9c6SHerbert Xu */ 629f2dba9c6SHerbert Xu void rhashtable_walk_stop(struct rhashtable_iter *iter) 630db4374f4SThomas Graf __releases(RCU) 631f2dba9c6SHerbert Xu { 632eddee5baSHerbert Xu struct rhashtable *ht; 633eddee5baSHerbert Xu struct bucket_table *tbl = iter->walker->tbl; 634eddee5baSHerbert Xu 635eddee5baSHerbert Xu if (!tbl) 636963ecbd4SHerbert Xu goto out; 637eddee5baSHerbert Xu 638eddee5baSHerbert Xu ht = iter->ht; 639eddee5baSHerbert Xu 640ba7c95eaSHerbert Xu spin_lock(&ht->lock); 641c4db8848SHerbert Xu if (tbl->rehash < tbl->size) 642eddee5baSHerbert Xu list_add(&iter->walker->list, &tbl->walkers); 643eddee5baSHerbert Xu else 644eddee5baSHerbert Xu iter->walker->tbl = NULL; 645ba7c95eaSHerbert Xu spin_unlock(&ht->lock); 646eddee5baSHerbert Xu 647f2dba9c6SHerbert Xu iter->p = NULL; 648963ecbd4SHerbert Xu 649963ecbd4SHerbert Xu out: 650963ecbd4SHerbert Xu rcu_read_unlock(); 651f2dba9c6SHerbert Xu } 652f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_stop); 653f2dba9c6SHerbert Xu 654488fb86eSHerbert Xu static size_t rounded_hashtable_size(const struct rhashtable_params *params) 6557e1e7763SThomas Graf { 65694000176SYing Xue return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), 657e2e21c1cSHerbert Xu (unsigned long)params->min_size); 6587e1e7763SThomas Graf } 6597e1e7763SThomas Graf 66031ccde2dSHerbert Xu static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) 66131ccde2dSHerbert Xu { 66231ccde2dSHerbert Xu return jhash2(key, length, seed); 66331ccde2dSHerbert Xu } 66431ccde2dSHerbert Xu 6657e1e7763SThomas Graf /** 6667e1e7763SThomas Graf * rhashtable_init - initialize a new hash table 6677e1e7763SThomas Graf * @ht: hash table to be initialized 6687e1e7763SThomas Graf * @params: configuration parameters 6697e1e7763SThomas Graf * 6707e1e7763SThomas Graf * Initializes a new hash table based on the provided configuration 6717e1e7763SThomas Graf * parameters. A table can be configured either with a variable or 6727e1e7763SThomas Graf * fixed length key: 6737e1e7763SThomas Graf * 6747e1e7763SThomas Graf * Configuration Example 1: Fixed length keys 6757e1e7763SThomas Graf * struct test_obj { 6767e1e7763SThomas Graf * int key; 6777e1e7763SThomas Graf * void * my_member; 6787e1e7763SThomas Graf * struct rhash_head node; 6797e1e7763SThomas Graf * }; 6807e1e7763SThomas Graf * 6817e1e7763SThomas Graf * struct rhashtable_params params = { 6827e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 6837e1e7763SThomas Graf * .key_offset = offsetof(struct test_obj, key), 6847e1e7763SThomas Graf * .key_len = sizeof(int), 68587545899SDaniel Borkmann * .hashfn = jhash, 686f89bd6f8SThomas Graf * .nulls_base = (1U << RHT_BASE_SHIFT), 6877e1e7763SThomas Graf * }; 6887e1e7763SThomas Graf * 6897e1e7763SThomas Graf * Configuration Example 2: Variable length keys 6907e1e7763SThomas Graf * struct test_obj { 6917e1e7763SThomas Graf * [...] 6927e1e7763SThomas Graf * struct rhash_head node; 6937e1e7763SThomas Graf * }; 6947e1e7763SThomas Graf * 6957e1e7763SThomas Graf * u32 my_hash_fn(const void *data, u32 seed) 6967e1e7763SThomas Graf * { 6977e1e7763SThomas Graf * struct test_obj *obj = data; 6987e1e7763SThomas Graf * 6997e1e7763SThomas Graf * return [... hash ...]; 7007e1e7763SThomas Graf * } 7017e1e7763SThomas Graf * 7027e1e7763SThomas Graf * struct rhashtable_params params = { 7037e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 70487545899SDaniel Borkmann * .hashfn = jhash, 7057e1e7763SThomas Graf * .obj_hashfn = my_hash_fn, 7067e1e7763SThomas Graf * }; 7077e1e7763SThomas Graf */ 708488fb86eSHerbert Xu int rhashtable_init(struct rhashtable *ht, 709488fb86eSHerbert Xu const struct rhashtable_params *params) 7107e1e7763SThomas Graf { 7117e1e7763SThomas Graf struct bucket_table *tbl; 7127e1e7763SThomas Graf size_t size; 7137e1e7763SThomas Graf 7147e1e7763SThomas Graf size = HASH_DEFAULT_SIZE; 7157e1e7763SThomas Graf 71631ccde2dSHerbert Xu if ((!params->key_len && !params->obj_hashfn) || 71702fd97c3SHerbert Xu (params->obj_hashfn && !params->obj_cmpfn)) 7187e1e7763SThomas Graf return -EINVAL; 7197e1e7763SThomas Graf 720f89bd6f8SThomas Graf if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) 721f89bd6f8SThomas Graf return -EINVAL; 722f89bd6f8SThomas Graf 7237e1e7763SThomas Graf if (params->nelem_hint) 72494000176SYing Xue size = rounded_hashtable_size(params); 7257e1e7763SThomas Graf 72697defe1eSThomas Graf memset(ht, 0, sizeof(*ht)); 72797defe1eSThomas Graf mutex_init(&ht->mutex); 728ba7c95eaSHerbert Xu spin_lock_init(&ht->lock); 72997defe1eSThomas Graf memcpy(&ht->p, params, sizeof(*params)); 73097defe1eSThomas Graf 731a998f712SThomas Graf if (params->min_size) 732a998f712SThomas Graf ht->p.min_size = roundup_pow_of_two(params->min_size); 733a998f712SThomas Graf 734a998f712SThomas Graf if (params->max_size) 735a998f712SThomas Graf ht->p.max_size = rounddown_pow_of_two(params->max_size); 736a998f712SThomas Graf 737488fb86eSHerbert Xu ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); 738a998f712SThomas Graf 739*27ed44a5SHerbert Xu /* The maximum (not average) chain length grows with the 740*27ed44a5SHerbert Xu * size of the hash table, at a rate of (log N)/(log log N). 741*27ed44a5SHerbert Xu * The value of 16 is selected so that even if the hash 742*27ed44a5SHerbert Xu * table grew to 2^32 you would not expect the maximum 743*27ed44a5SHerbert Xu * chain length to exceed it unless we are under attack 744*27ed44a5SHerbert Xu * (or extremely unlucky). 745*27ed44a5SHerbert Xu * 746*27ed44a5SHerbert Xu * As this limit is only to detect attacks, we don't need 747*27ed44a5SHerbert Xu * to set it to a lower value as you'd need the chain 748*27ed44a5SHerbert Xu * length to vastly exceed 16 to have any real effect 749*27ed44a5SHerbert Xu * on the system. 750*27ed44a5SHerbert Xu */ 751ccd57b1bSHerbert Xu if (!params->insecure_elasticity) 752ccd57b1bSHerbert Xu ht->elasticity = 16; 753ccd57b1bSHerbert Xu 75497defe1eSThomas Graf if (params->locks_mul) 75597defe1eSThomas Graf ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); 75697defe1eSThomas Graf else 75797defe1eSThomas Graf ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; 75897defe1eSThomas Graf 75931ccde2dSHerbert Xu ht->key_len = ht->p.key_len; 76031ccde2dSHerbert Xu if (!params->hashfn) { 76131ccde2dSHerbert Xu ht->p.hashfn = jhash; 76231ccde2dSHerbert Xu 76331ccde2dSHerbert Xu if (!(ht->key_len & (sizeof(u32) - 1))) { 76431ccde2dSHerbert Xu ht->key_len /= sizeof(u32); 76531ccde2dSHerbert Xu ht->p.hashfn = rhashtable_jhash2; 76631ccde2dSHerbert Xu } 76731ccde2dSHerbert Xu } 76831ccde2dSHerbert Xu 769b9ecfdaaSHerbert Xu tbl = bucket_table_alloc(ht, size, GFP_KERNEL); 7707e1e7763SThomas Graf if (tbl == NULL) 7717e1e7763SThomas Graf return -ENOMEM; 7727e1e7763SThomas Graf 773545a148eSYing Xue atomic_set(&ht->nelems, 0); 774a5b6846fSDaniel Borkmann 7757e1e7763SThomas Graf RCU_INIT_POINTER(ht->tbl, tbl); 7767e1e7763SThomas Graf 77757699a40SYing Xue INIT_WORK(&ht->run_work, rht_deferred_worker); 77897defe1eSThomas Graf 7797e1e7763SThomas Graf return 0; 7807e1e7763SThomas Graf } 7817e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init); 7827e1e7763SThomas Graf 7837e1e7763SThomas Graf /** 7847e1e7763SThomas Graf * rhashtable_destroy - destroy hash table 7857e1e7763SThomas Graf * @ht: the hash table to destroy 7867e1e7763SThomas Graf * 787ae82ddcfSPablo Neira Ayuso * Frees the bucket array. This function is not rcu safe, therefore the caller 788ae82ddcfSPablo Neira Ayuso * has to make sure that no resizing may happen by unpublishing the hashtable 789ae82ddcfSPablo Neira Ayuso * and waiting for the quiescent cycle before releasing the bucket array. 7907e1e7763SThomas Graf */ 79197defe1eSThomas Graf void rhashtable_destroy(struct rhashtable *ht) 7927e1e7763SThomas Graf { 79397defe1eSThomas Graf ht->being_destroyed = true; 79497defe1eSThomas Graf 79557699a40SYing Xue cancel_work_sync(&ht->run_work); 79657699a40SYing Xue 79797defe1eSThomas Graf mutex_lock(&ht->mutex); 79897defe1eSThomas Graf bucket_table_free(rht_dereference(ht->tbl, ht)); 79997defe1eSThomas Graf mutex_unlock(&ht->mutex); 8007e1e7763SThomas Graf } 8017e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy); 802