locking-selftest.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  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. #ifdef CONFIG_PROVE_LOCKING
  1663. /*
  1664. * Lockdep does indeed fail here, but there's nothing we can do about
  1665. * that now. Don't kill lockdep for it.
  1666. */
  1667. unexpected_testcase_failures--;
  1668. #endif
  1669. pr_cont(" |");
  1670. dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
  1671. print_testname("mixed read-lock/lock-read ABBA");
  1672. pr_cont(" |");
  1673. dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
  1674. pr_cont(" |");
  1675. dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
  1676. print_testname("mixed write-lock/lock-write ABBA");
  1677. pr_cont(" |");
  1678. dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
  1679. pr_cont(" |");
  1680. dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
  1681. printk(" --------------------------------------------------------------------------\n");
  1682. /*
  1683. * irq-context testcases:
  1684. */
  1685. DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
  1686. DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
  1687. DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
  1688. DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
  1689. DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
  1690. DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
  1691. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  1692. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  1693. ww_tests();
  1694. if (unexpected_testcase_failures) {
  1695. printk("-----------------------------------------------------------------\n");
  1696. debug_locks = 0;
  1697. printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
  1698. unexpected_testcase_failures, testcase_total);
  1699. printk("-----------------------------------------------------------------\n");
  1700. } else if (expected_testcase_failures && testcase_successes) {
  1701. printk("--------------------------------------------------------\n");
  1702. printk("%3d out of %3d testcases failed, as expected. |\n",
  1703. expected_testcase_failures, testcase_total);
  1704. printk("----------------------------------------------------\n");
  1705. debug_locks = 1;
  1706. } else if (expected_testcase_failures && !testcase_successes) {
  1707. printk("--------------------------------------------------------\n");
  1708. printk("All %3d testcases failed, as expected. |\n",
  1709. expected_testcase_failures);
  1710. printk("----------------------------------------\n");
  1711. debug_locks = 1;
  1712. } else {
  1713. printk("-------------------------------------------------------\n");
  1714. printk("Good, all %3d testcases passed! |\n",
  1715. testcase_successes);
  1716. printk("---------------------------------\n");
  1717. debug_locks = 1;
  1718. }
  1719. debug_locks_silent = 0;
  1720. }