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(bool, single_cpu_test, false,
27 "Use single first online CPU to run tests");
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, run_test_mask, INT_MAX,
39 "Set tests specified in the mask.\n\n"
40 "\t\tid: 1, name: fix_size_alloc_test\n"
41 "\t\tid: 2, name: full_fit_alloc_test\n"
42 "\t\tid: 4, name: long_busy_list_alloc_test\n"
43 "\t\tid: 8, name: random_size_alloc_test\n"
44 "\t\tid: 16, name: fix_align_alloc_test\n"
45 "\t\tid: 32, name: random_size_align_alloc_test\n"
46 "\t\tid: 64, name: align_shift_alloc_test\n"
47 "\t\tid: 128, name: pcpu_alloc_test\n"
48 "\t\tid: 256, name: kvfree_rcu_1_arg_vmalloc_test\n"
49 "\t\tid: 512, name: kvfree_rcu_2_arg_vmalloc_test\n"
50 /* Add a new test case description here. */
54 * Depends on single_cpu_test parameter. If it is true, then
55 * use first online CPU to trigger a test on, otherwise go with
58 static cpumask_t cpus_run_test_mask = CPU_MASK_NONE;
61 * Read write semaphore for synchronization of setup
62 * phase that is done in main thread and workers.
64 static DECLARE_RWSEM(prepare_for_test_rwsem);
67 * Completion tracking for worker threads.
69 static DECLARE_COMPLETION(test_all_done_comp);
70 static atomic_t test_n_undone = ATOMIC_INIT(0);
73 test_report_one_done(void)
75 if (atomic_dec_and_test(&test_n_undone))
76 complete(&test_all_done_comp);
79 static int random_size_align_alloc_test(void)
81 unsigned long size, align, rnd;
85 for (i = 0; i < test_loop_count; i++) {
86 get_random_bytes(&rnd, sizeof(rnd));
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 get_random_bytes(&n, sizeof(i));
160 p = vmalloc(n * PAGE_SIZE);
172 static int long_busy_list_alloc_test(void)
179 ptr = vmalloc(sizeof(void *) * 15000);
183 for (i = 0; i < 15000; i++)
184 ptr[i] = vmalloc(1 * PAGE_SIZE);
186 for (i = 0; i < test_loop_count; i++) {
187 ptr_1 = vmalloc(100 * PAGE_SIZE);
191 ptr_2 = vmalloc(1 * PAGE_SIZE);
197 *((__u8 *)ptr_1) = 0;
198 *((__u8 *)ptr_2) = 1;
208 for (i = 0; i < 15000; i++)
215 static int full_fit_alloc_test(void)
217 void **ptr, **junk_ptr, *tmp;
222 junk_length = fls(num_online_cpus());
223 junk_length *= (32 * 1024 * 1024 / PAGE_SIZE);
225 ptr = vmalloc(sizeof(void *) * junk_length);
229 junk_ptr = vmalloc(sizeof(void *) * junk_length);
235 for (i = 0; i < junk_length; i++) {
236 ptr[i] = vmalloc(1 * PAGE_SIZE);
237 junk_ptr[i] = vmalloc(1 * PAGE_SIZE);
240 for (i = 0; i < junk_length; i++)
243 for (i = 0; i < test_loop_count; i++) {
244 tmp = vmalloc(1 * PAGE_SIZE);
257 for (i = 0; i < junk_length; i++)
266 static int fix_size_alloc_test(void)
271 for (i = 0; i < test_loop_count; i++) {
272 ptr = vmalloc(3 * PAGE_SIZE);
286 pcpu_alloc_test(void)
289 #ifndef CONFIG_NEED_PER_CPU_KM
290 void __percpu **pcpu;
294 pcpu = vmalloc(sizeof(void __percpu *) * 35000);
298 for (i = 0; i < 35000; i++) {
301 get_random_bytes(&r, sizeof(i));
302 size = (r % (PAGE_SIZE / 4)) + 1;
307 get_random_bytes(&r, sizeof(i));
308 align = 1 << ((i % 11) + 1);
310 pcpu[i] = __alloc_percpu(size, align);
315 for (i = 0; i < 35000; i++)
316 free_percpu(pcpu[i]);
323 struct test_kvfree_rcu {
325 unsigned char array[20];
329 kvfree_rcu_1_arg_vmalloc_test(void)
331 struct test_kvfree_rcu *p;
334 for (i = 0; i < test_loop_count; i++) {
335 p = vmalloc(1 * PAGE_SIZE);
347 kvfree_rcu_2_arg_vmalloc_test(void)
349 struct test_kvfree_rcu *p;
352 for (i = 0; i < test_loop_count; i++) {
353 p = vmalloc(1 * PAGE_SIZE);
364 struct test_case_desc {
365 const char *test_name;
366 int (*test_func)(void);
369 static struct test_case_desc test_case_array[] = {
370 { "fix_size_alloc_test", fix_size_alloc_test },
371 { "full_fit_alloc_test", full_fit_alloc_test },
372 { "long_busy_list_alloc_test", long_busy_list_alloc_test },
373 { "random_size_alloc_test", random_size_alloc_test },
374 { "fix_align_alloc_test", fix_align_alloc_test },
375 { "random_size_align_alloc_test", random_size_align_alloc_test },
376 { "align_shift_alloc_test", align_shift_alloc_test },
377 { "pcpu_alloc_test", pcpu_alloc_test },
378 { "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test },
379 { "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test },
380 /* Add a new test case here. */
383 struct test_case_data {
389 /* Split it to get rid of: WARNING: line over 80 characters */
390 static struct test_case_data
391 per_cpu_test_data[NR_CPUS][ARRAY_SIZE(test_case_array)];
393 static struct test_driver {
394 struct task_struct *task;
398 } per_cpu_test_driver[NR_CPUS];
400 static void shuffle_array(int *arr, int n)
405 for (i = n - 1; i > 0; i--) {
406 get_random_bytes(&rnd, sizeof(rnd));
418 static int test_func(void *private)
420 struct test_driver *t = private;
421 int random_array[ARRAY_SIZE(test_case_array)];
426 if (set_cpus_allowed_ptr(current, cpumask_of(t->cpu)) < 0)
427 pr_err("Failed to set affinity to %d CPU\n", t->cpu);
429 for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
432 if (!sequential_test_order)
433 shuffle_array(random_array, ARRAY_SIZE(test_case_array));
436 * Block until initialization is done.
438 down_read(&prepare_for_test_rwsem);
440 t->start = get_cycles();
441 for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
442 index = random_array[i];
445 * Skip tests if run_test_mask has been specified.
447 if (!((run_test_mask & (1 << index)) >> index))
451 for (j = 0; j < test_repeat_count; j++) {
452 if (!test_case_array[index].test_func())
453 per_cpu_test_data[t->cpu][index].test_passed++;
455 per_cpu_test_data[t->cpu][index].test_failed++;
459 * Take an average time that test took.
461 delta = (u64) ktime_us_delta(ktime_get(), kt);
462 do_div(delta, (u32) test_repeat_count);
464 per_cpu_test_data[t->cpu][index].time = delta;
466 t->stop = get_cycles();
468 up_read(&prepare_for_test_rwsem);
469 test_report_one_done();
472 * Wait for the kthread_stop() call.
474 while (!kthread_should_stop())
481 init_test_configurtion(void)
484 * Reset all data of all CPUs.
486 memset(per_cpu_test_data, 0, sizeof(per_cpu_test_data));
489 cpumask_set_cpu(cpumask_first(cpu_online_mask),
490 &cpus_run_test_mask);
492 cpumask_and(&cpus_run_test_mask, cpu_online_mask,
495 if (test_repeat_count <= 0)
496 test_repeat_count = 1;
498 if (test_loop_count <= 0)
502 static void do_concurrent_test(void)
507 * Set some basic configurations plus sanity check.
509 init_test_configurtion();
512 * Put on hold all workers.
514 down_write(&prepare_for_test_rwsem);
516 for_each_cpu(cpu, &cpus_run_test_mask) {
517 struct test_driver *t = &per_cpu_test_driver[cpu];
520 t->task = kthread_run(test_func, t, "vmalloc_test/%d", cpu);
522 if (!IS_ERR(t->task))
524 atomic_inc(&test_n_undone);
526 pr_err("Failed to start kthread for %d CPU\n", cpu);
530 * Now let the workers do their job.
532 up_write(&prepare_for_test_rwsem);
535 * Sleep quiet until all workers are done with 1 second
536 * interval. Since the test can take a lot of time we
537 * can run into a stack trace of the hung task. That is
538 * why we go with completion_timeout and HZ value.
541 ret = wait_for_completion_timeout(&test_all_done_comp, HZ);
544 for_each_cpu(cpu, &cpus_run_test_mask) {
545 struct test_driver *t = &per_cpu_test_driver[cpu];
548 if (!IS_ERR(t->task))
549 kthread_stop(t->task);
551 for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
552 if (!((run_test_mask & (1 << i)) >> i))
556 "Summary: %s passed: %d failed: %d repeat: %d loops: %d avg: %llu usec\n",
557 test_case_array[i].test_name,
558 per_cpu_test_data[cpu][i].test_passed,
559 per_cpu_test_data[cpu][i].test_failed,
560 test_repeat_count, test_loop_count,
561 per_cpu_test_data[cpu][i].time);
564 pr_info("All test took CPU%d=%lu cycles\n",
565 cpu, t->stop - t->start);
569 static int vmalloc_test_init(void)
571 do_concurrent_test();
572 return -EAGAIN; /* Fail will directly unload the module */
575 static void vmalloc_test_exit(void)
579 module_init(vmalloc_test_init)
580 module_exit(vmalloc_test_exit)
582 MODULE_LICENSE("GPL");
583 MODULE_AUTHOR("Uladzislau Rezki");
584 MODULE_DESCRIPTION("vmalloc test module");