locking-selftest.c 44 KB

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