tree_plugin.h 82 KB

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