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