tree_plugin.h 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  3. * Internal non-public definitions that provide either classic
  4. * or preemptible semantics.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you can access it online at
  18. * http://www.gnu.org/licenses/gpl-2.0.html.
  19. *
  20. * Copyright Red Hat, 2009
  21. * Copyright IBM Corporation, 2009
  22. *
  23. * Author: Ingo Molnar <mingo@elte.hu>
  24. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  25. */
  26. #include <linux/delay.h>
  27. #include <linux/gfp.h>
  28. #include <linux/oom.h>
  29. #include <linux/smpboot.h>
  30. #include "../time/tick-internal.h"
  31. #define RCU_KTHREAD_PRIO 1
  32. #ifdef CONFIG_RCU_BOOST
  33. #include "../locking/rtmutex_common.h"
  34. #define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO
  35. #else
  36. #define RCU_BOOST_PRIO RCU_KTHREAD_PRIO
  37. #endif
  38. #ifdef CONFIG_RCU_NOCB_CPU
  39. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  40. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  41. static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
  42. static char __initdata nocb_buf[NR_CPUS * 5];
  43. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  44. /*
  45. * Check the RCU kernel configuration parameters and print informative
  46. * messages about anything out of the ordinary. If you like #ifdef, you
  47. * will love this function.
  48. */
  49. static void __init rcu_bootup_announce_oddness(void)
  50. {
  51. #ifdef CONFIG_RCU_TRACE
  52. pr_info("\tRCU debugfs-based tracing is enabled.\n");
  53. #endif
  54. #if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
  55. pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
  56. CONFIG_RCU_FANOUT);
  57. #endif
  58. #ifdef CONFIG_RCU_FANOUT_EXACT
  59. pr_info("\tHierarchical RCU autobalancing is disabled.\n");
  60. #endif
  61. #ifdef CONFIG_RCU_FAST_NO_HZ
  62. pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  63. #endif
  64. #ifdef CONFIG_PROVE_RCU
  65. pr_info("\tRCU lockdep checking is enabled.\n");
  66. #endif
  67. #ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
  68. pr_info("\tRCU torture testing starts during boot.\n");
  69. #endif
  70. #if defined(CONFIG_TREE_PREEMPT_RCU) && !defined(CONFIG_RCU_CPU_STALL_VERBOSE)
  71. pr_info("\tDump stacks of tasks blocking RCU-preempt GP.\n");
  72. #endif
  73. #if defined(CONFIG_RCU_CPU_STALL_INFO)
  74. pr_info("\tAdditional per-CPU info printed with stalls.\n");
  75. #endif
  76. #if NUM_RCU_LVL_4 != 0
  77. pr_info("\tFour-level hierarchy is enabled.\n");
  78. #endif
  79. if (rcu_fanout_leaf != CONFIG_RCU_FANOUT_LEAF)
  80. pr_info("\tBoot-time adjustment of leaf fanout to %d.\n", rcu_fanout_leaf);
  81. if (nr_cpu_ids != NR_CPUS)
  82. pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%d.\n", NR_CPUS, nr_cpu_ids);
  83. }
  84. #ifdef CONFIG_TREE_PREEMPT_RCU
  85. RCU_STATE_INITIALIZER(rcu_preempt, 'p', call_rcu);
  86. static struct rcu_state *rcu_state_p = &rcu_preempt_state;
  87. static int rcu_preempted_readers_exp(struct rcu_node *rnp);
  88. /*
  89. * Tell them what RCU they are running.
  90. */
  91. static void __init rcu_bootup_announce(void)
  92. {
  93. pr_info("Preemptible hierarchical RCU implementation.\n");
  94. rcu_bootup_announce_oddness();
  95. }
  96. /*
  97. * Return the number of RCU-preempt batches processed thus far
  98. * for debug and statistics.
  99. */
  100. long rcu_batches_completed_preempt(void)
  101. {
  102. return rcu_preempt_state.completed;
  103. }
  104. EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
  105. /*
  106. * Return the number of RCU batches processed thus far for debug & stats.
  107. */
  108. long rcu_batches_completed(void)
  109. {
  110. return rcu_batches_completed_preempt();
  111. }
  112. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  113. /*
  114. * Record a preemptible-RCU quiescent state for the specified CPU. Note
  115. * that this just means that the task currently running on the CPU is
  116. * not in a quiescent state. There might be any number of tasks blocked
  117. * while in an RCU read-side critical section.
  118. *
  119. * Unlike the other rcu_*_qs() functions, callers to this function
  120. * must disable irqs in order to protect the assignment to
  121. * ->rcu_read_unlock_special.
  122. */
  123. static void rcu_preempt_qs(int cpu)
  124. {
  125. struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
  126. if (rdp->passed_quiesce == 0)
  127. trace_rcu_grace_period(TPS("rcu_preempt"), rdp->gpnum, TPS("cpuqs"));
  128. rdp->passed_quiesce = 1;
  129. current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  130. }
  131. /*
  132. * We have entered the scheduler, and the current task might soon be
  133. * context-switched away from. If this task is in an RCU read-side
  134. * critical section, we will no longer be able to rely on the CPU to
  135. * record that fact, so we enqueue the task on the blkd_tasks list.
  136. * The task will dequeue itself when it exits the outermost enclosing
  137. * RCU read-side critical section. Therefore, the current grace period
  138. * cannot be permitted to complete until the blkd_tasks list entries
  139. * predating the current grace period drain, in other words, until
  140. * rnp->gp_tasks becomes NULL.
  141. *
  142. * Caller must disable preemption.
  143. */
  144. static void rcu_preempt_note_context_switch(int cpu)
  145. {
  146. struct task_struct *t = current;
  147. unsigned long flags;
  148. struct rcu_data *rdp;
  149. struct rcu_node *rnp;
  150. if (t->rcu_read_lock_nesting > 0 &&
  151. (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
  152. /* Possibly blocking in an RCU read-side critical section. */
  153. rdp = per_cpu_ptr(rcu_preempt_state.rda, cpu);
  154. rnp = rdp->mynode;
  155. raw_spin_lock_irqsave(&rnp->lock, flags);
  156. smp_mb__after_unlock_lock();
  157. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
  158. t->rcu_blocked_node = rnp;
  159. /*
  160. * If this CPU has already checked in, then this task
  161. * will hold up the next grace period rather than the
  162. * current grace period. Queue the task accordingly.
  163. * If the task is queued for the current grace period
  164. * (i.e., this CPU has not yet passed through a quiescent
  165. * state for the current grace period), then as long
  166. * as that task remains queued, the current grace period
  167. * cannot end. Note that there is some uncertainty as
  168. * to exactly when the current grace period started.
  169. * We take a conservative approach, which can result
  170. * in unnecessarily waiting on tasks that started very
  171. * slightly after the current grace period began. C'est
  172. * la vie!!!
  173. *
  174. * But first, note that the current CPU must still be
  175. * on line!
  176. */
  177. WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
  178. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  179. if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
  180. list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
  181. rnp->gp_tasks = &t->rcu_node_entry;
  182. #ifdef CONFIG_RCU_BOOST
  183. if (rnp->boost_tasks != NULL)
  184. rnp->boost_tasks = rnp->gp_tasks;
  185. #endif /* #ifdef CONFIG_RCU_BOOST */
  186. } else {
  187. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  188. if (rnp->qsmask & rdp->grpmask)
  189. rnp->gp_tasks = &t->rcu_node_entry;
  190. }
  191. trace_rcu_preempt_task(rdp->rsp->name,
  192. t->pid,
  193. (rnp->qsmask & rdp->grpmask)
  194. ? rnp->gpnum
  195. : rnp->gpnum + 1);
  196. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  197. } else if (t->rcu_read_lock_nesting < 0 &&
  198. t->rcu_read_unlock_special) {
  199. /*
  200. * Complete exit from RCU read-side critical section on
  201. * behalf of preempted instance of __rcu_read_unlock().
  202. */
  203. rcu_read_unlock_special(t);
  204. }
  205. /*
  206. * Either we were not in an RCU read-side critical section to
  207. * begin with, or we have now recorded that critical section
  208. * globally. Either way, we can now note a quiescent state
  209. * for this CPU. Again, if we were in an RCU read-side critical
  210. * section, and if that critical section was blocking the current
  211. * grace period, then the fact that the task has been enqueued
  212. * means that we continue to block the current grace period.
  213. */
  214. local_irq_save(flags);
  215. rcu_preempt_qs(cpu);
  216. local_irq_restore(flags);
  217. }
  218. /*
  219. * Check for preempted RCU readers blocking the current grace period
  220. * for the specified rcu_node structure. If the caller needs a reliable
  221. * answer, it must hold the rcu_node's ->lock.
  222. */
  223. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  224. {
  225. return rnp->gp_tasks != NULL;
  226. }
  227. /*
  228. * Record a quiescent state for all tasks that were previously queued
  229. * on the specified rcu_node structure and that were blocking the current
  230. * RCU grace period. The caller must hold the specified rnp->lock with
  231. * irqs disabled, and this lock is released upon return, but irqs remain
  232. * disabled.
  233. */
  234. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  235. __releases(rnp->lock)
  236. {
  237. unsigned long mask;
  238. struct rcu_node *rnp_p;
  239. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  240. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  241. return; /* Still need more quiescent states! */
  242. }
  243. rnp_p = rnp->parent;
  244. if (rnp_p == NULL) {
  245. /*
  246. * Either there is only one rcu_node in the tree,
  247. * or tasks were kicked up to root rcu_node due to
  248. * CPUs going offline.
  249. */
  250. rcu_report_qs_rsp(&rcu_preempt_state, flags);
  251. return;
  252. }
  253. /* Report up the rest of the hierarchy. */
  254. mask = rnp->grpmask;
  255. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  256. raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
  257. smp_mb__after_unlock_lock();
  258. rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
  259. }
  260. /*
  261. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  262. * returning NULL if at the end of the list.
  263. */
  264. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  265. struct rcu_node *rnp)
  266. {
  267. struct list_head *np;
  268. np = t->rcu_node_entry.next;
  269. if (np == &rnp->blkd_tasks)
  270. np = NULL;
  271. return np;
  272. }
  273. /*
  274. * Handle special cases during rcu_read_unlock(), such as needing to
  275. * notify RCU core processing or task having blocked during the RCU
  276. * read-side critical section.
  277. */
  278. void rcu_read_unlock_special(struct task_struct *t)
  279. {
  280. int empty;
  281. int empty_exp;
  282. int empty_exp_now;
  283. unsigned long flags;
  284. struct list_head *np;
  285. #ifdef CONFIG_RCU_BOOST
  286. bool drop_boost_mutex = false;
  287. #endif /* #ifdef CONFIG_RCU_BOOST */
  288. struct rcu_node *rnp;
  289. int special;
  290. /* NMI handlers cannot block and cannot safely manipulate state. */
  291. if (in_nmi())
  292. return;
  293. local_irq_save(flags);
  294. /*
  295. * If RCU core is waiting for this CPU to exit critical section,
  296. * let it know that we have done so.
  297. */
  298. special = t->rcu_read_unlock_special;
  299. if (special & RCU_READ_UNLOCK_NEED_QS) {
  300. rcu_preempt_qs(smp_processor_id());
  301. if (!t->rcu_read_unlock_special) {
  302. local_irq_restore(flags);
  303. return;
  304. }
  305. }
  306. /* Hardware IRQ handlers cannot block, complain if they get here. */
  307. if (WARN_ON_ONCE(in_irq() || in_serving_softirq())) {
  308. local_irq_restore(flags);
  309. return;
  310. }
  311. /* Clean up if blocked during RCU read-side critical section. */
  312. if (special & RCU_READ_UNLOCK_BLOCKED) {
  313. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  314. /*
  315. * Remove this task from the list it blocked on. The
  316. * task can migrate while we acquire the lock, but at
  317. * most one time. So at most two passes through loop.
  318. */
  319. for (;;) {
  320. rnp = t->rcu_blocked_node;
  321. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  322. smp_mb__after_unlock_lock();
  323. if (rnp == t->rcu_blocked_node)
  324. break;
  325. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  326. }
  327. empty = !rcu_preempt_blocked_readers_cgp(rnp);
  328. empty_exp = !rcu_preempted_readers_exp(rnp);
  329. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  330. np = rcu_next_node_entry(t, rnp);
  331. list_del_init(&t->rcu_node_entry);
  332. t->rcu_blocked_node = NULL;
  333. trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
  334. rnp->gpnum, t->pid);
  335. if (&t->rcu_node_entry == rnp->gp_tasks)
  336. rnp->gp_tasks = np;
  337. if (&t->rcu_node_entry == rnp->exp_tasks)
  338. rnp->exp_tasks = np;
  339. #ifdef CONFIG_RCU_BOOST
  340. if (&t->rcu_node_entry == rnp->boost_tasks)
  341. rnp->boost_tasks = np;
  342. /* Snapshot ->boost_mtx ownership with rcu_node lock held. */
  343. drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
  344. #endif /* #ifdef CONFIG_RCU_BOOST */
  345. /*
  346. * If this was the last task on the current list, and if
  347. * we aren't waiting on any CPUs, report the quiescent state.
  348. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
  349. * so we must take a snapshot of the expedited state.
  350. */
  351. empty_exp_now = !rcu_preempted_readers_exp(rnp);
  352. if (!empty && !rcu_preempt_blocked_readers_cgp(rnp)) {
  353. trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
  354. rnp->gpnum,
  355. 0, rnp->qsmask,
  356. rnp->level,
  357. rnp->grplo,
  358. rnp->grphi,
  359. !!rnp->gp_tasks);
  360. rcu_report_unblock_qs_rnp(rnp, flags);
  361. } else {
  362. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  363. }
  364. #ifdef CONFIG_RCU_BOOST
  365. /* Unboost if we were boosted. */
  366. if (drop_boost_mutex) {
  367. rt_mutex_unlock(&rnp->boost_mtx);
  368. complete(&rnp->boost_completion);
  369. }
  370. #endif /* #ifdef CONFIG_RCU_BOOST */
  371. /*
  372. * If this was the last task on the expedited lists,
  373. * then we need to report up the rcu_node hierarchy.
  374. */
  375. if (!empty_exp && empty_exp_now)
  376. rcu_report_exp_rnp(&rcu_preempt_state, rnp, true);
  377. } else {
  378. local_irq_restore(flags);
  379. }
  380. }
  381. #ifdef CONFIG_RCU_CPU_STALL_VERBOSE
  382. /*
  383. * Dump detailed information for all tasks blocking the current RCU
  384. * grace period on the specified rcu_node structure.
  385. */
  386. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  387. {
  388. unsigned long flags;
  389. struct task_struct *t;
  390. raw_spin_lock_irqsave(&rnp->lock, flags);
  391. if (!rcu_preempt_blocked_readers_cgp(rnp)) {
  392. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  393. return;
  394. }
  395. t = list_entry(rnp->gp_tasks,
  396. struct task_struct, rcu_node_entry);
  397. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
  398. sched_show_task(t);
  399. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  400. }
  401. /*
  402. * Dump detailed information for all tasks blocking the current RCU
  403. * grace period.
  404. */
  405. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  406. {
  407. struct rcu_node *rnp = rcu_get_root(rsp);
  408. rcu_print_detail_task_stall_rnp(rnp);
  409. rcu_for_each_leaf_node(rsp, rnp)
  410. rcu_print_detail_task_stall_rnp(rnp);
  411. }
  412. #else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  413. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  414. {
  415. }
  416. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  417. #ifdef CONFIG_RCU_CPU_STALL_INFO
  418. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  419. {
  420. pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
  421. rnp->level, rnp->grplo, rnp->grphi);
  422. }
  423. static void rcu_print_task_stall_end(void)
  424. {
  425. pr_cont("\n");
  426. }
  427. #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
  428. static void rcu_print_task_stall_begin(struct rcu_node *rnp)
  429. {
  430. }
  431. static void rcu_print_task_stall_end(void)
  432. {
  433. }
  434. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
  435. /*
  436. * Scan the current list of tasks blocked within RCU read-side critical
  437. * sections, printing out the tid of each.
  438. */
  439. static int rcu_print_task_stall(struct rcu_node *rnp)
  440. {
  441. struct task_struct *t;
  442. int ndetected = 0;
  443. if (!rcu_preempt_blocked_readers_cgp(rnp))
  444. return 0;
  445. rcu_print_task_stall_begin(rnp);
  446. t = list_entry(rnp->gp_tasks,
  447. struct task_struct, rcu_node_entry);
  448. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  449. pr_cont(" P%d", t->pid);
  450. ndetected++;
  451. }
  452. rcu_print_task_stall_end();
  453. return ndetected;
  454. }
  455. /*
  456. * Check that the list of blocked tasks for the newly completed grace
  457. * period is in fact empty. It is a serious bug to complete a grace
  458. * period that still has RCU readers blocked! This function must be
  459. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  460. * must be held by the caller.
  461. *
  462. * Also, if there are blocked tasks on the list, they automatically
  463. * block the newly created grace period, so set up ->gp_tasks accordingly.
  464. */
  465. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  466. {
  467. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  468. if (!list_empty(&rnp->blkd_tasks))
  469. rnp->gp_tasks = rnp->blkd_tasks.next;
  470. WARN_ON_ONCE(rnp->qsmask);
  471. }
  472. #ifdef CONFIG_HOTPLUG_CPU
  473. /*
  474. * Handle tasklist migration for case in which all CPUs covered by the
  475. * specified rcu_node have gone offline. Move them up to the root
  476. * rcu_node. The reason for not just moving them to the immediate
  477. * parent is to remove the need for rcu_read_unlock_special() to
  478. * make more than two attempts to acquire the target rcu_node's lock.
  479. * Returns true if there were tasks blocking the current RCU grace
  480. * period.
  481. *
  482. * Returns 1 if there was previously a task blocking the current grace
  483. * period on the specified rcu_node structure.
  484. *
  485. * The caller must hold rnp->lock with irqs disabled.
  486. */
  487. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  488. struct rcu_node *rnp,
  489. struct rcu_data *rdp)
  490. {
  491. struct list_head *lp;
  492. struct list_head *lp_root;
  493. int retval = 0;
  494. struct rcu_node *rnp_root = rcu_get_root(rsp);
  495. struct task_struct *t;
  496. if (rnp == rnp_root) {
  497. WARN_ONCE(1, "Last CPU thought to be offlined?");
  498. return 0; /* Shouldn't happen: at least one CPU online. */
  499. }
  500. /* If we are on an internal node, complain bitterly. */
  501. WARN_ON_ONCE(rnp != rdp->mynode);
  502. /*
  503. * Move tasks up to root rcu_node. Don't try to get fancy for
  504. * this corner-case operation -- just put this node's tasks
  505. * at the head of the root node's list, and update the root node's
  506. * ->gp_tasks and ->exp_tasks pointers to those of this node's,
  507. * if non-NULL. This might result in waiting for more tasks than
  508. * absolutely necessary, but this is a good performance/complexity
  509. * tradeoff.
  510. */
  511. if (rcu_preempt_blocked_readers_cgp(rnp) && rnp->qsmask == 0)
  512. retval |= RCU_OFL_TASKS_NORM_GP;
  513. if (rcu_preempted_readers_exp(rnp))
  514. retval |= RCU_OFL_TASKS_EXP_GP;
  515. lp = &rnp->blkd_tasks;
  516. lp_root = &rnp_root->blkd_tasks;
  517. while (!list_empty(lp)) {
  518. t = list_entry(lp->next, typeof(*t), rcu_node_entry);
  519. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  520. smp_mb__after_unlock_lock();
  521. list_del(&t->rcu_node_entry);
  522. t->rcu_blocked_node = rnp_root;
  523. list_add(&t->rcu_node_entry, lp_root);
  524. if (&t->rcu_node_entry == rnp->gp_tasks)
  525. rnp_root->gp_tasks = rnp->gp_tasks;
  526. if (&t->rcu_node_entry == rnp->exp_tasks)
  527. rnp_root->exp_tasks = rnp->exp_tasks;
  528. #ifdef CONFIG_RCU_BOOST
  529. if (&t->rcu_node_entry == rnp->boost_tasks)
  530. rnp_root->boost_tasks = rnp->boost_tasks;
  531. #endif /* #ifdef CONFIG_RCU_BOOST */
  532. raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
  533. }
  534. rnp->gp_tasks = NULL;
  535. rnp->exp_tasks = NULL;
  536. #ifdef CONFIG_RCU_BOOST
  537. rnp->boost_tasks = NULL;
  538. /*
  539. * In case root is being boosted and leaf was not. Make sure
  540. * that we boost the tasks blocking the current grace period
  541. * in this case.
  542. */
  543. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  544. smp_mb__after_unlock_lock();
  545. if (rnp_root->boost_tasks != NULL &&
  546. rnp_root->boost_tasks != rnp_root->gp_tasks &&
  547. rnp_root->boost_tasks != rnp_root->exp_tasks)
  548. rnp_root->boost_tasks = rnp_root->gp_tasks;
  549. raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
  550. #endif /* #ifdef CONFIG_RCU_BOOST */
  551. return retval;
  552. }
  553. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  554. /*
  555. * Check for a quiescent state from the current CPU. When a task blocks,
  556. * the task is recorded in the corresponding CPU's rcu_node structure,
  557. * which is checked elsewhere.
  558. *
  559. * Caller must disable hard irqs.
  560. */
  561. static void rcu_preempt_check_callbacks(int cpu)
  562. {
  563. struct task_struct *t = current;
  564. if (t->rcu_read_lock_nesting == 0) {
  565. rcu_preempt_qs(cpu);
  566. return;
  567. }
  568. if (t->rcu_read_lock_nesting > 0 &&
  569. per_cpu(rcu_preempt_data, cpu).qs_pending)
  570. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  571. }
  572. #ifdef CONFIG_RCU_BOOST
  573. static void rcu_preempt_do_callbacks(void)
  574. {
  575. rcu_do_batch(&rcu_preempt_state, this_cpu_ptr(&rcu_preempt_data));
  576. }
  577. #endif /* #ifdef CONFIG_RCU_BOOST */
  578. /*
  579. * Queue a preemptible-RCU callback for invocation after a grace period.
  580. */
  581. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  582. {
  583. __call_rcu(head, func, &rcu_preempt_state, -1, 0);
  584. }
  585. EXPORT_SYMBOL_GPL(call_rcu);
  586. /**
  587. * synchronize_rcu - wait until a grace period has elapsed.
  588. *
  589. * Control will return to the caller some time after a full grace
  590. * period has elapsed, in other words after all currently executing RCU
  591. * read-side critical sections have completed. Note, however, that
  592. * upon return from synchronize_rcu(), the caller might well be executing
  593. * concurrently with new RCU read-side critical sections that began while
  594. * synchronize_rcu() was waiting. RCU read-side critical sections are
  595. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  596. *
  597. * See the description of synchronize_sched() for more detailed information
  598. * on memory ordering guarantees.
  599. */
  600. void synchronize_rcu(void)
  601. {
  602. rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
  603. !lock_is_held(&rcu_lock_map) &&
  604. !lock_is_held(&rcu_sched_lock_map),
  605. "Illegal synchronize_rcu() in RCU read-side critical section");
  606. if (!rcu_scheduler_active)
  607. return;
  608. if (rcu_expedited)
  609. synchronize_rcu_expedited();
  610. else
  611. wait_rcu_gp(call_rcu);
  612. }
  613. EXPORT_SYMBOL_GPL(synchronize_rcu);
  614. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  615. static unsigned long sync_rcu_preempt_exp_count;
  616. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  617. /*
  618. * Return non-zero if there are any tasks in RCU read-side critical
  619. * sections blocking the current preemptible-RCU expedited grace period.
  620. * If there is no preemptible-RCU expedited grace period currently in
  621. * progress, returns zero unconditionally.
  622. */
  623. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  624. {
  625. return rnp->exp_tasks != NULL;
  626. }
  627. /*
  628. * return non-zero if there is no RCU expedited grace period in progress
  629. * for the specified rcu_node structure, in other words, if all CPUs and
  630. * tasks covered by the specified rcu_node structure have done their bit
  631. * for the current expedited grace period. Works only for preemptible
  632. * RCU -- other RCU implementation use other means.
  633. *
  634. * Caller must hold sync_rcu_preempt_exp_mutex.
  635. */
  636. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  637. {
  638. return !rcu_preempted_readers_exp(rnp) &&
  639. ACCESS_ONCE(rnp->expmask) == 0;
  640. }
  641. /*
  642. * Report the exit from RCU read-side critical section for the last task
  643. * that queued itself during or before the current expedited preemptible-RCU
  644. * grace period. This event is reported either to the rcu_node structure on
  645. * which the task was queued or to one of that rcu_node structure's ancestors,
  646. * recursively up the tree. (Calm down, calm down, we do the recursion
  647. * iteratively!)
  648. *
  649. * Most callers will set the "wake" flag, but the task initiating the
  650. * expedited grace period need not wake itself.
  651. *
  652. * Caller must hold sync_rcu_preempt_exp_mutex.
  653. */
  654. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  655. bool wake)
  656. {
  657. unsigned long flags;
  658. unsigned long mask;
  659. raw_spin_lock_irqsave(&rnp->lock, flags);
  660. smp_mb__after_unlock_lock();
  661. for (;;) {
  662. if (!sync_rcu_preempt_exp_done(rnp)) {
  663. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  664. break;
  665. }
  666. if (rnp->parent == NULL) {
  667. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  668. if (wake) {
  669. smp_mb(); /* EGP done before wake_up(). */
  670. wake_up(&sync_rcu_preempt_exp_wq);
  671. }
  672. break;
  673. }
  674. mask = rnp->grpmask;
  675. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  676. rnp = rnp->parent;
  677. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  678. smp_mb__after_unlock_lock();
  679. rnp->expmask &= ~mask;
  680. }
  681. }
  682. /*
  683. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  684. * grace period for the specified rcu_node structure. If there are no such
  685. * tasks, report it up the rcu_node hierarchy.
  686. *
  687. * Caller must hold sync_rcu_preempt_exp_mutex and must exclude
  688. * CPU hotplug operations.
  689. */
  690. static void
  691. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  692. {
  693. unsigned long flags;
  694. int must_wait = 0;
  695. raw_spin_lock_irqsave(&rnp->lock, flags);
  696. smp_mb__after_unlock_lock();
  697. if (list_empty(&rnp->blkd_tasks)) {
  698. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  699. } else {
  700. rnp->exp_tasks = rnp->blkd_tasks.next;
  701. rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
  702. must_wait = 1;
  703. }
  704. if (!must_wait)
  705. rcu_report_exp_rnp(rsp, rnp, false); /* Don't wake self. */
  706. }
  707. /**
  708. * synchronize_rcu_expedited - Brute-force RCU grace period
  709. *
  710. * Wait for an RCU-preempt grace period, but expedite it. The basic
  711. * idea is to invoke synchronize_sched_expedited() to push all the tasks to
  712. * the ->blkd_tasks lists and wait for this list to drain. This consumes
  713. * significant time on all CPUs and is unfriendly to real-time workloads,
  714. * so is thus not recommended for any sort of common-case code.
  715. * In fact, if you are using synchronize_rcu_expedited() in a loop,
  716. * please restructure your code to batch your updates, and then Use a
  717. * single synchronize_rcu() instead.
  718. *
  719. * Note that it is illegal to call this function while holding any lock
  720. * that is acquired by a CPU-hotplug notifier. And yes, it is also illegal
  721. * to call this function from a CPU-hotplug notifier. Failing to observe
  722. * these restriction will result in deadlock.
  723. */
  724. void synchronize_rcu_expedited(void)
  725. {
  726. unsigned long flags;
  727. struct rcu_node *rnp;
  728. struct rcu_state *rsp = &rcu_preempt_state;
  729. unsigned long snap;
  730. int trycount = 0;
  731. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  732. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  733. smp_mb(); /* Above access cannot bleed into critical section. */
  734. /*
  735. * Block CPU-hotplug operations. This means that any CPU-hotplug
  736. * operation that finds an rcu_node structure with tasks in the
  737. * process of being boosted will know that all tasks blocking
  738. * this expedited grace period will already be in the process of
  739. * being boosted. This simplifies the process of moving tasks
  740. * from leaf to root rcu_node structures.
  741. */
  742. get_online_cpus();
  743. /*
  744. * Acquire lock, falling back to synchronize_rcu() if too many
  745. * lock-acquisition failures. Of course, if someone does the
  746. * expedited grace period for us, just leave.
  747. */
  748. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  749. if (ULONG_CMP_LT(snap,
  750. ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
  751. put_online_cpus();
  752. goto mb_ret; /* Others did our work for us. */
  753. }
  754. if (trycount++ < 10) {
  755. udelay(trycount * num_online_cpus());
  756. } else {
  757. put_online_cpus();
  758. wait_rcu_gp(call_rcu);
  759. return;
  760. }
  761. }
  762. if (ULONG_CMP_LT(snap, ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
  763. put_online_cpus();
  764. goto unlock_mb_ret; /* Others did our work for us. */
  765. }
  766. /* force all RCU readers onto ->blkd_tasks lists. */
  767. synchronize_sched_expedited();
  768. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  769. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  770. raw_spin_lock_irqsave(&rnp->lock, flags);
  771. smp_mb__after_unlock_lock();
  772. rnp->expmask = rnp->qsmaskinit;
  773. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  774. }
  775. /* Snapshot current state of ->blkd_tasks lists. */
  776. rcu_for_each_leaf_node(rsp, rnp)
  777. sync_rcu_preempt_exp_init(rsp, rnp);
  778. if (NUM_RCU_NODES > 1)
  779. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  780. put_online_cpus();
  781. /* Wait for snapshotted ->blkd_tasks lists to drain. */
  782. rnp = rcu_get_root(rsp);
  783. wait_event(sync_rcu_preempt_exp_wq,
  784. sync_rcu_preempt_exp_done(rnp));
  785. /* Clean up and exit. */
  786. smp_mb(); /* ensure expedited GP seen before counter increment. */
  787. ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
  788. unlock_mb_ret:
  789. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  790. mb_ret:
  791. smp_mb(); /* ensure subsequent action seen after grace period. */
  792. }
  793. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  794. /**
  795. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  796. *
  797. * Note that this primitive does not necessarily wait for an RCU grace period
  798. * to complete. For example, if there are no RCU callbacks queued anywhere
  799. * in the system, then rcu_barrier() is within its rights to return
  800. * immediately, without waiting for anything, much less an RCU grace period.
  801. */
  802. void rcu_barrier(void)
  803. {
  804. _rcu_barrier(&rcu_preempt_state);
  805. }
  806. EXPORT_SYMBOL_GPL(rcu_barrier);
  807. /*
  808. * Initialize preemptible RCU's state structures.
  809. */
  810. static void __init __rcu_init_preempt(void)
  811. {
  812. rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
  813. }
  814. /*
  815. * Check for a task exiting while in a preemptible-RCU read-side
  816. * critical section, clean up if so. No need to issue warnings,
  817. * as debug_check_no_locks_held() already does this if lockdep
  818. * is enabled.
  819. */
  820. void exit_rcu(void)
  821. {
  822. struct task_struct *t = current;
  823. if (likely(list_empty(&current->rcu_node_entry)))
  824. return;
  825. t->rcu_read_lock_nesting = 1;
  826. barrier();
  827. t->rcu_read_unlock_special = RCU_READ_UNLOCK_BLOCKED;
  828. __rcu_read_unlock();
  829. }
  830. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  831. static struct rcu_state *rcu_state_p = &rcu_sched_state;
  832. /*
  833. * Tell them what RCU they are running.
  834. */
  835. static void __init rcu_bootup_announce(void)
  836. {
  837. pr_info("Hierarchical RCU implementation.\n");
  838. rcu_bootup_announce_oddness();
  839. }
  840. /*
  841. * Return the number of RCU batches processed thus far for debug & stats.
  842. */
  843. long rcu_batches_completed(void)
  844. {
  845. return rcu_batches_completed_sched();
  846. }
  847. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  848. /*
  849. * Because preemptible RCU does not exist, we never have to check for
  850. * CPUs being in quiescent states.
  851. */
  852. static void rcu_preempt_note_context_switch(int cpu)
  853. {
  854. }
  855. /*
  856. * Because preemptible RCU does not exist, there are never any preempted
  857. * RCU readers.
  858. */
  859. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  860. {
  861. return 0;
  862. }
  863. #ifdef CONFIG_HOTPLUG_CPU
  864. /* Because preemptible RCU does not exist, no quieting of tasks. */
  865. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  866. __releases(rnp->lock)
  867. {
  868. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  869. }
  870. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  871. /*
  872. * Because preemptible RCU does not exist, we never have to check for
  873. * tasks blocked within RCU read-side critical sections.
  874. */
  875. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  876. {
  877. }
  878. /*
  879. * Because preemptible RCU does not exist, we never have to check for
  880. * tasks blocked within RCU read-side critical sections.
  881. */
  882. static int rcu_print_task_stall(struct rcu_node *rnp)
  883. {
  884. return 0;
  885. }
  886. /*
  887. * Because there is no preemptible RCU, there can be no readers blocked,
  888. * so there is no need to check for blocked tasks. So check only for
  889. * bogus qsmask values.
  890. */
  891. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  892. {
  893. WARN_ON_ONCE(rnp->qsmask);
  894. }
  895. #ifdef CONFIG_HOTPLUG_CPU
  896. /*
  897. * Because preemptible RCU does not exist, it never needs to migrate
  898. * tasks that were blocked within RCU read-side critical sections, and
  899. * such non-existent tasks cannot possibly have been blocking the current
  900. * grace period.
  901. */
  902. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  903. struct rcu_node *rnp,
  904. struct rcu_data *rdp)
  905. {
  906. return 0;
  907. }
  908. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  909. /*
  910. * Because preemptible RCU does not exist, it never has any callbacks
  911. * to check.
  912. */
  913. static void rcu_preempt_check_callbacks(int cpu)
  914. {
  915. }
  916. /*
  917. * Wait for an rcu-preempt grace period, but make it happen quickly.
  918. * But because preemptible RCU does not exist, map to rcu-sched.
  919. */
  920. void synchronize_rcu_expedited(void)
  921. {
  922. synchronize_sched_expedited();
  923. }
  924. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  925. #ifdef CONFIG_HOTPLUG_CPU
  926. /*
  927. * Because preemptible RCU does not exist, there is never any need to
  928. * report on tasks preempted in RCU read-side critical sections during
  929. * expedited RCU grace periods.
  930. */
  931. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  932. bool wake)
  933. {
  934. }
  935. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  936. /*
  937. * Because preemptible RCU does not exist, rcu_barrier() is just
  938. * another name for rcu_barrier_sched().
  939. */
  940. void rcu_barrier(void)
  941. {
  942. rcu_barrier_sched();
  943. }
  944. EXPORT_SYMBOL_GPL(rcu_barrier);
  945. /*
  946. * Because preemptible RCU does not exist, it need not be initialized.
  947. */
  948. static void __init __rcu_init_preempt(void)
  949. {
  950. }
  951. /*
  952. * Because preemptible RCU does not exist, tasks cannot possibly exit
  953. * while in preemptible RCU read-side critical sections.
  954. */
  955. void exit_rcu(void)
  956. {
  957. }
  958. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
  959. #ifdef CONFIG_RCU_BOOST
  960. #include "../locking/rtmutex_common.h"
  961. #ifdef CONFIG_RCU_TRACE
  962. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  963. {
  964. if (list_empty(&rnp->blkd_tasks))
  965. rnp->n_balk_blkd_tasks++;
  966. else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
  967. rnp->n_balk_exp_gp_tasks++;
  968. else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
  969. rnp->n_balk_boost_tasks++;
  970. else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
  971. rnp->n_balk_notblocked++;
  972. else if (rnp->gp_tasks != NULL &&
  973. ULONG_CMP_LT(jiffies, rnp->boost_time))
  974. rnp->n_balk_notyet++;
  975. else
  976. rnp->n_balk_nos++;
  977. }
  978. #else /* #ifdef CONFIG_RCU_TRACE */
  979. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  980. {
  981. }
  982. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  983. static void rcu_wake_cond(struct task_struct *t, int status)
  984. {
  985. /*
  986. * If the thread is yielding, only wake it when this
  987. * is invoked from idle
  988. */
  989. if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  990. wake_up_process(t);
  991. }
  992. /*
  993. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  994. * or ->boost_tasks, advancing the pointer to the next task in the
  995. * ->blkd_tasks list.
  996. *
  997. * Note that irqs must be enabled: boosting the task can block.
  998. * Returns 1 if there are more tasks needing to be boosted.
  999. */
  1000. static int rcu_boost(struct rcu_node *rnp)
  1001. {
  1002. unsigned long flags;
  1003. struct task_struct *t;
  1004. struct list_head *tb;
  1005. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL)
  1006. return 0; /* Nothing left to boost. */
  1007. raw_spin_lock_irqsave(&rnp->lock, flags);
  1008. smp_mb__after_unlock_lock();
  1009. /*
  1010. * Recheck under the lock: all tasks in need of boosting
  1011. * might exit their RCU read-side critical sections on their own.
  1012. */
  1013. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  1014. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1015. return 0;
  1016. }
  1017. /*
  1018. * Preferentially boost tasks blocking expedited grace periods.
  1019. * This cannot starve the normal grace periods because a second
  1020. * expedited grace period must boost all blocked tasks, including
  1021. * those blocking the pre-existing normal grace period.
  1022. */
  1023. if (rnp->exp_tasks != NULL) {
  1024. tb = rnp->exp_tasks;
  1025. rnp->n_exp_boosts++;
  1026. } else {
  1027. tb = rnp->boost_tasks;
  1028. rnp->n_normal_boosts++;
  1029. }
  1030. rnp->n_tasks_boosted++;
  1031. /*
  1032. * We boost task t by manufacturing an rt_mutex that appears to
  1033. * be held by task t. We leave a pointer to that rt_mutex where
  1034. * task t can find it, and task t will release the mutex when it
  1035. * exits its outermost RCU read-side critical section. Then
  1036. * simply acquiring this artificial rt_mutex will boost task
  1037. * t's priority. (Thanks to tglx for suggesting this approach!)
  1038. *
  1039. * Note that task t must acquire rnp->lock to remove itself from
  1040. * the ->blkd_tasks list, which it will do from exit() if from
  1041. * nowhere else. We therefore are guaranteed that task t will
  1042. * stay around at least until we drop rnp->lock. Note that
  1043. * rnp->lock also resolves races between our priority boosting
  1044. * and task t's exiting its outermost RCU read-side critical
  1045. * section.
  1046. */
  1047. t = container_of(tb, struct task_struct, rcu_node_entry);
  1048. rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
  1049. init_completion(&rnp->boost_completion);
  1050. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1051. /* Lock only for side effect: boosts task t's priority. */
  1052. rt_mutex_lock(&rnp->boost_mtx);
  1053. rt_mutex_unlock(&rnp->boost_mtx); /* Then keep lockdep happy. */
  1054. /* Wait for boostee to be done w/boost_mtx before reinitializing. */
  1055. wait_for_completion(&rnp->boost_completion);
  1056. return ACCESS_ONCE(rnp->exp_tasks) != NULL ||
  1057. ACCESS_ONCE(rnp->boost_tasks) != NULL;
  1058. }
  1059. /*
  1060. * Priority-boosting kthread. One per leaf rcu_node and one for the
  1061. * root rcu_node.
  1062. */
  1063. static int rcu_boost_kthread(void *arg)
  1064. {
  1065. struct rcu_node *rnp = (struct rcu_node *)arg;
  1066. int spincnt = 0;
  1067. int more2boost;
  1068. trace_rcu_utilization(TPS("Start boost kthread@init"));
  1069. for (;;) {
  1070. rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
  1071. trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
  1072. rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
  1073. trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
  1074. rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
  1075. more2boost = rcu_boost(rnp);
  1076. if (more2boost)
  1077. spincnt++;
  1078. else
  1079. spincnt = 0;
  1080. if (spincnt > 10) {
  1081. rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
  1082. trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
  1083. schedule_timeout_interruptible(2);
  1084. trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
  1085. spincnt = 0;
  1086. }
  1087. }
  1088. /* NOTREACHED */
  1089. trace_rcu_utilization(TPS("End boost kthread@notreached"));
  1090. return 0;
  1091. }
  1092. /*
  1093. * Check to see if it is time to start boosting RCU readers that are
  1094. * blocking the current grace period, and, if so, tell the per-rcu_node
  1095. * kthread to start boosting them. If there is an expedited grace
  1096. * period in progress, it is always time to boost.
  1097. *
  1098. * The caller must hold rnp->lock, which this function releases.
  1099. * The ->boost_kthread_task is immortal, so we don't need to worry
  1100. * about it going away.
  1101. */
  1102. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1103. __releases(rnp->lock)
  1104. {
  1105. struct task_struct *t;
  1106. if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
  1107. rnp->n_balk_exp_gp_tasks++;
  1108. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1109. return;
  1110. }
  1111. if (rnp->exp_tasks != NULL ||
  1112. (rnp->gp_tasks != NULL &&
  1113. rnp->boost_tasks == NULL &&
  1114. rnp->qsmask == 0 &&
  1115. ULONG_CMP_GE(jiffies, rnp->boost_time))) {
  1116. if (rnp->exp_tasks == NULL)
  1117. rnp->boost_tasks = rnp->gp_tasks;
  1118. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1119. t = rnp->boost_kthread_task;
  1120. if (t)
  1121. rcu_wake_cond(t, rnp->boost_kthread_status);
  1122. } else {
  1123. rcu_initiate_boost_trace(rnp);
  1124. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1125. }
  1126. }
  1127. /*
  1128. * Wake up the per-CPU kthread to invoke RCU callbacks.
  1129. */
  1130. static void invoke_rcu_callbacks_kthread(void)
  1131. {
  1132. unsigned long flags;
  1133. local_irq_save(flags);
  1134. __this_cpu_write(rcu_cpu_has_work, 1);
  1135. if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  1136. current != __this_cpu_read(rcu_cpu_kthread_task)) {
  1137. rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  1138. __this_cpu_read(rcu_cpu_kthread_status));
  1139. }
  1140. local_irq_restore(flags);
  1141. }
  1142. /*
  1143. * Is the current CPU running the RCU-callbacks kthread?
  1144. * Caller must have preemption disabled.
  1145. */
  1146. static bool rcu_is_callbacks_kthread(void)
  1147. {
  1148. return __this_cpu_read(rcu_cpu_kthread_task) == current;
  1149. }
  1150. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  1151. /*
  1152. * Do priority-boost accounting for the start of a new grace period.
  1153. */
  1154. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1155. {
  1156. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  1157. }
  1158. /*
  1159. * Create an RCU-boost kthread for the specified node if one does not
  1160. * already exist. We only create this kthread for preemptible RCU.
  1161. * Returns zero if all is well, a negated errno otherwise.
  1162. */
  1163. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1164. struct rcu_node *rnp)
  1165. {
  1166. int rnp_index = rnp - &rsp->node[0];
  1167. unsigned long flags;
  1168. struct sched_param sp;
  1169. struct task_struct *t;
  1170. if (&rcu_preempt_state != rsp)
  1171. return 0;
  1172. if (!rcu_scheduler_fully_active || rnp->qsmaskinit == 0)
  1173. return 0;
  1174. rsp->boost = 1;
  1175. if (rnp->boost_kthread_task != NULL)
  1176. return 0;
  1177. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  1178. "rcub/%d", rnp_index);
  1179. if (IS_ERR(t))
  1180. return PTR_ERR(t);
  1181. raw_spin_lock_irqsave(&rnp->lock, flags);
  1182. smp_mb__after_unlock_lock();
  1183. rnp->boost_kthread_task = t;
  1184. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1185. sp.sched_priority = RCU_BOOST_PRIO;
  1186. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1187. wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
  1188. return 0;
  1189. }
  1190. static void rcu_kthread_do_work(void)
  1191. {
  1192. rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  1193. rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  1194. rcu_preempt_do_callbacks();
  1195. }
  1196. static void rcu_cpu_kthread_setup(unsigned int cpu)
  1197. {
  1198. struct sched_param sp;
  1199. sp.sched_priority = RCU_KTHREAD_PRIO;
  1200. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  1201. }
  1202. static void rcu_cpu_kthread_park(unsigned int cpu)
  1203. {
  1204. per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  1205. }
  1206. static int rcu_cpu_kthread_should_run(unsigned int cpu)
  1207. {
  1208. return __this_cpu_read(rcu_cpu_has_work);
  1209. }
  1210. /*
  1211. * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  1212. * RCU softirq used in flavors and configurations of RCU that do not
  1213. * support RCU priority boosting.
  1214. */
  1215. static void rcu_cpu_kthread(unsigned int cpu)
  1216. {
  1217. unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  1218. char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  1219. int spincnt;
  1220. for (spincnt = 0; spincnt < 10; spincnt++) {
  1221. trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  1222. local_bh_disable();
  1223. *statusp = RCU_KTHREAD_RUNNING;
  1224. this_cpu_inc(rcu_cpu_kthread_loops);
  1225. local_irq_disable();
  1226. work = *workp;
  1227. *workp = 0;
  1228. local_irq_enable();
  1229. if (work)
  1230. rcu_kthread_do_work();
  1231. local_bh_enable();
  1232. if (*workp == 0) {
  1233. trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  1234. *statusp = RCU_KTHREAD_WAITING;
  1235. return;
  1236. }
  1237. }
  1238. *statusp = RCU_KTHREAD_YIELDING;
  1239. trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  1240. schedule_timeout_interruptible(2);
  1241. trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  1242. *statusp = RCU_KTHREAD_WAITING;
  1243. }
  1244. /*
  1245. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  1246. * served by the rcu_node in question. The CPU hotplug lock is still
  1247. * held, so the value of rnp->qsmaskinit will be stable.
  1248. *
  1249. * We don't include outgoingcpu in the affinity set, use -1 if there is
  1250. * no outgoing CPU. If there are no CPUs left in the affinity set,
  1251. * this function allows the kthread to execute on any CPU.
  1252. */
  1253. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1254. {
  1255. struct task_struct *t = rnp->boost_kthread_task;
  1256. unsigned long mask = rnp->qsmaskinit;
  1257. cpumask_var_t cm;
  1258. int cpu;
  1259. if (!t)
  1260. return;
  1261. if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
  1262. return;
  1263. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
  1264. if ((mask & 0x1) && cpu != outgoingcpu)
  1265. cpumask_set_cpu(cpu, cm);
  1266. if (cpumask_weight(cm) == 0) {
  1267. cpumask_setall(cm);
  1268. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++)
  1269. cpumask_clear_cpu(cpu, cm);
  1270. WARN_ON_ONCE(cpumask_weight(cm) == 0);
  1271. }
  1272. set_cpus_allowed_ptr(t, cm);
  1273. free_cpumask_var(cm);
  1274. }
  1275. static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  1276. .store = &rcu_cpu_kthread_task,
  1277. .thread_should_run = rcu_cpu_kthread_should_run,
  1278. .thread_fn = rcu_cpu_kthread,
  1279. .thread_comm = "rcuc/%u",
  1280. .setup = rcu_cpu_kthread_setup,
  1281. .park = rcu_cpu_kthread_park,
  1282. };
  1283. /*
  1284. * Spawn all kthreads -- called as soon as the scheduler is running.
  1285. */
  1286. static int __init rcu_spawn_kthreads(void)
  1287. {
  1288. struct rcu_node *rnp;
  1289. int cpu;
  1290. rcu_scheduler_fully_active = 1;
  1291. for_each_possible_cpu(cpu)
  1292. per_cpu(rcu_cpu_has_work, cpu) = 0;
  1293. BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  1294. rnp = rcu_get_root(rcu_state_p);
  1295. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1296. if (NUM_RCU_NODES > 1) {
  1297. rcu_for_each_leaf_node(rcu_state_p, rnp)
  1298. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1299. }
  1300. return 0;
  1301. }
  1302. early_initcall(rcu_spawn_kthreads);
  1303. static void rcu_prepare_kthreads(int cpu)
  1304. {
  1305. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  1306. struct rcu_node *rnp = rdp->mynode;
  1307. /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
  1308. if (rcu_scheduler_fully_active)
  1309. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  1310. }
  1311. #else /* #ifdef CONFIG_RCU_BOOST */
  1312. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1313. __releases(rnp->lock)
  1314. {
  1315. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1316. }
  1317. static void invoke_rcu_callbacks_kthread(void)
  1318. {
  1319. WARN_ON_ONCE(1);
  1320. }
  1321. static bool rcu_is_callbacks_kthread(void)
  1322. {
  1323. return false;
  1324. }
  1325. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1326. {
  1327. }
  1328. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  1329. {
  1330. }
  1331. static int __init rcu_scheduler_really_started(void)
  1332. {
  1333. rcu_scheduler_fully_active = 1;
  1334. return 0;
  1335. }
  1336. early_initcall(rcu_scheduler_really_started);
  1337. static void rcu_prepare_kthreads(int cpu)
  1338. {
  1339. }
  1340. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1341. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  1342. /*
  1343. * Check to see if any future RCU-related work will need to be done
  1344. * by the current CPU, even if none need be done immediately, returning
  1345. * 1 if so. This function is part of the RCU implementation; it is -not-
  1346. * an exported member of the RCU API.
  1347. *
  1348. * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
  1349. * any flavor of RCU.
  1350. */
  1351. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1352. int rcu_needs_cpu(int cpu, unsigned long *delta_jiffies)
  1353. {
  1354. *delta_jiffies = ULONG_MAX;
  1355. return rcu_cpu_has_callbacks(cpu, NULL);
  1356. }
  1357. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1358. /*
  1359. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  1360. * after it.
  1361. */
  1362. static void rcu_cleanup_after_idle(int cpu)
  1363. {
  1364. }
  1365. /*
  1366. * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
  1367. * is nothing.
  1368. */
  1369. static void rcu_prepare_for_idle(int cpu)
  1370. {
  1371. }
  1372. /*
  1373. * Don't bother keeping a running count of the number of RCU callbacks
  1374. * posted because CONFIG_RCU_FAST_NO_HZ=n.
  1375. */
  1376. static void rcu_idle_count_callbacks_posted(void)
  1377. {
  1378. }
  1379. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1380. /*
  1381. * This code is invoked when a CPU goes idle, at which point we want
  1382. * to have the CPU do everything required for RCU so that it can enter
  1383. * the energy-efficient dyntick-idle mode. This is handled by a
  1384. * state machine implemented by rcu_prepare_for_idle() below.
  1385. *
  1386. * The following three proprocessor symbols control this state machine:
  1387. *
  1388. * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
  1389. * to sleep in dyntick-idle mode with RCU callbacks pending. This
  1390. * is sized to be roughly one RCU grace period. Those energy-efficiency
  1391. * benchmarkers who might otherwise be tempted to set this to a large
  1392. * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
  1393. * system. And if you are -that- concerned about energy efficiency,
  1394. * just power the system down and be done with it!
  1395. * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
  1396. * permitted to sleep in dyntick-idle mode with only lazy RCU
  1397. * callbacks pending. Setting this too high can OOM your system.
  1398. *
  1399. * The values below work well in practice. If future workloads require
  1400. * adjustment, they can be converted into kernel config parameters, though
  1401. * making the state machine smarter might be a better option.
  1402. */
  1403. #define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
  1404. #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
  1405. static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
  1406. module_param(rcu_idle_gp_delay, int, 0644);
  1407. static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
  1408. module_param(rcu_idle_lazy_gp_delay, int, 0644);
  1409. extern int tick_nohz_active;
  1410. /*
  1411. * Try to advance callbacks for all flavors of RCU on the current CPU, but
  1412. * only if it has been awhile since the last time we did so. Afterwards,
  1413. * if there are any callbacks ready for immediate invocation, return true.
  1414. */
  1415. static bool __maybe_unused rcu_try_advance_all_cbs(void)
  1416. {
  1417. bool cbs_ready = false;
  1418. struct rcu_data *rdp;
  1419. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  1420. struct rcu_node *rnp;
  1421. struct rcu_state *rsp;
  1422. /* Exit early if we advanced recently. */
  1423. if (jiffies == rdtp->last_advance_all)
  1424. return 0;
  1425. rdtp->last_advance_all = jiffies;
  1426. for_each_rcu_flavor(rsp) {
  1427. rdp = this_cpu_ptr(rsp->rda);
  1428. rnp = rdp->mynode;
  1429. /*
  1430. * Don't bother checking unless a grace period has
  1431. * completed since we last checked and there are
  1432. * callbacks not yet ready to invoke.
  1433. */
  1434. if (rdp->completed != rnp->completed &&
  1435. rdp->nxttail[RCU_DONE_TAIL] != rdp->nxttail[RCU_NEXT_TAIL])
  1436. note_gp_changes(rsp, rdp);
  1437. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1438. cbs_ready = true;
  1439. }
  1440. return cbs_ready;
  1441. }
  1442. /*
  1443. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  1444. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  1445. * caller to set the timeout based on whether or not there are non-lazy
  1446. * callbacks.
  1447. *
  1448. * The caller must have disabled interrupts.
  1449. */
  1450. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1451. int rcu_needs_cpu(int cpu, unsigned long *dj)
  1452. {
  1453. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1454. /* Snapshot to detect later posting of non-lazy callback. */
  1455. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1456. /* If no callbacks, RCU doesn't need the CPU. */
  1457. if (!rcu_cpu_has_callbacks(cpu, &rdtp->all_lazy)) {
  1458. *dj = ULONG_MAX;
  1459. return 0;
  1460. }
  1461. /* Attempt to advance callbacks. */
  1462. if (rcu_try_advance_all_cbs()) {
  1463. /* Some ready to invoke, so initiate later invocation. */
  1464. invoke_rcu_core();
  1465. return 1;
  1466. }
  1467. rdtp->last_accelerate = jiffies;
  1468. /* Request timer delay depending on laziness, and round. */
  1469. if (!rdtp->all_lazy) {
  1470. *dj = round_up(rcu_idle_gp_delay + jiffies,
  1471. rcu_idle_gp_delay) - jiffies;
  1472. } else {
  1473. *dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
  1474. }
  1475. return 0;
  1476. }
  1477. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1478. /*
  1479. * Prepare a CPU for idle from an RCU perspective. The first major task
  1480. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  1481. * The second major task is to check to see if a non-lazy callback has
  1482. * arrived at a CPU that previously had only lazy callbacks. The third
  1483. * major task is to accelerate (that is, assign grace-period numbers to)
  1484. * any recently arrived callbacks.
  1485. *
  1486. * The caller must have disabled interrupts.
  1487. */
  1488. static void rcu_prepare_for_idle(int cpu)
  1489. {
  1490. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1491. bool needwake;
  1492. struct rcu_data *rdp;
  1493. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1494. struct rcu_node *rnp;
  1495. struct rcu_state *rsp;
  1496. int tne;
  1497. /* Handle nohz enablement switches conservatively. */
  1498. tne = ACCESS_ONCE(tick_nohz_active);
  1499. if (tne != rdtp->tick_nohz_enabled_snap) {
  1500. if (rcu_cpu_has_callbacks(cpu, NULL))
  1501. invoke_rcu_core(); /* force nohz to see update. */
  1502. rdtp->tick_nohz_enabled_snap = tne;
  1503. return;
  1504. }
  1505. if (!tne)
  1506. return;
  1507. /* If this is a no-CBs CPU, no callbacks, just return. */
  1508. if (rcu_is_nocb_cpu(cpu))
  1509. return;
  1510. /*
  1511. * If a non-lazy callback arrived at a CPU having only lazy
  1512. * callbacks, invoke RCU core for the side-effect of recalculating
  1513. * idle duration on re-entry to idle.
  1514. */
  1515. if (rdtp->all_lazy &&
  1516. rdtp->nonlazy_posted != rdtp->nonlazy_posted_snap) {
  1517. rdtp->all_lazy = false;
  1518. rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
  1519. invoke_rcu_core();
  1520. return;
  1521. }
  1522. /*
  1523. * If we have not yet accelerated this jiffy, accelerate all
  1524. * callbacks on this CPU.
  1525. */
  1526. if (rdtp->last_accelerate == jiffies)
  1527. return;
  1528. rdtp->last_accelerate = jiffies;
  1529. for_each_rcu_flavor(rsp) {
  1530. rdp = per_cpu_ptr(rsp->rda, cpu);
  1531. if (!*rdp->nxttail[RCU_DONE_TAIL])
  1532. continue;
  1533. rnp = rdp->mynode;
  1534. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  1535. smp_mb__after_unlock_lock();
  1536. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  1537. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1538. if (needwake)
  1539. rcu_gp_kthread_wake(rsp);
  1540. }
  1541. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1542. }
  1543. /*
  1544. * Clean up for exit from idle. Attempt to advance callbacks based on
  1545. * any grace periods that elapsed while the CPU was idle, and if any
  1546. * callbacks are now ready to invoke, initiate invocation.
  1547. */
  1548. static void rcu_cleanup_after_idle(int cpu)
  1549. {
  1550. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1551. if (rcu_is_nocb_cpu(cpu))
  1552. return;
  1553. if (rcu_try_advance_all_cbs())
  1554. invoke_rcu_core();
  1555. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1556. }
  1557. /*
  1558. * Keep a running count of the number of non-lazy callbacks posted
  1559. * on this CPU. This running counter (which is never decremented) allows
  1560. * rcu_prepare_for_idle() to detect when something out of the idle loop
  1561. * posts a callback, even if an equal number of callbacks are invoked.
  1562. * Of course, callbacks should only be posted from within a trace event
  1563. * designed to be called from idle or from within RCU_NONIDLE().
  1564. */
  1565. static void rcu_idle_count_callbacks_posted(void)
  1566. {
  1567. __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
  1568. }
  1569. /*
  1570. * Data for flushing lazy RCU callbacks at OOM time.
  1571. */
  1572. static atomic_t oom_callback_count;
  1573. static DECLARE_WAIT_QUEUE_HEAD(oom_callback_wq);
  1574. /*
  1575. * RCU OOM callback -- decrement the outstanding count and deliver the
  1576. * wake-up if we are the last one.
  1577. */
  1578. static void rcu_oom_callback(struct rcu_head *rhp)
  1579. {
  1580. if (atomic_dec_and_test(&oom_callback_count))
  1581. wake_up(&oom_callback_wq);
  1582. }
  1583. /*
  1584. * Post an rcu_oom_notify callback on the current CPU if it has at
  1585. * least one lazy callback. This will unnecessarily post callbacks
  1586. * to CPUs that already have a non-lazy callback at the end of their
  1587. * callback list, but this is an infrequent operation, so accept some
  1588. * extra overhead to keep things simple.
  1589. */
  1590. static void rcu_oom_notify_cpu(void *unused)
  1591. {
  1592. struct rcu_state *rsp;
  1593. struct rcu_data *rdp;
  1594. for_each_rcu_flavor(rsp) {
  1595. rdp = raw_cpu_ptr(rsp->rda);
  1596. if (rdp->qlen_lazy != 0) {
  1597. atomic_inc(&oom_callback_count);
  1598. rsp->call(&rdp->oom_head, rcu_oom_callback);
  1599. }
  1600. }
  1601. }
  1602. /*
  1603. * If low on memory, ensure that each CPU has a non-lazy callback.
  1604. * This will wake up CPUs that have only lazy callbacks, in turn
  1605. * ensuring that they free up the corresponding memory in a timely manner.
  1606. * Because an uncertain amount of memory will be freed in some uncertain
  1607. * timeframe, we do not claim to have freed anything.
  1608. */
  1609. static int rcu_oom_notify(struct notifier_block *self,
  1610. unsigned long notused, void *nfreed)
  1611. {
  1612. int cpu;
  1613. /* Wait for callbacks from earlier instance to complete. */
  1614. wait_event(oom_callback_wq, atomic_read(&oom_callback_count) == 0);
  1615. smp_mb(); /* Ensure callback reuse happens after callback invocation. */
  1616. /*
  1617. * Prevent premature wakeup: ensure that all increments happen
  1618. * before there is a chance of the counter reaching zero.
  1619. */
  1620. atomic_set(&oom_callback_count, 1);
  1621. get_online_cpus();
  1622. for_each_online_cpu(cpu) {
  1623. smp_call_function_single(cpu, rcu_oom_notify_cpu, NULL, 1);
  1624. cond_resched();
  1625. }
  1626. put_online_cpus();
  1627. /* Unconditionally decrement: no need to wake ourselves up. */
  1628. atomic_dec(&oom_callback_count);
  1629. return NOTIFY_OK;
  1630. }
  1631. static struct notifier_block rcu_oom_nb = {
  1632. .notifier_call = rcu_oom_notify
  1633. };
  1634. static int __init rcu_register_oom_notifier(void)
  1635. {
  1636. register_oom_notifier(&rcu_oom_nb);
  1637. return 0;
  1638. }
  1639. early_initcall(rcu_register_oom_notifier);
  1640. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1641. #ifdef CONFIG_RCU_CPU_STALL_INFO
  1642. #ifdef CONFIG_RCU_FAST_NO_HZ
  1643. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1644. {
  1645. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  1646. unsigned long nlpd = rdtp->nonlazy_posted - rdtp->nonlazy_posted_snap;
  1647. sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
  1648. rdtp->last_accelerate & 0xffff, jiffies & 0xffff,
  1649. ulong2long(nlpd),
  1650. rdtp->all_lazy ? 'L' : '.',
  1651. rdtp->tick_nohz_enabled_snap ? '.' : 'D');
  1652. }
  1653. #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
  1654. static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
  1655. {
  1656. *cp = '\0';
  1657. }
  1658. #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
  1659. /* Initiate the stall-info list. */
  1660. static void print_cpu_stall_info_begin(void)
  1661. {
  1662. pr_cont("\n");
  1663. }
  1664. /*
  1665. * Print out diagnostic information for the specified stalled CPU.
  1666. *
  1667. * If the specified CPU is aware of the current RCU grace period
  1668. * (flavor specified by rsp), then print the number of scheduling
  1669. * clock interrupts the CPU has taken during the time that it has
  1670. * been aware. Otherwise, print the number of RCU grace periods
  1671. * that this CPU is ignorant of, for example, "1" if the CPU was
  1672. * aware of the previous grace period.
  1673. *
  1674. * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
  1675. */
  1676. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1677. {
  1678. char fast_no_hz[72];
  1679. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1680. struct rcu_dynticks *rdtp = rdp->dynticks;
  1681. char *ticks_title;
  1682. unsigned long ticks_value;
  1683. if (rsp->gpnum == rdp->gpnum) {
  1684. ticks_title = "ticks this GP";
  1685. ticks_value = rdp->ticks_this_gp;
  1686. } else {
  1687. ticks_title = "GPs behind";
  1688. ticks_value = rsp->gpnum - rdp->gpnum;
  1689. }
  1690. print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
  1691. pr_err("\t%d: (%lu %s) idle=%03x/%llx/%d softirq=%u/%u %s\n",
  1692. cpu, ticks_value, ticks_title,
  1693. atomic_read(&rdtp->dynticks) & 0xfff,
  1694. rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
  1695. rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
  1696. fast_no_hz);
  1697. }
  1698. /* Terminate the stall-info list. */
  1699. static void print_cpu_stall_info_end(void)
  1700. {
  1701. pr_err("\t");
  1702. }
  1703. /* Zero ->ticks_this_gp for all flavors of RCU. */
  1704. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1705. {
  1706. rdp->ticks_this_gp = 0;
  1707. rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
  1708. }
  1709. /* Increment ->ticks_this_gp for all flavors of RCU. */
  1710. static void increment_cpu_stall_ticks(void)
  1711. {
  1712. struct rcu_state *rsp;
  1713. for_each_rcu_flavor(rsp)
  1714. raw_cpu_inc(rsp->rda->ticks_this_gp);
  1715. }
  1716. #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
  1717. static void print_cpu_stall_info_begin(void)
  1718. {
  1719. pr_cont(" {");
  1720. }
  1721. static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
  1722. {
  1723. pr_cont(" %d", cpu);
  1724. }
  1725. static void print_cpu_stall_info_end(void)
  1726. {
  1727. pr_cont("} ");
  1728. }
  1729. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  1730. {
  1731. }
  1732. static void increment_cpu_stall_ticks(void)
  1733. {
  1734. }
  1735. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
  1736. #ifdef CONFIG_RCU_NOCB_CPU
  1737. /*
  1738. * Offload callback processing from the boot-time-specified set of CPUs
  1739. * specified by rcu_nocb_mask. For each CPU in the set, there is a
  1740. * kthread created that pulls the callbacks from the corresponding CPU,
  1741. * waits for a grace period to elapse, and invokes the callbacks.
  1742. * The no-CBs CPUs do a wake_up() on their kthread when they insert
  1743. * a callback into any empty list, unless the rcu_nocb_poll boot parameter
  1744. * has been specified, in which case each kthread actively polls its
  1745. * CPU. (Which isn't so great for energy efficiency, but which does
  1746. * reduce RCU's overhead on that CPU.)
  1747. *
  1748. * This is intended to be used in conjunction with Frederic Weisbecker's
  1749. * adaptive-idle work, which would seriously reduce OS jitter on CPUs
  1750. * running CPU-bound user-mode computations.
  1751. *
  1752. * Offloading of callback processing could also in theory be used as
  1753. * an energy-efficiency measure because CPUs with no RCU callbacks
  1754. * queued are more aggressive about entering dyntick-idle mode.
  1755. */
  1756. /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
  1757. static int __init rcu_nocb_setup(char *str)
  1758. {
  1759. alloc_bootmem_cpumask_var(&rcu_nocb_mask);
  1760. have_rcu_nocb_mask = true;
  1761. cpulist_parse(str, rcu_nocb_mask);
  1762. return 1;
  1763. }
  1764. __setup("rcu_nocbs=", rcu_nocb_setup);
  1765. static int __init parse_rcu_nocb_poll(char *arg)
  1766. {
  1767. rcu_nocb_poll = 1;
  1768. return 0;
  1769. }
  1770. early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  1771. /*
  1772. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  1773. * grace period.
  1774. */
  1775. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  1776. {
  1777. wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  1778. }
  1779. /*
  1780. * Set the root rcu_node structure's ->need_future_gp field
  1781. * based on the sum of those of all rcu_node structures. This does
  1782. * double-count the root rcu_node structure's requests, but this
  1783. * is necessary to handle the possibility of a rcu_nocb_kthread()
  1784. * having awakened during the time that the rcu_node structures
  1785. * were being updated for the end of the previous grace period.
  1786. */
  1787. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  1788. {
  1789. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  1790. }
  1791. static void rcu_init_one_nocb(struct rcu_node *rnp)
  1792. {
  1793. init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  1794. init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  1795. }
  1796. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  1797. /* Is the specified CPU a no-CBs CPU? */
  1798. bool rcu_is_nocb_cpu(int cpu)
  1799. {
  1800. if (have_rcu_nocb_mask)
  1801. return cpumask_test_cpu(cpu, rcu_nocb_mask);
  1802. return false;
  1803. }
  1804. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  1805. /*
  1806. * Kick the leader kthread for this NOCB group.
  1807. */
  1808. static void wake_nocb_leader(struct rcu_data *rdp, bool force)
  1809. {
  1810. struct rcu_data *rdp_leader = rdp->nocb_leader;
  1811. if (!ACCESS_ONCE(rdp_leader->nocb_kthread))
  1812. return;
  1813. if (ACCESS_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  1814. /* Prior xchg orders against prior callback enqueue. */
  1815. ACCESS_ONCE(rdp_leader->nocb_leader_sleep) = false;
  1816. wake_up(&rdp_leader->nocb_wq);
  1817. }
  1818. }
  1819. /*
  1820. * Enqueue the specified string of rcu_head structures onto the specified
  1821. * CPU's no-CBs lists. The CPU is specified by rdp, the head of the
  1822. * string by rhp, and the tail of the string by rhtp. The non-lazy/lazy
  1823. * counts are supplied by rhcount and rhcount_lazy.
  1824. *
  1825. * If warranted, also wake up the kthread servicing this CPUs queues.
  1826. */
  1827. static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
  1828. struct rcu_head *rhp,
  1829. struct rcu_head **rhtp,
  1830. int rhcount, int rhcount_lazy,
  1831. unsigned long flags)
  1832. {
  1833. int len;
  1834. struct rcu_head **old_rhpp;
  1835. struct task_struct *t;
  1836. /* Enqueue the callback on the nocb list and update counts. */
  1837. old_rhpp = xchg(&rdp->nocb_tail, rhtp);
  1838. ACCESS_ONCE(*old_rhpp) = rhp;
  1839. atomic_long_add(rhcount, &rdp->nocb_q_count);
  1840. atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
  1841. /* If we are not being polled and there is a kthread, awaken it ... */
  1842. t = ACCESS_ONCE(rdp->nocb_kthread);
  1843. if (rcu_nocb_poll || !t) {
  1844. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1845. TPS("WakeNotPoll"));
  1846. return;
  1847. }
  1848. len = atomic_long_read(&rdp->nocb_q_count);
  1849. if (old_rhpp == &rdp->nocb_head) {
  1850. if (!irqs_disabled_flags(flags)) {
  1851. /* ... if queue was empty ... */
  1852. wake_nocb_leader(rdp, false);
  1853. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1854. TPS("WakeEmpty"));
  1855. } else {
  1856. rdp->nocb_defer_wakeup = true;
  1857. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  1858. TPS("WakeEmptyIsDeferred"));
  1859. }
  1860. rdp->qlen_last_fqs_check = 0;
  1861. } else if (len > rdp->qlen_last_fqs_check + qhimark) {
  1862. /* ... or if many callbacks queued. */
  1863. wake_nocb_leader(rdp, true);
  1864. rdp->qlen_last_fqs_check = LONG_MAX / 2;
  1865. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WakeOvf"));
  1866. } else {
  1867. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WakeNot"));
  1868. }
  1869. return;
  1870. }
  1871. /*
  1872. * This is a helper for __call_rcu(), which invokes this when the normal
  1873. * callback queue is inoperable. If this is not a no-CBs CPU, this
  1874. * function returns failure back to __call_rcu(), which can complain
  1875. * appropriately.
  1876. *
  1877. * Otherwise, this function queues the callback where the corresponding
  1878. * "rcuo" kthread can find it.
  1879. */
  1880. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  1881. bool lazy, unsigned long flags)
  1882. {
  1883. if (!rcu_is_nocb_cpu(rdp->cpu))
  1884. return 0;
  1885. __call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy, flags);
  1886. if (__is_kfree_rcu_offset((unsigned long)rhp->func))
  1887. trace_rcu_kfree_callback(rdp->rsp->name, rhp,
  1888. (unsigned long)rhp->func,
  1889. -atomic_long_read(&rdp->nocb_q_count_lazy),
  1890. -atomic_long_read(&rdp->nocb_q_count));
  1891. else
  1892. trace_rcu_callback(rdp->rsp->name, rhp,
  1893. -atomic_long_read(&rdp->nocb_q_count_lazy),
  1894. -atomic_long_read(&rdp->nocb_q_count));
  1895. return 1;
  1896. }
  1897. /*
  1898. * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
  1899. * not a no-CBs CPU.
  1900. */
  1901. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
  1902. struct rcu_data *rdp,
  1903. unsigned long flags)
  1904. {
  1905. long ql = rsp->qlen;
  1906. long qll = rsp->qlen_lazy;
  1907. /* If this is not a no-CBs CPU, tell the caller to do it the old way. */
  1908. if (!rcu_is_nocb_cpu(smp_processor_id()))
  1909. return 0;
  1910. rsp->qlen = 0;
  1911. rsp->qlen_lazy = 0;
  1912. /* First, enqueue the donelist, if any. This preserves CB ordering. */
  1913. if (rsp->orphan_donelist != NULL) {
  1914. __call_rcu_nocb_enqueue(rdp, rsp->orphan_donelist,
  1915. rsp->orphan_donetail, ql, qll, flags);
  1916. ql = qll = 0;
  1917. rsp->orphan_donelist = NULL;
  1918. rsp->orphan_donetail = &rsp->orphan_donelist;
  1919. }
  1920. if (rsp->orphan_nxtlist != NULL) {
  1921. __call_rcu_nocb_enqueue(rdp, rsp->orphan_nxtlist,
  1922. rsp->orphan_nxttail, ql, qll, flags);
  1923. ql = qll = 0;
  1924. rsp->orphan_nxtlist = NULL;
  1925. rsp->orphan_nxttail = &rsp->orphan_nxtlist;
  1926. }
  1927. return 1;
  1928. }
  1929. /*
  1930. * If necessary, kick off a new grace period, and either way wait
  1931. * for a subsequent grace period to complete.
  1932. */
  1933. static void rcu_nocb_wait_gp(struct rcu_data *rdp)
  1934. {
  1935. unsigned long c;
  1936. bool d;
  1937. unsigned long flags;
  1938. bool needwake;
  1939. struct rcu_node *rnp = rdp->mynode;
  1940. raw_spin_lock_irqsave(&rnp->lock, flags);
  1941. smp_mb__after_unlock_lock();
  1942. needwake = rcu_start_future_gp(rnp, rdp, &c);
  1943. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1944. if (needwake)
  1945. rcu_gp_kthread_wake(rdp->rsp);
  1946. /*
  1947. * Wait for the grace period. Do so interruptibly to avoid messing
  1948. * up the load average.
  1949. */
  1950. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  1951. for (;;) {
  1952. wait_event_interruptible(
  1953. rnp->nocb_gp_wq[c & 0x1],
  1954. (d = ULONG_CMP_GE(ACCESS_ONCE(rnp->completed), c)));
  1955. if (likely(d))
  1956. break;
  1957. flush_signals(current);
  1958. trace_rcu_future_gp(rnp, rdp, c, TPS("ResumeWait"));
  1959. }
  1960. trace_rcu_future_gp(rnp, rdp, c, TPS("EndWait"));
  1961. smp_mb(); /* Ensure that CB invocation happens after GP end. */
  1962. }
  1963. /*
  1964. * Leaders come here to wait for additional callbacks to show up.
  1965. * This function does not return until callbacks appear.
  1966. */
  1967. static void nocb_leader_wait(struct rcu_data *my_rdp)
  1968. {
  1969. bool firsttime = true;
  1970. bool gotcbs;
  1971. struct rcu_data *rdp;
  1972. struct rcu_head **tail;
  1973. wait_again:
  1974. /* Wait for callbacks to appear. */
  1975. if (!rcu_nocb_poll) {
  1976. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  1977. wait_event_interruptible(my_rdp->nocb_wq,
  1978. !ACCESS_ONCE(my_rdp->nocb_leader_sleep));
  1979. /* Memory barrier handled by smp_mb() calls below and repoll. */
  1980. } else if (firsttime) {
  1981. firsttime = false; /* Don't drown trace log with "Poll"! */
  1982. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Poll");
  1983. }
  1984. /*
  1985. * Each pass through the following loop checks a follower for CBs.
  1986. * We are our own first follower. Any CBs found are moved to
  1987. * nocb_gp_head, where they await a grace period.
  1988. */
  1989. gotcbs = false;
  1990. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  1991. rdp->nocb_gp_head = ACCESS_ONCE(rdp->nocb_head);
  1992. if (!rdp->nocb_gp_head)
  1993. continue; /* No CBs here, try next follower. */
  1994. /* Move callbacks to wait-for-GP list, which is empty. */
  1995. ACCESS_ONCE(rdp->nocb_head) = NULL;
  1996. rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
  1997. rdp->nocb_gp_count = atomic_long_xchg(&rdp->nocb_q_count, 0);
  1998. rdp->nocb_gp_count_lazy =
  1999. atomic_long_xchg(&rdp->nocb_q_count_lazy, 0);
  2000. gotcbs = true;
  2001. }
  2002. /*
  2003. * If there were no callbacks, sleep a bit, rescan after a
  2004. * memory barrier, and go retry.
  2005. */
  2006. if (unlikely(!gotcbs)) {
  2007. if (!rcu_nocb_poll)
  2008. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu,
  2009. "WokeEmpty");
  2010. flush_signals(current);
  2011. schedule_timeout_interruptible(1);
  2012. /* Rescan in case we were a victim of memory ordering. */
  2013. my_rdp->nocb_leader_sleep = true;
  2014. smp_mb(); /* Ensure _sleep true before scan. */
  2015. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower)
  2016. if (ACCESS_ONCE(rdp->nocb_head)) {
  2017. /* Found CB, so short-circuit next wait. */
  2018. my_rdp->nocb_leader_sleep = false;
  2019. break;
  2020. }
  2021. goto wait_again;
  2022. }
  2023. /* Wait for one grace period. */
  2024. rcu_nocb_wait_gp(my_rdp);
  2025. /*
  2026. * We left ->nocb_leader_sleep unset to reduce cache thrashing.
  2027. * We set it now, but recheck for new callbacks while
  2028. * traversing our follower list.
  2029. */
  2030. my_rdp->nocb_leader_sleep = true;
  2031. smp_mb(); /* Ensure _sleep true before scan of ->nocb_head. */
  2032. /* Each pass through the following loop wakes a follower, if needed. */
  2033. for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
  2034. if (ACCESS_ONCE(rdp->nocb_head))
  2035. my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
  2036. if (!rdp->nocb_gp_head)
  2037. continue; /* No CBs, so no need to wake follower. */
  2038. /* Append callbacks to follower's "done" list. */
  2039. tail = xchg(&rdp->nocb_follower_tail, rdp->nocb_gp_tail);
  2040. *tail = rdp->nocb_gp_head;
  2041. atomic_long_add(rdp->nocb_gp_count, &rdp->nocb_follower_count);
  2042. atomic_long_add(rdp->nocb_gp_count_lazy,
  2043. &rdp->nocb_follower_count_lazy);
  2044. if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
  2045. /*
  2046. * List was empty, wake up the follower.
  2047. * Memory barriers supplied by atomic_long_add().
  2048. */
  2049. wake_up(&rdp->nocb_wq);
  2050. }
  2051. }
  2052. /* If we (the leader) don't have CBs, go wait some more. */
  2053. if (!my_rdp->nocb_follower_head)
  2054. goto wait_again;
  2055. }
  2056. /*
  2057. * Followers come here to wait for additional callbacks to show up.
  2058. * This function does not return until callbacks appear.
  2059. */
  2060. static void nocb_follower_wait(struct rcu_data *rdp)
  2061. {
  2062. bool firsttime = true;
  2063. for (;;) {
  2064. if (!rcu_nocb_poll) {
  2065. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2066. "FollowerSleep");
  2067. wait_event_interruptible(rdp->nocb_wq,
  2068. ACCESS_ONCE(rdp->nocb_follower_head));
  2069. } else if (firsttime) {
  2070. /* Don't drown trace log with "Poll"! */
  2071. firsttime = false;
  2072. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "Poll");
  2073. }
  2074. if (smp_load_acquire(&rdp->nocb_follower_head)) {
  2075. /* ^^^ Ensure CB invocation follows _head test. */
  2076. return;
  2077. }
  2078. if (!rcu_nocb_poll)
  2079. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2080. "WokeEmpty");
  2081. flush_signals(current);
  2082. schedule_timeout_interruptible(1);
  2083. }
  2084. }
  2085. /*
  2086. * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
  2087. * callbacks queued by the corresponding no-CBs CPU, however, there is
  2088. * an optional leader-follower relationship so that the grace-period
  2089. * kthreads don't have to do quite so many wakeups.
  2090. */
  2091. static int rcu_nocb_kthread(void *arg)
  2092. {
  2093. int c, cl;
  2094. struct rcu_head *list;
  2095. struct rcu_head *next;
  2096. struct rcu_head **tail;
  2097. struct rcu_data *rdp = arg;
  2098. /* Each pass through this loop invokes one batch of callbacks */
  2099. for (;;) {
  2100. /* Wait for callbacks. */
  2101. if (rdp->nocb_leader == rdp)
  2102. nocb_leader_wait(rdp);
  2103. else
  2104. nocb_follower_wait(rdp);
  2105. /* Pull the ready-to-invoke callbacks onto local list. */
  2106. list = ACCESS_ONCE(rdp->nocb_follower_head);
  2107. BUG_ON(!list);
  2108. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "WokeNonEmpty");
  2109. ACCESS_ONCE(rdp->nocb_follower_head) = NULL;
  2110. tail = xchg(&rdp->nocb_follower_tail, &rdp->nocb_follower_head);
  2111. c = atomic_long_xchg(&rdp->nocb_follower_count, 0);
  2112. cl = atomic_long_xchg(&rdp->nocb_follower_count_lazy, 0);
  2113. rdp->nocb_p_count += c;
  2114. rdp->nocb_p_count_lazy += cl;
  2115. /* Each pass through the following loop invokes a callback. */
  2116. trace_rcu_batch_start(rdp->rsp->name, cl, c, -1);
  2117. c = cl = 0;
  2118. while (list) {
  2119. next = list->next;
  2120. /* Wait for enqueuing to complete, if needed. */
  2121. while (next == NULL && &list->next != tail) {
  2122. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2123. TPS("WaitQueue"));
  2124. schedule_timeout_interruptible(1);
  2125. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  2126. TPS("WokeQueue"));
  2127. next = list->next;
  2128. }
  2129. debug_rcu_head_unqueue(list);
  2130. local_bh_disable();
  2131. if (__rcu_reclaim(rdp->rsp->name, list))
  2132. cl++;
  2133. c++;
  2134. local_bh_enable();
  2135. list = next;
  2136. }
  2137. trace_rcu_batch_end(rdp->rsp->name, c, !!list, 0, 0, 1);
  2138. ACCESS_ONCE(rdp->nocb_p_count) -= c;
  2139. ACCESS_ONCE(rdp->nocb_p_count_lazy) -= cl;
  2140. rdp->n_nocbs_invoked += c;
  2141. }
  2142. return 0;
  2143. }
  2144. /* Is a deferred wakeup of rcu_nocb_kthread() required? */
  2145. static bool rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  2146. {
  2147. return ACCESS_ONCE(rdp->nocb_defer_wakeup);
  2148. }
  2149. /* Do a deferred wakeup of rcu_nocb_kthread(). */
  2150. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  2151. {
  2152. if (!rcu_nocb_need_deferred_wakeup(rdp))
  2153. return;
  2154. ACCESS_ONCE(rdp->nocb_defer_wakeup) = false;
  2155. wake_nocb_leader(rdp, false);
  2156. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("DeferredWakeEmpty"));
  2157. }
  2158. void __init rcu_init_nohz(void)
  2159. {
  2160. int cpu;
  2161. bool need_rcu_nocb_mask = true;
  2162. struct rcu_state *rsp;
  2163. #ifdef CONFIG_RCU_NOCB_CPU_NONE
  2164. need_rcu_nocb_mask = false;
  2165. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_NONE */
  2166. #if defined(CONFIG_NO_HZ_FULL)
  2167. if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
  2168. need_rcu_nocb_mask = true;
  2169. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2170. if (!have_rcu_nocb_mask && need_rcu_nocb_mask) {
  2171. zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL);
  2172. have_rcu_nocb_mask = true;
  2173. }
  2174. if (!have_rcu_nocb_mask)
  2175. return;
  2176. #ifdef CONFIG_RCU_NOCB_CPU_ZERO
  2177. pr_info("\tOffload RCU callbacks from CPU 0\n");
  2178. cpumask_set_cpu(0, rcu_nocb_mask);
  2179. #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ZERO */
  2180. #ifdef CONFIG_RCU_NOCB_CPU_ALL
  2181. pr_info("\tOffload RCU callbacks from all CPUs\n");
  2182. cpumask_copy(rcu_nocb_mask, cpu_possible_mask);
  2183. #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ALL */
  2184. #if defined(CONFIG_NO_HZ_FULL)
  2185. if (tick_nohz_full_running)
  2186. cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
  2187. #endif /* #if defined(CONFIG_NO_HZ_FULL) */
  2188. if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
  2189. pr_info("\tNote: kernel parameter 'rcu_nocbs=' contains nonexistent CPUs.\n");
  2190. cpumask_and(rcu_nocb_mask, cpu_possible_mask,
  2191. rcu_nocb_mask);
  2192. }
  2193. cpulist_scnprintf(nocb_buf, sizeof(nocb_buf), rcu_nocb_mask);
  2194. pr_info("\tOffload RCU callbacks from CPUs: %s.\n", nocb_buf);
  2195. if (rcu_nocb_poll)
  2196. pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
  2197. for_each_rcu_flavor(rsp) {
  2198. for_each_cpu(cpu, rcu_nocb_mask) {
  2199. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  2200. /*
  2201. * If there are early callbacks, they will need
  2202. * to be moved to the nocb lists.
  2203. */
  2204. WARN_ON_ONCE(rdp->nxttail[RCU_NEXT_TAIL] !=
  2205. &rdp->nxtlist &&
  2206. rdp->nxttail[RCU_NEXT_TAIL] != NULL);
  2207. init_nocb_callback_list(rdp);
  2208. }
  2209. }
  2210. }
  2211. /* Initialize per-rcu_data variables for no-CBs CPUs. */
  2212. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2213. {
  2214. rdp->nocb_tail = &rdp->nocb_head;
  2215. init_waitqueue_head(&rdp->nocb_wq);
  2216. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  2217. }
  2218. /* How many follower CPU IDs per leader? Default of -1 for sqrt(nr_cpu_ids). */
  2219. static int rcu_nocb_leader_stride = -1;
  2220. module_param(rcu_nocb_leader_stride, int, 0444);
  2221. /*
  2222. * Create a kthread for each RCU flavor for each no-CBs CPU.
  2223. * Also initialize leader-follower relationships.
  2224. */
  2225. static void __init rcu_spawn_nocb_kthreads(struct rcu_state *rsp)
  2226. {
  2227. int cpu;
  2228. int ls = rcu_nocb_leader_stride;
  2229. int nl = 0; /* Next leader. */
  2230. struct rcu_data *rdp;
  2231. struct rcu_data *rdp_leader = NULL; /* Suppress misguided gcc warn. */
  2232. struct rcu_data *rdp_prev = NULL;
  2233. struct task_struct *t;
  2234. if (rcu_nocb_mask == NULL)
  2235. return;
  2236. if (ls == -1) {
  2237. ls = int_sqrt(nr_cpu_ids);
  2238. rcu_nocb_leader_stride = ls;
  2239. }
  2240. /*
  2241. * Each pass through this loop sets up one rcu_data structure and
  2242. * spawns one rcu_nocb_kthread().
  2243. */
  2244. for_each_cpu(cpu, rcu_nocb_mask) {
  2245. rdp = per_cpu_ptr(rsp->rda, cpu);
  2246. if (rdp->cpu >= nl) {
  2247. /* New leader, set up for followers & next leader. */
  2248. nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
  2249. rdp->nocb_leader = rdp;
  2250. rdp_leader = rdp;
  2251. } else {
  2252. /* Another follower, link to previous leader. */
  2253. rdp->nocb_leader = rdp_leader;
  2254. rdp_prev->nocb_next_follower = rdp;
  2255. }
  2256. rdp_prev = rdp;
  2257. /* Spawn the kthread for this CPU. */
  2258. t = kthread_run(rcu_nocb_kthread, rdp,
  2259. "rcuo%c/%d", rsp->abbr, cpu);
  2260. BUG_ON(IS_ERR(t));
  2261. ACCESS_ONCE(rdp->nocb_kthread) = t;
  2262. }
  2263. }
  2264. /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
  2265. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2266. {
  2267. if (rcu_nocb_mask == NULL ||
  2268. !cpumask_test_cpu(rdp->cpu, rcu_nocb_mask))
  2269. return false;
  2270. rdp->nxttail[RCU_NEXT_TAIL] = NULL;
  2271. return true;
  2272. }
  2273. #else /* #ifdef CONFIG_RCU_NOCB_CPU */
  2274. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  2275. {
  2276. }
  2277. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  2278. {
  2279. }
  2280. static void rcu_init_one_nocb(struct rcu_node *rnp)
  2281. {
  2282. }
  2283. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  2284. bool lazy, unsigned long flags)
  2285. {
  2286. return 0;
  2287. }
  2288. static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
  2289. struct rcu_data *rdp,
  2290. unsigned long flags)
  2291. {
  2292. return 0;
  2293. }
  2294. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  2295. {
  2296. }
  2297. static bool rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
  2298. {
  2299. return false;
  2300. }
  2301. static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
  2302. {
  2303. }
  2304. static void __init rcu_spawn_nocb_kthreads(struct rcu_state *rsp)
  2305. {
  2306. }
  2307. static bool init_nocb_callback_list(struct rcu_data *rdp)
  2308. {
  2309. return false;
  2310. }
  2311. #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
  2312. /*
  2313. * An adaptive-ticks CPU can potentially execute in kernel mode for an
  2314. * arbitrarily long period of time with the scheduling-clock tick turned
  2315. * off. RCU will be paying attention to this CPU because it is in the
  2316. * kernel, but the CPU cannot be guaranteed to be executing the RCU state
  2317. * machine because the scheduling-clock tick has been disabled. Therefore,
  2318. * if an adaptive-ticks CPU is failing to respond to the current grace
  2319. * period and has not be idle from an RCU perspective, kick it.
  2320. */
  2321. static void __maybe_unused rcu_kick_nohz_cpu(int cpu)
  2322. {
  2323. #ifdef CONFIG_NO_HZ_FULL
  2324. if (tick_nohz_full_cpu(cpu))
  2325. smp_send_reschedule(cpu);
  2326. #endif /* #ifdef CONFIG_NO_HZ_FULL */
  2327. }
  2328. #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
  2329. /*
  2330. * Define RCU flavor that holds sysidle state. This needs to be the
  2331. * most active flavor of RCU.
  2332. */
  2333. #ifdef CONFIG_PREEMPT_RCU
  2334. static struct rcu_state *rcu_sysidle_state = &rcu_preempt_state;
  2335. #else /* #ifdef CONFIG_PREEMPT_RCU */
  2336. static struct rcu_state *rcu_sysidle_state = &rcu_sched_state;
  2337. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  2338. static int full_sysidle_state; /* Current system-idle state. */
  2339. #define RCU_SYSIDLE_NOT 0 /* Some CPU is not idle. */
  2340. #define RCU_SYSIDLE_SHORT 1 /* All CPUs idle for brief period. */
  2341. #define RCU_SYSIDLE_LONG 2 /* All CPUs idle for long enough. */
  2342. #define RCU_SYSIDLE_FULL 3 /* All CPUs idle, ready for sysidle. */
  2343. #define RCU_SYSIDLE_FULL_NOTED 4 /* Actually entered sysidle state. */
  2344. /*
  2345. * Invoked to note exit from irq or task transition to idle. Note that
  2346. * usermode execution does -not- count as idle here! After all, we want
  2347. * to detect full-system idle states, not RCU quiescent states and grace
  2348. * periods. The caller must have disabled interrupts.
  2349. */
  2350. static void rcu_sysidle_enter(struct rcu_dynticks *rdtp, int irq)
  2351. {
  2352. unsigned long j;
  2353. /* Adjust nesting, check for fully idle. */
  2354. if (irq) {
  2355. rdtp->dynticks_idle_nesting--;
  2356. WARN_ON_ONCE(rdtp->dynticks_idle_nesting < 0);
  2357. if (rdtp->dynticks_idle_nesting != 0)
  2358. return; /* Still not fully idle. */
  2359. } else {
  2360. if ((rdtp->dynticks_idle_nesting & DYNTICK_TASK_NEST_MASK) ==
  2361. DYNTICK_TASK_NEST_VALUE) {
  2362. rdtp->dynticks_idle_nesting = 0;
  2363. } else {
  2364. rdtp->dynticks_idle_nesting -= DYNTICK_TASK_NEST_VALUE;
  2365. WARN_ON_ONCE(rdtp->dynticks_idle_nesting < 0);
  2366. return; /* Still not fully idle. */
  2367. }
  2368. }
  2369. /* Record start of fully idle period. */
  2370. j = jiffies;
  2371. ACCESS_ONCE(rdtp->dynticks_idle_jiffies) = j;
  2372. smp_mb__before_atomic();
  2373. atomic_inc(&rdtp->dynticks_idle);
  2374. smp_mb__after_atomic();
  2375. WARN_ON_ONCE(atomic_read(&rdtp->dynticks_idle) & 0x1);
  2376. }
  2377. /*
  2378. * Unconditionally force exit from full system-idle state. This is
  2379. * invoked when a normal CPU exits idle, but must be called separately
  2380. * for the timekeeping CPU (tick_do_timer_cpu). The reason for this
  2381. * is that the timekeeping CPU is permitted to take scheduling-clock
  2382. * interrupts while the system is in system-idle state, and of course
  2383. * rcu_sysidle_exit() has no way of distinguishing a scheduling-clock
  2384. * interrupt from any other type of interrupt.
  2385. */
  2386. void rcu_sysidle_force_exit(void)
  2387. {
  2388. int oldstate = ACCESS_ONCE(full_sysidle_state);
  2389. int newoldstate;
  2390. /*
  2391. * Each pass through the following loop attempts to exit full
  2392. * system-idle state. If contention proves to be a problem,
  2393. * a trylock-based contention tree could be used here.
  2394. */
  2395. while (oldstate > RCU_SYSIDLE_SHORT) {
  2396. newoldstate = cmpxchg(&full_sysidle_state,
  2397. oldstate, RCU_SYSIDLE_NOT);
  2398. if (oldstate == newoldstate &&
  2399. oldstate == RCU_SYSIDLE_FULL_NOTED) {
  2400. rcu_kick_nohz_cpu(tick_do_timer_cpu);
  2401. return; /* We cleared it, done! */
  2402. }
  2403. oldstate = newoldstate;
  2404. }
  2405. smp_mb(); /* Order initial oldstate fetch vs. later non-idle work. */
  2406. }
  2407. /*
  2408. * Invoked to note entry to irq or task transition from idle. Note that
  2409. * usermode execution does -not- count as idle here! The caller must
  2410. * have disabled interrupts.
  2411. */
  2412. static void rcu_sysidle_exit(struct rcu_dynticks *rdtp, int irq)
  2413. {
  2414. /* Adjust nesting, check for already non-idle. */
  2415. if (irq) {
  2416. rdtp->dynticks_idle_nesting++;
  2417. WARN_ON_ONCE(rdtp->dynticks_idle_nesting <= 0);
  2418. if (rdtp->dynticks_idle_nesting != 1)
  2419. return; /* Already non-idle. */
  2420. } else {
  2421. /*
  2422. * Allow for irq misnesting. Yes, it really is possible
  2423. * to enter an irq handler then never leave it, and maybe
  2424. * also vice versa. Handle both possibilities.
  2425. */
  2426. if (rdtp->dynticks_idle_nesting & DYNTICK_TASK_NEST_MASK) {
  2427. rdtp->dynticks_idle_nesting += DYNTICK_TASK_NEST_VALUE;
  2428. WARN_ON_ONCE(rdtp->dynticks_idle_nesting <= 0);
  2429. return; /* Already non-idle. */
  2430. } else {
  2431. rdtp->dynticks_idle_nesting = DYNTICK_TASK_EXIT_IDLE;
  2432. }
  2433. }
  2434. /* Record end of idle period. */
  2435. smp_mb__before_atomic();
  2436. atomic_inc(&rdtp->dynticks_idle);
  2437. smp_mb__after_atomic();
  2438. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks_idle) & 0x1));
  2439. /*
  2440. * If we are the timekeeping CPU, we are permitted to be non-idle
  2441. * during a system-idle state. This must be the case, because
  2442. * the timekeeping CPU has to take scheduling-clock interrupts
  2443. * during the time that the system is transitioning to full
  2444. * system-idle state. This means that the timekeeping CPU must
  2445. * invoke rcu_sysidle_force_exit() directly if it does anything
  2446. * more than take a scheduling-clock interrupt.
  2447. */
  2448. if (smp_processor_id() == tick_do_timer_cpu)
  2449. return;
  2450. /* Update system-idle state: We are clearly no longer fully idle! */
  2451. rcu_sysidle_force_exit();
  2452. }
  2453. /*
  2454. * Check to see if the current CPU is idle. Note that usermode execution
  2455. * does not count as idle. The caller must have disabled interrupts.
  2456. */
  2457. static void rcu_sysidle_check_cpu(struct rcu_data *rdp, bool *isidle,
  2458. unsigned long *maxj)
  2459. {
  2460. int cur;
  2461. unsigned long j;
  2462. struct rcu_dynticks *rdtp = rdp->dynticks;
  2463. /*
  2464. * If some other CPU has already reported non-idle, if this is
  2465. * not the flavor of RCU that tracks sysidle state, or if this
  2466. * is an offline or the timekeeping CPU, nothing to do.
  2467. */
  2468. if (!*isidle || rdp->rsp != rcu_sysidle_state ||
  2469. cpu_is_offline(rdp->cpu) || rdp->cpu == tick_do_timer_cpu)
  2470. return;
  2471. if (rcu_gp_in_progress(rdp->rsp))
  2472. WARN_ON_ONCE(smp_processor_id() != tick_do_timer_cpu);
  2473. /* Pick up current idle and NMI-nesting counter and check. */
  2474. cur = atomic_read(&rdtp->dynticks_idle);
  2475. if (cur & 0x1) {
  2476. *isidle = false; /* We are not idle! */
  2477. return;
  2478. }
  2479. smp_mb(); /* Read counters before timestamps. */
  2480. /* Pick up timestamps. */
  2481. j = ACCESS_ONCE(rdtp->dynticks_idle_jiffies);
  2482. /* If this CPU entered idle more recently, update maxj timestamp. */
  2483. if (ULONG_CMP_LT(*maxj, j))
  2484. *maxj = j;
  2485. }
  2486. /*
  2487. * Is this the flavor of RCU that is handling full-system idle?
  2488. */
  2489. static bool is_sysidle_rcu_state(struct rcu_state *rsp)
  2490. {
  2491. return rsp == rcu_sysidle_state;
  2492. }
  2493. /*
  2494. * Return a delay in jiffies based on the number of CPUs, rcu_node
  2495. * leaf fanout, and jiffies tick rate. The idea is to allow larger
  2496. * systems more time to transition to full-idle state in order to
  2497. * avoid the cache thrashing that otherwise occur on the state variable.
  2498. * Really small systems (less than a couple of tens of CPUs) should
  2499. * instead use a single global atomically incremented counter, and later
  2500. * versions of this will automatically reconfigure themselves accordingly.
  2501. */
  2502. static unsigned long rcu_sysidle_delay(void)
  2503. {
  2504. if (nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL)
  2505. return 0;
  2506. return DIV_ROUND_UP(nr_cpu_ids * HZ, rcu_fanout_leaf * 1000);
  2507. }
  2508. /*
  2509. * Advance the full-system-idle state. This is invoked when all of
  2510. * the non-timekeeping CPUs are idle.
  2511. */
  2512. static void rcu_sysidle(unsigned long j)
  2513. {
  2514. /* Check the current state. */
  2515. switch (ACCESS_ONCE(full_sysidle_state)) {
  2516. case RCU_SYSIDLE_NOT:
  2517. /* First time all are idle, so note a short idle period. */
  2518. ACCESS_ONCE(full_sysidle_state) = RCU_SYSIDLE_SHORT;
  2519. break;
  2520. case RCU_SYSIDLE_SHORT:
  2521. /*
  2522. * Idle for a bit, time to advance to next state?
  2523. * cmpxchg failure means race with non-idle, let them win.
  2524. */
  2525. if (ULONG_CMP_GE(jiffies, j + rcu_sysidle_delay()))
  2526. (void)cmpxchg(&full_sysidle_state,
  2527. RCU_SYSIDLE_SHORT, RCU_SYSIDLE_LONG);
  2528. break;
  2529. case RCU_SYSIDLE_LONG:
  2530. /*
  2531. * Do an additional check pass before advancing to full.
  2532. * cmpxchg failure means race with non-idle, let them win.
  2533. */
  2534. if (ULONG_CMP_GE(jiffies, j + rcu_sysidle_delay()))
  2535. (void)cmpxchg(&full_sysidle_state,
  2536. RCU_SYSIDLE_LONG, RCU_SYSIDLE_FULL);
  2537. break;
  2538. default:
  2539. break;
  2540. }
  2541. }
  2542. /*
  2543. * Found a non-idle non-timekeeping CPU, so kick the system-idle state
  2544. * back to the beginning.
  2545. */
  2546. static void rcu_sysidle_cancel(void)
  2547. {
  2548. smp_mb();
  2549. if (full_sysidle_state > RCU_SYSIDLE_SHORT)
  2550. ACCESS_ONCE(full_sysidle_state) = RCU_SYSIDLE_NOT;
  2551. }
  2552. /*
  2553. * Update the sysidle state based on the results of a force-quiescent-state
  2554. * scan of the CPUs' dyntick-idle state.
  2555. */
  2556. static void rcu_sysidle_report(struct rcu_state *rsp, int isidle,
  2557. unsigned long maxj, bool gpkt)
  2558. {
  2559. if (rsp != rcu_sysidle_state)
  2560. return; /* Wrong flavor, ignore. */
  2561. if (gpkt && nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL)
  2562. return; /* Running state machine from timekeeping CPU. */
  2563. if (isidle)
  2564. rcu_sysidle(maxj); /* More idle! */
  2565. else
  2566. rcu_sysidle_cancel(); /* Idle is over. */
  2567. }
  2568. /*
  2569. * Wrapper for rcu_sysidle_report() when called from the grace-period
  2570. * kthread's context.
  2571. */
  2572. static void rcu_sysidle_report_gp(struct rcu_state *rsp, int isidle,
  2573. unsigned long maxj)
  2574. {
  2575. rcu_sysidle_report(rsp, isidle, maxj, true);
  2576. }
  2577. /* Callback and function for forcing an RCU grace period. */
  2578. struct rcu_sysidle_head {
  2579. struct rcu_head rh;
  2580. int inuse;
  2581. };
  2582. static void rcu_sysidle_cb(struct rcu_head *rhp)
  2583. {
  2584. struct rcu_sysidle_head *rshp;
  2585. /*
  2586. * The following memory barrier is needed to replace the
  2587. * memory barriers that would normally be in the memory
  2588. * allocator.
  2589. */
  2590. smp_mb(); /* grace period precedes setting inuse. */
  2591. rshp = container_of(rhp, struct rcu_sysidle_head, rh);
  2592. ACCESS_ONCE(rshp->inuse) = 0;
  2593. }
  2594. /*
  2595. * Check to see if the system is fully idle, other than the timekeeping CPU.
  2596. * The caller must have disabled interrupts.
  2597. */
  2598. bool rcu_sys_is_idle(void)
  2599. {
  2600. static struct rcu_sysidle_head rsh;
  2601. int rss = ACCESS_ONCE(full_sysidle_state);
  2602. if (WARN_ON_ONCE(smp_processor_id() != tick_do_timer_cpu))
  2603. return false;
  2604. /* Handle small-system case by doing a full scan of CPUs. */
  2605. if (nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL) {
  2606. int oldrss = rss - 1;
  2607. /*
  2608. * One pass to advance to each state up to _FULL.
  2609. * Give up if any pass fails to advance the state.
  2610. */
  2611. while (rss < RCU_SYSIDLE_FULL && oldrss < rss) {
  2612. int cpu;
  2613. bool isidle = true;
  2614. unsigned long maxj = jiffies - ULONG_MAX / 4;
  2615. struct rcu_data *rdp;
  2616. /* Scan all the CPUs looking for nonidle CPUs. */
  2617. for_each_possible_cpu(cpu) {
  2618. rdp = per_cpu_ptr(rcu_sysidle_state->rda, cpu);
  2619. rcu_sysidle_check_cpu(rdp, &isidle, &maxj);
  2620. if (!isidle)
  2621. break;
  2622. }
  2623. rcu_sysidle_report(rcu_sysidle_state,
  2624. isidle, maxj, false);
  2625. oldrss = rss;
  2626. rss = ACCESS_ONCE(full_sysidle_state);
  2627. }
  2628. }
  2629. /* If this is the first observation of an idle period, record it. */
  2630. if (rss == RCU_SYSIDLE_FULL) {
  2631. rss = cmpxchg(&full_sysidle_state,
  2632. RCU_SYSIDLE_FULL, RCU_SYSIDLE_FULL_NOTED);
  2633. return rss == RCU_SYSIDLE_FULL;
  2634. }
  2635. smp_mb(); /* ensure rss load happens before later caller actions. */
  2636. /* If already fully idle, tell the caller (in case of races). */
  2637. if (rss == RCU_SYSIDLE_FULL_NOTED)
  2638. return true;
  2639. /*
  2640. * If we aren't there yet, and a grace period is not in flight,
  2641. * initiate a grace period. Either way, tell the caller that
  2642. * we are not there yet. We use an xchg() rather than an assignment
  2643. * to make up for the memory barriers that would otherwise be
  2644. * provided by the memory allocator.
  2645. */
  2646. if (nr_cpu_ids > CONFIG_NO_HZ_FULL_SYSIDLE_SMALL &&
  2647. !rcu_gp_in_progress(rcu_sysidle_state) &&
  2648. !rsh.inuse && xchg(&rsh.inuse, 1) == 0)
  2649. call_rcu(&rsh.rh, rcu_sysidle_cb);
  2650. return false;
  2651. }
  2652. /*
  2653. * Initialize dynticks sysidle state for CPUs coming online.
  2654. */
  2655. static void rcu_sysidle_init_percpu_data(struct rcu_dynticks *rdtp)
  2656. {
  2657. rdtp->dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE;
  2658. }
  2659. #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  2660. static void rcu_sysidle_enter(struct rcu_dynticks *rdtp, int irq)
  2661. {
  2662. }
  2663. static void rcu_sysidle_exit(struct rcu_dynticks *rdtp, int irq)
  2664. {
  2665. }
  2666. static void rcu_sysidle_check_cpu(struct rcu_data *rdp, bool *isidle,
  2667. unsigned long *maxj)
  2668. {
  2669. }
  2670. static bool is_sysidle_rcu_state(struct rcu_state *rsp)
  2671. {
  2672. return false;
  2673. }
  2674. static void rcu_sysidle_report_gp(struct rcu_state *rsp, int isidle,
  2675. unsigned long maxj)
  2676. {
  2677. }
  2678. static void rcu_sysidle_init_percpu_data(struct rcu_dynticks *rdtp)
  2679. {
  2680. }
  2681. #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  2682. /*
  2683. * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
  2684. * grace-period kthread will do force_quiescent_state() processing?
  2685. * The idea is to avoid waking up RCU core processing on such a
  2686. * CPU unless the grace period has extended for too long.
  2687. *
  2688. * This code relies on the fact that all NO_HZ_FULL CPUs are also
  2689. * CONFIG_RCU_NOCB_CPU CPUs.
  2690. */
  2691. static bool rcu_nohz_full_cpu(struct rcu_state *rsp)
  2692. {
  2693. #ifdef CONFIG_NO_HZ_FULL
  2694. if (tick_nohz_full_cpu(smp_processor_id()) &&
  2695. (!rcu_gp_in_progress(rsp) ||
  2696. ULONG_CMP_LT(jiffies, ACCESS_ONCE(rsp->gp_start) + HZ)))
  2697. return 1;
  2698. #endif /* #ifdef CONFIG_NO_HZ_FULL */
  2699. return 0;
  2700. }
  2701. /*
  2702. * Bind the grace-period kthread for the sysidle flavor of RCU to the
  2703. * timekeeping CPU.
  2704. */
  2705. static void rcu_bind_gp_kthread(void)
  2706. {
  2707. int __maybe_unused cpu;
  2708. if (!tick_nohz_full_enabled())
  2709. return;
  2710. #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
  2711. cpu = tick_do_timer_cpu;
  2712. if (cpu >= 0 && cpu < nr_cpu_ids && raw_smp_processor_id() != cpu)
  2713. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  2714. #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  2715. if (!is_housekeeping_cpu(raw_smp_processor_id()))
  2716. housekeeping_affine(current);
  2717. #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  2718. }