arm64: sdei: fix entry edge cases, pseudo-NMI
Subsystems (2)
v3 cover arm64: sdei: fix entry edge cases, pseudo-NMI
Hi,
This v3 has a few small fixes and commit message clarifications. More details in the [v1] cover.
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 on non-boot hotplugged CPU
might end up with `current == NULL`
I spent most of my time digging into the details of boot and CPU bringup so I still do not have an SDEI test setup, so any testing especially with CONFIG_ARM64_PSEUDO_NMI=y and irqchip.gicv3_pseudo_nmi=1 would be appreciated !
After some discussion and digging in the code, it looks like the issue patch 3 fixes should be incredibly rare, so we might be OK without pulling this patch at all. (Though the comment change for __entry_task remains a relevant clarification.)
Thanks in advance
Ada
Changes in v3:
- Clean up comment that an SDEI interrupt during KVM world switch would always hyp panic as patch 1 should avoid it (Kiryl)
- Reword the commit message to detail the specifics in which the SDEI handler might get a NULL __entry_task
Changes in [v2] (patch 2 only):
- Properly use pt_regs to save/restore the PMR in __sdei_handler() (Vladimir)
- Update commit message accordingly
[0]: https://lore.kernel.org/r/[email protected]
[v2]: https://lore.kernel.org/r/[email protected]
[v1]: https://lore.kernel.org/r/[email protected]
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 | 38 +++++++++++++++++++++++--------- arch/arm64/kernel/head.S | 3 +++ arch/arm64/kernel/process.c | 3 --- arch/arm64/kernel/sdei.c | 3 --- 4 files changed, 30 insertions(+), 17 deletions(-)
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Signature · 1 line
2.43.0
PATCH v3 1/3 arm64: entry: sdei: always set VBAR_EL1 on SDEI entry
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.
--- arch/arm64/kernel/entry-common.c | 25 ++++++++++++++----------- arch/arm64/kernel/sdei.c | 3 --- 2 files changed, 14 insertions(+), 14 deletions(-)
arch/arm64/kernel/entry-common.c +14 -11
@@ -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);
arch/arm64/kernel/sdei.c +0 -3
@@ -255,9 +255,6 @@ unsigned long __kprobes do_sdei_event(struct pt_regs *regs,* exception registers; __sdei_handler() puts them back. Only the last * one's syndrome survives; report it if it is not one known to be * harmless. -* -* If the event interrupted a KVM world switch, VBAR points at the KVM -* hyp vectors and any such exception is a hyp panic instead. */ nested_elr =read_sysreg (elr_el1); nested_esr =read_sysreg (esr_el1);
Signature · 1 line
2.43.0
PATCH v3 2/3 arm64: entry: sdei: save and restore PMR for pseudo-NMI
In reply to Ada Couprie DiazWhen using pseudo-NMIs, the PMR affects if IRQs are disabled and is not updated by the CPU on exception entry/exit. It must be saved and restored to/from pt_regs, as the DAIF bits are, so that regs_irqs_disabled() can reliably work with pseudo-NMIs.
We do so in the regular entry path, and set it to a known value reflecting that DAIF is masking all interrupts.
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 makes regs_irqs_disabled() reliable in the SDEI handler with pseudo-NMIs enabled, which is critical as the return path to the kernel depends on if the interrupted task had IRQs disabled or not. This also avoids returning to the interrupted task with a different PMR and a possible DAIF/PMR mismatch, as expected by the rest of the kernel.
--- arch/arm64/kernel/entry-common.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/arm64/kernel/entry-common.c +13 -0
@@ -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 ()) { + regs->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 (regs->pmr,SYS_ICC_PMR_EL1 ); + return ret; }#endif /* CONFIG_ARM_SDE_INTERFACE */
Signature · 1 line
2.43.0
PATCH v3 3/3 arm64: head: set __entry_task along with task setup
In reply to Ada Couprie Diaz__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()[0] 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.
[0]: This can only happen on secondary CPUs first brought online after
initial boot :
- all CPUs onlined at boot-time switch at least once before drivers are probed ;
- hotplugged CPUs that booted once still have their __entry_task in memory, so it would not be NULL.
--- arch/arm64/kernel/head.S | 3 +++ arch/arm64/kernel/process.c | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kernel/head.S +3 -0
@@ -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 /*
arch/arm64/kernel/process.c +0 -3
@@ -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);
Signature · 1 line
2.43.0