locking-selftest.c 39 KB

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