Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Module-based torture test facility for locking |
| 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> |
Davidlohr Bueso | e34191f | 2014-09-29 06:14:23 -0700 | [diff] [blame] | 30 | #include <linux/rwlock.h> |
Davidlohr Bueso | 42ddc75 | 2014-09-11 20:40:18 -0700 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 32 | #include <linux/smp.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/atomic.h> |
| 36 | #include <linux/bitops.h> |
| 37 | #include <linux/completion.h> |
| 38 | #include <linux/moduleparam.h> |
| 39 | #include <linux/percpu.h> |
| 40 | #include <linux/notifier.h> |
| 41 | #include <linux/reboot.h> |
| 42 | #include <linux/freezer.h> |
| 43 | #include <linux/cpu.h> |
| 44 | #include <linux/delay.h> |
| 45 | #include <linux/stat.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/trace_clock.h> |
| 48 | #include <asm/byteorder.h> |
| 49 | #include <linux/torture.h> |
| 50 | |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>"); |
| 53 | |
| 54 | torture_param(int, nwriters_stress, -1, |
| 55 | "Number of write-locking stress-test threads"); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 56 | torture_param(int, nreaders_stress, -1, |
| 57 | "Number of read-locking stress-test threads"); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 58 | torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)"); |
| 59 | torture_param(int, onoff_interval, 0, |
| 60 | "Time between CPU hotplugs (s), 0=disable"); |
| 61 | torture_param(int, shuffle_interval, 3, |
| 62 | "Number of jiffies between shuffles, 0=disable"); |
| 63 | torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable."); |
| 64 | torture_param(int, stat_interval, 60, |
| 65 | "Number of seconds between stats printk()s"); |
| 66 | torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable"); |
| 67 | torture_param(bool, verbose, true, |
| 68 | "Enable verbose debugging printk()s"); |
| 69 | |
| 70 | static char *torture_type = "spin_lock"; |
| 71 | module_param(torture_type, charp, 0444); |
| 72 | MODULE_PARM_DESC(torture_type, |
Davidlohr Bueso | 42ddc75 | 2014-09-11 20:40:18 -0700 | [diff] [blame] | 73 | "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)"); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 74 | |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 75 | static struct task_struct *stats_task; |
| 76 | static struct task_struct **writer_tasks; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 77 | static struct task_struct **reader_tasks; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 78 | |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 79 | static bool lock_is_write_held; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 80 | static bool lock_is_read_held; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 81 | |
Davidlohr Bueso | 1e6757a | 2014-09-11 20:40:20 -0700 | [diff] [blame] | 82 | struct lock_stress_stats { |
| 83 | long n_lock_fail; |
| 84 | long n_lock_acquired; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 85 | }; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 86 | |
Paul E. McKenney | d065eac | 2014-04-04 17:17:35 -0700 | [diff] [blame] | 87 | #if defined(MODULE) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 88 | #define LOCKTORTURE_RUNNABLE_INIT 1 |
| 89 | #else |
| 90 | #define LOCKTORTURE_RUNNABLE_INIT 0 |
| 91 | #endif |
Davidlohr Bueso | 23a8e5c | 2014-09-11 20:40:16 -0700 | [diff] [blame] | 92 | int torture_runnable = LOCKTORTURE_RUNNABLE_INIT; |
| 93 | module_param(torture_runnable, int, 0444); |
| 94 | MODULE_PARM_DESC(torture_runnable, "Start locktorture at module init"); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 95 | |
| 96 | /* Forward reference. */ |
| 97 | static void lock_torture_cleanup(void); |
| 98 | |
| 99 | /* |
| 100 | * Operations vector for selecting different types of tests. |
| 101 | */ |
| 102 | struct lock_torture_ops { |
| 103 | void (*init)(void); |
| 104 | int (*writelock)(void); |
| 105 | void (*write_delay)(struct torture_random_state *trsp); |
| 106 | void (*writeunlock)(void); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 107 | int (*readlock)(void); |
| 108 | void (*read_delay)(struct torture_random_state *trsp); |
| 109 | void (*readunlock)(void); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 110 | unsigned long flags; |
| 111 | const char *name; |
| 112 | }; |
| 113 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 114 | struct lock_torture_cxt { |
| 115 | int nrealwriters_stress; |
| 116 | int nrealreaders_stress; |
| 117 | bool debug_lock; |
| 118 | atomic_t n_lock_torture_errors; |
| 119 | struct lock_torture_ops *cur_ops; |
| 120 | struct lock_stress_stats *lwsa; /* writer statistics */ |
| 121 | struct lock_stress_stats *lrsa; /* reader statistics */ |
| 122 | }; |
| 123 | static struct lock_torture_cxt cxt = { 0, 0, false, |
| 124 | ATOMIC_INIT(0), |
| 125 | NULL, NULL}; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 126 | /* |
| 127 | * Definitions for lock torture testing. |
| 128 | */ |
| 129 | |
Paul E. McKenney | e086481 | 2014-02-11 08:05:07 -0800 | [diff] [blame] | 130 | static int torture_lock_busted_write_lock(void) |
| 131 | { |
| 132 | return 0; /* BUGGY, do not use in real life!!! */ |
| 133 | } |
| 134 | |
| 135 | static void torture_lock_busted_write_delay(struct torture_random_state *trsp) |
| 136 | { |
| 137 | const unsigned long longdelay_us = 100; |
| 138 | |
| 139 | /* We want a long delay occasionally to force massive contention. */ |
| 140 | if (!(torture_random(trsp) % |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 141 | (cxt.nrealwriters_stress * 2000 * longdelay_us))) |
Paul E. McKenney | e086481 | 2014-02-11 08:05:07 -0800 | [diff] [blame] | 142 | mdelay(longdelay_us); |
| 143 | #ifdef CONFIG_PREEMPT |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 144 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
Paul E. McKenney | e086481 | 2014-02-11 08:05:07 -0800 | [diff] [blame] | 145 | preempt_schedule(); /* Allow test to be preempted. */ |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | static void torture_lock_busted_write_unlock(void) |
| 150 | { |
| 151 | /* BUGGY, do not use in real life!!! */ |
| 152 | } |
| 153 | |
| 154 | static struct lock_torture_ops lock_busted_ops = { |
| 155 | .writelock = torture_lock_busted_write_lock, |
| 156 | .write_delay = torture_lock_busted_write_delay, |
| 157 | .writeunlock = torture_lock_busted_write_unlock, |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 158 | .readlock = NULL, |
| 159 | .read_delay = NULL, |
| 160 | .readunlock = NULL, |
Paul E. McKenney | e086481 | 2014-02-11 08:05:07 -0800 | [diff] [blame] | 161 | .name = "lock_busted" |
| 162 | }; |
| 163 | |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 164 | static DEFINE_SPINLOCK(torture_spinlock); |
| 165 | |
| 166 | static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock) |
| 167 | { |
| 168 | spin_lock(&torture_spinlock); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void torture_spin_lock_write_delay(struct torture_random_state *trsp) |
| 173 | { |
| 174 | const unsigned long shortdelay_us = 2; |
| 175 | const unsigned long longdelay_us = 100; |
| 176 | |
| 177 | /* We want a short delay mostly to emulate likely code, and |
| 178 | * we want a long delay occasionally to force massive contention. |
| 179 | */ |
| 180 | if (!(torture_random(trsp) % |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 181 | (cxt.nrealwriters_stress * 2000 * longdelay_us))) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 182 | mdelay(longdelay_us); |
| 183 | if (!(torture_random(trsp) % |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 184 | (cxt.nrealwriters_stress * 2 * shortdelay_us))) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 185 | udelay(shortdelay_us); |
| 186 | #ifdef CONFIG_PREEMPT |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 187 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 188 | preempt_schedule(); /* Allow test to be preempted. */ |
| 189 | #endif |
| 190 | } |
| 191 | |
| 192 | static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock) |
| 193 | { |
| 194 | spin_unlock(&torture_spinlock); |
| 195 | } |
| 196 | |
| 197 | static struct lock_torture_ops spin_lock_ops = { |
| 198 | .writelock = torture_spin_lock_write_lock, |
| 199 | .write_delay = torture_spin_lock_write_delay, |
| 200 | .writeunlock = torture_spin_lock_write_unlock, |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 201 | .readlock = NULL, |
| 202 | .read_delay = NULL, |
| 203 | .readunlock = NULL, |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 204 | .name = "spin_lock" |
| 205 | }; |
| 206 | |
| 207 | static int torture_spin_lock_write_lock_irq(void) |
Davidlohr Bueso | 219f800 | 2014-09-29 06:14:24 -0700 | [diff] [blame^] | 208 | __acquires(torture_spinlock) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 209 | { |
| 210 | unsigned long flags; |
| 211 | |
| 212 | spin_lock_irqsave(&torture_spinlock, flags); |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 213 | cxt.cur_ops->flags = flags; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static void torture_lock_spin_write_unlock_irq(void) |
| 218 | __releases(torture_spinlock) |
| 219 | { |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 220 | spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static struct lock_torture_ops spin_lock_irq_ops = { |
| 224 | .writelock = torture_spin_lock_write_lock_irq, |
| 225 | .write_delay = torture_spin_lock_write_delay, |
| 226 | .writeunlock = torture_lock_spin_write_unlock_irq, |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 227 | .readlock = NULL, |
| 228 | .read_delay = NULL, |
| 229 | .readunlock = NULL, |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 230 | .name = "spin_lock_irq" |
| 231 | }; |
| 232 | |
Davidlohr Bueso | e34191f | 2014-09-29 06:14:23 -0700 | [diff] [blame] | 233 | static DEFINE_RWLOCK(torture_rwlock); |
| 234 | |
| 235 | static int torture_rwlock_write_lock(void) __acquires(torture_rwlock) |
| 236 | { |
| 237 | write_lock(&torture_rwlock); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static void torture_rwlock_write_delay(struct torture_random_state *trsp) |
| 242 | { |
| 243 | const unsigned long shortdelay_us = 2; |
| 244 | const unsigned long longdelay_ms = 100; |
| 245 | |
| 246 | /* We want a short delay mostly to emulate likely code, and |
| 247 | * we want a long delay occasionally to force massive contention. |
| 248 | */ |
| 249 | if (!(torture_random(trsp) % |
| 250 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) |
| 251 | mdelay(longdelay_ms); |
| 252 | else |
| 253 | udelay(shortdelay_us); |
| 254 | } |
| 255 | |
| 256 | static void torture_rwlock_write_unlock(void) __releases(torture_rwlock) |
| 257 | { |
| 258 | write_unlock(&torture_rwlock); |
| 259 | } |
| 260 | |
| 261 | static int torture_rwlock_read_lock(void) __acquires(torture_rwlock) |
| 262 | { |
| 263 | read_lock(&torture_rwlock); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static void torture_rwlock_read_delay(struct torture_random_state *trsp) |
| 268 | { |
| 269 | const unsigned long shortdelay_us = 10; |
| 270 | const unsigned long longdelay_ms = 100; |
| 271 | |
| 272 | /* We want a short delay mostly to emulate likely code, and |
| 273 | * we want a long delay occasionally to force massive contention. |
| 274 | */ |
| 275 | if (!(torture_random(trsp) % |
| 276 | (cxt.nrealreaders_stress * 2000 * longdelay_ms))) |
| 277 | mdelay(longdelay_ms); |
| 278 | else |
| 279 | udelay(shortdelay_us); |
| 280 | } |
| 281 | |
| 282 | static void torture_rwlock_read_unlock(void) __releases(torture_rwlock) |
| 283 | { |
| 284 | read_unlock(&torture_rwlock); |
| 285 | } |
| 286 | |
| 287 | static struct lock_torture_ops rw_lock_ops = { |
| 288 | .writelock = torture_rwlock_write_lock, |
| 289 | .write_delay = torture_rwlock_write_delay, |
| 290 | .writeunlock = torture_rwlock_write_unlock, |
| 291 | .readlock = torture_rwlock_read_lock, |
| 292 | .read_delay = torture_rwlock_read_delay, |
| 293 | .readunlock = torture_rwlock_read_unlock, |
| 294 | .name = "rw_lock" |
| 295 | }; |
| 296 | |
| 297 | static int torture_rwlock_write_lock_irq(void) __acquires(torture_rwlock) |
| 298 | { |
| 299 | unsigned long flags; |
| 300 | |
| 301 | write_lock_irqsave(&torture_rwlock, flags); |
| 302 | cxt.cur_ops->flags = flags; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void torture_rwlock_write_unlock_irq(void) |
| 307 | __releases(torture_rwlock) |
| 308 | { |
| 309 | write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags); |
| 310 | } |
| 311 | |
| 312 | static int torture_rwlock_read_lock_irq(void) __acquires(torture_rwlock) |
| 313 | { |
| 314 | unsigned long flags; |
| 315 | |
| 316 | read_lock_irqsave(&torture_rwlock, flags); |
| 317 | cxt.cur_ops->flags = flags; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static void torture_rwlock_read_unlock_irq(void) |
| 322 | __releases(torture_rwlock) |
| 323 | { |
| 324 | write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags); |
| 325 | } |
| 326 | |
| 327 | static struct lock_torture_ops rw_lock_irq_ops = { |
| 328 | .writelock = torture_rwlock_write_lock_irq, |
| 329 | .write_delay = torture_rwlock_write_delay, |
| 330 | .writeunlock = torture_rwlock_write_unlock_irq, |
| 331 | .readlock = torture_rwlock_read_lock_irq, |
| 332 | .read_delay = torture_rwlock_read_delay, |
| 333 | .readunlock = torture_rwlock_read_unlock_irq, |
| 334 | .name = "rw_lock_irq" |
| 335 | }; |
| 336 | |
Davidlohr Bueso | 42ddc75 | 2014-09-11 20:40:18 -0700 | [diff] [blame] | 337 | static DEFINE_MUTEX(torture_mutex); |
| 338 | |
| 339 | static int torture_mutex_lock(void) __acquires(torture_mutex) |
| 340 | { |
| 341 | mutex_lock(&torture_mutex); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static void torture_mutex_delay(struct torture_random_state *trsp) |
| 346 | { |
| 347 | const unsigned long longdelay_ms = 100; |
| 348 | |
| 349 | /* We want a long delay occasionally to force massive contention. */ |
| 350 | if (!(torture_random(trsp) % |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 351 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) |
Davidlohr Bueso | 42ddc75 | 2014-09-11 20:40:18 -0700 | [diff] [blame] | 352 | mdelay(longdelay_ms * 5); |
| 353 | else |
| 354 | mdelay(longdelay_ms / 5); |
| 355 | #ifdef CONFIG_PREEMPT |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 356 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
Davidlohr Bueso | 42ddc75 | 2014-09-11 20:40:18 -0700 | [diff] [blame] | 357 | preempt_schedule(); /* Allow test to be preempted. */ |
| 358 | #endif |
| 359 | } |
| 360 | |
| 361 | static void torture_mutex_unlock(void) __releases(torture_mutex) |
| 362 | { |
| 363 | mutex_unlock(&torture_mutex); |
| 364 | } |
| 365 | |
| 366 | static struct lock_torture_ops mutex_lock_ops = { |
| 367 | .writelock = torture_mutex_lock, |
| 368 | .write_delay = torture_mutex_delay, |
| 369 | .writeunlock = torture_mutex_unlock, |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 370 | .readlock = NULL, |
| 371 | .read_delay = NULL, |
| 372 | .readunlock = NULL, |
Davidlohr Bueso | 42ddc75 | 2014-09-11 20:40:18 -0700 | [diff] [blame] | 373 | .name = "mutex_lock" |
| 374 | }; |
| 375 | |
Davidlohr Bueso | 4a3b427 | 2014-09-11 21:41:30 -0700 | [diff] [blame] | 376 | static DECLARE_RWSEM(torture_rwsem); |
| 377 | static int torture_rwsem_down_write(void) __acquires(torture_rwsem) |
| 378 | { |
| 379 | down_write(&torture_rwsem); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static void torture_rwsem_write_delay(struct torture_random_state *trsp) |
| 384 | { |
| 385 | const unsigned long longdelay_ms = 100; |
| 386 | |
| 387 | /* We want a long delay occasionally to force massive contention. */ |
| 388 | if (!(torture_random(trsp) % |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 389 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) |
Davidlohr Bueso | 4a3b427 | 2014-09-11 21:41:30 -0700 | [diff] [blame] | 390 | mdelay(longdelay_ms * 10); |
| 391 | else |
| 392 | mdelay(longdelay_ms / 10); |
| 393 | #ifdef CONFIG_PREEMPT |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 394 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
Davidlohr Bueso | 4a3b427 | 2014-09-11 21:41:30 -0700 | [diff] [blame] | 395 | preempt_schedule(); /* Allow test to be preempted. */ |
| 396 | #endif |
| 397 | } |
| 398 | |
| 399 | static void torture_rwsem_up_write(void) __releases(torture_rwsem) |
| 400 | { |
| 401 | up_write(&torture_rwsem); |
| 402 | } |
| 403 | |
| 404 | static int torture_rwsem_down_read(void) __acquires(torture_rwsem) |
| 405 | { |
| 406 | down_read(&torture_rwsem); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static void torture_rwsem_read_delay(struct torture_random_state *trsp) |
| 411 | { |
| 412 | const unsigned long longdelay_ms = 100; |
| 413 | |
| 414 | /* We want a long delay occasionally to force massive contention. */ |
| 415 | if (!(torture_random(trsp) % |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 416 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) |
Davidlohr Bueso | 4a3b427 | 2014-09-11 21:41:30 -0700 | [diff] [blame] | 417 | mdelay(longdelay_ms * 2); |
| 418 | else |
| 419 | mdelay(longdelay_ms / 2); |
| 420 | #ifdef CONFIG_PREEMPT |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 421 | if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000))) |
Davidlohr Bueso | 4a3b427 | 2014-09-11 21:41:30 -0700 | [diff] [blame] | 422 | preempt_schedule(); /* Allow test to be preempted. */ |
| 423 | #endif |
| 424 | } |
| 425 | |
| 426 | static void torture_rwsem_up_read(void) __releases(torture_rwsem) |
| 427 | { |
| 428 | up_read(&torture_rwsem); |
| 429 | } |
| 430 | |
| 431 | static struct lock_torture_ops rwsem_lock_ops = { |
| 432 | .writelock = torture_rwsem_down_write, |
| 433 | .write_delay = torture_rwsem_write_delay, |
| 434 | .writeunlock = torture_rwsem_up_write, |
| 435 | .readlock = torture_rwsem_down_read, |
| 436 | .read_delay = torture_rwsem_read_delay, |
| 437 | .readunlock = torture_rwsem_up_read, |
| 438 | .name = "rwsem_lock" |
| 439 | }; |
| 440 | |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 441 | /* |
| 442 | * Lock torture writer kthread. Repeatedly acquires and releases |
| 443 | * the lock, checking for duplicate acquisitions. |
| 444 | */ |
| 445 | static int lock_torture_writer(void *arg) |
| 446 | { |
Davidlohr Bueso | 1e6757a | 2014-09-11 20:40:20 -0700 | [diff] [blame] | 447 | struct lock_stress_stats *lwsp = arg; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 448 | static DEFINE_TORTURE_RANDOM(rand); |
| 449 | |
| 450 | VERBOSE_TOROUT_STRING("lock_torture_writer task started"); |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 451 | set_user_nice(current, MAX_NICE); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 452 | |
| 453 | do { |
Paul E. McKenney | da601c6 | 2014-02-26 12:14:51 -0800 | [diff] [blame] | 454 | if ((torture_random(&rand) & 0xfffff) == 0) |
| 455 | schedule_timeout_uninterruptible(1); |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 456 | cxt.cur_ops->writelock(); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 457 | if (WARN_ON_ONCE(lock_is_write_held)) |
Davidlohr Bueso | 1e6757a | 2014-09-11 20:40:20 -0700 | [diff] [blame] | 458 | lwsp->n_lock_fail++; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 459 | lock_is_write_held = 1; |
Davidlohr Bueso | 1e6757a | 2014-09-11 20:40:20 -0700 | [diff] [blame] | 460 | lwsp->n_lock_acquired++; |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 461 | cxt.cur_ops->write_delay(&rand); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 462 | lock_is_write_held = 0; |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 463 | cxt.cur_ops->writeunlock(); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 464 | stutter_wait("lock_torture_writer"); |
| 465 | } while (!torture_must_stop()); |
| 466 | torture_kthread_stopping("lock_torture_writer"); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /* |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 471 | * Lock torture reader kthread. Repeatedly acquires and releases |
| 472 | * the reader lock. |
| 473 | */ |
| 474 | static int lock_torture_reader(void *arg) |
| 475 | { |
| 476 | struct lock_stress_stats *lrsp = arg; |
| 477 | static DEFINE_TORTURE_RANDOM(rand); |
| 478 | |
| 479 | VERBOSE_TOROUT_STRING("lock_torture_reader task started"); |
| 480 | set_user_nice(current, MAX_NICE); |
| 481 | |
| 482 | do { |
| 483 | if ((torture_random(&rand) & 0xfffff) == 0) |
| 484 | schedule_timeout_uninterruptible(1); |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 485 | cxt.cur_ops->readlock(); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 486 | lock_is_read_held = 1; |
| 487 | lrsp->n_lock_acquired++; |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 488 | cxt.cur_ops->read_delay(&rand); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 489 | lock_is_read_held = 0; |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 490 | cxt.cur_ops->readunlock(); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 491 | stutter_wait("lock_torture_reader"); |
| 492 | } while (!torture_must_stop()); |
| 493 | torture_kthread_stopping("lock_torture_reader"); |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | /* |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 498 | * Create an lock-torture-statistics message in the specified buffer. |
| 499 | */ |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 500 | static void __torture_print_stats(char *page, |
| 501 | struct lock_stress_stats *statp, bool write) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 502 | { |
| 503 | bool fail = 0; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 504 | int i, n_stress; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 505 | long max = 0; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 506 | long min = statp[0].n_lock_acquired; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 507 | long long sum = 0; |
| 508 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 509 | n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 510 | for (i = 0; i < n_stress; i++) { |
| 511 | if (statp[i].n_lock_fail) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 512 | fail = true; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 513 | sum += statp[i].n_lock_acquired; |
| 514 | if (max < statp[i].n_lock_fail) |
| 515 | max = statp[i].n_lock_fail; |
| 516 | if (min > statp[i].n_lock_fail) |
| 517 | min = statp[i].n_lock_fail; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 518 | } |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 519 | page += sprintf(page, |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 520 | "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n", |
| 521 | write ? "Writes" : "Reads ", |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 522 | sum, max, min, max / 2 > min ? "???" : "", |
| 523 | fail, fail ? "!!!" : ""); |
| 524 | if (fail) |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 525 | atomic_inc(&cxt.n_lock_torture_errors); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Print torture statistics. Caller must ensure that there is only one |
| 530 | * call to this function at a given time!!! This is normally accomplished |
| 531 | * by relying on the module system to only have one copy of the module |
| 532 | * loaded, and then by giving the lock_torture_stats kthread full control |
| 533 | * (or the init/cleanup functions when lock_torture_stats thread is not |
| 534 | * running). |
| 535 | */ |
| 536 | static void lock_torture_stats_print(void) |
| 537 | { |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 538 | int size = cxt.nrealwriters_stress * 200 + 8192; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 539 | char *buf; |
| 540 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 541 | if (cxt.cur_ops->readlock) |
| 542 | size += cxt.nrealreaders_stress * 200 + 8192; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 543 | |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 544 | buf = kmalloc(size, GFP_KERNEL); |
| 545 | if (!buf) { |
| 546 | pr_err("lock_torture_stats_print: Out of memory, need: %d", |
| 547 | size); |
| 548 | return; |
| 549 | } |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 550 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 551 | __torture_print_stats(buf, cxt.lwsa, true); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 552 | pr_alert("%s", buf); |
| 553 | kfree(buf); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 554 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 555 | if (cxt.cur_ops->readlock) { |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 556 | buf = kmalloc(size, GFP_KERNEL); |
| 557 | if (!buf) { |
| 558 | pr_err("lock_torture_stats_print: Out of memory, need: %d", |
| 559 | size); |
| 560 | return; |
| 561 | } |
| 562 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 563 | __torture_print_stats(buf, cxt.lrsa, false); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 564 | pr_alert("%s", buf); |
| 565 | kfree(buf); |
| 566 | } |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Periodically prints torture statistics, if periodic statistics printing |
| 571 | * was specified via the stat_interval module parameter. |
| 572 | * |
| 573 | * No need to worry about fullstop here, since this one doesn't reference |
| 574 | * volatile state or register callbacks. |
| 575 | */ |
| 576 | static int lock_torture_stats(void *arg) |
| 577 | { |
| 578 | VERBOSE_TOROUT_STRING("lock_torture_stats task started"); |
| 579 | do { |
| 580 | schedule_timeout_interruptible(stat_interval * HZ); |
| 581 | lock_torture_stats_print(); |
| 582 | torture_shutdown_absorb("lock_torture_stats"); |
| 583 | } while (!torture_must_stop()); |
| 584 | torture_kthread_stopping("lock_torture_stats"); |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static inline void |
| 589 | lock_torture_print_module_parms(struct lock_torture_ops *cur_ops, |
| 590 | const char *tag) |
| 591 | { |
| 592 | pr_alert("%s" TORTURE_FLAG |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 593 | "--- %s%s: nwriters_stress=%d nreaders_stress=%d stat_interval=%d verbose=%d shuffle_interval=%d stutter=%d shutdown_secs=%d onoff_interval=%d onoff_holdoff=%d\n", |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 594 | torture_type, tag, cxt.debug_lock ? " [debug]": "", |
| 595 | cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval, |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 596 | verbose, shuffle_interval, stutter, shutdown_secs, |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 597 | onoff_interval, onoff_holdoff); |
| 598 | } |
| 599 | |
| 600 | static void lock_torture_cleanup(void) |
| 601 | { |
| 602 | int i; |
| 603 | |
Davidlohr Bueso | d36a7a0 | 2014-09-11 20:40:21 -0700 | [diff] [blame] | 604 | if (torture_cleanup_begin()) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 605 | return; |
| 606 | |
| 607 | if (writer_tasks) { |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 608 | for (i = 0; i < cxt.nrealwriters_stress; i++) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 609 | torture_stop_kthread(lock_torture_writer, |
| 610 | writer_tasks[i]); |
| 611 | kfree(writer_tasks); |
| 612 | writer_tasks = NULL; |
| 613 | } |
| 614 | |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 615 | if (reader_tasks) { |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 616 | for (i = 0; i < cxt.nrealreaders_stress; i++) |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 617 | torture_stop_kthread(lock_torture_reader, |
| 618 | reader_tasks[i]); |
| 619 | kfree(reader_tasks); |
| 620 | reader_tasks = NULL; |
| 621 | } |
| 622 | |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 623 | torture_stop_kthread(lock_torture_stats, stats_task); |
| 624 | lock_torture_stats_print(); /* -After- the stats thread is stopped! */ |
| 625 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 626 | if (atomic_read(&cxt.n_lock_torture_errors)) |
| 627 | lock_torture_print_module_parms(cxt.cur_ops, |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 628 | "End of test: FAILURE"); |
| 629 | else if (torture_onoff_failures()) |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 630 | lock_torture_print_module_parms(cxt.cur_ops, |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 631 | "End of test: LOCK_HOTPLUG"); |
| 632 | else |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 633 | lock_torture_print_module_parms(cxt.cur_ops, |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 634 | "End of test: SUCCESS"); |
Davidlohr Bueso | d36a7a0 | 2014-09-11 20:40:21 -0700 | [diff] [blame] | 635 | torture_cleanup_end(); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static int __init lock_torture_init(void) |
| 639 | { |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 640 | int i, j; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 641 | int firsterr = 0; |
| 642 | static struct lock_torture_ops *torture_ops[] = { |
Davidlohr Bueso | e34191f | 2014-09-29 06:14:23 -0700 | [diff] [blame] | 643 | &lock_busted_ops, |
| 644 | &spin_lock_ops, &spin_lock_irq_ops, |
| 645 | &rw_lock_ops, &rw_lock_irq_ops, |
| 646 | &mutex_lock_ops, |
| 647 | &rwsem_lock_ops, |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 648 | }; |
| 649 | |
Davidlohr Bueso | 23a8e5c | 2014-09-11 20:40:16 -0700 | [diff] [blame] | 650 | if (!torture_init_begin(torture_type, verbose, &torture_runnable)) |
Paul E. McKenney | 5228084 | 2014-04-07 09:14:11 -0700 | [diff] [blame] | 651 | return -EBUSY; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 652 | |
| 653 | /* Process args and tell the world that the torturer is on the job. */ |
| 654 | for (i = 0; i < ARRAY_SIZE(torture_ops); i++) { |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 655 | cxt.cur_ops = torture_ops[i]; |
| 656 | if (strcmp(torture_type, cxt.cur_ops->name) == 0) |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | if (i == ARRAY_SIZE(torture_ops)) { |
| 660 | pr_alert("lock-torture: invalid torture type: \"%s\"\n", |
| 661 | torture_type); |
| 662 | pr_alert("lock-torture types:"); |
| 663 | for (i = 0; i < ARRAY_SIZE(torture_ops); i++) |
| 664 | pr_alert(" %s", torture_ops[i]->name); |
| 665 | pr_alert("\n"); |
| 666 | torture_init_end(); |
| 667 | return -EINVAL; |
| 668 | } |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 669 | if (cxt.cur_ops->init) |
| 670 | cxt.cur_ops->init(); /* no "goto unwind" prior to this point!!! */ |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 671 | |
| 672 | if (nwriters_stress >= 0) |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 673 | cxt.nrealwriters_stress = nwriters_stress; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 674 | else |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 675 | cxt.nrealwriters_stress = 2 * num_online_cpus(); |
Davidlohr Bueso | f095bfc | 2014-09-11 20:40:19 -0700 | [diff] [blame] | 676 | |
| 677 | #ifdef CONFIG_DEBUG_MUTEXES |
| 678 | if (strncmp(torture_type, "mutex", 5) == 0) |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 679 | cxt.debug_lock = true; |
Davidlohr Bueso | f095bfc | 2014-09-11 20:40:19 -0700 | [diff] [blame] | 680 | #endif |
| 681 | #ifdef CONFIG_DEBUG_SPINLOCK |
Davidlohr Bueso | e34191f | 2014-09-29 06:14:23 -0700 | [diff] [blame] | 682 | if ((strncmp(torture_type, "spin", 4) == 0) || |
| 683 | (strncmp(torture_type, "rw_lock", 7) == 0)) |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 684 | cxt.debug_lock = true; |
Davidlohr Bueso | f095bfc | 2014-09-11 20:40:19 -0700 | [diff] [blame] | 685 | #endif |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 686 | |
| 687 | /* Initialize the statistics so that each run gets its own numbers. */ |
| 688 | |
| 689 | lock_is_write_held = 0; |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 690 | cxt.lwsa = kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KERNEL); |
| 691 | if (cxt.lwsa == NULL) { |
| 692 | VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory"); |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 693 | firsterr = -ENOMEM; |
| 694 | goto unwind; |
| 695 | } |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 696 | for (i = 0; i < cxt.nrealwriters_stress; i++) { |
| 697 | cxt.lwsa[i].n_lock_fail = 0; |
| 698 | cxt.lwsa[i].n_lock_acquired = 0; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 701 | if (cxt.cur_ops->readlock) { |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 702 | if (nreaders_stress >= 0) |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 703 | cxt.nrealreaders_stress = nreaders_stress; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 704 | else { |
| 705 | /* |
| 706 | * By default distribute evenly the number of |
| 707 | * readers and writers. We still run the same number |
| 708 | * of threads as the writer-only locks default. |
| 709 | */ |
| 710 | if (nwriters_stress < 0) /* user doesn't care */ |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 711 | cxt.nrealwriters_stress = num_online_cpus(); |
| 712 | cxt.nrealreaders_stress = cxt.nrealwriters_stress; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 713 | } |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 714 | |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 715 | lock_is_read_held = 0; |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 716 | cxt.lrsa = kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_KERNEL); |
| 717 | if (cxt.lrsa == NULL) { |
| 718 | VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory"); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 719 | firsterr = -ENOMEM; |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 720 | kfree(cxt.lwsa); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 721 | goto unwind; |
| 722 | } |
| 723 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 724 | for (i = 0; i < cxt.nrealreaders_stress; i++) { |
| 725 | cxt.lrsa[i].n_lock_fail = 0; |
| 726 | cxt.lrsa[i].n_lock_acquired = 0; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 729 | lock_torture_print_module_parms(cxt.cur_ops, "Start of test"); |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 730 | |
| 731 | /* Prepare torture context. */ |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 732 | if (onoff_interval > 0) { |
| 733 | firsterr = torture_onoff_init(onoff_holdoff * HZ, |
| 734 | onoff_interval * HZ); |
| 735 | if (firsterr) |
| 736 | goto unwind; |
| 737 | } |
| 738 | if (shuffle_interval > 0) { |
| 739 | firsterr = torture_shuffle_init(shuffle_interval); |
| 740 | if (firsterr) |
| 741 | goto unwind; |
| 742 | } |
| 743 | if (shutdown_secs > 0) { |
| 744 | firsterr = torture_shutdown_init(shutdown_secs, |
| 745 | lock_torture_cleanup); |
| 746 | if (firsterr) |
| 747 | goto unwind; |
| 748 | } |
| 749 | if (stutter > 0) { |
| 750 | firsterr = torture_stutter_init(stutter); |
| 751 | if (firsterr) |
| 752 | goto unwind; |
| 753 | } |
| 754 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 755 | writer_tasks = kzalloc(cxt.nrealwriters_stress * sizeof(writer_tasks[0]), |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 756 | GFP_KERNEL); |
| 757 | if (writer_tasks == NULL) { |
| 758 | VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory"); |
| 759 | firsterr = -ENOMEM; |
| 760 | goto unwind; |
| 761 | } |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 762 | |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 763 | if (cxt.cur_ops->readlock) { |
| 764 | reader_tasks = kzalloc(cxt.nrealreaders_stress * sizeof(reader_tasks[0]), |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 765 | GFP_KERNEL); |
| 766 | if (reader_tasks == NULL) { |
| 767 | VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory"); |
| 768 | firsterr = -ENOMEM; |
| 769 | goto unwind; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Create the kthreads and start torturing (oh, those poor little locks). |
| 775 | * |
| 776 | * TODO: Note that we interleave writers with readers, giving writers a |
| 777 | * slight advantage, by creating its kthread first. This can be modified |
| 778 | * for very specific needs, or even let the user choose the policy, if |
| 779 | * ever wanted. |
| 780 | */ |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 781 | for (i = 0, j = 0; i < cxt.nrealwriters_stress || |
| 782 | j < cxt.nrealreaders_stress; i++, j++) { |
| 783 | if (i >= cxt.nrealwriters_stress) |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 784 | goto create_reader; |
| 785 | |
| 786 | /* Create writer. */ |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 787 | firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i], |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 788 | writer_tasks[i]); |
| 789 | if (firsterr) |
| 790 | goto unwind; |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 791 | |
| 792 | create_reader: |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 793 | if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress)) |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 794 | continue; |
| 795 | /* Create reader. */ |
Davidlohr Bueso | 630952c | 2014-09-11 21:42:25 -0700 | [diff] [blame] | 796 | firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j], |
Davidlohr Bueso | 4f6332c | 2014-09-11 21:40:41 -0700 | [diff] [blame] | 797 | reader_tasks[j]); |
| 798 | if (firsterr) |
| 799 | goto unwind; |
Paul E. McKenney | 0af3fe1 | 2014-02-04 15:51:41 -0800 | [diff] [blame] | 800 | } |
| 801 | if (stat_interval > 0) { |
| 802 | firsterr = torture_create_kthread(lock_torture_stats, NULL, |
| 803 | stats_task); |
| 804 | if (firsterr) |
| 805 | goto unwind; |
| 806 | } |
| 807 | torture_init_end(); |
| 808 | return 0; |
| 809 | |
| 810 | unwind: |
| 811 | torture_init_end(); |
| 812 | lock_torture_cleanup(); |
| 813 | return firsterr; |
| 814 | } |
| 815 | |
| 816 | module_init(lock_torture_init); |
| 817 | module_exit(lock_torture_cleanup); |