tree_plugin.h 90 KB

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