tree_plugin.h 82 KB

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