tree_plugin.h 91 KB

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