mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] arm64: sdei: fix entry edge cases, pseudo-NMI
@ 2026-11-17 17:48 Ada Couprie Diaz
  2026-11-17 17:48 ` [PATCH 1/3] arm64: entry: sdei: always set VBAR_EL1 on SDEI entry Ada Couprie Diaz
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Ada Couprie Diaz @ 2026-11-17 17:48 UTC (permalink / raw)
  To: linux-arm-kernel, Will Deacon, Mark Rutland, Catalin Marinas
  Cc: Vladimir Murzin, Marc Zyngier, Kiryl Shutsemau, Breno Leitao,
	Jinjie Ruan, Doug Anderson, Linus Walleij, Peter Zijlstra,
	Thomas Huth, Dawei Li, mrigendrachaubey, kernel-team,
	linux-kernel, Ada Couprie Diaz

Hi,

As discussed in [0], SDEI entry has a number of remaining issues.
This small series aims to fix the biggest potential issues :
 - Patch 1 : with KVM in VHE, VBAR_EL1 might not be correct
 - Patch 2 : PMR was not handled at all when using pseudo-NMI
 - Patch 3 : very early SDEI interrupt might end up with `current == NULL`

I have booted the patches, but I don't have an SDEI test setup yet,
so I would really appreciate real-world testing that nothing breaks while
I set up something, /especially/ with `CONFIG_ARM64_PSEUDO_NMI=y` and
`irqchip.gicv3_pseudo_nmi=1` given the new PMR handling !


Contrary to what I said in [0], patch 1 and 2 fix things in C rather than
in entry.S. SDEI is already a special case and very edge-case-y, so
as we cannot really hope to share code with the regular entry path,
we might as well handle the edge cases that we can in C.

I think everything should be easily backportable if needed, though patch 3
depends on commit 3d8c1a013d78 ("arm64: smp: initialize cpu offset
earlier") which I don't think is in the 5.10 tree.


There are a couple of possible un-addressed issues still:
 - SWPAN is not handled on SDEI entry, but users of both SDEI and SWPAN
   are quite unlikely. Given that SDEI can interrupt anything at any time,
   and SWPAN is already very fiddly because of non-atomic register
   accesses, we might disallow the combination if it causes issues ;
 - Pointer Authentication is not handled either, but is not covered here as
   it does not present an immediate functionnal issue.


Thanks in advance
Ada

Based on v7.4-rc3
[0]: https://lore.kernel.org/r/20261105120835.3321912-1-kirill@shutemov.name 

Ada Couprie Diaz (3):
  arm64: entry: sdei: always set VBAR_EL1 on SDEI entry
  arm64: entry: sdei: save and restore PMR for pseudo-NMI
  arm64: head: set __entry_task along with task setup

 arch/arm64/kernel/entry-common.c | 40 ++++++++++++++++++++++----------
 arch/arm64/kernel/head.S         |  3 +++
 arch/arm64/kernel/process.c      |  3 ---
 3 files changed, 31 insertions(+), 15 deletions(-)


base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
-- 
2.43.0


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

* [PATCH 1/3] arm64: entry: sdei: always set VBAR_EL1 on SDEI entry
  2026-11-17 17:48 [PATCH 0/3] arm64: sdei: fix entry edge cases, pseudo-NMI Ada Couprie Diaz
@ 2026-11-17 17:48 ` Ada Couprie Diaz
  2026-11-17 17:48 ` [PATCH 2/3] arm64: entry: sdei: save and restore PMR for pseudo-NMI Ada Couprie Diaz
  2026-11-17 17:48 ` [PATCH 3/3] arm64: head: set __entry_task along with task setup Ada Couprie Diaz
  2 siblings, 0 replies; 6+ messages in thread
From: Ada Couprie Diaz @ 2026-11-17 17:48 UTC (permalink / raw)
  To: linux-arm-kernel, Will Deacon, Mark Rutland, Catalin Marinas
  Cc: Vladimir Murzin, Marc Zyngier, Kiryl Shutsemau, Breno Leitao,
	Jinjie Ruan, Doug Anderson, Linus Walleij, Peter Zijlstra,
	Thomas Huth, Dawei Li, mrigendrachaubey, kernel-team,
	linux-kernel, Ada Couprie Diaz, stable

SDEI can interrupt at any time, so we cannot guarantee that VBAR_EL1
points to the kernel vectors.
Unconditionally set VBAR_EL1 to the kernel vectors on SDEI entry so
the SDEI handler always runs on the correct vectors.

Commit 9bf6ee7952c7 ("arm64: sdei: Run the event handler on the kernel
vectors") sets VBAR_EL1 to the kernel vectors when KPTI is on
and it still points to the trampoline vectors.

This is only covers the KPTI case, as any other VBAR_EL1 value would
not be changed, while pointing to the wrong exception vectors.
KVM in VHE mode would be such a case as, in VHE, VBAR_EL1 is an alias for
VBAR_EL2, which KVM uses for the hypervisor vectors when running guests.

Fixes: 79e9aa59dc29 ("arm64: sdei: Add trampoline code for remapping the kernel")
Cc: stable@vger.kernel.org
Cc: Kiryl Shutsemau (Meta) <kas@kernel.org>
Cc: Breno Leitao <leitao@debian.org>
Signed-off-by: Ada Couprie Diaz <ada.coupriediaz@arm.com>
---
 arch/arm64/kernel/entry-common.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-common.c
index beb56891fc4f..cb122163a68d 100644
--- a/arch/arm64/kernel/entry-common.c
+++ b/arch/arm64/kernel/entry-common.c
@@ -1079,19 +1079,22 @@ __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
 	far = read_sysreg(far_el1);
 
 	/*
-	 * With KPTI, VBAR_EL1 holds the trampoline vectors from the return to
-	 * EL0 until the next exception from EL0 maps the kernel. They only
-	 * handle exceptions from EL0: an exception taken here would run their
-	 * empty current-EL half and never come back. Switch to the kernel
-	 * vectors. The IRQ-like resume keeps using the interrupted VBAR, so it
-	 * still goes through the trampoline.
+	 * As an SDEI event can take over at any point, VBAR_EL1 might not allow
+	 * us to take an exception safely :
+	 * - With KPTI, VBAR_EL1 holds the trampoline vectors from the return to
+	 *   EL0 until the next exception from EL0 maps the kernel. They only
+	 *   handle exceptions from EL0: an exception taken here would run their
+	 *   empty current-EL half and never come back ;
+	 * - With VHE, the kernel runs at EL2 and VBAR_EL1 accesses VBAR_EL2.
+	 *   KVM might be running a guest and switched VBAR_EL2 to the
+	 *   hypervisor vectors.
+	 *
+	 * Switch to the kernel vectors unconditionally and restore
+	 * the interrupted VBAR_EL1 before returning.
 	 */
 	vbar = read_sysreg(vbar_el1);
-	if (arm64_kernel_unmapped_at_el0() &&
-	    vbar == (unsigned long)raw_cpu_read(this_cpu_vector)) {
-		write_sysreg((unsigned long)vectors, vbar_el1);
-		isb();
-	}
+	write_sysreg((unsigned long)vectors, vbar_el1);
+	isb();
 
 	state = irqentry_nmi_enter(regs);
 	ret = do_sdei_event(regs, arg, vbar);
-- 
2.43.0


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

* [PATCH 2/3] arm64: entry: sdei: save and restore PMR for pseudo-NMI
  2026-11-17 17:48 [PATCH 0/3] arm64: sdei: fix entry edge cases, pseudo-NMI Ada Couprie Diaz
  2026-11-17 17:48 ` [PATCH 1/3] arm64: entry: sdei: always set VBAR_EL1 on SDEI entry Ada Couprie Diaz
@ 2026-11-17 17:48 ` Ada Couprie Diaz
  2026-11-18  9:58   ` Vladimir Murzin
  2026-11-17 17:48 ` [PATCH 3/3] arm64: head: set __entry_task along with task setup Ada Couprie Diaz
  2 siblings, 1 reply; 6+ messages in thread
From: Ada Couprie Diaz @ 2026-11-17 17:48 UTC (permalink / raw)
  To: linux-arm-kernel, Will Deacon, Mark Rutland, Catalin Marinas
  Cc: Vladimir Murzin, Marc Zyngier, Kiryl Shutsemau, Breno Leitao,
	Jinjie Ruan, Doug Anderson, Linus Walleij, Peter Zijlstra,
	Thomas Huth, Dawei Li, mrigendrachaubey, kernel-team,
	linux-kernel, Ada Couprie Diaz, stable

If using pseudo-NMI, PMR must be saved and restored on exception
entry/exit. We do so, and set it to a known value reflecting that DAIF
is masking all interrupts, in the regular entry path.

Do the same for SDEI, as it can interrupt the kernel at any point
and does not mandate firmware to touch PMR, but does mandate masking DAIF.
This avoids potentially returning with a different PMR than interrupted,
and SDEI having the DAIF/PMR pair in a unexpected state compared to
the rest of the kernel.

Fixes: 133d05186325 ("arm64: Make PMR part of task context")
Cc: stable@vger.kernel.org
Signed-off-by: Ada Couprie Diaz <ada.coupriediaz@arm.com>
---
 arch/arm64/kernel/entry-common.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-common.c
index cb122163a68d..17a14c55532c 100644
--- a/arch/arm64/kernel/entry-common.c
+++ b/arch/arm64/kernel/entry-common.c
@@ -1041,7 +1041,7 @@ asmlinkage void noinstr __noreturn handle_bad_stack(struct pt_regs *regs)
 asmlinkage noinstr unsigned long
 __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
 {
-	unsigned long elr, spsr, esr, far, vbar;
+	unsigned long pmr, elr, spsr, esr, far, vbar;
 	irqentry_state_t state;
 	unsigned long ret;
 
@@ -1067,6 +1067,16 @@ __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
 	else if (cpu_has_pan())
 		set_pstate_pan(0);
 
+	/*
+	 * Regular kernel entry saves and resets the PMR to align with DAIF,
+	 * but __sdei_asm_handler() does not ; do it now.
+	 */
+	if (system_uses_irq_prio_masking()) {
+		pmr = read_sysreg_s(SYS_ICC_PMR_EL1);
+		write_sysreg_s(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET,
+				SYS_ICC_PMR_EL1);
+	}
+
 	/*
 	 * Nothing saved the interrupted context's exception registers on the
 	 * way in, and it may be in the middle of exception entry or return
@@ -1110,6 +1120,9 @@ __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
 	write_sysreg(far, far_el1);
 	write_sysreg(vbar, vbar_el1);
 
+	if (system_uses_irq_prio_masking())
+		write_sysreg_s(pmr, SYS_ICC_PMR_EL1);
+
 	return ret;
 }
 #endif /* CONFIG_ARM_SDE_INTERFACE */
-- 
2.43.0


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

* [PATCH 3/3] arm64: head: set __entry_task along with task setup
  2026-11-17 17:48 [PATCH 0/3] arm64: sdei: fix entry edge cases, pseudo-NMI Ada Couprie Diaz
  2026-11-17 17:48 ` [PATCH 1/3] arm64: entry: sdei: always set VBAR_EL1 on SDEI entry Ada Couprie Diaz
  2026-11-17 17:48 ` [PATCH 2/3] arm64: entry: sdei: save and restore PMR for pseudo-NMI Ada Couprie Diaz
@ 2026-11-17 17:48 ` Ada Couprie Diaz
  2026-11-18 11:01   ` Vladimir Murzin
  2 siblings, 1 reply; 6+ messages in thread
From: Ada Couprie Diaz @ 2026-11-17 17:48 UTC (permalink / raw)
  To: linux-arm-kernel, Will Deacon, Mark Rutland, Catalin Marinas
  Cc: Vladimir Murzin, Marc Zyngier, Kiryl Shutsemau, Breno Leitao,
	Jinjie Ruan, Doug Anderson, Linus Walleij, Peter Zijlstra,
	Thomas Huth, Dawei Li, mrigendrachaubey, kernel-team,
	linux-kernel, Ada Couprie Diaz, stable

`__entry_task` is only set after the first `switch_to()`, despite
a proper task being set up in SP_EL0 as soon as CPUs go through
`__{primary,secondary}_switched()`.

As SDEI can interrupt execution at any time, it can interrupt before
the first `switch_to()` and use a null `__entry_task` to set
its current task.

Set `__entry_task` as soon as we have a proper task set up in
`init_cpu_task()` in case we get interrupted before switching threads.

Remove the second paragraph of the comment on `__entry_task`, as it has
been incorrect since the introduction of SDEI, and even more so
with this change.

Fixes: f5df26961853 ("arm64: kernel: Add arch-specific SDEI entry code and CPU masking")
Cc: stable@vger.kernel.org
Signed-off-by: Ada Couprie Diaz <ada.coupriediaz@arm.com>
---
 arch/arm64/kernel/head.S    | 3 +++
 arch/arm64/kernel/process.c | 3 ---
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
index d6ba3c0af811..e53a9c6fec83 100644
--- a/arch/arm64/kernel/head.S
+++ b/arch/arm64/kernel/head.S
@@ -209,6 +209,9 @@ SYM_CODE_END(preserve_boot_args)
 	ldr	w\tmp2, [\tsk, #TSK_TI_CPU]
 	ldr	\tmp1, [\tmp1, \tmp2, lsl #3]
 	set_this_cpu_offset \tmp1
+
+	adr_l	\tmp2, __entry_task
+	str	\tsk, [\tmp2, \tmp1]
 	.endm
 
 /*
diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c
index 42b15b0c069f..c3d194676efc 100644
--- a/arch/arm64/kernel/process.c
+++ b/arch/arm64/kernel/process.c
@@ -584,9 +584,6 @@ static void ssbs_thread_switch(struct task_struct *next)
 /*
  * We store our current task in sp_el0, which is clobbered by userspace. Keep a
  * shadow copy so that we can restore this upon entry from userspace.
- *
- * This is *only* for exception entry from EL0, and is not valid until we
- * __switch_to() a user task.
  */
 DEFINE_PER_CPU(struct task_struct *, __entry_task);
 
-- 
2.43.0


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

* Re: [PATCH 2/3] arm64: entry: sdei: save and restore PMR for pseudo-NMI
  2026-11-17 17:48 ` [PATCH 2/3] arm64: entry: sdei: save and restore PMR for pseudo-NMI Ada Couprie Diaz
@ 2026-11-18  9:58   ` Vladimir Murzin
  0 siblings, 0 replies; 6+ messages in thread
From: Vladimir Murzin @ 2026-11-18  9:58 UTC (permalink / raw)
  To: Ada Couprie Diaz, linux-arm-kernel, Will Deacon, Mark Rutland,
	Catalin Marinas
  Cc: Marc Zyngier, Kiryl Shutsemau, Breno Leitao, Jinjie Ruan,
	Doug Anderson, Linus Walleij, Peter Zijlstra, Thomas Huth,
	Dawei Li, mrigendrachaubey, kernel-team, linux-kernel, stable

Hi Ada,

On 11/17/26 17:48, Ada Couprie Diaz wrote:
> If using pseudo-NMI, PMR must be saved and restored on exception
> entry/exit. We do so, and set it to a known value reflecting that DAIF
> is masking all interrupts, in the regular entry path.
> 
> Do the same for SDEI, as it can interrupt the kernel at any point
> and does not mandate firmware to touch PMR, but does mandate masking DAIF.
> This avoids potentially returning with a different PMR than interrupted,
> and SDEI having the DAIF/PMR pair in a unexpected state compared to
> the rest of the kernel.
> 

I think we need to save/restore PMR into/from pt_regs similarly to regular
entry path, otherwise regs_irqs_disabled() is still broken, like it was
pointed in the thread [1] (and Sashiko has raised the same concern on
FEAT_NMI series)

Cheers
Vladimir

[1] https://lore.kernel.org/all/auzM-l40BLsT3clg@arm.com/

> Fixes: 133d05186325 ("arm64: Make PMR part of task context")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ada Couprie Diaz <ada.coupriediaz@arm.com>
> ---
>  arch/arm64/kernel/entry-common.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-common.c
> index cb122163a68d..17a14c55532c 100644
> --- a/arch/arm64/kernel/entry-common.c
> +++ b/arch/arm64/kernel/entry-common.c
> @@ -1041,7 +1041,7 @@ asmlinkage void noinstr __noreturn handle_bad_stack(struct pt_regs *regs)
>  asmlinkage noinstr unsigned long
>  __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
>  {
> -	unsigned long elr, spsr, esr, far, vbar;
> +	unsigned long pmr, elr, spsr, esr, far, vbar;
>  	irqentry_state_t state;
>  	unsigned long ret;
>  
> @@ -1067,6 +1067,16 @@ __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
>  	else if (cpu_has_pan())
>  		set_pstate_pan(0);
>  
> +	/*
> +	 * Regular kernel entry saves and resets the PMR to align with DAIF,
> +	 * but __sdei_asm_handler() does not ; do it now.
> +	 */
> +	if (system_uses_irq_prio_masking()) {
> +		pmr = read_sysreg_s(SYS_ICC_PMR_EL1);
> +		write_sysreg_s(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET,
> +				SYS_ICC_PMR_EL1);
> +	}
> +
>  	/*
>  	 * Nothing saved the interrupted context's exception registers on the
>  	 * way in, and it may be in the middle of exception entry or return
> @@ -1110,6 +1120,9 @@ __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
>  	write_sysreg(far, far_el1);
>  	write_sysreg(vbar, vbar_el1);
>  
> +	if (system_uses_irq_prio_masking())
> +		write_sysreg_s(pmr, SYS_ICC_PMR_EL1);
> +
>  	return ret;
>  }
>  #endif /* CONFIG_ARM_SDE_INTERFACE */
> -- 2.43.0
> 


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

* Re: [PATCH 3/3] arm64: head: set __entry_task along with task setup
  2026-11-17 17:48 ` [PATCH 3/3] arm64: head: set __entry_task along with task setup Ada Couprie Diaz
@ 2026-11-18 11:01   ` Vladimir Murzin
  0 siblings, 0 replies; 6+ messages in thread
From: Vladimir Murzin @ 2026-11-18 11:01 UTC (permalink / raw)
  To: Ada Couprie Diaz, linux-arm-kernel, Will Deacon, Mark Rutland,
	Catalin Marinas
  Cc: Marc Zyngier, Kiryl Shutsemau, Breno Leitao, Jinjie Ruan,
	Doug Anderson, Linus Walleij, Peter Zijlstra, Thomas Huth,
	Dawei Li, mrigendrachaubey, kernel-team, linux-kernel, stable

Hi Ada,

On 11/17/26 17:48, Ada Couprie Diaz wrote:
> `__entry_task` is only set after the first `switch_to()`, despite
> a proper task being set up in SP_EL0 as soon as CPUs go through
> `__{primary,secondary}_switched()`.
> 
> As SDEI can interrupt execution at any time, it can interrupt before
> the first `switch_to()` and use a null `__entry_task` to set
> its current task.
> 

TBH I do not know much about SDEI, but don't we need opt-in for
SDEI before we can get any SDEI events? If so, can we really
receive SDEI event before first switch_to()?

Cheers
Vladimir


> Set `__entry_task` as soon as we have a proper task set up in
> `init_cpu_task()` in case we get interrupted before switching threads.
> 
> Remove the second paragraph of the comment on `__entry_task`, as it has
> been incorrect since the introduction of SDEI, and even more so
> with this change.
> 
> Fixes: f5df26961853 ("arm64: kernel: Add arch-specific SDEI entry code and CPU masking")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ada Couprie Diaz <ada.coupriediaz@arm.com>
> ---
>  arch/arm64/kernel/head.S    | 3 +++
>  arch/arm64/kernel/process.c | 3 ---
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
> index d6ba3c0af811..e53a9c6fec83 100644
> --- a/arch/arm64/kernel/head.S
> +++ b/arch/arm64/kernel/head.S
> @@ -209,6 +209,9 @@ SYM_CODE_END(preserve_boot_args)
>  	ldr	w\tmp2, [\tsk, #TSK_TI_CPU]
>  	ldr	\tmp1, [\tmp1, \tmp2, lsl #3]
>  	set_this_cpu_offset \tmp1
> +
> +	adr_l	\tmp2, __entry_task
> +	str	\tsk, [\tmp2, \tmp1]
>  	.endm
>  
>  /*
> diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c
> index 42b15b0c069f..c3d194676efc 100644
> --- a/arch/arm64/kernel/process.c
> +++ b/arch/arm64/kernel/process.c
> @@ -584,9 +584,6 @@ static void ssbs_thread_switch(struct task_struct *next)
>  /*
>   * We store our current task in sp_el0, which is clobbered by userspace. Keep a
>   * shadow copy so that we can restore this upon entry from userspace.
> - *
> - * This is *only* for exception entry from EL0, and is not valid until we
> - * __switch_to() a user task.
>   */
>  DEFINE_PER_CPU(struct task_struct *, __entry_task);
>  
> -- 2.43.0
> 


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

end of thread, other threads:[~2026-11-18 11:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 17:48 [PATCH 0/3] arm64: sdei: fix entry edge cases, pseudo-NMI Ada Couprie Diaz
2026-11-17 17:48 ` [PATCH 1/3] arm64: entry: sdei: always set VBAR_EL1 on SDEI entry Ada Couprie Diaz
2026-11-17 17:48 ` [PATCH 2/3] arm64: entry: sdei: save and restore PMR for pseudo-NMI Ada Couprie Diaz
2026-11-18  9:58   ` Vladimir Murzin
2026-11-17 17:48 ` [PATCH 3/3] arm64: head: set __entry_task along with task setup Ada Couprie Diaz
2026-11-18 11:01   ` Vladimir Murzin

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®