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