rcutree.c 94 KB

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