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