tree.c 130 KB

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