Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 1/2] firmware: arm_sdei: Do not signal event 0 to a masked PE
       [not found] <20261124194707.1030653-1-kas@kernel.org>
@ 2026-11-24 19:47 ` Kiryl Shutsemau (Meta)
  2026-11-24 19:47 ` [PATCH 2/2] firmware: arm_sdei_nmi: Send the backtrace IPI to CPUs with SDEI masked Kiryl Shutsemau (Meta)
  1 sibling, 0 replies; 2+ messages in thread
From: Kiryl Shutsemau (Meta) @ 2026-11-24 19:47 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, James Morse
  Cc: Kiryl Shutsemau (Meta), Jinjie Ruan, Vladimir Murzin,
	Ada Couprie Diaz, Marc Zyngier, Lorenzo Pieralisi, Liao Chang,
	Ashok Raj, Doug Anderson, Yin Fengwei, Pengjie Zhang, Cong Wang,
	kernel-team, linux-arm-kernel, linux-kernel, stable

The SDEI NMI backtrace signals event 0 to every other CPU, idle ones
included. The kernel masks SDEI on a PE around power-down idle states,
on its way offline and when it stops.

TF-A raises event 0 with an SGI. On a masked PE it acknowledges the SGI
and never ends it; the event should stay pending until SDEI_PE_UNMASK.
TF-A v2.13 and later refuse to signal a masked PE, but a target that masks
just after the check still takes the SGI. A fix is under review [1].

The PE keeps running at the SDEI priority and takes no Non-secure
interrupt again, even after SDEI_PE_UNMASK. Other CPUs hang in
csd_lock_wait() on their next cross-call to it. One sysrq-l locked up a
384-CPU machine.

Keep event 0 away from masked PEs. Each PE gets a lock, an unmasked flag
and a count of signals raised at it. sdei_event_signal() signals a PE
only under its lock with the flag set, and returns -EAGAIN otherwise.
Before it masks, a PE clears the flag under the lock and waits for the
signals already raised to arrive, which SDEI delivers whatever the PE's
interrupt masks. The wait is limited to a millisecond: a signal raised
while another is pending merges into it, and the count never drains.

Senders can run from an SDEI event or a pseudo-NMI, so they only try the
lock. A panicking CPU skips it, as a panic from such a context may have
left it held by that CPU.

The cpu_pm notifier also runs on a PE that is masked already: before
sdei_cpuhp_up(), and between sdei_cpuhp_down() and CPU_OFF. Make it
unmask only what it masked itself, or the PE comes back from CPU_OFF
masked while the kernel takes it as unmasked.

sdei_event_signal() now takes a logical CPU number.

[1] https://review.trustedfirmware.org/c/TF-A/trusted-firmware-a/+/55414

Fixes: 9141eda9eec7 ("firmware: arm_sdei: add SDEI_EVENT_SIGNAL support")
Cc: stable@vger.kernel.org
Reported-by: Jinjie Ruan <ruanjinjie@huawei.com>
Closes: https://lore.kernel.org/r/17149b32-6b9c-4364-b664-5789f0633e9d@huawei.com
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
 drivers/firmware/arm_sdei.c     | 130 ++++++++++++++++++++++++++++++--
 drivers/firmware/arm_sdei_nmi.c |   9 ++-
 include/linux/arm_sdei.h        |   4 +-
 3 files changed, 131 insertions(+), 12 deletions(-)

diff --git a/drivers/firmware/arm_sdei.c b/drivers/firmware/arm_sdei.c
index e8dd2f0f3919..47f36efdc04e 100644
--- a/drivers/firmware/arm_sdei.c
+++ b/drivers/firmware/arm_sdei.c
@@ -12,6 +12,7 @@
 #include <linux/cpuhotplug.h>
 #include <linux/cpu.h>
 #include <linux/cpu_pm.h>
+#include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/hardirq.h>
 #include <linux/kernel.h>
@@ -32,6 +33,8 @@
 #include <linux/smp.h>
 #include <linux/spinlock.h>
 
+#include <asm/smp_plat.h>
+
 /*
  * The call to use to reach the firmware.
  */
@@ -299,10 +302,78 @@ static int sdei_api_get_version(u64 *version)
 	return invoke_sdei_fn(SDEI_1_0_FN_SDEI_VERSION, 0, 0, 0, 0, 0, version);
 }
 
+/*
+ * Event 0 must not reach a PE that has SDEI masked. Firmware may
+ * acknowledge the SGI behind it and never end it, and the PE then takes
+ * no Non-secure interrupt at all.
+ *
+ * A sender signals a PE only with the PE's lock held, and only if the PE
+ * is unmasked. The PE takes its lock to mark itself masked: no sender gets
+ * through after that, and a sender that got through before has counted
+ * its signal in @raised and raised it. The PE lets its signals arrive
+ * before it masks: each event 0 takes one off @raised. @pm_masked records
+ * what the cpu_pm notifier masked.
+ */
+struct sdei_pe {
+	raw_spinlock_t	lock;
+	atomic_t	raised;
+	bool		unmasked;
+	bool		pm_masked;
+};
+
+static DEFINE_PER_CPU(struct sdei_pe, sdei_pe) = {
+	.lock = __RAW_SPIN_LOCK_UNLOCKED(sdei_pe.lock),
+};
+
+/* Wait for event 0 signals in flight to this PE to arrive */
+static void sdei_wait_in_flight(struct sdei_pe *pe)
+{
+	int i;
+
+	/* Bounded: a signal that never arrives must not hold up masking */
+	for (i = 0; i < USEC_PER_MSEC && atomic_read(&pe->raised) > 0; i++)
+		udelay(1);
+
+	if (atomic_read(&pe->raised) > 0) {
+		pr_warn_once("event 0 to CPU[%u] did not arrive within 1ms\n",
+			     smp_processor_id());
+	}
+
+	/*
+	 * A signal raised while an earlier one is still pending merges into
+	 * it, and the count stays high with nothing on its way: drop it. A
+	 * signal that arrives after this finds the count at zero and leaves it.
+	 */
+	atomic_set(&pe->raised, 0);
+}
+
+/* Stop event 0 signals to this PE, waiting for the senders under the lock */
+static void sdei_block_signal(void)
+{
+	struct sdei_pe *pe = this_cpu_ptr(&sdei_pe);
+	unsigned long flags;
+
+	/*
+	 * A panic from an SDEI event or a pseudo-NMI may have left the lock
+	 * held by this CPU. The CPUs that could signal it are stopped by now.
+	 */
+	if (panic_on_this_cpu()) {
+		WRITE_ONCE(pe->unmasked, false);
+	} else {
+		raw_spin_lock_irqsave(&pe->lock, flags);
+		WRITE_ONCE(pe->unmasked, false);
+		raw_spin_unlock_irqrestore(&pe->lock, flags);
+	}
+
+	sdei_wait_in_flight(pe);
+}
+
 int sdei_mask_local_cpu(void)
 {
 	int err;
 
+	sdei_block_signal();
+
 	err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_MASK, 0, 0, 0, 0, 0, NULL);
 	if (err && err != -EIO) {
 		pr_warn_once("failed to mask CPU[%u]: %d\n",
@@ -330,6 +401,8 @@ int sdei_unmask_local_cpu(void)
 		return err;
 	}
 
+	/* No lock: a sender that misses this only refuses an unmasked PE */
+	WRITE_ONCE(this_cpu_ptr(&sdei_pe)->unmasked, true);
 	return 0;
 }
 
@@ -340,14 +413,34 @@ static void _ipi_unmask_cpu(void *ignored)
 }
 
 /*
- * Signal the software-signalled event (event 0) to @mpidr. Does nothing
- * but the SMC -- no locks, no event lookup -- so it is safe from NMI /
- * crash context (e.g. the cross-CPU NMI service).
+ * Signal the software-signalled event (event 0) to @cpu. It waits for no
+ * lock and looks up no event, so it is safe from NMI / crash context (e.g.
+ * the cross-CPU NMI service).
+ *
+ * Returns -EAGAIN without signalling if @cpu has SDEI masked or is masking.
  */
-int sdei_event_signal(u32 event_num, u64 mpidr)
+int sdei_event_signal(u32 event_num, unsigned int cpu)
 {
-	return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_SIGNAL, event_num,
-			      mpidr, 0, 0, 0, NULL);
+	struct sdei_pe *pe = per_cpu_ptr(&sdei_pe, cpu);
+	u64 mpidr = cpu_logical_map(cpu);
+	unsigned long flags;
+	int err = -EAGAIN;
+
+	/* IRQs off: the PE may be waiting for the lock to mask */
+	if (!raw_spin_trylock_irqsave(&pe->lock, flags))
+		return -EAGAIN;
+
+	if (READ_ONCE(pe->unmasked)) {
+		atomic_inc(&pe->raised);
+		err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_SIGNAL, event_num,
+				     mpidr, 0, 0, 0, NULL);
+		/* A late signal may have taken this count already */
+		if (err)
+			atomic_dec_if_positive(&pe->raised);
+	}
+
+	raw_spin_unlock_irqrestore(&pe->lock, flags);
+	return err;
 }
 NOKPROBE_SYMBOL(sdei_event_signal);
 
@@ -680,6 +773,9 @@ static int sdei_cpuhp_down(unsigned int cpu)
 	struct sdei_event *event;
 	int err;
 
+	/* A signal must arrive while event 0 is still registered here */
+	sdei_block_signal();
+
 	/* un-register private events */
 	spin_lock(&sdei_list_lock);
 	list_for_each_entry(event, &sdei_list, list) {
@@ -733,16 +829,30 @@ static int sdei_cpuhp_up(unsigned int cpu)
 static int sdei_pm_notifier(struct notifier_block *nb, unsigned long action,
 			    void *data)
 {
+	struct sdei_pe *pe = this_cpu_ptr(&sdei_pe);
 	int rv;
 
 	WARN_ON_ONCE(preemptible());
 
 	switch (action) {
 	case CPU_PM_ENTER:
+		/*
+		 * Leave alone a PE that is masked already: before
+		 * sdei_cpuhp_up(), or after sdei_cpuhp_down(), as cpuidle runs
+		 * until CPU_OFF. CPU_PM_EXIT must not unmask it.
+		 */
+		if (!pe->unmasked)
+			return NOTIFY_DONE;
 		rv = sdei_mask_local_cpu();
+		if (!rv)
+			pe->pm_masked = true;
 		break;
 	case CPU_PM_EXIT:
 	case CPU_PM_ENTER_FAILED:
+		/* Unmask only what CPU_PM_ENTER masked */
+		if (!pe->pm_masked)
+			return NOTIFY_DONE;
+		pe->pm_masked = false;
 		rv = sdei_unmask_local_cpu();
 		break;
 	default:
@@ -1114,6 +1224,14 @@ int sdei_event_handler(struct pt_regs *regs,
 	int err;
 	u32 event_num = arg->event_num;
 
+	/*
+	 * One signal raised at this PE has arrived. If the PE gave up waiting
+	 * for it and dropped the count, leave the count at zero: below zero,
+	 * the PE would not wait for the next signal raised here.
+	 */
+	if (!event_num)
+		atomic_dec_if_positive(&this_cpu_ptr(&sdei_pe)->raised);
+
 	err = arg->callback(event_num, regs, arg->callback_arg);
 	if (err)
 		pr_err_ratelimited("event %u on CPU %u failed with error: %d\n",
diff --git a/drivers/firmware/arm_sdei_nmi.c b/drivers/firmware/arm_sdei_nmi.c
index 038eb79e28ac..edea68ef881e 100644
--- a/drivers/firmware/arm_sdei_nmi.c
+++ b/drivers/firmware/arm_sdei_nmi.c
@@ -36,7 +36,7 @@
  *
  * Delivery uses the standard SDEI software-signalled event (event 0) and
  * SDEI_EVENT_SIGNAL. We register a handler for event 0, enable it, and
- * poke a target CPU with sdei_event_signal(0, mpidr): firmware makes
+ * poke a target CPU with sdei_event_signal(0, cpu): firmware makes
  * event 0 pending on that PE and dispatches the handler NMI-like,
  * regardless of the target's DAIF.
  * Availability is simply whether event 0 registers and enables -- if SDEI
@@ -58,7 +58,6 @@
 #include <linux/types.h>
 
 #include <asm/nmi.h>
-#include <asm/smp_plat.h>
 
 static bool sdei_nmi_available;
 
@@ -127,11 +126,13 @@ NOKPROBE_SYMBOL(sdei_nmi_handler);
 
 static void sdei_nmi_fire(unsigned int target_cpu)
 {
-	int err = sdei_event_signal(SDEI_NMI_EVENT, cpu_logical_map(target_cpu));
+	int err = sdei_event_signal(SDEI_NMI_EVENT, target_cpu);
 
-	if (err)
+	/* -EAGAIN: the CPU has SDEI masked and is not signalled */
+	if (err && err != -EAGAIN) {
 		pr_warn("SDEI_EVENT_SIGNAL to CPU %u failed: %d\n",
 			target_cpu, err);
+	}
 }
 
 /*
diff --git a/include/linux/arm_sdei.h b/include/linux/arm_sdei.h
index b9dc21c241be..a63ab4774783 100644
--- a/include/linux/arm_sdei.h
+++ b/include/linux/arm_sdei.h
@@ -39,9 +39,9 @@ int sdei_event_disable(u32 event_num);
 
 /*
  * Signal the software-signalled event (event 0) to another PE, NMI-like.
- * @mpidr is the target's MPIDR affinity.
+ * Returns -EAGAIN if @cpu has SDEI masked.
  */
-int sdei_event_signal(u32 event_num, u64 mpidr);
+int sdei_event_signal(u32 event_num, unsigned int cpu);
 
 /* Was SDEI firmware probed and usable? */
 bool sdei_is_present(void);
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] firmware: arm_sdei_nmi: Send the backtrace IPI to CPUs with SDEI masked
       [not found] <20261124194707.1030653-1-kas@kernel.org>
  2026-11-24 19:47 ` [PATCH 1/2] firmware: arm_sdei: Do not signal event 0 to a masked PE Kiryl Shutsemau (Meta)
@ 2026-11-24 19:47 ` Kiryl Shutsemau (Meta)
  1 sibling, 0 replies; 2+ messages in thread
From: Kiryl Shutsemau (Meta) @ 2026-11-24 19:47 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, James Morse
  Cc: Kiryl Shutsemau (Meta), Jinjie Ruan, Vladimir Murzin,
	Ada Couprie Diaz, Marc Zyngier, Lorenzo Pieralisi, Liao Chang,
	Ashok Raj, Doug Anderson, Yin Fengwei, Pengjie Zhang, Cong Wang,
	kernel-team, linux-arm-kernel, linux-kernel, stable

sdei_event_signal() refuses a CPU that has SDEI masked. An NMI
backtrace then gets nothing from such a CPU and, after 10 seconds,
reports it as not responding.

The CPU is in a power-down idle state or going offline, and the regular
backtrace IPI wakes it. Send that IPI to the CPUs SDEI refused; every
other CPU still gets event 0.

A stop needs nothing extra: smp_send_stop() has sent its IPIs before it
signals SDEI, and a CPU still online after that is reported as not
stopped.

Fixes: 0c2c56598cc1 ("drivers/firmware: add SDEI cross-CPU NMI service for arm64")
Cc: stable@vger.kernel.org
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
 arch/arm64/include/asm/nmi.h    |  7 +++++++
 arch/arm64/kernel/smp.c         |  2 +-
 drivers/firmware/arm_sdei_nmi.c | 21 ++++++++++++++++++---
 3 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/include/asm/nmi.h b/arch/arm64/include/asm/nmi.h
index 2e8974ff8d63..447d681a5475 100644
--- a/arch/arm64/include/asm/nmi.h
+++ b/arch/arm64/include/asm/nmi.h
@@ -45,4 +45,11 @@ static inline void sdei_nmi_stop_cpus(const cpumask_t *mask) { }
  */
 void __noreturn arm64_nmi_cpu_stop(struct pt_regs *regs, bool die_on_crash);
 
+/*
+ * Send the regular backtrace IPI (an NMI with pseudo-NMI) to @mask. The
+ * SDEI provider uses it for the CPUs it cannot signal. Defined in
+ * arch/arm64/kernel/smp.c.
+ */
+void arm64_backtrace_ipi(cpumask_t *mask);
+
 #endif /* __ASM_NMI_H */
diff --git a/arch/arm64/kernel/smp.c b/arch/arm64/kernel/smp.c
index 014b163ce658..8369a4cd3e32 100644
--- a/arch/arm64/kernel/smp.c
+++ b/arch/arm64/kernel/smp.c
@@ -946,7 +946,7 @@ static void arm64_send_ipi(const cpumask_t *mask, unsigned int nr)
 			__ipi_send_single(get_ipi_desc(cpu, nr), cpu);
 }
 
-static void arm64_backtrace_ipi(cpumask_t *mask)
+void arm64_backtrace_ipi(cpumask_t *mask)
 {
 	arm64_send_ipi(mask, IPI_CPU_BACKTRACE);
 }
diff --git a/drivers/firmware/arm_sdei_nmi.c b/drivers/firmware/arm_sdei_nmi.c
index edea68ef881e..b8ebb97caf18 100644
--- a/drivers/firmware/arm_sdei_nmi.c
+++ b/drivers/firmware/arm_sdei_nmi.c
@@ -124,7 +124,7 @@ static int sdei_nmi_handler(u32 event, struct pt_regs *regs, void *arg)
 }
 NOKPROBE_SYMBOL(sdei_nmi_handler);
 
-static void sdei_nmi_fire(unsigned int target_cpu)
+static int sdei_nmi_fire(unsigned int target_cpu)
 {
 	int err = sdei_event_signal(SDEI_NMI_EVENT, target_cpu);
 
@@ -133,8 +133,13 @@ static void sdei_nmi_fire(unsigned int target_cpu)
 		pr_warn("SDEI_EVENT_SIGNAL to CPU %u failed: %d\n",
 			target_cpu, err);
 	}
+
+	return err;
 }
 
+/* CPUs to IPI instead; nmi_trigger_cpumask_backtrace() serialises its users */
+static cpumask_t sdei_nmi_ipi_mask;
+
 /*
  * Raise callback for nmi_trigger_cpumask_backtrace(): signal event 0
  * at every CPU still pending in @mask. The framework excludes the local
@@ -151,8 +156,18 @@ static void sdei_nmi_raise_backtrace(cpumask_t *mask)
 	 */
 	dsb(ishst);
 
-	for_each_cpu(cpu, mask)
-		sdei_nmi_fire(cpu);
+	cpumask_clear(&sdei_nmi_ipi_mask);
+	for_each_cpu(cpu, mask) {
+		if (sdei_nmi_fire(cpu) == -EAGAIN)
+			cpumask_set_cpu(cpu, &sdei_nmi_ipi_mask);
+	}
+
+	/*
+	 * A CPU with SDEI masked is in a power-down idle state or going
+	 * offline. The IPI wakes it and it reports like any other.
+	 */
+	if (!cpumask_empty(&sdei_nmi_ipi_mask))
+		arm64_backtrace_ipi(&sdei_nmi_ipi_mask);
 }
 
 /*
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 19:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261124194707.1030653-1-kas@kernel.org>
2026-11-24 19:47 ` [PATCH 1/2] firmware: arm_sdei: Do not signal event 0 to a masked PE Kiryl Shutsemau (Meta)
2026-11-24 19:47 ` [PATCH 2/2] firmware: arm_sdei_nmi: Send the backtrace IPI to CPUs with SDEI masked Kiryl Shutsemau (Meta)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox