rcutree.c 91 KB

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