tree_plugin.h 88 KB

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