rcutree_plugin.h 68 KB

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