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