tree_plugin.h 89 KB

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