From ef04b4e6df103a120498e3be81e22e76183ad434 Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Thu, 21 Mar 2019 21:07:07 +0800 Subject: [PATCH] arm64: Add support for on-demand backtrace of other CPUs hulk inclusion category: feature bugzilla: 12268 CVE: NA ------------------------------------------------- Currently arm64 has no implementation of arch_trigger_cpumask_backtrace. The patch provides one using library code recently added by Russell King for for the majority of the implementation. Currently this is realized using regular irqs but could, in the future, be implemented using NMI-like mechanisms. Note: There is a small (and nasty) change to the generic code to ensure good stack traces. The generic code currently assumes that show_regs() will include a stack trace but arch/arm64 does not do this so we must add extra code here. Ideas on a better approach here would be very welcome (is there any appetite to change arm64 show_regs() or should we just tease out the dump code into a callback?). Signed-off-by: Daniel Thompson Signed-off-by: Oleksandr Andrushchenko Cc: Russell King Signed-off-by: Wei Li Signed-off-by: Yang Yingliang Reviewed-by: Hanjun Guo Signed-off-by: Yang Yingliang --- arch/arm64/include/asm/hardirq.h | 2 +- arch/arm64/include/asm/irq.h | 6 ++++ arch/arm64/include/asm/smp.h | 2 ++ arch/arm64/kernel/smp.c | 47 +++++++++++++++++++++++++++++++- drivers/irqchip/irq-gic-v3.c | 31 ++++++++++++++++----- 5 files changed, 79 insertions(+), 9 deletions(-) diff --git a/arch/arm64/include/asm/hardirq.h b/arch/arm64/include/asm/hardirq.h index 89691c86640a..a5d94aa59c7c 100644 --- a/arch/arm64/include/asm/hardirq.h +++ b/arch/arm64/include/asm/hardirq.h @@ -24,7 +24,7 @@ #include #include -#define NR_IPI 7 +#define NR_IPI 8 typedef struct { unsigned int __softirq_pending; diff --git a/arch/arm64/include/asm/irq.h b/arch/arm64/include/asm/irq.h index b2b0c6405eb0..28471df488c0 100644 --- a/arch/arm64/include/asm/irq.h +++ b/arch/arm64/include/asm/irq.h @@ -13,5 +13,11 @@ static inline int nr_legacy_irqs(void) return 0; } +#ifdef CONFIG_SMP +extern void arch_trigger_cpumask_backtrace(const cpumask_t *mask, + bool exclude_self); +#define arch_trigger_cpumask_backtrace arch_trigger_cpumask_backtrace +#endif + #endif /* !__ASSEMBLER__ */ #endif diff --git a/arch/arm64/include/asm/smp.h b/arch/arm64/include/asm/smp.h index f82b447bd34f..403c22f62b58 100644 --- a/arch/arm64/include/asm/smp.h +++ b/arch/arm64/include/asm/smp.h @@ -151,6 +151,8 @@ bool cpus_are_stuck_in_kernel(void); extern void crash_smp_send_stop(void); extern bool smp_crash_stop_failed(void); +void ipi_set_nmi_prio(void __iomem *base, u8 prio); + #endif /* ifndef __ASSEMBLY__ */ #endif /* ifndef __ASM_SMP_H */ diff --git a/arch/arm64/kernel/smp.c b/arch/arm64/kernel/smp.c index 8f2500360ba5..9a4b67e88bd7 100644 --- a/arch/arm64/kernel/smp.c +++ b/arch/arm64/kernel/smp.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -83,7 +84,8 @@ enum ipi_msg_type { IPI_CPU_CRASH_STOP, IPI_TIMER, IPI_IRQ_WORK, - IPI_WAKEUP + IPI_WAKEUP, + IPI_CPU_BACKTRACE }; #ifdef CONFIG_HOTPLUG_CPU @@ -782,6 +784,7 @@ static const char *ipi_types[NR_IPI] __tracepoint_string = { S(IPI_TIMER, "Timer broadcast interrupts"), S(IPI_IRQ_WORK, "IRQ work interrupts"), S(IPI_WAKEUP, "CPU wake-up interrupts"), + S(IPI_CPU_BACKTRACE, "backtrace interrupts"), }; static void smp_cross_call(const struct cpumask *target, unsigned int ipinr) @@ -941,6 +944,12 @@ void handle_IPI(int ipinr, struct pt_regs *regs) break; #endif + case IPI_CPU_BACKTRACE: + nmi_enter(); + nmi_cpu_backtrace(regs); + nmi_exit(); + break; + default: pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr); break; @@ -1062,3 +1071,39 @@ bool cpus_are_stuck_in_kernel(void) return !!cpus_stuck_in_kernel || smp_spin_tables; } + +void ipi_set_nmi_prio(void __iomem *base, u8 prio) +{ + /* + * Use writeb here may cause hardware error on D05, + * aovid this problem by using writel. + */ + + u32 offset = (IPI_CPU_BACKTRACE / 4) * 4; + u32 shift = (IPI_CPU_BACKTRACE % 4) * 8; + u32 prios = readl_relaxed(base + offset); + + /* clean old priority */ + prios &= ~(0xff << shift); + /* set new priority*/ + prios |= (prio << offset); + + writel_relaxed(prios, base + GICR_IPRIORITYR0 + offset); +} + +static void raise_nmi(cpumask_t *mask) +{ + /* + * Generate the backtrace directly if we are running in a + * calling context that is not preemptible by the backtrace IPI. + */ + if (cpumask_test_cpu(smp_processor_id(), mask) && irqs_disabled()) + nmi_cpu_backtrace(NULL); + + smp_cross_call(mask, IPI_CPU_BACKTRACE); +} + +void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self) +{ + nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_nmi); +} diff --git a/drivers/irqchip/irq-gic-v3.c b/drivers/irqchip/irq-gic-v3.c index d4ef8a1a3e4b..700a59a67bba 100644 --- a/drivers/irqchip/irq-gic-v3.c +++ b/drivers/irqchip/irq-gic-v3.c @@ -468,6 +468,16 @@ static inline void gic_handle_nmi(u32 irqnr, struct pt_regs *regs) { int err; + if (unlikely(irqnr < 16)) { + gic_write_eoir(irqnr); + if (static_branch_likely(&supports_deactivate_key)) + gic_write_dir(irqnr); +#ifdef CONFIG_SMP + handle_IPI(irqnr, regs); +#endif + return; + } + if (static_branch_likely(&supports_deactivate_key)) gic_write_eoir(irqnr); /* @@ -853,6 +863,9 @@ static void gic_cpu_init(void) gic_cpu_config(rbase, gic_redist_wait_for_rwp); + if (gic_supports_nmi()) + ipi_set_nmi_prio(rbase, GICD_INT_NMI_PRI); + /* initialise system registers */ gic_cpu_sys_reg_init(); } @@ -1320,6 +1333,17 @@ static int __init gic_init_bases(void __iomem *dist_base, gic_update_vlpi_properties(); + /* + * NMI backtrace DFX need check nmi support, this should be + * called before enable NMI backtrace DFX. + */ + if (gic_prio_masking_enabled()) { + if (!gic_has_group0() || gic_dist_security_disabled()) + gic_enable_nmi_support(); + else + pr_warn("SCR_EL3.FIQ is cleared, cannot enable use of pseudo-NMIs\n"); + } + gic_smp_init(); gic_dist_init(); gic_cpu_init(); @@ -1330,13 +1354,6 @@ static int __init gic_init_bases(void __iomem *dist_base, its_cpu_init(); } - if (gic_prio_masking_enabled()) { - if (!gic_has_group0() || gic_dist_security_disabled()) - gic_enable_nmi_support(); - else - pr_warn("SCR_EL3.FIQ is cleared, cannot enable use of pseudo-NMIs\n"); - } - return 0; out_free: -- GitLab