Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2 | /* |
| 3 | * lib/locking-selftest.c |
| 4 | * |
| 5 | * Testsuite for various locking APIs: spinlocks, rwlocks, |
| 6 | * mutexes and rw-semaphores. |
| 7 | * |
| 8 | * It is checking both false positives and false negatives. |
| 9 | * |
| 10 | * Started by Ingo Molnar: |
| 11 | * |
| 12 | * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 13 | */ |
| 14 | #include <linux/rwsem.h> |
| 15 | #include <linux/mutex.h> |
Maarten Lankhorst | 1b375dc | 2013-07-05 09:29:32 +0200 | [diff] [blame] | 16 | #include <linux/ww_mutex.h> |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 17 | #include <linux/sched.h> |
| 18 | #include <linux/delay.h> |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 19 | #include <linux/lockdep.h> |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/kallsyms.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/debug_locks.h> |
| 24 | #include <linux/irqflags.h> |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 25 | #include <linux/rtmutex.h> |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * Change this to 1 if you want to see the failure printouts: |
| 29 | */ |
| 30 | static unsigned int debug_locks_verbose; |
Boqun Feng | e918188 | 2020-08-07 15:42:20 +0800 | [diff] [blame] | 31 | unsigned int force_read_lock_recursive; |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 32 | |
Thomas Hellstrom | 08295b3 | 2018-06-15 10:17:38 +0200 | [diff] [blame] | 33 | static DEFINE_WD_CLASS(ww_lockdep); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 34 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 35 | static int __init setup_debug_locks_verbose(char *str) |
| 36 | { |
| 37 | get_option(&str, &debug_locks_verbose); |
| 38 | |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | __setup("debug_locks_verbose=", setup_debug_locks_verbose); |
| 43 | |
| 44 | #define FAILURE 0 |
| 45 | #define SUCCESS 1 |
| 46 | |
| 47 | #define LOCKTYPE_SPIN 0x1 |
| 48 | #define LOCKTYPE_RWLOCK 0x2 |
| 49 | #define LOCKTYPE_MUTEX 0x4 |
| 50 | #define LOCKTYPE_RWSEM 0x8 |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 51 | #define LOCKTYPE_WW 0x10 |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 52 | #define LOCKTYPE_RTMUTEX 0x20 |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 53 | |
| 54 | static struct ww_acquire_ctx t, t2; |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 55 | static struct ww_mutex o, o2, o3; |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Normal standalone locks, for the circular and irq-context |
| 59 | * dependency tests: |
| 60 | */ |
Yong Zhang | 9fb1b90 | 2012-04-16 15:01:55 +0800 | [diff] [blame] | 61 | static DEFINE_RAW_SPINLOCK(lock_A); |
| 62 | static DEFINE_RAW_SPINLOCK(lock_B); |
| 63 | static DEFINE_RAW_SPINLOCK(lock_C); |
| 64 | static DEFINE_RAW_SPINLOCK(lock_D); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 65 | |
| 66 | static DEFINE_RWLOCK(rwlock_A); |
| 67 | static DEFINE_RWLOCK(rwlock_B); |
| 68 | static DEFINE_RWLOCK(rwlock_C); |
| 69 | static DEFINE_RWLOCK(rwlock_D); |
| 70 | |
| 71 | static DEFINE_MUTEX(mutex_A); |
| 72 | static DEFINE_MUTEX(mutex_B); |
| 73 | static DEFINE_MUTEX(mutex_C); |
| 74 | static DEFINE_MUTEX(mutex_D); |
| 75 | |
| 76 | static DECLARE_RWSEM(rwsem_A); |
| 77 | static DECLARE_RWSEM(rwsem_B); |
| 78 | static DECLARE_RWSEM(rwsem_C); |
| 79 | static DECLARE_RWSEM(rwsem_D); |
| 80 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 81 | #ifdef CONFIG_RT_MUTEXES |
| 82 | |
| 83 | static DEFINE_RT_MUTEX(rtmutex_A); |
| 84 | static DEFINE_RT_MUTEX(rtmutex_B); |
| 85 | static DEFINE_RT_MUTEX(rtmutex_C); |
| 86 | static DEFINE_RT_MUTEX(rtmutex_D); |
| 87 | |
| 88 | #endif |
| 89 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 90 | /* |
| 91 | * Locks that we initialize dynamically as well so that |
| 92 | * e.g. X1 and X2 becomes two instances of the same class, |
| 93 | * but X* and Y* are different classes. We do this so that |
| 94 | * we do not trigger a real lockup: |
| 95 | */ |
Yong Zhang | 9fb1b90 | 2012-04-16 15:01:55 +0800 | [diff] [blame] | 96 | static DEFINE_RAW_SPINLOCK(lock_X1); |
| 97 | static DEFINE_RAW_SPINLOCK(lock_X2); |
| 98 | static DEFINE_RAW_SPINLOCK(lock_Y1); |
| 99 | static DEFINE_RAW_SPINLOCK(lock_Y2); |
| 100 | static DEFINE_RAW_SPINLOCK(lock_Z1); |
| 101 | static DEFINE_RAW_SPINLOCK(lock_Z2); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 102 | |
| 103 | static DEFINE_RWLOCK(rwlock_X1); |
| 104 | static DEFINE_RWLOCK(rwlock_X2); |
| 105 | static DEFINE_RWLOCK(rwlock_Y1); |
| 106 | static DEFINE_RWLOCK(rwlock_Y2); |
| 107 | static DEFINE_RWLOCK(rwlock_Z1); |
| 108 | static DEFINE_RWLOCK(rwlock_Z2); |
| 109 | |
| 110 | static DEFINE_MUTEX(mutex_X1); |
| 111 | static DEFINE_MUTEX(mutex_X2); |
| 112 | static DEFINE_MUTEX(mutex_Y1); |
| 113 | static DEFINE_MUTEX(mutex_Y2); |
| 114 | static DEFINE_MUTEX(mutex_Z1); |
| 115 | static DEFINE_MUTEX(mutex_Z2); |
| 116 | |
| 117 | static DECLARE_RWSEM(rwsem_X1); |
| 118 | static DECLARE_RWSEM(rwsem_X2); |
| 119 | static DECLARE_RWSEM(rwsem_Y1); |
| 120 | static DECLARE_RWSEM(rwsem_Y2); |
| 121 | static DECLARE_RWSEM(rwsem_Z1); |
| 122 | static DECLARE_RWSEM(rwsem_Z2); |
| 123 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 124 | #ifdef CONFIG_RT_MUTEXES |
| 125 | |
| 126 | static DEFINE_RT_MUTEX(rtmutex_X1); |
| 127 | static DEFINE_RT_MUTEX(rtmutex_X2); |
| 128 | static DEFINE_RT_MUTEX(rtmutex_Y1); |
| 129 | static DEFINE_RT_MUTEX(rtmutex_Y2); |
| 130 | static DEFINE_RT_MUTEX(rtmutex_Z1); |
| 131 | static DEFINE_RT_MUTEX(rtmutex_Z2); |
| 132 | |
| 133 | #endif |
| 134 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 135 | /* |
| 136 | * non-inlined runtime initializers, to let separate locks share |
| 137 | * the same lock-class: |
| 138 | */ |
| 139 | #define INIT_CLASS_FUNC(class) \ |
| 140 | static noinline void \ |
Yong Zhang | 9fb1b90 | 2012-04-16 15:01:55 +0800 | [diff] [blame] | 141 | init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \ |
| 142 | struct mutex *mutex, struct rw_semaphore *rwsem)\ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 143 | { \ |
Yong Zhang | 9fb1b90 | 2012-04-16 15:01:55 +0800 | [diff] [blame] | 144 | raw_spin_lock_init(lock); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 145 | rwlock_init(rwlock); \ |
| 146 | mutex_init(mutex); \ |
| 147 | init_rwsem(rwsem); \ |
| 148 | } |
| 149 | |
| 150 | INIT_CLASS_FUNC(X) |
| 151 | INIT_CLASS_FUNC(Y) |
| 152 | INIT_CLASS_FUNC(Z) |
| 153 | |
| 154 | static void init_shared_classes(void) |
| 155 | { |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 156 | #ifdef CONFIG_RT_MUTEXES |
| 157 | static struct lock_class_key rt_X, rt_Y, rt_Z; |
| 158 | |
| 159 | __rt_mutex_init(&rtmutex_X1, __func__, &rt_X); |
| 160 | __rt_mutex_init(&rtmutex_X2, __func__, &rt_X); |
| 161 | __rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y); |
| 162 | __rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y); |
| 163 | __rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z); |
| 164 | __rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z); |
| 165 | #endif |
| 166 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 167 | init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1); |
| 168 | init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2); |
| 169 | |
| 170 | init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1); |
| 171 | init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2); |
| 172 | |
| 173 | init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1); |
| 174 | init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2); |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests. |
| 179 | * The following functions use a lock from a simulated hardirq/softirq |
| 180 | * context, causing the locks to be marked as hardirq-safe/softirq-safe: |
| 181 | */ |
| 182 | |
| 183 | #define HARDIRQ_DISABLE local_irq_disable |
| 184 | #define HARDIRQ_ENABLE local_irq_enable |
| 185 | |
| 186 | #define HARDIRQ_ENTER() \ |
| 187 | local_irq_disable(); \ |
Frederic Weisbecker | ba9f207 | 2011-05-20 02:09:54 +0200 | [diff] [blame] | 188 | __irq_enter(); \ |
Peter Zijlstra | f18f7a2 | 2021-06-17 20:57:19 +0200 | [diff] [blame] | 189 | lockdep_hardirq_threaded(); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 190 | WARN_ON(!in_irq()); |
| 191 | |
| 192 | #define HARDIRQ_EXIT() \ |
| 193 | __irq_exit(); \ |
| 194 | local_irq_enable(); |
| 195 | |
| 196 | #define SOFTIRQ_DISABLE local_bh_disable |
| 197 | #define SOFTIRQ_ENABLE local_bh_enable |
| 198 | |
| 199 | #define SOFTIRQ_ENTER() \ |
| 200 | local_bh_disable(); \ |
| 201 | local_irq_disable(); \ |
Ingo Molnar | d820ac4 | 2009-03-13 01:30:40 +0100 | [diff] [blame] | 202 | lockdep_softirq_enter(); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 203 | WARN_ON(!in_softirq()); |
| 204 | |
| 205 | #define SOFTIRQ_EXIT() \ |
Ingo Molnar | d820ac4 | 2009-03-13 01:30:40 +0100 | [diff] [blame] | 206 | lockdep_softirq_exit(); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 207 | local_irq_enable(); \ |
| 208 | local_bh_enable(); |
| 209 | |
| 210 | /* |
| 211 | * Shortcuts for lock/unlock API variants, to keep |
| 212 | * the testcases compact: |
| 213 | */ |
Yong Zhang | 9fb1b90 | 2012-04-16 15:01:55 +0800 | [diff] [blame] | 214 | #define L(x) raw_spin_lock(&lock_##x) |
| 215 | #define U(x) raw_spin_unlock(&lock_##x) |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 216 | #define LU(x) L(x); U(x) |
Yong Zhang | 9fb1b90 | 2012-04-16 15:01:55 +0800 | [diff] [blame] | 217 | #define SI(x) raw_spin_lock_init(&lock_##x) |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 218 | |
| 219 | #define WL(x) write_lock(&rwlock_##x) |
| 220 | #define WU(x) write_unlock(&rwlock_##x) |
| 221 | #define WLU(x) WL(x); WU(x) |
| 222 | |
| 223 | #define RL(x) read_lock(&rwlock_##x) |
| 224 | #define RU(x) read_unlock(&rwlock_##x) |
| 225 | #define RLU(x) RL(x); RU(x) |
| 226 | #define RWI(x) rwlock_init(&rwlock_##x) |
| 227 | |
| 228 | #define ML(x) mutex_lock(&mutex_##x) |
| 229 | #define MU(x) mutex_unlock(&mutex_##x) |
| 230 | #define MI(x) mutex_init(&mutex_##x) |
| 231 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 232 | #define RTL(x) rt_mutex_lock(&rtmutex_##x) |
| 233 | #define RTU(x) rt_mutex_unlock(&rtmutex_##x) |
| 234 | #define RTI(x) rt_mutex_init(&rtmutex_##x) |
| 235 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 236 | #define WSL(x) down_write(&rwsem_##x) |
| 237 | #define WSU(x) up_write(&rwsem_##x) |
| 238 | |
| 239 | #define RSL(x) down_read(&rwsem_##x) |
| 240 | #define RSU(x) up_read(&rwsem_##x) |
| 241 | #define RWSI(x) init_rwsem(&rwsem_##x) |
| 242 | |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 243 | #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH |
| 244 | #define WWAI(x) ww_acquire_init(x, &ww_lockdep) |
| 245 | #else |
| 246 | #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0) |
| 247 | #endif |
| 248 | #define WWAD(x) ww_acquire_done(x) |
| 249 | #define WWAF(x) ww_acquire_fini(x) |
| 250 | |
| 251 | #define WWL(x, c) ww_mutex_lock(x, c) |
| 252 | #define WWT(x) ww_mutex_trylock(x) |
| 253 | #define WWL1(x) ww_mutex_lock(x, NULL) |
| 254 | #define WWU(x) ww_mutex_unlock(x) |
| 255 | |
| 256 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 257 | #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x) |
| 258 | |
| 259 | /* |
| 260 | * Generate different permutations of the same testcase, using |
| 261 | * the same basic lock-dependency/state events: |
| 262 | */ |
| 263 | |
| 264 | #define GENERATE_TESTCASE(name) \ |
| 265 | \ |
| 266 | static void name(void) { E(); } |
| 267 | |
| 268 | #define GENERATE_PERMUTATIONS_2_EVENTS(name) \ |
| 269 | \ |
| 270 | static void name##_12(void) { E1(); E2(); } \ |
| 271 | static void name##_21(void) { E2(); E1(); } |
| 272 | |
| 273 | #define GENERATE_PERMUTATIONS_3_EVENTS(name) \ |
| 274 | \ |
| 275 | static void name##_123(void) { E1(); E2(); E3(); } \ |
| 276 | static void name##_132(void) { E1(); E3(); E2(); } \ |
| 277 | static void name##_213(void) { E2(); E1(); E3(); } \ |
| 278 | static void name##_231(void) { E2(); E3(); E1(); } \ |
| 279 | static void name##_312(void) { E3(); E1(); E2(); } \ |
| 280 | static void name##_321(void) { E3(); E2(); E1(); } |
| 281 | |
| 282 | /* |
| 283 | * AA deadlock: |
| 284 | */ |
| 285 | |
| 286 | #define E() \ |
| 287 | \ |
| 288 | LOCK(X1); \ |
| 289 | LOCK(X2); /* this one should fail */ |
| 290 | |
| 291 | /* |
| 292 | * 6 testcases: |
| 293 | */ |
| 294 | #include "locking-selftest-spin.h" |
| 295 | GENERATE_TESTCASE(AA_spin) |
| 296 | #include "locking-selftest-wlock.h" |
| 297 | GENERATE_TESTCASE(AA_wlock) |
| 298 | #include "locking-selftest-rlock.h" |
| 299 | GENERATE_TESTCASE(AA_rlock) |
| 300 | #include "locking-selftest-mutex.h" |
| 301 | GENERATE_TESTCASE(AA_mutex) |
| 302 | #include "locking-selftest-wsem.h" |
| 303 | GENERATE_TESTCASE(AA_wsem) |
| 304 | #include "locking-selftest-rsem.h" |
| 305 | GENERATE_TESTCASE(AA_rsem) |
| 306 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 307 | #ifdef CONFIG_RT_MUTEXES |
| 308 | #include "locking-selftest-rtmutex.h" |
| 309 | GENERATE_TESTCASE(AA_rtmutex); |
| 310 | #endif |
| 311 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 312 | #undef E |
| 313 | |
| 314 | /* |
| 315 | * Special-case for read-locking, they are |
Ingo Molnar | 6c9076e | 2006-07-03 00:24:51 -0700 | [diff] [blame] | 316 | * allowed to recurse on the same lock class: |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 317 | */ |
| 318 | static void rlock_AA1(void) |
| 319 | { |
| 320 | RL(X1); |
| 321 | RL(X1); // this one should NOT fail |
| 322 | } |
| 323 | |
| 324 | static void rlock_AA1B(void) |
| 325 | { |
| 326 | RL(X1); |
Ingo Molnar | 6c9076e | 2006-07-03 00:24:51 -0700 | [diff] [blame] | 327 | RL(X2); // this one should NOT fail |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void rsem_AA1(void) |
| 331 | { |
| 332 | RSL(X1); |
| 333 | RSL(X1); // this one should fail |
| 334 | } |
| 335 | |
| 336 | static void rsem_AA1B(void) |
| 337 | { |
| 338 | RSL(X1); |
| 339 | RSL(X2); // this one should fail |
| 340 | } |
| 341 | /* |
| 342 | * The mixing of read and write locks is not allowed: |
| 343 | */ |
| 344 | static void rlock_AA2(void) |
| 345 | { |
| 346 | RL(X1); |
| 347 | WL(X2); // this one should fail |
| 348 | } |
| 349 | |
| 350 | static void rsem_AA2(void) |
| 351 | { |
| 352 | RSL(X1); |
| 353 | WSL(X2); // this one should fail |
| 354 | } |
| 355 | |
| 356 | static void rlock_AA3(void) |
| 357 | { |
| 358 | WL(X1); |
| 359 | RL(X2); // this one should fail |
| 360 | } |
| 361 | |
| 362 | static void rsem_AA3(void) |
| 363 | { |
| 364 | WSL(X1); |
| 365 | RSL(X2); // this one should fail |
| 366 | } |
| 367 | |
| 368 | /* |
Peter Zijlstra | e914985 | 2017-08-23 13:13:11 +0200 | [diff] [blame] | 369 | * read_lock(A) |
| 370 | * spin_lock(B) |
| 371 | * spin_lock(B) |
| 372 | * write_lock(A) |
| 373 | */ |
| 374 | static void rlock_ABBA1(void) |
| 375 | { |
| 376 | RL(X1); |
| 377 | L(Y1); |
| 378 | U(Y1); |
| 379 | RU(X1); |
| 380 | |
| 381 | L(Y1); |
| 382 | WL(X1); |
| 383 | WU(X1); |
| 384 | U(Y1); // should fail |
| 385 | } |
| 386 | |
| 387 | static void rwsem_ABBA1(void) |
| 388 | { |
| 389 | RSL(X1); |
| 390 | ML(Y1); |
| 391 | MU(Y1); |
| 392 | RSU(X1); |
| 393 | |
| 394 | ML(Y1); |
| 395 | WSL(X1); |
| 396 | WSU(X1); |
| 397 | MU(Y1); // should fail |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * read_lock(A) |
| 402 | * spin_lock(B) |
| 403 | * spin_lock(B) |
Boqun Feng | d4f200e5 | 2020-08-07 15:42:32 +0800 | [diff] [blame] | 404 | * write_lock(A) |
| 405 | * |
| 406 | * This test case is aimed at poking whether the chain cache prevents us from |
| 407 | * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ |
| 408 | * read/write locks, the following case may happen |
| 409 | * |
| 410 | * { read_lock(A)->lock(B) dependency exists } |
| 411 | * |
| 412 | * P0: |
| 413 | * lock(B); |
| 414 | * read_lock(A); |
| 415 | * |
| 416 | * { Not a deadlock, B -> A is added in the chain cache } |
| 417 | * |
| 418 | * P1: |
| 419 | * lock(B); |
| 420 | * write_lock(A); |
| 421 | * |
| 422 | * { B->A found in chain cache, not reported as a deadlock } |
| 423 | * |
| 424 | */ |
| 425 | static void rlock_chaincache_ABBA1(void) |
| 426 | { |
| 427 | RL(X1); |
| 428 | L(Y1); |
| 429 | U(Y1); |
| 430 | RU(X1); |
| 431 | |
| 432 | L(Y1); |
| 433 | RL(X1); |
| 434 | RU(X1); |
| 435 | U(Y1); |
| 436 | |
| 437 | L(Y1); |
| 438 | WL(X1); |
| 439 | WU(X1); |
| 440 | U(Y1); // should fail |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * read_lock(A) |
| 445 | * spin_lock(B) |
| 446 | * spin_lock(B) |
Peter Zijlstra | e914985 | 2017-08-23 13:13:11 +0200 | [diff] [blame] | 447 | * read_lock(A) |
| 448 | */ |
| 449 | static void rlock_ABBA2(void) |
| 450 | { |
| 451 | RL(X1); |
| 452 | L(Y1); |
| 453 | U(Y1); |
| 454 | RU(X1); |
| 455 | |
| 456 | L(Y1); |
| 457 | RL(X1); |
| 458 | RU(X1); |
| 459 | U(Y1); // should NOT fail |
| 460 | } |
| 461 | |
| 462 | static void rwsem_ABBA2(void) |
| 463 | { |
| 464 | RSL(X1); |
| 465 | ML(Y1); |
| 466 | MU(Y1); |
| 467 | RSU(X1); |
| 468 | |
| 469 | ML(Y1); |
| 470 | RSL(X1); |
| 471 | RSU(X1); |
| 472 | MU(Y1); // should fail |
| 473 | } |
| 474 | |
| 475 | |
| 476 | /* |
| 477 | * write_lock(A) |
| 478 | * spin_lock(B) |
| 479 | * spin_lock(B) |
| 480 | * write_lock(A) |
| 481 | */ |
| 482 | static void rlock_ABBA3(void) |
| 483 | { |
| 484 | WL(X1); |
| 485 | L(Y1); |
| 486 | U(Y1); |
| 487 | WU(X1); |
| 488 | |
| 489 | L(Y1); |
| 490 | WL(X1); |
| 491 | WU(X1); |
| 492 | U(Y1); // should fail |
| 493 | } |
| 494 | |
| 495 | static void rwsem_ABBA3(void) |
| 496 | { |
| 497 | WSL(X1); |
| 498 | ML(Y1); |
| 499 | MU(Y1); |
| 500 | WSU(X1); |
| 501 | |
| 502 | ML(Y1); |
| 503 | WSL(X1); |
| 504 | WSU(X1); |
| 505 | MU(Y1); // should fail |
| 506 | } |
| 507 | |
| 508 | /* |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 509 | * ABBA deadlock: |
| 510 | */ |
| 511 | |
| 512 | #define E() \ |
| 513 | \ |
| 514 | LOCK_UNLOCK_2(A, B); \ |
| 515 | LOCK_UNLOCK_2(B, A); /* fail */ |
| 516 | |
| 517 | /* |
| 518 | * 6 testcases: |
| 519 | */ |
| 520 | #include "locking-selftest-spin.h" |
| 521 | GENERATE_TESTCASE(ABBA_spin) |
| 522 | #include "locking-selftest-wlock.h" |
| 523 | GENERATE_TESTCASE(ABBA_wlock) |
| 524 | #include "locking-selftest-rlock.h" |
| 525 | GENERATE_TESTCASE(ABBA_rlock) |
| 526 | #include "locking-selftest-mutex.h" |
| 527 | GENERATE_TESTCASE(ABBA_mutex) |
| 528 | #include "locking-selftest-wsem.h" |
| 529 | GENERATE_TESTCASE(ABBA_wsem) |
| 530 | #include "locking-selftest-rsem.h" |
| 531 | GENERATE_TESTCASE(ABBA_rsem) |
| 532 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 533 | #ifdef CONFIG_RT_MUTEXES |
| 534 | #include "locking-selftest-rtmutex.h" |
| 535 | GENERATE_TESTCASE(ABBA_rtmutex); |
| 536 | #endif |
| 537 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 538 | #undef E |
| 539 | |
| 540 | /* |
| 541 | * AB BC CA deadlock: |
| 542 | */ |
| 543 | |
| 544 | #define E() \ |
| 545 | \ |
| 546 | LOCK_UNLOCK_2(A, B); \ |
| 547 | LOCK_UNLOCK_2(B, C); \ |
| 548 | LOCK_UNLOCK_2(C, A); /* fail */ |
| 549 | |
| 550 | /* |
| 551 | * 6 testcases: |
| 552 | */ |
| 553 | #include "locking-selftest-spin.h" |
| 554 | GENERATE_TESTCASE(ABBCCA_spin) |
| 555 | #include "locking-selftest-wlock.h" |
| 556 | GENERATE_TESTCASE(ABBCCA_wlock) |
| 557 | #include "locking-selftest-rlock.h" |
| 558 | GENERATE_TESTCASE(ABBCCA_rlock) |
| 559 | #include "locking-selftest-mutex.h" |
| 560 | GENERATE_TESTCASE(ABBCCA_mutex) |
| 561 | #include "locking-selftest-wsem.h" |
| 562 | GENERATE_TESTCASE(ABBCCA_wsem) |
| 563 | #include "locking-selftest-rsem.h" |
| 564 | GENERATE_TESTCASE(ABBCCA_rsem) |
| 565 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 566 | #ifdef CONFIG_RT_MUTEXES |
| 567 | #include "locking-selftest-rtmutex.h" |
| 568 | GENERATE_TESTCASE(ABBCCA_rtmutex); |
| 569 | #endif |
| 570 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 571 | #undef E |
| 572 | |
| 573 | /* |
| 574 | * AB CA BC deadlock: |
| 575 | */ |
| 576 | |
| 577 | #define E() \ |
| 578 | \ |
| 579 | LOCK_UNLOCK_2(A, B); \ |
| 580 | LOCK_UNLOCK_2(C, A); \ |
| 581 | LOCK_UNLOCK_2(B, C); /* fail */ |
| 582 | |
| 583 | /* |
| 584 | * 6 testcases: |
| 585 | */ |
| 586 | #include "locking-selftest-spin.h" |
| 587 | GENERATE_TESTCASE(ABCABC_spin) |
| 588 | #include "locking-selftest-wlock.h" |
| 589 | GENERATE_TESTCASE(ABCABC_wlock) |
| 590 | #include "locking-selftest-rlock.h" |
| 591 | GENERATE_TESTCASE(ABCABC_rlock) |
| 592 | #include "locking-selftest-mutex.h" |
| 593 | GENERATE_TESTCASE(ABCABC_mutex) |
| 594 | #include "locking-selftest-wsem.h" |
| 595 | GENERATE_TESTCASE(ABCABC_wsem) |
| 596 | #include "locking-selftest-rsem.h" |
| 597 | GENERATE_TESTCASE(ABCABC_rsem) |
| 598 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 599 | #ifdef CONFIG_RT_MUTEXES |
| 600 | #include "locking-selftest-rtmutex.h" |
| 601 | GENERATE_TESTCASE(ABCABC_rtmutex); |
| 602 | #endif |
| 603 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 604 | #undef E |
| 605 | |
| 606 | /* |
| 607 | * AB BC CD DA deadlock: |
| 608 | */ |
| 609 | |
| 610 | #define E() \ |
| 611 | \ |
| 612 | LOCK_UNLOCK_2(A, B); \ |
| 613 | LOCK_UNLOCK_2(B, C); \ |
| 614 | LOCK_UNLOCK_2(C, D); \ |
| 615 | LOCK_UNLOCK_2(D, A); /* fail */ |
| 616 | |
| 617 | /* |
| 618 | * 6 testcases: |
| 619 | */ |
| 620 | #include "locking-selftest-spin.h" |
| 621 | GENERATE_TESTCASE(ABBCCDDA_spin) |
| 622 | #include "locking-selftest-wlock.h" |
| 623 | GENERATE_TESTCASE(ABBCCDDA_wlock) |
| 624 | #include "locking-selftest-rlock.h" |
| 625 | GENERATE_TESTCASE(ABBCCDDA_rlock) |
| 626 | #include "locking-selftest-mutex.h" |
| 627 | GENERATE_TESTCASE(ABBCCDDA_mutex) |
| 628 | #include "locking-selftest-wsem.h" |
| 629 | GENERATE_TESTCASE(ABBCCDDA_wsem) |
| 630 | #include "locking-selftest-rsem.h" |
| 631 | GENERATE_TESTCASE(ABBCCDDA_rsem) |
| 632 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 633 | #ifdef CONFIG_RT_MUTEXES |
| 634 | #include "locking-selftest-rtmutex.h" |
| 635 | GENERATE_TESTCASE(ABBCCDDA_rtmutex); |
| 636 | #endif |
| 637 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 638 | #undef E |
| 639 | |
| 640 | /* |
| 641 | * AB CD BD DA deadlock: |
| 642 | */ |
| 643 | #define E() \ |
| 644 | \ |
| 645 | LOCK_UNLOCK_2(A, B); \ |
| 646 | LOCK_UNLOCK_2(C, D); \ |
| 647 | LOCK_UNLOCK_2(B, D); \ |
| 648 | LOCK_UNLOCK_2(D, A); /* fail */ |
| 649 | |
| 650 | /* |
| 651 | * 6 testcases: |
| 652 | */ |
| 653 | #include "locking-selftest-spin.h" |
| 654 | GENERATE_TESTCASE(ABCDBDDA_spin) |
| 655 | #include "locking-selftest-wlock.h" |
| 656 | GENERATE_TESTCASE(ABCDBDDA_wlock) |
| 657 | #include "locking-selftest-rlock.h" |
| 658 | GENERATE_TESTCASE(ABCDBDDA_rlock) |
| 659 | #include "locking-selftest-mutex.h" |
| 660 | GENERATE_TESTCASE(ABCDBDDA_mutex) |
| 661 | #include "locking-selftest-wsem.h" |
| 662 | GENERATE_TESTCASE(ABCDBDDA_wsem) |
| 663 | #include "locking-selftest-rsem.h" |
| 664 | GENERATE_TESTCASE(ABCDBDDA_rsem) |
| 665 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 666 | #ifdef CONFIG_RT_MUTEXES |
| 667 | #include "locking-selftest-rtmutex.h" |
| 668 | GENERATE_TESTCASE(ABCDBDDA_rtmutex); |
| 669 | #endif |
| 670 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 671 | #undef E |
| 672 | |
| 673 | /* |
| 674 | * AB CD BC DA deadlock: |
| 675 | */ |
| 676 | #define E() \ |
| 677 | \ |
| 678 | LOCK_UNLOCK_2(A, B); \ |
| 679 | LOCK_UNLOCK_2(C, D); \ |
| 680 | LOCK_UNLOCK_2(B, C); \ |
| 681 | LOCK_UNLOCK_2(D, A); /* fail */ |
| 682 | |
| 683 | /* |
| 684 | * 6 testcases: |
| 685 | */ |
| 686 | #include "locking-selftest-spin.h" |
| 687 | GENERATE_TESTCASE(ABCDBCDA_spin) |
| 688 | #include "locking-selftest-wlock.h" |
| 689 | GENERATE_TESTCASE(ABCDBCDA_wlock) |
| 690 | #include "locking-selftest-rlock.h" |
| 691 | GENERATE_TESTCASE(ABCDBCDA_rlock) |
| 692 | #include "locking-selftest-mutex.h" |
| 693 | GENERATE_TESTCASE(ABCDBCDA_mutex) |
| 694 | #include "locking-selftest-wsem.h" |
| 695 | GENERATE_TESTCASE(ABCDBCDA_wsem) |
| 696 | #include "locking-selftest-rsem.h" |
| 697 | GENERATE_TESTCASE(ABCDBCDA_rsem) |
| 698 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 699 | #ifdef CONFIG_RT_MUTEXES |
| 700 | #include "locking-selftest-rtmutex.h" |
| 701 | GENERATE_TESTCASE(ABCDBCDA_rtmutex); |
| 702 | #endif |
| 703 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 704 | #undef E |
| 705 | |
| 706 | /* |
| 707 | * Double unlock: |
| 708 | */ |
| 709 | #define E() \ |
| 710 | \ |
| 711 | LOCK(A); \ |
| 712 | UNLOCK(A); \ |
| 713 | UNLOCK(A); /* fail */ |
| 714 | |
| 715 | /* |
| 716 | * 6 testcases: |
| 717 | */ |
| 718 | #include "locking-selftest-spin.h" |
| 719 | GENERATE_TESTCASE(double_unlock_spin) |
| 720 | #include "locking-selftest-wlock.h" |
| 721 | GENERATE_TESTCASE(double_unlock_wlock) |
| 722 | #include "locking-selftest-rlock.h" |
| 723 | GENERATE_TESTCASE(double_unlock_rlock) |
| 724 | #include "locking-selftest-mutex.h" |
| 725 | GENERATE_TESTCASE(double_unlock_mutex) |
| 726 | #include "locking-selftest-wsem.h" |
| 727 | GENERATE_TESTCASE(double_unlock_wsem) |
| 728 | #include "locking-selftest-rsem.h" |
| 729 | GENERATE_TESTCASE(double_unlock_rsem) |
| 730 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 731 | #ifdef CONFIG_RT_MUTEXES |
| 732 | #include "locking-selftest-rtmutex.h" |
| 733 | GENERATE_TESTCASE(double_unlock_rtmutex); |
| 734 | #endif |
| 735 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 736 | #undef E |
| 737 | |
| 738 | /* |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 739 | * initializing a held lock: |
| 740 | */ |
| 741 | #define E() \ |
| 742 | \ |
| 743 | LOCK(A); \ |
| 744 | INIT(A); /* fail */ |
| 745 | |
| 746 | /* |
| 747 | * 6 testcases: |
| 748 | */ |
| 749 | #include "locking-selftest-spin.h" |
| 750 | GENERATE_TESTCASE(init_held_spin) |
| 751 | #include "locking-selftest-wlock.h" |
| 752 | GENERATE_TESTCASE(init_held_wlock) |
| 753 | #include "locking-selftest-rlock.h" |
| 754 | GENERATE_TESTCASE(init_held_rlock) |
| 755 | #include "locking-selftest-mutex.h" |
| 756 | GENERATE_TESTCASE(init_held_mutex) |
| 757 | #include "locking-selftest-wsem.h" |
| 758 | GENERATE_TESTCASE(init_held_wsem) |
| 759 | #include "locking-selftest-rsem.h" |
| 760 | GENERATE_TESTCASE(init_held_rsem) |
| 761 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 762 | #ifdef CONFIG_RT_MUTEXES |
| 763 | #include "locking-selftest-rtmutex.h" |
| 764 | GENERATE_TESTCASE(init_held_rtmutex); |
| 765 | #endif |
| 766 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 767 | #undef E |
| 768 | |
| 769 | /* |
| 770 | * locking an irq-safe lock with irqs enabled: |
| 771 | */ |
| 772 | #define E1() \ |
| 773 | \ |
| 774 | IRQ_ENTER(); \ |
| 775 | LOCK(A); \ |
| 776 | UNLOCK(A); \ |
| 777 | IRQ_EXIT(); |
| 778 | |
| 779 | #define E2() \ |
| 780 | \ |
| 781 | LOCK(A); \ |
| 782 | UNLOCK(A); |
| 783 | |
| 784 | /* |
| 785 | * Generate 24 testcases: |
| 786 | */ |
| 787 | #include "locking-selftest-spin-hardirq.h" |
| 788 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin) |
| 789 | |
| 790 | #include "locking-selftest-rlock-hardirq.h" |
| 791 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock) |
| 792 | |
| 793 | #include "locking-selftest-wlock-hardirq.h" |
| 794 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock) |
| 795 | |
| 796 | #include "locking-selftest-spin-softirq.h" |
| 797 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin) |
| 798 | |
| 799 | #include "locking-selftest-rlock-softirq.h" |
| 800 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock) |
| 801 | |
| 802 | #include "locking-selftest-wlock-softirq.h" |
| 803 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock) |
| 804 | |
| 805 | #undef E1 |
| 806 | #undef E2 |
| 807 | |
| 808 | /* |
| 809 | * Enabling hardirqs with a softirq-safe lock held: |
| 810 | */ |
| 811 | #define E1() \ |
| 812 | \ |
| 813 | SOFTIRQ_ENTER(); \ |
| 814 | LOCK(A); \ |
| 815 | UNLOCK(A); \ |
| 816 | SOFTIRQ_EXIT(); |
| 817 | |
| 818 | #define E2() \ |
| 819 | \ |
| 820 | HARDIRQ_DISABLE(); \ |
| 821 | LOCK(A); \ |
| 822 | HARDIRQ_ENABLE(); \ |
| 823 | UNLOCK(A); |
| 824 | |
| 825 | /* |
| 826 | * Generate 12 testcases: |
| 827 | */ |
| 828 | #include "locking-selftest-spin.h" |
| 829 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin) |
| 830 | |
| 831 | #include "locking-selftest-wlock.h" |
| 832 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock) |
| 833 | |
| 834 | #include "locking-selftest-rlock.h" |
| 835 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock) |
| 836 | |
| 837 | #undef E1 |
| 838 | #undef E2 |
| 839 | |
| 840 | /* |
| 841 | * Enabling irqs with an irq-safe lock held: |
| 842 | */ |
| 843 | #define E1() \ |
| 844 | \ |
| 845 | IRQ_ENTER(); \ |
| 846 | LOCK(A); \ |
| 847 | UNLOCK(A); \ |
| 848 | IRQ_EXIT(); |
| 849 | |
| 850 | #define E2() \ |
| 851 | \ |
| 852 | IRQ_DISABLE(); \ |
| 853 | LOCK(A); \ |
| 854 | IRQ_ENABLE(); \ |
| 855 | UNLOCK(A); |
| 856 | |
| 857 | /* |
| 858 | * Generate 24 testcases: |
| 859 | */ |
| 860 | #include "locking-selftest-spin-hardirq.h" |
| 861 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin) |
| 862 | |
| 863 | #include "locking-selftest-rlock-hardirq.h" |
| 864 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock) |
| 865 | |
| 866 | #include "locking-selftest-wlock-hardirq.h" |
| 867 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock) |
| 868 | |
| 869 | #include "locking-selftest-spin-softirq.h" |
| 870 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin) |
| 871 | |
| 872 | #include "locking-selftest-rlock-softirq.h" |
| 873 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock) |
| 874 | |
| 875 | #include "locking-selftest-wlock-softirq.h" |
| 876 | GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock) |
| 877 | |
| 878 | #undef E1 |
| 879 | #undef E2 |
| 880 | |
| 881 | /* |
| 882 | * Acquiring a irq-unsafe lock while holding an irq-safe-lock: |
| 883 | */ |
| 884 | #define E1() \ |
| 885 | \ |
| 886 | LOCK(A); \ |
| 887 | LOCK(B); \ |
| 888 | UNLOCK(B); \ |
| 889 | UNLOCK(A); \ |
| 890 | |
| 891 | #define E2() \ |
| 892 | \ |
| 893 | LOCK(B); \ |
| 894 | UNLOCK(B); |
| 895 | |
| 896 | #define E3() \ |
| 897 | \ |
| 898 | IRQ_ENTER(); \ |
| 899 | LOCK(A); \ |
| 900 | UNLOCK(A); \ |
| 901 | IRQ_EXIT(); |
| 902 | |
| 903 | /* |
| 904 | * Generate 36 testcases: |
| 905 | */ |
| 906 | #include "locking-selftest-spin-hardirq.h" |
| 907 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin) |
| 908 | |
| 909 | #include "locking-selftest-rlock-hardirq.h" |
| 910 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock) |
| 911 | |
| 912 | #include "locking-selftest-wlock-hardirq.h" |
| 913 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock) |
| 914 | |
| 915 | #include "locking-selftest-spin-softirq.h" |
| 916 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin) |
| 917 | |
| 918 | #include "locking-selftest-rlock-softirq.h" |
| 919 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock) |
| 920 | |
| 921 | #include "locking-selftest-wlock-softirq.h" |
| 922 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock) |
| 923 | |
| 924 | #undef E1 |
| 925 | #undef E2 |
| 926 | #undef E3 |
| 927 | |
| 928 | /* |
| 929 | * If a lock turns into softirq-safe, but earlier it took |
| 930 | * a softirq-unsafe lock: |
| 931 | */ |
| 932 | |
| 933 | #define E1() \ |
| 934 | IRQ_DISABLE(); \ |
| 935 | LOCK(A); \ |
| 936 | LOCK(B); \ |
| 937 | UNLOCK(B); \ |
| 938 | UNLOCK(A); \ |
| 939 | IRQ_ENABLE(); |
| 940 | |
| 941 | #define E2() \ |
| 942 | LOCK(B); \ |
| 943 | UNLOCK(B); |
| 944 | |
| 945 | #define E3() \ |
| 946 | IRQ_ENTER(); \ |
| 947 | LOCK(A); \ |
| 948 | UNLOCK(A); \ |
| 949 | IRQ_EXIT(); |
| 950 | |
| 951 | /* |
| 952 | * Generate 36 testcases: |
| 953 | */ |
| 954 | #include "locking-selftest-spin-hardirq.h" |
| 955 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin) |
| 956 | |
| 957 | #include "locking-selftest-rlock-hardirq.h" |
| 958 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock) |
| 959 | |
| 960 | #include "locking-selftest-wlock-hardirq.h" |
| 961 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock) |
| 962 | |
| 963 | #include "locking-selftest-spin-softirq.h" |
| 964 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin) |
| 965 | |
| 966 | #include "locking-selftest-rlock-softirq.h" |
| 967 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock) |
| 968 | |
| 969 | #include "locking-selftest-wlock-softirq.h" |
| 970 | GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock) |
| 971 | |
| 972 | #undef E1 |
| 973 | #undef E2 |
| 974 | #undef E3 |
| 975 | |
| 976 | /* |
| 977 | * read-lock / write-lock irq inversion. |
| 978 | * |
| 979 | * Deadlock scenario: |
| 980 | * |
| 981 | * CPU#1 is at #1, i.e. it has write-locked A, but has not |
| 982 | * taken B yet. |
| 983 | * |
| 984 | * CPU#2 is at #2, i.e. it has locked B. |
| 985 | * |
| 986 | * Hardirq hits CPU#2 at point #2 and is trying to read-lock A. |
| 987 | * |
| 988 | * The deadlock occurs because CPU#1 will spin on B, and CPU#2 |
| 989 | * will spin on A. |
| 990 | */ |
| 991 | |
| 992 | #define E1() \ |
| 993 | \ |
| 994 | IRQ_DISABLE(); \ |
| 995 | WL(A); \ |
| 996 | LOCK(B); \ |
| 997 | UNLOCK(B); \ |
| 998 | WU(A); \ |
| 999 | IRQ_ENABLE(); |
| 1000 | |
| 1001 | #define E2() \ |
| 1002 | \ |
| 1003 | LOCK(B); \ |
| 1004 | UNLOCK(B); |
| 1005 | |
| 1006 | #define E3() \ |
| 1007 | \ |
| 1008 | IRQ_ENTER(); \ |
| 1009 | RL(A); \ |
| 1010 | RU(A); \ |
| 1011 | IRQ_EXIT(); |
| 1012 | |
| 1013 | /* |
| 1014 | * Generate 36 testcases: |
| 1015 | */ |
| 1016 | #include "locking-selftest-spin-hardirq.h" |
| 1017 | GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin) |
| 1018 | |
| 1019 | #include "locking-selftest-rlock-hardirq.h" |
| 1020 | GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock) |
| 1021 | |
| 1022 | #include "locking-selftest-wlock-hardirq.h" |
| 1023 | GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock) |
| 1024 | |
| 1025 | #include "locking-selftest-spin-softirq.h" |
| 1026 | GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin) |
| 1027 | |
| 1028 | #include "locking-selftest-rlock-softirq.h" |
| 1029 | GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock) |
| 1030 | |
| 1031 | #include "locking-selftest-wlock-softirq.h" |
| 1032 | GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock) |
| 1033 | |
| 1034 | #undef E1 |
| 1035 | #undef E2 |
| 1036 | #undef E3 |
| 1037 | |
| 1038 | /* |
Boqun Feng | 8ef7ca7 | 2020-08-07 15:42:35 +0800 | [diff] [blame] | 1039 | * write-read / write-read / write-read deadlock even if read is recursive |
| 1040 | */ |
| 1041 | |
| 1042 | #define E1() \ |
| 1043 | \ |
| 1044 | WL(X1); \ |
| 1045 | RL(Y1); \ |
| 1046 | RU(Y1); \ |
| 1047 | WU(X1); |
| 1048 | |
| 1049 | #define E2() \ |
| 1050 | \ |
| 1051 | WL(Y1); \ |
| 1052 | RL(Z1); \ |
| 1053 | RU(Z1); \ |
| 1054 | WU(Y1); |
| 1055 | |
| 1056 | #define E3() \ |
| 1057 | \ |
| 1058 | WL(Z1); \ |
| 1059 | RL(X1); \ |
| 1060 | RU(X1); \ |
| 1061 | WU(Z1); |
| 1062 | |
| 1063 | #include "locking-selftest-rlock.h" |
| 1064 | GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1) |
| 1065 | |
| 1066 | #undef E1 |
| 1067 | #undef E2 |
| 1068 | #undef E3 |
| 1069 | |
| 1070 | /* |
| 1071 | * write-write / read-read / write-read deadlock even if read is recursive |
| 1072 | */ |
| 1073 | |
| 1074 | #define E1() \ |
| 1075 | \ |
| 1076 | WL(X1); \ |
| 1077 | WL(Y1); \ |
| 1078 | WU(Y1); \ |
| 1079 | WU(X1); |
| 1080 | |
| 1081 | #define E2() \ |
| 1082 | \ |
| 1083 | RL(Y1); \ |
| 1084 | RL(Z1); \ |
| 1085 | RU(Z1); \ |
| 1086 | RU(Y1); |
| 1087 | |
| 1088 | #define E3() \ |
| 1089 | \ |
| 1090 | WL(Z1); \ |
| 1091 | RL(X1); \ |
| 1092 | RU(X1); \ |
| 1093 | WU(Z1); |
| 1094 | |
| 1095 | #include "locking-selftest-rlock.h" |
| 1096 | GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1) |
| 1097 | |
| 1098 | #undef E1 |
| 1099 | #undef E2 |
| 1100 | #undef E3 |
| 1101 | |
| 1102 | /* |
| 1103 | * write-write / read-read / read-write is not deadlock when read is recursive |
| 1104 | */ |
| 1105 | |
| 1106 | #define E1() \ |
| 1107 | \ |
| 1108 | WL(X1); \ |
| 1109 | WL(Y1); \ |
| 1110 | WU(Y1); \ |
| 1111 | WU(X1); |
| 1112 | |
| 1113 | #define E2() \ |
| 1114 | \ |
| 1115 | RL(Y1); \ |
| 1116 | RL(Z1); \ |
| 1117 | RU(Z1); \ |
| 1118 | RU(Y1); |
| 1119 | |
| 1120 | #define E3() \ |
| 1121 | \ |
| 1122 | RL(Z1); \ |
| 1123 | WL(X1); \ |
| 1124 | WU(X1); \ |
| 1125 | RU(Z1); |
| 1126 | |
| 1127 | #include "locking-selftest-rlock.h" |
| 1128 | GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1) |
| 1129 | |
| 1130 | #undef E1 |
| 1131 | #undef E2 |
| 1132 | #undef E3 |
| 1133 | |
| 1134 | /* |
| 1135 | * write-read / read-read / write-write is not deadlock when read is recursive |
| 1136 | */ |
| 1137 | |
| 1138 | #define E1() \ |
| 1139 | \ |
| 1140 | WL(X1); \ |
| 1141 | RL(Y1); \ |
| 1142 | RU(Y1); \ |
| 1143 | WU(X1); |
| 1144 | |
| 1145 | #define E2() \ |
| 1146 | \ |
| 1147 | RL(Y1); \ |
| 1148 | RL(Z1); \ |
| 1149 | RU(Z1); \ |
| 1150 | RU(Y1); |
| 1151 | |
| 1152 | #define E3() \ |
| 1153 | \ |
| 1154 | WL(Z1); \ |
| 1155 | WL(X1); \ |
| 1156 | WU(X1); \ |
| 1157 | WU(Z1); |
| 1158 | |
| 1159 | #include "locking-selftest-rlock.h" |
| 1160 | GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1) |
| 1161 | |
| 1162 | #undef E1 |
| 1163 | #undef E2 |
| 1164 | #undef E3 |
| 1165 | /* |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1166 | * read-lock / write-lock recursion that is actually safe. |
| 1167 | */ |
| 1168 | |
| 1169 | #define E1() \ |
| 1170 | \ |
| 1171 | IRQ_DISABLE(); \ |
| 1172 | WL(A); \ |
| 1173 | WU(A); \ |
| 1174 | IRQ_ENABLE(); |
| 1175 | |
| 1176 | #define E2() \ |
| 1177 | \ |
| 1178 | RL(A); \ |
| 1179 | RU(A); \ |
| 1180 | |
| 1181 | #define E3() \ |
| 1182 | \ |
| 1183 | IRQ_ENTER(); \ |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1184 | LOCK(A); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1185 | L(B); \ |
| 1186 | U(B); \ |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1187 | UNLOCK(A); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1188 | IRQ_EXIT(); |
| 1189 | |
| 1190 | /* |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1191 | * Generate 24 testcases: |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1192 | */ |
| 1193 | #include "locking-selftest-hardirq.h" |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1194 | #include "locking-selftest-rlock.h" |
| 1195 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock) |
| 1196 | |
| 1197 | #include "locking-selftest-wlock.h" |
| 1198 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock) |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1199 | |
| 1200 | #include "locking-selftest-softirq.h" |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1201 | #include "locking-selftest-rlock.h" |
| 1202 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock) |
| 1203 | |
| 1204 | #include "locking-selftest-wlock.h" |
| 1205 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock) |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1206 | |
| 1207 | #undef E1 |
| 1208 | #undef E2 |
| 1209 | #undef E3 |
| 1210 | |
| 1211 | /* |
| 1212 | * read-lock / write-lock recursion that is unsafe. |
| 1213 | */ |
| 1214 | |
| 1215 | #define E1() \ |
| 1216 | \ |
| 1217 | IRQ_DISABLE(); \ |
| 1218 | L(B); \ |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1219 | LOCK(A); \ |
| 1220 | UNLOCK(A); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1221 | U(B); \ |
| 1222 | IRQ_ENABLE(); |
| 1223 | |
| 1224 | #define E2() \ |
| 1225 | \ |
| 1226 | RL(A); \ |
| 1227 | RU(A); \ |
| 1228 | |
| 1229 | #define E3() \ |
| 1230 | \ |
| 1231 | IRQ_ENTER(); \ |
| 1232 | L(B); \ |
| 1233 | U(B); \ |
| 1234 | IRQ_EXIT(); |
| 1235 | |
| 1236 | /* |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1237 | * Generate 24 testcases: |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1238 | */ |
| 1239 | #include "locking-selftest-hardirq.h" |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1240 | #include "locking-selftest-rlock.h" |
| 1241 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock) |
| 1242 | |
| 1243 | #include "locking-selftest-wlock.h" |
| 1244 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock) |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1245 | |
| 1246 | #include "locking-selftest-softirq.h" |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1247 | #include "locking-selftest-rlock.h" |
| 1248 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock) |
| 1249 | |
| 1250 | #include "locking-selftest-wlock.h" |
| 1251 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock) |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1252 | |
Boqun Feng | 96a16f4 | 2020-08-07 15:42:38 +0800 | [diff] [blame] | 1253 | #undef E1 |
| 1254 | #undef E2 |
| 1255 | #undef E3 |
| 1256 | /* |
| 1257 | * read-lock / write-lock recursion that is unsafe. |
| 1258 | * |
| 1259 | * A is a ENABLED_*_READ lock |
| 1260 | * B is a USED_IN_*_READ lock |
| 1261 | * |
| 1262 | * read_lock(A); |
| 1263 | * write_lock(B); |
| 1264 | * <interrupt> |
| 1265 | * read_lock(B); |
| 1266 | * write_lock(A); // if this one is read_lock(), no deadlock |
| 1267 | */ |
| 1268 | |
| 1269 | #define E1() \ |
| 1270 | \ |
| 1271 | IRQ_DISABLE(); \ |
| 1272 | WL(B); \ |
| 1273 | LOCK(A); \ |
| 1274 | UNLOCK(A); \ |
| 1275 | WU(B); \ |
| 1276 | IRQ_ENABLE(); |
| 1277 | |
| 1278 | #define E2() \ |
| 1279 | \ |
| 1280 | RL(A); \ |
| 1281 | RU(A); \ |
| 1282 | |
| 1283 | #define E3() \ |
| 1284 | \ |
| 1285 | IRQ_ENTER(); \ |
| 1286 | RL(B); \ |
| 1287 | RU(B); \ |
| 1288 | IRQ_EXIT(); |
| 1289 | |
| 1290 | /* |
| 1291 | * Generate 24 testcases: |
| 1292 | */ |
| 1293 | #include "locking-selftest-hardirq.h" |
| 1294 | #include "locking-selftest-rlock.h" |
| 1295 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock) |
| 1296 | |
| 1297 | #include "locking-selftest-wlock.h" |
| 1298 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock) |
| 1299 | |
| 1300 | #include "locking-selftest-softirq.h" |
| 1301 | #include "locking-selftest-rlock.h" |
| 1302 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock) |
| 1303 | |
| 1304 | #include "locking-selftest-wlock.h" |
| 1305 | GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock) |
| 1306 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1307 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 1308 | # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map) |
| 1309 | # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map) |
| 1310 | # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map) |
| 1311 | # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map) |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1312 | # define I_WW(x) lockdep_reset_lock(&x.dep_map) |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 1313 | #ifdef CONFIG_RT_MUTEXES |
| 1314 | # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map) |
| 1315 | #endif |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1316 | #else |
| 1317 | # define I_SPINLOCK(x) |
| 1318 | # define I_RWLOCK(x) |
| 1319 | # define I_MUTEX(x) |
| 1320 | # define I_RWSEM(x) |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1321 | # define I_WW(x) |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1322 | #endif |
| 1323 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 1324 | #ifndef I_RTMUTEX |
| 1325 | # define I_RTMUTEX(x) |
| 1326 | #endif |
| 1327 | |
| 1328 | #ifdef CONFIG_RT_MUTEXES |
| 1329 | #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x) |
| 1330 | #else |
| 1331 | #define I2_RTMUTEX(x) |
| 1332 | #endif |
| 1333 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1334 | #define I1(x) \ |
| 1335 | do { \ |
| 1336 | I_SPINLOCK(x); \ |
| 1337 | I_RWLOCK(x); \ |
| 1338 | I_MUTEX(x); \ |
| 1339 | I_RWSEM(x); \ |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 1340 | I_RTMUTEX(x); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1341 | } while (0) |
| 1342 | |
| 1343 | #define I2(x) \ |
| 1344 | do { \ |
Yong Zhang | 9fb1b90 | 2012-04-16 15:01:55 +0800 | [diff] [blame] | 1345 | raw_spin_lock_init(&lock_##x); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1346 | rwlock_init(&rwlock_##x); \ |
| 1347 | mutex_init(&mutex_##x); \ |
| 1348 | init_rwsem(&rwsem_##x); \ |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 1349 | I2_RTMUTEX(x); \ |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1350 | } while (0) |
| 1351 | |
| 1352 | static void reset_locks(void) |
| 1353 | { |
| 1354 | local_irq_disable(); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1355 | lockdep_free_key_range(&ww_lockdep.acquire_key, 1); |
| 1356 | lockdep_free_key_range(&ww_lockdep.mutex_key, 1); |
| 1357 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1358 | I1(A); I1(B); I1(C); I1(D); |
| 1359 | I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1360 | I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1361 | lockdep_reset(); |
| 1362 | I2(A); I2(B); I2(C); I2(D); |
| 1363 | init_shared_classes(); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1364 | |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1365 | ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1366 | memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2)); |
| 1367 | memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key)); |
| 1368 | memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key)); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1369 | local_irq_enable(); |
| 1370 | } |
| 1371 | |
| 1372 | #undef I |
| 1373 | |
| 1374 | static int testcase_total; |
| 1375 | static int testcase_successes; |
| 1376 | static int expected_testcase_failures; |
| 1377 | static int unexpected_testcase_failures; |
| 1378 | |
| 1379 | static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask) |
| 1380 | { |
| 1381 | unsigned long saved_preempt_count = preempt_count(); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1382 | |
| 1383 | WARN_ON(irqs_disabled()); |
| 1384 | |
| 1385 | testcase_fn(); |
| 1386 | /* |
| 1387 | * Filter out expected failures: |
| 1388 | */ |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1389 | #ifndef CONFIG_PROVE_LOCKING |
Maarten Lankhorst | 166989e | 2013-06-20 13:31:51 +0200 | [diff] [blame] | 1390 | if (expected == FAILURE && debug_locks) { |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1391 | expected_testcase_failures++; |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1392 | pr_cont("failed|"); |
Maarten Lankhorst | 166989e | 2013-06-20 13:31:51 +0200 | [diff] [blame] | 1393 | } |
| 1394 | else |
| 1395 | #endif |
| 1396 | if (debug_locks != expected) { |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1397 | unexpected_testcase_failures++; |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1398 | pr_cont("FAILED|"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1399 | } else { |
| 1400 | testcase_successes++; |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1401 | pr_cont(" ok |"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1402 | } |
| 1403 | testcase_total++; |
| 1404 | |
| 1405 | if (debug_locks_verbose) |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1406 | pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n", |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1407 | lockclass_mask, debug_locks, expected); |
| 1408 | /* |
| 1409 | * Some tests (e.g. double-unlock) might corrupt the preemption |
| 1410 | * count, so restore it: |
| 1411 | */ |
Peter Zijlstra | 4a2b4b2 | 2013-08-14 14:55:24 +0200 | [diff] [blame] | 1412 | preempt_count_set(saved_preempt_count); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1413 | #ifdef CONFIG_TRACE_IRQFLAGS |
| 1414 | if (softirq_count()) |
| 1415 | current->softirqs_enabled = 0; |
| 1416 | else |
| 1417 | current->softirqs_enabled = 1; |
| 1418 | #endif |
| 1419 | |
| 1420 | reset_locks(); |
| 1421 | } |
| 1422 | |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 1423 | #ifdef CONFIG_RT_MUTEXES |
| 1424 | #define dotest_rt(fn, e, m) dotest((fn), (e), (m)) |
| 1425 | #else |
| 1426 | #define dotest_rt(fn, e, m) |
| 1427 | #endif |
| 1428 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1429 | static inline void print_testname(const char *testname) |
| 1430 | { |
| 1431 | printk("%33s:", testname); |
| 1432 | } |
| 1433 | |
| 1434 | #define DO_TESTCASE_1(desc, name, nr) \ |
| 1435 | print_testname(desc"/"#nr); \ |
| 1436 | dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1437 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1438 | |
| 1439 | #define DO_TESTCASE_1B(desc, name, nr) \ |
| 1440 | print_testname(desc"/"#nr); \ |
| 1441 | dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \ |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1442 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1443 | |
Boqun Feng | 8ef7ca7 | 2020-08-07 15:42:35 +0800 | [diff] [blame] | 1444 | #define DO_TESTCASE_1RR(desc, name, nr) \ |
| 1445 | print_testname(desc"/"#nr); \ |
| 1446 | pr_cont(" |"); \ |
| 1447 | dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ |
| 1448 | pr_cont("\n"); |
| 1449 | |
| 1450 | #define DO_TESTCASE_1RRB(desc, name, nr) \ |
| 1451 | print_testname(desc"/"#nr); \ |
| 1452 | pr_cont(" |"); \ |
| 1453 | dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \ |
| 1454 | pr_cont("\n"); |
| 1455 | |
| 1456 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1457 | #define DO_TESTCASE_3(desc, name, nr) \ |
| 1458 | print_testname(desc"/"#nr); \ |
| 1459 | dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \ |
| 1460 | dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \ |
| 1461 | dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1462 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1463 | |
| 1464 | #define DO_TESTCASE_3RW(desc, name, nr) \ |
| 1465 | print_testname(desc"/"#nr); \ |
| 1466 | dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\ |
| 1467 | dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \ |
| 1468 | dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1469 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1470 | |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 1471 | #define DO_TESTCASE_2RW(desc, name, nr) \ |
| 1472 | print_testname(desc"/"#nr); \ |
| 1473 | pr_cont(" |"); \ |
| 1474 | dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \ |
| 1475 | dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ |
| 1476 | pr_cont("\n"); |
| 1477 | |
| 1478 | #define DO_TESTCASE_2x2RW(desc, name, nr) \ |
| 1479 | DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \ |
| 1480 | DO_TESTCASE_2RW("soft-"desc, name##_soft, nr) \ |
| 1481 | |
| 1482 | #define DO_TESTCASE_6x2x2RW(desc, name) \ |
| 1483 | DO_TESTCASE_2x2RW(desc, name, 123); \ |
| 1484 | DO_TESTCASE_2x2RW(desc, name, 132); \ |
| 1485 | DO_TESTCASE_2x2RW(desc, name, 213); \ |
| 1486 | DO_TESTCASE_2x2RW(desc, name, 231); \ |
| 1487 | DO_TESTCASE_2x2RW(desc, name, 312); \ |
| 1488 | DO_TESTCASE_2x2RW(desc, name, 321); |
| 1489 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1490 | #define DO_TESTCASE_6(desc, name) \ |
| 1491 | print_testname(desc); \ |
| 1492 | dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \ |
| 1493 | dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \ |
| 1494 | dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \ |
| 1495 | dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \ |
| 1496 | dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \ |
| 1497 | dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \ |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 1498 | dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \ |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1499 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1500 | |
| 1501 | #define DO_TESTCASE_6_SUCCESS(desc, name) \ |
| 1502 | print_testname(desc); \ |
| 1503 | dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \ |
| 1504 | dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \ |
| 1505 | dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \ |
| 1506 | dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \ |
| 1507 | dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \ |
| 1508 | dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \ |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 1509 | dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \ |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1510 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1511 | |
| 1512 | /* |
| 1513 | * 'read' variant: rlocks must not trigger. |
| 1514 | */ |
| 1515 | #define DO_TESTCASE_6R(desc, name) \ |
| 1516 | print_testname(desc); \ |
| 1517 | dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \ |
| 1518 | dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \ |
| 1519 | dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \ |
| 1520 | dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \ |
| 1521 | dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \ |
| 1522 | dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \ |
Peter Zijlstra | 018956d | 2017-05-31 17:11:49 +0200 | [diff] [blame] | 1523 | dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \ |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 1524 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1525 | |
| 1526 | #define DO_TESTCASE_2I(desc, name, nr) \ |
| 1527 | DO_TESTCASE_1("hard-"desc, name##_hard, nr); \ |
| 1528 | DO_TESTCASE_1("soft-"desc, name##_soft, nr); |
| 1529 | |
| 1530 | #define DO_TESTCASE_2IB(desc, name, nr) \ |
| 1531 | DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \ |
| 1532 | DO_TESTCASE_1B("soft-"desc, name##_soft, nr); |
| 1533 | |
| 1534 | #define DO_TESTCASE_6I(desc, name, nr) \ |
| 1535 | DO_TESTCASE_3("hard-"desc, name##_hard, nr); \ |
| 1536 | DO_TESTCASE_3("soft-"desc, name##_soft, nr); |
| 1537 | |
| 1538 | #define DO_TESTCASE_6IRW(desc, name, nr) \ |
| 1539 | DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \ |
| 1540 | DO_TESTCASE_3RW("soft-"desc, name##_soft, nr); |
| 1541 | |
| 1542 | #define DO_TESTCASE_2x3(desc, name) \ |
| 1543 | DO_TESTCASE_3(desc, name, 12); \ |
| 1544 | DO_TESTCASE_3(desc, name, 21); |
| 1545 | |
| 1546 | #define DO_TESTCASE_2x6(desc, name) \ |
| 1547 | DO_TESTCASE_6I(desc, name, 12); \ |
| 1548 | DO_TESTCASE_6I(desc, name, 21); |
| 1549 | |
| 1550 | #define DO_TESTCASE_6x2(desc, name) \ |
| 1551 | DO_TESTCASE_2I(desc, name, 123); \ |
| 1552 | DO_TESTCASE_2I(desc, name, 132); \ |
| 1553 | DO_TESTCASE_2I(desc, name, 213); \ |
| 1554 | DO_TESTCASE_2I(desc, name, 231); \ |
| 1555 | DO_TESTCASE_2I(desc, name, 312); \ |
| 1556 | DO_TESTCASE_2I(desc, name, 321); |
| 1557 | |
| 1558 | #define DO_TESTCASE_6x2B(desc, name) \ |
| 1559 | DO_TESTCASE_2IB(desc, name, 123); \ |
| 1560 | DO_TESTCASE_2IB(desc, name, 132); \ |
| 1561 | DO_TESTCASE_2IB(desc, name, 213); \ |
| 1562 | DO_TESTCASE_2IB(desc, name, 231); \ |
| 1563 | DO_TESTCASE_2IB(desc, name, 312); \ |
| 1564 | DO_TESTCASE_2IB(desc, name, 321); |
| 1565 | |
Boqun Feng | 8ef7ca7 | 2020-08-07 15:42:35 +0800 | [diff] [blame] | 1566 | #define DO_TESTCASE_6x1RR(desc, name) \ |
| 1567 | DO_TESTCASE_1RR(desc, name, 123); \ |
| 1568 | DO_TESTCASE_1RR(desc, name, 132); \ |
| 1569 | DO_TESTCASE_1RR(desc, name, 213); \ |
| 1570 | DO_TESTCASE_1RR(desc, name, 231); \ |
| 1571 | DO_TESTCASE_1RR(desc, name, 312); \ |
| 1572 | DO_TESTCASE_1RR(desc, name, 321); |
| 1573 | |
| 1574 | #define DO_TESTCASE_6x1RRB(desc, name) \ |
| 1575 | DO_TESTCASE_1RRB(desc, name, 123); \ |
| 1576 | DO_TESTCASE_1RRB(desc, name, 132); \ |
| 1577 | DO_TESTCASE_1RRB(desc, name, 213); \ |
| 1578 | DO_TESTCASE_1RRB(desc, name, 231); \ |
| 1579 | DO_TESTCASE_1RRB(desc, name, 312); \ |
| 1580 | DO_TESTCASE_1RRB(desc, name, 321); |
| 1581 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 1582 | #define DO_TESTCASE_6x6(desc, name) \ |
| 1583 | DO_TESTCASE_6I(desc, name, 123); \ |
| 1584 | DO_TESTCASE_6I(desc, name, 132); \ |
| 1585 | DO_TESTCASE_6I(desc, name, 213); \ |
| 1586 | DO_TESTCASE_6I(desc, name, 231); \ |
| 1587 | DO_TESTCASE_6I(desc, name, 312); \ |
| 1588 | DO_TESTCASE_6I(desc, name, 321); |
| 1589 | |
| 1590 | #define DO_TESTCASE_6x6RW(desc, name) \ |
| 1591 | DO_TESTCASE_6IRW(desc, name, 123); \ |
| 1592 | DO_TESTCASE_6IRW(desc, name, 132); \ |
| 1593 | DO_TESTCASE_6IRW(desc, name, 213); \ |
| 1594 | DO_TESTCASE_6IRW(desc, name, 231); \ |
| 1595 | DO_TESTCASE_6IRW(desc, name, 312); \ |
| 1596 | DO_TESTCASE_6IRW(desc, name, 321); |
| 1597 | |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1598 | static void ww_test_fail_acquire(void) |
| 1599 | { |
| 1600 | int ret; |
| 1601 | |
| 1602 | WWAI(&t); |
| 1603 | t.stamp++; |
| 1604 | |
| 1605 | ret = WWL(&o, &t); |
| 1606 | |
| 1607 | if (WARN_ON(!o.ctx) || |
| 1608 | WARN_ON(ret)) |
| 1609 | return; |
| 1610 | |
| 1611 | /* No lockdep test, pure API */ |
| 1612 | ret = WWL(&o, &t); |
| 1613 | WARN_ON(ret != -EALREADY); |
| 1614 | |
| 1615 | ret = WWT(&o); |
| 1616 | WARN_ON(ret); |
| 1617 | |
| 1618 | t2 = t; |
| 1619 | t2.stamp++; |
| 1620 | ret = WWL(&o, &t2); |
| 1621 | WARN_ON(ret != -EDEADLK); |
| 1622 | WWU(&o); |
| 1623 | |
| 1624 | if (WWT(&o)) |
| 1625 | WWU(&o); |
| 1626 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 1627 | else |
| 1628 | DEBUG_LOCKS_WARN_ON(1); |
| 1629 | #endif |
| 1630 | } |
| 1631 | |
Maarten Lankhorst | 2fe3d4b | 2013-06-20 13:31:30 +0200 | [diff] [blame] | 1632 | static void ww_test_normal(void) |
| 1633 | { |
| 1634 | int ret; |
| 1635 | |
| 1636 | WWAI(&t); |
| 1637 | |
| 1638 | /* |
| 1639 | * None of the ww_mutex codepaths should be taken in the 'normal' |
| 1640 | * mutex calls. The easiest way to verify this is by using the |
| 1641 | * normal mutex calls, and making sure o.ctx is unmodified. |
| 1642 | */ |
| 1643 | |
| 1644 | /* mutex_lock (and indirectly, mutex_lock_nested) */ |
| 1645 | o.ctx = (void *)~0UL; |
| 1646 | mutex_lock(&o.base); |
| 1647 | mutex_unlock(&o.base); |
| 1648 | WARN_ON(o.ctx != (void *)~0UL); |
| 1649 | |
| 1650 | /* mutex_lock_interruptible (and *_nested) */ |
| 1651 | o.ctx = (void *)~0UL; |
| 1652 | ret = mutex_lock_interruptible(&o.base); |
| 1653 | if (!ret) |
| 1654 | mutex_unlock(&o.base); |
| 1655 | else |
| 1656 | WARN_ON(1); |
| 1657 | WARN_ON(o.ctx != (void *)~0UL); |
| 1658 | |
| 1659 | /* mutex_lock_killable (and *_nested) */ |
| 1660 | o.ctx = (void *)~0UL; |
| 1661 | ret = mutex_lock_killable(&o.base); |
| 1662 | if (!ret) |
| 1663 | mutex_unlock(&o.base); |
| 1664 | else |
| 1665 | WARN_ON(1); |
| 1666 | WARN_ON(o.ctx != (void *)~0UL); |
| 1667 | |
| 1668 | /* trylock, succeeding */ |
| 1669 | o.ctx = (void *)~0UL; |
| 1670 | ret = mutex_trylock(&o.base); |
| 1671 | WARN_ON(!ret); |
| 1672 | if (ret) |
| 1673 | mutex_unlock(&o.base); |
| 1674 | else |
| 1675 | WARN_ON(1); |
| 1676 | WARN_ON(o.ctx != (void *)~0UL); |
| 1677 | |
| 1678 | /* trylock, failing */ |
| 1679 | o.ctx = (void *)~0UL; |
| 1680 | mutex_lock(&o.base); |
| 1681 | ret = mutex_trylock(&o.base); |
| 1682 | WARN_ON(ret); |
| 1683 | mutex_unlock(&o.base); |
| 1684 | WARN_ON(o.ctx != (void *)~0UL); |
| 1685 | |
| 1686 | /* nest_lock */ |
| 1687 | o.ctx = (void *)~0UL; |
| 1688 | mutex_lock_nest_lock(&o.base, &t); |
| 1689 | mutex_unlock(&o.base); |
| 1690 | WARN_ON(o.ctx != (void *)~0UL); |
| 1691 | } |
| 1692 | |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 1693 | static void ww_test_two_contexts(void) |
| 1694 | { |
| 1695 | WWAI(&t); |
| 1696 | WWAI(&t2); |
| 1697 | } |
| 1698 | |
| 1699 | static void ww_test_diff_class(void) |
| 1700 | { |
| 1701 | WWAI(&t); |
| 1702 | #ifdef CONFIG_DEBUG_MUTEXES |
| 1703 | t.ww_class = NULL; |
| 1704 | #endif |
| 1705 | WWL(&o, &t); |
| 1706 | } |
| 1707 | |
| 1708 | static void ww_test_context_done_twice(void) |
| 1709 | { |
| 1710 | WWAI(&t); |
| 1711 | WWAD(&t); |
| 1712 | WWAD(&t); |
| 1713 | WWAF(&t); |
| 1714 | } |
| 1715 | |
| 1716 | static void ww_test_context_unlock_twice(void) |
| 1717 | { |
| 1718 | WWAI(&t); |
| 1719 | WWAD(&t); |
| 1720 | WWAF(&t); |
| 1721 | WWAF(&t); |
| 1722 | } |
| 1723 | |
| 1724 | static void ww_test_context_fini_early(void) |
| 1725 | { |
| 1726 | WWAI(&t); |
| 1727 | WWL(&o, &t); |
| 1728 | WWAD(&t); |
| 1729 | WWAF(&t); |
| 1730 | } |
| 1731 | |
| 1732 | static void ww_test_context_lock_after_done(void) |
| 1733 | { |
| 1734 | WWAI(&t); |
| 1735 | WWAD(&t); |
| 1736 | WWL(&o, &t); |
| 1737 | } |
| 1738 | |
| 1739 | static void ww_test_object_unlock_twice(void) |
| 1740 | { |
| 1741 | WWL1(&o); |
| 1742 | WWU(&o); |
| 1743 | WWU(&o); |
| 1744 | } |
| 1745 | |
| 1746 | static void ww_test_object_lock_unbalanced(void) |
| 1747 | { |
| 1748 | WWAI(&t); |
| 1749 | WWL(&o, &t); |
| 1750 | t.acquired = 0; |
| 1751 | WWU(&o); |
| 1752 | WWAF(&t); |
| 1753 | } |
| 1754 | |
| 1755 | static void ww_test_object_lock_stale_context(void) |
| 1756 | { |
| 1757 | WWAI(&t); |
| 1758 | o.ctx = &t2; |
| 1759 | WWL(&o, &t); |
| 1760 | } |
| 1761 | |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1762 | static void ww_test_edeadlk_normal(void) |
| 1763 | { |
| 1764 | int ret; |
| 1765 | |
| 1766 | mutex_lock(&o2.base); |
| 1767 | o2.ctx = &t2; |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1768 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1769 | |
| 1770 | WWAI(&t); |
| 1771 | t2 = t; |
| 1772 | t2.stamp--; |
| 1773 | |
| 1774 | ret = WWL(&o, &t); |
| 1775 | WARN_ON(ret); |
| 1776 | |
| 1777 | ret = WWL(&o2, &t); |
| 1778 | WARN_ON(ret != -EDEADLK); |
| 1779 | |
| 1780 | o2.ctx = NULL; |
| 1781 | mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_); |
| 1782 | mutex_unlock(&o2.base); |
| 1783 | WWU(&o); |
| 1784 | |
| 1785 | WWL(&o2, &t); |
| 1786 | } |
| 1787 | |
| 1788 | static void ww_test_edeadlk_normal_slow(void) |
| 1789 | { |
| 1790 | int ret; |
| 1791 | |
| 1792 | mutex_lock(&o2.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1793 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1794 | o2.ctx = &t2; |
| 1795 | |
| 1796 | WWAI(&t); |
| 1797 | t2 = t; |
| 1798 | t2.stamp--; |
| 1799 | |
| 1800 | ret = WWL(&o, &t); |
| 1801 | WARN_ON(ret); |
| 1802 | |
| 1803 | ret = WWL(&o2, &t); |
| 1804 | WARN_ON(ret != -EDEADLK); |
| 1805 | |
| 1806 | o2.ctx = NULL; |
| 1807 | mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_); |
| 1808 | mutex_unlock(&o2.base); |
| 1809 | WWU(&o); |
| 1810 | |
| 1811 | ww_mutex_lock_slow(&o2, &t); |
| 1812 | } |
| 1813 | |
| 1814 | static void ww_test_edeadlk_no_unlock(void) |
| 1815 | { |
| 1816 | int ret; |
| 1817 | |
| 1818 | mutex_lock(&o2.base); |
| 1819 | o2.ctx = &t2; |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1820 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1821 | |
| 1822 | WWAI(&t); |
| 1823 | t2 = t; |
| 1824 | t2.stamp--; |
| 1825 | |
| 1826 | ret = WWL(&o, &t); |
| 1827 | WARN_ON(ret); |
| 1828 | |
| 1829 | ret = WWL(&o2, &t); |
| 1830 | WARN_ON(ret != -EDEADLK); |
| 1831 | |
| 1832 | o2.ctx = NULL; |
| 1833 | mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_); |
| 1834 | mutex_unlock(&o2.base); |
| 1835 | |
| 1836 | WWL(&o2, &t); |
| 1837 | } |
| 1838 | |
| 1839 | static void ww_test_edeadlk_no_unlock_slow(void) |
| 1840 | { |
| 1841 | int ret; |
| 1842 | |
| 1843 | mutex_lock(&o2.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1844 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1845 | o2.ctx = &t2; |
| 1846 | |
| 1847 | WWAI(&t); |
| 1848 | t2 = t; |
| 1849 | t2.stamp--; |
| 1850 | |
| 1851 | ret = WWL(&o, &t); |
| 1852 | WARN_ON(ret); |
| 1853 | |
| 1854 | ret = WWL(&o2, &t); |
| 1855 | WARN_ON(ret != -EDEADLK); |
| 1856 | |
| 1857 | o2.ctx = NULL; |
| 1858 | mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_); |
| 1859 | mutex_unlock(&o2.base); |
| 1860 | |
| 1861 | ww_mutex_lock_slow(&o2, &t); |
| 1862 | } |
| 1863 | |
| 1864 | static void ww_test_edeadlk_acquire_more(void) |
| 1865 | { |
| 1866 | int ret; |
| 1867 | |
| 1868 | mutex_lock(&o2.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1869 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1870 | o2.ctx = &t2; |
| 1871 | |
| 1872 | WWAI(&t); |
| 1873 | t2 = t; |
| 1874 | t2.stamp--; |
| 1875 | |
| 1876 | ret = WWL(&o, &t); |
| 1877 | WARN_ON(ret); |
| 1878 | |
| 1879 | ret = WWL(&o2, &t); |
| 1880 | WARN_ON(ret != -EDEADLK); |
| 1881 | |
| 1882 | ret = WWL(&o3, &t); |
| 1883 | } |
| 1884 | |
| 1885 | static void ww_test_edeadlk_acquire_more_slow(void) |
| 1886 | { |
| 1887 | int ret; |
| 1888 | |
| 1889 | mutex_lock(&o2.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1890 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1891 | o2.ctx = &t2; |
| 1892 | |
| 1893 | WWAI(&t); |
| 1894 | t2 = t; |
| 1895 | t2.stamp--; |
| 1896 | |
| 1897 | ret = WWL(&o, &t); |
| 1898 | WARN_ON(ret); |
| 1899 | |
| 1900 | ret = WWL(&o2, &t); |
| 1901 | WARN_ON(ret != -EDEADLK); |
| 1902 | |
| 1903 | ww_mutex_lock_slow(&o3, &t); |
| 1904 | } |
| 1905 | |
| 1906 | static void ww_test_edeadlk_acquire_more_edeadlk(void) |
| 1907 | { |
| 1908 | int ret; |
| 1909 | |
| 1910 | mutex_lock(&o2.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1911 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1912 | o2.ctx = &t2; |
| 1913 | |
| 1914 | mutex_lock(&o3.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1915 | mutex_release(&o3.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1916 | o3.ctx = &t2; |
| 1917 | |
| 1918 | WWAI(&t); |
| 1919 | t2 = t; |
| 1920 | t2.stamp--; |
| 1921 | |
| 1922 | ret = WWL(&o, &t); |
| 1923 | WARN_ON(ret); |
| 1924 | |
| 1925 | ret = WWL(&o2, &t); |
| 1926 | WARN_ON(ret != -EDEADLK); |
| 1927 | |
| 1928 | ret = WWL(&o3, &t); |
| 1929 | WARN_ON(ret != -EDEADLK); |
| 1930 | } |
| 1931 | |
| 1932 | static void ww_test_edeadlk_acquire_more_edeadlk_slow(void) |
| 1933 | { |
| 1934 | int ret; |
| 1935 | |
| 1936 | mutex_lock(&o2.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1937 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1938 | o2.ctx = &t2; |
| 1939 | |
| 1940 | mutex_lock(&o3.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1941 | mutex_release(&o3.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1942 | o3.ctx = &t2; |
| 1943 | |
| 1944 | WWAI(&t); |
| 1945 | t2 = t; |
| 1946 | t2.stamp--; |
| 1947 | |
| 1948 | ret = WWL(&o, &t); |
| 1949 | WARN_ON(ret); |
| 1950 | |
| 1951 | ret = WWL(&o2, &t); |
| 1952 | WARN_ON(ret != -EDEADLK); |
| 1953 | |
| 1954 | ww_mutex_lock_slow(&o3, &t); |
| 1955 | } |
| 1956 | |
| 1957 | static void ww_test_edeadlk_acquire_wrong(void) |
| 1958 | { |
| 1959 | int ret; |
| 1960 | |
| 1961 | mutex_lock(&o2.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1962 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1963 | o2.ctx = &t2; |
| 1964 | |
| 1965 | WWAI(&t); |
| 1966 | t2 = t; |
| 1967 | t2.stamp--; |
| 1968 | |
| 1969 | ret = WWL(&o, &t); |
| 1970 | WARN_ON(ret); |
| 1971 | |
| 1972 | ret = WWL(&o2, &t); |
| 1973 | WARN_ON(ret != -EDEADLK); |
| 1974 | if (!ret) |
| 1975 | WWU(&o2); |
| 1976 | |
| 1977 | WWU(&o); |
| 1978 | |
| 1979 | ret = WWL(&o3, &t); |
| 1980 | } |
| 1981 | |
| 1982 | static void ww_test_edeadlk_acquire_wrong_slow(void) |
| 1983 | { |
| 1984 | int ret; |
| 1985 | |
| 1986 | mutex_lock(&o2.base); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1987 | mutex_release(&o2.base.dep_map, _THIS_IP_); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 1988 | o2.ctx = &t2; |
| 1989 | |
| 1990 | WWAI(&t); |
| 1991 | t2 = t; |
| 1992 | t2.stamp--; |
| 1993 | |
| 1994 | ret = WWL(&o, &t); |
| 1995 | WARN_ON(ret); |
| 1996 | |
| 1997 | ret = WWL(&o2, &t); |
| 1998 | WARN_ON(ret != -EDEADLK); |
| 1999 | if (!ret) |
| 2000 | WWU(&o2); |
| 2001 | |
| 2002 | WWU(&o); |
| 2003 | |
| 2004 | ww_mutex_lock_slow(&o3, &t); |
| 2005 | } |
| 2006 | |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2007 | static void ww_test_spin_nest_unlocked(void) |
| 2008 | { |
| 2009 | raw_spin_lock_nest_lock(&lock_A, &o.base); |
| 2010 | U(A); |
| 2011 | } |
| 2012 | |
| 2013 | static void ww_test_unneeded_slow(void) |
| 2014 | { |
| 2015 | WWAI(&t); |
| 2016 | |
| 2017 | ww_mutex_lock_slow(&o, &t); |
| 2018 | } |
| 2019 | |
| 2020 | static void ww_test_context_block(void) |
| 2021 | { |
| 2022 | int ret; |
| 2023 | |
| 2024 | WWAI(&t); |
| 2025 | |
| 2026 | ret = WWL(&o, &t); |
| 2027 | WARN_ON(ret); |
| 2028 | WWL1(&o2); |
| 2029 | } |
| 2030 | |
| 2031 | static void ww_test_context_try(void) |
| 2032 | { |
| 2033 | int ret; |
| 2034 | |
| 2035 | WWAI(&t); |
| 2036 | |
| 2037 | ret = WWL(&o, &t); |
| 2038 | WARN_ON(ret); |
| 2039 | |
| 2040 | ret = WWT(&o2); |
| 2041 | WARN_ON(!ret); |
| 2042 | WWU(&o2); |
| 2043 | WWU(&o); |
| 2044 | } |
| 2045 | |
| 2046 | static void ww_test_context_context(void) |
| 2047 | { |
| 2048 | int ret; |
| 2049 | |
| 2050 | WWAI(&t); |
| 2051 | |
| 2052 | ret = WWL(&o, &t); |
| 2053 | WARN_ON(ret); |
| 2054 | |
| 2055 | ret = WWL(&o2, &t); |
| 2056 | WARN_ON(ret); |
| 2057 | |
| 2058 | WWU(&o2); |
| 2059 | WWU(&o); |
| 2060 | } |
| 2061 | |
| 2062 | static void ww_test_try_block(void) |
| 2063 | { |
| 2064 | bool ret; |
| 2065 | |
| 2066 | ret = WWT(&o); |
| 2067 | WARN_ON(!ret); |
| 2068 | |
| 2069 | WWL1(&o2); |
| 2070 | WWU(&o2); |
| 2071 | WWU(&o); |
| 2072 | } |
| 2073 | |
| 2074 | static void ww_test_try_try(void) |
| 2075 | { |
| 2076 | bool ret; |
| 2077 | |
| 2078 | ret = WWT(&o); |
| 2079 | WARN_ON(!ret); |
| 2080 | ret = WWT(&o2); |
| 2081 | WARN_ON(!ret); |
| 2082 | WWU(&o2); |
| 2083 | WWU(&o); |
| 2084 | } |
| 2085 | |
| 2086 | static void ww_test_try_context(void) |
| 2087 | { |
| 2088 | int ret; |
| 2089 | |
| 2090 | ret = WWT(&o); |
| 2091 | WARN_ON(!ret); |
| 2092 | |
| 2093 | WWAI(&t); |
| 2094 | |
| 2095 | ret = WWL(&o2, &t); |
| 2096 | WARN_ON(ret); |
| 2097 | } |
| 2098 | |
| 2099 | static void ww_test_block_block(void) |
| 2100 | { |
| 2101 | WWL1(&o); |
| 2102 | WWL1(&o2); |
| 2103 | } |
| 2104 | |
| 2105 | static void ww_test_block_try(void) |
| 2106 | { |
| 2107 | bool ret; |
| 2108 | |
| 2109 | WWL1(&o); |
| 2110 | ret = WWT(&o2); |
| 2111 | WARN_ON(!ret); |
| 2112 | } |
| 2113 | |
| 2114 | static void ww_test_block_context(void) |
| 2115 | { |
| 2116 | int ret; |
| 2117 | |
| 2118 | WWL1(&o); |
| 2119 | WWAI(&t); |
| 2120 | |
| 2121 | ret = WWL(&o2, &t); |
| 2122 | WARN_ON(ret); |
| 2123 | } |
| 2124 | |
| 2125 | static void ww_test_spin_block(void) |
| 2126 | { |
| 2127 | L(A); |
| 2128 | U(A); |
| 2129 | |
| 2130 | WWL1(&o); |
| 2131 | L(A); |
| 2132 | U(A); |
| 2133 | WWU(&o); |
| 2134 | |
| 2135 | L(A); |
| 2136 | WWL1(&o); |
| 2137 | WWU(&o); |
| 2138 | U(A); |
| 2139 | } |
| 2140 | |
| 2141 | static void ww_test_spin_try(void) |
| 2142 | { |
| 2143 | bool ret; |
| 2144 | |
| 2145 | L(A); |
| 2146 | U(A); |
| 2147 | |
| 2148 | ret = WWT(&o); |
| 2149 | WARN_ON(!ret); |
| 2150 | L(A); |
| 2151 | U(A); |
| 2152 | WWU(&o); |
| 2153 | |
| 2154 | L(A); |
| 2155 | ret = WWT(&o); |
| 2156 | WARN_ON(!ret); |
| 2157 | WWU(&o); |
| 2158 | U(A); |
| 2159 | } |
| 2160 | |
| 2161 | static void ww_test_spin_context(void) |
| 2162 | { |
| 2163 | int ret; |
| 2164 | |
| 2165 | L(A); |
| 2166 | U(A); |
| 2167 | |
| 2168 | WWAI(&t); |
| 2169 | |
| 2170 | ret = WWL(&o, &t); |
| 2171 | WARN_ON(ret); |
| 2172 | L(A); |
| 2173 | U(A); |
| 2174 | WWU(&o); |
| 2175 | |
| 2176 | L(A); |
| 2177 | ret = WWL(&o, &t); |
| 2178 | WARN_ON(ret); |
| 2179 | WWU(&o); |
| 2180 | U(A); |
| 2181 | } |
| 2182 | |
| 2183 | static void ww_tests(void) |
| 2184 | { |
| 2185 | printk(" --------------------------------------------------------------------------\n"); |
| 2186 | printk(" | Wound/wait tests |\n"); |
| 2187 | printk(" ---------------------\n"); |
| 2188 | |
| 2189 | print_testname("ww api failures"); |
| 2190 | dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW); |
Maarten Lankhorst | 2fe3d4b | 2013-06-20 13:31:30 +0200 | [diff] [blame] | 2191 | dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2192 | dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2193 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2194 | |
| 2195 | print_testname("ww contexts mixing"); |
| 2196 | dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW); |
| 2197 | dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2198 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2199 | |
| 2200 | print_testname("finishing ww context"); |
| 2201 | dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW); |
| 2202 | dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW); |
| 2203 | dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW); |
| 2204 | dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2205 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2206 | |
| 2207 | print_testname("locking mismatches"); |
| 2208 | dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW); |
| 2209 | dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW); |
| 2210 | dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2211 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2212 | |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 2213 | print_testname("EDEADLK handling"); |
| 2214 | dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW); |
| 2215 | dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW); |
| 2216 | dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW); |
| 2217 | dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW); |
| 2218 | dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW); |
| 2219 | dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW); |
| 2220 | dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW); |
| 2221 | dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW); |
| 2222 | dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW); |
| 2223 | dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2224 | pr_cont("\n"); |
Maarten Lankhorst | f3cf139 | 2013-06-20 13:31:42 +0200 | [diff] [blame] | 2225 | |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2226 | print_testname("spinlock nest unlocked"); |
| 2227 | dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2228 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2229 | |
| 2230 | printk(" -----------------------------------------------------\n"); |
| 2231 | printk(" |block | try |context|\n"); |
| 2232 | printk(" -----------------------------------------------------\n"); |
| 2233 | |
| 2234 | print_testname("context"); |
| 2235 | dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW); |
| 2236 | dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW); |
| 2237 | dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2238 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2239 | |
| 2240 | print_testname("try"); |
| 2241 | dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW); |
| 2242 | dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW); |
| 2243 | dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2244 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2245 | |
| 2246 | print_testname("block"); |
| 2247 | dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW); |
| 2248 | dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW); |
| 2249 | dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2250 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2251 | |
| 2252 | print_testname("spinlock"); |
| 2253 | dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW); |
| 2254 | dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW); |
| 2255 | dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2256 | pr_cont("\n"); |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2257 | } |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2258 | |
Boqun Feng | ad56450 | 2020-08-07 15:42:37 +0800 | [diff] [blame] | 2259 | |
| 2260 | /* |
| 2261 | * <in hardirq handler> |
| 2262 | * read_lock(&A); |
| 2263 | * <hardirq disable> |
| 2264 | * spin_lock(&B); |
| 2265 | * spin_lock(&B); |
| 2266 | * read_lock(&A); |
| 2267 | * |
| 2268 | * is a deadlock. |
| 2269 | */ |
| 2270 | static void queued_read_lock_hardirq_RE_Er(void) |
| 2271 | { |
| 2272 | HARDIRQ_ENTER(); |
| 2273 | read_lock(&rwlock_A); |
| 2274 | LOCK(B); |
| 2275 | UNLOCK(B); |
| 2276 | read_unlock(&rwlock_A); |
| 2277 | HARDIRQ_EXIT(); |
| 2278 | |
| 2279 | HARDIRQ_DISABLE(); |
| 2280 | LOCK(B); |
| 2281 | read_lock(&rwlock_A); |
| 2282 | read_unlock(&rwlock_A); |
| 2283 | UNLOCK(B); |
| 2284 | HARDIRQ_ENABLE(); |
| 2285 | } |
| 2286 | |
| 2287 | /* |
| 2288 | * <in hardirq handler> |
| 2289 | * spin_lock(&B); |
| 2290 | * <hardirq disable> |
| 2291 | * read_lock(&A); |
| 2292 | * read_lock(&A); |
| 2293 | * spin_lock(&B); |
| 2294 | * |
| 2295 | * is not a deadlock. |
| 2296 | */ |
| 2297 | static void queued_read_lock_hardirq_ER_rE(void) |
| 2298 | { |
| 2299 | HARDIRQ_ENTER(); |
| 2300 | LOCK(B); |
| 2301 | read_lock(&rwlock_A); |
| 2302 | read_unlock(&rwlock_A); |
| 2303 | UNLOCK(B); |
| 2304 | HARDIRQ_EXIT(); |
| 2305 | |
| 2306 | HARDIRQ_DISABLE(); |
| 2307 | read_lock(&rwlock_A); |
| 2308 | LOCK(B); |
| 2309 | UNLOCK(B); |
| 2310 | read_unlock(&rwlock_A); |
| 2311 | HARDIRQ_ENABLE(); |
| 2312 | } |
| 2313 | |
| 2314 | /* |
| 2315 | * <hardirq disable> |
| 2316 | * spin_lock(&B); |
| 2317 | * read_lock(&A); |
| 2318 | * <in hardirq handler> |
| 2319 | * spin_lock(&B); |
| 2320 | * read_lock(&A); |
| 2321 | * |
| 2322 | * is a deadlock. Because the two read_lock()s are both non-recursive readers. |
| 2323 | */ |
| 2324 | static void queued_read_lock_hardirq_inversion(void) |
| 2325 | { |
| 2326 | |
| 2327 | HARDIRQ_ENTER(); |
| 2328 | LOCK(B); |
| 2329 | UNLOCK(B); |
| 2330 | HARDIRQ_EXIT(); |
| 2331 | |
| 2332 | HARDIRQ_DISABLE(); |
| 2333 | LOCK(B); |
| 2334 | read_lock(&rwlock_A); |
| 2335 | read_unlock(&rwlock_A); |
| 2336 | UNLOCK(B); |
| 2337 | HARDIRQ_ENABLE(); |
| 2338 | |
| 2339 | read_lock(&rwlock_A); |
| 2340 | read_unlock(&rwlock_A); |
| 2341 | } |
| 2342 | |
| 2343 | static void queued_read_lock_tests(void) |
| 2344 | { |
| 2345 | printk(" --------------------------------------------------------------------------\n"); |
| 2346 | printk(" | queued read lock tests |\n"); |
| 2347 | printk(" ---------------------------\n"); |
| 2348 | print_testname("hardirq read-lock/lock-read"); |
| 2349 | dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK); |
| 2350 | pr_cont("\n"); |
| 2351 | |
| 2352 | print_testname("hardirq lock-read/read-lock"); |
| 2353 | dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK); |
| 2354 | pr_cont("\n"); |
| 2355 | |
| 2356 | print_testname("hardirq inversion"); |
| 2357 | dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK); |
| 2358 | pr_cont("\n"); |
| 2359 | } |
| 2360 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2361 | void locking_selftest(void) |
| 2362 | { |
| 2363 | /* |
| 2364 | * Got a locking failure before the selftest ran? |
| 2365 | */ |
| 2366 | if (!debug_locks) { |
| 2367 | printk("----------------------------------\n"); |
| 2368 | printk("| Locking API testsuite disabled |\n"); |
| 2369 | printk("----------------------------------\n"); |
| 2370 | return; |
| 2371 | } |
| 2372 | |
| 2373 | /* |
Boqun Feng | e918188 | 2020-08-07 15:42:20 +0800 | [diff] [blame] | 2374 | * treats read_lock() as recursive read locks for testing purpose |
| 2375 | */ |
| 2376 | force_read_lock_recursive = 1; |
| 2377 | |
| 2378 | /* |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2379 | * Run the testsuite: |
| 2380 | */ |
| 2381 | printk("------------------------\n"); |
| 2382 | printk("| Locking API testsuite:\n"); |
| 2383 | printk("----------------------------------------------------------------------------\n"); |
| 2384 | printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n"); |
| 2385 | printk(" --------------------------------------------------------------------------\n"); |
| 2386 | |
| 2387 | init_shared_classes(); |
| 2388 | debug_locks_silent = !debug_locks_verbose; |
Bart Van Assche | cdc84d7 | 2019-02-14 15:00:44 -0800 | [diff] [blame] | 2389 | lockdep_set_selftest_task(current); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2390 | |
Ingo Molnar | 6c9076e | 2006-07-03 00:24:51 -0700 | [diff] [blame] | 2391 | DO_TESTCASE_6R("A-A deadlock", AA); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2392 | DO_TESTCASE_6R("A-B-B-A deadlock", ABBA); |
| 2393 | DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA); |
| 2394 | DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC); |
| 2395 | DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA); |
| 2396 | DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA); |
| 2397 | DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA); |
| 2398 | DO_TESTCASE_6("double unlock", double_unlock); |
| 2399 | DO_TESTCASE_6("initialize held", init_held); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2400 | |
| 2401 | printk(" --------------------------------------------------------------------------\n"); |
| 2402 | print_testname("recursive read-lock"); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2403 | pr_cont(" |"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2404 | dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2405 | pr_cont(" |"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2406 | dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2407 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2408 | |
| 2409 | print_testname("recursive read-lock #2"); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2410 | pr_cont(" |"); |
Ingo Molnar | 6c9076e | 2006-07-03 00:24:51 -0700 | [diff] [blame] | 2411 | dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2412 | pr_cont(" |"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2413 | dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2414 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2415 | |
| 2416 | print_testname("mixed read-write-lock"); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2417 | pr_cont(" |"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2418 | dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2419 | pr_cont(" |"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2420 | dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2421 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2422 | |
| 2423 | print_testname("mixed write-read-lock"); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2424 | pr_cont(" |"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2425 | dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2426 | pr_cont(" |"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2427 | dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM); |
Michael Ellerman | 2513940 | 2016-11-25 09:45:28 +1100 | [diff] [blame] | 2428 | pr_cont("\n"); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2429 | |
Peter Zijlstra | e914985 | 2017-08-23 13:13:11 +0200 | [diff] [blame] | 2430 | print_testname("mixed read-lock/lock-write ABBA"); |
| 2431 | pr_cont(" |"); |
| 2432 | dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK); |
| 2433 | pr_cont(" |"); |
| 2434 | dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM); |
| 2435 | |
| 2436 | print_testname("mixed read-lock/lock-read ABBA"); |
| 2437 | pr_cont(" |"); |
| 2438 | dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK); |
| 2439 | pr_cont(" |"); |
| 2440 | dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM); |
| 2441 | |
| 2442 | print_testname("mixed write-lock/lock-write ABBA"); |
| 2443 | pr_cont(" |"); |
| 2444 | dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK); |
| 2445 | pr_cont(" |"); |
| 2446 | dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM); |
| 2447 | |
Boqun Feng | d4f200e5 | 2020-08-07 15:42:32 +0800 | [diff] [blame] | 2448 | print_testname("chain cached mixed R-L/L-W ABBA"); |
| 2449 | pr_cont(" |"); |
| 2450 | dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK); |
| 2451 | |
Boqun Feng | 8ef7ca7 | 2020-08-07 15:42:35 +0800 | [diff] [blame] | 2452 | DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1); |
| 2453 | DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1); |
| 2454 | DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1); |
| 2455 | DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1); |
| 2456 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2457 | printk(" --------------------------------------------------------------------------\n"); |
| 2458 | |
| 2459 | /* |
| 2460 | * irq-context testcases: |
| 2461 | */ |
| 2462 | DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1); |
| 2463 | DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A); |
| 2464 | DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B); |
| 2465 | DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3); |
| 2466 | DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4); |
| 2467 | DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion); |
| 2468 | |
Boqun Feng | 31e0d74 | 2020-08-07 15:42:34 +0800 | [diff] [blame] | 2469 | DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion); |
| 2470 | DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2); |
Boqun Feng | 96a16f4 | 2020-08-07 15:42:38 +0800 | [diff] [blame] | 2471 | DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2472 | |
Maarten Lankhorst | 1de9944 | 2013-06-20 13:31:24 +0200 | [diff] [blame] | 2473 | ww_tests(); |
| 2474 | |
Boqun Feng | e918188 | 2020-08-07 15:42:20 +0800 | [diff] [blame] | 2475 | force_read_lock_recursive = 0; |
| 2476 | /* |
| 2477 | * queued_read_lock() specific test cases can be put here |
| 2478 | */ |
Boqun Feng | ad56450 | 2020-08-07 15:42:37 +0800 | [diff] [blame] | 2479 | if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS)) |
| 2480 | queued_read_lock_tests(); |
Boqun Feng | e918188 | 2020-08-07 15:42:20 +0800 | [diff] [blame] | 2481 | |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2482 | if (unexpected_testcase_failures) { |
| 2483 | printk("-----------------------------------------------------------------\n"); |
| 2484 | debug_locks = 0; |
| 2485 | printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n", |
| 2486 | unexpected_testcase_failures, testcase_total); |
| 2487 | printk("-----------------------------------------------------------------\n"); |
| 2488 | } else if (expected_testcase_failures && testcase_successes) { |
| 2489 | printk("--------------------------------------------------------\n"); |
| 2490 | printk("%3d out of %3d testcases failed, as expected. |\n", |
| 2491 | expected_testcase_failures, testcase_total); |
| 2492 | printk("----------------------------------------------------\n"); |
| 2493 | debug_locks = 1; |
| 2494 | } else if (expected_testcase_failures && !testcase_successes) { |
| 2495 | printk("--------------------------------------------------------\n"); |
| 2496 | printk("All %3d testcases failed, as expected. |\n", |
| 2497 | expected_testcase_failures); |
| 2498 | printk("----------------------------------------\n"); |
| 2499 | debug_locks = 1; |
| 2500 | } else { |
| 2501 | printk("-------------------------------------------------------\n"); |
| 2502 | printk("Good, all %3d testcases passed! |\n", |
| 2503 | testcase_successes); |
| 2504 | printk("---------------------------------\n"); |
| 2505 | debug_locks = 1; |
| 2506 | } |
Bart Van Assche | cdc84d7 | 2019-02-14 15:00:44 -0800 | [diff] [blame] | 2507 | lockdep_set_selftest_task(NULL); |
Ingo Molnar | cae2ed9 | 2006-07-03 00:24:48 -0700 | [diff] [blame] | 2508 | debug_locks_silent = 0; |
| 2509 | } |