Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Resizable, Scalable, Concurrent Hash Table |
| 3 | * |
| 4 | * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch> |
| 5 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
| 6 | * |
| 7 | * Based on the following paper: |
| 8 | * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf |
| 9 | * |
| 10 | * Code partially derived from nft_hash |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/log2.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/mm.h> |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 23 | #include <linux/jhash.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 24 | #include <linux/random.h> |
| 25 | #include <linux/rhashtable.h> |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 26 | |
| 27 | #define HASH_DEFAULT_SIZE 64UL |
| 28 | #define HASH_MIN_SIZE 4UL |
| 29 | |
| 30 | #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) |
| 31 | |
| 32 | #ifdef CONFIG_PROVE_LOCKING |
| 33 | int lockdep_rht_mutex_is_held(const struct rhashtable *ht) |
| 34 | { |
Herbert Xu | 7b4ce23 | 2014-11-13 18:11:22 +0800 | [diff] [blame] | 35 | return ht->p.mutex_is_held(ht->p.parent); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 36 | } |
| 37 | EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 38 | |
| 39 | int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) |
| 40 | { |
| 41 | return 1; |
| 42 | } |
| 43 | EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
Thomas Graf | c91eee5 | 2014-08-13 16:38:30 +0200 | [diff] [blame] | 46 | static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 47 | { |
| 48 | return (void *) he - ht->p.head_offset; |
| 49 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 50 | |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 51 | static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 52 | { |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 53 | return hash & (tbl->size - 1); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 56 | static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr) |
| 57 | { |
| 58 | u32 hash; |
| 59 | |
| 60 | if (unlikely(!ht->p.key_len)) |
| 61 | hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd); |
| 62 | else |
| 63 | hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len, |
| 64 | ht->p.hash_rnd); |
| 65 | |
| 66 | return hash; |
| 67 | } |
| 68 | |
| 69 | static u32 key_hashfn(const struct rhashtable *ht, const void *key, u32 len) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 70 | { |
| 71 | struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 72 | u32 hash; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 73 | |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 74 | hash = ht->p.hashfn(key, len, ht->p.hash_rnd); |
| 75 | |
| 76 | return rht_bucket_index(tbl, hash); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 77 | } |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 78 | |
| 79 | static u32 head_hashfn(const struct rhashtable *ht, |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 80 | const struct bucket_table *tbl, |
| 81 | const struct rhash_head *he) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 82 | { |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 83 | return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he))); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Thomas Graf | b8e1943 | 2015-01-02 23:00:17 +0100 | [diff] [blame] | 86 | static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n) |
| 87 | { |
| 88 | struct rhash_head __rcu **pprev; |
| 89 | |
| 90 | for (pprev = &tbl->buckets[n]; |
| 91 | rht_dereference_bucket(*pprev, tbl, n); |
| 92 | pprev = &rht_dereference_bucket(*pprev, tbl, n)->next) |
| 93 | ; |
| 94 | |
| 95 | return pprev; |
| 96 | } |
| 97 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 98 | static struct bucket_table *bucket_table_alloc(size_t nbuckets) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 99 | { |
| 100 | struct bucket_table *tbl; |
| 101 | size_t size; |
| 102 | |
| 103 | size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 104 | tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 105 | if (tbl == NULL) |
| 106 | tbl = vzalloc(size); |
| 107 | |
| 108 | if (tbl == NULL) |
| 109 | return NULL; |
| 110 | |
| 111 | tbl->size = nbuckets; |
| 112 | |
| 113 | return tbl; |
| 114 | } |
| 115 | |
| 116 | static void bucket_table_free(const struct bucket_table *tbl) |
| 117 | { |
| 118 | kvfree(tbl); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * rht_grow_above_75 - returns true if nelems > 0.75 * table-size |
| 123 | * @ht: hash table |
| 124 | * @new_size: new table size |
| 125 | */ |
| 126 | bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size) |
| 127 | { |
| 128 | /* Expand table when exceeding 75% load */ |
| 129 | return ht->nelems > (new_size / 4 * 3); |
| 130 | } |
| 131 | EXPORT_SYMBOL_GPL(rht_grow_above_75); |
| 132 | |
| 133 | /** |
| 134 | * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size |
| 135 | * @ht: hash table |
| 136 | * @new_size: new table size |
| 137 | */ |
| 138 | bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size) |
| 139 | { |
| 140 | /* Shrink table beneath 30% load */ |
| 141 | return ht->nelems < (new_size * 3 / 10); |
| 142 | } |
| 143 | EXPORT_SYMBOL_GPL(rht_shrink_below_30); |
| 144 | |
| 145 | static void hashtable_chain_unzip(const struct rhashtable *ht, |
| 146 | const struct bucket_table *new_tbl, |
| 147 | struct bucket_table *old_tbl, size_t n) |
| 148 | { |
| 149 | struct rhash_head *he, *p, *next; |
| 150 | unsigned int h; |
| 151 | |
| 152 | /* Old bucket empty, no work needed. */ |
| 153 | p = rht_dereference(old_tbl->buckets[n], ht); |
| 154 | if (!p) |
| 155 | return; |
| 156 | |
| 157 | /* Advance the old bucket pointer one or more times until it |
| 158 | * reaches a node that doesn't hash to the same bucket as the |
| 159 | * previous node p. Call the previous node p; |
| 160 | */ |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 161 | h = head_hashfn(ht, new_tbl, p); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 162 | rht_for_each_continue(he, p->next, old_tbl, n) { |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 163 | if (head_hashfn(ht, new_tbl, he) != h) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 164 | break; |
| 165 | p = he; |
| 166 | } |
| 167 | RCU_INIT_POINTER(old_tbl->buckets[n], p->next); |
| 168 | |
| 169 | /* Find the subsequent node which does hash to the same |
| 170 | * bucket as node P, or NULL if no such node exists. |
| 171 | */ |
| 172 | next = NULL; |
| 173 | if (he) { |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 174 | rht_for_each_continue(he, he->next, old_tbl, n) { |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 175 | if (head_hashfn(ht, new_tbl, he) == h) { |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 176 | next = he; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* Set p's next pointer to that subsequent node pointer, |
| 183 | * bypassing the nodes which do not hash to p's bucket |
| 184 | */ |
| 185 | RCU_INIT_POINTER(p->next, next); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * rhashtable_expand - Expand hash table while allowing concurrent lookups |
| 190 | * @ht: the hash table to expand |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 191 | * |
| 192 | * A secondary bucket array is allocated and the hash entries are migrated |
| 193 | * while keeping them on both lists until the end of the RCU grace period. |
| 194 | * |
| 195 | * This function may only be called in a context where it is safe to call |
| 196 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. |
| 197 | * |
| 198 | * The caller must ensure that no concurrent table mutations take place. |
| 199 | * It is however valid to have concurrent lookups if they are RCU protected. |
| 200 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 201 | int rhashtable_expand(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 202 | { |
| 203 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
| 204 | struct rhash_head *he; |
| 205 | unsigned int i, h; |
| 206 | bool complete; |
| 207 | |
| 208 | ASSERT_RHT_MUTEX(ht); |
| 209 | |
| 210 | if (ht->p.max_shift && ht->shift >= ht->p.max_shift) |
| 211 | return 0; |
| 212 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 213 | new_tbl = bucket_table_alloc(old_tbl->size * 2); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 214 | if (new_tbl == NULL) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | ht->shift++; |
| 218 | |
| 219 | /* For each new bucket, search the corresponding old bucket |
Herbert Xu | 0c828f2 | 2014-11-13 13:10:48 +0800 | [diff] [blame] | 220 | * for the first entry that hashes to the new bucket, and |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 221 | * link the new bucket to that entry. Since all the entries |
| 222 | * which will end up in the new bucket appear in the same |
| 223 | * old bucket, this constructs an entirely valid new hash |
| 224 | * table, but with multiple buckets "zipped" together into a |
| 225 | * single imprecise chain. |
| 226 | */ |
| 227 | for (i = 0; i < new_tbl->size; i++) { |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 228 | h = rht_bucket_index(old_tbl, i); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 229 | rht_for_each(he, old_tbl, h) { |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 230 | if (head_hashfn(ht, new_tbl, he) == i) { |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 231 | RCU_INIT_POINTER(new_tbl->buckets[i], he); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* Publish the new table pointer. Lookups may now traverse |
Herbert Xu | 0c828f2 | 2014-11-13 13:10:48 +0800 | [diff] [blame] | 238 | * the new table, but they will not benefit from any |
| 239 | * additional efficiency until later steps unzip the buckets. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 240 | */ |
| 241 | rcu_assign_pointer(ht->tbl, new_tbl); |
| 242 | |
| 243 | /* Unzip interleaved hash chains */ |
| 244 | do { |
| 245 | /* Wait for readers. All new readers will see the new |
| 246 | * table, and thus no references to the old table will |
| 247 | * remain. |
| 248 | */ |
| 249 | synchronize_rcu(); |
| 250 | |
| 251 | /* For each bucket in the old table (each of which |
| 252 | * contains items from multiple buckets of the new |
| 253 | * table): ... |
| 254 | */ |
| 255 | complete = true; |
| 256 | for (i = 0; i < old_tbl->size; i++) { |
| 257 | hashtable_chain_unzip(ht, new_tbl, old_tbl, i); |
| 258 | if (old_tbl->buckets[i] != NULL) |
| 259 | complete = false; |
| 260 | } |
| 261 | } while (!complete); |
| 262 | |
| 263 | bucket_table_free(old_tbl); |
| 264 | return 0; |
| 265 | } |
| 266 | EXPORT_SYMBOL_GPL(rhashtable_expand); |
| 267 | |
| 268 | /** |
| 269 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups |
| 270 | * @ht: the hash table to shrink |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 271 | * |
| 272 | * This function may only be called in a context where it is safe to call |
| 273 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. |
| 274 | * |
| 275 | * The caller must ensure that no concurrent table mutations take place. |
| 276 | * It is however valid to have concurrent lookups if they are RCU protected. |
| 277 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 278 | int rhashtable_shrink(struct rhashtable *ht) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 279 | { |
| 280 | struct bucket_table *ntbl, *tbl = rht_dereference(ht->tbl, ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 281 | unsigned int i; |
| 282 | |
| 283 | ASSERT_RHT_MUTEX(ht); |
| 284 | |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 285 | if (ht->shift <= ht->p.min_shift) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 286 | return 0; |
| 287 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 288 | ntbl = bucket_table_alloc(tbl->size / 2); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 289 | if (ntbl == NULL) |
| 290 | return -ENOMEM; |
| 291 | |
| 292 | ht->shift--; |
| 293 | |
Herbert Xu | 0c828f2 | 2014-11-13 13:10:48 +0800 | [diff] [blame] | 294 | /* Link each bucket in the new table to the first bucket |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 295 | * in the old table that contains entries which will hash |
| 296 | * to the new bucket. |
| 297 | */ |
| 298 | for (i = 0; i < ntbl->size; i++) { |
| 299 | ntbl->buckets[i] = tbl->buckets[i]; |
Thomas Graf | b8e1943 | 2015-01-02 23:00:17 +0100 | [diff] [blame] | 300 | RCU_INIT_POINTER(*bucket_tail(ntbl, i), |
| 301 | tbl->buckets[i + ntbl->size]); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 302 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* Publish the new, valid hash table */ |
| 306 | rcu_assign_pointer(ht->tbl, ntbl); |
| 307 | |
| 308 | /* Wait for readers. No new readers will have references to the |
| 309 | * old hash table. |
| 310 | */ |
| 311 | synchronize_rcu(); |
| 312 | |
| 313 | bucket_table_free(tbl); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(rhashtable_shrink); |
| 318 | |
| 319 | /** |
| 320 | * rhashtable_insert - insert object into hash hash table |
| 321 | * @ht: hash table |
| 322 | * @obj: pointer to hash head inside object |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 323 | * |
| 324 | * Will automatically grow the table via rhashtable_expand() if the the |
| 325 | * grow_decision function specified at rhashtable_init() returns true. |
| 326 | * |
| 327 | * The caller must ensure that no concurrent table mutations occur. It is |
| 328 | * however valid to have concurrent lookups if they are RCU protected. |
| 329 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 330 | void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 331 | { |
| 332 | struct bucket_table *tbl = rht_dereference(ht->tbl, ht); |
| 333 | u32 hash; |
| 334 | |
| 335 | ASSERT_RHT_MUTEX(ht); |
| 336 | |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 337 | hash = head_hashfn(ht, tbl, obj); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 338 | RCU_INIT_POINTER(obj->next, tbl->buckets[hash]); |
| 339 | rcu_assign_pointer(tbl->buckets[hash], obj); |
| 340 | ht->nelems++; |
| 341 | |
| 342 | if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size)) |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 343 | rhashtable_expand(ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 344 | } |
| 345 | EXPORT_SYMBOL_GPL(rhashtable_insert); |
| 346 | |
| 347 | /** |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 348 | * rhashtable_remove - remove object from hash table |
| 349 | * @ht: hash table |
| 350 | * @obj: pointer to hash head inside object |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 351 | * |
| 352 | * Since the hash chain is single linked, the removal operation needs to |
| 353 | * walk the bucket chain upon removal. The removal operation is thus |
| 354 | * considerable slow if the hash table is not correctly sized. |
| 355 | * |
| 356 | * Will automatically shrink the table via rhashtable_expand() if the the |
| 357 | * shrink_decision function specified at rhashtable_init() returns true. |
| 358 | * |
| 359 | * The caller must ensure that no concurrent table mutations occur. It is |
| 360 | * however valid to have concurrent lookups if they are RCU protected. |
| 361 | */ |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 362 | bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 363 | { |
| 364 | struct bucket_table *tbl = rht_dereference(ht->tbl, ht); |
| 365 | struct rhash_head __rcu **pprev; |
| 366 | struct rhash_head *he; |
| 367 | u32 h; |
| 368 | |
| 369 | ASSERT_RHT_MUTEX(ht); |
| 370 | |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 371 | h = head_hashfn(ht, tbl, obj); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 372 | |
| 373 | pprev = &tbl->buckets[h]; |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 374 | rht_for_each(he, tbl, h) { |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 375 | if (he != obj) { |
| 376 | pprev = &he->next; |
| 377 | continue; |
| 378 | } |
| 379 | |
Thomas Graf | 897362e | 2015-01-02 23:00:18 +0100 | [diff] [blame^] | 380 | RCU_INIT_POINTER(*pprev, he->next); |
| 381 | ht->nelems--; |
| 382 | |
| 383 | if (ht->p.shrink_decision && |
| 384 | ht->p.shrink_decision(ht, tbl->size)) |
| 385 | rhashtable_shrink(ht); |
| 386 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 387 | return true; |
| 388 | } |
| 389 | |
| 390 | return false; |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(rhashtable_remove); |
| 393 | |
| 394 | /** |
| 395 | * rhashtable_lookup - lookup key in hash table |
| 396 | * @ht: hash table |
| 397 | * @key: pointer to key |
| 398 | * |
| 399 | * Computes the hash value for the key and traverses the bucket chain looking |
| 400 | * for a entry with an identical key. The first matching entry is returned. |
| 401 | * |
| 402 | * This lookup function may only be used for fixed key hash table (key_len |
| 403 | * paramter set). It will BUG() if used inappropriately. |
| 404 | * |
| 405 | * Lookups may occur in parallel with hash mutations as long as the lookup is |
| 406 | * guarded by rcu_read_lock(). The caller must take care of this. |
| 407 | */ |
| 408 | void *rhashtable_lookup(const struct rhashtable *ht, const void *key) |
| 409 | { |
| 410 | const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); |
| 411 | struct rhash_head *he; |
| 412 | u32 h; |
| 413 | |
| 414 | BUG_ON(!ht->p.key_len); |
| 415 | |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 416 | h = key_hashfn(ht, key, ht->p.key_len); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 417 | rht_for_each_rcu(he, tbl, h) { |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 418 | if (memcmp(rht_obj(ht, he) + ht->p.key_offset, key, |
| 419 | ht->p.key_len)) |
| 420 | continue; |
Thomas Graf | a4b18cd | 2015-01-02 23:00:15 +0100 | [diff] [blame] | 421 | return rht_obj(ht, he); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return NULL; |
| 425 | } |
| 426 | EXPORT_SYMBOL_GPL(rhashtable_lookup); |
| 427 | |
| 428 | /** |
| 429 | * rhashtable_lookup_compare - search hash table with compare function |
| 430 | * @ht: hash table |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 431 | * @key: the pointer to the key |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 432 | * @compare: compare function, must return true on match |
| 433 | * @arg: argument passed on to compare function |
| 434 | * |
| 435 | * Traverses the bucket chain behind the provided hash value and calls the |
| 436 | * specified compare function for each entry. |
| 437 | * |
| 438 | * Lookups may occur in parallel with hash mutations as long as the lookup is |
| 439 | * guarded by rcu_read_lock(). The caller must take care of this. |
| 440 | * |
| 441 | * Returns the first entry on which the compare function returned true. |
| 442 | */ |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 443 | void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 444 | bool (*compare)(void *, void *), void *arg) |
| 445 | { |
| 446 | const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); |
| 447 | struct rhash_head *he; |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 448 | u32 hash; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 449 | |
Thomas Graf | 8d24c0b | 2015-01-02 23:00:14 +0100 | [diff] [blame] | 450 | hash = key_hashfn(ht, key, ht->p.key_len); |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 451 | rht_for_each_rcu(he, tbl, hash) { |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 452 | if (!compare(rht_obj(ht, he), arg)) |
| 453 | continue; |
Thomas Graf | a4b18cd | 2015-01-02 23:00:15 +0100 | [diff] [blame] | 454 | return rht_obj(ht, he); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | return NULL; |
| 458 | } |
| 459 | EXPORT_SYMBOL_GPL(rhashtable_lookup_compare); |
| 460 | |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 461 | static size_t rounded_hashtable_size(struct rhashtable_params *params) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 462 | { |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 463 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
| 464 | 1UL << params->min_shift); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
| 468 | * rhashtable_init - initialize a new hash table |
| 469 | * @ht: hash table to be initialized |
| 470 | * @params: configuration parameters |
| 471 | * |
| 472 | * Initializes a new hash table based on the provided configuration |
| 473 | * parameters. A table can be configured either with a variable or |
| 474 | * fixed length key: |
| 475 | * |
| 476 | * Configuration Example 1: Fixed length keys |
| 477 | * struct test_obj { |
| 478 | * int key; |
| 479 | * void * my_member; |
| 480 | * struct rhash_head node; |
| 481 | * }; |
| 482 | * |
| 483 | * struct rhashtable_params params = { |
| 484 | * .head_offset = offsetof(struct test_obj, node), |
| 485 | * .key_offset = offsetof(struct test_obj, key), |
| 486 | * .key_len = sizeof(int), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 487 | * .hashfn = jhash, |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 488 | * #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 489 | * .mutex_is_held = &my_mutex_is_held, |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 490 | * #endif |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 491 | * }; |
| 492 | * |
| 493 | * Configuration Example 2: Variable length keys |
| 494 | * struct test_obj { |
| 495 | * [...] |
| 496 | * struct rhash_head node; |
| 497 | * }; |
| 498 | * |
| 499 | * u32 my_hash_fn(const void *data, u32 seed) |
| 500 | * { |
| 501 | * struct test_obj *obj = data; |
| 502 | * |
| 503 | * return [... hash ...]; |
| 504 | * } |
| 505 | * |
| 506 | * struct rhashtable_params params = { |
| 507 | * .head_offset = offsetof(struct test_obj, node), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 508 | * .hashfn = jhash, |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 509 | * .obj_hashfn = my_hash_fn, |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 510 | * #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 511 | * .mutex_is_held = &my_mutex_is_held, |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 512 | * #endif |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 513 | * }; |
| 514 | */ |
| 515 | int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params) |
| 516 | { |
| 517 | struct bucket_table *tbl; |
| 518 | size_t size; |
| 519 | |
| 520 | size = HASH_DEFAULT_SIZE; |
| 521 | |
| 522 | if ((params->key_len && !params->hashfn) || |
| 523 | (!params->key_len && !params->obj_hashfn)) |
| 524 | return -EINVAL; |
| 525 | |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 526 | params->min_shift = max_t(size_t, params->min_shift, |
| 527 | ilog2(HASH_MIN_SIZE)); |
| 528 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 529 | if (params->nelem_hint) |
Ying Xue | 9400017 | 2014-09-03 09:22:36 +0800 | [diff] [blame] | 530 | size = rounded_hashtable_size(params); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 531 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 532 | tbl = bucket_table_alloc(size); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 533 | if (tbl == NULL) |
| 534 | return -ENOMEM; |
| 535 | |
| 536 | memset(ht, 0, sizeof(*ht)); |
| 537 | ht->shift = ilog2(tbl->size); |
| 538 | memcpy(&ht->p, params, sizeof(*params)); |
| 539 | RCU_INIT_POINTER(ht->tbl, tbl); |
| 540 | |
| 541 | if (!ht->p.hash_rnd) |
| 542 | get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd)); |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | EXPORT_SYMBOL_GPL(rhashtable_init); |
| 547 | |
| 548 | /** |
| 549 | * rhashtable_destroy - destroy hash table |
| 550 | * @ht: the hash table to destroy |
| 551 | * |
Pablo Neira Ayuso | ae82ddc | 2014-09-02 00:26:05 +0200 | [diff] [blame] | 552 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
| 553 | * has to make sure that no resizing may happen by unpublishing the hashtable |
| 554 | * and waiting for the quiescent cycle before releasing the bucket array. |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 555 | */ |
| 556 | void rhashtable_destroy(const struct rhashtable *ht) |
| 557 | { |
Pablo Neira Ayuso | ae82ddc | 2014-09-02 00:26:05 +0200 | [diff] [blame] | 558 | bucket_table_free(ht->tbl); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 559 | } |
| 560 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |
| 561 | |
| 562 | /************************************************************************** |
| 563 | * Self Test |
| 564 | **************************************************************************/ |
| 565 | |
| 566 | #ifdef CONFIG_TEST_RHASHTABLE |
| 567 | |
| 568 | #define TEST_HT_SIZE 8 |
| 569 | #define TEST_ENTRIES 2048 |
| 570 | #define TEST_PTR ((void *) 0xdeadbeef) |
| 571 | #define TEST_NEXPANDS 4 |
| 572 | |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 573 | #ifdef CONFIG_PROVE_LOCKING |
Herbert Xu | 7b4ce23 | 2014-11-13 18:11:22 +0800 | [diff] [blame] | 574 | static int test_mutex_is_held(void *parent) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 575 | { |
| 576 | return 1; |
| 577 | } |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 578 | #endif |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 579 | |
| 580 | struct test_obj { |
| 581 | void *ptr; |
| 582 | int value; |
| 583 | struct rhash_head node; |
| 584 | }; |
| 585 | |
| 586 | static int __init test_rht_lookup(struct rhashtable *ht) |
| 587 | { |
| 588 | unsigned int i; |
| 589 | |
| 590 | for (i = 0; i < TEST_ENTRIES * 2; i++) { |
| 591 | struct test_obj *obj; |
| 592 | bool expected = !(i % 2); |
| 593 | u32 key = i; |
| 594 | |
| 595 | obj = rhashtable_lookup(ht, &key); |
| 596 | |
| 597 | if (expected && !obj) { |
| 598 | pr_warn("Test failed: Could not find key %u\n", key); |
| 599 | return -ENOENT; |
| 600 | } else if (!expected && obj) { |
| 601 | pr_warn("Test failed: Unexpected entry found for key %u\n", |
| 602 | key); |
| 603 | return -EEXIST; |
| 604 | } else if (expected && obj) { |
| 605 | if (obj->ptr != TEST_PTR || obj->value != i) { |
| 606 | pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n", |
| 607 | obj->ptr, TEST_PTR, obj->value, i); |
| 608 | return -EINVAL; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 616 | static void test_bucket_stats(struct rhashtable *ht, bool quiet) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 617 | { |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 618 | unsigned int cnt, rcu_cnt, i, total = 0; |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 619 | struct rhash_head *pos; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 620 | struct test_obj *obj; |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 621 | struct bucket_table *tbl; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 622 | |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 623 | tbl = rht_dereference_rcu(ht->tbl, ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 624 | for (i = 0; i < tbl->size; i++) { |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 625 | rcu_cnt = cnt = 0; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 626 | |
| 627 | if (!quiet) |
| 628 | pr_info(" [%#4x/%zu]", i, tbl->size); |
| 629 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 630 | rht_for_each_entry_rcu(obj, pos, tbl, i, node) { |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 631 | cnt++; |
| 632 | total++; |
| 633 | if (!quiet) |
| 634 | pr_cont(" [%p],", obj); |
| 635 | } |
| 636 | |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 637 | rht_for_each_entry_rcu(obj, pos, tbl, i, node) |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 638 | rcu_cnt++; |
| 639 | |
| 640 | if (rcu_cnt != cnt) |
| 641 | pr_warn("Test failed: Chain count mismach %d != %d", |
| 642 | cnt, rcu_cnt); |
| 643 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 644 | if (!quiet) |
| 645 | pr_cont("\n [%#x] first element: %p, chain length: %u\n", |
| 646 | i, tbl->buckets[i], cnt); |
| 647 | } |
| 648 | |
| 649 | pr_info(" Traversal complete: counted=%u, nelems=%zu, entries=%d\n", |
| 650 | total, ht->nelems, TEST_ENTRIES); |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 651 | |
| 652 | if (total != ht->nelems || total != TEST_ENTRIES) |
| 653 | pr_warn("Test failed: Total count mismatch ^^^"); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static int __init test_rhashtable(struct rhashtable *ht) |
| 657 | { |
| 658 | struct bucket_table *tbl; |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 659 | struct test_obj *obj; |
| 660 | struct rhash_head *pos, *next; |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 661 | int err; |
| 662 | unsigned int i; |
| 663 | |
| 664 | /* |
| 665 | * Insertion Test: |
| 666 | * Insert TEST_ENTRIES into table with all keys even numbers |
| 667 | */ |
| 668 | pr_info(" Adding %d keys\n", TEST_ENTRIES); |
| 669 | for (i = 0; i < TEST_ENTRIES; i++) { |
| 670 | struct test_obj *obj; |
| 671 | |
| 672 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 673 | if (!obj) { |
| 674 | err = -ENOMEM; |
| 675 | goto error; |
| 676 | } |
| 677 | |
| 678 | obj->ptr = TEST_PTR; |
| 679 | obj->value = i * 2; |
| 680 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 681 | rhashtable_insert(ht, &obj->node); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | rcu_read_lock(); |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 685 | test_bucket_stats(ht, true); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 686 | test_rht_lookup(ht); |
| 687 | rcu_read_unlock(); |
| 688 | |
| 689 | for (i = 0; i < TEST_NEXPANDS; i++) { |
| 690 | pr_info(" Table expansion iteration %u...\n", i); |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 691 | rhashtable_expand(ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 692 | |
| 693 | rcu_read_lock(); |
| 694 | pr_info(" Verifying lookups...\n"); |
| 695 | test_rht_lookup(ht); |
| 696 | rcu_read_unlock(); |
| 697 | } |
| 698 | |
| 699 | for (i = 0; i < TEST_NEXPANDS; i++) { |
| 700 | pr_info(" Table shrinkage iteration %u...\n", i); |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 701 | rhashtable_shrink(ht); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 702 | |
| 703 | rcu_read_lock(); |
| 704 | pr_info(" Verifying lookups...\n"); |
| 705 | test_rht_lookup(ht); |
| 706 | rcu_read_unlock(); |
| 707 | } |
| 708 | |
Thomas Graf | 3e7b2ec | 2014-11-24 12:37:58 +0100 | [diff] [blame] | 709 | rcu_read_lock(); |
| 710 | test_bucket_stats(ht, true); |
| 711 | rcu_read_unlock(); |
| 712 | |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 713 | pr_info(" Deleting %d keys\n", TEST_ENTRIES); |
| 714 | for (i = 0; i < TEST_ENTRIES; i++) { |
| 715 | u32 key = i * 2; |
| 716 | |
| 717 | obj = rhashtable_lookup(ht, &key); |
| 718 | BUG_ON(!obj); |
| 719 | |
Thomas Graf | 6eba822 | 2014-11-13 13:45:46 +0100 | [diff] [blame] | 720 | rhashtable_remove(ht, &obj->node); |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 721 | kfree(obj); |
| 722 | } |
| 723 | |
| 724 | return 0; |
| 725 | |
| 726 | error: |
| 727 | tbl = rht_dereference_rcu(ht->tbl, ht); |
| 728 | for (i = 0; i < tbl->size; i++) |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 729 | rht_for_each_entry_safe(obj, pos, next, tbl, i, node) |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 730 | kfree(obj); |
| 731 | |
| 732 | return err; |
| 733 | } |
| 734 | |
| 735 | static int __init test_rht_init(void) |
| 736 | { |
| 737 | struct rhashtable ht; |
| 738 | struct rhashtable_params params = { |
| 739 | .nelem_hint = TEST_HT_SIZE, |
| 740 | .head_offset = offsetof(struct test_obj, node), |
| 741 | .key_offset = offsetof(struct test_obj, value), |
| 742 | .key_len = sizeof(int), |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 743 | .hashfn = jhash, |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 744 | #ifdef CONFIG_PROVE_LOCKING |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 745 | .mutex_is_held = &test_mutex_is_held, |
Herbert Xu | 1b2f309 | 2014-11-13 18:11:20 +0800 | [diff] [blame] | 746 | #endif |
Thomas Graf | 7e1e776 | 2014-08-02 11:47:44 +0200 | [diff] [blame] | 747 | .grow_decision = rht_grow_above_75, |
| 748 | .shrink_decision = rht_shrink_below_30, |
| 749 | }; |
| 750 | int err; |
| 751 | |
| 752 | pr_info("Running resizable hashtable tests...\n"); |
| 753 | |
| 754 | err = rhashtable_init(&ht, ¶ms); |
| 755 | if (err < 0) { |
| 756 | pr_warn("Test failed: Unable to initialize hashtable: %d\n", |
| 757 | err); |
| 758 | return err; |
| 759 | } |
| 760 | |
| 761 | err = test_rhashtable(&ht); |
| 762 | |
| 763 | rhashtable_destroy(&ht); |
| 764 | |
| 765 | return err; |
| 766 | } |
| 767 | |
| 768 | subsys_initcall(test_rht_init); |
| 769 | |
| 770 | #endif /* CONFIG_TEST_RHASHTABLE */ |