locking-selftest.c 44 KB

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