tree_plugin.h 79 KB

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