locking-selftest.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  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/sched/mm.h>
  19. #include <linux/delay.h>
  20. #include <linux/lockdep.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/debug_locks.h>
  25. #include <linux/irqflags.h>
  26. #include <linux/rtmutex.h>
  27. #include <linux/local_lock.h>
  28. #ifdef CONFIG_PREEMPT_RT
  29. # define NON_RT(...)
  30. #else
  31. # define NON_RT(...) __VA_ARGS__
  32. #endif
  33. /*
  34. * Change this to 1 if you want to see the failure printouts:
  35. */
  36. static unsigned int debug_locks_verbose;
  37. unsigned int force_read_lock_recursive;
  38. static DEFINE_WD_CLASS(ww_lockdep);
  39. static int __init setup_debug_locks_verbose(char *str)
  40. {
  41. get_option(&str, &debug_locks_verbose);
  42. return 1;
  43. }
  44. __setup("debug_locks_verbose=", setup_debug_locks_verbose);
  45. #define FAILURE 0
  46. #define SUCCESS 1
  47. #define LOCKTYPE_SPIN 0x1
  48. #define LOCKTYPE_RWLOCK 0x2
  49. #define LOCKTYPE_MUTEX 0x4
  50. #define LOCKTYPE_RWSEM 0x8
  51. #define LOCKTYPE_WW 0x10
  52. #define LOCKTYPE_RTMUTEX 0x20
  53. #define LOCKTYPE_LL 0x40
  54. #define LOCKTYPE_SPECIAL 0x80
  55. static struct ww_acquire_ctx t, t2;
  56. static struct ww_mutex o, o2, o3;
  57. /*
  58. * Normal standalone locks, for the circular and irq-context
  59. * dependency tests:
  60. */
  61. static DEFINE_SPINLOCK(lock_A);
  62. static DEFINE_SPINLOCK(lock_B);
  63. static DEFINE_SPINLOCK(lock_C);
  64. static DEFINE_SPINLOCK(lock_D);
  65. static DEFINE_RAW_SPINLOCK(raw_lock_A);
  66. static DEFINE_RAW_SPINLOCK(raw_lock_B);
  67. static DEFINE_RWLOCK(rwlock_A);
  68. static DEFINE_RWLOCK(rwlock_B);
  69. static DEFINE_RWLOCK(rwlock_C);
  70. static DEFINE_RWLOCK(rwlock_D);
  71. static DEFINE_MUTEX(mutex_A);
  72. static DEFINE_MUTEX(mutex_B);
  73. static DEFINE_MUTEX(mutex_C);
  74. static DEFINE_MUTEX(mutex_D);
  75. static DECLARE_RWSEM(rwsem_A);
  76. static DECLARE_RWSEM(rwsem_B);
  77. static DECLARE_RWSEM(rwsem_C);
  78. static DECLARE_RWSEM(rwsem_D);
  79. #ifdef CONFIG_RT_MUTEXES
  80. static DEFINE_RT_MUTEX(rtmutex_A);
  81. static DEFINE_RT_MUTEX(rtmutex_B);
  82. static DEFINE_RT_MUTEX(rtmutex_C);
  83. static DEFINE_RT_MUTEX(rtmutex_D);
  84. #endif
  85. /*
  86. * Locks that we initialize dynamically as well so that
  87. * e.g. X1 and X2 becomes two instances of the same class,
  88. * but X* and Y* are different classes. We do this so that
  89. * we do not trigger a real lockup:
  90. */
  91. static DEFINE_SPINLOCK(lock_X1);
  92. static DEFINE_SPINLOCK(lock_X2);
  93. static DEFINE_SPINLOCK(lock_Y1);
  94. static DEFINE_SPINLOCK(lock_Y2);
  95. static DEFINE_SPINLOCK(lock_Z1);
  96. static DEFINE_SPINLOCK(lock_Z2);
  97. static DEFINE_RWLOCK(rwlock_X1);
  98. static DEFINE_RWLOCK(rwlock_X2);
  99. static DEFINE_RWLOCK(rwlock_Y1);
  100. static DEFINE_RWLOCK(rwlock_Y2);
  101. static DEFINE_RWLOCK(rwlock_Z1);
  102. static DEFINE_RWLOCK(rwlock_Z2);
  103. static DEFINE_MUTEX(mutex_X1);
  104. static DEFINE_MUTEX(mutex_X2);
  105. static DEFINE_MUTEX(mutex_Y1);
  106. static DEFINE_MUTEX(mutex_Y2);
  107. static DEFINE_MUTEX(mutex_Z1);
  108. static DEFINE_MUTEX(mutex_Z2);
  109. static DECLARE_RWSEM(rwsem_X1);
  110. static DECLARE_RWSEM(rwsem_X2);
  111. static DECLARE_RWSEM(rwsem_Y1);
  112. static DECLARE_RWSEM(rwsem_Y2);
  113. static DECLARE_RWSEM(rwsem_Z1);
  114. static DECLARE_RWSEM(rwsem_Z2);
  115. #ifdef CONFIG_RT_MUTEXES
  116. static DEFINE_RT_MUTEX(rtmutex_X1);
  117. static DEFINE_RT_MUTEX(rtmutex_X2);
  118. static DEFINE_RT_MUTEX(rtmutex_Y1);
  119. static DEFINE_RT_MUTEX(rtmutex_Y2);
  120. static DEFINE_RT_MUTEX(rtmutex_Z1);
  121. static DEFINE_RT_MUTEX(rtmutex_Z2);
  122. #endif
  123. static DEFINE_PER_CPU(local_lock_t, local_A);
  124. /*
  125. * non-inlined runtime initializers, to let separate locks share
  126. * the same lock-class:
  127. */
  128. #define INIT_CLASS_FUNC(class) \
  129. static noinline void \
  130. init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \
  131. struct mutex *mutex, struct rw_semaphore *rwsem)\
  132. { \
  133. spin_lock_init(lock); \
  134. rwlock_init(rwlock); \
  135. mutex_init(mutex); \
  136. init_rwsem(rwsem); \
  137. }
  138. INIT_CLASS_FUNC(X)
  139. INIT_CLASS_FUNC(Y)
  140. INIT_CLASS_FUNC(Z)
  141. static void init_shared_classes(void)
  142. {
  143. #ifdef CONFIG_RT_MUTEXES
  144. static struct lock_class_key rt_X, rt_Y, rt_Z;
  145. __rt_mutex_init(&rtmutex_X1, __func__, &rt_X);
  146. __rt_mutex_init(&rtmutex_X2, __func__, &rt_X);
  147. __rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);
  148. __rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);
  149. __rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);
  150. __rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);
  151. #endif
  152. init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
  153. init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
  154. init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
  155. init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
  156. init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
  157. init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
  158. }
  159. /*
  160. * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
  161. * The following functions use a lock from a simulated hardirq/softirq
  162. * context, causing the locks to be marked as hardirq-safe/softirq-safe:
  163. */
  164. #define HARDIRQ_DISABLE local_irq_disable
  165. #define HARDIRQ_ENABLE local_irq_enable
  166. #define HARDIRQ_ENTER() \
  167. local_irq_disable(); \
  168. __irq_enter(); \
  169. lockdep_hardirq_threaded(); \
  170. WARN_ON(!in_hardirq());
  171. #define HARDIRQ_EXIT() \
  172. __irq_exit(); \
  173. local_irq_enable();
  174. #define SOFTIRQ_DISABLE local_bh_disable
  175. #define SOFTIRQ_ENABLE local_bh_enable
  176. #define SOFTIRQ_ENTER() \
  177. local_bh_disable(); \
  178. local_irq_disable(); \
  179. lockdep_softirq_enter(); \
  180. WARN_ON(!in_softirq());
  181. #define SOFTIRQ_EXIT() \
  182. lockdep_softirq_exit(); \
  183. local_irq_enable(); \
  184. local_bh_enable();
  185. /*
  186. * Shortcuts for lock/unlock API variants, to keep
  187. * the testcases compact:
  188. */
  189. #define L(x) spin_lock(&lock_##x)
  190. #define U(x) spin_unlock(&lock_##x)
  191. #define LU(x) L(x); U(x)
  192. #define SI(x) spin_lock_init(&lock_##x)
  193. #define WL(x) write_lock(&rwlock_##x)
  194. #define WU(x) write_unlock(&rwlock_##x)
  195. #define WLU(x) WL(x); WU(x)
  196. #define RL(x) read_lock(&rwlock_##x)
  197. #define RU(x) read_unlock(&rwlock_##x)
  198. #define RLU(x) RL(x); RU(x)
  199. #define RWI(x) rwlock_init(&rwlock_##x)
  200. #define ML(x) mutex_lock(&mutex_##x)
  201. #define MU(x) mutex_unlock(&mutex_##x)
  202. #define MI(x) mutex_init(&mutex_##x)
  203. #define RTL(x) rt_mutex_lock(&rtmutex_##x)
  204. #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
  205. #define RTI(x) rt_mutex_init(&rtmutex_##x)
  206. #define WSL(x) down_write(&rwsem_##x)
  207. #define WSU(x) up_write(&rwsem_##x)
  208. #define RSL(x) down_read(&rwsem_##x)
  209. #define RSU(x) up_read(&rwsem_##x)
  210. #define RWSI(x) init_rwsem(&rwsem_##x)
  211. #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  212. #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
  213. #else
  214. #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
  215. #endif
  216. #define WWAD(x) ww_acquire_done(x)
  217. #define WWAF(x) ww_acquire_fini(x)
  218. #define WWL(x, c) ww_mutex_lock(x, c)
  219. #define WWT(x) ww_mutex_trylock(x, NULL)
  220. #define WWL1(x) ww_mutex_lock(x, NULL)
  221. #define WWU(x) ww_mutex_unlock(x)
  222. #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
  223. /*
  224. * Generate different permutations of the same testcase, using
  225. * the same basic lock-dependency/state events:
  226. */
  227. #define GENERATE_TESTCASE(name) \
  228. \
  229. static void name(void) { E(); }
  230. #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
  231. \
  232. static void name##_12(void) { E1(); E2(); } \
  233. static void name##_21(void) { E2(); E1(); }
  234. #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
  235. \
  236. static void name##_123(void) { E1(); E2(); E3(); } \
  237. static void name##_132(void) { E1(); E3(); E2(); } \
  238. static void name##_213(void) { E2(); E1(); E3(); } \
  239. static void name##_231(void) { E2(); E3(); E1(); } \
  240. static void name##_312(void) { E3(); E1(); E2(); } \
  241. static void name##_321(void) { E3(); E2(); E1(); }
  242. /*
  243. * AA deadlock:
  244. */
  245. #define E() \
  246. \
  247. LOCK(X1); \
  248. LOCK(X2); /* this one should fail */
  249. /*
  250. * 6 testcases:
  251. */
  252. #include "locking-selftest-spin.h"
  253. GENERATE_TESTCASE(AA_spin)
  254. #include "locking-selftest-wlock.h"
  255. GENERATE_TESTCASE(AA_wlock)
  256. #include "locking-selftest-rlock.h"
  257. GENERATE_TESTCASE(AA_rlock)
  258. #include "locking-selftest-mutex.h"
  259. GENERATE_TESTCASE(AA_mutex)
  260. #include "locking-selftest-wsem.h"
  261. GENERATE_TESTCASE(AA_wsem)
  262. #include "locking-selftest-rsem.h"
  263. GENERATE_TESTCASE(AA_rsem)
  264. #ifdef CONFIG_RT_MUTEXES
  265. #include "locking-selftest-rtmutex.h"
  266. GENERATE_TESTCASE(AA_rtmutex);
  267. #endif
  268. #undef E
  269. /*
  270. * Special-case for read-locking, they are
  271. * allowed to recurse on the same lock class:
  272. */
  273. static void rlock_AA1(void)
  274. {
  275. RL(X1);
  276. RL(X1); // this one should NOT fail
  277. }
  278. static void rlock_AA1B(void)
  279. {
  280. RL(X1);
  281. RL(X2); // this one should NOT fail
  282. }
  283. static void rsem_AA1(void)
  284. {
  285. RSL(X1);
  286. RSL(X1); // this one should fail
  287. }
  288. static void rsem_AA1B(void)
  289. {
  290. RSL(X1);
  291. RSL(X2); // this one should fail
  292. }
  293. /*
  294. * The mixing of read and write locks is not allowed:
  295. */
  296. static void rlock_AA2(void)
  297. {
  298. RL(X1);
  299. WL(X2); // this one should fail
  300. }
  301. static void rsem_AA2(void)
  302. {
  303. RSL(X1);
  304. WSL(X2); // this one should fail
  305. }
  306. static void rlock_AA3(void)
  307. {
  308. WL(X1);
  309. RL(X2); // this one should fail
  310. }
  311. static void rsem_AA3(void)
  312. {
  313. WSL(X1);
  314. RSL(X2); // this one should fail
  315. }
  316. /*
  317. * read_lock(A)
  318. * spin_lock(B)
  319. * spin_lock(B)
  320. * write_lock(A)
  321. */
  322. static void rlock_ABBA1(void)
  323. {
  324. RL(X1);
  325. L(Y1);
  326. U(Y1);
  327. RU(X1);
  328. L(Y1);
  329. WL(X1);
  330. WU(X1);
  331. U(Y1); // should fail
  332. }
  333. static void rwsem_ABBA1(void)
  334. {
  335. RSL(X1);
  336. ML(Y1);
  337. MU(Y1);
  338. RSU(X1);
  339. ML(Y1);
  340. WSL(X1);
  341. WSU(X1);
  342. MU(Y1); // should fail
  343. }
  344. /*
  345. * read_lock(A)
  346. * spin_lock(B)
  347. * spin_lock(B)
  348. * write_lock(A)
  349. *
  350. * This test case is aimed at poking whether the chain cache prevents us from
  351. * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
  352. * read/write locks, the following case may happen
  353. *
  354. * { read_lock(A)->lock(B) dependency exists }
  355. *
  356. * P0:
  357. * lock(B);
  358. * read_lock(A);
  359. *
  360. * { Not a deadlock, B -> A is added in the chain cache }
  361. *
  362. * P1:
  363. * lock(B);
  364. * write_lock(A);
  365. *
  366. * { B->A found in chain cache, not reported as a deadlock }
  367. *
  368. */
  369. static void rlock_chaincache_ABBA1(void)
  370. {
  371. RL(X1);
  372. L(Y1);
  373. U(Y1);
  374. RU(X1);
  375. L(Y1);
  376. RL(X1);
  377. RU(X1);
  378. U(Y1);
  379. L(Y1);
  380. WL(X1);
  381. WU(X1);
  382. U(Y1); // should fail
  383. }
  384. /*
  385. * read_lock(A)
  386. * spin_lock(B)
  387. * spin_lock(B)
  388. * read_lock(A)
  389. */
  390. static void rlock_ABBA2(void)
  391. {
  392. RL(X1);
  393. L(Y1);
  394. U(Y1);
  395. RU(X1);
  396. L(Y1);
  397. RL(X1);
  398. RU(X1);
  399. U(Y1); // should NOT fail
  400. }
  401. static void rwsem_ABBA2(void)
  402. {
  403. RSL(X1);
  404. ML(Y1);
  405. MU(Y1);
  406. RSU(X1);
  407. ML(Y1);
  408. RSL(X1);
  409. RSU(X1);
  410. MU(Y1); // should fail
  411. }
  412. /*
  413. * write_lock(A)
  414. * spin_lock(B)
  415. * spin_lock(B)
  416. * write_lock(A)
  417. */
  418. static void rlock_ABBA3(void)
  419. {
  420. WL(X1);
  421. L(Y1);
  422. U(Y1);
  423. WU(X1);
  424. L(Y1);
  425. WL(X1);
  426. WU(X1);
  427. U(Y1); // should fail
  428. }
  429. static void rwsem_ABBA3(void)
  430. {
  431. WSL(X1);
  432. ML(Y1);
  433. MU(Y1);
  434. WSU(X1);
  435. ML(Y1);
  436. WSL(X1);
  437. WSU(X1);
  438. MU(Y1); // should fail
  439. }
  440. /*
  441. * ABBA deadlock:
  442. */
  443. #define E() \
  444. \
  445. LOCK_UNLOCK_2(A, B); \
  446. LOCK_UNLOCK_2(B, A); /* fail */
  447. /*
  448. * 6 testcases:
  449. */
  450. #include "locking-selftest-spin.h"
  451. GENERATE_TESTCASE(ABBA_spin)
  452. #include "locking-selftest-wlock.h"
  453. GENERATE_TESTCASE(ABBA_wlock)
  454. #include "locking-selftest-rlock.h"
  455. GENERATE_TESTCASE(ABBA_rlock)
  456. #include "locking-selftest-mutex.h"
  457. GENERATE_TESTCASE(ABBA_mutex)
  458. #include "locking-selftest-wsem.h"
  459. GENERATE_TESTCASE(ABBA_wsem)
  460. #include "locking-selftest-rsem.h"
  461. GENERATE_TESTCASE(ABBA_rsem)
  462. #ifdef CONFIG_RT_MUTEXES
  463. #include "locking-selftest-rtmutex.h"
  464. GENERATE_TESTCASE(ABBA_rtmutex);
  465. #endif
  466. #undef E
  467. /*
  468. * AB BC CA deadlock:
  469. */
  470. #define E() \
  471. \
  472. LOCK_UNLOCK_2(A, B); \
  473. LOCK_UNLOCK_2(B, C); \
  474. LOCK_UNLOCK_2(C, A); /* fail */
  475. /*
  476. * 6 testcases:
  477. */
  478. #include "locking-selftest-spin.h"
  479. GENERATE_TESTCASE(ABBCCA_spin)
  480. #include "locking-selftest-wlock.h"
  481. GENERATE_TESTCASE(ABBCCA_wlock)
  482. #include "locking-selftest-rlock.h"
  483. GENERATE_TESTCASE(ABBCCA_rlock)
  484. #include "locking-selftest-mutex.h"
  485. GENERATE_TESTCASE(ABBCCA_mutex)
  486. #include "locking-selftest-wsem.h"
  487. GENERATE_TESTCASE(ABBCCA_wsem)
  488. #include "locking-selftest-rsem.h"
  489. GENERATE_TESTCASE(ABBCCA_rsem)
  490. #ifdef CONFIG_RT_MUTEXES
  491. #include "locking-selftest-rtmutex.h"
  492. GENERATE_TESTCASE(ABBCCA_rtmutex);
  493. #endif
  494. #undef E
  495. /*
  496. * AB CA BC deadlock:
  497. */
  498. #define E() \
  499. \
  500. LOCK_UNLOCK_2(A, B); \
  501. LOCK_UNLOCK_2(C, A); \
  502. LOCK_UNLOCK_2(B, C); /* fail */
  503. /*
  504. * 6 testcases:
  505. */
  506. #include "locking-selftest-spin.h"
  507. GENERATE_TESTCASE(ABCABC_spin)
  508. #include "locking-selftest-wlock.h"
  509. GENERATE_TESTCASE(ABCABC_wlock)
  510. #include "locking-selftest-rlock.h"
  511. GENERATE_TESTCASE(ABCABC_rlock)
  512. #include "locking-selftest-mutex.h"
  513. GENERATE_TESTCASE(ABCABC_mutex)
  514. #include "locking-selftest-wsem.h"
  515. GENERATE_TESTCASE(ABCABC_wsem)
  516. #include "locking-selftest-rsem.h"
  517. GENERATE_TESTCASE(ABCABC_rsem)
  518. #ifdef CONFIG_RT_MUTEXES
  519. #include "locking-selftest-rtmutex.h"
  520. GENERATE_TESTCASE(ABCABC_rtmutex);
  521. #endif
  522. #undef E
  523. /*
  524. * AB BC CD DA deadlock:
  525. */
  526. #define E() \
  527. \
  528. LOCK_UNLOCK_2(A, B); \
  529. LOCK_UNLOCK_2(B, C); \
  530. LOCK_UNLOCK_2(C, D); \
  531. LOCK_UNLOCK_2(D, A); /* fail */
  532. /*
  533. * 6 testcases:
  534. */
  535. #include "locking-selftest-spin.h"
  536. GENERATE_TESTCASE(ABBCCDDA_spin)
  537. #include "locking-selftest-wlock.h"
  538. GENERATE_TESTCASE(ABBCCDDA_wlock)
  539. #include "locking-selftest-rlock.h"
  540. GENERATE_TESTCASE(ABBCCDDA_rlock)
  541. #include "locking-selftest-mutex.h"
  542. GENERATE_TESTCASE(ABBCCDDA_mutex)
  543. #include "locking-selftest-wsem.h"
  544. GENERATE_TESTCASE(ABBCCDDA_wsem)
  545. #include "locking-selftest-rsem.h"
  546. GENERATE_TESTCASE(ABBCCDDA_rsem)
  547. #ifdef CONFIG_RT_MUTEXES
  548. #include "locking-selftest-rtmutex.h"
  549. GENERATE_TESTCASE(ABBCCDDA_rtmutex);
  550. #endif
  551. #undef E
  552. /*
  553. * AB CD BD DA deadlock:
  554. */
  555. #define E() \
  556. \
  557. LOCK_UNLOCK_2(A, B); \
  558. LOCK_UNLOCK_2(C, D); \
  559. LOCK_UNLOCK_2(B, D); \
  560. LOCK_UNLOCK_2(D, A); /* fail */
  561. /*
  562. * 6 testcases:
  563. */
  564. #include "locking-selftest-spin.h"
  565. GENERATE_TESTCASE(ABCDBDDA_spin)
  566. #include "locking-selftest-wlock.h"
  567. GENERATE_TESTCASE(ABCDBDDA_wlock)
  568. #include "locking-selftest-rlock.h"
  569. GENERATE_TESTCASE(ABCDBDDA_rlock)
  570. #include "locking-selftest-mutex.h"
  571. GENERATE_TESTCASE(ABCDBDDA_mutex)
  572. #include "locking-selftest-wsem.h"
  573. GENERATE_TESTCASE(ABCDBDDA_wsem)
  574. #include "locking-selftest-rsem.h"
  575. GENERATE_TESTCASE(ABCDBDDA_rsem)
  576. #ifdef CONFIG_RT_MUTEXES
  577. #include "locking-selftest-rtmutex.h"
  578. GENERATE_TESTCASE(ABCDBDDA_rtmutex);
  579. #endif
  580. #undef E
  581. /*
  582. * AB CD BC DA deadlock:
  583. */
  584. #define E() \
  585. \
  586. LOCK_UNLOCK_2(A, B); \
  587. LOCK_UNLOCK_2(C, D); \
  588. LOCK_UNLOCK_2(B, C); \
  589. LOCK_UNLOCK_2(D, A); /* fail */
  590. /*
  591. * 6 testcases:
  592. */
  593. #include "locking-selftest-spin.h"
  594. GENERATE_TESTCASE(ABCDBCDA_spin)
  595. #include "locking-selftest-wlock.h"
  596. GENERATE_TESTCASE(ABCDBCDA_wlock)
  597. #include "locking-selftest-rlock.h"
  598. GENERATE_TESTCASE(ABCDBCDA_rlock)
  599. #include "locking-selftest-mutex.h"
  600. GENERATE_TESTCASE(ABCDBCDA_mutex)
  601. #include "locking-selftest-wsem.h"
  602. GENERATE_TESTCASE(ABCDBCDA_wsem)
  603. #include "locking-selftest-rsem.h"
  604. GENERATE_TESTCASE(ABCDBCDA_rsem)
  605. #ifdef CONFIG_RT_MUTEXES
  606. #include "locking-selftest-rtmutex.h"
  607. GENERATE_TESTCASE(ABCDBCDA_rtmutex);
  608. #endif
  609. #undef E
  610. #ifdef CONFIG_PREEMPT_RT
  611. # define RT_PREPARE_DBL_UNLOCK() { migrate_disable(); rcu_read_lock(); }
  612. #else
  613. # define RT_PREPARE_DBL_UNLOCK()
  614. #endif
  615. /*
  616. * Double unlock:
  617. */
  618. #define E() \
  619. \
  620. LOCK(A); \
  621. RT_PREPARE_DBL_UNLOCK(); \
  622. UNLOCK(A); \
  623. UNLOCK(A); /* fail */
  624. /*
  625. * 6 testcases:
  626. */
  627. #include "locking-selftest-spin.h"
  628. GENERATE_TESTCASE(double_unlock_spin)
  629. #include "locking-selftest-wlock.h"
  630. GENERATE_TESTCASE(double_unlock_wlock)
  631. #include "locking-selftest-rlock.h"
  632. GENERATE_TESTCASE(double_unlock_rlock)
  633. #include "locking-selftest-mutex.h"
  634. GENERATE_TESTCASE(double_unlock_mutex)
  635. #include "locking-selftest-wsem.h"
  636. GENERATE_TESTCASE(double_unlock_wsem)
  637. #include "locking-selftest-rsem.h"
  638. GENERATE_TESTCASE(double_unlock_rsem)
  639. #ifdef CONFIG_RT_MUTEXES
  640. #include "locking-selftest-rtmutex.h"
  641. GENERATE_TESTCASE(double_unlock_rtmutex);
  642. #endif
  643. #undef E
  644. /*
  645. * initializing a held lock:
  646. */
  647. #define E() \
  648. \
  649. LOCK(A); \
  650. INIT(A); /* fail */
  651. /*
  652. * 6 testcases:
  653. */
  654. #include "locking-selftest-spin.h"
  655. GENERATE_TESTCASE(init_held_spin)
  656. #include "locking-selftest-wlock.h"
  657. GENERATE_TESTCASE(init_held_wlock)
  658. #include "locking-selftest-rlock.h"
  659. GENERATE_TESTCASE(init_held_rlock)
  660. #include "locking-selftest-mutex.h"
  661. GENERATE_TESTCASE(init_held_mutex)
  662. #include "locking-selftest-wsem.h"
  663. GENERATE_TESTCASE(init_held_wsem)
  664. #include "locking-selftest-rsem.h"
  665. GENERATE_TESTCASE(init_held_rsem)
  666. #ifdef CONFIG_RT_MUTEXES
  667. #include "locking-selftest-rtmutex.h"
  668. GENERATE_TESTCASE(init_held_rtmutex);
  669. #endif
  670. #undef E
  671. /*
  672. * locking an irq-safe lock with irqs enabled:
  673. */
  674. #define E1() \
  675. \
  676. IRQ_ENTER(); \
  677. LOCK(A); \
  678. UNLOCK(A); \
  679. IRQ_EXIT();
  680. #define E2() \
  681. \
  682. LOCK(A); \
  683. UNLOCK(A);
  684. /*
  685. * Generate 24 testcases:
  686. */
  687. #include "locking-selftest-spin-hardirq.h"
  688. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  689. #include "locking-selftest-rlock-hardirq.h"
  690. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  691. #include "locking-selftest-wlock-hardirq.h"
  692. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
  693. #ifndef CONFIG_PREEMPT_RT
  694. #include "locking-selftest-spin-softirq.h"
  695. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
  696. #include "locking-selftest-rlock-softirq.h"
  697. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
  698. #include "locking-selftest-wlock-softirq.h"
  699. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  700. #endif
  701. #undef E1
  702. #undef E2
  703. #ifndef CONFIG_PREEMPT_RT
  704. /*
  705. * Enabling hardirqs with a softirq-safe lock held:
  706. */
  707. #define E1() \
  708. \
  709. SOFTIRQ_ENTER(); \
  710. LOCK(A); \
  711. UNLOCK(A); \
  712. SOFTIRQ_EXIT();
  713. #define E2() \
  714. \
  715. HARDIRQ_DISABLE(); \
  716. LOCK(A); \
  717. HARDIRQ_ENABLE(); \
  718. UNLOCK(A);
  719. /*
  720. * Generate 12 testcases:
  721. */
  722. #include "locking-selftest-spin.h"
  723. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
  724. #include "locking-selftest-wlock.h"
  725. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
  726. #include "locking-selftest-rlock.h"
  727. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  728. #undef E1
  729. #undef E2
  730. #endif
  731. /*
  732. * Enabling irqs with an irq-safe lock held:
  733. */
  734. #define E1() \
  735. \
  736. IRQ_ENTER(); \
  737. LOCK(A); \
  738. UNLOCK(A); \
  739. IRQ_EXIT();
  740. #define E2() \
  741. \
  742. IRQ_DISABLE(); \
  743. LOCK(A); \
  744. IRQ_ENABLE(); \
  745. UNLOCK(A);
  746. /*
  747. * Generate 24 testcases:
  748. */
  749. #include "locking-selftest-spin-hardirq.h"
  750. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  751. #include "locking-selftest-rlock-hardirq.h"
  752. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  753. #include "locking-selftest-wlock-hardirq.h"
  754. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
  755. #ifndef CONFIG_PREEMPT_RT
  756. #include "locking-selftest-spin-softirq.h"
  757. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
  758. #include "locking-selftest-rlock-softirq.h"
  759. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
  760. #include "locking-selftest-wlock-softirq.h"
  761. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  762. #endif
  763. #undef E1
  764. #undef E2
  765. /*
  766. * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
  767. */
  768. #define E1() \
  769. \
  770. LOCK(A); \
  771. LOCK(B); \
  772. UNLOCK(B); \
  773. UNLOCK(A); \
  774. #define E2() \
  775. \
  776. LOCK(B); \
  777. UNLOCK(B);
  778. #define E3() \
  779. \
  780. IRQ_ENTER(); \
  781. LOCK(A); \
  782. UNLOCK(A); \
  783. IRQ_EXIT();
  784. /*
  785. * Generate 36 testcases:
  786. */
  787. #include "locking-selftest-spin-hardirq.h"
  788. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  789. #include "locking-selftest-rlock-hardirq.h"
  790. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  791. #include "locking-selftest-wlock-hardirq.h"
  792. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
  793. #ifndef CONFIG_PREEMPT_RT
  794. #include "locking-selftest-spin-softirq.h"
  795. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
  796. #include "locking-selftest-rlock-softirq.h"
  797. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
  798. #include "locking-selftest-wlock-softirq.h"
  799. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  800. #endif
  801. #undef E1
  802. #undef E2
  803. #undef E3
  804. /*
  805. * If a lock turns into softirq-safe, but earlier it took
  806. * a softirq-unsafe lock:
  807. */
  808. #define E1() \
  809. IRQ_DISABLE(); \
  810. LOCK(A); \
  811. LOCK(B); \
  812. UNLOCK(B); \
  813. UNLOCK(A); \
  814. IRQ_ENABLE();
  815. #define E2() \
  816. LOCK(B); \
  817. UNLOCK(B);
  818. #define E3() \
  819. IRQ_ENTER(); \
  820. LOCK(A); \
  821. UNLOCK(A); \
  822. IRQ_EXIT();
  823. /*
  824. * Generate 36 testcases:
  825. */
  826. #include "locking-selftest-spin-hardirq.h"
  827. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  828. #include "locking-selftest-rlock-hardirq.h"
  829. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  830. #include "locking-selftest-wlock-hardirq.h"
  831. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
  832. #ifndef CONFIG_PREEMPT_RT
  833. #include "locking-selftest-spin-softirq.h"
  834. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
  835. #include "locking-selftest-rlock-softirq.h"
  836. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
  837. #include "locking-selftest-wlock-softirq.h"
  838. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  839. #endif
  840. #undef E1
  841. #undef E2
  842. #undef E3
  843. /*
  844. * read-lock / write-lock irq inversion.
  845. *
  846. * Deadlock scenario:
  847. *
  848. * CPU#1 is at #1, i.e. it has write-locked A, but has not
  849. * taken B yet.
  850. *
  851. * CPU#2 is at #2, i.e. it has locked B.
  852. *
  853. * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
  854. *
  855. * The deadlock occurs because CPU#1 will spin on B, and CPU#2
  856. * will spin on A.
  857. */
  858. #define E1() \
  859. \
  860. IRQ_DISABLE(); \
  861. WL(A); \
  862. LOCK(B); \
  863. UNLOCK(B); \
  864. WU(A); \
  865. IRQ_ENABLE();
  866. #define E2() \
  867. \
  868. LOCK(B); \
  869. UNLOCK(B);
  870. #define E3() \
  871. \
  872. IRQ_ENTER(); \
  873. RL(A); \
  874. RU(A); \
  875. IRQ_EXIT();
  876. /*
  877. * Generate 36 testcases:
  878. */
  879. #include "locking-selftest-spin-hardirq.h"
  880. GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
  881. #include "locking-selftest-rlock-hardirq.h"
  882. GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
  883. #include "locking-selftest-wlock-hardirq.h"
  884. GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
  885. #ifndef CONFIG_PREEMPT_RT
  886. #include "locking-selftest-spin-softirq.h"
  887. GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
  888. #include "locking-selftest-rlock-softirq.h"
  889. GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
  890. #include "locking-selftest-wlock-softirq.h"
  891. GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
  892. #endif
  893. #undef E1
  894. #undef E2
  895. #undef E3
  896. /*
  897. * write-read / write-read / write-read deadlock even if read is recursive
  898. */
  899. #define E1() \
  900. \
  901. WL(X1); \
  902. RL(Y1); \
  903. RU(Y1); \
  904. WU(X1);
  905. #define E2() \
  906. \
  907. WL(Y1); \
  908. RL(Z1); \
  909. RU(Z1); \
  910. WU(Y1);
  911. #define E3() \
  912. \
  913. WL(Z1); \
  914. RL(X1); \
  915. RU(X1); \
  916. WU(Z1);
  917. #include "locking-selftest-rlock.h"
  918. GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
  919. #undef E1
  920. #undef E2
  921. #undef E3
  922. /*
  923. * write-write / read-read / write-read deadlock even if read is recursive
  924. */
  925. #define E1() \
  926. \
  927. WL(X1); \
  928. WL(Y1); \
  929. WU(Y1); \
  930. WU(X1);
  931. #define E2() \
  932. \
  933. RL(Y1); \
  934. RL(Z1); \
  935. RU(Z1); \
  936. RU(Y1);
  937. #define E3() \
  938. \
  939. WL(Z1); \
  940. RL(X1); \
  941. RU(X1); \
  942. WU(Z1);
  943. #include "locking-selftest-rlock.h"
  944. GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
  945. #undef E1
  946. #undef E2
  947. #undef E3
  948. /*
  949. * write-write / read-read / read-write is not deadlock when read is recursive
  950. */
  951. #define E1() \
  952. \
  953. WL(X1); \
  954. WL(Y1); \
  955. WU(Y1); \
  956. WU(X1);
  957. #define E2() \
  958. \
  959. RL(Y1); \
  960. RL(Z1); \
  961. RU(Z1); \
  962. RU(Y1);
  963. #define E3() \
  964. \
  965. RL(Z1); \
  966. WL(X1); \
  967. WU(X1); \
  968. RU(Z1);
  969. #include "locking-selftest-rlock.h"
  970. GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
  971. #undef E1
  972. #undef E2
  973. #undef E3
  974. /*
  975. * write-read / read-read / write-write is not deadlock when read is recursive
  976. */
  977. #define E1() \
  978. \
  979. WL(X1); \
  980. RL(Y1); \
  981. RU(Y1); \
  982. WU(X1);
  983. #define E2() \
  984. \
  985. RL(Y1); \
  986. RL(Z1); \
  987. RU(Z1); \
  988. RU(Y1);
  989. #define E3() \
  990. \
  991. WL(Z1); \
  992. WL(X1); \
  993. WU(X1); \
  994. WU(Z1);
  995. #include "locking-selftest-rlock.h"
  996. GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
  997. #undef E1
  998. #undef E2
  999. #undef E3
  1000. /*
  1001. * read-lock / write-lock recursion that is actually safe.
  1002. */
  1003. #define E1() \
  1004. \
  1005. IRQ_DISABLE(); \
  1006. WL(A); \
  1007. WU(A); \
  1008. IRQ_ENABLE();
  1009. #define E2() \
  1010. \
  1011. RL(A); \
  1012. RU(A); \
  1013. #define E3() \
  1014. \
  1015. IRQ_ENTER(); \
  1016. LOCK(A); \
  1017. L(B); \
  1018. U(B); \
  1019. UNLOCK(A); \
  1020. IRQ_EXIT();
  1021. /*
  1022. * Generate 24 testcases:
  1023. */
  1024. #include "locking-selftest-hardirq.h"
  1025. #include "locking-selftest-rlock.h"
  1026. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
  1027. #include "locking-selftest-wlock.h"
  1028. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
  1029. #ifndef CONFIG_PREEMPT_RT
  1030. #include "locking-selftest-softirq.h"
  1031. #include "locking-selftest-rlock.h"
  1032. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
  1033. #include "locking-selftest-wlock.h"
  1034. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
  1035. #endif
  1036. #undef E1
  1037. #undef E2
  1038. #undef E3
  1039. /*
  1040. * read-lock / write-lock recursion that is unsafe.
  1041. */
  1042. #define E1() \
  1043. \
  1044. IRQ_DISABLE(); \
  1045. L(B); \
  1046. LOCK(A); \
  1047. UNLOCK(A); \
  1048. U(B); \
  1049. IRQ_ENABLE();
  1050. #define E2() \
  1051. \
  1052. RL(A); \
  1053. RU(A); \
  1054. #define E3() \
  1055. \
  1056. IRQ_ENTER(); \
  1057. L(B); \
  1058. U(B); \
  1059. IRQ_EXIT();
  1060. /*
  1061. * Generate 24 testcases:
  1062. */
  1063. #include "locking-selftest-hardirq.h"
  1064. #include "locking-selftest-rlock.h"
  1065. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
  1066. #include "locking-selftest-wlock.h"
  1067. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
  1068. #ifndef CONFIG_PREEMPT_RT
  1069. #include "locking-selftest-softirq.h"
  1070. #include "locking-selftest-rlock.h"
  1071. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
  1072. #include "locking-selftest-wlock.h"
  1073. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
  1074. #endif
  1075. #undef E1
  1076. #undef E2
  1077. #undef E3
  1078. /*
  1079. * read-lock / write-lock recursion that is unsafe.
  1080. *
  1081. * A is a ENABLED_*_READ lock
  1082. * B is a USED_IN_*_READ lock
  1083. *
  1084. * read_lock(A);
  1085. * write_lock(B);
  1086. * <interrupt>
  1087. * read_lock(B);
  1088. * write_lock(A); // if this one is read_lock(), no deadlock
  1089. */
  1090. #define E1() \
  1091. \
  1092. IRQ_DISABLE(); \
  1093. WL(B); \
  1094. LOCK(A); \
  1095. UNLOCK(A); \
  1096. WU(B); \
  1097. IRQ_ENABLE();
  1098. #define E2() \
  1099. \
  1100. RL(A); \
  1101. RU(A); \
  1102. #define E3() \
  1103. \
  1104. IRQ_ENTER(); \
  1105. RL(B); \
  1106. RU(B); \
  1107. IRQ_EXIT();
  1108. /*
  1109. * Generate 24 testcases:
  1110. */
  1111. #include "locking-selftest-hardirq.h"
  1112. #include "locking-selftest-rlock.h"
  1113. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
  1114. #include "locking-selftest-wlock.h"
  1115. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
  1116. #ifndef CONFIG_PREEMPT_RT
  1117. #include "locking-selftest-softirq.h"
  1118. #include "locking-selftest-rlock.h"
  1119. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
  1120. #include "locking-selftest-wlock.h"
  1121. GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
  1122. #endif
  1123. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1124. # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
  1125. # define I_RAW_SPINLOCK(x) lockdep_reset_lock(&raw_lock_##x.dep_map)
  1126. # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
  1127. # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
  1128. # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
  1129. # define I_WW(x) lockdep_reset_lock(&x.dep_map)
  1130. # define I_LOCAL_LOCK(x) lockdep_reset_lock(this_cpu_ptr(&local_##x.dep_map))
  1131. #ifdef CONFIG_RT_MUTEXES
  1132. # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
  1133. #endif
  1134. #else
  1135. # define I_SPINLOCK(x)
  1136. # define I_RAW_SPINLOCK(x)
  1137. # define I_RWLOCK(x)
  1138. # define I_MUTEX(x)
  1139. # define I_RWSEM(x)
  1140. # define I_WW(x)
  1141. # define I_LOCAL_LOCK(x)
  1142. #endif
  1143. #ifndef I_RTMUTEX
  1144. # define I_RTMUTEX(x)
  1145. #endif
  1146. #ifdef CONFIG_RT_MUTEXES
  1147. #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
  1148. #else
  1149. #define I2_RTMUTEX(x)
  1150. #endif
  1151. #define I1(x) \
  1152. do { \
  1153. I_SPINLOCK(x); \
  1154. I_RWLOCK(x); \
  1155. I_MUTEX(x); \
  1156. I_RWSEM(x); \
  1157. I_RTMUTEX(x); \
  1158. } while (0)
  1159. #define I2(x) \
  1160. do { \
  1161. spin_lock_init(&lock_##x); \
  1162. rwlock_init(&rwlock_##x); \
  1163. mutex_init(&mutex_##x); \
  1164. init_rwsem(&rwsem_##x); \
  1165. I2_RTMUTEX(x); \
  1166. } while (0)
  1167. static void reset_locks(void)
  1168. {
  1169. local_irq_disable();
  1170. lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
  1171. lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
  1172. I1(A); I1(B); I1(C); I1(D);
  1173. I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
  1174. I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
  1175. I_RAW_SPINLOCK(A); I_RAW_SPINLOCK(B);
  1176. I_LOCAL_LOCK(A);
  1177. lockdep_reset();
  1178. I2(A); I2(B); I2(C); I2(D);
  1179. init_shared_classes();
  1180. raw_spin_lock_init(&raw_lock_A);
  1181. raw_spin_lock_init(&raw_lock_B);
  1182. local_lock_init(this_cpu_ptr(&local_A));
  1183. ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
  1184. memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
  1185. memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
  1186. memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
  1187. local_irq_enable();
  1188. }
  1189. #undef I
  1190. static int testcase_total;
  1191. static int testcase_successes;
  1192. static int expected_testcase_failures;
  1193. static int unexpected_testcase_failures;
  1194. static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
  1195. {
  1196. int saved_preempt_count = preempt_count();
  1197. #ifdef CONFIG_PREEMPT_RT
  1198. #ifdef CONFIG_SMP
  1199. int saved_mgd_count = current->migration_disabled;
  1200. #endif
  1201. int saved_rcu_count = current->rcu_read_lock_nesting;
  1202. #endif
  1203. WARN_ON(irqs_disabled());
  1204. debug_locks_silent = !(debug_locks_verbose & lockclass_mask);
  1205. testcase_fn();
  1206. /*
  1207. * Filter out expected failures:
  1208. */
  1209. #ifndef CONFIG_PROVE_LOCKING
  1210. if (expected == FAILURE && debug_locks) {
  1211. expected_testcase_failures++;
  1212. pr_cont("failed|");
  1213. }
  1214. else
  1215. #endif
  1216. if (debug_locks != expected) {
  1217. unexpected_testcase_failures++;
  1218. pr_cont("FAILED|");
  1219. } else {
  1220. testcase_successes++;
  1221. pr_cont(" ok |");
  1222. }
  1223. testcase_total++;
  1224. if (debug_locks_verbose & lockclass_mask)
  1225. pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
  1226. lockclass_mask, debug_locks, expected);
  1227. /*
  1228. * Some tests (e.g. double-unlock) might corrupt the preemption
  1229. * count, so restore it:
  1230. */
  1231. preempt_count_set(saved_preempt_count);
  1232. #ifdef CONFIG_PREEMPT_RT
  1233. #ifdef CONFIG_SMP
  1234. while (current->migration_disabled > saved_mgd_count)
  1235. migrate_enable();
  1236. #endif
  1237. while (current->rcu_read_lock_nesting > saved_rcu_count)
  1238. rcu_read_unlock();
  1239. WARN_ON_ONCE(current->rcu_read_lock_nesting < saved_rcu_count);
  1240. #endif
  1241. #ifdef CONFIG_TRACE_IRQFLAGS
  1242. if (softirq_count())
  1243. current->softirqs_enabled = 0;
  1244. else
  1245. current->softirqs_enabled = 1;
  1246. #endif
  1247. reset_locks();
  1248. }
  1249. #ifdef CONFIG_RT_MUTEXES
  1250. #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
  1251. #else
  1252. #define dotest_rt(fn, e, m)
  1253. #endif
  1254. static inline void print_testname(const char *testname)
  1255. {
  1256. printk("%33s:", testname);
  1257. }
  1258. #define DO_TESTCASE_1(desc, name, nr) \
  1259. print_testname(desc"/"#nr); \
  1260. dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
  1261. pr_cont("\n");
  1262. #define DO_TESTCASE_1B(desc, name, nr) \
  1263. print_testname(desc"/"#nr); \
  1264. dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
  1265. pr_cont("\n");
  1266. #define DO_TESTCASE_1RR(desc, name, nr) \
  1267. print_testname(desc"/"#nr); \
  1268. pr_cont(" |"); \
  1269. dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
  1270. pr_cont("\n");
  1271. #define DO_TESTCASE_1RRB(desc, name, nr) \
  1272. print_testname(desc"/"#nr); \
  1273. pr_cont(" |"); \
  1274. dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
  1275. pr_cont("\n");
  1276. #define DO_TESTCASE_3(desc, name, nr) \
  1277. print_testname(desc"/"#nr); \
  1278. dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
  1279. dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
  1280. dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
  1281. pr_cont("\n");
  1282. #define DO_TESTCASE_3RW(desc, name, nr) \
  1283. print_testname(desc"/"#nr); \
  1284. dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
  1285. dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
  1286. dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
  1287. pr_cont("\n");
  1288. #define DO_TESTCASE_2RW(desc, name, nr) \
  1289. print_testname(desc"/"#nr); \
  1290. pr_cont(" |"); \
  1291. dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
  1292. dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
  1293. pr_cont("\n");
  1294. #define DO_TESTCASE_2x2RW(desc, name, nr) \
  1295. DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \
  1296. NON_RT(DO_TESTCASE_2RW("soft-"desc, name##_soft, nr)) \
  1297. #define DO_TESTCASE_6x2x2RW(desc, name) \
  1298. DO_TESTCASE_2x2RW(desc, name, 123); \
  1299. DO_TESTCASE_2x2RW(desc, name, 132); \
  1300. DO_TESTCASE_2x2RW(desc, name, 213); \
  1301. DO_TESTCASE_2x2RW(desc, name, 231); \
  1302. DO_TESTCASE_2x2RW(desc, name, 312); \
  1303. DO_TESTCASE_2x2RW(desc, name, 321);
  1304. #define DO_TESTCASE_6(desc, name) \
  1305. print_testname(desc); \
  1306. dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
  1307. dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
  1308. dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
  1309. dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
  1310. dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
  1311. dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
  1312. dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
  1313. pr_cont("\n");
  1314. #define DO_TESTCASE_6_SUCCESS(desc, name) \
  1315. print_testname(desc); \
  1316. dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
  1317. dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
  1318. dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
  1319. dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
  1320. dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
  1321. dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
  1322. dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
  1323. pr_cont("\n");
  1324. /*
  1325. * 'read' variant: rlocks must not trigger.
  1326. */
  1327. #define DO_TESTCASE_6R(desc, name) \
  1328. print_testname(desc); \
  1329. dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
  1330. dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
  1331. dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
  1332. dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
  1333. dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
  1334. dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
  1335. dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
  1336. pr_cont("\n");
  1337. #define DO_TESTCASE_2I(desc, name, nr) \
  1338. DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
  1339. NON_RT(DO_TESTCASE_1("soft-"desc, name##_soft, nr));
  1340. #define DO_TESTCASE_2IB(desc, name, nr) \
  1341. DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
  1342. NON_RT(DO_TESTCASE_1B("soft-"desc, name##_soft, nr));
  1343. #define DO_TESTCASE_6I(desc, name, nr) \
  1344. DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
  1345. NON_RT(DO_TESTCASE_3("soft-"desc, name##_soft, nr));
  1346. #define DO_TESTCASE_6IRW(desc, name, nr) \
  1347. DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
  1348. NON_RT(DO_TESTCASE_3RW("soft-"desc, name##_soft, nr));
  1349. #define DO_TESTCASE_2x3(desc, name) \
  1350. DO_TESTCASE_3(desc, name, 12); \
  1351. DO_TESTCASE_3(desc, name, 21);
  1352. #define DO_TESTCASE_2x6(desc, name) \
  1353. DO_TESTCASE_6I(desc, name, 12); \
  1354. DO_TESTCASE_6I(desc, name, 21);
  1355. #define DO_TESTCASE_6x2(desc, name) \
  1356. DO_TESTCASE_2I(desc, name, 123); \
  1357. DO_TESTCASE_2I(desc, name, 132); \
  1358. DO_TESTCASE_2I(desc, name, 213); \
  1359. DO_TESTCASE_2I(desc, name, 231); \
  1360. DO_TESTCASE_2I(desc, name, 312); \
  1361. DO_TESTCASE_2I(desc, name, 321);
  1362. #define DO_TESTCASE_6x2B(desc, name) \
  1363. DO_TESTCASE_2IB(desc, name, 123); \
  1364. DO_TESTCASE_2IB(desc, name, 132); \
  1365. DO_TESTCASE_2IB(desc, name, 213); \
  1366. DO_TESTCASE_2IB(desc, name, 231); \
  1367. DO_TESTCASE_2IB(desc, name, 312); \
  1368. DO_TESTCASE_2IB(desc, name, 321);
  1369. #define DO_TESTCASE_6x1RR(desc, name) \
  1370. DO_TESTCASE_1RR(desc, name, 123); \
  1371. DO_TESTCASE_1RR(desc, name, 132); \
  1372. DO_TESTCASE_1RR(desc, name, 213); \
  1373. DO_TESTCASE_1RR(desc, name, 231); \
  1374. DO_TESTCASE_1RR(desc, name, 312); \
  1375. DO_TESTCASE_1RR(desc, name, 321);
  1376. #define DO_TESTCASE_6x1RRB(desc, name) \
  1377. DO_TESTCASE_1RRB(desc, name, 123); \
  1378. DO_TESTCASE_1RRB(desc, name, 132); \
  1379. DO_TESTCASE_1RRB(desc, name, 213); \
  1380. DO_TESTCASE_1RRB(desc, name, 231); \
  1381. DO_TESTCASE_1RRB(desc, name, 312); \
  1382. DO_TESTCASE_1RRB(desc, name, 321);
  1383. #define DO_TESTCASE_6x6(desc, name) \
  1384. DO_TESTCASE_6I(desc, name, 123); \
  1385. DO_TESTCASE_6I(desc, name, 132); \
  1386. DO_TESTCASE_6I(desc, name, 213); \
  1387. DO_TESTCASE_6I(desc, name, 231); \
  1388. DO_TESTCASE_6I(desc, name, 312); \
  1389. DO_TESTCASE_6I(desc, name, 321);
  1390. #define DO_TESTCASE_6x6RW(desc, name) \
  1391. DO_TESTCASE_6IRW(desc, name, 123); \
  1392. DO_TESTCASE_6IRW(desc, name, 132); \
  1393. DO_TESTCASE_6IRW(desc, name, 213); \
  1394. DO_TESTCASE_6IRW(desc, name, 231); \
  1395. DO_TESTCASE_6IRW(desc, name, 312); \
  1396. DO_TESTCASE_6IRW(desc, name, 321);
  1397. static void ww_test_fail_acquire(void)
  1398. {
  1399. int ret;
  1400. WWAI(&t);
  1401. t.stamp++;
  1402. ret = WWL(&o, &t);
  1403. if (WARN_ON(!o.ctx) ||
  1404. WARN_ON(ret))
  1405. return;
  1406. /* No lockdep test, pure API */
  1407. ret = WWL(&o, &t);
  1408. WARN_ON(ret != -EALREADY);
  1409. ret = WWT(&o);
  1410. WARN_ON(ret);
  1411. t2 = t;
  1412. t2.stamp++;
  1413. ret = WWL(&o, &t2);
  1414. WARN_ON(ret != -EDEADLK);
  1415. WWU(&o);
  1416. if (WWT(&o))
  1417. WWU(&o);
  1418. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1419. else
  1420. DEBUG_LOCKS_WARN_ON(1);
  1421. #endif
  1422. }
  1423. #ifdef CONFIG_PREEMPT_RT
  1424. #define ww_mutex_base_lock(b) rt_mutex_lock(b)
  1425. #define ww_mutex_base_trylock(b) rt_mutex_trylock(b)
  1426. #define ww_mutex_base_lock_nest_lock(b, b2) rt_mutex_lock_nest_lock(b, b2)
  1427. #define ww_mutex_base_lock_interruptible(b) rt_mutex_lock_interruptible(b)
  1428. #define ww_mutex_base_lock_killable(b) rt_mutex_lock_killable(b)
  1429. #define ww_mutex_base_unlock(b) rt_mutex_unlock(b)
  1430. #else
  1431. #define ww_mutex_base_lock(b) mutex_lock(b)
  1432. #define ww_mutex_base_trylock(b) mutex_trylock(b)
  1433. #define ww_mutex_base_lock_nest_lock(b, b2) mutex_lock_nest_lock(b, b2)
  1434. #define ww_mutex_base_lock_interruptible(b) mutex_lock_interruptible(b)
  1435. #define ww_mutex_base_lock_killable(b) mutex_lock_killable(b)
  1436. #define ww_mutex_base_unlock(b) mutex_unlock(b)
  1437. #endif
  1438. static void ww_test_normal(void)
  1439. {
  1440. int ret;
  1441. /*
  1442. * None of the ww_mutex codepaths should be taken in the 'normal'
  1443. * mutex calls. The easiest way to verify this is by using the
  1444. * normal mutex calls, and making sure o.ctx is unmodified.
  1445. */
  1446. /* mutex_lock (and indirectly, mutex_lock_nested) */
  1447. o.ctx = (void *)~0UL;
  1448. ww_mutex_base_lock(&o.base);
  1449. ww_mutex_base_unlock(&o.base);
  1450. WARN_ON(o.ctx != (void *)~0UL);
  1451. /* mutex_lock_interruptible (and *_nested) */
  1452. o.ctx = (void *)~0UL;
  1453. ret = ww_mutex_base_lock_interruptible(&o.base);
  1454. if (!ret)
  1455. ww_mutex_base_unlock(&o.base);
  1456. else
  1457. WARN_ON(1);
  1458. WARN_ON(o.ctx != (void *)~0UL);
  1459. /* mutex_lock_killable (and *_nested) */
  1460. o.ctx = (void *)~0UL;
  1461. ret = ww_mutex_base_lock_killable(&o.base);
  1462. if (!ret)
  1463. ww_mutex_base_unlock(&o.base);
  1464. else
  1465. WARN_ON(1);
  1466. WARN_ON(o.ctx != (void *)~0UL);
  1467. /* trylock, succeeding */
  1468. o.ctx = (void *)~0UL;
  1469. ret = ww_mutex_base_trylock(&o.base);
  1470. WARN_ON(!ret);
  1471. if (ret)
  1472. ww_mutex_base_unlock(&o.base);
  1473. else
  1474. WARN_ON(1);
  1475. WARN_ON(o.ctx != (void *)~0UL);
  1476. /* trylock, failing */
  1477. o.ctx = (void *)~0UL;
  1478. ww_mutex_base_lock(&o.base);
  1479. ret = ww_mutex_base_trylock(&o.base);
  1480. WARN_ON(ret);
  1481. ww_mutex_base_unlock(&o.base);
  1482. WARN_ON(o.ctx != (void *)~0UL);
  1483. WWAI(&t);
  1484. /* nest_lock */
  1485. o.ctx = (void *)~0UL;
  1486. ww_mutex_base_lock_nest_lock(&o.base, &t);
  1487. ww_mutex_base_unlock(&o.base);
  1488. WARN_ON(o.ctx != (void *)~0UL);
  1489. }
  1490. static void ww_test_two_contexts(void)
  1491. {
  1492. WWAI(&t);
  1493. WWAI(&t2);
  1494. }
  1495. static void ww_test_diff_class(void)
  1496. {
  1497. WWAI(&t);
  1498. #ifdef DEBUG_WW_MUTEXES
  1499. t.ww_class = NULL;
  1500. #endif
  1501. WWL(&o, &t);
  1502. }
  1503. static void ww_test_context_done_twice(void)
  1504. {
  1505. WWAI(&t);
  1506. WWAD(&t);
  1507. WWAD(&t);
  1508. WWAF(&t);
  1509. }
  1510. static void ww_test_context_unlock_twice(void)
  1511. {
  1512. WWAI(&t);
  1513. WWAD(&t);
  1514. WWAF(&t);
  1515. WWAF(&t);
  1516. }
  1517. static void ww_test_context_fini_early(void)
  1518. {
  1519. WWAI(&t);
  1520. WWL(&o, &t);
  1521. WWAD(&t);
  1522. WWAF(&t);
  1523. }
  1524. static void ww_test_context_lock_after_done(void)
  1525. {
  1526. WWAI(&t);
  1527. WWAD(&t);
  1528. WWL(&o, &t);
  1529. }
  1530. static void ww_test_object_unlock_twice(void)
  1531. {
  1532. WWL1(&o);
  1533. WWU(&o);
  1534. WWU(&o);
  1535. }
  1536. static void ww_test_object_lock_unbalanced(void)
  1537. {
  1538. WWAI(&t);
  1539. WWL(&o, &t);
  1540. t.acquired = 0;
  1541. WWU(&o);
  1542. WWAF(&t);
  1543. }
  1544. static void ww_test_object_lock_stale_context(void)
  1545. {
  1546. WWAI(&t);
  1547. o.ctx = &t2;
  1548. WWL(&o, &t);
  1549. }
  1550. static void ww_test_edeadlk_normal(void)
  1551. {
  1552. int ret;
  1553. ww_mutex_base_lock(&o2.base);
  1554. o2.ctx = &t2;
  1555. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1556. WWAI(&t);
  1557. t2 = t;
  1558. t2.stamp--;
  1559. ret = WWL(&o, &t);
  1560. WARN_ON(ret);
  1561. ret = WWL(&o2, &t);
  1562. WARN_ON(ret != -EDEADLK);
  1563. o2.ctx = NULL;
  1564. mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
  1565. ww_mutex_base_unlock(&o2.base);
  1566. WWU(&o);
  1567. WWL(&o2, &t);
  1568. }
  1569. static void ww_test_edeadlk_normal_slow(void)
  1570. {
  1571. int ret;
  1572. ww_mutex_base_lock(&o2.base);
  1573. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1574. o2.ctx = &t2;
  1575. WWAI(&t);
  1576. t2 = t;
  1577. t2.stamp--;
  1578. ret = WWL(&o, &t);
  1579. WARN_ON(ret);
  1580. ret = WWL(&o2, &t);
  1581. WARN_ON(ret != -EDEADLK);
  1582. o2.ctx = NULL;
  1583. mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
  1584. ww_mutex_base_unlock(&o2.base);
  1585. WWU(&o);
  1586. ww_mutex_lock_slow(&o2, &t);
  1587. }
  1588. static void ww_test_edeadlk_no_unlock(void)
  1589. {
  1590. int ret;
  1591. ww_mutex_base_lock(&o2.base);
  1592. o2.ctx = &t2;
  1593. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1594. WWAI(&t);
  1595. t2 = t;
  1596. t2.stamp--;
  1597. ret = WWL(&o, &t);
  1598. WARN_ON(ret);
  1599. ret = WWL(&o2, &t);
  1600. WARN_ON(ret != -EDEADLK);
  1601. o2.ctx = NULL;
  1602. mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
  1603. ww_mutex_base_unlock(&o2.base);
  1604. WWL(&o2, &t);
  1605. }
  1606. static void ww_test_edeadlk_no_unlock_slow(void)
  1607. {
  1608. int ret;
  1609. ww_mutex_base_lock(&o2.base);
  1610. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1611. o2.ctx = &t2;
  1612. WWAI(&t);
  1613. t2 = t;
  1614. t2.stamp--;
  1615. ret = WWL(&o, &t);
  1616. WARN_ON(ret);
  1617. ret = WWL(&o2, &t);
  1618. WARN_ON(ret != -EDEADLK);
  1619. o2.ctx = NULL;
  1620. mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
  1621. ww_mutex_base_unlock(&o2.base);
  1622. ww_mutex_lock_slow(&o2, &t);
  1623. }
  1624. static void ww_test_edeadlk_acquire_more(void)
  1625. {
  1626. int ret;
  1627. ww_mutex_base_lock(&o2.base);
  1628. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1629. o2.ctx = &t2;
  1630. WWAI(&t);
  1631. t2 = t;
  1632. t2.stamp--;
  1633. ret = WWL(&o, &t);
  1634. WARN_ON(ret);
  1635. ret = WWL(&o2, &t);
  1636. WARN_ON(ret != -EDEADLK);
  1637. ret = WWL(&o3, &t);
  1638. }
  1639. static void ww_test_edeadlk_acquire_more_slow(void)
  1640. {
  1641. int ret;
  1642. ww_mutex_base_lock(&o2.base);
  1643. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1644. o2.ctx = &t2;
  1645. WWAI(&t);
  1646. t2 = t;
  1647. t2.stamp--;
  1648. ret = WWL(&o, &t);
  1649. WARN_ON(ret);
  1650. ret = WWL(&o2, &t);
  1651. WARN_ON(ret != -EDEADLK);
  1652. ww_mutex_lock_slow(&o3, &t);
  1653. }
  1654. static void ww_test_edeadlk_acquire_more_edeadlk(void)
  1655. {
  1656. int ret;
  1657. ww_mutex_base_lock(&o2.base);
  1658. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1659. o2.ctx = &t2;
  1660. ww_mutex_base_lock(&o3.base);
  1661. mutex_release(&o3.base.dep_map, _THIS_IP_);
  1662. o3.ctx = &t2;
  1663. WWAI(&t);
  1664. t2 = t;
  1665. t2.stamp--;
  1666. ret = WWL(&o, &t);
  1667. WARN_ON(ret);
  1668. ret = WWL(&o2, &t);
  1669. WARN_ON(ret != -EDEADLK);
  1670. ret = WWL(&o3, &t);
  1671. WARN_ON(ret != -EDEADLK);
  1672. }
  1673. static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
  1674. {
  1675. int ret;
  1676. ww_mutex_base_lock(&o2.base);
  1677. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1678. o2.ctx = &t2;
  1679. ww_mutex_base_lock(&o3.base);
  1680. mutex_release(&o3.base.dep_map, _THIS_IP_);
  1681. o3.ctx = &t2;
  1682. WWAI(&t);
  1683. t2 = t;
  1684. t2.stamp--;
  1685. ret = WWL(&o, &t);
  1686. WARN_ON(ret);
  1687. ret = WWL(&o2, &t);
  1688. WARN_ON(ret != -EDEADLK);
  1689. ww_mutex_lock_slow(&o3, &t);
  1690. }
  1691. static void ww_test_edeadlk_acquire_wrong(void)
  1692. {
  1693. int ret;
  1694. ww_mutex_base_lock(&o2.base);
  1695. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1696. o2.ctx = &t2;
  1697. WWAI(&t);
  1698. t2 = t;
  1699. t2.stamp--;
  1700. ret = WWL(&o, &t);
  1701. WARN_ON(ret);
  1702. ret = WWL(&o2, &t);
  1703. WARN_ON(ret != -EDEADLK);
  1704. if (!ret)
  1705. WWU(&o2);
  1706. WWU(&o);
  1707. ret = WWL(&o3, &t);
  1708. }
  1709. static void ww_test_edeadlk_acquire_wrong_slow(void)
  1710. {
  1711. int ret;
  1712. ww_mutex_base_lock(&o2.base);
  1713. mutex_release(&o2.base.dep_map, _THIS_IP_);
  1714. o2.ctx = &t2;
  1715. WWAI(&t);
  1716. t2 = t;
  1717. t2.stamp--;
  1718. ret = WWL(&o, &t);
  1719. WARN_ON(ret);
  1720. ret = WWL(&o2, &t);
  1721. WARN_ON(ret != -EDEADLK);
  1722. if (!ret)
  1723. WWU(&o2);
  1724. WWU(&o);
  1725. ww_mutex_lock_slow(&o3, &t);
  1726. }
  1727. static void ww_test_spin_nest_unlocked(void)
  1728. {
  1729. spin_lock_nest_lock(&lock_A, &o.base);
  1730. U(A);
  1731. }
  1732. /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
  1733. static void ww_test_spin_nest_lock(void)
  1734. {
  1735. spin_lock(&lock_X1);
  1736. spin_lock_nest_lock(&lock_Y1, &lock_X1);
  1737. spin_lock(&lock_A);
  1738. spin_lock_nest_lock(&lock_Y2, &lock_X1);
  1739. spin_unlock(&lock_A);
  1740. spin_unlock(&lock_Y2);
  1741. spin_unlock(&lock_Y1);
  1742. spin_unlock(&lock_X1);
  1743. }
  1744. static void ww_test_unneeded_slow(void)
  1745. {
  1746. WWAI(&t);
  1747. ww_mutex_lock_slow(&o, &t);
  1748. }
  1749. static void ww_test_context_block(void)
  1750. {
  1751. int ret;
  1752. WWAI(&t);
  1753. ret = WWL(&o, &t);
  1754. WARN_ON(ret);
  1755. WWL1(&o2);
  1756. }
  1757. static void ww_test_context_try(void)
  1758. {
  1759. int ret;
  1760. WWAI(&t);
  1761. ret = WWL(&o, &t);
  1762. WARN_ON(ret);
  1763. ret = WWT(&o2);
  1764. WARN_ON(!ret);
  1765. WWU(&o2);
  1766. WWU(&o);
  1767. }
  1768. static void ww_test_context_context(void)
  1769. {
  1770. int ret;
  1771. WWAI(&t);
  1772. ret = WWL(&o, &t);
  1773. WARN_ON(ret);
  1774. ret = WWL(&o2, &t);
  1775. WARN_ON(ret);
  1776. WWU(&o2);
  1777. WWU(&o);
  1778. }
  1779. static void ww_test_try_block(void)
  1780. {
  1781. bool ret;
  1782. ret = WWT(&o);
  1783. WARN_ON(!ret);
  1784. WWL1(&o2);
  1785. WWU(&o2);
  1786. WWU(&o);
  1787. }
  1788. static void ww_test_try_try(void)
  1789. {
  1790. bool ret;
  1791. ret = WWT(&o);
  1792. WARN_ON(!ret);
  1793. ret = WWT(&o2);
  1794. WARN_ON(!ret);
  1795. WWU(&o2);
  1796. WWU(&o);
  1797. }
  1798. static void ww_test_try_context(void)
  1799. {
  1800. int ret;
  1801. ret = WWT(&o);
  1802. WARN_ON(!ret);
  1803. WWAI(&t);
  1804. ret = WWL(&o2, &t);
  1805. WARN_ON(ret);
  1806. }
  1807. static void ww_test_block_block(void)
  1808. {
  1809. WWL1(&o);
  1810. WWL1(&o2);
  1811. }
  1812. static void ww_test_block_try(void)
  1813. {
  1814. bool ret;
  1815. WWL1(&o);
  1816. ret = WWT(&o2);
  1817. WARN_ON(!ret);
  1818. }
  1819. static void ww_test_block_context(void)
  1820. {
  1821. int ret;
  1822. WWL1(&o);
  1823. WWAI(&t);
  1824. ret = WWL(&o2, &t);
  1825. WARN_ON(ret);
  1826. }
  1827. static void ww_test_spin_block(void)
  1828. {
  1829. L(A);
  1830. U(A);
  1831. WWL1(&o);
  1832. L(A);
  1833. U(A);
  1834. WWU(&o);
  1835. L(A);
  1836. WWL1(&o);
  1837. WWU(&o);
  1838. U(A);
  1839. }
  1840. static void ww_test_spin_try(void)
  1841. {
  1842. bool ret;
  1843. L(A);
  1844. U(A);
  1845. ret = WWT(&o);
  1846. WARN_ON(!ret);
  1847. L(A);
  1848. U(A);
  1849. WWU(&o);
  1850. L(A);
  1851. ret = WWT(&o);
  1852. WARN_ON(!ret);
  1853. WWU(&o);
  1854. U(A);
  1855. }
  1856. static void ww_test_spin_context(void)
  1857. {
  1858. int ret;
  1859. L(A);
  1860. U(A);
  1861. WWAI(&t);
  1862. ret = WWL(&o, &t);
  1863. WARN_ON(ret);
  1864. L(A);
  1865. U(A);
  1866. WWU(&o);
  1867. L(A);
  1868. ret = WWL(&o, &t);
  1869. WARN_ON(ret);
  1870. WWU(&o);
  1871. U(A);
  1872. }
  1873. static void ww_tests(void)
  1874. {
  1875. printk(" --------------------------------------------------------------------------\n");
  1876. printk(" | Wound/wait tests |\n");
  1877. printk(" ---------------------\n");
  1878. print_testname("ww api failures");
  1879. dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
  1880. dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
  1881. dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
  1882. pr_cont("\n");
  1883. print_testname("ww contexts mixing");
  1884. dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
  1885. dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
  1886. pr_cont("\n");
  1887. print_testname("finishing ww context");
  1888. dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
  1889. dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
  1890. dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
  1891. dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
  1892. pr_cont("\n");
  1893. print_testname("locking mismatches");
  1894. dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
  1895. dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
  1896. dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
  1897. pr_cont("\n");
  1898. print_testname("EDEADLK handling");
  1899. dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
  1900. dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
  1901. dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
  1902. dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
  1903. dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
  1904. dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
  1905. dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
  1906. dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
  1907. dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
  1908. dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
  1909. pr_cont("\n");
  1910. print_testname("spinlock nest unlocked");
  1911. dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
  1912. pr_cont("\n");
  1913. print_testname("spinlock nest test");
  1914. dotest(ww_test_spin_nest_lock, SUCCESS, LOCKTYPE_WW);
  1915. pr_cont("\n");
  1916. printk(" -----------------------------------------------------\n");
  1917. printk(" |block | try |context|\n");
  1918. printk(" -----------------------------------------------------\n");
  1919. print_testname("context");
  1920. dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
  1921. dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
  1922. dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
  1923. pr_cont("\n");
  1924. print_testname("try");
  1925. dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
  1926. dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
  1927. dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
  1928. pr_cont("\n");
  1929. print_testname("block");
  1930. dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
  1931. dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
  1932. dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
  1933. pr_cont("\n");
  1934. print_testname("spinlock");
  1935. dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
  1936. dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
  1937. dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
  1938. pr_cont("\n");
  1939. }
  1940. /*
  1941. * <in hardirq handler>
  1942. * read_lock(&A);
  1943. * <hardirq disable>
  1944. * spin_lock(&B);
  1945. * spin_lock(&B);
  1946. * read_lock(&A);
  1947. *
  1948. * is a deadlock.
  1949. */
  1950. static void queued_read_lock_hardirq_RE_Er(void)
  1951. {
  1952. HARDIRQ_ENTER();
  1953. read_lock(&rwlock_A);
  1954. LOCK(B);
  1955. UNLOCK(B);
  1956. read_unlock(&rwlock_A);
  1957. HARDIRQ_EXIT();
  1958. HARDIRQ_DISABLE();
  1959. LOCK(B);
  1960. read_lock(&rwlock_A);
  1961. read_unlock(&rwlock_A);
  1962. UNLOCK(B);
  1963. HARDIRQ_ENABLE();
  1964. }
  1965. /*
  1966. * <in hardirq handler>
  1967. * spin_lock(&B);
  1968. * <hardirq disable>
  1969. * read_lock(&A);
  1970. * read_lock(&A);
  1971. * spin_lock(&B);
  1972. *
  1973. * is not a deadlock.
  1974. */
  1975. static void queued_read_lock_hardirq_ER_rE(void)
  1976. {
  1977. HARDIRQ_ENTER();
  1978. LOCK(B);
  1979. read_lock(&rwlock_A);
  1980. read_unlock(&rwlock_A);
  1981. UNLOCK(B);
  1982. HARDIRQ_EXIT();
  1983. HARDIRQ_DISABLE();
  1984. read_lock(&rwlock_A);
  1985. LOCK(B);
  1986. UNLOCK(B);
  1987. read_unlock(&rwlock_A);
  1988. HARDIRQ_ENABLE();
  1989. }
  1990. /*
  1991. * <hardirq disable>
  1992. * spin_lock(&B);
  1993. * read_lock(&A);
  1994. * <in hardirq handler>
  1995. * spin_lock(&B);
  1996. * read_lock(&A);
  1997. *
  1998. * is a deadlock. Because the two read_lock()s are both non-recursive readers.
  1999. */
  2000. static void queued_read_lock_hardirq_inversion(void)
  2001. {
  2002. HARDIRQ_ENTER();
  2003. LOCK(B);
  2004. UNLOCK(B);
  2005. HARDIRQ_EXIT();
  2006. HARDIRQ_DISABLE();
  2007. LOCK(B);
  2008. read_lock(&rwlock_A);
  2009. read_unlock(&rwlock_A);
  2010. UNLOCK(B);
  2011. HARDIRQ_ENABLE();
  2012. read_lock(&rwlock_A);
  2013. read_unlock(&rwlock_A);
  2014. }
  2015. static void queued_read_lock_tests(void)
  2016. {
  2017. printk(" --------------------------------------------------------------------------\n");
  2018. printk(" | queued read lock tests |\n");
  2019. printk(" ---------------------------\n");
  2020. print_testname("hardirq read-lock/lock-read");
  2021. dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK);
  2022. pr_cont("\n");
  2023. print_testname("hardirq lock-read/read-lock");
  2024. dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
  2025. pr_cont("\n");
  2026. print_testname("hardirq inversion");
  2027. dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
  2028. pr_cont("\n");
  2029. }
  2030. static void fs_reclaim_correct_nesting(void)
  2031. {
  2032. fs_reclaim_acquire(GFP_KERNEL);
  2033. might_alloc(GFP_NOFS);
  2034. fs_reclaim_release(GFP_KERNEL);
  2035. }
  2036. static void fs_reclaim_wrong_nesting(void)
  2037. {
  2038. fs_reclaim_acquire(GFP_KERNEL);
  2039. might_alloc(GFP_KERNEL);
  2040. fs_reclaim_release(GFP_KERNEL);
  2041. }
  2042. static void fs_reclaim_protected_nesting(void)
  2043. {
  2044. unsigned int flags;
  2045. fs_reclaim_acquire(GFP_KERNEL);
  2046. flags = memalloc_nofs_save();
  2047. might_alloc(GFP_KERNEL);
  2048. memalloc_nofs_restore(flags);
  2049. fs_reclaim_release(GFP_KERNEL);
  2050. }
  2051. static void fs_reclaim_tests(void)
  2052. {
  2053. printk(" --------------------\n");
  2054. printk(" | fs_reclaim tests |\n");
  2055. printk(" --------------------\n");
  2056. print_testname("correct nesting");
  2057. dotest(fs_reclaim_correct_nesting, SUCCESS, 0);
  2058. pr_cont("\n");
  2059. print_testname("wrong nesting");
  2060. dotest(fs_reclaim_wrong_nesting, FAILURE, 0);
  2061. pr_cont("\n");
  2062. print_testname("protected nesting");
  2063. dotest(fs_reclaim_protected_nesting, SUCCESS, 0);
  2064. pr_cont("\n");
  2065. }
  2066. /* Defines guard classes to create contexts */
  2067. DEFINE_LOCK_GUARD_0(HARDIRQ, HARDIRQ_ENTER(), HARDIRQ_EXIT())
  2068. DEFINE_LOCK_GUARD_0(NOTTHREADED_HARDIRQ,
  2069. do {
  2070. local_irq_disable();
  2071. __irq_enter();
  2072. WARN_ON(!in_hardirq());
  2073. } while(0), HARDIRQ_EXIT())
  2074. DEFINE_LOCK_GUARD_0(SOFTIRQ, SOFTIRQ_ENTER(), SOFTIRQ_EXIT())
  2075. /* Define RCU guards, should go away when RCU has its own guard definitions */
  2076. DEFINE_LOCK_GUARD_0(RCU, rcu_read_lock(), rcu_read_unlock())
  2077. DEFINE_LOCK_GUARD_0(RCU_BH, rcu_read_lock_bh(), rcu_read_unlock_bh())
  2078. DEFINE_LOCK_GUARD_0(RCU_SCHED, rcu_read_lock_sched(), rcu_read_unlock_sched())
  2079. #define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock) \
  2080. \
  2081. static void __maybe_unused inner##_in_##outer(void) \
  2082. { \
  2083. /* Relies the reversed clean-up ordering: inner first */ \
  2084. guard(outer)(outer_lock); \
  2085. guard(inner)(inner_lock); \
  2086. }
  2087. /*
  2088. * wait contexts (considering PREEMPT_RT)
  2089. *
  2090. * o: inner is allowed in outer
  2091. * x: inner is disallowed in outer
  2092. *
  2093. * \ inner | RCU | RAW_SPIN | SPIN | MUTEX
  2094. * outer \ | | | |
  2095. * ---------------+-------+----------+------+-------
  2096. * HARDIRQ | o | o | o | x
  2097. * ---------------+-------+----------+------+-------
  2098. * NOTTHREADED_IRQ| o | o | x | x
  2099. * ---------------+-------+----------+------+-------
  2100. * SOFTIRQ | o | o | o | x
  2101. * ---------------+-------+----------+------+-------
  2102. * RCU | o | o | o | x
  2103. * ---------------+-------+----------+------+-------
  2104. * RCU_BH | o | o | o | x
  2105. * ---------------+-------+----------+------+-------
  2106. * RCU_SCHED | o | o | x | x
  2107. * ---------------+-------+----------+------+-------
  2108. * RAW_SPIN | o | o | x | x
  2109. * ---------------+-------+----------+------+-------
  2110. * SPIN | o | o | o | x
  2111. * ---------------+-------+----------+------+-------
  2112. * MUTEX | o | o | o | o
  2113. * ---------------+-------+----------+------+-------
  2114. */
  2115. #define GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(inner, inner_lock) \
  2116. GENERATE_2_CONTEXT_TESTCASE(HARDIRQ, , inner, inner_lock) \
  2117. GENERATE_2_CONTEXT_TESTCASE(NOTTHREADED_HARDIRQ, , inner, inner_lock) \
  2118. GENERATE_2_CONTEXT_TESTCASE(SOFTIRQ, , inner, inner_lock) \
  2119. GENERATE_2_CONTEXT_TESTCASE(RCU, , inner, inner_lock) \
  2120. GENERATE_2_CONTEXT_TESTCASE(RCU_BH, , inner, inner_lock) \
  2121. GENERATE_2_CONTEXT_TESTCASE(RCU_SCHED, , inner, inner_lock) \
  2122. GENERATE_2_CONTEXT_TESTCASE(raw_spinlock, &raw_lock_A, inner, inner_lock) \
  2123. GENERATE_2_CONTEXT_TESTCASE(spinlock, &lock_A, inner, inner_lock) \
  2124. GENERATE_2_CONTEXT_TESTCASE(mutex, &mutex_A, inner, inner_lock)
  2125. GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RCU, )
  2126. GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(raw_spinlock, &raw_lock_B)
  2127. GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(spinlock, &lock_B)
  2128. GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(mutex, &mutex_B)
  2129. /* the outer context allows all kinds of preemption */
  2130. #define DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(outer) \
  2131. dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
  2132. dotest(raw_spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
  2133. dotest(spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
  2134. dotest(mutex_in_##outer, SUCCESS, LOCKTYPE_MUTEX); \
  2135. /*
  2136. * the outer context only allows the preemption introduced by spinlock_t (which
  2137. * is a sleepable lock for PREEMPT_RT)
  2138. */
  2139. #define DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(outer) \
  2140. dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
  2141. dotest(raw_spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
  2142. dotest(spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
  2143. dotest(mutex_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
  2144. /* the outer doesn't allows any kind of preemption */
  2145. #define DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(outer) \
  2146. dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
  2147. dotest(raw_spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
  2148. dotest(spinlock_in_##outer, FAILURE, LOCKTYPE_SPIN); \
  2149. dotest(mutex_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
  2150. static void wait_context_tests(void)
  2151. {
  2152. printk(" --------------------------------------------------------------------------\n");
  2153. printk(" | wait context tests |\n");
  2154. printk(" --------------------------------------------------------------------------\n");
  2155. printk(" | rcu | raw | spin |mutex |\n");
  2156. printk(" --------------------------------------------------------------------------\n");
  2157. print_testname("in hardirq context");
  2158. DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(HARDIRQ);
  2159. pr_cont("\n");
  2160. print_testname("in hardirq context (not threaded)");
  2161. DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ);
  2162. pr_cont("\n");
  2163. print_testname("in softirq context");
  2164. DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ);
  2165. pr_cont("\n");
  2166. print_testname("in RCU context");
  2167. DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU);
  2168. pr_cont("\n");
  2169. print_testname("in RCU-bh context");
  2170. DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH);
  2171. pr_cont("\n");
  2172. print_testname("in RCU-sched context");
  2173. DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED);
  2174. pr_cont("\n");
  2175. print_testname("in RAW_SPINLOCK context");
  2176. DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(raw_spinlock);
  2177. pr_cont("\n");
  2178. print_testname("in SPINLOCK context");
  2179. DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(spinlock);
  2180. pr_cont("\n");
  2181. print_testname("in MUTEX context");
  2182. DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(mutex);
  2183. pr_cont("\n");
  2184. }
  2185. static void local_lock_2(void)
  2186. {
  2187. local_lock(&local_A); /* IRQ-ON */
  2188. local_unlock(&local_A);
  2189. HARDIRQ_ENTER();
  2190. spin_lock(&lock_A); /* IN-IRQ */
  2191. spin_unlock(&lock_A);
  2192. HARDIRQ_EXIT()
  2193. HARDIRQ_DISABLE();
  2194. spin_lock(&lock_A);
  2195. local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle, false */
  2196. local_unlock(&local_A);
  2197. spin_unlock(&lock_A);
  2198. HARDIRQ_ENABLE();
  2199. }
  2200. static void local_lock_3A(void)
  2201. {
  2202. local_lock(&local_A); /* IRQ-ON */
  2203. spin_lock(&lock_B); /* IRQ-ON */
  2204. spin_unlock(&lock_B);
  2205. local_unlock(&local_A);
  2206. HARDIRQ_ENTER();
  2207. spin_lock(&lock_A); /* IN-IRQ */
  2208. spin_unlock(&lock_A);
  2209. HARDIRQ_EXIT()
  2210. HARDIRQ_DISABLE();
  2211. spin_lock(&lock_A);
  2212. local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
  2213. local_unlock(&local_A);
  2214. spin_unlock(&lock_A);
  2215. HARDIRQ_ENABLE();
  2216. }
  2217. static void local_lock_3B(void)
  2218. {
  2219. local_lock(&local_A); /* IRQ-ON */
  2220. spin_lock(&lock_B); /* IRQ-ON */
  2221. spin_unlock(&lock_B);
  2222. local_unlock(&local_A);
  2223. HARDIRQ_ENTER();
  2224. spin_lock(&lock_A); /* IN-IRQ */
  2225. spin_unlock(&lock_A);
  2226. HARDIRQ_EXIT()
  2227. HARDIRQ_DISABLE();
  2228. spin_lock(&lock_A);
  2229. local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
  2230. local_unlock(&local_A);
  2231. spin_unlock(&lock_A);
  2232. HARDIRQ_ENABLE();
  2233. HARDIRQ_DISABLE();
  2234. spin_lock(&lock_A);
  2235. spin_lock(&lock_B); /* IN-IRQ <-> IRQ-ON cycle, true */
  2236. spin_unlock(&lock_B);
  2237. spin_unlock(&lock_A);
  2238. HARDIRQ_DISABLE();
  2239. }
  2240. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2241. static inline const char *rw_semaphore_lockdep_name(struct rw_semaphore *rwsem)
  2242. {
  2243. return rwsem->dep_map.name;
  2244. }
  2245. #else
  2246. static inline const char *rw_semaphore_lockdep_name(struct rw_semaphore *rwsem)
  2247. {
  2248. return NULL;
  2249. }
  2250. #endif
  2251. static void test_lockdep_set_subclass_name(void)
  2252. {
  2253. const char *name_before = rw_semaphore_lockdep_name(&rwsem_X1);
  2254. const char *name_after;
  2255. lockdep_set_subclass(&rwsem_X1, 1);
  2256. name_after = rw_semaphore_lockdep_name(&rwsem_X1);
  2257. DEBUG_LOCKS_WARN_ON(name_before != name_after);
  2258. }
  2259. /*
  2260. * lockdep_set_subclass() should reuse the existing lock class name instead
  2261. * of creating a new one.
  2262. */
  2263. static void lockdep_set_subclass_name_test(void)
  2264. {
  2265. printk(" --------------------------------------------------------------------------\n");
  2266. printk(" | lockdep_set_subclass() name test|\n");
  2267. printk(" -----------------------------------\n");
  2268. print_testname("compare name before and after");
  2269. dotest(test_lockdep_set_subclass_name, SUCCESS, LOCKTYPE_RWSEM);
  2270. pr_cont("\n");
  2271. }
  2272. static void local_lock_tests(void)
  2273. {
  2274. printk(" --------------------------------------------------------------------------\n");
  2275. printk(" | local_lock tests |\n");
  2276. printk(" ---------------------\n");
  2277. print_testname("local_lock inversion 2");
  2278. dotest(local_lock_2, SUCCESS, LOCKTYPE_LL);
  2279. pr_cont("\n");
  2280. print_testname("local_lock inversion 3A");
  2281. dotest(local_lock_3A, SUCCESS, LOCKTYPE_LL);
  2282. pr_cont("\n");
  2283. print_testname("local_lock inversion 3B");
  2284. dotest(local_lock_3B, FAILURE, LOCKTYPE_LL);
  2285. pr_cont("\n");
  2286. }
  2287. static void hardirq_deadlock_softirq_not_deadlock(void)
  2288. {
  2289. /* mutex_A is hardirq-unsafe and softirq-unsafe */
  2290. /* mutex_A -> lock_C */
  2291. mutex_lock(&mutex_A);
  2292. HARDIRQ_DISABLE();
  2293. spin_lock(&lock_C);
  2294. spin_unlock(&lock_C);
  2295. HARDIRQ_ENABLE();
  2296. mutex_unlock(&mutex_A);
  2297. /* lock_A is hardirq-safe */
  2298. HARDIRQ_ENTER();
  2299. spin_lock(&lock_A);
  2300. spin_unlock(&lock_A);
  2301. HARDIRQ_EXIT();
  2302. /* lock_A -> lock_B */
  2303. HARDIRQ_DISABLE();
  2304. spin_lock(&lock_A);
  2305. spin_lock(&lock_B);
  2306. spin_unlock(&lock_B);
  2307. spin_unlock(&lock_A);
  2308. HARDIRQ_ENABLE();
  2309. /* lock_B -> lock_C */
  2310. HARDIRQ_DISABLE();
  2311. spin_lock(&lock_B);
  2312. spin_lock(&lock_C);
  2313. spin_unlock(&lock_C);
  2314. spin_unlock(&lock_B);
  2315. HARDIRQ_ENABLE();
  2316. /* lock_D is softirq-safe */
  2317. SOFTIRQ_ENTER();
  2318. spin_lock(&lock_D);
  2319. spin_unlock(&lock_D);
  2320. SOFTIRQ_EXIT();
  2321. /* And lock_D is hardirq-unsafe */
  2322. SOFTIRQ_DISABLE();
  2323. spin_lock(&lock_D);
  2324. spin_unlock(&lock_D);
  2325. SOFTIRQ_ENABLE();
  2326. /*
  2327. * mutex_A -> lock_C -> lock_D is softirq-unsafe -> softirq-safe, not
  2328. * deadlock.
  2329. *
  2330. * lock_A -> lock_B -> lock_C -> lock_D is hardirq-safe ->
  2331. * hardirq-unsafe, deadlock.
  2332. */
  2333. HARDIRQ_DISABLE();
  2334. spin_lock(&lock_C);
  2335. spin_lock(&lock_D);
  2336. spin_unlock(&lock_D);
  2337. spin_unlock(&lock_C);
  2338. HARDIRQ_ENABLE();
  2339. }
  2340. void locking_selftest(void)
  2341. {
  2342. /*
  2343. * Got a locking failure before the selftest ran?
  2344. */
  2345. if (!debug_locks) {
  2346. printk("----------------------------------\n");
  2347. printk("| Locking API testsuite disabled |\n");
  2348. printk("----------------------------------\n");
  2349. return;
  2350. }
  2351. /*
  2352. * treats read_lock() as recursive read locks for testing purpose
  2353. */
  2354. force_read_lock_recursive = 1;
  2355. /*
  2356. * Run the testsuite:
  2357. */
  2358. printk("------------------------\n");
  2359. printk("| Locking API testsuite:\n");
  2360. printk("----------------------------------------------------------------------------\n");
  2361. printk(" | spin |wlock |rlock |mutex | wsem | rsem |rtmutex\n");
  2362. printk(" --------------------------------------------------------------------------\n");
  2363. init_shared_classes();
  2364. lockdep_set_selftest_task(current);
  2365. DO_TESTCASE_6R("A-A deadlock", AA);
  2366. DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
  2367. DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
  2368. DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
  2369. DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
  2370. DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
  2371. DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
  2372. DO_TESTCASE_6("double unlock", double_unlock);
  2373. DO_TESTCASE_6("initialize held", init_held);
  2374. printk(" --------------------------------------------------------------------------\n");
  2375. print_testname("recursive read-lock");
  2376. pr_cont(" |");
  2377. dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
  2378. pr_cont(" |");
  2379. dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
  2380. pr_cont("\n");
  2381. print_testname("recursive read-lock #2");
  2382. pr_cont(" |");
  2383. dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
  2384. pr_cont(" |");
  2385. dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
  2386. pr_cont("\n");
  2387. print_testname("mixed read-write-lock");
  2388. pr_cont(" |");
  2389. dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
  2390. pr_cont(" |");
  2391. dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
  2392. pr_cont("\n");
  2393. print_testname("mixed write-read-lock");
  2394. pr_cont(" |");
  2395. dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
  2396. pr_cont(" |");
  2397. dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
  2398. pr_cont("\n");
  2399. print_testname("mixed read-lock/lock-write ABBA");
  2400. pr_cont(" |");
  2401. dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
  2402. pr_cont(" |");
  2403. dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
  2404. print_testname("mixed read-lock/lock-read ABBA");
  2405. pr_cont(" |");
  2406. dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
  2407. pr_cont(" |");
  2408. dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
  2409. print_testname("mixed write-lock/lock-write ABBA");
  2410. pr_cont(" |");
  2411. dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
  2412. pr_cont(" |");
  2413. dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
  2414. print_testname("chain cached mixed R-L/L-W ABBA");
  2415. pr_cont(" |");
  2416. dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
  2417. DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1);
  2418. DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1);
  2419. DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1);
  2420. DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1);
  2421. printk(" --------------------------------------------------------------------------\n");
  2422. /*
  2423. * irq-context testcases:
  2424. */
  2425. DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
  2426. NON_RT(DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A));
  2427. DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
  2428. DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
  2429. DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
  2430. DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
  2431. DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion);
  2432. DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2);
  2433. DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3);
  2434. ww_tests();
  2435. force_read_lock_recursive = 0;
  2436. /*
  2437. * queued_read_lock() specific test cases can be put here
  2438. */
  2439. if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
  2440. queued_read_lock_tests();
  2441. fs_reclaim_tests();
  2442. /* Wait context test cases that are specific for RAW_LOCK_NESTING */
  2443. if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING))
  2444. wait_context_tests();
  2445. local_lock_tests();
  2446. print_testname("hardirq_unsafe_softirq_safe");
  2447. dotest(hardirq_deadlock_softirq_not_deadlock, FAILURE, LOCKTYPE_SPECIAL);
  2448. pr_cont("\n");
  2449. lockdep_set_subclass_name_test();
  2450. if (unexpected_testcase_failures) {
  2451. printk("-----------------------------------------------------------------\n");
  2452. debug_locks = 0;
  2453. printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
  2454. unexpected_testcase_failures, testcase_total);
  2455. printk("-----------------------------------------------------------------\n");
  2456. } else if (expected_testcase_failures && testcase_successes) {
  2457. printk("--------------------------------------------------------\n");
  2458. printk("%3d out of %3d testcases failed, as expected. |\n",
  2459. expected_testcase_failures, testcase_total);
  2460. printk("----------------------------------------------------\n");
  2461. debug_locks = 1;
  2462. } else if (expected_testcase_failures && !testcase_successes) {
  2463. printk("--------------------------------------------------------\n");
  2464. printk("All %3d testcases failed, as expected. |\n",
  2465. expected_testcase_failures);
  2466. printk("----------------------------------------\n");
  2467. debug_locks = 1;
  2468. } else {
  2469. printk("-------------------------------------------------------\n");
  2470. printk("Good, all %3d testcases passed! |\n",
  2471. testcase_successes);
  2472. printk("---------------------------------\n");
  2473. debug_locks = 1;
  2474. }
  2475. lockdep_set_selftest_task(NULL);
  2476. debug_locks_silent = 0;
  2477. }