Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * RCU CPU stall warnings for normal RCU grace periods
0004  *
0005  * Copyright IBM Corporation, 2019
0006  *
0007  * Author: Paul E. McKenney <paulmck@linux.ibm.com>
0008  */
0009 
0010 #include <linux/kvm_para.h>
0011 
0012 //////////////////////////////////////////////////////////////////////////////
0013 //
0014 // Controlling CPU stall warnings, including delay calculation.
0015 
0016 /* panic() on RCU Stall sysctl. */
0017 int sysctl_panic_on_rcu_stall __read_mostly;
0018 int sysctl_max_rcu_stall_to_panic __read_mostly;
0019 
0020 #ifdef CONFIG_PROVE_RCU
0021 #define RCU_STALL_DELAY_DELTA       (5 * HZ)
0022 #else
0023 #define RCU_STALL_DELAY_DELTA       0
0024 #endif
0025 #define RCU_STALL_MIGHT_DIV     8
0026 #define RCU_STALL_MIGHT_MIN     (2 * HZ)
0027 
0028 int rcu_exp_jiffies_till_stall_check(void)
0029 {
0030     int cpu_stall_timeout = READ_ONCE(rcu_exp_cpu_stall_timeout);
0031     int exp_stall_delay_delta = 0;
0032     int till_stall_check;
0033 
0034     // Zero says to use rcu_cpu_stall_timeout, but in milliseconds.
0035     if (!cpu_stall_timeout)
0036         cpu_stall_timeout = jiffies_to_msecs(rcu_jiffies_till_stall_check());
0037 
0038     // Limit check must be consistent with the Kconfig limits for
0039     // CONFIG_RCU_EXP_CPU_STALL_TIMEOUT, so check the allowed range.
0040     // The minimum clamped value is "2UL", because at least one full
0041     // tick has to be guaranteed.
0042     till_stall_check = clamp(msecs_to_jiffies(cpu_stall_timeout), 2UL, 21UL * HZ);
0043 
0044     if (cpu_stall_timeout && jiffies_to_msecs(till_stall_check) != cpu_stall_timeout)
0045         WRITE_ONCE(rcu_exp_cpu_stall_timeout, jiffies_to_msecs(till_stall_check));
0046 
0047 #ifdef CONFIG_PROVE_RCU
0048     /* Add extra ~25% out of till_stall_check. */
0049     exp_stall_delay_delta = ((till_stall_check * 25) / 100) + 1;
0050 #endif
0051 
0052     return till_stall_check + exp_stall_delay_delta;
0053 }
0054 EXPORT_SYMBOL_GPL(rcu_exp_jiffies_till_stall_check);
0055 
0056 /* Limit-check stall timeouts specified at boottime and runtime. */
0057 int rcu_jiffies_till_stall_check(void)
0058 {
0059     int till_stall_check = READ_ONCE(rcu_cpu_stall_timeout);
0060 
0061     /*
0062      * Limit check must be consistent with the Kconfig limits
0063      * for CONFIG_RCU_CPU_STALL_TIMEOUT.
0064      */
0065     if (till_stall_check < 3) {
0066         WRITE_ONCE(rcu_cpu_stall_timeout, 3);
0067         till_stall_check = 3;
0068     } else if (till_stall_check > 300) {
0069         WRITE_ONCE(rcu_cpu_stall_timeout, 300);
0070         till_stall_check = 300;
0071     }
0072     return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
0073 }
0074 EXPORT_SYMBOL_GPL(rcu_jiffies_till_stall_check);
0075 
0076 /**
0077  * rcu_gp_might_be_stalled - Is it likely that the grace period is stalled?
0078  *
0079  * Returns @true if the current grace period is sufficiently old that
0080  * it is reasonable to assume that it might be stalled.  This can be
0081  * useful when deciding whether to allocate memory to enable RCU-mediated
0082  * freeing on the one hand or just invoking synchronize_rcu() on the other.
0083  * The latter is preferable when the grace period is stalled.
0084  *
0085  * Note that sampling of the .gp_start and .gp_seq fields must be done
0086  * carefully to avoid false positives at the beginnings and ends of
0087  * grace periods.
0088  */
0089 bool rcu_gp_might_be_stalled(void)
0090 {
0091     unsigned long d = rcu_jiffies_till_stall_check() / RCU_STALL_MIGHT_DIV;
0092     unsigned long j = jiffies;
0093 
0094     if (d < RCU_STALL_MIGHT_MIN)
0095         d = RCU_STALL_MIGHT_MIN;
0096     smp_mb(); // jiffies before .gp_seq to avoid false positives.
0097     if (!rcu_gp_in_progress())
0098         return false;
0099     // Long delays at this point avoids false positive, but a delay
0100     // of ULONG_MAX/4 jiffies voids your no-false-positive warranty.
0101     smp_mb(); // .gp_seq before second .gp_start
0102     // And ditto here.
0103     return !time_before(j, READ_ONCE(rcu_state.gp_start) + d);
0104 }
0105 
0106 /* Don't do RCU CPU stall warnings during long sysrq printouts. */
0107 void rcu_sysrq_start(void)
0108 {
0109     if (!rcu_cpu_stall_suppress)
0110         rcu_cpu_stall_suppress = 2;
0111 }
0112 
0113 void rcu_sysrq_end(void)
0114 {
0115     if (rcu_cpu_stall_suppress == 2)
0116         rcu_cpu_stall_suppress = 0;
0117 }
0118 
0119 /* Don't print RCU CPU stall warnings during a kernel panic. */
0120 static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
0121 {
0122     rcu_cpu_stall_suppress = 1;
0123     return NOTIFY_DONE;
0124 }
0125 
0126 static struct notifier_block rcu_panic_block = {
0127     .notifier_call = rcu_panic,
0128 };
0129 
0130 static int __init check_cpu_stall_init(void)
0131 {
0132     atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
0133     return 0;
0134 }
0135 early_initcall(check_cpu_stall_init);
0136 
0137 /* If so specified via sysctl, panic, yielding cleaner stall-warning output. */
0138 static void panic_on_rcu_stall(void)
0139 {
0140     static int cpu_stall;
0141 
0142     if (++cpu_stall < sysctl_max_rcu_stall_to_panic)
0143         return;
0144 
0145     if (sysctl_panic_on_rcu_stall)
0146         panic("RCU Stall\n");
0147 }
0148 
0149 /**
0150  * rcu_cpu_stall_reset - restart stall-warning timeout for current grace period
0151  *
0152  * The caller must disable hard irqs.
0153  */
0154 void rcu_cpu_stall_reset(void)
0155 {
0156     WRITE_ONCE(rcu_state.jiffies_stall,
0157            jiffies + rcu_jiffies_till_stall_check());
0158 }
0159 
0160 //////////////////////////////////////////////////////////////////////////////
0161 //
0162 // Interaction with RCU grace periods
0163 
0164 /* Start of new grace period, so record stall time (and forcing times). */
0165 static void record_gp_stall_check_time(void)
0166 {
0167     unsigned long j = jiffies;
0168     unsigned long j1;
0169 
0170     WRITE_ONCE(rcu_state.gp_start, j);
0171     j1 = rcu_jiffies_till_stall_check();
0172     smp_mb(); // ->gp_start before ->jiffies_stall and caller's ->gp_seq.
0173     WRITE_ONCE(rcu_state.jiffies_stall, j + j1);
0174     rcu_state.jiffies_resched = j + j1 / 2;
0175     rcu_state.n_force_qs_gpstart = READ_ONCE(rcu_state.n_force_qs);
0176 }
0177 
0178 /* Zero ->ticks_this_gp and snapshot the number of RCU softirq handlers. */
0179 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
0180 {
0181     rdp->ticks_this_gp = 0;
0182     rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
0183     WRITE_ONCE(rdp->last_fqs_resched, jiffies);
0184 }
0185 
0186 /*
0187  * If too much time has passed in the current grace period, and if
0188  * so configured, go kick the relevant kthreads.
0189  */
0190 static void rcu_stall_kick_kthreads(void)
0191 {
0192     unsigned long j;
0193 
0194     if (!READ_ONCE(rcu_kick_kthreads))
0195         return;
0196     j = READ_ONCE(rcu_state.jiffies_kick_kthreads);
0197     if (time_after(jiffies, j) && rcu_state.gp_kthread &&
0198         (rcu_gp_in_progress() || READ_ONCE(rcu_state.gp_flags))) {
0199         WARN_ONCE(1, "Kicking %s grace-period kthread\n",
0200               rcu_state.name);
0201         rcu_ftrace_dump(DUMP_ALL);
0202         wake_up_process(rcu_state.gp_kthread);
0203         WRITE_ONCE(rcu_state.jiffies_kick_kthreads, j + HZ);
0204     }
0205 }
0206 
0207 /*
0208  * Handler for the irq_work request posted about halfway into the RCU CPU
0209  * stall timeout, and used to detect excessive irq disabling.  Set state
0210  * appropriately, but just complain if there is unexpected state on entry.
0211  */
0212 static void rcu_iw_handler(struct irq_work *iwp)
0213 {
0214     struct rcu_data *rdp;
0215     struct rcu_node *rnp;
0216 
0217     rdp = container_of(iwp, struct rcu_data, rcu_iw);
0218     rnp = rdp->mynode;
0219     raw_spin_lock_rcu_node(rnp);
0220     if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
0221         rdp->rcu_iw_gp_seq = rnp->gp_seq;
0222         rdp->rcu_iw_pending = false;
0223     }
0224     raw_spin_unlock_rcu_node(rnp);
0225 }
0226 
0227 //////////////////////////////////////////////////////////////////////////////
0228 //
0229 // Printing RCU CPU stall warnings
0230 
0231 #ifdef CONFIG_PREEMPT_RCU
0232 
0233 /*
0234  * Dump detailed information for all tasks blocking the current RCU
0235  * grace period on the specified rcu_node structure.
0236  */
0237 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
0238 {
0239     unsigned long flags;
0240     struct task_struct *t;
0241 
0242     raw_spin_lock_irqsave_rcu_node(rnp, flags);
0243     if (!rcu_preempt_blocked_readers_cgp(rnp)) {
0244         raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0245         return;
0246     }
0247     t = list_entry(rnp->gp_tasks->prev,
0248                struct task_struct, rcu_node_entry);
0249     list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
0250         /*
0251          * We could be printing a lot while holding a spinlock.
0252          * Avoid triggering hard lockup.
0253          */
0254         touch_nmi_watchdog();
0255         sched_show_task(t);
0256     }
0257     raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0258 }
0259 
0260 // Communicate task state back to the RCU CPU stall warning request.
0261 struct rcu_stall_chk_rdr {
0262     int nesting;
0263     union rcu_special rs;
0264     bool on_blkd_list;
0265 };
0266 
0267 /*
0268  * Report out the state of a not-running task that is stalling the
0269  * current RCU grace period.
0270  */
0271 static int check_slow_task(struct task_struct *t, void *arg)
0272 {
0273     struct rcu_stall_chk_rdr *rscrp = arg;
0274 
0275     if (task_curr(t))
0276         return -EBUSY; // It is running, so decline to inspect it.
0277     rscrp->nesting = t->rcu_read_lock_nesting;
0278     rscrp->rs = t->rcu_read_unlock_special;
0279     rscrp->on_blkd_list = !list_empty(&t->rcu_node_entry);
0280     return 0;
0281 }
0282 
0283 /*
0284  * Scan the current list of tasks blocked within RCU read-side critical
0285  * sections, printing out the tid of each of the first few of them.
0286  */
0287 static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
0288     __releases(rnp->lock)
0289 {
0290     int i = 0;
0291     int ndetected = 0;
0292     struct rcu_stall_chk_rdr rscr;
0293     struct task_struct *t;
0294     struct task_struct *ts[8];
0295 
0296     lockdep_assert_irqs_disabled();
0297     if (!rcu_preempt_blocked_readers_cgp(rnp)) {
0298         raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0299         return 0;
0300     }
0301     pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
0302            rnp->level, rnp->grplo, rnp->grphi);
0303     t = list_entry(rnp->gp_tasks->prev,
0304                struct task_struct, rcu_node_entry);
0305     list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
0306         get_task_struct(t);
0307         ts[i++] = t;
0308         if (i >= ARRAY_SIZE(ts))
0309             break;
0310     }
0311     raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0312     while (i) {
0313         t = ts[--i];
0314         if (task_call_func(t, check_slow_task, &rscr))
0315             pr_cont(" P%d", t->pid);
0316         else
0317             pr_cont(" P%d/%d:%c%c%c%c",
0318                 t->pid, rscr.nesting,
0319                 ".b"[rscr.rs.b.blocked],
0320                 ".q"[rscr.rs.b.need_qs],
0321                 ".e"[rscr.rs.b.exp_hint],
0322                 ".l"[rscr.on_blkd_list]);
0323         lockdep_assert_irqs_disabled();
0324         put_task_struct(t);
0325         ndetected++;
0326     }
0327     pr_cont("\n");
0328     return ndetected;
0329 }
0330 
0331 #else /* #ifdef CONFIG_PREEMPT_RCU */
0332 
0333 /*
0334  * Because preemptible RCU does not exist, we never have to check for
0335  * tasks blocked within RCU read-side critical sections.
0336  */
0337 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
0338 {
0339 }
0340 
0341 /*
0342  * Because preemptible RCU does not exist, we never have to check for
0343  * tasks blocked within RCU read-side critical sections.
0344  */
0345 static int rcu_print_task_stall(struct rcu_node *rnp, unsigned long flags)
0346     __releases(rnp->lock)
0347 {
0348     raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0349     return 0;
0350 }
0351 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
0352 
0353 /*
0354  * Dump stacks of all tasks running on stalled CPUs.  First try using
0355  * NMIs, but fall back to manual remote stack tracing on architectures
0356  * that don't support NMI-based stack dumps.  The NMI-triggered stack
0357  * traces are more accurate because they are printed by the target CPU.
0358  */
0359 static void rcu_dump_cpu_stacks(void)
0360 {
0361     int cpu;
0362     unsigned long flags;
0363     struct rcu_node *rnp;
0364 
0365     rcu_for_each_leaf_node(rnp) {
0366         raw_spin_lock_irqsave_rcu_node(rnp, flags);
0367         for_each_leaf_node_possible_cpu(rnp, cpu)
0368             if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
0369                 if (cpu_is_offline(cpu))
0370                     pr_err("Offline CPU %d blocking current GP.\n", cpu);
0371                 else if (!trigger_single_cpu_backtrace(cpu))
0372                     dump_cpu_task(cpu);
0373             }
0374         raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0375     }
0376 }
0377 
0378 static const char * const gp_state_names[] = {
0379     [RCU_GP_IDLE] = "RCU_GP_IDLE",
0380     [RCU_GP_WAIT_GPS] = "RCU_GP_WAIT_GPS",
0381     [RCU_GP_DONE_GPS] = "RCU_GP_DONE_GPS",
0382     [RCU_GP_ONOFF] = "RCU_GP_ONOFF",
0383     [RCU_GP_INIT] = "RCU_GP_INIT",
0384     [RCU_GP_WAIT_FQS] = "RCU_GP_WAIT_FQS",
0385     [RCU_GP_DOING_FQS] = "RCU_GP_DOING_FQS",
0386     [RCU_GP_CLEANUP] = "RCU_GP_CLEANUP",
0387     [RCU_GP_CLEANED] = "RCU_GP_CLEANED",
0388 };
0389 
0390 /*
0391  * Convert a ->gp_state value to a character string.
0392  */
0393 static const char *gp_state_getname(short gs)
0394 {
0395     if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
0396         return "???";
0397     return gp_state_names[gs];
0398 }
0399 
0400 /* Is the RCU grace-period kthread being starved of CPU time? */
0401 static bool rcu_is_gp_kthread_starving(unsigned long *jp)
0402 {
0403     unsigned long j = jiffies - READ_ONCE(rcu_state.gp_activity);
0404 
0405     if (jp)
0406         *jp = j;
0407     return j > 2 * HZ;
0408 }
0409 
0410 static bool rcu_is_rcuc_kthread_starving(struct rcu_data *rdp, unsigned long *jp)
0411 {
0412     int cpu;
0413     struct task_struct *rcuc;
0414     unsigned long j;
0415 
0416     rcuc = rdp->rcu_cpu_kthread_task;
0417     if (!rcuc)
0418         return false;
0419 
0420     cpu = task_cpu(rcuc);
0421     if (cpu_is_offline(cpu) || idle_cpu(cpu))
0422         return false;
0423 
0424     j = jiffies - READ_ONCE(rdp->rcuc_activity);
0425 
0426     if (jp)
0427         *jp = j;
0428     return j > 2 * HZ;
0429 }
0430 
0431 /*
0432  * Print out diagnostic information for the specified stalled CPU.
0433  *
0434  * If the specified CPU is aware of the current RCU grace period, then
0435  * print the number of scheduling clock interrupts the CPU has taken
0436  * during the time that it has been aware.  Otherwise, print the number
0437  * of RCU grace periods that this CPU is ignorant of, for example, "1"
0438  * if the CPU was aware of the previous grace period.
0439  *
0440  * Also print out idle info.
0441  */
0442 static void print_cpu_stall_info(int cpu)
0443 {
0444     unsigned long delta;
0445     bool falsepositive;
0446     struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
0447     char *ticks_title;
0448     unsigned long ticks_value;
0449     bool rcuc_starved;
0450     unsigned long j;
0451     char buf[32];
0452 
0453     /*
0454      * We could be printing a lot while holding a spinlock.  Avoid
0455      * triggering hard lockup.
0456      */
0457     touch_nmi_watchdog();
0458 
0459     ticks_value = rcu_seq_ctr(rcu_state.gp_seq - rdp->gp_seq);
0460     if (ticks_value) {
0461         ticks_title = "GPs behind";
0462     } else {
0463         ticks_title = "ticks this GP";
0464         ticks_value = rdp->ticks_this_gp;
0465     }
0466     delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
0467     falsepositive = rcu_is_gp_kthread_starving(NULL) &&
0468             rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
0469     rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
0470     if (rcuc_starved)
0471         sprintf(buf, " rcuc=%ld jiffies(starved)", j);
0472     pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
0473            cpu,
0474            "O."[!!cpu_online(cpu)],
0475            "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
0476            "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
0477            !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
0478             rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
0479                 "!."[!delta],
0480            ticks_value, ticks_title,
0481            rcu_dynticks_snap(cpu) & 0xffff,
0482            ct_dynticks_nesting_cpu(cpu), ct_dynticks_nmi_nesting_cpu(cpu),
0483            rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
0484            data_race(rcu_state.n_force_qs) - rcu_state.n_force_qs_gpstart,
0485            rcuc_starved ? buf : "",
0486            falsepositive ? " (false positive?)" : "");
0487 }
0488 
0489 /* Complain about starvation of grace-period kthread.  */
0490 static void rcu_check_gp_kthread_starvation(void)
0491 {
0492     int cpu;
0493     struct task_struct *gpk = rcu_state.gp_kthread;
0494     unsigned long j;
0495 
0496     if (rcu_is_gp_kthread_starving(&j)) {
0497         cpu = gpk ? task_cpu(gpk) : -1;
0498         pr_err("%s kthread starved for %ld jiffies! g%ld f%#x %s(%d) ->state=%#x ->cpu=%d\n",
0499                rcu_state.name, j,
0500                (long)rcu_seq_current(&rcu_state.gp_seq),
0501                data_race(READ_ONCE(rcu_state.gp_flags)),
0502                gp_state_getname(rcu_state.gp_state),
0503                data_race(READ_ONCE(rcu_state.gp_state)),
0504                gpk ? data_race(READ_ONCE(gpk->__state)) : ~0, cpu);
0505         if (gpk) {
0506             pr_err("\tUnless %s kthread gets sufficient CPU time, OOM is now expected behavior.\n", rcu_state.name);
0507             pr_err("RCU grace-period kthread stack dump:\n");
0508             sched_show_task(gpk);
0509             if (cpu >= 0) {
0510                 if (cpu_is_offline(cpu)) {
0511                     pr_err("RCU GP kthread last ran on offline CPU %d.\n", cpu);
0512                 } else  {
0513                     pr_err("Stack dump where RCU GP kthread last ran:\n");
0514                     if (!trigger_single_cpu_backtrace(cpu))
0515                         dump_cpu_task(cpu);
0516                 }
0517             }
0518             wake_up_process(gpk);
0519         }
0520     }
0521 }
0522 
0523 /* Complain about missing wakeups from expired fqs wait timer */
0524 static void rcu_check_gp_kthread_expired_fqs_timer(void)
0525 {
0526     struct task_struct *gpk = rcu_state.gp_kthread;
0527     short gp_state;
0528     unsigned long jiffies_fqs;
0529     int cpu;
0530 
0531     /*
0532      * Order reads of .gp_state and .jiffies_force_qs.
0533      * Matching smp_wmb() is present in rcu_gp_fqs_loop().
0534      */
0535     gp_state = smp_load_acquire(&rcu_state.gp_state);
0536     jiffies_fqs = READ_ONCE(rcu_state.jiffies_force_qs);
0537 
0538     if (gp_state == RCU_GP_WAIT_FQS &&
0539         time_after(jiffies, jiffies_fqs + RCU_STALL_MIGHT_MIN) &&
0540         gpk && !READ_ONCE(gpk->on_rq)) {
0541         cpu = task_cpu(gpk);
0542         pr_err("%s kthread timer wakeup didn't happen for %ld jiffies! g%ld f%#x %s(%d) ->state=%#x\n",
0543                rcu_state.name, (jiffies - jiffies_fqs),
0544                (long)rcu_seq_current(&rcu_state.gp_seq),
0545                data_race(rcu_state.gp_flags),
0546                gp_state_getname(RCU_GP_WAIT_FQS), RCU_GP_WAIT_FQS,
0547                data_race(READ_ONCE(gpk->__state)));
0548         pr_err("\tPossible timer handling issue on cpu=%d timer-softirq=%u\n",
0549                cpu, kstat_softirqs_cpu(TIMER_SOFTIRQ, cpu));
0550     }
0551 }
0552 
0553 static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
0554 {
0555     int cpu;
0556     unsigned long flags;
0557     unsigned long gpa;
0558     unsigned long j;
0559     int ndetected = 0;
0560     struct rcu_node *rnp;
0561     long totqlen = 0;
0562 
0563     lockdep_assert_irqs_disabled();
0564 
0565     /* Kick and suppress, if so configured. */
0566     rcu_stall_kick_kthreads();
0567     if (rcu_stall_is_suppressed())
0568         return;
0569 
0570     /*
0571      * OK, time to rat on our buddy...
0572      * See Documentation/RCU/stallwarn.rst for info on how to debug
0573      * RCU CPU stall warnings.
0574      */
0575     trace_rcu_stall_warning(rcu_state.name, TPS("StallDetected"));
0576     pr_err("INFO: %s detected stalls on CPUs/tasks:\n", rcu_state.name);
0577     rcu_for_each_leaf_node(rnp) {
0578         raw_spin_lock_irqsave_rcu_node(rnp, flags);
0579         if (rnp->qsmask != 0) {
0580             for_each_leaf_node_possible_cpu(rnp, cpu)
0581                 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
0582                     print_cpu_stall_info(cpu);
0583                     ndetected++;
0584                 }
0585         }
0586         ndetected += rcu_print_task_stall(rnp, flags); // Releases rnp->lock.
0587         lockdep_assert_irqs_disabled();
0588     }
0589 
0590     for_each_possible_cpu(cpu)
0591         totqlen += rcu_get_n_cbs_cpu(cpu);
0592     pr_cont("\t(detected by %d, t=%ld jiffies, g=%ld, q=%lu ncpus=%d)\n",
0593            smp_processor_id(), (long)(jiffies - gps),
0594            (long)rcu_seq_current(&rcu_state.gp_seq), totqlen, rcu_state.n_online_cpus);
0595     if (ndetected) {
0596         rcu_dump_cpu_stacks();
0597 
0598         /* Complain about tasks blocking the grace period. */
0599         rcu_for_each_leaf_node(rnp)
0600             rcu_print_detail_task_stall_rnp(rnp);
0601     } else {
0602         if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
0603             pr_err("INFO: Stall ended before state dump start\n");
0604         } else {
0605             j = jiffies;
0606             gpa = data_race(READ_ONCE(rcu_state.gp_activity));
0607             pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
0608                    rcu_state.name, j - gpa, j, gpa,
0609                    data_race(READ_ONCE(jiffies_till_next_fqs)),
0610                    data_race(READ_ONCE(rcu_get_root()->qsmask)));
0611         }
0612     }
0613     /* Rewrite if needed in case of slow consoles. */
0614     if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
0615         WRITE_ONCE(rcu_state.jiffies_stall,
0616                jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
0617 
0618     rcu_check_gp_kthread_expired_fqs_timer();
0619     rcu_check_gp_kthread_starvation();
0620 
0621     panic_on_rcu_stall();
0622 
0623     rcu_force_quiescent_state();  /* Kick them all. */
0624 }
0625 
0626 static void print_cpu_stall(unsigned long gps)
0627 {
0628     int cpu;
0629     unsigned long flags;
0630     struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
0631     struct rcu_node *rnp = rcu_get_root();
0632     long totqlen = 0;
0633 
0634     lockdep_assert_irqs_disabled();
0635 
0636     /* Kick and suppress, if so configured. */
0637     rcu_stall_kick_kthreads();
0638     if (rcu_stall_is_suppressed())
0639         return;
0640 
0641     /*
0642      * OK, time to rat on ourselves...
0643      * See Documentation/RCU/stallwarn.rst for info on how to debug
0644      * RCU CPU stall warnings.
0645      */
0646     trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected"));
0647     pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name);
0648     raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
0649     print_cpu_stall_info(smp_processor_id());
0650     raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
0651     for_each_possible_cpu(cpu)
0652         totqlen += rcu_get_n_cbs_cpu(cpu);
0653     pr_cont("\t(t=%lu jiffies g=%ld q=%lu ncpus=%d)\n",
0654         jiffies - gps,
0655         (long)rcu_seq_current(&rcu_state.gp_seq), totqlen, rcu_state.n_online_cpus);
0656 
0657     rcu_check_gp_kthread_expired_fqs_timer();
0658     rcu_check_gp_kthread_starvation();
0659 
0660     rcu_dump_cpu_stacks();
0661 
0662     raw_spin_lock_irqsave_rcu_node(rnp, flags);
0663     /* Rewrite if needed in case of slow consoles. */
0664     if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
0665         WRITE_ONCE(rcu_state.jiffies_stall,
0666                jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
0667     raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0668 
0669     panic_on_rcu_stall();
0670 
0671     /*
0672      * Attempt to revive the RCU machinery by forcing a context switch.
0673      *
0674      * A context switch would normally allow the RCU state machine to make
0675      * progress and it could be we're stuck in kernel space without context
0676      * switches for an entirely unreasonable amount of time.
0677      */
0678     set_tsk_need_resched(current);
0679     set_preempt_need_resched();
0680 }
0681 
0682 static void check_cpu_stall(struct rcu_data *rdp)
0683 {
0684     bool didstall = false;
0685     unsigned long gs1;
0686     unsigned long gs2;
0687     unsigned long gps;
0688     unsigned long j;
0689     unsigned long jn;
0690     unsigned long js;
0691     struct rcu_node *rnp;
0692 
0693     lockdep_assert_irqs_disabled();
0694     if ((rcu_stall_is_suppressed() && !READ_ONCE(rcu_kick_kthreads)) ||
0695         !rcu_gp_in_progress())
0696         return;
0697     rcu_stall_kick_kthreads();
0698     j = jiffies;
0699 
0700     /*
0701      * Lots of memory barriers to reject false positives.
0702      *
0703      * The idea is to pick up rcu_state.gp_seq, then
0704      * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
0705      * another copy of rcu_state.gp_seq.  These values are updated in
0706      * the opposite order with memory barriers (or equivalent) during
0707      * grace-period initialization and cleanup.  Now, a false positive
0708      * can occur if we get an new value of rcu_state.gp_start and a old
0709      * value of rcu_state.jiffies_stall.  But given the memory barriers,
0710      * the only way that this can happen is if one grace period ends
0711      * and another starts between these two fetches.  This is detected
0712      * by comparing the second fetch of rcu_state.gp_seq with the
0713      * previous fetch from rcu_state.gp_seq.
0714      *
0715      * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
0716      * and rcu_state.gp_start suffice to forestall false positives.
0717      */
0718     gs1 = READ_ONCE(rcu_state.gp_seq);
0719     smp_rmb(); /* Pick up ->gp_seq first... */
0720     js = READ_ONCE(rcu_state.jiffies_stall);
0721     smp_rmb(); /* ...then ->jiffies_stall before the rest... */
0722     gps = READ_ONCE(rcu_state.gp_start);
0723     smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
0724     gs2 = READ_ONCE(rcu_state.gp_seq);
0725     if (gs1 != gs2 ||
0726         ULONG_CMP_LT(j, js) ||
0727         ULONG_CMP_GE(gps, js))
0728         return; /* No stall or GP completed since entering function. */
0729     rnp = rdp->mynode;
0730     jn = jiffies + ULONG_MAX / 2;
0731     if (rcu_gp_in_progress() &&
0732         (READ_ONCE(rnp->qsmask) & rdp->grpmask) &&
0733         cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
0734 
0735         /*
0736          * If a virtual machine is stopped by the host it can look to
0737          * the watchdog like an RCU stall. Check to see if the host
0738          * stopped the vm.
0739          */
0740         if (kvm_check_and_clear_guest_paused())
0741             return;
0742 
0743         /* We haven't checked in, so go dump stack. */
0744         print_cpu_stall(gps);
0745         if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
0746             rcu_ftrace_dump(DUMP_ALL);
0747         didstall = true;
0748 
0749     } else if (rcu_gp_in_progress() &&
0750            ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY) &&
0751            cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
0752 
0753         /*
0754          * If a virtual machine is stopped by the host it can look to
0755          * the watchdog like an RCU stall. Check to see if the host
0756          * stopped the vm.
0757          */
0758         if (kvm_check_and_clear_guest_paused())
0759             return;
0760 
0761         /* They had a few time units to dump stack, so complain. */
0762         print_other_cpu_stall(gs2, gps);
0763         if (READ_ONCE(rcu_cpu_stall_ftrace_dump))
0764             rcu_ftrace_dump(DUMP_ALL);
0765         didstall = true;
0766     }
0767     if (didstall && READ_ONCE(rcu_state.jiffies_stall) == jn) {
0768         jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
0769         WRITE_ONCE(rcu_state.jiffies_stall, jn);
0770     }
0771 }
0772 
0773 //////////////////////////////////////////////////////////////////////////////
0774 //
0775 // RCU forward-progress mechanisms, including of callback invocation.
0776 
0777 
0778 /*
0779  * Check to see if a failure to end RCU priority inversion was due to
0780  * a CPU not passing through a quiescent state.  When this happens, there
0781  * is nothing that RCU priority boosting can do to help, so we shouldn't
0782  * count this as an RCU priority boosting failure.  A return of true says
0783  * RCU priority boosting is to blame, and false says otherwise.  If false
0784  * is returned, the first of the CPUs to blame is stored through cpup.
0785  * If there was no CPU blocking the current grace period, but also nothing
0786  * in need of being boosted, *cpup is set to -1.  This can happen in case
0787  * of vCPU preemption while the last CPU is reporting its quiscent state,
0788  * for example.
0789  *
0790  * If cpup is NULL, then a lockless quick check is carried out, suitable
0791  * for high-rate usage.  On the other hand, if cpup is non-NULL, each
0792  * rcu_node structure's ->lock is acquired, ruling out high-rate usage.
0793  */
0794 bool rcu_check_boost_fail(unsigned long gp_state, int *cpup)
0795 {
0796     bool atb = false;
0797     int cpu;
0798     unsigned long flags;
0799     struct rcu_node *rnp;
0800 
0801     rcu_for_each_leaf_node(rnp) {
0802         if (!cpup) {
0803             if (data_race(READ_ONCE(rnp->qsmask))) {
0804                 return false;
0805             } else {
0806                 if (READ_ONCE(rnp->gp_tasks))
0807                     atb = true;
0808                 continue;
0809             }
0810         }
0811         *cpup = -1;
0812         raw_spin_lock_irqsave_rcu_node(rnp, flags);
0813         if (rnp->gp_tasks)
0814             atb = true;
0815         if (!rnp->qsmask) {
0816             // No CPUs without quiescent states for this rnp.
0817             raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0818             continue;
0819         }
0820         // Find the first holdout CPU.
0821         for_each_leaf_node_possible_cpu(rnp, cpu) {
0822             if (rnp->qsmask & (1UL << (cpu - rnp->grplo))) {
0823                 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0824                 *cpup = cpu;
0825                 return false;
0826             }
0827         }
0828         raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0829     }
0830     // Can't blame CPUs, so must blame RCU priority boosting.
0831     return atb;
0832 }
0833 EXPORT_SYMBOL_GPL(rcu_check_boost_fail);
0834 
0835 /*
0836  * Show the state of the grace-period kthreads.
0837  */
0838 void show_rcu_gp_kthreads(void)
0839 {
0840     unsigned long cbs = 0;
0841     int cpu;
0842     unsigned long j;
0843     unsigned long ja;
0844     unsigned long jr;
0845     unsigned long js;
0846     unsigned long jw;
0847     struct rcu_data *rdp;
0848     struct rcu_node *rnp;
0849     struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
0850 
0851     j = jiffies;
0852     ja = j - data_race(READ_ONCE(rcu_state.gp_activity));
0853     jr = j - data_race(READ_ONCE(rcu_state.gp_req_activity));
0854     js = j - data_race(READ_ONCE(rcu_state.gp_start));
0855     jw = j - data_race(READ_ONCE(rcu_state.gp_wake_time));
0856     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",
0857         rcu_state.name, gp_state_getname(rcu_state.gp_state),
0858         data_race(READ_ONCE(rcu_state.gp_state)),
0859         t ? data_race(READ_ONCE(t->__state)) : 0x1ffff, t ? t->rt_priority : 0xffU,
0860         js, ja, jr, jw, (long)data_race(READ_ONCE(rcu_state.gp_wake_seq)),
0861         (long)data_race(READ_ONCE(rcu_state.gp_seq)),
0862         (long)data_race(READ_ONCE(rcu_get_root()->gp_seq_needed)),
0863         data_race(READ_ONCE(rcu_state.gp_max)),
0864         data_race(READ_ONCE(rcu_state.gp_flags)));
0865     rcu_for_each_node_breadth_first(rnp) {
0866         if (ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq), READ_ONCE(rnp->gp_seq_needed)) &&
0867             !data_race(READ_ONCE(rnp->qsmask)) && !data_race(READ_ONCE(rnp->boost_tasks)) &&
0868             !data_race(READ_ONCE(rnp->exp_tasks)) && !data_race(READ_ONCE(rnp->gp_tasks)))
0869             continue;
0870         pr_info("\trcu_node %d:%d ->gp_seq %ld ->gp_seq_needed %ld ->qsmask %#lx %c%c%c%c ->n_boosts %ld\n",
0871             rnp->grplo, rnp->grphi,
0872             (long)data_race(READ_ONCE(rnp->gp_seq)),
0873             (long)data_race(READ_ONCE(rnp->gp_seq_needed)),
0874             data_race(READ_ONCE(rnp->qsmask)),
0875             ".b"[!!data_race(READ_ONCE(rnp->boost_kthread_task))],
0876             ".B"[!!data_race(READ_ONCE(rnp->boost_tasks))],
0877             ".E"[!!data_race(READ_ONCE(rnp->exp_tasks))],
0878             ".G"[!!data_race(READ_ONCE(rnp->gp_tasks))],
0879             data_race(READ_ONCE(rnp->n_boosts)));
0880         if (!rcu_is_leaf_node(rnp))
0881             continue;
0882         for_each_leaf_node_possible_cpu(rnp, cpu) {
0883             rdp = per_cpu_ptr(&rcu_data, cpu);
0884             if (READ_ONCE(rdp->gpwrap) ||
0885                 ULONG_CMP_GE(READ_ONCE(rcu_state.gp_seq),
0886                      READ_ONCE(rdp->gp_seq_needed)))
0887                 continue;
0888             pr_info("\tcpu %d ->gp_seq_needed %ld\n",
0889                 cpu, (long)data_race(READ_ONCE(rdp->gp_seq_needed)));
0890         }
0891     }
0892     for_each_possible_cpu(cpu) {
0893         rdp = per_cpu_ptr(&rcu_data, cpu);
0894         cbs += data_race(READ_ONCE(rdp->n_cbs_invoked));
0895         if (rcu_segcblist_is_offloaded(&rdp->cblist))
0896             show_rcu_nocb_state(rdp);
0897     }
0898     pr_info("RCU callbacks invoked since boot: %lu\n", cbs);
0899     show_rcu_tasks_gp_kthreads();
0900 }
0901 EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
0902 
0903 /*
0904  * This function checks for grace-period requests that fail to motivate
0905  * RCU to come out of its idle mode.
0906  */
0907 static void rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp,
0908                      const unsigned long gpssdelay)
0909 {
0910     unsigned long flags;
0911     unsigned long j;
0912     struct rcu_node *rnp_root = rcu_get_root();
0913     static atomic_t warned = ATOMIC_INIT(0);
0914 
0915     if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
0916         ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
0917              READ_ONCE(rnp_root->gp_seq_needed)) ||
0918         !smp_load_acquire(&rcu_state.gp_kthread)) // Get stable kthread.
0919         return;
0920     j = jiffies; /* Expensive access, and in common case don't get here. */
0921     if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
0922         time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
0923         atomic_read(&warned))
0924         return;
0925 
0926     raw_spin_lock_irqsave_rcu_node(rnp, flags);
0927     j = jiffies;
0928     if (rcu_gp_in_progress() ||
0929         ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
0930              READ_ONCE(rnp_root->gp_seq_needed)) ||
0931         time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
0932         time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
0933         atomic_read(&warned)) {
0934         raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0935         return;
0936     }
0937     /* Hold onto the leaf lock to make others see warned==1. */
0938 
0939     if (rnp_root != rnp)
0940         raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
0941     j = jiffies;
0942     if (rcu_gp_in_progress() ||
0943         ULONG_CMP_GE(READ_ONCE(rnp_root->gp_seq),
0944              READ_ONCE(rnp_root->gp_seq_needed)) ||
0945         time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
0946         time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
0947         atomic_xchg(&warned, 1)) {
0948         if (rnp_root != rnp)
0949             /* irqs remain disabled. */
0950             raw_spin_unlock_rcu_node(rnp_root);
0951         raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0952         return;
0953     }
0954     WARN_ON(1);
0955     if (rnp_root != rnp)
0956         raw_spin_unlock_rcu_node(rnp_root);
0957     raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
0958     show_rcu_gp_kthreads();
0959 }
0960 
0961 /*
0962  * Do a forward-progress check for rcutorture.  This is normally invoked
0963  * due to an OOM event.  The argument "j" gives the time period during
0964  * which rcutorture would like progress to have been made.
0965  */
0966 void rcu_fwd_progress_check(unsigned long j)
0967 {
0968     unsigned long cbs;
0969     int cpu;
0970     unsigned long max_cbs = 0;
0971     int max_cpu = -1;
0972     struct rcu_data *rdp;
0973 
0974     if (rcu_gp_in_progress()) {
0975         pr_info("%s: GP age %lu jiffies\n",
0976             __func__, jiffies - data_race(READ_ONCE(rcu_state.gp_start)));
0977         show_rcu_gp_kthreads();
0978     } else {
0979         pr_info("%s: Last GP end %lu jiffies ago\n",
0980             __func__, jiffies - data_race(READ_ONCE(rcu_state.gp_end)));
0981         preempt_disable();
0982         rdp = this_cpu_ptr(&rcu_data);
0983         rcu_check_gp_start_stall(rdp->mynode, rdp, j);
0984         preempt_enable();
0985     }
0986     for_each_possible_cpu(cpu) {
0987         cbs = rcu_get_n_cbs_cpu(cpu);
0988         if (!cbs)
0989             continue;
0990         if (max_cpu < 0)
0991             pr_info("%s: callbacks", __func__);
0992         pr_cont(" %d: %lu", cpu, cbs);
0993         if (cbs <= max_cbs)
0994             continue;
0995         max_cbs = cbs;
0996         max_cpu = cpu;
0997     }
0998     if (max_cpu >= 0)
0999         pr_cont("\n");
1000 }
1001 EXPORT_SYMBOL_GPL(rcu_fwd_progress_check);
1002 
1003 /* Commandeer a sysrq key to dump RCU's tree. */
1004 static bool sysrq_rcu;
1005 module_param(sysrq_rcu, bool, 0444);
1006 
1007 /* Dump grace-period-request information due to commandeered sysrq. */
1008 static void sysrq_show_rcu(int key)
1009 {
1010     show_rcu_gp_kthreads();
1011 }
1012 
1013 static const struct sysrq_key_op sysrq_rcudump_op = {
1014     .handler = sysrq_show_rcu,
1015     .help_msg = "show-rcu(y)",
1016     .action_msg = "Show RCU tree",
1017     .enable_mask = SYSRQ_ENABLE_DUMP,
1018 };
1019 
1020 static int __init rcu_sysrq_init(void)
1021 {
1022     if (sysrq_rcu)
1023         return register_sysrq_key('y', &sysrq_rcudump_op);
1024     return 0;
1025 }
1026 early_initcall(rcu_sysrq_init);