tree_plugin.h 79 KB

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