rcutree_plugin.h 74 KB

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