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