tree.c 119 KB

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