tree.c 134 KB

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