locking-selftest.c 34 KB

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