tree.c 130 KB

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