tree.c 121 KB

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