tree_plugin.h 82 KB

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