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); | |
88d6ed15 TG |
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); | |
7e1e7763 TG |
44 | #endif |
45 | ||
c91eee56 | 46 | static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he) |
7e1e7763 TG |
47 | { |
48 | return (void *) he - ht->p.head_offset; | |
49 | } | |
7e1e7763 | 50 | |
8d24c0b4 | 51 | static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash) |
7e1e7763 | 52 | { |
8d24c0b4 | 53 | return hash & (tbl->size - 1); |
7e1e7763 | 54 | } |
7e1e7763 | 55 | |
8d24c0b4 | 56 | static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr) |
7e1e7763 | 57 | { |
8d24c0b4 | 58 | u32 hash; |
7e1e7763 | 59 | |
8d24c0b4 TG |
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); | |
7e1e7763 | 65 | |
8d24c0b4 | 66 | return hash; |
7e1e7763 TG |
67 | } |
68 | ||
8d24c0b4 | 69 | static u32 key_hashfn(const struct rhashtable *ht, const void *key, u32 len) |
7e1e7763 TG |
70 | { |
71 | struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); | |
8d24c0b4 TG |
72 | u32 hash; |
73 | ||
74 | hash = ht->p.hashfn(key, len, ht->p.hash_rnd); | |
7e1e7763 | 75 | |
8d24c0b4 | 76 | return rht_bucket_index(tbl, hash); |
7e1e7763 | 77 | } |
7e1e7763 TG |
78 | |
79 | static u32 head_hashfn(const struct rhashtable *ht, | |
8d24c0b4 TG |
80 | const struct bucket_table *tbl, |
81 | const struct rhash_head *he) | |
7e1e7763 | 82 | { |
8d24c0b4 | 83 | return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he))); |
7e1e7763 TG |
84 | } |
85 | ||
b8e1943e TG |
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 | ||
6eba8224 | 98 | static struct bucket_table *bucket_table_alloc(size_t nbuckets) |
7e1e7763 TG |
99 | { |
100 | struct bucket_table *tbl; | |
101 | size_t size; | |
102 | ||
103 | size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); | |
6eba8224 | 104 | tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
7e1e7763 TG |
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 | */ | |
8d24c0b4 | 161 | h = head_hashfn(ht, new_tbl, p); |
88d6ed15 | 162 | rht_for_each_continue(he, p->next, old_tbl, n) { |
8d24c0b4 | 163 | if (head_hashfn(ht, new_tbl, he) != h) |
7e1e7763 TG |
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) { | |
88d6ed15 | 174 | rht_for_each_continue(he, he->next, old_tbl, n) { |
8d24c0b4 | 175 | if (head_hashfn(ht, new_tbl, he) == h) { |
7e1e7763 TG |
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 | |
7e1e7763 TG |
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 | */ | |
6eba8224 | 201 | int rhashtable_expand(struct rhashtable *ht) |
7e1e7763 TG |
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 | ||
6eba8224 | 213 | new_tbl = bucket_table_alloc(old_tbl->size * 2); |
7e1e7763 TG |
214 | if (new_tbl == NULL) |
215 | return -ENOMEM; | |
216 | ||
217 | ht->shift++; | |
218 | ||
219 | /* For each new bucket, search the corresponding old bucket | |
0c828f2f | 220 | * for the first entry that hashes to the new bucket, and |
7e1e7763 TG |
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++) { | |
8d24c0b4 | 228 | h = rht_bucket_index(old_tbl, i); |
88d6ed15 | 229 | rht_for_each(he, old_tbl, h) { |
8d24c0b4 | 230 | if (head_hashfn(ht, new_tbl, he) == i) { |
7e1e7763 TG |
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 | |
0c828f2f HX |
238 | * the new table, but they will not benefit from any |
239 | * additional efficiency until later steps unzip the buckets. | |
7e1e7763 TG |
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 | |
7e1e7763 TG |
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 | */ | |
6eba8224 | 278 | int rhashtable_shrink(struct rhashtable *ht) |
7e1e7763 TG |
279 | { |
280 | struct bucket_table *ntbl, *tbl = rht_dereference(ht->tbl, ht); | |
7e1e7763 TG |
281 | unsigned int i; |
282 | ||
283 | ASSERT_RHT_MUTEX(ht); | |
284 | ||
94000176 | 285 | if (ht->shift <= ht->p.min_shift) |
7e1e7763 TG |
286 | return 0; |
287 | ||
6eba8224 | 288 | ntbl = bucket_table_alloc(tbl->size / 2); |
7e1e7763 TG |
289 | if (ntbl == NULL) |
290 | return -ENOMEM; | |
291 | ||
292 | ht->shift--; | |
293 | ||
0c828f2f | 294 | /* Link each bucket in the new table to the first bucket |
7e1e7763 TG |
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]; | |
b8e1943e TG |
300 | RCU_INIT_POINTER(*bucket_tail(ntbl, i), |
301 | tbl->buckets[i + ntbl->size]); | |
7e1e7763 | 302 | |
7e1e7763 TG |
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 | |
7e1e7763 TG |
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 | */ | |
6eba8224 | 330 | void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj) |
7e1e7763 TG |
331 | { |
332 | struct bucket_table *tbl = rht_dereference(ht->tbl, ht); | |
333 | u32 hash; | |
334 | ||
335 | ASSERT_RHT_MUTEX(ht); | |
336 | ||
8d24c0b4 | 337 | hash = head_hashfn(ht, tbl, obj); |
7e1e7763 TG |
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)) | |
6eba8224 | 343 | rhashtable_expand(ht); |
7e1e7763 TG |
344 | } |
345 | EXPORT_SYMBOL_GPL(rhashtable_insert); | |
346 | ||
347 | /** | |
348 | * rhashtable_remove_pprev - remove object from hash table given previous element | |
349 | * @ht: hash table | |
350 | * @obj: pointer to hash head inside object | |
351 | * @pprev: pointer to previous element | |
7e1e7763 TG |
352 | * |
353 | * Identical to rhashtable_remove() but caller is alreayd aware of the element | |
354 | * in front of the element to be deleted. This is in particular useful for | |
355 | * deletion when combined with walking or lookup. | |
356 | */ | |
357 | void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj, | |
6eba8224 | 358 | struct rhash_head __rcu **pprev) |
7e1e7763 TG |
359 | { |
360 | struct bucket_table *tbl = rht_dereference(ht->tbl, ht); | |
361 | ||
362 | ASSERT_RHT_MUTEX(ht); | |
363 | ||
364 | RCU_INIT_POINTER(*pprev, obj->next); | |
365 | ht->nelems--; | |
366 | ||
367 | if (ht->p.shrink_decision && | |
368 | ht->p.shrink_decision(ht, tbl->size)) | |
6eba8224 | 369 | rhashtable_shrink(ht); |
7e1e7763 TG |
370 | } |
371 | EXPORT_SYMBOL_GPL(rhashtable_remove_pprev); | |
372 | ||
373 | /** | |
374 | * rhashtable_remove - remove object from hash table | |
375 | * @ht: hash table | |
376 | * @obj: pointer to hash head inside object | |
7e1e7763 TG |
377 | * |
378 | * Since the hash chain is single linked, the removal operation needs to | |
379 | * walk the bucket chain upon removal. The removal operation is thus | |
380 | * considerable slow if the hash table is not correctly sized. | |
381 | * | |
382 | * Will automatically shrink the table via rhashtable_expand() if the the | |
383 | * shrink_decision function specified at rhashtable_init() returns true. | |
384 | * | |
385 | * The caller must ensure that no concurrent table mutations occur. It is | |
386 | * however valid to have concurrent lookups if they are RCU protected. | |
387 | */ | |
6eba8224 | 388 | bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) |
7e1e7763 TG |
389 | { |
390 | struct bucket_table *tbl = rht_dereference(ht->tbl, ht); | |
391 | struct rhash_head __rcu **pprev; | |
392 | struct rhash_head *he; | |
393 | u32 h; | |
394 | ||
395 | ASSERT_RHT_MUTEX(ht); | |
396 | ||
8d24c0b4 | 397 | h = head_hashfn(ht, tbl, obj); |
7e1e7763 TG |
398 | |
399 | pprev = &tbl->buckets[h]; | |
88d6ed15 | 400 | rht_for_each(he, tbl, h) { |
7e1e7763 TG |
401 | if (he != obj) { |
402 | pprev = &he->next; | |
403 | continue; | |
404 | } | |
405 | ||
6eba8224 | 406 | rhashtable_remove_pprev(ht, he, pprev); |
7e1e7763 TG |
407 | return true; |
408 | } | |
409 | ||
410 | return false; | |
411 | } | |
412 | EXPORT_SYMBOL_GPL(rhashtable_remove); | |
413 | ||
414 | /** | |
415 | * rhashtable_lookup - lookup key in hash table | |
416 | * @ht: hash table | |
417 | * @key: pointer to key | |
418 | * | |
419 | * Computes the hash value for the key and traverses the bucket chain looking | |
420 | * for a entry with an identical key. The first matching entry is returned. | |
421 | * | |
422 | * This lookup function may only be used for fixed key hash table (key_len | |
423 | * paramter set). It will BUG() if used inappropriately. | |
424 | * | |
425 | * Lookups may occur in parallel with hash mutations as long as the lookup is | |
426 | * guarded by rcu_read_lock(). The caller must take care of this. | |
427 | */ | |
428 | void *rhashtable_lookup(const struct rhashtable *ht, const void *key) | |
429 | { | |
430 | const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); | |
431 | struct rhash_head *he; | |
432 | u32 h; | |
433 | ||
434 | BUG_ON(!ht->p.key_len); | |
435 | ||
8d24c0b4 | 436 | h = key_hashfn(ht, key, ht->p.key_len); |
88d6ed15 | 437 | rht_for_each_rcu(he, tbl, h) { |
7e1e7763 TG |
438 | if (memcmp(rht_obj(ht, he) + ht->p.key_offset, key, |
439 | ht->p.key_len)) | |
440 | continue; | |
a4b18cda | 441 | return rht_obj(ht, he); |
7e1e7763 TG |
442 | } |
443 | ||
444 | return NULL; | |
445 | } | |
446 | EXPORT_SYMBOL_GPL(rhashtable_lookup); | |
447 | ||
448 | /** | |
449 | * rhashtable_lookup_compare - search hash table with compare function | |
450 | * @ht: hash table | |
8d24c0b4 | 451 | * @key: the pointer to the key |
7e1e7763 TG |
452 | * @compare: compare function, must return true on match |
453 | * @arg: argument passed on to compare function | |
454 | * | |
455 | * Traverses the bucket chain behind the provided hash value and calls the | |
456 | * specified compare function for each entry. | |
457 | * | |
458 | * Lookups may occur in parallel with hash mutations as long as the lookup is | |
459 | * guarded by rcu_read_lock(). The caller must take care of this. | |
460 | * | |
461 | * Returns the first entry on which the compare function returned true. | |
462 | */ | |
8d24c0b4 | 463 | void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key, |
7e1e7763 TG |
464 | bool (*compare)(void *, void *), void *arg) |
465 | { | |
466 | const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); | |
467 | struct rhash_head *he; | |
8d24c0b4 | 468 | u32 hash; |
7e1e7763 | 469 | |
8d24c0b4 | 470 | hash = key_hashfn(ht, key, ht->p.key_len); |
88d6ed15 | 471 | rht_for_each_rcu(he, tbl, hash) { |
7e1e7763 TG |
472 | if (!compare(rht_obj(ht, he), arg)) |
473 | continue; | |
a4b18cda | 474 | return rht_obj(ht, he); |
7e1e7763 TG |
475 | } |
476 | ||
477 | return NULL; | |
478 | } | |
479 | EXPORT_SYMBOL_GPL(rhashtable_lookup_compare); | |
480 | ||
94000176 | 481 | static size_t rounded_hashtable_size(struct rhashtable_params *params) |
7e1e7763 | 482 | { |
94000176 YX |
483 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
484 | 1UL << params->min_shift); | |
7e1e7763 TG |
485 | } |
486 | ||
487 | /** | |
488 | * rhashtable_init - initialize a new hash table | |
489 | * @ht: hash table to be initialized | |
490 | * @params: configuration parameters | |
491 | * | |
492 | * Initializes a new hash table based on the provided configuration | |
493 | * parameters. A table can be configured either with a variable or | |
494 | * fixed length key: | |
495 | * | |
496 | * Configuration Example 1: Fixed length keys | |
497 | * struct test_obj { | |
498 | * int key; | |
499 | * void * my_member; | |
500 | * struct rhash_head node; | |
501 | * }; | |
502 | * | |
503 | * struct rhashtable_params params = { | |
504 | * .head_offset = offsetof(struct test_obj, node), | |
505 | * .key_offset = offsetof(struct test_obj, key), | |
506 | * .key_len = sizeof(int), | |
87545899 | 507 | * .hashfn = jhash, |
1b2f309d | 508 | * #ifdef CONFIG_PROVE_LOCKING |
7e1e7763 | 509 | * .mutex_is_held = &my_mutex_is_held, |
1b2f309d | 510 | * #endif |
7e1e7763 TG |
511 | * }; |
512 | * | |
513 | * Configuration Example 2: Variable length keys | |
514 | * struct test_obj { | |
515 | * [...] | |
516 | * struct rhash_head node; | |
517 | * }; | |
518 | * | |
519 | * u32 my_hash_fn(const void *data, u32 seed) | |
520 | * { | |
521 | * struct test_obj *obj = data; | |
522 | * | |
523 | * return [... hash ...]; | |
524 | * } | |
525 | * | |
526 | * struct rhashtable_params params = { | |
527 | * .head_offset = offsetof(struct test_obj, node), | |
87545899 | 528 | * .hashfn = jhash, |
7e1e7763 | 529 | * .obj_hashfn = my_hash_fn, |
1b2f309d | 530 | * #ifdef CONFIG_PROVE_LOCKING |
7e1e7763 | 531 | * .mutex_is_held = &my_mutex_is_held, |
1b2f309d | 532 | * #endif |
7e1e7763 TG |
533 | * }; |
534 | */ | |
535 | int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params) | |
536 | { | |
537 | struct bucket_table *tbl; | |
538 | size_t size; | |
539 | ||
540 | size = HASH_DEFAULT_SIZE; | |
541 | ||
542 | if ((params->key_len && !params->hashfn) || | |
543 | (!params->key_len && !params->obj_hashfn)) | |
544 | return -EINVAL; | |
545 | ||
94000176 YX |
546 | params->min_shift = max_t(size_t, params->min_shift, |
547 | ilog2(HASH_MIN_SIZE)); | |
548 | ||
7e1e7763 | 549 | if (params->nelem_hint) |
94000176 | 550 | size = rounded_hashtable_size(params); |
7e1e7763 | 551 | |
6eba8224 | 552 | tbl = bucket_table_alloc(size); |
7e1e7763 TG |
553 | if (tbl == NULL) |
554 | return -ENOMEM; | |
555 | ||
556 | memset(ht, 0, sizeof(*ht)); | |
557 | ht->shift = ilog2(tbl->size); | |
558 | memcpy(&ht->p, params, sizeof(*params)); | |
559 | RCU_INIT_POINTER(ht->tbl, tbl); | |
560 | ||
561 | if (!ht->p.hash_rnd) | |
562 | get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd)); | |
563 | ||
564 | return 0; | |
565 | } | |
566 | EXPORT_SYMBOL_GPL(rhashtable_init); | |
567 | ||
568 | /** | |
569 | * rhashtable_destroy - destroy hash table | |
570 | * @ht: the hash table to destroy | |
571 | * | |
ae82ddcf PNA |
572 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
573 | * has to make sure that no resizing may happen by unpublishing the hashtable | |
574 | * and waiting for the quiescent cycle before releasing the bucket array. | |
7e1e7763 TG |
575 | */ |
576 | void rhashtable_destroy(const struct rhashtable *ht) | |
577 | { | |
ae82ddcf | 578 | bucket_table_free(ht->tbl); |
7e1e7763 TG |
579 | } |
580 | EXPORT_SYMBOL_GPL(rhashtable_destroy); | |
581 | ||
582 | /************************************************************************** | |
583 | * Self Test | |
584 | **************************************************************************/ | |
585 | ||
586 | #ifdef CONFIG_TEST_RHASHTABLE | |
587 | ||
588 | #define TEST_HT_SIZE 8 | |
589 | #define TEST_ENTRIES 2048 | |
590 | #define TEST_PTR ((void *) 0xdeadbeef) | |
591 | #define TEST_NEXPANDS 4 | |
592 | ||
1b2f309d | 593 | #ifdef CONFIG_PROVE_LOCKING |
7b4ce235 | 594 | static int test_mutex_is_held(void *parent) |
7e1e7763 TG |
595 | { |
596 | return 1; | |
597 | } | |
1b2f309d | 598 | #endif |
7e1e7763 TG |
599 | |
600 | struct test_obj { | |
601 | void *ptr; | |
602 | int value; | |
603 | struct rhash_head node; | |
604 | }; | |
605 | ||
606 | static int __init test_rht_lookup(struct rhashtable *ht) | |
607 | { | |
608 | unsigned int i; | |
609 | ||
610 | for (i = 0; i < TEST_ENTRIES * 2; i++) { | |
611 | struct test_obj *obj; | |
612 | bool expected = !(i % 2); | |
613 | u32 key = i; | |
614 | ||
615 | obj = rhashtable_lookup(ht, &key); | |
616 | ||
617 | if (expected && !obj) { | |
618 | pr_warn("Test failed: Could not find key %u\n", key); | |
619 | return -ENOENT; | |
620 | } else if (!expected && obj) { | |
621 | pr_warn("Test failed: Unexpected entry found for key %u\n", | |
622 | key); | |
623 | return -EEXIST; | |
624 | } else if (expected && obj) { | |
625 | if (obj->ptr != TEST_PTR || obj->value != i) { | |
626 | pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n", | |
627 | obj->ptr, TEST_PTR, obj->value, i); | |
628 | return -EINVAL; | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
633 | return 0; | |
634 | } | |
635 | ||
3e7b2ec4 | 636 | static void test_bucket_stats(struct rhashtable *ht, bool quiet) |
7e1e7763 | 637 | { |
3e7b2ec4 | 638 | unsigned int cnt, rcu_cnt, i, total = 0; |
88d6ed15 | 639 | struct rhash_head *pos; |
7e1e7763 | 640 | struct test_obj *obj; |
3e7b2ec4 | 641 | struct bucket_table *tbl; |
7e1e7763 | 642 | |
3e7b2ec4 | 643 | tbl = rht_dereference_rcu(ht->tbl, ht); |
7e1e7763 | 644 | for (i = 0; i < tbl->size; i++) { |
3e7b2ec4 | 645 | rcu_cnt = cnt = 0; |
7e1e7763 TG |
646 | |
647 | if (!quiet) | |
648 | pr_info(" [%#4x/%zu]", i, tbl->size); | |
649 | ||
88d6ed15 | 650 | rht_for_each_entry_rcu(obj, pos, tbl, i, node) { |
7e1e7763 TG |
651 | cnt++; |
652 | total++; | |
653 | if (!quiet) | |
654 | pr_cont(" [%p],", obj); | |
655 | } | |
656 | ||
88d6ed15 | 657 | rht_for_each_entry_rcu(obj, pos, tbl, i, node) |
3e7b2ec4 TG |
658 | rcu_cnt++; |
659 | ||
660 | if (rcu_cnt != cnt) | |
661 | pr_warn("Test failed: Chain count mismach %d != %d", | |
662 | cnt, rcu_cnt); | |
663 | ||
7e1e7763 TG |
664 | if (!quiet) |
665 | pr_cont("\n [%#x] first element: %p, chain length: %u\n", | |
666 | i, tbl->buckets[i], cnt); | |
667 | } | |
668 | ||
669 | pr_info(" Traversal complete: counted=%u, nelems=%zu, entries=%d\n", | |
670 | total, ht->nelems, TEST_ENTRIES); | |
3e7b2ec4 TG |
671 | |
672 | if (total != ht->nelems || total != TEST_ENTRIES) | |
673 | pr_warn("Test failed: Total count mismatch ^^^"); | |
7e1e7763 TG |
674 | } |
675 | ||
676 | static int __init test_rhashtable(struct rhashtable *ht) | |
677 | { | |
678 | struct bucket_table *tbl; | |
88d6ed15 TG |
679 | struct test_obj *obj; |
680 | struct rhash_head *pos, *next; | |
7e1e7763 TG |
681 | int err; |
682 | unsigned int i; | |
683 | ||
684 | /* | |
685 | * Insertion Test: | |
686 | * Insert TEST_ENTRIES into table with all keys even numbers | |
687 | */ | |
688 | pr_info(" Adding %d keys\n", TEST_ENTRIES); | |
689 | for (i = 0; i < TEST_ENTRIES; i++) { | |
690 | struct test_obj *obj; | |
691 | ||
692 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); | |
693 | if (!obj) { | |
694 | err = -ENOMEM; | |
695 | goto error; | |
696 | } | |
697 | ||
698 | obj->ptr = TEST_PTR; | |
699 | obj->value = i * 2; | |
700 | ||
6eba8224 | 701 | rhashtable_insert(ht, &obj->node); |
7e1e7763 TG |
702 | } |
703 | ||
704 | rcu_read_lock(); | |
3e7b2ec4 | 705 | test_bucket_stats(ht, true); |
7e1e7763 TG |
706 | test_rht_lookup(ht); |
707 | rcu_read_unlock(); | |
708 | ||
709 | for (i = 0; i < TEST_NEXPANDS; i++) { | |
710 | pr_info(" Table expansion iteration %u...\n", i); | |
6eba8224 | 711 | rhashtable_expand(ht); |
7e1e7763 TG |
712 | |
713 | rcu_read_lock(); | |
714 | pr_info(" Verifying lookups...\n"); | |
715 | test_rht_lookup(ht); | |
716 | rcu_read_unlock(); | |
717 | } | |
718 | ||
719 | for (i = 0; i < TEST_NEXPANDS; i++) { | |
720 | pr_info(" Table shrinkage iteration %u...\n", i); | |
6eba8224 | 721 | rhashtable_shrink(ht); |
7e1e7763 TG |
722 | |
723 | rcu_read_lock(); | |
724 | pr_info(" Verifying lookups...\n"); | |
725 | test_rht_lookup(ht); | |
726 | rcu_read_unlock(); | |
727 | } | |
728 | ||
3e7b2ec4 TG |
729 | rcu_read_lock(); |
730 | test_bucket_stats(ht, true); | |
731 | rcu_read_unlock(); | |
732 | ||
7e1e7763 TG |
733 | pr_info(" Deleting %d keys\n", TEST_ENTRIES); |
734 | for (i = 0; i < TEST_ENTRIES; i++) { | |
735 | u32 key = i * 2; | |
736 | ||
737 | obj = rhashtable_lookup(ht, &key); | |
738 | BUG_ON(!obj); | |
739 | ||
6eba8224 | 740 | rhashtable_remove(ht, &obj->node); |
7e1e7763 TG |
741 | kfree(obj); |
742 | } | |
743 | ||
744 | return 0; | |
745 | ||
746 | error: | |
747 | tbl = rht_dereference_rcu(ht->tbl, ht); | |
748 | for (i = 0; i < tbl->size; i++) | |
88d6ed15 | 749 | rht_for_each_entry_safe(obj, pos, next, tbl, i, node) |
7e1e7763 TG |
750 | kfree(obj); |
751 | ||
752 | return err; | |
753 | } | |
754 | ||
755 | static int __init test_rht_init(void) | |
756 | { | |
757 | struct rhashtable ht; | |
758 | struct rhashtable_params params = { | |
759 | .nelem_hint = TEST_HT_SIZE, | |
760 | .head_offset = offsetof(struct test_obj, node), | |
761 | .key_offset = offsetof(struct test_obj, value), | |
762 | .key_len = sizeof(int), | |
87545899 | 763 | .hashfn = jhash, |
1b2f309d | 764 | #ifdef CONFIG_PROVE_LOCKING |
7e1e7763 | 765 | .mutex_is_held = &test_mutex_is_held, |
1b2f309d | 766 | #endif |
7e1e7763 TG |
767 | .grow_decision = rht_grow_above_75, |
768 | .shrink_decision = rht_shrink_below_30, | |
769 | }; | |
770 | int err; | |
771 | ||
772 | pr_info("Running resizable hashtable tests...\n"); | |
773 | ||
774 | err = rhashtable_init(&ht, ¶ms); | |
775 | if (err < 0) { | |
776 | pr_warn("Test failed: Unable to initialize hashtable: %d\n", | |
777 | err); | |
778 | return err; | |
779 | } | |
780 | ||
781 | err = test_rhashtable(&ht); | |
782 | ||
783 | rhashtable_destroy(&ht); | |
784 | ||
785 | return err; | |
786 | } | |
787 | ||
788 | subsys_initcall(test_rht_init); | |
789 | ||
790 | #endif /* CONFIG_TEST_RHASHTABLE */ |