rcutree.c 103 KB

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