1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Common functions for in-kernel torture tests. 4 * 5 * Copyright (C) IBM Corporation, 2014 6 * 7 * Author: Paul E. McKenney <paulmck@linux.ibm.com> 8 * Based on kernel/rcu/torture.c. 9 */ 10 11 #define pr_fmt(fmt) fmt 12 13 #include <linux/types.h> 14 #include <linux/kernel.h> 15 #include <linux/init.h> 16 #include <linux/module.h> 17 #include <linux/kthread.h> 18 #include <linux/err.h> 19 #include <linux/spinlock.h> 20 #include <linux/smp.h> 21 #include <linux/interrupt.h> 22 #include <linux/sched.h> 23 #include <linux/sched/clock.h> 24 #include <linux/atomic.h> 25 #include <linux/bitops.h> 26 #include <linux/completion.h> 27 #include <linux/moduleparam.h> 28 #include <linux/percpu.h> 29 #include <linux/notifier.h> 30 #include <linux/reboot.h> 31 #include <linux/freezer.h> 32 #include <linux/cpu.h> 33 #include <linux/delay.h> 34 #include <linux/stat.h> 35 #include <linux/slab.h> 36 #include <linux/trace_clock.h> 37 #include <linux/ktime.h> 38 #include <asm/byteorder.h> 39 #include <linux/torture.h> 40 #include "rcu/rcu.h" 41 42 MODULE_LICENSE("GPL"); 43 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>"); 44 45 static bool disable_onoff_at_boot; 46 module_param(disable_onoff_at_boot, bool, 0444); 47 48 static char *torture_type; 49 static int verbose; 50 51 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */ 52 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */ 53 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */ 54 #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */ 55 static int fullstop = FULLSTOP_RMMOD; 56 static DEFINE_MUTEX(fullstop_mutex); 57 58 #ifdef CONFIG_HOTPLUG_CPU 59 60 /* 61 * Variables for online-offline handling. Only present if CPU hotplug 62 * is enabled, otherwise does nothing. 63 */ 64 65 static struct task_struct *onoff_task; 66 static long onoff_holdoff; 67 static long onoff_interval; 68 static torture_ofl_func *onoff_f; 69 static long n_offline_attempts; 70 static long n_offline_successes; 71 static unsigned long sum_offline; 72 static int min_offline = -1; 73 static int max_offline; 74 static long n_online_attempts; 75 static long n_online_successes; 76 static unsigned long sum_online; 77 static int min_online = -1; 78 static int max_online; 79 80 /* 81 * Attempt to take a CPU offline. Return false if the CPU is already 82 * offline or if it is not subject to CPU-hotplug operations. The 83 * caller can detect other failures by looking at the statistics. 84 */ 85 bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes, 86 unsigned long *sum_offl, int *min_offl, int *max_offl) 87 { 88 unsigned long delta; 89 int ret; 90 char *s; 91 unsigned long starttime; 92 93 if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 94 return false; 95 if (num_online_cpus() <= 1) 96 return false; /* Can't offline the last CPU. */ 97 98 if (verbose > 1) 99 pr_alert("%s" TORTURE_FLAG 100 "torture_onoff task: offlining %d\n", 101 torture_type, cpu); 102 starttime = jiffies; 103 (*n_offl_attempts)++; 104 ret = cpu_down(cpu); 105 if (ret) { 106 s = ""; 107 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { 108 // PCI probe frequently disables hotplug during boot. 109 (*n_offl_attempts)--; 110 s = " (-EBUSY forgiven during boot)"; 111 } 112 if (verbose) 113 pr_alert("%s" TORTURE_FLAG 114 "torture_onoff task: offline %d failed%s: errno %d\n", 115 torture_type, cpu, s, ret); 116 } else { 117 if (verbose > 1) 118 pr_alert("%s" TORTURE_FLAG 119 "torture_onoff task: offlined %d\n", 120 torture_type, cpu); 121 if (onoff_f) 122 onoff_f(); 123 (*n_offl_successes)++; 124 delta = jiffies - starttime; 125 *sum_offl += delta; 126 if (*min_offl < 0) { 127 *min_offl = delta; 128 *max_offl = delta; 129 } 130 if (*min_offl > delta) 131 *min_offl = delta; 132 if (*max_offl < delta) 133 *max_offl = delta; 134 } 135 136 return true; 137 } 138 EXPORT_SYMBOL_GPL(torture_offline); 139 140 /* 141 * Attempt to bring a CPU online. Return false if the CPU is already 142 * online or if it is not subject to CPU-hotplug operations. The 143 * caller can detect other failures by looking at the statistics. 144 */ 145 bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes, 146 unsigned long *sum_onl, int *min_onl, int *max_onl) 147 { 148 unsigned long delta; 149 int ret; 150 char *s; 151 unsigned long starttime; 152 153 if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 154 return false; 155 156 if (verbose > 1) 157 pr_alert("%s" TORTURE_FLAG 158 "torture_onoff task: onlining %d\n", 159 torture_type, cpu); 160 starttime = jiffies; 161 (*n_onl_attempts)++; 162 ret = cpu_up(cpu); 163 if (ret) { 164 s = ""; 165 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { 166 // PCI probe frequently disables hotplug during boot. 167 (*n_onl_attempts)--; 168 s = " (-EBUSY forgiven during boot)"; 169 } 170 if (verbose) 171 pr_alert("%s" TORTURE_FLAG 172 "torture_onoff task: online %d failed%s: errno %d\n", 173 torture_type, cpu, s, ret); 174 } else { 175 if (verbose > 1) 176 pr_alert("%s" TORTURE_FLAG 177 "torture_onoff task: onlined %d\n", 178 torture_type, cpu); 179 (*n_onl_successes)++; 180 delta = jiffies - starttime; 181 *sum_onl += delta; 182 if (*min_onl < 0) { 183 *min_onl = delta; 184 *max_onl = delta; 185 } 186 if (*min_onl > delta) 187 *min_onl = delta; 188 if (*max_onl < delta) 189 *max_onl = delta; 190 } 191 192 return true; 193 } 194 EXPORT_SYMBOL_GPL(torture_online); 195 196 /* 197 * Execute random CPU-hotplug operations at the interval specified 198 * by the onoff_interval. 199 */ 200 static int 201 torture_onoff(void *arg) 202 { 203 int cpu; 204 int maxcpu = -1; 205 DEFINE_TORTURE_RANDOM(rand); 206 int ret; 207 208 VERBOSE_TOROUT_STRING("torture_onoff task started"); 209 for_each_online_cpu(cpu) 210 maxcpu = cpu; 211 WARN_ON(maxcpu < 0); 212 if (!IS_MODULE(CONFIG_TORTURE_TEST)) 213 for_each_possible_cpu(cpu) { 214 if (cpu_online(cpu)) 215 continue; 216 ret = cpu_up(cpu); 217 if (ret && verbose) { 218 pr_alert("%s" TORTURE_FLAG 219 "%s: Initial online %d: errno %d\n", 220 __func__, torture_type, cpu, ret); 221 } 222 } 223 224 if (maxcpu == 0) { 225 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled"); 226 goto stop; 227 } 228 229 if (onoff_holdoff > 0) { 230 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff"); 231 schedule_timeout_interruptible(onoff_holdoff); 232 VERBOSE_TOROUT_STRING("torture_onoff end holdoff"); 233 } 234 while (!torture_must_stop()) { 235 if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) { 236 schedule_timeout_interruptible(HZ / 10); 237 continue; 238 } 239 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1); 240 if (!torture_offline(cpu, 241 &n_offline_attempts, &n_offline_successes, 242 &sum_offline, &min_offline, &max_offline)) 243 torture_online(cpu, 244 &n_online_attempts, &n_online_successes, 245 &sum_online, &min_online, &max_online); 246 schedule_timeout_interruptible(onoff_interval); 247 } 248 249 stop: 250 torture_kthread_stopping("torture_onoff"); 251 return 0; 252 } 253 254 #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 255 256 /* 257 * Initiate online-offline handling. 258 */ 259 int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f) 260 { 261 #ifdef CONFIG_HOTPLUG_CPU 262 onoff_holdoff = ooholdoff; 263 onoff_interval = oointerval; 264 onoff_f = f; 265 if (onoff_interval <= 0) 266 return 0; 267 return torture_create_kthread(torture_onoff, NULL, onoff_task); 268 #else /* #ifdef CONFIG_HOTPLUG_CPU */ 269 return 0; 270 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 271 } 272 EXPORT_SYMBOL_GPL(torture_onoff_init); 273 274 /* 275 * Clean up after online/offline testing. 276 */ 277 static void torture_onoff_cleanup(void) 278 { 279 #ifdef CONFIG_HOTPLUG_CPU 280 if (onoff_task == NULL) 281 return; 282 VERBOSE_TOROUT_STRING("Stopping torture_onoff task"); 283 kthread_stop(onoff_task); 284 onoff_task = NULL; 285 #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 286 } 287 288 /* 289 * Print online/offline testing statistics. 290 */ 291 void torture_onoff_stats(void) 292 { 293 #ifdef CONFIG_HOTPLUG_CPU 294 pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ", 295 n_online_successes, n_online_attempts, 296 n_offline_successes, n_offline_attempts, 297 min_online, max_online, 298 min_offline, max_offline, 299 sum_online, sum_offline, HZ); 300 #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 301 } 302 EXPORT_SYMBOL_GPL(torture_onoff_stats); 303 304 /* 305 * Were all the online/offline operations successful? 306 */ 307 bool torture_onoff_failures(void) 308 { 309 #ifdef CONFIG_HOTPLUG_CPU 310 return n_online_successes != n_online_attempts || 311 n_offline_successes != n_offline_attempts; 312 #else /* #ifdef CONFIG_HOTPLUG_CPU */ 313 return false; 314 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 315 } 316 EXPORT_SYMBOL_GPL(torture_onoff_failures); 317 318 #define TORTURE_RANDOM_MULT 39916801 /* prime */ 319 #define TORTURE_RANDOM_ADD 479001701 /* prime */ 320 #define TORTURE_RANDOM_REFRESH 10000 321 322 /* 323 * Crude but fast random-number generator. Uses a linear congruential 324 * generator, with occasional help from cpu_clock(). 325 */ 326 unsigned long 327 torture_random(struct torture_random_state *trsp) 328 { 329 if (--trsp->trs_count < 0) { 330 trsp->trs_state += (unsigned long)local_clock(); 331 trsp->trs_count = TORTURE_RANDOM_REFRESH; 332 } 333 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + 334 TORTURE_RANDOM_ADD; 335 return swahw32(trsp->trs_state); 336 } 337 EXPORT_SYMBOL_GPL(torture_random); 338 339 /* 340 * Variables for shuffling. The idea is to ensure that each CPU stays 341 * idle for an extended period to test interactions with dyntick idle, 342 * as well as interactions with any per-CPU variables. 343 */ 344 struct shuffle_task { 345 struct list_head st_l; 346 struct task_struct *st_t; 347 }; 348 349 static long shuffle_interval; /* In jiffies. */ 350 static struct task_struct *shuffler_task; 351 static cpumask_var_t shuffle_tmp_mask; 352 static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ 353 static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); 354 static DEFINE_MUTEX(shuffle_task_mutex); 355 356 /* 357 * Register a task to be shuffled. If there is no memory, just splat 358 * and don't bother registering. 359 */ 360 void torture_shuffle_task_register(struct task_struct *tp) 361 { 362 struct shuffle_task *stp; 363 364 if (WARN_ON_ONCE(tp == NULL)) 365 return; 366 stp = kmalloc(sizeof(*stp), GFP_KERNEL); 367 if (WARN_ON_ONCE(stp == NULL)) 368 return; 369 stp->st_t = tp; 370 mutex_lock(&shuffle_task_mutex); 371 list_add(&stp->st_l, &shuffle_task_list); 372 mutex_unlock(&shuffle_task_mutex); 373 } 374 EXPORT_SYMBOL_GPL(torture_shuffle_task_register); 375 376 /* 377 * Unregister all tasks, for example, at the end of the torture run. 378 */ 379 static void torture_shuffle_task_unregister_all(void) 380 { 381 struct shuffle_task *stp; 382 struct shuffle_task *p; 383 384 mutex_lock(&shuffle_task_mutex); 385 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { 386 list_del(&stp->st_l); 387 kfree(stp); 388 } 389 mutex_unlock(&shuffle_task_mutex); 390 } 391 392 /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. 393 * A special case is when shuffle_idle_cpu = -1, in which case we allow 394 * the tasks to run on all CPUs. 395 */ 396 static void torture_shuffle_tasks(void) 397 { 398 struct shuffle_task *stp; 399 400 cpumask_setall(shuffle_tmp_mask); 401 get_online_cpus(); 402 403 /* No point in shuffling if there is only one online CPU (ex: UP) */ 404 if (num_online_cpus() == 1) { 405 put_online_cpus(); 406 return; 407 } 408 409 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ 410 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); 411 if (shuffle_idle_cpu >= nr_cpu_ids) 412 shuffle_idle_cpu = -1; 413 else 414 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); 415 416 mutex_lock(&shuffle_task_mutex); 417 list_for_each_entry(stp, &shuffle_task_list, st_l) 418 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); 419 mutex_unlock(&shuffle_task_mutex); 420 421 put_online_cpus(); 422 } 423 424 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the 425 * system to become idle at a time and cut off its timer ticks. This is meant 426 * to test the support for such tickless idle CPU in RCU. 427 */ 428 static int torture_shuffle(void *arg) 429 { 430 VERBOSE_TOROUT_STRING("torture_shuffle task started"); 431 do { 432 schedule_timeout_interruptible(shuffle_interval); 433 torture_shuffle_tasks(); 434 torture_shutdown_absorb("torture_shuffle"); 435 } while (!torture_must_stop()); 436 torture_kthread_stopping("torture_shuffle"); 437 return 0; 438 } 439 440 /* 441 * Start the shuffler, with shuffint in jiffies. 442 */ 443 int torture_shuffle_init(long shuffint) 444 { 445 shuffle_interval = shuffint; 446 447 shuffle_idle_cpu = -1; 448 449 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { 450 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask"); 451 return -ENOMEM; 452 } 453 454 /* Create the shuffler thread */ 455 return torture_create_kthread(torture_shuffle, NULL, shuffler_task); 456 } 457 EXPORT_SYMBOL_GPL(torture_shuffle_init); 458 459 /* 460 * Stop the shuffling. 461 */ 462 static void torture_shuffle_cleanup(void) 463 { 464 torture_shuffle_task_unregister_all(); 465 if (shuffler_task) { 466 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); 467 kthread_stop(shuffler_task); 468 free_cpumask_var(shuffle_tmp_mask); 469 } 470 shuffler_task = NULL; 471 } 472 473 /* 474 * Variables for auto-shutdown. This allows "lights out" torture runs 475 * to be fully scripted. 476 */ 477 static struct task_struct *shutdown_task; 478 static ktime_t shutdown_time; /* time to system shutdown. */ 479 static void (*torture_shutdown_hook)(void); 480 481 /* 482 * Absorb kthreads into a kernel function that won't return, so that 483 * they won't ever access module text or data again. 484 */ 485 void torture_shutdown_absorb(const char *title) 486 { 487 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 488 pr_notice("torture thread %s parking due to system shutdown\n", 489 title); 490 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); 491 } 492 } 493 EXPORT_SYMBOL_GPL(torture_shutdown_absorb); 494 495 /* 496 * Cause the torture test to shutdown the system after the test has 497 * run for the time specified by the shutdown_secs parameter. 498 */ 499 static int torture_shutdown(void *arg) 500 { 501 ktime_t ktime_snap; 502 503 VERBOSE_TOROUT_STRING("torture_shutdown task started"); 504 ktime_snap = ktime_get(); 505 while (ktime_before(ktime_snap, shutdown_time) && 506 !torture_must_stop()) { 507 if (verbose) 508 pr_alert("%s" TORTURE_FLAG 509 "torture_shutdown task: %llu ms remaining\n", 510 torture_type, 511 ktime_ms_delta(shutdown_time, ktime_snap)); 512 set_current_state(TASK_INTERRUPTIBLE); 513 schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS); 514 ktime_snap = ktime_get(); 515 } 516 if (torture_must_stop()) { 517 torture_kthread_stopping("torture_shutdown"); 518 return 0; 519 } 520 521 /* OK, shut down the system. */ 522 523 VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system"); 524 shutdown_task = NULL; /* Avoid self-kill deadlock. */ 525 if (torture_shutdown_hook) 526 torture_shutdown_hook(); 527 else 528 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping."); 529 rcu_ftrace_dump(DUMP_ALL); 530 kernel_power_off(); /* Shut down the system. */ 531 return 0; 532 } 533 534 /* 535 * Start up the shutdown task. 536 */ 537 int torture_shutdown_init(int ssecs, void (*cleanup)(void)) 538 { 539 torture_shutdown_hook = cleanup; 540 if (ssecs > 0) { 541 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0)); 542 return torture_create_kthread(torture_shutdown, NULL, 543 shutdown_task); 544 } 545 return 0; 546 } 547 EXPORT_SYMBOL_GPL(torture_shutdown_init); 548 549 /* 550 * Detect and respond to a system shutdown. 551 */ 552 static int torture_shutdown_notify(struct notifier_block *unused1, 553 unsigned long unused2, void *unused3) 554 { 555 mutex_lock(&fullstop_mutex); 556 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) { 557 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected"); 558 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN); 559 } else { 560 pr_warn("Concurrent rmmod and shutdown illegal!\n"); 561 } 562 mutex_unlock(&fullstop_mutex); 563 return NOTIFY_DONE; 564 } 565 566 static struct notifier_block torture_shutdown_nb = { 567 .notifier_call = torture_shutdown_notify, 568 }; 569 570 /* 571 * Shut down the shutdown task. Say what??? Heh! This can happen if 572 * the torture module gets an rmmod before the shutdown time arrives. ;-) 573 */ 574 static void torture_shutdown_cleanup(void) 575 { 576 unregister_reboot_notifier(&torture_shutdown_nb); 577 if (shutdown_task != NULL) { 578 VERBOSE_TOROUT_STRING("Stopping torture_shutdown task"); 579 kthread_stop(shutdown_task); 580 } 581 shutdown_task = NULL; 582 } 583 584 /* 585 * Variables for stuttering, which means to periodically pause and 586 * restart testing in order to catch bugs that appear when load is 587 * suddenly applied to or removed from the system. 588 */ 589 static struct task_struct *stutter_task; 590 static int stutter_pause_test; 591 static int stutter; 592 static int stutter_gap; 593 594 /* 595 * Block until the stutter interval ends. This must be called periodically 596 * by all running kthreads that need to be subject to stuttering. 597 */ 598 bool stutter_wait(const char *title) 599 { 600 int spt; 601 bool ret = false; 602 603 cond_resched_tasks_rcu_qs(); 604 spt = READ_ONCE(stutter_pause_test); 605 for (; spt; spt = READ_ONCE(stutter_pause_test)) { 606 ret = true; 607 if (spt == 1) { 608 schedule_timeout_interruptible(1); 609 } else if (spt == 2) { 610 while (READ_ONCE(stutter_pause_test)) 611 cond_resched(); 612 } else { 613 schedule_timeout_interruptible(round_jiffies_relative(HZ)); 614 } 615 torture_shutdown_absorb(title); 616 } 617 return ret; 618 } 619 EXPORT_SYMBOL_GPL(stutter_wait); 620 621 /* 622 * Cause the torture test to "stutter", starting and stopping all 623 * threads periodically. 624 */ 625 static int torture_stutter(void *arg) 626 { 627 int wtime; 628 629 VERBOSE_TOROUT_STRING("torture_stutter task started"); 630 do { 631 if (!torture_must_stop() && stutter > 1) { 632 wtime = stutter; 633 if (stutter > HZ + 1) { 634 WRITE_ONCE(stutter_pause_test, 1); 635 wtime = stutter - HZ - 1; 636 schedule_timeout_interruptible(wtime); 637 wtime = HZ + 1; 638 } 639 WRITE_ONCE(stutter_pause_test, 2); 640 schedule_timeout_interruptible(wtime); 641 } 642 WRITE_ONCE(stutter_pause_test, 0); 643 if (!torture_must_stop()) 644 schedule_timeout_interruptible(stutter_gap); 645 torture_shutdown_absorb("torture_stutter"); 646 } while (!torture_must_stop()); 647 torture_kthread_stopping("torture_stutter"); 648 return 0; 649 } 650 651 /* 652 * Initialize and kick off the torture_stutter kthread. 653 */ 654 int torture_stutter_init(const int s, const int sgap) 655 { 656 stutter = s; 657 stutter_gap = sgap; 658 return torture_create_kthread(torture_stutter, NULL, stutter_task); 659 } 660 EXPORT_SYMBOL_GPL(torture_stutter_init); 661 662 /* 663 * Cleanup after the torture_stutter kthread. 664 */ 665 static void torture_stutter_cleanup(void) 666 { 667 if (!stutter_task) 668 return; 669 VERBOSE_TOROUT_STRING("Stopping torture_stutter task"); 670 kthread_stop(stutter_task); 671 stutter_task = NULL; 672 } 673 674 /* 675 * Initialize torture module. Please note that this is -not- invoked via 676 * the usual module_init() mechanism, but rather by an explicit call from 677 * the client torture module. This call must be paired with a later 678 * torture_init_end(). 679 * 680 * The runnable parameter points to a flag that controls whether or not 681 * the test is currently runnable. If there is no such flag, pass in NULL. 682 */ 683 bool torture_init_begin(char *ttype, int v) 684 { 685 mutex_lock(&fullstop_mutex); 686 if (torture_type != NULL) { 687 pr_alert("torture_init_begin: Refusing %s init: %s running.\n", 688 ttype, torture_type); 689 pr_alert("torture_init_begin: One torture test at a time!\n"); 690 mutex_unlock(&fullstop_mutex); 691 return false; 692 } 693 torture_type = ttype; 694 verbose = v; 695 fullstop = FULLSTOP_DONTSTOP; 696 return true; 697 } 698 EXPORT_SYMBOL_GPL(torture_init_begin); 699 700 /* 701 * Tell the torture module that initialization is complete. 702 */ 703 void torture_init_end(void) 704 { 705 mutex_unlock(&fullstop_mutex); 706 register_reboot_notifier(&torture_shutdown_nb); 707 } 708 EXPORT_SYMBOL_GPL(torture_init_end); 709 710 /* 711 * Clean up torture module. Please note that this is -not- invoked via 712 * the usual module_exit() mechanism, but rather by an explicit call from 713 * the client torture module. Returns true if a race with system shutdown 714 * is detected, otherwise, all kthreads started by functions in this file 715 * will be shut down. 716 * 717 * This must be called before the caller starts shutting down its own 718 * kthreads. 719 * 720 * Both torture_cleanup_begin() and torture_cleanup_end() must be paired, 721 * in order to correctly perform the cleanup. They are separated because 722 * threads can still need to reference the torture_type type, thus nullify 723 * only after completing all other relevant calls. 724 */ 725 bool torture_cleanup_begin(void) 726 { 727 mutex_lock(&fullstop_mutex); 728 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 729 pr_warn("Concurrent rmmod and shutdown illegal!\n"); 730 mutex_unlock(&fullstop_mutex); 731 schedule_timeout_uninterruptible(10); 732 return true; 733 } 734 WRITE_ONCE(fullstop, FULLSTOP_RMMOD); 735 mutex_unlock(&fullstop_mutex); 736 torture_shutdown_cleanup(); 737 torture_shuffle_cleanup(); 738 torture_stutter_cleanup(); 739 torture_onoff_cleanup(); 740 return false; 741 } 742 EXPORT_SYMBOL_GPL(torture_cleanup_begin); 743 744 void torture_cleanup_end(void) 745 { 746 mutex_lock(&fullstop_mutex); 747 torture_type = NULL; 748 mutex_unlock(&fullstop_mutex); 749 } 750 EXPORT_SYMBOL_GPL(torture_cleanup_end); 751 752 /* 753 * Is it time for the current torture test to stop? 754 */ 755 bool torture_must_stop(void) 756 { 757 return torture_must_stop_irq() || kthread_should_stop(); 758 } 759 EXPORT_SYMBOL_GPL(torture_must_stop); 760 761 /* 762 * Is it time for the current torture test to stop? This is the irq-safe 763 * version, hence no check for kthread_should_stop(). 764 */ 765 bool torture_must_stop_irq(void) 766 { 767 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP; 768 } 769 EXPORT_SYMBOL_GPL(torture_must_stop_irq); 770 771 /* 772 * Each kthread must wait for kthread_should_stop() before returning from 773 * its top-level function, otherwise segfaults ensue. This function 774 * prints a "stopping" message and waits for kthread_should_stop(), and 775 * should be called from all torture kthreads immediately prior to 776 * returning. 777 */ 778 void torture_kthread_stopping(char *title) 779 { 780 char buf[128]; 781 782 snprintf(buf, sizeof(buf), "Stopping %s", title); 783 VERBOSE_TOROUT_STRING(buf); 784 while (!kthread_should_stop()) { 785 torture_shutdown_absorb(title); 786 schedule_timeout_uninterruptible(1); 787 } 788 } 789 EXPORT_SYMBOL_GPL(torture_kthread_stopping); 790 791 /* 792 * Create a generic torture kthread that is immediately runnable. If you 793 * need the kthread to be stopped so that you can do something to it before 794 * it starts, you will need to open-code your own. 795 */ 796 int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m, 797 char *f, struct task_struct **tp) 798 { 799 int ret = 0; 800 801 VERBOSE_TOROUT_STRING(m); 802 *tp = kthread_run(fn, arg, "%s", s); 803 if (IS_ERR(*tp)) { 804 ret = PTR_ERR(*tp); 805 VERBOSE_TOROUT_ERRSTRING(f); 806 *tp = NULL; 807 } 808 torture_shuffle_task_register(*tp); 809 return ret; 810 } 811 EXPORT_SYMBOL_GPL(_torture_create_kthread); 812 813 /* 814 * Stop a generic kthread, emitting a message. 815 */ 816 void _torture_stop_kthread(char *m, struct task_struct **tp) 817 { 818 if (*tp == NULL) 819 return; 820 VERBOSE_TOROUT_STRING(m); 821 kthread_stop(*tp); 822 *tp = NULL; 823 } 824 EXPORT_SYMBOL_GPL(_torture_stop_kthread); 825