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