rcutorture.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Read-Copy Update module-based torture test facility
  4. *
  5. * Copyright (C) IBM Corporation, 2005, 2006
  6. *
  7. * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
  8. * Josh Triplett <josh@joshtriplett.org>
  9. *
  10. * See also: Documentation/RCU/torture.rst
  11. */
  12. #define pr_fmt(fmt) fmt
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/kthread.h>
  18. #include <linux/err.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/smp.h>
  21. #include <linux/rcupdate_wait.h>
  22. #include <linux/rcu_notifier.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sched/signal.h>
  25. #include <uapi/linux/sched/types.h>
  26. #include <linux/atomic.h>
  27. #include <linux/bitops.h>
  28. #include <linux/completion.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/percpu.h>
  31. #include <linux/notifier.h>
  32. #include <linux/reboot.h>
  33. #include <linux/freezer.h>
  34. #include <linux/cpu.h>
  35. #include <linux/delay.h>
  36. #include <linux/stat.h>
  37. #include <linux/srcu.h>
  38. #include <linux/slab.h>
  39. #include <linux/trace_clock.h>
  40. #include <asm/byteorder.h>
  41. #include <linux/torture.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/sched/debug.h>
  44. #include <linux/sched/sysctl.h>
  45. #include <linux/oom.h>
  46. #include <linux/tick.h>
  47. #include <linux/rcupdate_trace.h>
  48. #include <linux/nmi.h>
  49. #include "rcu.h"
  50. MODULE_DESCRIPTION("Read-Copy Update module-based torture test facility");
  51. MODULE_LICENSE("GPL");
  52. MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
  53. /* Bits for ->extendables field, extendables param, and related definitions. */
  54. #define RCUTORTURE_RDR_SHIFT_1 8 /* Put SRCU index in upper bits. */
  55. #define RCUTORTURE_RDR_MASK_1 (1 << RCUTORTURE_RDR_SHIFT_1)
  56. #define RCUTORTURE_RDR_SHIFT_2 9 /* Put SRCU index in upper bits. */
  57. #define RCUTORTURE_RDR_MASK_2 (1 << RCUTORTURE_RDR_SHIFT_2)
  58. #define RCUTORTURE_RDR_BH 0x01 /* Extend readers by disabling bh. */
  59. #define RCUTORTURE_RDR_IRQ 0x02 /* ... disabling interrupts. */
  60. #define RCUTORTURE_RDR_PREEMPT 0x04 /* ... disabling preemption. */
  61. #define RCUTORTURE_RDR_RBH 0x08 /* ... rcu_read_lock_bh(). */
  62. #define RCUTORTURE_RDR_SCHED 0x10 /* ... rcu_read_lock_sched(). */
  63. #define RCUTORTURE_RDR_RCU_1 0x20 /* ... entering another RCU reader. */
  64. #define RCUTORTURE_RDR_RCU_2 0x40 /* ... entering another RCU reader. */
  65. #define RCUTORTURE_RDR_NBITS 7 /* Number of bits defined above. */
  66. #define RCUTORTURE_MAX_EXTEND \
  67. (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ | RCUTORTURE_RDR_PREEMPT | \
  68. RCUTORTURE_RDR_RBH | RCUTORTURE_RDR_SCHED)
  69. #define RCUTORTURE_RDR_MAX_LOOPS 0x7 /* Maximum reader extensions. */
  70. /* Must be power of two minus one. */
  71. #define RCUTORTURE_RDR_MAX_SEGS (RCUTORTURE_RDR_MAX_LOOPS + 3)
  72. torture_param(int, extendables, RCUTORTURE_MAX_EXTEND,
  73. "Extend readers by disabling bh (1), irqs (2), or preempt (4)");
  74. torture_param(int, fqs_duration, 0, "Duration of fqs bursts (us), 0 to disable");
  75. torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
  76. torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
  77. torture_param(int, fwd_progress, 1, "Number of grace-period forward progress tasks (0 to disable)");
  78. torture_param(int, fwd_progress_div, 4, "Fraction of CPU stall to wait");
  79. torture_param(int, fwd_progress_holdoff, 60, "Time between forward-progress tests (s)");
  80. torture_param(bool, fwd_progress_need_resched, 1, "Hide cond_resched() behind need_resched()");
  81. torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
  82. torture_param(bool, gp_cond_exp, false, "Use conditional/async expedited GP wait primitives");
  83. torture_param(bool, gp_cond_full, false, "Use conditional/async full-state GP wait primitives");
  84. torture_param(bool, gp_cond_exp_full, false,
  85. "Use conditional/async full-stateexpedited GP wait primitives");
  86. torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
  87. torture_param(bool, gp_normal, false, "Use normal (non-expedited) GP wait primitives");
  88. torture_param(bool, gp_poll, false, "Use polling GP wait primitives");
  89. torture_param(bool, gp_poll_exp, false, "Use polling expedited GP wait primitives");
  90. torture_param(bool, gp_poll_full, false, "Use polling full-state GP wait primitives");
  91. torture_param(bool, gp_poll_exp_full, false, "Use polling full-state expedited GP wait primitives");
  92. torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
  93. torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
  94. torture_param(int, leakpointer, 0, "Leak pointer dereferences from readers");
  95. torture_param(int, n_barrier_cbs, 0, "# of callbacks/kthreads for barrier testing");
  96. torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
  97. torture_param(int, nreaders, -1, "Number of RCU reader threads");
  98. torture_param(int, object_debug, 0, "Enable debug-object double call_rcu() testing");
  99. torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
  100. torture_param(int, onoff_interval, 0, "Time between CPU hotplugs (jiffies), 0=disable");
  101. torture_param(int, nocbs_nthreads, 0, "Number of NOCB toggle threads, 0 to disable");
  102. torture_param(int, nocbs_toggle, 1000, "Time between toggling nocb state (ms)");
  103. torture_param(int, read_exit_delay, 13, "Delay between read-then-exit episodes (s)");
  104. torture_param(int, read_exit_burst, 16, "# of read-then-exit bursts per episode, zero to disable");
  105. torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
  106. torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
  107. torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
  108. torture_param(int, stall_cpu_holdoff, 10, "Time to wait before starting stall (s).");
  109. torture_param(bool, stall_no_softlockup, false, "Avoid softlockup warning during cpu stall.");
  110. torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
  111. torture_param(int, stall_cpu_block, 0, "Sleep while stalling.");
  112. torture_param(int, stall_cpu_repeat, 0, "Number of additional stalls after the first one.");
  113. torture_param(int, stall_gp_kthread, 0, "Grace-period kthread stall duration (s).");
  114. torture_param(int, stat_interval, 60, "Number of seconds between stats printk()s");
  115. torture_param(int, stutter, 5, "Number of seconds to run/halt test");
  116. torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
  117. torture_param(int, test_boost_duration, 4, "Duration of each boost test, seconds.");
  118. torture_param(int, test_boost_interval, 7, "Interval between boost tests, seconds.");
  119. torture_param(int, test_nmis, 0, "End-test NMI tests, 0 to disable.");
  120. torture_param(bool, test_no_idle_hz, true, "Test support for tickless idle CPUs");
  121. torture_param(int, test_srcu_lockdep, 0, "Test specified SRCU deadlock scenario.");
  122. torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
  123. static char *torture_type = "rcu";
  124. module_param(torture_type, charp, 0444);
  125. MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, srcu, ...)");
  126. static int nrealnocbers;
  127. static int nrealreaders;
  128. static struct task_struct *writer_task;
  129. static struct task_struct **fakewriter_tasks;
  130. static struct task_struct **reader_tasks;
  131. static struct task_struct **nocb_tasks;
  132. static struct task_struct *stats_task;
  133. static struct task_struct *fqs_task;
  134. static struct task_struct *boost_tasks[NR_CPUS];
  135. static struct task_struct *stall_task;
  136. static struct task_struct **fwd_prog_tasks;
  137. static struct task_struct **barrier_cbs_tasks;
  138. static struct task_struct *barrier_task;
  139. static struct task_struct *read_exit_task;
  140. #define RCU_TORTURE_PIPE_LEN 10
  141. // Mailbox-like structure to check RCU global memory ordering.
  142. struct rcu_torture_reader_check {
  143. unsigned long rtc_myloops;
  144. int rtc_chkrdr;
  145. unsigned long rtc_chkloops;
  146. int rtc_ready;
  147. struct rcu_torture_reader_check *rtc_assigner;
  148. } ____cacheline_internodealigned_in_smp;
  149. // Update-side data structure used to check RCU readers.
  150. struct rcu_torture {
  151. struct rcu_head rtort_rcu;
  152. int rtort_pipe_count;
  153. struct list_head rtort_free;
  154. int rtort_mbtest;
  155. struct rcu_torture_reader_check *rtort_chkp;
  156. };
  157. static LIST_HEAD(rcu_torture_freelist);
  158. static struct rcu_torture __rcu *rcu_torture_current;
  159. static unsigned long rcu_torture_current_version;
  160. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  161. static DEFINE_SPINLOCK(rcu_torture_lock);
  162. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
  163. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
  164. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  165. static struct rcu_torture_reader_check *rcu_torture_reader_mbchk;
  166. static atomic_t n_rcu_torture_alloc;
  167. static atomic_t n_rcu_torture_alloc_fail;
  168. static atomic_t n_rcu_torture_free;
  169. static atomic_t n_rcu_torture_mberror;
  170. static atomic_t n_rcu_torture_mbchk_fail;
  171. static atomic_t n_rcu_torture_mbchk_tries;
  172. static atomic_t n_rcu_torture_error;
  173. static long n_rcu_torture_barrier_error;
  174. static long n_rcu_torture_boost_ktrerror;
  175. static long n_rcu_torture_boost_failure;
  176. static long n_rcu_torture_boosts;
  177. static atomic_long_t n_rcu_torture_timers;
  178. static long n_barrier_attempts;
  179. static long n_barrier_successes; /* did rcu_barrier test succeed? */
  180. static unsigned long n_read_exits;
  181. static struct list_head rcu_torture_removed;
  182. static unsigned long shutdown_jiffies;
  183. static unsigned long start_gp_seq;
  184. static atomic_long_t n_nocb_offload;
  185. static atomic_long_t n_nocb_deoffload;
  186. static int rcu_torture_writer_state;
  187. #define RTWS_FIXED_DELAY 0
  188. #define RTWS_DELAY 1
  189. #define RTWS_REPLACE 2
  190. #define RTWS_DEF_FREE 3
  191. #define RTWS_EXP_SYNC 4
  192. #define RTWS_COND_GET 5
  193. #define RTWS_COND_GET_FULL 6
  194. #define RTWS_COND_GET_EXP 7
  195. #define RTWS_COND_GET_EXP_FULL 8
  196. #define RTWS_COND_SYNC 9
  197. #define RTWS_COND_SYNC_FULL 10
  198. #define RTWS_COND_SYNC_EXP 11
  199. #define RTWS_COND_SYNC_EXP_FULL 12
  200. #define RTWS_POLL_GET 13
  201. #define RTWS_POLL_GET_FULL 14
  202. #define RTWS_POLL_GET_EXP 15
  203. #define RTWS_POLL_GET_EXP_FULL 16
  204. #define RTWS_POLL_WAIT 17
  205. #define RTWS_POLL_WAIT_FULL 18
  206. #define RTWS_POLL_WAIT_EXP 19
  207. #define RTWS_POLL_WAIT_EXP_FULL 20
  208. #define RTWS_SYNC 21
  209. #define RTWS_STUTTER 22
  210. #define RTWS_STOPPING 23
  211. static const char * const rcu_torture_writer_state_names[] = {
  212. "RTWS_FIXED_DELAY",
  213. "RTWS_DELAY",
  214. "RTWS_REPLACE",
  215. "RTWS_DEF_FREE",
  216. "RTWS_EXP_SYNC",
  217. "RTWS_COND_GET",
  218. "RTWS_COND_GET_FULL",
  219. "RTWS_COND_GET_EXP",
  220. "RTWS_COND_GET_EXP_FULL",
  221. "RTWS_COND_SYNC",
  222. "RTWS_COND_SYNC_FULL",
  223. "RTWS_COND_SYNC_EXP",
  224. "RTWS_COND_SYNC_EXP_FULL",
  225. "RTWS_POLL_GET",
  226. "RTWS_POLL_GET_FULL",
  227. "RTWS_POLL_GET_EXP",
  228. "RTWS_POLL_GET_EXP_FULL",
  229. "RTWS_POLL_WAIT",
  230. "RTWS_POLL_WAIT_FULL",
  231. "RTWS_POLL_WAIT_EXP",
  232. "RTWS_POLL_WAIT_EXP_FULL",
  233. "RTWS_SYNC",
  234. "RTWS_STUTTER",
  235. "RTWS_STOPPING",
  236. };
  237. /* Record reader segment types and duration for first failing read. */
  238. struct rt_read_seg {
  239. int rt_readstate;
  240. unsigned long rt_delay_jiffies;
  241. unsigned long rt_delay_ms;
  242. unsigned long rt_delay_us;
  243. bool rt_preempted;
  244. };
  245. static int err_segs_recorded;
  246. static struct rt_read_seg err_segs[RCUTORTURE_RDR_MAX_SEGS];
  247. static int rt_read_nsegs;
  248. static const char *rcu_torture_writer_state_getname(void)
  249. {
  250. unsigned int i = READ_ONCE(rcu_torture_writer_state);
  251. if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
  252. return "???";
  253. return rcu_torture_writer_state_names[i];
  254. }
  255. #ifdef CONFIG_RCU_TRACE
  256. static u64 notrace rcu_trace_clock_local(void)
  257. {
  258. u64 ts = trace_clock_local();
  259. (void)do_div(ts, NSEC_PER_USEC);
  260. return ts;
  261. }
  262. #else /* #ifdef CONFIG_RCU_TRACE */
  263. static u64 notrace rcu_trace_clock_local(void)
  264. {
  265. return 0ULL;
  266. }
  267. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  268. /*
  269. * Stop aggressive CPU-hog tests a bit before the end of the test in order
  270. * to avoid interfering with test shutdown.
  271. */
  272. static bool shutdown_time_arrived(void)
  273. {
  274. return shutdown_secs && time_after(jiffies, shutdown_jiffies - 30 * HZ);
  275. }
  276. static unsigned long boost_starttime; /* jiffies of next boost test start. */
  277. static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
  278. /* and boost task create/destroy. */
  279. static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
  280. static bool barrier_phase; /* Test phase. */
  281. static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
  282. static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
  283. static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
  284. static atomic_t rcu_fwd_cb_nodelay; /* Short rcu_torture_delay() delays. */
  285. /*
  286. * Allocate an element from the rcu_tortures pool.
  287. */
  288. static struct rcu_torture *
  289. rcu_torture_alloc(void)
  290. {
  291. struct list_head *p;
  292. spin_lock_bh(&rcu_torture_lock);
  293. if (list_empty(&rcu_torture_freelist)) {
  294. atomic_inc(&n_rcu_torture_alloc_fail);
  295. spin_unlock_bh(&rcu_torture_lock);
  296. return NULL;
  297. }
  298. atomic_inc(&n_rcu_torture_alloc);
  299. p = rcu_torture_freelist.next;
  300. list_del_init(p);
  301. spin_unlock_bh(&rcu_torture_lock);
  302. return container_of(p, struct rcu_torture, rtort_free);
  303. }
  304. /*
  305. * Free an element to the rcu_tortures pool.
  306. */
  307. static void
  308. rcu_torture_free(struct rcu_torture *p)
  309. {
  310. atomic_inc(&n_rcu_torture_free);
  311. spin_lock_bh(&rcu_torture_lock);
  312. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  313. spin_unlock_bh(&rcu_torture_lock);
  314. }
  315. /*
  316. * Operations vector for selecting different types of tests.
  317. */
  318. struct rcu_torture_ops {
  319. int ttype;
  320. void (*init)(void);
  321. void (*cleanup)(void);
  322. int (*readlock)(void);
  323. void (*read_delay)(struct torture_random_state *rrsp,
  324. struct rt_read_seg *rtrsp);
  325. void (*readunlock)(int idx);
  326. int (*readlock_held)(void);
  327. unsigned long (*get_gp_seq)(void);
  328. unsigned long (*gp_diff)(unsigned long new, unsigned long old);
  329. void (*deferred_free)(struct rcu_torture *p);
  330. void (*sync)(void);
  331. void (*exp_sync)(void);
  332. unsigned long (*get_gp_state_exp)(void);
  333. unsigned long (*start_gp_poll_exp)(void);
  334. void (*start_gp_poll_exp_full)(struct rcu_gp_oldstate *rgosp);
  335. bool (*poll_gp_state_exp)(unsigned long oldstate);
  336. void (*cond_sync_exp)(unsigned long oldstate);
  337. void (*cond_sync_exp_full)(struct rcu_gp_oldstate *rgosp);
  338. unsigned long (*get_comp_state)(void);
  339. void (*get_comp_state_full)(struct rcu_gp_oldstate *rgosp);
  340. bool (*same_gp_state)(unsigned long oldstate1, unsigned long oldstate2);
  341. bool (*same_gp_state_full)(struct rcu_gp_oldstate *rgosp1, struct rcu_gp_oldstate *rgosp2);
  342. unsigned long (*get_gp_state)(void);
  343. void (*get_gp_state_full)(struct rcu_gp_oldstate *rgosp);
  344. unsigned long (*start_gp_poll)(void);
  345. void (*start_gp_poll_full)(struct rcu_gp_oldstate *rgosp);
  346. bool (*poll_gp_state)(unsigned long oldstate);
  347. bool (*poll_gp_state_full)(struct rcu_gp_oldstate *rgosp);
  348. bool (*poll_need_2gp)(bool poll, bool poll_full);
  349. void (*cond_sync)(unsigned long oldstate);
  350. void (*cond_sync_full)(struct rcu_gp_oldstate *rgosp);
  351. int poll_active;
  352. int poll_active_full;
  353. call_rcu_func_t call;
  354. void (*cb_barrier)(void);
  355. void (*fqs)(void);
  356. void (*stats)(void);
  357. void (*gp_kthread_dbg)(void);
  358. bool (*check_boost_failed)(unsigned long gp_state, int *cpup);
  359. int (*stall_dur)(void);
  360. void (*get_gp_data)(int *flags, unsigned long *gp_seq);
  361. void (*gp_slow_register)(atomic_t *rgssp);
  362. void (*gp_slow_unregister)(atomic_t *rgssp);
  363. long cbflood_max;
  364. int irq_capable;
  365. int can_boost;
  366. int extendables;
  367. int slow_gps;
  368. int no_pi_lock;
  369. int debug_objects;
  370. const char *name;
  371. };
  372. static struct rcu_torture_ops *cur_ops;
  373. /*
  374. * Definitions for rcu torture testing.
  375. */
  376. static int torture_readlock_not_held(void)
  377. {
  378. return rcu_read_lock_bh_held() || rcu_read_lock_sched_held();
  379. }
  380. static int rcu_torture_read_lock(void)
  381. {
  382. rcu_read_lock();
  383. return 0;
  384. }
  385. static void
  386. rcu_read_delay(struct torture_random_state *rrsp, struct rt_read_seg *rtrsp)
  387. {
  388. unsigned long started;
  389. unsigned long completed;
  390. const unsigned long shortdelay_us = 200;
  391. unsigned long longdelay_ms = 300;
  392. unsigned long long ts;
  393. /* We want a short delay sometimes to make a reader delay the grace
  394. * period, and we want a long delay occasionally to trigger
  395. * force_quiescent_state. */
  396. if (!atomic_read(&rcu_fwd_cb_nodelay) &&
  397. !(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
  398. started = cur_ops->get_gp_seq();
  399. ts = rcu_trace_clock_local();
  400. if (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK))
  401. longdelay_ms = 5; /* Avoid triggering BH limits. */
  402. mdelay(longdelay_ms);
  403. rtrsp->rt_delay_ms = longdelay_ms;
  404. completed = cur_ops->get_gp_seq();
  405. do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
  406. started, completed);
  407. }
  408. if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us))) {
  409. udelay(shortdelay_us);
  410. rtrsp->rt_delay_us = shortdelay_us;
  411. }
  412. if (!preempt_count() &&
  413. !(torture_random(rrsp) % (nrealreaders * 500))) {
  414. torture_preempt_schedule(); /* QS only if preemptible. */
  415. rtrsp->rt_preempted = true;
  416. }
  417. }
  418. static void rcu_torture_read_unlock(int idx)
  419. {
  420. rcu_read_unlock();
  421. }
  422. /*
  423. * Update callback in the pipe. This should be invoked after a grace period.
  424. */
  425. static bool
  426. rcu_torture_pipe_update_one(struct rcu_torture *rp)
  427. {
  428. int i;
  429. struct rcu_torture_reader_check *rtrcp = READ_ONCE(rp->rtort_chkp);
  430. if (rtrcp) {
  431. WRITE_ONCE(rp->rtort_chkp, NULL);
  432. smp_store_release(&rtrcp->rtc_ready, 1); // Pair with smp_load_acquire().
  433. }
  434. i = rp->rtort_pipe_count;
  435. if (i > RCU_TORTURE_PIPE_LEN)
  436. i = RCU_TORTURE_PIPE_LEN;
  437. atomic_inc(&rcu_torture_wcount[i]);
  438. WRITE_ONCE(rp->rtort_pipe_count, i + 1);
  439. ASSERT_EXCLUSIVE_WRITER(rp->rtort_pipe_count);
  440. if (i + 1 >= RCU_TORTURE_PIPE_LEN) {
  441. rp->rtort_mbtest = 0;
  442. return true;
  443. }
  444. return false;
  445. }
  446. /*
  447. * Update all callbacks in the pipe. Suitable for synchronous grace-period
  448. * primitives.
  449. */
  450. static void
  451. rcu_torture_pipe_update(struct rcu_torture *old_rp)
  452. {
  453. struct rcu_torture *rp;
  454. struct rcu_torture *rp1;
  455. if (old_rp)
  456. list_add(&old_rp->rtort_free, &rcu_torture_removed);
  457. list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
  458. if (rcu_torture_pipe_update_one(rp)) {
  459. list_del(&rp->rtort_free);
  460. rcu_torture_free(rp);
  461. }
  462. }
  463. }
  464. static void
  465. rcu_torture_cb(struct rcu_head *p)
  466. {
  467. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  468. if (torture_must_stop_irq()) {
  469. /* Test is ending, just drop callbacks on the floor. */
  470. /* The next initialization will pick up the pieces. */
  471. return;
  472. }
  473. if (rcu_torture_pipe_update_one(rp))
  474. rcu_torture_free(rp);
  475. else
  476. cur_ops->deferred_free(rp);
  477. }
  478. static unsigned long rcu_no_completed(void)
  479. {
  480. return 0;
  481. }
  482. static void rcu_torture_deferred_free(struct rcu_torture *p)
  483. {
  484. call_rcu_hurry(&p->rtort_rcu, rcu_torture_cb);
  485. }
  486. static void rcu_sync_torture_init(void)
  487. {
  488. INIT_LIST_HEAD(&rcu_torture_removed);
  489. }
  490. static bool rcu_poll_need_2gp(bool poll, bool poll_full)
  491. {
  492. return poll;
  493. }
  494. static struct rcu_torture_ops rcu_ops = {
  495. .ttype = RCU_FLAVOR,
  496. .init = rcu_sync_torture_init,
  497. .readlock = rcu_torture_read_lock,
  498. .read_delay = rcu_read_delay,
  499. .readunlock = rcu_torture_read_unlock,
  500. .readlock_held = torture_readlock_not_held,
  501. .get_gp_seq = rcu_get_gp_seq,
  502. .gp_diff = rcu_seq_diff,
  503. .deferred_free = rcu_torture_deferred_free,
  504. .sync = synchronize_rcu,
  505. .exp_sync = synchronize_rcu_expedited,
  506. .same_gp_state = same_state_synchronize_rcu,
  507. .same_gp_state_full = same_state_synchronize_rcu_full,
  508. .get_comp_state = get_completed_synchronize_rcu,
  509. .get_comp_state_full = get_completed_synchronize_rcu_full,
  510. .get_gp_state = get_state_synchronize_rcu,
  511. .get_gp_state_full = get_state_synchronize_rcu_full,
  512. .start_gp_poll = start_poll_synchronize_rcu,
  513. .start_gp_poll_full = start_poll_synchronize_rcu_full,
  514. .poll_gp_state = poll_state_synchronize_rcu,
  515. .poll_gp_state_full = poll_state_synchronize_rcu_full,
  516. .poll_need_2gp = rcu_poll_need_2gp,
  517. .cond_sync = cond_synchronize_rcu,
  518. .cond_sync_full = cond_synchronize_rcu_full,
  519. .poll_active = NUM_ACTIVE_RCU_POLL_OLDSTATE,
  520. .poll_active_full = NUM_ACTIVE_RCU_POLL_FULL_OLDSTATE,
  521. .get_gp_state_exp = get_state_synchronize_rcu,
  522. .start_gp_poll_exp = start_poll_synchronize_rcu_expedited,
  523. .start_gp_poll_exp_full = start_poll_synchronize_rcu_expedited_full,
  524. .poll_gp_state_exp = poll_state_synchronize_rcu,
  525. .cond_sync_exp = cond_synchronize_rcu_expedited,
  526. .call = call_rcu_hurry,
  527. .cb_barrier = rcu_barrier,
  528. .fqs = rcu_force_quiescent_state,
  529. .gp_kthread_dbg = show_rcu_gp_kthreads,
  530. .check_boost_failed = rcu_check_boost_fail,
  531. .stall_dur = rcu_jiffies_till_stall_check,
  532. .get_gp_data = rcutorture_get_gp_data,
  533. .gp_slow_register = rcu_gp_slow_register,
  534. .gp_slow_unregister = rcu_gp_slow_unregister,
  535. .irq_capable = 1,
  536. .can_boost = IS_ENABLED(CONFIG_RCU_BOOST),
  537. .extendables = RCUTORTURE_MAX_EXTEND,
  538. .debug_objects = 1,
  539. .name = "rcu"
  540. };
  541. /*
  542. * Don't even think about trying any of these in real life!!!
  543. * The names includes "busted", and they really means it!
  544. * The only purpose of these functions is to provide a buggy RCU
  545. * implementation to make sure that rcutorture correctly emits
  546. * buggy-RCU error messages.
  547. */
  548. static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
  549. {
  550. /* This is a deliberate bug for testing purposes only! */
  551. rcu_torture_cb(&p->rtort_rcu);
  552. }
  553. static void synchronize_rcu_busted(void)
  554. {
  555. /* This is a deliberate bug for testing purposes only! */
  556. }
  557. static void
  558. call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
  559. {
  560. /* This is a deliberate bug for testing purposes only! */
  561. func(head);
  562. }
  563. static struct rcu_torture_ops rcu_busted_ops = {
  564. .ttype = INVALID_RCU_FLAVOR,
  565. .init = rcu_sync_torture_init,
  566. .readlock = rcu_torture_read_lock,
  567. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  568. .readunlock = rcu_torture_read_unlock,
  569. .readlock_held = torture_readlock_not_held,
  570. .get_gp_seq = rcu_no_completed,
  571. .deferred_free = rcu_busted_torture_deferred_free,
  572. .sync = synchronize_rcu_busted,
  573. .exp_sync = synchronize_rcu_busted,
  574. .call = call_rcu_busted,
  575. .irq_capable = 1,
  576. .name = "busted"
  577. };
  578. /*
  579. * Definitions for srcu torture testing.
  580. */
  581. DEFINE_STATIC_SRCU(srcu_ctl);
  582. static struct srcu_struct srcu_ctld;
  583. static struct srcu_struct *srcu_ctlp = &srcu_ctl;
  584. static struct rcu_torture_ops srcud_ops;
  585. static void srcu_get_gp_data(int *flags, unsigned long *gp_seq)
  586. {
  587. srcutorture_get_gp_data(srcu_ctlp, flags, gp_seq);
  588. }
  589. static int srcu_torture_read_lock(void)
  590. {
  591. if (cur_ops == &srcud_ops)
  592. return srcu_read_lock_nmisafe(srcu_ctlp);
  593. else
  594. return srcu_read_lock(srcu_ctlp);
  595. }
  596. static void
  597. srcu_read_delay(struct torture_random_state *rrsp, struct rt_read_seg *rtrsp)
  598. {
  599. long delay;
  600. const long uspertick = 1000000 / HZ;
  601. const long longdelay = 10;
  602. /* We want there to be long-running readers, but not all the time. */
  603. delay = torture_random(rrsp) %
  604. (nrealreaders * 2 * longdelay * uspertick);
  605. if (!delay && in_task()) {
  606. schedule_timeout_interruptible(longdelay);
  607. rtrsp->rt_delay_jiffies = longdelay;
  608. } else {
  609. rcu_read_delay(rrsp, rtrsp);
  610. }
  611. }
  612. static void srcu_torture_read_unlock(int idx)
  613. {
  614. if (cur_ops == &srcud_ops)
  615. srcu_read_unlock_nmisafe(srcu_ctlp, idx);
  616. else
  617. srcu_read_unlock(srcu_ctlp, idx);
  618. }
  619. static int torture_srcu_read_lock_held(void)
  620. {
  621. return srcu_read_lock_held(srcu_ctlp);
  622. }
  623. static unsigned long srcu_torture_completed(void)
  624. {
  625. return srcu_batches_completed(srcu_ctlp);
  626. }
  627. static void srcu_torture_deferred_free(struct rcu_torture *rp)
  628. {
  629. call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
  630. }
  631. static void srcu_torture_synchronize(void)
  632. {
  633. synchronize_srcu(srcu_ctlp);
  634. }
  635. static unsigned long srcu_torture_get_gp_state(void)
  636. {
  637. return get_state_synchronize_srcu(srcu_ctlp);
  638. }
  639. static unsigned long srcu_torture_start_gp_poll(void)
  640. {
  641. return start_poll_synchronize_srcu(srcu_ctlp);
  642. }
  643. static bool srcu_torture_poll_gp_state(unsigned long oldstate)
  644. {
  645. return poll_state_synchronize_srcu(srcu_ctlp, oldstate);
  646. }
  647. static void srcu_torture_call(struct rcu_head *head,
  648. rcu_callback_t func)
  649. {
  650. call_srcu(srcu_ctlp, head, func);
  651. }
  652. static void srcu_torture_barrier(void)
  653. {
  654. srcu_barrier(srcu_ctlp);
  655. }
  656. static void srcu_torture_stats(void)
  657. {
  658. srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
  659. }
  660. static void srcu_torture_synchronize_expedited(void)
  661. {
  662. synchronize_srcu_expedited(srcu_ctlp);
  663. }
  664. static struct rcu_torture_ops srcu_ops = {
  665. .ttype = SRCU_FLAVOR,
  666. .init = rcu_sync_torture_init,
  667. .readlock = srcu_torture_read_lock,
  668. .read_delay = srcu_read_delay,
  669. .readunlock = srcu_torture_read_unlock,
  670. .readlock_held = torture_srcu_read_lock_held,
  671. .get_gp_seq = srcu_torture_completed,
  672. .deferred_free = srcu_torture_deferred_free,
  673. .sync = srcu_torture_synchronize,
  674. .exp_sync = srcu_torture_synchronize_expedited,
  675. .same_gp_state = same_state_synchronize_srcu,
  676. .get_comp_state = get_completed_synchronize_srcu,
  677. .get_gp_state = srcu_torture_get_gp_state,
  678. .start_gp_poll = srcu_torture_start_gp_poll,
  679. .poll_gp_state = srcu_torture_poll_gp_state,
  680. .poll_active = NUM_ACTIVE_SRCU_POLL_OLDSTATE,
  681. .call = srcu_torture_call,
  682. .cb_barrier = srcu_torture_barrier,
  683. .stats = srcu_torture_stats,
  684. .get_gp_data = srcu_get_gp_data,
  685. .cbflood_max = 50000,
  686. .irq_capable = 1,
  687. .no_pi_lock = IS_ENABLED(CONFIG_TINY_SRCU),
  688. .debug_objects = 1,
  689. .name = "srcu"
  690. };
  691. static void srcu_torture_init(void)
  692. {
  693. rcu_sync_torture_init();
  694. WARN_ON(init_srcu_struct(&srcu_ctld));
  695. srcu_ctlp = &srcu_ctld;
  696. }
  697. static void srcu_torture_cleanup(void)
  698. {
  699. cleanup_srcu_struct(&srcu_ctld);
  700. srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
  701. }
  702. /* As above, but dynamically allocated. */
  703. static struct rcu_torture_ops srcud_ops = {
  704. .ttype = SRCU_FLAVOR,
  705. .init = srcu_torture_init,
  706. .cleanup = srcu_torture_cleanup,
  707. .readlock = srcu_torture_read_lock,
  708. .read_delay = srcu_read_delay,
  709. .readunlock = srcu_torture_read_unlock,
  710. .readlock_held = torture_srcu_read_lock_held,
  711. .get_gp_seq = srcu_torture_completed,
  712. .deferred_free = srcu_torture_deferred_free,
  713. .sync = srcu_torture_synchronize,
  714. .exp_sync = srcu_torture_synchronize_expedited,
  715. .same_gp_state = same_state_synchronize_srcu,
  716. .get_comp_state = get_completed_synchronize_srcu,
  717. .get_gp_state = srcu_torture_get_gp_state,
  718. .start_gp_poll = srcu_torture_start_gp_poll,
  719. .poll_gp_state = srcu_torture_poll_gp_state,
  720. .poll_active = NUM_ACTIVE_SRCU_POLL_OLDSTATE,
  721. .call = srcu_torture_call,
  722. .cb_barrier = srcu_torture_barrier,
  723. .stats = srcu_torture_stats,
  724. .get_gp_data = srcu_get_gp_data,
  725. .cbflood_max = 50000,
  726. .irq_capable = 1,
  727. .no_pi_lock = IS_ENABLED(CONFIG_TINY_SRCU),
  728. .debug_objects = 1,
  729. .name = "srcud"
  730. };
  731. /* As above, but broken due to inappropriate reader extension. */
  732. static struct rcu_torture_ops busted_srcud_ops = {
  733. .ttype = SRCU_FLAVOR,
  734. .init = srcu_torture_init,
  735. .cleanup = srcu_torture_cleanup,
  736. .readlock = srcu_torture_read_lock,
  737. .read_delay = rcu_read_delay,
  738. .readunlock = srcu_torture_read_unlock,
  739. .readlock_held = torture_srcu_read_lock_held,
  740. .get_gp_seq = srcu_torture_completed,
  741. .deferred_free = srcu_torture_deferred_free,
  742. .sync = srcu_torture_synchronize,
  743. .exp_sync = srcu_torture_synchronize_expedited,
  744. .call = srcu_torture_call,
  745. .cb_barrier = srcu_torture_barrier,
  746. .stats = srcu_torture_stats,
  747. .irq_capable = 1,
  748. .no_pi_lock = IS_ENABLED(CONFIG_TINY_SRCU),
  749. .extendables = RCUTORTURE_MAX_EXTEND,
  750. .name = "busted_srcud"
  751. };
  752. /*
  753. * Definitions for trivial CONFIG_PREEMPT=n-only torture testing.
  754. * This implementation does not necessarily work well with CPU hotplug.
  755. */
  756. static void synchronize_rcu_trivial(void)
  757. {
  758. int cpu;
  759. for_each_online_cpu(cpu) {
  760. torture_sched_setaffinity(current->pid, cpumask_of(cpu));
  761. WARN_ON_ONCE(raw_smp_processor_id() != cpu);
  762. }
  763. }
  764. static int rcu_torture_read_lock_trivial(void)
  765. {
  766. preempt_disable();
  767. return 0;
  768. }
  769. static void rcu_torture_read_unlock_trivial(int idx)
  770. {
  771. preempt_enable();
  772. }
  773. static struct rcu_torture_ops trivial_ops = {
  774. .ttype = RCU_TRIVIAL_FLAVOR,
  775. .init = rcu_sync_torture_init,
  776. .readlock = rcu_torture_read_lock_trivial,
  777. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  778. .readunlock = rcu_torture_read_unlock_trivial,
  779. .readlock_held = torture_readlock_not_held,
  780. .get_gp_seq = rcu_no_completed,
  781. .sync = synchronize_rcu_trivial,
  782. .exp_sync = synchronize_rcu_trivial,
  783. .irq_capable = 1,
  784. .name = "trivial"
  785. };
  786. #ifdef CONFIG_TASKS_RCU
  787. /*
  788. * Definitions for RCU-tasks torture testing.
  789. */
  790. static int tasks_torture_read_lock(void)
  791. {
  792. return 0;
  793. }
  794. static void tasks_torture_read_unlock(int idx)
  795. {
  796. }
  797. static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
  798. {
  799. call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
  800. }
  801. static void synchronize_rcu_mult_test(void)
  802. {
  803. synchronize_rcu_mult(call_rcu_tasks, call_rcu_hurry);
  804. }
  805. static struct rcu_torture_ops tasks_ops = {
  806. .ttype = RCU_TASKS_FLAVOR,
  807. .init = rcu_sync_torture_init,
  808. .readlock = tasks_torture_read_lock,
  809. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  810. .readunlock = tasks_torture_read_unlock,
  811. .get_gp_seq = rcu_no_completed,
  812. .deferred_free = rcu_tasks_torture_deferred_free,
  813. .sync = synchronize_rcu_tasks,
  814. .exp_sync = synchronize_rcu_mult_test,
  815. .call = call_rcu_tasks,
  816. .cb_barrier = rcu_barrier_tasks,
  817. .gp_kthread_dbg = show_rcu_tasks_classic_gp_kthread,
  818. .get_gp_data = rcu_tasks_get_gp_data,
  819. .irq_capable = 1,
  820. .slow_gps = 1,
  821. .name = "tasks"
  822. };
  823. #define TASKS_OPS &tasks_ops,
  824. #else // #ifdef CONFIG_TASKS_RCU
  825. #define TASKS_OPS
  826. #endif // #else #ifdef CONFIG_TASKS_RCU
  827. #ifdef CONFIG_TASKS_RUDE_RCU
  828. /*
  829. * Definitions for rude RCU-tasks torture testing.
  830. */
  831. static struct rcu_torture_ops tasks_rude_ops = {
  832. .ttype = RCU_TASKS_RUDE_FLAVOR,
  833. .init = rcu_sync_torture_init,
  834. .readlock = rcu_torture_read_lock_trivial,
  835. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  836. .readunlock = rcu_torture_read_unlock_trivial,
  837. .get_gp_seq = rcu_no_completed,
  838. .sync = synchronize_rcu_tasks_rude,
  839. .exp_sync = synchronize_rcu_tasks_rude,
  840. .gp_kthread_dbg = show_rcu_tasks_rude_gp_kthread,
  841. .get_gp_data = rcu_tasks_rude_get_gp_data,
  842. .cbflood_max = 50000,
  843. .irq_capable = 1,
  844. .name = "tasks-rude"
  845. };
  846. #define TASKS_RUDE_OPS &tasks_rude_ops,
  847. #else // #ifdef CONFIG_TASKS_RUDE_RCU
  848. #define TASKS_RUDE_OPS
  849. #endif // #else #ifdef CONFIG_TASKS_RUDE_RCU
  850. #ifdef CONFIG_TASKS_TRACE_RCU
  851. /*
  852. * Definitions for tracing RCU-tasks torture testing.
  853. */
  854. static int tasks_tracing_torture_read_lock(void)
  855. {
  856. rcu_read_lock_trace();
  857. return 0;
  858. }
  859. static void tasks_tracing_torture_read_unlock(int idx)
  860. {
  861. rcu_read_unlock_trace();
  862. }
  863. static void rcu_tasks_tracing_torture_deferred_free(struct rcu_torture *p)
  864. {
  865. call_rcu_tasks_trace(&p->rtort_rcu, rcu_torture_cb);
  866. }
  867. static struct rcu_torture_ops tasks_tracing_ops = {
  868. .ttype = RCU_TASKS_TRACING_FLAVOR,
  869. .init = rcu_sync_torture_init,
  870. .readlock = tasks_tracing_torture_read_lock,
  871. .read_delay = srcu_read_delay, /* just reuse srcu's version. */
  872. .readunlock = tasks_tracing_torture_read_unlock,
  873. .readlock_held = rcu_read_lock_trace_held,
  874. .get_gp_seq = rcu_no_completed,
  875. .deferred_free = rcu_tasks_tracing_torture_deferred_free,
  876. .sync = synchronize_rcu_tasks_trace,
  877. .exp_sync = synchronize_rcu_tasks_trace,
  878. .call = call_rcu_tasks_trace,
  879. .cb_barrier = rcu_barrier_tasks_trace,
  880. .gp_kthread_dbg = show_rcu_tasks_trace_gp_kthread,
  881. .get_gp_data = rcu_tasks_trace_get_gp_data,
  882. .cbflood_max = 50000,
  883. .irq_capable = 1,
  884. .slow_gps = 1,
  885. .name = "tasks-tracing"
  886. };
  887. #define TASKS_TRACING_OPS &tasks_tracing_ops,
  888. #else // #ifdef CONFIG_TASKS_TRACE_RCU
  889. #define TASKS_TRACING_OPS
  890. #endif // #else #ifdef CONFIG_TASKS_TRACE_RCU
  891. static unsigned long rcutorture_seq_diff(unsigned long new, unsigned long old)
  892. {
  893. if (!cur_ops->gp_diff)
  894. return new - old;
  895. return cur_ops->gp_diff(new, old);
  896. }
  897. /*
  898. * RCU torture priority-boost testing. Runs one real-time thread per
  899. * CPU for moderate bursts, repeatedly starting grace periods and waiting
  900. * for them to complete. If a given grace period takes too long, we assume
  901. * that priority inversion has occurred.
  902. */
  903. static int old_rt_runtime = -1;
  904. static void rcu_torture_disable_rt_throttle(void)
  905. {
  906. /*
  907. * Disable RT throttling so that rcutorture's boost threads don't get
  908. * throttled. Only possible if rcutorture is built-in otherwise the
  909. * user should manually do this by setting the sched_rt_period_us and
  910. * sched_rt_runtime sysctls.
  911. */
  912. if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime != -1)
  913. return;
  914. old_rt_runtime = sysctl_sched_rt_runtime;
  915. sysctl_sched_rt_runtime = -1;
  916. }
  917. static void rcu_torture_enable_rt_throttle(void)
  918. {
  919. if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST) || old_rt_runtime == -1)
  920. return;
  921. sysctl_sched_rt_runtime = old_rt_runtime;
  922. old_rt_runtime = -1;
  923. }
  924. static bool rcu_torture_boost_failed(unsigned long gp_state, unsigned long *start)
  925. {
  926. int cpu;
  927. static int dbg_done;
  928. unsigned long end = jiffies;
  929. bool gp_done;
  930. unsigned long j;
  931. static unsigned long last_persist;
  932. unsigned long lp;
  933. unsigned long mininterval = test_boost_duration * HZ - HZ / 2;
  934. if (end - *start > mininterval) {
  935. // Recheck after checking time to avoid false positives.
  936. smp_mb(); // Time check before grace-period check.
  937. if (cur_ops->poll_gp_state(gp_state))
  938. return false; // passed, though perhaps just barely
  939. if (cur_ops->check_boost_failed && !cur_ops->check_boost_failed(gp_state, &cpu)) {
  940. // At most one persisted message per boost test.
  941. j = jiffies;
  942. lp = READ_ONCE(last_persist);
  943. if (time_after(j, lp + mininterval) && cmpxchg(&last_persist, lp, j) == lp)
  944. pr_info("Boost inversion persisted: No QS from CPU %d\n", cpu);
  945. return false; // passed on a technicality
  946. }
  947. VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
  948. n_rcu_torture_boost_failure++;
  949. if (!xchg(&dbg_done, 1) && cur_ops->gp_kthread_dbg) {
  950. pr_info("Boost inversion thread ->rt_priority %u gp_state %lu jiffies %lu\n",
  951. current->rt_priority, gp_state, end - *start);
  952. cur_ops->gp_kthread_dbg();
  953. // Recheck after print to flag grace period ending during splat.
  954. gp_done = cur_ops->poll_gp_state(gp_state);
  955. pr_info("Boost inversion: GP %lu %s.\n", gp_state,
  956. gp_done ? "ended already" : "still pending");
  957. }
  958. return true; // failed
  959. } else if (cur_ops->check_boost_failed && !cur_ops->check_boost_failed(gp_state, NULL)) {
  960. *start = jiffies;
  961. }
  962. return false; // passed
  963. }
  964. static int rcu_torture_boost(void *arg)
  965. {
  966. unsigned long endtime;
  967. unsigned long gp_state;
  968. unsigned long gp_state_time;
  969. unsigned long oldstarttime;
  970. VERBOSE_TOROUT_STRING("rcu_torture_boost started");
  971. /* Set real-time priority. */
  972. sched_set_fifo_low(current);
  973. /* Each pass through the following loop does one boost-test cycle. */
  974. do {
  975. bool failed = false; // Test failed already in this test interval
  976. bool gp_initiated = false;
  977. if (kthread_should_stop())
  978. goto checkwait;
  979. /* Wait for the next test interval. */
  980. oldstarttime = READ_ONCE(boost_starttime);
  981. while (time_before(jiffies, oldstarttime)) {
  982. schedule_timeout_interruptible(oldstarttime - jiffies);
  983. if (stutter_wait("rcu_torture_boost"))
  984. sched_set_fifo_low(current);
  985. if (torture_must_stop())
  986. goto checkwait;
  987. }
  988. // Do one boost-test interval.
  989. endtime = oldstarttime + test_boost_duration * HZ;
  990. while (time_before(jiffies, endtime)) {
  991. // Has current GP gone too long?
  992. if (gp_initiated && !failed && !cur_ops->poll_gp_state(gp_state))
  993. failed = rcu_torture_boost_failed(gp_state, &gp_state_time);
  994. // If we don't have a grace period in flight, start one.
  995. if (!gp_initiated || cur_ops->poll_gp_state(gp_state)) {
  996. gp_state = cur_ops->start_gp_poll();
  997. gp_initiated = true;
  998. gp_state_time = jiffies;
  999. }
  1000. if (stutter_wait("rcu_torture_boost")) {
  1001. sched_set_fifo_low(current);
  1002. // If the grace period already ended,
  1003. // we don't know when that happened, so
  1004. // start over.
  1005. if (cur_ops->poll_gp_state(gp_state))
  1006. gp_initiated = false;
  1007. }
  1008. if (torture_must_stop())
  1009. goto checkwait;
  1010. }
  1011. // In case the grace period extended beyond the end of the loop.
  1012. if (gp_initiated && !failed && !cur_ops->poll_gp_state(gp_state))
  1013. rcu_torture_boost_failed(gp_state, &gp_state_time);
  1014. /*
  1015. * Set the start time of the next test interval.
  1016. * Yes, this is vulnerable to long delays, but such
  1017. * delays simply cause a false negative for the next
  1018. * interval. Besides, we are running at RT priority,
  1019. * so delays should be relatively rare.
  1020. */
  1021. while (oldstarttime == READ_ONCE(boost_starttime) && !kthread_should_stop()) {
  1022. if (mutex_trylock(&boost_mutex)) {
  1023. if (oldstarttime == boost_starttime) {
  1024. WRITE_ONCE(boost_starttime,
  1025. jiffies + test_boost_interval * HZ);
  1026. n_rcu_torture_boosts++;
  1027. }
  1028. mutex_unlock(&boost_mutex);
  1029. break;
  1030. }
  1031. schedule_timeout_uninterruptible(HZ / 20);
  1032. }
  1033. /* Go do the stutter. */
  1034. checkwait: if (stutter_wait("rcu_torture_boost"))
  1035. sched_set_fifo_low(current);
  1036. } while (!torture_must_stop());
  1037. /* Clean up and exit. */
  1038. while (!kthread_should_stop()) {
  1039. torture_shutdown_absorb("rcu_torture_boost");
  1040. schedule_timeout_uninterruptible(HZ / 20);
  1041. }
  1042. torture_kthread_stopping("rcu_torture_boost");
  1043. return 0;
  1044. }
  1045. /*
  1046. * RCU torture force-quiescent-state kthread. Repeatedly induces
  1047. * bursts of calls to force_quiescent_state(), increasing the probability
  1048. * of occurrence of some important types of race conditions.
  1049. */
  1050. static int
  1051. rcu_torture_fqs(void *arg)
  1052. {
  1053. unsigned long fqs_resume_time;
  1054. int fqs_burst_remaining;
  1055. int oldnice = task_nice(current);
  1056. VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
  1057. do {
  1058. fqs_resume_time = jiffies + fqs_stutter * HZ;
  1059. while (time_before(jiffies, fqs_resume_time) &&
  1060. !kthread_should_stop()) {
  1061. schedule_timeout_interruptible(HZ / 20);
  1062. }
  1063. fqs_burst_remaining = fqs_duration;
  1064. while (fqs_burst_remaining > 0 &&
  1065. !kthread_should_stop()) {
  1066. cur_ops->fqs();
  1067. udelay(fqs_holdoff);
  1068. fqs_burst_remaining -= fqs_holdoff;
  1069. }
  1070. if (stutter_wait("rcu_torture_fqs"))
  1071. sched_set_normal(current, oldnice);
  1072. } while (!torture_must_stop());
  1073. torture_kthread_stopping("rcu_torture_fqs");
  1074. return 0;
  1075. }
  1076. // Used by writers to randomly choose from the available grace-period primitives.
  1077. static int synctype[ARRAY_SIZE(rcu_torture_writer_state_names)] = { };
  1078. static int nsynctypes;
  1079. /*
  1080. * Determine which grace-period primitives are available.
  1081. */
  1082. static void rcu_torture_write_types(void)
  1083. {
  1084. bool gp_cond1 = gp_cond, gp_cond_exp1 = gp_cond_exp, gp_cond_full1 = gp_cond_full;
  1085. bool gp_cond_exp_full1 = gp_cond_exp_full, gp_exp1 = gp_exp, gp_poll_exp1 = gp_poll_exp;
  1086. bool gp_poll_exp_full1 = gp_poll_exp_full, gp_normal1 = gp_normal, gp_poll1 = gp_poll;
  1087. bool gp_poll_full1 = gp_poll_full, gp_sync1 = gp_sync;
  1088. /* Initialize synctype[] array. If none set, take default. */
  1089. if (!gp_cond1 &&
  1090. !gp_cond_exp1 &&
  1091. !gp_cond_full1 &&
  1092. !gp_cond_exp_full1 &&
  1093. !gp_exp1 &&
  1094. !gp_poll_exp1 &&
  1095. !gp_poll_exp_full1 &&
  1096. !gp_normal1 &&
  1097. !gp_poll1 &&
  1098. !gp_poll_full1 &&
  1099. !gp_sync1) {
  1100. gp_cond1 = true;
  1101. gp_cond_exp1 = true;
  1102. gp_cond_full1 = true;
  1103. gp_cond_exp_full1 = true;
  1104. gp_exp1 = true;
  1105. gp_poll_exp1 = true;
  1106. gp_poll_exp_full1 = true;
  1107. gp_normal1 = true;
  1108. gp_poll1 = true;
  1109. gp_poll_full1 = true;
  1110. gp_sync1 = true;
  1111. }
  1112. if (gp_cond1 && cur_ops->get_gp_state && cur_ops->cond_sync) {
  1113. synctype[nsynctypes++] = RTWS_COND_GET;
  1114. pr_info("%s: Testing conditional GPs.\n", __func__);
  1115. } else if (gp_cond && (!cur_ops->get_gp_state || !cur_ops->cond_sync)) {
  1116. pr_alert("%s: gp_cond without primitives.\n", __func__);
  1117. }
  1118. if (gp_cond_exp1 && cur_ops->get_gp_state_exp && cur_ops->cond_sync_exp) {
  1119. synctype[nsynctypes++] = RTWS_COND_GET_EXP;
  1120. pr_info("%s: Testing conditional expedited GPs.\n", __func__);
  1121. } else if (gp_cond_exp && (!cur_ops->get_gp_state_exp || !cur_ops->cond_sync_exp)) {
  1122. pr_alert("%s: gp_cond_exp without primitives.\n", __func__);
  1123. }
  1124. if (gp_cond_full1 && cur_ops->get_gp_state && cur_ops->cond_sync_full) {
  1125. synctype[nsynctypes++] = RTWS_COND_GET_FULL;
  1126. pr_info("%s: Testing conditional full-state GPs.\n", __func__);
  1127. } else if (gp_cond_full && (!cur_ops->get_gp_state || !cur_ops->cond_sync_full)) {
  1128. pr_alert("%s: gp_cond_full without primitives.\n", __func__);
  1129. }
  1130. if (gp_cond_exp_full1 && cur_ops->get_gp_state_exp && cur_ops->cond_sync_exp_full) {
  1131. synctype[nsynctypes++] = RTWS_COND_GET_EXP_FULL;
  1132. pr_info("%s: Testing conditional full-state expedited GPs.\n", __func__);
  1133. } else if (gp_cond_exp_full &&
  1134. (!cur_ops->get_gp_state_exp || !cur_ops->cond_sync_exp_full)) {
  1135. pr_alert("%s: gp_cond_exp_full without primitives.\n", __func__);
  1136. }
  1137. if (gp_exp1 && cur_ops->exp_sync) {
  1138. synctype[nsynctypes++] = RTWS_EXP_SYNC;
  1139. pr_info("%s: Testing expedited GPs.\n", __func__);
  1140. } else if (gp_exp && !cur_ops->exp_sync) {
  1141. pr_alert("%s: gp_exp without primitives.\n", __func__);
  1142. }
  1143. if (gp_normal1 && cur_ops->deferred_free) {
  1144. synctype[nsynctypes++] = RTWS_DEF_FREE;
  1145. pr_info("%s: Testing asynchronous GPs.\n", __func__);
  1146. } else if (gp_normal && !cur_ops->deferred_free) {
  1147. pr_alert("%s: gp_normal without primitives.\n", __func__);
  1148. }
  1149. if (gp_poll1 && cur_ops->get_comp_state && cur_ops->same_gp_state &&
  1150. cur_ops->start_gp_poll && cur_ops->poll_gp_state) {
  1151. synctype[nsynctypes++] = RTWS_POLL_GET;
  1152. pr_info("%s: Testing polling GPs.\n", __func__);
  1153. } else if (gp_poll && (!cur_ops->start_gp_poll || !cur_ops->poll_gp_state)) {
  1154. pr_alert("%s: gp_poll without primitives.\n", __func__);
  1155. }
  1156. if (gp_poll_full1 && cur_ops->get_comp_state_full && cur_ops->same_gp_state_full
  1157. && cur_ops->start_gp_poll_full && cur_ops->poll_gp_state_full) {
  1158. synctype[nsynctypes++] = RTWS_POLL_GET_FULL;
  1159. pr_info("%s: Testing polling full-state GPs.\n", __func__);
  1160. } else if (gp_poll_full && (!cur_ops->start_gp_poll_full || !cur_ops->poll_gp_state_full)) {
  1161. pr_alert("%s: gp_poll_full without primitives.\n", __func__);
  1162. }
  1163. if (gp_poll_exp1 && cur_ops->start_gp_poll_exp && cur_ops->poll_gp_state_exp) {
  1164. synctype[nsynctypes++] = RTWS_POLL_GET_EXP;
  1165. pr_info("%s: Testing polling expedited GPs.\n", __func__);
  1166. } else if (gp_poll_exp && (!cur_ops->start_gp_poll_exp || !cur_ops->poll_gp_state_exp)) {
  1167. pr_alert("%s: gp_poll_exp without primitives.\n", __func__);
  1168. }
  1169. if (gp_poll_exp_full1 && cur_ops->start_gp_poll_exp_full && cur_ops->poll_gp_state_full) {
  1170. synctype[nsynctypes++] = RTWS_POLL_GET_EXP_FULL;
  1171. pr_info("%s: Testing polling full-state expedited GPs.\n", __func__);
  1172. } else if (gp_poll_exp_full &&
  1173. (!cur_ops->start_gp_poll_exp_full || !cur_ops->poll_gp_state_full)) {
  1174. pr_alert("%s: gp_poll_exp_full without primitives.\n", __func__);
  1175. }
  1176. if (gp_sync1 && cur_ops->sync) {
  1177. synctype[nsynctypes++] = RTWS_SYNC;
  1178. pr_info("%s: Testing normal GPs.\n", __func__);
  1179. } else if (gp_sync && !cur_ops->sync) {
  1180. pr_alert("%s: gp_sync without primitives.\n", __func__);
  1181. }
  1182. pr_alert("%s: Testing %d update types.\n", __func__, nsynctypes);
  1183. }
  1184. /*
  1185. * Do the specified rcu_torture_writer() synchronous grace period,
  1186. * while also testing out the polled APIs. Note well that the single-CPU
  1187. * grace-period optimizations must be accounted for.
  1188. */
  1189. static void do_rtws_sync(struct torture_random_state *trsp, void (*sync)(void))
  1190. {
  1191. unsigned long cookie;
  1192. struct rcu_gp_oldstate cookie_full;
  1193. bool dopoll;
  1194. bool dopoll_full;
  1195. unsigned long r = torture_random(trsp);
  1196. dopoll = cur_ops->get_gp_state && cur_ops->poll_gp_state && !(r & 0x300);
  1197. dopoll_full = cur_ops->get_gp_state_full && cur_ops->poll_gp_state_full && !(r & 0xc00);
  1198. if (dopoll || dopoll_full)
  1199. cpus_read_lock();
  1200. if (dopoll)
  1201. cookie = cur_ops->get_gp_state();
  1202. if (dopoll_full)
  1203. cur_ops->get_gp_state_full(&cookie_full);
  1204. if (cur_ops->poll_need_2gp && cur_ops->poll_need_2gp(dopoll, dopoll_full))
  1205. sync();
  1206. sync();
  1207. WARN_ONCE(dopoll && !cur_ops->poll_gp_state(cookie),
  1208. "%s: Cookie check 3 failed %pS() online %*pbl.",
  1209. __func__, sync, cpumask_pr_args(cpu_online_mask));
  1210. WARN_ONCE(dopoll_full && !cur_ops->poll_gp_state_full(&cookie_full),
  1211. "%s: Cookie check 4 failed %pS() online %*pbl",
  1212. __func__, sync, cpumask_pr_args(cpu_online_mask));
  1213. if (dopoll || dopoll_full)
  1214. cpus_read_unlock();
  1215. }
  1216. /*
  1217. * RCU torture writer kthread. Repeatedly substitutes a new structure
  1218. * for that pointed to by rcu_torture_current, freeing the old structure
  1219. * after a series of grace periods (the "pipeline").
  1220. */
  1221. static int
  1222. rcu_torture_writer(void *arg)
  1223. {
  1224. bool boot_ended;
  1225. bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
  1226. unsigned long cookie;
  1227. struct rcu_gp_oldstate cookie_full;
  1228. int expediting = 0;
  1229. unsigned long gp_snap;
  1230. unsigned long gp_snap1;
  1231. struct rcu_gp_oldstate gp_snap_full;
  1232. struct rcu_gp_oldstate gp_snap1_full;
  1233. int i;
  1234. int idx;
  1235. int oldnice = task_nice(current);
  1236. struct rcu_gp_oldstate *rgo = NULL;
  1237. int rgo_size = 0;
  1238. struct rcu_torture *rp;
  1239. struct rcu_torture *old_rp;
  1240. static DEFINE_TORTURE_RANDOM(rand);
  1241. unsigned long stallsdone = jiffies;
  1242. bool stutter_waited;
  1243. unsigned long *ulo = NULL;
  1244. int ulo_size = 0;
  1245. // If a new stall test is added, this must be adjusted.
  1246. if (stall_cpu_holdoff + stall_gp_kthread + stall_cpu)
  1247. stallsdone += (stall_cpu_holdoff + stall_gp_kthread + stall_cpu + 60) *
  1248. HZ * (stall_cpu_repeat + 1);
  1249. VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
  1250. if (!can_expedite)
  1251. pr_alert("%s" TORTURE_FLAG
  1252. " GP expediting controlled from boot/sysfs for %s.\n",
  1253. torture_type, cur_ops->name);
  1254. if (WARN_ONCE(nsynctypes == 0,
  1255. "%s: No update-side primitives.\n", __func__)) {
  1256. /*
  1257. * No updates primitives, so don't try updating.
  1258. * The resulting test won't be testing much, hence the
  1259. * above WARN_ONCE().
  1260. */
  1261. rcu_torture_writer_state = RTWS_STOPPING;
  1262. torture_kthread_stopping("rcu_torture_writer");
  1263. return 0;
  1264. }
  1265. if (cur_ops->poll_active > 0) {
  1266. ulo = kzalloc(cur_ops->poll_active * sizeof(ulo[0]), GFP_KERNEL);
  1267. if (!WARN_ON(!ulo))
  1268. ulo_size = cur_ops->poll_active;
  1269. }
  1270. if (cur_ops->poll_active_full > 0) {
  1271. rgo = kzalloc(cur_ops->poll_active_full * sizeof(rgo[0]), GFP_KERNEL);
  1272. if (!WARN_ON(!rgo))
  1273. rgo_size = cur_ops->poll_active_full;
  1274. }
  1275. do {
  1276. rcu_torture_writer_state = RTWS_FIXED_DELAY;
  1277. torture_hrtimeout_us(500, 1000, &rand);
  1278. rp = rcu_torture_alloc();
  1279. if (rp == NULL)
  1280. continue;
  1281. rp->rtort_pipe_count = 0;
  1282. ASSERT_EXCLUSIVE_WRITER(rp->rtort_pipe_count);
  1283. rcu_torture_writer_state = RTWS_DELAY;
  1284. udelay(torture_random(&rand) & 0x3ff);
  1285. rcu_torture_writer_state = RTWS_REPLACE;
  1286. old_rp = rcu_dereference_check(rcu_torture_current,
  1287. current == writer_task);
  1288. rp->rtort_mbtest = 1;
  1289. rcu_assign_pointer(rcu_torture_current, rp);
  1290. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  1291. if (old_rp) {
  1292. i = old_rp->rtort_pipe_count;
  1293. if (i > RCU_TORTURE_PIPE_LEN)
  1294. i = RCU_TORTURE_PIPE_LEN;
  1295. atomic_inc(&rcu_torture_wcount[i]);
  1296. WRITE_ONCE(old_rp->rtort_pipe_count,
  1297. old_rp->rtort_pipe_count + 1);
  1298. ASSERT_EXCLUSIVE_WRITER(old_rp->rtort_pipe_count);
  1299. // Make sure readers block polled grace periods.
  1300. if (cur_ops->get_gp_state && cur_ops->poll_gp_state) {
  1301. idx = cur_ops->readlock();
  1302. cookie = cur_ops->get_gp_state();
  1303. WARN_ONCE(cur_ops->poll_gp_state(cookie),
  1304. "%s: Cookie check 1 failed %s(%d) %lu->%lu\n",
  1305. __func__,
  1306. rcu_torture_writer_state_getname(),
  1307. rcu_torture_writer_state,
  1308. cookie, cur_ops->get_gp_state());
  1309. if (cur_ops->get_comp_state) {
  1310. cookie = cur_ops->get_comp_state();
  1311. WARN_ON_ONCE(!cur_ops->poll_gp_state(cookie));
  1312. }
  1313. cur_ops->readunlock(idx);
  1314. }
  1315. if (cur_ops->get_gp_state_full && cur_ops->poll_gp_state_full) {
  1316. idx = cur_ops->readlock();
  1317. cur_ops->get_gp_state_full(&cookie_full);
  1318. WARN_ONCE(cur_ops->poll_gp_state_full(&cookie_full),
  1319. "%s: Cookie check 5 failed %s(%d) online %*pbl\n",
  1320. __func__,
  1321. rcu_torture_writer_state_getname(),
  1322. rcu_torture_writer_state,
  1323. cpumask_pr_args(cpu_online_mask));
  1324. if (cur_ops->get_comp_state_full) {
  1325. cur_ops->get_comp_state_full(&cookie_full);
  1326. WARN_ON_ONCE(!cur_ops->poll_gp_state_full(&cookie_full));
  1327. }
  1328. cur_ops->readunlock(idx);
  1329. }
  1330. switch (synctype[torture_random(&rand) % nsynctypes]) {
  1331. case RTWS_DEF_FREE:
  1332. rcu_torture_writer_state = RTWS_DEF_FREE;
  1333. cur_ops->deferred_free(old_rp);
  1334. break;
  1335. case RTWS_EXP_SYNC:
  1336. rcu_torture_writer_state = RTWS_EXP_SYNC;
  1337. do_rtws_sync(&rand, cur_ops->exp_sync);
  1338. rcu_torture_pipe_update(old_rp);
  1339. break;
  1340. case RTWS_COND_GET:
  1341. rcu_torture_writer_state = RTWS_COND_GET;
  1342. gp_snap = cur_ops->get_gp_state();
  1343. torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
  1344. rcu_torture_writer_state = RTWS_COND_SYNC;
  1345. cur_ops->cond_sync(gp_snap);
  1346. rcu_torture_pipe_update(old_rp);
  1347. break;
  1348. case RTWS_COND_GET_EXP:
  1349. rcu_torture_writer_state = RTWS_COND_GET_EXP;
  1350. gp_snap = cur_ops->get_gp_state_exp();
  1351. torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
  1352. rcu_torture_writer_state = RTWS_COND_SYNC_EXP;
  1353. cur_ops->cond_sync_exp(gp_snap);
  1354. rcu_torture_pipe_update(old_rp);
  1355. break;
  1356. case RTWS_COND_GET_FULL:
  1357. rcu_torture_writer_state = RTWS_COND_GET_FULL;
  1358. cur_ops->get_gp_state_full(&gp_snap_full);
  1359. torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
  1360. rcu_torture_writer_state = RTWS_COND_SYNC_FULL;
  1361. cur_ops->cond_sync_full(&gp_snap_full);
  1362. rcu_torture_pipe_update(old_rp);
  1363. break;
  1364. case RTWS_COND_GET_EXP_FULL:
  1365. rcu_torture_writer_state = RTWS_COND_GET_EXP_FULL;
  1366. cur_ops->get_gp_state_full(&gp_snap_full);
  1367. torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
  1368. rcu_torture_writer_state = RTWS_COND_SYNC_EXP_FULL;
  1369. cur_ops->cond_sync_exp_full(&gp_snap_full);
  1370. rcu_torture_pipe_update(old_rp);
  1371. break;
  1372. case RTWS_POLL_GET:
  1373. rcu_torture_writer_state = RTWS_POLL_GET;
  1374. for (i = 0; i < ulo_size; i++)
  1375. ulo[i] = cur_ops->get_comp_state();
  1376. gp_snap = cur_ops->start_gp_poll();
  1377. rcu_torture_writer_state = RTWS_POLL_WAIT;
  1378. while (!cur_ops->poll_gp_state(gp_snap)) {
  1379. gp_snap1 = cur_ops->get_gp_state();
  1380. for (i = 0; i < ulo_size; i++)
  1381. if (cur_ops->poll_gp_state(ulo[i]) ||
  1382. cur_ops->same_gp_state(ulo[i], gp_snap1)) {
  1383. ulo[i] = gp_snap1;
  1384. break;
  1385. }
  1386. WARN_ON_ONCE(ulo_size > 0 && i >= ulo_size);
  1387. torture_hrtimeout_jiffies(torture_random(&rand) % 16,
  1388. &rand);
  1389. }
  1390. rcu_torture_pipe_update(old_rp);
  1391. break;
  1392. case RTWS_POLL_GET_FULL:
  1393. rcu_torture_writer_state = RTWS_POLL_GET_FULL;
  1394. for (i = 0; i < rgo_size; i++)
  1395. cur_ops->get_comp_state_full(&rgo[i]);
  1396. cur_ops->start_gp_poll_full(&gp_snap_full);
  1397. rcu_torture_writer_state = RTWS_POLL_WAIT_FULL;
  1398. while (!cur_ops->poll_gp_state_full(&gp_snap_full)) {
  1399. cur_ops->get_gp_state_full(&gp_snap1_full);
  1400. for (i = 0; i < rgo_size; i++)
  1401. if (cur_ops->poll_gp_state_full(&rgo[i]) ||
  1402. cur_ops->same_gp_state_full(&rgo[i],
  1403. &gp_snap1_full)) {
  1404. rgo[i] = gp_snap1_full;
  1405. break;
  1406. }
  1407. WARN_ON_ONCE(rgo_size > 0 && i >= rgo_size);
  1408. torture_hrtimeout_jiffies(torture_random(&rand) % 16,
  1409. &rand);
  1410. }
  1411. rcu_torture_pipe_update(old_rp);
  1412. break;
  1413. case RTWS_POLL_GET_EXP:
  1414. rcu_torture_writer_state = RTWS_POLL_GET_EXP;
  1415. gp_snap = cur_ops->start_gp_poll_exp();
  1416. rcu_torture_writer_state = RTWS_POLL_WAIT_EXP;
  1417. while (!cur_ops->poll_gp_state_exp(gp_snap))
  1418. torture_hrtimeout_jiffies(torture_random(&rand) % 16,
  1419. &rand);
  1420. rcu_torture_pipe_update(old_rp);
  1421. break;
  1422. case RTWS_POLL_GET_EXP_FULL:
  1423. rcu_torture_writer_state = RTWS_POLL_GET_EXP_FULL;
  1424. cur_ops->start_gp_poll_exp_full(&gp_snap_full);
  1425. rcu_torture_writer_state = RTWS_POLL_WAIT_EXP_FULL;
  1426. while (!cur_ops->poll_gp_state_full(&gp_snap_full))
  1427. torture_hrtimeout_jiffies(torture_random(&rand) % 16,
  1428. &rand);
  1429. rcu_torture_pipe_update(old_rp);
  1430. break;
  1431. case RTWS_SYNC:
  1432. rcu_torture_writer_state = RTWS_SYNC;
  1433. do_rtws_sync(&rand, cur_ops->sync);
  1434. rcu_torture_pipe_update(old_rp);
  1435. break;
  1436. default:
  1437. WARN_ON_ONCE(1);
  1438. break;
  1439. }
  1440. }
  1441. WRITE_ONCE(rcu_torture_current_version,
  1442. rcu_torture_current_version + 1);
  1443. /* Cycle through nesting levels of rcu_expedite_gp() calls. */
  1444. if (can_expedite &&
  1445. !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
  1446. WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
  1447. if (expediting >= 0)
  1448. rcu_expedite_gp();
  1449. else
  1450. rcu_unexpedite_gp();
  1451. if (++expediting > 3)
  1452. expediting = -expediting;
  1453. } else if (!can_expedite) { /* Disabled during boot, recheck. */
  1454. can_expedite = !rcu_gp_is_expedited() &&
  1455. !rcu_gp_is_normal();
  1456. }
  1457. rcu_torture_writer_state = RTWS_STUTTER;
  1458. boot_ended = rcu_inkernel_boot_has_ended();
  1459. stutter_waited = stutter_wait("rcu_torture_writer");
  1460. if (stutter_waited &&
  1461. !atomic_read(&rcu_fwd_cb_nodelay) &&
  1462. !cur_ops->slow_gps &&
  1463. !torture_must_stop() &&
  1464. boot_ended &&
  1465. time_after(jiffies, stallsdone))
  1466. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++)
  1467. if (list_empty(&rcu_tortures[i].rtort_free) &&
  1468. rcu_access_pointer(rcu_torture_current) != &rcu_tortures[i]) {
  1469. tracing_off();
  1470. if (cur_ops->gp_kthread_dbg)
  1471. cur_ops->gp_kthread_dbg();
  1472. WARN(1, "%s: rtort_pipe_count: %d\n", __func__, rcu_tortures[i].rtort_pipe_count);
  1473. rcu_ftrace_dump(DUMP_ALL);
  1474. }
  1475. if (stutter_waited)
  1476. sched_set_normal(current, oldnice);
  1477. } while (!torture_must_stop());
  1478. rcu_torture_current = NULL; // Let stats task know that we are done.
  1479. /* Reset expediting back to unexpedited. */
  1480. if (expediting > 0)
  1481. expediting = -expediting;
  1482. while (can_expedite && expediting++ < 0)
  1483. rcu_unexpedite_gp();
  1484. WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
  1485. if (!can_expedite)
  1486. pr_alert("%s" TORTURE_FLAG
  1487. " Dynamic grace-period expediting was disabled.\n",
  1488. torture_type);
  1489. kfree(ulo);
  1490. kfree(rgo);
  1491. rcu_torture_writer_state = RTWS_STOPPING;
  1492. torture_kthread_stopping("rcu_torture_writer");
  1493. return 0;
  1494. }
  1495. /*
  1496. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  1497. * delay between calls.
  1498. */
  1499. static int
  1500. rcu_torture_fakewriter(void *arg)
  1501. {
  1502. unsigned long gp_snap;
  1503. struct rcu_gp_oldstate gp_snap_full;
  1504. DEFINE_TORTURE_RANDOM(rand);
  1505. VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
  1506. set_user_nice(current, MAX_NICE);
  1507. if (WARN_ONCE(nsynctypes == 0,
  1508. "%s: No update-side primitives.\n", __func__)) {
  1509. /*
  1510. * No updates primitives, so don't try updating.
  1511. * The resulting test won't be testing much, hence the
  1512. * above WARN_ONCE().
  1513. */
  1514. torture_kthread_stopping("rcu_torture_fakewriter");
  1515. return 0;
  1516. }
  1517. do {
  1518. torture_hrtimeout_jiffies(torture_random(&rand) % 10, &rand);
  1519. if (cur_ops->cb_barrier != NULL &&
  1520. torture_random(&rand) % (nfakewriters * 8) == 0) {
  1521. cur_ops->cb_barrier();
  1522. } else {
  1523. switch (synctype[torture_random(&rand) % nsynctypes]) {
  1524. case RTWS_DEF_FREE:
  1525. break;
  1526. case RTWS_EXP_SYNC:
  1527. cur_ops->exp_sync();
  1528. break;
  1529. case RTWS_COND_GET:
  1530. gp_snap = cur_ops->get_gp_state();
  1531. torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
  1532. cur_ops->cond_sync(gp_snap);
  1533. break;
  1534. case RTWS_COND_GET_EXP:
  1535. gp_snap = cur_ops->get_gp_state_exp();
  1536. torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
  1537. cur_ops->cond_sync_exp(gp_snap);
  1538. break;
  1539. case RTWS_COND_GET_FULL:
  1540. cur_ops->get_gp_state_full(&gp_snap_full);
  1541. torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
  1542. cur_ops->cond_sync_full(&gp_snap_full);
  1543. break;
  1544. case RTWS_COND_GET_EXP_FULL:
  1545. cur_ops->get_gp_state_full(&gp_snap_full);
  1546. torture_hrtimeout_jiffies(torture_random(&rand) % 16, &rand);
  1547. cur_ops->cond_sync_exp_full(&gp_snap_full);
  1548. break;
  1549. case RTWS_POLL_GET:
  1550. gp_snap = cur_ops->start_gp_poll();
  1551. while (!cur_ops->poll_gp_state(gp_snap)) {
  1552. torture_hrtimeout_jiffies(torture_random(&rand) % 16,
  1553. &rand);
  1554. }
  1555. break;
  1556. case RTWS_POLL_GET_FULL:
  1557. cur_ops->start_gp_poll_full(&gp_snap_full);
  1558. while (!cur_ops->poll_gp_state_full(&gp_snap_full)) {
  1559. torture_hrtimeout_jiffies(torture_random(&rand) % 16,
  1560. &rand);
  1561. }
  1562. break;
  1563. case RTWS_POLL_GET_EXP:
  1564. gp_snap = cur_ops->start_gp_poll_exp();
  1565. while (!cur_ops->poll_gp_state_exp(gp_snap)) {
  1566. torture_hrtimeout_jiffies(torture_random(&rand) % 16,
  1567. &rand);
  1568. }
  1569. break;
  1570. case RTWS_POLL_GET_EXP_FULL:
  1571. cur_ops->start_gp_poll_exp_full(&gp_snap_full);
  1572. while (!cur_ops->poll_gp_state_full(&gp_snap_full)) {
  1573. torture_hrtimeout_jiffies(torture_random(&rand) % 16,
  1574. &rand);
  1575. }
  1576. break;
  1577. case RTWS_SYNC:
  1578. cur_ops->sync();
  1579. break;
  1580. default:
  1581. WARN_ON_ONCE(1);
  1582. break;
  1583. }
  1584. }
  1585. stutter_wait("rcu_torture_fakewriter");
  1586. } while (!torture_must_stop());
  1587. torture_kthread_stopping("rcu_torture_fakewriter");
  1588. return 0;
  1589. }
  1590. static void rcu_torture_timer_cb(struct rcu_head *rhp)
  1591. {
  1592. kfree(rhp);
  1593. }
  1594. // Set up and carry out testing of RCU's global memory ordering
  1595. static void rcu_torture_reader_do_mbchk(long myid, struct rcu_torture *rtp,
  1596. struct torture_random_state *trsp)
  1597. {
  1598. unsigned long loops;
  1599. int noc = torture_num_online_cpus();
  1600. int rdrchked;
  1601. int rdrchker;
  1602. struct rcu_torture_reader_check *rtrcp; // Me.
  1603. struct rcu_torture_reader_check *rtrcp_assigner; // Assigned us to do checking.
  1604. struct rcu_torture_reader_check *rtrcp_chked; // Reader being checked.
  1605. struct rcu_torture_reader_check *rtrcp_chker; // Reader doing checking when not me.
  1606. if (myid < 0)
  1607. return; // Don't try this from timer handlers.
  1608. // Increment my counter.
  1609. rtrcp = &rcu_torture_reader_mbchk[myid];
  1610. WRITE_ONCE(rtrcp->rtc_myloops, rtrcp->rtc_myloops + 1);
  1611. // Attempt to assign someone else some checking work.
  1612. rdrchked = torture_random(trsp) % nrealreaders;
  1613. rtrcp_chked = &rcu_torture_reader_mbchk[rdrchked];
  1614. rdrchker = torture_random(trsp) % nrealreaders;
  1615. rtrcp_chker = &rcu_torture_reader_mbchk[rdrchker];
  1616. if (rdrchked != myid && rdrchked != rdrchker && noc >= rdrchked && noc >= rdrchker &&
  1617. smp_load_acquire(&rtrcp->rtc_chkrdr) < 0 && // Pairs with smp_store_release below.
  1618. !READ_ONCE(rtp->rtort_chkp) &&
  1619. !smp_load_acquire(&rtrcp_chker->rtc_assigner)) { // Pairs with smp_store_release below.
  1620. rtrcp->rtc_chkloops = READ_ONCE(rtrcp_chked->rtc_myloops);
  1621. WARN_ON_ONCE(rtrcp->rtc_chkrdr >= 0);
  1622. rtrcp->rtc_chkrdr = rdrchked;
  1623. WARN_ON_ONCE(rtrcp->rtc_ready); // This gets set after the grace period ends.
  1624. if (cmpxchg_relaxed(&rtrcp_chker->rtc_assigner, NULL, rtrcp) ||
  1625. cmpxchg_relaxed(&rtp->rtort_chkp, NULL, rtrcp))
  1626. (void)cmpxchg_relaxed(&rtrcp_chker->rtc_assigner, rtrcp, NULL); // Back out.
  1627. }
  1628. // If assigned some completed work, do it!
  1629. rtrcp_assigner = READ_ONCE(rtrcp->rtc_assigner);
  1630. if (!rtrcp_assigner || !smp_load_acquire(&rtrcp_assigner->rtc_ready))
  1631. return; // No work or work not yet ready.
  1632. rdrchked = rtrcp_assigner->rtc_chkrdr;
  1633. if (WARN_ON_ONCE(rdrchked < 0))
  1634. return;
  1635. rtrcp_chked = &rcu_torture_reader_mbchk[rdrchked];
  1636. loops = READ_ONCE(rtrcp_chked->rtc_myloops);
  1637. atomic_inc(&n_rcu_torture_mbchk_tries);
  1638. if (ULONG_CMP_LT(loops, rtrcp_assigner->rtc_chkloops))
  1639. atomic_inc(&n_rcu_torture_mbchk_fail);
  1640. rtrcp_assigner->rtc_chkloops = loops + ULONG_MAX / 2;
  1641. rtrcp_assigner->rtc_ready = 0;
  1642. smp_store_release(&rtrcp->rtc_assigner, NULL); // Someone else can assign us work.
  1643. smp_store_release(&rtrcp_assigner->rtc_chkrdr, -1); // Assigner can again assign.
  1644. }
  1645. /*
  1646. * Do one extension of an RCU read-side critical section using the
  1647. * current reader state in readstate (set to zero for initial entry
  1648. * to extended critical section), set the new state as specified by
  1649. * newstate (set to zero for final exit from extended critical section),
  1650. * and random-number-generator state in trsp. If this is neither the
  1651. * beginning or end of the critical section and if there was actually a
  1652. * change, do a ->read_delay().
  1653. */
  1654. static void rcutorture_one_extend(int *readstate, int newstate,
  1655. struct torture_random_state *trsp,
  1656. struct rt_read_seg *rtrsp)
  1657. {
  1658. unsigned long flags;
  1659. int idxnew1 = -1;
  1660. int idxnew2 = -1;
  1661. int idxold1 = *readstate;
  1662. int idxold2 = idxold1;
  1663. int statesnew = ~*readstate & newstate;
  1664. int statesold = *readstate & ~newstate;
  1665. WARN_ON_ONCE(idxold2 < 0);
  1666. WARN_ON_ONCE((idxold2 >> RCUTORTURE_RDR_SHIFT_2) > 1);
  1667. rtrsp->rt_readstate = newstate;
  1668. /* First, put new protection in place to avoid critical-section gap. */
  1669. if (statesnew & RCUTORTURE_RDR_BH)
  1670. local_bh_disable();
  1671. if (statesnew & RCUTORTURE_RDR_RBH)
  1672. rcu_read_lock_bh();
  1673. if (statesnew & RCUTORTURE_RDR_IRQ)
  1674. local_irq_disable();
  1675. if (statesnew & RCUTORTURE_RDR_PREEMPT)
  1676. preempt_disable();
  1677. if (statesnew & RCUTORTURE_RDR_SCHED)
  1678. rcu_read_lock_sched();
  1679. if (statesnew & RCUTORTURE_RDR_RCU_1)
  1680. idxnew1 = (cur_ops->readlock() & 0x1) << RCUTORTURE_RDR_SHIFT_1;
  1681. if (statesnew & RCUTORTURE_RDR_RCU_2)
  1682. idxnew2 = (cur_ops->readlock() & 0x1) << RCUTORTURE_RDR_SHIFT_2;
  1683. /*
  1684. * Next, remove old protection, in decreasing order of strength
  1685. * to avoid unlock paths that aren't safe in the stronger
  1686. * context. Namely: BH can not be enabled with disabled interrupts.
  1687. * Additionally PREEMPT_RT requires that BH is enabled in preemptible
  1688. * context.
  1689. */
  1690. if (statesold & RCUTORTURE_RDR_IRQ)
  1691. local_irq_enable();
  1692. if (statesold & RCUTORTURE_RDR_PREEMPT)
  1693. preempt_enable();
  1694. if (statesold & RCUTORTURE_RDR_SCHED)
  1695. rcu_read_unlock_sched();
  1696. if (statesold & RCUTORTURE_RDR_BH)
  1697. local_bh_enable();
  1698. if (statesold & RCUTORTURE_RDR_RBH)
  1699. rcu_read_unlock_bh();
  1700. if (statesold & RCUTORTURE_RDR_RCU_2) {
  1701. cur_ops->readunlock((idxold2 >> RCUTORTURE_RDR_SHIFT_2) & 0x1);
  1702. WARN_ON_ONCE(idxnew2 != -1);
  1703. idxold2 = 0;
  1704. }
  1705. if (statesold & RCUTORTURE_RDR_RCU_1) {
  1706. bool lockit;
  1707. lockit = !cur_ops->no_pi_lock && !statesnew && !(torture_random(trsp) & 0xffff);
  1708. if (lockit)
  1709. raw_spin_lock_irqsave(&current->pi_lock, flags);
  1710. cur_ops->readunlock((idxold1 >> RCUTORTURE_RDR_SHIFT_1) & 0x1);
  1711. WARN_ON_ONCE(idxnew1 != -1);
  1712. idxold1 = 0;
  1713. if (lockit)
  1714. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  1715. }
  1716. /* Delay if neither beginning nor end and there was a change. */
  1717. if ((statesnew || statesold) && *readstate && newstate)
  1718. cur_ops->read_delay(trsp, rtrsp);
  1719. /* Update the reader state. */
  1720. if (idxnew1 == -1)
  1721. idxnew1 = idxold1 & RCUTORTURE_RDR_MASK_1;
  1722. WARN_ON_ONCE(idxnew1 < 0);
  1723. if (WARN_ON_ONCE((idxnew1 >> RCUTORTURE_RDR_SHIFT_1) > 1))
  1724. pr_info("Unexpected idxnew1 value of %#x\n", idxnew1);
  1725. if (idxnew2 == -1)
  1726. idxnew2 = idxold2 & RCUTORTURE_RDR_MASK_2;
  1727. WARN_ON_ONCE(idxnew2 < 0);
  1728. WARN_ON_ONCE((idxnew2 >> RCUTORTURE_RDR_SHIFT_2) > 1);
  1729. *readstate = idxnew1 | idxnew2 | newstate;
  1730. WARN_ON_ONCE(*readstate < 0);
  1731. if (WARN_ON_ONCE((*readstate >> RCUTORTURE_RDR_SHIFT_2) > 1))
  1732. pr_info("Unexpected idxnew2 value of %#x\n", idxnew2);
  1733. }
  1734. /* Return the biggest extendables mask given current RCU and boot parameters. */
  1735. static int rcutorture_extend_mask_max(void)
  1736. {
  1737. int mask;
  1738. WARN_ON_ONCE(extendables & ~RCUTORTURE_MAX_EXTEND);
  1739. mask = extendables & RCUTORTURE_MAX_EXTEND & cur_ops->extendables;
  1740. mask = mask | RCUTORTURE_RDR_RCU_1 | RCUTORTURE_RDR_RCU_2;
  1741. return mask;
  1742. }
  1743. /* Return a random protection state mask, but with at least one bit set. */
  1744. static int
  1745. rcutorture_extend_mask(int oldmask, struct torture_random_state *trsp)
  1746. {
  1747. int mask = rcutorture_extend_mask_max();
  1748. unsigned long randmask1 = torture_random(trsp);
  1749. unsigned long randmask2 = randmask1 >> 3;
  1750. unsigned long preempts = RCUTORTURE_RDR_PREEMPT | RCUTORTURE_RDR_SCHED;
  1751. unsigned long preempts_irq = preempts | RCUTORTURE_RDR_IRQ;
  1752. unsigned long bhs = RCUTORTURE_RDR_BH | RCUTORTURE_RDR_RBH;
  1753. WARN_ON_ONCE(mask >> RCUTORTURE_RDR_SHIFT_1);
  1754. /* Mostly only one bit (need preemption!), sometimes lots of bits. */
  1755. if (!(randmask1 & 0x7))
  1756. mask = mask & randmask2;
  1757. else
  1758. mask = mask & (1 << (randmask2 % RCUTORTURE_RDR_NBITS));
  1759. // Can't have nested RCU reader without outer RCU reader.
  1760. if (!(mask & RCUTORTURE_RDR_RCU_1) && (mask & RCUTORTURE_RDR_RCU_2)) {
  1761. if (oldmask & RCUTORTURE_RDR_RCU_1)
  1762. mask &= ~RCUTORTURE_RDR_RCU_2;
  1763. else
  1764. mask |= RCUTORTURE_RDR_RCU_1;
  1765. }
  1766. /*
  1767. * Can't enable bh w/irq disabled.
  1768. */
  1769. if (mask & RCUTORTURE_RDR_IRQ)
  1770. mask |= oldmask & bhs;
  1771. /*
  1772. * Ideally these sequences would be detected in debug builds
  1773. * (regardless of RT), but until then don't stop testing
  1774. * them on non-RT.
  1775. */
  1776. if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
  1777. /* Can't modify BH in atomic context */
  1778. if (oldmask & preempts_irq)
  1779. mask &= ~bhs;
  1780. if ((oldmask | mask) & preempts_irq)
  1781. mask |= oldmask & bhs;
  1782. }
  1783. return mask ?: RCUTORTURE_RDR_RCU_1;
  1784. }
  1785. /*
  1786. * Do a randomly selected number of extensions of an existing RCU read-side
  1787. * critical section.
  1788. */
  1789. static struct rt_read_seg *
  1790. rcutorture_loop_extend(int *readstate, struct torture_random_state *trsp,
  1791. struct rt_read_seg *rtrsp)
  1792. {
  1793. int i;
  1794. int j;
  1795. int mask = rcutorture_extend_mask_max();
  1796. WARN_ON_ONCE(!*readstate); /* -Existing- RCU read-side critsect! */
  1797. if (!((mask - 1) & mask))
  1798. return rtrsp; /* Current RCU reader not extendable. */
  1799. /* Bias towards larger numbers of loops. */
  1800. i = torture_random(trsp);
  1801. i = ((i | (i >> 3)) & RCUTORTURE_RDR_MAX_LOOPS) + 1;
  1802. for (j = 0; j < i; j++) {
  1803. mask = rcutorture_extend_mask(*readstate, trsp);
  1804. rcutorture_one_extend(readstate, mask, trsp, &rtrsp[j]);
  1805. }
  1806. return &rtrsp[j];
  1807. }
  1808. /*
  1809. * Do one read-side critical section, returning false if there was
  1810. * no data to read. Can be invoked both from process context and
  1811. * from a timer handler.
  1812. */
  1813. static bool rcu_torture_one_read(struct torture_random_state *trsp, long myid)
  1814. {
  1815. bool checkpolling = !(torture_random(trsp) & 0xfff);
  1816. unsigned long cookie;
  1817. struct rcu_gp_oldstate cookie_full;
  1818. int i;
  1819. unsigned long started;
  1820. unsigned long completed;
  1821. int newstate;
  1822. struct rcu_torture *p;
  1823. int pipe_count;
  1824. int readstate = 0;
  1825. struct rt_read_seg rtseg[RCUTORTURE_RDR_MAX_SEGS] = { { 0 } };
  1826. struct rt_read_seg *rtrsp = &rtseg[0];
  1827. struct rt_read_seg *rtrsp1;
  1828. unsigned long long ts;
  1829. WARN_ON_ONCE(!rcu_is_watching());
  1830. newstate = rcutorture_extend_mask(readstate, trsp);
  1831. rcutorture_one_extend(&readstate, newstate, trsp, rtrsp++);
  1832. if (checkpolling) {
  1833. if (cur_ops->get_gp_state && cur_ops->poll_gp_state)
  1834. cookie = cur_ops->get_gp_state();
  1835. if (cur_ops->get_gp_state_full && cur_ops->poll_gp_state_full)
  1836. cur_ops->get_gp_state_full(&cookie_full);
  1837. }
  1838. started = cur_ops->get_gp_seq();
  1839. ts = rcu_trace_clock_local();
  1840. p = rcu_dereference_check(rcu_torture_current,
  1841. !cur_ops->readlock_held || cur_ops->readlock_held());
  1842. if (p == NULL) {
  1843. /* Wait for rcu_torture_writer to get underway */
  1844. rcutorture_one_extend(&readstate, 0, trsp, rtrsp);
  1845. return false;
  1846. }
  1847. if (p->rtort_mbtest == 0)
  1848. atomic_inc(&n_rcu_torture_mberror);
  1849. rcu_torture_reader_do_mbchk(myid, p, trsp);
  1850. rtrsp = rcutorture_loop_extend(&readstate, trsp, rtrsp);
  1851. preempt_disable();
  1852. pipe_count = READ_ONCE(p->rtort_pipe_count);
  1853. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  1854. // Should not happen in a correct RCU implementation,
  1855. // happens quite often for torture_type=busted.
  1856. pipe_count = RCU_TORTURE_PIPE_LEN;
  1857. }
  1858. completed = cur_ops->get_gp_seq();
  1859. if (pipe_count > 1) {
  1860. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
  1861. ts, started, completed);
  1862. rcu_ftrace_dump(DUMP_ALL);
  1863. }
  1864. __this_cpu_inc(rcu_torture_count[pipe_count]);
  1865. completed = rcutorture_seq_diff(completed, started);
  1866. if (completed > RCU_TORTURE_PIPE_LEN) {
  1867. /* Should not happen, but... */
  1868. completed = RCU_TORTURE_PIPE_LEN;
  1869. }
  1870. __this_cpu_inc(rcu_torture_batch[completed]);
  1871. preempt_enable();
  1872. if (checkpolling) {
  1873. if (cur_ops->get_gp_state && cur_ops->poll_gp_state)
  1874. WARN_ONCE(cur_ops->poll_gp_state(cookie),
  1875. "%s: Cookie check 2 failed %s(%d) %lu->%lu\n",
  1876. __func__,
  1877. rcu_torture_writer_state_getname(),
  1878. rcu_torture_writer_state,
  1879. cookie, cur_ops->get_gp_state());
  1880. if (cur_ops->get_gp_state_full && cur_ops->poll_gp_state_full)
  1881. WARN_ONCE(cur_ops->poll_gp_state_full(&cookie_full),
  1882. "%s: Cookie check 6 failed %s(%d) online %*pbl\n",
  1883. __func__,
  1884. rcu_torture_writer_state_getname(),
  1885. rcu_torture_writer_state,
  1886. cpumask_pr_args(cpu_online_mask));
  1887. }
  1888. rcutorture_one_extend(&readstate, 0, trsp, rtrsp);
  1889. WARN_ON_ONCE(readstate);
  1890. // This next splat is expected behavior if leakpointer, especially
  1891. // for CONFIG_RCU_STRICT_GRACE_PERIOD=y kernels.
  1892. WARN_ON_ONCE(leakpointer && READ_ONCE(p->rtort_pipe_count) > 1);
  1893. /* If error or close call, record the sequence of reader protections. */
  1894. if ((pipe_count > 1 || completed > 1) && !xchg(&err_segs_recorded, 1)) {
  1895. i = 0;
  1896. for (rtrsp1 = &rtseg[0]; rtrsp1 < rtrsp; rtrsp1++)
  1897. err_segs[i++] = *rtrsp1;
  1898. rt_read_nsegs = i;
  1899. }
  1900. return true;
  1901. }
  1902. static DEFINE_TORTURE_RANDOM_PERCPU(rcu_torture_timer_rand);
  1903. /*
  1904. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  1905. * incrementing the corresponding element of the pipeline array. The
  1906. * counter in the element should never be greater than 1, otherwise, the
  1907. * RCU implementation is broken.
  1908. */
  1909. static void rcu_torture_timer(struct timer_list *unused)
  1910. {
  1911. atomic_long_inc(&n_rcu_torture_timers);
  1912. (void)rcu_torture_one_read(this_cpu_ptr(&rcu_torture_timer_rand), -1);
  1913. /* Test call_rcu() invocation from interrupt handler. */
  1914. if (cur_ops->call) {
  1915. struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
  1916. if (rhp)
  1917. cur_ops->call(rhp, rcu_torture_timer_cb);
  1918. }
  1919. }
  1920. /*
  1921. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  1922. * incrementing the corresponding element of the pipeline array. The
  1923. * counter in the element should never be greater than 1, otherwise, the
  1924. * RCU implementation is broken.
  1925. */
  1926. static int
  1927. rcu_torture_reader(void *arg)
  1928. {
  1929. unsigned long lastsleep = jiffies;
  1930. long myid = (long)arg;
  1931. int mynumonline = myid;
  1932. DEFINE_TORTURE_RANDOM(rand);
  1933. struct timer_list t;
  1934. VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
  1935. set_user_nice(current, MAX_NICE);
  1936. if (irqreader && cur_ops->irq_capable)
  1937. timer_setup_on_stack(&t, rcu_torture_timer, 0);
  1938. tick_dep_set_task(current, TICK_DEP_BIT_RCU);
  1939. do {
  1940. if (irqreader && cur_ops->irq_capable) {
  1941. if (!timer_pending(&t))
  1942. mod_timer(&t, jiffies + 1);
  1943. }
  1944. if (!rcu_torture_one_read(&rand, myid) && !torture_must_stop())
  1945. schedule_timeout_interruptible(HZ);
  1946. if (time_after(jiffies, lastsleep) && !torture_must_stop()) {
  1947. torture_hrtimeout_us(500, 1000, &rand);
  1948. lastsleep = jiffies + 10;
  1949. }
  1950. while (torture_num_online_cpus() < mynumonline && !torture_must_stop())
  1951. schedule_timeout_interruptible(HZ / 5);
  1952. stutter_wait("rcu_torture_reader");
  1953. } while (!torture_must_stop());
  1954. if (irqreader && cur_ops->irq_capable) {
  1955. del_timer_sync(&t);
  1956. destroy_timer_on_stack(&t);
  1957. }
  1958. tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
  1959. torture_kthread_stopping("rcu_torture_reader");
  1960. return 0;
  1961. }
  1962. /*
  1963. * Randomly Toggle CPUs' callback-offload state. This uses hrtimers to
  1964. * increase race probabilities and fuzzes the interval between toggling.
  1965. */
  1966. static int rcu_nocb_toggle(void *arg)
  1967. {
  1968. int cpu;
  1969. int maxcpu = -1;
  1970. int oldnice = task_nice(current);
  1971. long r;
  1972. DEFINE_TORTURE_RANDOM(rand);
  1973. ktime_t toggle_delay;
  1974. unsigned long toggle_fuzz;
  1975. ktime_t toggle_interval = ms_to_ktime(nocbs_toggle);
  1976. VERBOSE_TOROUT_STRING("rcu_nocb_toggle task started");
  1977. while (!rcu_inkernel_boot_has_ended())
  1978. schedule_timeout_interruptible(HZ / 10);
  1979. for_each_possible_cpu(cpu)
  1980. maxcpu = cpu;
  1981. WARN_ON(maxcpu < 0);
  1982. if (toggle_interval > ULONG_MAX)
  1983. toggle_fuzz = ULONG_MAX >> 3;
  1984. else
  1985. toggle_fuzz = toggle_interval >> 3;
  1986. if (toggle_fuzz <= 0)
  1987. toggle_fuzz = NSEC_PER_USEC;
  1988. do {
  1989. r = torture_random(&rand);
  1990. cpu = (r >> 1) % (maxcpu + 1);
  1991. if (r & 0x1) {
  1992. rcu_nocb_cpu_offload(cpu);
  1993. atomic_long_inc(&n_nocb_offload);
  1994. } else {
  1995. rcu_nocb_cpu_deoffload(cpu);
  1996. atomic_long_inc(&n_nocb_deoffload);
  1997. }
  1998. toggle_delay = torture_random(&rand) % toggle_fuzz + toggle_interval;
  1999. set_current_state(TASK_INTERRUPTIBLE);
  2000. schedule_hrtimeout(&toggle_delay, HRTIMER_MODE_REL);
  2001. if (stutter_wait("rcu_nocb_toggle"))
  2002. sched_set_normal(current, oldnice);
  2003. } while (!torture_must_stop());
  2004. torture_kthread_stopping("rcu_nocb_toggle");
  2005. return 0;
  2006. }
  2007. /*
  2008. * Print torture statistics. Caller must ensure that there is only
  2009. * one call to this function at a given time!!! This is normally
  2010. * accomplished by relying on the module system to only have one copy
  2011. * of the module loaded, and then by giving the rcu_torture_stats
  2012. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  2013. * thread is not running).
  2014. */
  2015. static void
  2016. rcu_torture_stats_print(void)
  2017. {
  2018. int cpu;
  2019. int i;
  2020. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  2021. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  2022. struct rcu_torture *rtcp;
  2023. static unsigned long rtcv_snap = ULONG_MAX;
  2024. static bool splatted;
  2025. struct task_struct *wtp;
  2026. for_each_possible_cpu(cpu) {
  2027. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  2028. pipesummary[i] += READ_ONCE(per_cpu(rcu_torture_count, cpu)[i]);
  2029. batchsummary[i] += READ_ONCE(per_cpu(rcu_torture_batch, cpu)[i]);
  2030. }
  2031. }
  2032. for (i = RCU_TORTURE_PIPE_LEN; i >= 0; i--) {
  2033. if (pipesummary[i] != 0)
  2034. break;
  2035. }
  2036. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  2037. rtcp = rcu_access_pointer(rcu_torture_current);
  2038. pr_cont("rtc: %p %s: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
  2039. rtcp,
  2040. rtcp && !rcu_stall_is_suppressed_at_boot() ? "ver" : "VER",
  2041. rcu_torture_current_version,
  2042. list_empty(&rcu_torture_freelist),
  2043. atomic_read(&n_rcu_torture_alloc),
  2044. atomic_read(&n_rcu_torture_alloc_fail),
  2045. atomic_read(&n_rcu_torture_free));
  2046. pr_cont("rtmbe: %d rtmbkf: %d/%d rtbe: %ld rtbke: %ld ",
  2047. atomic_read(&n_rcu_torture_mberror),
  2048. atomic_read(&n_rcu_torture_mbchk_fail), atomic_read(&n_rcu_torture_mbchk_tries),
  2049. n_rcu_torture_barrier_error,
  2050. n_rcu_torture_boost_ktrerror);
  2051. pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
  2052. n_rcu_torture_boost_failure,
  2053. n_rcu_torture_boosts,
  2054. atomic_long_read(&n_rcu_torture_timers));
  2055. torture_onoff_stats();
  2056. pr_cont("barrier: %ld/%ld:%ld ",
  2057. data_race(n_barrier_successes),
  2058. data_race(n_barrier_attempts),
  2059. data_race(n_rcu_torture_barrier_error));
  2060. pr_cont("read-exits: %ld ", data_race(n_read_exits)); // Statistic.
  2061. pr_cont("nocb-toggles: %ld:%ld\n",
  2062. atomic_long_read(&n_nocb_offload), atomic_long_read(&n_nocb_deoffload));
  2063. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  2064. if (atomic_read(&n_rcu_torture_mberror) ||
  2065. atomic_read(&n_rcu_torture_mbchk_fail) ||
  2066. n_rcu_torture_barrier_error || n_rcu_torture_boost_ktrerror ||
  2067. n_rcu_torture_boost_failure || i > 1) {
  2068. pr_cont("%s", "!!! ");
  2069. atomic_inc(&n_rcu_torture_error);
  2070. WARN_ON_ONCE(atomic_read(&n_rcu_torture_mberror));
  2071. WARN_ON_ONCE(atomic_read(&n_rcu_torture_mbchk_fail));
  2072. WARN_ON_ONCE(n_rcu_torture_barrier_error); // rcu_barrier()
  2073. WARN_ON_ONCE(n_rcu_torture_boost_ktrerror); // no boost kthread
  2074. WARN_ON_ONCE(n_rcu_torture_boost_failure); // boost failed (TIMER_SOFTIRQ RT prio?)
  2075. WARN_ON_ONCE(i > 1); // Too-short grace period
  2076. }
  2077. pr_cont("Reader Pipe: ");
  2078. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  2079. pr_cont(" %ld", pipesummary[i]);
  2080. pr_cont("\n");
  2081. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  2082. pr_cont("Reader Batch: ");
  2083. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  2084. pr_cont(" %ld", batchsummary[i]);
  2085. pr_cont("\n");
  2086. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  2087. pr_cont("Free-Block Circulation: ");
  2088. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  2089. pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
  2090. }
  2091. pr_cont("\n");
  2092. if (cur_ops->stats)
  2093. cur_ops->stats();
  2094. if (rtcv_snap == rcu_torture_current_version &&
  2095. rcu_access_pointer(rcu_torture_current) &&
  2096. !rcu_stall_is_suppressed()) {
  2097. int __maybe_unused flags = 0;
  2098. unsigned long __maybe_unused gp_seq = 0;
  2099. if (cur_ops->get_gp_data)
  2100. cur_ops->get_gp_data(&flags, &gp_seq);
  2101. wtp = READ_ONCE(writer_task);
  2102. pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#x cpu %d\n",
  2103. rcu_torture_writer_state_getname(),
  2104. rcu_torture_writer_state, gp_seq, flags,
  2105. wtp == NULL ? ~0U : wtp->__state,
  2106. wtp == NULL ? -1 : (int)task_cpu(wtp));
  2107. if (!splatted && wtp) {
  2108. sched_show_task(wtp);
  2109. splatted = true;
  2110. }
  2111. if (cur_ops->gp_kthread_dbg)
  2112. cur_ops->gp_kthread_dbg();
  2113. rcu_ftrace_dump(DUMP_ALL);
  2114. }
  2115. rtcv_snap = rcu_torture_current_version;
  2116. }
  2117. /*
  2118. * Periodically prints torture statistics, if periodic statistics printing
  2119. * was specified via the stat_interval module parameter.
  2120. */
  2121. static int
  2122. rcu_torture_stats(void *arg)
  2123. {
  2124. VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
  2125. do {
  2126. schedule_timeout_interruptible(stat_interval * HZ);
  2127. rcu_torture_stats_print();
  2128. torture_shutdown_absorb("rcu_torture_stats");
  2129. } while (!torture_must_stop());
  2130. torture_kthread_stopping("rcu_torture_stats");
  2131. return 0;
  2132. }
  2133. /* Test mem_dump_obj() and friends. */
  2134. static void rcu_torture_mem_dump_obj(void)
  2135. {
  2136. struct rcu_head *rhp;
  2137. struct kmem_cache *kcp;
  2138. static int z;
  2139. kcp = kmem_cache_create("rcuscale", 136, 8, SLAB_STORE_USER, NULL);
  2140. if (WARN_ON_ONCE(!kcp))
  2141. return;
  2142. rhp = kmem_cache_alloc(kcp, GFP_KERNEL);
  2143. if (WARN_ON_ONCE(!rhp)) {
  2144. kmem_cache_destroy(kcp);
  2145. return;
  2146. }
  2147. pr_alert("mem_dump_obj() slab test: rcu_torture_stats = %px, &rhp = %px, rhp = %px, &z = %px\n", stats_task, &rhp, rhp, &z);
  2148. pr_alert("mem_dump_obj(ZERO_SIZE_PTR):");
  2149. mem_dump_obj(ZERO_SIZE_PTR);
  2150. pr_alert("mem_dump_obj(NULL):");
  2151. mem_dump_obj(NULL);
  2152. pr_alert("mem_dump_obj(%px):", &rhp);
  2153. mem_dump_obj(&rhp);
  2154. pr_alert("mem_dump_obj(%px):", rhp);
  2155. mem_dump_obj(rhp);
  2156. pr_alert("mem_dump_obj(%px):", &rhp->func);
  2157. mem_dump_obj(&rhp->func);
  2158. pr_alert("mem_dump_obj(%px):", &z);
  2159. mem_dump_obj(&z);
  2160. kmem_cache_free(kcp, rhp);
  2161. kmem_cache_destroy(kcp);
  2162. rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
  2163. if (WARN_ON_ONCE(!rhp))
  2164. return;
  2165. pr_alert("mem_dump_obj() kmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
  2166. pr_alert("mem_dump_obj(kmalloc %px):", rhp);
  2167. mem_dump_obj(rhp);
  2168. pr_alert("mem_dump_obj(kmalloc %px):", &rhp->func);
  2169. mem_dump_obj(&rhp->func);
  2170. kfree(rhp);
  2171. rhp = vmalloc(4096);
  2172. if (WARN_ON_ONCE(!rhp))
  2173. return;
  2174. pr_alert("mem_dump_obj() vmalloc test: rcu_torture_stats = %px, &rhp = %px, rhp = %px\n", stats_task, &rhp, rhp);
  2175. pr_alert("mem_dump_obj(vmalloc %px):", rhp);
  2176. mem_dump_obj(rhp);
  2177. pr_alert("mem_dump_obj(vmalloc %px):", &rhp->func);
  2178. mem_dump_obj(&rhp->func);
  2179. vfree(rhp);
  2180. }
  2181. static void
  2182. rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
  2183. {
  2184. pr_alert("%s" TORTURE_FLAG
  2185. "--- %s: nreaders=%d nfakewriters=%d "
  2186. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  2187. "shuffle_interval=%d stutter=%d irqreader=%d "
  2188. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
  2189. "test_boost=%d/%d test_boost_interval=%d "
  2190. "test_boost_duration=%d shutdown_secs=%d "
  2191. "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
  2192. "stall_cpu_block=%d stall_cpu_repeat=%d "
  2193. "n_barrier_cbs=%d "
  2194. "onoff_interval=%d onoff_holdoff=%d "
  2195. "read_exit_delay=%d read_exit_burst=%d "
  2196. "nocbs_nthreads=%d nocbs_toggle=%d "
  2197. "test_nmis=%d\n",
  2198. torture_type, tag, nrealreaders, nfakewriters,
  2199. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  2200. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
  2201. test_boost, cur_ops->can_boost,
  2202. test_boost_interval, test_boost_duration, shutdown_secs,
  2203. stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
  2204. stall_cpu_block, stall_cpu_repeat,
  2205. n_barrier_cbs,
  2206. onoff_interval, onoff_holdoff,
  2207. read_exit_delay, read_exit_burst,
  2208. nocbs_nthreads, nocbs_toggle,
  2209. test_nmis);
  2210. }
  2211. static int rcutorture_booster_cleanup(unsigned int cpu)
  2212. {
  2213. struct task_struct *t;
  2214. if (boost_tasks[cpu] == NULL)
  2215. return 0;
  2216. mutex_lock(&boost_mutex);
  2217. t = boost_tasks[cpu];
  2218. boost_tasks[cpu] = NULL;
  2219. rcu_torture_enable_rt_throttle();
  2220. mutex_unlock(&boost_mutex);
  2221. /* This must be outside of the mutex, otherwise deadlock! */
  2222. torture_stop_kthread(rcu_torture_boost, t);
  2223. return 0;
  2224. }
  2225. static int rcutorture_booster_init(unsigned int cpu)
  2226. {
  2227. int retval;
  2228. if (boost_tasks[cpu] != NULL)
  2229. return 0; /* Already created, nothing more to do. */
  2230. // Testing RCU priority boosting requires rcutorture do
  2231. // some serious abuse. Counter this by running ksoftirqd
  2232. // at higher priority.
  2233. if (IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)) {
  2234. struct sched_param sp;
  2235. struct task_struct *t;
  2236. t = per_cpu(ksoftirqd, cpu);
  2237. WARN_ON_ONCE(!t);
  2238. sp.sched_priority = 2;
  2239. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  2240. }
  2241. /* Don't allow time recalculation while creating a new task. */
  2242. mutex_lock(&boost_mutex);
  2243. rcu_torture_disable_rt_throttle();
  2244. VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
  2245. boost_tasks[cpu] = kthread_run_on_cpu(rcu_torture_boost, NULL,
  2246. cpu, "rcu_torture_boost_%u");
  2247. if (IS_ERR(boost_tasks[cpu])) {
  2248. retval = PTR_ERR(boost_tasks[cpu]);
  2249. VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
  2250. n_rcu_torture_boost_ktrerror++;
  2251. boost_tasks[cpu] = NULL;
  2252. mutex_unlock(&boost_mutex);
  2253. return retval;
  2254. }
  2255. mutex_unlock(&boost_mutex);
  2256. return 0;
  2257. }
  2258. static int rcu_torture_stall_nf(struct notifier_block *nb, unsigned long v, void *ptr)
  2259. {
  2260. pr_info("%s: v=%lu, duration=%lu.\n", __func__, v, (unsigned long)ptr);
  2261. return NOTIFY_OK;
  2262. }
  2263. static struct notifier_block rcu_torture_stall_block = {
  2264. .notifier_call = rcu_torture_stall_nf,
  2265. };
  2266. /*
  2267. * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
  2268. * induces a CPU stall for the time specified by stall_cpu. If a new
  2269. * stall test is added, stallsdone in rcu_torture_writer() must be adjusted.
  2270. */
  2271. static void rcu_torture_stall_one(int rep, int irqsoff)
  2272. {
  2273. int idx;
  2274. unsigned long stop_at;
  2275. if (stall_cpu_holdoff > 0) {
  2276. VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
  2277. schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
  2278. VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
  2279. }
  2280. if (!kthread_should_stop() && stall_gp_kthread > 0) {
  2281. VERBOSE_TOROUT_STRING("rcu_torture_stall begin GP stall");
  2282. rcu_gp_set_torture_wait(stall_gp_kthread * HZ);
  2283. for (idx = 0; idx < stall_gp_kthread + 2; idx++) {
  2284. if (kthread_should_stop())
  2285. break;
  2286. schedule_timeout_uninterruptible(HZ);
  2287. }
  2288. }
  2289. if (!kthread_should_stop() && stall_cpu > 0) {
  2290. VERBOSE_TOROUT_STRING("rcu_torture_stall begin CPU stall");
  2291. stop_at = ktime_get_seconds() + stall_cpu;
  2292. /* RCU CPU stall is expected behavior in following code. */
  2293. idx = cur_ops->readlock();
  2294. if (irqsoff)
  2295. local_irq_disable();
  2296. else if (!stall_cpu_block)
  2297. preempt_disable();
  2298. pr_alert("%s start stall episode %d on CPU %d.\n",
  2299. __func__, rep + 1, raw_smp_processor_id());
  2300. while (ULONG_CMP_LT((unsigned long)ktime_get_seconds(), stop_at) &&
  2301. !kthread_should_stop())
  2302. if (stall_cpu_block) {
  2303. #ifdef CONFIG_PREEMPTION
  2304. preempt_schedule();
  2305. #else
  2306. schedule_timeout_uninterruptible(HZ);
  2307. #endif
  2308. } else if (stall_no_softlockup) {
  2309. touch_softlockup_watchdog();
  2310. }
  2311. if (irqsoff)
  2312. local_irq_enable();
  2313. else if (!stall_cpu_block)
  2314. preempt_enable();
  2315. cur_ops->readunlock(idx);
  2316. }
  2317. }
  2318. /*
  2319. * CPU-stall kthread. Invokes rcu_torture_stall_one() once, and then as many
  2320. * additional times as specified by the stall_cpu_repeat module parameter.
  2321. * Note that stall_cpu_irqsoff is ignored on the second and subsequent
  2322. * stall.
  2323. */
  2324. static int rcu_torture_stall(void *args)
  2325. {
  2326. int i;
  2327. int repeat = stall_cpu_repeat;
  2328. int ret;
  2329. VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
  2330. if (repeat < 0) {
  2331. repeat = 0;
  2332. WARN_ON_ONCE(IS_BUILTIN(CONFIG_RCU_TORTURE_TEST));
  2333. }
  2334. if (rcu_cpu_stall_notifiers) {
  2335. ret = rcu_stall_chain_notifier_register(&rcu_torture_stall_block);
  2336. if (ret)
  2337. pr_info("%s: rcu_stall_chain_notifier_register() returned %d, %sexpected.\n",
  2338. __func__, ret, !IS_ENABLED(CONFIG_RCU_STALL_COMMON) ? "un" : "");
  2339. }
  2340. for (i = 0; i <= repeat; i++) {
  2341. if (kthread_should_stop())
  2342. break;
  2343. rcu_torture_stall_one(i, i == 0 ? stall_cpu_irqsoff : 0);
  2344. }
  2345. pr_alert("%s end.\n", __func__);
  2346. if (rcu_cpu_stall_notifiers && !ret) {
  2347. ret = rcu_stall_chain_notifier_unregister(&rcu_torture_stall_block);
  2348. if (ret)
  2349. pr_info("%s: rcu_stall_chain_notifier_unregister() returned %d.\n", __func__, ret);
  2350. }
  2351. torture_shutdown_absorb("rcu_torture_stall");
  2352. while (!kthread_should_stop())
  2353. schedule_timeout_interruptible(10 * HZ);
  2354. return 0;
  2355. }
  2356. /* Spawn CPU-stall kthread, if stall_cpu specified. */
  2357. static int __init rcu_torture_stall_init(void)
  2358. {
  2359. if (stall_cpu <= 0 && stall_gp_kthread <= 0)
  2360. return 0;
  2361. return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
  2362. }
  2363. /* State structure for forward-progress self-propagating RCU callback. */
  2364. struct fwd_cb_state {
  2365. struct rcu_head rh;
  2366. int stop;
  2367. };
  2368. /*
  2369. * Forward-progress self-propagating RCU callback function. Because
  2370. * callbacks run from softirq, this function is an implicit RCU read-side
  2371. * critical section.
  2372. */
  2373. static void rcu_torture_fwd_prog_cb(struct rcu_head *rhp)
  2374. {
  2375. struct fwd_cb_state *fcsp = container_of(rhp, struct fwd_cb_state, rh);
  2376. if (READ_ONCE(fcsp->stop)) {
  2377. WRITE_ONCE(fcsp->stop, 2);
  2378. return;
  2379. }
  2380. cur_ops->call(&fcsp->rh, rcu_torture_fwd_prog_cb);
  2381. }
  2382. /* State for continuous-flood RCU callbacks. */
  2383. struct rcu_fwd_cb {
  2384. struct rcu_head rh;
  2385. struct rcu_fwd_cb *rfc_next;
  2386. struct rcu_fwd *rfc_rfp;
  2387. int rfc_gps;
  2388. };
  2389. #define MAX_FWD_CB_JIFFIES (8 * HZ) /* Maximum CB test duration. */
  2390. #define MIN_FWD_CB_LAUNDERS 3 /* This many CB invocations to count. */
  2391. #define MIN_FWD_CBS_LAUNDERED 100 /* Number of counted CBs. */
  2392. #define FWD_CBS_HIST_DIV 10 /* Histogram buckets/second. */
  2393. #define N_LAUNDERS_HIST (2 * MAX_FWD_CB_JIFFIES / (HZ / FWD_CBS_HIST_DIV))
  2394. struct rcu_launder_hist {
  2395. long n_launders;
  2396. unsigned long launder_gp_seq;
  2397. };
  2398. struct rcu_fwd {
  2399. spinlock_t rcu_fwd_lock;
  2400. struct rcu_fwd_cb *rcu_fwd_cb_head;
  2401. struct rcu_fwd_cb **rcu_fwd_cb_tail;
  2402. long n_launders_cb;
  2403. unsigned long rcu_fwd_startat;
  2404. struct rcu_launder_hist n_launders_hist[N_LAUNDERS_HIST];
  2405. unsigned long rcu_launder_gp_seq_start;
  2406. int rcu_fwd_id;
  2407. };
  2408. static DEFINE_MUTEX(rcu_fwd_mutex);
  2409. static struct rcu_fwd *rcu_fwds;
  2410. static unsigned long rcu_fwd_seq;
  2411. static atomic_long_t rcu_fwd_max_cbs;
  2412. static bool rcu_fwd_emergency_stop;
  2413. static void rcu_torture_fwd_cb_hist(struct rcu_fwd *rfp)
  2414. {
  2415. unsigned long gps;
  2416. unsigned long gps_old;
  2417. int i;
  2418. int j;
  2419. for (i = ARRAY_SIZE(rfp->n_launders_hist) - 1; i > 0; i--)
  2420. if (rfp->n_launders_hist[i].n_launders > 0)
  2421. break;
  2422. pr_alert("%s: Callback-invocation histogram %d (duration %lu jiffies):",
  2423. __func__, rfp->rcu_fwd_id, jiffies - rfp->rcu_fwd_startat);
  2424. gps_old = rfp->rcu_launder_gp_seq_start;
  2425. for (j = 0; j <= i; j++) {
  2426. gps = rfp->n_launders_hist[j].launder_gp_seq;
  2427. pr_cont(" %ds/%d: %ld:%ld",
  2428. j + 1, FWD_CBS_HIST_DIV,
  2429. rfp->n_launders_hist[j].n_launders,
  2430. rcutorture_seq_diff(gps, gps_old));
  2431. gps_old = gps;
  2432. }
  2433. pr_cont("\n");
  2434. }
  2435. /* Callback function for continuous-flood RCU callbacks. */
  2436. static void rcu_torture_fwd_cb_cr(struct rcu_head *rhp)
  2437. {
  2438. unsigned long flags;
  2439. int i;
  2440. struct rcu_fwd_cb *rfcp = container_of(rhp, struct rcu_fwd_cb, rh);
  2441. struct rcu_fwd_cb **rfcpp;
  2442. struct rcu_fwd *rfp = rfcp->rfc_rfp;
  2443. rfcp->rfc_next = NULL;
  2444. rfcp->rfc_gps++;
  2445. spin_lock_irqsave(&rfp->rcu_fwd_lock, flags);
  2446. rfcpp = rfp->rcu_fwd_cb_tail;
  2447. rfp->rcu_fwd_cb_tail = &rfcp->rfc_next;
  2448. smp_store_release(rfcpp, rfcp);
  2449. WRITE_ONCE(rfp->n_launders_cb, rfp->n_launders_cb + 1);
  2450. i = ((jiffies - rfp->rcu_fwd_startat) / (HZ / FWD_CBS_HIST_DIV));
  2451. if (i >= ARRAY_SIZE(rfp->n_launders_hist))
  2452. i = ARRAY_SIZE(rfp->n_launders_hist) - 1;
  2453. rfp->n_launders_hist[i].n_launders++;
  2454. rfp->n_launders_hist[i].launder_gp_seq = cur_ops->get_gp_seq();
  2455. spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
  2456. }
  2457. // Give the scheduler a chance, even on nohz_full CPUs.
  2458. static void rcu_torture_fwd_prog_cond_resched(unsigned long iter)
  2459. {
  2460. if (IS_ENABLED(CONFIG_PREEMPTION) && IS_ENABLED(CONFIG_NO_HZ_FULL)) {
  2461. // Real call_rcu() floods hit userspace, so emulate that.
  2462. if (need_resched() || (iter & 0xfff))
  2463. schedule();
  2464. return;
  2465. }
  2466. // No userspace emulation: CB invocation throttles call_rcu()
  2467. cond_resched();
  2468. }
  2469. /*
  2470. * Free all callbacks on the rcu_fwd_cb_head list, either because the
  2471. * test is over or because we hit an OOM event.
  2472. */
  2473. static unsigned long rcu_torture_fwd_prog_cbfree(struct rcu_fwd *rfp)
  2474. {
  2475. unsigned long flags;
  2476. unsigned long freed = 0;
  2477. struct rcu_fwd_cb *rfcp;
  2478. for (;;) {
  2479. spin_lock_irqsave(&rfp->rcu_fwd_lock, flags);
  2480. rfcp = rfp->rcu_fwd_cb_head;
  2481. if (!rfcp) {
  2482. spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
  2483. break;
  2484. }
  2485. rfp->rcu_fwd_cb_head = rfcp->rfc_next;
  2486. if (!rfp->rcu_fwd_cb_head)
  2487. rfp->rcu_fwd_cb_tail = &rfp->rcu_fwd_cb_head;
  2488. spin_unlock_irqrestore(&rfp->rcu_fwd_lock, flags);
  2489. kfree(rfcp);
  2490. freed++;
  2491. rcu_torture_fwd_prog_cond_resched(freed);
  2492. if (tick_nohz_full_enabled()) {
  2493. local_irq_save(flags);
  2494. rcu_momentary_eqs();
  2495. local_irq_restore(flags);
  2496. }
  2497. }
  2498. return freed;
  2499. }
  2500. /* Carry out need_resched()/cond_resched() forward-progress testing. */
  2501. static void rcu_torture_fwd_prog_nr(struct rcu_fwd *rfp,
  2502. int *tested, int *tested_tries)
  2503. {
  2504. unsigned long cver;
  2505. unsigned long dur;
  2506. struct fwd_cb_state fcs;
  2507. unsigned long gps;
  2508. int idx;
  2509. int sd;
  2510. int sd4;
  2511. bool selfpropcb = false;
  2512. unsigned long stopat;
  2513. static DEFINE_TORTURE_RANDOM(trs);
  2514. pr_alert("%s: Starting forward-progress test %d\n", __func__, rfp->rcu_fwd_id);
  2515. if (!cur_ops->sync)
  2516. return; // Cannot do need_resched() forward progress testing without ->sync.
  2517. if (cur_ops->call && cur_ops->cb_barrier) {
  2518. init_rcu_head_on_stack(&fcs.rh);
  2519. selfpropcb = true;
  2520. }
  2521. /* Tight loop containing cond_resched(). */
  2522. atomic_inc(&rcu_fwd_cb_nodelay);
  2523. cur_ops->sync(); /* Later readers see above write. */
  2524. if (selfpropcb) {
  2525. WRITE_ONCE(fcs.stop, 0);
  2526. cur_ops->call(&fcs.rh, rcu_torture_fwd_prog_cb);
  2527. }
  2528. cver = READ_ONCE(rcu_torture_current_version);
  2529. gps = cur_ops->get_gp_seq();
  2530. sd = cur_ops->stall_dur() + 1;
  2531. sd4 = (sd + fwd_progress_div - 1) / fwd_progress_div;
  2532. dur = sd4 + torture_random(&trs) % (sd - sd4);
  2533. WRITE_ONCE(rfp->rcu_fwd_startat, jiffies);
  2534. stopat = rfp->rcu_fwd_startat + dur;
  2535. while (time_before(jiffies, stopat) &&
  2536. !shutdown_time_arrived() &&
  2537. !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
  2538. idx = cur_ops->readlock();
  2539. udelay(10);
  2540. cur_ops->readunlock(idx);
  2541. if (!fwd_progress_need_resched || need_resched())
  2542. cond_resched();
  2543. }
  2544. (*tested_tries)++;
  2545. if (!time_before(jiffies, stopat) &&
  2546. !shutdown_time_arrived() &&
  2547. !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
  2548. (*tested)++;
  2549. cver = READ_ONCE(rcu_torture_current_version) - cver;
  2550. gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
  2551. WARN_ON(!cver && gps < 2);
  2552. pr_alert("%s: %d Duration %ld cver %ld gps %ld\n", __func__,
  2553. rfp->rcu_fwd_id, dur, cver, gps);
  2554. }
  2555. if (selfpropcb) {
  2556. WRITE_ONCE(fcs.stop, 1);
  2557. cur_ops->sync(); /* Wait for running CB to complete. */
  2558. pr_alert("%s: Waiting for CBs: %pS() %d\n", __func__, cur_ops->cb_barrier, rfp->rcu_fwd_id);
  2559. cur_ops->cb_barrier(); /* Wait for queued callbacks. */
  2560. }
  2561. if (selfpropcb) {
  2562. WARN_ON(READ_ONCE(fcs.stop) != 2);
  2563. destroy_rcu_head_on_stack(&fcs.rh);
  2564. }
  2565. schedule_timeout_uninterruptible(HZ / 10); /* Let kthreads recover. */
  2566. atomic_dec(&rcu_fwd_cb_nodelay);
  2567. }
  2568. /* Carry out call_rcu() forward-progress testing. */
  2569. static void rcu_torture_fwd_prog_cr(struct rcu_fwd *rfp)
  2570. {
  2571. unsigned long cver;
  2572. unsigned long flags;
  2573. unsigned long gps;
  2574. int i;
  2575. long n_launders;
  2576. long n_launders_cb_snap;
  2577. long n_launders_sa;
  2578. long n_max_cbs;
  2579. long n_max_gps;
  2580. struct rcu_fwd_cb *rfcp;
  2581. struct rcu_fwd_cb *rfcpn;
  2582. unsigned long stopat;
  2583. unsigned long stoppedat;
  2584. pr_alert("%s: Starting forward-progress test %d\n", __func__, rfp->rcu_fwd_id);
  2585. if (READ_ONCE(rcu_fwd_emergency_stop))
  2586. return; /* Get out of the way quickly, no GP wait! */
  2587. if (!cur_ops->call)
  2588. return; /* Can't do call_rcu() fwd prog without ->call. */
  2589. /* Loop continuously posting RCU callbacks. */
  2590. atomic_inc(&rcu_fwd_cb_nodelay);
  2591. cur_ops->sync(); /* Later readers see above write. */
  2592. WRITE_ONCE(rfp->rcu_fwd_startat, jiffies);
  2593. stopat = rfp->rcu_fwd_startat + MAX_FWD_CB_JIFFIES;
  2594. n_launders = 0;
  2595. rfp->n_launders_cb = 0; // Hoist initialization for multi-kthread
  2596. n_launders_sa = 0;
  2597. n_max_cbs = 0;
  2598. n_max_gps = 0;
  2599. for (i = 0; i < ARRAY_SIZE(rfp->n_launders_hist); i++)
  2600. rfp->n_launders_hist[i].n_launders = 0;
  2601. cver = READ_ONCE(rcu_torture_current_version);
  2602. gps = cur_ops->get_gp_seq();
  2603. rfp->rcu_launder_gp_seq_start = gps;
  2604. tick_dep_set_task(current, TICK_DEP_BIT_RCU);
  2605. while (time_before(jiffies, stopat) &&
  2606. !shutdown_time_arrived() &&
  2607. !READ_ONCE(rcu_fwd_emergency_stop) && !torture_must_stop()) {
  2608. rfcp = READ_ONCE(rfp->rcu_fwd_cb_head);
  2609. rfcpn = NULL;
  2610. if (rfcp)
  2611. rfcpn = READ_ONCE(rfcp->rfc_next);
  2612. if (rfcpn) {
  2613. if (rfcp->rfc_gps >= MIN_FWD_CB_LAUNDERS &&
  2614. ++n_max_gps >= MIN_FWD_CBS_LAUNDERED)
  2615. break;
  2616. rfp->rcu_fwd_cb_head = rfcpn;
  2617. n_launders++;
  2618. n_launders_sa++;
  2619. } else if (!cur_ops->cbflood_max || cur_ops->cbflood_max > n_max_cbs) {
  2620. rfcp = kmalloc(sizeof(*rfcp), GFP_KERNEL);
  2621. if (WARN_ON_ONCE(!rfcp)) {
  2622. schedule_timeout_interruptible(1);
  2623. continue;
  2624. }
  2625. n_max_cbs++;
  2626. n_launders_sa = 0;
  2627. rfcp->rfc_gps = 0;
  2628. rfcp->rfc_rfp = rfp;
  2629. } else {
  2630. rfcp = NULL;
  2631. }
  2632. if (rfcp)
  2633. cur_ops->call(&rfcp->rh, rcu_torture_fwd_cb_cr);
  2634. rcu_torture_fwd_prog_cond_resched(n_launders + n_max_cbs);
  2635. if (tick_nohz_full_enabled()) {
  2636. local_irq_save(flags);
  2637. rcu_momentary_eqs();
  2638. local_irq_restore(flags);
  2639. }
  2640. }
  2641. stoppedat = jiffies;
  2642. n_launders_cb_snap = READ_ONCE(rfp->n_launders_cb);
  2643. cver = READ_ONCE(rcu_torture_current_version) - cver;
  2644. gps = rcutorture_seq_diff(cur_ops->get_gp_seq(), gps);
  2645. pr_alert("%s: Waiting for CBs: %pS() %d\n", __func__, cur_ops->cb_barrier, rfp->rcu_fwd_id);
  2646. cur_ops->cb_barrier(); /* Wait for callbacks to be invoked. */
  2647. (void)rcu_torture_fwd_prog_cbfree(rfp);
  2648. if (!torture_must_stop() && !READ_ONCE(rcu_fwd_emergency_stop) &&
  2649. !shutdown_time_arrived()) {
  2650. if (WARN_ON(n_max_gps < MIN_FWD_CBS_LAUNDERED) && cur_ops->gp_kthread_dbg)
  2651. cur_ops->gp_kthread_dbg();
  2652. pr_alert("%s Duration %lu barrier: %lu pending %ld n_launders: %ld n_launders_sa: %ld n_max_gps: %ld n_max_cbs: %ld cver %ld gps %ld #online %u\n",
  2653. __func__,
  2654. stoppedat - rfp->rcu_fwd_startat, jiffies - stoppedat,
  2655. n_launders + n_max_cbs - n_launders_cb_snap,
  2656. n_launders, n_launders_sa,
  2657. n_max_gps, n_max_cbs, cver, gps, num_online_cpus());
  2658. atomic_long_add(n_max_cbs, &rcu_fwd_max_cbs);
  2659. mutex_lock(&rcu_fwd_mutex); // Serialize histograms.
  2660. rcu_torture_fwd_cb_hist(rfp);
  2661. mutex_unlock(&rcu_fwd_mutex);
  2662. }
  2663. schedule_timeout_uninterruptible(HZ); /* Let CBs drain. */
  2664. tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
  2665. atomic_dec(&rcu_fwd_cb_nodelay);
  2666. }
  2667. /*
  2668. * OOM notifier, but this only prints diagnostic information for the
  2669. * current forward-progress test.
  2670. */
  2671. static int rcutorture_oom_notify(struct notifier_block *self,
  2672. unsigned long notused, void *nfreed)
  2673. {
  2674. int i;
  2675. long ncbs;
  2676. struct rcu_fwd *rfp;
  2677. mutex_lock(&rcu_fwd_mutex);
  2678. rfp = rcu_fwds;
  2679. if (!rfp) {
  2680. mutex_unlock(&rcu_fwd_mutex);
  2681. return NOTIFY_OK;
  2682. }
  2683. WARN(1, "%s invoked upon OOM during forward-progress testing.\n",
  2684. __func__);
  2685. for (i = 0; i < fwd_progress; i++) {
  2686. rcu_torture_fwd_cb_hist(&rfp[i]);
  2687. rcu_fwd_progress_check(1 + (jiffies - READ_ONCE(rfp[i].rcu_fwd_startat)) / 2);
  2688. }
  2689. WRITE_ONCE(rcu_fwd_emergency_stop, true);
  2690. smp_mb(); /* Emergency stop before free and wait to avoid hangs. */
  2691. ncbs = 0;
  2692. for (i = 0; i < fwd_progress; i++)
  2693. ncbs += rcu_torture_fwd_prog_cbfree(&rfp[i]);
  2694. pr_info("%s: Freed %lu RCU callbacks.\n", __func__, ncbs);
  2695. cur_ops->cb_barrier();
  2696. ncbs = 0;
  2697. for (i = 0; i < fwd_progress; i++)
  2698. ncbs += rcu_torture_fwd_prog_cbfree(&rfp[i]);
  2699. pr_info("%s: Freed %lu RCU callbacks.\n", __func__, ncbs);
  2700. cur_ops->cb_barrier();
  2701. ncbs = 0;
  2702. for (i = 0; i < fwd_progress; i++)
  2703. ncbs += rcu_torture_fwd_prog_cbfree(&rfp[i]);
  2704. pr_info("%s: Freed %lu RCU callbacks.\n", __func__, ncbs);
  2705. smp_mb(); /* Frees before return to avoid redoing OOM. */
  2706. (*(unsigned long *)nfreed)++; /* Forward progress CBs freed! */
  2707. pr_info("%s returning after OOM processing.\n", __func__);
  2708. mutex_unlock(&rcu_fwd_mutex);
  2709. return NOTIFY_OK;
  2710. }
  2711. static struct notifier_block rcutorture_oom_nb = {
  2712. .notifier_call = rcutorture_oom_notify
  2713. };
  2714. /* Carry out grace-period forward-progress testing. */
  2715. static int rcu_torture_fwd_prog(void *args)
  2716. {
  2717. bool firsttime = true;
  2718. long max_cbs;
  2719. int oldnice = task_nice(current);
  2720. unsigned long oldseq = READ_ONCE(rcu_fwd_seq);
  2721. struct rcu_fwd *rfp = args;
  2722. int tested = 0;
  2723. int tested_tries = 0;
  2724. VERBOSE_TOROUT_STRING("rcu_torture_fwd_progress task started");
  2725. rcu_bind_current_to_nocb();
  2726. if (!IS_ENABLED(CONFIG_SMP) || !IS_ENABLED(CONFIG_RCU_BOOST))
  2727. set_user_nice(current, MAX_NICE);
  2728. do {
  2729. if (!rfp->rcu_fwd_id) {
  2730. schedule_timeout_interruptible(fwd_progress_holdoff * HZ);
  2731. WRITE_ONCE(rcu_fwd_emergency_stop, false);
  2732. if (!firsttime) {
  2733. max_cbs = atomic_long_xchg(&rcu_fwd_max_cbs, 0);
  2734. pr_alert("%s n_max_cbs: %ld\n", __func__, max_cbs);
  2735. }
  2736. firsttime = false;
  2737. WRITE_ONCE(rcu_fwd_seq, rcu_fwd_seq + 1);
  2738. } else {
  2739. while (READ_ONCE(rcu_fwd_seq) == oldseq && !torture_must_stop())
  2740. schedule_timeout_interruptible(HZ / 20);
  2741. oldseq = READ_ONCE(rcu_fwd_seq);
  2742. }
  2743. pr_alert("%s: Starting forward-progress test %d\n", __func__, rfp->rcu_fwd_id);
  2744. if (rcu_inkernel_boot_has_ended() && torture_num_online_cpus() > rfp->rcu_fwd_id)
  2745. rcu_torture_fwd_prog_cr(rfp);
  2746. if ((cur_ops->stall_dur && cur_ops->stall_dur() > 0) &&
  2747. (!IS_ENABLED(CONFIG_TINY_RCU) ||
  2748. (rcu_inkernel_boot_has_ended() &&
  2749. torture_num_online_cpus() > rfp->rcu_fwd_id)))
  2750. rcu_torture_fwd_prog_nr(rfp, &tested, &tested_tries);
  2751. /* Avoid slow periods, better to test when busy. */
  2752. if (stutter_wait("rcu_torture_fwd_prog"))
  2753. sched_set_normal(current, oldnice);
  2754. } while (!torture_must_stop());
  2755. /* Short runs might not contain a valid forward-progress attempt. */
  2756. if (!rfp->rcu_fwd_id) {
  2757. WARN_ON(!tested && tested_tries >= 5);
  2758. pr_alert("%s: tested %d tested_tries %d\n", __func__, tested, tested_tries);
  2759. }
  2760. torture_kthread_stopping("rcu_torture_fwd_prog");
  2761. return 0;
  2762. }
  2763. /* If forward-progress checking is requested and feasible, spawn the thread. */
  2764. static int __init rcu_torture_fwd_prog_init(void)
  2765. {
  2766. int i;
  2767. int ret = 0;
  2768. struct rcu_fwd *rfp;
  2769. if (!fwd_progress)
  2770. return 0; /* Not requested, so don't do it. */
  2771. if (fwd_progress >= nr_cpu_ids) {
  2772. VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Limiting fwd_progress to # CPUs.\n");
  2773. fwd_progress = nr_cpu_ids;
  2774. } else if (fwd_progress < 0) {
  2775. fwd_progress = nr_cpu_ids;
  2776. }
  2777. if ((!cur_ops->sync && !cur_ops->call) ||
  2778. (!cur_ops->cbflood_max && (!cur_ops->stall_dur || cur_ops->stall_dur() <= 0)) ||
  2779. cur_ops == &rcu_busted_ops) {
  2780. VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, unsupported by RCU flavor under test");
  2781. fwd_progress = 0;
  2782. return 0;
  2783. }
  2784. if (stall_cpu > 0) {
  2785. VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, conflicts with CPU-stall testing");
  2786. fwd_progress = 0;
  2787. if (IS_MODULE(CONFIG_RCU_TORTURE_TEST))
  2788. return -EINVAL; /* In module, can fail back to user. */
  2789. WARN_ON(1); /* Make sure rcutorture notices conflict. */
  2790. return 0;
  2791. }
  2792. if (fwd_progress_holdoff <= 0)
  2793. fwd_progress_holdoff = 1;
  2794. if (fwd_progress_div <= 0)
  2795. fwd_progress_div = 4;
  2796. rfp = kcalloc(fwd_progress, sizeof(*rfp), GFP_KERNEL);
  2797. fwd_prog_tasks = kcalloc(fwd_progress, sizeof(*fwd_prog_tasks), GFP_KERNEL);
  2798. if (!rfp || !fwd_prog_tasks) {
  2799. kfree(rfp);
  2800. kfree(fwd_prog_tasks);
  2801. fwd_prog_tasks = NULL;
  2802. fwd_progress = 0;
  2803. return -ENOMEM;
  2804. }
  2805. for (i = 0; i < fwd_progress; i++) {
  2806. spin_lock_init(&rfp[i].rcu_fwd_lock);
  2807. rfp[i].rcu_fwd_cb_tail = &rfp[i].rcu_fwd_cb_head;
  2808. rfp[i].rcu_fwd_id = i;
  2809. }
  2810. mutex_lock(&rcu_fwd_mutex);
  2811. rcu_fwds = rfp;
  2812. mutex_unlock(&rcu_fwd_mutex);
  2813. register_oom_notifier(&rcutorture_oom_nb);
  2814. for (i = 0; i < fwd_progress; i++) {
  2815. ret = torture_create_kthread(rcu_torture_fwd_prog, &rcu_fwds[i], fwd_prog_tasks[i]);
  2816. if (ret) {
  2817. fwd_progress = i;
  2818. return ret;
  2819. }
  2820. }
  2821. return 0;
  2822. }
  2823. static void rcu_torture_fwd_prog_cleanup(void)
  2824. {
  2825. int i;
  2826. struct rcu_fwd *rfp;
  2827. if (!rcu_fwds || !fwd_prog_tasks)
  2828. return;
  2829. for (i = 0; i < fwd_progress; i++)
  2830. torture_stop_kthread(rcu_torture_fwd_prog, fwd_prog_tasks[i]);
  2831. unregister_oom_notifier(&rcutorture_oom_nb);
  2832. mutex_lock(&rcu_fwd_mutex);
  2833. rfp = rcu_fwds;
  2834. rcu_fwds = NULL;
  2835. mutex_unlock(&rcu_fwd_mutex);
  2836. kfree(rfp);
  2837. kfree(fwd_prog_tasks);
  2838. fwd_prog_tasks = NULL;
  2839. }
  2840. /* Callback function for RCU barrier testing. */
  2841. static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
  2842. {
  2843. atomic_inc(&barrier_cbs_invoked);
  2844. }
  2845. /* IPI handler to get callback posted on desired CPU, if online. */
  2846. static int rcu_torture_barrier1cb(void *rcu_void)
  2847. {
  2848. struct rcu_head *rhp = rcu_void;
  2849. cur_ops->call(rhp, rcu_torture_barrier_cbf);
  2850. return 0;
  2851. }
  2852. /* kthread function to register callbacks used to test RCU barriers. */
  2853. static int rcu_torture_barrier_cbs(void *arg)
  2854. {
  2855. long myid = (long)arg;
  2856. bool lastphase = false;
  2857. bool newphase;
  2858. struct rcu_head rcu;
  2859. init_rcu_head_on_stack(&rcu);
  2860. VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
  2861. set_user_nice(current, MAX_NICE);
  2862. do {
  2863. wait_event(barrier_cbs_wq[myid],
  2864. (newphase =
  2865. smp_load_acquire(&barrier_phase)) != lastphase ||
  2866. torture_must_stop());
  2867. lastphase = newphase;
  2868. if (torture_must_stop())
  2869. break;
  2870. /*
  2871. * The above smp_load_acquire() ensures barrier_phase load
  2872. * is ordered before the following ->call().
  2873. */
  2874. if (smp_call_on_cpu(myid, rcu_torture_barrier1cb, &rcu, 1))
  2875. cur_ops->call(&rcu, rcu_torture_barrier_cbf);
  2876. if (atomic_dec_and_test(&barrier_cbs_count))
  2877. wake_up(&barrier_wq);
  2878. } while (!torture_must_stop());
  2879. if (cur_ops->cb_barrier != NULL)
  2880. cur_ops->cb_barrier();
  2881. destroy_rcu_head_on_stack(&rcu);
  2882. torture_kthread_stopping("rcu_torture_barrier_cbs");
  2883. return 0;
  2884. }
  2885. /* kthread function to drive and coordinate RCU barrier testing. */
  2886. static int rcu_torture_barrier(void *arg)
  2887. {
  2888. int i;
  2889. VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
  2890. do {
  2891. atomic_set(&barrier_cbs_invoked, 0);
  2892. atomic_set(&barrier_cbs_count, n_barrier_cbs);
  2893. /* Ensure barrier_phase ordered after prior assignments. */
  2894. smp_store_release(&barrier_phase, !barrier_phase);
  2895. for (i = 0; i < n_barrier_cbs; i++)
  2896. wake_up(&barrier_cbs_wq[i]);
  2897. wait_event(barrier_wq,
  2898. atomic_read(&barrier_cbs_count) == 0 ||
  2899. torture_must_stop());
  2900. if (torture_must_stop())
  2901. break;
  2902. n_barrier_attempts++;
  2903. cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
  2904. if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
  2905. n_rcu_torture_barrier_error++;
  2906. pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
  2907. atomic_read(&barrier_cbs_invoked),
  2908. n_barrier_cbs);
  2909. WARN_ON(1);
  2910. // Wait manually for the remaining callbacks
  2911. i = 0;
  2912. do {
  2913. if (WARN_ON(i++ > HZ))
  2914. i = INT_MIN;
  2915. schedule_timeout_interruptible(1);
  2916. cur_ops->cb_barrier();
  2917. } while (atomic_read(&barrier_cbs_invoked) !=
  2918. n_barrier_cbs &&
  2919. !torture_must_stop());
  2920. smp_mb(); // Can't trust ordering if broken.
  2921. if (!torture_must_stop())
  2922. pr_err("Recovered: barrier_cbs_invoked = %d\n",
  2923. atomic_read(&barrier_cbs_invoked));
  2924. } else {
  2925. n_barrier_successes++;
  2926. }
  2927. schedule_timeout_interruptible(HZ / 10);
  2928. } while (!torture_must_stop());
  2929. torture_kthread_stopping("rcu_torture_barrier");
  2930. return 0;
  2931. }
  2932. /* Initialize RCU barrier testing. */
  2933. static int rcu_torture_barrier_init(void)
  2934. {
  2935. int i;
  2936. int ret;
  2937. if (n_barrier_cbs <= 0)
  2938. return 0;
  2939. if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
  2940. pr_alert("%s" TORTURE_FLAG
  2941. " Call or barrier ops missing for %s,\n",
  2942. torture_type, cur_ops->name);
  2943. pr_alert("%s" TORTURE_FLAG
  2944. " RCU barrier testing omitted from run.\n",
  2945. torture_type);
  2946. return 0;
  2947. }
  2948. atomic_set(&barrier_cbs_count, 0);
  2949. atomic_set(&barrier_cbs_invoked, 0);
  2950. barrier_cbs_tasks =
  2951. kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
  2952. GFP_KERNEL);
  2953. barrier_cbs_wq =
  2954. kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
  2955. if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
  2956. return -ENOMEM;
  2957. for (i = 0; i < n_barrier_cbs; i++) {
  2958. init_waitqueue_head(&barrier_cbs_wq[i]);
  2959. ret = torture_create_kthread(rcu_torture_barrier_cbs,
  2960. (void *)(long)i,
  2961. barrier_cbs_tasks[i]);
  2962. if (ret)
  2963. return ret;
  2964. }
  2965. return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
  2966. }
  2967. /* Clean up after RCU barrier testing. */
  2968. static void rcu_torture_barrier_cleanup(void)
  2969. {
  2970. int i;
  2971. torture_stop_kthread(rcu_torture_barrier, barrier_task);
  2972. if (barrier_cbs_tasks != NULL) {
  2973. for (i = 0; i < n_barrier_cbs; i++)
  2974. torture_stop_kthread(rcu_torture_barrier_cbs,
  2975. barrier_cbs_tasks[i]);
  2976. kfree(barrier_cbs_tasks);
  2977. barrier_cbs_tasks = NULL;
  2978. }
  2979. if (barrier_cbs_wq != NULL) {
  2980. kfree(barrier_cbs_wq);
  2981. barrier_cbs_wq = NULL;
  2982. }
  2983. }
  2984. static bool rcu_torture_can_boost(void)
  2985. {
  2986. static int boost_warn_once;
  2987. int prio;
  2988. if (!(test_boost == 1 && cur_ops->can_boost) && test_boost != 2)
  2989. return false;
  2990. if (!cur_ops->start_gp_poll || !cur_ops->poll_gp_state)
  2991. return false;
  2992. prio = rcu_get_gp_kthreads_prio();
  2993. if (!prio)
  2994. return false;
  2995. if (prio < 2) {
  2996. if (boost_warn_once == 1)
  2997. return false;
  2998. pr_alert("%s: WARN: RCU kthread priority too low to test boosting. Skipping RCU boost test. Try passing rcutree.kthread_prio > 1 on the kernel command line.\n", KBUILD_MODNAME);
  2999. boost_warn_once = 1;
  3000. return false;
  3001. }
  3002. return true;
  3003. }
  3004. static bool read_exit_child_stop;
  3005. static bool read_exit_child_stopped;
  3006. static wait_queue_head_t read_exit_wq;
  3007. // Child kthread which just does an rcutorture reader and exits.
  3008. static int rcu_torture_read_exit_child(void *trsp_in)
  3009. {
  3010. struct torture_random_state *trsp = trsp_in;
  3011. set_user_nice(current, MAX_NICE);
  3012. // Minimize time between reading and exiting.
  3013. while (!kthread_should_stop())
  3014. schedule_timeout_uninterruptible(HZ / 20);
  3015. (void)rcu_torture_one_read(trsp, -1);
  3016. return 0;
  3017. }
  3018. // Parent kthread which creates and destroys read-exit child kthreads.
  3019. static int rcu_torture_read_exit(void *unused)
  3020. {
  3021. bool errexit = false;
  3022. int i;
  3023. struct task_struct *tsp;
  3024. DEFINE_TORTURE_RANDOM(trs);
  3025. // Allocate and initialize.
  3026. set_user_nice(current, MAX_NICE);
  3027. VERBOSE_TOROUT_STRING("rcu_torture_read_exit: Start of test");
  3028. // Each pass through this loop does one read-exit episode.
  3029. do {
  3030. VERBOSE_TOROUT_STRING("rcu_torture_read_exit: Start of episode");
  3031. for (i = 0; i < read_exit_burst; i++) {
  3032. if (READ_ONCE(read_exit_child_stop))
  3033. break;
  3034. stutter_wait("rcu_torture_read_exit");
  3035. // Spawn child.
  3036. tsp = kthread_run(rcu_torture_read_exit_child,
  3037. &trs, "%s", "rcu_torture_read_exit_child");
  3038. if (IS_ERR(tsp)) {
  3039. TOROUT_ERRSTRING("out of memory");
  3040. errexit = true;
  3041. break;
  3042. }
  3043. cond_resched();
  3044. kthread_stop(tsp);
  3045. n_read_exits++;
  3046. }
  3047. VERBOSE_TOROUT_STRING("rcu_torture_read_exit: End of episode");
  3048. rcu_barrier(); // Wait for task_struct free, avoid OOM.
  3049. i = 0;
  3050. for (; !errexit && !READ_ONCE(read_exit_child_stop) && i < read_exit_delay; i++)
  3051. schedule_timeout_uninterruptible(HZ);
  3052. } while (!errexit && !READ_ONCE(read_exit_child_stop));
  3053. // Clean up and exit.
  3054. smp_store_release(&read_exit_child_stopped, true); // After reaping.
  3055. smp_mb(); // Store before wakeup.
  3056. wake_up(&read_exit_wq);
  3057. while (!torture_must_stop())
  3058. schedule_timeout_uninterruptible(HZ / 20);
  3059. torture_kthread_stopping("rcu_torture_read_exit");
  3060. return 0;
  3061. }
  3062. static int rcu_torture_read_exit_init(void)
  3063. {
  3064. if (read_exit_burst <= 0)
  3065. return 0;
  3066. init_waitqueue_head(&read_exit_wq);
  3067. read_exit_child_stop = false;
  3068. read_exit_child_stopped = false;
  3069. return torture_create_kthread(rcu_torture_read_exit, NULL,
  3070. read_exit_task);
  3071. }
  3072. static void rcu_torture_read_exit_cleanup(void)
  3073. {
  3074. if (!read_exit_task)
  3075. return;
  3076. WRITE_ONCE(read_exit_child_stop, true);
  3077. smp_mb(); // Above write before wait.
  3078. wait_event(read_exit_wq, smp_load_acquire(&read_exit_child_stopped));
  3079. torture_stop_kthread(rcutorture_read_exit, read_exit_task);
  3080. }
  3081. static void rcutorture_test_nmis(int n)
  3082. {
  3083. #if IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)
  3084. int cpu;
  3085. int dumpcpu;
  3086. int i;
  3087. for (i = 0; i < n; i++) {
  3088. preempt_disable();
  3089. cpu = smp_processor_id();
  3090. dumpcpu = cpu + 1;
  3091. if (dumpcpu >= nr_cpu_ids)
  3092. dumpcpu = 0;
  3093. pr_alert("%s: CPU %d invoking dump_cpu_task(%d)\n", __func__, cpu, dumpcpu);
  3094. dump_cpu_task(dumpcpu);
  3095. preempt_enable();
  3096. schedule_timeout_uninterruptible(15 * HZ);
  3097. }
  3098. #else // #if IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)
  3099. WARN_ONCE(n, "Non-zero rcutorture.test_nmis=%d permitted only when rcutorture is built in.\n", test_nmis);
  3100. #endif // #else // #if IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)
  3101. }
  3102. static enum cpuhp_state rcutor_hp;
  3103. static void
  3104. rcu_torture_cleanup(void)
  3105. {
  3106. int firsttime;
  3107. int flags = 0;
  3108. unsigned long gp_seq = 0;
  3109. int i;
  3110. if (torture_cleanup_begin()) {
  3111. if (cur_ops->cb_barrier != NULL) {
  3112. pr_info("%s: Invoking %pS().\n", __func__, cur_ops->cb_barrier);
  3113. cur_ops->cb_barrier();
  3114. }
  3115. if (cur_ops->gp_slow_unregister)
  3116. cur_ops->gp_slow_unregister(NULL);
  3117. return;
  3118. }
  3119. if (!cur_ops) {
  3120. torture_cleanup_end();
  3121. return;
  3122. }
  3123. rcutorture_test_nmis(test_nmis);
  3124. if (cur_ops->gp_kthread_dbg)
  3125. cur_ops->gp_kthread_dbg();
  3126. rcu_torture_read_exit_cleanup();
  3127. rcu_torture_barrier_cleanup();
  3128. rcu_torture_fwd_prog_cleanup();
  3129. torture_stop_kthread(rcu_torture_stall, stall_task);
  3130. torture_stop_kthread(rcu_torture_writer, writer_task);
  3131. if (nocb_tasks) {
  3132. for (i = 0; i < nrealnocbers; i++)
  3133. torture_stop_kthread(rcu_nocb_toggle, nocb_tasks[i]);
  3134. kfree(nocb_tasks);
  3135. nocb_tasks = NULL;
  3136. }
  3137. if (reader_tasks) {
  3138. for (i = 0; i < nrealreaders; i++)
  3139. torture_stop_kthread(rcu_torture_reader,
  3140. reader_tasks[i]);
  3141. kfree(reader_tasks);
  3142. reader_tasks = NULL;
  3143. }
  3144. kfree(rcu_torture_reader_mbchk);
  3145. rcu_torture_reader_mbchk = NULL;
  3146. if (fakewriter_tasks) {
  3147. for (i = 0; i < nfakewriters; i++)
  3148. torture_stop_kthread(rcu_torture_fakewriter,
  3149. fakewriter_tasks[i]);
  3150. kfree(fakewriter_tasks);
  3151. fakewriter_tasks = NULL;
  3152. }
  3153. if (cur_ops->get_gp_data)
  3154. cur_ops->get_gp_data(&flags, &gp_seq);
  3155. pr_alert("%s: End-test grace-period state: g%ld f%#x total-gps=%ld\n",
  3156. cur_ops->name, (long)gp_seq, flags,
  3157. rcutorture_seq_diff(gp_seq, start_gp_seq));
  3158. torture_stop_kthread(rcu_torture_stats, stats_task);
  3159. torture_stop_kthread(rcu_torture_fqs, fqs_task);
  3160. if (rcu_torture_can_boost() && rcutor_hp >= 0)
  3161. cpuhp_remove_state(rcutor_hp);
  3162. /*
  3163. * Wait for all RCU callbacks to fire, then do torture-type-specific
  3164. * cleanup operations.
  3165. */
  3166. if (cur_ops->cb_barrier != NULL) {
  3167. pr_info("%s: Invoking %pS().\n", __func__, cur_ops->cb_barrier);
  3168. cur_ops->cb_barrier();
  3169. }
  3170. if (cur_ops->cleanup != NULL)
  3171. cur_ops->cleanup();
  3172. rcu_torture_mem_dump_obj();
  3173. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  3174. if (err_segs_recorded) {
  3175. pr_alert("Failure/close-call rcutorture reader segments:\n");
  3176. if (rt_read_nsegs == 0)
  3177. pr_alert("\t: No segments recorded!!!\n");
  3178. firsttime = 1;
  3179. for (i = 0; i < rt_read_nsegs; i++) {
  3180. pr_alert("\t%d: %#x ", i, err_segs[i].rt_readstate);
  3181. if (err_segs[i].rt_delay_jiffies != 0) {
  3182. pr_cont("%s%ldjiffies", firsttime ? "" : "+",
  3183. err_segs[i].rt_delay_jiffies);
  3184. firsttime = 0;
  3185. }
  3186. if (err_segs[i].rt_delay_ms != 0) {
  3187. pr_cont("%s%ldms", firsttime ? "" : "+",
  3188. err_segs[i].rt_delay_ms);
  3189. firsttime = 0;
  3190. }
  3191. if (err_segs[i].rt_delay_us != 0) {
  3192. pr_cont("%s%ldus", firsttime ? "" : "+",
  3193. err_segs[i].rt_delay_us);
  3194. firsttime = 0;
  3195. }
  3196. pr_cont("%s\n",
  3197. err_segs[i].rt_preempted ? "preempted" : "");
  3198. }
  3199. }
  3200. if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
  3201. rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
  3202. else if (torture_onoff_failures())
  3203. rcu_torture_print_module_parms(cur_ops,
  3204. "End of test: RCU_HOTPLUG");
  3205. else
  3206. rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
  3207. torture_cleanup_end();
  3208. if (cur_ops->gp_slow_unregister)
  3209. cur_ops->gp_slow_unregister(NULL);
  3210. }
  3211. static void rcu_torture_leak_cb(struct rcu_head *rhp)
  3212. {
  3213. }
  3214. static void rcu_torture_err_cb(struct rcu_head *rhp)
  3215. {
  3216. /*
  3217. * This -might- happen due to race conditions, but is unlikely.
  3218. * The scenario that leads to this happening is that the
  3219. * first of the pair of duplicate callbacks is queued,
  3220. * someone else starts a grace period that includes that
  3221. * callback, then the second of the pair must wait for the
  3222. * next grace period. Unlikely, but can happen. If it
  3223. * does happen, the debug-objects subsystem won't have splatted.
  3224. */
  3225. pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
  3226. }
  3227. /*
  3228. * Verify that double-free causes debug-objects to complain, but only
  3229. * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
  3230. * cannot be carried out.
  3231. */
  3232. static void rcu_test_debug_objects(void)
  3233. {
  3234. struct rcu_head rh1;
  3235. struct rcu_head rh2;
  3236. int idx;
  3237. if (!IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD)) {
  3238. pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_%s()\n",
  3239. KBUILD_MODNAME, cur_ops->name);
  3240. return;
  3241. }
  3242. if (WARN_ON_ONCE(cur_ops->debug_objects &&
  3243. (!cur_ops->call || !cur_ops->cb_barrier)))
  3244. return;
  3245. struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
  3246. init_rcu_head_on_stack(&rh1);
  3247. init_rcu_head_on_stack(&rh2);
  3248. pr_alert("%s: WARN: Duplicate call_%s() test starting.\n", KBUILD_MODNAME, cur_ops->name);
  3249. /* Try to queue the rh2 pair of callbacks for the same grace period. */
  3250. idx = cur_ops->readlock(); /* Make it impossible to finish a grace period. */
  3251. cur_ops->call(&rh1, rcu_torture_leak_cb); /* Start grace period. */
  3252. cur_ops->call(&rh2, rcu_torture_leak_cb);
  3253. cur_ops->call(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
  3254. if (rhp) {
  3255. cur_ops->call(rhp, rcu_torture_leak_cb);
  3256. cur_ops->call(rhp, rcu_torture_err_cb); /* Another duplicate callback. */
  3257. }
  3258. cur_ops->readunlock(idx);
  3259. /* Wait for them all to get done so we can safely return. */
  3260. cur_ops->cb_barrier();
  3261. pr_alert("%s: WARN: Duplicate call_%s() test complete.\n", KBUILD_MODNAME, cur_ops->name);
  3262. destroy_rcu_head_on_stack(&rh1);
  3263. destroy_rcu_head_on_stack(&rh2);
  3264. kfree(rhp);
  3265. }
  3266. static void rcutorture_sync(void)
  3267. {
  3268. static unsigned long n;
  3269. if (cur_ops->sync && !(++n & 0xfff))
  3270. cur_ops->sync();
  3271. }
  3272. static DEFINE_MUTEX(mut0);
  3273. static DEFINE_MUTEX(mut1);
  3274. static DEFINE_MUTEX(mut2);
  3275. static DEFINE_MUTEX(mut3);
  3276. static DEFINE_MUTEX(mut4);
  3277. static DEFINE_MUTEX(mut5);
  3278. static DEFINE_MUTEX(mut6);
  3279. static DEFINE_MUTEX(mut7);
  3280. static DEFINE_MUTEX(mut8);
  3281. static DEFINE_MUTEX(mut9);
  3282. static DECLARE_RWSEM(rwsem0);
  3283. static DECLARE_RWSEM(rwsem1);
  3284. static DECLARE_RWSEM(rwsem2);
  3285. static DECLARE_RWSEM(rwsem3);
  3286. static DECLARE_RWSEM(rwsem4);
  3287. static DECLARE_RWSEM(rwsem5);
  3288. static DECLARE_RWSEM(rwsem6);
  3289. static DECLARE_RWSEM(rwsem7);
  3290. static DECLARE_RWSEM(rwsem8);
  3291. static DECLARE_RWSEM(rwsem9);
  3292. DEFINE_STATIC_SRCU(srcu0);
  3293. DEFINE_STATIC_SRCU(srcu1);
  3294. DEFINE_STATIC_SRCU(srcu2);
  3295. DEFINE_STATIC_SRCU(srcu3);
  3296. DEFINE_STATIC_SRCU(srcu4);
  3297. DEFINE_STATIC_SRCU(srcu5);
  3298. DEFINE_STATIC_SRCU(srcu6);
  3299. DEFINE_STATIC_SRCU(srcu7);
  3300. DEFINE_STATIC_SRCU(srcu8);
  3301. DEFINE_STATIC_SRCU(srcu9);
  3302. static int srcu_lockdep_next(const char *f, const char *fl, const char *fs, const char *fu, int i,
  3303. int cyclelen, int deadlock)
  3304. {
  3305. int j = i + 1;
  3306. if (j >= cyclelen)
  3307. j = deadlock ? 0 : -1;
  3308. if (j >= 0)
  3309. pr_info("%s: %s(%d), %s(%d), %s(%d)\n", f, fl, i, fs, j, fu, i);
  3310. else
  3311. pr_info("%s: %s(%d), %s(%d)\n", f, fl, i, fu, i);
  3312. return j;
  3313. }
  3314. // Test lockdep on SRCU-based deadlock scenarios.
  3315. static void rcu_torture_init_srcu_lockdep(void)
  3316. {
  3317. int cyclelen;
  3318. int deadlock;
  3319. bool err = false;
  3320. int i;
  3321. int j;
  3322. int idx;
  3323. struct mutex *muts[] = { &mut0, &mut1, &mut2, &mut3, &mut4,
  3324. &mut5, &mut6, &mut7, &mut8, &mut9 };
  3325. struct rw_semaphore *rwsems[] = { &rwsem0, &rwsem1, &rwsem2, &rwsem3, &rwsem4,
  3326. &rwsem5, &rwsem6, &rwsem7, &rwsem8, &rwsem9 };
  3327. struct srcu_struct *srcus[] = { &srcu0, &srcu1, &srcu2, &srcu3, &srcu4,
  3328. &srcu5, &srcu6, &srcu7, &srcu8, &srcu9 };
  3329. int testtype;
  3330. if (!test_srcu_lockdep)
  3331. return;
  3332. deadlock = test_srcu_lockdep / 1000;
  3333. testtype = (test_srcu_lockdep / 10) % 100;
  3334. cyclelen = test_srcu_lockdep % 10;
  3335. WARN_ON_ONCE(ARRAY_SIZE(muts) != ARRAY_SIZE(srcus));
  3336. if (WARN_ONCE(deadlock != !!deadlock,
  3337. "%s: test_srcu_lockdep=%d and deadlock digit %d must be zero or one.\n",
  3338. __func__, test_srcu_lockdep, deadlock))
  3339. err = true;
  3340. if (WARN_ONCE(cyclelen <= 0,
  3341. "%s: test_srcu_lockdep=%d and cycle-length digit %d must be greater than zero.\n",
  3342. __func__, test_srcu_lockdep, cyclelen))
  3343. err = true;
  3344. if (err)
  3345. goto err_out;
  3346. if (testtype == 0) {
  3347. pr_info("%s: test_srcu_lockdep = %05d: SRCU %d-way %sdeadlock.\n",
  3348. __func__, test_srcu_lockdep, cyclelen, deadlock ? "" : "non-");
  3349. if (deadlock && cyclelen == 1)
  3350. pr_info("%s: Expect hang.\n", __func__);
  3351. for (i = 0; i < cyclelen; i++) {
  3352. j = srcu_lockdep_next(__func__, "srcu_read_lock", "synchronize_srcu",
  3353. "srcu_read_unlock", i, cyclelen, deadlock);
  3354. idx = srcu_read_lock(srcus[i]);
  3355. if (j >= 0)
  3356. synchronize_srcu(srcus[j]);
  3357. srcu_read_unlock(srcus[i], idx);
  3358. }
  3359. return;
  3360. }
  3361. if (testtype == 1) {
  3362. pr_info("%s: test_srcu_lockdep = %05d: SRCU/mutex %d-way %sdeadlock.\n",
  3363. __func__, test_srcu_lockdep, cyclelen, deadlock ? "" : "non-");
  3364. for (i = 0; i < cyclelen; i++) {
  3365. pr_info("%s: srcu_read_lock(%d), mutex_lock(%d), mutex_unlock(%d), srcu_read_unlock(%d)\n",
  3366. __func__, i, i, i, i);
  3367. idx = srcu_read_lock(srcus[i]);
  3368. mutex_lock(muts[i]);
  3369. mutex_unlock(muts[i]);
  3370. srcu_read_unlock(srcus[i], idx);
  3371. j = srcu_lockdep_next(__func__, "mutex_lock", "synchronize_srcu",
  3372. "mutex_unlock", i, cyclelen, deadlock);
  3373. mutex_lock(muts[i]);
  3374. if (j >= 0)
  3375. synchronize_srcu(srcus[j]);
  3376. mutex_unlock(muts[i]);
  3377. }
  3378. return;
  3379. }
  3380. if (testtype == 2) {
  3381. pr_info("%s: test_srcu_lockdep = %05d: SRCU/rwsem %d-way %sdeadlock.\n",
  3382. __func__, test_srcu_lockdep, cyclelen, deadlock ? "" : "non-");
  3383. for (i = 0; i < cyclelen; i++) {
  3384. pr_info("%s: srcu_read_lock(%d), down_read(%d), up_read(%d), srcu_read_unlock(%d)\n",
  3385. __func__, i, i, i, i);
  3386. idx = srcu_read_lock(srcus[i]);
  3387. down_read(rwsems[i]);
  3388. up_read(rwsems[i]);
  3389. srcu_read_unlock(srcus[i], idx);
  3390. j = srcu_lockdep_next(__func__, "down_write", "synchronize_srcu",
  3391. "up_write", i, cyclelen, deadlock);
  3392. down_write(rwsems[i]);
  3393. if (j >= 0)
  3394. synchronize_srcu(srcus[j]);
  3395. up_write(rwsems[i]);
  3396. }
  3397. return;
  3398. }
  3399. #ifdef CONFIG_TASKS_TRACE_RCU
  3400. if (testtype == 3) {
  3401. pr_info("%s: test_srcu_lockdep = %05d: SRCU and Tasks Trace RCU %d-way %sdeadlock.\n",
  3402. __func__, test_srcu_lockdep, cyclelen, deadlock ? "" : "non-");
  3403. if (deadlock && cyclelen == 1)
  3404. pr_info("%s: Expect hang.\n", __func__);
  3405. for (i = 0; i < cyclelen; i++) {
  3406. char *fl = i == 0 ? "rcu_read_lock_trace" : "srcu_read_lock";
  3407. char *fs = i == cyclelen - 1 ? "synchronize_rcu_tasks_trace"
  3408. : "synchronize_srcu";
  3409. char *fu = i == 0 ? "rcu_read_unlock_trace" : "srcu_read_unlock";
  3410. j = srcu_lockdep_next(__func__, fl, fs, fu, i, cyclelen, deadlock);
  3411. if (i == 0)
  3412. rcu_read_lock_trace();
  3413. else
  3414. idx = srcu_read_lock(srcus[i]);
  3415. if (j >= 0) {
  3416. if (i == cyclelen - 1)
  3417. synchronize_rcu_tasks_trace();
  3418. else
  3419. synchronize_srcu(srcus[j]);
  3420. }
  3421. if (i == 0)
  3422. rcu_read_unlock_trace();
  3423. else
  3424. srcu_read_unlock(srcus[i], idx);
  3425. }
  3426. return;
  3427. }
  3428. #endif // #ifdef CONFIG_TASKS_TRACE_RCU
  3429. err_out:
  3430. pr_info("%s: test_srcu_lockdep = %05d does nothing.\n", __func__, test_srcu_lockdep);
  3431. pr_info("%s: test_srcu_lockdep = DNNL.\n", __func__);
  3432. pr_info("%s: D: Deadlock if nonzero.\n", __func__);
  3433. pr_info("%s: NN: Test number, 0=SRCU, 1=SRCU/mutex, 2=SRCU/rwsem, 3=SRCU/Tasks Trace RCU.\n", __func__);
  3434. pr_info("%s: L: Cycle length.\n", __func__);
  3435. if (!IS_ENABLED(CONFIG_TASKS_TRACE_RCU))
  3436. pr_info("%s: NN=3 disallowed because kernel is built with CONFIG_TASKS_TRACE_RCU=n\n", __func__);
  3437. }
  3438. static int __init
  3439. rcu_torture_init(void)
  3440. {
  3441. long i;
  3442. int cpu;
  3443. int firsterr = 0;
  3444. int flags = 0;
  3445. unsigned long gp_seq = 0;
  3446. static struct rcu_torture_ops *torture_ops[] = {
  3447. &rcu_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops, &busted_srcud_ops,
  3448. TASKS_OPS TASKS_RUDE_OPS TASKS_TRACING_OPS
  3449. &trivial_ops,
  3450. };
  3451. if (!torture_init_begin(torture_type, verbose))
  3452. return -EBUSY;
  3453. /* Process args and tell the world that the torturer is on the job. */
  3454. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  3455. cur_ops = torture_ops[i];
  3456. if (strcmp(torture_type, cur_ops->name) == 0)
  3457. break;
  3458. }
  3459. if (i == ARRAY_SIZE(torture_ops)) {
  3460. pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
  3461. torture_type);
  3462. pr_alert("rcu-torture types:");
  3463. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  3464. pr_cont(" %s", torture_ops[i]->name);
  3465. pr_cont("\n");
  3466. firsterr = -EINVAL;
  3467. cur_ops = NULL;
  3468. goto unwind;
  3469. }
  3470. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  3471. pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
  3472. fqs_duration = 0;
  3473. }
  3474. if (nocbs_nthreads != 0 && (cur_ops != &rcu_ops ||
  3475. !IS_ENABLED(CONFIG_RCU_NOCB_CPU))) {
  3476. pr_alert("rcu-torture types: %s and CONFIG_RCU_NOCB_CPU=%d, nocb toggle disabled.\n",
  3477. cur_ops->name, IS_ENABLED(CONFIG_RCU_NOCB_CPU));
  3478. nocbs_nthreads = 0;
  3479. }
  3480. if (cur_ops->init)
  3481. cur_ops->init();
  3482. rcu_torture_init_srcu_lockdep();
  3483. if (nreaders >= 0) {
  3484. nrealreaders = nreaders;
  3485. } else {
  3486. nrealreaders = num_online_cpus() - 2 - nreaders;
  3487. if (nrealreaders <= 0)
  3488. nrealreaders = 1;
  3489. }
  3490. rcu_torture_print_module_parms(cur_ops, "Start of test");
  3491. if (cur_ops->get_gp_data)
  3492. cur_ops->get_gp_data(&flags, &gp_seq);
  3493. start_gp_seq = gp_seq;
  3494. pr_alert("%s: Start-test grace-period state: g%ld f%#x\n",
  3495. cur_ops->name, (long)gp_seq, flags);
  3496. /* Set up the freelist. */
  3497. INIT_LIST_HEAD(&rcu_torture_freelist);
  3498. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  3499. rcu_tortures[i].rtort_mbtest = 0;
  3500. list_add_tail(&rcu_tortures[i].rtort_free,
  3501. &rcu_torture_freelist);
  3502. }
  3503. /* Initialize the statistics so that each run gets its own numbers. */
  3504. rcu_torture_current = NULL;
  3505. rcu_torture_current_version = 0;
  3506. atomic_set(&n_rcu_torture_alloc, 0);
  3507. atomic_set(&n_rcu_torture_alloc_fail, 0);
  3508. atomic_set(&n_rcu_torture_free, 0);
  3509. atomic_set(&n_rcu_torture_mberror, 0);
  3510. atomic_set(&n_rcu_torture_mbchk_fail, 0);
  3511. atomic_set(&n_rcu_torture_mbchk_tries, 0);
  3512. atomic_set(&n_rcu_torture_error, 0);
  3513. n_rcu_torture_barrier_error = 0;
  3514. n_rcu_torture_boost_ktrerror = 0;
  3515. n_rcu_torture_boost_failure = 0;
  3516. n_rcu_torture_boosts = 0;
  3517. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  3518. atomic_set(&rcu_torture_wcount[i], 0);
  3519. for_each_possible_cpu(cpu) {
  3520. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  3521. per_cpu(rcu_torture_count, cpu)[i] = 0;
  3522. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  3523. }
  3524. }
  3525. err_segs_recorded = 0;
  3526. rt_read_nsegs = 0;
  3527. /* Start up the kthreads. */
  3528. rcu_torture_write_types();
  3529. firsterr = torture_create_kthread(rcu_torture_writer, NULL,
  3530. writer_task);
  3531. if (torture_init_error(firsterr))
  3532. goto unwind;
  3533. if (nfakewriters > 0) {
  3534. fakewriter_tasks = kcalloc(nfakewriters,
  3535. sizeof(fakewriter_tasks[0]),
  3536. GFP_KERNEL);
  3537. if (fakewriter_tasks == NULL) {
  3538. TOROUT_ERRSTRING("out of memory");
  3539. firsterr = -ENOMEM;
  3540. goto unwind;
  3541. }
  3542. }
  3543. for (i = 0; i < nfakewriters; i++) {
  3544. firsterr = torture_create_kthread(rcu_torture_fakewriter,
  3545. NULL, fakewriter_tasks[i]);
  3546. if (torture_init_error(firsterr))
  3547. goto unwind;
  3548. }
  3549. reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
  3550. GFP_KERNEL);
  3551. rcu_torture_reader_mbchk = kcalloc(nrealreaders, sizeof(*rcu_torture_reader_mbchk),
  3552. GFP_KERNEL);
  3553. if (!reader_tasks || !rcu_torture_reader_mbchk) {
  3554. TOROUT_ERRSTRING("out of memory");
  3555. firsterr = -ENOMEM;
  3556. goto unwind;
  3557. }
  3558. for (i = 0; i < nrealreaders; i++) {
  3559. rcu_torture_reader_mbchk[i].rtc_chkrdr = -1;
  3560. firsterr = torture_create_kthread(rcu_torture_reader, (void *)i,
  3561. reader_tasks[i]);
  3562. if (torture_init_error(firsterr))
  3563. goto unwind;
  3564. }
  3565. nrealnocbers = nocbs_nthreads;
  3566. if (WARN_ON(nrealnocbers < 0))
  3567. nrealnocbers = 1;
  3568. if (WARN_ON(nocbs_toggle < 0))
  3569. nocbs_toggle = HZ;
  3570. if (nrealnocbers > 0) {
  3571. nocb_tasks = kcalloc(nrealnocbers, sizeof(nocb_tasks[0]), GFP_KERNEL);
  3572. if (nocb_tasks == NULL) {
  3573. TOROUT_ERRSTRING("out of memory");
  3574. firsterr = -ENOMEM;
  3575. goto unwind;
  3576. }
  3577. } else {
  3578. nocb_tasks = NULL;
  3579. }
  3580. for (i = 0; i < nrealnocbers; i++) {
  3581. firsterr = torture_create_kthread(rcu_nocb_toggle, NULL, nocb_tasks[i]);
  3582. if (torture_init_error(firsterr))
  3583. goto unwind;
  3584. }
  3585. if (stat_interval > 0) {
  3586. firsterr = torture_create_kthread(rcu_torture_stats, NULL,
  3587. stats_task);
  3588. if (torture_init_error(firsterr))
  3589. goto unwind;
  3590. }
  3591. if (test_no_idle_hz && shuffle_interval > 0) {
  3592. firsterr = torture_shuffle_init(shuffle_interval * HZ);
  3593. if (torture_init_error(firsterr))
  3594. goto unwind;
  3595. }
  3596. if (stutter < 0)
  3597. stutter = 0;
  3598. if (stutter) {
  3599. int t;
  3600. t = cur_ops->stall_dur ? cur_ops->stall_dur() : stutter * HZ;
  3601. firsterr = torture_stutter_init(stutter * HZ, t);
  3602. if (torture_init_error(firsterr))
  3603. goto unwind;
  3604. }
  3605. if (fqs_duration < 0)
  3606. fqs_duration = 0;
  3607. if (fqs_holdoff < 0)
  3608. fqs_holdoff = 0;
  3609. if (fqs_duration && fqs_holdoff) {
  3610. /* Create the fqs thread */
  3611. firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
  3612. fqs_task);
  3613. if (torture_init_error(firsterr))
  3614. goto unwind;
  3615. }
  3616. if (test_boost_interval < 1)
  3617. test_boost_interval = 1;
  3618. if (test_boost_duration < 2)
  3619. test_boost_duration = 2;
  3620. if (rcu_torture_can_boost()) {
  3621. boost_starttime = jiffies + test_boost_interval * HZ;
  3622. firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
  3623. rcutorture_booster_init,
  3624. rcutorture_booster_cleanup);
  3625. rcutor_hp = firsterr;
  3626. if (torture_init_error(firsterr))
  3627. goto unwind;
  3628. }
  3629. shutdown_jiffies = jiffies + shutdown_secs * HZ;
  3630. firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
  3631. if (torture_init_error(firsterr))
  3632. goto unwind;
  3633. firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval,
  3634. rcutorture_sync);
  3635. if (torture_init_error(firsterr))
  3636. goto unwind;
  3637. firsterr = rcu_torture_stall_init();
  3638. if (torture_init_error(firsterr))
  3639. goto unwind;
  3640. firsterr = rcu_torture_fwd_prog_init();
  3641. if (torture_init_error(firsterr))
  3642. goto unwind;
  3643. firsterr = rcu_torture_barrier_init();
  3644. if (torture_init_error(firsterr))
  3645. goto unwind;
  3646. firsterr = rcu_torture_read_exit_init();
  3647. if (torture_init_error(firsterr))
  3648. goto unwind;
  3649. if (object_debug)
  3650. rcu_test_debug_objects();
  3651. torture_init_end();
  3652. if (cur_ops->gp_slow_register && !WARN_ON_ONCE(!cur_ops->gp_slow_unregister))
  3653. cur_ops->gp_slow_register(&rcu_fwd_cb_nodelay);
  3654. return 0;
  3655. unwind:
  3656. torture_init_end();
  3657. rcu_torture_cleanup();
  3658. if (shutdown_secs) {
  3659. WARN_ON(!IS_MODULE(CONFIG_RCU_TORTURE_TEST));
  3660. kernel_power_off();
  3661. }
  3662. return firsterr;
  3663. }
  3664. module_init(rcu_torture_init);
  3665. module_exit(rcu_torture_cleanup);