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