mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
To: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	James Morse <james.morse@arm.com>
Cc: "Kiryl Shutsemau (Meta)" <kas@kernel.org>,
	Jinjie Ruan <ruanjinjie@huawei.com>,
	Vladimir Murzin <vladimir.murzin@arm.com>,
	Ada Couprie Diaz <ada.coupriediaz@arm.com>,
	Marc Zyngier <maz@kernel.org>,
	Lorenzo Pieralisi <lpieralisi@kernel.org>,
	Liao Chang <liaochang1@huawei.com>,
	Ashok Raj <ashok.raj@oss.qualcomm.com>,
	Doug Anderson <dianders@chromium.org>,
	Yin Fengwei <fengwei_yin@linux.alibaba.com>,
	Pengjie Zhang <zhangpengjie2@huawei.com>,
	Cong Wang <cwang@multikernel.io>,
	kernel-team@meta.com, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH 1/2] firmware: arm_sdei: Do not signal event 0 to a masked PE
Date: Tue, 24 Nov 2026 19:47:04 +0000	[thread overview]
Message-ID: <20261124194707.1030653-2-kas@kernel.org> (raw)
In-Reply-To: <20261124194707.1030653-1-kas@kernel.org>

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


  reply	other threads:[~2026-11-24 19:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 19:47 [PATCH 0/2] firmware: arm_sdei: Fix NMI backtrace lockups on idle CPUs Kiryl Shutsemau (Meta)
2026-11-24 19:47 ` Kiryl Shutsemau (Meta) [this message]
2026-11-24 19:47 ` [PATCH 2/2] firmware: arm_sdei_nmi: Send the backtrace IPI to CPUs with SDEI masked Kiryl Shutsemau (Meta)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124194707.1030653-2-kas@kernel.org \
    --to=kas@kernel.org \
    --cc=ada.coupriediaz@arm.com \
    --cc=ashok.raj@oss.qualcomm.com \
    --cc=catalin.marinas@arm.com \
    --cc=cwang@multikernel.io \
    --cc=dianders@chromium.org \
    --cc=fengwei_yin@linux.alibaba.com \
    --cc=james.morse@arm.com \
    --cc=kernel-team@meta.com \
    --cc=liaochang1@huawei.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lpieralisi@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=ruanjinjie@huawei.com \
    --cc=stable@vger.kernel.org \
    --cc=vladimir.murzin@arm.com \
    --cc=will@kernel.org \
    --cc=zhangpengjie2@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®