tree_stall.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * RCU CPU stall warnings for normal RCU grace periods
  4. *
  5. * Copyright IBM Corporation, 2019
  6. *
  7. * Author: Paul E. McKenney <[email protected]>
  8. */
  9. #include <linux/kvm_para.h>
  10. //////////////////////////////////////////////////////////////////////////////
  11. //
  12. // Controlling CPU stall warnings, including delay calculation.
  13. /* panic() on RCU Stall sysctl. */
  14. int sysctl_panic_on_rcu_stall __read_mostly;
  15. int sysctl_max_rcu_stall_to_panic __read_mostly;
  16. #ifdef CONFIG_PROVE_RCU
  17. #define RCU_STALL_DELAY_DELTA (5 * HZ)
  18. #else
  19. #define RCU_STALL_DELAY_DELTA 0
  20. #endif
  21. #define RCU_STALL_MIGHT_DIV 8
  22. #define RCU_STALL_MIGHT_MIN (2 * HZ)
  23. int rcu_exp_jiffies_till_stall_check(void)
  24. {
  25. int cpu_stall_timeout = READ_ONCE(rcu_exp_cpu_stall_timeout);
  26. int exp_stall_delay_delta = 0;
  27. int till_stall_check;
  28. // Zero says to use rcu_cpu_stall_timeout, but in milliseconds.
  29. if (!cpu_stall_timeout)
  30. cpu_stall_timeout = jiffies_to_msecs(rcu_jiffies_till_stall_check());
  31. // Limit check must be consistent with the Kconfig limits for
  32. // CONFIG_RCU_EXP_CPU_STALL_TIMEOUT, so check the allowed range.
  33. // The minimum clamped value is "2UL", because at least one full
  34. // tick has to be guaranteed.
  35. till_stall_check = clamp(msecs_to_jiffies(cpu_stall_timeout), 2UL, 21UL * HZ);
  36. if (cpu_stall_timeout && jiffies_to_msecs(till_stall_check) != cpu_stall_timeout)
  37. WRITE_ONCE(rcu_exp_cpu_stall_timeout, jiffies_to_msecs(till_stall_check));
  38. #ifdef CONFIG_PROVE_RCU
  39. /* Add extra ~25% out of till_stall_check. */
  40. exp_stall_delay_delta = ((till_stall_check * 25) / 100) + 1;
  41. #endif
  42. return till_stall_check + exp_stall_delay_delta;
  43. }
  44. EXPORT_SYMBOL_GPL(rcu_exp_jiffies_till_stall_check);
  45. /* Limit-check stall timeouts specified at boottime and runtime. */
  46. int rcu_jiffies_till_stall_check(void)
  47. {
  48. int till_stall_check = READ_ONCE(rcu_cpu_stall_timeout);
  49. /*
  50. * Limit check must be consistent with the Kconfig limits
  51. * for CONFIG_RCU_CPU_STALL_TIMEOUT.
  52. */
  53. if (till_stall_check < 3) {
  54. WRITE_ONCE(rcu_cpu_stall_timeout, 3);
  55. till_stall_check = 3;
  56. } else if (till_stall_check > 300) {
  57. WRITE_ONCE(rcu_cpu_stall_timeout, 300);
  58. till_stall_check = 300;
  59. }
  60. return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
  61. }
  62. EXPORT_SYMBOL_GPL(rcu_jiffies_till_stall_check);
  63. /**
  64. * rcu_gp_might_be_stalled - Is it likely that the grace period is stalled?
  65. *
  66. * Returns @true if the current grace period is sufficiently old that
  67. * it is reasonable to assume that it might be stalled. This can be
  68. * useful when deciding whether to allocate memory to enable RCU-mediated
  69. * freeing on the one hand or just invoking synchronize_rcu() on the other.
  70. * The latter is preferable when the grace period is stalled.
  71. *
  72. * Note that sampling of the .gp_start and .gp_seq fields must be done
  73. * carefully to avoid false positives at the beginnings and ends of
  74. * grace periods.
  75. */
  76. bool rcu_gp_might_be_stalled(void)
  77. {
  78. unsigned long d = rcu_jiffies_till_stall_check() / RCU_STALL_MIGHT_DIV;
  79. unsigned long j = jiffies;
  80. if (d < RCU_STALL_MIGHT_MIN)
  81. d = RCU_STALL_MIGHT_MIN;
  82. smp_mb(); // jiffies before .gp_seq to avoid false positives.
  83. if (!rcu_gp_in_progress())
  84. return false;
  85. // Long delays at this point avoids false positive, but a delay
  86. // of ULONG_MAX/4 jiffies voids your no-false-positive warranty.
  87. smp_mb(); // .gp_seq before second .gp_start
  88. // And ditto here.
  89. return !time_before(j, READ_ONCE(rcu_state.gp_start) + d);
  90. }
  91. /* Don't do RCU CPU stall warnings during long sysrq printouts. */
  92. void rcu_sysrq_start(void)
  93. {
  94. if (!rcu_cpu_stall_suppress)
  95. rcu_cpu_stall_suppress = 2;
  96. }
  97. void rcu_sysrq_end(void)
  98. {
  99. if (rcu_cpu_stall_suppress == 2)
  100. rcu_cpu_stall_suppress = 0;
  101. }
  102. /* Don't print RCU CPU stall warnings during a kernel panic. */
  103. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  104. {
  105. rcu_cpu_stall_suppress = 1;
  106. return NOTIFY_DONE;
  107. }
  108. static struct notifier_block rcu_panic_block = {
  109. .notifier_call = rcu_panic,
  110. };
  111. static int __init check_cpu_stall_init(void)
  112. {
  113. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  114. return 0;
  115. }
  116. early_initcall(check_cpu_stall_init);
  117. /* If so specified via sysctl, panic, yielding cleaner stall-warning output. */
  118. static void panic_on_rcu_stall(void)
  119. {
  120. static int cpu_stall;
  121. if (++cpu_stall < sysctl_max_rcu_stall_to_panic)
  122. return;
  123. if (sysctl_panic_on_rcu_stall)
  124. panic("RCU Stall\n");
  125. }
  126. /**
  127. * rcu_cpu_stall_reset - restart stall-warning timeout for current grace period
  128. *
  129. * To perform the reset request from the caller, disable stall detection until
  130. * 3 fqs loops have passed. This is required to ensure a fresh jiffies is
  131. * loaded. It should be safe to do from the fqs loop as enough timer
  132. * interrupts and context switches should have passed.
  133. *
  134. * The caller must disable hard irqs.
  135. */
  136. void rcu_cpu_stall_reset(void)
  137. {
  138. WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, 3);
  139. WRITE_ONCE(rcu_state.jiffies_stall, ULONG_MAX);
  140. }
  141. //////////////////////////////////////////////////////////////////////////////
  142. //
  143. // Interaction with RCU grace periods
  144. /* Start of new grace period, so record stall time (and forcing times). */
  145. static void record_gp_stall_check_time(void)
  146. {
  147. unsigned long j = jiffies;
  148. unsigned long j1;
  149. WRITE_ONCE(rcu_state.gp_start, j);
  150. j1 = rcu_jiffies_till_stall_check();
  151. smp_mb(); // ->gp_start before ->jiffies_stall and caller's ->gp_seq.
  152. WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, 0);
  153. WRITE_ONCE(rcu_state.jiffies_stall, j + j1);
  154. rcu_state.jiffies_resched = j + j1 / 2;
  155. rcu_state.n_force_qs_gpstart = READ_ONCE(rcu_state.n_force_qs);
  156. }
  157. /* Zero ->ticks_this_gp and snapshot the number of RCU softirq handlers. */
  158. static void zero_cpu_stall_ticks(struct rcu_data *rdp)
  159. {
  160. rdp->ticks_this_gp = 0;
  161. rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
  162. WRITE_ONCE(rdp->last_fqs_resched, jiffies);
  163. }
  164. /*
  165. * If too much time has passed in the current grace period, and if
  166. * so configured, go kick the relevant kthreads.
  167. */
  168. static void rcu_stall_kick_kthreads(void)
  169. {
  170. unsigned long j;
  171. if (!READ_ONCE(rcu_kick_kthreads))
  172. return;
  173. j = READ_ONCE(rcu_state.jiffies_kick_kthreads);
  174. if (time_after(jiffies, j) && rcu_state.gp_kthread &&
  175. (rcu_gp_in_progress() || READ_ONCE(rcu_state.gp_flags))) {
  176. WARN_ONCE(1, "Kicking %s grace-period kthread\n",
  177. rcu_state.name);
  178. rcu_ftrace_dump(DUMP_ALL);
  179. wake_up_process(rcu_state.gp_kthread);
  180. WRITE_ONCE(rcu_state.jiffies_kick_kthreads, j + HZ);
  181. }
  182. }
  183. /*
  184. * Handler for the irq_work request posted about halfway into the RCU CPU
  185. * stall timeout, and used to detect excessive irq disabling. Set state
  186. * appropriately, but just complain if there is unexpected state on entry.
  187. */
  188. static void rcu_iw_handler(struct irq_work *iwp)
  189. {
  190. struct rcu_data *rdp;
  191. struct rcu_node *rnp;
  192. rdp = container_of(iwp, struct rcu_data, rcu_iw);
  193. rnp = rdp->mynode;
  194. raw_spin_lock_rcu_node(rnp);
  195. if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
  196. rdp->rcu_iw_gp_seq = rnp->gp_seq;
  197. rdp->rcu_iw_pending = false;
  198. }
  199. raw_spin_unlock_rcu_node(rnp);
  200. }
  201. //////////////////////////////////////////////////////////////////////////////
  202. //
  203. // Printing RCU CPU stall warnings
  204. #ifdef CONFIG_PREEMPT_RCU
  205. /*
  206. * Dump detailed information for all tasks blocking the current RCU
  207. * grace period on the specified rcu_node structure.
  208. */
  209. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  210. {
  211. unsigned long flags;
  212. struct task_struct *t;
  213. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  214. if (!rcu_preempt_blocked_readers_cgp(rnp)) {
  215. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  216. return;
  217. }
  218. t = list_entry(rnp->gp_tasks->prev,
  219. struct task_struct, rcu_node_entry);
  220. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  221. /*
  222. * We could be printing a lot while holding a spinlock.
  223. * Avoid triggering hard lockup.
  224. */
  225. touch_nmi_watchdog();
  226. sched_show_task(t);
  227. }
  228. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  229. }
  230. // Communicate task state back to the RCU CPU stall warning request.
  231. struct rcu_stall_chk_rdr {
  232. int nesting;
  233. union rcu_special rs;
  234. bool on_blkd_list;
  235. };
  236. /*
  237. * Report out the state of a not-running task that is stalling the
  238. * current RCU grace period.
  239. */
  240. static int check_slow_task(struct task_struct *t, void *arg)
  241. {
  242. struct rcu_stall_chk_rdr *rscrp = arg;
  243. if (task_curr(t))
  244. return -EBUSY; // It is running, so decline to inspect it.
  245. rscrp->nesting = t->rcu_read_lock_nesting;
  246. rscrp->rs = t->rcu_read_unlock_special;
  247. rscrp->on_blkd_list = !list_empty(&t->rcu_node_entry);
  248. return 0;
  249. }
  250. /*
  251. * Scan the current list of tasks blocked within RCU read-side critical
  252. * sections, printing out the tid of each of the first few of them.
  253. */
  254. static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
  255. __releases(rnp->lock)
  256. {
  257. int i = 0;
  258. int ndetected = 0;
  259. struct rcu_stall_chk_rdr rscr;
  260. struct task_struct *t;
  261. struct task_struct *ts[8];
  262. lockdep_assert_irqs_disabled();
  263. if (!rcu_preempt_blocked_readers_cgp(rnp)) {
  264. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  265. return 0;
  266. }
  267. pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
  268. rnp->level, rnp->grplo, rnp->grphi);
  269. t = list_entry(rnp->gp_tasks->prev,
  270. struct task_struct, rcu_node_entry);
  271. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
  272. get_task_struct(t);
  273. ts[i++] = t;
  274. if (i >= ARRAY_SIZE(ts))
  275. break;
  276. }
  277. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  278. while (i) {
  279. t = ts[--i];
  280. if (task_call_func(t, check_slow_task, &rscr))
  281. pr_cont(" P%d", t->pid);
  282. else
  283. pr_cont(" P%d/%d:%c%c%c%c",
  284. t->pid, rscr.nesting,
  285. ".b"[rscr.rs.b.blocked],
  286. ".q"[rscr.rs.b.need_qs],
  287. ".e"[rscr.rs.b.exp_hint],
  288. ".l"[rscr.on_blkd_list]);
  289. lockdep_assert_irqs_disabled();
  290. put_task_struct(t);
  291. ndetected++;
  292. }
  293. pr_cont("\n");
  294. return ndetected;
  295. }
  296. #else /* #ifdef CONFIG_PREEMPT_RCU */
  297. /*
  298. * Because preemptible RCU does not exist, we never have to check for
  299. * tasks blocked within RCU read-side critical sections.
  300. */
  301. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  302. {
  303. }
  304. /*
  305. * Because preemptible RCU does not exist, we never have to check for
  306. * tasks blocked within RCU read-side critical sections.
  307. */
  308. static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
  309. __releases(rnp->lock)
  310. {
  311. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  312. return 0;
  313. }
  314. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  315. /*
  316. * Dump stacks of all tasks running on stalled CPUs. First try using
  317. * NMIs, but fall back to manual remote stack tracing on architectures
  318. * that don't support NMI-based stack dumps. The NMI-triggered stack
  319. * traces are more accurate because they are printed by the target CPU.
  320. */
  321. static void rcu_dump_cpu_stacks(void)
  322. {
  323. int cpu;
  324. unsigned long flags;
  325. struct rcu_node *rnp;
  326. rcu_for_each_leaf_node(rnp) {
  327. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  328. for_each_leaf_node_possible_cpu(rnp, cpu)
  329. if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
  330. if (cpu_is_offline(cpu))
  331. pr_err("Offline CPU %d blocking current GP.\n", cpu);
  332. else
  333. dump_cpu_task(cpu);
  334. }
  335. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  336. }
  337. }
  338. static const char * const gp_state_names[] = {
  339. [RCU_GP_IDLE] = "RCU_GP_IDLE",
  340. [RCU_GP_WAIT_GPS] = "RCU_GP_WAIT_GPS",
  341. [RCU_GP_DONE_GPS] = "RCU_GP_DONE_GPS",
  342. [RCU_GP_ONOFF] = "RCU_GP_ONOFF",
  343. [RCU_GP_INIT] = "RCU_GP_INIT",
  344. [RCU_GP_WAIT_FQS] = "RCU_GP_WAIT_FQS",
  345. [RCU_GP_DOING_FQS] = "RCU_GP_DOING_FQS",
  346. [RCU_GP_CLEANUP] = "RCU_GP_CLEANUP",
  347. [RCU_GP_CLEANED] = "RCU_GP_CLEANED",
  348. };
  349. /*
  350. * Convert a ->gp_state value to a character string.
  351. */
  352. static const char *gp_state_getname(short gs)
  353. {
  354. if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
  355. return "???";
  356. return gp_state_names[gs];
  357. }
  358. /* Is the RCU grace-period kthread being starved of CPU time? */
  359. static bool rcu_is_gp_kthread_starving(unsigned long *jp)
  360. {
  361. unsigned long j = jiffies - READ_ONCE(rcu_state.gp_activity);
  362. if (jp)
  363. *jp = j;
  364. return j > 2 * HZ;
  365. }
  366. static bool rcu_is_rcuc_kthread_starving(struct rcu_data *rdp, unsigned long *jp)
  367. {
  368. int cpu;
  369. struct task_struct *rcuc;
  370. unsigned long j;
  371. rcuc = rdp->rcu_cpu_kthread_task;
  372. if (!rcuc)
  373. return false;
  374. cpu = task_cpu(rcuc);
  375. if (cpu_is_offline(cpu) || idle_cpu(cpu))
  376. return false;
  377. j = jiffies - READ_ONCE(rdp->rcuc_activity);
  378. if (jp)
  379. *jp = j;
  380. return j > 2 * HZ;
  381. }
  382. /*
  383. * Print out diagnostic information for the specified stalled CPU.
  384. *
  385. * If the specified CPU is aware of the current RCU grace period, then
  386. * print the number of scheduling clock interrupts the CPU has taken
  387. * during the time that it has been aware. Otherwise, print the number
  388. * of RCU grace periods that this CPU is ignorant of, for example, "1"
  389. * if the CPU was aware of the previous grace period.
  390. *
  391. * Also print out idle info.
  392. */
  393. static void print_cpu_stall_info(int cpu)
  394. {
  395. unsigned long delta;
  396. bool falsepositive;
  397. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  398. char *ticks_title;
  399. unsigned long ticks_value;
  400. bool rcuc_starved;
  401. unsigned long j;
  402. char buf[32];
  403. /*
  404. * We could be printing a lot while holding a spinlock. Avoid
  405. * triggering hard lockup.
  406. */
  407. touch_nmi_watchdog();
  408. ticks_value = rcu_seq_ctr(rcu_state.gp_seq - rdp->gp_seq);
  409. if (ticks_value) {
  410. ticks_title = "GPs behind";
  411. } else {
  412. ticks_title = "ticks this GP";
  413. ticks_value = rdp->ticks_this_gp;
  414. }
  415. delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
  416. falsepositive = rcu_is_gp_kthread_starving(NULL) &&
  417. rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
  418. rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
  419. if (rcuc_starved)
  420. sprintf(buf, " rcuc=%ld jiffies(starved)", j);
  421. pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
  422. cpu,
  423. "O."[!!cpu_online(cpu)],
  424. "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
  425. "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
  426. !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
  427. rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
  428. "!."[!delta],
  429. ticks_value, ticks_title,
  430. rcu_dynticks_snap(cpu) & 0xffff,
  431. ct_dynticks_nesting_cpu(cpu), ct_dynticks_nmi_nesting_cpu(cpu),
  432. rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
  433. data_race(rcu_state.n_force_qs) - rcu_state.n_force_qs_gpstart,
  434. rcuc_starved ? buf : "",
  435. falsepositive ? " (false positive?)" : "");
  436. }
  437. /* Complain about starvation of grace-period kthread. */
  438. static void rcu_check_gp_kthread_starvation(void)
  439. {
  440. int cpu;
  441. struct task_struct *gpk = rcu_state.gp_kthread;
  442. unsigned long j;
  443. if (rcu_is_gp_kthread_starving(&j)) {
  444. cpu = gpk ? task_cpu(gpk) : -1;
  445. pr_err("%s kthread starved for %ld jiffies! g%ld f%#x %s(%d) ->state=%#x ->cpu=%d\n",
  446. rcu_state.name, j,
  447. (long)rcu_seq_current(&rcu_state.gp_seq),
  448. data_race(READ_ONCE(rcu_state.gp_flags)),
  449. gp_state_getname(rcu_state.gp_state),
  450. data_race(READ_ONCE(rcu_state.gp_state)),
  451. gpk ? data_race(READ_ONCE(gpk->__state)) : ~0, cpu);
  452. if (gpk) {
  453. pr_err("\tUnless %s kthread gets sufficient CPU time, OOM is now expected behavior.\n", rcu_state.name);
  454. pr_err("RCU grace-period kthread stack dump:\n");
  455. sched_show_task(gpk);
  456. if (cpu >= 0) {
  457. if (cpu_is_offline(cpu)) {
  458. pr_err("RCU GP kthread last ran on offline CPU %d.\n", cpu);
  459. } else {
  460. pr_err("Stack dump where RCU GP kthread last ran:\n");
  461. dump_cpu_task(cpu);
  462. }
  463. }
  464. wake_up_process(gpk);
  465. }
  466. }
  467. }
  468. /* Complain about missing wakeups from expired fqs wait timer */
  469. static void rcu_check_gp_kthread_expired_fqs_timer(void)
  470. {
  471. struct task_struct *gpk = rcu_state.gp_kthread;
  472. short gp_state;
  473. unsigned long jiffies_fqs;
  474. int cpu;
  475. /*
  476. * Order reads of .gp_state and .jiffies_force_qs.
  477. * Matching smp_wmb() is present in rcu_gp_fqs_loop().
  478. */
  479. gp_state = smp_load_acquire(&rcu_state.gp_state);
  480. jiffies_fqs = READ_ONCE(rcu_state.jiffies_force_qs);
  481. if (gp_state == RCU_GP_WAIT_FQS &&
  482. time_after(jiffies, jiffies_fqs + RCU_STALL_MIGHT_MIN) &&
  483. gpk && !READ_ONCE(gpk->on_rq)) {
  484. cpu = task_cpu(gpk);
  485. pr_err("%s kthread timer wakeup didn't happen for %ld jiffies! g%ld f%#x %s(%d) ->state=%#x\n",
  486. rcu_state.name, (jiffies - jiffies_fqs),
  487. (long)rcu_seq_current(&rcu_state.gp_seq),
  488. data_race(rcu_state.gp_flags),
  489. gp_state_getname(RCU_GP_WAIT_FQS), RCU_GP_WAIT_FQS,
  490. data_race(READ_ONCE(gpk->__state)));
  491. pr_err("\tPossible timer handling issue on cpu=%d timer-softirq=%u\n",
  492. cpu, kstat_softirqs_cpu(TIMER_SOFTIRQ, cpu));
  493. }
  494. }
  495. static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
  496. {
  497. int cpu;
  498. unsigned long flags;
  499. unsigned long gpa;
  500. unsigned long j;
  501. int ndetected = 0;
  502. struct rcu_node *rnp;
  503. long totqlen = 0;
  504. lockdep_assert_irqs_disabled();
  505. /* Kick and suppress, if so configured. */
  506. rcu_stall_kick_kthreads();
  507. if (rcu_stall_is_suppressed())
  508. return;
  509. /*
  510. * OK, time to rat on our buddy...
  511. * See Documentation/RCU/stallwarn.rst for info on how to debug
  512. * RCU CPU stall warnings.
  513. */
  514. trace_rcu_stall_warning(rcu_state.name, TPS("StallDetected"));
  515. pr_err("INFO: %s detected stalls on CPUs/tasks:\n", rcu_state.name);
  516. rcu_for_each_leaf_node(rnp) {
  517. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  518. if (rnp->qsmask != 0) {
  519. for_each_leaf_node_possible_cpu(rnp, cpu)
  520. if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
  521. print_cpu_stall_info(cpu);
  522. ndetected++;
  523. }
  524. }
  525. ndetected += rcu_print_task_stall(rnp, flags); // Releases rnp->lock.
  526. lockdep_assert_irqs_disabled();
  527. }
  528. for_each_possible_cpu(cpu)
  529. totqlen += rcu_get_n_cbs_cpu(cpu);
  530. pr_cont("\t(detected by %d, t=%ld jiffies, g=%ld, q=%lu ncpus=%d)\n",
  531. smp_processor_id(), (long)(jiffies - gps),
  532. (long)rcu_seq_current(&rcu_state.gp_seq), totqlen, rcu_state.n_online_cpus);
  533. if (ndetected) {
  534. rcu_dump_cpu_stacks();
  535. /* Complain about tasks blocking the grace period. */
  536. rcu_for_each_leaf_node(rnp)
  537. rcu_print_detail_task_stall_rnp(rnp);
  538. } else {
  539. if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
  540. pr_err("INFO: Stall ended before state dump start\n");
  541. } else {
  542. j = jiffies;
  543. gpa = data_race(READ_ONCE(rcu_state.gp_activity));
  544. pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
  545. rcu_state.name, j - gpa, j, gpa,
  546. data_race(READ_ONCE(jiffies_till_next_fqs)),
  547. data_race(READ_ONCE(rcu_get_root()->qsmask)));
  548. }
  549. }
  550. /* Rewrite if needed in case of slow consoles. */
  551. if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
  552. WRITE_ONCE(rcu_state.jiffies_stall,
  553. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  554. rcu_check_gp_kthread_expired_fqs_timer();
  555. rcu_check_gp_kthread_starvation();
  556. panic_on_rcu_stall();
  557. rcu_force_quiescent_state(); /* Kick them all. */
  558. }
  559. static void print_cpu_stall(unsigned long gps)
  560. {
  561. int cpu;
  562. unsigned long flags;
  563. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  564. struct rcu_node *rnp = rcu_get_root();
  565. long totqlen = 0;
  566. lockdep_assert_irqs_disabled();
  567. /* Kick and suppress, if so configured. */
  568. rcu_stall_kick_kthreads();
  569. if (rcu_stall_is_suppressed())
  570. return;
  571. /*
  572. * OK, time to rat on ourselves...
  573. * See Documentation/RCU/stallwarn.rst for info on how to debug
  574. * RCU CPU stall warnings.
  575. */
  576. trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected"));
  577. pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name);
  578. raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
  579. print_cpu_stall_info(smp_processor_id());
  580. raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
  581. for_each_possible_cpu(cpu)
  582. totqlen += rcu_get_n_cbs_cpu(cpu);
  583. pr_cont("\t(t=%lu jiffies g=%ld q=%lu ncpus=%d)\n",
  584. jiffies - gps,
  585. (long)rcu_seq_current(&rcu_state.gp_seq), totqlen, rcu_state.n_online_cpus);
  586. rcu_check_gp_kthread_expired_fqs_timer();
  587. rcu_check_gp_kthread_starvation();
  588. rcu_dump_cpu_stacks();
  589. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  590. /* Rewrite if needed in case of slow consoles. */
  591. if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
  592. WRITE_ONCE(rcu_state.jiffies_stall,
  593. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  594. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  595. panic_on_rcu_stall();
  596. /*
  597. * Attempt to revive the RCU machinery by forcing a context switch.
  598. *
  599. * A context switch would normally allow the RCU state machine to make
  600. * progress and it could be we're stuck in kernel space without context
  601. * switches for an entirely unreasonable amount of time.
  602. */
  603. set_tsk_need_resched(current);
  604. set_preempt_need_resched();
  605. }
  606. static void check_cpu_stall(struct rcu_data *rdp)
  607. {
  608. bool didstall = false;
  609. unsigned long gs1;
  610. unsigned long gs2;
  611. unsigned long gps;
  612. unsigned long j;
  613. unsigned long jn;
  614. unsigned long js;
  615. struct rcu_node *rnp;
  616. lockdep_assert_irqs_disabled();
  617. if ((rcu_stall_is_suppressed() && !READ_ONCE(rcu_kick_kthreads)) ||
  618. !rcu_gp_in_progress())
  619. return;
  620. rcu_stall_kick_kthreads();
  621. /*
  622. * Check if it was requested (via rcu_cpu_stall_reset()) that the FQS
  623. * loop has to set jiffies to ensure a non-stale jiffies value. This
  624. * is required to have good jiffies value after coming out of long
  625. * breaks of jiffies updates. Not doing so can cause false positives.
  626. */
  627. if (READ_ONCE(rcu_state.nr_fqs_jiffies_stall) > 0)
  628. return;
  629. j = jiffies;
  630. /*
  631. * Lots of memory barriers to reject false positives.
  632. *
  633. * The idea is to pick up rcu_state.gp_seq, then
  634. * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
  635. * another copy of rcu_state.gp_seq. These values are updated in
  636. * the opposite order with memory barriers (or equivalent) during
  637. * grace-period initialization and cleanup. Now, a false positive
  638. * can occur if we get an new value of rcu_state.gp_start and a old
  639. * value of rcu_state.jiffies_stall. But given the memory barriers,
  640. * the only way that this can happen is if one grace period ends
  641. * and another starts between these two fetches. This is detected
  642. * by comparing the second fetch of rcu_state.gp_seq with the
  643. * previous fetch from rcu_state.gp_seq.
  644. *
  645. * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
  646. * and rcu_state.gp_start suffice to forestall false positives.
  647. */
  648. gs1 = READ_ONCE(rcu_state.gp_seq);
  649. smp_rmb(); /* Pick up ->gp_seq first... */
  650. js = READ_ONCE(rcu_state.jiffies_stall);
  651. smp_rmb(); /* ...then ->jiffies_stall before the rest... */
  652. gps = READ_ONCE(rcu_state.gp_start);
  653. smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
  654. gs2 = READ_ONCE(rcu_state.gp_seq);
  655. if (gs1 != gs2 ||
  656. ULONG_CMP_LT(j, js) ||
  657. ULONG_CMP_GE(gps, js))
  658. return; /* No stall or GP completed since entering function. */
  659. rnp = rdp->mynode;
  660. jn = jiffies + ULONG_MAX / 2;
  661. if (rcu_gp_in_progress() &&
  662. (READ_ONCE(rnp->qsmask) & rdp->grpmask) &&
  663. cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
  664. /*
  665. * If a virtual machine is stopped by the host it can look to
  666. * the watchdog like an RCU stall. Check to see if the host
  667. * stopped the vm.
  668. */
  669. if (kvm_check_and_clear_guest_paused())
  670. return;
  671. /* We haven't checked in, so go dump stack. */
  672. print_cpu_stall(gps);
  673. if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
  674. rcu_ftrace_dump(DUMP_ALL);
  675. didstall = true;
  676. } else if (rcu_gp_in_progress() &&
  677. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY) &&
  678. cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
  679. /*
  680. * If a virtual machine is stopped by the host it can look to
  681. * the watchdog like an RCU stall. Check to see if the host
  682. * stopped the vm.
  683. */
  684. if (kvm_check_and_clear_guest_paused())
  685. return;
  686. /* They had a few time units to dump stack, so complain. */
  687. print_other_cpu_stall(gs2, gps);
  688. if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
  689. rcu_ftrace_dump(DUMP_ALL);
  690. didstall = true;
  691. }
  692. if (didstall && READ_ONCE(rcu_state.jiffies_stall) == jn) {
  693. jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
  694. WRITE_ONCE(rcu_state.jiffies_stall, jn);
  695. }
  696. }
  697. //////////////////////////////////////////////////////////////////////////////
  698. //
  699. // RCU forward-progress mechanisms, including of callback invocation.
  700. /*
  701. * Check to see if a failure to end RCU priority inversion was due to
  702. * a CPU not passing through a quiescent state. When this happens, there
  703. * is nothing that RCU priority boosting can do to help, so we shouldn't
  704. * count this as an RCU priority boosting failure. A return of true says
  705. * RCU priority boosting is to blame, and false says otherwise. If false
  706. * is returned, the first of the CPUs to blame is stored through cpup.
  707. * If there was no CPU blocking the current grace period, but also nothing
  708. * in need of being boosted, *cpup is set to -1. This can happen in case
  709. * of vCPU preemption while the last CPU is reporting its quiscent state,
  710. * for example.
  711. *
  712. * If cpup is NULL, then a lockless quick check is carried out, suitable
  713. * for high-rate usage. On the other hand, if cpup is non-NULL, each
  714. * rcu_node structure's ->lock is acquired, ruling out high-rate usage.
  715. */
  716. bool rcu_check_boost_fail(unsigned long gp_state, int *cpup)
  717. {
  718. bool atb = false;
  719. int cpu;
  720. unsigned long flags;
  721. struct rcu_node *rnp;
  722. rcu_for_each_leaf_node(rnp) {
  723. if (!cpup) {
  724. if (data_race(READ_ONCE(rnp->qsmask))) {
  725. return false;
  726. } else {
  727. if (READ_ONCE(rnp->gp_tasks))
  728. atb = true;
  729. continue;
  730. }
  731. }
  732. *cpup = -1;
  733. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  734. if (rnp->gp_tasks)
  735. atb = true;
  736. if (!rnp->qsmask) {
  737. // No CPUs without quiescent states for this rnp.
  738. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  739. continue;
  740. }
  741. // Find the first holdout CPU.
  742. for_each_leaf_node_possible_cpu(rnp, cpu) {
  743. if (rnp->qsmask & (1UL << (cpu - rnp->grplo))) {
  744. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  745. *cpup = cpu;
  746. return false;
  747. }
  748. }
  749. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  750. }
  751. // Can't blame CPUs, so must blame RCU priority boosting.
  752. return atb;
  753. }
  754. EXPORT_SYMBOL_GPL(rcu_check_boost_fail);
  755. /*
  756. * Show the state of the grace-period kthreads.
  757. */
  758. void show_rcu_gp_kthreads(void)
  759. {
  760. unsigned long cbs = 0;
  761. int cpu;
  762. unsigned long j;
  763. unsigned long ja;
  764. unsigned long jr;
  765. unsigned long js;
  766. unsigned long jw;
  767. struct rcu_data *rdp;
  768. struct rcu_node *rnp;
  769. struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
  770. j = jiffies;
  771. ja = j - data_race(READ_ONCE(rcu_state.gp_activity));
  772. jr = j - data_race(READ_ONCE(rcu_state.gp_req_activity));
  773. js = j - data_race(READ_ONCE(rcu_state.gp_start));
  774. jw = j - data_race(READ_ONCE(rcu_state.gp_wake_time));
  775. pr_info("%s: wait state: %s(%d) ->state: %#x ->rt_priority %u delta ->gp_start %lu ->gp_activity %lu ->gp_req_activity %lu ->gp_wake_time %lu ->gp_wake_seq %ld ->gp_seq %ld ->gp_seq_needed %ld ->gp_max %lu ->gp_flags %#x\n",
  776. rcu_state.name, gp_state_getname(rcu_state.gp_state),
  777. data_race(READ_ONCE(rcu_state.gp_state)),
  778. t ? data_race(READ_ONCE(t->__state)) : 0x1ffff, t ? t->rt_priority : 0xffU,
  779. js, ja, jr, jw, (long)data_race(READ_ONCE(rcu_state.gp_wake_seq)),
  780. (long)data_race(READ_ONCE(rcu_state.gp_seq)),
  781. (long)data_race(READ_ONCE(rcu_get_root()->gp_seq_needed)),
  782. data_race(READ_ONCE(rcu_state.gp_max)),
  783. data_race(READ_ONCE(rcu_state.gp_flags)));
  784. rcu_for_each_node_breadth_first(rnp) {
  785. if (ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq), READ_ONCE(rnp->gp_seq_needed)) &&
  786. !data_race(READ_ONCE(rnp->qsmask)) && !data_race(READ_ONCE(rnp->boost_tasks)) &&
  787. !data_race(READ_ONCE(rnp->exp_tasks)) && !data_race(READ_ONCE(rnp->gp_tasks)))
  788. continue;
  789. pr_info("\trcu_node %d:%d ->gp_seq %ld ->gp_seq_needed %ld ->qsmask %#lx %c%c%c%c ->n_boosts %ld\n",
  790. rnp->grplo, rnp->grphi,
  791. (long)data_race(READ_ONCE(rnp->gp_seq)),
  792. (long)data_race(READ_ONCE(rnp->gp_seq_needed)),
  793. data_race(READ_ONCE(rnp->qsmask)),
  794. ".b"[!!data_race(READ_ONCE(rnp->boost_kthread_task))],
  795. ".B"[!!data_race(READ_ONCE(rnp->boost_tasks))],
  796. ".E"[!!data_race(READ_ONCE(rnp->exp_tasks))],
  797. ".G"[!!data_race(READ_ONCE(rnp->gp_tasks))],
  798. data_race(READ_ONCE(rnp->n_boosts)));
  799. if (!rcu_is_leaf_node(rnp))
  800. continue;
  801. for_each_leaf_node_possible_cpu(rnp, cpu) {
  802. rdp = per_cpu_ptr(&rcu_data, cpu);
  803. if (READ_ONCE(rdp->gpwrap) ||
  804. ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq),
  805. READ_ONCE(rdp->gp_seq_needed)))
  806. continue;
  807. pr_info("\tcpu %d ->gp_seq_needed %ld\n",
  808. cpu, (long)data_race(READ_ONCE(rdp->gp_seq_needed)));
  809. }
  810. }
  811. for_each_possible_cpu(cpu) {
  812. rdp = per_cpu_ptr(&rcu_data, cpu);
  813. cbs += data_race(READ_ONCE(rdp->n_cbs_invoked));
  814. if (rcu_segcblist_is_offloaded(&rdp->cblist))
  815. show_rcu_nocb_state(rdp);
  816. }
  817. pr_info("RCU callbacks invoked since boot: %lu\n", cbs);
  818. show_rcu_tasks_gp_kthreads();
  819. }
  820. EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
  821. /*
  822. * This function checks for grace-period requests that fail to motivate
  823. * RCU to come out of its idle mode.
  824. */
  825. static void rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
  826. const unsigned long gpssdelay)
  827. {
  828. unsigned long flags;
  829. unsigned long j;
  830. struct rcu_node *rnp_root = rcu_get_root();
  831. static atomic_t warned = ATOMIC_INIT(0);
  832. if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
  833. ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
  834. READ_ONCE(rnp_root->gp_seq_needed)) ||
  835. !smp_load_acquire(&rcu_state.gp_kthread)) // Get stable kthread.
  836. return;
  837. j = jiffies; /* Expensive access, and in common case don't get here. */
  838. if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
  839. time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
  840. atomic_read(&warned))
  841. return;
  842. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  843. j = jiffies;
  844. if (rcu_gp_in_progress() ||
  845. ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
  846. READ_ONCE(rnp_root->gp_seq_needed)) ||
  847. time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
  848. time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
  849. atomic_read(&warned)) {
  850. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  851. return;
  852. }
  853. /* Hold onto the leaf lock to make others see warned==1. */
  854. if (rnp_root != rnp)
  855. raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
  856. j = jiffies;
  857. if (rcu_gp_in_progress() ||
  858. ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
  859. READ_ONCE(rnp_root->gp_seq_needed)) ||
  860. time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
  861. time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
  862. atomic_xchg(&warned, 1)) {
  863. if (rnp_root != rnp)
  864. /* irqs remain disabled. */
  865. raw_spin_unlock_rcu_node(rnp_root);
  866. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  867. return;
  868. }
  869. WARN_ON(1);
  870. if (rnp_root != rnp)
  871. raw_spin_unlock_rcu_node(rnp_root);
  872. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  873. show_rcu_gp_kthreads();
  874. }
  875. /*
  876. * Do a forward-progress check for rcutorture. This is normally invoked
  877. * due to an OOM event. The argument "j" gives the time period during
  878. * which rcutorture would like progress to have been made.
  879. */
  880. void rcu_fwd_progress_check(unsigned long j)
  881. {
  882. unsigned long cbs;
  883. int cpu;
  884. unsigned long max_cbs = 0;
  885. int max_cpu = -1;
  886. struct rcu_data *rdp;
  887. if (rcu_gp_in_progress()) {
  888. pr_info("%s: GP age %lu jiffies\n",
  889. __func__, jiffies - data_race(READ_ONCE(rcu_state.gp_start)));
  890. show_rcu_gp_kthreads();
  891. } else {
  892. pr_info("%s: Last GP end %lu jiffies ago\n",
  893. __func__, jiffies - data_race(READ_ONCE(rcu_state.gp_end)));
  894. preempt_disable();
  895. rdp = this_cpu_ptr(&rcu_data);
  896. rcu_check_gp_start_stall(rdp->mynode, rdp, j);
  897. preempt_enable();
  898. }
  899. for_each_possible_cpu(cpu) {
  900. cbs = rcu_get_n_cbs_cpu(cpu);
  901. if (!cbs)
  902. continue;
  903. if (max_cpu < 0)
  904. pr_info("%s: callbacks", __func__);
  905. pr_cont(" %d: %lu", cpu, cbs);
  906. if (cbs <= max_cbs)
  907. continue;
  908. max_cbs = cbs;
  909. max_cpu = cpu;
  910. }
  911. if (max_cpu >= 0)
  912. pr_cont("\n");
  913. }
  914. EXPORT_SYMBOL_GPL(rcu_fwd_progress_check);
  915. /* Commandeer a sysrq key to dump RCU's tree. */
  916. static bool sysrq_rcu;
  917. module_param(sysrq_rcu, bool, 0444);
  918. /* Dump grace-period-request information due to commandeered sysrq. */
  919. static void sysrq_show_rcu(int key)
  920. {
  921. show_rcu_gp_kthreads();
  922. }
  923. static const struct sysrq_key_op sysrq_rcudump_op = {
  924. .handler = sysrq_show_rcu,
  925. .help_msg = "show-rcu(y)",
  926. .action_msg = "Show RCU tree",
  927. .enable_mask = SYSRQ_ENABLE_DUMP,
  928. };
  929. static int __init rcu_sysrq_init(void)
  930. {
  931. if (sysrq_rcu)
  932. return register_sysrq_key('y', &sysrq_rcudump_op);
  933. return 0;
  934. }
  935. early_initcall(rcu_sysrq_init);