rcutree.c 91 KB

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