tree.c 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright IBM Corporation, 2008
  19. *
  20. * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  21. * Manfred Spraul <manfred@colorfullife.com>
  22. * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version
  23. *
  24. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  25. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  26. *
  27. * For detailed explanation of Read-Copy Update mechanism see -
  28. * Documentation/RCU
  29. */
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/smp.h>
  35. #include <linux/rcupdate_wait.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/sched.h>
  38. #include <linux/sched/debug.h>
  39. #include <linux/nmi.h>
  40. #include <linux/atomic.h>
  41. #include <linux/bitops.h>
  42. #include <linux/export.h>
  43. #include <linux/completion.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/percpu.h>
  46. #include <linux/notifier.h>
  47. #include <linux/cpu.h>
  48. #include <linux/mutex.h>
  49. #include <linux/time.h>
  50. #include <linux/kernel_stat.h>
  51. #include <linux/wait.h>
  52. #include <linux/kthread.h>
  53. #include <uapi/linux/sched/types.h>
  54. #include <linux/prefetch.h>
  55. #include <linux/delay.h>
  56. #include <linux/stop_machine.h>
  57. #include <linux/random.h>
  58. #include <linux/trace_events.h>
  59. #include <linux/suspend.h>
  60. #include <linux/ftrace.h>
  61. #include "tree.h"
  62. #include "rcu.h"
  63. #ifdef MODULE_PARAM_PREFIX
  64. #undef MODULE_PARAM_PREFIX
  65. #endif
  66. #define MODULE_PARAM_PREFIX "rcutree."
  67. /* Data structures. */
  68. /*
  69. * In order to export the rcu_state name to the tracing tools, it
  70. * needs to be added in the __tracepoint_string section.
  71. * This requires defining a separate variable tp_<sname>_varname
  72. * that points to the string being used, and this will allow
  73. * the tracing userspace tools to be able to decipher the string
  74. * address to the matching string.
  75. */
  76. #ifdef CONFIG_TRACING
  77. # define DEFINE_RCU_TPS(sname) \
  78. static char sname##_varname[] = #sname; \
  79. static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname;
  80. # define RCU_STATE_NAME(sname) sname##_varname
  81. #else
  82. # define DEFINE_RCU_TPS(sname)
  83. # define RCU_STATE_NAME(sname) __stringify(sname)
  84. #endif
  85. #define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
  86. DEFINE_RCU_TPS(sname) \
  87. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, sname##_data); \
  88. struct rcu_state sname##_state = { \
  89. .level = { &sname##_state.node[0] }, \
  90. .rda = &sname##_data, \
  91. .call = cr, \
  92. .gp_state = RCU_GP_IDLE, \
  93. .gpnum = 0UL - 300UL, \
  94. .completed = 0UL - 300UL, \
  95. .orphan_lock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.orphan_lock), \
  96. .orphan_nxttail = &sname##_state.orphan_nxtlist, \
  97. .orphan_donetail = &sname##_state.orphan_donelist, \
  98. .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
  99. .name = RCU_STATE_NAME(sname), \
  100. .abbr = sabbr, \
  101. .exp_mutex = __MUTEX_INITIALIZER(sname##_state.exp_mutex), \
  102. .exp_wake_mutex = __MUTEX_INITIALIZER(sname##_state.exp_wake_mutex), \
  103. }
  104. RCU_STATE_INITIALIZER(rcu_sched, 's', call_rcu_sched);
  105. RCU_STATE_INITIALIZER(rcu_bh, 'b', call_rcu_bh);
  106. static struct rcu_state *const rcu_state_p;
  107. LIST_HEAD(rcu_struct_flavors);
  108. /* Dump rcu_node combining tree at boot to verify correct setup. */
  109. static bool dump_tree;
  110. module_param(dump_tree, bool, 0444);
  111. /* Control rcu_node-tree auto-balancing at boot time. */
  112. static bool rcu_fanout_exact;
  113. module_param(rcu_fanout_exact, bool, 0444);
  114. /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
  115. static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
  116. module_param(rcu_fanout_leaf, int, 0444);
  117. int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
  118. /* Number of rcu_nodes at specified level. */
  119. static int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
  120. int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
  121. /* panic() on RCU Stall sysctl. */
  122. int sysctl_panic_on_rcu_stall __read_mostly;
  123. /*
  124. * The rcu_scheduler_active variable is initialized to the value
  125. * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
  126. * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
  127. * RCU can assume that there is but one task, allowing RCU to (for example)
  128. * optimize synchronize_rcu() to a simple barrier(). When this variable
  129. * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
  130. * to detect real grace periods. This variable is also used to suppress
  131. * boot-time false positives from lockdep-RCU error checking. Finally, it
  132. * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
  133. * is fully initialized, including all of its kthreads having been spawned.
  134. */
  135. int rcu_scheduler_active __read_mostly;
  136. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  137. /*
  138. * The rcu_scheduler_fully_active variable transitions from zero to one
  139. * during the early_initcall() processing, which is after the scheduler
  140. * is capable of creating new tasks. So RCU processing (for example,
  141. * creating tasks for RCU priority boosting) must be delayed until after
  142. * rcu_scheduler_fully_active transitions from zero to one. We also
  143. * currently delay invocation of any RCU callbacks until after this point.
  144. *
  145. * It might later prove better for people registering RCU callbacks during
  146. * early boot to take responsibility for these callbacks, but one step at
  147. * a time.
  148. */
  149. static int rcu_scheduler_fully_active __read_mostly;
  150. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
  151. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
  152. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  153. static void invoke_rcu_core(void);
  154. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
  155. static void rcu_report_exp_rdp(struct rcu_state *rsp,
  156. struct rcu_data *rdp, bool wake);
  157. static void sync_sched_exp_online_cleanup(int cpu);
  158. /* rcuc/rcub kthread realtime priority */
  159. #ifdef CONFIG_RCU_KTHREAD_PRIO
  160. static int kthread_prio = CONFIG_RCU_KTHREAD_PRIO;
  161. #else /* #ifdef CONFIG_RCU_KTHREAD_PRIO */
  162. static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
  163. #endif /* #else #ifdef CONFIG_RCU_KTHREAD_PRIO */
  164. module_param(kthread_prio, int, 0644);
  165. /* Delay in jiffies for grace-period initialization delays, debug only. */
  166. #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT
  167. static int gp_preinit_delay = CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT_DELAY;
  168. module_param(gp_preinit_delay, int, 0644);
  169. #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
  170. static const int gp_preinit_delay;
  171. #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
  172. #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT
  173. static int gp_init_delay = CONFIG_RCU_TORTURE_TEST_SLOW_INIT_DELAY;
  174. module_param(gp_init_delay, int, 0644);
  175. #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
  176. static const int gp_init_delay;
  177. #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
  178. #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP
  179. static int gp_cleanup_delay = CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP_DELAY;
  180. module_param(gp_cleanup_delay, int, 0644);
  181. #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
  182. static const int gp_cleanup_delay;
  183. #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
  184. /*
  185. * Number of grace periods between delays, normalized by the duration of
  186. * the delay. The longer the the delay, the more the grace periods between
  187. * each delay. The reason for this normalization is that it means that,
  188. * for non-zero delays, the overall slowdown of grace periods is constant
  189. * regardless of the duration of the delay. This arrangement balances
  190. * the need for long delays to increase some race probabilities with the
  191. * need for fast grace periods to increase other race probabilities.
  192. */
  193. #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
  194. /*
  195. * Track the rcutorture test sequence number and the update version
  196. * number within a given test. The rcutorture_testseq is incremented
  197. * on every rcutorture module load and unload, so has an odd value
  198. * when a test is running. The rcutorture_vernum is set to zero
  199. * when rcutorture starts and is incremented on each rcutorture update.
  200. * These variables enable correlating rcutorture output with the
  201. * RCU tracing information.
  202. */
  203. unsigned long rcutorture_testseq;
  204. unsigned long rcutorture_vernum;
  205. /*
  206. * Compute the mask of online CPUs for the specified rcu_node structure.
  207. * This will not be stable unless the rcu_node structure's ->lock is
  208. * held, but the bit corresponding to the current CPU will be stable
  209. * in most contexts.
  210. */
  211. unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
  212. {
  213. return READ_ONCE(rnp->qsmaskinitnext);
  214. }
  215. /*
  216. * Return true if an RCU grace period is in progress. The READ_ONCE()s
  217. * permit this function to be invoked without holding the root rcu_node
  218. * structure's ->lock, but of course results can be subject to change.
  219. */
  220. static int rcu_gp_in_progress(struct rcu_state *rsp)
  221. {
  222. return READ_ONCE(rsp->completed) != READ_ONCE(rsp->gpnum);
  223. }
  224. /*
  225. * Note a quiescent state. Because we do not need to know
  226. * how many quiescent states passed, just if there was at least
  227. * one since the start of the grace period, this just sets a flag.
  228. * The caller must have disabled preemption.
  229. */
  230. void rcu_sched_qs(void)
  231. {
  232. if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.s))
  233. return;
  234. trace_rcu_grace_period(TPS("rcu_sched"),
  235. __this_cpu_read(rcu_sched_data.gpnum),
  236. TPS("cpuqs"));
  237. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.norm, false);
  238. if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
  239. return;
  240. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, false);
  241. rcu_report_exp_rdp(&rcu_sched_state,
  242. this_cpu_ptr(&rcu_sched_data), true);
  243. }
  244. void rcu_bh_qs(void)
  245. {
  246. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  247. trace_rcu_grace_period(TPS("rcu_bh"),
  248. __this_cpu_read(rcu_bh_data.gpnum),
  249. TPS("cpuqs"));
  250. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  251. }
  252. }
  253. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  254. static DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  255. .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
  256. .dynticks = ATOMIC_INIT(1),
  257. #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
  258. .dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE,
  259. .dynticks_idle = ATOMIC_INIT(1),
  260. #endif /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  261. };
  262. /*
  263. * There's a few places, currently just in the tracing infrastructure,
  264. * that uses rcu_irq_enter() to make sure RCU is watching. But there's
  265. * a small location where that will not even work. In those cases
  266. * rcu_irq_enter_disabled() needs to be checked to make sure rcu_irq_enter()
  267. * can be called.
  268. */
  269. static DEFINE_PER_CPU(bool, disable_rcu_irq_enter);
  270. bool rcu_irq_enter_disabled(void)
  271. {
  272. return this_cpu_read(disable_rcu_irq_enter);
  273. }
  274. /*
  275. * Record entry into an extended quiescent state. This is only to be
  276. * called when not already in an extended quiescent state.
  277. */
  278. static void rcu_dynticks_eqs_enter(void)
  279. {
  280. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  281. int special;
  282. /*
  283. * CPUs seeing atomic_inc_return() must see prior RCU read-side
  284. * critical sections, and we also must force ordering with the
  285. * next idle sojourn.
  286. */
  287. special = atomic_inc_return(&rdtp->dynticks);
  288. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && special & 0x1);
  289. }
  290. /*
  291. * Record exit from an extended quiescent state. This is only to be
  292. * called from an extended quiescent state.
  293. */
  294. static void rcu_dynticks_eqs_exit(void)
  295. {
  296. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  297. int special;
  298. /*
  299. * CPUs seeing atomic_inc_return() must see prior idle sojourns,
  300. * and we also must force ordering with the next RCU read-side
  301. * critical section.
  302. */
  303. special = atomic_inc_return(&rdtp->dynticks);
  304. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !(special & 0x1));
  305. }
  306. /*
  307. * Reset the current CPU's ->dynticks counter to indicate that the
  308. * newly onlined CPU is no longer in an extended quiescent state.
  309. * This will either leave the counter unchanged, or increment it
  310. * to the next non-quiescent value.
  311. *
  312. * The non-atomic test/increment sequence works because the upper bits
  313. * of the ->dynticks counter are manipulated only by the corresponding CPU,
  314. * or when the corresponding CPU is offline.
  315. */
  316. static void rcu_dynticks_eqs_online(void)
  317. {
  318. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  319. if (atomic_read(&rdtp->dynticks) & 0x1)
  320. return;
  321. atomic_add(0x1, &rdtp->dynticks);
  322. }
  323. /*
  324. * Is the current CPU in an extended quiescent state?
  325. *
  326. * No ordering, as we are sampling CPU-local information.
  327. */
  328. bool rcu_dynticks_curr_cpu_in_eqs(void)
  329. {
  330. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  331. return !(atomic_read(&rdtp->dynticks) & 0x1);
  332. }
  333. /*
  334. * Snapshot the ->dynticks counter with full ordering so as to allow
  335. * stable comparison of this counter with past and future snapshots.
  336. */
  337. int rcu_dynticks_snap(struct rcu_dynticks *rdtp)
  338. {
  339. int snap = atomic_add_return(0, &rdtp->dynticks);
  340. return snap;
  341. }
  342. /*
  343. * Return true if the snapshot returned from rcu_dynticks_snap()
  344. * indicates that RCU is in an extended quiescent state.
  345. */
  346. static bool rcu_dynticks_in_eqs(int snap)
  347. {
  348. return !(snap & 0x1);
  349. }
  350. /*
  351. * Return true if the CPU corresponding to the specified rcu_dynticks
  352. * structure has spent some time in an extended quiescent state since
  353. * rcu_dynticks_snap() returned the specified snapshot.
  354. */
  355. static bool rcu_dynticks_in_eqs_since(struct rcu_dynticks *rdtp, int snap)
  356. {
  357. return snap != rcu_dynticks_snap(rdtp);
  358. }
  359. /*
  360. * Do a double-increment of the ->dynticks counter to emulate a
  361. * momentary idle-CPU quiescent state.
  362. */
  363. static void rcu_dynticks_momentary_idle(void)
  364. {
  365. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  366. int special = atomic_add_return(2, &rdtp->dynticks);
  367. /* It is illegal to call this from idle state. */
  368. WARN_ON_ONCE(!(special & 0x1));
  369. }
  370. DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, rcu_qs_ctr);
  371. EXPORT_PER_CPU_SYMBOL_GPL(rcu_qs_ctr);
  372. /*
  373. * Let the RCU core know that this CPU has gone through the scheduler,
  374. * which is a quiescent state. This is called when the need for a
  375. * quiescent state is urgent, so we burn an atomic operation and full
  376. * memory barriers to let the RCU core know about it, regardless of what
  377. * this CPU might (or might not) do in the near future.
  378. *
  379. * We inform the RCU core by emulating a zero-duration dyntick-idle
  380. * period, which we in turn do by incrementing the ->dynticks counter
  381. * by two.
  382. *
  383. * The caller must have disabled interrupts.
  384. */
  385. static void rcu_momentary_dyntick_idle(void)
  386. {
  387. struct rcu_data *rdp;
  388. int resched_mask;
  389. struct rcu_state *rsp;
  390. /*
  391. * Yes, we can lose flag-setting operations. This is OK, because
  392. * the flag will be set again after some delay.
  393. */
  394. resched_mask = raw_cpu_read(rcu_sched_qs_mask);
  395. raw_cpu_write(rcu_sched_qs_mask, 0);
  396. /* Find the flavor that needs a quiescent state. */
  397. for_each_rcu_flavor(rsp) {
  398. rdp = raw_cpu_ptr(rsp->rda);
  399. if (!(resched_mask & rsp->flavor_mask))
  400. continue;
  401. smp_mb(); /* rcu_sched_qs_mask before cond_resched_completed. */
  402. if (READ_ONCE(rdp->mynode->completed) !=
  403. READ_ONCE(rdp->cond_resched_completed))
  404. continue;
  405. /*
  406. * Pretend to be momentarily idle for the quiescent state.
  407. * This allows the grace-period kthread to record the
  408. * quiescent state, with no need for this CPU to do anything
  409. * further.
  410. */
  411. rcu_dynticks_momentary_idle();
  412. break;
  413. }
  414. }
  415. /*
  416. * Note a context switch. This is a quiescent state for RCU-sched,
  417. * and requires special handling for preemptible RCU.
  418. * The caller must have disabled interrupts.
  419. */
  420. void rcu_note_context_switch(void)
  421. {
  422. barrier(); /* Avoid RCU read-side critical sections leaking down. */
  423. trace_rcu_utilization(TPS("Start context switch"));
  424. rcu_sched_qs();
  425. rcu_preempt_note_context_switch();
  426. if (unlikely(raw_cpu_read(rcu_sched_qs_mask)))
  427. rcu_momentary_dyntick_idle();
  428. trace_rcu_utilization(TPS("End context switch"));
  429. barrier(); /* Avoid RCU read-side critical sections leaking up. */
  430. }
  431. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  432. /*
  433. * Register a quiescent state for all RCU flavors. If there is an
  434. * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
  435. * dyntick-idle quiescent state visible to other CPUs (but only for those
  436. * RCU flavors in desperate need of a quiescent state, which will normally
  437. * be none of them). Either way, do a lightweight quiescent state for
  438. * all RCU flavors.
  439. *
  440. * The barrier() calls are redundant in the common case when this is
  441. * called externally, but just in case this is called from within this
  442. * file.
  443. *
  444. */
  445. void rcu_all_qs(void)
  446. {
  447. unsigned long flags;
  448. barrier(); /* Avoid RCU read-side critical sections leaking down. */
  449. if (unlikely(raw_cpu_read(rcu_sched_qs_mask))) {
  450. local_irq_save(flags);
  451. rcu_momentary_dyntick_idle();
  452. local_irq_restore(flags);
  453. }
  454. if (unlikely(raw_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))) {
  455. /*
  456. * Yes, we just checked a per-CPU variable with preemption
  457. * enabled, so we might be migrated to some other CPU at
  458. * this point. That is OK because in that case, the
  459. * migration will supply the needed quiescent state.
  460. * We might end up needlessly disabling preemption and
  461. * invoking rcu_sched_qs() on the destination CPU, but
  462. * the probability and cost are both quite low, so this
  463. * should not be a problem in practice.
  464. */
  465. preempt_disable();
  466. rcu_sched_qs();
  467. preempt_enable();
  468. }
  469. this_cpu_inc(rcu_qs_ctr);
  470. barrier(); /* Avoid RCU read-side critical sections leaking up. */
  471. }
  472. EXPORT_SYMBOL_GPL(rcu_all_qs);
  473. static long blimit = 10; /* Maximum callbacks per rcu_do_batch. */
  474. static long qhimark = 10000; /* If this many pending, ignore blimit. */
  475. static long qlowmark = 100; /* Once only this many pending, use blimit. */
  476. module_param(blimit, long, 0444);
  477. module_param(qhimark, long, 0444);
  478. module_param(qlowmark, long, 0444);
  479. static ulong jiffies_till_first_fqs = ULONG_MAX;
  480. static ulong jiffies_till_next_fqs = ULONG_MAX;
  481. static bool rcu_kick_kthreads;
  482. module_param(jiffies_till_first_fqs, ulong, 0644);
  483. module_param(jiffies_till_next_fqs, ulong, 0644);
  484. module_param(rcu_kick_kthreads, bool, 0644);
  485. /*
  486. * How long the grace period must be before we start recruiting
  487. * quiescent-state help from rcu_note_context_switch().
  488. */
  489. static ulong jiffies_till_sched_qs = HZ / 20;
  490. module_param(jiffies_till_sched_qs, ulong, 0644);
  491. static bool rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  492. struct rcu_data *rdp);
  493. static void force_qs_rnp(struct rcu_state *rsp,
  494. int (*f)(struct rcu_data *rsp, bool *isidle,
  495. unsigned long *maxj),
  496. bool *isidle, unsigned long *maxj);
  497. static void force_quiescent_state(struct rcu_state *rsp);
  498. static int rcu_pending(void);
  499. /*
  500. * Return the number of RCU batches started thus far for debug & stats.
  501. */
  502. unsigned long rcu_batches_started(void)
  503. {
  504. return rcu_state_p->gpnum;
  505. }
  506. EXPORT_SYMBOL_GPL(rcu_batches_started);
  507. /*
  508. * Return the number of RCU-sched batches started thus far for debug & stats.
  509. */
  510. unsigned long rcu_batches_started_sched(void)
  511. {
  512. return rcu_sched_state.gpnum;
  513. }
  514. EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
  515. /*
  516. * Return the number of RCU BH batches started thus far for debug & stats.
  517. */
  518. unsigned long rcu_batches_started_bh(void)
  519. {
  520. return rcu_bh_state.gpnum;
  521. }
  522. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  523. /*
  524. * Return the number of RCU batches completed thus far for debug & stats.
  525. */
  526. unsigned long rcu_batches_completed(void)
  527. {
  528. return rcu_state_p->completed;
  529. }
  530. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  531. /*
  532. * Return the number of RCU-sched batches completed thus far for debug & stats.
  533. */
  534. unsigned long rcu_batches_completed_sched(void)
  535. {
  536. return rcu_sched_state.completed;
  537. }
  538. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  539. /*
  540. * Return the number of RCU BH batches completed thus far for debug & stats.
  541. */
  542. unsigned long rcu_batches_completed_bh(void)
  543. {
  544. return rcu_bh_state.completed;
  545. }
  546. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  547. /*
  548. * Return the number of RCU expedited batches completed thus far for
  549. * debug & stats. Odd numbers mean that a batch is in progress, even
  550. * numbers mean idle. The value returned will thus be roughly double
  551. * the cumulative batches since boot.
  552. */
  553. unsigned long rcu_exp_batches_completed(void)
  554. {
  555. return rcu_state_p->expedited_sequence;
  556. }
  557. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
  558. /*
  559. * Return the number of RCU-sched expedited batches completed thus far
  560. * for debug & stats. Similar to rcu_exp_batches_completed().
  561. */
  562. unsigned long rcu_exp_batches_completed_sched(void)
  563. {
  564. return rcu_sched_state.expedited_sequence;
  565. }
  566. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed_sched);
  567. /*
  568. * Force a quiescent state.
  569. */
  570. void rcu_force_quiescent_state(void)
  571. {
  572. force_quiescent_state(rcu_state_p);
  573. }
  574. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  575. /*
  576. * Force a quiescent state for RCU BH.
  577. */
  578. void rcu_bh_force_quiescent_state(void)
  579. {
  580. force_quiescent_state(&rcu_bh_state);
  581. }
  582. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  583. /*
  584. * Force a quiescent state for RCU-sched.
  585. */
  586. void rcu_sched_force_quiescent_state(void)
  587. {
  588. force_quiescent_state(&rcu_sched_state);
  589. }
  590. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  591. /*
  592. * Show the state of the grace-period kthreads.
  593. */
  594. void show_rcu_gp_kthreads(void)
  595. {
  596. struct rcu_state *rsp;
  597. for_each_rcu_flavor(rsp) {
  598. pr_info("%s: wait state: %d ->state: %#lx\n",
  599. rsp->name, rsp->gp_state, rsp->gp_kthread->state);
  600. /* sched_show_task(rsp->gp_kthread); */
  601. }
  602. }
  603. EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
  604. /*
  605. * Record the number of times rcutorture tests have been initiated and
  606. * terminated. This information allows the debugfs tracing stats to be
  607. * correlated to the rcutorture messages, even when the rcutorture module
  608. * is being repeatedly loaded and unloaded. In other words, we cannot
  609. * store this state in rcutorture itself.
  610. */
  611. void rcutorture_record_test_transition(void)
  612. {
  613. rcutorture_testseq++;
  614. rcutorture_vernum = 0;
  615. }
  616. EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
  617. /*
  618. * Send along grace-period-related data for rcutorture diagnostics.
  619. */
  620. void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  621. unsigned long *gpnum, unsigned long *completed)
  622. {
  623. struct rcu_state *rsp = NULL;
  624. switch (test_type) {
  625. case RCU_FLAVOR:
  626. rsp = rcu_state_p;
  627. break;
  628. case RCU_BH_FLAVOR:
  629. rsp = &rcu_bh_state;
  630. break;
  631. case RCU_SCHED_FLAVOR:
  632. rsp = &rcu_sched_state;
  633. break;
  634. default:
  635. break;
  636. }
  637. if (rsp != NULL) {
  638. *flags = READ_ONCE(rsp->gp_flags);
  639. *gpnum = READ_ONCE(rsp->gpnum);
  640. *completed = READ_ONCE(rsp->completed);
  641. return;
  642. }
  643. *flags = 0;
  644. *gpnum = 0;
  645. *completed = 0;
  646. }
  647. EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
  648. /*
  649. * Record the number of writer passes through the current rcutorture test.
  650. * This is also used to correlate debugfs tracing stats with the rcutorture
  651. * messages.
  652. */
  653. void rcutorture_record_progress(unsigned long vernum)
  654. {
  655. rcutorture_vernum++;
  656. }
  657. EXPORT_SYMBOL_GPL(rcutorture_record_progress);
  658. /*
  659. * Does the CPU have callbacks ready to be invoked?
  660. */
  661. static int
  662. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  663. {
  664. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL] &&
  665. rdp->nxttail[RCU_NEXT_TAIL] != NULL;
  666. }
  667. /*
  668. * Return the root node of the specified rcu_state structure.
  669. */
  670. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  671. {
  672. return &rsp->node[0];
  673. }
  674. /*
  675. * Is there any need for future grace periods?
  676. * Interrupts must be disabled. If the caller does not hold the root
  677. * rnp_node structure's ->lock, the results are advisory only.
  678. */
  679. static int rcu_future_needs_gp(struct rcu_state *rsp)
  680. {
  681. struct rcu_node *rnp = rcu_get_root(rsp);
  682. int idx = (READ_ONCE(rnp->completed) + 1) & 0x1;
  683. int *fp = &rnp->need_future_gp[idx];
  684. return READ_ONCE(*fp);
  685. }
  686. /*
  687. * Does the current CPU require a not-yet-started grace period?
  688. * The caller must have disabled interrupts to prevent races with
  689. * normal callback registry.
  690. */
  691. static bool
  692. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  693. {
  694. int i;
  695. if (rcu_gp_in_progress(rsp))
  696. return false; /* No, a grace period is already in progress. */
  697. if (rcu_future_needs_gp(rsp))
  698. return true; /* Yes, a no-CBs CPU needs one. */
  699. if (!rdp->nxttail[RCU_NEXT_TAIL])
  700. return false; /* No, this is a no-CBs (or offline) CPU. */
  701. if (*rdp->nxttail[RCU_NEXT_READY_TAIL])
  702. return true; /* Yes, CPU has newly registered callbacks. */
  703. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
  704. if (rdp->nxttail[i - 1] != rdp->nxttail[i] &&
  705. ULONG_CMP_LT(READ_ONCE(rsp->completed),
  706. rdp->nxtcompleted[i]))
  707. return true; /* Yes, CBs for future grace period. */
  708. return false; /* No grace period needed. */
  709. }
  710. /*
  711. * rcu_eqs_enter_common - current CPU is entering an extended quiescent state
  712. *
  713. * Enter idle, doing appropriate accounting. The caller must have
  714. * disabled interrupts.
  715. */
  716. static void rcu_eqs_enter_common(bool user)
  717. {
  718. struct rcu_state *rsp;
  719. struct rcu_data *rdp;
  720. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  721. trace_rcu_dyntick(TPS("Start"), rdtp->dynticks_nesting, 0);
  722. if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  723. !user && !is_idle_task(current)) {
  724. struct task_struct *idle __maybe_unused =
  725. idle_task(smp_processor_id());
  726. trace_rcu_dyntick(TPS("Error on entry: not idle task"), rdtp->dynticks_nesting, 0);
  727. rcu_ftrace_dump(DUMP_ORIG);
  728. WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
  729. current->pid, current->comm,
  730. idle->pid, idle->comm); /* must be idle task! */
  731. }
  732. for_each_rcu_flavor(rsp) {
  733. rdp = this_cpu_ptr(rsp->rda);
  734. do_nocb_deferred_wakeup(rdp);
  735. }
  736. rcu_prepare_for_idle();
  737. __this_cpu_inc(disable_rcu_irq_enter);
  738. rdtp->dynticks_nesting = 0; /* Breaks tracing momentarily. */
  739. rcu_dynticks_eqs_enter(); /* After this, tracing works again. */
  740. __this_cpu_dec(disable_rcu_irq_enter);
  741. rcu_dynticks_task_enter();
  742. /*
  743. * It is illegal to enter an extended quiescent state while
  744. * in an RCU read-side critical section.
  745. */
  746. RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
  747. "Illegal idle entry in RCU read-side critical section.");
  748. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),
  749. "Illegal idle entry in RCU-bh read-side critical section.");
  750. RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),
  751. "Illegal idle entry in RCU-sched read-side critical section.");
  752. }
  753. /*
  754. * Enter an RCU extended quiescent state, which can be either the
  755. * idle loop or adaptive-tickless usermode execution.
  756. */
  757. static void rcu_eqs_enter(bool user)
  758. {
  759. struct rcu_dynticks *rdtp;
  760. rdtp = this_cpu_ptr(&rcu_dynticks);
  761. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  762. (rdtp->dynticks_nesting & DYNTICK_TASK_NEST_MASK) == 0);
  763. if ((rdtp->dynticks_nesting & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE)
  764. rcu_eqs_enter_common(user);
  765. else
  766. rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
  767. }
  768. /**
  769. * rcu_idle_enter - inform RCU that current CPU is entering idle
  770. *
  771. * Enter idle mode, in other words, -leave- the mode in which RCU
  772. * read-side critical sections can occur. (Though RCU read-side
  773. * critical sections can occur in irq handlers in idle, a possibility
  774. * handled by irq_enter() and irq_exit().)
  775. *
  776. * We crowbar the ->dynticks_nesting field to zero to allow for
  777. * the possibility of usermode upcalls having messed up our count
  778. * of interrupt nesting level during the prior busy period.
  779. */
  780. void rcu_idle_enter(void)
  781. {
  782. unsigned long flags;
  783. local_irq_save(flags);
  784. rcu_eqs_enter(false);
  785. rcu_sysidle_enter(0);
  786. local_irq_restore(flags);
  787. }
  788. EXPORT_SYMBOL_GPL(rcu_idle_enter);
  789. #ifdef CONFIG_NO_HZ_FULL
  790. /**
  791. * rcu_user_enter - inform RCU that we are resuming userspace.
  792. *
  793. * Enter RCU idle mode right before resuming userspace. No use of RCU
  794. * is permitted between this call and rcu_user_exit(). This way the
  795. * CPU doesn't need to maintain the tick for RCU maintenance purposes
  796. * when the CPU runs in userspace.
  797. */
  798. void rcu_user_enter(void)
  799. {
  800. rcu_eqs_enter(1);
  801. }
  802. #endif /* CONFIG_NO_HZ_FULL */
  803. /**
  804. * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
  805. *
  806. * Exit from an interrupt handler, which might possibly result in entering
  807. * idle mode, in other words, leaving the mode in which read-side critical
  808. * sections can occur. The caller must have disabled interrupts.
  809. *
  810. * This code assumes that the idle loop never does anything that might
  811. * result in unbalanced calls to irq_enter() and irq_exit(). If your
  812. * architecture violates this assumption, RCU will give you what you
  813. * deserve, good and hard. But very infrequently and irreproducibly.
  814. *
  815. * Use things like work queues to work around this limitation.
  816. *
  817. * You have been warned.
  818. */
  819. void rcu_irq_exit(void)
  820. {
  821. struct rcu_dynticks *rdtp;
  822. RCU_LOCKDEP_WARN(!irqs_disabled(), "rcu_irq_exit() invoked with irqs enabled!!!");
  823. rdtp = this_cpu_ptr(&rcu_dynticks);
  824. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  825. rdtp->dynticks_nesting < 1);
  826. if (rdtp->dynticks_nesting <= 1) {
  827. rcu_eqs_enter_common(true);
  828. } else {
  829. trace_rcu_dyntick(TPS("--="), rdtp->dynticks_nesting, rdtp->dynticks_nesting - 1);
  830. rdtp->dynticks_nesting--;
  831. }
  832. rcu_sysidle_enter(1);
  833. }
  834. /*
  835. * Wrapper for rcu_irq_exit() where interrupts are enabled.
  836. */
  837. void rcu_irq_exit_irqson(void)
  838. {
  839. unsigned long flags;
  840. local_irq_save(flags);
  841. rcu_irq_exit();
  842. local_irq_restore(flags);
  843. }
  844. /*
  845. * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
  846. *
  847. * If the new value of the ->dynticks_nesting counter was previously zero,
  848. * we really have exited idle, and must do the appropriate accounting.
  849. * The caller must have disabled interrupts.
  850. */
  851. static void rcu_eqs_exit_common(long long oldval, int user)
  852. {
  853. RCU_TRACE(struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);)
  854. rcu_dynticks_task_exit();
  855. rcu_dynticks_eqs_exit();
  856. rcu_cleanup_after_idle();
  857. trace_rcu_dyntick(TPS("End"), oldval, rdtp->dynticks_nesting);
  858. if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  859. !user && !is_idle_task(current)) {
  860. struct task_struct *idle __maybe_unused =
  861. idle_task(smp_processor_id());
  862. trace_rcu_dyntick(TPS("Error on exit: not idle task"),
  863. oldval, rdtp->dynticks_nesting);
  864. rcu_ftrace_dump(DUMP_ORIG);
  865. WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
  866. current->pid, current->comm,
  867. idle->pid, idle->comm); /* must be idle task! */
  868. }
  869. }
  870. /*
  871. * Exit an RCU extended quiescent state, which can be either the
  872. * idle loop or adaptive-tickless usermode execution.
  873. */
  874. static void rcu_eqs_exit(bool user)
  875. {
  876. struct rcu_dynticks *rdtp;
  877. long long oldval;
  878. rdtp = this_cpu_ptr(&rcu_dynticks);
  879. oldval = rdtp->dynticks_nesting;
  880. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
  881. if (oldval & DYNTICK_TASK_NEST_MASK) {
  882. rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
  883. } else {
  884. rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
  885. rcu_eqs_exit_common(oldval, user);
  886. }
  887. }
  888. /**
  889. * rcu_idle_exit - inform RCU that current CPU is leaving idle
  890. *
  891. * Exit idle mode, in other words, -enter- the mode in which RCU
  892. * read-side critical sections can occur.
  893. *
  894. * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
  895. * allow for the possibility of usermode upcalls messing up our count
  896. * of interrupt nesting level during the busy period that is just
  897. * now starting.
  898. */
  899. void rcu_idle_exit(void)
  900. {
  901. unsigned long flags;
  902. local_irq_save(flags);
  903. rcu_eqs_exit(false);
  904. rcu_sysidle_exit(0);
  905. local_irq_restore(flags);
  906. }
  907. EXPORT_SYMBOL_GPL(rcu_idle_exit);
  908. #ifdef CONFIG_NO_HZ_FULL
  909. /**
  910. * rcu_user_exit - inform RCU that we are exiting userspace.
  911. *
  912. * Exit RCU idle mode while entering the kernel because it can
  913. * run a RCU read side critical section anytime.
  914. */
  915. void rcu_user_exit(void)
  916. {
  917. rcu_eqs_exit(1);
  918. }
  919. #endif /* CONFIG_NO_HZ_FULL */
  920. /**
  921. * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
  922. *
  923. * Enter an interrupt handler, which might possibly result in exiting
  924. * idle mode, in other words, entering the mode in which read-side critical
  925. * sections can occur. The caller must have disabled interrupts.
  926. *
  927. * Note that the Linux kernel is fully capable of entering an interrupt
  928. * handler that it never exits, for example when doing upcalls to
  929. * user mode! This code assumes that the idle loop never does upcalls to
  930. * user mode. If your architecture does do upcalls from the idle loop (or
  931. * does anything else that results in unbalanced calls to the irq_enter()
  932. * and irq_exit() functions), RCU will give you what you deserve, good
  933. * and hard. But very infrequently and irreproducibly.
  934. *
  935. * Use things like work queues to work around this limitation.
  936. *
  937. * You have been warned.
  938. */
  939. void rcu_irq_enter(void)
  940. {
  941. struct rcu_dynticks *rdtp;
  942. long long oldval;
  943. RCU_LOCKDEP_WARN(!irqs_disabled(), "rcu_irq_enter() invoked with irqs enabled!!!");
  944. rdtp = this_cpu_ptr(&rcu_dynticks);
  945. oldval = rdtp->dynticks_nesting;
  946. rdtp->dynticks_nesting++;
  947. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  948. rdtp->dynticks_nesting == 0);
  949. if (oldval)
  950. trace_rcu_dyntick(TPS("++="), oldval, rdtp->dynticks_nesting);
  951. else
  952. rcu_eqs_exit_common(oldval, true);
  953. rcu_sysidle_exit(1);
  954. }
  955. /*
  956. * Wrapper for rcu_irq_enter() where interrupts are enabled.
  957. */
  958. void rcu_irq_enter_irqson(void)
  959. {
  960. unsigned long flags;
  961. local_irq_save(flags);
  962. rcu_irq_enter();
  963. local_irq_restore(flags);
  964. }
  965. /**
  966. * rcu_nmi_enter - inform RCU of entry to NMI context
  967. *
  968. * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
  969. * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
  970. * that the CPU is active. This implementation permits nested NMIs, as
  971. * long as the nesting level does not overflow an int. (You will probably
  972. * run out of stack space first.)
  973. */
  974. void rcu_nmi_enter(void)
  975. {
  976. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  977. int incby = 2;
  978. /* Complain about underflow. */
  979. WARN_ON_ONCE(rdtp->dynticks_nmi_nesting < 0);
  980. /*
  981. * If idle from RCU viewpoint, atomically increment ->dynticks
  982. * to mark non-idle and increment ->dynticks_nmi_nesting by one.
  983. * Otherwise, increment ->dynticks_nmi_nesting by two. This means
  984. * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
  985. * to be in the outermost NMI handler that interrupted an RCU-idle
  986. * period (observation due to Andy Lutomirski).
  987. */
  988. if (rcu_dynticks_curr_cpu_in_eqs()) {
  989. rcu_dynticks_eqs_exit();
  990. incby = 1;
  991. }
  992. rdtp->dynticks_nmi_nesting += incby;
  993. barrier();
  994. }
  995. /**
  996. * rcu_nmi_exit - inform RCU of exit from NMI context
  997. *
  998. * If we are returning from the outermost NMI handler that interrupted an
  999. * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
  1000. * to let the RCU grace-period handling know that the CPU is back to
  1001. * being RCU-idle.
  1002. */
  1003. void rcu_nmi_exit(void)
  1004. {
  1005. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1006. /*
  1007. * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
  1008. * (We are exiting an NMI handler, so RCU better be paying attention
  1009. * to us!)
  1010. */
  1011. WARN_ON_ONCE(rdtp->dynticks_nmi_nesting <= 0);
  1012. WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
  1013. /*
  1014. * If the nesting level is not 1, the CPU wasn't RCU-idle, so
  1015. * leave it in non-RCU-idle state.
  1016. */
  1017. if (rdtp->dynticks_nmi_nesting != 1) {
  1018. rdtp->dynticks_nmi_nesting -= 2;
  1019. return;
  1020. }
  1021. /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
  1022. rdtp->dynticks_nmi_nesting = 0;
  1023. rcu_dynticks_eqs_enter();
  1024. }
  1025. /**
  1026. * __rcu_is_watching - are RCU read-side critical sections safe?
  1027. *
  1028. * Return true if RCU is watching the running CPU, which means that
  1029. * this CPU can safely enter RCU read-side critical sections. Unlike
  1030. * rcu_is_watching(), the caller of __rcu_is_watching() must have at
  1031. * least disabled preemption.
  1032. */
  1033. bool notrace __rcu_is_watching(void)
  1034. {
  1035. return !rcu_dynticks_curr_cpu_in_eqs();
  1036. }
  1037. /**
  1038. * rcu_is_watching - see if RCU thinks that the current CPU is idle
  1039. *
  1040. * If the current CPU is in its idle loop and is neither in an interrupt
  1041. * or NMI handler, return true.
  1042. */
  1043. bool notrace rcu_is_watching(void)
  1044. {
  1045. bool ret;
  1046. preempt_disable_notrace();
  1047. ret = __rcu_is_watching();
  1048. preempt_enable_notrace();
  1049. return ret;
  1050. }
  1051. EXPORT_SYMBOL_GPL(rcu_is_watching);
  1052. #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
  1053. /*
  1054. * Is the current CPU online? Disable preemption to avoid false positives
  1055. * that could otherwise happen due to the current CPU number being sampled,
  1056. * this task being preempted, its old CPU being taken offline, resuming
  1057. * on some other CPU, then determining that its old CPU is now offline.
  1058. * It is OK to use RCU on an offline processor during initial boot, hence
  1059. * the check for rcu_scheduler_fully_active. Note also that it is OK
  1060. * for a CPU coming online to use RCU for one jiffy prior to marking itself
  1061. * online in the cpu_online_mask. Similarly, it is OK for a CPU going
  1062. * offline to continue to use RCU for one jiffy after marking itself
  1063. * offline in the cpu_online_mask. This leniency is necessary given the
  1064. * non-atomic nature of the online and offline processing, for example,
  1065. * the fact that a CPU enters the scheduler after completing the teardown
  1066. * of the CPU.
  1067. *
  1068. * This is also why RCU internally marks CPUs online during in the
  1069. * preparation phase and offline after the CPU has been taken down.
  1070. *
  1071. * Disable checking if in an NMI handler because we cannot safely report
  1072. * errors from NMI handlers anyway.
  1073. */
  1074. bool rcu_lockdep_current_cpu_online(void)
  1075. {
  1076. struct rcu_data *rdp;
  1077. struct rcu_node *rnp;
  1078. bool ret;
  1079. if (in_nmi())
  1080. return true;
  1081. preempt_disable();
  1082. rdp = this_cpu_ptr(&rcu_sched_data);
  1083. rnp = rdp->mynode;
  1084. ret = (rdp->grpmask & rcu_rnp_online_cpus(rnp)) ||
  1085. !rcu_scheduler_fully_active;
  1086. preempt_enable();
  1087. return ret;
  1088. }
  1089. EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
  1090. #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
  1091. /**
  1092. * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
  1093. *
  1094. * If the current CPU is idle or running at a first-level (not nested)
  1095. * interrupt from idle, return true. The caller must have at least
  1096. * disabled preemption.
  1097. */
  1098. static int rcu_is_cpu_rrupt_from_idle(void)
  1099. {
  1100. return __this_cpu_read(rcu_dynticks.dynticks_nesting) <= 1;
  1101. }
  1102. /*
  1103. * Snapshot the specified CPU's dynticks counter so that we can later
  1104. * credit them with an implicit quiescent state. Return 1 if this CPU
  1105. * is in dynticks idle mode, which is an extended quiescent state.
  1106. */
  1107. static int dyntick_save_progress_counter(struct rcu_data *rdp,
  1108. bool *isidle, unsigned long *maxj)
  1109. {
  1110. rdp->dynticks_snap = rcu_dynticks_snap(rdp->dynticks);
  1111. rcu_sysidle_check_cpu(rdp, isidle, maxj);
  1112. if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
  1113. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  1114. if (ULONG_CMP_LT(READ_ONCE(rdp->gpnum) + ULONG_MAX / 4,
  1115. rdp->mynode->gpnum))
  1116. WRITE_ONCE(rdp->gpwrap, true);
  1117. return 1;
  1118. }
  1119. return 0;
  1120. }
  1121. /*
  1122. * Return true if the specified CPU has passed through a quiescent
  1123. * state by virtue of being in or having passed through an dynticks
  1124. * idle state since the last call to dyntick_save_progress_counter()
  1125. * for this same CPU, or by virtue of having been offline.
  1126. */
  1127. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp,
  1128. bool *isidle, unsigned long *maxj)
  1129. {
  1130. unsigned long jtsq;
  1131. int *rcrmp;
  1132. unsigned long rjtsc;
  1133. struct rcu_node *rnp;
  1134. /*
  1135. * If the CPU passed through or entered a dynticks idle phase with
  1136. * no active irq/NMI handlers, then we can safely pretend that the CPU
  1137. * already acknowledged the request to pass through a quiescent
  1138. * state. Either way, that CPU cannot possibly be in an RCU
  1139. * read-side critical section that started before the beginning
  1140. * of the current RCU grace period.
  1141. */
  1142. if (rcu_dynticks_in_eqs_since(rdp->dynticks, rdp->dynticks_snap)) {
  1143. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  1144. rdp->dynticks_fqs++;
  1145. return 1;
  1146. }
  1147. /* Compute and saturate jiffies_till_sched_qs. */
  1148. jtsq = jiffies_till_sched_qs;
  1149. rjtsc = rcu_jiffies_till_stall_check();
  1150. if (jtsq > rjtsc / 2) {
  1151. WRITE_ONCE(jiffies_till_sched_qs, rjtsc);
  1152. jtsq = rjtsc / 2;
  1153. } else if (jtsq < 1) {
  1154. WRITE_ONCE(jiffies_till_sched_qs, 1);
  1155. jtsq = 1;
  1156. }
  1157. /*
  1158. * Has this CPU encountered a cond_resched_rcu_qs() since the
  1159. * beginning of the grace period? For this to be the case,
  1160. * the CPU has to have noticed the current grace period. This
  1161. * might not be the case for nohz_full CPUs looping in the kernel.
  1162. */
  1163. rnp = rdp->mynode;
  1164. if (time_after(jiffies, rdp->rsp->gp_start + jtsq) &&
  1165. READ_ONCE(rdp->rcu_qs_ctr_snap) != per_cpu(rcu_qs_ctr, rdp->cpu) &&
  1166. READ_ONCE(rdp->gpnum) == rnp->gpnum && !rdp->gpwrap) {
  1167. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("rqc"));
  1168. return 1;
  1169. }
  1170. /* Check for the CPU being offline. */
  1171. if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp))) {
  1172. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("ofl"));
  1173. rdp->offline_fqs++;
  1174. return 1;
  1175. }
  1176. /*
  1177. * A CPU running for an extended time within the kernel can
  1178. * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
  1179. * even context-switching back and forth between a pair of
  1180. * in-kernel CPU-bound tasks cannot advance grace periods.
  1181. * So if the grace period is old enough, make the CPU pay attention.
  1182. * Note that the unsynchronized assignments to the per-CPU
  1183. * rcu_sched_qs_mask variable are safe. Yes, setting of
  1184. * bits can be lost, but they will be set again on the next
  1185. * force-quiescent-state pass. So lost bit sets do not result
  1186. * in incorrect behavior, merely in a grace period lasting
  1187. * a few jiffies longer than it might otherwise. Because
  1188. * there are at most four threads involved, and because the
  1189. * updates are only once every few jiffies, the probability of
  1190. * lossage (and thus of slight grace-period extension) is
  1191. * quite low.
  1192. *
  1193. * Note that if the jiffies_till_sched_qs boot/sysfs parameter
  1194. * is set too high, we override with half of the RCU CPU stall
  1195. * warning delay.
  1196. */
  1197. rcrmp = &per_cpu(rcu_sched_qs_mask, rdp->cpu);
  1198. if (time_after(jiffies, rdp->rsp->gp_start + jtsq) ||
  1199. time_after(jiffies, rdp->rsp->jiffies_resched)) {
  1200. if (!(READ_ONCE(*rcrmp) & rdp->rsp->flavor_mask)) {
  1201. WRITE_ONCE(rdp->cond_resched_completed,
  1202. READ_ONCE(rdp->mynode->completed));
  1203. smp_mb(); /* ->cond_resched_completed before *rcrmp. */
  1204. WRITE_ONCE(*rcrmp,
  1205. READ_ONCE(*rcrmp) + rdp->rsp->flavor_mask);
  1206. }
  1207. rdp->rsp->jiffies_resched += 5; /* Re-enable beating. */
  1208. }
  1209. /*
  1210. * If more than halfway to RCU CPU stall-warning time, do
  1211. * a resched_cpu() to try to loosen things up a bit.
  1212. */
  1213. if (jiffies - rdp->rsp->gp_start > rcu_jiffies_till_stall_check() / 2)
  1214. resched_cpu(rdp->cpu);
  1215. return 0;
  1216. }
  1217. static void record_gp_stall_check_time(struct rcu_state *rsp)
  1218. {
  1219. unsigned long j = jiffies;
  1220. unsigned long j1;
  1221. rsp->gp_start = j;
  1222. smp_wmb(); /* Record start time before stall time. */
  1223. j1 = rcu_jiffies_till_stall_check();
  1224. WRITE_ONCE(rsp->jiffies_stall, j + j1);
  1225. rsp->jiffies_resched = j + j1 / 2;
  1226. rsp->n_force_qs_gpstart = READ_ONCE(rsp->n_force_qs);
  1227. }
  1228. /*
  1229. * Convert a ->gp_state value to a character string.
  1230. */
  1231. static const char *gp_state_getname(short gs)
  1232. {
  1233. if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
  1234. return "???";
  1235. return gp_state_names[gs];
  1236. }
  1237. /*
  1238. * Complain about starvation of grace-period kthread.
  1239. */
  1240. static void rcu_check_gp_kthread_starvation(struct rcu_state *rsp)
  1241. {
  1242. unsigned long gpa;
  1243. unsigned long j;
  1244. j = jiffies;
  1245. gpa = READ_ONCE(rsp->gp_activity);
  1246. if (j - gpa > 2 * HZ) {
  1247. pr_err("%s kthread starved for %ld jiffies! g%lu c%lu f%#x %s(%d) ->state=%#lx\n",
  1248. rsp->name, j - gpa,
  1249. rsp->gpnum, rsp->completed,
  1250. rsp->gp_flags,
  1251. gp_state_getname(rsp->gp_state), rsp->gp_state,
  1252. rsp->gp_kthread ? rsp->gp_kthread->state : ~0);
  1253. if (rsp->gp_kthread) {
  1254. sched_show_task(rsp->gp_kthread);
  1255. wake_up_process(rsp->gp_kthread);
  1256. }
  1257. }
  1258. }
  1259. /*
  1260. * Dump stacks of all tasks running on stalled CPUs. First try using
  1261. * NMIs, but fall back to manual remote stack tracing on architectures
  1262. * that don't support NMI-based stack dumps. The NMI-triggered stack
  1263. * traces are more accurate because they are printed by the target CPU.
  1264. */
  1265. static void rcu_dump_cpu_stacks(struct rcu_state *rsp)
  1266. {
  1267. int cpu;
  1268. unsigned long flags;
  1269. struct rcu_node *rnp;
  1270. rcu_for_each_leaf_node(rsp, rnp) {
  1271. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1272. for_each_leaf_node_possible_cpu(rnp, cpu)
  1273. if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu))
  1274. if (!trigger_single_cpu_backtrace(cpu))
  1275. dump_cpu_task(cpu);
  1276. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1277. }
  1278. }
  1279. /*
  1280. * If too much time has passed in the current grace period, and if
  1281. * so configured, go kick the relevant kthreads.
  1282. */
  1283. static void rcu_stall_kick_kthreads(struct rcu_state *rsp)
  1284. {
  1285. unsigned long j;
  1286. if (!rcu_kick_kthreads)
  1287. return;
  1288. j = READ_ONCE(rsp->jiffies_kick_kthreads);
  1289. if (time_after(jiffies, j) && rsp->gp_kthread &&
  1290. (rcu_gp_in_progress(rsp) || READ_ONCE(rsp->gp_flags))) {
  1291. WARN_ONCE(1, "Kicking %s grace-period kthread\n", rsp->name);
  1292. rcu_ftrace_dump(DUMP_ALL);
  1293. wake_up_process(rsp->gp_kthread);
  1294. WRITE_ONCE(rsp->jiffies_kick_kthreads, j + HZ);
  1295. }
  1296. }
  1297. static inline void panic_on_rcu_stall(void)
  1298. {
  1299. if (sysctl_panic_on_rcu_stall)
  1300. panic("RCU Stall\n");
  1301. }
  1302. static void print_other_cpu_stall(struct rcu_state *rsp, unsigned long gpnum)
  1303. {
  1304. int cpu;
  1305. long delta;
  1306. unsigned long flags;
  1307. unsigned long gpa;
  1308. unsigned long j;
  1309. int ndetected = 0;
  1310. struct rcu_node *rnp = rcu_get_root(rsp);
  1311. long totqlen = 0;
  1312. /* Kick and suppress, if so configured. */
  1313. rcu_stall_kick_kthreads(rsp);
  1314. if (rcu_cpu_stall_suppress)
  1315. return;
  1316. /* Only let one CPU complain about others per time interval. */
  1317. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1318. delta = jiffies - READ_ONCE(rsp->jiffies_stall);
  1319. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  1320. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1321. return;
  1322. }
  1323. WRITE_ONCE(rsp->jiffies_stall,
  1324. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  1325. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1326. /*
  1327. * OK, time to rat on our buddy...
  1328. * See Documentation/RCU/stallwarn.txt for info on how to debug
  1329. * RCU CPU stall warnings.
  1330. */
  1331. pr_err("INFO: %s detected stalls on CPUs/tasks:",
  1332. rsp->name);
  1333. print_cpu_stall_info_begin();
  1334. rcu_for_each_leaf_node(rsp, rnp) {
  1335. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1336. ndetected += rcu_print_task_stall(rnp);
  1337. if (rnp->qsmask != 0) {
  1338. for_each_leaf_node_possible_cpu(rnp, cpu)
  1339. if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
  1340. print_cpu_stall_info(rsp, cpu);
  1341. ndetected++;
  1342. }
  1343. }
  1344. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1345. }
  1346. print_cpu_stall_info_end();
  1347. for_each_possible_cpu(cpu)
  1348. totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
  1349. pr_cont("(detected by %d, t=%ld jiffies, g=%ld, c=%ld, q=%lu)\n",
  1350. smp_processor_id(), (long)(jiffies - rsp->gp_start),
  1351. (long)rsp->gpnum, (long)rsp->completed, totqlen);
  1352. if (ndetected) {
  1353. rcu_dump_cpu_stacks(rsp);
  1354. /* Complain about tasks blocking the grace period. */
  1355. rcu_print_detail_task_stall(rsp);
  1356. } else {
  1357. if (READ_ONCE(rsp->gpnum) != gpnum ||
  1358. READ_ONCE(rsp->completed) == gpnum) {
  1359. pr_err("INFO: Stall ended before state dump start\n");
  1360. } else {
  1361. j = jiffies;
  1362. gpa = READ_ONCE(rsp->gp_activity);
  1363. pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
  1364. rsp->name, j - gpa, j, gpa,
  1365. jiffies_till_next_fqs,
  1366. rcu_get_root(rsp)->qsmask);
  1367. /* In this case, the current CPU might be at fault. */
  1368. sched_show_task(current);
  1369. }
  1370. }
  1371. rcu_check_gp_kthread_starvation(rsp);
  1372. panic_on_rcu_stall();
  1373. force_quiescent_state(rsp); /* Kick them all. */
  1374. }
  1375. static void print_cpu_stall(struct rcu_state *rsp)
  1376. {
  1377. int cpu;
  1378. unsigned long flags;
  1379. struct rcu_node *rnp = rcu_get_root(rsp);
  1380. long totqlen = 0;
  1381. /* Kick and suppress, if so configured. */
  1382. rcu_stall_kick_kthreads(rsp);
  1383. if (rcu_cpu_stall_suppress)
  1384. return;
  1385. /*
  1386. * OK, time to rat on ourselves...
  1387. * See Documentation/RCU/stallwarn.txt for info on how to debug
  1388. * RCU CPU stall warnings.
  1389. */
  1390. pr_err("INFO: %s self-detected stall on CPU", rsp->name);
  1391. print_cpu_stall_info_begin();
  1392. print_cpu_stall_info(rsp, smp_processor_id());
  1393. print_cpu_stall_info_end();
  1394. for_each_possible_cpu(cpu)
  1395. totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
  1396. pr_cont(" (t=%lu jiffies g=%ld c=%ld q=%lu)\n",
  1397. jiffies - rsp->gp_start,
  1398. (long)rsp->gpnum, (long)rsp->completed, totqlen);
  1399. rcu_check_gp_kthread_starvation(rsp);
  1400. rcu_dump_cpu_stacks(rsp);
  1401. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1402. if (ULONG_CMP_GE(jiffies, READ_ONCE(rsp->jiffies_stall)))
  1403. WRITE_ONCE(rsp->jiffies_stall,
  1404. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  1405. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1406. panic_on_rcu_stall();
  1407. /*
  1408. * Attempt to revive the RCU machinery by forcing a context switch.
  1409. *
  1410. * A context switch would normally allow the RCU state machine to make
  1411. * progress and it could be we're stuck in kernel space without context
  1412. * switches for an entirely unreasonable amount of time.
  1413. */
  1414. resched_cpu(smp_processor_id());
  1415. }
  1416. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  1417. {
  1418. unsigned long completed;
  1419. unsigned long gpnum;
  1420. unsigned long gps;
  1421. unsigned long j;
  1422. unsigned long js;
  1423. struct rcu_node *rnp;
  1424. if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
  1425. !rcu_gp_in_progress(rsp))
  1426. return;
  1427. rcu_stall_kick_kthreads(rsp);
  1428. j = jiffies;
  1429. /*
  1430. * Lots of memory barriers to reject false positives.
  1431. *
  1432. * The idea is to pick up rsp->gpnum, then rsp->jiffies_stall,
  1433. * then rsp->gp_start, and finally rsp->completed. These values
  1434. * are updated in the opposite order with memory barriers (or
  1435. * equivalent) during grace-period initialization and cleanup.
  1436. * Now, a false positive can occur if we get an new value of
  1437. * rsp->gp_start and a old value of rsp->jiffies_stall. But given
  1438. * the memory barriers, the only way that this can happen is if one
  1439. * grace period ends and another starts between these two fetches.
  1440. * Detect this by comparing rsp->completed with the previous fetch
  1441. * from rsp->gpnum.
  1442. *
  1443. * Given this check, comparisons of jiffies, rsp->jiffies_stall,
  1444. * and rsp->gp_start suffice to forestall false positives.
  1445. */
  1446. gpnum = READ_ONCE(rsp->gpnum);
  1447. smp_rmb(); /* Pick up ->gpnum first... */
  1448. js = READ_ONCE(rsp->jiffies_stall);
  1449. smp_rmb(); /* ...then ->jiffies_stall before the rest... */
  1450. gps = READ_ONCE(rsp->gp_start);
  1451. smp_rmb(); /* ...and finally ->gp_start before ->completed. */
  1452. completed = READ_ONCE(rsp->completed);
  1453. if (ULONG_CMP_GE(completed, gpnum) ||
  1454. ULONG_CMP_LT(j, js) ||
  1455. ULONG_CMP_GE(gps, js))
  1456. return; /* No stall or GP completed since entering function. */
  1457. rnp = rdp->mynode;
  1458. if (rcu_gp_in_progress(rsp) &&
  1459. (READ_ONCE(rnp->qsmask) & rdp->grpmask)) {
  1460. /* We haven't checked in, so go dump stack. */
  1461. print_cpu_stall(rsp);
  1462. } else if (rcu_gp_in_progress(rsp) &&
  1463. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
  1464. /* They had a few time units to dump stack, so complain. */
  1465. print_other_cpu_stall(rsp, gpnum);
  1466. }
  1467. }
  1468. /**
  1469. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  1470. *
  1471. * Set the stall-warning timeout way off into the future, thus preventing
  1472. * any RCU CPU stall-warning messages from appearing in the current set of
  1473. * RCU grace periods.
  1474. *
  1475. * The caller must disable hard irqs.
  1476. */
  1477. void rcu_cpu_stall_reset(void)
  1478. {
  1479. struct rcu_state *rsp;
  1480. for_each_rcu_flavor(rsp)
  1481. WRITE_ONCE(rsp->jiffies_stall, jiffies + ULONG_MAX / 2);
  1482. }
  1483. /*
  1484. * Initialize the specified rcu_data structure's default callback list
  1485. * to empty. The default callback list is the one that is not used by
  1486. * no-callbacks CPUs.
  1487. */
  1488. static void init_default_callback_list(struct rcu_data *rdp)
  1489. {
  1490. int i;
  1491. rdp->nxtlist = NULL;
  1492. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1493. rdp->nxttail[i] = &rdp->nxtlist;
  1494. }
  1495. /*
  1496. * Initialize the specified rcu_data structure's callback list to empty.
  1497. */
  1498. static void init_callback_list(struct rcu_data *rdp)
  1499. {
  1500. if (init_nocb_callback_list(rdp))
  1501. return;
  1502. init_default_callback_list(rdp);
  1503. }
  1504. /*
  1505. * Determine the value that ->completed will have at the end of the
  1506. * next subsequent grace period. This is used to tag callbacks so that
  1507. * a CPU can invoke callbacks in a timely fashion even if that CPU has
  1508. * been dyntick-idle for an extended period with callbacks under the
  1509. * influence of RCU_FAST_NO_HZ.
  1510. *
  1511. * The caller must hold rnp->lock with interrupts disabled.
  1512. */
  1513. static unsigned long rcu_cbs_completed(struct rcu_state *rsp,
  1514. struct rcu_node *rnp)
  1515. {
  1516. /*
  1517. * If RCU is idle, we just wait for the next grace period.
  1518. * But we can only be sure that RCU is idle if we are looking
  1519. * at the root rcu_node structure -- otherwise, a new grace
  1520. * period might have started, but just not yet gotten around
  1521. * to initializing the current non-root rcu_node structure.
  1522. */
  1523. if (rcu_get_root(rsp) == rnp && rnp->gpnum == rnp->completed)
  1524. return rnp->completed + 1;
  1525. /*
  1526. * Otherwise, wait for a possible partial grace period and
  1527. * then the subsequent full grace period.
  1528. */
  1529. return rnp->completed + 2;
  1530. }
  1531. /*
  1532. * Trace-event helper function for rcu_start_future_gp() and
  1533. * rcu_nocb_wait_gp().
  1534. */
  1535. static void trace_rcu_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  1536. unsigned long c, const char *s)
  1537. {
  1538. trace_rcu_future_grace_period(rdp->rsp->name, rnp->gpnum,
  1539. rnp->completed, c, rnp->level,
  1540. rnp->grplo, rnp->grphi, s);
  1541. }
  1542. /*
  1543. * Start some future grace period, as needed to handle newly arrived
  1544. * callbacks. The required future grace periods are recorded in each
  1545. * rcu_node structure's ->need_future_gp field. Returns true if there
  1546. * is reason to awaken the grace-period kthread.
  1547. *
  1548. * The caller must hold the specified rcu_node structure's ->lock.
  1549. */
  1550. static bool __maybe_unused
  1551. rcu_start_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  1552. unsigned long *c_out)
  1553. {
  1554. unsigned long c;
  1555. int i;
  1556. bool ret = false;
  1557. struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
  1558. /*
  1559. * Pick up grace-period number for new callbacks. If this
  1560. * grace period is already marked as needed, return to the caller.
  1561. */
  1562. c = rcu_cbs_completed(rdp->rsp, rnp);
  1563. trace_rcu_future_gp(rnp, rdp, c, TPS("Startleaf"));
  1564. if (rnp->need_future_gp[c & 0x1]) {
  1565. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartleaf"));
  1566. goto out;
  1567. }
  1568. /*
  1569. * If either this rcu_node structure or the root rcu_node structure
  1570. * believe that a grace period is in progress, then we must wait
  1571. * for the one following, which is in "c". Because our request
  1572. * will be noticed at the end of the current grace period, we don't
  1573. * need to explicitly start one. We only do the lockless check
  1574. * of rnp_root's fields if the current rcu_node structure thinks
  1575. * there is no grace period in flight, and because we hold rnp->lock,
  1576. * the only possible change is when rnp_root's two fields are
  1577. * equal, in which case rnp_root->gpnum might be concurrently
  1578. * incremented. But that is OK, as it will just result in our
  1579. * doing some extra useless work.
  1580. */
  1581. if (rnp->gpnum != rnp->completed ||
  1582. READ_ONCE(rnp_root->gpnum) != READ_ONCE(rnp_root->completed)) {
  1583. rnp->need_future_gp[c & 0x1]++;
  1584. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleaf"));
  1585. goto out;
  1586. }
  1587. /*
  1588. * There might be no grace period in progress. If we don't already
  1589. * hold it, acquire the root rcu_node structure's lock in order to
  1590. * start one (if needed).
  1591. */
  1592. if (rnp != rnp_root)
  1593. raw_spin_lock_rcu_node(rnp_root);
  1594. /*
  1595. * Get a new grace-period number. If there really is no grace
  1596. * period in progress, it will be smaller than the one we obtained
  1597. * earlier. Adjust callbacks as needed. Note that even no-CBs
  1598. * CPUs have a ->nxtcompleted[] array, so no no-CBs checks needed.
  1599. */
  1600. c = rcu_cbs_completed(rdp->rsp, rnp_root);
  1601. for (i = RCU_DONE_TAIL; i < RCU_NEXT_TAIL; i++)
  1602. if (ULONG_CMP_LT(c, rdp->nxtcompleted[i]))
  1603. rdp->nxtcompleted[i] = c;
  1604. /*
  1605. * If the needed for the required grace period is already
  1606. * recorded, trace and leave.
  1607. */
  1608. if (rnp_root->need_future_gp[c & 0x1]) {
  1609. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartedroot"));
  1610. goto unlock_out;
  1611. }
  1612. /* Record the need for the future grace period. */
  1613. rnp_root->need_future_gp[c & 0x1]++;
  1614. /* If a grace period is not already in progress, start one. */
  1615. if (rnp_root->gpnum != rnp_root->completed) {
  1616. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleafroot"));
  1617. } else {
  1618. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedroot"));
  1619. ret = rcu_start_gp_advanced(rdp->rsp, rnp_root, rdp);
  1620. }
  1621. unlock_out:
  1622. if (rnp != rnp_root)
  1623. raw_spin_unlock_rcu_node(rnp_root);
  1624. out:
  1625. if (c_out != NULL)
  1626. *c_out = c;
  1627. return ret;
  1628. }
  1629. /*
  1630. * Clean up any old requests for the just-ended grace period. Also return
  1631. * whether any additional grace periods have been requested. Also invoke
  1632. * rcu_nocb_gp_cleanup() in order to wake up any no-callbacks kthreads
  1633. * waiting for this grace period to complete.
  1634. */
  1635. static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  1636. {
  1637. int c = rnp->completed;
  1638. int needmore;
  1639. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  1640. rnp->need_future_gp[c & 0x1] = 0;
  1641. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  1642. trace_rcu_future_gp(rnp, rdp, c,
  1643. needmore ? TPS("CleanupMore") : TPS("Cleanup"));
  1644. return needmore;
  1645. }
  1646. /*
  1647. * Awaken the grace-period kthread for the specified flavor of RCU.
  1648. * Don't do a self-awaken, and don't bother awakening when there is
  1649. * nothing for the grace-period kthread to do (as in several CPUs
  1650. * raced to awaken, and we lost), and finally don't try to awaken
  1651. * a kthread that has not yet been created.
  1652. */
  1653. static void rcu_gp_kthread_wake(struct rcu_state *rsp)
  1654. {
  1655. if (current == rsp->gp_kthread ||
  1656. !READ_ONCE(rsp->gp_flags) ||
  1657. !rsp->gp_kthread)
  1658. return;
  1659. swake_up(&rsp->gp_wq);
  1660. }
  1661. /*
  1662. * If there is room, assign a ->completed number to any callbacks on
  1663. * this CPU that have not already been assigned. Also accelerate any
  1664. * callbacks that were previously assigned a ->completed number that has
  1665. * since proven to be too conservative, which can happen if callbacks get
  1666. * assigned a ->completed number while RCU is idle, but with reference to
  1667. * a non-root rcu_node structure. This function is idempotent, so it does
  1668. * not hurt to call it repeatedly. Returns an flag saying that we should
  1669. * awaken the RCU grace-period kthread.
  1670. *
  1671. * The caller must hold rnp->lock with interrupts disabled.
  1672. */
  1673. static bool rcu_accelerate_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1674. struct rcu_data *rdp)
  1675. {
  1676. unsigned long c;
  1677. int i;
  1678. bool ret;
  1679. /* If the CPU has no callbacks, nothing to do. */
  1680. if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
  1681. return false;
  1682. /*
  1683. * Starting from the sublist containing the callbacks most
  1684. * recently assigned a ->completed number and working down, find the
  1685. * first sublist that is not assignable to an upcoming grace period.
  1686. * Such a sublist has something in it (first two tests) and has
  1687. * a ->completed number assigned that will complete sooner than
  1688. * the ->completed number for newly arrived callbacks (last test).
  1689. *
  1690. * The key point is that any later sublist can be assigned the
  1691. * same ->completed number as the newly arrived callbacks, which
  1692. * means that the callbacks in any of these later sublist can be
  1693. * grouped into a single sublist, whether or not they have already
  1694. * been assigned a ->completed number.
  1695. */
  1696. c = rcu_cbs_completed(rsp, rnp);
  1697. for (i = RCU_NEXT_TAIL - 1; i > RCU_DONE_TAIL; i--)
  1698. if (rdp->nxttail[i] != rdp->nxttail[i - 1] &&
  1699. !ULONG_CMP_GE(rdp->nxtcompleted[i], c))
  1700. break;
  1701. /*
  1702. * If there are no sublist for unassigned callbacks, leave.
  1703. * At the same time, advance "i" one sublist, so that "i" will
  1704. * index into the sublist where all the remaining callbacks should
  1705. * be grouped into.
  1706. */
  1707. if (++i >= RCU_NEXT_TAIL)
  1708. return false;
  1709. /*
  1710. * Assign all subsequent callbacks' ->completed number to the next
  1711. * full grace period and group them all in the sublist initially
  1712. * indexed by "i".
  1713. */
  1714. for (; i <= RCU_NEXT_TAIL; i++) {
  1715. rdp->nxttail[i] = rdp->nxttail[RCU_NEXT_TAIL];
  1716. rdp->nxtcompleted[i] = c;
  1717. }
  1718. /* Record any needed additional grace periods. */
  1719. ret = rcu_start_future_gp(rnp, rdp, NULL);
  1720. /* Trace depending on how much we were able to accelerate. */
  1721. if (!*rdp->nxttail[RCU_WAIT_TAIL])
  1722. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccWaitCB"));
  1723. else
  1724. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccReadyCB"));
  1725. return ret;
  1726. }
  1727. /*
  1728. * Move any callbacks whose grace period has completed to the
  1729. * RCU_DONE_TAIL sublist, then compact the remaining sublists and
  1730. * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
  1731. * sublist. This function is idempotent, so it does not hurt to
  1732. * invoke it repeatedly. As long as it is not invoked -too- often...
  1733. * Returns true if the RCU grace-period kthread needs to be awakened.
  1734. *
  1735. * The caller must hold rnp->lock with interrupts disabled.
  1736. */
  1737. static bool rcu_advance_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1738. struct rcu_data *rdp)
  1739. {
  1740. int i, j;
  1741. /* If the CPU has no callbacks, nothing to do. */
  1742. if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
  1743. return false;
  1744. /*
  1745. * Find all callbacks whose ->completed numbers indicate that they
  1746. * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
  1747. */
  1748. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
  1749. if (ULONG_CMP_LT(rnp->completed, rdp->nxtcompleted[i]))
  1750. break;
  1751. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[i];
  1752. }
  1753. /* Clean up any sublist tail pointers that were misordered above. */
  1754. for (j = RCU_WAIT_TAIL; j < i; j++)
  1755. rdp->nxttail[j] = rdp->nxttail[RCU_DONE_TAIL];
  1756. /* Copy down callbacks to fill in empty sublists. */
  1757. for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
  1758. if (rdp->nxttail[j] == rdp->nxttail[RCU_NEXT_TAIL])
  1759. break;
  1760. rdp->nxttail[j] = rdp->nxttail[i];
  1761. rdp->nxtcompleted[j] = rdp->nxtcompleted[i];
  1762. }
  1763. /* Classify any remaining callbacks. */
  1764. return rcu_accelerate_cbs(rsp, rnp, rdp);
  1765. }
  1766. /*
  1767. * Update CPU-local rcu_data state to record the beginnings and ends of
  1768. * grace periods. The caller must hold the ->lock of the leaf rcu_node
  1769. * structure corresponding to the current CPU, and must have irqs disabled.
  1770. * Returns true if the grace-period kthread needs to be awakened.
  1771. */
  1772. static bool __note_gp_changes(struct rcu_state *rsp, struct rcu_node *rnp,
  1773. struct rcu_data *rdp)
  1774. {
  1775. bool ret;
  1776. bool need_gp;
  1777. /* Handle the ends of any preceding grace periods first. */
  1778. if (rdp->completed == rnp->completed &&
  1779. !unlikely(READ_ONCE(rdp->gpwrap))) {
  1780. /* No grace period end, so just accelerate recent callbacks. */
  1781. ret = rcu_accelerate_cbs(rsp, rnp, rdp);
  1782. } else {
  1783. /* Advance callbacks. */
  1784. ret = rcu_advance_cbs(rsp, rnp, rdp);
  1785. /* Remember that we saw this grace-period completion. */
  1786. rdp->completed = rnp->completed;
  1787. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuend"));
  1788. }
  1789. if (rdp->gpnum != rnp->gpnum || unlikely(READ_ONCE(rdp->gpwrap))) {
  1790. /*
  1791. * If the current grace period is waiting for this CPU,
  1792. * set up to detect a quiescent state, otherwise don't
  1793. * go looking for one.
  1794. */
  1795. rdp->gpnum = rnp->gpnum;
  1796. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpustart"));
  1797. need_gp = !!(rnp->qsmask & rdp->grpmask);
  1798. rdp->cpu_no_qs.b.norm = need_gp;
  1799. rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
  1800. rdp->core_needs_qs = need_gp;
  1801. zero_cpu_stall_ticks(rdp);
  1802. WRITE_ONCE(rdp->gpwrap, false);
  1803. }
  1804. return ret;
  1805. }
  1806. static void note_gp_changes(struct rcu_state *rsp, struct rcu_data *rdp)
  1807. {
  1808. unsigned long flags;
  1809. bool needwake;
  1810. struct rcu_node *rnp;
  1811. local_irq_save(flags);
  1812. rnp = rdp->mynode;
  1813. if ((rdp->gpnum == READ_ONCE(rnp->gpnum) &&
  1814. rdp->completed == READ_ONCE(rnp->completed) &&
  1815. !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
  1816. !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
  1817. local_irq_restore(flags);
  1818. return;
  1819. }
  1820. needwake = __note_gp_changes(rsp, rnp, rdp);
  1821. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1822. if (needwake)
  1823. rcu_gp_kthread_wake(rsp);
  1824. }
  1825. static void rcu_gp_slow(struct rcu_state *rsp, int delay)
  1826. {
  1827. if (delay > 0 &&
  1828. !(rsp->gpnum % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
  1829. schedule_timeout_uninterruptible(delay);
  1830. }
  1831. /*
  1832. * Initialize a new grace period. Return false if no grace period required.
  1833. */
  1834. static bool rcu_gp_init(struct rcu_state *rsp)
  1835. {
  1836. unsigned long oldmask;
  1837. struct rcu_data *rdp;
  1838. struct rcu_node *rnp = rcu_get_root(rsp);
  1839. WRITE_ONCE(rsp->gp_activity, jiffies);
  1840. raw_spin_lock_irq_rcu_node(rnp);
  1841. if (!READ_ONCE(rsp->gp_flags)) {
  1842. /* Spurious wakeup, tell caller to go back to sleep. */
  1843. raw_spin_unlock_irq_rcu_node(rnp);
  1844. return false;
  1845. }
  1846. WRITE_ONCE(rsp->gp_flags, 0); /* Clear all flags: New grace period. */
  1847. if (WARN_ON_ONCE(rcu_gp_in_progress(rsp))) {
  1848. /*
  1849. * Grace period already in progress, don't start another.
  1850. * Not supposed to be able to happen.
  1851. */
  1852. raw_spin_unlock_irq_rcu_node(rnp);
  1853. return false;
  1854. }
  1855. /* Advance to a new grace period and initialize state. */
  1856. record_gp_stall_check_time(rsp);
  1857. /* Record GP times before starting GP, hence smp_store_release(). */
  1858. smp_store_release(&rsp->gpnum, rsp->gpnum + 1);
  1859. trace_rcu_grace_period(rsp->name, rsp->gpnum, TPS("start"));
  1860. raw_spin_unlock_irq_rcu_node(rnp);
  1861. /*
  1862. * Apply per-leaf buffered online and offline operations to the
  1863. * rcu_node tree. Note that this new grace period need not wait
  1864. * for subsequent online CPUs, and that quiescent-state forcing
  1865. * will handle subsequent offline CPUs.
  1866. */
  1867. rcu_for_each_leaf_node(rsp, rnp) {
  1868. rcu_gp_slow(rsp, gp_preinit_delay);
  1869. raw_spin_lock_irq_rcu_node(rnp);
  1870. if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
  1871. !rnp->wait_blkd_tasks) {
  1872. /* Nothing to do on this leaf rcu_node structure. */
  1873. raw_spin_unlock_irq_rcu_node(rnp);
  1874. continue;
  1875. }
  1876. /* Record old state, apply changes to ->qsmaskinit field. */
  1877. oldmask = rnp->qsmaskinit;
  1878. rnp->qsmaskinit = rnp->qsmaskinitnext;
  1879. /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
  1880. if (!oldmask != !rnp->qsmaskinit) {
  1881. if (!oldmask) /* First online CPU for this rcu_node. */
  1882. rcu_init_new_rnp(rnp);
  1883. else if (rcu_preempt_has_tasks(rnp)) /* blocked tasks */
  1884. rnp->wait_blkd_tasks = true;
  1885. else /* Last offline CPU and can propagate. */
  1886. rcu_cleanup_dead_rnp(rnp);
  1887. }
  1888. /*
  1889. * If all waited-on tasks from prior grace period are
  1890. * done, and if all this rcu_node structure's CPUs are
  1891. * still offline, propagate up the rcu_node tree and
  1892. * clear ->wait_blkd_tasks. Otherwise, if one of this
  1893. * rcu_node structure's CPUs has since come back online,
  1894. * simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp()
  1895. * checks for this, so just call it unconditionally).
  1896. */
  1897. if (rnp->wait_blkd_tasks &&
  1898. (!rcu_preempt_has_tasks(rnp) ||
  1899. rnp->qsmaskinit)) {
  1900. rnp->wait_blkd_tasks = false;
  1901. rcu_cleanup_dead_rnp(rnp);
  1902. }
  1903. raw_spin_unlock_irq_rcu_node(rnp);
  1904. }
  1905. /*
  1906. * Set the quiescent-state-needed bits in all the rcu_node
  1907. * structures for all currently online CPUs in breadth-first order,
  1908. * starting from the root rcu_node structure, relying on the layout
  1909. * of the tree within the rsp->node[] array. Note that other CPUs
  1910. * will access only the leaves of the hierarchy, thus seeing that no
  1911. * grace period is in progress, at least until the corresponding
  1912. * leaf node has been initialized.
  1913. *
  1914. * The grace period cannot complete until the initialization
  1915. * process finishes, because this kthread handles both.
  1916. */
  1917. rcu_for_each_node_breadth_first(rsp, rnp) {
  1918. rcu_gp_slow(rsp, gp_init_delay);
  1919. raw_spin_lock_irq_rcu_node(rnp);
  1920. rdp = this_cpu_ptr(rsp->rda);
  1921. rcu_preempt_check_blocked_tasks(rnp);
  1922. rnp->qsmask = rnp->qsmaskinit;
  1923. WRITE_ONCE(rnp->gpnum, rsp->gpnum);
  1924. if (WARN_ON_ONCE(rnp->completed != rsp->completed))
  1925. WRITE_ONCE(rnp->completed, rsp->completed);
  1926. if (rnp == rdp->mynode)
  1927. (void)__note_gp_changes(rsp, rnp, rdp);
  1928. rcu_preempt_boost_start_gp(rnp);
  1929. trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
  1930. rnp->level, rnp->grplo,
  1931. rnp->grphi, rnp->qsmask);
  1932. raw_spin_unlock_irq_rcu_node(rnp);
  1933. cond_resched_rcu_qs();
  1934. WRITE_ONCE(rsp->gp_activity, jiffies);
  1935. }
  1936. return true;
  1937. }
  1938. /*
  1939. * Helper function for wait_event_interruptible_timeout() wakeup
  1940. * at force-quiescent-state time.
  1941. */
  1942. static bool rcu_gp_fqs_check_wake(struct rcu_state *rsp, int *gfp)
  1943. {
  1944. struct rcu_node *rnp = rcu_get_root(rsp);
  1945. /* Someone like call_rcu() requested a force-quiescent-state scan. */
  1946. *gfp = READ_ONCE(rsp->gp_flags);
  1947. if (*gfp & RCU_GP_FLAG_FQS)
  1948. return true;
  1949. /* The current grace period has completed. */
  1950. if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
  1951. return true;
  1952. return false;
  1953. }
  1954. /*
  1955. * Do one round of quiescent-state forcing.
  1956. */
  1957. static void rcu_gp_fqs(struct rcu_state *rsp, bool first_time)
  1958. {
  1959. bool isidle = false;
  1960. unsigned long maxj;
  1961. struct rcu_node *rnp = rcu_get_root(rsp);
  1962. WRITE_ONCE(rsp->gp_activity, jiffies);
  1963. rsp->n_force_qs++;
  1964. if (first_time) {
  1965. /* Collect dyntick-idle snapshots. */
  1966. if (is_sysidle_rcu_state(rsp)) {
  1967. isidle = true;
  1968. maxj = jiffies - ULONG_MAX / 4;
  1969. }
  1970. force_qs_rnp(rsp, dyntick_save_progress_counter,
  1971. &isidle, &maxj);
  1972. rcu_sysidle_report_gp(rsp, isidle, maxj);
  1973. } else {
  1974. /* Handle dyntick-idle and offline CPUs. */
  1975. isidle = true;
  1976. force_qs_rnp(rsp, rcu_implicit_dynticks_qs, &isidle, &maxj);
  1977. }
  1978. /* Clear flag to prevent immediate re-entry. */
  1979. if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  1980. raw_spin_lock_irq_rcu_node(rnp);
  1981. WRITE_ONCE(rsp->gp_flags,
  1982. READ_ONCE(rsp->gp_flags) & ~RCU_GP_FLAG_FQS);
  1983. raw_spin_unlock_irq_rcu_node(rnp);
  1984. }
  1985. }
  1986. /*
  1987. * Clean up after the old grace period.
  1988. */
  1989. static void rcu_gp_cleanup(struct rcu_state *rsp)
  1990. {
  1991. unsigned long gp_duration;
  1992. bool needgp = false;
  1993. int nocb = 0;
  1994. struct rcu_data *rdp;
  1995. struct rcu_node *rnp = rcu_get_root(rsp);
  1996. struct swait_queue_head *sq;
  1997. WRITE_ONCE(rsp->gp_activity, jiffies);
  1998. raw_spin_lock_irq_rcu_node(rnp);
  1999. gp_duration = jiffies - rsp->gp_start;
  2000. if (gp_duration > rsp->gp_max)
  2001. rsp->gp_max = gp_duration;
  2002. /*
  2003. * We know the grace period is complete, but to everyone else
  2004. * it appears to still be ongoing. But it is also the case
  2005. * that to everyone else it looks like there is nothing that
  2006. * they can do to advance the grace period. It is therefore
  2007. * safe for us to drop the lock in order to mark the grace
  2008. * period as completed in all of the rcu_node structures.
  2009. */
  2010. raw_spin_unlock_irq_rcu_node(rnp);
  2011. /*
  2012. * Propagate new ->completed value to rcu_node structures so
  2013. * that other CPUs don't have to wait until the start of the next
  2014. * grace period to process their callbacks. This also avoids
  2015. * some nasty RCU grace-period initialization races by forcing
  2016. * the end of the current grace period to be completely recorded in
  2017. * all of the rcu_node structures before the beginning of the next
  2018. * grace period is recorded in any of the rcu_node structures.
  2019. */
  2020. rcu_for_each_node_breadth_first(rsp, rnp) {
  2021. raw_spin_lock_irq_rcu_node(rnp);
  2022. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  2023. WARN_ON_ONCE(rnp->qsmask);
  2024. WRITE_ONCE(rnp->completed, rsp->gpnum);
  2025. rdp = this_cpu_ptr(rsp->rda);
  2026. if (rnp == rdp->mynode)
  2027. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  2028. /* smp_mb() provided by prior unlock-lock pair. */
  2029. nocb += rcu_future_gp_cleanup(rsp, rnp);
  2030. sq = rcu_nocb_gp_get(rnp);
  2031. raw_spin_unlock_irq_rcu_node(rnp);
  2032. rcu_nocb_gp_cleanup(sq);
  2033. cond_resched_rcu_qs();
  2034. WRITE_ONCE(rsp->gp_activity, jiffies);
  2035. rcu_gp_slow(rsp, gp_cleanup_delay);
  2036. }
  2037. rnp = rcu_get_root(rsp);
  2038. raw_spin_lock_irq_rcu_node(rnp); /* Order GP before ->completed update. */
  2039. rcu_nocb_gp_set(rnp, nocb);
  2040. /* Declare grace period done. */
  2041. WRITE_ONCE(rsp->completed, rsp->gpnum);
  2042. trace_rcu_grace_period(rsp->name, rsp->completed, TPS("end"));
  2043. rsp->gp_state = RCU_GP_IDLE;
  2044. rdp = this_cpu_ptr(rsp->rda);
  2045. /* Advance CBs to reduce false positives below. */
  2046. needgp = rcu_advance_cbs(rsp, rnp, rdp) || needgp;
  2047. if (needgp || cpu_needs_another_gp(rsp, rdp)) {
  2048. WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
  2049. trace_rcu_grace_period(rsp->name,
  2050. READ_ONCE(rsp->gpnum),
  2051. TPS("newreq"));
  2052. }
  2053. raw_spin_unlock_irq_rcu_node(rnp);
  2054. }
  2055. /*
  2056. * Body of kthread that handles grace periods.
  2057. */
  2058. static int __noreturn rcu_gp_kthread(void *arg)
  2059. {
  2060. bool first_gp_fqs;
  2061. int gf;
  2062. unsigned long j;
  2063. int ret;
  2064. struct rcu_state *rsp = arg;
  2065. struct rcu_node *rnp = rcu_get_root(rsp);
  2066. rcu_bind_gp_kthread();
  2067. for (;;) {
  2068. /* Handle grace-period start. */
  2069. for (;;) {
  2070. trace_rcu_grace_period(rsp->name,
  2071. READ_ONCE(rsp->gpnum),
  2072. TPS("reqwait"));
  2073. rsp->gp_state = RCU_GP_WAIT_GPS;
  2074. swait_event_interruptible(rsp->gp_wq,
  2075. READ_ONCE(rsp->gp_flags) &
  2076. RCU_GP_FLAG_INIT);
  2077. rsp->gp_state = RCU_GP_DONE_GPS;
  2078. /* Locking provides needed memory barrier. */
  2079. if (rcu_gp_init(rsp))
  2080. break;
  2081. cond_resched_rcu_qs();
  2082. WRITE_ONCE(rsp->gp_activity, jiffies);
  2083. WARN_ON(signal_pending(current));
  2084. trace_rcu_grace_period(rsp->name,
  2085. READ_ONCE(rsp->gpnum),
  2086. TPS("reqwaitsig"));
  2087. }
  2088. /* Handle quiescent-state forcing. */
  2089. first_gp_fqs = true;
  2090. j = jiffies_till_first_fqs;
  2091. if (j > HZ) {
  2092. j = HZ;
  2093. jiffies_till_first_fqs = HZ;
  2094. }
  2095. ret = 0;
  2096. for (;;) {
  2097. if (!ret) {
  2098. rsp->jiffies_force_qs = jiffies + j;
  2099. WRITE_ONCE(rsp->jiffies_kick_kthreads,
  2100. jiffies + 3 * j);
  2101. }
  2102. trace_rcu_grace_period(rsp->name,
  2103. READ_ONCE(rsp->gpnum),
  2104. TPS("fqswait"));
  2105. rsp->gp_state = RCU_GP_WAIT_FQS;
  2106. ret = swait_event_interruptible_timeout(rsp->gp_wq,
  2107. rcu_gp_fqs_check_wake(rsp, &gf), j);
  2108. rsp->gp_state = RCU_GP_DOING_FQS;
  2109. /* Locking provides needed memory barriers. */
  2110. /* If grace period done, leave loop. */
  2111. if (!READ_ONCE(rnp->qsmask) &&
  2112. !rcu_preempt_blocked_readers_cgp(rnp))
  2113. break;
  2114. /* If time for quiescent-state forcing, do it. */
  2115. if (ULONG_CMP_GE(jiffies, rsp->jiffies_force_qs) ||
  2116. (gf & RCU_GP_FLAG_FQS)) {
  2117. trace_rcu_grace_period(rsp->name,
  2118. READ_ONCE(rsp->gpnum),
  2119. TPS("fqsstart"));
  2120. rcu_gp_fqs(rsp, first_gp_fqs);
  2121. first_gp_fqs = false;
  2122. trace_rcu_grace_period(rsp->name,
  2123. READ_ONCE(rsp->gpnum),
  2124. TPS("fqsend"));
  2125. cond_resched_rcu_qs();
  2126. WRITE_ONCE(rsp->gp_activity, jiffies);
  2127. ret = 0; /* Force full wait till next FQS. */
  2128. j = jiffies_till_next_fqs;
  2129. if (j > HZ) {
  2130. j = HZ;
  2131. jiffies_till_next_fqs = HZ;
  2132. } else if (j < 1) {
  2133. j = 1;
  2134. jiffies_till_next_fqs = 1;
  2135. }
  2136. } else {
  2137. /* Deal with stray signal. */
  2138. cond_resched_rcu_qs();
  2139. WRITE_ONCE(rsp->gp_activity, jiffies);
  2140. WARN_ON(signal_pending(current));
  2141. trace_rcu_grace_period(rsp->name,
  2142. READ_ONCE(rsp->gpnum),
  2143. TPS("fqswaitsig"));
  2144. ret = 1; /* Keep old FQS timing. */
  2145. j = jiffies;
  2146. if (time_after(jiffies, rsp->jiffies_force_qs))
  2147. j = 1;
  2148. else
  2149. j = rsp->jiffies_force_qs - j;
  2150. }
  2151. }
  2152. /* Handle grace-period end. */
  2153. rsp->gp_state = RCU_GP_CLEANUP;
  2154. rcu_gp_cleanup(rsp);
  2155. rsp->gp_state = RCU_GP_CLEANED;
  2156. }
  2157. }
  2158. /*
  2159. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  2160. * in preparation for detecting the next grace period. The caller must hold
  2161. * the root node's ->lock and hard irqs must be disabled.
  2162. *
  2163. * Note that it is legal for a dying CPU (which is marked as offline) to
  2164. * invoke this function. This can happen when the dying CPU reports its
  2165. * quiescent state.
  2166. *
  2167. * Returns true if the grace-period kthread must be awakened.
  2168. */
  2169. static bool
  2170. rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  2171. struct rcu_data *rdp)
  2172. {
  2173. if (!rsp->gp_kthread || !cpu_needs_another_gp(rsp, rdp)) {
  2174. /*
  2175. * Either we have not yet spawned the grace-period
  2176. * task, this CPU does not need another grace period,
  2177. * or a grace period is already in progress.
  2178. * Either way, don't start a new grace period.
  2179. */
  2180. return false;
  2181. }
  2182. WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
  2183. trace_rcu_grace_period(rsp->name, READ_ONCE(rsp->gpnum),
  2184. TPS("newreq"));
  2185. /*
  2186. * We can't do wakeups while holding the rnp->lock, as that
  2187. * could cause possible deadlocks with the rq->lock. Defer
  2188. * the wakeup to our caller.
  2189. */
  2190. return true;
  2191. }
  2192. /*
  2193. * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
  2194. * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
  2195. * is invoked indirectly from rcu_advance_cbs(), which would result in
  2196. * endless recursion -- or would do so if it wasn't for the self-deadlock
  2197. * that is encountered beforehand.
  2198. *
  2199. * Returns true if the grace-period kthread needs to be awakened.
  2200. */
  2201. static bool rcu_start_gp(struct rcu_state *rsp)
  2202. {
  2203. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  2204. struct rcu_node *rnp = rcu_get_root(rsp);
  2205. bool ret = false;
  2206. /*
  2207. * If there is no grace period in progress right now, any
  2208. * callbacks we have up to this point will be satisfied by the
  2209. * next grace period. Also, advancing the callbacks reduces the
  2210. * probability of false positives from cpu_needs_another_gp()
  2211. * resulting in pointless grace periods. So, advance callbacks
  2212. * then start the grace period!
  2213. */
  2214. ret = rcu_advance_cbs(rsp, rnp, rdp) || ret;
  2215. ret = rcu_start_gp_advanced(rsp, rnp, rdp) || ret;
  2216. return ret;
  2217. }
  2218. /*
  2219. * Report a full set of quiescent states to the specified rcu_state data
  2220. * structure. Invoke rcu_gp_kthread_wake() to awaken the grace-period
  2221. * kthread if another grace period is required. Whether we wake
  2222. * the grace-period kthread or it awakens itself for the next round
  2223. * of quiescent-state forcing, that kthread will clean up after the
  2224. * just-completed grace period. Note that the caller must hold rnp->lock,
  2225. * which is released before return.
  2226. */
  2227. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  2228. __releases(rcu_get_root(rsp)->lock)
  2229. {
  2230. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  2231. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  2232. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
  2233. rcu_gp_kthread_wake(rsp);
  2234. }
  2235. /*
  2236. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  2237. * Allows quiescent states for a group of CPUs to be reported at one go
  2238. * to the specified rcu_node structure, though all the CPUs in the group
  2239. * must be represented by the same rcu_node structure (which need not be a
  2240. * leaf rcu_node structure, though it often will be). The gps parameter
  2241. * is the grace-period snapshot, which means that the quiescent states
  2242. * are valid only if rnp->gpnum is equal to gps. That structure's lock
  2243. * must be held upon entry, and it is released before return.
  2244. */
  2245. static void
  2246. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  2247. struct rcu_node *rnp, unsigned long gps, unsigned long flags)
  2248. __releases(rnp->lock)
  2249. {
  2250. unsigned long oldmask = 0;
  2251. struct rcu_node *rnp_c;
  2252. /* Walk up the rcu_node hierarchy. */
  2253. for (;;) {
  2254. if (!(rnp->qsmask & mask) || rnp->gpnum != gps) {
  2255. /*
  2256. * Our bit has already been cleared, or the
  2257. * relevant grace period is already over, so done.
  2258. */
  2259. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2260. return;
  2261. }
  2262. WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
  2263. rnp->qsmask &= ~mask;
  2264. trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
  2265. mask, rnp->qsmask, rnp->level,
  2266. rnp->grplo, rnp->grphi,
  2267. !!rnp->gp_tasks);
  2268. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2269. /* Other bits still set at this level, so done. */
  2270. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2271. return;
  2272. }
  2273. mask = rnp->grpmask;
  2274. if (rnp->parent == NULL) {
  2275. /* No more levels. Exit loop holding root lock. */
  2276. break;
  2277. }
  2278. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2279. rnp_c = rnp;
  2280. rnp = rnp->parent;
  2281. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2282. oldmask = rnp_c->qsmask;
  2283. }
  2284. /*
  2285. * Get here if we are the last CPU to pass through a quiescent
  2286. * state for this grace period. Invoke rcu_report_qs_rsp()
  2287. * to clean up and start the next grace period if one is needed.
  2288. */
  2289. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  2290. }
  2291. /*
  2292. * Record a quiescent state for all tasks that were previously queued
  2293. * on the specified rcu_node structure and that were blocking the current
  2294. * RCU grace period. The caller must hold the specified rnp->lock with
  2295. * irqs disabled, and this lock is released upon return, but irqs remain
  2296. * disabled.
  2297. */
  2298. static void rcu_report_unblock_qs_rnp(struct rcu_state *rsp,
  2299. struct rcu_node *rnp, unsigned long flags)
  2300. __releases(rnp->lock)
  2301. {
  2302. unsigned long gps;
  2303. unsigned long mask;
  2304. struct rcu_node *rnp_p;
  2305. if (rcu_state_p == &rcu_sched_state || rsp != rcu_state_p ||
  2306. rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2307. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2308. return; /* Still need more quiescent states! */
  2309. }
  2310. rnp_p = rnp->parent;
  2311. if (rnp_p == NULL) {
  2312. /*
  2313. * Only one rcu_node structure in the tree, so don't
  2314. * try to report up to its nonexistent parent!
  2315. */
  2316. rcu_report_qs_rsp(rsp, flags);
  2317. return;
  2318. }
  2319. /* Report up the rest of the hierarchy, tracking current ->gpnum. */
  2320. gps = rnp->gpnum;
  2321. mask = rnp->grpmask;
  2322. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2323. raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
  2324. rcu_report_qs_rnp(mask, rsp, rnp_p, gps, flags);
  2325. }
  2326. /*
  2327. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  2328. * structure. This must be called from the specified CPU.
  2329. */
  2330. static void
  2331. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
  2332. {
  2333. unsigned long flags;
  2334. unsigned long mask;
  2335. bool needwake;
  2336. struct rcu_node *rnp;
  2337. rnp = rdp->mynode;
  2338. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2339. if (rdp->cpu_no_qs.b.norm || rdp->gpnum != rnp->gpnum ||
  2340. rnp->completed == rnp->gpnum || rdp->gpwrap) {
  2341. /*
  2342. * The grace period in which this quiescent state was
  2343. * recorded has ended, so don't report it upwards.
  2344. * We will instead need a new quiescent state that lies
  2345. * within the current grace period.
  2346. */
  2347. rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
  2348. rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
  2349. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2350. return;
  2351. }
  2352. mask = rdp->grpmask;
  2353. if ((rnp->qsmask & mask) == 0) {
  2354. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2355. } else {
  2356. rdp->core_needs_qs = false;
  2357. /*
  2358. * This GP can't end until cpu checks in, so all of our
  2359. * callbacks can be processed during the next GP.
  2360. */
  2361. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  2362. rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
  2363. /* ^^^ Released rnp->lock */
  2364. if (needwake)
  2365. rcu_gp_kthread_wake(rsp);
  2366. }
  2367. }
  2368. /*
  2369. * Check to see if there is a new grace period of which this CPU
  2370. * is not yet aware, and if so, set up local rcu_data state for it.
  2371. * Otherwise, see if this CPU has just passed through its first
  2372. * quiescent state for this grace period, and record that fact if so.
  2373. */
  2374. static void
  2375. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  2376. {
  2377. /* Check for grace-period ends and beginnings. */
  2378. note_gp_changes(rsp, rdp);
  2379. /*
  2380. * Does this CPU still need to do its part for current grace period?
  2381. * If no, return and let the other CPUs do their part as well.
  2382. */
  2383. if (!rdp->core_needs_qs)
  2384. return;
  2385. /*
  2386. * Was there a quiescent state since the beginning of the grace
  2387. * period? If no, then exit and wait for the next call.
  2388. */
  2389. if (rdp->cpu_no_qs.b.norm)
  2390. return;
  2391. /*
  2392. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  2393. * judge of that).
  2394. */
  2395. rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
  2396. }
  2397. /*
  2398. * Send the specified CPU's RCU callbacks to the orphanage. The
  2399. * specified CPU must be offline, and the caller must hold the
  2400. * ->orphan_lock.
  2401. */
  2402. static void
  2403. rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
  2404. struct rcu_node *rnp, struct rcu_data *rdp)
  2405. {
  2406. /* No-CBs CPUs do not have orphanable callbacks. */
  2407. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) || rcu_is_nocb_cpu(rdp->cpu))
  2408. return;
  2409. /*
  2410. * Orphan the callbacks. First adjust the counts. This is safe
  2411. * because _rcu_barrier() excludes CPU-hotplug operations, so it
  2412. * cannot be running now. Thus no memory barrier is required.
  2413. */
  2414. if (rdp->nxtlist != NULL) {
  2415. rsp->qlen_lazy += rdp->qlen_lazy;
  2416. rsp->qlen += rdp->qlen;
  2417. rdp->n_cbs_orphaned += rdp->qlen;
  2418. rdp->qlen_lazy = 0;
  2419. WRITE_ONCE(rdp->qlen, 0);
  2420. }
  2421. /*
  2422. * Next, move those callbacks still needing a grace period to
  2423. * the orphanage, where some other CPU will pick them up.
  2424. * Some of the callbacks might have gone partway through a grace
  2425. * period, but that is too bad. They get to start over because we
  2426. * cannot assume that grace periods are synchronized across CPUs.
  2427. * We don't bother updating the ->nxttail[] array yet, instead
  2428. * we just reset the whole thing later on.
  2429. */
  2430. if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
  2431. *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
  2432. rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
  2433. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  2434. }
  2435. /*
  2436. * Then move the ready-to-invoke callbacks to the orphanage,
  2437. * where some other CPU will pick them up. These will not be
  2438. * required to pass though another grace period: They are done.
  2439. */
  2440. if (rdp->nxtlist != NULL) {
  2441. *rsp->orphan_donetail = rdp->nxtlist;
  2442. rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
  2443. }
  2444. /*
  2445. * Finally, initialize the rcu_data structure's list to empty and
  2446. * disallow further callbacks on this CPU.
  2447. */
  2448. init_callback_list(rdp);
  2449. rdp->nxttail[RCU_NEXT_TAIL] = NULL;
  2450. }
  2451. /*
  2452. * Adopt the RCU callbacks from the specified rcu_state structure's
  2453. * orphanage. The caller must hold the ->orphan_lock.
  2454. */
  2455. static void rcu_adopt_orphan_cbs(struct rcu_state *rsp, unsigned long flags)
  2456. {
  2457. int i;
  2458. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  2459. /* No-CBs CPUs are handled specially. */
  2460. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
  2461. rcu_nocb_adopt_orphan_cbs(rsp, rdp, flags))
  2462. return;
  2463. /* Do the accounting first. */
  2464. rdp->qlen_lazy += rsp->qlen_lazy;
  2465. rdp->qlen += rsp->qlen;
  2466. rdp->n_cbs_adopted += rsp->qlen;
  2467. if (rsp->qlen_lazy != rsp->qlen)
  2468. rcu_idle_count_callbacks_posted();
  2469. rsp->qlen_lazy = 0;
  2470. rsp->qlen = 0;
  2471. /*
  2472. * We do not need a memory barrier here because the only way we
  2473. * can get here if there is an rcu_barrier() in flight is if
  2474. * we are the task doing the rcu_barrier().
  2475. */
  2476. /* First adopt the ready-to-invoke callbacks. */
  2477. if (rsp->orphan_donelist != NULL) {
  2478. *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
  2479. *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
  2480. for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
  2481. if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
  2482. rdp->nxttail[i] = rsp->orphan_donetail;
  2483. rsp->orphan_donelist = NULL;
  2484. rsp->orphan_donetail = &rsp->orphan_donelist;
  2485. }
  2486. /* And then adopt the callbacks that still need a grace period. */
  2487. if (rsp->orphan_nxtlist != NULL) {
  2488. *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
  2489. rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
  2490. rsp->orphan_nxtlist = NULL;
  2491. rsp->orphan_nxttail = &rsp->orphan_nxtlist;
  2492. }
  2493. }
  2494. /*
  2495. * Trace the fact that this CPU is going offline.
  2496. */
  2497. static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
  2498. {
  2499. RCU_TRACE(unsigned long mask);
  2500. RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
  2501. RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
  2502. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  2503. return;
  2504. RCU_TRACE(mask = rdp->grpmask);
  2505. trace_rcu_grace_period(rsp->name,
  2506. rnp->gpnum + 1 - !!(rnp->qsmask & mask),
  2507. TPS("cpuofl"));
  2508. }
  2509. /*
  2510. * All CPUs for the specified rcu_node structure have gone offline,
  2511. * and all tasks that were preempted within an RCU read-side critical
  2512. * section while running on one of those CPUs have since exited their RCU
  2513. * read-side critical section. Some other CPU is reporting this fact with
  2514. * the specified rcu_node structure's ->lock held and interrupts disabled.
  2515. * This function therefore goes up the tree of rcu_node structures,
  2516. * clearing the corresponding bits in the ->qsmaskinit fields. Note that
  2517. * the leaf rcu_node structure's ->qsmaskinit field has already been
  2518. * updated
  2519. *
  2520. * This function does check that the specified rcu_node structure has
  2521. * all CPUs offline and no blocked tasks, so it is OK to invoke it
  2522. * prematurely. That said, invoking it after the fact will cost you
  2523. * a needless lock acquisition. So once it has done its work, don't
  2524. * invoke it again.
  2525. */
  2526. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
  2527. {
  2528. long mask;
  2529. struct rcu_node *rnp = rnp_leaf;
  2530. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
  2531. rnp->qsmaskinit || rcu_preempt_has_tasks(rnp))
  2532. return;
  2533. for (;;) {
  2534. mask = rnp->grpmask;
  2535. rnp = rnp->parent;
  2536. if (!rnp)
  2537. break;
  2538. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  2539. rnp->qsmaskinit &= ~mask;
  2540. rnp->qsmask &= ~mask;
  2541. if (rnp->qsmaskinit) {
  2542. raw_spin_unlock_rcu_node(rnp);
  2543. /* irqs remain disabled. */
  2544. return;
  2545. }
  2546. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2547. }
  2548. }
  2549. /*
  2550. * The CPU has been completely removed, and some other CPU is reporting
  2551. * this fact from process context. Do the remainder of the cleanup,
  2552. * including orphaning the outgoing CPU's RCU callbacks, and also
  2553. * adopting them. There can only be one CPU hotplug operation at a time,
  2554. * so no other CPU can be attempting to update rcu_cpu_kthread_task.
  2555. */
  2556. static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
  2557. {
  2558. unsigned long flags;
  2559. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  2560. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  2561. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  2562. return;
  2563. /* Adjust any no-longer-needed kthreads. */
  2564. rcu_boost_kthread_setaffinity(rnp, -1);
  2565. /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
  2566. raw_spin_lock_irqsave(&rsp->orphan_lock, flags);
  2567. rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
  2568. rcu_adopt_orphan_cbs(rsp, flags);
  2569. raw_spin_unlock_irqrestore(&rsp->orphan_lock, flags);
  2570. WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
  2571. "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
  2572. cpu, rdp->qlen, rdp->nxtlist);
  2573. }
  2574. /*
  2575. * Invoke any RCU callbacks that have made it to the end of their grace
  2576. * period. Thottle as specified by rdp->blimit.
  2577. */
  2578. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  2579. {
  2580. unsigned long flags;
  2581. struct rcu_head *next, *list, **tail;
  2582. long bl, count, count_lazy;
  2583. int i;
  2584. /* If no callbacks are ready, just return. */
  2585. if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
  2586. trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
  2587. trace_rcu_batch_end(rsp->name, 0, !!READ_ONCE(rdp->nxtlist),
  2588. need_resched(), is_idle_task(current),
  2589. rcu_is_callbacks_kthread());
  2590. return;
  2591. }
  2592. /*
  2593. * Extract the list of ready callbacks, disabling to prevent
  2594. * races with call_rcu() from interrupt handlers.
  2595. */
  2596. local_irq_save(flags);
  2597. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  2598. bl = rdp->blimit;
  2599. trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
  2600. list = rdp->nxtlist;
  2601. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  2602. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  2603. tail = rdp->nxttail[RCU_DONE_TAIL];
  2604. for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
  2605. if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
  2606. rdp->nxttail[i] = &rdp->nxtlist;
  2607. local_irq_restore(flags);
  2608. /* Invoke callbacks. */
  2609. count = count_lazy = 0;
  2610. while (list) {
  2611. next = list->next;
  2612. prefetch(next);
  2613. debug_rcu_head_unqueue(list);
  2614. if (__rcu_reclaim(rsp->name, list))
  2615. count_lazy++;
  2616. list = next;
  2617. /* Stop only if limit reached and CPU has something to do. */
  2618. if (++count >= bl &&
  2619. (need_resched() ||
  2620. (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
  2621. break;
  2622. }
  2623. local_irq_save(flags);
  2624. trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
  2625. is_idle_task(current),
  2626. rcu_is_callbacks_kthread());
  2627. /* Update count, and requeue any remaining callbacks. */
  2628. if (list != NULL) {
  2629. *tail = rdp->nxtlist;
  2630. rdp->nxtlist = list;
  2631. for (i = 0; i < RCU_NEXT_SIZE; i++)
  2632. if (&rdp->nxtlist == rdp->nxttail[i])
  2633. rdp->nxttail[i] = tail;
  2634. else
  2635. break;
  2636. }
  2637. smp_mb(); /* List handling before counting for rcu_barrier(). */
  2638. rdp->qlen_lazy -= count_lazy;
  2639. WRITE_ONCE(rdp->qlen, rdp->qlen - count);
  2640. rdp->n_cbs_invoked += count;
  2641. /* Reinstate batch limit if we have worked down the excess. */
  2642. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  2643. rdp->blimit = blimit;
  2644. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  2645. if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
  2646. rdp->qlen_last_fqs_check = 0;
  2647. rdp->n_force_qs_snap = rsp->n_force_qs;
  2648. } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
  2649. rdp->qlen_last_fqs_check = rdp->qlen;
  2650. WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
  2651. local_irq_restore(flags);
  2652. /* Re-invoke RCU core processing if there are callbacks remaining. */
  2653. if (cpu_has_callbacks_ready_to_invoke(rdp))
  2654. invoke_rcu_core();
  2655. }
  2656. /*
  2657. * Check to see if this CPU is in a non-context-switch quiescent state
  2658. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  2659. * Also schedule RCU core processing.
  2660. *
  2661. * This function must be called from hardirq context. It is normally
  2662. * invoked from the scheduling-clock interrupt.
  2663. */
  2664. void rcu_check_callbacks(int user)
  2665. {
  2666. trace_rcu_utilization(TPS("Start scheduler-tick"));
  2667. increment_cpu_stall_ticks();
  2668. if (user || rcu_is_cpu_rrupt_from_idle()) {
  2669. /*
  2670. * Get here if this CPU took its interrupt from user
  2671. * mode or from the idle loop, and if this is not a
  2672. * nested interrupt. In this case, the CPU is in
  2673. * a quiescent state, so note it.
  2674. *
  2675. * No memory barrier is required here because both
  2676. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  2677. * variables that other CPUs neither access nor modify,
  2678. * at least not while the corresponding CPU is online.
  2679. */
  2680. rcu_sched_qs();
  2681. rcu_bh_qs();
  2682. } else if (!in_softirq()) {
  2683. /*
  2684. * Get here if this CPU did not take its interrupt from
  2685. * softirq, in other words, if it is not interrupting
  2686. * a rcu_bh read-side critical section. This is an _bh
  2687. * critical section, so note it.
  2688. */
  2689. rcu_bh_qs();
  2690. }
  2691. rcu_preempt_check_callbacks();
  2692. if (rcu_pending())
  2693. invoke_rcu_core();
  2694. if (user)
  2695. rcu_note_voluntary_context_switch(current);
  2696. trace_rcu_utilization(TPS("End scheduler-tick"));
  2697. }
  2698. /*
  2699. * Scan the leaf rcu_node structures, processing dyntick state for any that
  2700. * have not yet encountered a quiescent state, using the function specified.
  2701. * Also initiate boosting for any threads blocked on the root rcu_node.
  2702. *
  2703. * The caller must have suppressed start of new grace periods.
  2704. */
  2705. static void force_qs_rnp(struct rcu_state *rsp,
  2706. int (*f)(struct rcu_data *rsp, bool *isidle,
  2707. unsigned long *maxj),
  2708. bool *isidle, unsigned long *maxj)
  2709. {
  2710. int cpu;
  2711. unsigned long flags;
  2712. unsigned long mask;
  2713. struct rcu_node *rnp;
  2714. rcu_for_each_leaf_node(rsp, rnp) {
  2715. cond_resched_rcu_qs();
  2716. mask = 0;
  2717. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2718. if (rnp->qsmask == 0) {
  2719. if (rcu_state_p == &rcu_sched_state ||
  2720. rsp != rcu_state_p ||
  2721. rcu_preempt_blocked_readers_cgp(rnp)) {
  2722. /*
  2723. * No point in scanning bits because they
  2724. * are all zero. But we might need to
  2725. * priority-boost blocked readers.
  2726. */
  2727. rcu_initiate_boost(rnp, flags);
  2728. /* rcu_initiate_boost() releases rnp->lock */
  2729. continue;
  2730. }
  2731. if (rnp->parent &&
  2732. (rnp->parent->qsmask & rnp->grpmask)) {
  2733. /*
  2734. * Race between grace-period
  2735. * initialization and task exiting RCU
  2736. * read-side critical section: Report.
  2737. */
  2738. rcu_report_unblock_qs_rnp(rsp, rnp, flags);
  2739. /* rcu_report_unblock_qs_rnp() rlses ->lock */
  2740. continue;
  2741. }
  2742. }
  2743. for_each_leaf_node_possible_cpu(rnp, cpu) {
  2744. unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
  2745. if ((rnp->qsmask & bit) != 0) {
  2746. if (f(per_cpu_ptr(rsp->rda, cpu), isidle, maxj))
  2747. mask |= bit;
  2748. }
  2749. }
  2750. if (mask != 0) {
  2751. /* Idle/offline CPUs, report (releases rnp->lock. */
  2752. rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
  2753. } else {
  2754. /* Nothing to do here, so just drop the lock. */
  2755. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2756. }
  2757. }
  2758. }
  2759. /*
  2760. * Force quiescent states on reluctant CPUs, and also detect which
  2761. * CPUs are in dyntick-idle mode.
  2762. */
  2763. static void force_quiescent_state(struct rcu_state *rsp)
  2764. {
  2765. unsigned long flags;
  2766. bool ret;
  2767. struct rcu_node *rnp;
  2768. struct rcu_node *rnp_old = NULL;
  2769. /* Funnel through hierarchy to reduce memory contention. */
  2770. rnp = __this_cpu_read(rsp->rda->mynode);
  2771. for (; rnp != NULL; rnp = rnp->parent) {
  2772. ret = (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
  2773. !raw_spin_trylock(&rnp->fqslock);
  2774. if (rnp_old != NULL)
  2775. raw_spin_unlock(&rnp_old->fqslock);
  2776. if (ret) {
  2777. rsp->n_force_qs_lh++;
  2778. return;
  2779. }
  2780. rnp_old = rnp;
  2781. }
  2782. /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
  2783. /* Reached the root of the rcu_node tree, acquire lock. */
  2784. raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
  2785. raw_spin_unlock(&rnp_old->fqslock);
  2786. if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  2787. rsp->n_force_qs_lh++;
  2788. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2789. return; /* Someone beat us to it. */
  2790. }
  2791. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  2792. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2793. rcu_gp_kthread_wake(rsp);
  2794. }
  2795. /*
  2796. * This does the RCU core processing work for the specified rcu_state
  2797. * and rcu_data structures. This may be called only from the CPU to
  2798. * whom the rdp belongs.
  2799. */
  2800. static void
  2801. __rcu_process_callbacks(struct rcu_state *rsp)
  2802. {
  2803. unsigned long flags;
  2804. bool needwake;
  2805. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  2806. WARN_ON_ONCE(rdp->beenonline == 0);
  2807. /* Update RCU state based on any recent quiescent states. */
  2808. rcu_check_quiescent_state(rsp, rdp);
  2809. /* Does this CPU require a not-yet-started grace period? */
  2810. local_irq_save(flags);
  2811. if (cpu_needs_another_gp(rsp, rdp)) {
  2812. raw_spin_lock_rcu_node(rcu_get_root(rsp)); /* irqs disabled. */
  2813. needwake = rcu_start_gp(rsp);
  2814. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
  2815. if (needwake)
  2816. rcu_gp_kthread_wake(rsp);
  2817. } else {
  2818. local_irq_restore(flags);
  2819. }
  2820. /* If there are callbacks ready, invoke them. */
  2821. if (cpu_has_callbacks_ready_to_invoke(rdp))
  2822. invoke_rcu_callbacks(rsp, rdp);
  2823. /* Do any needed deferred wakeups of rcuo kthreads. */
  2824. do_nocb_deferred_wakeup(rdp);
  2825. }
  2826. /*
  2827. * Do RCU core processing for the current CPU.
  2828. */
  2829. static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
  2830. {
  2831. struct rcu_state *rsp;
  2832. if (cpu_is_offline(smp_processor_id()))
  2833. return;
  2834. trace_rcu_utilization(TPS("Start RCU core"));
  2835. for_each_rcu_flavor(rsp)
  2836. __rcu_process_callbacks(rsp);
  2837. trace_rcu_utilization(TPS("End RCU core"));
  2838. }
  2839. /*
  2840. * Schedule RCU callback invocation. If the specified type of RCU
  2841. * does not support RCU priority boosting, just do a direct call,
  2842. * otherwise wake up the per-CPU kernel kthread. Note that because we
  2843. * are running on the current CPU with softirqs disabled, the
  2844. * rcu_cpu_kthread_task cannot disappear out from under us.
  2845. */
  2846. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  2847. {
  2848. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  2849. return;
  2850. if (likely(!rsp->boost)) {
  2851. rcu_do_batch(rsp, rdp);
  2852. return;
  2853. }
  2854. invoke_rcu_callbacks_kthread();
  2855. }
  2856. static void invoke_rcu_core(void)
  2857. {
  2858. if (cpu_online(smp_processor_id()))
  2859. raise_softirq(RCU_SOFTIRQ);
  2860. }
  2861. /*
  2862. * Handle any core-RCU processing required by a call_rcu() invocation.
  2863. */
  2864. static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
  2865. struct rcu_head *head, unsigned long flags)
  2866. {
  2867. bool needwake;
  2868. /*
  2869. * If called from an extended quiescent state, invoke the RCU
  2870. * core in order to force a re-evaluation of RCU's idleness.
  2871. */
  2872. if (!rcu_is_watching())
  2873. invoke_rcu_core();
  2874. /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
  2875. if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
  2876. return;
  2877. /*
  2878. * Force the grace period if too many callbacks or too long waiting.
  2879. * Enforce hysteresis, and don't invoke force_quiescent_state()
  2880. * if some other CPU has recently done so. Also, don't bother
  2881. * invoking force_quiescent_state() if the newly enqueued callback
  2882. * is the only one waiting for a grace period to complete.
  2883. */
  2884. if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
  2885. /* Are we ignoring a completed grace period? */
  2886. note_gp_changes(rsp, rdp);
  2887. /* Start a new grace period if one not already started. */
  2888. if (!rcu_gp_in_progress(rsp)) {
  2889. struct rcu_node *rnp_root = rcu_get_root(rsp);
  2890. raw_spin_lock_rcu_node(rnp_root);
  2891. needwake = rcu_start_gp(rsp);
  2892. raw_spin_unlock_rcu_node(rnp_root);
  2893. if (needwake)
  2894. rcu_gp_kthread_wake(rsp);
  2895. } else {
  2896. /* Give the grace period a kick. */
  2897. rdp->blimit = LONG_MAX;
  2898. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  2899. *rdp->nxttail[RCU_DONE_TAIL] != head)
  2900. force_quiescent_state(rsp);
  2901. rdp->n_force_qs_snap = rsp->n_force_qs;
  2902. rdp->qlen_last_fqs_check = rdp->qlen;
  2903. }
  2904. }
  2905. }
  2906. /*
  2907. * RCU callback function to leak a callback.
  2908. */
  2909. static void rcu_leak_callback(struct rcu_head *rhp)
  2910. {
  2911. }
  2912. /*
  2913. * Helper function for call_rcu() and friends. The cpu argument will
  2914. * normally be -1, indicating "currently running CPU". It may specify
  2915. * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
  2916. * is expected to specify a CPU.
  2917. */
  2918. static void
  2919. __call_rcu(struct rcu_head *head, rcu_callback_t func,
  2920. struct rcu_state *rsp, int cpu, bool lazy)
  2921. {
  2922. unsigned long flags;
  2923. struct rcu_data *rdp;
  2924. /* Misaligned rcu_head! */
  2925. WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
  2926. if (debug_rcu_head_queue(head)) {
  2927. /* Probable double call_rcu(), so leak the callback. */
  2928. WRITE_ONCE(head->func, rcu_leak_callback);
  2929. WARN_ONCE(1, "__call_rcu(): Leaked duplicate callback\n");
  2930. return;
  2931. }
  2932. head->func = func;
  2933. head->next = NULL;
  2934. local_irq_save(flags);
  2935. rdp = this_cpu_ptr(rsp->rda);
  2936. /* Add the callback to our list. */
  2937. if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL) || cpu != -1) {
  2938. int offline;
  2939. if (cpu != -1)
  2940. rdp = per_cpu_ptr(rsp->rda, cpu);
  2941. if (likely(rdp->mynode)) {
  2942. /* Post-boot, so this should be for a no-CBs CPU. */
  2943. offline = !__call_rcu_nocb(rdp, head, lazy, flags);
  2944. WARN_ON_ONCE(offline);
  2945. /* Offline CPU, _call_rcu() illegal, leak callback. */
  2946. local_irq_restore(flags);
  2947. return;
  2948. }
  2949. /*
  2950. * Very early boot, before rcu_init(). Initialize if needed
  2951. * and then drop through to queue the callback.
  2952. */
  2953. BUG_ON(cpu != -1);
  2954. WARN_ON_ONCE(!rcu_is_watching());
  2955. if (!likely(rdp->nxtlist))
  2956. init_default_callback_list(rdp);
  2957. }
  2958. WRITE_ONCE(rdp->qlen, rdp->qlen + 1);
  2959. if (lazy)
  2960. rdp->qlen_lazy++;
  2961. else
  2962. rcu_idle_count_callbacks_posted();
  2963. smp_mb(); /* Count before adding callback for rcu_barrier(). */
  2964. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  2965. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  2966. if (__is_kfree_rcu_offset((unsigned long)func))
  2967. trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
  2968. rdp->qlen_lazy, rdp->qlen);
  2969. else
  2970. trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
  2971. /* Go handle any RCU core processing required. */
  2972. __call_rcu_core(rsp, rdp, head, flags);
  2973. local_irq_restore(flags);
  2974. }
  2975. /*
  2976. * Queue an RCU-sched callback for invocation after a grace period.
  2977. */
  2978. void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
  2979. {
  2980. __call_rcu(head, func, &rcu_sched_state, -1, 0);
  2981. }
  2982. EXPORT_SYMBOL_GPL(call_rcu_sched);
  2983. /*
  2984. * Queue an RCU callback for invocation after a quicker grace period.
  2985. */
  2986. void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
  2987. {
  2988. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  2989. }
  2990. EXPORT_SYMBOL_GPL(call_rcu_bh);
  2991. /*
  2992. * Queue an RCU callback for lazy invocation after a grace period.
  2993. * This will likely be later named something like "call_rcu_lazy()",
  2994. * but this change will require some way of tagging the lazy RCU
  2995. * callbacks in the list of pending callbacks. Until then, this
  2996. * function may only be called from __kfree_rcu().
  2997. */
  2998. void kfree_call_rcu(struct rcu_head *head,
  2999. rcu_callback_t func)
  3000. {
  3001. __call_rcu(head, func, rcu_state_p, -1, 1);
  3002. }
  3003. EXPORT_SYMBOL_GPL(kfree_call_rcu);
  3004. /*
  3005. * Because a context switch is a grace period for RCU-sched and RCU-bh,
  3006. * any blocking grace-period wait automatically implies a grace period
  3007. * if there is only one CPU online at any point time during execution
  3008. * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
  3009. * occasionally incorrectly indicate that there are multiple CPUs online
  3010. * when there was in fact only one the whole time, as this just adds
  3011. * some overhead: RCU still operates correctly.
  3012. */
  3013. static inline int rcu_blocking_is_gp(void)
  3014. {
  3015. int ret;
  3016. might_sleep(); /* Check for RCU read-side critical section. */
  3017. preempt_disable();
  3018. ret = num_online_cpus() <= 1;
  3019. preempt_enable();
  3020. return ret;
  3021. }
  3022. /**
  3023. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  3024. *
  3025. * Control will return to the caller some time after a full rcu-sched
  3026. * grace period has elapsed, in other words after all currently executing
  3027. * rcu-sched read-side critical sections have completed. These read-side
  3028. * critical sections are delimited by rcu_read_lock_sched() and
  3029. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  3030. * local_irq_disable(), and so on may be used in place of
  3031. * rcu_read_lock_sched().
  3032. *
  3033. * This means that all preempt_disable code sequences, including NMI and
  3034. * non-threaded hardware-interrupt handlers, in progress on entry will
  3035. * have completed before this primitive returns. However, this does not
  3036. * guarantee that softirq handlers will have completed, since in some
  3037. * kernels, these handlers can run in process context, and can block.
  3038. *
  3039. * Note that this guarantee implies further memory-ordering guarantees.
  3040. * On systems with more than one CPU, when synchronize_sched() returns,
  3041. * each CPU is guaranteed to have executed a full memory barrier since the
  3042. * end of its last RCU-sched read-side critical section whose beginning
  3043. * preceded the call to synchronize_sched(). In addition, each CPU having
  3044. * an RCU read-side critical section that extends beyond the return from
  3045. * synchronize_sched() is guaranteed to have executed a full memory barrier
  3046. * after the beginning of synchronize_sched() and before the beginning of
  3047. * that RCU read-side critical section. Note that these guarantees include
  3048. * CPUs that are offline, idle, or executing in user mode, as well as CPUs
  3049. * that are executing in the kernel.
  3050. *
  3051. * Furthermore, if CPU A invoked synchronize_sched(), which returned
  3052. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  3053. * to have executed a full memory barrier during the execution of
  3054. * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
  3055. * again only if the system has more than one CPU).
  3056. *
  3057. * This primitive provides the guarantees made by the (now removed)
  3058. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  3059. * guarantees that rcu_read_lock() sections will have completed.
  3060. * In "classic RCU", these two guarantees happen to be one and
  3061. * the same, but can differ in realtime RCU implementations.
  3062. */
  3063. void synchronize_sched(void)
  3064. {
  3065. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  3066. lock_is_held(&rcu_lock_map) ||
  3067. lock_is_held(&rcu_sched_lock_map),
  3068. "Illegal synchronize_sched() in RCU-sched read-side critical section");
  3069. if (rcu_blocking_is_gp())
  3070. return;
  3071. if (rcu_gp_is_expedited())
  3072. synchronize_sched_expedited();
  3073. else
  3074. wait_rcu_gp(call_rcu_sched);
  3075. }
  3076. EXPORT_SYMBOL_GPL(synchronize_sched);
  3077. /**
  3078. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  3079. *
  3080. * Control will return to the caller some time after a full rcu_bh grace
  3081. * period has elapsed, in other words after all currently executing rcu_bh
  3082. * read-side critical sections have completed. RCU read-side critical
  3083. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  3084. * and may be nested.
  3085. *
  3086. * See the description of synchronize_sched() for more detailed information
  3087. * on memory ordering guarantees.
  3088. */
  3089. void synchronize_rcu_bh(void)
  3090. {
  3091. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  3092. lock_is_held(&rcu_lock_map) ||
  3093. lock_is_held(&rcu_sched_lock_map),
  3094. "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
  3095. if (rcu_blocking_is_gp())
  3096. return;
  3097. if (rcu_gp_is_expedited())
  3098. synchronize_rcu_bh_expedited();
  3099. else
  3100. wait_rcu_gp(call_rcu_bh);
  3101. }
  3102. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  3103. /**
  3104. * get_state_synchronize_rcu - Snapshot current RCU state
  3105. *
  3106. * Returns a cookie that is used by a later call to cond_synchronize_rcu()
  3107. * to determine whether or not a full grace period has elapsed in the
  3108. * meantime.
  3109. */
  3110. unsigned long get_state_synchronize_rcu(void)
  3111. {
  3112. /*
  3113. * Any prior manipulation of RCU-protected data must happen
  3114. * before the load from ->gpnum.
  3115. */
  3116. smp_mb(); /* ^^^ */
  3117. /*
  3118. * Make sure this load happens before the purportedly
  3119. * time-consuming work between get_state_synchronize_rcu()
  3120. * and cond_synchronize_rcu().
  3121. */
  3122. return smp_load_acquire(&rcu_state_p->gpnum);
  3123. }
  3124. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
  3125. /**
  3126. * cond_synchronize_rcu - Conditionally wait for an RCU grace period
  3127. *
  3128. * @oldstate: return value from earlier call to get_state_synchronize_rcu()
  3129. *
  3130. * If a full RCU grace period has elapsed since the earlier call to
  3131. * get_state_synchronize_rcu(), just return. Otherwise, invoke
  3132. * synchronize_rcu() to wait for a full grace period.
  3133. *
  3134. * Yes, this function does not take counter wrap into account. But
  3135. * counter wrap is harmless. If the counter wraps, we have waited for
  3136. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3137. * so waiting for one additional grace period should be just fine.
  3138. */
  3139. void cond_synchronize_rcu(unsigned long oldstate)
  3140. {
  3141. unsigned long newstate;
  3142. /*
  3143. * Ensure that this load happens before any RCU-destructive
  3144. * actions the caller might carry out after we return.
  3145. */
  3146. newstate = smp_load_acquire(&rcu_state_p->completed);
  3147. if (ULONG_CMP_GE(oldstate, newstate))
  3148. synchronize_rcu();
  3149. }
  3150. EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
  3151. /**
  3152. * get_state_synchronize_sched - Snapshot current RCU-sched state
  3153. *
  3154. * Returns a cookie that is used by a later call to cond_synchronize_sched()
  3155. * to determine whether or not a full grace period has elapsed in the
  3156. * meantime.
  3157. */
  3158. unsigned long get_state_synchronize_sched(void)
  3159. {
  3160. /*
  3161. * Any prior manipulation of RCU-protected data must happen
  3162. * before the load from ->gpnum.
  3163. */
  3164. smp_mb(); /* ^^^ */
  3165. /*
  3166. * Make sure this load happens before the purportedly
  3167. * time-consuming work between get_state_synchronize_sched()
  3168. * and cond_synchronize_sched().
  3169. */
  3170. return smp_load_acquire(&rcu_sched_state.gpnum);
  3171. }
  3172. EXPORT_SYMBOL_GPL(get_state_synchronize_sched);
  3173. /**
  3174. * cond_synchronize_sched - Conditionally wait for an RCU-sched grace period
  3175. *
  3176. * @oldstate: return value from earlier call to get_state_synchronize_sched()
  3177. *
  3178. * If a full RCU-sched grace period has elapsed since the earlier call to
  3179. * get_state_synchronize_sched(), just return. Otherwise, invoke
  3180. * synchronize_sched() to wait for a full grace period.
  3181. *
  3182. * Yes, this function does not take counter wrap into account. But
  3183. * counter wrap is harmless. If the counter wraps, we have waited for
  3184. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3185. * so waiting for one additional grace period should be just fine.
  3186. */
  3187. void cond_synchronize_sched(unsigned long oldstate)
  3188. {
  3189. unsigned long newstate;
  3190. /*
  3191. * Ensure that this load happens before any RCU-destructive
  3192. * actions the caller might carry out after we return.
  3193. */
  3194. newstate = smp_load_acquire(&rcu_sched_state.completed);
  3195. if (ULONG_CMP_GE(oldstate, newstate))
  3196. synchronize_sched();
  3197. }
  3198. EXPORT_SYMBOL_GPL(cond_synchronize_sched);
  3199. /* Adjust sequence number for start of update-side operation. */
  3200. static void rcu_seq_start(unsigned long *sp)
  3201. {
  3202. WRITE_ONCE(*sp, *sp + 1);
  3203. smp_mb(); /* Ensure update-side operation after counter increment. */
  3204. WARN_ON_ONCE(!(*sp & 0x1));
  3205. }
  3206. /* Adjust sequence number for end of update-side operation. */
  3207. static void rcu_seq_end(unsigned long *sp)
  3208. {
  3209. smp_mb(); /* Ensure update-side operation before counter increment. */
  3210. WRITE_ONCE(*sp, *sp + 1);
  3211. WARN_ON_ONCE(*sp & 0x1);
  3212. }
  3213. /* Take a snapshot of the update side's sequence number. */
  3214. static unsigned long rcu_seq_snap(unsigned long *sp)
  3215. {
  3216. unsigned long s;
  3217. s = (READ_ONCE(*sp) + 3) & ~0x1;
  3218. smp_mb(); /* Above access must not bleed into critical section. */
  3219. return s;
  3220. }
  3221. /*
  3222. * Given a snapshot from rcu_seq_snap(), determine whether or not a
  3223. * full update-side operation has occurred.
  3224. */
  3225. static bool rcu_seq_done(unsigned long *sp, unsigned long s)
  3226. {
  3227. return ULONG_CMP_GE(READ_ONCE(*sp), s);
  3228. }
  3229. /*
  3230. * Check to see if there is any immediate RCU-related work to be done
  3231. * by the current CPU, for the specified type of RCU, returning 1 if so.
  3232. * The checks are in order of increasing expense: checks that can be
  3233. * carried out against CPU-local state are performed first. However,
  3234. * we must check for CPU stalls first, else we might not get a chance.
  3235. */
  3236. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  3237. {
  3238. struct rcu_node *rnp = rdp->mynode;
  3239. rdp->n_rcu_pending++;
  3240. /* Check for CPU stalls, if enabled. */
  3241. check_cpu_stall(rsp, rdp);
  3242. /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
  3243. if (rcu_nohz_full_cpu(rsp))
  3244. return 0;
  3245. /* Is the RCU core waiting for a quiescent state from this CPU? */
  3246. if (rcu_scheduler_fully_active &&
  3247. rdp->core_needs_qs && rdp->cpu_no_qs.b.norm &&
  3248. rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) {
  3249. rdp->n_rp_core_needs_qs++;
  3250. } else if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm) {
  3251. rdp->n_rp_report_qs++;
  3252. return 1;
  3253. }
  3254. /* Does this CPU have callbacks ready to invoke? */
  3255. if (cpu_has_callbacks_ready_to_invoke(rdp)) {
  3256. rdp->n_rp_cb_ready++;
  3257. return 1;
  3258. }
  3259. /* Has RCU gone idle with this CPU needing another grace period? */
  3260. if (cpu_needs_another_gp(rsp, rdp)) {
  3261. rdp->n_rp_cpu_needs_gp++;
  3262. return 1;
  3263. }
  3264. /* Has another RCU grace period completed? */
  3265. if (READ_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
  3266. rdp->n_rp_gp_completed++;
  3267. return 1;
  3268. }
  3269. /* Has a new RCU grace period started? */
  3270. if (READ_ONCE(rnp->gpnum) != rdp->gpnum ||
  3271. unlikely(READ_ONCE(rdp->gpwrap))) { /* outside lock */
  3272. rdp->n_rp_gp_started++;
  3273. return 1;
  3274. }
  3275. /* Does this CPU need a deferred NOCB wakeup? */
  3276. if (rcu_nocb_need_deferred_wakeup(rdp)) {
  3277. rdp->n_rp_nocb_defer_wakeup++;
  3278. return 1;
  3279. }
  3280. /* nothing to do */
  3281. rdp->n_rp_need_nothing++;
  3282. return 0;
  3283. }
  3284. /*
  3285. * Check to see if there is any immediate RCU-related work to be done
  3286. * by the current CPU, returning 1 if so. This function is part of the
  3287. * RCU implementation; it is -not- an exported member of the RCU API.
  3288. */
  3289. static int rcu_pending(void)
  3290. {
  3291. struct rcu_state *rsp;
  3292. for_each_rcu_flavor(rsp)
  3293. if (__rcu_pending(rsp, this_cpu_ptr(rsp->rda)))
  3294. return 1;
  3295. return 0;
  3296. }
  3297. /*
  3298. * Return true if the specified CPU has any callback. If all_lazy is
  3299. * non-NULL, store an indication of whether all callbacks are lazy.
  3300. * (If there are no callbacks, all of them are deemed to be lazy.)
  3301. */
  3302. static bool __maybe_unused rcu_cpu_has_callbacks(bool *all_lazy)
  3303. {
  3304. bool al = true;
  3305. bool hc = false;
  3306. struct rcu_data *rdp;
  3307. struct rcu_state *rsp;
  3308. for_each_rcu_flavor(rsp) {
  3309. rdp = this_cpu_ptr(rsp->rda);
  3310. if (!rdp->nxtlist)
  3311. continue;
  3312. hc = true;
  3313. if (rdp->qlen != rdp->qlen_lazy || !all_lazy) {
  3314. al = false;
  3315. break;
  3316. }
  3317. }
  3318. if (all_lazy)
  3319. *all_lazy = al;
  3320. return hc;
  3321. }
  3322. /*
  3323. * Helper function for _rcu_barrier() tracing. If tracing is disabled,
  3324. * the compiler is expected to optimize this away.
  3325. */
  3326. static void _rcu_barrier_trace(struct rcu_state *rsp, const char *s,
  3327. int cpu, unsigned long done)
  3328. {
  3329. trace_rcu_barrier(rsp->name, s, cpu,
  3330. atomic_read(&rsp->barrier_cpu_count), done);
  3331. }
  3332. /*
  3333. * RCU callback function for _rcu_barrier(). If we are last, wake
  3334. * up the task executing _rcu_barrier().
  3335. */
  3336. static void rcu_barrier_callback(struct rcu_head *rhp)
  3337. {
  3338. struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
  3339. struct rcu_state *rsp = rdp->rsp;
  3340. if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
  3341. _rcu_barrier_trace(rsp, "LastCB", -1, rsp->barrier_sequence);
  3342. complete(&rsp->barrier_completion);
  3343. } else {
  3344. _rcu_barrier_trace(rsp, "CB", -1, rsp->barrier_sequence);
  3345. }
  3346. }
  3347. /*
  3348. * Called with preemption disabled, and from cross-cpu IRQ context.
  3349. */
  3350. static void rcu_barrier_func(void *type)
  3351. {
  3352. struct rcu_state *rsp = type;
  3353. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  3354. _rcu_barrier_trace(rsp, "IRQ", -1, rsp->barrier_sequence);
  3355. atomic_inc(&rsp->barrier_cpu_count);
  3356. rsp->call(&rdp->barrier_head, rcu_barrier_callback);
  3357. }
  3358. /*
  3359. * Orchestrate the specified type of RCU barrier, waiting for all
  3360. * RCU callbacks of the specified type to complete.
  3361. */
  3362. static void _rcu_barrier(struct rcu_state *rsp)
  3363. {
  3364. int cpu;
  3365. struct rcu_data *rdp;
  3366. unsigned long s = rcu_seq_snap(&rsp->barrier_sequence);
  3367. _rcu_barrier_trace(rsp, "Begin", -1, s);
  3368. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  3369. mutex_lock(&rsp->barrier_mutex);
  3370. /* Did someone else do our work for us? */
  3371. if (rcu_seq_done(&rsp->barrier_sequence, s)) {
  3372. _rcu_barrier_trace(rsp, "EarlyExit", -1, rsp->barrier_sequence);
  3373. smp_mb(); /* caller's subsequent code after above check. */
  3374. mutex_unlock(&rsp->barrier_mutex);
  3375. return;
  3376. }
  3377. /* Mark the start of the barrier operation. */
  3378. rcu_seq_start(&rsp->barrier_sequence);
  3379. _rcu_barrier_trace(rsp, "Inc1", -1, rsp->barrier_sequence);
  3380. /*
  3381. * Initialize the count to one rather than to zero in order to
  3382. * avoid a too-soon return to zero in case of a short grace period
  3383. * (or preemption of this task). Exclude CPU-hotplug operations
  3384. * to ensure that no offline CPU has callbacks queued.
  3385. */
  3386. init_completion(&rsp->barrier_completion);
  3387. atomic_set(&rsp->barrier_cpu_count, 1);
  3388. get_online_cpus();
  3389. /*
  3390. * Force each CPU with callbacks to register a new callback.
  3391. * When that callback is invoked, we will know that all of the
  3392. * corresponding CPU's preceding callbacks have been invoked.
  3393. */
  3394. for_each_possible_cpu(cpu) {
  3395. if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
  3396. continue;
  3397. rdp = per_cpu_ptr(rsp->rda, cpu);
  3398. if (rcu_is_nocb_cpu(cpu)) {
  3399. if (!rcu_nocb_cpu_needs_barrier(rsp, cpu)) {
  3400. _rcu_barrier_trace(rsp, "OfflineNoCB", cpu,
  3401. rsp->barrier_sequence);
  3402. } else {
  3403. _rcu_barrier_trace(rsp, "OnlineNoCB", cpu,
  3404. rsp->barrier_sequence);
  3405. smp_mb__before_atomic();
  3406. atomic_inc(&rsp->barrier_cpu_count);
  3407. __call_rcu(&rdp->barrier_head,
  3408. rcu_barrier_callback, rsp, cpu, 0);
  3409. }
  3410. } else if (READ_ONCE(rdp->qlen)) {
  3411. _rcu_barrier_trace(rsp, "OnlineQ", cpu,
  3412. rsp->barrier_sequence);
  3413. smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
  3414. } else {
  3415. _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
  3416. rsp->barrier_sequence);
  3417. }
  3418. }
  3419. put_online_cpus();
  3420. /*
  3421. * Now that we have an rcu_barrier_callback() callback on each
  3422. * CPU, and thus each counted, remove the initial count.
  3423. */
  3424. if (atomic_dec_and_test(&rsp->barrier_cpu_count))
  3425. complete(&rsp->barrier_completion);
  3426. /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
  3427. wait_for_completion(&rsp->barrier_completion);
  3428. /* Mark the end of the barrier operation. */
  3429. _rcu_barrier_trace(rsp, "Inc2", -1, rsp->barrier_sequence);
  3430. rcu_seq_end(&rsp->barrier_sequence);
  3431. /* Other rcu_barrier() invocations can now safely proceed. */
  3432. mutex_unlock(&rsp->barrier_mutex);
  3433. }
  3434. /**
  3435. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  3436. */
  3437. void rcu_barrier_bh(void)
  3438. {
  3439. _rcu_barrier(&rcu_bh_state);
  3440. }
  3441. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  3442. /**
  3443. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  3444. */
  3445. void rcu_barrier_sched(void)
  3446. {
  3447. _rcu_barrier(&rcu_sched_state);
  3448. }
  3449. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  3450. /*
  3451. * Propagate ->qsinitmask bits up the rcu_node tree to account for the
  3452. * first CPU in a given leaf rcu_node structure coming online. The caller
  3453. * must hold the corresponding leaf rcu_node ->lock with interrrupts
  3454. * disabled.
  3455. */
  3456. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
  3457. {
  3458. long mask;
  3459. struct rcu_node *rnp = rnp_leaf;
  3460. for (;;) {
  3461. mask = rnp->grpmask;
  3462. rnp = rnp->parent;
  3463. if (rnp == NULL)
  3464. return;
  3465. raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
  3466. rnp->qsmaskinit |= mask;
  3467. raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
  3468. }
  3469. }
  3470. /*
  3471. * Do boot-time initialization of a CPU's per-CPU RCU data.
  3472. */
  3473. static void __init
  3474. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  3475. {
  3476. unsigned long flags;
  3477. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3478. struct rcu_node *rnp = rcu_get_root(rsp);
  3479. /* Set up local state, ensuring consistent view of global state. */
  3480. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3481. rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
  3482. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  3483. WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
  3484. WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp->dynticks)));
  3485. rdp->cpu = cpu;
  3486. rdp->rsp = rsp;
  3487. rcu_boot_init_nocb_percpu_data(rdp);
  3488. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3489. }
  3490. /*
  3491. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  3492. * offline event can be happening at a given time. Note also that we
  3493. * can accept some slop in the rsp->completed access due to the fact
  3494. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  3495. */
  3496. static void
  3497. rcu_init_percpu_data(int cpu, struct rcu_state *rsp)
  3498. {
  3499. unsigned long flags;
  3500. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3501. struct rcu_node *rnp = rcu_get_root(rsp);
  3502. /* Set up local state, ensuring consistent view of global state. */
  3503. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3504. rdp->qlen_last_fqs_check = 0;
  3505. rdp->n_force_qs_snap = rsp->n_force_qs;
  3506. rdp->blimit = blimit;
  3507. if (!rdp->nxtlist)
  3508. init_callback_list(rdp); /* Re-enable callbacks on this CPU. */
  3509. rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
  3510. rcu_sysidle_init_percpu_data(rdp->dynticks);
  3511. rcu_dynticks_eqs_online();
  3512. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  3513. /*
  3514. * Add CPU to leaf rcu_node pending-online bitmask. Any needed
  3515. * propagation up the rcu_node tree will happen at the beginning
  3516. * of the next grace period.
  3517. */
  3518. rnp = rdp->mynode;
  3519. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  3520. if (!rdp->beenonline)
  3521. WRITE_ONCE(rsp->ncpus, READ_ONCE(rsp->ncpus) + 1);
  3522. rdp->beenonline = true; /* We have now been online. */
  3523. rdp->gpnum = rnp->completed; /* Make CPU later note any new GP. */
  3524. rdp->completed = rnp->completed;
  3525. rdp->cpu_no_qs.b.norm = true;
  3526. rdp->rcu_qs_ctr_snap = per_cpu(rcu_qs_ctr, cpu);
  3527. rdp->core_needs_qs = false;
  3528. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuonl"));
  3529. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3530. }
  3531. int rcutree_prepare_cpu(unsigned int cpu)
  3532. {
  3533. struct rcu_state *rsp;
  3534. for_each_rcu_flavor(rsp)
  3535. rcu_init_percpu_data(cpu, rsp);
  3536. rcu_prepare_kthreads(cpu);
  3537. rcu_spawn_all_nocb_kthreads(cpu);
  3538. return 0;
  3539. }
  3540. static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
  3541. {
  3542. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  3543. rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
  3544. }
  3545. int rcutree_online_cpu(unsigned int cpu)
  3546. {
  3547. sync_sched_exp_online_cleanup(cpu);
  3548. rcutree_affinity_setting(cpu, -1);
  3549. return 0;
  3550. }
  3551. int rcutree_offline_cpu(unsigned int cpu)
  3552. {
  3553. rcutree_affinity_setting(cpu, cpu);
  3554. return 0;
  3555. }
  3556. int rcutree_dying_cpu(unsigned int cpu)
  3557. {
  3558. struct rcu_state *rsp;
  3559. for_each_rcu_flavor(rsp)
  3560. rcu_cleanup_dying_cpu(rsp);
  3561. return 0;
  3562. }
  3563. int rcutree_dead_cpu(unsigned int cpu)
  3564. {
  3565. struct rcu_state *rsp;
  3566. for_each_rcu_flavor(rsp) {
  3567. rcu_cleanup_dead_cpu(cpu, rsp);
  3568. do_nocb_deferred_wakeup(per_cpu_ptr(rsp->rda, cpu));
  3569. }
  3570. return 0;
  3571. }
  3572. /*
  3573. * Mark the specified CPU as being online so that subsequent grace periods
  3574. * (both expedited and normal) will wait on it. Note that this means that
  3575. * incoming CPUs are not allowed to use RCU read-side critical sections
  3576. * until this function is called. Failing to observe this restriction
  3577. * will result in lockdep splats.
  3578. */
  3579. void rcu_cpu_starting(unsigned int cpu)
  3580. {
  3581. unsigned long flags;
  3582. unsigned long mask;
  3583. struct rcu_data *rdp;
  3584. struct rcu_node *rnp;
  3585. struct rcu_state *rsp;
  3586. for_each_rcu_flavor(rsp) {
  3587. rdp = per_cpu_ptr(rsp->rda, cpu);
  3588. rnp = rdp->mynode;
  3589. mask = rdp->grpmask;
  3590. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3591. rnp->qsmaskinitnext |= mask;
  3592. rnp->expmaskinitnext |= mask;
  3593. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3594. }
  3595. }
  3596. #ifdef CONFIG_HOTPLUG_CPU
  3597. /*
  3598. * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
  3599. * function. We now remove it from the rcu_node tree's ->qsmaskinit
  3600. * bit masks.
  3601. * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
  3602. * function. We now remove it from the rcu_node tree's ->qsmaskinit
  3603. * bit masks.
  3604. */
  3605. static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp)
  3606. {
  3607. unsigned long flags;
  3608. unsigned long mask;
  3609. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3610. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  3611. /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
  3612. mask = rdp->grpmask;
  3613. raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
  3614. rnp->qsmaskinitnext &= ~mask;
  3615. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3616. }
  3617. void rcu_report_dead(unsigned int cpu)
  3618. {
  3619. struct rcu_state *rsp;
  3620. /* QS for any half-done expedited RCU-sched GP. */
  3621. preempt_disable();
  3622. rcu_report_exp_rdp(&rcu_sched_state,
  3623. this_cpu_ptr(rcu_sched_state.rda), true);
  3624. preempt_enable();
  3625. for_each_rcu_flavor(rsp)
  3626. rcu_cleanup_dying_idle_cpu(cpu, rsp);
  3627. }
  3628. #endif
  3629. static int rcu_pm_notify(struct notifier_block *self,
  3630. unsigned long action, void *hcpu)
  3631. {
  3632. switch (action) {
  3633. case PM_HIBERNATION_PREPARE:
  3634. case PM_SUSPEND_PREPARE:
  3635. if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
  3636. rcu_expedite_gp();
  3637. break;
  3638. case PM_POST_HIBERNATION:
  3639. case PM_POST_SUSPEND:
  3640. if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
  3641. rcu_unexpedite_gp();
  3642. break;
  3643. default:
  3644. break;
  3645. }
  3646. return NOTIFY_OK;
  3647. }
  3648. /*
  3649. * Spawn the kthreads that handle each RCU flavor's grace periods.
  3650. */
  3651. static int __init rcu_spawn_gp_kthread(void)
  3652. {
  3653. unsigned long flags;
  3654. int kthread_prio_in = kthread_prio;
  3655. struct rcu_node *rnp;
  3656. struct rcu_state *rsp;
  3657. struct sched_param sp;
  3658. struct task_struct *t;
  3659. /* Force priority into range. */
  3660. if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
  3661. kthread_prio = 1;
  3662. else if (kthread_prio < 0)
  3663. kthread_prio = 0;
  3664. else if (kthread_prio > 99)
  3665. kthread_prio = 99;
  3666. if (kthread_prio != kthread_prio_in)
  3667. pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
  3668. kthread_prio, kthread_prio_in);
  3669. rcu_scheduler_fully_active = 1;
  3670. for_each_rcu_flavor(rsp) {
  3671. t = kthread_create(rcu_gp_kthread, rsp, "%s", rsp->name);
  3672. BUG_ON(IS_ERR(t));
  3673. rnp = rcu_get_root(rsp);
  3674. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3675. rsp->gp_kthread = t;
  3676. if (kthread_prio) {
  3677. sp.sched_priority = kthread_prio;
  3678. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  3679. }
  3680. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3681. wake_up_process(t);
  3682. }
  3683. rcu_spawn_nocb_kthreads();
  3684. rcu_spawn_boost_kthreads();
  3685. return 0;
  3686. }
  3687. early_initcall(rcu_spawn_gp_kthread);
  3688. /*
  3689. * This function is invoked towards the end of the scheduler's
  3690. * initialization process. Before this is called, the idle task might
  3691. * contain synchronous grace-period primitives (during which time, this idle
  3692. * task is booting the system, and such primitives are no-ops). After this
  3693. * function is called, any synchronous grace-period primitives are run as
  3694. * expedited, with the requesting task driving the grace period forward.
  3695. * A later core_initcall() rcu_exp_runtime_mode() will switch to full
  3696. * runtime RCU functionality.
  3697. */
  3698. void rcu_scheduler_starting(void)
  3699. {
  3700. WARN_ON(num_online_cpus() != 1);
  3701. WARN_ON(nr_context_switches() > 0);
  3702. rcu_test_sync_prims();
  3703. rcu_scheduler_active = RCU_SCHEDULER_INIT;
  3704. rcu_test_sync_prims();
  3705. }
  3706. /*
  3707. * Compute the per-level fanout, either using the exact fanout specified
  3708. * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
  3709. */
  3710. static void __init rcu_init_levelspread(int *levelspread, const int *levelcnt)
  3711. {
  3712. int i;
  3713. if (rcu_fanout_exact) {
  3714. levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
  3715. for (i = rcu_num_lvls - 2; i >= 0; i--)
  3716. levelspread[i] = RCU_FANOUT;
  3717. } else {
  3718. int ccur;
  3719. int cprv;
  3720. cprv = nr_cpu_ids;
  3721. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  3722. ccur = levelcnt[i];
  3723. levelspread[i] = (cprv + ccur - 1) / ccur;
  3724. cprv = ccur;
  3725. }
  3726. }
  3727. }
  3728. /*
  3729. * Helper function for rcu_init() that initializes one rcu_state structure.
  3730. */
  3731. static void __init rcu_init_one(struct rcu_state *rsp)
  3732. {
  3733. static const char * const buf[] = RCU_NODE_NAME_INIT;
  3734. static const char * const fqs[] = RCU_FQS_NAME_INIT;
  3735. static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
  3736. static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
  3737. static u8 fl_mask = 0x1;
  3738. int levelcnt[RCU_NUM_LVLS]; /* # nodes in each level. */
  3739. int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
  3740. int cpustride = 1;
  3741. int i;
  3742. int j;
  3743. struct rcu_node *rnp;
  3744. BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  3745. /* Silence gcc 4.8 false positive about array index out of range. */
  3746. if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
  3747. panic("rcu_init_one: rcu_num_lvls out of range");
  3748. /* Initialize the level-tracking arrays. */
  3749. for (i = 0; i < rcu_num_lvls; i++)
  3750. levelcnt[i] = num_rcu_lvl[i];
  3751. for (i = 1; i < rcu_num_lvls; i++)
  3752. rsp->level[i] = rsp->level[i - 1] + levelcnt[i - 1];
  3753. rcu_init_levelspread(levelspread, levelcnt);
  3754. rsp->flavor_mask = fl_mask;
  3755. fl_mask <<= 1;
  3756. /* Initialize the elements themselves, starting from the leaves. */
  3757. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  3758. cpustride *= levelspread[i];
  3759. rnp = rsp->level[i];
  3760. for (j = 0; j < levelcnt[i]; j++, rnp++) {
  3761. raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
  3762. lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
  3763. &rcu_node_class[i], buf[i]);
  3764. raw_spin_lock_init(&rnp->fqslock);
  3765. lockdep_set_class_and_name(&rnp->fqslock,
  3766. &rcu_fqs_class[i], fqs[i]);
  3767. rnp->gpnum = rsp->gpnum;
  3768. rnp->completed = rsp->completed;
  3769. rnp->qsmask = 0;
  3770. rnp->qsmaskinit = 0;
  3771. rnp->grplo = j * cpustride;
  3772. rnp->grphi = (j + 1) * cpustride - 1;
  3773. if (rnp->grphi >= nr_cpu_ids)
  3774. rnp->grphi = nr_cpu_ids - 1;
  3775. if (i == 0) {
  3776. rnp->grpnum = 0;
  3777. rnp->grpmask = 0;
  3778. rnp->parent = NULL;
  3779. } else {
  3780. rnp->grpnum = j % levelspread[i - 1];
  3781. rnp->grpmask = 1UL << rnp->grpnum;
  3782. rnp->parent = rsp->level[i - 1] +
  3783. j / levelspread[i - 1];
  3784. }
  3785. rnp->level = i;
  3786. INIT_LIST_HEAD(&rnp->blkd_tasks);
  3787. rcu_init_one_nocb(rnp);
  3788. init_waitqueue_head(&rnp->exp_wq[0]);
  3789. init_waitqueue_head(&rnp->exp_wq[1]);
  3790. init_waitqueue_head(&rnp->exp_wq[2]);
  3791. init_waitqueue_head(&rnp->exp_wq[3]);
  3792. spin_lock_init(&rnp->exp_lock);
  3793. }
  3794. }
  3795. init_swait_queue_head(&rsp->gp_wq);
  3796. init_swait_queue_head(&rsp->expedited_wq);
  3797. rnp = rsp->level[rcu_num_lvls - 1];
  3798. for_each_possible_cpu(i) {
  3799. while (i > rnp->grphi)
  3800. rnp++;
  3801. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  3802. rcu_boot_init_percpu_data(i, rsp);
  3803. }
  3804. list_add(&rsp->flavors, &rcu_struct_flavors);
  3805. }
  3806. /*
  3807. * Compute the rcu_node tree geometry from kernel parameters. This cannot
  3808. * replace the definitions in tree.h because those are needed to size
  3809. * the ->node array in the rcu_state structure.
  3810. */
  3811. static void __init rcu_init_geometry(void)
  3812. {
  3813. ulong d;
  3814. int i;
  3815. int rcu_capacity[RCU_NUM_LVLS];
  3816. /*
  3817. * Initialize any unspecified boot parameters.
  3818. * The default values of jiffies_till_first_fqs and
  3819. * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
  3820. * value, which is a function of HZ, then adding one for each
  3821. * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
  3822. */
  3823. d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  3824. if (jiffies_till_first_fqs == ULONG_MAX)
  3825. jiffies_till_first_fqs = d;
  3826. if (jiffies_till_next_fqs == ULONG_MAX)
  3827. jiffies_till_next_fqs = d;
  3828. /* If the compile-time values are accurate, just leave. */
  3829. if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
  3830. nr_cpu_ids == NR_CPUS)
  3831. return;
  3832. pr_info("RCU: Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%d\n",
  3833. rcu_fanout_leaf, nr_cpu_ids);
  3834. /*
  3835. * The boot-time rcu_fanout_leaf parameter must be at least two
  3836. * and cannot exceed the number of bits in the rcu_node masks.
  3837. * Complain and fall back to the compile-time values if this
  3838. * limit is exceeded.
  3839. */
  3840. if (rcu_fanout_leaf < 2 ||
  3841. rcu_fanout_leaf > sizeof(unsigned long) * 8) {
  3842. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  3843. WARN_ON(1);
  3844. return;
  3845. }
  3846. /*
  3847. * Compute number of nodes that can be handled an rcu_node tree
  3848. * with the given number of levels.
  3849. */
  3850. rcu_capacity[0] = rcu_fanout_leaf;
  3851. for (i = 1; i < RCU_NUM_LVLS; i++)
  3852. rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
  3853. /*
  3854. * The tree must be able to accommodate the configured number of CPUs.
  3855. * If this limit is exceeded, fall back to the compile-time values.
  3856. */
  3857. if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
  3858. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  3859. WARN_ON(1);
  3860. return;
  3861. }
  3862. /* Calculate the number of levels in the tree. */
  3863. for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
  3864. }
  3865. rcu_num_lvls = i + 1;
  3866. /* Calculate the number of rcu_nodes at each level of the tree. */
  3867. for (i = 0; i < rcu_num_lvls; i++) {
  3868. int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
  3869. num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
  3870. }
  3871. /* Calculate the total number of rcu_node structures. */
  3872. rcu_num_nodes = 0;
  3873. for (i = 0; i < rcu_num_lvls; i++)
  3874. rcu_num_nodes += num_rcu_lvl[i];
  3875. }
  3876. /*
  3877. * Dump out the structure of the rcu_node combining tree associated
  3878. * with the rcu_state structure referenced by rsp.
  3879. */
  3880. static void __init rcu_dump_rcu_node_tree(struct rcu_state *rsp)
  3881. {
  3882. int level = 0;
  3883. struct rcu_node *rnp;
  3884. pr_info("rcu_node tree layout dump\n");
  3885. pr_info(" ");
  3886. rcu_for_each_node_breadth_first(rsp, rnp) {
  3887. if (rnp->level != level) {
  3888. pr_cont("\n");
  3889. pr_info(" ");
  3890. level = rnp->level;
  3891. }
  3892. pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
  3893. }
  3894. pr_cont("\n");
  3895. }
  3896. void __init rcu_init(void)
  3897. {
  3898. int cpu;
  3899. rcu_early_boot_tests();
  3900. rcu_bootup_announce();
  3901. rcu_init_geometry();
  3902. rcu_init_one(&rcu_bh_state);
  3903. rcu_init_one(&rcu_sched_state);
  3904. if (dump_tree)
  3905. rcu_dump_rcu_node_tree(&rcu_sched_state);
  3906. __rcu_init_preempt();
  3907. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  3908. /*
  3909. * We don't need protection against CPU-hotplug here because
  3910. * this is called early in boot, before either interrupts
  3911. * or the scheduler are operational.
  3912. */
  3913. pm_notifier(rcu_pm_notify, 0);
  3914. for_each_online_cpu(cpu) {
  3915. rcutree_prepare_cpu(cpu);
  3916. rcu_cpu_starting(cpu);
  3917. }
  3918. }
  3919. #include "tree_exp.h"
  3920. #include "tree_plugin.h"