rcutree_plugin.h 74 KB

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