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 "\t\tid: 1024, name: vm_map_ram_test\n"
57 /* Add a new test case description here. */
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
;
86 for (i
= 0; i
< test_loop_count
; i
++) {
87 rnd
= get_random_u8();
90 * Maximum 1024 pages, if PAGE_SIZE is 4096.
92 align
= 1 << (rnd
% 23);
97 size
= ((rnd
% 10) + 1) * PAGE_SIZE
;
99 ptr
= __vmalloc_node(size
, align
, GFP_KERNEL
| __GFP_ZERO
, 0,
100 __builtin_return_address(0));
111 * This test case is supposed to be failed.
113 static int align_shift_alloc_test(void)
119 for (i
= 0; i
< BITS_PER_LONG
; i
++) {
122 ptr
= __vmalloc_node(PAGE_SIZE
, align
, GFP_KERNEL
|__GFP_ZERO
, 0,
123 __builtin_return_address(0));
133 static int fix_align_alloc_test(void)
138 for (i
= 0; i
< test_loop_count
; i
++) {
139 ptr
= __vmalloc_node(5 * PAGE_SIZE
, THREAD_ALIGN
<< 1,
140 GFP_KERNEL
| __GFP_ZERO
, 0,
141 __builtin_return_address(0));
151 static int random_size_alloc_test(void)
157 for (i
= 0; i
< test_loop_count
; i
++) {
158 n
= get_random_u32_inclusive(1, 100);
159 p
= vmalloc(n
* PAGE_SIZE
);
171 static int long_busy_list_alloc_test(void)
178 ptr
= vmalloc(sizeof(void *) * 15000);
182 for (i
= 0; i
< 15000; i
++)
183 ptr
[i
] = vmalloc(1 * PAGE_SIZE
);
185 for (i
= 0; i
< test_loop_count
; i
++) {
186 ptr_1
= vmalloc(100 * PAGE_SIZE
);
190 ptr_2
= vmalloc(1 * PAGE_SIZE
);
196 *((__u8
*)ptr_1
) = 0;
197 *((__u8
*)ptr_2
) = 1;
207 for (i
= 0; i
< 15000; i
++)
214 static int full_fit_alloc_test(void)
216 void **ptr
, **junk_ptr
, *tmp
;
221 junk_length
= fls(num_online_cpus());
222 junk_length
*= (32 * 1024 * 1024 / PAGE_SIZE
);
224 ptr
= vmalloc(sizeof(void *) * junk_length
);
228 junk_ptr
= vmalloc(sizeof(void *) * junk_length
);
234 for (i
= 0; i
< junk_length
; i
++) {
235 ptr
[i
] = vmalloc(1 * PAGE_SIZE
);
236 junk_ptr
[i
] = vmalloc(1 * PAGE_SIZE
);
239 for (i
= 0; i
< junk_length
; i
++)
242 for (i
= 0; i
< test_loop_count
; i
++) {
243 tmp
= vmalloc(1 * PAGE_SIZE
);
256 for (i
= 0; i
< junk_length
; i
++)
265 static int fix_size_alloc_test(void)
270 for (i
= 0; i
< test_loop_count
; i
++) {
272 ptr
= vmalloc_huge((nr_pages
> 0 ? nr_pages
:1) * PAGE_SIZE
, GFP_KERNEL
);
274 ptr
= vmalloc((nr_pages
> 0 ? nr_pages
:1) * PAGE_SIZE
);
288 pcpu_alloc_test(void)
291 #ifndef CONFIG_NEED_PER_CPU_KM
292 void __percpu
**pcpu
;
296 pcpu
= vmalloc(sizeof(void __percpu
*) * 35000);
300 for (i
= 0; i
< 35000; i
++) {
301 size
= get_random_u32_inclusive(1, PAGE_SIZE
/ 4);
306 align
= 1 << get_random_u32_inclusive(1, 11);
308 pcpu
[i
] = __alloc_percpu(size
, align
);
313 for (i
= 0; i
< 35000; i
++)
314 free_percpu(pcpu
[i
]);
321 struct test_kvfree_rcu
{
323 unsigned char array
[20];
327 kvfree_rcu_1_arg_vmalloc_test(void)
329 struct test_kvfree_rcu
*p
;
332 for (i
= 0; i
< test_loop_count
; i
++) {
333 p
= vmalloc(1 * PAGE_SIZE
);
338 kvfree_rcu_mightsleep(p
);
345 kvfree_rcu_2_arg_vmalloc_test(void)
347 struct test_kvfree_rcu
*p
;
350 for (i
= 0; i
< test_loop_count
; i
++) {
351 p
= vmalloc(1 * PAGE_SIZE
);
363 vm_map_ram_test(void)
365 unsigned long nr_allocated
;
366 unsigned int map_nr_pages
;
367 unsigned char *v_ptr
;
371 map_nr_pages
= nr_pages
> 0 ? nr_pages
:1;
372 pages
= kcalloc(map_nr_pages
, sizeof(struct page
*), GFP_KERNEL
);
376 nr_allocated
= alloc_pages_bulk_array(GFP_KERNEL
, map_nr_pages
, pages
);
377 if (nr_allocated
!= map_nr_pages
)
380 /* Run the test loop. */
381 for (i
= 0; i
< test_loop_count
; i
++) {
382 v_ptr
= vm_map_ram(pages
, map_nr_pages
, NUMA_NO_NODE
);
384 vm_unmap_ram(v_ptr
, map_nr_pages
);
388 for (i
= 0; i
< nr_allocated
; i
++)
389 __free_page(pages
[i
]);
393 /* 0 indicates success. */
394 return nr_allocated
!= map_nr_pages
;
397 struct test_case_desc
{
398 const char *test_name
;
399 int (*test_func
)(void);
402 static struct test_case_desc test_case_array
[] = {
403 { "fix_size_alloc_test", fix_size_alloc_test
},
404 { "full_fit_alloc_test", full_fit_alloc_test
},
405 { "long_busy_list_alloc_test", long_busy_list_alloc_test
},
406 { "random_size_alloc_test", random_size_alloc_test
},
407 { "fix_align_alloc_test", fix_align_alloc_test
},
408 { "random_size_align_alloc_test", random_size_align_alloc_test
},
409 { "align_shift_alloc_test", align_shift_alloc_test
},
410 { "pcpu_alloc_test", pcpu_alloc_test
},
411 { "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test
},
412 { "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test
},
413 { "vm_map_ram_test", vm_map_ram_test
},
414 /* Add a new test case here. */
417 struct test_case_data
{
423 static struct test_driver
{
424 struct task_struct
*task
;
425 struct test_case_data data
[ARRAY_SIZE(test_case_array
)];
431 static void shuffle_array(int *arr
, int n
)
435 for (i
= n
- 1; i
> 0; i
--) {
437 j
= get_random_u32_below(i
);
440 swap(arr
[i
], arr
[j
]);
444 static int test_func(void *private)
446 struct test_driver
*t
= private;
447 int random_array
[ARRAY_SIZE(test_case_array
)];
452 for (i
= 0; i
< ARRAY_SIZE(test_case_array
); i
++)
455 if (!sequential_test_order
)
456 shuffle_array(random_array
, ARRAY_SIZE(test_case_array
));
459 * Block until initialization is done.
461 down_read(&prepare_for_test_rwsem
);
463 t
->start
= get_cycles();
464 for (i
= 0; i
< ARRAY_SIZE(test_case_array
); i
++) {
465 index
= random_array
[i
];
468 * Skip tests if run_test_mask has been specified.
470 if (!((run_test_mask
& (1 << index
)) >> index
))
474 for (j
= 0; j
< test_repeat_count
; j
++) {
475 if (!test_case_array
[index
].test_func())
476 t
->data
[index
].test_passed
++;
478 t
->data
[index
].test_failed
++;
482 * Take an average time that test took.
484 delta
= (u64
) ktime_us_delta(ktime_get(), kt
);
485 do_div(delta
, (u32
) test_repeat_count
);
487 t
->data
[index
].time
= delta
;
489 t
->stop
= get_cycles();
491 up_read(&prepare_for_test_rwsem
);
492 test_report_one_done();
495 * Wait for the kthread_stop() call.
497 while (!kthread_should_stop())
504 init_test_configuration(void)
507 * A maximum number of workers is defined as hard-coded
508 * value and set to USHRT_MAX. We add such gap just in
509 * case and for potential heavy stressing.
511 nr_threads
= clamp(nr_threads
, 1, (int) USHRT_MAX
);
513 /* Allocate the space for test instances. */
514 tdriver
= kvcalloc(nr_threads
, sizeof(*tdriver
), GFP_KERNEL
);
518 if (test_repeat_count
<= 0)
519 test_repeat_count
= 1;
521 if (test_loop_count
<= 0)
527 static void do_concurrent_test(void)
532 * Set some basic configurations plus sanity check.
534 ret
= init_test_configuration();
539 * Put on hold all workers.
541 down_write(&prepare_for_test_rwsem
);
543 for (i
= 0; i
< nr_threads
; i
++) {
544 struct test_driver
*t
= &tdriver
[i
];
546 t
->task
= kthread_run(test_func
, t
, "vmalloc_test/%d", i
);
548 if (!IS_ERR(t
->task
))
550 atomic_inc(&test_n_undone
);
552 pr_err("Failed to start %d kthread\n", i
);
556 * Now let the workers do their job.
558 up_write(&prepare_for_test_rwsem
);
561 * Sleep quiet until all workers are done with 1 second
562 * interval. Since the test can take a lot of time we
563 * can run into a stack trace of the hung task. That is
564 * why we go with completion_timeout and HZ value.
567 ret
= wait_for_completion_timeout(&test_all_done_comp
, HZ
);
570 for (i
= 0; i
< nr_threads
; i
++) {
571 struct test_driver
*t
= &tdriver
[i
];
574 if (!IS_ERR(t
->task
))
575 kthread_stop(t
->task
);
577 for (j
= 0; j
< ARRAY_SIZE(test_case_array
); j
++) {
578 if (!((run_test_mask
& (1 << j
)) >> j
))
582 "Summary: %s passed: %d failed: %d repeat: %d loops: %d avg: %llu usec\n",
583 test_case_array
[j
].test_name
,
584 t
->data
[j
].test_passed
,
585 t
->data
[j
].test_failed
,
586 test_repeat_count
, test_loop_count
,
590 pr_info("All test took worker%d=%lu cycles\n",
591 i
, t
->stop
- t
->start
);
597 static int vmalloc_test_init(void)
599 do_concurrent_test();
600 return -EAGAIN
; /* Fail will directly unload the module */
603 module_init(vmalloc_test_init
)
605 MODULE_LICENSE("GPL");
606 MODULE_AUTHOR("Uladzislau Rezki");
607 MODULE_DESCRIPTION("vmalloc test module");