Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * lib/locking-selftest.c
  3  *
  4  * Testsuite for various locking APIs: spinlocks, rwlocks,
  5  * mutexes and rw-semaphores.
  6  *
  7  * It is checking both false positives and false negatives.
  8  *
  9  * Started by Ingo Molnar:
 10  *
 11  *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
 12  */
 13 #include <linux/rwsem.h>
 14 #include <linux/mutex.h>
 15 #include <linux/sched.h>
 16 #include <linux/delay.h>
 17 #include <linux/module.h>
 18 #include <linux/lockdep.h>
 19 #include <linux/spinlock.h>
 20 #include <linux/kallsyms.h>
 21 #include <linux/interrupt.h>
 22 #include <linux/debug_locks.h>
 23 #include <linux/irqflags.h>
 24 
 25 /*
 26  * Change this to 1 if you want to see the failure printouts:
 27  */
 28 static unsigned int debug_locks_verbose;
 29 
 30 static int __init setup_debug_locks_verbose(char *str)
 31 {
 32         get_option(&str, &debug_locks_verbose);
 33 
 34         return 1;
 35 }
 36 
 37 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
 38 
 39 #define FAILURE         0
 40 #define SUCCESS         1
 41 
 42 #define LOCKTYPE_SPIN   0x1
 43 #define LOCKTYPE_RWLOCK 0x2
 44 #define LOCKTYPE_MUTEX  0x4
 45 #define LOCKTYPE_RWSEM  0x8
 46 
 47 /*
 48  * Normal standalone locks, for the circular and irq-context
 49  * dependency tests:
 50  */
 51 static DEFINE_SPINLOCK(lock_A);
 52 static DEFINE_SPINLOCK(lock_B);
 53 static DEFINE_SPINLOCK(lock_C);
 54 static DEFINE_SPINLOCK(lock_D);
 55 
 56 static DEFINE_RWLOCK(rwlock_A);
 57 static DEFINE_RWLOCK(rwlock_B);
 58 static DEFINE_RWLOCK(rwlock_C);
 59 static DEFINE_RWLOCK(rwlock_D);
 60 
 61 static DEFINE_MUTEX(mutex_A);
 62 static DEFINE_MUTEX(mutex_B);
 63 static DEFINE_MUTEX(mutex_C);
 64 static DEFINE_MUTEX(mutex_D);
 65 
 66 static DECLARE_RWSEM(rwsem_A);
 67 static DECLARE_RWSEM(rwsem_B);
 68 static DECLARE_RWSEM(rwsem_C);
 69 static DECLARE_RWSEM(rwsem_D);
 70 
 71 /*
 72  * Locks that we initialize dynamically as well so that
 73  * e.g. X1 and X2 becomes two instances of the same class,
 74  * but X* and Y* are different classes. We do this so that
 75  * we do not trigger a real lockup:
 76  */
 77 static DEFINE_SPINLOCK(lock_X1);
 78 static DEFINE_SPINLOCK(lock_X2);
 79 static DEFINE_SPINLOCK(lock_Y1);
 80 static DEFINE_SPINLOCK(lock_Y2);
 81 static DEFINE_SPINLOCK(lock_Z1);
 82 static DEFINE_SPINLOCK(lock_Z2);
 83 
 84 static DEFINE_RWLOCK(rwlock_X1);
 85 static DEFINE_RWLOCK(rwlock_X2);
 86 static DEFINE_RWLOCK(rwlock_Y1);
 87 static DEFINE_RWLOCK(rwlock_Y2);
 88 static DEFINE_RWLOCK(rwlock_Z1);
 89 static DEFINE_RWLOCK(rwlock_Z2);
 90 
 91 static DEFINE_MUTEX(mutex_X1);
 92 static DEFINE_MUTEX(mutex_X2);
 93 static DEFINE_MUTEX(mutex_Y1);
 94 static DEFINE_MUTEX(mutex_Y2);
 95 static DEFINE_MUTEX(mutex_Z1);
 96 static DEFINE_MUTEX(mutex_Z2);
 97 
 98 static DECLARE_RWSEM(rwsem_X1);
 99 static DECLARE_RWSEM(rwsem_X2);
100 static DECLARE_RWSEM(rwsem_Y1);
101 static DECLARE_RWSEM(rwsem_Y2);
102 static DECLARE_RWSEM(rwsem_Z1);
103 static DECLARE_RWSEM(rwsem_Z2);
104 
105 /*
106  * non-inlined runtime initializers, to let separate locks share
107  * the same lock-class:
108  */
109 #define INIT_CLASS_FUNC(class)                          \
110 static noinline void                                    \
111 init_class_##class(spinlock_t *lock, rwlock_t *rwlock, struct mutex *mutex, \
112                  struct rw_semaphore *rwsem)            \
113 {                                                       \
114         spin_lock_init(lock);                           \
115         rwlock_init(rwlock);                            \
116         mutex_init(mutex);                              \
117         init_rwsem(rwsem);                              \
118 }
119 
120 INIT_CLASS_FUNC(X)
121 INIT_CLASS_FUNC(Y)
122 INIT_CLASS_FUNC(Z)
123 
124 static void init_shared_classes(void)
125 {
126         init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
127         init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
128 
129         init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
130         init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
131 
132         init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
133         init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
134 }
135 
136 /*
137  * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
138  * The following functions use a lock from a simulated hardirq/softirq
139  * context, causing the locks to be marked as hardirq-safe/softirq-safe:
140  */
141 
142 #define HARDIRQ_DISABLE         local_irq_disable
143 #define HARDIRQ_ENABLE          local_irq_enable
144 
145 #define HARDIRQ_ENTER()                         \
146         local_irq_disable();                    \
147         irq_enter();                            \
148         WARN_ON(!in_irq());
149 
150 #define HARDIRQ_EXIT()                          \
151         __irq_exit();                           \
152         local_irq_enable();
153 
154 #define SOFTIRQ_DISABLE         local_bh_disable
155 #define SOFTIRQ_ENABLE          local_bh_enable
156 
157 #define SOFTIRQ_ENTER()                         \
158                 local_bh_disable();             \
159                 local_irq_disable();            \
160                 trace_softirq_enter();          \
161                 /* FIXME: preemptible softirqs. WARN_ON(!in_softirq()); */
162 
163 #define SOFTIRQ_EXIT()                          \
164                 trace_softirq_exit();           \
165                 local_irq_enable();             \
166                 local_bh_enable();
167 
168 /*
169  * Shortcuts for lock/unlock API variants, to keep
170  * the testcases compact:
171  */
172 #define L(x)                    spin_lock(&lock_##x)
173 #define U(x)                    spin_unlock(&lock_##x)
174 #define LU(x)                   L(x); U(x)
175 #define SI(x)                   spin_lock_init(&lock_##x)
176 
177 #define WL(x)                   write_lock(&rwlock_##x)
178 #define WU(x)                   write_unlock(&rwlock_##x)
179 #define WLU(x)                  WL(x); WU(x)
180 
181 #define RL(x)                   read_lock(&rwlock_##x)
182 #define RU(x)                   read_unlock(&rwlock_##x)
183 #define RLU(x)                  RL(x); RU(x)
184 #define RWI(x)                  rwlock_init(&rwlock_##x)
185 
186 #define ML(x)                   mutex_lock(&mutex_##x)
187 #define MU(x)                   mutex_unlock(&mutex_##x)
188 #define MI(x)                   mutex_init(&mutex_##x)
189 
190 #define WSL(x)                  down_write(&rwsem_##x)
191 #define WSU(x)                  up_write(&rwsem_##x)
192 
193 #define RSL(x)                  down_read(&rwsem_##x)
194 #define RSU(x)                  up_read(&rwsem_##x)
195 #define RWSI(x)                 init_rwsem(&rwsem_##x)
196 
197 #define LOCK_UNLOCK_2(x,y)      LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
198 
199 /*
200  * Generate different permutations of the same testcase, using
201  * the same basic lock-dependency/state events:
202  */
203 
204 #define GENERATE_TESTCASE(name)                 \
205                                                 \
206 static void name(void) { E(); }
207 
208 #define GENERATE_PERMUTATIONS_2_EVENTS(name)    \
209                                                 \
210 static void name##_12(void) { E1(); E2(); }     \
211 static void name##_21(void) { E2(); E1(); }
212 
213 #define GENERATE_PERMUTATIONS_3_EVENTS(name)            \
214                                                         \
215 static void name##_123(void) { E1(); E2(); E3(); }      \
216 static void name##_132(void) { E1(); E3(); E2(); }      \
217 static void name##_213(void) { E2(); E1(); E3(); }      \
218 static void name##_231(void) { E2(); E3(); E1(); }      \
219 static void name##_312(void) { E3(); E1(); E2(); }      \
220 static void name##_321(void) { E3(); E2(); E1(); }
221 
222 /*
223  * AA deadlock:
224  */
225 
226 #define E()                                     \
227                                                 \
228         LOCK(X1);                               \
229         LOCK(X2); /* this one should fail */
230 
231 /*
232  * 6 testcases:
233  */
234 #include "locking-selftest-spin.h"
235 GENERATE_TESTCASE(AA_spin)
236 #include "locking-selftest-wlock.h"
237 GENERATE_TESTCASE(AA_wlock)
238 #include "locking-selftest-rlock.h"
239 GENERATE_TESTCASE(AA_rlock)
240 #include "locking-selftest-mutex.h"
241 GENERATE_TESTCASE(AA_mutex)
242 #include "locking-selftest-wsem.h"
243 GENERATE_TESTCASE(AA_wsem)
244 #include "locking-selftest-rsem.h"
245 GENERATE_TESTCASE(AA_rsem)
246 
247 #undef E
248 
249 /*
250  * Special-case for read-locking, they are
251  * allowed to recurse on the same lock class:
252  */
253 static void rlock_AA1(void)
254 {
255         RL(X1);
256         RL(X1); // this one should NOT fail
257 }
258 
259 static void rlock_AA1B(void)
260 {
261         RL(X1);
262         RL(X2); // this one should NOT fail
263 }
264 
265 static void rsem_AA1(void)
266 {
267         RSL(X1);
268         RSL(X1); // this one should fail
269 }
270 
271 static void rsem_AA1B(void)
272 {
273         RSL(X1);
274         RSL(X2); // this one should fail
275 }
276 /*
277  * The mixing of read and write locks is not allowed:
278  */
279 static void rlock_AA2(void)
280 {
281         RL(X1);
282         WL(X2); // this one should fail
283 }
284 
285 static void rsem_AA2(void)
286 {
287         RSL(X1);
288         WSL(X2); // this one should fail
289 }
290 
291 static void rlock_AA3(void)
292 {
293         WL(X1);
294         RL(X2); // this one should fail
295 }
296 
297 static void rsem_AA3(void)
298 {
299         WSL(X1);
300         RSL(X2); // this one should fail
301 }
302 
303 /*
304  * ABBA deadlock:
305  */
306 
307 #define E()                                     \
308                                                 \
309         LOCK_UNLOCK_2(A, B);                    \
310         LOCK_UNLOCK_2(B, A); /* fail */
311 
312 /*
313  * 6 testcases:
314  */
315 #include "locking-selftest-spin.h"
316 GENERATE_TESTCASE(ABBA_spin)
317 #include "locking-selftest-wlock.h"
318 GENERATE_TESTCASE(ABBA_wlock)
319 #include "locking-selftest-rlock.h"
320 GENERATE_TESTCASE(ABBA_rlock)
321 #include "locking-selftest-mutex.h"
322 GENERATE_TESTCASE(ABBA_mutex)
323 #include "locking-selftest-wsem.h"
324 GENERATE_TESTCASE(ABBA_wsem)
325 #include "locking-selftest-rsem.h"
326 GENERATE_TESTCASE(ABBA_rsem)
327 
328 #undef E
329 
330 /*
331  * AB BC CA deadlock:
332  */
333 
334 #define E()                                     \
335                                                 \
336         LOCK_UNLOCK_2(A, B);                    \
337         LOCK_UNLOCK_2(B, C);                    \
338         LOCK_UNLOCK_2(C, A); /* fail */
339 
340 /*
341  * 6 testcases:
342  */
343 #include "locking-selftest-spin.h"
344 GENERATE_TESTCASE(ABBCCA_spin)
345 #include "locking-selftest-wlock.h"
346 GENERATE_TESTCASE(ABBCCA_wlock)
347 #include "locking-selftest-rlock.h"
348 GENERATE_TESTCASE(ABBCCA_rlock)
349 #include "locking-selftest-mutex.h"
350 GENERATE_TESTCASE(ABBCCA_mutex)
351 #include "locking-selftest-wsem.h"
352 GENERATE_TESTCASE(ABBCCA_wsem)
353 #include "locking-selftest-rsem.h"
354 GENERATE_TESTCASE(ABBCCA_rsem)
355 
356 #undef E
357 
358 /*
359  * AB CA BC deadlock:
360  */
361 
362 #define E()                                     \
363                                                 \
364         LOCK_UNLOCK_2(A, B);                    \
365         LOCK_UNLOCK_2(C, A);                    \
366         LOCK_UNLOCK_2(B, C); /* fail */
367 
368 /*
369  * 6 testcases:
370  */
371 #include "locking-selftest-spin.h"
372 GENERATE_TESTCASE(ABCABC_spin)
373 #include "locking-selftest-wlock.h"
374 GENERATE_TESTCASE(ABCABC_wlock)
375 #include "locking-selftest-rlock.h"
376 GENERATE_TESTCASE(ABCABC_rlock)
377 #include "locking-selftest-mutex.h"
378 GENERATE_TESTCASE(ABCABC_mutex)
379 #include "locking-selftest-wsem.h"
380 GENERATE_TESTCASE(ABCABC_wsem)
381 #include "locking-selftest-rsem.h"
382 GENERATE_TESTCASE(ABCABC_rsem)
383 
384 #undef E
385 
386 /*
387  * AB BC CD DA deadlock:
388  */
389 
390 #define E()                                     \
391                                                 \
392         LOCK_UNLOCK_2(A, B);                    \
393         LOCK_UNLOCK_2(B, C);                    \
394         LOCK_UNLOCK_2(C, D);                    \
395         LOCK_UNLOCK_2(D, A); /* fail */
396 
397 /*
398  * 6 testcases:
399  */
400 #include "locking-selftest-spin.h"
401 GENERATE_TESTCASE(ABBCCDDA_spin)
402 #include "locking-selftest-wlock.h"
403 GENERATE_TESTCASE(ABBCCDDA_wlock)
404 #include "locking-selftest-rlock.h"
405 GENERATE_TESTCASE(ABBCCDDA_rlock)
406 #include "locking-selftest-mutex.h"
407 GENERATE_TESTCASE(ABBCCDDA_mutex)
408 #include "locking-selftest-wsem.h"
409 GENERATE_TESTCASE(ABBCCDDA_wsem)
410 #include "locking-selftest-rsem.h"
411 GENERATE_TESTCASE(ABBCCDDA_rsem)
412 
413 #undef E
414 
415 /*
416  * AB CD BD DA deadlock:
417  */
418 #define E()                                     \
419                                                 \
420         LOCK_UNLOCK_2(A, B);                    \
421         LOCK_UNLOCK_2(C, D);                    \
422         LOCK_UNLOCK_2(B, D);                    \
423         LOCK_UNLOCK_2(D, A); /* fail */
424 
425 /*
426  * 6 testcases:
427  */
428 #include "locking-selftest-spin.h"
429 GENERATE_TESTCASE(ABCDBDDA_spin)
430 #include "locking-selftest-wlock.h"
431 GENERATE_TESTCASE(ABCDBDDA_wlock)
432 #include "locking-selftest-rlock.h"
433 GENERATE_TESTCASE(ABCDBDDA_rlock)
434 #include "locking-selftest-mutex.h"
435 GENERATE_TESTCASE(ABCDBDDA_mutex)
436 #include "locking-selftest-wsem.h"
437 GENERATE_TESTCASE(ABCDBDDA_wsem)
438 #include "locking-selftest-rsem.h"
439 GENERATE_TESTCASE(ABCDBDDA_rsem)
440 
441 #undef E
442 
443 /*
444  * AB CD BC DA deadlock:
445  */
446 #define E()                                     \
447                                                 \
448         LOCK_UNLOCK_2(A, B);                    \
449         LOCK_UNLOCK_2(C, D);                    \
450         LOCK_UNLOCK_2(B, C);                    \
451         LOCK_UNLOCK_2(D, A); /* fail */
452 
453 /*
454  * 6 testcases:
455  */
456 #include "locking-selftest-spin.h"
457 GENERATE_TESTCASE(ABCDBCDA_spin)
458 #include "locking-selftest-wlock.h"
459 GENERATE_TESTCASE(ABCDBCDA_wlock)
460 #include "locking-selftest-rlock.h"
461 GENERATE_TESTCASE(ABCDBCDA_rlock)
462 #include "locking-selftest-mutex.h"
463 GENERATE_TESTCASE(ABCDBCDA_mutex)
464 #include "locking-selftest-wsem.h"
465 GENERATE_TESTCASE(ABCDBCDA_wsem)
466 #include "locking-selftest-rsem.h"
467 GENERATE_TESTCASE(ABCDBCDA_rsem)
468 
469 #undef E
470 
471 /*
472  * Double unlock:
473  */
474 #define E()                                     \
475                                                 \
476         LOCK(A);                                \
477         UNLOCK(A);                              \
478         UNLOCK(A); /* fail */
479 
480 /*
481  * 6 testcases:
482  */
483 #include "locking-selftest-spin.h"
484 GENERATE_TESTCASE(double_unlock_spin)
485 #include "locking-selftest-wlock.h"
486 GENERATE_TESTCASE(double_unlock_wlock)
487 #include "locking-selftest-rlock.h"
488 GENERATE_TESTCASE(double_unlock_rlock)
489 #include "locking-selftest-mutex.h"
490 GENERATE_TESTCASE(double_unlock_mutex)
491 #include "locking-selftest-wsem.h"
492 GENERATE_TESTCASE(double_unlock_wsem)
493 #include "locking-selftest-rsem.h"
494 GENERATE_TESTCASE(double_unlock_rsem)
495 
496 #undef E
497 
498 /*
499  * Bad unlock ordering:
500  */
501 #define E()                                     \
502                                                 \
503         LOCK(A);                                \
504         LOCK(B);                                \
505         UNLOCK(A); /* fail */                   \
506         UNLOCK(B);
507 
508 /*
509  * 6 testcases:
510  */
511 #include "locking-selftest-spin.h"
512 GENERATE_TESTCASE(bad_unlock_order_spin)
513 #include "locking-selftest-wlock.h"
514 GENERATE_TESTCASE(bad_unlock_order_wlock)
515 #include "locking-selftest-rlock.h"
516 GENERATE_TESTCASE(bad_unlock_order_rlock)
517 #include "locking-selftest-mutex.h"
518 GENERATE_TESTCASE(bad_unlock_order_mutex)
519 #include "locking-selftest-wsem.h"
520 GENERATE_TESTCASE(bad_unlock_order_wsem)
521 #include "locking-selftest-rsem.h"
522 GENERATE_TESTCASE(bad_unlock_order_rsem)
523 
524 #undef E
525 
526 /*
527  * initializing a held lock:
528  */
529 #define E()                                     \
530                                                 \
531         LOCK(A);                                \
532         INIT(A); /* fail */
533 
534 /*
535  * 6 testcases:
536  */
537 #include "locking-selftest-spin.h"
538 GENERATE_TESTCASE(init_held_spin)
539 #include "locking-selftest-wlock.h"
540 GENERATE_TESTCASE(init_held_wlock)
541 #include "locking-selftest-rlock.h"
542 GENERATE_TESTCASE(init_held_rlock)
543 #include "locking-selftest-mutex.h"
544 GENERATE_TESTCASE(init_held_mutex)
545 #include "locking-selftest-wsem.h"
546 GENERATE_TESTCASE(init_held_wsem)
547 #include "locking-selftest-rsem.h"
548 GENERATE_TESTCASE(init_held_rsem)
549 
550 #undef E
551 
552 /*
553  * FIXME: turns these into raw-spinlock tests on -rt
554  */
555 #ifndef CONFIG_PREEMPT_RT
556 
557 /*
558  * locking an irq-safe lock with irqs enabled:
559  */
560 #define E1()                            \
561                                         \
562         IRQ_ENTER();                    \
563         LOCK(A);                        \
564         UNLOCK(A);                      \
565         IRQ_EXIT();
566 
567 #define E2()                            \
568                                         \
569         LOCK(A);                        \
570         UNLOCK(A);
571 
572 /*
573  * Generate 24 testcases:
574  */
575 #include "locking-selftest-spin-hardirq.h"
576 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
577 
578 #include "locking-selftest-rlock-hardirq.h"
579 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
580 
581 #include "locking-selftest-wlock-hardirq.h"
582 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
583 
584 #include "locking-selftest-spin-softirq.h"
585 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
586 
587 #include "locking-selftest-rlock-softirq.h"
588 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
589 
590 #include "locking-selftest-wlock-softirq.h"
591 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
592 
593 #undef E1
594 #undef E2
595 
596 /*
597  * Enabling hardirqs with a softirq-safe lock held:
598  */
599 #define E1()                            \
600                                         \
601         SOFTIRQ_ENTER();                \
602         LOCK(A);                        \
603         UNLOCK(A);                      \
604         SOFTIRQ_EXIT();
605 
606 #define E2()                            \
607                                         \
608         HARDIRQ_DISABLE();              \
609         LOCK(A);                        \
610         HARDIRQ_ENABLE();               \
611         UNLOCK(A);
612 
613 /*
614  * Generate 12 testcases:
615  */
616 #include "locking-selftest-spin.h"
617 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
618 
619 #include "locking-selftest-wlock.h"
620 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
621 
622 #include "locking-selftest-rlock.h"
623 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
624 
625 #undef E1
626 #undef E2
627 
628 /*
629  * Enabling irqs with an irq-safe lock held:
630  */
631 #define E1()                            \
632                                         \
633         IRQ_ENTER();                    \
634         LOCK(A);                        \
635         UNLOCK(A);                      \
636         IRQ_EXIT();
637 
638 #define E2()                            \
639                                         \
640         IRQ_DISABLE();                  \
641         LOCK(A);                        \
642         IRQ_ENABLE();                   \
643         UNLOCK(A);
644 
645 /*
646  * Generate 24 testcases:
647  */
648 #include "locking-selftest-spin-hardirq.h"
649 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
650 
651 #include "locking-selftest-rlock-hardirq.h"
652 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
653 
654 #include "locking-selftest-wlock-hardirq.h"
655 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
656 
657 #include "locking-selftest-spin-softirq.h"
658 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
659 
660 #include "locking-selftest-rlock-softirq.h"
661 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
662 
663 #include "locking-selftest-wlock-softirq.h"
664 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
665 
666 #undef E1
667 #undef E2
668 
669 /*
670  * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
671  */
672 #define E1()                            \
673                                         \
674         LOCK(A);                        \
675         LOCK(B);                        \
676         UNLOCK(B);                      \
677         UNLOCK(A);                      \
678 
679 #define E2()                            \
680                                         \
681         LOCK(B);                        \
682         UNLOCK(B);
683 
684 #define E3()                            \
685                                         \
686         IRQ_ENTER();                    \
687         LOCK(A);                        \
688         UNLOCK(A);                      \
689         IRQ_EXIT();
690 
691 /*
692  * Generate 36 testcases:
693  */
694 #include "locking-selftest-spin-hardirq.h"
695 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
696 
697 #include "locking-selftest-rlock-hardirq.h"
698 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
699 
700 #include "locking-selftest-wlock-hardirq.h"
701 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
702 
703 #include "locking-selftest-spin-softirq.h"
704 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
705 
706 #include "locking-selftest-rlock-softirq.h"
707 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
708 
709 #include "locking-selftest-wlock-softirq.h"
710 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
711 
712 #undef E1
713 #undef E2
714 #undef E3
715 
716 /*
717  * If a lock turns into softirq-safe, but earlier it took
718  * a softirq-unsafe lock:
719  */
720 
721 #define E1()                            \
722         IRQ_DISABLE();                  \
723         LOCK(A);                        \
724         LOCK(B);                        \
725         UNLOCK(B);                      \
726         UNLOCK(A);                      \
727         IRQ_ENABLE();
728 
729 #define E2()                            \
730         LOCK(B);                        \
731         UNLOCK(B);
732 
733 #define E3()                            \
734         IRQ_ENTER();                    \
735         LOCK(A);                        \
736         UNLOCK(A);                      \
737         IRQ_EXIT();
738 
739 /*
740  * Generate 36 testcases:
741  */
742 #include "locking-selftest-spin-hardirq.h"
743 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
744 
745 #include "locking-selftest-rlock-hardirq.h"
746 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
747 
748 #include "locking-selftest-wlock-hardirq.h"
749 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
750 
751 #include "locking-selftest-spin-softirq.h"
752 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
753 
754 #include "locking-selftest-rlock-softirq.h"
755 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
756 
757 #include "locking-selftest-wlock-softirq.h"
758 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
759 
760 #undef E1
761 #undef E2
762 #undef E3
763 
764 /*
765  * read-lock / write-lock irq inversion.
766  *
767  * Deadlock scenario:
768  *
769  * CPU#1 is at #1, i.e. it has write-locked A, but has not
770  * taken B yet.
771  *
772  * CPU#2 is at #2, i.e. it has locked B.
773  *
774  * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
775  *
776  * The deadlock occurs because CPU#1 will spin on B, and CPU#2
777  * will spin on A.
778  */
779 
780 #define E1()                            \
781                                         \
782         IRQ_DISABLE();                  \
783         WL(A);                          \
784         LOCK(B);                        \
785         UNLOCK(B);                      \
786         WU(A);                          \
787         IRQ_ENABLE();
788 
789 #define E2()                            \
790                                         \
791         LOCK(B);                        \
792         UNLOCK(B);
793 
794 #define E3()                            \
795                                         \
796         IRQ_ENTER();                    \
797         RL(A);                          \
798         RU(A);                          \
799         IRQ_EXIT();
800 
801 /*
802  * Generate 36 testcases:
803  */
804 #include "locking-selftest-spin-hardirq.h"
805 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
806 
807 #include "locking-selftest-rlock-hardirq.h"
808 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
809 
810 #include "locking-selftest-wlock-hardirq.h"
811 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
812 
813 #include "locking-selftest-spin-softirq.h"
814 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
815 
816 #include "locking-selftest-rlock-softirq.h"
817 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
818 
819 #include "locking-selftest-wlock-softirq.h"
820 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
821 
822 #undef E1
823 #undef E2
824 #undef E3
825 
826 /*
827  * read-lock / write-lock recursion that is actually safe.
828  */
829 
830 #define E1()                            \
831                                         \
832         IRQ_DISABLE();                  \
833         WL(A);                          \
834         WU(A);                          \
835         IRQ_ENABLE();
836 
837 #define E2()                            \
838                                         \
839         RL(A);                          \
840         RU(A);                          \
841 
842 #define E3()                            \
843                                         \
844         IRQ_ENTER();                    \
845         RL(A);                          \
846         L(B);                           \
847         U(B);                           \
848         RU(A);                          \
849         IRQ_EXIT();
850 
851 /*
852  * Generate 12 testcases:
853  */
854 #include "locking-selftest-hardirq.h"
855 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
856 
857 #include "locking-selftest-softirq.h"
858 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
859 
860 #undef E1
861 #undef E2
862 #undef E3
863 
864 /*
865  * read-lock / write-lock recursion that is unsafe.
866  */
867 
868 #define E1()                            \
869                                         \
870         IRQ_DISABLE();                  \
871         L(B);                           \
872         WL(A);                          \
873         WU(A);                          \
874         U(B);                           \
875         IRQ_ENABLE();
876 
877 #define E2()                            \
878                                         \
879         RL(A);                          \
880         RU(A);                          \
881 
882 #define E3()                            \
883                                         \
884         IRQ_ENTER();                    \
885         L(B);                           \
886         U(B);                           \
887         IRQ_EXIT();
888 
889 /*
890  * Generate 12 testcases:
891  */
892 #include "locking-selftest-hardirq.h"
893 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
894 
895 #include "locking-selftest-softirq.h"
896 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
897 
898 #endif /* !CONFIG_PREEMPT_RT */
899 
900 #ifdef CONFIG_DEBUG_LOCK_ALLOC
901 # define I_SPINLOCK(x)  lockdep_reset_lock(&lock_##x.dep_map)
902 # define I_RWLOCK(x)    lockdep_reset_lock(&rwlock_##x.dep_map)
903 # define I_MUTEX(x)     lockdep_reset_lock(&mutex_##x.dep_map)
904 # define I_RWSEM(x)     lockdep_reset_lock(&rwsem_##x.dep_map)
905 #else
906 # define I_SPINLOCK(x)
907 # define I_RWLOCK(x)
908 # define I_MUTEX(x)
909 # define I_RWSEM(x)
910 #endif
911 
912 #define I1(x)                                   \
913         do {                                    \
914                 I_SPINLOCK(x);                  \
915                 I_RWLOCK(x);                    \
916                 I_MUTEX(x);                     \
917                 I_RWSEM(x);                     \
918         } while (0)
919 
920 #define I2(x)                                   \
921         do {                                    \
922                 spin_lock_init(&lock_##x);      \
923                 rwlock_init(&rwlock_##x);       \
924                 mutex_init(&mutex_##x);         \
925                 init_rwsem(&rwsem_##x);         \
926         } while (0)
927 
928 static void reset_locks(void)
929 {
930         local_irq_disable();
931         I1(A); I1(B); I1(C); I1(D);
932         I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
933         lockdep_reset();
934         I2(A); I2(B); I2(C); I2(D);
935         init_shared_classes();
936         local_irq_enable();
937 }
938 
939 #undef I
940 
941 static int testcase_total;
942 static int testcase_successes;
943 static int expected_testcase_failures;
944 static int unexpected_testcase_failures;
945 
946 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
947 {
948         unsigned long saved_preempt_count = preempt_count();
949         int expected_failure = 0;
950 #if defined(CONFIG_DEBUG_PREEMPT) && defined(CONFIG_DEBUG_RT_MUTEXES)
951         int saved_lock_count = current->lock_count;
952 #endif
953 
954         WARN_ON(irqs_disabled());
955 
956         testcase_fn();
957         /*
958          * Filter out expected failures:
959          */
960 #ifndef CONFIG_PROVE_LOCKING
961         if ((lockclass_mask & LOCKTYPE_SPIN) && debug_locks != expected)
962                 expected_failure = 1;
963         if ((lockclass_mask & LOCKTYPE_RWLOCK) && debug_locks != expected)
964                 expected_failure = 1;
965         if ((lockclass_mask & LOCKTYPE_MUTEX) && debug_locks != expected)
966                 expected_failure = 1;
967         if ((lockclass_mask & LOCKTYPE_RWSEM) && debug_locks != expected)
968                 expected_failure = 1;
969 #endif
970         if (debug_locks != expected) {
971                 if (expected_failure) {
972                         expected_testcase_failures++;
973                         printk("failed|");
974                 } else {
975                         unexpected_testcase_failures++;
976 
977                         printk("FAILED|");
978                         dump_stack();
979                 }
980         } else {
981                 testcase_successes++;
982                 printk("  ok  |");
983         }
984         testcase_total++;
985 
986         if (debug_locks_verbose)
987                 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
988                         lockclass_mask, debug_locks, expected);
989         /*
990          * Some tests (e.g. double-unlock) might corrupt the preemption
991          * count, so restore it:
992          */
993         preempt_count() = saved_preempt_count;
994 #ifdef CONFIG_TRACE_IRQFLAGS
995         if (softirq_count())
996                 current->softirqs_enabled = 0;
997         else
998                 current->softirqs_enabled = 1;
999 #endif
1000 
1001         reset_locks();
1002 #if defined(CONFIG_DEBUG_PREEMPT) && defined(CONFIG_DEBUG_RT_MUTEXES)
1003         current->lock_count = saved_lock_count;
1004 #endif
1005 }
1006 
1007 static inline void print_testname(const char *testname)
1008 {
1009         printk("%33s:", testname);
1010 }
1011 
1012 #define DO_TESTCASE_1(desc, name, nr)                           \
1013         print_testname(desc"/"#nr);                             \
1014         dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK);  \
1015         printk("\n");
1016 
1017 #define DO_TESTCASE_1B(desc, name, nr)                          \
1018         print_testname(desc"/"#nr);                             \
1019         dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);          \
1020         printk("\n");
1021 
1022 #define DO_TESTCASE_3(desc, name, nr)                                   \
1023         print_testname(desc"/"#nr);                                     \
1024         dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN);               \
1025         dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);            \
1026         dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);    \
1027         printk("\n");
1028 
1029 #define DO_TESTCASE_3RW(desc, name, nr)                                 \
1030         print_testname(desc"/"#nr);                                     \
1031         dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1032         dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);            \
1033         dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);    \
1034         printk("\n");
1035 
1036 #define DO_TESTCASE_6(desc, name)                               \
1037         print_testname(desc);                                   \
1038         dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);            \
1039         dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);         \
1040         dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK);         \
1041         dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);          \
1042         dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);           \
1043         dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);           \
1044         printk("\n");
1045 
1046 #define DO_TESTCASE_6_SUCCESS(desc, name)                       \
1047         print_testname(desc);                                   \
1048         dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN);            \
1049         dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK);         \
1050         dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);         \
1051         dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX);          \
1052         dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM);           \
1053         dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM);           \
1054         printk("\n");
1055 
1056 /*
1057  * 'read' variant: rlocks must not trigger.
1058  */
1059 #define DO_TESTCASE_6R(desc, name)                              \
1060         print_testname(desc);                                   \
1061         dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);            \
1062         dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);         \
1063         dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1064         dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);          \
1065         dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);           \
1066         dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);           \
1067         printk("\n");
1068 
1069 #define DO_TESTCASE_2I(desc, name, nr)                          \
1070         DO_TESTCASE_1("hard-"desc, name##_hard, nr);            \
1071         DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1072 
1073 #define DO_TESTCASE_2IB(desc, name, nr)                         \
1074         DO_TESTCASE_1B("hard-"desc, name##_hard, nr);           \
1075         DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1076 
1077 #define DO_TESTCASE_6I(desc, name, nr)                          \
1078         DO_TESTCASE_3("hard-"desc, name##_hard, nr);            \
1079         DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1080 
1081 #define DO_TESTCASE_6IRW(desc, name, nr)                        \
1082         DO_TESTCASE_3RW("hard-"desc, name##_hard, nr);          \
1083         DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1084 
1085 #define DO_TESTCASE_2x3(desc, name)                             \
1086         DO_TESTCASE_3(desc, name, 12);                          \
1087         DO_TESTCASE_3(desc, name, 21);
1088 
1089 #define DO_TESTCASE_2x6(desc, name)                             \
1090         DO_TESTCASE_6I(desc, name, 12);                         \
1091         DO_TESTCASE_6I(desc, name, 21);
1092 
1093 #define DO_TESTCASE_6x2(desc, name)                             \
1094         DO_TESTCASE_2I(desc, name, 123);                        \
1095         DO_TESTCASE_2I(desc, name, 132);                        \
1096         DO_TESTCASE_2I(desc, name, 213);                        \
1097         DO_TESTCASE_2I(desc, name, 231);                        \
1098         DO_TESTCASE_2I(desc, name, 312);                        \
1099         DO_TESTCASE_2I(desc, name, 321);
1100 
1101 #define DO_TESTCASE_6x2B(desc, name)                            \
1102         DO_TESTCASE_2IB(desc, name, 123);                       \
1103         DO_TESTCASE_2IB(desc, name, 132);                       \
1104         DO_TESTCASE_2IB(desc, name, 213);                       \
1105         DO_TESTCASE_2IB(desc, name, 231);                       \
1106         DO_TESTCASE_2IB(desc, name, 312);                       \
1107         DO_TESTCASE_2IB(desc, name, 321);
1108 
1109 #define DO_TESTCASE_6x6(desc, name)                             \
1110         DO_TESTCASE_6I(desc, name, 123);                        \
1111         DO_TESTCASE_6I(desc, name, 132);                        \
1112         DO_TESTCASE_6I(desc, name, 213);                        \
1113         DO_TESTCASE_6I(desc, name, 231);                        \
1114         DO_TESTCASE_6I(desc, name, 312);                        \
1115         DO_TESTCASE_6I(desc, name, 321);
1116 
1117 #define DO_TESTCASE_6x6RW(desc, name)                           \
1118         DO_TESTCASE_6IRW(desc, name, 123);                      \
1119         DO_TESTCASE_6IRW(desc, name, 132);                      \
1120         DO_TESTCASE_6IRW(desc, name, 213);                      \
1121         DO_TESTCASE_6IRW(desc, name, 231);                      \
1122         DO_TESTCASE_6IRW(desc, name, 312);                      \
1123         DO_TESTCASE_6IRW(desc, name, 321);
1124 
1125 
1126 void locking_selftest(void)
1127 {
1128         /*
1129          * Got a locking failure before the selftest ran?
1130          */
1131         if (!debug_locks) {
1132                 printk("----------------------------------\n");
1133                 printk("| Locking API testsuite disabled |\n");
1134                 printk("----------------------------------\n");
1135                 return;
1136         }
1137 
1138         /*
1139          * Run the testsuite:
1140          */
1141         printk("------------------------\n");
1142         printk("| Locking API testsuite:\n");
1143         printk("----------------------------------------------------------------------------\n");
1144         printk("                                 | spin |wlock |rlock |mutex | wsem | rsem |\n");
1145         printk("  --------------------------------------------------------------------------\n");
1146 
1147         init_shared_classes();
1148         debug_locks_silent = !debug_locks_verbose;
1149 
1150         DO_TESTCASE_6R("A-A deadlock", AA);
1151         DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1152         DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1153         DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1154         DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1155         DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1156         DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1157         DO_TESTCASE_6("double unlock", double_unlock);
1158         DO_TESTCASE_6("initialize held", init_held);
1159         DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1160 
1161         printk("  --------------------------------------------------------------------------\n");
1162         print_testname("recursive read-lock");
1163         printk("             |");
1164         dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1165         printk("             |");
1166         dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1167         printk("\n");
1168 
1169         print_testname("recursive read-lock #2");
1170         printk("             |");
1171         dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
1172         printk("             |");
1173         dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1174         printk("\n");
1175 
1176         print_testname("mixed read-write-lock");
1177         printk("             |");
1178         dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1179         printk("             |");
1180         dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1181         printk("\n");
1182 
1183         print_testname("mixed write-read-lock");
1184         printk("             |");
1185         dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1186         printk("             |");
1187         dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1188         printk("\n");
1189 
1190         printk("  --------------------------------------------------------------------------\n");
1191 
1192         /*
1193          * irq-context testcases:
1194          */
1195 #ifndef CONFIG_PREEMPT_RT
1196         DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1197         DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1198         DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1199         DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1200         DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1201         DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1202 
1203         DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1204 //      DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1205 #endif
1206 
1207         if (unexpected_testcase_failures) {
1208                 printk("-----------------------------------------------------------------\n");
1209                 debug_locks = 0;
1210                 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1211                         unexpected_testcase_failures, testcase_total);
1212                 printk("-----------------------------------------------------------------\n");
1213         } else if (expected_testcase_failures && testcase_successes) {
1214                 printk("--------------------------------------------------------\n");
1215                 printk("%3d out of %3d testcases failed, as expected. |\n",
1216                         expected_testcase_failures, testcase_total);
1217                 printk("----------------------------------------------------\n");
1218                 debug_locks = 1;
1219         } else if (expected_testcase_failures && !testcase_successes) {
1220                 printk("--------------------------------------------------------\n");
1221                 printk("All %3d testcases failed, as expected. |\n",
1222                         expected_testcase_failures);
1223                 printk("----------------------------------------\n");
1224                 debug_locks = 1;
1225         } else {
1226                 printk("-------------------------------------------------------\n");
1227                 printk("Good, all %3d testcases passed! |\n",
1228                         testcase_successes);
1229                 printk("---------------------------------\n");
1230                 debug_locks = 1;
1231         }
1232         debug_locks_silent = 0;
1233 }
1234 
  This page was automatically generated by the LXR engine.