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(bool, use_huge, false,
42 "Use vmalloc_huge in fix_size_alloc_test");
44 __param(int, run_test_mask, INT_MAX,
45 "Set tests specified in the mask.\n\n"
46 "\t\tid: 1, name: fix_size_alloc_test\n"
47 "\t\tid: 2, name: full_fit_alloc_test\n"
48 "\t\tid: 4, name: long_busy_list_alloc_test\n"
49 "\t\tid: 8, name: random_size_alloc_test\n"
50 "\t\tid: 16, name: fix_align_alloc_test\n"
51 "\t\tid: 32, name: random_size_align_alloc_test\n"
52 "\t\tid: 64, name: align_shift_alloc_test\n"
53 "\t\tid: 128, name: pcpu_alloc_test\n"
54 "\t\tid: 256, name: kvfree_rcu_1_arg_vmalloc_test\n"
55 "\t\tid: 512, name: kvfree_rcu_2_arg_vmalloc_test\n"
56 /* Add a new test case description here. */
60 * Read write semaphore for synchronization of setup
61 * phase that is done in main thread and workers.
63 static DECLARE_RWSEM(prepare_for_test_rwsem);
66 * Completion tracking for worker threads.
68 static DECLARE_COMPLETION(test_all_done_comp);
69 static atomic_t test_n_undone = ATOMIC_INIT(0);
72 test_report_one_done(void)
74 if (atomic_dec_and_test(&test_n_undone))
75 complete(&test_all_done_comp);
78 static int random_size_align_alloc_test(void)
80 unsigned long size, align;
85 for (i = 0; i < test_loop_count; i++) {
86 rnd = get_random_u8();
89 * Maximum 1024 pages, if PAGE_SIZE is 4096.
91 align = 1 << (rnd % 23);
96 size = ((rnd % 10) + 1) * PAGE_SIZE;
98 ptr = __vmalloc_node(size, align, GFP_KERNEL | __GFP_ZERO, 0,
99 __builtin_return_address(0));
110 * This test case is supposed to be failed.
112 static int align_shift_alloc_test(void)
118 for (i = 0; i < BITS_PER_LONG; i++) {
119 align = ((unsigned long) 1) << i;
121 ptr = __vmalloc_node(PAGE_SIZE, align, GFP_KERNEL|__GFP_ZERO, 0,
122 __builtin_return_address(0));
132 static int fix_align_alloc_test(void)
137 for (i = 0; i < test_loop_count; i++) {
138 ptr = __vmalloc_node(5 * PAGE_SIZE, THREAD_ALIGN << 1,
139 GFP_KERNEL | __GFP_ZERO, 0,
140 __builtin_return_address(0));
150 static int random_size_alloc_test(void)
156 for (i = 0; i < test_loop_count; i++) {
157 n = get_random_u32_inclusive(1, 100);
158 p = vmalloc(n * PAGE_SIZE);
170 static int long_busy_list_alloc_test(void)
177 ptr = vmalloc(sizeof(void *) * 15000);
181 for (i = 0; i < 15000; i++)
182 ptr[i] = vmalloc(1 * PAGE_SIZE);
184 for (i = 0; i < test_loop_count; i++) {
185 ptr_1 = vmalloc(100 * PAGE_SIZE);
189 ptr_2 = vmalloc(1 * PAGE_SIZE);
195 *((__u8 *)ptr_1) = 0;
196 *((__u8 *)ptr_2) = 1;
206 for (i = 0; i < 15000; i++)
213 static int full_fit_alloc_test(void)
215 void **ptr, **junk_ptr, *tmp;
220 junk_length = fls(num_online_cpus());
221 junk_length *= (32 * 1024 * 1024 / PAGE_SIZE);
223 ptr = vmalloc(sizeof(void *) * junk_length);
227 junk_ptr = vmalloc(sizeof(void *) * junk_length);
233 for (i = 0; i < junk_length; i++) {
234 ptr[i] = vmalloc(1 * PAGE_SIZE);
235 junk_ptr[i] = vmalloc(1 * PAGE_SIZE);
238 for (i = 0; i < junk_length; i++)
241 for (i = 0; i < test_loop_count; i++) {
242 tmp = vmalloc(1 * PAGE_SIZE);
255 for (i = 0; i < junk_length; i++)
264 static int fix_size_alloc_test(void)
269 for (i = 0; i < test_loop_count; i++) {
271 ptr = vmalloc_huge((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE, GFP_KERNEL);
273 ptr = vmalloc((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE);
287 pcpu_alloc_test(void)
290 #ifndef CONFIG_NEED_PER_CPU_KM
291 void __percpu **pcpu;
295 pcpu = vmalloc(sizeof(void __percpu *) * 35000);
299 for (i = 0; i < 35000; i++) {
300 size = get_random_u32_inclusive(1, PAGE_SIZE / 4);
305 align = 1 << get_random_u32_inclusive(1, 11);
307 pcpu[i] = __alloc_percpu(size, align);
312 for (i = 0; i < 35000; i++)
313 free_percpu(pcpu[i]);
320 struct test_kvfree_rcu {
322 unsigned char array[20];
326 kvfree_rcu_1_arg_vmalloc_test(void)
328 struct test_kvfree_rcu *p;
331 for (i = 0; i < test_loop_count; i++) {
332 p = vmalloc(1 * PAGE_SIZE);
344 kvfree_rcu_2_arg_vmalloc_test(void)
346 struct test_kvfree_rcu *p;
349 for (i = 0; i < test_loop_count; i++) {
350 p = vmalloc(1 * PAGE_SIZE);
361 struct test_case_desc {
362 const char *test_name;
363 int (*test_func)(void);
366 static struct test_case_desc test_case_array[] = {
367 { "fix_size_alloc_test", fix_size_alloc_test },
368 { "full_fit_alloc_test", full_fit_alloc_test },
369 { "long_busy_list_alloc_test", long_busy_list_alloc_test },
370 { "random_size_alloc_test", random_size_alloc_test },
371 { "fix_align_alloc_test", fix_align_alloc_test },
372 { "random_size_align_alloc_test", random_size_align_alloc_test },
373 { "align_shift_alloc_test", align_shift_alloc_test },
374 { "pcpu_alloc_test", pcpu_alloc_test },
375 { "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test },
376 { "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test },
377 /* Add a new test case here. */
380 struct test_case_data {
386 static struct test_driver {
387 struct task_struct *task;
388 struct test_case_data data[ARRAY_SIZE(test_case_array)];
394 static void shuffle_array(int *arr, int n)
398 for (i = n - 1; i > 0; i--) {
400 j = get_random_u32_below(i);
403 swap(arr[i], arr[j]);
407 static int test_func(void *private)
409 struct test_driver *t = private;
410 int random_array[ARRAY_SIZE(test_case_array)];
415 for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
418 if (!sequential_test_order)
419 shuffle_array(random_array, ARRAY_SIZE(test_case_array));
422 * Block until initialization is done.
424 down_read(&prepare_for_test_rwsem);
426 t->start = get_cycles();
427 for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
428 index = random_array[i];
431 * Skip tests if run_test_mask has been specified.
433 if (!((run_test_mask & (1 << index)) >> index))
437 for (j = 0; j < test_repeat_count; j++) {
438 if (!test_case_array[index].test_func())
439 t->data[index].test_passed++;
441 t->data[index].test_failed++;
445 * Take an average time that test took.
447 delta = (u64) ktime_us_delta(ktime_get(), kt);
448 do_div(delta, (u32) test_repeat_count);
450 t->data[index].time = delta;
452 t->stop = get_cycles();
454 up_read(&prepare_for_test_rwsem);
455 test_report_one_done();
458 * Wait for the kthread_stop() call.
460 while (!kthread_should_stop())
467 init_test_configurtion(void)
470 * A maximum number of workers is defined as hard-coded
471 * value and set to USHRT_MAX. We add such gap just in
472 * case and for potential heavy stressing.
474 nr_threads = clamp(nr_threads, 1, (int) USHRT_MAX);
476 /* Allocate the space for test instances. */
477 tdriver = kvcalloc(nr_threads, sizeof(*tdriver), GFP_KERNEL);
481 if (test_repeat_count <= 0)
482 test_repeat_count = 1;
484 if (test_loop_count <= 0)
490 static void do_concurrent_test(void)
495 * Set some basic configurations plus sanity check.
497 ret = init_test_configurtion();
502 * Put on hold all workers.
504 down_write(&prepare_for_test_rwsem);
506 for (i = 0; i < nr_threads; i++) {
507 struct test_driver *t = &tdriver[i];
509 t->task = kthread_run(test_func, t, "vmalloc_test/%d", i);
511 if (!IS_ERR(t->task))
513 atomic_inc(&test_n_undone);
515 pr_err("Failed to start %d kthread\n", i);
519 * Now let the workers do their job.
521 up_write(&prepare_for_test_rwsem);
524 * Sleep quiet until all workers are done with 1 second
525 * interval. Since the test can take a lot of time we
526 * can run into a stack trace of the hung task. That is
527 * why we go with completion_timeout and HZ value.
530 ret = wait_for_completion_timeout(&test_all_done_comp, HZ);
533 for (i = 0; i < nr_threads; i++) {
534 struct test_driver *t = &tdriver[i];
537 if (!IS_ERR(t->task))
538 kthread_stop(t->task);
540 for (j = 0; j < ARRAY_SIZE(test_case_array); j++) {
541 if (!((run_test_mask & (1 << j)) >> j))
545 "Summary: %s passed: %d failed: %d repeat: %d loops: %d avg: %llu usec\n",
546 test_case_array[j].test_name,
547 t->data[j].test_passed,
548 t->data[j].test_failed,
549 test_repeat_count, test_loop_count,
553 pr_info("All test took worker%d=%lu cycles\n",
554 i, t->stop - t->start);
560 static int vmalloc_test_init(void)
562 do_concurrent_test();
563 return -EAGAIN; /* Fail will directly unload the module */
566 static void vmalloc_test_exit(void)
570 module_init(vmalloc_test_init)
571 module_exit(vmalloc_test_exit)
573 MODULE_LICENSE("GPL");
574 MODULE_AUTHOR("Uladzislau Rezki");
575 MODULE_DESCRIPTION("vmalloc test module");