tree.c 131 KB

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