Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Test module for stress and analyze performance of vmalloc allocator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * (C) 2018 Uladzislau Rezki (Sony) <urezki@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define __param(type, name, init, msg)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	static type name = init;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	module_param(name, type, 0444);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	MODULE_PARM_DESC(name, msg)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) __param(bool, single_cpu_test, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	"Use single first online CPU to run tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) __param(bool, sequential_test_order, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	"Use sequential stress tests order");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) __param(int, test_repeat_count, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	"Set test repeat counter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) __param(int, test_loop_count, 1000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	"Set test loop counter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) __param(int, run_test_mask, INT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	"Set tests specified in the mask.\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		"\t\tid: 1,    name: fix_size_alloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		"\t\tid: 2,    name: full_fit_alloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		"\t\tid: 4,    name: long_busy_list_alloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		"\t\tid: 8,    name: random_size_alloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		"\t\tid: 16,   name: fix_align_alloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		"\t\tid: 32,   name: random_size_align_alloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		"\t\tid: 64,   name: align_shift_alloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		"\t\tid: 128,  name: pcpu_alloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		"\t\tid: 256,  name: kvfree_rcu_1_arg_vmalloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		"\t\tid: 512,  name: kvfree_rcu_2_arg_vmalloc_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		"\t\tid: 1024, name: kvfree_rcu_1_arg_slab_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		"\t\tid: 2048, name: kvfree_rcu_2_arg_slab_test\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		/* Add a new test case description here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * Depends on single_cpu_test parameter. If it is true, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * use first online CPU to trigger a test on, otherwise go with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * all online CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static cpumask_t cpus_run_test_mask = CPU_MASK_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * Read write semaphore for synchronization of setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * phase that is done in main thread and workers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static DECLARE_RWSEM(prepare_for_test_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * Completion tracking for worker threads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static DECLARE_COMPLETION(test_all_done_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static atomic_t test_n_undone = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) test_report_one_done(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (atomic_dec_and_test(&test_n_undone))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		complete(&test_all_done_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static int random_size_align_alloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	unsigned long size, align, rnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		get_random_bytes(&rnd, sizeof(rnd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		 * Maximum 1024 pages, if PAGE_SIZE is 4096.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		align = 1 << (rnd % 23);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		 * Maximum 10 pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		size = ((rnd % 10) + 1) * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		ptr = __vmalloc_node(size, align, GFP_KERNEL | __GFP_ZERO, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 				__builtin_return_address(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		vfree(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * This test case is supposed to be failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int align_shift_alloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	unsigned long align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	for (i = 0; i < BITS_PER_LONG; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		align = ((unsigned long) 1) << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		ptr = __vmalloc_node(PAGE_SIZE, align, GFP_KERNEL|__GFP_ZERO, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 				__builtin_return_address(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		vfree(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int fix_align_alloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ptr = __vmalloc_node(5 * PAGE_SIZE, THREAD_ALIGN << 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				GFP_KERNEL | __GFP_ZERO, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 				__builtin_return_address(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		vfree(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int random_size_alloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	unsigned int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		get_random_bytes(&n, sizeof(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		n = (n % 100) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		p = vmalloc(n * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		*((__u8 *)p) = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		vfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int long_busy_list_alloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	void *ptr_1, *ptr_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	void **ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int rv = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	ptr = vmalloc(sizeof(void *) * 15000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	for (i = 0; i < 15000; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		ptr[i] = vmalloc(1 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		ptr_1 = vmalloc(100 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		if (!ptr_1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		ptr_2 = vmalloc(1 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		if (!ptr_2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			vfree(ptr_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		*((__u8 *)ptr_1) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		*((__u8 *)ptr_2) = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		vfree(ptr_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		vfree(ptr_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/*  Success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	for (i = 0; i < 15000; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		vfree(ptr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	vfree(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int full_fit_alloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	void **ptr, **junk_ptr, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int junk_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	int rv = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	junk_length = fls(num_online_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	junk_length *= (32 * 1024 * 1024 / PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	ptr = vmalloc(sizeof(void *) * junk_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	junk_ptr = vmalloc(sizeof(void *) * junk_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (!junk_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		vfree(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	for (i = 0; i < junk_length; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		ptr[i] = vmalloc(1 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		junk_ptr[i] = vmalloc(1 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	for (i = 0; i < junk_length; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		vfree(junk_ptr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		tmp = vmalloc(1 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		*((__u8 *)tmp) = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		vfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/* Success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	for (i = 0; i < junk_length; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		vfree(ptr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	vfree(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	vfree(junk_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int fix_size_alloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		ptr = vmalloc(3 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		*((__u8 *)ptr) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		vfree(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) pcpu_alloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	int rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) #ifndef CONFIG_NEED_PER_CPU_KM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	void __percpu **pcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	size_t size, align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	pcpu = vmalloc(sizeof(void __percpu *) * 35000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (!pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	for (i = 0; i < 35000; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		unsigned int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		get_random_bytes(&r, sizeof(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		size = (r % (PAGE_SIZE / 4)) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		 * Maximum PAGE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		get_random_bytes(&r, sizeof(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		align = 1 << ((i % 11) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		pcpu[i] = __alloc_percpu(size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (!pcpu[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			rv = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	for (i = 0; i < 35000; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		free_percpu(pcpu[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	vfree(pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct test_kvfree_rcu {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	unsigned char array[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) kvfree_rcu_1_arg_vmalloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct test_kvfree_rcu *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		p = vmalloc(1 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		p->array[0] = 'a';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		kvfree_rcu(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) kvfree_rcu_2_arg_vmalloc_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct test_kvfree_rcu *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		p = vmalloc(1 * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		p->array[0] = 'a';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		kvfree_rcu(p, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) kvfree_rcu_1_arg_slab_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct test_kvfree_rcu *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		p = kmalloc(sizeof(*p), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		p->array[0] = 'a';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		kvfree_rcu(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) kvfree_rcu_2_arg_slab_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	struct test_kvfree_rcu *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	for (i = 0; i < test_loop_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		p = kmalloc(sizeof(*p), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		p->array[0] = 'a';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		kvfree_rcu(p, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct test_case_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	const char *test_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	int (*test_func)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static struct test_case_desc test_case_array[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	{ "fix_size_alloc_test", fix_size_alloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	{ "full_fit_alloc_test", full_fit_alloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	{ "long_busy_list_alloc_test", long_busy_list_alloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	{ "random_size_alloc_test", random_size_alloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	{ "fix_align_alloc_test", fix_align_alloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	{ "random_size_align_alloc_test", random_size_align_alloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	{ "align_shift_alloc_test", align_shift_alloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	{ "pcpu_alloc_test", pcpu_alloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	{ "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	{ "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	{ "kvfree_rcu_1_arg_slab_test", kvfree_rcu_1_arg_slab_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	{ "kvfree_rcu_2_arg_slab_test", kvfree_rcu_2_arg_slab_test },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	/* Add a new test case here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) struct test_case_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	int test_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	int test_passed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	u64 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /* Split it to get rid of: WARNING: line over 80 characters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static struct test_case_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	per_cpu_test_data[NR_CPUS][ARRAY_SIZE(test_case_array)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static struct test_driver {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	unsigned long stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) } per_cpu_test_driver[NR_CPUS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static void shuffle_array(int *arr, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	unsigned int rnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	int i, j, x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	for (i = n - 1; i > 0; i--)  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		get_random_bytes(&rnd, sizeof(rnd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		/* Cut the range. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		j = rnd % i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		/* Swap indexes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		x = arr[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		arr[i] = arr[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		arr[j] = x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static int test_func(void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	struct test_driver *t = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	int random_array[ARRAY_SIZE(test_case_array)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	int index, i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	ktime_t kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	u64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (set_cpus_allowed_ptr(current, cpumask_of(t->cpu)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		pr_err("Failed to set affinity to %d CPU\n", t->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		random_array[i] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	if (!sequential_test_order)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		shuffle_array(random_array, ARRAY_SIZE(test_case_array));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	 * Block until initialization is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	down_read(&prepare_for_test_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	t->start = get_cycles();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		index = random_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		 * Skip tests if run_test_mask has been specified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		if (!((run_test_mask & (1 << index)) >> index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		kt = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		for (j = 0; j < test_repeat_count; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			if (!test_case_array[index].test_func())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 				per_cpu_test_data[t->cpu][index].test_passed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 				per_cpu_test_data[t->cpu][index].test_failed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		 * Take an average time that test took.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		delta = (u64) ktime_us_delta(ktime_get(), kt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		do_div(delta, (u32) test_repeat_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		per_cpu_test_data[t->cpu][index].time = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	t->stop = get_cycles();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	up_read(&prepare_for_test_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	test_report_one_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	 * Wait for the kthread_stop() call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	while (!kthread_should_stop())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		msleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) init_test_configurtion(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	 * Reset all data of all CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	memset(per_cpu_test_data, 0, sizeof(per_cpu_test_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	if (single_cpu_test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		cpumask_set_cpu(cpumask_first(cpu_online_mask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			&cpus_run_test_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		cpumask_and(&cpus_run_test_mask, cpu_online_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	if (test_repeat_count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		test_repeat_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	if (test_loop_count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		test_loop_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) static void do_concurrent_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	int cpu, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	 * Set some basic configurations plus sanity check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	init_test_configurtion();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	 * Put on hold all workers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	down_write(&prepare_for_test_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	for_each_cpu(cpu, &cpus_run_test_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		struct test_driver *t = &per_cpu_test_driver[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		t->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		t->task = kthread_run(test_func, t, "vmalloc_test/%d", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		if (!IS_ERR(t->task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			/* Success. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			atomic_inc(&test_n_undone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			pr_err("Failed to start kthread for %d CPU\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	 * Now let the workers do their job.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	up_write(&prepare_for_test_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	 * Sleep quiet until all workers are done with 1 second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	 * interval. Since the test can take a lot of time we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	 * can run into a stack trace of the hung task. That is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	 * why we go with completion_timeout and HZ value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		ret = wait_for_completion_timeout(&test_all_done_comp, HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	} while (!ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	for_each_cpu(cpu, &cpus_run_test_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		struct test_driver *t = &per_cpu_test_driver[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		if (!IS_ERR(t->task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			kthread_stop(t->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 			if (!((run_test_mask & (1 << i)) >> i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			pr_info(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 				"Summary: %s passed: %d failed: %d repeat: %d loops: %d avg: %llu usec\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 				test_case_array[i].test_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 				per_cpu_test_data[cpu][i].test_passed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 				per_cpu_test_data[cpu][i].test_failed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 				test_repeat_count, test_loop_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 				per_cpu_test_data[cpu][i].time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		pr_info("All test took CPU%d=%lu cycles\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 			cpu, t->stop - t->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static int vmalloc_test_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	do_concurrent_test();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	return -EAGAIN; /* Fail will directly unload the module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) static void vmalloc_test_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) module_init(vmalloc_test_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) module_exit(vmalloc_test_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) MODULE_AUTHOR("Uladzislau Rezki");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) MODULE_DESCRIPTION("vmalloc test module");