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