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 1707ee0722SHerbert Xu #include <linux/atomic.h> 187e1e7763SThomas Graf #include <linux/kernel.h> 197e1e7763SThomas Graf #include <linux/init.h> 207e1e7763SThomas Graf #include <linux/log2.h> 215beb5c90SEric Dumazet #include <linux/sched.h> 22b2d09103SIngo Molnar #include <linux/rculist.h> 237e1e7763SThomas Graf #include <linux/slab.h> 247e1e7763SThomas Graf #include <linux/vmalloc.h> 257e1e7763SThomas Graf #include <linux/mm.h> 2687545899SDaniel Borkmann #include <linux/jhash.h> 277e1e7763SThomas Graf #include <linux/random.h> 287e1e7763SThomas Graf #include <linux/rhashtable.h> 2961d7b097SStephen Rothwell #include <linux/err.h> 306d795413SHauke Mehrtens #include <linux/export.h> 317e1e7763SThomas Graf 327e1e7763SThomas Graf #define HASH_DEFAULT_SIZE 64UL 33c2e213cfSHerbert Xu #define HASH_MIN_SIZE 4U 344cf0b354SFlorian Westphal #define BUCKET_LOCKS_PER_CPU 32UL 3597defe1eSThomas Graf 36da20420fSHerbert Xu union nested_table { 37da20420fSHerbert Xu union nested_table __rcu *table; 38da20420fSHerbert Xu struct rhash_head __rcu *bucket; 39da20420fSHerbert Xu }; 40da20420fSHerbert Xu 41988dfbd7SHerbert Xu static u32 head_hashfn(struct rhashtable *ht, 428d24c0b4SThomas Graf const struct bucket_table *tbl, 438d24c0b4SThomas Graf const struct rhash_head *he) 447e1e7763SThomas Graf { 4502fd97c3SHerbert Xu return rht_head_hashfn(ht, tbl, he, ht->p); 467e1e7763SThomas Graf } 477e1e7763SThomas Graf 48a03eaec0SThomas Graf #ifdef CONFIG_PROVE_LOCKING 49a03eaec0SThomas Graf #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) 50a03eaec0SThomas Graf 51a03eaec0SThomas Graf int lockdep_rht_mutex_is_held(struct rhashtable *ht) 52a03eaec0SThomas Graf { 53a03eaec0SThomas Graf return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; 54a03eaec0SThomas Graf } 55a03eaec0SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); 56a03eaec0SThomas Graf 57a03eaec0SThomas Graf int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) 58a03eaec0SThomas Graf { 5902fd97c3SHerbert Xu spinlock_t *lock = rht_bucket_lock(tbl, hash); 60a03eaec0SThomas Graf 61a03eaec0SThomas Graf return (debug_locks) ? lockdep_is_held(lock) : 1; 62a03eaec0SThomas Graf } 63a03eaec0SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); 64a03eaec0SThomas Graf #else 65a03eaec0SThomas Graf #define ASSERT_RHT_MUTEX(HT) 66a03eaec0SThomas Graf #endif 67a03eaec0SThomas Graf 68da20420fSHerbert Xu static void nested_table_free(union nested_table *ntbl, unsigned int size) 69da20420fSHerbert Xu { 70da20420fSHerbert Xu const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); 71da20420fSHerbert Xu const unsigned int len = 1 << shift; 72da20420fSHerbert Xu unsigned int i; 73da20420fSHerbert Xu 74da20420fSHerbert Xu ntbl = rcu_dereference_raw(ntbl->table); 75da20420fSHerbert Xu if (!ntbl) 76da20420fSHerbert Xu return; 77da20420fSHerbert Xu 78da20420fSHerbert Xu if (size > len) { 79da20420fSHerbert Xu size >>= shift; 80da20420fSHerbert Xu for (i = 0; i < len; i++) 81da20420fSHerbert Xu nested_table_free(ntbl + i, size); 82da20420fSHerbert Xu } 83da20420fSHerbert Xu 84da20420fSHerbert Xu kfree(ntbl); 85da20420fSHerbert Xu } 86da20420fSHerbert Xu 87da20420fSHerbert Xu static void nested_bucket_table_free(const struct bucket_table *tbl) 88da20420fSHerbert Xu { 89da20420fSHerbert Xu unsigned int size = tbl->size >> tbl->nest; 90da20420fSHerbert Xu unsigned int len = 1 << tbl->nest; 91da20420fSHerbert Xu union nested_table *ntbl; 92da20420fSHerbert Xu unsigned int i; 93da20420fSHerbert Xu 94da20420fSHerbert Xu ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); 95da20420fSHerbert Xu 96da20420fSHerbert Xu for (i = 0; i < len; i++) 97da20420fSHerbert Xu nested_table_free(ntbl + i, size); 98da20420fSHerbert Xu 99da20420fSHerbert Xu kfree(ntbl); 100da20420fSHerbert Xu } 101da20420fSHerbert Xu 10297defe1eSThomas Graf static void bucket_table_free(const struct bucket_table *tbl) 10397defe1eSThomas Graf { 104da20420fSHerbert Xu if (tbl->nest) 105da20420fSHerbert Xu nested_bucket_table_free(tbl); 106da20420fSHerbert Xu 10764e0cd0dSTom Herbert free_bucket_spinlocks(tbl->locks); 10897defe1eSThomas Graf kvfree(tbl); 10997defe1eSThomas Graf } 11097defe1eSThomas Graf 1119d901bc0SHerbert Xu static void bucket_table_free_rcu(struct rcu_head *head) 1129d901bc0SHerbert Xu { 1139d901bc0SHerbert Xu bucket_table_free(container_of(head, struct bucket_table, rcu)); 1149d901bc0SHerbert Xu } 1159d901bc0SHerbert Xu 116da20420fSHerbert Xu static union nested_table *nested_table_alloc(struct rhashtable *ht, 117da20420fSHerbert Xu union nested_table __rcu **prev, 118da20420fSHerbert Xu unsigned int shifted, 119da20420fSHerbert Xu unsigned int nhash) 120da20420fSHerbert Xu { 121da20420fSHerbert Xu union nested_table *ntbl; 122da20420fSHerbert Xu int i; 123da20420fSHerbert Xu 124da20420fSHerbert Xu ntbl = rcu_dereference(*prev); 125da20420fSHerbert Xu if (ntbl) 126da20420fSHerbert Xu return ntbl; 127da20420fSHerbert Xu 128da20420fSHerbert Xu ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC); 129da20420fSHerbert Xu 130da20420fSHerbert Xu if (ntbl && shifted) { 131da20420fSHerbert Xu for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0].bucket); i++) 132da20420fSHerbert Xu INIT_RHT_NULLS_HEAD(ntbl[i].bucket, ht, 133da20420fSHerbert Xu (i << shifted) | nhash); 134da20420fSHerbert Xu } 135da20420fSHerbert Xu 136da20420fSHerbert Xu rcu_assign_pointer(*prev, ntbl); 137da20420fSHerbert Xu 138da20420fSHerbert Xu return ntbl; 139da20420fSHerbert Xu } 140da20420fSHerbert Xu 141da20420fSHerbert Xu static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht, 142da20420fSHerbert Xu size_t nbuckets, 143da20420fSHerbert Xu gfp_t gfp) 144da20420fSHerbert Xu { 145da20420fSHerbert Xu const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); 146da20420fSHerbert Xu struct bucket_table *tbl; 147da20420fSHerbert Xu size_t size; 148da20420fSHerbert Xu 149da20420fSHerbert Xu if (nbuckets < (1 << (shift + 1))) 150da20420fSHerbert Xu return NULL; 151da20420fSHerbert Xu 152da20420fSHerbert Xu size = sizeof(*tbl) + sizeof(tbl->buckets[0]); 153da20420fSHerbert Xu 154da20420fSHerbert Xu tbl = kzalloc(size, gfp); 155da20420fSHerbert Xu if (!tbl) 156da20420fSHerbert Xu return NULL; 157da20420fSHerbert Xu 158da20420fSHerbert Xu if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets, 159da20420fSHerbert Xu 0, 0)) { 160da20420fSHerbert Xu kfree(tbl); 161da20420fSHerbert Xu return NULL; 162da20420fSHerbert Xu } 163da20420fSHerbert Xu 164da20420fSHerbert Xu tbl->nest = (ilog2(nbuckets) - 1) % shift + 1; 165da20420fSHerbert Xu 166da20420fSHerbert Xu return tbl; 167da20420fSHerbert Xu } 168da20420fSHerbert Xu 16997defe1eSThomas Graf static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, 170b9ecfdaaSHerbert Xu size_t nbuckets, 171b9ecfdaaSHerbert Xu gfp_t gfp) 1727e1e7763SThomas Graf { 173eb6d1abfSDaniel Borkmann struct bucket_table *tbl = NULL; 17464e0cd0dSTom Herbert size_t size, max_locks; 175f89bd6f8SThomas Graf int i; 1767e1e7763SThomas Graf 1777e1e7763SThomas Graf size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); 17812e8fd6fSMichal Hocko if (gfp != GFP_KERNEL) 179b9ecfdaaSHerbert Xu tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY); 18012e8fd6fSMichal Hocko else 18112e8fd6fSMichal Hocko tbl = kvzalloc(size, gfp); 182da20420fSHerbert Xu 183da20420fSHerbert Xu size = nbuckets; 184da20420fSHerbert Xu 185da20420fSHerbert Xu if (tbl == NULL && gfp != GFP_KERNEL) { 186da20420fSHerbert Xu tbl = nested_bucket_table_alloc(ht, nbuckets, gfp); 187da20420fSHerbert Xu nbuckets = 0; 188da20420fSHerbert Xu } 1897e1e7763SThomas Graf if (tbl == NULL) 1907e1e7763SThomas Graf return NULL; 1917e1e7763SThomas Graf 192da20420fSHerbert Xu tbl->size = size; 1937e1e7763SThomas Graf 19464e0cd0dSTom Herbert max_locks = size >> 1; 19564e0cd0dSTom Herbert if (tbl->nest) 19664e0cd0dSTom Herbert max_locks = min_t(size_t, max_locks, 1U << tbl->nest); 19764e0cd0dSTom Herbert 19864e0cd0dSTom Herbert if (alloc_bucket_spinlocks(&tbl->locks, &tbl->locks_mask, max_locks, 19964e0cd0dSTom Herbert ht->p.locks_mul, gfp) < 0) { 20097defe1eSThomas Graf bucket_table_free(tbl); 20197defe1eSThomas Graf return NULL; 2027e1e7763SThomas Graf } 2037e1e7763SThomas Graf 204eddee5baSHerbert Xu INIT_LIST_HEAD(&tbl->walkers); 205eddee5baSHerbert Xu 206d48ad080SJason A. Donenfeld tbl->hash_rnd = get_random_u32(); 2075269b53dSHerbert Xu 208f89bd6f8SThomas Graf for (i = 0; i < nbuckets; i++) 209f89bd6f8SThomas Graf INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); 210f89bd6f8SThomas Graf 21197defe1eSThomas Graf return tbl; 2127e1e7763SThomas Graf } 2137e1e7763SThomas Graf 214b824478bSHerbert Xu static struct bucket_table *rhashtable_last_table(struct rhashtable *ht, 215b824478bSHerbert Xu struct bucket_table *tbl) 216b824478bSHerbert Xu { 217b824478bSHerbert Xu struct bucket_table *new_tbl; 218b824478bSHerbert Xu 219b824478bSHerbert Xu do { 220b824478bSHerbert Xu new_tbl = tbl; 221b824478bSHerbert Xu tbl = rht_dereference_rcu(tbl->future_tbl, ht); 222b824478bSHerbert Xu } while (tbl); 223b824478bSHerbert Xu 224b824478bSHerbert Xu return new_tbl; 225b824478bSHerbert Xu } 226b824478bSHerbert Xu 227299e5c32SThomas Graf static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash) 228a5ec68e3SThomas Graf { 229aa34a6cbSHerbert Xu struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); 230b824478bSHerbert Xu struct bucket_table *new_tbl = rhashtable_last_table(ht, 231b824478bSHerbert Xu rht_dereference_rcu(old_tbl->future_tbl, ht)); 232da20420fSHerbert Xu struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash); 233da20420fSHerbert Xu int err = -EAGAIN; 234aa34a6cbSHerbert Xu struct rhash_head *head, *next, *entry; 235aa34a6cbSHerbert Xu spinlock_t *new_bucket_lock; 236299e5c32SThomas Graf unsigned int new_hash; 237a5ec68e3SThomas Graf 238da20420fSHerbert Xu if (new_tbl->nest) 239da20420fSHerbert Xu goto out; 240da20420fSHerbert Xu 241da20420fSHerbert Xu err = -ENOENT; 242da20420fSHerbert Xu 243aa34a6cbSHerbert Xu rht_for_each(entry, old_tbl, old_hash) { 244aa34a6cbSHerbert Xu err = 0; 245aa34a6cbSHerbert Xu next = rht_dereference_bucket(entry->next, old_tbl, old_hash); 246a5ec68e3SThomas Graf 247aa34a6cbSHerbert Xu if (rht_is_a_nulls(next)) 2487e1e7763SThomas Graf break; 24997defe1eSThomas Graf 250aa34a6cbSHerbert Xu pprev = &entry->next; 2517e1e7763SThomas Graf } 2527e1e7763SThomas Graf 253aa34a6cbSHerbert Xu if (err) 254aa34a6cbSHerbert Xu goto out; 25597defe1eSThomas Graf 256aa34a6cbSHerbert Xu new_hash = head_hashfn(ht, new_tbl, entry); 257a5ec68e3SThomas Graf 25802fd97c3SHerbert Xu new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); 259aa34a6cbSHerbert Xu 2608f2484bdSHerbert Xu spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); 261aa34a6cbSHerbert Xu head = rht_dereference_bucket(new_tbl->buckets[new_hash], 262aa34a6cbSHerbert Xu new_tbl, new_hash); 263aa34a6cbSHerbert Xu 264aa34a6cbSHerbert Xu RCU_INIT_POINTER(entry->next, head); 265aa34a6cbSHerbert Xu 266aa34a6cbSHerbert Xu rcu_assign_pointer(new_tbl->buckets[new_hash], entry); 267aa34a6cbSHerbert Xu spin_unlock(new_bucket_lock); 268aa34a6cbSHerbert Xu 269aa34a6cbSHerbert Xu rcu_assign_pointer(*pprev, next); 270aa34a6cbSHerbert Xu 271aa34a6cbSHerbert Xu out: 272aa34a6cbSHerbert Xu return err; 27397defe1eSThomas Graf } 27497defe1eSThomas Graf 275da20420fSHerbert Xu static int rhashtable_rehash_chain(struct rhashtable *ht, 276299e5c32SThomas Graf unsigned int old_hash) 27797defe1eSThomas Graf { 278aa34a6cbSHerbert Xu struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); 279aa34a6cbSHerbert Xu spinlock_t *old_bucket_lock; 280da20420fSHerbert Xu int err; 2817cd10db8SThomas Graf 28202fd97c3SHerbert Xu old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); 283aa34a6cbSHerbert Xu 284aa34a6cbSHerbert Xu spin_lock_bh(old_bucket_lock); 285da20420fSHerbert Xu while (!(err = rhashtable_rehash_one(ht, old_hash))) 286aa34a6cbSHerbert Xu ; 287da20420fSHerbert Xu 288da20420fSHerbert Xu if (err == -ENOENT) { 28963d512d0SHerbert Xu old_tbl->rehash++; 290da20420fSHerbert Xu err = 0; 291da20420fSHerbert Xu } 292aa34a6cbSHerbert Xu spin_unlock_bh(old_bucket_lock); 293da20420fSHerbert Xu 294da20420fSHerbert Xu return err; 295aa34a6cbSHerbert Xu } 296aa34a6cbSHerbert Xu 297b824478bSHerbert Xu static int rhashtable_rehash_attach(struct rhashtable *ht, 298b824478bSHerbert Xu struct bucket_table *old_tbl, 299aa34a6cbSHerbert Xu struct bucket_table *new_tbl) 300aa34a6cbSHerbert Xu { 301b824478bSHerbert Xu /* Protect future_tbl using the first bucket lock. */ 302b824478bSHerbert Xu spin_lock_bh(old_tbl->locks); 303b824478bSHerbert Xu 304b824478bSHerbert Xu /* Did somebody beat us to it? */ 305b824478bSHerbert Xu if (rcu_access_pointer(old_tbl->future_tbl)) { 306b824478bSHerbert Xu spin_unlock_bh(old_tbl->locks); 307b824478bSHerbert Xu return -EEXIST; 308b824478bSHerbert Xu } 309aa34a6cbSHerbert Xu 310aa34a6cbSHerbert Xu /* Make insertions go into the new, empty table right away. Deletions 311aa34a6cbSHerbert Xu * and lookups will be attempted in both tables until we synchronize. 312aa34a6cbSHerbert Xu */ 313c4db8848SHerbert Xu rcu_assign_pointer(old_tbl->future_tbl, new_tbl); 314aa34a6cbSHerbert Xu 315b824478bSHerbert Xu spin_unlock_bh(old_tbl->locks); 316b824478bSHerbert Xu 317b824478bSHerbert Xu return 0; 318b824478bSHerbert Xu } 319b824478bSHerbert Xu 320b824478bSHerbert Xu static int rhashtable_rehash_table(struct rhashtable *ht) 321b824478bSHerbert Xu { 322b824478bSHerbert Xu struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); 323b824478bSHerbert Xu struct bucket_table *new_tbl; 324b824478bSHerbert Xu struct rhashtable_walker *walker; 325299e5c32SThomas Graf unsigned int old_hash; 326da20420fSHerbert Xu int err; 327b824478bSHerbert Xu 328b824478bSHerbert Xu new_tbl = rht_dereference(old_tbl->future_tbl, ht); 329b824478bSHerbert Xu if (!new_tbl) 330b824478bSHerbert Xu return 0; 331b824478bSHerbert Xu 332da20420fSHerbert Xu for (old_hash = 0; old_hash < old_tbl->size; old_hash++) { 333da20420fSHerbert Xu err = rhashtable_rehash_chain(ht, old_hash); 334da20420fSHerbert Xu if (err) 335da20420fSHerbert Xu return err; 336*ae6da1f5SEric Dumazet cond_resched(); 337da20420fSHerbert Xu } 338aa34a6cbSHerbert Xu 339aa34a6cbSHerbert Xu /* Publish the new table pointer. */ 340aa34a6cbSHerbert Xu rcu_assign_pointer(ht->tbl, new_tbl); 341aa34a6cbSHerbert Xu 342ba7c95eaSHerbert Xu spin_lock(&ht->lock); 343eddee5baSHerbert Xu list_for_each_entry(walker, &old_tbl->walkers, list) 344eddee5baSHerbert Xu walker->tbl = NULL; 345ba7c95eaSHerbert Xu spin_unlock(&ht->lock); 346eddee5baSHerbert Xu 347aa34a6cbSHerbert Xu /* Wait for readers. All new readers will see the new 348aa34a6cbSHerbert Xu * table, and thus no references to the old table will 349aa34a6cbSHerbert Xu * remain. 350aa34a6cbSHerbert Xu */ 3519d901bc0SHerbert Xu call_rcu(&old_tbl->rcu, bucket_table_free_rcu); 352b824478bSHerbert Xu 353b824478bSHerbert Xu return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0; 3547e1e7763SThomas Graf } 3557e1e7763SThomas Graf 356da20420fSHerbert Xu static int rhashtable_rehash_alloc(struct rhashtable *ht, 357da20420fSHerbert Xu struct bucket_table *old_tbl, 358da20420fSHerbert Xu unsigned int size) 3597e1e7763SThomas Graf { 360da20420fSHerbert Xu struct bucket_table *new_tbl; 361b824478bSHerbert Xu int err; 3627e1e7763SThomas Graf 3637e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 3647e1e7763SThomas Graf 365da20420fSHerbert Xu new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL); 3667e1e7763SThomas Graf if (new_tbl == NULL) 3677e1e7763SThomas Graf return -ENOMEM; 3687e1e7763SThomas Graf 369b824478bSHerbert Xu err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); 370b824478bSHerbert Xu if (err) 371b824478bSHerbert Xu bucket_table_free(new_tbl); 372b824478bSHerbert Xu 373b824478bSHerbert Xu return err; 3747e1e7763SThomas Graf } 3757e1e7763SThomas Graf 3767e1e7763SThomas Graf /** 3777e1e7763SThomas Graf * rhashtable_shrink - Shrink hash table while allowing concurrent lookups 3787e1e7763SThomas Graf * @ht: the hash table to shrink 3797e1e7763SThomas Graf * 38018093d1cSHerbert Xu * This function shrinks the hash table to fit, i.e., the smallest 38118093d1cSHerbert Xu * size would not cause it to expand right away automatically. 3827e1e7763SThomas Graf * 38397defe1eSThomas Graf * The caller must ensure that no concurrent resizing occurs by holding 38497defe1eSThomas Graf * ht->mutex. 38597defe1eSThomas Graf * 3867e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations take place. 3877e1e7763SThomas Graf * It is however valid to have concurrent lookups if they are RCU protected. 38897defe1eSThomas Graf * 38997defe1eSThomas Graf * It is valid to have concurrent insertions and deletions protected by per 39097defe1eSThomas Graf * bucket locks or concurrent RCU protected lookups and traversals. 3917e1e7763SThomas Graf */ 392b824478bSHerbert Xu static int rhashtable_shrink(struct rhashtable *ht) 3937e1e7763SThomas Graf { 394da20420fSHerbert Xu struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); 39512311959SVegard Nossum unsigned int nelems = atomic_read(&ht->nelems); 39612311959SVegard Nossum unsigned int size = 0; 3977e1e7763SThomas Graf 39812311959SVegard Nossum if (nelems) 39912311959SVegard Nossum size = roundup_pow_of_two(nelems * 3 / 2); 40018093d1cSHerbert Xu if (size < ht->p.min_size) 40118093d1cSHerbert Xu size = ht->p.min_size; 40218093d1cSHerbert Xu 40318093d1cSHerbert Xu if (old_tbl->size <= size) 40418093d1cSHerbert Xu return 0; 40518093d1cSHerbert Xu 406b824478bSHerbert Xu if (rht_dereference(old_tbl->future_tbl, ht)) 407b824478bSHerbert Xu return -EEXIST; 408b824478bSHerbert Xu 409da20420fSHerbert Xu return rhashtable_rehash_alloc(ht, old_tbl, size); 4107e1e7763SThomas Graf } 4117e1e7763SThomas Graf 41297defe1eSThomas Graf static void rht_deferred_worker(struct work_struct *work) 41397defe1eSThomas Graf { 41497defe1eSThomas Graf struct rhashtable *ht; 41597defe1eSThomas Graf struct bucket_table *tbl; 416b824478bSHerbert Xu int err = 0; 41797defe1eSThomas Graf 41857699a40SYing Xue ht = container_of(work, struct rhashtable, run_work); 41997defe1eSThomas Graf mutex_lock(&ht->mutex); 42028134a53SHerbert Xu 42197defe1eSThomas Graf tbl = rht_dereference(ht->tbl, ht); 422b824478bSHerbert Xu tbl = rhashtable_last_table(ht, tbl); 42397defe1eSThomas Graf 424a5b6846fSDaniel Borkmann if (rht_grow_above_75(ht, tbl)) 425da20420fSHerbert Xu err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2); 426b5e2c150SThomas Graf else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl)) 427da20420fSHerbert Xu err = rhashtable_shrink(ht); 428da20420fSHerbert Xu else if (tbl->nest) 429da20420fSHerbert Xu err = rhashtable_rehash_alloc(ht, tbl, tbl->size); 430b824478bSHerbert Xu 431da20420fSHerbert Xu if (!err) 432b824478bSHerbert Xu err = rhashtable_rehash_table(ht); 433b824478bSHerbert Xu 43497defe1eSThomas Graf mutex_unlock(&ht->mutex); 435b824478bSHerbert Xu 436b824478bSHerbert Xu if (err) 437b824478bSHerbert Xu schedule_work(&ht->run_work); 43897defe1eSThomas Graf } 43997defe1eSThomas Graf 440ca26893fSHerbert Xu static int rhashtable_insert_rehash(struct rhashtable *ht, 4413cf92222SHerbert Xu struct bucket_table *tbl) 442ccd57b1bSHerbert Xu { 443ccd57b1bSHerbert Xu struct bucket_table *old_tbl; 444ccd57b1bSHerbert Xu struct bucket_table *new_tbl; 445ccd57b1bSHerbert Xu unsigned int size; 446ccd57b1bSHerbert Xu int err; 447ccd57b1bSHerbert Xu 448ccd57b1bSHerbert Xu old_tbl = rht_dereference_rcu(ht->tbl, ht); 449ccd57b1bSHerbert Xu 450ccd57b1bSHerbert Xu size = tbl->size; 451ccd57b1bSHerbert Xu 4523cf92222SHerbert Xu err = -EBUSY; 4533cf92222SHerbert Xu 454ccd57b1bSHerbert Xu if (rht_grow_above_75(ht, tbl)) 455ccd57b1bSHerbert Xu size *= 2; 456a87b9ebfSThomas Graf /* Do not schedule more than one rehash */ 457a87b9ebfSThomas Graf else if (old_tbl != tbl) 4583cf92222SHerbert Xu goto fail; 4593cf92222SHerbert Xu 4603cf92222SHerbert Xu err = -ENOMEM; 461ccd57b1bSHerbert Xu 462ccd57b1bSHerbert Xu new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC); 4633cf92222SHerbert Xu if (new_tbl == NULL) 4643cf92222SHerbert Xu goto fail; 465ccd57b1bSHerbert Xu 466ccd57b1bSHerbert Xu err = rhashtable_rehash_attach(ht, tbl, new_tbl); 467ccd57b1bSHerbert Xu if (err) { 468ccd57b1bSHerbert Xu bucket_table_free(new_tbl); 469ccd57b1bSHerbert Xu if (err == -EEXIST) 470ccd57b1bSHerbert Xu err = 0; 471ccd57b1bSHerbert Xu } else 472ccd57b1bSHerbert Xu schedule_work(&ht->run_work); 473ccd57b1bSHerbert Xu 474ccd57b1bSHerbert Xu return err; 4753cf92222SHerbert Xu 4763cf92222SHerbert Xu fail: 4773cf92222SHerbert Xu /* Do not fail the insert if someone else did a rehash. */ 4783cf92222SHerbert Xu if (likely(rcu_dereference_raw(tbl->future_tbl))) 4793cf92222SHerbert Xu return 0; 4803cf92222SHerbert Xu 4813cf92222SHerbert Xu /* Schedule async rehash to retry allocation in process context. */ 4823cf92222SHerbert Xu if (err == -ENOMEM) 4833cf92222SHerbert Xu schedule_work(&ht->run_work); 4843cf92222SHerbert Xu 4853cf92222SHerbert Xu return err; 486ccd57b1bSHerbert Xu } 487ccd57b1bSHerbert Xu 488ca26893fSHerbert Xu static void *rhashtable_lookup_one(struct rhashtable *ht, 489ca26893fSHerbert Xu struct bucket_table *tbl, unsigned int hash, 490ca26893fSHerbert Xu const void *key, struct rhash_head *obj) 49102fd97c3SHerbert Xu { 492ca26893fSHerbert Xu struct rhashtable_compare_arg arg = { 493ca26893fSHerbert Xu .ht = ht, 494ca26893fSHerbert Xu .key = key, 495ca26893fSHerbert Xu }; 496ca26893fSHerbert Xu struct rhash_head __rcu **pprev; 49702fd97c3SHerbert Xu struct rhash_head *head; 498ca26893fSHerbert Xu int elasticity; 49902fd97c3SHerbert Xu 5005f8ddeabSFlorian Westphal elasticity = RHT_ELASTICITY; 501da20420fSHerbert Xu pprev = rht_bucket_var(tbl, hash); 502da20420fSHerbert Xu rht_for_each_continue(head, *pprev, tbl, hash) { 503ca26893fSHerbert Xu struct rhlist_head *list; 504ca26893fSHerbert Xu struct rhlist_head *plist; 50502fd97c3SHerbert Xu 506ca26893fSHerbert Xu elasticity--; 507ca26893fSHerbert Xu if (!key || 508ca26893fSHerbert Xu (ht->p.obj_cmpfn ? 509ca26893fSHerbert Xu ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) : 510d3dcf8ebSPaul Blakey rhashtable_compare(&arg, rht_obj(ht, head)))) { 511d3dcf8ebSPaul Blakey pprev = &head->next; 512ca26893fSHerbert Xu continue; 513d3dcf8ebSPaul Blakey } 514ca26893fSHerbert Xu 515ca26893fSHerbert Xu if (!ht->rhlist) 516ca26893fSHerbert Xu return rht_obj(ht, head); 517ca26893fSHerbert Xu 518ca26893fSHerbert Xu list = container_of(obj, struct rhlist_head, rhead); 519ca26893fSHerbert Xu plist = container_of(head, struct rhlist_head, rhead); 520ca26893fSHerbert Xu 521ca26893fSHerbert Xu RCU_INIT_POINTER(list->next, plist); 522ca26893fSHerbert Xu head = rht_dereference_bucket(head->next, tbl, hash); 523ca26893fSHerbert Xu RCU_INIT_POINTER(list->rhead.next, head); 524ca26893fSHerbert Xu rcu_assign_pointer(*pprev, obj); 525ca26893fSHerbert Xu 526ca26893fSHerbert Xu return NULL; 5275ca8cc5bSPablo Neira Ayuso } 52802fd97c3SHerbert Xu 529ca26893fSHerbert Xu if (elasticity <= 0) 530ca26893fSHerbert Xu return ERR_PTR(-EAGAIN); 531ca26893fSHerbert Xu 532ca26893fSHerbert Xu return ERR_PTR(-ENOENT); 533ca26893fSHerbert Xu } 534ca26893fSHerbert Xu 535ca26893fSHerbert Xu static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht, 536ca26893fSHerbert Xu struct bucket_table *tbl, 537ca26893fSHerbert Xu unsigned int hash, 538ca26893fSHerbert Xu struct rhash_head *obj, 539ca26893fSHerbert Xu void *data) 540ca26893fSHerbert Xu { 541da20420fSHerbert Xu struct rhash_head __rcu **pprev; 542ca26893fSHerbert Xu struct bucket_table *new_tbl; 543ca26893fSHerbert Xu struct rhash_head *head; 544ca26893fSHerbert Xu 545ca26893fSHerbert Xu if (!IS_ERR_OR_NULL(data)) 546ca26893fSHerbert Xu return ERR_PTR(-EEXIST); 547ca26893fSHerbert Xu 548ca26893fSHerbert Xu if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT) 549ca26893fSHerbert Xu return ERR_CAST(data); 550ca26893fSHerbert Xu 551ca26893fSHerbert Xu new_tbl = rcu_dereference(tbl->future_tbl); 552ca26893fSHerbert Xu if (new_tbl) 553ca26893fSHerbert Xu return new_tbl; 554ca26893fSHerbert Xu 555ca26893fSHerbert Xu if (PTR_ERR(data) != -ENOENT) 556ca26893fSHerbert Xu return ERR_CAST(data); 557ca26893fSHerbert Xu 55807ee0722SHerbert Xu if (unlikely(rht_grow_above_max(ht, tbl))) 559ca26893fSHerbert Xu return ERR_PTR(-E2BIG); 56007ee0722SHerbert Xu 561ca26893fSHerbert Xu if (unlikely(rht_grow_above_100(ht, tbl))) 562ca26893fSHerbert Xu return ERR_PTR(-EAGAIN); 56302fd97c3SHerbert Xu 564da20420fSHerbert Xu pprev = rht_bucket_insert(ht, tbl, hash); 565da20420fSHerbert Xu if (!pprev) 566da20420fSHerbert Xu return ERR_PTR(-ENOMEM); 567da20420fSHerbert Xu 568da20420fSHerbert Xu head = rht_dereference_bucket(*pprev, tbl, hash); 56902fd97c3SHerbert Xu 57002fd97c3SHerbert Xu RCU_INIT_POINTER(obj->next, head); 571ca26893fSHerbert Xu if (ht->rhlist) { 572ca26893fSHerbert Xu struct rhlist_head *list; 573ca26893fSHerbert Xu 574ca26893fSHerbert Xu list = container_of(obj, struct rhlist_head, rhead); 575ca26893fSHerbert Xu RCU_INIT_POINTER(list->next, NULL); 576ca26893fSHerbert Xu } 57702fd97c3SHerbert Xu 578da20420fSHerbert Xu rcu_assign_pointer(*pprev, obj); 57902fd97c3SHerbert Xu 58002fd97c3SHerbert Xu atomic_inc(&ht->nelems); 581ca26893fSHerbert Xu if (rht_grow_above_75(ht, tbl)) 582ca26893fSHerbert Xu schedule_work(&ht->run_work); 58302fd97c3SHerbert Xu 5843cf92222SHerbert Xu return NULL; 585ca26893fSHerbert Xu } 586ca26893fSHerbert Xu 587ca26893fSHerbert Xu static void *rhashtable_try_insert(struct rhashtable *ht, const void *key, 588ca26893fSHerbert Xu struct rhash_head *obj) 589ca26893fSHerbert Xu { 590ca26893fSHerbert Xu struct bucket_table *new_tbl; 591ca26893fSHerbert Xu struct bucket_table *tbl; 592ca26893fSHerbert Xu unsigned int hash; 593ca26893fSHerbert Xu spinlock_t *lock; 594ca26893fSHerbert Xu void *data; 595ca26893fSHerbert Xu 596ca26893fSHerbert Xu tbl = rcu_dereference(ht->tbl); 597ca26893fSHerbert Xu 598ca26893fSHerbert Xu /* All insertions must grab the oldest table containing 599ca26893fSHerbert Xu * the hashed bucket that is yet to be rehashed. 600ca26893fSHerbert Xu */ 601ca26893fSHerbert Xu for (;;) { 602ca26893fSHerbert Xu hash = rht_head_hashfn(ht, tbl, obj, ht->p); 603ca26893fSHerbert Xu lock = rht_bucket_lock(tbl, hash); 604ca26893fSHerbert Xu spin_lock_bh(lock); 605ca26893fSHerbert Xu 606ca26893fSHerbert Xu if (tbl->rehash <= hash) 607ca26893fSHerbert Xu break; 608ca26893fSHerbert Xu 609ca26893fSHerbert Xu spin_unlock_bh(lock); 610ca26893fSHerbert Xu tbl = rcu_dereference(tbl->future_tbl); 611ca26893fSHerbert Xu } 612ca26893fSHerbert Xu 613ca26893fSHerbert Xu data = rhashtable_lookup_one(ht, tbl, hash, key, obj); 614ca26893fSHerbert Xu new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data); 615ca26893fSHerbert Xu if (PTR_ERR(new_tbl) != -EEXIST) 616ca26893fSHerbert Xu data = ERR_CAST(new_tbl); 617ca26893fSHerbert Xu 618ca26893fSHerbert Xu while (!IS_ERR_OR_NULL(new_tbl)) { 619ca26893fSHerbert Xu tbl = new_tbl; 620ca26893fSHerbert Xu hash = rht_head_hashfn(ht, tbl, obj, ht->p); 621ca26893fSHerbert Xu spin_lock_nested(rht_bucket_lock(tbl, hash), 622ca26893fSHerbert Xu SINGLE_DEPTH_NESTING); 623ca26893fSHerbert Xu 624ca26893fSHerbert Xu data = rhashtable_lookup_one(ht, tbl, hash, key, obj); 625ca26893fSHerbert Xu new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data); 626ca26893fSHerbert Xu if (PTR_ERR(new_tbl) != -EEXIST) 627ca26893fSHerbert Xu data = ERR_CAST(new_tbl); 628ca26893fSHerbert Xu 629ca26893fSHerbert Xu spin_unlock(rht_bucket_lock(tbl, hash)); 630ca26893fSHerbert Xu } 631ca26893fSHerbert Xu 632ca26893fSHerbert Xu spin_unlock_bh(lock); 633ca26893fSHerbert Xu 634ca26893fSHerbert Xu if (PTR_ERR(data) == -EAGAIN) 635ca26893fSHerbert Xu data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?: 636ca26893fSHerbert Xu -EAGAIN); 637ca26893fSHerbert Xu 638ca26893fSHerbert Xu return data; 639ca26893fSHerbert Xu } 640ca26893fSHerbert Xu 641ca26893fSHerbert Xu void *rhashtable_insert_slow(struct rhashtable *ht, const void *key, 642ca26893fSHerbert Xu struct rhash_head *obj) 643ca26893fSHerbert Xu { 644ca26893fSHerbert Xu void *data; 645ca26893fSHerbert Xu 646ca26893fSHerbert Xu do { 647ca26893fSHerbert Xu rcu_read_lock(); 648ca26893fSHerbert Xu data = rhashtable_try_insert(ht, key, obj); 649ca26893fSHerbert Xu rcu_read_unlock(); 650ca26893fSHerbert Xu } while (PTR_ERR(data) == -EAGAIN); 651ca26893fSHerbert Xu 652ca26893fSHerbert Xu return data; 65302fd97c3SHerbert Xu } 65402fd97c3SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_insert_slow); 65502fd97c3SHerbert Xu 656f2dba9c6SHerbert Xu /** 657246779ddSHerbert Xu * rhashtable_walk_enter - Initialise an iterator 658f2dba9c6SHerbert Xu * @ht: Table to walk over 659f2dba9c6SHerbert Xu * @iter: Hash table Iterator 660f2dba9c6SHerbert Xu * 661f2dba9c6SHerbert Xu * This function prepares a hash table walk. 662f2dba9c6SHerbert Xu * 663f2dba9c6SHerbert Xu * Note that if you restart a walk after rhashtable_walk_stop you 664f2dba9c6SHerbert Xu * may see the same object twice. Also, you may miss objects if 665f2dba9c6SHerbert Xu * there are removals in between rhashtable_walk_stop and the next 666f2dba9c6SHerbert Xu * call to rhashtable_walk_start. 667f2dba9c6SHerbert Xu * 668f2dba9c6SHerbert Xu * For a completely stable walk you should construct your own data 669f2dba9c6SHerbert Xu * structure outside the hash table. 670f2dba9c6SHerbert Xu * 671f2dba9c6SHerbert Xu * This function may sleep so you must not call it from interrupt 672f2dba9c6SHerbert Xu * context or with spin locks held. 673f2dba9c6SHerbert Xu * 674246779ddSHerbert Xu * You must call rhashtable_walk_exit after this function returns. 675f2dba9c6SHerbert Xu */ 676246779ddSHerbert Xu void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter) 677f2dba9c6SHerbert Xu { 678f2dba9c6SHerbert Xu iter->ht = ht; 679f2dba9c6SHerbert Xu iter->p = NULL; 680f2dba9c6SHerbert Xu iter->slot = 0; 681f2dba9c6SHerbert Xu iter->skip = 0; 6822db54b47STom Herbert iter->end_of_table = 0; 683f2dba9c6SHerbert Xu 684c6ff5268SHerbert Xu spin_lock(&ht->lock); 685246779ddSHerbert Xu iter->walker.tbl = 686179ccc0aSHerbert Xu rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock)); 687246779ddSHerbert Xu list_add(&iter->walker.list, &iter->walker.tbl->walkers); 688c6ff5268SHerbert Xu spin_unlock(&ht->lock); 689f2dba9c6SHerbert Xu } 690246779ddSHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_enter); 691f2dba9c6SHerbert Xu 692f2dba9c6SHerbert Xu /** 693f2dba9c6SHerbert Xu * rhashtable_walk_exit - Free an iterator 694f2dba9c6SHerbert Xu * @iter: Hash table Iterator 695f2dba9c6SHerbert Xu * 696f2dba9c6SHerbert Xu * This function frees resources allocated by rhashtable_walk_init. 697f2dba9c6SHerbert Xu */ 698f2dba9c6SHerbert Xu void rhashtable_walk_exit(struct rhashtable_iter *iter) 699f2dba9c6SHerbert Xu { 700c6ff5268SHerbert Xu spin_lock(&iter->ht->lock); 701246779ddSHerbert Xu if (iter->walker.tbl) 702246779ddSHerbert Xu list_del(&iter->walker.list); 703c6ff5268SHerbert Xu spin_unlock(&iter->ht->lock); 704f2dba9c6SHerbert Xu } 705f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_exit); 706f2dba9c6SHerbert Xu 707f2dba9c6SHerbert Xu /** 70897a6ec4aSTom Herbert * rhashtable_walk_start_check - Start a hash table walk 709f2dba9c6SHerbert Xu * @iter: Hash table iterator 710f2dba9c6SHerbert Xu * 7110647169cSAndreas Gruenbacher * Start a hash table walk at the current iterator position. Note that we take 7120647169cSAndreas Gruenbacher * the RCU lock in all cases including when we return an error. So you must 7130647169cSAndreas Gruenbacher * always call rhashtable_walk_stop to clean up. 714f2dba9c6SHerbert Xu * 715f2dba9c6SHerbert Xu * Returns zero if successful. 716f2dba9c6SHerbert Xu * 717f2dba9c6SHerbert Xu * Returns -EAGAIN if resize event occured. Note that the iterator 718f2dba9c6SHerbert Xu * will rewind back to the beginning and you may use it immediately 719f2dba9c6SHerbert Xu * by calling rhashtable_walk_next. 72097a6ec4aSTom Herbert * 72197a6ec4aSTom Herbert * rhashtable_walk_start is defined as an inline variant that returns 72297a6ec4aSTom Herbert * void. This is preferred in cases where the caller would ignore 72397a6ec4aSTom Herbert * resize events and always continue. 724f2dba9c6SHerbert Xu */ 72597a6ec4aSTom Herbert int rhashtable_walk_start_check(struct rhashtable_iter *iter) 726db4374f4SThomas Graf __acquires(RCU) 727f2dba9c6SHerbert Xu { 728eddee5baSHerbert Xu struct rhashtable *ht = iter->ht; 729eddee5baSHerbert Xu 730f2dba9c6SHerbert Xu rcu_read_lock(); 731f2dba9c6SHerbert Xu 732c6ff5268SHerbert Xu spin_lock(&ht->lock); 733246779ddSHerbert Xu if (iter->walker.tbl) 734246779ddSHerbert Xu list_del(&iter->walker.list); 735c6ff5268SHerbert Xu spin_unlock(&ht->lock); 736eddee5baSHerbert Xu 7372db54b47STom Herbert if (!iter->walker.tbl && !iter->end_of_table) { 738246779ddSHerbert Xu iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht); 739f2dba9c6SHerbert Xu return -EAGAIN; 740f2dba9c6SHerbert Xu } 741f2dba9c6SHerbert Xu 742f2dba9c6SHerbert Xu return 0; 743f2dba9c6SHerbert Xu } 74497a6ec4aSTom Herbert EXPORT_SYMBOL_GPL(rhashtable_walk_start_check); 745f2dba9c6SHerbert Xu 746f2dba9c6SHerbert Xu /** 7472db54b47STom Herbert * __rhashtable_walk_find_next - Find the next element in a table (or the first 7482db54b47STom Herbert * one in case of a new walk). 7492db54b47STom Herbert * 750f2dba9c6SHerbert Xu * @iter: Hash table iterator 751f2dba9c6SHerbert Xu * 7522db54b47STom Herbert * Returns the found object or NULL when the end of the table is reached. 753f2dba9c6SHerbert Xu * 7542db54b47STom Herbert * Returns -EAGAIN if resize event occurred. 755f2dba9c6SHerbert Xu */ 7562db54b47STom Herbert static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter) 757f2dba9c6SHerbert Xu { 758246779ddSHerbert Xu struct bucket_table *tbl = iter->walker.tbl; 759ca26893fSHerbert Xu struct rhlist_head *list = iter->list; 760f2dba9c6SHerbert Xu struct rhashtable *ht = iter->ht; 761f2dba9c6SHerbert Xu struct rhash_head *p = iter->p; 762ca26893fSHerbert Xu bool rhlist = ht->rhlist; 763f2dba9c6SHerbert Xu 7642db54b47STom Herbert if (!tbl) 7652db54b47STom Herbert return NULL; 766f2dba9c6SHerbert Xu 767f2dba9c6SHerbert Xu for (; iter->slot < tbl->size; iter->slot++) { 768f2dba9c6SHerbert Xu int skip = iter->skip; 769f2dba9c6SHerbert Xu 770f2dba9c6SHerbert Xu rht_for_each_rcu(p, tbl, iter->slot) { 771ca26893fSHerbert Xu if (rhlist) { 772ca26893fSHerbert Xu list = container_of(p, struct rhlist_head, 773ca26893fSHerbert Xu rhead); 774ca26893fSHerbert Xu do { 775ca26893fSHerbert Xu if (!skip) 776ca26893fSHerbert Xu goto next; 777ca26893fSHerbert Xu skip--; 778ca26893fSHerbert Xu list = rcu_dereference(list->next); 779ca26893fSHerbert Xu } while (list); 780ca26893fSHerbert Xu 781ca26893fSHerbert Xu continue; 782ca26893fSHerbert Xu } 783f2dba9c6SHerbert Xu if (!skip) 784f2dba9c6SHerbert Xu break; 785f2dba9c6SHerbert Xu skip--; 786f2dba9c6SHerbert Xu } 787f2dba9c6SHerbert Xu 788f2dba9c6SHerbert Xu next: 789f2dba9c6SHerbert Xu if (!rht_is_a_nulls(p)) { 790f2dba9c6SHerbert Xu iter->skip++; 791f2dba9c6SHerbert Xu iter->p = p; 792ca26893fSHerbert Xu iter->list = list; 793ca26893fSHerbert Xu return rht_obj(ht, rhlist ? &list->rhead : p); 794f2dba9c6SHerbert Xu } 795f2dba9c6SHerbert Xu 796f2dba9c6SHerbert Xu iter->skip = 0; 797f2dba9c6SHerbert Xu } 798f2dba9c6SHerbert Xu 799142b942aSPhil Sutter iter->p = NULL; 800142b942aSPhil Sutter 801d88252f9SHerbert Xu /* Ensure we see any new tables. */ 802d88252f9SHerbert Xu smp_rmb(); 803d88252f9SHerbert Xu 804246779ddSHerbert Xu iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht); 805246779ddSHerbert Xu if (iter->walker.tbl) { 806eddee5baSHerbert Xu iter->slot = 0; 807eddee5baSHerbert Xu iter->skip = 0; 808eddee5baSHerbert Xu return ERR_PTR(-EAGAIN); 8092db54b47STom Herbert } else { 8102db54b47STom Herbert iter->end_of_table = true; 811eddee5baSHerbert Xu } 812eddee5baSHerbert Xu 813c936a79fSThomas Graf return NULL; 814f2dba9c6SHerbert Xu } 8152db54b47STom Herbert 8162db54b47STom Herbert /** 8172db54b47STom Herbert * rhashtable_walk_next - Return the next object and advance the iterator 8182db54b47STom Herbert * @iter: Hash table iterator 8192db54b47STom Herbert * 8202db54b47STom Herbert * Note that you must call rhashtable_walk_stop when you are finished 8212db54b47STom Herbert * with the walk. 8222db54b47STom Herbert * 8232db54b47STom Herbert * Returns the next object or NULL when the end of the table is reached. 8242db54b47STom Herbert * 8252db54b47STom Herbert * Returns -EAGAIN if resize event occurred. Note that the iterator 8262db54b47STom Herbert * will rewind back to the beginning and you may continue to use it. 8272db54b47STom Herbert */ 8282db54b47STom Herbert void *rhashtable_walk_next(struct rhashtable_iter *iter) 8292db54b47STom Herbert { 8302db54b47STom Herbert struct rhlist_head *list = iter->list; 8312db54b47STom Herbert struct rhashtable *ht = iter->ht; 8322db54b47STom Herbert struct rhash_head *p = iter->p; 8332db54b47STom Herbert bool rhlist = ht->rhlist; 8342db54b47STom Herbert 8352db54b47STom Herbert if (p) { 8362db54b47STom Herbert if (!rhlist || !(list = rcu_dereference(list->next))) { 8372db54b47STom Herbert p = rcu_dereference(p->next); 8382db54b47STom Herbert list = container_of(p, struct rhlist_head, rhead); 8392db54b47STom Herbert } 8402db54b47STom Herbert if (!rht_is_a_nulls(p)) { 8412db54b47STom Herbert iter->skip++; 8422db54b47STom Herbert iter->p = p; 8432db54b47STom Herbert iter->list = list; 8442db54b47STom Herbert return rht_obj(ht, rhlist ? &list->rhead : p); 8452db54b47STom Herbert } 8462db54b47STom Herbert 8472db54b47STom Herbert /* At the end of this slot, switch to next one and then find 8482db54b47STom Herbert * next entry from that point. 8492db54b47STom Herbert */ 8502db54b47STom Herbert iter->skip = 0; 8512db54b47STom Herbert iter->slot++; 8522db54b47STom Herbert } 8532db54b47STom Herbert 8542db54b47STom Herbert return __rhashtable_walk_find_next(iter); 8552db54b47STom Herbert } 856f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_next); 857f2dba9c6SHerbert Xu 858f2dba9c6SHerbert Xu /** 8592db54b47STom Herbert * rhashtable_walk_peek - Return the next object but don't advance the iterator 8602db54b47STom Herbert * @iter: Hash table iterator 8612db54b47STom Herbert * 8622db54b47STom Herbert * Returns the next object or NULL when the end of the table is reached. 8632db54b47STom Herbert * 8642db54b47STom Herbert * Returns -EAGAIN if resize event occurred. Note that the iterator 8652db54b47STom Herbert * will rewind back to the beginning and you may continue to use it. 8662db54b47STom Herbert */ 8672db54b47STom Herbert void *rhashtable_walk_peek(struct rhashtable_iter *iter) 8682db54b47STom Herbert { 8692db54b47STom Herbert struct rhlist_head *list = iter->list; 8702db54b47STom Herbert struct rhashtable *ht = iter->ht; 8712db54b47STom Herbert struct rhash_head *p = iter->p; 8722db54b47STom Herbert 8732db54b47STom Herbert if (p) 8742db54b47STom Herbert return rht_obj(ht, ht->rhlist ? &list->rhead : p); 8752db54b47STom Herbert 8762db54b47STom Herbert /* No object found in current iter, find next one in the table. */ 8772db54b47STom Herbert 8782db54b47STom Herbert if (iter->skip) { 8792db54b47STom Herbert /* A nonzero skip value points to the next entry in the table 8802db54b47STom Herbert * beyond that last one that was found. Decrement skip so 8812db54b47STom Herbert * we find the current value. __rhashtable_walk_find_next 8822db54b47STom Herbert * will restore the original value of skip assuming that 8832db54b47STom Herbert * the table hasn't changed. 8842db54b47STom Herbert */ 8852db54b47STom Herbert iter->skip--; 8862db54b47STom Herbert } 8872db54b47STom Herbert 8882db54b47STom Herbert return __rhashtable_walk_find_next(iter); 8892db54b47STom Herbert } 8902db54b47STom Herbert EXPORT_SYMBOL_GPL(rhashtable_walk_peek); 8912db54b47STom Herbert 8922db54b47STom Herbert /** 893f2dba9c6SHerbert Xu * rhashtable_walk_stop - Finish a hash table walk 894f2dba9c6SHerbert Xu * @iter: Hash table iterator 895f2dba9c6SHerbert Xu * 8960647169cSAndreas Gruenbacher * Finish a hash table walk. Does not reset the iterator to the start of the 8970647169cSAndreas Gruenbacher * hash table. 898f2dba9c6SHerbert Xu */ 899f2dba9c6SHerbert Xu void rhashtable_walk_stop(struct rhashtable_iter *iter) 900db4374f4SThomas Graf __releases(RCU) 901f2dba9c6SHerbert Xu { 902eddee5baSHerbert Xu struct rhashtable *ht; 903246779ddSHerbert Xu struct bucket_table *tbl = iter->walker.tbl; 904eddee5baSHerbert Xu 905eddee5baSHerbert Xu if (!tbl) 906963ecbd4SHerbert Xu goto out; 907eddee5baSHerbert Xu 908eddee5baSHerbert Xu ht = iter->ht; 909eddee5baSHerbert Xu 910ba7c95eaSHerbert Xu spin_lock(&ht->lock); 911c4db8848SHerbert Xu if (tbl->rehash < tbl->size) 912246779ddSHerbert Xu list_add(&iter->walker.list, &tbl->walkers); 913eddee5baSHerbert Xu else 914246779ddSHerbert Xu iter->walker.tbl = NULL; 915ba7c95eaSHerbert Xu spin_unlock(&ht->lock); 916eddee5baSHerbert Xu 917f2dba9c6SHerbert Xu iter->p = NULL; 918963ecbd4SHerbert Xu 919963ecbd4SHerbert Xu out: 920963ecbd4SHerbert Xu rcu_read_unlock(); 921f2dba9c6SHerbert Xu } 922f2dba9c6SHerbert Xu EXPORT_SYMBOL_GPL(rhashtable_walk_stop); 923f2dba9c6SHerbert Xu 924488fb86eSHerbert Xu static size_t rounded_hashtable_size(const struct rhashtable_params *params) 9257e1e7763SThomas Graf { 92694000176SYing Xue return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), 927e2e21c1cSHerbert Xu (unsigned long)params->min_size); 9287e1e7763SThomas Graf } 9297e1e7763SThomas Graf 93031ccde2dSHerbert Xu static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) 93131ccde2dSHerbert Xu { 93231ccde2dSHerbert Xu return jhash2(key, length, seed); 93331ccde2dSHerbert Xu } 93431ccde2dSHerbert Xu 9357e1e7763SThomas Graf /** 9367e1e7763SThomas Graf * rhashtable_init - initialize a new hash table 9377e1e7763SThomas Graf * @ht: hash table to be initialized 9387e1e7763SThomas Graf * @params: configuration parameters 9397e1e7763SThomas Graf * 9407e1e7763SThomas Graf * Initializes a new hash table based on the provided configuration 9417e1e7763SThomas Graf * parameters. A table can be configured either with a variable or 9427e1e7763SThomas Graf * fixed length key: 9437e1e7763SThomas Graf * 9447e1e7763SThomas Graf * Configuration Example 1: Fixed length keys 9457e1e7763SThomas Graf * struct test_obj { 9467e1e7763SThomas Graf * int key; 9477e1e7763SThomas Graf * void * my_member; 9487e1e7763SThomas Graf * struct rhash_head node; 9497e1e7763SThomas Graf * }; 9507e1e7763SThomas Graf * 9517e1e7763SThomas Graf * struct rhashtable_params params = { 9527e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 9537e1e7763SThomas Graf * .key_offset = offsetof(struct test_obj, key), 9547e1e7763SThomas Graf * .key_len = sizeof(int), 95587545899SDaniel Borkmann * .hashfn = jhash, 956f89bd6f8SThomas Graf * .nulls_base = (1U << RHT_BASE_SHIFT), 9577e1e7763SThomas Graf * }; 9587e1e7763SThomas Graf * 9597e1e7763SThomas Graf * Configuration Example 2: Variable length keys 9607e1e7763SThomas Graf * struct test_obj { 9617e1e7763SThomas Graf * [...] 9627e1e7763SThomas Graf * struct rhash_head node; 9637e1e7763SThomas Graf * }; 9647e1e7763SThomas Graf * 96549f7b33eSPatrick McHardy * u32 my_hash_fn(const void *data, u32 len, u32 seed) 9667e1e7763SThomas Graf * { 9677e1e7763SThomas Graf * struct test_obj *obj = data; 9687e1e7763SThomas Graf * 9697e1e7763SThomas Graf * return [... hash ...]; 9707e1e7763SThomas Graf * } 9717e1e7763SThomas Graf * 9727e1e7763SThomas Graf * struct rhashtable_params params = { 9737e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 97487545899SDaniel Borkmann * .hashfn = jhash, 9757e1e7763SThomas Graf * .obj_hashfn = my_hash_fn, 9767e1e7763SThomas Graf * }; 9777e1e7763SThomas Graf */ 978488fb86eSHerbert Xu int rhashtable_init(struct rhashtable *ht, 979488fb86eSHerbert Xu const struct rhashtable_params *params) 9807e1e7763SThomas Graf { 9817e1e7763SThomas Graf struct bucket_table *tbl; 9827e1e7763SThomas Graf size_t size; 9837e1e7763SThomas Graf 9847e1e7763SThomas Graf size = HASH_DEFAULT_SIZE; 9857e1e7763SThomas Graf 98631ccde2dSHerbert Xu if ((!params->key_len && !params->obj_hashfn) || 98702fd97c3SHerbert Xu (params->obj_hashfn && !params->obj_cmpfn)) 9887e1e7763SThomas Graf return -EINVAL; 9897e1e7763SThomas Graf 990f89bd6f8SThomas Graf if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) 991f89bd6f8SThomas Graf return -EINVAL; 992f89bd6f8SThomas Graf 99397defe1eSThomas Graf memset(ht, 0, sizeof(*ht)); 99497defe1eSThomas Graf mutex_init(&ht->mutex); 995ba7c95eaSHerbert Xu spin_lock_init(&ht->lock); 99697defe1eSThomas Graf memcpy(&ht->p, params, sizeof(*params)); 99797defe1eSThomas Graf 998a998f712SThomas Graf if (params->min_size) 999a998f712SThomas Graf ht->p.min_size = roundup_pow_of_two(params->min_size); 1000a998f712SThomas Graf 10016d684e54SHerbert Xu /* Cap total entries at 2^31 to avoid nelems overflow. */ 10026d684e54SHerbert Xu ht->max_elems = 1u << 31; 10032d2ab658SHerbert Xu 10042d2ab658SHerbert Xu if (params->max_size) { 10052d2ab658SHerbert Xu ht->p.max_size = rounddown_pow_of_two(params->max_size); 10066d684e54SHerbert Xu if (ht->p.max_size < ht->max_elems / 2) 10076d684e54SHerbert Xu ht->max_elems = ht->p.max_size * 2; 10082d2ab658SHerbert Xu } 10096d684e54SHerbert Xu 101048e75b43SFlorian Westphal ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE); 1011a998f712SThomas Graf 10123a324606SHerbert Xu if (params->nelem_hint) 10133a324606SHerbert Xu size = rounded_hashtable_size(&ht->p); 10143a324606SHerbert Xu 101597defe1eSThomas Graf if (params->locks_mul) 101697defe1eSThomas Graf ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); 101797defe1eSThomas Graf else 101897defe1eSThomas Graf ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; 101997defe1eSThomas Graf 102031ccde2dSHerbert Xu ht->key_len = ht->p.key_len; 102131ccde2dSHerbert Xu if (!params->hashfn) { 102231ccde2dSHerbert Xu ht->p.hashfn = jhash; 102331ccde2dSHerbert Xu 102431ccde2dSHerbert Xu if (!(ht->key_len & (sizeof(u32) - 1))) { 102531ccde2dSHerbert Xu ht->key_len /= sizeof(u32); 102631ccde2dSHerbert Xu ht->p.hashfn = rhashtable_jhash2; 102731ccde2dSHerbert Xu } 102831ccde2dSHerbert Xu } 102931ccde2dSHerbert Xu 1030b9ecfdaaSHerbert Xu tbl = bucket_table_alloc(ht, size, GFP_KERNEL); 10317e1e7763SThomas Graf if (tbl == NULL) 10327e1e7763SThomas Graf return -ENOMEM; 10337e1e7763SThomas Graf 1034545a148eSYing Xue atomic_set(&ht->nelems, 0); 1035a5b6846fSDaniel Borkmann 10367e1e7763SThomas Graf RCU_INIT_POINTER(ht->tbl, tbl); 10377e1e7763SThomas Graf 103857699a40SYing Xue INIT_WORK(&ht->run_work, rht_deferred_worker); 103997defe1eSThomas Graf 10407e1e7763SThomas Graf return 0; 10417e1e7763SThomas Graf } 10427e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init); 10437e1e7763SThomas Graf 10447e1e7763SThomas Graf /** 1045ca26893fSHerbert Xu * rhltable_init - initialize a new hash list table 1046ca26893fSHerbert Xu * @hlt: hash list table to be initialized 1047ca26893fSHerbert Xu * @params: configuration parameters 1048ca26893fSHerbert Xu * 1049ca26893fSHerbert Xu * Initializes a new hash list table. 1050ca26893fSHerbert Xu * 1051ca26893fSHerbert Xu * See documentation for rhashtable_init. 1052ca26893fSHerbert Xu */ 1053ca26893fSHerbert Xu int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params) 1054ca26893fSHerbert Xu { 1055ca26893fSHerbert Xu int err; 1056ca26893fSHerbert Xu 1057ca26893fSHerbert Xu /* No rhlist NULLs marking for now. */ 1058ca26893fSHerbert Xu if (params->nulls_base) 1059ca26893fSHerbert Xu return -EINVAL; 1060ca26893fSHerbert Xu 1061ca26893fSHerbert Xu err = rhashtable_init(&hlt->ht, params); 1062ca26893fSHerbert Xu hlt->ht.rhlist = true; 1063ca26893fSHerbert Xu return err; 1064ca26893fSHerbert Xu } 1065ca26893fSHerbert Xu EXPORT_SYMBOL_GPL(rhltable_init); 1066ca26893fSHerbert Xu 1067ca26893fSHerbert Xu static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj, 1068ca26893fSHerbert Xu void (*free_fn)(void *ptr, void *arg), 1069ca26893fSHerbert Xu void *arg) 1070ca26893fSHerbert Xu { 1071ca26893fSHerbert Xu struct rhlist_head *list; 1072ca26893fSHerbert Xu 1073ca26893fSHerbert Xu if (!ht->rhlist) { 1074ca26893fSHerbert Xu free_fn(rht_obj(ht, obj), arg); 1075ca26893fSHerbert Xu return; 1076ca26893fSHerbert Xu } 1077ca26893fSHerbert Xu 1078ca26893fSHerbert Xu list = container_of(obj, struct rhlist_head, rhead); 1079ca26893fSHerbert Xu do { 1080ca26893fSHerbert Xu obj = &list->rhead; 1081ca26893fSHerbert Xu list = rht_dereference(list->next, ht); 1082ca26893fSHerbert Xu free_fn(rht_obj(ht, obj), arg); 1083ca26893fSHerbert Xu } while (list); 1084ca26893fSHerbert Xu } 1085ca26893fSHerbert Xu 1086ca26893fSHerbert Xu /** 10876b6f302cSThomas Graf * rhashtable_free_and_destroy - free elements and destroy hash table 10887e1e7763SThomas Graf * @ht: the hash table to destroy 10896b6f302cSThomas Graf * @free_fn: callback to release resources of element 10906b6f302cSThomas Graf * @arg: pointer passed to free_fn 10917e1e7763SThomas Graf * 10926b6f302cSThomas Graf * Stops an eventual async resize. If defined, invokes free_fn for each 10936b6f302cSThomas Graf * element to releasal resources. Please note that RCU protected 10946b6f302cSThomas Graf * readers may still be accessing the elements. Releasing of resources 10956b6f302cSThomas Graf * must occur in a compatible manner. Then frees the bucket array. 10966b6f302cSThomas Graf * 10976b6f302cSThomas Graf * This function will eventually sleep to wait for an async resize 10986b6f302cSThomas Graf * to complete. The caller is responsible that no further write operations 10996b6f302cSThomas Graf * occurs in parallel. 11007e1e7763SThomas Graf */ 11016b6f302cSThomas Graf void rhashtable_free_and_destroy(struct rhashtable *ht, 11026b6f302cSThomas Graf void (*free_fn)(void *ptr, void *arg), 11036b6f302cSThomas Graf void *arg) 11047e1e7763SThomas Graf { 1105da20420fSHerbert Xu struct bucket_table *tbl; 11066b6f302cSThomas Graf unsigned int i; 110797defe1eSThomas Graf 110857699a40SYing Xue cancel_work_sync(&ht->run_work); 110957699a40SYing Xue 111097defe1eSThomas Graf mutex_lock(&ht->mutex); 11116b6f302cSThomas Graf tbl = rht_dereference(ht->tbl, ht); 11126b6f302cSThomas Graf if (free_fn) { 11136b6f302cSThomas Graf for (i = 0; i < tbl->size; i++) { 11146b6f302cSThomas Graf struct rhash_head *pos, *next; 11156b6f302cSThomas Graf 1116*ae6da1f5SEric Dumazet cond_resched(); 1117da20420fSHerbert Xu for (pos = rht_dereference(*rht_bucket(tbl, i), ht), 11186b6f302cSThomas Graf next = !rht_is_a_nulls(pos) ? 11196b6f302cSThomas Graf rht_dereference(pos->next, ht) : NULL; 11206b6f302cSThomas Graf !rht_is_a_nulls(pos); 11216b6f302cSThomas Graf pos = next, 11226b6f302cSThomas Graf next = !rht_is_a_nulls(pos) ? 11236b6f302cSThomas Graf rht_dereference(pos->next, ht) : NULL) 1124ca26893fSHerbert Xu rhashtable_free_one(ht, pos, free_fn, arg); 11256b6f302cSThomas Graf } 11266b6f302cSThomas Graf } 11276b6f302cSThomas Graf 11286b6f302cSThomas Graf bucket_table_free(tbl); 112997defe1eSThomas Graf mutex_unlock(&ht->mutex); 11307e1e7763SThomas Graf } 11316b6f302cSThomas Graf EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy); 11326b6f302cSThomas Graf 11336b6f302cSThomas Graf void rhashtable_destroy(struct rhashtable *ht) 11346b6f302cSThomas Graf { 11356b6f302cSThomas Graf return rhashtable_free_and_destroy(ht, NULL, NULL); 11366b6f302cSThomas Graf } 11377e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy); 1138da20420fSHerbert Xu 1139da20420fSHerbert Xu struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, 1140da20420fSHerbert Xu unsigned int hash) 1141da20420fSHerbert Xu { 1142da20420fSHerbert Xu const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); 1143da20420fSHerbert Xu static struct rhash_head __rcu *rhnull = 1144da20420fSHerbert Xu (struct rhash_head __rcu *)NULLS_MARKER(0); 1145da20420fSHerbert Xu unsigned int index = hash & ((1 << tbl->nest) - 1); 1146da20420fSHerbert Xu unsigned int size = tbl->size >> tbl->nest; 1147da20420fSHerbert Xu unsigned int subhash = hash; 1148da20420fSHerbert Xu union nested_table *ntbl; 1149da20420fSHerbert Xu 1150da20420fSHerbert Xu ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); 1151c4d2603dSHerbert Xu ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash); 1152da20420fSHerbert Xu subhash >>= tbl->nest; 1153da20420fSHerbert Xu 1154da20420fSHerbert Xu while (ntbl && size > (1 << shift)) { 1155da20420fSHerbert Xu index = subhash & ((1 << shift) - 1); 1156c4d2603dSHerbert Xu ntbl = rht_dereference_bucket_rcu(ntbl[index].table, 1157c4d2603dSHerbert Xu tbl, hash); 1158da20420fSHerbert Xu size >>= shift; 1159da20420fSHerbert Xu subhash >>= shift; 1160da20420fSHerbert Xu } 1161da20420fSHerbert Xu 1162da20420fSHerbert Xu if (!ntbl) 1163da20420fSHerbert Xu return &rhnull; 1164da20420fSHerbert Xu 1165da20420fSHerbert Xu return &ntbl[subhash].bucket; 1166da20420fSHerbert Xu 1167da20420fSHerbert Xu } 1168da20420fSHerbert Xu EXPORT_SYMBOL_GPL(rht_bucket_nested); 1169da20420fSHerbert Xu 1170da20420fSHerbert Xu struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht, 1171da20420fSHerbert Xu struct bucket_table *tbl, 1172da20420fSHerbert Xu unsigned int hash) 1173da20420fSHerbert Xu { 1174da20420fSHerbert Xu const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); 1175da20420fSHerbert Xu unsigned int index = hash & ((1 << tbl->nest) - 1); 1176da20420fSHerbert Xu unsigned int size = tbl->size >> tbl->nest; 1177da20420fSHerbert Xu union nested_table *ntbl; 1178da20420fSHerbert Xu unsigned int shifted; 1179da20420fSHerbert Xu unsigned int nhash; 1180da20420fSHerbert Xu 1181da20420fSHerbert Xu ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); 1182da20420fSHerbert Xu hash >>= tbl->nest; 1183da20420fSHerbert Xu nhash = index; 1184da20420fSHerbert Xu shifted = tbl->nest; 1185da20420fSHerbert Xu ntbl = nested_table_alloc(ht, &ntbl[index].table, 1186da20420fSHerbert Xu size <= (1 << shift) ? shifted : 0, nhash); 1187da20420fSHerbert Xu 1188da20420fSHerbert Xu while (ntbl && size > (1 << shift)) { 1189da20420fSHerbert Xu index = hash & ((1 << shift) - 1); 1190da20420fSHerbert Xu size >>= shift; 1191da20420fSHerbert Xu hash >>= shift; 1192da20420fSHerbert Xu nhash |= index << shifted; 1193da20420fSHerbert Xu shifted += shift; 1194da20420fSHerbert Xu ntbl = nested_table_alloc(ht, &ntbl[index].table, 1195da20420fSHerbert Xu size <= (1 << shift) ? shifted : 0, 1196da20420fSHerbert Xu nhash); 1197da20420fSHerbert Xu } 1198da20420fSHerbert Xu 1199da20420fSHerbert Xu if (!ntbl) 1200da20420fSHerbert Xu return NULL; 1201da20420fSHerbert Xu 1202da20420fSHerbert Xu return &ntbl[hash].bucket; 1203da20420fSHerbert Xu 1204da20420fSHerbert Xu } 1205da20420fSHerbert Xu EXPORT_SYMBOL_GPL(rht_bucket_nested_insert); 1206