17e1e7763SThomas Graf /* 27e1e7763SThomas Graf * Resizable, Scalable, Concurrent Hash Table 37e1e7763SThomas Graf * 47e1e7763SThomas Graf * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch> 57e1e7763SThomas Graf * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> 67e1e7763SThomas Graf * 77e1e7763SThomas Graf * Based on the following paper: 87e1e7763SThomas Graf * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf 97e1e7763SThomas Graf * 107e1e7763SThomas Graf * Code partially derived from nft_hash 117e1e7763SThomas Graf * 127e1e7763SThomas Graf * This program is free software; you can redistribute it and/or modify 137e1e7763SThomas Graf * it under the terms of the GNU General Public License version 2 as 147e1e7763SThomas Graf * published by the Free Software Foundation. 157e1e7763SThomas Graf */ 167e1e7763SThomas Graf 177e1e7763SThomas Graf #include <linux/kernel.h> 187e1e7763SThomas Graf #include <linux/init.h> 197e1e7763SThomas Graf #include <linux/log2.h> 207e1e7763SThomas Graf #include <linux/slab.h> 217e1e7763SThomas Graf #include <linux/vmalloc.h> 227e1e7763SThomas Graf #include <linux/mm.h> 2387545899SDaniel Borkmann #include <linux/jhash.h> 247e1e7763SThomas Graf #include <linux/random.h> 257e1e7763SThomas Graf #include <linux/rhashtable.h> 267e1e7763SThomas Graf 277e1e7763SThomas Graf #define HASH_DEFAULT_SIZE 64UL 287e1e7763SThomas Graf #define HASH_MIN_SIZE 4UL 297e1e7763SThomas Graf 307e1e7763SThomas Graf #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) 317e1e7763SThomas Graf 327e1e7763SThomas Graf #ifdef CONFIG_PROVE_LOCKING 337e1e7763SThomas Graf int lockdep_rht_mutex_is_held(const struct rhashtable *ht) 347e1e7763SThomas Graf { 357b4ce235SHerbert Xu return ht->p.mutex_is_held(ht->p.parent); 367e1e7763SThomas Graf } 377e1e7763SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); 3888d6ed15SThomas Graf 3988d6ed15SThomas Graf int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) 4088d6ed15SThomas Graf { 4188d6ed15SThomas Graf return 1; 4288d6ed15SThomas Graf } 4388d6ed15SThomas Graf EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); 447e1e7763SThomas Graf #endif 457e1e7763SThomas Graf 46c91eee56SThomas Graf static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he) 477e1e7763SThomas Graf { 487e1e7763SThomas Graf return (void *) he - ht->p.head_offset; 497e1e7763SThomas Graf } 507e1e7763SThomas Graf 518d24c0b4SThomas Graf static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash) 527e1e7763SThomas Graf { 538d24c0b4SThomas Graf return hash & (tbl->size - 1); 547e1e7763SThomas Graf } 557e1e7763SThomas Graf 568d24c0b4SThomas Graf static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr) 578d24c0b4SThomas Graf { 588d24c0b4SThomas Graf u32 hash; 598d24c0b4SThomas Graf 608d24c0b4SThomas Graf if (unlikely(!ht->p.key_len)) 618d24c0b4SThomas Graf hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd); 628d24c0b4SThomas Graf else 638d24c0b4SThomas Graf hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len, 648d24c0b4SThomas Graf ht->p.hash_rnd); 658d24c0b4SThomas Graf 668d24c0b4SThomas Graf return hash; 678d24c0b4SThomas Graf } 688d24c0b4SThomas Graf 698d24c0b4SThomas Graf static u32 key_hashfn(const struct rhashtable *ht, const void *key, u32 len) 707e1e7763SThomas Graf { 717e1e7763SThomas Graf struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); 728d24c0b4SThomas Graf u32 hash; 737e1e7763SThomas Graf 748d24c0b4SThomas Graf hash = ht->p.hashfn(key, len, ht->p.hash_rnd); 758d24c0b4SThomas Graf 768d24c0b4SThomas Graf return rht_bucket_index(tbl, hash); 777e1e7763SThomas Graf } 787e1e7763SThomas Graf 797e1e7763SThomas Graf static u32 head_hashfn(const struct rhashtable *ht, 808d24c0b4SThomas Graf const struct bucket_table *tbl, 818d24c0b4SThomas Graf const struct rhash_head *he) 827e1e7763SThomas Graf { 838d24c0b4SThomas Graf return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he))); 847e1e7763SThomas Graf } 857e1e7763SThomas Graf 86b8e1943eSThomas Graf static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n) 87b8e1943eSThomas Graf { 88b8e1943eSThomas Graf struct rhash_head __rcu **pprev; 89b8e1943eSThomas Graf 90b8e1943eSThomas Graf for (pprev = &tbl->buckets[n]; 91b8e1943eSThomas Graf rht_dereference_bucket(*pprev, tbl, n); 92b8e1943eSThomas Graf pprev = &rht_dereference_bucket(*pprev, tbl, n)->next) 93b8e1943eSThomas Graf ; 94b8e1943eSThomas Graf 95b8e1943eSThomas Graf return pprev; 96b8e1943eSThomas Graf } 97b8e1943eSThomas Graf 986eba8224SThomas Graf static struct bucket_table *bucket_table_alloc(size_t nbuckets) 997e1e7763SThomas Graf { 1007e1e7763SThomas Graf struct bucket_table *tbl; 1017e1e7763SThomas Graf size_t size; 1027e1e7763SThomas Graf 1037e1e7763SThomas Graf size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); 1046eba8224SThomas Graf tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); 1057e1e7763SThomas Graf if (tbl == NULL) 1067e1e7763SThomas Graf tbl = vzalloc(size); 1077e1e7763SThomas Graf 1087e1e7763SThomas Graf if (tbl == NULL) 1097e1e7763SThomas Graf return NULL; 1107e1e7763SThomas Graf 1117e1e7763SThomas Graf tbl->size = nbuckets; 1127e1e7763SThomas Graf 1137e1e7763SThomas Graf return tbl; 1147e1e7763SThomas Graf } 1157e1e7763SThomas Graf 1167e1e7763SThomas Graf static void bucket_table_free(const struct bucket_table *tbl) 1177e1e7763SThomas Graf { 1187e1e7763SThomas Graf kvfree(tbl); 1197e1e7763SThomas Graf } 1207e1e7763SThomas Graf 1217e1e7763SThomas Graf /** 1227e1e7763SThomas Graf * rht_grow_above_75 - returns true if nelems > 0.75 * table-size 1237e1e7763SThomas Graf * @ht: hash table 1247e1e7763SThomas Graf * @new_size: new table size 1257e1e7763SThomas Graf */ 1267e1e7763SThomas Graf bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size) 1277e1e7763SThomas Graf { 1287e1e7763SThomas Graf /* Expand table when exceeding 75% load */ 1297e1e7763SThomas Graf return ht->nelems > (new_size / 4 * 3); 1307e1e7763SThomas Graf } 1317e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rht_grow_above_75); 1327e1e7763SThomas Graf 1337e1e7763SThomas Graf /** 1347e1e7763SThomas Graf * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size 1357e1e7763SThomas Graf * @ht: hash table 1367e1e7763SThomas Graf * @new_size: new table size 1377e1e7763SThomas Graf */ 1387e1e7763SThomas Graf bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size) 1397e1e7763SThomas Graf { 1407e1e7763SThomas Graf /* Shrink table beneath 30% load */ 1417e1e7763SThomas Graf return ht->nelems < (new_size * 3 / 10); 1427e1e7763SThomas Graf } 1437e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rht_shrink_below_30); 1447e1e7763SThomas Graf 1457e1e7763SThomas Graf static void hashtable_chain_unzip(const struct rhashtable *ht, 1467e1e7763SThomas Graf const struct bucket_table *new_tbl, 1477e1e7763SThomas Graf struct bucket_table *old_tbl, size_t n) 1487e1e7763SThomas Graf { 1497e1e7763SThomas Graf struct rhash_head *he, *p, *next; 1507e1e7763SThomas Graf unsigned int h; 1517e1e7763SThomas Graf 1527e1e7763SThomas Graf /* Old bucket empty, no work needed. */ 1537e1e7763SThomas Graf p = rht_dereference(old_tbl->buckets[n], ht); 1547e1e7763SThomas Graf if (!p) 1557e1e7763SThomas Graf return; 1567e1e7763SThomas Graf 1577e1e7763SThomas Graf /* Advance the old bucket pointer one or more times until it 1587e1e7763SThomas Graf * reaches a node that doesn't hash to the same bucket as the 1597e1e7763SThomas Graf * previous node p. Call the previous node p; 1607e1e7763SThomas Graf */ 1618d24c0b4SThomas Graf h = head_hashfn(ht, new_tbl, p); 16288d6ed15SThomas Graf rht_for_each_continue(he, p->next, old_tbl, n) { 1638d24c0b4SThomas Graf if (head_hashfn(ht, new_tbl, he) != h) 1647e1e7763SThomas Graf break; 1657e1e7763SThomas Graf p = he; 1667e1e7763SThomas Graf } 1677e1e7763SThomas Graf RCU_INIT_POINTER(old_tbl->buckets[n], p->next); 1687e1e7763SThomas Graf 1697e1e7763SThomas Graf /* Find the subsequent node which does hash to the same 1707e1e7763SThomas Graf * bucket as node P, or NULL if no such node exists. 1717e1e7763SThomas Graf */ 1727e1e7763SThomas Graf next = NULL; 1737e1e7763SThomas Graf if (he) { 17488d6ed15SThomas Graf rht_for_each_continue(he, he->next, old_tbl, n) { 1758d24c0b4SThomas Graf if (head_hashfn(ht, new_tbl, he) == h) { 1767e1e7763SThomas Graf next = he; 1777e1e7763SThomas Graf break; 1787e1e7763SThomas Graf } 1797e1e7763SThomas Graf } 1807e1e7763SThomas Graf } 1817e1e7763SThomas Graf 1827e1e7763SThomas Graf /* Set p's next pointer to that subsequent node pointer, 1837e1e7763SThomas Graf * bypassing the nodes which do not hash to p's bucket 1847e1e7763SThomas Graf */ 1857e1e7763SThomas Graf RCU_INIT_POINTER(p->next, next); 1867e1e7763SThomas Graf } 1877e1e7763SThomas Graf 1887e1e7763SThomas Graf /** 1897e1e7763SThomas Graf * rhashtable_expand - Expand hash table while allowing concurrent lookups 1907e1e7763SThomas Graf * @ht: the hash table to expand 1917e1e7763SThomas Graf * 1927e1e7763SThomas Graf * A secondary bucket array is allocated and the hash entries are migrated 1937e1e7763SThomas Graf * while keeping them on both lists until the end of the RCU grace period. 1947e1e7763SThomas Graf * 1957e1e7763SThomas Graf * This function may only be called in a context where it is safe to call 1967e1e7763SThomas Graf * synchronize_rcu(), e.g. not within a rcu_read_lock() section. 1977e1e7763SThomas Graf * 1987e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations take place. 1997e1e7763SThomas Graf * It is however valid to have concurrent lookups if they are RCU protected. 2007e1e7763SThomas Graf */ 2016eba8224SThomas Graf int rhashtable_expand(struct rhashtable *ht) 2027e1e7763SThomas Graf { 2037e1e7763SThomas Graf struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); 2047e1e7763SThomas Graf struct rhash_head *he; 2057e1e7763SThomas Graf unsigned int i, h; 2067e1e7763SThomas Graf bool complete; 2077e1e7763SThomas Graf 2087e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 2097e1e7763SThomas Graf 2107e1e7763SThomas Graf if (ht->p.max_shift && ht->shift >= ht->p.max_shift) 2117e1e7763SThomas Graf return 0; 2127e1e7763SThomas Graf 2136eba8224SThomas Graf new_tbl = bucket_table_alloc(old_tbl->size * 2); 2147e1e7763SThomas Graf if (new_tbl == NULL) 2157e1e7763SThomas Graf return -ENOMEM; 2167e1e7763SThomas Graf 2177e1e7763SThomas Graf ht->shift++; 2187e1e7763SThomas Graf 2197e1e7763SThomas Graf /* For each new bucket, search the corresponding old bucket 2200c828f2fSHerbert Xu * for the first entry that hashes to the new bucket, and 2217e1e7763SThomas Graf * link the new bucket to that entry. Since all the entries 2227e1e7763SThomas Graf * which will end up in the new bucket appear in the same 2237e1e7763SThomas Graf * old bucket, this constructs an entirely valid new hash 2247e1e7763SThomas Graf * table, but with multiple buckets "zipped" together into a 2257e1e7763SThomas Graf * single imprecise chain. 2267e1e7763SThomas Graf */ 2277e1e7763SThomas Graf for (i = 0; i < new_tbl->size; i++) { 2288d24c0b4SThomas Graf h = rht_bucket_index(old_tbl, i); 22988d6ed15SThomas Graf rht_for_each(he, old_tbl, h) { 2308d24c0b4SThomas Graf if (head_hashfn(ht, new_tbl, he) == i) { 2317e1e7763SThomas Graf RCU_INIT_POINTER(new_tbl->buckets[i], he); 2327e1e7763SThomas Graf break; 2337e1e7763SThomas Graf } 2347e1e7763SThomas Graf } 2357e1e7763SThomas Graf } 2367e1e7763SThomas Graf 2377e1e7763SThomas Graf /* Publish the new table pointer. Lookups may now traverse 2380c828f2fSHerbert Xu * the new table, but they will not benefit from any 2390c828f2fSHerbert Xu * additional efficiency until later steps unzip the buckets. 2407e1e7763SThomas Graf */ 2417e1e7763SThomas Graf rcu_assign_pointer(ht->tbl, new_tbl); 2427e1e7763SThomas Graf 2437e1e7763SThomas Graf /* Unzip interleaved hash chains */ 2447e1e7763SThomas Graf do { 2457e1e7763SThomas Graf /* Wait for readers. All new readers will see the new 2467e1e7763SThomas Graf * table, and thus no references to the old table will 2477e1e7763SThomas Graf * remain. 2487e1e7763SThomas Graf */ 2497e1e7763SThomas Graf synchronize_rcu(); 2507e1e7763SThomas Graf 2517e1e7763SThomas Graf /* For each bucket in the old table (each of which 2527e1e7763SThomas Graf * contains items from multiple buckets of the new 2537e1e7763SThomas Graf * table): ... 2547e1e7763SThomas Graf */ 2557e1e7763SThomas Graf complete = true; 2567e1e7763SThomas Graf for (i = 0; i < old_tbl->size; i++) { 2577e1e7763SThomas Graf hashtable_chain_unzip(ht, new_tbl, old_tbl, i); 2587e1e7763SThomas Graf if (old_tbl->buckets[i] != NULL) 2597e1e7763SThomas Graf complete = false; 2607e1e7763SThomas Graf } 2617e1e7763SThomas Graf } while (!complete); 2627e1e7763SThomas Graf 2637e1e7763SThomas Graf bucket_table_free(old_tbl); 2647e1e7763SThomas Graf return 0; 2657e1e7763SThomas Graf } 2667e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_expand); 2677e1e7763SThomas Graf 2687e1e7763SThomas Graf /** 2697e1e7763SThomas Graf * rhashtable_shrink - Shrink hash table while allowing concurrent lookups 2707e1e7763SThomas Graf * @ht: the hash table to shrink 2717e1e7763SThomas Graf * 2727e1e7763SThomas Graf * This function may only be called in a context where it is safe to call 2737e1e7763SThomas Graf * synchronize_rcu(), e.g. not within a rcu_read_lock() section. 2747e1e7763SThomas Graf * 2757e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations take place. 2767e1e7763SThomas Graf * It is however valid to have concurrent lookups if they are RCU protected. 2777e1e7763SThomas Graf */ 2786eba8224SThomas Graf int rhashtable_shrink(struct rhashtable *ht) 2797e1e7763SThomas Graf { 2807e1e7763SThomas Graf struct bucket_table *ntbl, *tbl = rht_dereference(ht->tbl, ht); 2817e1e7763SThomas Graf unsigned int i; 2827e1e7763SThomas Graf 2837e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 2847e1e7763SThomas Graf 28594000176SYing Xue if (ht->shift <= ht->p.min_shift) 2867e1e7763SThomas Graf return 0; 2877e1e7763SThomas Graf 2886eba8224SThomas Graf ntbl = bucket_table_alloc(tbl->size / 2); 2897e1e7763SThomas Graf if (ntbl == NULL) 2907e1e7763SThomas Graf return -ENOMEM; 2917e1e7763SThomas Graf 2927e1e7763SThomas Graf ht->shift--; 2937e1e7763SThomas Graf 2940c828f2fSHerbert Xu /* Link each bucket in the new table to the first bucket 2957e1e7763SThomas Graf * in the old table that contains entries which will hash 2967e1e7763SThomas Graf * to the new bucket. 2977e1e7763SThomas Graf */ 2987e1e7763SThomas Graf for (i = 0; i < ntbl->size; i++) { 2997e1e7763SThomas Graf ntbl->buckets[i] = tbl->buckets[i]; 300b8e1943eSThomas Graf RCU_INIT_POINTER(*bucket_tail(ntbl, i), 301b8e1943eSThomas Graf tbl->buckets[i + ntbl->size]); 3027e1e7763SThomas Graf 3037e1e7763SThomas Graf } 3047e1e7763SThomas Graf 3057e1e7763SThomas Graf /* Publish the new, valid hash table */ 3067e1e7763SThomas Graf rcu_assign_pointer(ht->tbl, ntbl); 3077e1e7763SThomas Graf 3087e1e7763SThomas Graf /* Wait for readers. No new readers will have references to the 3097e1e7763SThomas Graf * old hash table. 3107e1e7763SThomas Graf */ 3117e1e7763SThomas Graf synchronize_rcu(); 3127e1e7763SThomas Graf 3137e1e7763SThomas Graf bucket_table_free(tbl); 3147e1e7763SThomas Graf 3157e1e7763SThomas Graf return 0; 3167e1e7763SThomas Graf } 3177e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_shrink); 3187e1e7763SThomas Graf 3197e1e7763SThomas Graf /** 3207e1e7763SThomas Graf * rhashtable_insert - insert object into hash hash table 3217e1e7763SThomas Graf * @ht: hash table 3227e1e7763SThomas Graf * @obj: pointer to hash head inside object 3237e1e7763SThomas Graf * 3247e1e7763SThomas Graf * Will automatically grow the table via rhashtable_expand() if the the 3257e1e7763SThomas Graf * grow_decision function specified at rhashtable_init() returns true. 3267e1e7763SThomas Graf * 3277e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations occur. It is 3287e1e7763SThomas Graf * however valid to have concurrent lookups if they are RCU protected. 3297e1e7763SThomas Graf */ 3306eba8224SThomas Graf void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj) 3317e1e7763SThomas Graf { 3327e1e7763SThomas Graf struct bucket_table *tbl = rht_dereference(ht->tbl, ht); 3337e1e7763SThomas Graf u32 hash; 3347e1e7763SThomas Graf 3357e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 3367e1e7763SThomas Graf 3378d24c0b4SThomas Graf hash = head_hashfn(ht, tbl, obj); 3387e1e7763SThomas Graf RCU_INIT_POINTER(obj->next, tbl->buckets[hash]); 3397e1e7763SThomas Graf rcu_assign_pointer(tbl->buckets[hash], obj); 3407e1e7763SThomas Graf ht->nelems++; 3417e1e7763SThomas Graf 3427e1e7763SThomas Graf if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size)) 3436eba8224SThomas Graf rhashtable_expand(ht); 3447e1e7763SThomas Graf } 3457e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_insert); 3467e1e7763SThomas Graf 3477e1e7763SThomas Graf /** 3487e1e7763SThomas Graf * rhashtable_remove - remove object from hash table 3497e1e7763SThomas Graf * @ht: hash table 3507e1e7763SThomas Graf * @obj: pointer to hash head inside object 3517e1e7763SThomas Graf * 3527e1e7763SThomas Graf * Since the hash chain is single linked, the removal operation needs to 3537e1e7763SThomas Graf * walk the bucket chain upon removal. The removal operation is thus 3547e1e7763SThomas Graf * considerable slow if the hash table is not correctly sized. 3557e1e7763SThomas Graf * 3567e1e7763SThomas Graf * Will automatically shrink the table via rhashtable_expand() if the the 3577e1e7763SThomas Graf * shrink_decision function specified at rhashtable_init() returns true. 3587e1e7763SThomas Graf * 3597e1e7763SThomas Graf * The caller must ensure that no concurrent table mutations occur. It is 3607e1e7763SThomas Graf * however valid to have concurrent lookups if they are RCU protected. 3617e1e7763SThomas Graf */ 3626eba8224SThomas Graf bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) 3637e1e7763SThomas Graf { 3647e1e7763SThomas Graf struct bucket_table *tbl = rht_dereference(ht->tbl, ht); 3657e1e7763SThomas Graf struct rhash_head __rcu **pprev; 3667e1e7763SThomas Graf struct rhash_head *he; 3677e1e7763SThomas Graf u32 h; 3687e1e7763SThomas Graf 3697e1e7763SThomas Graf ASSERT_RHT_MUTEX(ht); 3707e1e7763SThomas Graf 3718d24c0b4SThomas Graf h = head_hashfn(ht, tbl, obj); 3727e1e7763SThomas Graf 3737e1e7763SThomas Graf pprev = &tbl->buckets[h]; 37488d6ed15SThomas Graf rht_for_each(he, tbl, h) { 3757e1e7763SThomas Graf if (he != obj) { 3767e1e7763SThomas Graf pprev = &he->next; 3777e1e7763SThomas Graf continue; 3787e1e7763SThomas Graf } 3797e1e7763SThomas Graf 380*897362e4SThomas Graf RCU_INIT_POINTER(*pprev, he->next); 381*897362e4SThomas Graf ht->nelems--; 382*897362e4SThomas Graf 383*897362e4SThomas Graf if (ht->p.shrink_decision && 384*897362e4SThomas Graf ht->p.shrink_decision(ht, tbl->size)) 385*897362e4SThomas Graf rhashtable_shrink(ht); 386*897362e4SThomas Graf 3877e1e7763SThomas Graf return true; 3887e1e7763SThomas Graf } 3897e1e7763SThomas Graf 3907e1e7763SThomas Graf return false; 3917e1e7763SThomas Graf } 3927e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_remove); 3937e1e7763SThomas Graf 3947e1e7763SThomas Graf /** 3957e1e7763SThomas Graf * rhashtable_lookup - lookup key in hash table 3967e1e7763SThomas Graf * @ht: hash table 3977e1e7763SThomas Graf * @key: pointer to key 3987e1e7763SThomas Graf * 3997e1e7763SThomas Graf * Computes the hash value for the key and traverses the bucket chain looking 4007e1e7763SThomas Graf * for a entry with an identical key. The first matching entry is returned. 4017e1e7763SThomas Graf * 4027e1e7763SThomas Graf * This lookup function may only be used for fixed key hash table (key_len 4037e1e7763SThomas Graf * paramter set). It will BUG() if used inappropriately. 4047e1e7763SThomas Graf * 4057e1e7763SThomas Graf * Lookups may occur in parallel with hash mutations as long as the lookup is 4067e1e7763SThomas Graf * guarded by rcu_read_lock(). The caller must take care of this. 4077e1e7763SThomas Graf */ 4087e1e7763SThomas Graf void *rhashtable_lookup(const struct rhashtable *ht, const void *key) 4097e1e7763SThomas Graf { 4107e1e7763SThomas Graf const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); 4117e1e7763SThomas Graf struct rhash_head *he; 4127e1e7763SThomas Graf u32 h; 4137e1e7763SThomas Graf 4147e1e7763SThomas Graf BUG_ON(!ht->p.key_len); 4157e1e7763SThomas Graf 4168d24c0b4SThomas Graf h = key_hashfn(ht, key, ht->p.key_len); 41788d6ed15SThomas Graf rht_for_each_rcu(he, tbl, h) { 4187e1e7763SThomas Graf if (memcmp(rht_obj(ht, he) + ht->p.key_offset, key, 4197e1e7763SThomas Graf ht->p.key_len)) 4207e1e7763SThomas Graf continue; 421a4b18cdaSThomas Graf return rht_obj(ht, he); 4227e1e7763SThomas Graf } 4237e1e7763SThomas Graf 4247e1e7763SThomas Graf return NULL; 4257e1e7763SThomas Graf } 4267e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup); 4277e1e7763SThomas Graf 4287e1e7763SThomas Graf /** 4297e1e7763SThomas Graf * rhashtable_lookup_compare - search hash table with compare function 4307e1e7763SThomas Graf * @ht: hash table 4318d24c0b4SThomas Graf * @key: the pointer to the key 4327e1e7763SThomas Graf * @compare: compare function, must return true on match 4337e1e7763SThomas Graf * @arg: argument passed on to compare function 4347e1e7763SThomas Graf * 4357e1e7763SThomas Graf * Traverses the bucket chain behind the provided hash value and calls the 4367e1e7763SThomas Graf * specified compare function for each entry. 4377e1e7763SThomas Graf * 4387e1e7763SThomas Graf * Lookups may occur in parallel with hash mutations as long as the lookup is 4397e1e7763SThomas Graf * guarded by rcu_read_lock(). The caller must take care of this. 4407e1e7763SThomas Graf * 4417e1e7763SThomas Graf * Returns the first entry on which the compare function returned true. 4427e1e7763SThomas Graf */ 4438d24c0b4SThomas Graf void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key, 4447e1e7763SThomas Graf bool (*compare)(void *, void *), void *arg) 4457e1e7763SThomas Graf { 4467e1e7763SThomas Graf const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); 4477e1e7763SThomas Graf struct rhash_head *he; 4488d24c0b4SThomas Graf u32 hash; 4497e1e7763SThomas Graf 4508d24c0b4SThomas Graf hash = key_hashfn(ht, key, ht->p.key_len); 45188d6ed15SThomas Graf rht_for_each_rcu(he, tbl, hash) { 4527e1e7763SThomas Graf if (!compare(rht_obj(ht, he), arg)) 4537e1e7763SThomas Graf continue; 454a4b18cdaSThomas Graf return rht_obj(ht, he); 4557e1e7763SThomas Graf } 4567e1e7763SThomas Graf 4577e1e7763SThomas Graf return NULL; 4587e1e7763SThomas Graf } 4597e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_lookup_compare); 4607e1e7763SThomas Graf 46194000176SYing Xue static size_t rounded_hashtable_size(struct rhashtable_params *params) 4627e1e7763SThomas Graf { 46394000176SYing Xue return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), 46494000176SYing Xue 1UL << params->min_shift); 4657e1e7763SThomas Graf } 4667e1e7763SThomas Graf 4677e1e7763SThomas Graf /** 4687e1e7763SThomas Graf * rhashtable_init - initialize a new hash table 4697e1e7763SThomas Graf * @ht: hash table to be initialized 4707e1e7763SThomas Graf * @params: configuration parameters 4717e1e7763SThomas Graf * 4727e1e7763SThomas Graf * Initializes a new hash table based on the provided configuration 4737e1e7763SThomas Graf * parameters. A table can be configured either with a variable or 4747e1e7763SThomas Graf * fixed length key: 4757e1e7763SThomas Graf * 4767e1e7763SThomas Graf * Configuration Example 1: Fixed length keys 4777e1e7763SThomas Graf * struct test_obj { 4787e1e7763SThomas Graf * int key; 4797e1e7763SThomas Graf * void * my_member; 4807e1e7763SThomas Graf * struct rhash_head node; 4817e1e7763SThomas Graf * }; 4827e1e7763SThomas Graf * 4837e1e7763SThomas Graf * struct rhashtable_params params = { 4847e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 4857e1e7763SThomas Graf * .key_offset = offsetof(struct test_obj, key), 4867e1e7763SThomas Graf * .key_len = sizeof(int), 48787545899SDaniel Borkmann * .hashfn = jhash, 4881b2f309dSHerbert Xu * #ifdef CONFIG_PROVE_LOCKING 4897e1e7763SThomas Graf * .mutex_is_held = &my_mutex_is_held, 4901b2f309dSHerbert Xu * #endif 4917e1e7763SThomas Graf * }; 4927e1e7763SThomas Graf * 4937e1e7763SThomas Graf * Configuration Example 2: Variable length keys 4947e1e7763SThomas Graf * struct test_obj { 4957e1e7763SThomas Graf * [...] 4967e1e7763SThomas Graf * struct rhash_head node; 4977e1e7763SThomas Graf * }; 4987e1e7763SThomas Graf * 4997e1e7763SThomas Graf * u32 my_hash_fn(const void *data, u32 seed) 5007e1e7763SThomas Graf * { 5017e1e7763SThomas Graf * struct test_obj *obj = data; 5027e1e7763SThomas Graf * 5037e1e7763SThomas Graf * return [... hash ...]; 5047e1e7763SThomas Graf * } 5057e1e7763SThomas Graf * 5067e1e7763SThomas Graf * struct rhashtable_params params = { 5077e1e7763SThomas Graf * .head_offset = offsetof(struct test_obj, node), 50887545899SDaniel Borkmann * .hashfn = jhash, 5097e1e7763SThomas Graf * .obj_hashfn = my_hash_fn, 5101b2f309dSHerbert Xu * #ifdef CONFIG_PROVE_LOCKING 5117e1e7763SThomas Graf * .mutex_is_held = &my_mutex_is_held, 5121b2f309dSHerbert Xu * #endif 5137e1e7763SThomas Graf * }; 5147e1e7763SThomas Graf */ 5157e1e7763SThomas Graf int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params) 5167e1e7763SThomas Graf { 5177e1e7763SThomas Graf struct bucket_table *tbl; 5187e1e7763SThomas Graf size_t size; 5197e1e7763SThomas Graf 5207e1e7763SThomas Graf size = HASH_DEFAULT_SIZE; 5217e1e7763SThomas Graf 5227e1e7763SThomas Graf if ((params->key_len && !params->hashfn) || 5237e1e7763SThomas Graf (!params->key_len && !params->obj_hashfn)) 5247e1e7763SThomas Graf return -EINVAL; 5257e1e7763SThomas Graf 52694000176SYing Xue params->min_shift = max_t(size_t, params->min_shift, 52794000176SYing Xue ilog2(HASH_MIN_SIZE)); 52894000176SYing Xue 5297e1e7763SThomas Graf if (params->nelem_hint) 53094000176SYing Xue size = rounded_hashtable_size(params); 5317e1e7763SThomas Graf 5326eba8224SThomas Graf tbl = bucket_table_alloc(size); 5337e1e7763SThomas Graf if (tbl == NULL) 5347e1e7763SThomas Graf return -ENOMEM; 5357e1e7763SThomas Graf 5367e1e7763SThomas Graf memset(ht, 0, sizeof(*ht)); 5377e1e7763SThomas Graf ht->shift = ilog2(tbl->size); 5387e1e7763SThomas Graf memcpy(&ht->p, params, sizeof(*params)); 5397e1e7763SThomas Graf RCU_INIT_POINTER(ht->tbl, tbl); 5407e1e7763SThomas Graf 5417e1e7763SThomas Graf if (!ht->p.hash_rnd) 5427e1e7763SThomas Graf get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd)); 5437e1e7763SThomas Graf 5447e1e7763SThomas Graf return 0; 5457e1e7763SThomas Graf } 5467e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_init); 5477e1e7763SThomas Graf 5487e1e7763SThomas Graf /** 5497e1e7763SThomas Graf * rhashtable_destroy - destroy hash table 5507e1e7763SThomas Graf * @ht: the hash table to destroy 5517e1e7763SThomas Graf * 552ae82ddcfSPablo Neira Ayuso * Frees the bucket array. This function is not rcu safe, therefore the caller 553ae82ddcfSPablo Neira Ayuso * has to make sure that no resizing may happen by unpublishing the hashtable 554ae82ddcfSPablo Neira Ayuso * and waiting for the quiescent cycle before releasing the bucket array. 5557e1e7763SThomas Graf */ 5567e1e7763SThomas Graf void rhashtable_destroy(const struct rhashtable *ht) 5577e1e7763SThomas Graf { 558ae82ddcfSPablo Neira Ayuso bucket_table_free(ht->tbl); 5597e1e7763SThomas Graf } 5607e1e7763SThomas Graf EXPORT_SYMBOL_GPL(rhashtable_destroy); 5617e1e7763SThomas Graf 5627e1e7763SThomas Graf /************************************************************************** 5637e1e7763SThomas Graf * Self Test 5647e1e7763SThomas Graf **************************************************************************/ 5657e1e7763SThomas Graf 5667e1e7763SThomas Graf #ifdef CONFIG_TEST_RHASHTABLE 5677e1e7763SThomas Graf 5687e1e7763SThomas Graf #define TEST_HT_SIZE 8 5697e1e7763SThomas Graf #define TEST_ENTRIES 2048 5707e1e7763SThomas Graf #define TEST_PTR ((void *) 0xdeadbeef) 5717e1e7763SThomas Graf #define TEST_NEXPANDS 4 5727e1e7763SThomas Graf 5731b2f309dSHerbert Xu #ifdef CONFIG_PROVE_LOCKING 5747b4ce235SHerbert Xu static int test_mutex_is_held(void *parent) 5757e1e7763SThomas Graf { 5767e1e7763SThomas Graf return 1; 5777e1e7763SThomas Graf } 5781b2f309dSHerbert Xu #endif 5797e1e7763SThomas Graf 5807e1e7763SThomas Graf struct test_obj { 5817e1e7763SThomas Graf void *ptr; 5827e1e7763SThomas Graf int value; 5837e1e7763SThomas Graf struct rhash_head node; 5847e1e7763SThomas Graf }; 5857e1e7763SThomas Graf 5867e1e7763SThomas Graf static int __init test_rht_lookup(struct rhashtable *ht) 5877e1e7763SThomas Graf { 5887e1e7763SThomas Graf unsigned int i; 5897e1e7763SThomas Graf 5907e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES * 2; i++) { 5917e1e7763SThomas Graf struct test_obj *obj; 5927e1e7763SThomas Graf bool expected = !(i % 2); 5937e1e7763SThomas Graf u32 key = i; 5947e1e7763SThomas Graf 5957e1e7763SThomas Graf obj = rhashtable_lookup(ht, &key); 5967e1e7763SThomas Graf 5977e1e7763SThomas Graf if (expected && !obj) { 5987e1e7763SThomas Graf pr_warn("Test failed: Could not find key %u\n", key); 5997e1e7763SThomas Graf return -ENOENT; 6007e1e7763SThomas Graf } else if (!expected && obj) { 6017e1e7763SThomas Graf pr_warn("Test failed: Unexpected entry found for key %u\n", 6027e1e7763SThomas Graf key); 6037e1e7763SThomas Graf return -EEXIST; 6047e1e7763SThomas Graf } else if (expected && obj) { 6057e1e7763SThomas Graf if (obj->ptr != TEST_PTR || obj->value != i) { 6067e1e7763SThomas Graf pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n", 6077e1e7763SThomas Graf obj->ptr, TEST_PTR, obj->value, i); 6087e1e7763SThomas Graf return -EINVAL; 6097e1e7763SThomas Graf } 6107e1e7763SThomas Graf } 6117e1e7763SThomas Graf } 6127e1e7763SThomas Graf 6137e1e7763SThomas Graf return 0; 6147e1e7763SThomas Graf } 6157e1e7763SThomas Graf 6163e7b2ec4SThomas Graf static void test_bucket_stats(struct rhashtable *ht, bool quiet) 6177e1e7763SThomas Graf { 6183e7b2ec4SThomas Graf unsigned int cnt, rcu_cnt, i, total = 0; 61988d6ed15SThomas Graf struct rhash_head *pos; 6207e1e7763SThomas Graf struct test_obj *obj; 6213e7b2ec4SThomas Graf struct bucket_table *tbl; 6227e1e7763SThomas Graf 6233e7b2ec4SThomas Graf tbl = rht_dereference_rcu(ht->tbl, ht); 6247e1e7763SThomas Graf for (i = 0; i < tbl->size; i++) { 6253e7b2ec4SThomas Graf rcu_cnt = cnt = 0; 6267e1e7763SThomas Graf 6277e1e7763SThomas Graf if (!quiet) 6287e1e7763SThomas Graf pr_info(" [%#4x/%zu]", i, tbl->size); 6297e1e7763SThomas Graf 63088d6ed15SThomas Graf rht_for_each_entry_rcu(obj, pos, tbl, i, node) { 6317e1e7763SThomas Graf cnt++; 6327e1e7763SThomas Graf total++; 6337e1e7763SThomas Graf if (!quiet) 6347e1e7763SThomas Graf pr_cont(" [%p],", obj); 6357e1e7763SThomas Graf } 6367e1e7763SThomas Graf 63788d6ed15SThomas Graf rht_for_each_entry_rcu(obj, pos, tbl, i, node) 6383e7b2ec4SThomas Graf rcu_cnt++; 6393e7b2ec4SThomas Graf 6403e7b2ec4SThomas Graf if (rcu_cnt != cnt) 6413e7b2ec4SThomas Graf pr_warn("Test failed: Chain count mismach %d != %d", 6423e7b2ec4SThomas Graf cnt, rcu_cnt); 6433e7b2ec4SThomas Graf 6447e1e7763SThomas Graf if (!quiet) 6457e1e7763SThomas Graf pr_cont("\n [%#x] first element: %p, chain length: %u\n", 6467e1e7763SThomas Graf i, tbl->buckets[i], cnt); 6477e1e7763SThomas Graf } 6487e1e7763SThomas Graf 6497e1e7763SThomas Graf pr_info(" Traversal complete: counted=%u, nelems=%zu, entries=%d\n", 6507e1e7763SThomas Graf total, ht->nelems, TEST_ENTRIES); 6513e7b2ec4SThomas Graf 6523e7b2ec4SThomas Graf if (total != ht->nelems || total != TEST_ENTRIES) 6533e7b2ec4SThomas Graf pr_warn("Test failed: Total count mismatch ^^^"); 6547e1e7763SThomas Graf } 6557e1e7763SThomas Graf 6567e1e7763SThomas Graf static int __init test_rhashtable(struct rhashtable *ht) 6577e1e7763SThomas Graf { 6587e1e7763SThomas Graf struct bucket_table *tbl; 65988d6ed15SThomas Graf struct test_obj *obj; 66088d6ed15SThomas Graf struct rhash_head *pos, *next; 6617e1e7763SThomas Graf int err; 6627e1e7763SThomas Graf unsigned int i; 6637e1e7763SThomas Graf 6647e1e7763SThomas Graf /* 6657e1e7763SThomas Graf * Insertion Test: 6667e1e7763SThomas Graf * Insert TEST_ENTRIES into table with all keys even numbers 6677e1e7763SThomas Graf */ 6687e1e7763SThomas Graf pr_info(" Adding %d keys\n", TEST_ENTRIES); 6697e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES; i++) { 6707e1e7763SThomas Graf struct test_obj *obj; 6717e1e7763SThomas Graf 6727e1e7763SThomas Graf obj = kzalloc(sizeof(*obj), GFP_KERNEL); 6737e1e7763SThomas Graf if (!obj) { 6747e1e7763SThomas Graf err = -ENOMEM; 6757e1e7763SThomas Graf goto error; 6767e1e7763SThomas Graf } 6777e1e7763SThomas Graf 6787e1e7763SThomas Graf obj->ptr = TEST_PTR; 6797e1e7763SThomas Graf obj->value = i * 2; 6807e1e7763SThomas Graf 6816eba8224SThomas Graf rhashtable_insert(ht, &obj->node); 6827e1e7763SThomas Graf } 6837e1e7763SThomas Graf 6847e1e7763SThomas Graf rcu_read_lock(); 6853e7b2ec4SThomas Graf test_bucket_stats(ht, true); 6867e1e7763SThomas Graf test_rht_lookup(ht); 6877e1e7763SThomas Graf rcu_read_unlock(); 6887e1e7763SThomas Graf 6897e1e7763SThomas Graf for (i = 0; i < TEST_NEXPANDS; i++) { 6907e1e7763SThomas Graf pr_info(" Table expansion iteration %u...\n", i); 6916eba8224SThomas Graf rhashtable_expand(ht); 6927e1e7763SThomas Graf 6937e1e7763SThomas Graf rcu_read_lock(); 6947e1e7763SThomas Graf pr_info(" Verifying lookups...\n"); 6957e1e7763SThomas Graf test_rht_lookup(ht); 6967e1e7763SThomas Graf rcu_read_unlock(); 6977e1e7763SThomas Graf } 6987e1e7763SThomas Graf 6997e1e7763SThomas Graf for (i = 0; i < TEST_NEXPANDS; i++) { 7007e1e7763SThomas Graf pr_info(" Table shrinkage iteration %u...\n", i); 7016eba8224SThomas Graf rhashtable_shrink(ht); 7027e1e7763SThomas Graf 7037e1e7763SThomas Graf rcu_read_lock(); 7047e1e7763SThomas Graf pr_info(" Verifying lookups...\n"); 7057e1e7763SThomas Graf test_rht_lookup(ht); 7067e1e7763SThomas Graf rcu_read_unlock(); 7077e1e7763SThomas Graf } 7087e1e7763SThomas Graf 7093e7b2ec4SThomas Graf rcu_read_lock(); 7103e7b2ec4SThomas Graf test_bucket_stats(ht, true); 7113e7b2ec4SThomas Graf rcu_read_unlock(); 7123e7b2ec4SThomas Graf 7137e1e7763SThomas Graf pr_info(" Deleting %d keys\n", TEST_ENTRIES); 7147e1e7763SThomas Graf for (i = 0; i < TEST_ENTRIES; i++) { 7157e1e7763SThomas Graf u32 key = i * 2; 7167e1e7763SThomas Graf 7177e1e7763SThomas Graf obj = rhashtable_lookup(ht, &key); 7187e1e7763SThomas Graf BUG_ON(!obj); 7197e1e7763SThomas Graf 7206eba8224SThomas Graf rhashtable_remove(ht, &obj->node); 7217e1e7763SThomas Graf kfree(obj); 7227e1e7763SThomas Graf } 7237e1e7763SThomas Graf 7247e1e7763SThomas Graf return 0; 7257e1e7763SThomas Graf 7267e1e7763SThomas Graf error: 7277e1e7763SThomas Graf tbl = rht_dereference_rcu(ht->tbl, ht); 7287e1e7763SThomas Graf for (i = 0; i < tbl->size; i++) 72988d6ed15SThomas Graf rht_for_each_entry_safe(obj, pos, next, tbl, i, node) 7307e1e7763SThomas Graf kfree(obj); 7317e1e7763SThomas Graf 7327e1e7763SThomas Graf return err; 7337e1e7763SThomas Graf } 7347e1e7763SThomas Graf 7357e1e7763SThomas Graf static int __init test_rht_init(void) 7367e1e7763SThomas Graf { 7377e1e7763SThomas Graf struct rhashtable ht; 7387e1e7763SThomas Graf struct rhashtable_params params = { 7397e1e7763SThomas Graf .nelem_hint = TEST_HT_SIZE, 7407e1e7763SThomas Graf .head_offset = offsetof(struct test_obj, node), 7417e1e7763SThomas Graf .key_offset = offsetof(struct test_obj, value), 7427e1e7763SThomas Graf .key_len = sizeof(int), 74387545899SDaniel Borkmann .hashfn = jhash, 7441b2f309dSHerbert Xu #ifdef CONFIG_PROVE_LOCKING 7457e1e7763SThomas Graf .mutex_is_held = &test_mutex_is_held, 7461b2f309dSHerbert Xu #endif 7477e1e7763SThomas Graf .grow_decision = rht_grow_above_75, 7487e1e7763SThomas Graf .shrink_decision = rht_shrink_below_30, 7497e1e7763SThomas Graf }; 7507e1e7763SThomas Graf int err; 7517e1e7763SThomas Graf 7527e1e7763SThomas Graf pr_info("Running resizable hashtable tests...\n"); 7537e1e7763SThomas Graf 7547e1e7763SThomas Graf err = rhashtable_init(&ht, ¶ms); 7557e1e7763SThomas Graf if (err < 0) { 7567e1e7763SThomas Graf pr_warn("Test failed: Unable to initialize hashtable: %d\n", 7577e1e7763SThomas Graf err); 7587e1e7763SThomas Graf return err; 7597e1e7763SThomas Graf } 7607e1e7763SThomas Graf 7617e1e7763SThomas Graf err = test_rhashtable(&ht); 7627e1e7763SThomas Graf 7637e1e7763SThomas Graf rhashtable_destroy(&ht); 7647e1e7763SThomas Graf 7657e1e7763SThomas Graf return err; 7667e1e7763SThomas Graf } 7677e1e7763SThomas Graf 7687e1e7763SThomas Graf subsys_initcall(test_rht_init); 7697e1e7763SThomas Graf 7707e1e7763SThomas Graf #endif /* CONFIG_TEST_RHASHTABLE */ 771