]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - lib/test_rhashtable.c
radix tree: fix multi-order iteration race
[mirror_ubuntu-bionic-kernel.git] / lib / test_rhashtable.c
1 /*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
4 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12 /**************************************************************************
13 * Self Test
14 **************************************************************************/
15
16 #include <linux/init.h>
17 #include <linux/jhash.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/module.h>
21 #include <linux/rcupdate.h>
22 #include <linux/rhashtable.h>
23 #include <linux/semaphore.h>
24 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/random.h>
27 #include <linux/vmalloc.h>
28
29 #define MAX_ENTRIES 1000000
30 #define TEST_INSERT_FAIL INT_MAX
31
32 static int parm_entries = 50000;
33 module_param(parm_entries, int, 0);
34 MODULE_PARM_DESC(parm_entries, "Number of entries to add (default: 50000)");
35
36 static int runs = 4;
37 module_param(runs, int, 0);
38 MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
39
40 static int max_size = 0;
41 module_param(max_size, int, 0);
42 MODULE_PARM_DESC(max_size, "Maximum table size (default: calculated)");
43
44 static bool shrinking = false;
45 module_param(shrinking, bool, 0);
46 MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
47
48 static int size = 8;
49 module_param(size, int, 0);
50 MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
51
52 static int tcount = 10;
53 module_param(tcount, int, 0);
54 MODULE_PARM_DESC(tcount, "Number of threads to spawn (default: 10)");
55
56 static bool enomem_retry = false;
57 module_param(enomem_retry, bool, 0);
58 MODULE_PARM_DESC(enomem_retry, "Retry insert even if -ENOMEM was returned (default: off)");
59
60 struct test_obj_val {
61 int id;
62 int tid;
63 };
64
65 struct test_obj {
66 struct test_obj_val value;
67 struct rhash_head node;
68 };
69
70 struct test_obj_rhl {
71 struct test_obj_val value;
72 struct rhlist_head list_node;
73 };
74
75 struct thread_data {
76 unsigned int entries;
77 int id;
78 struct task_struct *task;
79 struct test_obj *objs;
80 };
81
82 static u32 my_hashfn(const void *data, u32 len, u32 seed)
83 {
84 const struct test_obj_rhl *obj = data;
85
86 return (obj->value.id % 10) << RHT_HASH_RESERVED_SPACE;
87 }
88
89 static int my_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
90 {
91 const struct test_obj_rhl *test_obj = obj;
92 const struct test_obj_val *val = arg->key;
93
94 return test_obj->value.id - val->id;
95 }
96
97 static struct rhashtable_params test_rht_params = {
98 .head_offset = offsetof(struct test_obj, node),
99 .key_offset = offsetof(struct test_obj, value),
100 .key_len = sizeof(struct test_obj_val),
101 .hashfn = jhash,
102 .nulls_base = (3U << RHT_BASE_SHIFT),
103 };
104
105 static struct rhashtable_params test_rht_params_dup = {
106 .head_offset = offsetof(struct test_obj_rhl, list_node),
107 .key_offset = offsetof(struct test_obj_rhl, value),
108 .key_len = sizeof(struct test_obj_val),
109 .hashfn = jhash,
110 .obj_hashfn = my_hashfn,
111 .obj_cmpfn = my_cmpfn,
112 .nelem_hint = 128,
113 .automatic_shrinking = false,
114 };
115
116 static struct semaphore prestart_sem;
117 static struct semaphore startup_sem = __SEMAPHORE_INITIALIZER(startup_sem, 0);
118
119 static int insert_retry(struct rhashtable *ht, struct test_obj *obj,
120 const struct rhashtable_params params)
121 {
122 int err, retries = -1, enomem_retries = 0;
123
124 do {
125 retries++;
126 cond_resched();
127 err = rhashtable_insert_fast(ht, &obj->node, params);
128 if (err == -ENOMEM && enomem_retry) {
129 enomem_retries++;
130 err = -EBUSY;
131 }
132 } while (err == -EBUSY);
133
134 if (enomem_retries)
135 pr_info(" %u insertions retried after -ENOMEM\n",
136 enomem_retries);
137
138 return err ? : retries;
139 }
140
141 static int __init test_rht_lookup(struct rhashtable *ht, struct test_obj *array,
142 unsigned int entries)
143 {
144 unsigned int i;
145
146 for (i = 0; i < entries; i++) {
147 struct test_obj *obj;
148 bool expected = !(i % 2);
149 struct test_obj_val key = {
150 .id = i,
151 };
152
153 if (array[i / 2].value.id == TEST_INSERT_FAIL)
154 expected = false;
155
156 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
157
158 if (expected && !obj) {
159 pr_warn("Test failed: Could not find key %u\n", key.id);
160 return -ENOENT;
161 } else if (!expected && obj) {
162 pr_warn("Test failed: Unexpected entry found for key %u\n",
163 key.id);
164 return -EEXIST;
165 } else if (expected && obj) {
166 if (obj->value.id != i) {
167 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
168 obj->value.id, i);
169 return -EINVAL;
170 }
171 }
172
173 cond_resched_rcu();
174 }
175
176 return 0;
177 }
178
179 static void test_bucket_stats(struct rhashtable *ht, unsigned int entries)
180 {
181 unsigned int err, total = 0, chain_len = 0;
182 struct rhashtable_iter hti;
183 struct rhash_head *pos;
184
185 err = rhashtable_walk_init(ht, &hti, GFP_KERNEL);
186 if (err) {
187 pr_warn("Test failed: allocation error");
188 return;
189 }
190
191 err = rhashtable_walk_start(&hti);
192 if (err && err != -EAGAIN) {
193 pr_warn("Test failed: iterator failed: %d\n", err);
194 return;
195 }
196
197 while ((pos = rhashtable_walk_next(&hti))) {
198 if (PTR_ERR(pos) == -EAGAIN) {
199 pr_info("Info: encountered resize\n");
200 chain_len++;
201 continue;
202 } else if (IS_ERR(pos)) {
203 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
204 PTR_ERR(pos));
205 break;
206 }
207
208 total++;
209 }
210
211 rhashtable_walk_stop(&hti);
212 rhashtable_walk_exit(&hti);
213
214 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
215 total, atomic_read(&ht->nelems), entries, chain_len);
216
217 if (total != atomic_read(&ht->nelems) || total != entries)
218 pr_warn("Test failed: Total count mismatch ^^^");
219 }
220
221 static s64 __init test_rhashtable(struct rhashtable *ht, struct test_obj *array,
222 unsigned int entries)
223 {
224 struct test_obj *obj;
225 int err;
226 unsigned int i, insert_retries = 0;
227 s64 start, end;
228
229 /*
230 * Insertion Test:
231 * Insert entries into table with all keys even numbers
232 */
233 pr_info(" Adding %d keys\n", entries);
234 start = ktime_get_ns();
235 for (i = 0; i < entries; i++) {
236 struct test_obj *obj = &array[i];
237
238 obj->value.id = i * 2;
239 err = insert_retry(ht, obj, test_rht_params);
240 if (err > 0)
241 insert_retries += err;
242 else if (err)
243 return err;
244 }
245
246 if (insert_retries)
247 pr_info(" %u insertions retried due to memory pressure\n",
248 insert_retries);
249
250 test_bucket_stats(ht, entries);
251 rcu_read_lock();
252 test_rht_lookup(ht, array, entries);
253 rcu_read_unlock();
254
255 test_bucket_stats(ht, entries);
256
257 pr_info(" Deleting %d keys\n", entries);
258 for (i = 0; i < entries; i++) {
259 struct test_obj_val key = {
260 .id = i * 2,
261 };
262
263 if (array[i].value.id != TEST_INSERT_FAIL) {
264 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
265 BUG_ON(!obj);
266
267 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
268 }
269
270 cond_resched();
271 }
272
273 end = ktime_get_ns();
274 pr_info(" Duration of test: %lld ns\n", end - start);
275
276 return end - start;
277 }
278
279 static struct rhashtable ht;
280 static struct rhltable rhlt;
281
282 static int __init test_rhltable(unsigned int entries)
283 {
284 struct test_obj_rhl *rhl_test_objects;
285 unsigned long *obj_in_table;
286 unsigned int i, j, k;
287 int ret, err;
288
289 if (entries == 0)
290 entries = 1;
291
292 rhl_test_objects = vzalloc(sizeof(*rhl_test_objects) * entries);
293 if (!rhl_test_objects)
294 return -ENOMEM;
295
296 ret = -ENOMEM;
297 obj_in_table = vzalloc(BITS_TO_LONGS(entries) * sizeof(unsigned long));
298 if (!obj_in_table)
299 goto out_free;
300
301 /* nulls_base not supported in rhlist interface */
302 test_rht_params.nulls_base = 0;
303 err = rhltable_init(&rhlt, &test_rht_params);
304 if (WARN_ON(err))
305 goto out_free;
306
307 k = prandom_u32();
308 ret = 0;
309 for (i = 0; i < entries; i++) {
310 rhl_test_objects[i].value.id = k;
311 err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node,
312 test_rht_params);
313 if (WARN(err, "error %d on element %d\n", err, i))
314 break;
315 if (err == 0)
316 set_bit(i, obj_in_table);
317 }
318
319 if (err)
320 ret = err;
321
322 pr_info("test %d add/delete pairs into rhlist\n", entries);
323 for (i = 0; i < entries; i++) {
324 struct rhlist_head *h, *pos;
325 struct test_obj_rhl *obj;
326 struct test_obj_val key = {
327 .id = k,
328 };
329 bool found;
330
331 rcu_read_lock();
332 h = rhltable_lookup(&rhlt, &key, test_rht_params);
333 if (WARN(!h, "key not found during iteration %d of %d", i, entries)) {
334 rcu_read_unlock();
335 break;
336 }
337
338 if (i) {
339 j = i - 1;
340 rhl_for_each_entry_rcu(obj, pos, h, list_node) {
341 if (WARN(pos == &rhl_test_objects[j].list_node, "old element found, should be gone"))
342 break;
343 }
344 }
345
346 cond_resched_rcu();
347
348 found = false;
349
350 rhl_for_each_entry_rcu(obj, pos, h, list_node) {
351 if (pos == &rhl_test_objects[i].list_node) {
352 found = true;
353 break;
354 }
355 }
356
357 rcu_read_unlock();
358
359 if (WARN(!found, "element %d not found", i))
360 break;
361
362 err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
363 WARN(err, "rhltable_remove: err %d for iteration %d\n", err, i);
364 if (err == 0)
365 clear_bit(i, obj_in_table);
366 }
367
368 if (ret == 0 && err)
369 ret = err;
370
371 for (i = 0; i < entries; i++) {
372 WARN(test_bit(i, obj_in_table), "elem %d allegedly still present", i);
373
374 err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node,
375 test_rht_params);
376 if (WARN(err, "error %d on element %d\n", err, i))
377 break;
378 if (err == 0)
379 set_bit(i, obj_in_table);
380 }
381
382 pr_info("test %d random rhlist add/delete operations\n", entries);
383 for (j = 0; j < entries; j++) {
384 u32 i = prandom_u32_max(entries);
385 u32 prand = prandom_u32();
386
387 cond_resched();
388
389 if (prand == 0)
390 prand = prandom_u32();
391
392 if (prand & 1) {
393 prand >>= 1;
394 continue;
395 }
396
397 err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
398 if (test_bit(i, obj_in_table)) {
399 clear_bit(i, obj_in_table);
400 if (WARN(err, "cannot remove element at slot %d", i))
401 continue;
402 } else {
403 if (WARN(err != -ENOENT, "removed non-existant element %d, error %d not %d",
404 i, err, -ENOENT))
405 continue;
406 }
407
408 if (prand & 1) {
409 prand >>= 1;
410 continue;
411 }
412
413 err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
414 if (err == 0) {
415 if (WARN(test_and_set_bit(i, obj_in_table), "succeeded to insert same object %d", i))
416 continue;
417 } else {
418 if (WARN(!test_bit(i, obj_in_table), "failed to insert object %d", i))
419 continue;
420 }
421
422 if (prand & 1) {
423 prand >>= 1;
424 continue;
425 }
426
427 i = prandom_u32_max(entries);
428 if (test_bit(i, obj_in_table)) {
429 err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
430 WARN(err, "cannot remove element at slot %d", i);
431 if (err == 0)
432 clear_bit(i, obj_in_table);
433 } else {
434 err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
435 WARN(err, "failed to insert object %d", i);
436 if (err == 0)
437 set_bit(i, obj_in_table);
438 }
439 }
440
441 for (i = 0; i < entries; i++) {
442 cond_resched();
443 err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
444 if (test_bit(i, obj_in_table)) {
445 if (WARN(err, "cannot remove element at slot %d", i))
446 continue;
447 } else {
448 if (WARN(err != -ENOENT, "removed non-existant element, error %d not %d",
449 err, -ENOENT))
450 continue;
451 }
452 }
453
454 rhltable_destroy(&rhlt);
455 out_free:
456 vfree(rhl_test_objects);
457 vfree(obj_in_table);
458 return ret;
459 }
460
461 static int __init test_rhashtable_max(struct test_obj *array,
462 unsigned int entries)
463 {
464 unsigned int i, insert_retries = 0;
465 int err;
466
467 test_rht_params.max_size = roundup_pow_of_two(entries / 8);
468 err = rhashtable_init(&ht, &test_rht_params);
469 if (err)
470 return err;
471
472 for (i = 0; i < ht.max_elems; i++) {
473 struct test_obj *obj = &array[i];
474
475 obj->value.id = i * 2;
476 err = insert_retry(&ht, obj, test_rht_params);
477 if (err > 0)
478 insert_retries += err;
479 else if (err)
480 return err;
481 }
482
483 err = insert_retry(&ht, &array[ht.max_elems], test_rht_params);
484 if (err == -E2BIG) {
485 err = 0;
486 } else {
487 pr_info("insert element %u should have failed with %d, got %d\n",
488 ht.max_elems, -E2BIG, err);
489 if (err == 0)
490 err = -1;
491 }
492
493 rhashtable_destroy(&ht);
494
495 return err;
496 }
497
498 static unsigned int __init print_ht(struct rhltable *rhlt)
499 {
500 struct rhashtable *ht;
501 const struct bucket_table *tbl;
502 char buff[512] = "";
503 unsigned int i, cnt = 0;
504
505 ht = &rhlt->ht;
506 tbl = rht_dereference(ht->tbl, ht);
507 for (i = 0; i < tbl->size; i++) {
508 struct rhash_head *pos, *next;
509 struct test_obj_rhl *p;
510
511 pos = rht_dereference(tbl->buckets[i], ht);
512 next = !rht_is_a_nulls(pos) ? rht_dereference(pos->next, ht) : NULL;
513
514 if (!rht_is_a_nulls(pos)) {
515 sprintf(buff, "%s\nbucket[%d] -> ", buff, i);
516 }
517
518 while (!rht_is_a_nulls(pos)) {
519 struct rhlist_head *list = container_of(pos, struct rhlist_head, rhead);
520 sprintf(buff, "%s[[", buff);
521 do {
522 pos = &list->rhead;
523 list = rht_dereference(list->next, ht);
524 p = rht_obj(ht, pos);
525
526 sprintf(buff, "%s val %d (tid=%d)%s", buff, p->value.id, p->value.tid,
527 list? ", " : " ");
528 cnt++;
529 } while (list);
530
531 pos = next,
532 next = !rht_is_a_nulls(pos) ?
533 rht_dereference(pos->next, ht) : NULL;
534
535 sprintf(buff, "%s]]%s", buff, !rht_is_a_nulls(pos) ? " -> " : "");
536 }
537 }
538 printk(KERN_ERR "\n---- ht: ----%s\n-------------\n", buff);
539
540 return cnt;
541 }
542
543 static int __init test_insert_dup(struct test_obj_rhl *rhl_test_objects,
544 int cnt, bool slow)
545 {
546 struct rhltable rhlt;
547 unsigned int i, ret;
548 const char *key;
549 int err = 0;
550
551 err = rhltable_init(&rhlt, &test_rht_params_dup);
552 if (WARN_ON(err))
553 return err;
554
555 for (i = 0; i < cnt; i++) {
556 rhl_test_objects[i].value.tid = i;
557 key = rht_obj(&rhlt.ht, &rhl_test_objects[i].list_node.rhead);
558 key += test_rht_params_dup.key_offset;
559
560 if (slow) {
561 err = PTR_ERR(rhashtable_insert_slow(&rhlt.ht, key,
562 &rhl_test_objects[i].list_node.rhead));
563 if (err == -EAGAIN)
564 err = 0;
565 } else
566 err = rhltable_insert(&rhlt,
567 &rhl_test_objects[i].list_node,
568 test_rht_params_dup);
569 if (WARN(err, "error %d on element %d/%d (%s)\n", err, i, cnt, slow? "slow" : "fast"))
570 goto skip_print;
571 }
572
573 ret = print_ht(&rhlt);
574 WARN(ret != cnt, "missing rhltable elements (%d != %d, %s)\n", ret, cnt, slow? "slow" : "fast");
575
576 skip_print:
577 rhltable_destroy(&rhlt);
578
579 return 0;
580 }
581
582 static int __init test_insert_duplicates_run(void)
583 {
584 struct test_obj_rhl rhl_test_objects[3] = {};
585
586 pr_info("test inserting duplicates\n");
587
588 /* two different values that map to same bucket */
589 rhl_test_objects[0].value.id = 1;
590 rhl_test_objects[1].value.id = 21;
591
592 /* and another duplicate with same as [0] value
593 * which will be second on the bucket list */
594 rhl_test_objects[2].value.id = rhl_test_objects[0].value.id;
595
596 test_insert_dup(rhl_test_objects, 2, false);
597 test_insert_dup(rhl_test_objects, 3, false);
598 test_insert_dup(rhl_test_objects, 2, true);
599 test_insert_dup(rhl_test_objects, 3, true);
600
601 return 0;
602 }
603
604 static int thread_lookup_test(struct thread_data *tdata)
605 {
606 unsigned int entries = tdata->entries;
607 int i, err = 0;
608
609 for (i = 0; i < entries; i++) {
610 struct test_obj *obj;
611 struct test_obj_val key = {
612 .id = i,
613 .tid = tdata->id,
614 };
615
616 obj = rhashtable_lookup_fast(&ht, &key, test_rht_params);
617 if (obj && (tdata->objs[i].value.id == TEST_INSERT_FAIL)) {
618 pr_err(" found unexpected object %d-%d\n", key.tid, key.id);
619 err++;
620 } else if (!obj && (tdata->objs[i].value.id != TEST_INSERT_FAIL)) {
621 pr_err(" object %d-%d not found!\n", key.tid, key.id);
622 err++;
623 } else if (obj && memcmp(&obj->value, &key, sizeof(key))) {
624 pr_err(" wrong object returned (got %d-%d, expected %d-%d)\n",
625 obj->value.tid, obj->value.id, key.tid, key.id);
626 err++;
627 }
628
629 cond_resched();
630 }
631 return err;
632 }
633
634 static int threadfunc(void *data)
635 {
636 int i, step, err = 0, insert_retries = 0;
637 struct thread_data *tdata = data;
638
639 up(&prestart_sem);
640 if (down_interruptible(&startup_sem))
641 pr_err(" thread[%d]: down_interruptible failed\n", tdata->id);
642
643 for (i = 0; i < tdata->entries; i++) {
644 tdata->objs[i].value.id = i;
645 tdata->objs[i].value.tid = tdata->id;
646 err = insert_retry(&ht, &tdata->objs[i], test_rht_params);
647 if (err > 0) {
648 insert_retries += err;
649 } else if (err) {
650 pr_err(" thread[%d]: rhashtable_insert_fast failed\n",
651 tdata->id);
652 goto out;
653 }
654 }
655 if (insert_retries)
656 pr_info(" thread[%d]: %u insertions retried due to memory pressure\n",
657 tdata->id, insert_retries);
658
659 err = thread_lookup_test(tdata);
660 if (err) {
661 pr_err(" thread[%d]: rhashtable_lookup_test failed\n",
662 tdata->id);
663 goto out;
664 }
665
666 for (step = 10; step > 0; step--) {
667 for (i = 0; i < tdata->entries; i += step) {
668 if (tdata->objs[i].value.id == TEST_INSERT_FAIL)
669 continue;
670 err = rhashtable_remove_fast(&ht, &tdata->objs[i].node,
671 test_rht_params);
672 if (err) {
673 pr_err(" thread[%d]: rhashtable_remove_fast failed\n",
674 tdata->id);
675 goto out;
676 }
677 tdata->objs[i].value.id = TEST_INSERT_FAIL;
678
679 cond_resched();
680 }
681 err = thread_lookup_test(tdata);
682 if (err) {
683 pr_err(" thread[%d]: rhashtable_lookup_test (2) failed\n",
684 tdata->id);
685 goto out;
686 }
687 }
688 out:
689 while (!kthread_should_stop()) {
690 set_current_state(TASK_INTERRUPTIBLE);
691 schedule();
692 }
693 return err;
694 }
695
696 static int __init test_rht_init(void)
697 {
698 unsigned int entries;
699 int i, err, started_threads = 0, failed_threads = 0;
700 u64 total_time = 0;
701 struct thread_data *tdata;
702 struct test_obj *objs;
703
704 if (parm_entries < 0)
705 parm_entries = 1;
706
707 entries = min(parm_entries, MAX_ENTRIES);
708
709 test_rht_params.automatic_shrinking = shrinking;
710 test_rht_params.max_size = max_size ? : roundup_pow_of_two(entries);
711 test_rht_params.nelem_hint = size;
712
713 objs = vzalloc((test_rht_params.max_size + 1) * sizeof(struct test_obj));
714 if (!objs)
715 return -ENOMEM;
716
717 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
718 size, max_size, shrinking);
719
720 for (i = 0; i < runs; i++) {
721 s64 time;
722
723 pr_info("Test %02d:\n", i);
724 memset(objs, 0, test_rht_params.max_size * sizeof(struct test_obj));
725
726 err = rhashtable_init(&ht, &test_rht_params);
727 if (err < 0) {
728 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
729 err);
730 continue;
731 }
732
733 time = test_rhashtable(&ht, objs, entries);
734 rhashtable_destroy(&ht);
735 if (time < 0) {
736 vfree(objs);
737 pr_warn("Test failed: return code %lld\n", time);
738 return -EINVAL;
739 }
740
741 total_time += time;
742 }
743
744 pr_info("test if its possible to exceed max_size %d: %s\n",
745 test_rht_params.max_size, test_rhashtable_max(objs, entries) == 0 ?
746 "no, ok" : "YES, failed");
747 vfree(objs);
748
749 do_div(total_time, runs);
750 pr_info("Average test time: %llu\n", total_time);
751
752 test_insert_duplicates_run();
753
754 if (!tcount)
755 return 0;
756
757 pr_info("Testing concurrent rhashtable access from %d threads\n",
758 tcount);
759 sema_init(&prestart_sem, 1 - tcount);
760 tdata = vzalloc(tcount * sizeof(struct thread_data));
761 if (!tdata)
762 return -ENOMEM;
763 objs = vzalloc(tcount * entries * sizeof(struct test_obj));
764 if (!objs) {
765 vfree(tdata);
766 return -ENOMEM;
767 }
768
769 test_rht_params.max_size = max_size ? :
770 roundup_pow_of_two(tcount * entries);
771 err = rhashtable_init(&ht, &test_rht_params);
772 if (err < 0) {
773 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
774 err);
775 vfree(tdata);
776 vfree(objs);
777 return -EINVAL;
778 }
779 for (i = 0; i < tcount; i++) {
780 tdata[i].id = i;
781 tdata[i].entries = entries;
782 tdata[i].objs = objs + i * entries;
783 tdata[i].task = kthread_run(threadfunc, &tdata[i],
784 "rhashtable_thrad[%d]", i);
785 if (IS_ERR(tdata[i].task))
786 pr_err(" kthread_run failed for thread %d\n", i);
787 else
788 started_threads++;
789 }
790 if (down_interruptible(&prestart_sem))
791 pr_err(" down interruptible failed\n");
792 for (i = 0; i < tcount; i++)
793 up(&startup_sem);
794 for (i = 0; i < tcount; i++) {
795 if (IS_ERR(tdata[i].task))
796 continue;
797 if ((err = kthread_stop(tdata[i].task))) {
798 pr_warn("Test failed: thread %d returned: %d\n",
799 i, err);
800 failed_threads++;
801 }
802 }
803 rhashtable_destroy(&ht);
804 vfree(tdata);
805 vfree(objs);
806
807 /*
808 * rhltable_remove is very expensive, default values can cause test
809 * to run for 2 minutes or more, use a smaller number instead.
810 */
811 err = test_rhltable(entries / 16);
812 pr_info("Started %d threads, %d failed, rhltable test returns %d\n",
813 started_threads, failed_threads, err);
814 return 0;
815 }
816
817 static void __exit test_rht_exit(void)
818 {
819 }
820
821 module_init(test_rht_init);
822 module_exit(test_rht_exit);
823
824 MODULE_LICENSE("GPL v2");