Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Jinjie Ruan <ruanjinjie@huawei.com>
To: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
Cc: mark.rutland@arm.com, Vladimir Murzin <vladimir.murzin@arm.com>,
	catalin.marinas@arm.com, lpieralisi@kernel.org,
	liaochang1@huawei.com, ashok.raj@oss.qualcomm.com,
	maz@kernel.org, will@kernel.org,
	linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH v2 00/39] arm64: Add support for FEAT_NMI
Date: Mon, 23 Nov 2026 10:35:17 +0800	[thread overview]
Message-ID: <fa5c46cc-0f35-4043-9be8-af1102319fb1@huawei.com> (raw)
In-Reply-To: <awBeQIq4WvfIIiqA@shutemov.name>



在 2026/11/20 23:07, Kiryl Shutsemau (Meta) 写道:
> On Fri, Nov 20, 2026 at 10:11:33AM +0800, Jinjie Ruan wrote:
>> Sorry, I seem unable to open this link.
> 
> It is Cong Wang's "fix(sdei): keep event 0 pending while the PE is
> masked", not reviewed yet. I attached it, in case your firmware is
> based on TF-A.
> 
>> Zero. two logs are 0 (rc3 + rc2). The firmware returns success for all
>> targets, including the masked CPU.
> 
> Thanks. So on your firmware it is not a race: every CPU that is in the
> power-down state when the broadcast goes out gets wedged.
> 
>> This method might require a per-CPU mask. If the mask is already set, it
>> will not send an SDEI but instead use a regular IPI instead. However,
>> the code modification appears to be relatively complex.
> 
> I sent a sketch of it in a follow-up to Vladimir. It has two holes,
> fixed in the version below.
> 
> With one flag per CPU, a sender that backs off clears the flag while an
> earlier signal is still on its way, and the event 0 handler can clear
> the flag of a later sender that has passed its check but not yet made
> the SMC. Either way the PE masks with an SGI on its way. Counting the
> signals instead closes it.
> 
> The notifier also has to leave alone a PE that is already masked, as
> one is between sdei_cpuhp_down() and CPU_OFF, where cpuidle still runs.
> A veto there marks it unmasked. Without a veto, CPU_PM_EXIT unmasks it,
> and the flag is still set when the PE comes back masked from CPU_OFF.
> Either way the next signal goes to a masked PE.
> 
> Could you try it on HIP12 with the power-down state enabled? It applies
> to v7.4-rc3.

Hi, Kiryl,

Tested your sdei fix patch on HIP12 against v7.4-rc3 with the power-down
state enabled.  I

sysrq-l has 384/384 answering every time with no timeouts, no
SDEI_EVENT_SIGNAL failures and no lockups.

So in my tests, the fix was effective.

> 
>> Shall I send a patch for the switch?
> 
> Yes, please.

I will add your suggested-by and cc you.

Thanks,
Jinjie

> 
> diff --git a/arch/arm64/include/asm/nmi.h b/arch/arm64/include/asm/nmi.h
> index 2e8974ff8d63..561d062921e4 100644
> --- a/arch/arm64/include/asm/nmi.h
> +++ b/arch/arm64/include/asm/nmi.h
> @@ -44,5 +44,6 @@ static inline void sdei_nmi_stop_cpus(const cpumask_t *mask) { }
>   * Defined in arch/arm64/kernel/smp.c.
>   */
>  void __noreturn arm64_nmi_cpu_stop(struct pt_regs *regs, bool die_on_crash);
> +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.c b/drivers/firmware/arm_sdei.c
> index e8dd2f0f3919..b9972f01e55d 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,51 @@ 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 land on a masked PE. A signal is in flight from the
> + * sender's check until the target takes it.
> + */
> +static DEFINE_PER_CPU(bool, sdei_pe_unmasked);
> +static DEFINE_PER_CPU(atomic_t, sdei_signalling);
> +static DEFINE_PER_CPU(atomic_t, sdei_signals_sent);
> +static DEFINE_PER_CPU(unsigned int, sdei_signals_taken);
> +static DEFINE_PER_CPU(bool, sdei_pm_masked);
> +
> +static bool sdei_signal_in_flight(void)
> +{
> +	unsigned int sent;
> +
> +	if (atomic_read_acquire(this_cpu_ptr(&sdei_signalling)))
> +		return true;
> +
> +	sent = atomic_read(this_cpu_ptr(&sdei_signals_sent));
> +	return sent != this_cpu_read(sdei_signals_taken);
> +}
> +
> +static bool sdei_mark_masked(void)
> +{
> +	this_cpu_write(sdei_pe_unmasked, false);
> +	smp_mb();
> +	return sdei_signal_in_flight();
> +}
> +
> +static void sdei_block_signal(void)
> +{
> +	int i;
> +
> +	if (!sdei_mark_masked())
> +		return;
> +
> +	for (i = 0; i < USEC_PER_MSEC && sdei_signal_in_flight(); i++)
> +		udelay(1);
> +}
> +
>  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 +374,7 @@ int sdei_unmask_local_cpu(void)
>  		return err;
>  	}
>  
> +	this_cpu_write(sdei_pe_unmasked, true);
>  	return 0;
>  }
>  
> @@ -344,10 +389,27 @@ static void _ipi_unmask_cpu(void *ignored)
>   * but the SMC -- no locks, no event lookup -- so it is safe from NMI /
>   * crash context (e.g. the cross-CPU NMI service).
>   */
> -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);
> +	atomic_t *signalling = per_cpu_ptr(&sdei_signalling, cpu);
> +	u64 mpidr = cpu_logical_map(cpu);
> +	int err;
> +
> +	atomic_inc(signalling);
> +	smp_mb__after_atomic();
> +	if (!READ_ONCE(per_cpu(sdei_pe_unmasked, cpu))) {
> +		atomic_dec(signalling);
> +		return -EAGAIN;
> +	}
> +
> +	err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_SIGNAL, event_num,
> +			     mpidr, 0, 0, 0, NULL);
> +	if (!err)
> +		atomic_inc(per_cpu_ptr(&sdei_signals_sent, cpu));
> +
> +	atomic_dec_return_release(signalling);
> +
> +	return err;
>  }
>  NOKPROBE_SYMBOL(sdei_event_signal);
>  
> @@ -680,6 +742,8 @@ static int sdei_cpuhp_down(unsigned int cpu)
>  	struct sdei_event *event;
>  	int err;
>  
> +	sdei_block_signal();
> +
>  	/* un-register private events */
>  	spin_lock(&sdei_list_lock);
>  	list_for_each_entry(event, &sdei_list, list) {
> @@ -700,8 +764,12 @@ static int sdei_cpuhp_down(unsigned int cpu)
>  static int sdei_cpuhp_up(unsigned int cpu)
>  {
>  	struct sdei_event *event;
> +	unsigned int sent;
>  	int err;
>  
> +	sent = atomic_read(this_cpu_ptr(&sdei_signals_sent));
> +	this_cpu_write(sdei_signals_taken, sent);
> +
>  	/* re-register/enable private events */
>  	spin_lock(&sdei_list_lock);
>  	list_for_each_entry(event, &sdei_list, list) {
> @@ -739,10 +807,21 @@ static int sdei_pm_notifier(struct notifier_block *nb, unsigned long action,
>  
>  	switch (action) {
>  	case CPU_PM_ENTER:
> +		if (!this_cpu_read(sdei_pe_unmasked))
> +			return NOTIFY_DONE;
> +		if (sdei_mark_masked()) {
> +			this_cpu_write(sdei_pe_unmasked, true);
> +			return notifier_from_errno(-EBUSY);
> +		}
>  		rv = sdei_mask_local_cpu();
> +		if (!rv)
> +			this_cpu_write(sdei_pm_masked, true);
>  		break;
>  	case CPU_PM_EXIT:
>  	case CPU_PM_ENTER_FAILED:
> +		if (!this_cpu_read(sdei_pm_masked))
> +			return NOTIFY_DONE;
> +		this_cpu_write(sdei_pm_masked, false);
>  		rv = sdei_unmask_local_cpu();
>  		break;
>  	default:
> @@ -1113,6 +1192,12 @@ int sdei_event_handler(struct pt_regs *regs,
>  {
>  	int err;
>  	u32 event_num = arg->event_num;
> +	unsigned int sent;
> +
> +	if (!event_num) {
> +		sent = atomic_read(this_cpu_ptr(&sdei_signals_sent));
> +		__this_cpu_write(sdei_signals_taken, sent);
> +	}
>  
>  	err = arg->callback(event_num, regs, arg->callback_arg);
>  	if (err)
> diff --git a/drivers/firmware/arm_sdei_nmi.c b/drivers/firmware/arm_sdei_nmi.c
> index 038eb79e28ac..9e2ea532888a 100644
> --- a/drivers/firmware/arm_sdei_nmi.c
> +++ b/drivers/firmware/arm_sdei_nmi.c
> @@ -125,15 +125,19 @@ 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, cpu_logical_map(target_cpu));
> +	int err = sdei_event_signal(SDEI_NMI_EVENT, target_cpu);
>  
> -	if (err)
> +	if (err && err != -EAGAIN)
>  		pr_warn("SDEI_EVENT_SIGNAL to CPU %u failed: %d\n",
>  			target_cpu, err);
> +
> +	return err;
>  }
>  
> +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
> @@ -150,8 +154,14 @@ 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);
> +	}
> +
> +	if (!cpumask_empty(&sdei_nmi_ipi_mask))
> +		arm64_backtrace_ipi(&sdei_nmi_ipi_mask);
>  }
>  
>  /*
> diff --git a/include/linux/arm_sdei.h b/include/linux/arm_sdei.h
> index b9dc21c241be..3f565a0f4a17 100644
> --- a/include/linux/arm_sdei.h
> +++ b/include/linux/arm_sdei.h
> @@ -41,7 +41,7 @@ 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.
>   */
> -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);



  reply	other threads:[~2026-11-23  2:35 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 13:20 [PATCH v2 00/39] arm64: Add support for FEAT_NMI Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 01/39] arm64: irqflags: Introduce arm64-specific irqflags type Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 02/39] arm64: irqflags: save and use both DAIF and PMR Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 03/39] arm64: interrupts: Add common exception state helpers Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 04/39] arm64: process: Lift check for exception state to generic infrastructure Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 05/39] arm64: entry: Introduce entry specific exception masking helpers Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 06/39] arm64: entry: replace DAIF helpers with entry helpers Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 07/39] arm64: interrupts: Introduce exception masking save/restore helpers Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 08/39] arm64: interrupts: introduce a helper for GIC priority initialization Vladimir Murzin
2026-11-30 11:14   ` Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 09/39] arm64: replace local_daif helpers Vladimir Murzin
2026-11-25 14:46   ` Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 10/39] arm64: cpuidle: use new helpers to bypass interrupt priority masking Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 11/39] arm64: remove daifflags.h Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 12/39] arm64: gicv3: remove GIC_PRIO_PSR_I_SET Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 13/39] arm64: cpufeature: Remove system_has_prio_mask_debugging() Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 14/39] arm64: irqflags: Switch to CONFIG_DEBUG_IRQFLAGS Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 15/39] arm64: Kconfig: Remove CONFIG_ARM64_DEBUG_PRIORITY_MASKING Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 16/39] efi/runtime-wrappers: Permit architectures to override IRQ flags checks Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 17/39] arm64/efi: Implement override for " Vladimir Murzin
2026-11-13 13:22   ` Mark Brown
2026-11-25 14:53     ` Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 18/39] arm64: booting: Document boot requirements for FEAT_NMI Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 19/39] arm64: sysreg: Add definitions for immediate versions of MSR ALLINT Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 20/39] arm64: probes: Reject stepping over ALLINT Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 21/39] arm64: ptrace: Add PSR_ALLINT_BIT Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 22/39] arm64: idreg: Add an override for FEAT_NMI Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 23/39] arm64: cpufeature: Detect PE support " Vladimir Murzin
     [not found]   ` <303d8cc4-cb4a-443c-a74c-5c5db5588751@sirena.org.uk>
2026-11-25 14:56     ` Vladimir Murzin
2026-11-25 15:05   ` Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 24/39] arm64: nmi: Manage masking for superpriority interrupts Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 25/39] arm64: irq: Report FEAT_NMI masking local IRQs Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 26/39] arm64: nmi: Add handling of superpriority interrupts as NMIs Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 27/39] arm64: suspend: Always initialise PSTATE.ALLINT Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 28/39] arm64/efi: Add ALLINT to IRQ flags checks Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 29/39] arm64: kprobes: Disable NMIs Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 30/39] arm64: smp: Abstract SGI and LPI operations Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 31/39] arm64: smp: Fall back to IRQ when IPI NMI request fails Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 32/39] arm64: nmi: Add Kconfig for NMI Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 33/39] arm64: nmi: Select HAVE_PERF_EVENTS_NMI for FEAT_NMI Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 34/39] irqchip/gic-v3: Rename nmi to pseudo_nmi Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 35/39] irqchip/gic-v3: Implement FEAT_GICv3_NMI support Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 36/39] arm64: smp: Add NMI support for LPI-backed IPIs Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 37/39] genirq: Trust local irqchip when considering IRQ for NMI Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 38/39] genirq: Do not call add_interrupt_randomness() in NMI context Vladimir Murzin
2026-11-12 13:20 ` [PATCH v2 39/39] irqchip/gic-v5: Add NMI support Vladimir Murzin
2026-11-14  5:03 ` [PATCH v2 00/39] arm64: Add support for FEAT_NMI Ashok Raj
2026-11-19 11:18   ` Vladimir Murzin
2026-11-16  6:58 ` Jinjie Ruan
2026-11-16 10:02   ` Vladimir Murzin
2026-11-16 11:01     ` Jinjie Ruan
2026-11-18 13:22       ` Vladimir Murzin
2026-11-19  2:33         ` Jinjie Ruan
2026-11-18 12:17 ` Jinjie Ruan
2026-11-19  1:54   ` Jinjie Ruan
2026-11-19 10:14     ` Vladimir Murzin
2026-11-19 12:45       ` Jinjie Ruan
2026-11-19 14:50         ` Vladimir Murzin
2026-11-19 16:07           ` Kiryl Shutsemau (Meta)
2026-11-19 16:38             ` Kiryl Shutsemau (Meta)
2026-11-20  2:11             ` Jinjie Ruan
2026-11-20 15:07               ` Kiryl Shutsemau (Meta)
2026-11-23  2:35                 ` Jinjie Ruan [this message]
2026-11-19 11:41   ` Jinjie Ruan
2026-11-19 14:46     ` Vladimir Murzin
2026-11-23  2:45       ` Jinjie Ruan

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=fa5c46cc-0f35-4043-9be8-af1102319fb1@huawei.com \
    --to=ruanjinjie@huawei.com \
    --cc=ashok.raj@oss.qualcomm.com \
    --cc=catalin.marinas@arm.com \
    --cc=kas@kernel.org \
    --cc=liaochang1@huawei.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=lpieralisi@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=vladimir.murzin@arm.com \
    --cc=will@kernel.org \
    /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