Back to home page

LXR

 
 

    


0001 /*
0002  * Common functions for in-kernel torture tests.
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, you can access it online at
0016  * http://www.gnu.org/licenses/gpl-2.0.html.
0017  *
0018  * Copyright (C) IBM Corporation, 2014
0019  *
0020  * Author: Paul E. McKenney <paulmck@us.ibm.com>
0021  *  Based on kernel/rcu/torture.c.
0022  */
0023 #include <linux/types.h>
0024 #include <linux/kernel.h>
0025 #include <linux/init.h>
0026 #include <linux/module.h>
0027 #include <linux/kthread.h>
0028 #include <linux/err.h>
0029 #include <linux/spinlock.h>
0030 #include <linux/smp.h>
0031 #include <linux/interrupt.h>
0032 #include <linux/sched.h>
0033 #include <linux/atomic.h>
0034 #include <linux/bitops.h>
0035 #include <linux/completion.h>
0036 #include <linux/moduleparam.h>
0037 #include <linux/percpu.h>
0038 #include <linux/notifier.h>
0039 #include <linux/reboot.h>
0040 #include <linux/freezer.h>
0041 #include <linux/cpu.h>
0042 #include <linux/delay.h>
0043 #include <linux/stat.h>
0044 #include <linux/slab.h>
0045 #include <linux/trace_clock.h>
0046 #include <linux/ktime.h>
0047 #include <asm/byteorder.h>
0048 #include <linux/torture.h>
0049 
0050 MODULE_LICENSE("GPL");
0051 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
0052 
0053 static char *torture_type;
0054 static bool verbose;
0055 
0056 /* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
0057 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
0058 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
0059 #define FULLSTOP_RMMOD    2 /* Normal rmmod of torture. */
0060 static int fullstop = FULLSTOP_RMMOD;
0061 static DEFINE_MUTEX(fullstop_mutex);
0062 static int *torture_runnable;
0063 
0064 #ifdef CONFIG_HOTPLUG_CPU
0065 
0066 /*
0067  * Variables for online-offline handling.  Only present if CPU hotplug
0068  * is enabled, otherwise does nothing.
0069  */
0070 
0071 static struct task_struct *onoff_task;
0072 static long onoff_holdoff;
0073 static long onoff_interval;
0074 static long n_offline_attempts;
0075 static long n_offline_successes;
0076 static unsigned long sum_offline;
0077 static int min_offline = -1;
0078 static int max_offline;
0079 static long n_online_attempts;
0080 static long n_online_successes;
0081 static unsigned long sum_online;
0082 static int min_online = -1;
0083 static int max_online;
0084 
0085 /*
0086  * Attempt to take a CPU offline.  Return false if the CPU is already
0087  * offline or if it is not subject to CPU-hotplug operations.  The
0088  * caller can detect other failures by looking at the statistics.
0089  */
0090 bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
0091              unsigned long *sum_offl, int *min_offl, int *max_offl)
0092 {
0093     unsigned long delta;
0094     int ret;
0095     unsigned long starttime;
0096 
0097     if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
0098         return false;
0099 
0100     if (verbose)
0101         pr_alert("%s" TORTURE_FLAG
0102              "torture_onoff task: offlining %d\n",
0103              torture_type, cpu);
0104     starttime = jiffies;
0105     (*n_offl_attempts)++;
0106     ret = cpu_down(cpu);
0107     if (ret) {
0108         if (verbose)
0109             pr_alert("%s" TORTURE_FLAG
0110                  "torture_onoff task: offline %d failed: errno %d\n",
0111                  torture_type, cpu, ret);
0112     } else {
0113         if (verbose)
0114             pr_alert("%s" TORTURE_FLAG
0115                  "torture_onoff task: offlined %d\n",
0116                  torture_type, cpu);
0117         (*n_offl_successes)++;
0118         delta = jiffies - starttime;
0119         sum_offl += delta;
0120         if (*min_offl < 0) {
0121             *min_offl = delta;
0122             *max_offl = delta;
0123         }
0124         if (*min_offl > delta)
0125             *min_offl = delta;
0126         if (*max_offl < delta)
0127             *max_offl = delta;
0128     }
0129 
0130     return true;
0131 }
0132 EXPORT_SYMBOL_GPL(torture_offline);
0133 
0134 /*
0135  * Attempt to bring a CPU online.  Return false if the CPU is already
0136  * online or if it is not subject to CPU-hotplug operations.  The
0137  * caller can detect other failures by looking at the statistics.
0138  */
0139 bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
0140             unsigned long *sum_onl, int *min_onl, int *max_onl)
0141 {
0142     unsigned long delta;
0143     int ret;
0144     unsigned long starttime;
0145 
0146     if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
0147         return false;
0148 
0149     if (verbose)
0150         pr_alert("%s" TORTURE_FLAG
0151              "torture_onoff task: onlining %d\n",
0152              torture_type, cpu);
0153     starttime = jiffies;
0154     (*n_onl_attempts)++;
0155     ret = cpu_up(cpu);
0156     if (ret) {
0157         if (verbose)
0158             pr_alert("%s" TORTURE_FLAG
0159                  "torture_onoff task: online %d failed: errno %d\n",
0160                  torture_type, cpu, ret);
0161     } else {
0162         if (verbose)
0163             pr_alert("%s" TORTURE_FLAG
0164                  "torture_onoff task: onlined %d\n",
0165                  torture_type, cpu);
0166         (*n_onl_successes)++;
0167         delta = jiffies - starttime;
0168         *sum_onl += delta;
0169         if (*min_onl < 0) {
0170             *min_onl = delta;
0171             *max_onl = delta;
0172         }
0173         if (*min_onl > delta)
0174             *min_onl = delta;
0175         if (*max_onl < delta)
0176             *max_onl = delta;
0177     }
0178 
0179     return true;
0180 }
0181 EXPORT_SYMBOL_GPL(torture_online);
0182 
0183 /*
0184  * Execute random CPU-hotplug operations at the interval specified
0185  * by the onoff_interval.
0186  */
0187 static int
0188 torture_onoff(void *arg)
0189 {
0190     int cpu;
0191     int maxcpu = -1;
0192     DEFINE_TORTURE_RANDOM(rand);
0193 
0194     VERBOSE_TOROUT_STRING("torture_onoff task started");
0195     for_each_online_cpu(cpu)
0196         maxcpu = cpu;
0197     WARN_ON(maxcpu < 0);
0198 
0199     if (maxcpu == 0) {
0200         VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
0201         goto stop;
0202     }
0203 
0204     if (onoff_holdoff > 0) {
0205         VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
0206         schedule_timeout_interruptible(onoff_holdoff);
0207         VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
0208     }
0209     while (!torture_must_stop()) {
0210         cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
0211         if (!torture_offline(cpu,
0212                      &n_offline_attempts, &n_offline_successes,
0213                      &sum_offline, &min_offline, &max_offline))
0214             torture_online(cpu,
0215                        &n_online_attempts, &n_online_successes,
0216                        &sum_online, &min_online, &max_online);
0217         schedule_timeout_interruptible(onoff_interval);
0218     }
0219 
0220 stop:
0221     torture_kthread_stopping("torture_onoff");
0222     return 0;
0223 }
0224 
0225 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
0226 
0227 /*
0228  * Initiate online-offline handling.
0229  */
0230 int torture_onoff_init(long ooholdoff, long oointerval)
0231 {
0232     int ret = 0;
0233 
0234 #ifdef CONFIG_HOTPLUG_CPU
0235     onoff_holdoff = ooholdoff;
0236     onoff_interval = oointerval;
0237     if (onoff_interval <= 0)
0238         return 0;
0239     ret = torture_create_kthread(torture_onoff, NULL, onoff_task);
0240 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
0241     return ret;
0242 }
0243 EXPORT_SYMBOL_GPL(torture_onoff_init);
0244 
0245 /*
0246  * Clean up after online/offline testing.
0247  */
0248 static void torture_onoff_cleanup(void)
0249 {
0250 #ifdef CONFIG_HOTPLUG_CPU
0251     if (onoff_task == NULL)
0252         return;
0253     VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
0254     kthread_stop(onoff_task);
0255     onoff_task = NULL;
0256 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
0257 }
0258 EXPORT_SYMBOL_GPL(torture_onoff_cleanup);
0259 
0260 /*
0261  * Print online/offline testing statistics.
0262  */
0263 void torture_onoff_stats(void)
0264 {
0265 #ifdef CONFIG_HOTPLUG_CPU
0266     pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
0267         n_online_successes, n_online_attempts,
0268         n_offline_successes, n_offline_attempts,
0269         min_online, max_online,
0270         min_offline, max_offline,
0271         sum_online, sum_offline, HZ);
0272 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
0273 }
0274 EXPORT_SYMBOL_GPL(torture_onoff_stats);
0275 
0276 /*
0277  * Were all the online/offline operations successful?
0278  */
0279 bool torture_onoff_failures(void)
0280 {
0281 #ifdef CONFIG_HOTPLUG_CPU
0282     return n_online_successes != n_online_attempts ||
0283            n_offline_successes != n_offline_attempts;
0284 #else /* #ifdef CONFIG_HOTPLUG_CPU */
0285     return false;
0286 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
0287 }
0288 EXPORT_SYMBOL_GPL(torture_onoff_failures);
0289 
0290 #define TORTURE_RANDOM_MULT 39916801  /* prime */
0291 #define TORTURE_RANDOM_ADD  479001701 /* prime */
0292 #define TORTURE_RANDOM_REFRESH  10000
0293 
0294 /*
0295  * Crude but fast random-number generator.  Uses a linear congruential
0296  * generator, with occasional help from cpu_clock().
0297  */
0298 unsigned long
0299 torture_random(struct torture_random_state *trsp)
0300 {
0301     if (--trsp->trs_count < 0) {
0302         trsp->trs_state += (unsigned long)local_clock();
0303         trsp->trs_count = TORTURE_RANDOM_REFRESH;
0304     }
0305     trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
0306         TORTURE_RANDOM_ADD;
0307     return swahw32(trsp->trs_state);
0308 }
0309 EXPORT_SYMBOL_GPL(torture_random);
0310 
0311 /*
0312  * Variables for shuffling.  The idea is to ensure that each CPU stays
0313  * idle for an extended period to test interactions with dyntick idle,
0314  * as well as interactions with any per-CPU varibles.
0315  */
0316 struct shuffle_task {
0317     struct list_head st_l;
0318     struct task_struct *st_t;
0319 };
0320 
0321 static long shuffle_interval;   /* In jiffies. */
0322 static struct task_struct *shuffler_task;
0323 static cpumask_var_t shuffle_tmp_mask;
0324 static int shuffle_idle_cpu;    /* Force all torture tasks off this CPU */
0325 static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
0326 static DEFINE_MUTEX(shuffle_task_mutex);
0327 
0328 /*
0329  * Register a task to be shuffled.  If there is no memory, just splat
0330  * and don't bother registering.
0331  */
0332 void torture_shuffle_task_register(struct task_struct *tp)
0333 {
0334     struct shuffle_task *stp;
0335 
0336     if (WARN_ON_ONCE(tp == NULL))
0337         return;
0338     stp = kmalloc(sizeof(*stp), GFP_KERNEL);
0339     if (WARN_ON_ONCE(stp == NULL))
0340         return;
0341     stp->st_t = tp;
0342     mutex_lock(&shuffle_task_mutex);
0343     list_add(&stp->st_l, &shuffle_task_list);
0344     mutex_unlock(&shuffle_task_mutex);
0345 }
0346 EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
0347 
0348 /*
0349  * Unregister all tasks, for example, at the end of the torture run.
0350  */
0351 static void torture_shuffle_task_unregister_all(void)
0352 {
0353     struct shuffle_task *stp;
0354     struct shuffle_task *p;
0355 
0356     mutex_lock(&shuffle_task_mutex);
0357     list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
0358         list_del(&stp->st_l);
0359         kfree(stp);
0360     }
0361     mutex_unlock(&shuffle_task_mutex);
0362 }
0363 
0364 /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
0365  * A special case is when shuffle_idle_cpu = -1, in which case we allow
0366  * the tasks to run on all CPUs.
0367  */
0368 static void torture_shuffle_tasks(void)
0369 {
0370     struct shuffle_task *stp;
0371 
0372     cpumask_setall(shuffle_tmp_mask);
0373     get_online_cpus();
0374 
0375     /* No point in shuffling if there is only one online CPU (ex: UP) */
0376     if (num_online_cpus() == 1) {
0377         put_online_cpus();
0378         return;
0379     }
0380 
0381     /* Advance to the next CPU.  Upon overflow, don't idle any CPUs. */
0382     shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
0383     if (shuffle_idle_cpu >= nr_cpu_ids)
0384         shuffle_idle_cpu = -1;
0385     else
0386         cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
0387 
0388     mutex_lock(&shuffle_task_mutex);
0389     list_for_each_entry(stp, &shuffle_task_list, st_l)
0390         set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
0391     mutex_unlock(&shuffle_task_mutex);
0392 
0393     put_online_cpus();
0394 }
0395 
0396 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
0397  * system to become idle at a time and cut off its timer ticks. This is meant
0398  * to test the support for such tickless idle CPU in RCU.
0399  */
0400 static int torture_shuffle(void *arg)
0401 {
0402     VERBOSE_TOROUT_STRING("torture_shuffle task started");
0403     do {
0404         schedule_timeout_interruptible(shuffle_interval);
0405         torture_shuffle_tasks();
0406         torture_shutdown_absorb("torture_shuffle");
0407     } while (!torture_must_stop());
0408     torture_kthread_stopping("torture_shuffle");
0409     return 0;
0410 }
0411 
0412 /*
0413  * Start the shuffler, with shuffint in jiffies.
0414  */
0415 int torture_shuffle_init(long shuffint)
0416 {
0417     shuffle_interval = shuffint;
0418 
0419     shuffle_idle_cpu = -1;
0420 
0421     if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
0422         VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
0423         return -ENOMEM;
0424     }
0425 
0426     /* Create the shuffler thread */
0427     return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
0428 }
0429 EXPORT_SYMBOL_GPL(torture_shuffle_init);
0430 
0431 /*
0432  * Stop the shuffling.
0433  */
0434 static void torture_shuffle_cleanup(void)
0435 {
0436     torture_shuffle_task_unregister_all();
0437     if (shuffler_task) {
0438         VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
0439         kthread_stop(shuffler_task);
0440         free_cpumask_var(shuffle_tmp_mask);
0441     }
0442     shuffler_task = NULL;
0443 }
0444 EXPORT_SYMBOL_GPL(torture_shuffle_cleanup);
0445 
0446 /*
0447  * Variables for auto-shutdown.  This allows "lights out" torture runs
0448  * to be fully scripted.
0449  */
0450 static struct task_struct *shutdown_task;
0451 static ktime_t shutdown_time;       /* time to system shutdown. */
0452 static void (*torture_shutdown_hook)(void);
0453 
0454 /*
0455  * Absorb kthreads into a kernel function that won't return, so that
0456  * they won't ever access module text or data again.
0457  */
0458 void torture_shutdown_absorb(const char *title)
0459 {
0460     while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
0461         pr_notice("torture thread %s parking due to system shutdown\n",
0462               title);
0463         schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
0464     }
0465 }
0466 EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
0467 
0468 /*
0469  * Cause the torture test to shutdown the system after the test has
0470  * run for the time specified by the shutdown_secs parameter.
0471  */
0472 static int torture_shutdown(void *arg)
0473 {
0474     ktime_t ktime_snap;
0475 
0476     VERBOSE_TOROUT_STRING("torture_shutdown task started");
0477     ktime_snap = ktime_get();
0478     while (ktime_before(ktime_snap, shutdown_time) &&
0479            !torture_must_stop()) {
0480         if (verbose)
0481             pr_alert("%s" TORTURE_FLAG
0482                  "torture_shutdown task: %llu ms remaining\n",
0483                  torture_type,
0484                  ktime_ms_delta(shutdown_time, ktime_snap));
0485         set_current_state(TASK_INTERRUPTIBLE);
0486         schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
0487         ktime_snap = ktime_get();
0488     }
0489     if (torture_must_stop()) {
0490         torture_kthread_stopping("torture_shutdown");
0491         return 0;
0492     }
0493 
0494     /* OK, shut down the system. */
0495 
0496     VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
0497     shutdown_task = NULL;   /* Avoid self-kill deadlock. */
0498     if (torture_shutdown_hook)
0499         torture_shutdown_hook();
0500     else
0501         VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
0502     ftrace_dump(DUMP_ALL);
0503     kernel_power_off(); /* Shut down the system. */
0504     return 0;
0505 }
0506 
0507 /*
0508  * Start up the shutdown task.
0509  */
0510 int torture_shutdown_init(int ssecs, void (*cleanup)(void))
0511 {
0512     int ret = 0;
0513 
0514     torture_shutdown_hook = cleanup;
0515     if (ssecs > 0) {
0516         shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
0517         ret = torture_create_kthread(torture_shutdown, NULL,
0518                          shutdown_task);
0519     }
0520     return ret;
0521 }
0522 EXPORT_SYMBOL_GPL(torture_shutdown_init);
0523 
0524 /*
0525  * Detect and respond to a system shutdown.
0526  */
0527 static int torture_shutdown_notify(struct notifier_block *unused1,
0528                    unsigned long unused2, void *unused3)
0529 {
0530     mutex_lock(&fullstop_mutex);
0531     if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
0532         VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
0533         WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
0534     } else {
0535         pr_warn("Concurrent rmmod and shutdown illegal!\n");
0536     }
0537     mutex_unlock(&fullstop_mutex);
0538     return NOTIFY_DONE;
0539 }
0540 
0541 static struct notifier_block torture_shutdown_nb = {
0542     .notifier_call = torture_shutdown_notify,
0543 };
0544 
0545 /*
0546  * Shut down the shutdown task.  Say what???  Heh!  This can happen if
0547  * the torture module gets an rmmod before the shutdown time arrives.  ;-)
0548  */
0549 static void torture_shutdown_cleanup(void)
0550 {
0551     unregister_reboot_notifier(&torture_shutdown_nb);
0552     if (shutdown_task != NULL) {
0553         VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
0554         kthread_stop(shutdown_task);
0555     }
0556     shutdown_task = NULL;
0557 }
0558 
0559 /*
0560  * Variables for stuttering, which means to periodically pause and
0561  * restart testing in order to catch bugs that appear when load is
0562  * suddenly applied to or removed from the system.
0563  */
0564 static struct task_struct *stutter_task;
0565 static int stutter_pause_test;
0566 static int stutter;
0567 
0568 /*
0569  * Block until the stutter interval ends.  This must be called periodically
0570  * by all running kthreads that need to be subject to stuttering.
0571  */
0572 void stutter_wait(const char *title)
0573 {
0574     cond_resched_rcu_qs();
0575     while (READ_ONCE(stutter_pause_test) ||
0576            (torture_runnable && !READ_ONCE(*torture_runnable))) {
0577         if (stutter_pause_test)
0578             if (READ_ONCE(stutter_pause_test) == 1)
0579                 schedule_timeout_interruptible(1);
0580             else
0581                 while (READ_ONCE(stutter_pause_test))
0582                     cond_resched();
0583         else
0584             schedule_timeout_interruptible(round_jiffies_relative(HZ));
0585         torture_shutdown_absorb(title);
0586     }
0587 }
0588 EXPORT_SYMBOL_GPL(stutter_wait);
0589 
0590 /*
0591  * Cause the torture test to "stutter", starting and stopping all
0592  * threads periodically.
0593  */
0594 static int torture_stutter(void *arg)
0595 {
0596     VERBOSE_TOROUT_STRING("torture_stutter task started");
0597     do {
0598         if (!torture_must_stop()) {
0599             if (stutter > 1) {
0600                 schedule_timeout_interruptible(stutter - 1);
0601                 WRITE_ONCE(stutter_pause_test, 2);
0602             }
0603             schedule_timeout_interruptible(1);
0604             WRITE_ONCE(stutter_pause_test, 1);
0605         }
0606         if (!torture_must_stop())
0607             schedule_timeout_interruptible(stutter);
0608         WRITE_ONCE(stutter_pause_test, 0);
0609         torture_shutdown_absorb("torture_stutter");
0610     } while (!torture_must_stop());
0611     torture_kthread_stopping("torture_stutter");
0612     return 0;
0613 }
0614 
0615 /*
0616  * Initialize and kick off the torture_stutter kthread.
0617  */
0618 int torture_stutter_init(int s)
0619 {
0620     int ret;
0621 
0622     stutter = s;
0623     ret = torture_create_kthread(torture_stutter, NULL, stutter_task);
0624     return ret;
0625 }
0626 EXPORT_SYMBOL_GPL(torture_stutter_init);
0627 
0628 /*
0629  * Cleanup after the torture_stutter kthread.
0630  */
0631 static void torture_stutter_cleanup(void)
0632 {
0633     if (!stutter_task)
0634         return;
0635     VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
0636     kthread_stop(stutter_task);
0637     stutter_task = NULL;
0638 }
0639 
0640 /*
0641  * Initialize torture module.  Please note that this is -not- invoked via
0642  * the usual module_init() mechanism, but rather by an explicit call from
0643  * the client torture module.  This call must be paired with a later
0644  * torture_init_end().
0645  *
0646  * The runnable parameter points to a flag that controls whether or not
0647  * the test is currently runnable.  If there is no such flag, pass in NULL.
0648  */
0649 bool torture_init_begin(char *ttype, bool v, int *runnable)
0650 {
0651     mutex_lock(&fullstop_mutex);
0652     if (torture_type != NULL) {
0653         pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
0654              ttype, torture_type);
0655         pr_alert("torture_init_begin: One torture test at a time!\n");
0656         mutex_unlock(&fullstop_mutex);
0657         return false;
0658     }
0659     torture_type = ttype;
0660     verbose = v;
0661     torture_runnable = runnable;
0662     fullstop = FULLSTOP_DONTSTOP;
0663     return true;
0664 }
0665 EXPORT_SYMBOL_GPL(torture_init_begin);
0666 
0667 /*
0668  * Tell the torture module that initialization is complete.
0669  */
0670 void torture_init_end(void)
0671 {
0672     mutex_unlock(&fullstop_mutex);
0673     register_reboot_notifier(&torture_shutdown_nb);
0674 }
0675 EXPORT_SYMBOL_GPL(torture_init_end);
0676 
0677 /*
0678  * Clean up torture module.  Please note that this is -not- invoked via
0679  * the usual module_exit() mechanism, but rather by an explicit call from
0680  * the client torture module.  Returns true if a race with system shutdown
0681  * is detected, otherwise, all kthreads started by functions in this file
0682  * will be shut down.
0683  *
0684  * This must be called before the caller starts shutting down its own
0685  * kthreads.
0686  *
0687  * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
0688  * in order to correctly perform the cleanup. They are separated because
0689  * threads can still need to reference the torture_type type, thus nullify
0690  * only after completing all other relevant calls.
0691  */
0692 bool torture_cleanup_begin(void)
0693 {
0694     mutex_lock(&fullstop_mutex);
0695     if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
0696         pr_warn("Concurrent rmmod and shutdown illegal!\n");
0697         mutex_unlock(&fullstop_mutex);
0698         schedule_timeout_uninterruptible(10);
0699         return true;
0700     }
0701     WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
0702     mutex_unlock(&fullstop_mutex);
0703     torture_shutdown_cleanup();
0704     torture_shuffle_cleanup();
0705     torture_stutter_cleanup();
0706     torture_onoff_cleanup();
0707     return false;
0708 }
0709 EXPORT_SYMBOL_GPL(torture_cleanup_begin);
0710 
0711 void torture_cleanup_end(void)
0712 {
0713     mutex_lock(&fullstop_mutex);
0714     torture_type = NULL;
0715     mutex_unlock(&fullstop_mutex);
0716 }
0717 EXPORT_SYMBOL_GPL(torture_cleanup_end);
0718 
0719 /*
0720  * Is it time for the current torture test to stop?
0721  */
0722 bool torture_must_stop(void)
0723 {
0724     return torture_must_stop_irq() || kthread_should_stop();
0725 }
0726 EXPORT_SYMBOL_GPL(torture_must_stop);
0727 
0728 /*
0729  * Is it time for the current torture test to stop?  This is the irq-safe
0730  * version, hence no check for kthread_should_stop().
0731  */
0732 bool torture_must_stop_irq(void)
0733 {
0734     return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
0735 }
0736 EXPORT_SYMBOL_GPL(torture_must_stop_irq);
0737 
0738 /*
0739  * Each kthread must wait for kthread_should_stop() before returning from
0740  * its top-level function, otherwise segfaults ensue.  This function
0741  * prints a "stopping" message and waits for kthread_should_stop(), and
0742  * should be called from all torture kthreads immediately prior to
0743  * returning.
0744  */
0745 void torture_kthread_stopping(char *title)
0746 {
0747     char buf[128];
0748 
0749     snprintf(buf, sizeof(buf), "Stopping %s", title);
0750     VERBOSE_TOROUT_STRING(buf);
0751     while (!kthread_should_stop()) {
0752         torture_shutdown_absorb(title);
0753         schedule_timeout_uninterruptible(1);
0754     }
0755 }
0756 EXPORT_SYMBOL_GPL(torture_kthread_stopping);
0757 
0758 /*
0759  * Create a generic torture kthread that is immediately runnable.  If you
0760  * need the kthread to be stopped so that you can do something to it before
0761  * it starts, you will need to open-code your own.
0762  */
0763 int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
0764                 char *f, struct task_struct **tp)
0765 {
0766     int ret = 0;
0767 
0768     VERBOSE_TOROUT_STRING(m);
0769     *tp = kthread_run(fn, arg, "%s", s);
0770     if (IS_ERR(*tp)) {
0771         ret = PTR_ERR(*tp);
0772         VERBOSE_TOROUT_ERRSTRING(f);
0773         *tp = NULL;
0774     }
0775     torture_shuffle_task_register(*tp);
0776     return ret;
0777 }
0778 EXPORT_SYMBOL_GPL(_torture_create_kthread);
0779 
0780 /*
0781  * Stop a generic kthread, emitting a message.
0782  */
0783 void _torture_stop_kthread(char *m, struct task_struct **tp)
0784 {
0785     if (*tp == NULL)
0786         return;
0787     VERBOSE_TOROUT_STRING(m);
0788     kthread_stop(*tp);
0789     *tp = NULL;
0790 }
0791 EXPORT_SYMBOL_GPL(_torture_stop_kthread);