tree.c 130 KB

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