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> 18 #include <linux/rcupdate.h> 19 #include <linux/slab.h> 19 #include <linux/slab.h> 20 20 21 #define __param(type, name, init, msg) 21 #define __param(type, name, init, msg) \ 22 static type name = init; 22 static type name = init; \ 23 module_param(name, type, 0444); 23 module_param(name, type, 0444); \ 24 MODULE_PARM_DESC(name, msg) 24 MODULE_PARM_DESC(name, msg) \ 25 25 26 __param(int, nr_threads, 0, 26 __param(int, nr_threads, 0, 27 "Number of workers to perform tests(mi 27 "Number of workers to perform tests(min: 1 max: USHRT_MAX)"); 28 28 29 __param(bool, sequential_test_order, false, 29 __param(bool, sequential_test_order, false, 30 "Use sequential stress tests order"); 30 "Use sequential stress tests order"); 31 31 32 __param(int, test_repeat_count, 1, 32 __param(int, test_repeat_count, 1, 33 "Set test repeat counter"); 33 "Set test repeat counter"); 34 34 35 __param(int, test_loop_count, 1000000, 35 __param(int, test_loop_count, 1000000, 36 "Set test loop counter"); 36 "Set test loop counter"); 37 37 38 __param(int, nr_pages, 0, 38 __param(int, nr_pages, 0, 39 "Set number of pages for fix_size_allo 39 "Set number of pages for fix_size_alloc_test(default: 1)"); 40 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, 41 __param(int, run_test_mask, INT_MAX, 45 "Set tests specified in the mask.\n\n" 42 "Set tests specified in the mask.\n\n" 46 "\t\tid: 1, name: fix_size_ 43 "\t\tid: 1, name: fix_size_alloc_test\n" 47 "\t\tid: 2, name: full_fit_ 44 "\t\tid: 2, name: full_fit_alloc_test\n" 48 "\t\tid: 4, name: long_busy 45 "\t\tid: 4, name: long_busy_list_alloc_test\n" 49 "\t\tid: 8, name: random_si 46 "\t\tid: 8, name: random_size_alloc_test\n" 50 "\t\tid: 16, name: fix_align 47 "\t\tid: 16, name: fix_align_alloc_test\n" 51 "\t\tid: 32, name: random_si 48 "\t\tid: 32, name: random_size_align_alloc_test\n" 52 "\t\tid: 64, name: align_shi 49 "\t\tid: 64, name: align_shift_alloc_test\n" 53 "\t\tid: 128, name: pcpu_allo 50 "\t\tid: 128, name: pcpu_alloc_test\n" 54 "\t\tid: 256, name: kvfree_rc 51 "\t\tid: 256, name: kvfree_rcu_1_arg_vmalloc_test\n" 55 "\t\tid: 512, name: kvfree_rc 52 "\t\tid: 512, name: kvfree_rcu_2_arg_vmalloc_test\n" 56 "\t\tid: 1024, name: vm_map_ra << 57 /* Add a new test case descrip 53 /* Add a new test case description here. */ 58 ); 54 ); 59 55 60 /* 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((nr_pages > 0 ? nr_pages:1) * 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 { 319 struct test_kvfree_rcu { 322 struct rcu_head rcu; 320 struct rcu_head rcu; 323 unsigned char array[20]; 321 unsigned char array[20]; 324 }; 322 }; 325 323 326 static int 324 static int 327 kvfree_rcu_1_arg_vmalloc_test(void) 325 kvfree_rcu_1_arg_vmalloc_test(void) 328 { 326 { 329 struct test_kvfree_rcu *p; 327 struct test_kvfree_rcu *p; 330 int i; 328 int i; 331 329 332 for (i = 0; i < test_loop_count; i++) 330 for (i = 0; i < test_loop_count; i++) { 333 p = vmalloc(1 * PAGE_SIZE); 331 p = vmalloc(1 * PAGE_SIZE); 334 if (!p) 332 if (!p) 335 return -1; 333 return -1; 336 334 337 p->array[0] = 'a'; 335 p->array[0] = 'a'; 338 kvfree_rcu_mightsleep(p); !! 336 kvfree_rcu(p); 339 } 337 } 340 338 341 return 0; 339 return 0; 342 } 340 } 343 341 344 static int 342 static int 345 kvfree_rcu_2_arg_vmalloc_test(void) 343 kvfree_rcu_2_arg_vmalloc_test(void) 346 { 344 { 347 struct test_kvfree_rcu *p; 345 struct test_kvfree_rcu *p; 348 int i; 346 int i; 349 347 350 for (i = 0; i < test_loop_count; i++) 348 for (i = 0; i < test_loop_count; i++) { 351 p = vmalloc(1 * PAGE_SIZE); 349 p = vmalloc(1 * PAGE_SIZE); 352 if (!p) 350 if (!p) 353 return -1; 351 return -1; 354 352 355 p->array[0] = 'a'; 353 p->array[0] = 'a'; 356 kvfree_rcu(p, rcu); 354 kvfree_rcu(p, rcu); 357 } 355 } 358 356 359 return 0; 357 return 0; 360 } 358 } 361 359 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 { 360 struct test_case_desc { 398 const char *test_name; 361 const char *test_name; 399 int (*test_func)(void); 362 int (*test_func)(void); 400 }; 363 }; 401 364 402 static struct test_case_desc test_case_array[] 365 static struct test_case_desc test_case_array[] = { 403 { "fix_size_alloc_test", fix_size_allo 366 { "fix_size_alloc_test", fix_size_alloc_test }, 404 { "full_fit_alloc_test", full_fit_allo 367 { "full_fit_alloc_test", full_fit_alloc_test }, 405 { "long_busy_list_alloc_test", long_bu 368 { "long_busy_list_alloc_test", long_busy_list_alloc_test }, 406 { "random_size_alloc_test", random_siz 369 { "random_size_alloc_test", random_size_alloc_test }, 407 { "fix_align_alloc_test", fix_align_al 370 { "fix_align_alloc_test", fix_align_alloc_test }, 408 { "random_size_align_alloc_test", rand 371 { "random_size_align_alloc_test", random_size_align_alloc_test }, 409 { "align_shift_alloc_test", align_shif 372 { "align_shift_alloc_test", align_shift_alloc_test }, 410 { "pcpu_alloc_test", pcpu_alloc_test } 373 { "pcpu_alloc_test", pcpu_alloc_test }, 411 { "kvfree_rcu_1_arg_vmalloc_test", kvf 374 { "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test }, 412 { "kvfree_rcu_2_arg_vmalloc_test", kvf 375 { "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. */ 376 /* Add a new test case here. */ 415 }; 377 }; 416 378 417 struct test_case_data { 379 struct test_case_data { 418 int test_failed; 380 int test_failed; 419 int test_passed; 381 int test_passed; 420 u64 time; 382 u64 time; 421 }; 383 }; 422 384 423 static struct test_driver { 385 static struct test_driver { 424 struct task_struct *task; 386 struct task_struct *task; 425 struct test_case_data data[ARRAY_SIZE( 387 struct test_case_data data[ARRAY_SIZE(test_case_array)]; 426 388 427 unsigned long start; 389 unsigned long start; 428 unsigned long stop; 390 unsigned long stop; 429 } *tdriver; 391 } *tdriver; 430 392 431 static void shuffle_array(int *arr, int n) 393 static void shuffle_array(int *arr, int n) 432 { 394 { >> 395 unsigned int rnd; 433 int i, j; 396 int i, j; 434 397 435 for (i = n - 1; i > 0; i--) { 398 for (i = n - 1; i > 0; i--) { >> 399 get_random_bytes(&rnd, sizeof(rnd)); >> 400 436 /* Cut the range. */ 401 /* Cut the range. */ 437 j = get_random_u32_below(i); !! 402 j = rnd % i; 438 403 439 /* Swap indexes. */ 404 /* Swap indexes. */ 440 swap(arr[i], arr[j]); 405 swap(arr[i], arr[j]); 441 } 406 } 442 } 407 } 443 408 444 static int test_func(void *private) 409 static int test_func(void *private) 445 { 410 { 446 struct test_driver *t = private; 411 struct test_driver *t = private; 447 int random_array[ARRAY_SIZE(test_case_ 412 int random_array[ARRAY_SIZE(test_case_array)]; 448 int index, i, j; 413 int index, i, j; 449 ktime_t kt; 414 ktime_t kt; 450 u64 delta; 415 u64 delta; 451 416 452 for (i = 0; i < ARRAY_SIZE(test_case_a 417 for (i = 0; i < ARRAY_SIZE(test_case_array); i++) 453 random_array[i] = i; 418 random_array[i] = i; 454 419 455 if (!sequential_test_order) 420 if (!sequential_test_order) 456 shuffle_array(random_array, AR 421 shuffle_array(random_array, ARRAY_SIZE(test_case_array)); 457 422 458 /* 423 /* 459 * Block until initialization is done. 424 * Block until initialization is done. 460 */ 425 */ 461 down_read(&prepare_for_test_rwsem); 426 down_read(&prepare_for_test_rwsem); 462 427 463 t->start = get_cycles(); 428 t->start = get_cycles(); 464 for (i = 0; i < ARRAY_SIZE(test_case_a 429 for (i = 0; i < ARRAY_SIZE(test_case_array); i++) { 465 index = random_array[i]; 430 index = random_array[i]; 466 431 467 /* 432 /* 468 * Skip tests if run_test_mask 433 * Skip tests if run_test_mask has been specified. 469 */ 434 */ 470 if (!((run_test_mask & (1 << i 435 if (!((run_test_mask & (1 << index)) >> index)) 471 continue; 436 continue; 472 437 473 kt = ktime_get(); 438 kt = ktime_get(); 474 for (j = 0; j < test_repeat_co 439 for (j = 0; j < test_repeat_count; j++) { 475 if (!test_case_array[i 440 if (!test_case_array[index].test_func()) 476 t->data[index] 441 t->data[index].test_passed++; 477 else 442 else 478 t->data[index] 443 t->data[index].test_failed++; 479 } 444 } 480 445 481 /* 446 /* 482 * Take an average time that t 447 * Take an average time that test took. 483 */ 448 */ 484 delta = (u64) ktime_us_delta(k 449 delta = (u64) ktime_us_delta(ktime_get(), kt); 485 do_div(delta, (u32) test_repea 450 do_div(delta, (u32) test_repeat_count); 486 451 487 t->data[index].time = delta; 452 t->data[index].time = delta; 488 } 453 } 489 t->stop = get_cycles(); 454 t->stop = get_cycles(); 490 455 491 up_read(&prepare_for_test_rwsem); 456 up_read(&prepare_for_test_rwsem); 492 test_report_one_done(); 457 test_report_one_done(); 493 458 494 /* 459 /* 495 * Wait for the kthread_stop() call. 460 * Wait for the kthread_stop() call. 496 */ 461 */ 497 while (!kthread_should_stop()) 462 while (!kthread_should_stop()) 498 msleep(10); 463 msleep(10); 499 464 500 return 0; 465 return 0; 501 } 466 } 502 467 503 static int 468 static int 504 init_test_configuration(void) !! 469 init_test_configurtion(void) 505 { 470 { 506 /* 471 /* 507 * A maximum number of workers is defi 472 * A maximum number of workers is defined as hard-coded 508 * value and set to USHRT_MAX. We add 473 * value and set to USHRT_MAX. We add such gap just in 509 * case and for potential heavy stress 474 * case and for potential heavy stressing. 510 */ 475 */ 511 nr_threads = clamp(nr_threads, 1, (int 476 nr_threads = clamp(nr_threads, 1, (int) USHRT_MAX); 512 477 513 /* Allocate the space for test instanc 478 /* Allocate the space for test instances. */ 514 tdriver = kvcalloc(nr_threads, sizeof( 479 tdriver = kvcalloc(nr_threads, sizeof(*tdriver), GFP_KERNEL); 515 if (tdriver == NULL) 480 if (tdriver == NULL) 516 return -1; 481 return -1; 517 482 518 if (test_repeat_count <= 0) 483 if (test_repeat_count <= 0) 519 test_repeat_count = 1; 484 test_repeat_count = 1; 520 485 521 if (test_loop_count <= 0) 486 if (test_loop_count <= 0) 522 test_loop_count = 1; 487 test_loop_count = 1; 523 488 524 return 0; 489 return 0; 525 } 490 } 526 491 527 static void do_concurrent_test(void) 492 static void do_concurrent_test(void) 528 { 493 { 529 int i, ret; 494 int i, ret; 530 495 531 /* 496 /* 532 * Set some basic configurations plus 497 * Set some basic configurations plus sanity check. 533 */ 498 */ 534 ret = init_test_configuration(); !! 499 ret = init_test_configurtion(); 535 if (ret < 0) 500 if (ret < 0) 536 return; 501 return; 537 502 538 /* 503 /* 539 * Put on hold all workers. 504 * Put on hold all workers. 540 */ 505 */ 541 down_write(&prepare_for_test_rwsem); 506 down_write(&prepare_for_test_rwsem); 542 507 543 for (i = 0; i < nr_threads; i++) { 508 for (i = 0; i < nr_threads; i++) { 544 struct test_driver *t = &tdriv 509 struct test_driver *t = &tdriver[i]; 545 510 546 t->task = kthread_run(test_fun 511 t->task = kthread_run(test_func, t, "vmalloc_test/%d", i); 547 512 548 if (!IS_ERR(t->task)) 513 if (!IS_ERR(t->task)) 549 /* Success. */ 514 /* Success. */ 550 atomic_inc(&test_n_und 515 atomic_inc(&test_n_undone); 551 else 516 else 552 pr_err("Failed to star 517 pr_err("Failed to start %d kthread\n", i); 553 } 518 } 554 519 555 /* 520 /* 556 * Now let the workers do their job. 521 * Now let the workers do their job. 557 */ 522 */ 558 up_write(&prepare_for_test_rwsem); 523 up_write(&prepare_for_test_rwsem); 559 524 560 /* 525 /* 561 * Sleep quiet until all workers are d 526 * Sleep quiet until all workers are done with 1 second 562 * interval. Since the test can take a 527 * interval. Since the test can take a lot of time we 563 * can run into a stack trace of the h 528 * can run into a stack trace of the hung task. That is 564 * why we go with completion_timeout a 529 * why we go with completion_timeout and HZ value. 565 */ 530 */ 566 do { 531 do { 567 ret = wait_for_completion_time 532 ret = wait_for_completion_timeout(&test_all_done_comp, HZ); 568 } while (!ret); 533 } while (!ret); 569 534 570 for (i = 0; i < nr_threads; i++) { 535 for (i = 0; i < nr_threads; i++) { 571 struct test_driver *t = &tdriv 536 struct test_driver *t = &tdriver[i]; 572 int j; 537 int j; 573 538 574 if (!IS_ERR(t->task)) 539 if (!IS_ERR(t->task)) 575 kthread_stop(t->task); 540 kthread_stop(t->task); 576 541 577 for (j = 0; j < ARRAY_SIZE(tes 542 for (j = 0; j < ARRAY_SIZE(test_case_array); j++) { 578 if (!((run_test_mask & 543 if (!((run_test_mask & (1 << j)) >> j)) 579 continue; 544 continue; 580 545 581 pr_info( 546 pr_info( 582 "Summary: %s p 547 "Summary: %s passed: %d failed: %d repeat: %d loops: %d avg: %llu usec\n", 583 test_case_arra 548 test_case_array[j].test_name, 584 t->data[j].tes 549 t->data[j].test_passed, 585 t->data[j].tes 550 t->data[j].test_failed, 586 test_repeat_co 551 test_repeat_count, test_loop_count, 587 t->data[j].tim 552 t->data[j].time); 588 } 553 } 589 554 590 pr_info("All test took worker% 555 pr_info("All test took worker%d=%lu cycles\n", 591 i, t->stop - t->start) 556 i, t->stop - t->start); 592 } 557 } 593 558 594 kvfree(tdriver); 559 kvfree(tdriver); 595 } 560 } 596 561 597 static int vmalloc_test_init(void) 562 static int vmalloc_test_init(void) 598 { 563 { 599 do_concurrent_test(); 564 do_concurrent_test(); 600 return -EAGAIN; /* Fail will directly 565 return -EAGAIN; /* Fail will directly unload the module */ 601 } 566 } 602 567 >> 568 static void vmalloc_test_exit(void) >> 569 { >> 570 } >> 571 603 module_init(vmalloc_test_init) 572 module_init(vmalloc_test_init) >> 573 module_exit(vmalloc_test_exit) 604 574 605 MODULE_LICENSE("GPL"); 575 MODULE_LICENSE("GPL"); 606 MODULE_AUTHOR("Uladzislau Rezki"); 576 MODULE_AUTHOR("Uladzislau Rezki"); 607 MODULE_DESCRIPTION("vmalloc test module"); 577 MODULE_DESCRIPTION("vmalloc test module"); 608 578
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.