1 // SPDX-License-Identifier: GPL-2.0
4 * Test module for stress and analyze performance of vmalloc allocator.
5 * (C) 2018 Uladzislau Rezki (Sony) <urezki@gmail.com>
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/vmalloc.h>
11 #include <linux/random.h>
12 #include <linux/kthread.h>
13 #include <linux/moduleparam.h>
14 #include <linux/completion.h>
15 #include <linux/delay.h>
16 #include <linux/rwsem.h>
18 #include <linux/rcupdate.h>
19 #include <linux/slab.h>
21 #define __param(type, name, init, msg) \
22 static type name = init; \
23 module_param(name, type, 0444); \
24 MODULE_PARM_DESC(name, msg) \
26 __param(int, nr_threads, 0,
27 "Number of workers to perform tests(min: 1 max: USHRT_MAX)");
29 __param(bool, sequential_test_order, false,
30 "Use sequential stress tests order");
32 __param(int, test_repeat_count, 1,
33 "Set test repeat counter");
35 __param(int, test_loop_count, 1000000,
36 "Set test loop counter");
38 __param(int, nr_pages, 0,
39 "Set number of pages for fix_size_alloc_test(default: 1)");
41 __param(int, run_test_mask, INT_MAX,
42 "Set tests specified in the mask.\n\n"
43 "\t\tid: 1, name: fix_size_alloc_test\n"
44 "\t\tid: 2, name: full_fit_alloc_test\n"
45 "\t\tid: 4, name: long_busy_list_alloc_test\n"
46 "\t\tid: 8, name: random_size_alloc_test\n"
47 "\t\tid: 16, name: fix_align_alloc_test\n"
48 "\t\tid: 32, name: random_size_align_alloc_test\n"
49 "\t\tid: 64, name: align_shift_alloc_test\n"
50 "\t\tid: 128, name: pcpu_alloc_test\n"
51 "\t\tid: 256, name: kvfree_rcu_1_arg_vmalloc_test\n"
52 "\t\tid: 512, name: kvfree_rcu_2_arg_vmalloc_test\n"
53 /* Add a new test case description here. */
57 * Read write semaphore for synchronization of setup
58 * phase that is done in main thread and workers.
60 static DECLARE_RWSEM(prepare_for_test_rwsem);
63 * Completion tracking for worker threads.
65 static DECLARE_COMPLETION(test_all_done_comp);
66 static atomic_t test_n_undone = ATOMIC_INIT(0);
69 test_report_one_done(void)
71 if (atomic_dec_and_test(&test_n_undone))
72 complete(&test_all_done_comp);
75 static int random_size_align_alloc_test(void)
77 unsigned long size, align, rnd;
81 for (i = 0; i < test_loop_count; i++) {
82 get_random_bytes(&rnd, sizeof(rnd));
85 * Maximum 1024 pages, if PAGE_SIZE is 4096.
87 align = 1 << (rnd % 23);
92 size = ((rnd % 10) + 1) * PAGE_SIZE;
94 ptr = __vmalloc_node(size, align, GFP_KERNEL | __GFP_ZERO, 0,
95 __builtin_return_address(0));
106 * This test case is supposed to be failed.
108 static int align_shift_alloc_test(void)
114 for (i = 0; i < BITS_PER_LONG; i++) {
115 align = ((unsigned long) 1) << i;
117 ptr = __vmalloc_node(PAGE_SIZE, align, GFP_KERNEL|__GFP_ZERO, 0,
118 __builtin_return_address(0));
128 static int fix_align_alloc_test(void)
133 for (i = 0; i < test_loop_count; i++) {
134 ptr = __vmalloc_node(5 * PAGE_SIZE, THREAD_ALIGN << 1,
135 GFP_KERNEL | __GFP_ZERO, 0,
136 __builtin_return_address(0));
146 static int random_size_alloc_test(void)
152 for (i = 0; i < test_loop_count; i++) {
153 get_random_bytes(&n, sizeof(i));
156 p = vmalloc(n * PAGE_SIZE);
168 static int long_busy_list_alloc_test(void)
175 ptr = vmalloc(sizeof(void *) * 15000);
179 for (i = 0; i < 15000; i++)
180 ptr[i] = vmalloc(1 * PAGE_SIZE);
182 for (i = 0; i < test_loop_count; i++) {
183 ptr_1 = vmalloc(100 * PAGE_SIZE);
187 ptr_2 = vmalloc(1 * PAGE_SIZE);
193 *((__u8 *)ptr_1) = 0;
194 *((__u8 *)ptr_2) = 1;
204 for (i = 0; i < 15000; i++)
211 static int full_fit_alloc_test(void)
213 void **ptr, **junk_ptr, *tmp;
218 junk_length = fls(num_online_cpus());
219 junk_length *= (32 * 1024 * 1024 / PAGE_SIZE);
221 ptr = vmalloc(sizeof(void *) * junk_length);
225 junk_ptr = vmalloc(sizeof(void *) * junk_length);
231 for (i = 0; i < junk_length; i++) {
232 ptr[i] = vmalloc(1 * PAGE_SIZE);
233 junk_ptr[i] = vmalloc(1 * PAGE_SIZE);
236 for (i = 0; i < junk_length; i++)
239 for (i = 0; i < test_loop_count; i++) {
240 tmp = vmalloc(1 * PAGE_SIZE);
253 for (i = 0; i < junk_length; i++)
262 static int fix_size_alloc_test(void)
267 for (i = 0; i < test_loop_count; i++) {
268 ptr = vmalloc((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE);
282 pcpu_alloc_test(void)
285 #ifndef CONFIG_NEED_PER_CPU_KM
286 void __percpu **pcpu;
290 pcpu = vmalloc(sizeof(void __percpu *) * 35000);
294 for (i = 0; i < 35000; i++) {
297 get_random_bytes(&r, sizeof(i));
298 size = (r % (PAGE_SIZE / 4)) + 1;
303 get_random_bytes(&r, sizeof(i));
304 align = 1 << ((i % 11) + 1);
306 pcpu[i] = __alloc_percpu(size, align);
311 for (i = 0; i < 35000; i++)
312 free_percpu(pcpu[i]);
319 struct test_kvfree_rcu {
321 unsigned char array[20];
325 kvfree_rcu_1_arg_vmalloc_test(void)
327 struct test_kvfree_rcu *p;
330 for (i = 0; i < test_loop_count; i++) {
331 p = vmalloc(1 * PAGE_SIZE);
343 kvfree_rcu_2_arg_vmalloc_test(void)
345 struct test_kvfree_rcu *p;
348 for (i = 0; i < test_loop_count; i++) {
349 p = vmalloc(1 * PAGE_SIZE);
360 struct test_case_desc {
361 const char *test_name;
362 int (*test_func)(void);
365 static struct test_case_desc test_case_array[] = {
366 { "fix_size_alloc_test", fix_size_alloc_test },
367 { "full_fit_alloc_test", full_fit_alloc_test },
368 { "long_busy_list_alloc_test", long_busy_list_alloc_test },
369 { "random_size_alloc_test", random_size_alloc_test },
370 { "fix_align_alloc_test", fix_align_alloc_test },
371 { "random_size_align_alloc_test", random_size_align_alloc_test },
372 { "align_shift_alloc_test", align_shift_alloc_test },
373 { "pcpu_alloc_test", pcpu_alloc_test },
374 { "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test },
375 { "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test },
376 /* Add a new test case here. */
379 struct test_case_data {
385 static struct test_driver {
386 struct task_struct *task;
387 struct test_case_data data[ARRAY_SIZE(test_case_array)];
393 static void shuffle_array(int *arr, int n)
398 for (i = n - 1; i > 0; i--) {
399 get_random_bytes(&rnd, sizeof(rnd));
411 static int test_func(void *private)
413 struct test_driver *t = private;
414 int random_array[ARRAY_SIZE(test_case_array)];
419 for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
422 if (!sequential_test_order)
423 shuffle_array(random_array, ARRAY_SIZE(test_case_array));
426 * Block until initialization is done.
428 down_read(&prepare_for_test_rwsem);
430 t->start = get_cycles();
431 for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
432 index = random_array[i];
435 * Skip tests if run_test_mask has been specified.
437 if (!((run_test_mask & (1 << index)) >> index))
441 for (j = 0; j < test_repeat_count; j++) {
442 if (!test_case_array[index].test_func())
443 t->data[index].test_passed++;
445 t->data[index].test_failed++;
449 * Take an average time that test took.
451 delta = (u64) ktime_us_delta(ktime_get(), kt);
452 do_div(delta, (u32) test_repeat_count);
454 t->data[index].time = delta;
456 t->stop = get_cycles();
458 up_read(&prepare_for_test_rwsem);
459 test_report_one_done();
462 * Wait for the kthread_stop() call.
464 while (!kthread_should_stop())
471 init_test_configurtion(void)
474 * A maximum number of workers is defined as hard-coded
475 * value and set to USHRT_MAX. We add such gap just in
476 * case and for potential heavy stressing.
478 nr_threads = clamp(nr_threads, 1, (int) USHRT_MAX);
480 /* Allocate the space for test instances. */
481 tdriver = kvcalloc(nr_threads, sizeof(*tdriver), GFP_KERNEL);
485 if (test_repeat_count <= 0)
486 test_repeat_count = 1;
488 if (test_loop_count <= 0)
494 static void do_concurrent_test(void)
499 * Set some basic configurations plus sanity check.
501 ret = init_test_configurtion();
506 * Put on hold all workers.
508 down_write(&prepare_for_test_rwsem);
510 for (i = 0; i < nr_threads; i++) {
511 struct test_driver *t = &tdriver[i];
513 t->task = kthread_run(test_func, t, "vmalloc_test/%d", i);
515 if (!IS_ERR(t->task))
517 atomic_inc(&test_n_undone);
519 pr_err("Failed to start %d kthread\n", i);
523 * Now let the workers do their job.
525 up_write(&prepare_for_test_rwsem);
528 * Sleep quiet until all workers are done with 1 second
529 * interval. Since the test can take a lot of time we
530 * can run into a stack trace of the hung task. That is
531 * why we go with completion_timeout and HZ value.
534 ret = wait_for_completion_timeout(&test_all_done_comp, HZ);
537 for (i = 0; i < nr_threads; i++) {
538 struct test_driver *t = &tdriver[i];
541 if (!IS_ERR(t->task))
542 kthread_stop(t->task);
544 for (j = 0; j < ARRAY_SIZE(test_case_array); j++) {
545 if (!((run_test_mask & (1 << j)) >> j))
549 "Summary: %s passed: %d failed: %d repeat: %d loops: %d avg: %llu usec\n",
550 test_case_array[j].test_name,
551 t->data[j].test_passed,
552 t->data[j].test_failed,
553 test_repeat_count, test_loop_count,
557 pr_info("All test took worker%d=%lu cycles\n",
558 i, t->stop - t->start);
564 static int vmalloc_test_init(void)
566 do_concurrent_test();
567 return -EAGAIN; /* Fail will directly unload the module */
570 static void vmalloc_test_exit(void)
574 module_init(vmalloc_test_init)
575 module_exit(vmalloc_test_exit)
577 MODULE_LICENSE("GPL");
578 MODULE_AUTHOR("Uladzislau Rezki");
579 MODULE_DESCRIPTION("vmalloc test module");