tree_plugin.h 89 KB

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