tree_plugin.h 83 KB

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