arm64: Add support for FEAT_NMI
Subsystems (10)
v2 cover arm64: Add support for FEAT_NMI
FEAT_NMI provides an architected mechanism for supporting non-maskable interrupts (NMIs) and less-masked interrupts (LMIs).
Since we already support pseudo-NMIs via priority masking, introducing another flavour of NMI on top of the existing infrastructure could easily become messy, making the code harder to follow and reason about.
To avoid that, this series first makes room for the new NMI "tenant" by restructuring the existing exception masking logic.
The main idea is to separate the logical view of exception state from its hardware representation. To achieve this, we introduce logical exception contexts that can be mapped onto the corresponding hardware state. This naturally consolidates the hardware-specific handling into a small number of places, while allowing the rest of the code to operate purely in terms of logical exception contexts.
Since this restructuring is non-trivial and carries a risk of subtle behavioural changes, the series adds extensive debug checks to verify that the hardware state always matches the expected logical state.
With this restructuring in place, wiring FEAT_NMI into the new framework becomes much more manageable.
This work would not have been possible without the contributions of Ada Couprie Diaz, Mark Brown, and Lorenzo Pieralisi - please credit them for everything that works well. Any remaining bugs or issues are entirely my own.
I'd especially appreciate feedback on the overall approach. Please don't hesitate to bikeshed the naming or other details - improving clarity is one of the main goals of this series. Of course, technical review is more than welcome as well.
The series would also benefit from extensive testing on real hardware (without NMI, with pseudo-NMI, and with FEAT_NMI), as most of the testing so far has been done on QEMU and Fast Model.
Changelog
RFC v1 -> RFC v2
- Rebased on v7.2-rc5.
- Improved commit descriptions.
- Split some patches into smaller ones.
- Added a graceful fallback for IPI requests when FEAT_NMI is supported by the CPU but unsupported by the interrupt controller.
- Split the GICv3 preparation from the FEAT_GICv3_NMI implementation.
- Added NMI support to GICv5, primarily as an illustration of the implementation, although it also happens to work on Fast Model.
- Added review tags only to unchanged patches. Review tags were intentionally dropped from modified patches to reflect those changes and encourage re-review.
Thanks!
Vladimir
Ada Couprie Diaz (20):
arm64: debug: don't mask DAIF for mdscr_write()
arm64: hibernate: mask DAIF before restoring hibernated kernel
arm64: suspend: rely on daif helpers to handle PMR
arm64: entry: mask DAIF before returning from C EL1 handlers irqchip/gic-v3: make the unmasking of pseudo-NMIs explicit when
handling IRQs
arm64: irqflags: Introduce arm64-specific irqflags type
arm64: irqflags: save and use both DAIF and PMR
arm64: interrupts: Add common exception state helpers
arm64: entry: Introduce entry specific exception masking helpers
arm64: entry: replace DAIF helpers with entry helpers
arm64: interrupts: Introduce exception masking save/restore helpers
arm64: interrupts: introduce a helper for GIC priority initialization
arm64: replace local_daif helpers
arm64: cpuidle: use new helpers to bypass interrupt priority masking
arm64: remove daifflags.h
arm64: gicv3: remove GIC_PRIO_PSR_I_SET
arm64: ptrace: Add PSR_ALLINT_BIT
arm64: cpufeature: Detect PE support for FEAT_NMI
arm64: nmi: Manage masking for superpriority interrupts
arm64: irq: Report FEAT_NMI masking local IRQs
Lorenzo Pieralisi (2):
irqchip/gic-v3: Prepare for FEAT_GICv3_NMI support irqchip/gic-v3: Implement FEAT_GICv3_NMI support
Mark Brown (5):
arm64: booting: Document boot requirements for FEAT_NMI
arm64: sysreg: Add definitions for immediate versions of MSR ALLINT
arm64: idreg: Add an override for FEAT_NMI
arm64: nmi: Add handling of superpriority interrupts as NMIs
arm64: nmi: Add Kconfig for NMI
Vladimir Murzin (18):
arm64: ptrace: Remove INIT_PSTATE_EL2
arm64: hibernate: Restore DAIF state on error
arm64: suspend: Initialize PMR on resume
arm64: entry: Avoid unnecessary local_irq_disable() on kernel exit
arm64: process: Use helper to check exception state
arm64: cpufeature: Remove system_has_prio_mask_debugging()
arm64: irqflags: Switch to CONFIG_DEBUG_IRQFLAGS
arm64: Kconfig: Remove CONFIG_ARM64_DEBUG_PRIORITY_MASKING efi/runtime-wrappers: Permit architectures to override IRQ flags
checks
arm64/efi: Implement override for IRQ flags checks
arm64: suspend: Always initialise PSTATE.ALLINT arm64/efi: Add ALLINT to IRQ flags checks
arm64: kprobes: Disable NMIs
arm64: smp: Abstract SGI and LPI operations
arm64: smp: Fall back to IRQ when IPI NMI request fails
arm64: smp: Add NMI support for LPI-backed IPIs irqchip/gic-v5: Add NMI support for PPIs, SPIs and LPIs irqchip/gic-v5: Add NMI support for IPIs
Documentation/arch/arm64/booting.rst | 6 + arch/arm/include/asm/arch_gicv3.h | 6 +- arch/arm64/Kconfig | 29 +- arch/arm64/include/asm/arch_gicv3.h | 7 +- arch/arm64/include/asm/assembler.h | 24 +- arch/arm64/include/asm/cpucaps.h | 2 + arch/arm64/include/asm/cpufeature.h | 14 +- arch/arm64/include/asm/cpuidle.h | 18 +- arch/arm64/include/asm/daifflags.h | 144 -------- arch/arm64/include/asm/efi.h | 42 ++- arch/arm64/include/asm/entry-common.h | 10 +- .../include/asm/interrupts/common_flags.h | 259 +++++++++++++ arch/arm64/include/asm/interrupts/entry.h | 119 ++++++ arch/arm64/include/asm/interrupts/masking.h | 170 +++++++++ arch/arm64/include/asm/irqflags.h | 149 ++++---- arch/arm64/include/asm/kvm_host.h | 1 - arch/arm64/include/asm/mmu_context.h | 1 - arch/arm64/include/asm/ptrace.h | 27 +- arch/arm64/include/asm/sysreg.h | 2 + arch/arm64/include/uapi/asm/ptrace.h | 1 + arch/arm64/kernel/acpi.c | 15 +- arch/arm64/kernel/cpufeature.c | 68 +++- arch/arm64/kernel/debug-monitors.c | 4 - arch/arm64/kernel/entry-common.c | 347 +++++++++++++----- arch/arm64/kernel/entry.S | 18 +- arch/arm64/kernel/hibernate.c | 26 +- arch/arm64/kernel/irq.c | 45 ++- arch/arm64/kernel/machine_kexec.c | 4 +- arch/arm64/kernel/pi/idreg-override.c | 1 + arch/arm64/kernel/probes/kprobes.c | 24 +- arch/arm64/kernel/process.c | 24 +- arch/arm64/kernel/setup.c | 4 +- arch/arm64/kernel/signal.c | 1 - arch/arm64/kernel/smp.c | 230 ++++++++---- arch/arm64/kernel/suspend.c | 26 +- arch/arm64/kernel/traps.c | 1 - arch/arm64/kvm/hyp/nvhe/switch.c | 2 +- arch/arm64/kvm/hyp/vgic-v3-sr.c | 7 +- arch/arm64/kvm/hyp/vhe/switch.c | 14 +- arch/arm64/mm/fault.c | 1 - arch/arm64/mm/mmu.c | 7 +- arch/arm64/mm/proc.S | 7 + arch/arm64/tools/cpucaps | 2 + drivers/firmware/efi/runtime-wrappers.c | 32 +- drivers/irqchip/irq-gic-v3.c | 137 ++++++- drivers/irqchip/irq-gic-v5.c | 244 +++++++++++- include/linux/irqchip/arm-gic-v3-prio.h | 8 - include/linux/irqchip/arm-gic-v3.h | 4 + kernel/irq/handle.c | 3 +- kernel/irq/manage.c | 11 +- 50 files changed, 1734 insertions(+), 614 deletions(-) delete mode 100644 arch/arm64/include/asm/daifflags.h create mode 100644 arch/arm64/include/asm/interrupts/common_flags.h create mode 100644 arch/arm64/include/asm/interrupts/entry.h create mode 100644 arch/arm64/include/asm/interrupts/masking.h
Signature · 1 line
2.34.1
RFC PATCH v2 1/45 arm64: ptrace: Remove INIT_PSTATE_EL2
Last user of INIT_PSTATE_EL2 has gone with ae4b7e38e9a9 ("arm64: Allow sticky E2H when entering EL1"), so remove it.
--- arch/arm64/include/asm/ptrace.h | 2 -- 1 file changed, 2 deletions(-)
arch/arm64/include/asm/ptrace.h +0 -2
@@ -18,8 +18,6 @@#define INIT_PSTATE_EL1 \ (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL1h) -#define INIT_PSTATE_EL2 \ - (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL2h)#include <linux/irqchip/arm-gic-v3-prio.h>
Signature · 1 line
2.34.1
RFC PATCH v2 2/45 arm64: debug: don't mask DAIF for mdscr_write()
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Masking DAIF around the write to MDSCR_EL1 doesn't do anything: we can write to sysregs with interrupts unmasked, and writing to PSTATE is not a context synchronization event so it does not synchronize it.
This is done in the context of a general interrupt handling cleanup, so it does not address the missing context synchronization for the MDSCR_EL1 write, staying consistent with the current state. This should be addressed in a future patch.
--- arch/arm64/kernel/debug-monitors.c | 3 --- 1 file changed, 3 deletions(-)
arch/arm64/kernel/debug-monitors.c +0 -3
@@ -40,10 +40,7 @@ u8 debug_monitors_arch(void)*/ static void mdscr_write (u64 mdscr) { -unsigned long flags; - flags =local_daif_save ();write_sysreg (mdscr, mdscr_el1); -local_daif_restore (flags); }NOKPROBE_SYMBOL (mdscr_write);
Signature · 1 line
2.34.1
RFC PATCH v2 3/45 arm64: hibernate: mask DAIF before restoring hibernated kernel
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
The arm64 hibernate code manages the exception masking in an unsound way, leading to potential crashes and/or warnings during resume.
When a hibernation image is saved in swsusp_arch_suspend(), all DAIF exceptions are masked (by virtue of local_daif_save()), and the suspended image is saved assuming that all DAIF exceptions will remain masked when the image is restored.
When a hibernation image is resumed by swsusp_arch_resume(), only interrupts are masked (by virtue of local_irq_disable() in resume_target_kernel()). When pseudo-NMI is enabled the DAIF.IF bits will be clear, and regardless of pseudo-NMI the DAIF.DA bits will be clear.
This means that there are two problems:
(1) It is possible to take Debug, SError, or pseudo-NMI exceptions during the resume process. This is unsafe, as during the resume process both the old ane new kernels will tranisently be in an inconsistent state, and swsusp_arch_suspend_exit() won't retain an executable mapping of any exception vectors.
Any exception taken here will be fatal and silent.
(2) When re-entering the resumed kernel, some DAIF bits will be clear unexpectedly. This permits Debug, SError, or pseudo-NMI exceptions to be taken for a short period while the resumed kernel is not yet in a consistent state.
This is detected by CONFIG_ARM64_DEBUG_PRIORITY_MASKING.
Avoid these issues by masking all DAIF exceptions during resume.
--- arch/arm64/kernel/hibernate.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/arm64/kernel/hibernate.c +12 -0
@@ -465,9 +465,21 @@ int __nocfi swsusp_arch_resume(void)if (el2_reset_needed ())__hyp_set_vectors (el2_vectors); +/* +* It is necessary to mask all DAIF exceptions here as: +* +* - The copy of swsusp_arch_suspend_exit() in the hibernation +* text cannot handle taking any exceptions. +* +* - The suspended kernel masked all DAIF exceptions in +* swsusp_arch_resume(), and expects to be re-entered in the +* same state : with all DAIF exceptions masked. +*/ +local_daif_save ();hibernate_exit (virt_to_phys (tmp_pg_dir), resume_hdr.ttbr1_el1, resume_hdr.reenter_kernel, restore_pblist, resume_hdr.__hyp_stub_vectors,virt_to_phys (zero_page)); +unreachable ();return 0 ; }
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 03/45] arm64: hibernate: mask DAIF before restoring hibernated kernel
Quoted 55 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The arm64 hibernate code manages the exception masking in an unsound way, leading to potential crashes and/or warnings during resume.
When a hibernation image is saved in
swsusp_arch_suspend(), all DAIF exceptions are masked (by virtue oflocal_daif_save()), and the suspended image is saved assuming that all DAIF exceptions will remain masked when the image is restored.When a hibernation image is resumed by
swsusp_arch_resume(), only interrupts are masked (by virtue oflocal_irq_disable()inresume_target_kernel()). When pseudo-NMI is enabled the DAIF.IF bits will be clear, and regardless of pseudo-NMI the DAIF.DA bits will be clear.This means that there are two problems:
(1) It is possible to take Debug, SError, or pseudo-NMI exceptions during the resume process. This is unsafe, as during the resume process both the old ane new kernels will tranisently be in an inconsistent state, and
swsusp_arch_suspend_exit()won't retain an executable mapping of any exception vectors.Any exception taken here will be fatal and silent.
(2) When re-entering the resumed kernel, some DAIF bits will be clear unexpectedly. This permits Debug, SError, or pseudo-NMI exceptions to be taken for a short period while the resumed kernel is not yet in a consistent state.
This is detected by
CONFIG_ARM64_DEBUG_PRIORITY_MASKING.Avoid these issues by masking all DAIF exceptions during resume.
Fixes 82869ac57b5d ("arm64: kernel: Add support for hibernate/suspend-to-disk")Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/hibernate.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/arm64/kernel/hibernate.c +5
-0@@ -465,9 +465,21 @@ int __nocfi swsusp_arch_resume(void)if (el2_reset_needed ())__hyp_set_vectors (el2_vectors); +/* +* It is necessary to mask all DAIF exceptions here as: +* +* - The copy of swsusp_arch_suspend_exit() in the hibernation +* text cannot handle taking any exceptions.
arch/arm64/kernel/hibernate.c int __nocfi swsusp_arch_resume(void) · line 482
Quoted 10 lines
arch/arm64/kernel/hibernate.c +7
-0+* +* - The suspended kernel masked all DAIF exceptions in +* swsusp_arch_resume(), and expects to be re-entered in the +* same state : with all DAIF exceptions masked. +*/ +local_daif_save ();hibernate_exit (virt_to_phys (tmp_pg_dir), resume_hdr.ttbr1_el1, resume_hdr.reenter_kernel, restore_pblist, resume_hdr.__hyp_stub_vectors,virt_to_phys (zero_page)); +unreachable ();
Quoted 3 lines
arch/arm64/kernel/hibernate.c +0
-0return 0 ; }
RFC PATCH v2 4/45 arm64: hibernate: Restore DAIF state on error
In reply to Vladimir MurzinSashiko AI has reported that if swsusp_mte_save_tags() for some reason fails we return from swsusp_arch_suspend() with DAIF being masked - that is not what we'd expect. Restore the saved DAIF state before returning from the error path.
--- arch/arm64/kernel/hibernate.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/kernel/hibernate.c +3 -1
@@ -348,8 +348,10 @@ int swsusp_arch_suspend(void)crash_prepare_suspend (); ret =swsusp_mte_save_tags (); -if (ret) +if (ret) { +local_daif_restore (flags);return ret; + } sleep_cpu =smp_processor_id (); ret =swsusp_save ();
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 04/45] arm64: hibernate: Restore DAIF state on error
Quoted 16 lines · Vladimir Murzin
Sashiko AI has reported that if
swsusp_mte_save_tags()for some reason fails we return fromswsusp_arch_suspend()with DAIF being masked - that is not what we'd expect. Restore the saved DAIF state before returning from the error path.Fixes ee11f332af96 ("arm64: mte: Save tags when hibernating")Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/hibernate.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/arm64/kernel/hibernate.c +0
-0
arch/arm64/kernel/hibernate.c int swsusp_arch_suspend(void) · line 354
Quoted 8 lines
arch/arm64/kernel/hibernate.c +3
-1@@ -348,8 +348,10 @@ int swsusp_arch_suspend(void)crash_prepare_suspend (); ret =swsusp_mte_save_tags (); -if (ret) +if (ret) { +local_daif_restore (flags);return ret; + }
We can use a label to reuse the exit path, otherwise LGTM
arch/arm64/kernel/hibernate.c +2 -1
@@ -349,7 +349,7 @@ int swsusp_arch_suspend(void) ret =swsusp_mte_save_tags ();if (ret) -return ret; + goto restore_daif; sleep_cpu =smp_processor_id (); ret =swsusp_save ();
@@ -391,6 +391,7 @@ int swsusp_arch_suspend(void)spectre_v4_enable_mitigation (NULL ); } +restore_daif:local_daif_restore (flags);return ret;
Quoted 3 lines
arch/arm64/kernel/hibernate.c +0
-0sleep_cpu =smp_processor_id (); ret =swsusp_save ();
RFC PATCH v2 5/45 arm64: suspend: rely on daif helpers to handle PMR
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Commit 77345ef70445 ("arm64: suspend: Use cpuidle context helpers in cpu_suspend()") added cpuidle helpers to handle PMR manipulation and restoration to ensure that the CPU receives interrupts when suspended and pseudo-NMIs are enabled.
However, those helpers are called in between a pair of local_daif_save() and local_daif_restore(), which already configure the PMR as expected. Effectively, arm_cpuidle_save_irq_context() is a no-op here, even when using pseudo-NMIs, and arm_cpuidle_restore_irq_context() would not restore proper interrupt masking configuration early enough if there were unexpected changes during suspend or resume. (This can be observed with Trusted Firmware A (TF-A) at EL3 handling suspend through PSCI. Even though it should not be the case, TF-A can reset ICC_PMR_EL1 during CPU_SUSPEND, thus resuming the kernel with an inconsistent priority mask value on hardware implementing more than the minimum number of priority levels, such as Morello.)
Thus : remove the cpuidle context helpers as they do not do anything, but keep the comment mentioning the need for interrupts to reach the CPU if we are using pseudo-NMIs.
--- arch/arm64/kernel/suspend.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
arch/arm64/kernel/suspend.c +3 -9
@@ -99,7 +99,6 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))int ret =0 ;unsigned long flags;struct sleep_stack_data state; -struct arm_cpuidle_irq_context context;/* * Some portions of CPU state (e.g. PSTATE.{PAN,DIT}) are initialized
@@ -121,6 +120,9 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))* Strictly speaking the trace_hardirqs_off() here is superfluous, * hardirqs should be firmly off by now. This really ought to use * something like raw_local_daif_save(). +* +* This also unmasks interrupts in PMR in order to reliably +* resume if we're using pseudo-NMIs. */ flags =local_daif_save ();
@@ -131,12 +133,6 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))*/ pause_graph_tracing (); -/* -* Switch to using DAIF.IF instead of PMR in order to reliably -* resume if we're using pseudo-NMIs. -*/ -arm_cpuidle_save_irq_context (&context); -ct_cpuidle_enter ();if (__cpu_suspend_enter (&state)) {
@@ -159,8 +155,6 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))__cpu_suspend_exit (); } -arm_cpuidle_restore_irq_context (&context); -unpause_graph_tracing ();/*
Signature · 1 line
2.34.1
RFC PATCH v2 6/45 arm64: suspend: Initialize PMR on resume
In reply to Vladimir MurzinWhen we resume from cpu_suspend() context tracking, specially, ct_idle_exit() performs IRQ save/restore sequence. It doesn't cause any functional issues since we have masked all exceptions prior suspend and have not restored them. However, in case of pseudo-NMI PMR can be set by firmware to arbitrary value, thus IRQ save/restore routines manipulates this arbitrary value. Again, it doesn't cause any issues since PMR variant of IRQ save helper carries a __pmr_irqs_disabled_flags() guard.
Going forward __pmr_irqs_disabled_flags() guard will be gone and we will call __pmr_local_irq_disable() unconditionally - that would cause warning in case CONFIG_ARM64_DEBUG_PRIORITY_MASKING is set.
Initialize PMR to a value known to Linux on resume until the normal exception restore path restores the saved DAIF and PMR state.
--- arch/arm64/mm/proc.S | 7 +++++++ 1 file changed, 7 insertions(+)
arch/arm64/mm/proc.S +7 -0
@@ -169,6 +169,13 @@ alternative_if ARM64_HAS_RAS_EXTN msr_s SYS_DISR_EL1, xzr alternative_else_nop_endif +#ifdef CONFIG_ARM64_PSEUDO_NMI +alternative_if ARM64_HAS_GIC_PRIO_MASKING + mov x1, #GIC_PRIO_IRQON + msr_s SYS_ICC_PMR_EL1, x1 +alternative_else_nop_endif +#endif + ptrauth_keys_install_kernel_nosync x14, x1, x2, x3 isb ret
Signature · 1 line
2.34.1
RFC PATCH v2 7/45 arm64: entry: mask DAIF before returning from C EL1 handlers
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Most EL1 exceptions already call local_daif_mask() before returning, with the exception of debug exception handlers which do not change DAIF, and the IRQ/FIQ/Error handlers.
However, DAIF get masked in kernel_exit() in all cases when returning from EL1 C handlers anyway.
Move this masking from assembly to C by calling local_daif_mask() before irqentry_nmi_exit(). Unlike the raw DAIF masking helper, local_daif_mask() invokes trace_hardirqs_off(), so it must execute while RCU is still watching.
Remove the disable_daif assembly macro, as this was its only use.
--- arch/arm64/include/asm/assembler.h | 4 ---- arch/arm64/kernel/entry-common.c | 2 ++ arch/arm64/kernel/entry.S | 4 ---- 3 files changed, 2 insertions(+), 8 deletions(-)
arch/arm64/include/asm/assembler.h +0 -4
@@ -34,10 +34,6 @@ wx\n .req w\n .endr - .macro disable_daif - msr daifset, #0xf - .endm -/* * Save/restore interrupts. */
arch/arm64/kernel/entry-common.c +2 -0
@@ -495,6 +495,7 @@ static __always_inline void __el1_pnmi(struct pt_regs *regs, state =irqentry_nmi_enter (regs);do_interrupt_handler (regs, handler); +local_daif_mask ();irqentry_nmi_exit (regs, state); }
@@ -540,6 +541,7 @@ asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs)local_daif_restore (DAIF_ERRCTX ); state =irqentry_nmi_enter (regs);do_serror (regs, esr); +local_daif_mask ();irqentry_nmi_exit (regs, state); }
arch/arm64/kernel/entry.S +0 -4
@@ -333,10 +333,6 @@ alternative_else_nop_endif .endm .macro kernel_exit, el - .if \el != 0 - disable_daif - .endif - #ifdef CONFIG_ARM64_PSEUDO_NMI alternative_if_not ARM64_HAS_GIC_PRIO_MASKING b .Lskip_pmr_restore\@
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 07/45] arm64: entry: mask DAIF before returning from C EL1 handlers
Quoted 9 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
Most EL1 exceptions already call
local_daif_mask()before returning, with the exception of debug exception handlers which do not change DAIF, and the IRQ/FIQ/Error handlers.However, DAIF get masked in
kernel_exit()in all cases when returning from EL1 C handlers anyway.
Quoted 6 lines
Move this masking from assembly to C by calling
local_daif_mask()beforeirqentry_nmi_exit(). Unlike the raw DAIF masking helper,local_daif_mask()invokestrace_hardirqs_off(), so it must execute while RCU is still watching.Remove the
disable_daifassembly macro, as this was its only use.
LGTM
Quoted 59 lines
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/assembler.h | 4 ---- arch/arm64/kernel/entry-common.c | 2 ++ arch/arm64/kernel/entry.S | 4 ---- 3 files changed, 2 insertions(+), 8 deletions(-)arch/arm64/include/asm/assembler.h +0
-4@@ -34,10 +34,6 @@ wx\n .req w\n .endr - .macro disable_daif - msr daifset, #0xf - .endm -/* * Save/restore interrupts. */ arch/arm64/kernel/entry-common.c +2
-0@@ -495,6 +495,7 @@ static __always_inline void __el1_pnmi(struct pt_regs *regs, state =irqentry_nmi_enter (regs);do_interrupt_handler (regs, handler); +local_daif_mask ();irqentry_nmi_exit (regs, state); }@@ -540,6 +541,7 @@ asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs)local_daif_restore (DAIF_ERRCTX ); state =irqentry_nmi_enter (regs);do_serror (regs, esr); +local_daif_mask ();irqentry_nmi_exit (regs, state); }arch/arm64/kernel/entry.S +0
-4@@ -333,10 +333,6 @@ alternative_else_nop_endif .endm .macro kernel_exit, el - .if \el != 0 - disable_daif - .endif - #ifdef CONFIG_ARM64_PSEUDO_NMI alternative_if_not ARM64_HAS_GIC_PRIO_MASKING b .Lskip_pmr_restore\@
RFC PATCH v2 8/45 irqchip/gic-v3: make the unmasking of pseudo-NMIs explicit when handling IRQs
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
gic_arch_enable_irqs() is only used when handling IRQs (which could be pseudo-NMIs) and unmasking pseudo-NMIs.
The chain of gic_pmr_mask_irqs() and gic_arch_enable_irqs() for it is slightly confusing without further explanation.
Remove gic_arch_enable_irqs() and instead do the whole pseudo-NMI umasking in gic_unmask_pnmis(), making the operation explicit.
--- arch/arm/include/asm/arch_gicv3.h | 6 +----- arch/arm64/include/asm/arch_gicv3.h | 7 +++++-- arch/arm64/include/asm/entry-common.h | 2 +- drivers/irqchip/irq-gic-v3.c | 5 +---- 4 files changed, 8 insertions(+), 12 deletions(-)
arch/arm/include/asm/arch_gicv3.h +1 -5
@@ -246,11 +246,7 @@ static inline void gic_pmr_mask_irqs(void)WARN_ON_ONCE (true); } -static inline void gic_arch_enable_irqs (void ) -{ -/* Should not get called. */ -WARN_ON_ONCE (true); -} +static inline void gic_unmask_pnmis (void ) {}static inline bool gic_has_relaxed_pmr_sync (void ) {
arch/arm64/include/asm/arch_gicv3.h +5 -2
@@ -178,9 +178,12 @@ static inline void gic_pmr_mask_irqs(void)gic_write_pmr (GIC_PRIO_IRQOFF ); } -static inline void gic_arch_enable_irqs (void ) +static inline void gic_unmask_pnmis (void ) { - asmvolatile ("msr daifclr, #3" : : :"memory" ); +if (gic_prio_masking_enabled ()) { +gic_pmr_mask_irqs (); + asmvolatile ("msr daifclr, #3" : : :"memory" ); + } }static inline bool gic_has_relaxed_pmr_sync (void )
arch/arm64/include/asm/entry-common.h +1 -1
@@ -32,7 +32,7 @@ static inline bool arch_irqentry_exit_need_resched(void)/* * DAIF.DA are cleared at the start of IRQ/FIQ handling, and when GIC * priority masking is used the GIC irqchip driver will clear DAIF.IF -* using gic_arch_enable_irqs() for normal IRQs. If anything is set in +* in gic_unmask_pnmis() for normal IRQs. If anything is set in * DAIF we must have handled an NMI, so skip preemption. */ if (system_uses_irq_prio_masking () &&read_sysreg (daif))
drivers/irqchip/irq-gic-v3.c +1 -4
@@ -867,10 +867,7 @@ static void __gic_handle_irq_from_irqson(struct pt_regs *regs)nmi_exit (); } -if (gic_prio_masking_enabled ()) { -gic_pmr_mask_irqs (); -gic_arch_enable_irqs (); - } +gic_unmask_pnmis ();if (!is_nmi)__gic_handle_irq (irqnr, regs);
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 08/45] irqchip/gic-v3: make the unmasking of pseudo-NMIs explicit when handling IRQs
Quoted 9 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
gic_arch_enable_irqs()is only used when handling IRQs (which could be pseudo-NMIs) and unmasking pseudo-NMIs.The chain of
gic_pmr_mask_irqs()andgic_arch_enable_irqs()for it is slightly confusing without further explanation.
Quoted 6 lines
Remove
gic_arch_enable_irqs()and instead do the whole pseudo-NMI umasking ingic_unmask_pnmis(), making the operation explicit.Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>Reviewed-by Jinjie Ruan <[email protected]>
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
RFC PATCH v2 9/45 arm64: entry: Avoid unnecessary local_irq_disable() on kernel exit
In reply to Vladimir MurzinCurrently, when exiting to kernel mode, we attempt involuntary preemption. The preemption logic expects IRQs to be disabled, which is why we call local_irq_disable() before attempting preemption.
However, depending on the context, local_irq_disable() may be unnecessary:
- __el1_irq(), the non-NMI EL1 IRQ path, already has IRQs disabled, so local_irq_disable() is redundant.
- irqentry_exit_to_kernel_mode_preempt() immediately returns when exiting from an NMI-like context, so calling local_irq_disable() beforehand is unnecessary work.
Split arm64_exit_to_kernel_mode() into preempt, non-preempt, and dispatch parts so that we can avoid this extra work where it is not needed.
--- arch/arm64/kernel/entry-common.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-)
arch/arm64/kernel/entry-common.c +25 -5
@@ -52,16 +52,36 @@ static noinstr irqentry_state_t arm64_enter_from_kernel_mode(struct pt_regs *reg* After this function returns it is not safe to call regular kernel code, * instrumentable code, or any code which may trigger an exception. */ -static void noinstr arm64_exit_to_kernel_mode(struct pt_regs *regs, -irqentry_state_t state) +static void noinstr __arm64_exit_to_kernel_mode(struct pt_regs *regs, +irqentry_state_t state) { -local_irq_disable (); -irqentry_exit_to_kernel_mode_preempt (regs, state);local_daif_mask ();mte_check_tfsr_exit ();irqentry_exit_to_kernel_mode_after_preempt (regs, state); } +/* +* We are returning from the context which allows involuntary kernel preemption +*/ +static void noinstr arm64_exit_to_kernel_mode_preempt(struct pt_regs *regs, +irqentry_state_t state) +{ +irqentry_exit_to_kernel_mode_preempt (regs, state); +__arm64_exit_to_kernel_mode (regs, state); +} + +static void noinstr arm64_exit_to_kernel_mode(struct pt_regs *regs, +irqentry_state_t state) +{ +if (!regs_irqs_disabled (regs)) { +local_irq_disable (); +arm64_exit_to_kernel_mode_preempt (regs, state); +return ; + } + +__arm64_exit_to_kernel_mode (regs, state); +} + static __always_inline void arm64_syscall_enter_from_user_mode(struct pt_regs *regs) {enter_from_user_mode (regs);
@@ -510,7 +530,7 @@ static __always_inline void __el1_irq(struct pt_regs *regs,do_interrupt_handler (regs, handler);irq_exit_rcu (); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode_preempt (regs, state); } static void noinstr el1_interrupt(struct pt_regs *regs,void (*handler)(struct pt_regs *))
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 09/45] arm64: entry: Avoid unnecessary local_irq_disable() on kernel exit
Quoted 10 lines · Vladimir Murzin
Currently, when exiting to kernel mode, we attempt involuntary preemption. The preemption logic expects IRQs to be disabled, which is why we call
local_irq_disable()before attempting preemption.However, depending on the context,
local_irq_disable()may be unnecessary:-
__el1_irq(), the non-NMI EL1 IRQ path, already has IRQs disabled, solocal_irq_disable()is redundant.
Quoted 7 lines
-
irqentry_exit_to_kernel_mode_preempt()immediately returns when exiting from an NMI-like context, so callinglocal_irq_disable()beforehand is unnecessary work.Split
arm64_exit_to_kernel_mode()into preempt, non-preempt, and dispatch parts so that we can avoid this extra work where it is not needed.
Make sense for me, in an NMI-like context, irqentry_exit_to_kernel_mode_preempt() and local_irq_disable() are both unnecessary.
Quoted 18 lines
Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/entry-common.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-)arch/arm64/kernel/entry-common.c +2
-2@@ -52,16 +52,36 @@ static noinstr irqentry_state_t arm64_enter_from_kernel_mode(struct pt_regs *reg* After this function returns it is not safe to call regular kernel code, * instrumentable code, or any code which may trigger an exception. */ -static void noinstr arm64_exit_to_kernel_mode(struct pt_regs *regs, -irqentry_state_t state) +static void noinstr __arm64_exit_to_kernel_mode(struct pt_regs *regs, +irqentry_state_t state)
arch/arm64/kernel/entry-common.c static noinstr irqentry_state_t arm64_enter_from_kernel_mode(struct pt_regs *reg · line 64
Quoted 10 lines
arch/arm64/kernel/entry-common.c +2
-2{ -local_irq_disable (); -irqentry_exit_to_kernel_mode_preempt (regs, state);local_daif_mask ();mte_check_tfsr_exit ();irqentry_exit_to_kernel_mode_after_preempt (regs, state); } +/* +* We are returning from the context which allows involuntary kernel preemption
Should we add the irq disabled status for preemption?
We are returning from the context which allows involuntary kernel preemption "with irq disabled" ?
Quoted 32 lines
arch/arm64/kernel/entry-common.c +21
-1+*/ +static void noinstr arm64_exit_to_kernel_mode_preempt(struct pt_regs *regs, +irqentry_state_t state) +{ +irqentry_exit_to_kernel_mode_preempt (regs, state); +__arm64_exit_to_kernel_mode (regs, state); +} + +static void noinstr arm64_exit_to_kernel_mode(struct pt_regs *regs, +irqentry_state_t state) +{ +if (!regs_irqs_disabled (regs)) { +local_irq_disable (); +arm64_exit_to_kernel_mode_preempt (regs, state); +return ; + } + +__arm64_exit_to_kernel_mode (regs, state); +} + static __always_inline void arm64_syscall_enter_from_user_mode(struct pt_regs *regs) {enter_from_user_mode (regs);@@ -510,7 +530,7 @@ static __always_inline void __el1_irq(struct pt_regs *regs,do_interrupt_handler (regs, handler);irq_exit_rcu (); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode_preempt (regs, state); } static void noinstr el1_interrupt(struct pt_regs *regs,void (*handler)(struct pt_regs *))
Re: [RFC PATCH v2 09/45] arm64: entry: Avoid unnecessary local_irq_disable() on kernel exit
Quoted 7 lines · Jinjie Ruan
Quoted 2 lines
+/*
+ * We are returning from the context which allows involuntary kernel preemptionShould we add the irq disabled status for preemption?
We are returning from the context which allows involuntary kernel preemption "with irq disabled" ?
Make sense. I'll add it in the next version.
Quoted 1 line
Reviewed-by Jinjie Ruan <[email protected]>
Cheers
Vladimir
RFC PATCH v2 10/45 arm64: irqflags: Introduce arm64-specific irqflags type
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
With pseudo-NMIs enabled, we have two mechanisms that control interrupt masking in parallel :
- The DAIF flags, masking at the CPU
- The GIC PMR, masking before the CPU
However, our irqflags implementation currently assumes that only one of the two is used at a time, so both DAIF and PMR masking use the same unsigned long flags in their own way.
This is incorrect, as some parts of the kernel will mask interrupts with DAIF directly or bypass the local_irq masking via the PMR, and makes tracking the state and changes of both in parallel impossible.
The irqflags API expects unsigned longs to be passed around, but they should not be manipulated outside of the arch-specific code. So, we can encode the information we need however we want as long as we return and accept unsigned longs.
Introduce a union type for arm64 irqflags whose first member is a struct allowing us to track DAIF and PMR in parallel, and the second is the unsigned long expected by the irqflags API.
DAIF is a two byte value, to maintain compatibility with existing defines. PMR is a one byte value, which is the maximum amount of priority bits allowed by the GICv3 architecture.
Update the internal irqflags functions to use this new union and convert back and forth with the irqflags unsigned long.
There should be no functional changes.
--- arch/arm64/include/asm/irqflags.h | 43 ++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-)
arch/arm64/include/asm/irqflags.h +37 -6
@@ -9,6 +9,8 @@#include <asm/ptrace.h> #include <asm/sysreg.h> +#include <linux/compiler.h> +/* * Aarch64 has flags for masking: Debug, Asynchronous (serror), Interrupts and * FIQ exceptions, in the 'daif' register. We mask and unmask them in 'daif'
@@ -20,6 +22,23 @@* exceptions should be unmasked. */ +/* +* Internally, we want to independently manipulate and track the different +* interrupt masking mechanisms. +* Externally, the generic irqflags API expects unsgined longs to represent +* the state of interrupts, which are treated as obscure arch-specific data. +*/ +typedef union arm64_exc_hwstate { +struct { +u16 daif; +u8 pmr; +u8 __padding[5 ]; + }; +unsigned long flags; +}arm64_exc_hwstate_t ; + +static_assert (sizeof (arm64_exc_hwstate_t) ==sizeof (unsigned long )); + static __always_inline void __daif_local_irq_enable(void) {barrier ();
@@ -79,12 +98,16 @@ static __always_inline void arch_local_irq_disable(void)static __always_inline unsigned long __daif_local_save_flags (void ) { -return read_sysreg (daif); +arm64_exc_hwstate_t hwstate = { .daif =read_sysreg (daif) }; + +return hwstate.flags; }static __always_inline unsigned long __pmr_local_save_flags (void ) { -return read_sysreg_s (SYS_ICC_PMR_EL1 ); +arm64_exc_hwstate_t hwstate = { .pmr =read_sysreg_s (SYS_ICC_PMR_EL1 ) }; + +return hwstate.flags; }/*
@@ -101,12 +124,16 @@ static __always_inline unsigned long arch_local_save_flags(void) static __always_inline bool __daif_irqs_disabled_flags(unsigned long flags) { -return flags &PSR_I_BIT ; +arm64_exc_hwstate_t hwstate = { .flags = flags }; + +return hwstate.daif &PSR_I_BIT ; } static __always_inline bool __pmr_irqs_disabled_flags(unsigned long flags) { -return flags !=GIC_PRIO_IRQON ; +arm64_exc_hwstate_t hwstate = { .flags = flags }; + +return hwstate.pmr !=GIC_PRIO_IRQON ; } static __always_inline bool arch_irqs_disabled_flags(unsigned long flags)
@@ -171,15 +198,19 @@ static __always_inline unsigned long arch_local_irq_save(void) static __always_inline void __daif_local_irq_restore(unsigned long flags) { +arm64_exc_hwstate_t hwstate = { .flags = flags }; +barrier (); -write_sysreg (flags, daif); +write_sysreg (hwstate.daif, daif);barrier (); } static __always_inline void __pmr_local_irq_restore(unsigned long flags) { +arm64_exc_hwstate_t hwstate = { .flags = flags }; +barrier (); -write_sysreg_s (flags,SYS_ICC_PMR_EL1 ); +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 );pmr_sync ();barrier (); }
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 10/45] arm64: irqflags: Introduce arm64-specific irqflags type
Quoted 31 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
With pseudo-NMIs enabled, we have two mechanisms that control interrupt masking in parallel :
- The DAIF flags, masking at the CPU
- The GIC PMR, masking before the CPUHowever, our irqflags implementation currently assumes that only one of the two is used at a time, so both DAIF and PMR masking use the same
unsigned long flagsin their own way.
This is incorrect, as some parts of the kernel will mask interrupts with DAIF directly or bypass thelocal_irqmasking via the PMR, and makes tracking the state and changes of both in parallel impossible.The irqflags API expects
unsigned longs to be passed around, but they should not be manipulated outside of the arch-specific code. So, we can encode the information we need however we want as long as we return and acceptunsigned longs.Introduce a union type for arm64 irqflags whose first member is a struct allowing us to track DAIF and PMR in parallel, and the second is the
unsigned longexpected by the irqflags API.DAIF is a two byte value, to maintain compatibility with existing defines. PMR is a one byte value, which is the maximum amount of priority bits allowed by the GICv3 architecture.
Update the internal irqflags functions to use this new union and convert back and forth with the irqflags unsigned long.
There should be no functional changes.
Quoted 5 lines
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/irqflags.h | 43 ++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-)
LGTM
Quoted 98 lines
arch/arm64/include/asm/irqflags.h +37
-6@@ -9,6 +9,8 @@#include <asm/ptrace.h> #include <asm/sysreg.h> +#include <linux/compiler.h> +/* * Aarch64 has flags for masking: Debug, Asynchronous (serror), Interrupts and * FIQ exceptions, in the 'daif' register. We mask and unmask them in 'daif' @@ -20,6 +22,23 @@* exceptions should be unmasked. */ +/* +* Internally, we want to independently manipulate and track the different +* interrupt masking mechanisms. +* Externally, the generic irqflags API expects unsgined longs to represent +* the state of interrupts, which are treated as obscure arch-specific data. +*/ +typedef union arm64_exc_hwstate { +struct { +u16 daif; +u8 pmr; +u8 __padding[5 ]; + }; +unsigned long flags; +}arm64_exc_hwstate_t ; + +static_assert (sizeof (arm64_exc_hwstate_t) ==sizeof (unsigned long )); + static __always_inline void __daif_local_irq_enable(void) {barrier ();@@ -79,12 +98,16 @@ static __always_inline void arch_local_irq_disable(void)static __always_inline unsigned long __daif_local_save_flags (void ) { -return read_sysreg (daif); +arm64_exc_hwstate_t hwstate = { .daif =read_sysreg (daif) }; + +return hwstate.flags; }static __always_inline unsigned long __pmr_local_save_flags (void ) { -return read_sysreg_s (SYS_ICC_PMR_EL1 ); +arm64_exc_hwstate_t hwstate = { .pmr =read_sysreg_s (SYS_ICC_PMR_EL1 ) }; + +return hwstate.flags; }/* @@ -101,12 +124,16 @@ static __always_inline unsigned long arch_local_save_flags(void) static __always_inline bool __daif_irqs_disabled_flags(unsigned long flags) { -return flags &PSR_I_BIT ; +arm64_exc_hwstate_t hwstate = { .flags = flags }; + +return hwstate.daif &PSR_I_BIT ; } static __always_inline bool __pmr_irqs_disabled_flags(unsigned long flags) { -return flags !=GIC_PRIO_IRQON ; +arm64_exc_hwstate_t hwstate = { .flags = flags }; + +return hwstate.pmr !=GIC_PRIO_IRQON ; } static __always_inline bool arch_irqs_disabled_flags(unsigned long flags)@@ -171,15 +198,19 @@ static __always_inline unsigned long arch_local_irq_save(void) static __always_inline void __daif_local_irq_restore(unsigned long flags) { +arm64_exc_hwstate_t hwstate = { .flags = flags }; +barrier (); -write_sysreg (flags, daif); +write_sysreg (hwstate.daif, daif);barrier (); } static __always_inline void __pmr_local_irq_restore(unsigned long flags) { +arm64_exc_hwstate_t hwstate = { .flags = flags }; +barrier (); -write_sysreg_s (flags,SYS_ICC_PMR_EL1 ); +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 );pmr_sync ();barrier (); }
Re: [RFC PATCH v2 10/45] arm64: irqflags: Introduce arm64-specific irqflags type
In reply to Vladimir Murzinarch/arm64/include/asm/irqflags.h line 35
Quoted 12 lines · Vladimir Murzin
+ /*
arch/arm64/include/asm/irqflags.h +10
-0+* Internally, we want to independently manipulate and track the different +* interrupt masking mechanisms. +* Externally, the generic irqflags API expects unsgined longs to represent +* the state of interrupts, which are treated as obscure arch-specific data. +*/ +typedef union arm64_exc_hwstate { + struct { + u16 daif; + u8 pmr; + u8 __padding[5];
This padding hurts code generation. It is better expressed as:
u8 __pad0;
u32 __pad1;
Quoted 4 lines
arch/arm64/include/asm/irqflags.h +4
-0+ }; +unsigned long flags; +} arm64_exc_hwstate_t; +
RFC PATCH v2 11/45 arm64: irqflags: save and use both DAIF and PMR
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
With pseudo-NMIs enabled, both DAIF and the PMR affect interrupt masking. Now that we have a type which can track both of them at the same time, update our irqflags implementation to use it.
Save DAIF flags in all cases, as they can be manipulated directly by other code, and the PMR if it is in use.
When checking if IRQs are disabled, now that we always save DAIF we can check that the I flag is set and bypass checking the PMR if it is. We can also properly check if PMR masks interrupts (PMR < GIC_PRIO_IRQON), now that we don't need to rely on the GIC_PRIO_PSR_I_SET bit being set in the PMR to know if DAIF is already masking interrupts.
Update irqs_priority_unmasked() to align with this change.
This allows us to remove the __daif_... and __pmr_... versions of the save and check functions, as they are now unified.
We can reasonably merge the two __{daif,pmr}_irq_restore() functions in the main one, as the DAIF and PMR values are properly split now.
--- arch/arm64/include/asm/irqflags.h | 118 +++++++----------------------- arch/arm64/include/asm/ptrace.h | 18 +++-- 2 files changed, 38 insertions(+), 98 deletions(-)
arch/arm64/include/asm/irqflags.h +26 -92
@@ -96,135 +96,69 @@ static __always_inline void arch_local_irq_disable(void) } } -static __always_inline unsigned long __daif_local_save_flags (void ) -{ -arm64_exc_hwstate_t hwstate = { .daif =read_sysreg (daif) }; - -return hwstate.flags; -} - -static __always_inline unsigned long __pmr_local_save_flags (void ) -{ -arm64_exc_hwstate_t hwstate = { .pmr =read_sysreg_s (SYS_ICC_PMR_EL1 ) }; - -return hwstate.flags; -} -/* * Save the current interrupt enable state. */ static __always_inline unsigned long arch_local_save_flags (void ) { -if (system_uses_irq_prio_masking ()) { -return __pmr_local_save_flags (); - }else { -return __daif_local_save_flags (); - } -} +arm64_exc_hwstate_t hwstate = { .daif =read_sysreg (daif) }; -static __always_inline bool __daif_irqs_disabled_flags(unsigned long flags) -{ -arm64_exc_hwstate_t hwstate = { .flags = flags }; +if (system_uses_irq_prio_masking ()) + hwstate.pmr =read_sysreg_s (SYS_ICC_PMR_EL1 ); -return hwstate.daif &PSR_I_BIT ; +return hwstate.flags; } -static __always_inline bool __pmr_irqs_disabled_flags(unsigned long flags) +static __always_inline bool arch_irqs_disabled_flags(unsigned long flags) {arm64_exc_hwstate_t hwstate = { .flags = flags }; -return hwstate.pmr !=GIC_PRIO_IRQON ; -} +if (hwstate.daif &PSR_I_BIT ) +return true; -static __always_inline bool arch_irqs_disabled_flags(unsigned long flags) -{if (system_uses_irq_prio_masking ()) { -return __pmr_irqs_disabled_flags (flags); - }else { -return __daif_irqs_disabled_flags (flags); - } -} +WARN_ON_ONCE (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS ) && + hwstate.pmr !=GIC_PRIO_IRQOFF && + hwstate.pmr !=GIC_PRIO_IRQON ); -static __always_inline bool __daif_irqs_disabled(void) -{ -return __daif_irqs_disabled_flags (__daif_local_save_flags ()); -} +return hwstate.pmr !=GIC_PRIO_IRQON ; + } -static __always_inline bool __pmr_irqs_disabled(void) -{ -return __pmr_irqs_disabled_flags (__pmr_local_save_flags ()); +return false; } static __always_inline bool arch_irqs_disabled(void) { -if (system_uses_irq_prio_masking ()) { -return __pmr_irqs_disabled (); - }else { -return __daif_irqs_disabled (); - } +return arch_irqs_disabled_flags (arch_local_save_flags ()); } -static __always_inline unsigned long __daif_local_irq_save (void ) -{ -unsigned long flags =__daif_local_save_flags (); - -__daif_local_irq_disable (); - -return flags; -} - -static __always_inline unsigned long __pmr_local_irq_save (void ) +static __always_inline unsigned long arch_local_irq_save (void ) { -unsigned long flags =__pmr_local_save_flags (); +unsigned long flags =arch_local_save_flags (); -/* -* There are too many states with IRQs disabled, just keep the current -* state if interrupts are already disabled/masked. -*/ -if (!__pmr_irqs_disabled_flags (flags)) +if (system_uses_irq_prio_masking ())__pmr_local_irq_disable (); +else +__daif_local_irq_disable ();return flags; } -static __always_inline unsigned long arch_local_irq_save (void ) -{ -if (system_uses_irq_prio_masking ()) { -return __pmr_local_irq_save (); - }else { -return __daif_local_irq_save (); - } -} - -static __always_inline void __daif_local_irq_restore(unsigned long flags) -{ -arm64_exc_hwstate_t hwstate = { .flags = flags }; - -barrier (); -write_sysreg (hwstate.daif, daif); -barrier (); -} - -static __always_inline void __pmr_local_irq_restore(unsigned long flags) -{ -arm64_exc_hwstate_t hwstate = { .flags = flags }; - -barrier (); -write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); -pmr_sync (); -barrier (); -} -/* * restore saved IRQ state */ static __always_inline void arch_local_irq_restore(unsigned long flags) { +arm64_exc_hwstate_t hwstate = { .flags = flags }; + +barrier ();if (system_uses_irq_prio_masking ()) { -__pmr_local_irq_restore (flags); +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); +pmr_sync (); }else { -__daif_local_irq_restore (flags); +write_sysreg (hwstate.daif, daif); } +barrier (); }#endif /* __ASM_IRQFLAGS_H */
arch/arm64/include/asm/ptrace.h +12 -6
@@ -203,14 +203,20 @@ static inline void forget_syscall(struct pt_regs *regs)#define processor_mode (regs) \ ((regs)->pstate & PSR_MODE_MASK) -#define irqs_priority_unmasked (regs) \ - (system_uses_irq_prio_masking() ? \ - (regs)->pmr == GIC_PRIO_IRQON : \ - true) - static __always_inline bool regs_irqs_disabled(const struct pt_regs *regs) { -return (regs->pstate &PSR_I_BIT ) || !irqs_priority_unmasked (regs); +if (regs->pstate &PSR_I_BIT ) +return true; + +if (system_uses_irq_prio_masking ()) { +WARN_ON_ONCE (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS ) && + regs->pmr !=GIC_PRIO_IRQOFF && + regs->pmr !=GIC_PRIO_IRQON ); + +return regs->pmr !=GIC_PRIO_IRQON ; + } + +return false; }#define interrupts_enabled (regs) (!regs_irqs_disabled(regs))
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 11/45] arm64: irqflags: save and use both DAIF and PMR
Quoted 6 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
With pseudo-NMIs enabled, both DAIF and the PMR affect interrupt masking. Now that we have a type which can track both of them at the same time, update our irqflags implementation to use it.
Quoted 6 lines
Save DAIF flags in all cases, as they can be manipulated directly by other code, and the PMR if it is in use.
When checking if IRQs are disabled, now that we always save DAIF we can check that the I flag is set and bypass checking the PMR if it is. We can also properly check if PMR masks interrupts (PMR <
GIC_PRIO_IRQON),
This commit message need to be updated, we now do not use "<"
Quoted 25 lines
now that we don't need to rely on the
GIC_PRIO_PSR_I_SETbit being set in the PMR to know if DAIF is already masking interrupts.
Updateirqs_priority_unmasked()to align with this change.This allows us to remove the
__daif_...and__pmr_...versions of the save and check functions, as they are now unified.We can reasonably merge the two
__{daif,pmr}_irq_restore()functions in the main one, as the DAIF and PMR values are properly split now.Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/irqflags.h | 118 +++++++----------------------- arch/arm64/include/asm/ptrace.h | 18 +++-- 2 files changed, 38 insertions(+), 98 deletions(-)arch/arm64/include/asm/irqflags.h +0
-0@@ -96,135 +96,69 @@ static __always_inline void arch_local_irq_disable(void) } }
[...]
Quoted 6 lines
arch/arm64/include/asm/irqflags.h +2
-3- -static __always_inline unsigned long __pmr_local_irq_save (void ) +static __always_inline unsigned long arch_local_irq_save (void ) { -unsigned long flags =__pmr_local_save_flags (); +unsigned long flags =arch_local_save_flags ();
arch/arm64/include/asm/irqflags.h static __always_inline void arch_local_irq_disable(void) · line 142
Quoted 10 lines
arch/arm64/include/asm/irqflags.h +3
-5-/* -* There are too many states with IRQs disabled, just keep the current -* state if interrupts are already disabled/masked. -*/ -if (!__pmr_irqs_disabled_flags (flags)) +if (system_uses_irq_prio_masking ())__pmr_local_irq_disable (); +else +__daif_local_irq_disable ();
Now we do not check whether pmr is GIC_PRIO_IRQON for __pmr_local_irq_disable(), however, it doesn't seem to be mentioned in the commit message.
And the previous "arm64: suspend: Initialize PMR on resume" patch should be merged into this patch, Otherwise, you cannot understand its commit message or associate it with the patch.
Quoted 4 lines
return flags;
}
[..]
Quoted 21 lines
}
#endif /*
__ASM_IRQFLAGS_H*/arch/arm64/include/asm/ptrace.h +2
-6@@ -203,14 +203,20 @@ static inline void forget_syscall(struct pt_regs *regs)#define processor_mode (regs) \ ((regs)->pstate & PSR_MODE_MASK) -#define irqs_priority_unmasked (regs) \ - (system_uses_irq_prio_masking() ? \ - (regs)->pmr == GIC_PRIO_IRQON : \ - true) - static __always_inline bool regs_irqs_disabled(const struct pt_regs *regs) { -return (regs->pstate &PSR_I_BIT ) || !irqs_priority_unmasked (regs); +if (regs->pstate &PSR_I_BIT ) +return true;
arch/arm64/include/asm/ptrace.h static inline void forget_syscall(struct pt_regs *regs) · line 219
Quoted 10 lines
arch/arm64/include/asm/ptrace.h +10
-0+ +if (system_uses_irq_prio_masking ()) { +WARN_ON_ONCE (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS ) && + regs->pmr !=GIC_PRIO_IRQOFF && + regs->pmr !=GIC_PRIO_IRQON ); + +return regs->pmr !=GIC_PRIO_IRQON ; + } + +return false;
Looks fine to me.
otherwise, LGTM
Quoted 3 lines
arch/arm64/include/asm/ptrace.h +0
-0}#define interrupts_enabled (regs) (!regs_irqs_disabled(regs))
RFC PATCH v2 12/45 arm64: interrupts: Add common exception state helpers
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
The entry code and the rest of the kernel require different interrupt masking APIs, but all need a common representation of the hardware exception state.
Introduce exception contexts that map DAIF and PMR state to the corresponding masking context. Provide helpers to translate between exception contexts and hardware state, verify the current state, and update the hardware state.
Updating PMR is unnecessary when a caller knows that its value has not changed. Allow such callers to avoid the update, while providing a force option for callers that cannot rely on the previous PMR state.
Entry specific and general purpose masking APIs will be built on these helpers in subsequent patches.
--- .../include/asm/interrupts/common_flags.h | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/common_flags.h
arch/arm64/include/asm/interrupts/common_flags.h new file +208 -0
@@ -0,0 +1,208 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_COMMON_FLAGS_H +#define __ASM_INTERRUPTS_COMMON_FLAGS_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/ptrace.h> +#include <asm/sysreg.h> +#include <asm/irqflags.h> + +#define DAIF_PROCCTX 0 +#define DAIF_PROCCTX_NOIRQ (PSR_I_BIT | PSR_F_BIT) +#define DAIF_ERRCTX (PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) +#define DAIF_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) + +/* +* Exception context mapping +* +* pseudo-NMI +* +* CRITICAL -> DAIF + IRQON (corresponds to the state on exception entry) +* ERROR -> AIF + IRQON +* NONMI -> IF + IRQON +* NOIRQ -> 0 + IRQOFF +* PROCESS -> 0 + IRQON +* +* Otherwise +* +* CRITICAL -> DAIF (corresponds to the state on exception entry) +* ERROR -> AIF +* NONMI -> IF +* NOIRQ -> IF +* PROCESS -> 0 +*/ +typedef enum arm64_exc_context { +PROCESS_CONTEXT , +NOIRQ_CONTEXT , +NONMI_CONTEXT , +ERROR_CONTEXT , +CRITICAL_CONTEXT , +}arm64_exc_context_t ; + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_process_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX , .pmr=GIC_PRIO_IRQON }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX }; +} + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_noirq_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX , .pmr=GIC_PRIO_IRQOFF }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ }; +} + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_nonmi_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ , .pmr=GIC_PRIO_IRQON }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ }; +} + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_error_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX , .pmr=GIC_PRIO_IRQON }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX }; +} + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_critical_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_MASK , .pmr=GIC_PRIO_IRQON }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_MASK }; +} + +static __always_inline +arm64_exc_hwstate_t arm64_exc_hwstate_of_context(arm64_exc_context_t context) { +switch (context) { +case PROCESS_CONTEXT : +return __arm64_exc_hwstate_of_process_context (); +case NOIRQ_CONTEXT : +return __arm64_exc_hwstate_of_noirq_context (); +case NONMI_CONTEXT : +return __arm64_exc_hwstate_of_nonmi_context (); +case ERROR_CONTEXT : +return __arm64_exc_hwstate_of_error_context (); +case CRITICAL_CONTEXT : +return __arm64_exc_hwstate_of_critical_context (); +default : +BUG (); + } +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_hwstate(struct pt_regs *regs) +{ +arm64_exc_hwstate_t state = {.daif=regs->pstate &DAIF_MASK }; + +if (system_uses_irq_prio_masking ()) + state.pmr = regs->pmr; + +return state; +} + +static __always_inline +void arm64_debug_exc_hwstate(arm64_exc_hwstate_t expected) +{ +arm64_exc_hwstate_t actual; + +if (!IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) +return ; + + actual.flags =arch_local_save_flags (); + +if (expected.flags == actual.flags) +return ; + +if (system_uses_irq_prio_masking ()) { +WARN_ONCE (1 ,"Unexpected DAIF+PMR: 0x%x + 0x%x (expected 0x%x + 0x%x)\n" , + actual.daif, actual.pmr, expected.daif, expected.pmr); + }else { +WARN_ONCE (1 ,"Unexpected DAIF: 0x%x (expected 0x%x)\n" , + actual.daif, expected.daif); + } +} + +static __always_inline +void arm64_debug_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t expected =arm64_exc_hwstate_of_context (context); + +arm64_debug_exc_hwstate (expected); +} + +static __always_inline +void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force) +{ +barrier (); + +if (system_uses_irq_prio_masking () && + hwstate.pmr ==GIC_PRIO_IRQOFF && + force) { +/* +* There has been concern that the write to daif +* might be reordered before this write to PMR. +* From the ARM ARM DDI 0487D.a, section D1.7.1 +* "Accessing PSTATE fields": +* Writes to the PSTATE fields have side-effects on +* various aspects of the PE operation. All of these +* side-effects are guaranteed: +* - Not to be visible to earlier instructions in +* the execution stream. +* - To be visible to later instructions in the +* execution stream +* +* Also, writes to PMR are self-synchronizing, so no +* interrupts with a lower priority than PMR is signaled +* to the PE after the write. +* +* So we don't need additional synchronization here. +*/ +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); + } + +barrier (); +write_sysreg (hwstate.daif, daif); +barrier (); + +if (system_uses_irq_prio_masking () && + hwstate.pmr ==GIC_PRIO_IRQON && + force) { +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); +pmr_sync (); + } + +barrier (); +} + +static __always_inline +void arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate) +{ +__arm64_update_exc_hwstate (hwstate, true); +} + +static __always_inline +void arm64_update_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t hwstate =arm64_exc_hwstate_of_context (context); + +arm64_update_exc_hwstate (hwstate); +} +#endif /* __ASM_INTERRUPTS_COMMON_FLAGS_H */
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 12/45] arm64: interrupts: Add common exception state helpers
Quoted 129 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code and the rest of the kernel require different interrupt masking APIs, but all need a common representation of the hardware exception state.
Introduce exception contexts that map DAIF and PMR state to the corresponding masking context. Provide helpers to translate between exception contexts and hardware state, verify the current state, and update the hardware state.
Updating PMR is unnecessary when a caller knows that its value has not changed. Allow such callers to avoid the update, while providing a force option for callers that cannot rely on the previous PMR state.
Entry specific and general purpose masking APIs will be built on these helpers in subsequent patches.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- .../include/asm/interrupts/common_flags.h | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/common_flags.harch/arm64/include/asm/interrupts/common_flags.h new file +98
-0@@ -0,0 +1,208 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_COMMON_FLAGS_H +#define __ASM_INTERRUPTS_COMMON_FLAGS_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/ptrace.h> +#include <asm/sysreg.h> +#include <asm/irqflags.h> + +#define DAIF_PROCCTX 0 +#define DAIF_PROCCTX_NOIRQ (PSR_I_BIT | PSR_F_BIT) +#define DAIF_ERRCTX (PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) +#define DAIF_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) + +/* +* Exception context mapping +* +* pseudo-NMI +* +* CRITICAL -> DAIF + IRQON (corresponds to the state on exception entry) +* ERROR -> AIF + IRQON +* NONMI -> IF + IRQON +* NOIRQ -> 0 + IRQOFF +* PROCESS -> 0 + IRQON +* +* Otherwise +* +* CRITICAL -> DAIF (corresponds to the state on exception entry) +* ERROR -> AIF +* NONMI -> IF +* NOIRQ -> IF +* PROCESS -> 0 +*/ +typedef enum arm64_exc_context { +PROCESS_CONTEXT , +NOIRQ_CONTEXT , +NONMI_CONTEXT , +ERROR_CONTEXT , +CRITICAL_CONTEXT , +}arm64_exc_context_t ; + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_process_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX , .pmr=GIC_PRIO_IRQON }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX }; +} + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_noirq_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX , .pmr=GIC_PRIO_IRQOFF }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ }; +} + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_nonmi_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ , .pmr=GIC_PRIO_IRQON }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ }; +} + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_error_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX , .pmr=GIC_PRIO_IRQON }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX }; +} + +static __always_inline +arm64_exc_hwstate_t __arm64_exc_hwstate_of_critical_context(void) +{ +if (system_uses_irq_prio_masking ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_MASK , .pmr=GIC_PRIO_IRQON }; + +return (arm64_exc_hwstate_t ){.daif=DAIF_MASK }; +} + +static __always_inline +arm64_exc_hwstate_t arm64_exc_hwstate_of_context(arm64_exc_context_t context) { +switch (context) { +case PROCESS_CONTEXT : +return __arm64_exc_hwstate_of_process_context (); +case NOIRQ_CONTEXT : +return __arm64_exc_hwstate_of_noirq_context ();
arch/arm64/include/asm/interrupts/common_flags.h line 108
Quoted 10 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +10
-0+case NONMI_CONTEXT : +return __arm64_exc_hwstate_of_nonmi_context (); +case ERROR_CONTEXT : +return __arm64_exc_hwstate_of_error_context (); +case CRITICAL_CONTEXT : +return __arm64_exc_hwstate_of_critical_context (); +default : +BUG (); + } +}
Hi Vladimir,
Using two tables as below to map different contexts to the hwstate status makes the code appear more concise and clear. What do you think?
--- /dev/null
+++ b/arch/arm64/include/asm/interrupts/common_flags.h +typedef enum arm64_exc_context {
+ PROCESS_CONTEXT, + NOIRQ_CONTEXT, + NONMI_CONTEXT, + ERROR_CONTEXT, + CRITICAL_CONTEXT, + NR_CONTEXTS,
+} arm64_exc_context_t;
+static const arm64_exc_hwstate_t daif_context_hwstate[] = {
+ [PROCESS_CONTEXT] = { .daif = DAIF_PROCCTX },
+ [NOIRQ_CONTEXT] = { .daif = DAIF_PROCCTX_NOIRQ },
+ [NONMI_CONTEXT] = { .daif = DAIF_PROCCTX_NOIRQ },
+ [ERROR_CONTEXT] = { .daif = DAIF_ERRCTX },
+ [CRITICAL_CONTEXT] = { .daif = DAIF_MASK },+};
+
+static const arm64_exc_hwstate_t pseudo_nmi_context_hwstate[] = {
+ [PROCESS_CONTEXT] = { .daif = DAIF_PROCCTX, .pmr =GIC_PRIO_IRQON },
+ [NOIRQ_CONTEXT] = { .daif = DAIF_PROCCTX, .pmr =GIC_PRIO_IRQOFF},
+ [NONMI_CONTEXT] = { .daif = DAIF_PROCCTX_NOIRQ, .pmr =GIC_PRIO_IRQON },
+ [ERROR_CONTEXT] = { .daif = DAIF_ERRCTX, .pmr =GIC_PRIO_IRQON },
+ [CRITICAL_CONTEXT] = { .daif = DAIF_MASK, .pmr =GIC_PRIO_IRQON },
+};
+
+static __always_inline
+arm64_exc_hwstate_t arm64_exc_hwstate_of_context(arm64_exc_context_t context)
+{
+ if (unlikely((unsigned int)context >= NR_CONTEXTS))
+ BUG();
+
+ if (system_uses_irq_prio_masking())
+ return pnmi_table[context];
+
+ return legacy_table[context];
+}
+
Quoted 36 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +36
-0+ +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_hwstate(struct pt_regs *regs) +{ +arm64_exc_hwstate_t state = {.daif=regs->pstate &DAIF_MASK }; + +if (system_uses_irq_prio_masking ()) + state.pmr = regs->pmr; + +return state; +} + +static __always_inline +void arm64_debug_exc_hwstate(arm64_exc_hwstate_t expected) +{ +arm64_exc_hwstate_t actual; + +if (!IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) +return ; + + actual.flags =arch_local_save_flags (); + +if (expected.flags == actual.flags) +return ; + +if (system_uses_irq_prio_masking ()) { +WARN_ONCE (1 ,"Unexpected DAIF+PMR: 0x%x + 0x%x (expected 0x%x + 0x%x)\n" , + actual.daif, actual.pmr, expected.daif, expected.pmr); + }else { +WARN_ONCE (1 ,"Unexpected DAIF: 0x%x (expected 0x%x)\n" , + actual.daif, expected.daif); + } +} + +static __always_inline +void arm64_debug_exc_context(arm64_exc_context_t context)
arch/arm64/include/asm/interrupts/common_flags.h line 154
Quoted 10 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +10
-0+{ +arm64_exc_hwstate_t expected =arm64_exc_hwstate_of_context (context); + +arm64_debug_exc_hwstate (expected); +} + +static __always_inline +void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force) +{ +barrier ();
I don't understand the purpose of the barrier at the beginning and end of the function. Is this fix also required for the original implementation of local_daif_restore()?
Quoted 17 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +17
-0+ +if (system_uses_irq_prio_masking () && + hwstate.pmr ==GIC_PRIO_IRQOFF && + force) { +/* +* There has been concern that the write to daif +* might be reordered before this write to PMR. +* From the ARM ARM DDI 0487D.a, section D1.7.1 +* "Accessing PSTATE fields": +* Writes to the PSTATE fields have side-effects on +* various aspects of the PE operation. All of these +* side-effects are guaranteed: +* - Not to be visible to earlier instructions in +* the execution stream. +* - To be visible to later instructions in the +* execution stream +*
arch/arm64/include/asm/interrupts/common_flags.h line 181
Quoted 10 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +10
-0+* Also, writes to PMR are self-synchronizing, so no +* interrupts with a lower priority than PMR is signaled +* to the PE after the write. +* +* So we don't need additional synchronization here. +*/ +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); + } + +barrier ();
This barrier seems redundant and inconsistent with the comment above.
arch/arm64/include/asm/interrupts/common_flags.h line 193
Quoted 12 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +12
-0+write_sysreg (hwstate.daif, daif); +barrier (); + +if (system_uses_irq_prio_masking () && + hwstate.pmr ==GIC_PRIO_IRQON && + force) { +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); +pmr_sync (); + } + +barrier (); +}
If we no use pesudo NMI, the barrier is not nessary,
static __always_inline
void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force)
{
if (system_uses_irq_prio_masking() && force) {
if (hwstate.pmr == GIC_PRIO_IRQOFF)
write_sysreg_s(hwstate.pmr, SYS_ICC_PMR_EL1);
write_sysreg(hwstate.daif, daif);
if (hwstate.pmr == GIC_PRIO_IRQON) {
barrier();
write_sysreg_s(hwstate.pmr, SYS_ICC_PMR_EL1);
pmr_sync();
}
} else {
write_sysreg(hwstate.daif, daif);
}
}otherwise, LGTM
Quoted 15 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +15
-0+ +static __always_inline +void arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate) +{ +__arm64_update_exc_hwstate (hwstate, true); +} + +static __always_inline +void arm64_update_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t hwstate =arm64_exc_hwstate_of_context (context); + +arm64_update_exc_hwstate (hwstate); +} +#endif /* __ASM_INTERRUPTS_COMMON_FLAGS_H */
Re: [RFC PATCH v2 12/45] arm64: interrupts: Add common exception state helpers
Hi Jinjie,
Quoted 4 lines · Jinjie Ruan
Hi Vladimir,
Using two tables as below to map different contexts to the hwstate status makes the code appear more concise and clear. What do you think?
I think I already tried something like that and IIRC it hurts code generation... I'll have another look before posting next version.
Cheers
Vladimir
Re: [RFC PATCH v2 12/45] arm64: interrupts: Add common exception state helpers
Quoted 10 lines · Vladimir Murzin
Hi Jinjie,
Quoted 4 lines · Jinjie Ruan
Hi Vladimir,
Using two tables as below to map different contexts to the hwstate status makes the code appear more concise and clear. What do you think?
I think I already tried something like that and IIRC it hurts code generation... I'll have another look before posting next version.
Thank you for very much.
Quoted 4 lines
Cheers
Vladimir
Re: [RFC PATCH v2 12/45] arm64: interrupts: Add common exception state helpers
Quoted 4 lines · Jinjie Ruan
Quoted 2 lines · Vladimir Murzin
Hi Jinjie,
Quoted 9 lines
Quoted 7 lines · Jinjie Ruan
Quoted 4 lines
Hi Vladimir,
Using two tables as below to map different contexts to the hwstate status makes the code appear more concise and clear. What do you think?
I think I already tried something like that and IIRC it hurts code generation... I'll have another look before posting next version.
Thank you for very much.
Interestingly, with CONFIG_ARM64_PSEUDO_NMI=n both versions seem to have the same codegen, which is not different from the original code. However, once CONFIG_ARM64_PSEUDO_NMI=y, codegen starts suffering from static branches, with the table approach being slightly worse. Since whether we use {p}NMI or not is a runtime decision, the compiler seems unable to fully optimize things out.
Perhaps this is the price we have to pay for flexibility, but if someone has ideas on how to tune the code generation, I'm all ears.
Cheers
Vladimir
Quoted 3 lines
Quoted 3 lines
Cheers
Vladimir
Re: [RFC PATCH v2 12/45] arm64: interrupts: Add common exception state helpers
In reply to Jinjie RuanQuoted 1 line · Jinjie Ruan
[snip]
Sorry, Jinjie, I missed other comments...
Quoted 9 lines
arch/arm64/include/asm/interrupts/common_flags.h line 154
Quoted 5 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +5
-0+ +static __always_inline +void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force) +{ +barrier ();I don't understand the purpose of the barrier at the beginning and end of the function. Is this fix also required for the original implementation of
local_daif_restore()?
The entry and exit barrier() calls prevent the compiler from moving surrounding memory accesses across the exception state switch, so caller memory accesses remain on the intended side of the exception state switch. This is modeled after local_irq_{en,dis}able() and is indeed different from local_daif_{restore,mask}().
__arm64_update_exc_hwstate() is a generic helper which can be used to mask and unmask exceptions, while local_daif_{restore,mask}() seem to be unidirectional. local_daif_mask() (and local_daif_save()) mask exceptions and have a compiler barrier in the form of a memory clobber, while local_daif_restore() and local_daif_inherit(), IIUC, are used to unmask exceptions, and I could not spot a compiler barrier in any form there.
I _assume_ that having a relaxed local_daif_{restore,inherit}() is OK, yet I have not validated all use cases.
Quoted 20 lines
Quoted 20 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +20
-0+ +if (system_uses_irq_prio_masking () && + hwstate.pmr ==GIC_PRIO_IRQOFF && + force) { +/* +* There has been concern that the write to daif +* might be reordered before this write to PMR. +* From the ARM ARM DDI 0487D.a, section D1.7.1 +* "Accessing PSTATE fields": +* Writes to the PSTATE fields have side-effects on +* various aspects of the PE operation. All of these +* side-effects are guaranteed: +* - Not to be visible to earlier instructions in +* the execution stream. +* - To be visible to later instructions in the +* execution stream +* +* Also, writes to PMR are self-synchronizing, so no +* interrupts with a lower priority than PMR is signaled +* to the PE after the write.
Quoted 9 lines
arch/arm64/include/asm/interrupts/common_flags.h line 181
Quoted 7 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +7
-0+* +* So we don't need additional synchronization here. +*/ +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); + } + +barrier ();This barrier seems redundant and inconsistent with the comment above.
This is a compiler barrier and has nothing to do with the comment above. However, it is not needed here and bellow, since the sysreg accessors use asm volatile, so the compiler preserves the relative order of the PMR and DAIF accesses.
Cheers
Vladimir
Quoted 51 lines
arch/arm64/include/asm/interrupts/common_flags.h line 193
Quoted 12 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +12
-0+write_sysreg (hwstate.daif, daif); +barrier (); + +if (system_uses_irq_prio_masking () && + hwstate.pmr ==GIC_PRIO_IRQON && + force) { +write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); +pmr_sync (); + } + +barrier (); +}If we no use pesudo NMI, the barrier is not nessary,
static
__always_inline
void__arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force){ if (system_uses_irq_prio_masking() && force) { if (hwstate.pmr == GIC_PRIO_IRQOFF) write_sysreg_s(hwstate.pmr, SYS_ICC_PMR_EL1); write_sysreg(hwstate.daif, daif); if (hwstate.pmr == GIC_PRIO_IRQON) { barrier(); write_sysreg_s(hwstate.pmr, SYS_ICC_PMR_EL1); pmr_sync(); } } else { write_sysreg(hwstate.daif, daif); } }otherwise, LGTM
Reviewed-by Jinjie Ruan <[email protected]>Quoted 15 lines
arch/arm64/include/asm/interrupts/common_flags.h new file +15
-0+ +static __always_inline +void arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate) +{ +__arm64_update_exc_hwstate (hwstate, true); +} + +static __always_inline +void arm64_update_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t hwstate =arm64_exc_hwstate_of_context (context); + +arm64_update_exc_hwstate (hwstate); +} +#endif /* __ASM_INTERRUPTS_COMMON_FLAGS_H */
RFC PATCH v2 13/45 arm64: process: Use helper to check exception state
In reply to Vladimir Murzindebug_switch_state() explicitly checks the hardware exception state for both the pseudo-NMI and non-NMI paths. The expected state matches the NOIRQ_CONTEXT exception context introduced alongside the exception masking helpers and state checkers.
Use the helper instead of open-coding these checks.
--- arch/arm64/kernel/process.c | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-)
arch/arm64/kernel/process.c +2 -19
@@ -51,6 +51,7 @@#include <asm/exec.h> #include <asm/fpsimd.h> #include <asm/gcs.h> +#include <asm/interrupts/common_flags.h> #include <asm/mmu_context.h> #include <asm/mpam.h> #include <asm/mte.h>
@@ -703,25 +704,7 @@ void update_sctlr_el1(u64 sctlr)static inline void debug_switch_state (void ) { -if (system_uses_irq_prio_masking ()) { -unsigned long daif_expected =0 ; -unsigned long daif_actual =read_sysreg (daif); -unsigned long pmr_expected =GIC_PRIO_IRQOFF ; -unsigned long pmr_actual =read_sysreg_s (SYS_ICC_PMR_EL1 ); - -WARN_ONCE (daif_actual != daif_expected || - pmr_actual != pmr_expected, -"Unexpected DAIF + PMR: 0x%lx + 0x%lx (expected 0x%lx + 0x%lx)\n" , - daif_actual, pmr_actual, - daif_expected, pmr_expected); - }else { -unsigned long daif_expected =DAIF_PROCCTX_NOIRQ ; -unsigned long daif_actual =read_sysreg (daif); - -WARN_ONCE (daif_actual != daif_expected, -"Unexpected DAIF value: 0x%lx (expected 0x%lx)\n" , - daif_actual, daif_expected); - } +arm64_debug_exc_context (NOIRQ_CONTEXT ); }/*
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 13/45] arm64: process: Use helper to check exception state
Quoted 39 lines · Vladimir Murzin
debug_switch_state()explicitly checks the hardware exception state for both the pseudo-NMI and non-NMI paths. The expected state matches theNOIRQ_CONTEXTexception context introduced alongside the exception masking helpers and state checkers.Use the helper instead of open-coding these checks.
Signed-off-by Vladimir Murzin <[email protected]>Reviewed-by Jinjie Ruan <[email protected]>--- arch/arm64/kernel/process.c | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-)arch/arm64/kernel/process.c +1
-10@@ -51,6 +51,7 @@#include <asm/exec.h> #include <asm/fpsimd.h> #include <asm/gcs.h> +#include <asm/interrupts/common_flags.h> #include <asm/mmu_context.h> #include <asm/mpam.h> #include <asm/mte.h> @@ -703,25 +704,7 @@ void update_sctlr_el1(u64 sctlr)static inline void debug_switch_state (void ) { -if (system_uses_irq_prio_masking ()) { -unsigned long daif_expected =0 ; -unsigned long daif_actual =read_sysreg (daif); -unsigned long pmr_expected =GIC_PRIO_IRQOFF ; -unsigned long pmr_actual =read_sysreg_s (SYS_ICC_PMR_EL1 ); - - WARN_ONCE(daif_actual != daif_expected || - pmr_actual != pmr_expected, - "Unexpected DAIF + PMR: 0x%lx + 0x%lx (expected 0x%lx + 0x%lx)\n", - daif_actual, pmr_actual,
arch/arm64/kernel/process.c void update_sctlr_el1(u64 sctlr) · line 707
Quoted 10 lines
arch/arm64/kernel/process.c +1
-9- daif_expected, pmr_expected); - }else { -unsigned long daif_expected =DAIF_PROCCTX_NOIRQ ; -unsigned long daif_actual =read_sysreg (daif); - -WARN_ONCE (daif_actual != daif_expected, -"Unexpected DAIF value: 0x%lx (expected 0x%lx)\n" , - daif_actual, daif_expected); - } +arm64_debug_exc_context (NOIRQ_CONTEXT );
Hi Vladimir,
After taking another look, I have a small suggestion:
if you postpone the introduction of arm64_debug_exc_context() and arm64_debug_exc_hwstate() from the previous patch to this one, the code review will be easier.
Best regards,
Jinjie
Quoted 3 lines
arch/arm64/kernel/process.c +0
-0}/*
Re: [RFC PATCH v2 13/45] arm64: process: Use helper to check exception state
Quoted 43 lines · Jinjie Ruan
Quoted 41 lines · Vladimir Murzin
debug_switch_state()explicitly checks the hardware exception state for both the pseudo-NMI and non-NMI paths. The expected state matches theNOIRQ_CONTEXTexception context introduced alongside the exception masking helpers and state checkers.Use the helper instead of open-coding these checks.
Signed-off-by Vladimir Murzin <[email protected]>Reviewed-by Jinjie Ruan <[email protected]>--- arch/arm64/kernel/process.c | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-)arch/arm64/kernel/process.c +1
-12@@ -51,6 +51,7 @@#include <asm/exec.h> #include <asm/fpsimd.h> #include <asm/gcs.h> +#include <asm/interrupts/common_flags.h> #include <asm/mmu_context.h> #include <asm/mpam.h> #include <asm/mte.h> @@ -703,25 +704,7 @@ void update_sctlr_el1(u64 sctlr)static inline void debug_switch_state (void ) { -if (system_uses_irq_prio_masking ()) { -unsigned long daif_expected =0 ; -unsigned long daif_actual =read_sysreg (daif); -unsigned long pmr_expected =GIC_PRIO_IRQOFF ; -unsigned long pmr_actual =read_sysreg_s (SYS_ICC_PMR_EL1 ); - -WARN_ONCE (daif_actual != daif_expected || - pmr_actual != pmr_expected, -"Unexpected DAIF + PMR: 0x%lx + 0x%lx (expected 0x%lx + 0x%lx)\n" , - daif_actual, pmr_actual, - daif_expected, pmr_expected); - }else {
Quoted 8 lines
arch/arm64/kernel/process.c void update_sctlr_el1(u64 sctlr) · line 707
Quoted 8 lines
arch/arm64/kernel/process.c +1
-7-unsigned long daif_expected =DAIF_PROCCTX_NOIRQ ; -unsigned long daif_actual =read_sysreg (daif); - -WARN_ONCE (daif_actual != daif_expected, -"Unexpected DAIF value: 0x%lx (expected 0x%lx)\n" , - daif_actual, daif_expected); - } +arm64_debug_exc_context (NOIRQ_CONTEXT );
Hi Jinjie,
Quoted 8 lines
Hi Vladimir,
After taking another look, I have a small suggestion:
if you postpone the introduction of
arm64_debug_exc_context()andarm64_debug_exc_hwstate()from the previous patch to this one, the code review will be easier.
Yes, I can do that in the next version. I assume I can preserve your Reviewed-by tags since it is just a mechanical change; otherwise, please shout.
Cheers
Vladimir
Quoted 7 lines
Best regards,
JinjieQuoted 3 lines
arch/arm64/kernel/process.c +0
-0}/*
RFC PATCH v2 14/45 arm64: entry: Introduce entry specific exception masking helpers
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.h
arch/arm64/include/asm/interrupts/entry.h new file +113 -0
@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next) +{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next; +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +} + +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 49 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +13
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + +
[...]
Quoted 38 lines
arch/arm64/include/asm/interrupts/entry.h new file +38
-0+static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX );
arch/arm64/include/asm/interrupts/entry.h line 95
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+ +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next);
Hi Vladimir,
These two functions appear to have similar functionality, with only the debug code being different. Can they be merged?
Quoted 6 lines
arch/arm64/include/asm/interrupts/entry.h new file +6
-0+} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{
arch/arm64/include/asm/interrupts/entry.h line 111
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +}
otherwise, LGTM
Quoted 2 lines
arch/arm64/include/asm/interrupts/entry.h new file +2
-0+ +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 92 lines · Jinjie Ruan
Quoted 49 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +13
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + +[...]
Quoted 40 lines
arch/arm64/include/asm/interrupts/entry.h new file +40
-0+static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + + WARN_ON_ONCE(context < NONMI_CONTEXT &&
Quoted 8 lines
arch/arm64/include/asm/interrupts/entry.h line 95
Quoted 8 lines
arch/arm64/include/asm/interrupts/entry.h new file +8
-0+ pnmi && prev.daif == DAIF_PROCCTX_NOIRQ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next);
Hi Jinjie,
Quoted 5 lines
Hi Vladimir,
These two functions appear to have similar functionality, with only the debug code being different. Can they be merged?
Functionality is indeed similar, but the intent is different. It gently encourages the user to think about the exception context they are currently in, which should make it easier to reason about exception transitions. That is not always possible with a merged "take me there" interface.
The debug code then ensures that the requested exception transition is in the correct direction.
Cheers
Vladimir
Quoted 22 lines
Quoted 6 lines
arch/arm64/include/asm/interrupts/entry.h new file +6
-0+} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{arch/arm64/include/asm/interrupts/entry.h line 111
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +}otherwise, LGTM
Reviewed-by Jinjie Ruan <[email protected]>Quoted 2 lines
arch/arm64/include/asm/interrupts/entry.h new file +2
-0+ +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 110 lines · Vladimir Murzin
Quoted 92 lines · Jinjie Ruan
Quoted 49 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +13
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + +[...]
Quoted 40 lines
arch/arm64/include/asm/interrupts/entry.h new file +40
-0+static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + + WARN_ON_ONCE(context < NONMI_CONTEXT &&Quoted 8 lines
arch/arm64/include/asm/interrupts/entry.h line 95
Quoted 8 lines
arch/arm64/include/asm/interrupts/entry.h new file +8
-0+ pnmi && prev.daif == DAIF_PROCCTX_NOIRQ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next);Hi Jinjie,
Quoted 5 lines
Hi Vladimir,
These two functions appear to have similar functionality, with only the debug code being different. Can they be merged?
Quoted 5 lines
Functionality is indeed similar, but the intent is different. It gently encourages the user to think about the exception context they are currently in, which should make it easier to reason about exception transitions. That is not always possible with a merged "take me there" interface.
I see the intent. The function names clearly indicate the relaxation of exception masking on entry and the tightening on exit, which makes reasoning about the transitions straightforward.
Quoted 3 lines
The debug code then ensures that the requested exception transition is in the correct direction.
Functions that distinguish two directions are more useful in debugging.
Quoted 28 lines
Cheers
VladimirQuoted 22 lines
Quoted 6 lines
arch/arm64/include/asm/interrupts/entry.h new file +6
-0+} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{arch/arm64/include/asm/interrupts/entry.h line 111
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +}otherwise, LGTM
Reviewed-by Jinjie Ruan <[email protected]>Quoted 2 lines
arch/arm64/include/asm/interrupts/entry.h new file +2
-0+ +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
In reply to Vladimir MurzinQuoted 86 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +50
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next) +{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next; +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{
arch/arm64/include/asm/interrupts/entry.h line 60
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );
For daif, we can directly compare next and prev because, as the context drops, the value of daif decreases.
This is also the opposite of the meanings of "drop" and "lift" in the function names, which is easy to understand.
WARN_ON_ONCE(prev.daif < next.daif);
Quoted 15 lines
arch/arm64/include/asm/interrupts/entry.h new file +15
-0+ +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{
arch/arm64/include/asm/interrupts/entry.h line 85
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX );
WARN_ON_ONCE(prev.daif > next.daif);
Quoted 28 lines
arch/arm64/include/asm/interrupts/entry.h new file +28
-0+ +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif == DAIF_PROCCTX_NOIRQ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +} + +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 101 lines · Jinjie Ruan
Quoted 86 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +50
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next) +{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next; +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{arch/arm64/include/asm/interrupts/entry.h line 60
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );For daif, we can directly compare next and prev because, as the context drops, the value of daif decreases.
Quoted 4 lines
This is also the opposite of the meanings of "drop" and "lift" in the function names, which is easy to understand.
WARN_ON_ONCE(prev.daif < next.daif);
That's a very good point! I was too focused on checking the hardware state against the logical exception context, so I missed that we could perform the checks using the hardware state alone.
What do you think about:
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) {
WARN_ON_ONCE(prev.daif < next.daif);
if (prev.daif == next.daif) {
/*
* GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so larger PMR value is weaker
*/
WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr > next.pmr);
WARN_ON_ONCE(system_uses_nmi() && prev.allint < next.allint);
}
}Quoted 19 lines
Quoted 19 lines
arch/arm64/include/asm/interrupts/entry.h new file +18
-0+ +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {
Quoted 9 lines
arch/arm64/include/asm/interrupts/entry.h line 85
Quoted 7 lines
arch/arm64/include/asm/interrupts/entry.h new file +7
-0+bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX );
WARN_ON_ONCE(prev.daif > next.daif);
... and
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) {
WARN_ON_ONCE(prev.daif > next.daif);
if (prev.daif == next.daif) {
/*
* GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so smaller PMR value is stronger
*/
WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr < next.pmr);
WARN_ON_ONCE(system_uses_nmi() && prev.allint > next.allint);
}
}Cheers
Vladimir
Quoted 29 lines
Quoted 28 lines
arch/arm64/include/asm/interrupts/entry.h new file +28
-0+ +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif == DAIF_PROCCTX_NOIRQ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +} + +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 113 lines · Vladimir Murzin
Quoted 101 lines · Jinjie Ruan
Quoted 86 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +50
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next) +{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next; +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{arch/arm64/include/asm/interrupts/entry.h line 60
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );For daif, we can directly compare next and prev because, as the context drops, the value of daif decreases.
Quoted 4 lines
This is also the opposite of the meanings of "drop" and "lift" in the function names, which is easy to understand.
WARN_ON_ONCE(prev.daif < next.daif);That's a very good point! I was too focused on checking the hardware state against the logical exception context, so I missed that we could perform the checks using the hardware state alone.
What do you think about:
Quoted 9 lines
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) {
WARN_ON_ONCE(prev.daif < next.daif);
if (prev.daif == next.daif) {
/*
* GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so larger PMR value is weaker
*/
WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr > next.pmr);
WARN_ON_ONCE(system_uses_nmi() && prev.allint < next.allint);Hi Vladimir,
I have come up with what I think is a more comprehensible debugging method.
Could we define a function that is the reverse of arm64_exc_hwstate_of_context(), such that we can derive the previous context from the previous hardware state? That would allow us to directly compare below enum types, making the logic significantly easier to follow. Any thoughts?
This way, we don't even need to distinguish between pseudo-NMI and FEAT_NMI.
+typedef enum arm64_exc_context {
+ PROCESS_CONTEXT,
+ NOIRQ_CONTEXT,
+ NONMI_CONTEXT,
+ ERROR_CONTEXT,
+ CRITICAL_CONTEXT,
+} arm64_exc_context_t;
Best regards,
Jinjie
Quoted 81 lines
} }Quoted 19 lines
Quoted 19 lines
arch/arm64/include/asm/interrupts/entry.h new file +18
-0+ +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {Quoted 9 lines
arch/arm64/include/asm/interrupts/entry.h line 85
Quoted 7 lines
arch/arm64/include/asm/interrupts/entry.h new file +7
-0+bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX );
WARN_ON_ONCE(prev.daif > next.daif);... and
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) { WARN_ON_ONCE(prev.daif > next.daif); if (prev.daif == next.daif) { /* * GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so smaller PMR value is stronger */ WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr < next.pmr); WARN_ON_ONCE(system_uses_nmi() && prev.allint > next.allint); } }Cheers
VladimirQuoted 29 lines
Quoted 28 lines
arch/arm64/include/asm/interrupts/entry.h new file +28
-0+ +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif == DAIF_PROCCTX_NOIRQ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +} + +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 116 lines · Jinjie Ruan
Quoted 114 lines · Vladimir Murzin
Quoted 100 lines · Jinjie Ruan
Quoted 86 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +50
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next) +{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next; +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{arch/arm64/include/asm/interrupts/entry.h line 60
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );For daif, we can directly compare next and prev because, as the context drops, the value of daif decreases.
Quoted 4 lines
This is also the opposite of the meanings of "drop" and "lift" in the function names, which is easy to understand.
WARN_ON_ONCE(prev.daif < next.daif);That's a very good point! I was too focused on checking the hardware state against the logical exception context, so I missed that we could perform the checks using the hardware state alone.
What do you think about:
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) { WARN_ON_ONCE(prev.daif < next.daif);
Quoted 6 lines
Quoted 6 lines
if (prev.daif == next.daif) { /* * GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so larger PMR value is weaker */ WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr > next.pmr); WARN_ON_ONCE(system_uses_nmi() && prev.allint < next.allint);
Hi Jinjie,
Quoted 10 lines
Hi Vladimir,
I have come up with what I think is a more comprehensible debugging method.
Could we define a function that is the reverse of
arm64_exc_hwstate_of_context(), such that we can derive the previous context from the previous hardware state? That would allow us to directly compare below enum types, making the logic significantly easier to follow. Any thoughts?
I used to have such reverse mapping in my internal version and feedback I got off-list is to avoid that at all cost. Since I needed it only for debug I open-coded checks in relevant functions (and that the reason I was focused on logical exception context rather than using hardware state alone).
Cheers
Vladimir
Quoted 90 lines
This way, we don't even need to distinguish between pseudo-NMI and
FEAT_NMI.(quoted diff) +7
-0+typedef enum arm64_exc_context { + PROCESS_CONTEXT, + NOIRQ_CONTEXT, + NONMI_CONTEXT, + ERROR_CONTEXT, + CRITICAL_CONTEXT, +} arm64_exc_context_t;Best regards,
JinjieQuoted 77 lines
} }Quoted 18 lines
Quoted 18 lines
arch/arm64/include/asm/interrupts/entry.h new file +18
-0+ +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {Quoted 9 lines
arch/arm64/include/asm/interrupts/entry.h line 85
Quoted 7 lines
arch/arm64/include/asm/interrupts/entry.h new file +7
-0+bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX );
WARN_ON_ONCE(prev.daif > next.daif);... and
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) { WARN_ON_ONCE(prev.daif > next.daif); if (prev.daif == next.daif) { /* * GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so smaller PMR value is stronger */ WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr < next.pmr); WARN_ON_ONCE(system_uses_nmi() && prev.allint > next.allint); } }Cheers
VladimirQuoted 28 lines
Quoted 28 lines
arch/arm64/include/asm/interrupts/entry.h new file +28
-0+ +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif == DAIF_PROCCTX_NOIRQ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +} + +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 137 lines · Vladimir Murzin
Quoted 116 lines · Jinjie Ruan
Quoted 114 lines · Vladimir Murzin
Quoted 100 lines · Jinjie Ruan
Quoted 86 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +50
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next) +{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next; +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{arch/arm64/include/asm/interrupts/entry.h line 60
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );For daif, we can directly compare next and prev because, as the context drops, the value of daif decreases.
Quoted 4 lines
This is also the opposite of the meanings of "drop" and "lift" in the function names, which is easy to understand.
WARN_ON_ONCE(prev.daif < next.daif);That's a very good point! I was too focused on checking the hardware state against the logical exception context, so I missed that we could perform the checks using the hardware state alone.
What do you think about:
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) { WARN_ON_ONCE(prev.daif < next.daif);Quoted 6 lines
Quoted 6 lines
if (prev.daif == next.daif) { /* * GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so larger PMR value is weaker */ WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr > next.pmr); WARN_ON_ONCE(system_uses_nmi() && prev.allint < next.allint);Hi Jinjie,
Quoted 10 lines
Hi Vladimir,
I have come up with what I think is a more comprehensible debugging method.
Could we define a function that is the reverse of
arm64_exc_hwstate_of_context(), such that we can derive the previous context from the previous hardware state? That would allow us to directly compare below enum types, making the logic significantly easier to follow. Any thoughts?
Quoted 4 lines
I used to have such reverse mapping in my internal version and feedback I got off-list is to avoid that at all cost. Since I needed it only for debug I open-coded checks in relevant functions (and that the reason I was focused on logical exception context rather than using hardware state alone).
I believe it would be cleaner to define and use this only when CONFIG_DEBUG_IRQFLAGS is enabled; this would not introduce any overhead in production kernels, correct?
Quoted 96 lines
Cheers
VladimirQuoted 90 lines
This way, we don't even need to distinguish between pseudo-NMI and
FEAT_NMI.(quoted diff) +7
-0+typedef enum arm64_exc_context { + PROCESS_CONTEXT, + NOIRQ_CONTEXT, + NONMI_CONTEXT, + ERROR_CONTEXT, + CRITICAL_CONTEXT, +} arm64_exc_context_t;Best regards,
JinjieQuoted 77 lines
} }Quoted 18 lines
Quoted 18 lines
arch/arm64/include/asm/interrupts/entry.h new file +18
-0+ +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {Quoted 9 lines
arch/arm64/include/asm/interrupts/entry.h line 85
Quoted 7 lines
arch/arm64/include/asm/interrupts/entry.h new file +7
-0+bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX );
WARN_ON_ONCE(prev.daif > next.daif);... and
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) { WARN_ON_ONCE(prev.daif > next.daif); if (prev.daif == next.daif) { /* * GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so smaller PMR value is stronger */ WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr < next.pmr); WARN_ON_ONCE(system_uses_nmi() && prev.allint > next.allint); } }Cheers
VladimirQuoted 28 lines
Quoted 28 lines
arch/arm64/include/asm/interrupts/entry.h new file +28
-0+ +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif == DAIF_PROCCTX_NOIRQ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next); +} + + +static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +} + +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 136 lines · Jinjie Ruan
Quoted 134 lines · Vladimir Murzin
Quoted 115 lines · Jinjie Ruan
Quoted 114 lines · Vladimir Murzin
Quoted 100 lines · Jinjie Ruan
Quoted 86 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +50
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next) +{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next; +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{arch/arm64/include/asm/interrupts/entry.h line 60
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );For daif, we can directly compare next and prev because, as the context drops, the value of daif decreases.
Quoted 4 lines
This is also the opposite of the meanings of "drop" and "lift" in the function names, which is easy to understand.
WARN_ON_ONCE(prev.daif < next.daif);That's a very good point! I was too focused on checking the hardware state against the logical exception context, so I missed that we could perform the checks using the hardware state alone.
What do you think about:
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) { WARN_ON_ONCE(prev.daif < next.daif);Quoted 6 lines
Quoted 6 lines
if (prev.daif == next.daif) { /* * GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so larger PMR value is weaker */ WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr > next.pmr); WARN_ON_ONCE(system_uses_nmi() && prev.allint < next.allint);Hi Jinjie,
Quoted 10 lines
Hi Vladimir,
I have come up with what I think is a more comprehensible debugging method.
Could we define a function that is the reverse of
arm64_exc_hwstate_of_context(), such that we can derive the previous context from the previous hardware state? That would allow us to directly compare below enum types, making the logic significantly easier to follow. Any thoughts?
Quoted 8 lines
Quoted 4 lines
I used to have such reverse mapping in my internal version and feedback I got off-list is to avoid that at all cost. Since I needed it only for debug I open-coded checks in relevant functions (and that the reason I was focused on logical exception context rather than using hardware state alone).
I believe it would be cleaner to define and use this only when
CONFIG_DEBUG_IRQFLAGSis enabled; this would not introduce any overhead in production kernels, correct?
It is not about overhead - there is no use of such helpers outside of debug code anyway. Reverse mapping, in general, comes with a few complications:
- For DAIF-only, NMI and IRQ contexts are represented by the same hardware state, so special handling would be required. Using hardware state for the DAIF-only case and logical context for NMI cases would be unlikely to be acceptable, since it would not be uniform.
- We might encounter cases where the hardware state temporarily cannot be directly mapped to a logical context. I do not know if we have such cases right now, but that would need to be somehow handled by helpers performing reverse mapping.
- Lastly, such a facility has already been deemed undesirable once, so I would rather not reintroduce it.
IMO, performing checks using the hardware state alone is a good compromise between clarity and functionality.
Thanks
Vladimir
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 146 lines · Vladimir Murzin
Quoted 136 lines · Jinjie Ruan
Quoted 134 lines · Vladimir Murzin
Quoted 115 lines · Jinjie Ruan
Quoted 114 lines · Vladimir Murzin
Quoted 100 lines · Jinjie Ruan
Quoted 86 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +50
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next) +{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next; +} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next); +} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{arch/arm64/include/asm/interrupts/entry.h line 60
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );For daif, we can directly compare next and prev because, as the context drops, the value of daif decreases.
Quoted 4 lines
This is also the opposite of the meanings of "drop" and "lift" in the function names, which is easy to understand.
WARN_ON_ONCE(prev.daif < next.daif);That's a very good point! I was too focused on checking the hardware state against the logical exception context, so I missed that we could perform the checks using the hardware state alone.
What do you think about:
if (IS_ENABLED(CONFIG_DEBUG_IRQFLAGS)) { WARN_ON_ONCE(prev.daif < next.daif);Quoted 6 lines
Quoted 6 lines
if (prev.daif == next.daif) { /* * GIC_PRIO_IRQON is larger that GIC_PRIO_IRQOFF so larger PMR value is weaker */ WARN_ON_ONCE(system_uses_irq_prio_masking() && prev.pmr > next.pmr); WARN_ON_ONCE(system_uses_nmi() && prev.allint < next.allint);Hi Jinjie,
Quoted 10 lines
Hi Vladimir,
I have come up with what I think is a more comprehensible debugging method.
Could we define a function that is the reverse of
arm64_exc_hwstate_of_context(), such that we can derive the previous context from the previous hardware state? That would allow us to directly compare below enum types, making the logic significantly easier to follow. Any thoughts?Quoted 8 lines
Quoted 4 lines
I used to have such reverse mapping in my internal version and feedback I got off-list is to avoid that at all cost. Since I needed it only for debug I open-coded checks in relevant functions (and that the reason I was focused on logical exception context rather than using hardware state alone).
I believe it would be cleaner to define and use this only when
CONFIG_DEBUG_IRQFLAGSis enabled; this would not introduce any overhead in production kernels, correct?
Quoted 6 lines
It is not about overhead - there is no use of such helpers outside of debug code anyway. Reverse mapping, in general, comes with a few complications:
- For DAIF-only, NMI and IRQ contexts are represented by the same hardware state, so special handling would be required. Using
Yes, in the case where only DAIF is present, the hwstate of NONMI and NOIRQ is the same (DAIF.IF is set), and reverse mapping cannot distinguish between these two scenarios when looking for the context.
Quoted 3 lines
hardware state for the DAIF-only case and logical context for NMI cases would be unlikely to be acceptable, since it would not be uniform.
Perhaps that's the case
Quoted 3 lines
- We might encounter cases where the hardware state temporarily cannot be directly mapped to a logical context. I do not know
In this case, the hwstate may be invalid.
Quoted 8 lines
if we have such cases right now, but that would need to be somehow handled by helpers performing reverse mapping.
- Lastly, such a facility has already been deemed undesirable once, so I would rather not reintroduce it.
IMO, performing checks using the hardware state alone is a good compromise between clarity and functionality.
I see.
Quoted 5 lines
Thanks
Vladimir
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
In reply to Vladimir MurzinQuoted 42 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +6
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_H
arch/arm64/include/asm/interrupts/entry.h line 16
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+ +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next)
may be const arm64_exc_hwstate_t next
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled)
arch/arm64/include/asm/interrupts/entry.h line 36
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next;
We do not update next; it should be "void".
arch/arm64/include/asm/interrupts/entry.h line 45
Quoted 9 lines
arch/arm64/include/asm/interrupts/entry.h new file +9
-0+} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next);
__arm64_switch_exc_hwstate_to(prev, next);
return next;
Quoted 15 lines
arch/arm64/include/asm/interrupts/entry.h new file +15
-0+} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );
arch/arm64/include/asm/interrupts/entry.h line 70
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+ +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next);
__arm64_switch_exc_hwstate_to(prev, next);
return next;
Quoted 15 lines
arch/arm64/include/asm/interrupts/entry.h new file +15
-0+} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX );
arch/arm64/include/asm/interrupts/entry.h line 95
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+ +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next);
__arm64_switch_exc_hwstate_to(prev, next);
return next;
arch/arm64/include/asm/interrupts/entry.h line 44
Quoted 3 lines
arch/arm64/include/asm/interrupts/entry.h new file +3
-0+} + +
Extra blank lines.
Quoted 15 lines
arch/arm64/include/asm/interrupts/entry.h new file +15
-0+static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +} + +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 71 lines · Jinjie Ruan
Quoted 42 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +6
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_Harch/arm64/include/asm/interrupts/entry.h line 16
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+ +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next)may be const
arm64_exc_hwstate_tnextQuoted 14 lines
arch/arm64/include/asm/interrupts/entry.h new file +14
-0+{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; +
Quoted 8 lines
arch/arm64/include/asm/interrupts/entry.h line 36
Quoted 6 lines
arch/arm64/include/asm/interrupts/entry.h new file +6
-0+__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next;We do not update next; it should be "void".
Hi Vladimir,
What do you think?
Quoted 94 lines
arch/arm64/include/asm/interrupts/entry.h line 45
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +9
-0+} + +static __always_inline +arm64_exc_hwstate_t arm64_inherit_exc_context(struct pt_regs *regs) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); +arm64_exc_hwstate_t next =arm64_inherit_exc_hwstate (regs); + +return __arm64_switch_exc_hwstate_to (prev, next);
__arm64_switch_exc_hwstate_to(prev, next);
return next;Quoted 15 lines
arch/arm64/include/asm/interrupts/entry.h new file +15
-0+} + +static __always_inline +arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context >ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX ); + +WARN_ON_ONCE (context >NONMI_CONTEXT && + prev.daif ==DAIF_PROCCTX_NOIRQ );arch/arm64/include/asm/interrupts/entry.h line 70
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+ +WARN_ON_ONCE (context >NOIRQ_CONTEXT && + pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + +WARN_ON_ONCE (context >PROCESS_CONTEXT && + ((pnmi && prev.daif ==DAIF_PROCCTX && prev.pmr ==GIC_PRIO_IRQON ) || + (!pnmi && prev.daif ==DAIF_PROCCTX ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next);
__arm64_switch_exc_hwstate_to(prev, next);
return next;Quoted 15 lines
arch/arm64/include/asm/interrupts/entry.h new file +15
-0+} + +static __always_inline +arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_context_t context) +{ +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (context); + +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) { +bool pnmi =system_uses_irq_prio_masking (); + +WARN_ON_ONCE (context <CRITICAL_CONTEXT && + prev.daif ==DAIF_MASK ); + +WARN_ON_ONCE (context <ERROR_CONTEXT && + prev.daif ==DAIF_ERRCTX );arch/arm64/include/asm/interrupts/entry.h line 95
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+ +WARN_ON_ONCE (context <NONMI_CONTEXT && + pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + +WARN_ON_ONCE (context <NOIRQ_CONTEXT && + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (!pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); + } + +return __arm64_switch_exc_hwstate_to (prev, next);
__arm64_switch_exc_hwstate_to(prev, next);
return next;arch/arm64/include/asm/interrupts/entry.h line 44
Quoted 3 lines
arch/arm64/include/asm/interrupts/entry.h new file +3
-0+} + +Extra blank lines.
Quoted 15 lines
arch/arm64/include/asm/interrupts/entry.h new file +15
-0+static __always_inline +arm64_exc_hwstate_t arm64_unmask_exc_context(arm64_exc_context_t context) +{ +arm64_exc_hwstate_t prev =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + +return arm64_drop_exc_context (prev, context); +} + +static __always_inline +arm64_exc_hwstate_t arm64_mask_exc_context(arm64_exc_hwstate_t prev) +{ +return arm64_lift_exc_context (prev,CRITICAL_CONTEXT ); +} + +#endif /* __ASM_INTERRUPTS_ENTRY_H */
Re: [RFC PATCH v2 14/45] arm64: entry: Introduce entry specific exception masking helpers
Quoted 79 lines · Jinjie Ruan
Quoted 69 lines · Jinjie Ruan
Quoted 42 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
The entry code handles interrupt masking differently from the rest of the kernel. Exception handlers enter and exit with all exceptions masked, but they must temporarily unmask the appropriate set of exceptions so that the rest of the handler executes with the expected exception state.
For EL0 handlers, this means dropping to masking context appropriate for the work to be performed. For EL1 handlers, this means restoring the masking context of the interrupted task. In both cases, all exceptions must be masked again before returning from the exception handler.
The rest of the kernel typically follows the opposite pattern: it raises the masking context to protect a critical section and later restores the previous context.
Given these different usage patterns, introduce a dedicated set of exception masking helpers for the entry code. Keeping these helpers separate from the generic interrupt masking APIs makes the intended usage explicit and helps avoid mixing the two masking models.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/entry.h | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 arch/arm64/include/asm/interrupts/entry.harch/arm64/include/asm/interrupts/entry.h new file +6
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_ENTRY_H +#define __ASM_INTERRUPTS_ENTRY_Harch/arm64/include/asm/interrupts/entry.h line 16
Quoted 10 lines
arch/arm64/include/asm/interrupts/entry.h new file +10
-0+ +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> + + +static __always_inline +arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev, +arm64_exc_hwstate_t next)may be const
arm64_exc_hwstate_tnextQuoted 13 lines
arch/arm64/include/asm/interrupts/entry.h new file +13
-0+{ +bool irqs_disabled =arch_irqs_disabled_flags (next.flags); +bool force; + +arm64_debug_exc_hwstate (prev); + +if (prev.flags == next.flags) +return next; + +if (!irqs_disabled) +trace_hardirqs_on (); + + force =system_uses_irq_prio_masking () && prev.pmr != next.pmr;Quoted 8 lines
arch/arm64/include/asm/interrupts/entry.h line 36
Quoted 7 lines
arch/arm64/include/asm/interrupts/entry.h new file +7
-0+ +__arm64_update_exc_hwstate (next, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return next;We do not update next; it should be "void".
Quoted 3 lines
Hi Vladimir,
What do you think?
I have no strong opinion, TBH. It is done that way to make it interchangeable with the wrappers around it. For instance, the arm64_inherit_exc_context() wrapper can be easily replaced with the bare version:
__arm64_switch_exc_hwstate_to(arm64_exc_hwstate_of_context(CRITICAL_CONTEXT),
arm64_inherit_exc_hwstate(regs))
yes, it is less readable (and it is the reason we have wrappers), but functionally equivalent.
I'm happy to change it if people feel strongly about it.
Cheers
Vladimir
RFC PATCH v2 15/45 arm64: entry: replace DAIF helpers with entry helpers
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Replace all uses of the local_daif_... helpers in entry-common.c with the new entry-specific exception masking helpers.
Also replace local_irq_disable() with a switch to NOIRQ context using the new helpers. This provides:
- consistency checks for hardware state
- a unified style for managing exception context
Now that both the irqflags API and entry code handle DAIF and PMR correctly, remove GIC_PRIO_PSR_I_SET from the PMR value set by kernel_entry in entry.S and by init_gic_priority_masking() in kernel/smp.c.
The local_daif_... helpers and other specialized code still use GIC_PRIO_PSR_I_SET for now; this commit does not change their behavior.
--- arch/arm64/kernel/entry-common.c | 258 ++++++++++++++++++++----------- arch/arm64/kernel/entry.S | 2 +- arch/arm64/kernel/smp.c | 2 +- 3 files changed, 173 insertions(+), 89 deletions(-)
arch/arm64/kernel/entry-common.c +171 -87
@@ -18,11 +18,11 @@#include <linux/thread_info.h> #include <asm/cpufeature.h> -#include <asm/daifflags.h> #include <asm/esr.h> #include <asm/exception.h> #include <asm/fpsimd.h> #include <asm/irq_regs.h> +#include <asm/interrupts/entry.h> #include <asm/kprobes.h> #include <asm/mmu.h> #include <asm/processor.h>
@@ -53,9 +53,10 @@ static noinstr irqentry_state_t arm64_enter_from_kernel_mode(struct pt_regs *reg* instrumentable code, or any code which may trigger an exception. */ static void noinstr __arm64_exit_to_kernel_mode(struct pt_regs *regs, -irqentry_state_t state) +irqentry_state_t state, +arm64_exc_hwstate_t hwstate) { -local_daif_mask (); +arm64_mask_exc_context (hwstate);mte_check_tfsr_exit ();irqentry_exit_to_kernel_mode_after_preempt (regs, state); }
@@ -64,22 +65,24 @@ static void noinstr __arm64_exit_to_kernel_mode(struct pt_regs *regs,* We are returning from the context which allows involuntary kernel preemption */ static void noinstr arm64_exit_to_kernel_mode_preempt(struct pt_regs *regs, -irqentry_state_t state) +irqentry_state_t state, +arm64_exc_hwstate_t hwstate) {irqentry_exit_to_kernel_mode_preempt (regs, state); -__arm64_exit_to_kernel_mode (regs, state); +__arm64_exit_to_kernel_mode (regs, state, hwstate); } static void noinstr arm64_exit_to_kernel_mode(struct pt_regs *regs, -irqentry_state_t state) +irqentry_state_t state, +arm64_exc_hwstate_t hwstate) {if (!regs_irqs_disabled (regs)) { -local_irq_disable (); -arm64_exit_to_kernel_mode_preempt (regs, state); + hwstate =arm64_lift_exc_context (hwstate,NOIRQ_CONTEXT ); +arm64_exit_to_kernel_mode_preempt (regs, state, hwstate);return ; } -__arm64_exit_to_kernel_mode (regs, state); +__arm64_exit_to_kernel_mode (regs, state, hwstate); } static __always_inline void arm64_syscall_enter_from_user_mode(struct pt_regs *regs)
@@ -102,11 +105,12 @@ static __always_inline void arm64_enter_from_user_mode(struct pt_regs *regs)sme_enter_from_user_mode (); } -static __always_inline void arm64_syscall_exit_to_user_mode(struct pt_regs *regs) +static __always_inline void arm64_syscall_exit_to_user_mode(struct pt_regs *regs, +arm64_exc_hwstate_t hwstate) { -local_irq_disable (); + hwstate =arm64_lift_exc_context (hwstate,NOIRQ_CONTEXT );syscall_exit_to_user_mode_prepare (regs); -local_daif_mask (); +arm64_mask_exc_context (hwstate);sme_exit_to_user_mode ();mte_check_tfsr_exit ();exit_to_user_mode ();
@@ -117,11 +121,12 @@ static __always_inline void arm64_syscall_exit_to_user_mode(struct pt_regs *regs* After this function returns it is not safe to call regular kernel code, * instrumentable code, or any code which may trigger an exception. */ -static __always_inline void arm64_exit_to_user_mode(struct pt_regs *regs) +static __always_inline void arm64_exit_to_user_mode(struct pt_regs *regs, +arm64_exc_hwstate_t hwstate) { -local_irq_disable (); + hwstate =arm64_lift_exc_context (hwstate,NOIRQ_CONTEXT );irqentry_exit_to_user_mode_prepare (regs); -local_daif_mask (); +arm64_mask_exc_context (hwstate);sme_exit_to_user_mode ();mte_check_tfsr_exit ();exit_to_user_mode ();
@@ -129,7 +134,10 @@ static __always_inline void arm64_exit_to_user_mode(struct pt_regs *regs) asmlinkage void noinstr asm_exit_to_user_mode(struct pt_regs *regs) { -arm64_syscall_exit_to_user_mode (regs); +arm64_exc_hwstate_t hwstate =arm64_exc_hwstate_of_context (PROCESS_CONTEXT ); + +arm64_syscall_exit_to_user_mode (regs, hwstate); +arm64_debug_exc_context (CRITICAL_CONTEXT ); }/*
@@ -335,63 +343,69 @@ UNHANDLED(el1t, 64, error) static void noinstr el1_abort(struct pt_regs *regs, unsigned long esr) {unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =arm64_enter_from_kernel_mode (regs); -local_daif_inherit (regs); + hwstate =arm64_inherit_exc_context (regs);do_mem_abort (far, esr, regs); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode (regs, state, hwstate); } static void noinstr el1_pc(struct pt_regs *regs, unsigned long esr) {unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =arm64_enter_from_kernel_mode (regs); -local_daif_inherit (regs); + hwstate =arm64_inherit_exc_context (regs);do_sp_pc_abort (far, esr, regs); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode (regs, state, hwstate); } static void noinstr el1_undef(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =arm64_enter_from_kernel_mode (regs); -local_daif_inherit (regs); + hwstate =arm64_inherit_exc_context (regs);do_el1_undef (regs, esr); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode (regs, state, hwstate); } static void noinstr el1_bti(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =arm64_enter_from_kernel_mode (regs); -local_daif_inherit (regs); + hwstate =arm64_inherit_exc_context (regs);do_el1_bti (regs, esr); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode (regs, state, hwstate); } static void noinstr el1_gcs(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =arm64_enter_from_kernel_mode (regs); -local_daif_inherit (regs); + hwstate =arm64_inherit_exc_context (regs);do_el1_gcs (regs, esr); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode (regs, state, hwstate); } static void noinstr el1_mops(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =arm64_enter_from_kernel_mode (regs); -local_daif_inherit (regs); + hwstate =arm64_inherit_exc_context (regs);do_el1_mops (regs, esr); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode (regs, state, hwstate); } static void noinstr el1_breakpt(struct pt_regs *regs, unsigned long esr)
@@ -451,12 +465,13 @@ static void noinstr el1_brk64(struct pt_regs *regs, unsigned long esr) static void noinstr el1_fpac(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =arm64_enter_from_kernel_mode (regs); -local_daif_inherit (regs); + hwstate =arm64_inherit_exc_context (regs);do_el1_fpac (regs, esr); -arm64_exit_to_kernel_mode (regs, state); +arm64_exit_to_kernel_mode (regs, state, hwstate); } asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)
@@ -506,16 +521,20 @@ asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)default :__panic_unhandled (regs,"64-bit el1h sync" , esr); } + +arm64_debug_exc_context (CRITICAL_CONTEXT ); } static __always_inline void __el1_pnmi(struct pt_regs *regs,void (*handler)(struct pt_regs *)) { +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =irqentry_nmi_enter (regs); + hwstate =arm64_unmask_exc_context (NONMI_CONTEXT );do_interrupt_handler (regs, handler); -local_daif_mask (); +arm64_mask_exc_context (hwstate);irqentry_nmi_exit (regs, state); }
@@ -526,21 +545,32 @@ static __always_inline void __el1_irq(struct pt_regs *regs, state =arm64_enter_from_kernel_mode (regs); +arm64_unmask_exc_context (NONMI_CONTEXT ); +irq_enter_rcu ();do_interrupt_handler (regs, handler);irq_exit_rcu (); -arm64_exit_to_kernel_mode_preempt (regs, state); +/* +* If pseudo-NMIs are enabled and the interrupted context had +* IRQs unmasked, the interrupt handler will have cleared DAIF +* and switched to PMR masking in order to handle +* NMIs. Otherwise it would keep IF. In both cases on return +* we effectivly have NOIRQ_CONTEXT - keep track of it +*/ +arm64_debug_exc_context (NOIRQ_CONTEXT ); +arm64_exit_to_kernel_mode (regs, state,arm64_exc_hwstate_of_context (NOIRQ_CONTEXT )); } + static void noinstr el1_interrupt(struct pt_regs *regs,void (*handler)(struct pt_regs *)) { -write_sysreg (DAIF_PROCCTX_NOIRQ , daif); -if (IS_ENABLED (CONFIG_ARM64_PSEUDO_NMI ) &®s_irqs_disabled (regs))__el1_pnmi (regs, handler);else __el1_irq (regs, handler); + +arm64_debug_exc_context (CRITICAL_CONTEXT ); } asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs)
@@ -556,28 +586,31 @@ asmlinkage void noinstr el1h_64_fiq_handler(struct pt_regs *regs) asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs) {unsigned long esr =read_sysreg (esr_el1); +arm64_exc_hwstate_t hwstate;irqentry_state_t state; -local_daif_restore (DAIF_ERRCTX ); state =irqentry_nmi_enter (regs); + hwstate =arm64_unmask_exc_context (ERROR_CONTEXT );do_serror (regs, esr); -local_daif_mask (); +arm64_mask_exc_context (hwstate);irqentry_nmi_exit (regs, state); } static void noinstr el0_da(struct pt_regs *regs, unsigned long esr) {unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_mem_abort (far, esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr) {unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;/* * We've taken an instruction abort from userspace and not yet
@@ -588,114 +621,139 @@ static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr)arm64_apply_bp_hardening ();arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_mem_abort (far, esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_fpsimd_acc(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_fpsimd_acc (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_sve_acc(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_sve_acc (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_sme_acc(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_sme_acc (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_fpsimd_exc(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_fpsimd_exc (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_sys(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_sys (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_pc(struct pt_regs *regs, unsigned long esr) {unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;if (!is_ttbr0_addr (instruction_pointer (regs)))arm64_apply_bp_hardening ();arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_sp_pc_abort (far, esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_sp(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_sp_pc_abort (regs->sp, esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_undef(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_undef (regs, esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_bti(struct pt_regs *regs) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_bti (regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_mops(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_mops (regs, esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_gcs(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_gcs (regs, esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );bad_el0_sync (regs,0 , esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_breakpt(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +if (!is_ttbr0_addr (regs->pc))arm64_apply_bp_hardening ();
@@ -703,12 +761,13 @@ static void noinstr el0_breakpt(struct pt_regs *regs, unsigned long esr)debug_exception_enter (regs);do_breakpoint (esr, regs);debug_exception_exit (regs); -local_daif_restore (DAIF_PROCCTX ); -arm64_exit_to_user_mode (regs); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT ); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_softstp(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate;bool step_done;if (!is_ttbr0_addr (regs->pc))
@@ -722,50 +781,56 @@ static void noinstr el0_softstp(struct pt_regs *regs, unsigned long esr)* the single-step is complete. */ step_done =try_step_suspended_breakpoints (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );if (!step_done)do_el0_softstep (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_watchpt(struct pt_regs *regs, unsigned long esr) {/* Watchpoints are the only debug exception to write FAR_EL1 */ unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;arm64_enter_from_user_mode (regs);debug_exception_enter (regs);do_watchpoint (far, esr, regs);debug_exception_exit (regs); -local_daif_restore (DAIF_PROCCTX ); -arm64_exit_to_user_mode (regs); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT ); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_brk64(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_brk64 (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_svc(struct pt_regs *regs) { +arm64_exc_hwstate_t hwstate;arm64_syscall_enter_from_user_mode (regs);cortex_a76_erratum_1463225_svc_handler ();fpsimd_syscall_enter (); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_svc (regs); -arm64_syscall_exit_to_user_mode (regs); +arm64_syscall_exit_to_user_mode (regs, hwstate);fpsimd_syscall_exit (); } static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_fpac (regs, esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)
@@ -834,6 +899,8 @@ asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)default :el0_inv (regs, esr); } + +arm64_debug_exc_context (CRITICAL_CONTEXT ); } static void noinstr el0_interrupt(struct pt_regs *regs,
@@ -841,7 +908,7 @@ static void noinstr el0_interrupt(struct pt_regs *regs, {arm64_enter_from_user_mode (regs); -write_sysreg (DAIF_PROCCTX_NOIRQ , daif); +arm64_unmask_exc_context (NONMI_CONTEXT );if (regs->pc &BIT (55 ))arm64_apply_bp_hardening ();
@@ -850,7 +917,14 @@ static void noinstr el0_interrupt(struct pt_regs *regs,do_interrupt_handler (regs, handler);irq_exit_rcu (); -arm64_exit_to_user_mode (regs); +/* +* For the same reason as in el1_irq() we effectivly +* have NOIRQ_CONTEXT on return from handler - keep +* track of it +*/ +arm64_debug_exc_context (NOIRQ_CONTEXT ); +arm64_exit_to_user_mode (regs,arm64_exc_hwstate_of_context (NOIRQ_CONTEXT )); +arm64_debug_exc_context (CRITICAL_CONTEXT ); } static void noinstr __el0_irq_handler_common(struct pt_regs *regs)
@@ -876,15 +950,17 @@ asmlinkage void noinstr el0t_64_fiq_handler(struct pt_regs *regs) static void noinstr __el0_error_handler_common(struct pt_regs *regs) {unsigned long esr =read_sysreg (esr_el1); +arm64_exc_hwstate_t hwstate;irqentry_state_t state;arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_ERRCTX ); + hwstate =arm64_unmask_exc_context (ERROR_CONTEXT ); state =irqentry_nmi_enter (regs);do_serror (regs, esr);irqentry_nmi_exit (regs, state); -local_daif_restore (DAIF_PROCCTX ); -arm64_exit_to_user_mode (regs); + hwstate =arm64_drop_exc_context (hwstate,PROCESS_CONTEXT ); +arm64_exit_to_user_mode (regs, hwstate); +arm64_debug_exc_context (CRITICAL_CONTEXT ); } asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)
@@ -895,27 +971,33 @@ asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)#ifdef CONFIG_COMPAT static void noinstr el0_cp15(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_cp15 (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_svc_compat(struct pt_regs *regs) { +arm64_exc_hwstate_t hwstate; +arm64_syscall_enter_from_user_mode (regs);cortex_a76_erratum_1463225_svc_handler (); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_svc_compat (regs); -arm64_syscall_exit_to_user_mode (regs); +arm64_syscall_exit_to_user_mode (regs, hwstate); } static void noinstr el0_bkpt32(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_bkpt32 (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)
@@ -966,6 +1048,8 @@ asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)default :el0_inv (regs, esr); } + +arm64_debug_exc_context (CRITICAL_CONTEXT ); } asmlinkage void noinstr el0t_32_irq_handler(struct pt_regs *regs)
arch/arm64/kernel/entry.S +1 -1
@@ -316,7 +316,7 @@ alternative_else_nop_endif mrs_s x20, SYS_ICC_PMR_EL1 str w20, [sp, #S_PMR] - mov x20, #GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET + mov x20, #GIC_PRIO_IRQON msr_s SYS_ICC_PMR_EL1, x20 .Lskip_pmr_save\@:
arch/arm64/kernel/smp.c +1 -1
@@ -185,7 +185,7 @@ static void init_gic_priority_masking(void)WARN_ON (!(cpuflags &PSR_I_BIT ));WARN_ON (!(cpuflags &PSR_F_BIT )); -gic_write_pmr (GIC_PRIO_IRQON |GIC_PRIO_PSR_I_SET ); +gic_write_pmr (GIC_PRIO_IRQON ); }/*
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 15/45] arm64: entry: replace DAIF helpers with entry helpers
Quoted 48 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
Replace all uses of the
local_daif_...helpers inentry-common.cwith the new entry-specific exception masking helpers.Also replace
local_irq_disable()with a switch to NOIRQ context using the new helpers. This provides:- consistency checks for hardware state
- a unified style for managing exception contextNow that both the irqflags API and entry code handle DAIF and PMR correctly, remove
GIC_PRIO_PSR_I_SETfrom the PMR value set bykernel_entryinentry.Sand byinit_gic_priority_masking()inkernel/smp.c.The
local_daif_...helpers and other specialized code still useGIC_PRIO_PSR_I_SETfor now; this commit does not change their behavior.Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/entry-common.c | 258 ++++++++++++++++++++----------- arch/arm64/kernel/entry.S | 2 +- arch/arm64/kernel/smp.c | 2 +- 3 files changed, 173 insertions(+), 89 deletions(-)arch/arm64/kernel/entry-common.c +1
-1@@ -18,11 +18,11 @@#include <linux/thread_info.h> #include <asm/cpufeature.h> -#include <asm/daifflags.h> #include <asm/esr.h> #include <asm/exception.h> #include <asm/fpsimd.h> #include <asm/irq_regs.h> +#include <asm/interrupts/entry.h> #include <asm/kprobes.h> #include <asm/mmu.h> #include <asm/processor.h> @@ -53,9 +53,10 @@ static noinstr irqentry_state_t arm64_enter_from_kernel_mode(struct pt_regs *reg* instrumentable code, or any code which may trigger an exception. */
[...]
Quoted 35 lines
asmlinkage void noinstr
el1h_64_sync_handler(struct pt_regs *regs)arch/arm64/kernel/entry-common.c +7
-2@@ -506,16 +521,20 @@ asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)default :__panic_unhandled (regs,"64-bit el1h sync" , esr); } + +arm64_debug_exc_context (CRITICAL_CONTEXT ); } static __always_inline void __el1_pnmi(struct pt_regs *regs,void (*handler)(struct pt_regs *)) { +arm64_exc_hwstate_t hwstate;irqentry_state_t state; state =irqentry_nmi_enter (regs); + hwstate =arm64_unmask_exc_context (NONMI_CONTEXT );do_interrupt_handler (regs, handler); -local_daif_mask (); +arm64_mask_exc_context (hwstate);irqentry_nmi_exit (regs, state); }@@ -526,21 +545,32 @@ static __always_inline void __el1_irq(struct pt_regs *regs, state =arm64_enter_from_kernel_mode (regs); +arm64_unmask_exc_context (NONMI_CONTEXT ); +irq_enter_rcu ();do_interrupt_handler (regs, handler);irq_exit_rcu (); -arm64_exit_to_kernel_mode_preempt (regs, state);
arch/arm64/kernel/entry-common.c static __always_inline void __el1_irq(struct pt_regs *regs, · line 563
Quoted 10 lines
arch/arm64/kernel/entry-common.c +9
-0+/* +* If pseudo-NMIs are enabled and the interrupted context had +* IRQs unmasked, the interrupt handler will have cleared DAIF +* and switched to PMR masking in order to handle +* NMIs. Otherwise it would keep IF. In both cases on return +* we effectivly have NOIRQ_CONTEXT - keep track of it +*/ +arm64_debug_exc_context (NOIRQ_CONTEXT ); +arm64_exit_to_kernel_mode (regs, state,arm64_exc_hwstate_of_context (NOIRQ_CONTEXT )); }
Is this a typo?
arm64_exit_to_kernel_mode_preempt(regs,arm64_exc_hwstate_of_context(NOIRQ_CONTEXT));
otherwise LGTM
Quoted 430 lines
arch/arm64/kernel/entry-common.c +110
-55+ static void noinstr el1_interrupt(struct pt_regs *regs,void (*handler)(struct pt_regs *)) { -write_sysreg (DAIF_PROCCTX_NOIRQ , daif); -if (IS_ENABLED (CONFIG_ARM64_PSEUDO_NMI ) &®s_irqs_disabled (regs))__el1_pnmi (regs, handler);else __el1_irq (regs, handler); + +arm64_debug_exc_context (CRITICAL_CONTEXT ); } asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs)@@ -556,28 +586,31 @@ asmlinkage void noinstr el1h_64_fiq_handler(struct pt_regs *regs) asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs) {unsigned long esr =read_sysreg (esr_el1); +arm64_exc_hwstate_t hwstate;irqentry_state_t state; -local_daif_restore (DAIF_ERRCTX ); state =irqentry_nmi_enter (regs); + hwstate =arm64_unmask_exc_context (ERROR_CONTEXT );do_serror (regs, esr); -local_daif_mask (); +arm64_mask_exc_context (hwstate);irqentry_nmi_exit (regs, state); } static void noinstr el0_da(struct pt_regs *regs, unsigned long esr) {unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_mem_abort (far, esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr) {unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;/* * We've taken an instruction abort from userspace and not yet @@ -588,114 +621,139 @@ static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr)arm64_apply_bp_hardening ();arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_mem_abort (far, esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_fpsimd_acc(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_fpsimd_acc (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_sve_acc(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_sve_acc (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_sme_acc(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_sme_acc (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_fpsimd_exc(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_fpsimd_exc (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_sys(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_sys (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_pc(struct pt_regs *regs, unsigned long esr) {unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;if (!is_ttbr0_addr (instruction_pointer (regs)))arm64_apply_bp_hardening ();arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_sp_pc_abort (far, esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_sp(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_sp_pc_abort (regs->sp, esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_undef(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_undef (regs, esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_bti(struct pt_regs *regs) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_bti (regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_mops(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_mops (regs, esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_gcs(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_gcs (regs, esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );bad_el0_sync (regs,0 , esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_breakpt(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +if (!is_ttbr0_addr (regs->pc))arm64_apply_bp_hardening ();@@ -703,12 +761,13 @@ static void noinstr el0_breakpt(struct pt_regs *regs, unsigned long esr)debug_exception_enter (regs);do_breakpoint (esr, regs);debug_exception_exit (regs); -local_daif_restore (DAIF_PROCCTX ); -arm64_exit_to_user_mode (regs); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT ); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_softstp(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate;bool step_done;if (!is_ttbr0_addr (regs->pc))@@ -722,50 +781,56 @@ static void noinstr el0_softstp(struct pt_regs *regs, unsigned long esr)* the single-step is complete. */ step_done =try_step_suspended_breakpoints (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );if (!step_done)do_el0_softstep (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_watchpt(struct pt_regs *regs, unsigned long esr) {/* Watchpoints are the only debug exception to write FAR_EL1 */ unsigned long far =read_sysreg (far_el1); +arm64_exc_hwstate_t hwstate;arm64_enter_from_user_mode (regs);debug_exception_enter (regs);do_watchpoint (far, esr, regs);debug_exception_exit (regs); -local_daif_restore (DAIF_PROCCTX ); -arm64_exit_to_user_mode (regs); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT ); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_brk64(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_brk64 (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_svc(struct pt_regs *regs) { +arm64_exc_hwstate_t hwstate;arm64_syscall_enter_from_user_mode (regs);cortex_a76_erratum_1463225_svc_handler ();fpsimd_syscall_enter (); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_svc (regs); -arm64_syscall_exit_to_user_mode (regs); +arm64_syscall_exit_to_user_mode (regs, hwstate);fpsimd_syscall_exit (); } static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_fpac (regs, esr); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)@@ -834,6 +899,8 @@ asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)default :el0_inv (regs, esr); } + +arm64_debug_exc_context (CRITICAL_CONTEXT ); } static void noinstr el0_interrupt(struct pt_regs *regs,@@ -841,7 +908,7 @@ static void noinstr el0_interrupt(struct pt_regs *regs, {arm64_enter_from_user_mode (regs); -write_sysreg (DAIF_PROCCTX_NOIRQ , daif); +arm64_unmask_exc_context (NONMI_CONTEXT );if (regs->pc &BIT (55 ))arm64_apply_bp_hardening ();@@ -850,7 +917,14 @@ static void noinstr el0_interrupt(struct pt_regs *regs,do_interrupt_handler (regs, handler);irq_exit_rcu (); -arm64_exit_to_user_mode (regs); +/* +* For the same reason as in el1_irq() we effectivly +* have NOIRQ_CONTEXT on return from handler - keep +* track of it +*/ +arm64_debug_exc_context (NOIRQ_CONTEXT ); +arm64_exit_to_user_mode (regs,arm64_exc_hwstate_of_context (NOIRQ_CONTEXT )); +arm64_debug_exc_context (CRITICAL_CONTEXT ); } static void noinstr __el0_irq_handler_common(struct pt_regs *regs)@@ -876,15 +950,17 @@ asmlinkage void noinstr el0t_64_fiq_handler(struct pt_regs *regs) static void noinstr __el0_error_handler_common(struct pt_regs *regs) {unsigned long esr =read_sysreg (esr_el1); +arm64_exc_hwstate_t hwstate;irqentry_state_t state;arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_ERRCTX ); + hwstate =arm64_unmask_exc_context (ERROR_CONTEXT ); state =irqentry_nmi_enter (regs);do_serror (regs, esr);irqentry_nmi_exit (regs, state); -local_daif_restore (DAIF_PROCCTX ); -arm64_exit_to_user_mode (regs); + hwstate =arm64_drop_exc_context (hwstate,PROCESS_CONTEXT ); +arm64_exit_to_user_mode (regs, hwstate); +arm64_debug_exc_context (CRITICAL_CONTEXT ); } asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)@@ -895,27 +971,33 @@ asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)#ifdef CONFIG_COMPAT static void noinstr el0_cp15(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_cp15 (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } static void noinstr el0_svc_compat(struct pt_regs *regs) { +arm64_exc_hwstate_t hwstate; +arm64_syscall_enter_from_user_mode (regs);cortex_a76_erratum_1463225_svc_handler (); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_el0_svc_compat (regs); -arm64_syscall_exit_to_user_mode (regs); +arm64_syscall_exit_to_user_mode (regs, hwstate); } static void noinstr el0_bkpt32(struct pt_regs *regs, unsigned long esr) { +arm64_exc_hwstate_t hwstate; +arm64_enter_from_user_mode (regs); -local_daif_restore (DAIF_PROCCTX ); + hwstate =arm64_unmask_exc_context (PROCESS_CONTEXT );do_bkpt32 (esr, regs); -arm64_exit_to_user_mode (regs); +arm64_exit_to_user_mode (regs, hwstate); } asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)@@ -966,6 +1048,8 @@ asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)default :el0_inv (regs, esr); } + +arm64_debug_exc_context (CRITICAL_CONTEXT ); } asmlinkage void noinstr el0t_32_irq_handler(struct pt_regs *regs)arch/arm64/kernel/entry.S +1
-1@@ -316,7 +316,7 @@ alternative_else_nop_endif mrs_s x20, SYS_ICC_PMR_EL1 str w20, [sp, #S_PMR] - mov x20, #GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET + mov x20, #GIC_PRIO_IRQON msr_s SYS_ICC_PMR_EL1, x20 .Lskip_pmr_save\@:arch/arm64/kernel/smp.c +1
-1@@ -185,7 +185,7 @@ static void init_gic_priority_masking(void)WARN_ON (!(cpuflags &PSR_I_BIT ));WARN_ON (!(cpuflags &PSR_F_BIT )); -gic_write_pmr (GIC_PRIO_IRQON |GIC_PRIO_PSR_I_SET ); +gic_write_pmr (GIC_PRIO_IRQON ); }/*
Re: [RFC PATCH v2 15/45] arm64: entry: replace DAIF helpers with entry helpers
Quoted 23 lines · Jinjie Ruan
Quoted 11 lines
arch/arm64/kernel/entry-common.c +2
-1@@ -526,21 +545,32 @@ static __always_inline void __el1_irq(struct pt_regs *regs, state =arm64_enter_from_kernel_mode (regs); +arm64_unmask_exc_context (NONMI_CONTEXT ); +irq_enter_rcu ();do_interrupt_handler (regs, handler);irq_exit_rcu (); -arm64_exit_to_kernel_mode_preempt (regs, state);arch/arm64/kernel/entry-common.c static __always_inline void __el1_irq(struct pt_regs *regs, · line 563
Quoted 10 lines
arch/arm64/kernel/entry-common.c +9
-0+/* +* If pseudo-NMIs are enabled and the interrupted context had +* IRQs unmasked, the interrupt handler will have cleared DAIF +* and switched to PMR masking in order to handle +* NMIs. Otherwise it would keep IF. In both cases on return +* we effectivly have NOIRQ_CONTEXT - keep track of it +*/ +arm64_debug_exc_context (NOIRQ_CONTEXT ); +arm64_exit_to_kernel_mode (regs, state,arm64_exc_hwstate_of_context (NOIRQ_CONTEXT )); }Is this a typo?
Quoted 3 lines
arm64_exit_to_kernel_mode_preempt(regs,
arm64_exc_hwstate_of_context(NOIRQ_CONTEXT));
Oops! Seems lost that while splitting the patch. Nice catch!
Quoted 2 lines
otherwise LGTM
Reviewed-by Jinjie Ruan <[email protected]>
Thanks
Vladimir
RFC PATCH v2 16/45 arm64: interrupts: Introduce exception masking save/restore helpers
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Outside the entry code, the local_daif_... helpers need to be replaced so that both DAIF and PMR are handled correctly and their use can be controlled more strongly.
Introduce local_exceptions_save_mask() and local_exceptions_restore(), which must be used as a pair. Save both the original and requested exception state so that inconsistent changes between the save and restore can be detected.
There are two cases where paired save and restore operations do not make sense:
- when initializing a CPU, and
- when preparing to turn it off.
Handle those cases with local_exceptions_cpu_init_mask() and local_exceptions_final_mask(). When CONFIG_DEBUG_IRQFLAGS is enabled, enforce that these helpers are not used outside their intended contexts.
--- arch/arm64/include/asm/interrupts/masking.h | 89 +++++++++++++++++++++ arch/arm64/kernel/irq.c | 32 +++++++- 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/interrupts/masking.h
arch/arm64/include/asm/interrupts/masking.h new file +89 -0
@@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_MASKING_H +#define __ASM_INTERRUPTS_MASKING_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> +#include <asm/ptrace.h> + +typedef struct arm64_exc_hwstates { +arm64_exc_hwstate_t saved; +arm64_exc_hwstate_t expected; +}arm64_exc_hwstates_t ; + +static inline +arm64_exc_hwstates_t local_exceptions_save_mask (arm64_exc_context_t new) +{ +arm64_exc_hwstate_t actual = {.flags =arch_local_save_flags ()}; +arm64_exc_hwstate_t state =arm64_exc_hwstate_of_context (new); +bool irqs_disabled =arch_irqs_disabled_flags (state.flags); +bool force; + +/* +* We've just got actual HW state so we can rely on that to +* optimize some unnecessary updates. +*/ + force =system_uses_irq_prio_masking () && actual.pmr != state.pmr; + +if (!irqs_disabled) +trace_hardirqs_on (); + +__arm64_update_exc_hwstate (state, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return (arm64_exc_hwstates_t ){.saved = actual, .expected = state}; +} + +static inline void local_exceptions_restore (arm64_exc_hwstates_t states) +{ +bool irqs_disabled =arch_irqs_disabled_flags (states.saved.flags); + +arm64_debug_exc_hwstate (states.expected); + +if (!irqs_disabled) +trace_hardirqs_on (); + +arm64_update_exc_hwstate (states.saved); + +if (irqs_disabled) +trace_hardirqs_off (); +} + +static __always_inline +void __local_exceptions_cpu_init_mask(arm64_exc_context_t context) +{ +if (context ==PROCESS_CONTEXT ) +trace_hardirqs_on (); + +arm64_update_exc_context (context); +} + +static __always_inline void __local_exceptions_final_mask(void) +{ +arm64_update_exc_context (CRITICAL_CONTEXT ); +trace_hardirqs_off (); +} + +#ifdef CONFIG_DEBUG_IRQFLAGS +void local_exceptions_cpu_init_mask (arm64_exc_context_t context); +void local_exceptions_final_mask (void ); +#else +static inline +void local_exceptions_cpu_init_mask (arm64_exc_context_t context) +{ +__local_exceptions_cpu_init_mask (context); +} + +static inline void local_exceptions_final_mask (void ) +{ +__local_exceptions_final_mask (); +} +#endif /* CONFIG_DEBUG_IRQFLAGS */ +#endif /* __ASM_INTERRUPTS_MASKING_H */
arch/arm64/kernel/irq.c +31 -1
@@ -21,7 +21,7 @@#include <linux/seq_file.h> #include <linux/smp.h> #include <linux/vmalloc.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/exception.h> #include <asm/numa.h> #include <asm/softirq_stack.h>
@@ -39,6 +39,12 @@ DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr); DEFINE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);#endif +#ifdef CONFIG_DEBUG_IRQFLAGS +/* Make sure the CPU init/tear down masking functions are only used once. */ +static cpumask_t irqs_masks_init_done; +static cpumask_t irqs_masks_final_done; +#endif +static int __init init_irq_scs (void ) {int cpu;
@@ -133,3 +139,27 @@ void __init init_IRQ(void)local_daif_restore (DAIF_PROCCTX_NOIRQ ); } } + +#ifdef CONFIG_DEBUG_IRQFLAGS +void local_exceptions_cpu_init_mask (arm64_exc_context_t context) +{ +int cpu =smp_processor_id (); + +WARN_ON (cpumask_test_cpu (cpu, &irqs_masks_init_done)); + +__local_exceptions_cpu_init_mask (context); +cpumask_set_cpu (cpu, &irqs_masks_init_done); +cpumask_clear_cpu (cpu, &irqs_masks_final_done); +} + +void local_exceptions_final_mask (void ) +{ +int cpu =smp_processor_id (); + +WARN_ON (cpumask_test_cpu (cpu, &irqs_masks_final_done)); + +__local_exceptions_final_mask (); +cpumask_set_cpu (cpu, &irqs_masks_final_done); +cpumask_clear_cpu (cpu, &irqs_masks_init_done); +} +#endif
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 16/45] arm64: interrupts: Introduce exception masking save/restore helpers
Quoted 91 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
Outside the entry code, the
local_daif_... helpers need to be replaced so that both DAIF and PMR are handled correctly and their use can be controlled more strongly.Introduce
local_exceptions_save_mask()andlocal_exceptions_restore(), which must be used as a pair. Save both the original and requested exception state so that inconsistent changes between the save and restore can be detected.There are two cases where paired save and restore operations do not make sense:
- when initializing a CPU, and
- when preparing to turn it off.Handle those cases with
local_exceptions_cpu_init_mask()andlocal_exceptions_final_mask(). WhenCONFIG_DEBUG_IRQFLAGSis enabled, enforce that these helpers are not used outside their intended contexts.Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/masking.h | 89 +++++++++++++++++++++ arch/arm64/kernel/irq.c | 32 +++++++- 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/interrupts/masking.harch/arm64/include/asm/interrupts/masking.h new file +55
-0@@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2025 Arm Ltd. +*/ +#ifndef __ASM_INTERRUPTS_MASKING_H +#define __ASM_INTERRUPTS_MASKING_H + +#include <asm/arch_gicv3.h> +#include <asm/bug.h> +#include <asm/cpufeature.h> +#include <asm/interrupts/common_flags.h> +#include <asm/ptrace.h> + +typedef struct arm64_exc_hwstates { +arm64_exc_hwstate_t saved; +arm64_exc_hwstate_t expected; +}arm64_exc_hwstates_t ; + +static inline +arm64_exc_hwstates_t local_exceptions_save_mask (arm64_exc_context_t new) +{ +arm64_exc_hwstate_t actual = {.flags =arch_local_save_flags ()}; +arm64_exc_hwstate_t state =arm64_exc_hwstate_of_context (new); +bool irqs_disabled =arch_irqs_disabled_flags (state.flags); +bool force; + +/* +* We've just got actual HW state so we can rely on that to +* optimize some unnecessary updates. +*/ + force =system_uses_irq_prio_masking () && actual.pmr != state.pmr; + +if (!irqs_disabled) +trace_hardirqs_on (); + +__arm64_update_exc_hwstate (state, force); + +if (irqs_disabled) +trace_hardirqs_off (); + +return (arm64_exc_hwstates_t ){.saved = actual, .expected = state}; +} + +static inline void local_exceptions_restore (arm64_exc_hwstates_t states) +{ +bool irqs_disabled =arch_irqs_disabled_flags (states.saved.flags); + +arm64_debug_exc_hwstate (states.expected); + +if (!irqs_disabled) +trace_hardirqs_on (); + +arm64_update_exc_hwstate (states.saved); + +if (irqs_disabled)
arch/arm64/include/asm/interrupts/masking.h line 65
Quoted 10 lines
arch/arm64/include/asm/interrupts/masking.h new file +10
-0+trace_hardirqs_off (); +} + +static __always_inline +void __local_exceptions_cpu_init_mask(arm64_exc_context_t context) +{ +if (context ==PROCESS_CONTEXT ) +trace_hardirqs_on (); + +arm64_update_exc_context (context);
Is following miss for setup_arch()?
if (context == NOIRQ_CONTEXT) trace_hardirqs_off();
Quoted 78 lines
arch/arm64/include/asm/interrupts/masking.h new file +24
-0+} + +static __always_inline void __local_exceptions_final_mask(void) +{ +arm64_update_exc_context (CRITICAL_CONTEXT ); +trace_hardirqs_off (); +} + +#ifdef CONFIG_DEBUG_IRQFLAGS +void local_exceptions_cpu_init_mask (arm64_exc_context_t context); +void local_exceptions_final_mask (void ); +#else +static inline +void local_exceptions_cpu_init_mask (arm64_exc_context_t context) +{ +__local_exceptions_cpu_init_mask (context); +} + +static inline void local_exceptions_final_mask (void ) +{ +__local_exceptions_final_mask (); +} +#endif /* CONFIG_DEBUG_IRQFLAGS */ +#endif /* __ASM_INTERRUPTS_MASKING_H */ arch/arm64/kernel/irq.c +31
-1@@ -21,7 +21,7 @@#include <linux/seq_file.h> #include <linux/smp.h> #include <linux/vmalloc.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/exception.h> #include <asm/numa.h> #include <asm/softirq_stack.h> @@ -39,6 +39,12 @@ DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr); DEFINE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);#endif +#ifdef CONFIG_DEBUG_IRQFLAGS +/* Make sure the CPU init/tear down masking functions are only used once. */ +static cpumask_t irqs_masks_init_done; +static cpumask_t irqs_masks_final_done; +#endif +static int __init init_irq_scs (void ) {int cpu;@@ -133,3 +139,27 @@ void __init init_IRQ(void)local_daif_restore (DAIF_PROCCTX_NOIRQ ); } } + +#ifdef CONFIG_DEBUG_IRQFLAGS +void local_exceptions_cpu_init_mask (arm64_exc_context_t context) +{ +int cpu =smp_processor_id (); + +WARN_ON (cpumask_test_cpu (cpu, &irqs_masks_init_done)); + +__local_exceptions_cpu_init_mask (context); +cpumask_set_cpu (cpu, &irqs_masks_init_done); +cpumask_clear_cpu (cpu, &irqs_masks_final_done); +} + +void local_exceptions_final_mask (void ) +{ +int cpu =smp_processor_id (); + +WARN_ON (cpumask_test_cpu (cpu, &irqs_masks_final_done)); + +__local_exceptions_final_mask (); +cpumask_set_cpu (cpu, &irqs_masks_final_done); +cpumask_clear_cpu (cpu, &irqs_masks_init_done); +} +#endif
Re: [RFC PATCH v2 16/45] arm64: interrupts: Introduce exception masking save/restore helpers
Quoted 9 lines · Jinjie Ruan
arch/arm64/include/asm/interrupts/masking.h line 65
Quoted 7 lines
arch/arm64/include/asm/interrupts/masking.h new file +7
-0+static __always_inline +void __local_exceptions_cpu_init_mask(arm64_exc_context_t context) +{ +if (context ==PROCESS_CONTEXT ) +trace_hardirqs_on (); + +arm64_update_exc_context (context);Is following miss for
setup_arch()?
Quoted 3 lines
if (context == NOIRQ_CONTEXT) trace_hardirqs_off();
Yup. Perhaps better do
if (context != PROCESS_CONTEXT) trace_hardirqs_off();
I'll fix that in the next version.
Cheers
Vladimir
RFC PATCH v2 17/45 arm64: interrupts: introduce a helper for GIC priority initialization
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Arm64's init_IRQ() calls local_daif_restore() to synchronize interrupt masking via DAIF and switch to masking via PMR. This depends on a very specific behaviour of local_daif_restore() which will clear DAIF if masking interrupts via PMR, which will get removed in the future.
As setup_arch() cleared DA only earlier, introduce a dedicated helper that explicitly initializes the PMR to mask interrupts and clears DAIF, switching to IRQ priority masking.
Given it is a dedicated helper, add a lockdep assertion as setup_arch() should always have called trace_hardirqs_off() when clearing DA, otherwise something bad happened.
--- arch/arm64/include/asm/interrupts/masking.h | 19 +++++++++++++++++++ arch/arm64/kernel/irq.c | 13 +++++-------- 2 files changed, 24 insertions(+), 8 deletions(-)
arch/arm64/include/asm/interrupts/masking.h +19 -0
@@ -5,6 +5,8 @@#ifndef __ASM_INTERRUPTS_MASKING_H#define __ASM_INTERRUPTS_MASKING_H +#include <linux/lockdep.h> +#include <asm/arch_gicv3.h> #include <asm/bug.h> #include <asm/cpufeature.h>
@@ -86,4 +88,21 @@ static inline void local_exceptions_final_mask(void)__local_exceptions_final_mask (); }#endif /* CONFIG_DEBUG_IRQFLAGS */ + +/* +* During early boot, we unmask PSR.DA before the GIC has been set up. +* If we use IRQ priority masking, the PMR and PSR will be out of sync +* after the GIC is enabled : sync them up. +*/ +static inline void local_interrupt_priority_init (void ) +{ +if (!system_uses_irq_prio_masking ()) +return ; + +WARN_ON (read_sysreg (daif) &PSR_A_BIT ); +lockdep_assert_irqs_disabled (); + +arm64_update_exc_context (NOIRQ_CONTEXT ); +} +#endif /* __ASM_INTERRUPTS_MASKING_H */
arch/arm64/kernel/irq.c +5 -8
@@ -130,14 +130,11 @@ void __init init_IRQ(void)irqchip_init (); -if (system_uses_irq_prio_masking ()) { -/* -* Now that we have a stack for our IRQ handler, set -* the PMR/PSR pair to a consistent state. -*/ -WARN_ON (read_sysreg (daif) &PSR_A_BIT ); -local_daif_restore (DAIF_PROCCTX_NOIRQ ); - } +/* +* Now that we have a stack for our IRQ handler, +* let's mask interrupts via the PMR. +*/ +local_interrupt_priority_init (); }#ifdef CONFIG_DEBUG_IRQFLAGS
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 17/45] arm64: interrupts: introduce a helper for GIC priority initialization
Quoted 69 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
Arm64's
init_IRQ()callslocal_daif_restore()to synchronize interrupt masking via DAIF and switch to masking via PMR. This depends on a very specific behaviour oflocal_daif_restore()which will clear DAIF if masking interrupts via PMR, which will get removed in the future.As
setup_arch()cleared DA only earlier, introduce a dedicated helper that explicitly initializes the PMR to mask interrupts and clears DAIF, switching to IRQ priority masking.Given it is a dedicated helper, add a lockdep assertion as
setup_arch()should always have calledtrace_hardirqs_off()when clearing DA, otherwise something bad happened.Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/interrupts/masking.h | 19 +++++++++++++++++++ arch/arm64/kernel/irq.c | 13 +++++-------- 2 files changed, 24 insertions(+), 8 deletions(-)arch/arm64/include/asm/interrupts/masking.h +19
-0@@ -5,6 +5,8 @@#ifndef __ASM_INTERRUPTS_MASKING_H#define __ASM_INTERRUPTS_MASKING_H +#include <linux/lockdep.h> +#include <asm/arch_gicv3.h> #include <asm/bug.h> #include <asm/cpufeature.h> @@ -86,4 +88,21 @@ static inline void local_exceptions_final_mask(void)__local_exceptions_final_mask (); }#endif /* CONFIG_DEBUG_IRQFLAGS */ + +/* +* During early boot, we unmask PSR.DA before the GIC has been set up. +* If we use IRQ priority masking, the PMR and PSR will be out of sync +* after the GIC is enabled : sync them up. +*/ +static inline void local_interrupt_priority_init (void ) +{ +if (!system_uses_irq_prio_masking ()) +return ; + +WARN_ON (read_sysreg (daif) &PSR_A_BIT ); +lockdep_assert_irqs_disabled (); + +arm64_update_exc_context (NOIRQ_CONTEXT ); +} +#endif /* __ASM_INTERRUPTS_MASKING_H */ arch/arm64/kernel/irq.c +0
-3@@ -130,14 +130,11 @@ void __init init_IRQ(void)irqchip_init (); -if (system_uses_irq_prio_masking ()) { -/* -* Now that we have a stack for our IRQ handler, set
arch/arm64/kernel/irq.c void __init init_IRQ(void) · line 137
Quoted 10 lines
arch/arm64/kernel/irq.c +5
-5-* the PMR/PSR pair to a consistent state. -*/ -WARN_ON (read_sysreg (daif) &PSR_A_BIT ); -local_daif_restore (DAIF_PROCCTX_NOIRQ ); - } +/* +* Now that we have a stack for our IRQ handler, +* let's mask interrupts via the PMR. +*/ +local_interrupt_priority_init ();
Quoted 3 lines
arch/arm64/kernel/irq.c +0
-0}#ifdef CONFIG_DEBUG_IRQFLAGS
RFC PATCH v2 18/45 arm64: replace local_daif helpers
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Replace existing local_daif_... usage with the new local_all_irqs... helpers, adding the necessary save-state struct where required.
Slightly rework apei_claim_sea()'s handling of interrupt masking to fit with the new APIs. Currently, it saves the DAIF flags to restore later and the local_irq ones to check if interrupts are disabled. It then masks AIF to signal the SEA (forcing DAIF masking), and will unmask to IF (re-enabling PMR masking if pseudo-NMIs are in use) if it interrupted a task with interrupts unmaksed, restoring with the DAIF flags from the beginning afterwards.
As we want to preserve a "mask/unmask" process, change the logic from "mask AIF/unmask A/unmask IF" to "mask IF/mask A/unmask A/unmask IF" :
first mask with local_irq_save(), mask/unmask further around the SEA signaling, and restore at the end with local_irq_restore(). Now that both APIs properly work with DAIF and PMR, this works as expected and also removes the need for a "save flags without masking" function for the new helpers, as this was the only user.
Remove the use of GIC_PRIO_PSR_I_SET in kvm/hyp/nvhe/switch.c, as we now check both PMR and DAIF everywhere and we don't need to signal interrupts being masked in DAIF in the PMR anymore.
--- arch/arm64/kernel/acpi.c | 15 +++++++-------- arch/arm64/kernel/hibernate.c | 14 +++++++------- arch/arm64/kernel/machine_kexec.c | 4 ++-- arch/arm64/kernel/setup.c | 4 ++-- arch/arm64/kernel/smp.c | 14 +++++++------- arch/arm64/kernel/suspend.c | 14 +++++++------- arch/arm64/kvm/hyp/nvhe/switch.c | 2 +- arch/arm64/kvm/hyp/vgic-v3-sr.c | 7 ++++--- arch/arm64/kvm/hyp/vhe/switch.c | 14 ++++++++------ arch/arm64/mm/mmu.c | 7 ++++--- 10 files changed, 49 insertions(+), 46 deletions(-)
arch/arm64/kernel/acpi.c +7 -8
@@ -33,7 +33,7 @@#include <acpi/processor.h> #include <asm/cputype.h> #include <asm/cpu_ops.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/smp_plat.h> int acpi_noirq =1 ;/* skip ACPI IRQ initialization */
@@ -391,15 +391,14 @@ int apei_claim_sea(struct pt_regs *regs)int err = -ENOENT ;bool return_to_irqs_enabled;unsigned long current_flags; +arm64_exc_hwstates_t hwstate;if (!IS_ENABLED (CONFIG_ACPI_APEI_GHES ))return err; - current_flags =local_daif_save_flags (); - -/* current_flags isn't useful here as daif doesn't tell us about pNMI */ - return_to_irqs_enabled = !irqs_disabled_flags (arch_local_save_flags ()); +local_irq_save (current_flags); + return_to_irqs_enabled = !irqs_disabled_flags (current_flags);if (regs) return_to_irqs_enabled = !regs_irqs_disabled (regs);
@@ -407,10 +406,11 @@ int apei_claim_sea(struct pt_regs *regs)* SEA can interrupt SError, mask it and describe this as an NMI so * that APEI defers the handling. */ -local_daif_restore (DAIF_ERRCTX ); + hwstate =local_exceptions_save_mask (ERROR_CONTEXT );nmi_enter (); err =ghes_notify_sea ();nmi_exit (); +local_exceptions_restore (hwstate);/* * APEI NMI-like notifications are deferred to irq_work. Unless
@@ -418,7 +418,6 @@ int apei_claim_sea(struct pt_regs *regs)*/ if (!err) {if (return_to_irqs_enabled) { -local_daif_restore (DAIF_PROCCTX_NOIRQ );__irq_enter ();irq_work_run ();__irq_exit ();
@@ -428,7 +427,7 @@ int apei_claim_sea(struct pt_regs *regs) } } -local_daif_restore (current_flags); +local_irq_restore (current_flags);return err; }
arch/arm64/kernel/hibernate.c +7 -7
@@ -20,7 +20,7 @@#include <asm/barrier.h> #include <asm/cacheflush.h> #include <asm/cputype.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/irqflags.h> #include <asm/kexec.h> #include <asm/memory.h>
@@ -332,16 +332,16 @@ static void swsusp_mte_restore_tags(void)int swsusp_arch_suspend (void ) { -int ret =0 ; -unsigned long flags;struct sleep_stack_data state; +arm64_exc_hwstates_t hwstate; +int ret =0 ;if (cpus_are_stuck_in_kernel ()) {pr_err ("Can't hibernate: no mechanism to offline secondary CPUs.\n" );return -EBUSY ; } - flags =local_daif_save (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT );if (__cpu_suspend_enter (&state)) {/* make the crash dump kernel image visible/saveable */
@@ -349,7 +349,7 @@ int swsusp_arch_suspend(void) ret =swsusp_mte_save_tags ();if (ret) { -local_daif_restore (flags); +local_exceptions_restore (hwstate);return ret; }
@@ -393,7 +393,7 @@ int swsusp_arch_suspend(void)spectre_v4_enable_mitigation (NULL ); } -local_daif_restore (flags); +local_exceptions_restore (hwstate);return ret; }
@@ -477,7 +477,7 @@ int __nocfi swsusp_arch_resume(void)* swsusp_arch_resume(), and expects to be re-entered in the * same state : with all DAIF exceptions masked. */ -local_daif_save (); +local_exceptions_save_mask (CRITICAL_CONTEXT );hibernate_exit (virt_to_phys (tmp_pg_dir), resume_hdr.ttbr1_el1, resume_hdr.reenter_kernel, restore_pblist, resume_hdr.__hyp_stub_vectors,virt_to_phys (zero_page));
arch/arm64/kernel/machine_kexec.c +2 -2
@@ -17,7 +17,7 @@#include <asm/cacheflush.h> #include <asm/cpu_ops.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/memory.h> #include <asm/mmu.h> #include <asm/mmu_context.h>
@@ -173,7 +173,7 @@ void machine_kexec(struct kimage *kimage)pr_info ("Bye!\n" ); -local_daif_mask (); +local_exceptions_final_mask ();/* * Both restart and kernel_reloc will shutdown the MMU, disable data
arch/arm64/kernel/setup.c +2 -2
@@ -37,7 +37,7 @@#include <asm/fixmap.h> #include <asm/cpu.h> #include <asm/cputype.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/elf.h> #include <asm/cpufeature.h> #include <asm/cpu_ops.h>
@@ -311,7 +311,7 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)* IRQ and FIQ will be unmasked after the root irqchip has been * detected and initialized. */ -local_daif_restore (DAIF_PROCCTX_NOIRQ ); +local_exceptions_cpu_init_mask (NOIRQ_CONTEXT );/* * TTBR0 is only used for the identity mapping at this stage. Make it
arch/arm64/kernel/smp.c +7 -7
@@ -42,7 +42,7 @@#include <asm/cpu.h> #include <asm/cputype.h> #include <asm/cpu_ops.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/kvm_mmu.h> #include <asm/mmu_context.h> #include <asm/numa.h>
@@ -263,7 +263,7 @@ asmlinkage notrace void secondary_start_kernel(void)* as the root irqchip has already been detected and initialized we can * unmask IRQ and FIQ at the same time. */ -local_daif_restore (DAIF_PROCCTX ); +local_exceptions_cpu_init_mask (PROCESS_CONTEXT );/* * OK, it's off to the idle thread for us
@@ -370,7 +370,7 @@ void __noreturn cpu_die(void)idle_task_exit (); -local_daif_mask (); +local_exceptions_final_mask ();/* Tell cpuhp_bp_sync_dead() that this CPU is now safe to dispose of */ cpuhp_ap_report_dead ();
@@ -870,7 +870,7 @@ static void __noreturn local_cpu_stop(unsigned int cpu) {set_cpu_online (cpu, false); -local_daif_mask (); +local_exceptions_final_mask ();sdei_mask_local_cpu ();cpu_park_loop (); }
@@ -889,14 +889,14 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs {#ifdef CONFIG_KEXEC_CORE /* -* Use local_daif_mask() instead of local_irq_disable() to make sure -* that pseudo-NMIs are disabled. The "crash stop" code starts with +* Use local_exceptions_final_mask() instead of local_irq_disable() to make +* sure that pseudo-NMIs are disabled. The "crash stop" code starts with * an IRQ and falls back to NMI (which might be pseudo). If the IRQ * finally goes through right as we're timing out then the NMI could * interrupt us. It's better to prevent the NMI and let the IRQ * finish since the pt_regs will be better. */ -local_daif_mask (); +local_exceptions_final_mask ();crash_save_cpu (regs, cpu);
arch/arm64/kernel/suspend.c +7 -7
@@ -9,7 +9,7 @@#include <asm/cacheflush.h> #include <asm/cpufeature.h> #include <asm/cpuidle.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/debug-monitors.h> #include <asm/exec.h> #include <asm/fpsimd.h>
@@ -69,7 +69,7 @@ void notrace __cpu_suspend_exit(void)/* * Restore HW breakpoint registers to sane values * before debug exceptions are possibly reenabled -* by cpu_suspend()s local_daif_restore() call. +* by cpu_suspend()s local_exceptions_restore() call. */ if (hw_breakpoint_restore)hw_breakpoint_restore (cpu);
@@ -97,7 +97,7 @@ void notrace __cpu_suspend_exit(void)int cpu_suspend (unsigned long arg,int (*fn)(unsigned long )) {int ret =0 ; -unsigned long flags; +arm64_exc_hwstates_t hwstate;struct sleep_stack_data state;/*
@@ -119,12 +119,12 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))* * Strictly speaking the trace_hardirqs_off() here is superfluous, * hardirqs should be firmly off by now. This really ought to use -* something like raw_local_daif_save(). +* something like raw_local_exceptions_save_mask(). * * This also unmasks interrupts in PMR in order to reliably * resume if we're using pseudo-NMIs. */ - flags =local_daif_save (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT );/* * Function graph tracer state gets inconsistent when the kernel
@@ -158,11 +158,11 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))unpause_graph_tracing ();/* -* Restore pstate flags. OS lock and mdscr have been already +* Restore interrupt masks. OS lock and mdscr have been already * restored, so from this point onwards, debugging is fully * reenabled if it was enabled when core started shutdown. */ -local_daif_restore (flags); +local_exceptions_restore (hwstate);return ret; }
arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
@@ -270,7 +270,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)* Naturally, we want to avoid this. */ if (system_uses_irq_prio_masking ()) { -gic_write_pmr (GIC_PRIO_IRQON |GIC_PRIO_PSR_I_SET ); +gic_write_pmr (GIC_PRIO_IRQON );pmr_sync (); }
arch/arm64/kvm/hyp/vgic-v3-sr.c +4 -3
@@ -10,6 +10,7 @@#include <linux/irqchip/arm-gic-v3.h> #include <linux/kvm_host.h> +#include <asm/interrupts/masking.h> #include <asm/kvm_emulate.h> #include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h>
@@ -445,8 +446,8 @@ void __vgic_v3_init_lrs(void)*/ u64 __vgic_v3_get_gic_config (void ) { +arm64_exc_hwstates_t hwstate;u64 val, sre; -unsigned long flags =0 ;/* * In compat mode, we cannot access ICC_SRE_EL1 at any EL
@@ -476,7 +477,7 @@ u64 __vgic_v3_get_gic_config(void)* of the exception entry to EL2. */ if (has_vhe ()) { - flags =local_daif_save (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT ); }else {sysreg_clear_set_hcr (0 ,HCR_AMO |HCR_FMO |HCR_IMO );isb ();
@@ -491,7 +492,7 @@ u64 __vgic_v3_get_gic_config(void)isb ();if (has_vhe ()) { -local_daif_restore (flags); +local_exceptions_restore (hwstate); }else {sysreg_clear_set_hcr (HCR_AMO |HCR_FMO |HCR_IMO ,0 );isb ();
arch/arm64/kvm/hyp/vhe/switch.c +8 -6
@@ -17,6 +17,7 @@#include <asm/barrier.h> #include <asm/cpufeature.h> +#include <asm/interrupts/masking.h> #include <asm/kprobes.h> #include <asm/kvm_asm.h> #include <asm/kvm_emulate.h>
@@ -629,9 +630,10 @@ NOKPROBE_SYMBOL(__kvm_vcpu_run_vhe);int __kvm_vcpu_run (struct kvm_vcpu *vcpu) { +arm64_exc_hwstates_t hwstate;int ret; -local_daif_mask (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT );/* * Having IRQs masked via PMR when entering the guest means the GIC
@@ -639,18 +641,18 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)* only way to get out will be via guest exceptions. * Naturally, we want to avoid this. * -* local_daif_mask() already sets GIC_PRIO_PSR_I_SET, we just need a -* dsb to ensure the redistributor is forwards EL2 IRQs to the CPU. +* local_exceptions_save_mask(), via helpers, sets +* GIC_PRIO_IRQON along with pmr_sync(), so we can be shure +* that redistributor forwards EL2 IRQs to the CPU. */ -pmr_sync (); ret =__kvm_vcpu_run_vhe (vcpu);/* -* local_daif_restore() takes care to properly restore PSTATE.DAIF +* local_exceptions_restore() takes care to properly restore PSTATE.DAIF * and the GIC PMR if the host is using IRQ priorities. */ -local_daif_restore (DAIF_PROCCTX_NOIRQ ); +local_exceptions_restore (hwstate);return ret; }
arch/arm64/mm/mmu.c +4 -3
@@ -32,6 +32,7 @@#include <asm/barrier.h> #include <asm/cputype.h> +#include <asm/interrupts/masking.h> #include <asm/fixmap.h> #include <asm/kasan.h> #include <asm/kernel-pgtable.h>
@@ -2314,7 +2315,7 @@ void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp)typedef void (ttbr_replace_func )(phys_addr_t );extern ttbr_replace_func idmap_cpu_replace_ttbr1;ttbr_replace_func *replace_phys; -unsigned long daif; +arm64_exc_hwstates_t hwstate;/* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */ phys_addr_t ttbr1 =phys_to_ttbr (virt_to_phys (pgdp));
@@ -2330,9 +2331,9 @@ void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp)* We really don't want to take *any* exceptions while TTBR1 is * in the process of being replaced so mask everything. */ - daif =local_daif_save (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT );replace_phys (ttbr1); -local_daif_restore (daif); +local_exceptions_restore (hwstate);cpu_uninstall_idmap (); }
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 18/45] arm64: replace local_daif helpers
Quoted 13 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
Replace existing
local_daif_...usage with the newlocal_all_irqs...helpers, adding the necessary save-state struct where required.Slightly rework
apei_claim_sea()'s handling of interrupt masking to fit with the new APIs. Currently, it saves the DAIF flags to restore later and thelocal_irqones to check if interrupts are disabled. It then masks AIF to signal the SEA (forcing DAIF masking), and will unmask to IF (re-enabling PMR masking if pseudo-NMIs are in use) if it interrupted a task with interrupts unmaksed, restoring with the DAIF flags from the beginning afterwards.
Quoted 2 lines
As we want to preserve a "mask/unmask" process, change the logic from "mask AIF/unmask A/unmask IF" to "mask IF/mask A/unmask A/unmask IF" :
Quoted 443 lines
first mask with
local_irq_save(), mask/unmask further around the SEA signaling, and restore at the end withlocal_irq_restore(). Now that both APIs properly work with DAIF and PMR, this works as expected and also removes the need for a "save flags without masking" function for the new helpers, as this was the only user.Remove the use of
GIC_PRIO_PSR_I_SETinkvm/hyp/nvhe/switch.c, as we now check both PMR and DAIF everywhere and we don't need to signal interrupts being masked in DAIF in the PMR anymore.Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/acpi.c | 15 +++++++-------- arch/arm64/kernel/hibernate.c | 14 +++++++------- arch/arm64/kernel/machine_kexec.c | 4 ++-- arch/arm64/kernel/setup.c | 4 ++-- arch/arm64/kernel/smp.c | 14 +++++++------- arch/arm64/kernel/suspend.c | 14 +++++++------- arch/arm64/kvm/hyp/nvhe/switch.c | 2 +- arch/arm64/kvm/hyp/vgic-v3-sr.c | 7 ++++--- arch/arm64/kvm/hyp/vhe/switch.c | 14 ++++++++------ arch/arm64/mm/mmu.c | 7 ++++--- 10 files changed, 49 insertions(+), 46 deletions(-)arch/arm64/kernel/acpi.c +7
-8@@ -33,7 +33,7 @@#include <acpi/processor.h> #include <asm/cputype.h> #include <asm/cpu_ops.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/smp_plat.h> int acpi_noirq =1 ;/* skip ACPI IRQ initialization */ @@ -391,15 +391,14 @@ int apei_claim_sea(struct pt_regs *regs)int err = -ENOENT ;bool return_to_irqs_enabled;unsigned long current_flags; +arm64_exc_hwstates_t hwstate;if (!IS_ENABLED (CONFIG_ACPI_APEI_GHES ))return err; - current_flags =local_daif_save_flags (); - -/* current_flags isn't useful here as daif doesn't tell us about pNMI */ - return_to_irqs_enabled = !irqs_disabled_flags (arch_local_save_flags ()); +local_irq_save (current_flags); + return_to_irqs_enabled = !irqs_disabled_flags (current_flags);if (regs) return_to_irqs_enabled = !regs_irqs_disabled (regs);@@ -407,10 +406,11 @@ int apei_claim_sea(struct pt_regs *regs)* SEA can interrupt SError, mask it and describe this as an NMI so * that APEI defers the handling. */ -local_daif_restore (DAIF_ERRCTX ); + hwstate =local_exceptions_save_mask (ERROR_CONTEXT );nmi_enter (); err =ghes_notify_sea ();nmi_exit (); +local_exceptions_restore (hwstate);/* * APEI NMI-like notifications are deferred to irq_work. Unless @@ -418,7 +418,6 @@ int apei_claim_sea(struct pt_regs *regs)*/ if (!err) {if (return_to_irqs_enabled) { -local_daif_restore (DAIF_PROCCTX_NOIRQ );__irq_enter ();irq_work_run ();__irq_exit ();@@ -428,7 +427,7 @@ int apei_claim_sea(struct pt_regs *regs) } } -local_daif_restore (current_flags); +local_irq_restore (current_flags);return err; }arch/arm64/kernel/hibernate.c +7
-7@@ -20,7 +20,7 @@#include <asm/barrier.h> #include <asm/cacheflush.h> #include <asm/cputype.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/irqflags.h> #include <asm/kexec.h> #include <asm/memory.h> @@ -332,16 +332,16 @@ static void swsusp_mte_restore_tags(void)int swsusp_arch_suspend (void ) { -int ret =0 ; -unsigned long flags;struct sleep_stack_data state; +arm64_exc_hwstates_t hwstate; +int ret =0 ;if (cpus_are_stuck_in_kernel ()) {pr_err ("Can't hibernate: no mechanism to offline secondary CPUs.\n" );return -EBUSY ; } - flags =local_daif_save (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT );if (__cpu_suspend_enter (&state)) {/* make the crash dump kernel image visible/saveable */ @@ -349,7 +349,7 @@ int swsusp_arch_suspend(void) ret =swsusp_mte_save_tags ();if (ret) { -local_daif_restore (flags); +local_exceptions_restore (hwstate);return ret; }@@ -393,7 +393,7 @@ int swsusp_arch_suspend(void)spectre_v4_enable_mitigation (NULL ); } -local_daif_restore (flags); +local_exceptions_restore (hwstate);return ret; }@@ -477,7 +477,7 @@ int __nocfi swsusp_arch_resume(void)* swsusp_arch_resume(), and expects to be re-entered in the * same state : with all DAIF exceptions masked. */ -local_daif_save (); +local_exceptions_save_mask (CRITICAL_CONTEXT );hibernate_exit (virt_to_phys (tmp_pg_dir), resume_hdr.ttbr1_el1, resume_hdr.reenter_kernel, restore_pblist, resume_hdr.__hyp_stub_vectors,virt_to_phys (zero_page));arch/arm64/kernel/machine_kexec.c +2
-2@@ -17,7 +17,7 @@#include <asm/cacheflush.h> #include <asm/cpu_ops.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/memory.h> #include <asm/mmu.h> #include <asm/mmu_context.h> @@ -173,7 +173,7 @@ void machine_kexec(struct kimage *kimage)pr_info ("Bye!\n" ); -local_daif_mask (); +local_exceptions_final_mask ();/* * Both restart and kernel_reloc will shutdown the MMU, disable data arch/arm64/kernel/setup.c +2
-2@@ -37,7 +37,7 @@#include <asm/fixmap.h> #include <asm/cpu.h> #include <asm/cputype.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/elf.h> #include <asm/cpufeature.h> #include <asm/cpu_ops.h> @@ -311,7 +311,7 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)* IRQ and FIQ will be unmasked after the root irqchip has been * detected and initialized. */ -local_daif_restore (DAIF_PROCCTX_NOIRQ ); +local_exceptions_cpu_init_mask (NOIRQ_CONTEXT );/* * TTBR0 is only used for the identity mapping at this stage. Make it arch/arm64/kernel/smp.c +7
-7@@ -42,7 +42,7 @@#include <asm/cpu.h> #include <asm/cputype.h> #include <asm/cpu_ops.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/kvm_mmu.h> #include <asm/mmu_context.h> #include <asm/numa.h> @@ -263,7 +263,7 @@ asmlinkage notrace void secondary_start_kernel(void)* as the root irqchip has already been detected and initialized we can * unmask IRQ and FIQ at the same time. */ -local_daif_restore (DAIF_PROCCTX ); +local_exceptions_cpu_init_mask (PROCESS_CONTEXT );/* * OK, it's off to the idle thread for us @@ -370,7 +370,7 @@ void __noreturn cpu_die(void)idle_task_exit (); -local_daif_mask (); +local_exceptions_final_mask ();/* Tell cpuhp_bp_sync_dead() that this CPU is now safe to dispose of */ cpuhp_ap_report_dead ();@@ -870,7 +870,7 @@ static void __noreturn local_cpu_stop(unsigned int cpu) {set_cpu_online (cpu, false); -local_daif_mask (); +local_exceptions_final_mask ();sdei_mask_local_cpu ();cpu_park_loop (); }@@ -889,14 +889,14 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs {#ifdef CONFIG_KEXEC_CORE /* -* Use local_daif_mask() instead of local_irq_disable() to make sure -* that pseudo-NMIs are disabled. The "crash stop" code starts with +* Use local_exceptions_final_mask() instead of local_irq_disable() to make +* sure that pseudo-NMIs are disabled. The "crash stop" code starts with * an IRQ and falls back to NMI (which might be pseudo). If the IRQ * finally goes through right as we're timing out then the NMI could * interrupt us. It's better to prevent the NMI and let the IRQ * finish since the pt_regs will be better. */ -local_daif_mask (); +local_exceptions_final_mask ();crash_save_cpu (regs, cpu);arch/arm64/kernel/suspend.c +7
-7@@ -9,7 +9,7 @@#include <asm/cacheflush.h> #include <asm/cpufeature.h> #include <asm/cpuidle.h> -#include <asm/daifflags.h> +#include <asm/interrupts/masking.h> #include <asm/debug-monitors.h> #include <asm/exec.h> #include <asm/fpsimd.h> @@ -69,7 +69,7 @@ void notrace __cpu_suspend_exit(void)/* * Restore HW breakpoint registers to sane values * before debug exceptions are possibly reenabled -* by cpu_suspend()s local_daif_restore() call. +* by cpu_suspend()s local_exceptions_restore() call. */ if (hw_breakpoint_restore)hw_breakpoint_restore (cpu);@@ -97,7 +97,7 @@ void notrace __cpu_suspend_exit(void)int cpu_suspend (unsigned long arg,int (*fn)(unsigned long )) {int ret =0 ; -unsigned long flags; +arm64_exc_hwstates_t hwstate;struct sleep_stack_data state;/* @@ -119,12 +119,12 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))* * Strictly speaking the trace_hardirqs_off() here is superfluous, * hardirqs should be firmly off by now. This really ought to use -* something like raw_local_daif_save(). +* something like raw_local_exceptions_save_mask(). * * This also unmasks interrupts in PMR in order to reliably * resume if we're using pseudo-NMIs. */ - flags =local_daif_save (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT );/* * Function graph tracer state gets inconsistent when the kernel @@ -158,11 +158,11 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))unpause_graph_tracing ();/* -* Restore pstate flags. OS lock and mdscr have been already +* Restore interrupt masks. OS lock and mdscr have been already * restored, so from this point onwards, debugging is fully * reenabled if it was enabled when core started shutdown. */ -local_daif_restore (flags); +local_exceptions_restore (hwstate);return ret; }arch/arm64/kvm/hyp/nvhe/switch.c +1
-1@@ -270,7 +270,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)* Naturally, we want to avoid this. */ if (system_uses_irq_prio_masking ()) { -gic_write_pmr (GIC_PRIO_IRQON |GIC_PRIO_PSR_I_SET ); +gic_write_pmr (GIC_PRIO_IRQON );pmr_sync (); }arch/arm64/kvm/hyp/vgic-v3-sr.c +4
-3@@ -10,6 +10,7 @@#include <linux/irqchip/arm-gic-v3.h> #include <linux/kvm_host.h> +#include <asm/interrupts/masking.h> #include <asm/kvm_emulate.h> #include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h> @@ -445,8 +446,8 @@ void __vgic_v3_init_lrs(void)*/ u64 __vgic_v3_get_gic_config (void ) { +arm64_exc_hwstates_t hwstate;u64 val, sre; -unsigned long flags =0 ;/* * In compat mode, we cannot access ICC_SRE_EL1 at any EL @@ -476,7 +477,7 @@ u64 __vgic_v3_get_gic_config(void)* of the exception entry to EL2. */ if (has_vhe ()) { - flags =local_daif_save (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT ); }else {sysreg_clear_set_hcr (0 ,HCR_AMO |HCR_FMO |HCR_IMO );isb ();@@ -491,7 +492,7 @@ u64 __vgic_v3_get_gic_config(void)isb ();if (has_vhe ()) { -local_daif_restore (flags); +local_exceptions_restore (hwstate); }else {sysreg_clear_set_hcr (HCR_AMO |HCR_FMO |HCR_IMO ,0 );isb ();arch/arm64/kvm/hyp/vhe/switch.c +8
-6@@ -17,6 +17,7 @@#include <asm/barrier.h> #include <asm/cpufeature.h> +#include <asm/interrupts/masking.h> #include <asm/kprobes.h> #include <asm/kvm_asm.h> #include <asm/kvm_emulate.h> @@ -629,9 +630,10 @@ NOKPROBE_SYMBOL(__kvm_vcpu_run_vhe);int __kvm_vcpu_run (struct kvm_vcpu *vcpu) { +arm64_exc_hwstates_t hwstate;int ret; -local_daif_mask (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT );/* * Having IRQs masked via PMR when entering the guest means the GIC @@ -639,18 +641,18 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)* only way to get out will be via guest exceptions. * Naturally, we want to avoid this. * -* local_daif_mask() already sets GIC_PRIO_PSR_I_SET, we just need a -* dsb to ensure the redistributor is forwards EL2 IRQs to the CPU. +* local_exceptions_save_mask(), via helpers, sets +* GIC_PRIO_IRQON along with pmr_sync(), so we can be shure +* that redistributor forwards EL2 IRQs to the CPU. */ -pmr_sync (); ret =__kvm_vcpu_run_vhe (vcpu);/* -* local_daif_restore() takes care to properly restore PSTATE.DAIF +* local_exceptions_restore() takes care to properly restore PSTATE.DAIF * and the GIC PMR if the host is using IRQ priorities. */ -local_daif_restore (DAIF_PROCCTX_NOIRQ ); +local_exceptions_restore (hwstate);return ret; }arch/arm64/mm/mmu.c +4
-3@@ -32,6 +32,7 @@#include <asm/barrier.h> #include <asm/cputype.h> +#include <asm/interrupts/masking.h> #include <asm/fixmap.h> #include <asm/kasan.h> #include <asm/kernel-pgtable.h> @@ -2314,7 +2315,7 @@ void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp)typedef void (ttbr_replace_func )(phys_addr_t );extern ttbr_replace_func idmap_cpu_replace_ttbr1;ttbr_replace_func *replace_phys; -unsigned long daif; +arm64_exc_hwstates_t hwstate;/* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */ phys_addr_t ttbr1 =phys_to_ttbr (virt_to_phys (pgdp));@@ -2330,9 +2331,9 @@ void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp)* We really don't want to take *any* exceptions while TTBR1 is * in the process of being replaced so mask everything. */ - daif =local_daif_save (); + hwstate =local_exceptions_save_mask (CRITICAL_CONTEXT );replace_phys (ttbr1); -local_daif_restore (daif); +local_exceptions_restore (hwstate);cpu_uninstall_idmap (); }
RFC PATCH v2 19/45 arm64: cpuidle: use new helpers to bypass interrupt priority masking
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
When the CPU goes into WFI and pseudo-NMIs are in use, we need to make sure that interrupts are not masked by the PMR otherwise they would not reach the CPU and wake it up.
Now that we have a proper API that handles both DAIF and PMR, extend it to provide helpers allowing to temporarily switch to masking interrupts via DAIF only when interrupt priorities are in use.
This could allow other parts of the code to make use of it and makes it easier to check for proper use.
Replace the custom cpuidle helper with those new helpers.
--- arch/arm64/include/asm/cpuidle.h | 18 ++---- arch/arm64/include/asm/interrupts/masking.h | 61 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 14 deletions(-)
arch/arm64/include/asm/cpuidle.h +4 -14
@@ -5,32 +5,22 @@#include <asm/proc-fns.h> #ifdef CONFIG_ARM64_PSEUDO_NMI -#include <asm/arch_gicv3.h> +#include <asm/interrupts/masking.h> struct arm_cpuidle_irq_context { -unsigned long pmr; -unsigned long daif_bits; +arm64_exc_hwstates_t arm64_context; };#define arm_cpuidle_save_irq_context (__c) \ do { \ struct arm_cpuidle_irq_context *c = __c; \ - if (system_uses_irq_prio_masking()) { \ - c->daif_bits = read_sysreg(daif); \ - write_sysreg(c->daif_bits | PSR_I_BIT | PSR_F_BIT, \ - daif); \ - c->pmr = gic_read_pmr(); \ - gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); \ - } \ + c->arm64_context = local_exceptions_force_daif_save(); \ } while (0)#define arm_cpuidle_restore_irq_context (__c) \ do { \ struct arm_cpuidle_irq_context *c = __c; \ - if (system_uses_irq_prio_masking()) { \ - gic_write_pmr(c->pmr); \ - write_sysreg(c->daif_bits, daif); \ - } \ + local_exceptions_force_daif_restore(c->arm64_context); \ } while (0)#else struct arm_cpuidle_irq_context { };
arch/arm64/include/asm/interrupts/masking.h +61 -0
67 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 19/45] arm64: cpuidle: use new helpers to bypass interrupt priority masking
Quoted 84 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
When the CPU goes into WFI and pseudo-NMIs are in use, we need to make sure that interrupts are not masked by the PMR otherwise they would not reach the CPU and wake it up.
Now that we have a proper API that handles both DAIF and PMR, extend it to provide helpers allowing to temporarily switch to masking interrupts via DAIF only when interrupt priorities are in use.
This could allow other parts of the code to make use of it and makes it easier to check for proper use.Replace the custom cpuidle helper with those new helpers.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/cpuidle.h | 18 ++---- arch/arm64/include/asm/interrupts/masking.h | 61 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 14 deletions(-)arch/arm64/include/asm/cpuidle.h +4
-14@@ -5,32 +5,22 @@#include <asm/proc-fns.h> #ifdef CONFIG_ARM64_PSEUDO_NMI -#include <asm/arch_gicv3.h> +#include <asm/interrupts/masking.h> struct arm_cpuidle_irq_context { -unsigned long pmr; -unsigned long daif_bits; +arm64_exc_hwstates_t arm64_context; };#define arm_cpuidle_save_irq_context (__c) \ do { \ struct arm_cpuidle_irq_context *c = __c; \ - if (system_uses_irq_prio_masking()) { \ - c->daif_bits = read_sysreg(daif); \ - write_sysreg(c->daif_bits | PSR_I_BIT | PSR_F_BIT, \ - daif); \ - c->pmr = gic_read_pmr(); \ - gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); \ - } \ + c->arm64_context = local_exceptions_force_daif_save(); \ } while (0)#define arm_cpuidle_restore_irq_context (__c) \ do { \ struct arm_cpuidle_irq_context *c = __c; \ - if (system_uses_irq_prio_masking()) { \ - gic_write_pmr(c->pmr); \ - write_sysreg(c->daif_bits, daif); \ - } \ + local_exceptions_force_daif_restore(c->arm64_context); \ } while (0)#else struct arm_cpuidle_irq_context { };arch/arm64/include/asm/interrupts/masking.h +14
-0@@ -89,6 +89,67 @@ static inline void local_exceptions_final_mask(void) }#endif /* CONFIG_DEBUG_IRQFLAGS */ +/* +* In some cases, WFI or guest entry for example, we always want interrupts +* to reach the CPU even if masked. Masking via the PMR prevents them from +* reaching the CPU and waking it up. +* Force IRQ masking using DAIF by raising the priority mask +* and setting the IF flags. +* +* Should only be called when IRQs are already masked. +*/ +static inline arm64_exc_hwstates_t local_exceptions_force_daif_save (void ) +{ +arm64_exc_hwstates_t states = {}; + +if (!system_uses_irq_prio_masking ())
arch/arm64/include/asm/interrupts/masking.h static inline void local_exceptions_final_mask(void) · line 115
Quoted 10 lines
arch/arm64/include/asm/interrupts/masking.h +10
-0+return states; + +/* +* Cannot use lockdep_assert here as idle entry enables hardirqs +* while keeping interrupts masked. +*/ +WARN_ON_ONCE (!irqs_disabled ()); + + states.saved.daif =read_sysreg (daif); + states.saved.pmr =read_sysreg_s (SYS_ICC_PMR_EL1 );
I'd suggest using gic_read_pmr() as it aligns better with the original implementation.
arch/arm64/include/asm/interrupts/masking.h static inline void local_exceptions_final_mask(void) · line 122
Quoted 7 lines
arch/arm64/include/asm/interrupts/masking.h +7
-0+/* +* We might have IF set or unset. In case IF already set orring with IF +* won't change anything, combined with PMR set to IRQON we might become +* one of contexts: CRITICAL, ERROR or NONMI. In case IF is unset (which +* also implies that DA is unset) orring with IF and combining with PMR +* set to IRQON would effectively gives us NONMI context. +*/
The comment seems redundant to me, because __arm64_update_exc_hwstate() only needs the hwstate parameter and has no dependency on the context type.
Otherwise, LGTM
Quoted 33 lines
arch/arm64/include/asm/interrupts/masking.h +30
-0+ states.expected.daif = states.saved.daif |DAIF_PROCCTX_NOIRQ ; + states.expected.pmr =GIC_PRIO_IRQON ; + +arm64_update_exc_hwstate (states.expected); + +return states; +} + +/* +* Return to masking with the PMR, restoring previously saved DAIF and PMR. +* +* IRQs or interrupt priority masking should not have been re-enabled in between +* the save and restore. +*/ +static inline +void local_exceptions_force_daif_restore (arm64_exc_hwstates_t states) +{ +if (!system_uses_irq_prio_masking ()) +return ; + +/* +* Cannot use lockdep_assert here as idle entry enables hardirqs +* while keeping interrupts masked. +*/ +WARN_ON_ONCE (!irqs_disabled ()); + +arm64_debug_exc_hwstate (states.expected); +arm64_update_exc_hwstate (states.saved); +} +/* * During early boot, we unmask PSR.DA before the GIC has been set up. * If we use IRQ priority masking, the PMR and PSR will be out of sync
Re: [RFC PATCH v2 19/45] arm64: cpuidle: use new helpers to bypass interrupt priority masking
Quoted 90 lines · Jinjie Ruan
Quoted 88 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
When the CPU goes into WFI and pseudo-NMIs are in use, we need to make sure that interrupts are not masked by the PMR otherwise they would not reach the CPU and wake it up.
Now that we have a proper API that handles both DAIF and PMR, extend it to provide helpers allowing to temporarily switch to masking interrupts via DAIF only when interrupt priorities are in use.
This could allow other parts of the code to make use of it and makes it easier to check for proper use.Replace the custom cpuidle helper with those new helpers.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/cpuidle.h | 18 ++---- arch/arm64/include/asm/interrupts/masking.h | 61 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 14 deletions(-)arch/arm64/include/asm/cpuidle.h +4
-14@@ -5,32 +5,22 @@#include <asm/proc-fns.h> #ifdef CONFIG_ARM64_PSEUDO_NMI -#include <asm/arch_gicv3.h> +#include <asm/interrupts/masking.h> struct arm_cpuidle_irq_context { -unsigned long pmr; -unsigned long daif_bits; +arm64_exc_hwstates_t arm64_context; };#define arm_cpuidle_save_irq_context (__c) \ do { \ struct arm_cpuidle_irq_context *c = __c; \ - if (system_uses_irq_prio_masking()) { \ - c->daif_bits = read_sysreg(daif); \ - write_sysreg(c->daif_bits | PSR_I_BIT | PSR_F_BIT, \ - daif); \ - c->pmr = gic_read_pmr(); \ - gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); \ - } \ + c->arm64_context = local_exceptions_force_daif_save(); \ } while (0)#define arm_cpuidle_restore_irq_context (__c) \ do { \ struct arm_cpuidle_irq_context *c = __c; \ - if (system_uses_irq_prio_masking()) { \ - gic_write_pmr(c->pmr); \ - write_sysreg(c->daif_bits, daif); \ - } \ + local_exceptions_force_daif_restore(c->arm64_context); \ } while (0)#else struct arm_cpuidle_irq_context { };arch/arm64/include/asm/interrupts/masking.h +18
-0@@ -89,6 +89,67 @@ static inline void local_exceptions_final_mask(void) }#endif /* CONFIG_DEBUG_IRQFLAGS */ +/* +* In some cases, WFI or guest entry for example, we always want interrupts +* to reach the CPU even if masked. Masking via the PMR prevents them from +* reaching the CPU and waking it up. +* Force IRQ masking using DAIF by raising the priority mask +* and setting the IF flags. +* +* Should only be called when IRQs are already masked. +*/ +static inline arm64_exc_hwstates_t local_exceptions_force_daif_save (void ) +{ +arm64_exc_hwstates_t states = {}; + +if (!system_uses_irq_prio_masking ()) +return states; + +/* +* Cannot use lockdep_assert here as idle entry enables hardirqs
Quoted 9 lines
arch/arm64/include/asm/interrupts/masking.h static inline void local_exceptions_final_mask(void) · line 115
Quoted 6 lines
arch/arm64/include/asm/interrupts/masking.h +6
-0+* while keeping interrupts masked. +*/ +WARN_ON_ONCE (!irqs_disabled ()); + + states.saved.daif =read_sysreg (daif); + states.saved.pmr =read_sysreg_s (SYS_ICC_PMR_EL1 );I'd suggest using
gic_read_pmr()as it aligns better with the original implementation.
IIUC, the reason gic_read_pmr() exists is to be used in code shared between arm64 and arm32. Since we are entirely in arm64, IMO, we better use sysreg directly.
Quoted 10 lines
arch/arm64/include/asm/interrupts/masking.h static inline void local_exceptions_final_mask(void) · line 122
Quoted 7 lines
arch/arm64/include/asm/interrupts/masking.h +7
-0+/* +* We might have IF set or unset. In case IF already set orring with IF +* won't change anything, combined with PMR set to IRQON we might become +* one of contexts: CRITICAL, ERROR or NONMI. In case IF is unset (which +* also implies that DA is unset) orring with IF and combining with PMR +* set to IRQON would effectively gives us NONMI context. +*/ The comment seems redundant to me, because
__arm64_update_exc_hwstate()only needs the hwstate parameter and has no dependency on the context type.
Ok. I'll drop it in the next version.
Quoted 3 lines
Otherwise, LGTM
Reviewed-by Jinjie Ruan <[email protected]>
Cheers
Vladimir
Re: [RFC PATCH v2 19/45] arm64: cpuidle: use new helpers to bypass interrupt priority masking
Quoted 101 lines · Vladimir Murzin
Quoted 90 lines · Jinjie Ruan
Quoted 88 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
When the CPU goes into WFI and pseudo-NMIs are in use, we need to make sure that interrupts are not masked by the PMR otherwise they would not reach the CPU and wake it up.
Now that we have a proper API that handles both DAIF and PMR, extend it to provide helpers allowing to temporarily switch to masking interrupts via DAIF only when interrupt priorities are in use.
This could allow other parts of the code to make use of it and makes it easier to check for proper use.Replace the custom cpuidle helper with those new helpers.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/cpuidle.h | 18 ++---- arch/arm64/include/asm/interrupts/masking.h | 61 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 14 deletions(-)arch/arm64/include/asm/cpuidle.h +4
-14@@ -5,32 +5,22 @@#include <asm/proc-fns.h> #ifdef CONFIG_ARM64_PSEUDO_NMI -#include <asm/arch_gicv3.h> +#include <asm/interrupts/masking.h> struct arm_cpuidle_irq_context { -unsigned long pmr; -unsigned long daif_bits; +arm64_exc_hwstates_t arm64_context; };#define arm_cpuidle_save_irq_context (__c) \ do { \ struct arm_cpuidle_irq_context *c = __c; \ - if (system_uses_irq_prio_masking()) { \ - c->daif_bits = read_sysreg(daif); \ - write_sysreg(c->daif_bits | PSR_I_BIT | PSR_F_BIT, \ - daif); \ - c->pmr = gic_read_pmr(); \ - gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); \ - } \ + c->arm64_context = local_exceptions_force_daif_save(); \ } while (0)#define arm_cpuidle_restore_irq_context (__c) \ do { \ struct arm_cpuidle_irq_context *c = __c; \ - if (system_uses_irq_prio_masking()) { \ - gic_write_pmr(c->pmr); \ - write_sysreg(c->daif_bits, daif); \ - } \ + local_exceptions_force_daif_restore(c->arm64_context); \ } while (0)#else struct arm_cpuidle_irq_context { };arch/arm64/include/asm/interrupts/masking.h +18
-0@@ -89,6 +89,67 @@ static inline void local_exceptions_final_mask(void) }#endif /* CONFIG_DEBUG_IRQFLAGS */ +/* +* In some cases, WFI or guest entry for example, we always want interrupts +* to reach the CPU even if masked. Masking via the PMR prevents them from +* reaching the CPU and waking it up. +* Force IRQ masking using DAIF by raising the priority mask +* and setting the IF flags. +* +* Should only be called when IRQs are already masked. +*/ +static inline arm64_exc_hwstates_t local_exceptions_force_daif_save (void ) +{ +arm64_exc_hwstates_t states = {}; + +if (!system_uses_irq_prio_masking ()) +return states; + +/* +* Cannot use lockdep_assert here as idle entry enables hardirqs Quoted 9 lines
arch/arm64/include/asm/interrupts/masking.h static inline void local_exceptions_final_mask(void) · line 115
Quoted 6 lines
arch/arm64/include/asm/interrupts/masking.h +6
-0+* while keeping interrupts masked. +*/ +WARN_ON_ONCE (!irqs_disabled ()); + + states.saved.daif =read_sysreg (daif); + states.saved.pmr =read_sysreg_s (SYS_ICC_PMR_EL1 );I'd suggest using
gic_read_pmr()as it aligns better with the original implementation.
Quoted 3 lines
IIUC, the reason
gic_read_pmr()exists is to be used in code shared between arm64 and arm32. Since we are entirely in arm64, IMO, we better use sysreg directly.
Make sense.
Quoted 22 lines
Quoted 11 lines
arch/arm64/include/asm/interrupts/masking.h static inline void local_exceptions_final_mask(void) · line 122
Quoted 8 lines
arch/arm64/include/asm/interrupts/masking.h +7
-0+/* +* We might have IF set or unset. In case IF already set orring with IF +* won't change anything, combined with PMR set to IRQON we might become +* one of contexts: CRITICAL, ERROR or NONMI. In case IF is unset (which +* also implies that DA is unset) orring with IF and combining with PMR +* set to IRQON would effectively gives us NONMI context. +*/ The comment seems redundant to me, because
__arm64_update_exc_hwstate()only needs the hwstate parameter and has no dependency on the context type.Ok. I'll drop it in the next version.
Quoted 3 lines
Otherwise, LGTM
Reviewed-by Jinjie Ruan <[email protected]>Cheers
Vladimir
RFC PATCH v2 20/45 arm64: remove daifflags.h
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Interrupt masking is now split in different APIs and local_daif_... functions are not in use anymore.
The DAIF_... masks defines are now in asm/interrupts/common_flags.h, so we can safely remove daifflags.h and all #includes referring to it.
Only kprobes.c uses DAIF_... masks defines, so add the new header there.
--- arch/arm64/include/asm/daifflags.h | 144 -------------------------- arch/arm64/include/asm/entry-common.h | 1 - arch/arm64/include/asm/kvm_host.h | 1 - arch/arm64/include/asm/mmu_context.h | 1 - arch/arm64/kernel/debug-monitors.c | 1 - arch/arm64/kernel/probes/kprobes.c | 2 +- arch/arm64/kernel/signal.c | 1 - arch/arm64/kernel/traps.c | 1 - arch/arm64/mm/fault.c | 1 - 9 files changed, 1 insertion(+), 152 deletions(-) delete mode 100644 arch/arm64/include/asm/daifflags.h
arch/arm64/include/asm/daifflags.h deleted +0 -144
@@ -1,144 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* -* Copyright (C) 2017 ARM Ltd. -*/ -#ifndef __ASM_DAIFFLAGS_H -#define __ASM_DAIFFLAGS_H - -#include <linux/irqflags.h> - -#include <asm/arch_gicv3.h> -#include <asm/barrier.h> -#include <asm/cpufeature.h> -#include <asm/ptrace.h> - -#define DAIF_PROCCTX 0 -#define DAIF_PROCCTX_NOIRQ (PSR_I_BIT | PSR_F_BIT) -#define DAIF_ERRCTX (PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) -#define DAIF_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) - - -/* mask/save/unmask/restore all exceptions, including interrupts. */ -static __always_inline void local_daif_mask(void) -{ -WARN_ON (system_has_prio_mask_debugging () && - (read_sysreg_s (SYS_ICC_PMR_EL1 ) == (GIC_PRIO_IRQOFF | -GIC_PRIO_PSR_I_SET ))); - - asmvolatile ( -"msr daifset, #0xf // local_daif_mask\n" - : - : - :"memory" ); - -/* Don't really care for a dsb here, we don't intend to enable IRQs */ -if (system_uses_irq_prio_masking ()) -gic_write_pmr (GIC_PRIO_IRQON |GIC_PRIO_PSR_I_SET ); - -trace_hardirqs_off (); -} - -static __always_inline unsigned long local_daif_save_flags (void ) -{ -unsigned long flags; - - flags =read_sysreg (daif); - -if (system_uses_irq_prio_masking ()) { -/* If IRQs are masked with PMR, reflect it in the flags */ -if (read_sysreg_s (SYS_ICC_PMR_EL1 ) !=GIC_PRIO_IRQON ) - flags |=PSR_I_BIT |PSR_F_BIT ; - } - -return flags; -} - -static __always_inline unsigned long local_daif_save (void ) -{ -unsigned long flags; - - flags =local_daif_save_flags (); - -local_daif_mask (); - -return flags; -} - -static __always_inline void local_daif_restore(unsigned long flags) -{ -bool irq_disabled = flags &PSR_I_BIT ; - -WARN_ON (system_has_prio_mask_debugging () && - (read_sysreg (daif) & (PSR_I_BIT |PSR_F_BIT )) != (PSR_I_BIT |PSR_F_BIT )); - -if (!irq_disabled) { -trace_hardirqs_on (); - -if (system_uses_irq_prio_masking ()) { -gic_write_pmr (GIC_PRIO_IRQON ); -pmr_sync (); - } - }else if (system_uses_irq_prio_masking ()) { -u64 pmr; - -if (!(flags &PSR_A_BIT )) { -/* -* If interrupts are disabled but we can take -* asynchronous errors, we can take NMIs -*/ - flags &= ~(PSR_I_BIT |PSR_F_BIT ); - pmr =GIC_PRIO_IRQOFF ; - }else { - pmr =GIC_PRIO_IRQON |GIC_PRIO_PSR_I_SET ; - } - -/* -* There has been concern that the write to daif -* might be reordered before this write to PMR. -* From the ARM ARM DDI 0487D.a, section D1.7.1 -* "Accessing PSTATE fields": -* Writes to the PSTATE fields have side-effects on -* various aspects of the PE operation. All of these -* side-effects are guaranteed: -* - Not to be visible to earlier instructions in -* the execution stream. -* - To be visible to later instructions in the -* execution stream -* -* Also, writes to PMR are self-synchronizing, so no -* interrupts with a lower priority than PMR is signaled -* to the PE after the write. -* -* So we don't need additional synchronization here. -*/ -gic_write_pmr (pmr); - } - -write_sysreg (flags, daif); - -if (irq_disabled) -trace_hardirqs_off (); -} - -/* -* Called by synchronous exception handlers to restore the DAIF bits that were -* modified by taking an exception. -*/ -static __always_inline void local_daif_inherit(struct pt_regs *regs) -{ -unsigned long flags = regs->pstate &DAIF_MASK ; - -if (!regs_irqs_disabled (regs)) -trace_hardirqs_on (); - -if (system_uses_irq_prio_masking ()) -gic_write_pmr (regs->pmr); - -/* -* We can't use local_daif_restore(regs->pstate) here as -* system_has_prio_mask_debugging() won't restore the I bit if it can -* use the pmr instead. -*/ -write_sysreg (flags, daif); -} -#endif
arch/arm64/include/asm/entry-common.h +0 -1
@@ -6,7 +6,6 @@#include <linux/thread_info.h> #include <asm/cpufeature.h> -#include <asm/daifflags.h> #include <asm/fpsimd.h> #include <asm/mte.h> #include <asm/stacktrace.h>
arch/arm64/include/asm/kvm_host.h +0 -1
@@ -23,7 +23,6 @@#include <asm/barrier.h> #include <asm/cpufeature.h> #include <asm/cputype.h> -#include <asm/daifflags.h> #include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h>
arch/arm64/include/asm/mmu_context.h +0 -1
@@ -19,7 +19,6 @@#include <asm/cacheflush.h> #include <asm/cpufeature.h> -#include <asm/daifflags.h> #include <asm/gcs.h> #include <asm/proc-fns.h> #include <asm/cputype.h>
arch/arm64/kernel/debug-monitors.c +0 -1
@@ -19,7 +19,6 @@#include <asm/cpufeature.h> #include <asm/cputype.h> -#include <asm/daifflags.h> #include <asm/debug-monitors.h> #include <asm/exception.h> #include <asm/kgdb.h>
arch/arm64/kernel/probes/kprobes.c +1 -1
@@ -24,9 +24,9 @@#include <linux/vmalloc.h> #include <asm/cacheflush.h> -#include <asm/daifflags.h> #include <asm/debug-monitors.h> #include <asm/insn.h> +#include <asm/interrupts/common_flags.h> #include <asm/irq.h> #include <asm/text-patching.h> #include <asm/ptrace.h>
arch/arm64/kernel/signal.c +0 -1
@@ -22,7 +22,6 @@#include <linux/syscalls.h> #include <linux/pkeys.h> -#include <asm/daifflags.h> #include <asm/debug-monitors.h> #include <asm/elf.h> #include <asm/exception.h>
arch/arm64/kernel/traps.c +0 -1
@@ -33,7 +33,6 @@#include <asm/atomic.h> #include <asm/bug.h> #include <asm/cpufeature.h> -#include <asm/daifflags.h> #include <asm/debug-monitors.h> #include <asm/efi.h> #include <asm/esr.h>
arch/arm64/mm/fault.c +0 -1
@@ -34,7 +34,6 @@#include <asm/cpufeature.h> #include <asm/efi.h> #include <asm/exception.h> -#include <asm/daifflags.h> #include <asm/debug-monitors.h> #include <asm/esr.h> #include <asm/kprobes.h>
Signature · 1 line
2.34.1
RFC PATCH v2 21/45 arm64: gicv3: remove GIC_PRIO_PSR_I_SET
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
GIC_PRIO_PSR_I_SET was used to keep track of sections of the kernel where interrupts always need to reach the CPU and could not be masked via interrupt priorities, or DAIF bypassed the status of the PMR.
This flag unmasked IRQs when OR'd to GIC_PRIO_IRQON and ensured that that IRQs were seen as disabled by the local_irqs_... functions, as they would only check the PMR if pseudo-NMIs were enabled.
This is not needed anymore as our IRQ masking APIs now know how to handle interrupt priorities properly and in parallel with DAIF : remove it.
--- arch/arm64/include/asm/ptrace.h | 2 -- include/linux/irqchip/arm-gic-v3-prio.h | 8 -------- 2 files changed, 10 deletions(-)
arch/arm64/include/asm/ptrace.h +0 -2
@@ -24,8 +24,6 @@#define GIC_PRIO_IRQON GICV3_PRIO_UNMASKED#define GIC_PRIO_IRQOFF GICV3_PRIO_IRQ -#define GIC_PRIO_PSR_I_SET GICV3_PRIO_PSR_I_SET -/* Additional SPSR bits not exposed in the UABI */ #define PSR_MODE_THREAD_BIT (1 << 0)#define PSR_IL_BIT (1 << 20)
include/linux/irqchip/arm-gic-v3-prio.h +0 -8
@@ -20,17 +20,11 @@* GICV3_PRIO_NMI is the PMR view of the priority of pseudo-NMIs. This can be * written to the PMR to mask pseudo-NMIs. * -* On arm64 some code sections either automatically switch back to PSR.I or -* explicitly require to not use priority masking. If bit GICV3_PRIO_PSR_I_SET -* is included in the priority mask, it indicates that PSR.I should be set and -* interrupt disabling temporarily does not rely on IRQ priorities. */ #define GICV3_PRIO_UNMASKED 0xe0#define GICV3_PRIO_IRQ 0xc0#define GICV3_PRIO_NMI 0x80 -#define GICV3_PRIO_PSR_I_SET (1 << 4) -#ifndef __ASSEMBLER__#define __gicv3_prio_to_ns (p) (0xff & ((p) << 1))
@@ -45,8 +39,6 @@ static_assert(__gicv3_prio_valid_ns(GICV3_PRIO_IRQ));static_assert (GICV3_PRIO_NMI <GICV3_PRIO_IRQ );static_assert (GICV3_PRIO_IRQ <GICV3_PRIO_UNMASKED ); -static_assert (GICV3_PRIO_IRQ < (GICV3_PRIO_IRQ |GICV3_PRIO_PSR_I_SET )); -#endif /* __ASSEMBLER */ #endif /* __LINUX_IRQCHIP_ARM_GIC_V3_PRIO_H */
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 21/45] arm64: gicv3: remove GIC_PRIO_PSR_I_SET
Quoted 10 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
GIC_PRIO_PSR_I_SETwas used to keep track of sections of the kernel where interrupts always need to reach the CPU and could not be masked via interrupt priorities, or DAIF bypassed the status of the PMR.This flag unmasked IRQs when OR'd to
GIC_PRIO_IRQONand ensured that that IRQs were seen as disabled by thelocal_irqs_...functions, as they would only check the PMR if pseudo-NMIs were enabled.
Quoted 5 lines
This is not needed anymore as our IRQ masking APIs now know how to handle interrupt priorities properly and in parallel with DAIF : remove it.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>
It seems that the reviewed-by for v1 is missing.
Quoted 49 lines
--- arch/arm64/include/asm/ptrace.h | 2 -- include/linux/irqchip/arm-gic-v3-prio.h | 8 -------- 2 files changed, 10 deletions(-)arch/arm64/include/asm/ptrace.h +0
-2@@ -24,8 +24,6 @@#define GIC_PRIO_IRQON GICV3_PRIO_UNMASKED#define GIC_PRIO_IRQOFF GICV3_PRIO_IRQ -#define GIC_PRIO_PSR_I_SET GICV3_PRIO_PSR_I_SET -/* Additional SPSR bits not exposed in the UABI */ #define PSR_MODE_THREAD_BIT (1 << 0)#define PSR_IL_BIT (1 << 20)include/linux/irqchip/arm-gic-v3-prio.h +0
-8@@ -20,17 +20,11 @@* GICV3_PRIO_NMI is the PMR view of the priority of pseudo-NMIs. This can be * written to the PMR to mask pseudo-NMIs. * -* On arm64 some code sections either automatically switch back to PSR.I or -* explicitly require to not use priority masking. If bit GICV3_PRIO_PSR_I_SET -* is included in the priority mask, it indicates that PSR.I should be set and -* interrupt disabling temporarily does not rely on IRQ priorities. */ #define GICV3_PRIO_UNMASKED 0xe0#define GICV3_PRIO_IRQ 0xc0#define GICV3_PRIO_NMI 0x80 -#define GICV3_PRIO_PSR_I_SET (1 << 4) -#ifndef __ASSEMBLER__#define __gicv3_prio_to_ns (p) (0xff & ((p) << 1))@@ -45,8 +39,6 @@ static_assert(__gicv3_prio_valid_ns(GICV3_PRIO_IRQ));static_assert (GICV3_PRIO_NMI <GICV3_PRIO_IRQ );static_assert (GICV3_PRIO_IRQ <GICV3_PRIO_UNMASKED ); -static_assert (GICV3_PRIO_IRQ < (GICV3_PRIO_IRQ |GICV3_PRIO_PSR_I_SET )); -#endif /* __ASSEMBLER */ #endif /* __LINUX_IRQCHIP_ARM_GIC_V3_PRIO_H */
Re: [RFC PATCH v2 21/45] arm64: gicv3: remove GIC_PRIO_PSR_I_SET
Quoted 11 lines · Jinjie Ruan
Quoted 10 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
GIC_PRIO_PSR_I_SETwas used to keep track of sections of the kernel where interrupts always need to reach the CPU and could not be masked via interrupt priorities, or DAIF bypassed the status of the PMR.This flag unmasked IRQs when OR'd to
GIC_PRIO_IRQONand ensured that that IRQs were seen as disabled by thelocal_irqs_...functions, as they would only check the PMR if pseudo-NMIs were enabled.
Quoted 6 lines
Quoted 5 lines
This is not needed anymore as our IRQ masking APIs now know how to handle interrupt priorities properly and in parallel with DAIF : remove it.
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>It seems that the reviewed-by for v1 is missing.
I think you had some comments on v1 [1], so I didn't want to apply the tag prematurely
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Cheers
Vladimir
Re: [RFC PATCH v2 21/45] arm64: gicv3: remove GIC_PRIO_PSR_I_SET
In reply to Vladimir MurzinQuoted 4 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
GIC_PRIO_PSR_I_SETwas used to keep track of sections of the kernel where interrupts always need to reach the CPU and could not be masked
I understand it's used in scenarios similar to CPU idle entering WFI, where the CPU needs to be woken up by interrupts.
Quoted 3 lines
via interrupt priorities, or DAIF bypassed the status of the PMR.
This flag unmasked IRQs when OR'd to
GIC_PRIO_IRQONand ensured that
Yes GIC_PRIO_PSR_I_SET | GIC_PRIO_IRQON has has a lower PMR priority ,so unmask the IRQs.
Quoted 1 line
that IRQs were seen as disabled by the
local_irqs_...functions,
Yes, a flags value that is not equal to GIC_PRIO_IRQON is regarded as the disabled state for interrupts.
Quoted 4 lines
as they would only check the PMR if pseudo-NMIs were enabled.
This is not needed anymore as our IRQ masking APIs now know how to handle interrupt priorities properly and in parallel with DAIF : remove it.
Quoted 52 lines
Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/ptrace.h | 2 -- include/linux/irqchip/arm-gic-v3-prio.h | 8 -------- 2 files changed, 10 deletions(-)arch/arm64/include/asm/ptrace.h +0
-2@@ -24,8 +24,6 @@#define GIC_PRIO_IRQON GICV3_PRIO_UNMASKED#define GIC_PRIO_IRQOFF GICV3_PRIO_IRQ -#define GIC_PRIO_PSR_I_SET GICV3_PRIO_PSR_I_SET -/* Additional SPSR bits not exposed in the UABI */ #define PSR_MODE_THREAD_BIT (1 << 0)#define PSR_IL_BIT (1 << 20)include/linux/irqchip/arm-gic-v3-prio.h +0
-8@@ -20,17 +20,11 @@* GICV3_PRIO_NMI is the PMR view of the priority of pseudo-NMIs. This can be * written to the PMR to mask pseudo-NMIs. * -* On arm64 some code sections either automatically switch back to PSR.I or -* explicitly require to not use priority masking. If bit GICV3_PRIO_PSR_I_SET -* is included in the priority mask, it indicates that PSR.I should be set and -* interrupt disabling temporarily does not rely on IRQ priorities. */ #define GICV3_PRIO_UNMASKED 0xe0#define GICV3_PRIO_IRQ 0xc0#define GICV3_PRIO_NMI 0x80 -#define GICV3_PRIO_PSR_I_SET (1 << 4) -#ifndef __ASSEMBLER__#define __gicv3_prio_to_ns (p) (0xff & ((p) << 1))@@ -45,8 +39,6 @@ static_assert(__gicv3_prio_valid_ns(GICV3_PRIO_IRQ));static_assert (GICV3_PRIO_NMI <GICV3_PRIO_IRQ );static_assert (GICV3_PRIO_IRQ <GICV3_PRIO_UNMASKED ); -static_assert (GICV3_PRIO_IRQ < (GICV3_PRIO_IRQ |GICV3_PRIO_PSR_I_SET )); -#endif /* __ASSEMBLER */ #endif /* __LINUX_IRQCHIP_ARM_GIC_V3_PRIO_H */
RFC PATCH v2 22/45 arm64: cpufeature: Remove system_has_prio_mask_debugging()
In reply to Vladimir MurzinUsers of system_has_prio_mask_debugging() have gone, so remove it.
--- arch/arm64/include/asm/cpufeature.h | 6 ------ 1 file changed, 6 deletions(-)
arch/arm64/include/asm/cpufeature.h +0 -6
@@ -821,12 +821,6 @@ static inline bool system_supports_mte(void)return alternative_has_cap_unlikely (ARM64_MTE ); } -static inline bool system_has_prio_mask_debugging (void ) -{ -return IS_ENABLED (CONFIG_ARM64_DEBUG_PRIORITY_MASKING ) && -system_uses_irq_prio_masking (); -} -static inline bool system_supports_bti (void ) {return cpus_have_final_cap (ARM64_BTI );
Signature · 1 line
2.34.1
RFC PATCH v2 23/45 arm64: irqflags: Switch to CONFIG_DEBUG_IRQFLAGS
In reply to Vladimir MurzinAll exception state debug logic now lives under the generic CONFIG_DEBUG_IRQFLAGS. Move the two remaining cases from the dedicated config option to CONFIG_DEBUG_IRQFLAGS as well, keeping all related debug logic under a single configuration option.
--- arch/arm64/include/asm/irqflags.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/irqflags.h +2 -2
@@ -48,7 +48,7 @@ static __always_inline void __daif_local_irq_enable(void) static __always_inline void __pmr_local_irq_enable(void) { -if (IS_ENABLED (CONFIG_ARM64_DEBUG_PRIORITY_MASKING )) { +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {u32 pmr =read_sysreg_s (SYS_ICC_PMR_EL1 );WARN_ON_ONCE (pmr !=GIC_PRIO_IRQON && pmr !=GIC_PRIO_IRQOFF ); }
@@ -77,7 +77,7 @@ static __always_inline void __daif_local_irq_disable(void) static __always_inline void __pmr_local_irq_disable(void) { -if (IS_ENABLED (CONFIG_ARM64_DEBUG_PRIORITY_MASKING )) { +if (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {u32 pmr =read_sysreg_s (SYS_ICC_PMR_EL1 );WARN_ON_ONCE (pmr !=GIC_PRIO_IRQON && pmr !=GIC_PRIO_IRQOFF ); }
Signature · 1 line
2.34.1
RFC PATCH v2 24/45 arm64: Kconfig: Remove CONFIG_ARM64_DEBUG_PRIORITY_MASKING
In reply to Vladimir MurzinLast users of CONFIG_ARM64_DEBUG_PRIORITY_MASKING have gone, so remove it.
--- arch/arm64/Kconfig | 11 ----------- 1 file changed, 11 deletions(-)
arch/arm64/Kconfig +0 -11
@@ -2334,17 +2334,6 @@ config ARM64_PSEUDO_NMI If unsure, say N -if ARM64_PSEUDO_NMI -config ARM64_DEBUG_PRIORITY_MASKING - bool "Debug interrupt priority masking" - help - This adds runtime checks to functions enabling/disabling - interrupts when using priority masking. The additional checks verify - the validity of ICC_PMR_EL1 when calling concerned functions. - - If unsure, say N -endif # ARM64_PSEUDO_NMI - config RELOCATABLE bool "Build a relocatable kernel image" if EXPERT select ARCH_HAS_RELR
Signature · 1 line
2.34.1
RFC PATCH v2 25/45 efi/runtime-wrappers: Permit architectures to override IRQ flags checks
In reply to Vladimir MurzinRepresentation of IRQ flags on arm64 has recently changed. They now track both DAIF and PMR state while remaining compatible with the irqflags API by encoding the state in an unsigned long.
While the generic check can still detect corruption of the IRQ flags caused by EFI runtime services, the resulting error message is cryptic because it does not understand the arm64-specific representation.
Permit architectures to override the IRQ flags checks and associated error messages, while keeping the existing implementation as the default.
--- drivers/firmware/efi/runtime-wrappers.c | 32 +++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-)
drivers/firmware/efi/runtime-wrappers.c +20 -12
@@ -137,6 +137,25 @@ struct efi_runtime_work efi_rts_work;#define arch_efi_restore_flags (state_flags) local_irq_restore(state_flags)#endif +#ifndef arch_efi_check_flags +#define arch_efi_check_flags (state_flags, caller) \ +do { \ + unsigned long cur_flags, mismatch; \ + \ + cur_flags = efi_call_virt_save_flags(); \ + \ + mismatch = state_flags ^ cur_flags; \ + if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK)) \ + break; \ + \ + add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE); \ + pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n", \ + state_flags, cur_flags, \ + caller ?: __builtin_return_address(0)); \ + arch_efi_restore_flags(state_flags); \ +} while(0) +#endif +unsigned long efi_call_virt_save_flags (void ) {unsigned long flags;
@@ -147,18 +166,7 @@ unsigned long efi_call_virt_save_flags(void)void efi_call_virt_check_flags (unsigned long flags,const void *caller) { -unsigned long cur_flags, mismatch; - - cur_flags =efi_call_virt_save_flags (); - - mismatch = flags ^ cur_flags; -if (!WARN_ON_ONCE (mismatch &ARCH_EFI_IRQ_FLAGS_MASK )) -return ; - -add_taint (TAINT_FIRMWARE_WORKAROUND ,LOCKDEP_NOW_UNRELIABLE ); -pr_err_ratelimited (FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n" , - flags, cur_flags, caller ?:__builtin_return_address (0 )); -arch_efi_restore_flags (flags); +arch_efi_check_flags (flags, caller); }/*
Signature · 1 line
2.34.1
RFC PATCH v2 26/45 arm64/efi: Implement override for IRQ flags checks
In reply to Vladimir MurzinTake advantage of the recent changes to the way arm64 represents IRQ flags and implement an architecture-specific override for the EFI IRQ flags check.
IRQ flags now carry both DAIF and PMR state. EFI is not expected to modify either of them, so check both when verifying that IRQ state is preserved across EFI runtime calls.
--- arch/arm64/include/asm/efi.h | 38 ++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-)
arch/arm64/include/asm/efi.h +32 -6
@@ -5,6 +5,7 @@#include <asm/boot.h> #include <asm/cpufeature.h> #include <asm/fpsimd.h> +#include <asm/interrupts/common_flags.h> #include <asm/io.h> #include <asm/memory.h> #include <asm/mmu_context.h>
@@ -48,18 +49,43 @@ void arch_efi_call_virt_teardown(void); (efi_rt_stack_top !=NULL && \on_task_stack (current,READ_ONCE (efi_rt_stack_top[-1 ]),1 )) -#define ARCH_EFI_IRQ_FLAGS_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) -/* * Even when Linux uses IRQ priorities for IRQ disabling, EFI does not. * And EFI shouldn't really play around with priority masking as it is not aware * which priorities the OS has assigned to its interrupts. */#define arch_efi_save_flags (state_flags) \ - ((void)((state_flags) = read_sysreg(daif))) - -#define arch_efi_restore_flags (state_flags) write_sysreg(state_flags, daif) - + ((void)((state_flags) = arch_local_save_flags())) + +#define arch_efi_restore_flags (state_flags) \ +do { \ + arm64_exc_hwstate_t __st = {.flags=state_flags}; \ + \ + arm64_update_exc_hwstate(__st); \ +} while(0) + +#define arch_efi_check_flags (state_flags, caller) \ +do { \ + arm64_exc_hwstate_t __actual = {.flags = arch_local_save_flags() }; \ + arm64_exc_hwstate_t __expected = {.flags=state_flags}; \ + \ + if (__expected.flags == __actual.flags) \ + break; \ + \ + add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE); \ + \ + if (system_uses_irq_prio_masking()) { \ + pr_err_ratelimited(FW_BUG "DAIF+PMR corrupted (0x%x + 0x%x=>0x%x + 0x%x) by EFI call from %pS\n", \ + __expected.daif, __expected.pmr, __actual.daif, __actual.pmr, \ + caller ?: __builtin_return_address(0)); \ + } else { \ + pr_err_ratelimited(FW_BUG "DAIF corrupted (0x%x=>0x%x) by EFI call from %pS\n", \ + __expected.daif, __actual.daif, \ + caller ?: __builtin_return_address(0)); \ + } \ + \ + arch_efi_restore_flags(state_flags); \ +} while(0)/* arch specific definitions used by the stub code */
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 26/45] arm64/efi: Implement override for IRQ flags checks
Quoted 61 lines · Vladimir Murzin
Take advantage of the recent changes to the way arm64 represents IRQ flags and implement an architecture-specific override for the EFI IRQ flags check.
IRQ flags now carry both DAIF and PMR state. EFI is not expected to modify either of them, so check both when verifying that IRQ state is preserved across EFI runtime calls.
Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/efi.h | 38 ++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-)arch/arm64/include/asm/efi.h +21
-6@@ -5,6 +5,7 @@#include <asm/boot.h> #include <asm/cpufeature.h> #include <asm/fpsimd.h> +#include <asm/interrupts/common_flags.h> #include <asm/io.h> #include <asm/memory.h> #include <asm/mmu_context.h> @@ -48,18 +49,43 @@ void arch_efi_call_virt_teardown(void); (efi_rt_stack_top !=NULL && \on_task_stack (current,READ_ONCE (efi_rt_stack_top[-1 ]),1 )) -#define ARCH_EFI_IRQ_FLAGS_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) -/* * Even when Linux uses IRQ priorities for IRQ disabling, EFI does not. * And EFI shouldn't really play around with priority masking as it is not aware * which priorities the OS has assigned to its interrupts. */#define arch_efi_save_flags (state_flags) \ - ((void)((state_flags) = read_sysreg(daif))) - -#define arch_efi_restore_flags (state_flags) write_sysreg(state_flags, daif) - + ((void)((state_flags) = arch_local_save_flags())) + +#define arch_efi_restore_flags (state_flags) \ +do { \ + arm64_exc_hwstate_t __st = {.flags=state_flags}; \ + \ + arm64_update_exc_hwstate(__st); \ +} while(0) + +#define arch_efi_check_flags (state_flags, caller) \ +do { \ + arm64_exc_hwstate_t __actual = {.flags = arch_local_save_flags() }; \ + arm64_exc_hwstate_t __expected = {.flags=state_flags}; \ + \ + if (__expected.flags == __actual.flags) \ + break; \ + \ + add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE); \ + \ + if (system_uses_irq_prio_masking()) { \
arch/arm64/include/asm/efi.h void arch_efi_call_virt_teardown(void); · line 87
Quoted 10 lines
arch/arm64/include/asm/efi.h +10
-0+pr_err_ratelimited (FW_BUG "DAIF+PMR corrupted (0x%x + 0x%x=>0x%x + 0x%x) by EFI call from %pS\n" , \ + __expected.daif, __expected.pmr, __actual.daif, __actual.pmr, \ + caller ?:__builtin_return_address (0 )); \ + }else { \ +pr_err_ratelimited (FW_BUG "DAIF corrupted (0x%x=>0x%x) by EFI call from %pS\n" , \ + __expected.daif, __actual.daif, \ + caller ?:__builtin_return_address (0 )); \ + } \ + \ +arch_efi_restore_flags (state_flags); \
Quoted 4 lines
arch/arm64/include/asm/efi.h +1
-0+}while (0 )/* arch specific definitions used by the stub code */
RFC PATCH v2 27/45 arm64: booting: Document boot requirements for FEAT_NMI
In reply to Vladimir MurzinFrom: Mark Brown <[email protected]>
In order to use FEAT_NMI we must be able to use ALLINT, require that it behave as though not trapped when it is present.
--- Documentation/arch/arm64/booting.rst | 6 ++++++ 1 file changed, 6 insertions(+)
Documentation/arch/arm64/booting.rst +6 -0
@@ -568,6 +568,12 @@ Before jumping into the kernel, the following conditions must be met: - HCRX_EL2.EnASR (bit 2) must be initialised to 0b1. + For CPUs with support for Non-maskable Interrupts (FEAT_NMI): + + - If the kernel is entered at EL1 and EL2 is present: + + - HCRX_EL2.TALLINT (bit 6) must be initialised to 0b0. + The requirements described above for CPU mode, caches, MMUs, architected timers, coherency and system registers apply to all CPUs. All CPUs must enter the kernel in the same exception level. Where the values documented
Signature · 1 line
2.34.1
RFC PATCH v2 28/45 arm64: sysreg: Add definitions for immediate versions of MSR ALLINT
In reply to Vladimir MurzinFrom: Mark Brown <[email protected]>
Encodings are provided for ALLINT which allow setting of ALLINT.ALLINT using an immediate rather than requiring that a register be loaded with the value to write. Since these don't currently fit within the scheme we have for sysreg generation add manual encodings like we currently do for other similar registers such as SVCR.
Since it is required that these immediate versions be encoded with xzr as the source register, provide asm wrappers which ensure this is the case.
--- arch/arm64/include/asm/interrupts/common_flags.h | 16 ++++++++++++++++ arch/arm64/include/asm/sysreg.h | 2 ++ 2 files changed, 18 insertions(+)
arch/arm64/include/asm/interrupts/common_flags.h +16 -0
@@ -44,6 +44,22 @@ typedef enum arm64_exc_context {CRITICAL_CONTEXT , } arm64_exc_context_t; + +#ifdef CONFIG_ARM64_NMI +static __always_inline void _allint_clear(void) +{ + asm volatile(__msr_s(SYS_ALLINT_CLR, "xzr")); +} + +static __always_inline void _allint_set(void) +{ + asm volatile(__msr_s(SYS_ALLINT_SET, "xzr")); +} +#else +static __always_inline void _allint_clear(void) {} +static __always_inline void _allint_set(void) {} +#endif /* CONFIG_ARM64_NMI */ +static __always_inline arm64_exc_hwstate_t __arm64_exc_hwstate_of_process_context(void) {
arch/arm64/include/asm/sysreg.h +2 -0
@@ -179,6 +179,8 @@* System registers, organised loosely by encoding but grouped together * where the architected name contains an index. e.g. ID_MMFR<n>_EL1. */ +#define SYS_ALLINT_CLR sys_reg(0, 1, 4, 0, 0) +#define SYS_ALLINT_SET sys_reg(0, 1, 4, 1, 0)#define SYS_SVCR_SMSTOP_SM_EL0 sys_reg(0, 3, 4, 2, 3)#define SYS_SVCR_SMSTART_SM_EL0 sys_reg(0, 3, 4, 3, 3)#define SYS_SVCR_SMSTOP_SMZA_EL0 sys_reg(0, 3, 4, 6, 3)
Signature · 1 line
2.34.1
RFC PATCH v2 29/45 arm64: ptrace: Add PSR_ALLINT_BIT
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
When FEAT_NMI is implemented, ALLINT is part of PSTATE and will be saved to SPSR on exception entry.
As it can mask interrupts, depending on configuration, we will need to check its value in interruted tasks, so add the corresponding bit to ptrace.h.
Given its impact on interrupt and NMI masking, use it to update print_pstate() so that it displays ALLINT state. Display it after DAIF so that interrupt masking fields are together.
--- arch/arm64/include/uapi/asm/ptrace.h | 1 + arch/arm64/kernel/process.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
arch/arm64/include/uapi/asm/ptrace.h +1 -0
@@ -48,6 +48,7 @@#define PSR_D_BIT 0x00000200#define PSR_BTYPE_MASK 0x00000c00#define PSR_SSBS_BIT 0x00001000 +#define PSR_ALLINT_BIT 0x00002000#define PSR_PAN_BIT 0x00400000#define PSR_UAO_BIT 0x00800000#define PSR_DIT_BIT 0x01000000
arch/arm64/kernel/process.c +2 -1
@@ -183,7 +183,7 @@ static void print_pstate(struct pt_regs *regs)const char *btype_str = btypes[(pstate &PSR_BTYPE_MASK ) >>PSR_BTYPE_SHIFT ]; - printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO %cTCO %cDIT %cSSBS BTYPE=%s)\n", + printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cALLINT %cPAN %cUAO %cTCO %cDIT %cSSBS BTYPE=%s)\n", pstate, pstate & PSR_N_BIT ? 'N' : 'n', pstate & PSR_Z_BIT ? 'Z' : 'z',
@@ -193,6 +193,7 @@ static void print_pstate(struct pt_regs *regs) pstate &PSR_A_BIT ?'A' :'a' , pstate &PSR_I_BIT ?'I' :'i' , pstate &PSR_F_BIT ?'F' :'f' , + pstate &PSR_ALLINT_BIT ?'+' :'-' , pstate &PSR_PAN_BIT ?'+' :'-' , pstate &PSR_UAO_BIT ?'+' :'-' , pstate & PSR_TCO_BIT ? '+' : '-',
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 29/45] arm64: ptrace: Add PSR_ALLINT_BIT
Quoted 36 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
When
FEAT_NMIis implemented, ALLINT is part of PSTATE and will be saved to SPSR on exception entry.
As it can mask interrupts, depending on configuration, we will need to check its value in interruted tasks, so add the corresponding bit toptrace.h.Given its impact on interrupt and NMI masking, use it to update
print_pstate()so that it displays ALLINT state. Display it after DAIF so that interrupt masking fields are together.Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/uapi/asm/ptrace.h | 1 + arch/arm64/kernel/process.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)arch/arm64/include/uapi/asm/ptrace.h +1
-0@@ -48,6 +48,7 @@#define PSR_D_BIT 0x00000200#define PSR_BTYPE_MASK 0x00000c00#define PSR_SSBS_BIT 0x00001000 +#define PSR_ALLINT_BIT 0x00002000#define PSR_PAN_BIT 0x00400000#define PSR_UAO_BIT 0x00800000#define PSR_DIT_BIT 0x01000000arch/arm64/kernel/process.c +0
-0
arch/arm64/kernel/process.c static void print_pstate(struct pt_regs *regs) · line 187
Quoted 6 lines
arch/arm64/kernel/process.c +1
-1@@ -183,7 +183,7 @@ static void print_pstate(struct pt_regs *regs)const char *btype_str = btypes[(pstate &PSR_BTYPE_MASK ) >>PSR_BTYPE_SHIFT ]; - printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO %cTCO %cDIT %cSSBS BTYPE=%s)\n", + printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cALLINT %cPAN %cUAO %cTCO %cDIT %cSSBS BTYPE=%s)\n", pstate,
Quoted 10 lines
arch/arm64/kernel/process.c +1
-0pstate &PSR_N_BIT ?'N' :'n' , pstate & PSR_Z_BIT ? 'Z' : 'z',@@ -193,6 +193,7 @@ static void print_pstate(struct pt_regs *regs) pstate &PSR_A_BIT ?'A' :'a' , pstate &PSR_I_BIT ?'I' :'i' , pstate &PSR_F_BIT ?'F' :'f' , + pstate &PSR_ALLINT_BIT ?'+' :'-' , pstate &PSR_PAN_BIT ?'+' :'-' , pstate &PSR_UAO_BIT ?'+' :'-' , pstate & PSR_TCO_BIT ? '+' : '-',
RFC PATCH v2 30/45 arm64: idreg: Add an override for FEAT_NMI
In reply to Vladimir MurzinFrom: Mark Brown <[email protected]>
Add a named override for FEAT_NMI, allowing it to be explicitly disabled in case of problems.
--- arch/arm64/kernel/pi/idreg-override.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kernel/pi/idreg-override.c +1 -0
@@ -154,6 +154,7 @@ static const struct ftr_set_desc pfr1 __prel64_initconst = {FIELD ("bt" ,ID_AA64PFR1_EL1_BT_SHIFT ,NULL ),FIELD ("gcs" ,ID_AA64PFR1_EL1_GCS_SHIFT ,NULL ),FIELD ("mte" ,ID_AA64PFR1_EL1_MTE_SHIFT ,NULL ), +FIELD ("nmi" ,ID_AA64PFR1_EL1_NMI_SHIFT ,NULL ),FIELD ("sme" ,ID_AA64PFR1_EL1_SME_SHIFT , pfr1_sme_filter), FIELD("mpam_frac", ID_AA64PFR1_EL1_MPAM_frac_SHIFT, NULL), {}
Signature · 1 line
2.34.1
RFC PATCH v2 31/45 arm64: cpufeature: Detect PE support for FEAT_NMI
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Use of FEAT_NMI requires that all the PEs in the system and the GIC have NMI support. This patch implements the PE part of that detection.
In order to avoid problematic interactions between real and pseudo NMIs we disable the architected feature if the user has enabled pseudo NMIs on the command line. If this is done on a system where support for the architected feature is detected then a warning is printed during boot in order to help users spot what is likely to be a misconfiguration.
As KVM does not care about the host kernel supporting FEAT_NMI or not to allow guests to use it, split the CPU cap in two : ARM64_HAS_NMI is the hardware support, ARM64_NMI is the kernel making use of it.
--- arch/arm64/include/asm/cpucaps.h | 2 + arch/arm64/include/asm/cpufeature.h | 10 +++++ arch/arm64/kernel/cpufeature.c | 68 ++++++++++++++++++++++++++++- arch/arm64/tools/cpucaps | 2 + 4 files changed, 81 insertions(+), 1 deletion(-)
arch/arm64/include/asm/cpucaps.h +2 -0
@@ -75,6 +75,8 @@ cpucap_is_possible(const unsigned int cap)return IS_ENABLED (CONFIG_HW_PERF_EVENTS );case ARM64_HAS_LSUI :return IS_ENABLED (CONFIG_ARM64_LSUI ); +case ARM64_NMI : +return IS_ENABLED (CONFIG_ARM64_NMI ); }return true;
arch/arm64/include/asm/cpufeature.h +10 -0
@@ -816,6 +816,16 @@ static __always_inline bool system_uses_irq_prio_masking(void)return alternative_has_cap_unlikely (ARM64_HAS_GIC_PRIO_MASKING ); } +static __always_inline bool system_supports_nmi(void) +{ +return alternative_has_cap_unlikely (ARM64_HAS_NMI ); +} + +static __always_inline bool system_uses_nmi(void) +{ +return alternative_has_cap_unlikely (ARM64_NMI ); +} +static inline bool system_supports_mte (void ) {return alternative_has_cap_unlikely (ARM64_MTE );
arch/arm64/kernel/cpufeature.c +67 -1
98 lines not on this page · raw message
arch/arm64/tools/cpucaps +2 -0
@@ -51,6 +51,7 @@ HAS_LS64_V HAS_LSUI HAS_MOPS HAS_NESTED_VIRT +HAS_NMI HAS_BBML2_NOABORT HAS_PAN HAS_PMUV3
@@ -80,6 +81,7 @@ MTE MTE_ASYMM MTE_FAR MTE_STORE_ONLY +NMI SME SME_FA64 SME2
Signature · 1 line
2.34.1
RFC PATCH v2 32/45 arm64: nmi: Manage masking for superpriority interrupts
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
Extend logic to handle and debug exception context/state with knowlage of FEAT_NMI.
Take care to order writes to ALLINT relative to DAIF, as clearing ALLINT before DAIF could result in taking an NMI while DAIF is fully masked, as could setting it after DAIF.
Since superpriority interrupts are not masked through DAIF like pseduo NMIs are, we also need to modify the assembler macros for managing DAIF to ensure that the masking is done in the assembly code.
Note that save_and_disable_irq/restore_irq and save_and_disable_daif/restore_irq pairs are used in distinct contextes:
- former is used in context of SW PAN to quickly disable/enable preemption
- latter is used to completely mask all exceptions.
For that reason split save_and_disable_daif/restore_irq into more generic exception save restore pair and plumb with FEAT_NMI logic.
--- arch/arm64/include/asm/assembler.h | 20 +++++++++-- .../include/asm/interrupts/common_flags.h | 35 +++++++++++++++++++ arch/arm64/include/asm/interrupts/entry.h | 18 ++++++---- arch/arm64/include/asm/interrupts/masking.h | 3 +- arch/arm64/include/asm/irqflags.h | 3 +- arch/arm64/kernel/entry.S | 12 +++---- 6 files changed, 75 insertions(+), 16 deletions(-)
arch/arm64/include/asm/assembler.h +18 -2
@@ -37,11 +37,27 @@/* * Save/restore interrupts. */ - .macro save_and_disable_daif, flags - mrs \flags, daif + .macro save_and_disable_exceptions, flags, tmp + mrs \flags, daif // updates flags[9:6] with DAIF +#ifdef CONFIG_ARM64_NMI +alternative_if ARM64_NMI + mrs_s \tmp, SYS_ALLINT // updates tmp[13] with AllInt + msr_s SYS_ALLINT_SET, xzr + orr \flags, \flags, \tmp // now flags[13,9:6] carry pair of AllInt,DAIF +alternative_else_nop_endif +#endif msr daifset, #0xf .endm + .macro restore_exceptions, flags + msr daif, \flags // bits other than flags[9:6] are ignored +#ifdef CONFIG_ARM64_NMI +alternative_if ARM64_NMI + msr_s SYS_ALLINT, \flags // bits other than flags[13] are ignored +alternative_else_nop_endif +#endif + .endm + .macro save_and_disable_irq, flags mrs \flags, daif msr daifset, #3
arch/arm64/include/asm/interrupts/common_flags.h +35 -0
@@ -20,6 +20,14 @@/* * Exception context mapping * +* FEAT_NMI +* +* CRITICAL -> DAIF + AllInt (corresponds to the state on exception entry) +* ERROR -> AIF + AllInt +* NONMI -> IF + AllInt +* NOIRQ -> IF +* PROCESS -> 0 +* * pseudo-NMI * * CRITICAL -> DAIF + IRQON (corresponds to the state on exception entry)
@@ -76,6 +84,7 @@ arm64_exc_hwstate_t __arm64_exc_hwstate_of_noirq_context(void)return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX , .pmr=GIC_PRIO_IRQOFF };return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ }; + } static __always_inline
@@ -84,6 +93,9 @@ arm64_exc_hwstate_t __arm64_exc_hwstate_of_nonmi_context(void)if (system_uses_irq_prio_masking ())return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ , .pmr=GIC_PRIO_IRQON }; +if (system_uses_nmi ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ , .allint=ALLINT_ALLINT }; +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ }; }
@@ -93,6 +105,9 @@ arm64_exc_hwstate_t __arm64_exc_hwstate_of_error_context(void)if (system_uses_irq_prio_masking ())return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX , .pmr=GIC_PRIO_IRQON }; +if (system_uses_nmi ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX , .allint=ALLINT_ALLINT }; +return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX }; }
@@ -102,6 +117,9 @@ arm64_exc_hwstate_t __arm64_exc_hwstate_of_critical_context(void)if (system_uses_irq_prio_masking ())return (arm64_exc_hwstate_t ){.daif=DAIF_MASK , .pmr=GIC_PRIO_IRQON }; +if (system_uses_nmi ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_MASK , .allint=ALLINT_ALLINT }; +return (arm64_exc_hwstate_t ){.daif=DAIF_MASK }; }
@@ -131,6 +149,9 @@ arm64_exc_hwstate_t arm64_inherit_exc_hwstate(struct pt_regs *regs)if (system_uses_irq_prio_masking ()) state.pmr = regs->pmr; +if (system_uses_nmi ()) + state.allint = regs->pstate &PSR_ALLINT_BIT ; +return state; }
@@ -150,6 +171,9 @@ void arm64_debug_exc_hwstate(arm64_exc_hwstate_t expected)if (system_uses_irq_prio_masking ()) {WARN_ONCE (1 ,"Unexpected DAIF+PMR: 0x%x + 0x%x (expected 0x%x + 0x%x)\n" , actual.daif, actual.pmr, expected.daif, expected.pmr); + }else if (system_uses_nmi ()) { +WARN_ONCE (1 ,"Unexpected DAIF+ALLINT: 0x%x + 0x%x (expected 0x%x + 0x%x)\n" , + actual.daif, actual.allint, expected.daif, expected.allint); }else {WARN_ONCE (1 ,"Unexpected DAIF: 0x%x (expected 0x%x)\n" , actual.daif, expected.daif);
@@ -194,10 +218,21 @@ void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force)write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); } +/* +* Try to order ALLINT writes to be consistent with the DAIF state : +* we don't want to take an NMI with DAIF masked or when it should +* be masked but isn't yet. +*/ +if (system_uses_nmi () && hwstate.allint && force) +_allint_set (); +barrier ();write_sysreg (hwstate.daif, daif);barrier (); +if (system_uses_nmi () && !hwstate.allint && force) +_allint_clear (); +if (system_uses_irq_prio_masking () && hwstate.pmr ==GIC_PRIO_IRQON && force) {
arch/arm64/include/asm/interrupts/entry.h +12 -6
@@ -10,7 +10,6 @@#include <asm/cpufeature.h> #include <asm/interrupts/common_flags.h> -static __always_inline arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev,arm64_exc_hwstate_t next)
@@ -26,7 +25,8 @@ arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev,if (!irqs_disabled)trace_hardirqs_on (); - force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + force = (system_uses_irq_prio_masking () && prev.pmr != next.pmr) || + (system_uses_nmi () && prev.allint != next.allint);__arm64_update_exc_hwstate (next, force);
@@ -52,15 +52,18 @@ arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_cif (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {bool pnmi =system_uses_irq_prio_masking (); +bool nmi =system_uses_nmi ();WARN_ON_ONCE (context >ERROR_CONTEXT && prev.daif ==DAIF_ERRCTX );WARN_ON_ONCE (context >NONMI_CONTEXT && - prev.daif ==DAIF_PROCCTX_NOIRQ ); + ((nmi && prev.daif ==DAIF_PROCCTX_NOIRQ && prev.allint ==ALLINT_ALLINT ) || + (!nmi && prev.daif ==DAIF_PROCCTX_NOIRQ )));WARN_ON_ONCE (context >NOIRQ_CONTEXT && - pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (nmi && prev.daif ==DAIF_PROCCTX_NOIRQ && prev.allint !=ALLINT_ALLINT ))); WARN_ON_ONCE(context > PROCESS_CONTEXT && ((pnmi && prev.daif == DAIF_PROCCTX && prev.pmr == GIC_PRIO_IRQON) ||
@@ -77,6 +80,7 @@ arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_cif (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {bool pnmi =system_uses_irq_prio_masking (); +bool nmi =system_uses_nmi ();WARN_ON_ONCE (context <CRITICAL_CONTEXT && prev.daif ==DAIF_MASK );
@@ -85,11 +89,13 @@ arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_c prev.daif == DAIF_ERRCTX);WARN_ON_ONCE (context <NONMI_CONTEXT && - pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + ((pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ) || + (nmi && prev.daif ==DAIF_PROCCTX_NOIRQ && prev.allint ==ALLINT_ALLINT )));WARN_ON_ONCE (context <NOIRQ_CONTEXT && ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || - (!pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); + (nmi && prev.daif ==DAIF_PROCCTX_NOIRQ && prev.allint !=ALLINT_ALLINT ) || + (!pnmi && !nmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); }return __arm64_switch_exc_hwstate_to (prev, next);
arch/arm64/include/asm/interrupts/masking.h +2 -1
@@ -30,7 +30,8 @@ arm64_exc_hwstates_t local_exceptions_save_mask(arm64_exc_context_t new)* We've just got actual HW state so we can rely on that to * optimize some unnecessary updates. */ - force =system_uses_irq_prio_masking () && actual.pmr != state.pmr; + force = (system_uses_irq_prio_masking () && actual.pmr != state.pmr) || + (system_uses_nmi () && actual.allint != state.allint);if (!irqs_disabled)trace_hardirqs_on ();
arch/arm64/include/asm/irqflags.h +2 -1
@@ -31,8 +31,9 @@typedef union arm64_exc_hwstate {struct {u16 daif; +u16 allint;u8 pmr; -u8 __padding[5 ]; +u8 __padding[3 ]; };unsigned long flags; }arm64_exc_hwstate_t ;
arch/arm64/kernel/entry.S +6 -6
@@ -815,7 +815,7 @@ SYM_CODE_END(__bp_harden_el1_vectors) * */ SYM_FUNC_START(cpu_switch_to) - save_and_disable_daif x11 + save_and_disable_exceptions x11, x12 mov x10, #THREAD_CPU_CONTEXT add x8, x0, x10 mov x9, sp
@@ -839,7 +839,7 @@ SYM_FUNC_START(cpu_switch_to) ptrauth_keys_install_kernel x1, x8, x9, x10 scs_save x0 scs_load_current - restore_irq x11 + restore_exceptions x11 ret SYM_FUNC_END(cpu_switch_to) NOKPROBE(cpu_switch_to)
@@ -866,7 +866,7 @@ NOKPROBE(ret_from_fork) * Calls func(regs) using this CPU's irq stack and shadow irq stack. */ SYM_FUNC_START(call_on_irq_stack) - save_and_disable_daif x9 + save_and_disable_exceptions x9, x10 #ifdef CONFIG_SHADOW_CALL_STACK get_current_task x16 scs_save x16
@@ -881,10 +881,10 @@ SYM_FUNC_START(call_on_irq_stack) /* Move to the new stack and call the function there */ add sp, x16, #IRQ_STACK_SIZE - restore_irq x9 + restore_exceptions x9 blr x1 - save_and_disable_daif x9 + save_and_disable_exceptions x9, x10 /* * Restore the SP from the FP, and restore the FP and LR from the frame * record.
@@ -892,7 +892,7 @@ SYM_FUNC_START(call_on_irq_stack) mov sp, x29 ldp x29, x30, [sp], #16 scs_load_current - restore_irq x9 + restore_exceptions x9 ret SYM_FUNC_END(call_on_irq_stack) NOKPROBE(call_on_irq_stack)
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 32/45] arm64: nmi: Manage masking for superpriority interrupts
Quoted 151 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
Extend logic to handle and debug exception context/state with knowlage of
FEAT_NMI.Take care to order writes to ALLINT relative to DAIF, as clearing ALLINT before DAIF could result in taking an NMI while DAIF is fully masked, as could setting it after DAIF.
Since superpriority interrupts are not masked through DAIF like pseduo NMIs are, we also need to modify the assembler macros for managing DAIF to ensure that the masking is done in the assembly code.
Note that save_and_disable_irq/restore_irq and save_and_disable_daif/restore_irq pairs are used in distinct contextes:
- former is used in context of SW PAN to quickly disable/enable preemption
- latter is used to completely mask all exceptions.
For that reason split save_and_disable_daif/restore_irq into more generic exception save restore pair and plumb with
FEAT_NMIlogic.Co-developed-by Mark Brown <[email protected]>Signed-off-by Mark Brown <[email protected]>Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/assembler.h | 20 +++++++++-- .../include/asm/interrupts/common_flags.h | 35 +++++++++++++++++++ arch/arm64/include/asm/interrupts/entry.h | 18 ++++++---- arch/arm64/include/asm/interrupts/masking.h | 3 +- arch/arm64/include/asm/irqflags.h | 3 +- arch/arm64/kernel/entry.S | 12 +++---- 6 files changed, 75 insertions(+), 16 deletions(-)arch/arm64/include/asm/assembler.h +18
-2@@ -37,11 +37,27 @@/* * Save/restore interrupts. */ - .macro save_and_disable_daif, flags - mrs \flags, daif + .macro save_and_disable_exceptions, flags, tmp + mrs \flags, daif // updates flags[9:6] with DAIF +#ifdef CONFIG_ARM64_NMI +alternative_if ARM64_NMI + mrs_s \tmp, SYS_ALLINT // updates tmp[13] with AllInt + msr_s SYS_ALLINT_SET, xzr + orr \flags, \flags, \tmp // now flags[13,9:6] carry pair of AllInt,DAIF +alternative_else_nop_endif +#endif msr daifset, #0xf .endm + .macro restore_exceptions, flags + msr daif, \flags // bits other than flags[9:6] are ignored +#ifdef CONFIG_ARM64_NMI +alternative_if ARM64_NMI + msr_s SYS_ALLINT, \flags // bits other than flags[13] are ignored +alternative_else_nop_endif +#endif + .endm + .macro save_and_disable_irq, flags mrs \flags, daif msr daifset, #3arch/arm64/include/asm/interrupts/common_flags.h +24
-0@@ -20,6 +20,14 @@/* * Exception context mapping * +* FEAT_NMI +* +* CRITICAL -> DAIF + AllInt (corresponds to the state on exception entry) +* ERROR -> AIF + AllInt +* NONMI -> IF + AllInt +* NOIRQ -> IF +* PROCESS -> 0 +* * pseudo-NMI * * CRITICAL -> DAIF + IRQON (corresponds to the state on exception entry) @@ -76,6 +84,7 @@ arm64_exc_hwstate_t __arm64_exc_hwstate_of_noirq_context(void)return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX , .pmr=GIC_PRIO_IRQOFF };return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ }; + } static __always_inline@@ -84,6 +93,9 @@ arm64_exc_hwstate_t __arm64_exc_hwstate_of_nonmi_context(void)if (system_uses_irq_prio_masking ())return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ , .pmr=GIC_PRIO_IRQON }; +if (system_uses_nmi ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ , .allint=ALLINT_ALLINT }; +return (arm64_exc_hwstate_t ){.daif=DAIF_PROCCTX_NOIRQ }; }@@ -93,6 +105,9 @@ arm64_exc_hwstate_t __arm64_exc_hwstate_of_error_context(void)if (system_uses_irq_prio_masking ())return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX , .pmr=GIC_PRIO_IRQON }; +if (system_uses_nmi ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX , .allint=ALLINT_ALLINT }; +return (arm64_exc_hwstate_t ){.daif=DAIF_ERRCTX }; }@@ -102,6 +117,9 @@ arm64_exc_hwstate_t __arm64_exc_hwstate_of_critical_context(void)if (system_uses_irq_prio_masking ())return (arm64_exc_hwstate_t ){.daif=DAIF_MASK , .pmr=GIC_PRIO_IRQON }; +if (system_uses_nmi ()) +return (arm64_exc_hwstate_t ){.daif=DAIF_MASK , .allint=ALLINT_ALLINT }; +return (arm64_exc_hwstate_t ){.daif=DAIF_MASK }; }@@ -131,6 +149,9 @@ arm64_exc_hwstate_t arm64_inherit_exc_hwstate(struct pt_regs *regs)if (system_uses_irq_prio_masking ()) state.pmr = regs->pmr; +if (system_uses_nmi ()) + state.allint = regs->pstate &PSR_ALLINT_BIT ; +return state; }@@ -150,6 +171,9 @@ void arm64_debug_exc_hwstate(arm64_exc_hwstate_t expected)if (system_uses_irq_prio_masking ()) {WARN_ONCE (1 ,"Unexpected DAIF+PMR: 0x%x + 0x%x (expected 0x%x + 0x%x)\n" , actual.daif, actual.pmr, expected.daif, expected.pmr); + }else if (system_uses_nmi ()) { +WARN_ONCE (1 ,"Unexpected DAIF+ALLINT: 0x%x + 0x%x (expected 0x%x + 0x%x)\n" , + actual.daif, actual.allint, expected.daif, expected.allint); }else {WARN_ONCE (1 ,"Unexpected DAIF: 0x%x (expected 0x%x)\n" , actual.daif, expected.daif);
arch/arm64/include/asm/interrupts/common_flags.h void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force) · line 226
Quoted 9 lines
arch/arm64/include/asm/interrupts/common_flags.h +6
-0@@ -194,10 +218,21 @@ void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force)write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); } +/* +* Try to order ALLINT writes to be consistent with the DAIF state : +* we don't want to take an NMI with DAIF masked or when it should +* be masked but isn't yet. +*/ +if (system_uses_nmi () && hwstate.allint && force)
hwstate.allint & ALLINT_ALLINT
arch/arm64/include/asm/interrupts/common_flags.h void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force) · line 233
Quoted 7 lines
arch/arm64/include/asm/interrupts/common_flags.h +3
-0+_allint_set (); +barrier ();write_sysreg (hwstate.daif, daif);barrier (); +if (system_uses_nmi () && !hwstate.allint && force)
!(hwstate.allint & ALLINT_ALLINT)
Quoted 154 lines
arch/arm64/include/asm/interrupts/common_flags.h +2
-0+_allint_clear (); +if (system_uses_irq_prio_masking () && hwstate.pmr ==GIC_PRIO_IRQON && force) {arch/arm64/include/asm/interrupts/entry.h +12
-6@@ -10,7 +10,6 @@#include <asm/cpufeature.h> #include <asm/interrupts/common_flags.h> -static __always_inline arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev,arm64_exc_hwstate_t next)@@ -26,7 +25,8 @@ arm64_exc_hwstate_t __arm64_switch_exc_hwstate_to(arm64_exc_hwstate_t prev,if (!irqs_disabled)trace_hardirqs_on (); - force =system_uses_irq_prio_masking () && prev.pmr != next.pmr; + force = (system_uses_irq_prio_masking () && prev.pmr != next.pmr) || + (system_uses_nmi () && prev.allint != next.allint);__arm64_update_exc_hwstate (next, force);@@ -52,15 +52,18 @@ arm64_exc_hwstate_t arm64_drop_exc_context(arm64_exc_hwstate_t prev, arm64_exc_cif (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {bool pnmi =system_uses_irq_prio_masking (); +bool nmi =system_uses_nmi ();WARN_ON_ONCE (context >ERROR_CONTEXT && prev.daif ==DAIF_ERRCTX );WARN_ON_ONCE (context >NONMI_CONTEXT && - prev.daif ==DAIF_PROCCTX_NOIRQ ); + ((nmi && prev.daif ==DAIF_PROCCTX_NOIRQ && prev.allint ==ALLINT_ALLINT ) || + (!nmi && prev.daif ==DAIF_PROCCTX_NOIRQ )));WARN_ON_ONCE (context >NOIRQ_CONTEXT && - pnmi && prev.pmr ==GIC_PRIO_IRQOFF ); + ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || + (nmi && prev.daif ==DAIF_PROCCTX_NOIRQ && prev.allint !=ALLINT_ALLINT ))); WARN_ON_ONCE(context > PROCESS_CONTEXT && ((pnmi && prev.daif == DAIF_PROCCTX && prev.pmr == GIC_PRIO_IRQON) ||@@ -77,6 +80,7 @@ arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_cif (IS_ENABLED (CONFIG_DEBUG_IRQFLAGS )) {bool pnmi =system_uses_irq_prio_masking (); +bool nmi =system_uses_nmi ();WARN_ON_ONCE (context <CRITICAL_CONTEXT && prev.daif ==DAIF_MASK );@@ -85,11 +89,13 @@ arm64_exc_hwstate_t arm64_lift_exc_context(arm64_exc_hwstate_t prev, arm64_exc_c prev.daif == DAIF_ERRCTX);WARN_ON_ONCE (context <NONMI_CONTEXT && - pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ); + ((pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ) || + (nmi && prev.daif ==DAIF_PROCCTX_NOIRQ && prev.allint ==ALLINT_ALLINT )));WARN_ON_ONCE (context <NOIRQ_CONTEXT && ((pnmi && prev.pmr ==GIC_PRIO_IRQOFF ) || - (!pnmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); + (nmi && prev.daif ==DAIF_PROCCTX_NOIRQ && prev.allint !=ALLINT_ALLINT ) || + (!pnmi && !nmi && prev.daif ==DAIF_PROCCTX_NOIRQ ))); }return __arm64_switch_exc_hwstate_to (prev, next);arch/arm64/include/asm/interrupts/masking.h +2
-1@@ -30,7 +30,8 @@ arm64_exc_hwstates_t local_exceptions_save_mask(arm64_exc_context_t new)* We've just got actual HW state so we can rely on that to * optimize some unnecessary updates. */ - force =system_uses_irq_prio_masking () && actual.pmr != state.pmr; + force = (system_uses_irq_prio_masking () && actual.pmr != state.pmr) || + (system_uses_nmi () && actual.allint != state.allint);if (!irqs_disabled)trace_hardirqs_on ();arch/arm64/include/asm/irqflags.h +2
-1@@ -31,8 +31,9 @@typedef union arm64_exc_hwstate {struct {u16 daif; +u16 allint;u8 pmr; -u8 __padding[5 ]; +u8 __padding[3 ]; };unsigned long flags; }arm64_exc_hwstate_t ;arch/arm64/kernel/entry.S +6
-6@@ -815,7 +815,7 @@ SYM_CODE_END(__bp_harden_el1_vectors) * */ SYM_FUNC_START(cpu_switch_to) - save_and_disable_daif x11 + save_and_disable_exceptions x11, x12 mov x10, #THREAD_CPU_CONTEXT add x8, x0, x10 mov x9, sp@@ -839,7 +839,7 @@ SYM_FUNC_START(cpu_switch_to) ptrauth_keys_install_kernel x1, x8, x9, x10 scs_save x0 scs_load_current - restore_irq x11 + restore_exceptions x11 ret SYM_FUNC_END(cpu_switch_to) NOKPROBE(cpu_switch_to)@@ -866,7 +866,7 @@ NOKPROBE(ret_from_fork) * Calls func(regs) using this CPU's irq stack and shadow irq stack. */ SYM_FUNC_START(call_on_irq_stack) - save_and_disable_daif x9 + save_and_disable_exceptions x9, x10 #ifdef CONFIG_SHADOW_CALL_STACK get_current_task x16 scs_save x16@@ -881,10 +881,10 @@ SYM_FUNC_START(call_on_irq_stack) /* Move to the new stack and call the function there */ add sp, x16, #IRQ_STACK_SIZE - restore_irq x9 + restore_exceptions x9 blr x1 - save_and_disable_daif x9 + save_and_disable_exceptions x9, x10 /* * Restore the SP from the FP, and restore the FP and LR from the frame * record.@@ -892,7 +892,7 @@ SYM_FUNC_START(call_on_irq_stack) mov sp, x29 ldp x29, x30, [sp], #16 scs_load_current - restore_irq x9 + restore_exceptions x9 ret SYM_FUNC_END(call_on_irq_stack) NOKPROBE(call_on_irq_stack)
Re: [RFC PATCH v2 32/45] arm64: nmi: Manage masking for superpriority interrupts
Quoted 4 lines · Jinjie Ruan
Quoted 4 lines
arch/arm64/include/asm/interrupts/common_flags.h +0
-0@@ -194,10 +218,21 @@ void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force)write_sysreg_s (hwstate.pmr,SYS_ICC_PMR_EL1 ); }
Quoted 9 lines
arch/arm64/include/asm/interrupts/common_flags.h void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force) · line 226
Quoted 6 lines
arch/arm64/include/asm/interrupts/common_flags.h +6
-0+/* +* Try to order ALLINT writes to be consistent with the DAIF state : +* we don't want to take an NMI with DAIF masked or when it should +* be masked but isn't yet. +*/ +if (system_uses_nmi () && hwstate.allint && force)hwstate.allint &
ALLINT_ALLINT
Fair enough.
Quoted 9 lines
arch/arm64/include/asm/interrupts/common_flags.h void __arm64_update_exc_hwstate(arm64_exc_hwstate_t hwstate, bool force) · line 233
Quoted 7 lines
arch/arm64/include/asm/interrupts/common_flags.h +3
-0+_allint_set (); +barrier ();write_sysreg (hwstate.daif, daif);barrier (); +if (system_uses_nmi () && !hwstate.allint && force)!(hwstate.allint & ALLINT_ALLINT)
Fair enough.
I'll fix both of them in the next version.
Thanks
Vladimir
Quoted 5 lines
Quoted 5 lines
arch/arm64/include/asm/interrupts/common_flags.h +2
-0+_allint_clear (); +if (system_uses_irq_prio_masking () && hwstate.pmr ==GIC_PRIO_IRQON && force) {
RFC PATCH v2 33/45 arm64: irq: Report FEAT_NMI masking local IRQs
In reply to Vladimir MurzinFrom: Ada Couprie Diaz <[email protected]>
As we clear SCTLR_EL1.SPINTMASK when enabling FEAT_NMI, ALLINT masks IRQs and FIQs regardless of superpriority.
Update irqflags.h and regs_irqs_disabled() to take it into account, so we properly keep track of the local IRQ masking state.
We have documentation at the top of irqflags.h which explains the DAIF masking. Since the additional masking with NMIs is related and also covers the IF in DAIF, extend the comment to note what's going on with NMIs.
--- arch/arm64/include/asm/irqflags.h | 17 +++++++++++++++++ arch/arm64/include/asm/ptrace.h | 3 +++ 2 files changed, 20 insertions(+)
arch/arm64/include/asm/irqflags.h +17 -0
@@ -20,6 +20,16 @@* always masked and unmasked together, and have no side effects for other * flags. Keeping to this order makes it easier for entry.S to know which * exceptions should be unmasked. +* +* With the addition of the FEAT_NMI extension we gain an additional +* class of superpriority IRQ/FIQ which is separately masked with a +* choice of modes controlled by SCTLR_ELn.{SPINTMASK,NMI}. +* Linux sets SPINTMASK to 0 and NMI to 1 which results in ALLINT.ALLINT +* masking both superpriority interrupts and IRQ/FIQ regardless of the +* I and F settings. Since these superpriority interrupts are being +* used as NMIs we do not include them in the interrupt masking here, +* anything that requires that NMIs be masked needs to explicitly do so, +* but we do check for ALLINT masking IRQs/FIQs. */ /*
@@ -104,6 +114,9 @@ static __always_inline unsigned long arch_local_save_flags(void) {arm64_exc_hwstate_t hwstate = { .daif =read_sysreg (daif) }; +if (system_uses_nmi ()) + hwstate.allint =read_sysreg_s (SYS_ALLINT ); +if (system_uses_irq_prio_masking ()) hwstate.pmr =read_sysreg_s (SYS_ICC_PMR_EL1 );
@@ -117,6 +130,10 @@ static __always_inline bool arch_irqs_disabled_flags(unsigned long flags)if (hwstate.daif &PSR_I_BIT )return true; +/* SCTLR_EL1.SPINTMASK is clear, so ALLINT masks *all* IRQs/FIQs. */ +if (system_uses_nmi ()) +return !!(hwstate.allint &ALLINT_ALLINT ); +if (system_uses_irq_prio_masking ()) { WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_IRQFLAGS) && hwstate.pmr != GIC_PRIO_IRQOFF &&
arch/arm64/include/asm/ptrace.h +3 -0
@@ -206,6 +206,9 @@ static __always_inline bool regs_irqs_disabled(const struct pt_regs *regs)if (regs->pstate &PSR_I_BIT )return true; +if (system_uses_nmi ()) +return !!(regs->pstate &PSR_ALLINT_BIT ); +if (system_uses_irq_prio_masking ()) { WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_IRQFLAGS) && regs->pmr != GIC_PRIO_IRQOFF &&
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 33/45] arm64: irq: Report FEAT_NMI masking local IRQs
Quoted 68 lines · Vladimir Murzin
From: Ada Couprie Diaz <[email protected]>
As we clear SCTLR_EL1.SPINTMASK when enabling
FEAT_NMI, ALLINT masks IRQs and FIQs regardless of superpriority.
Updateirqflags.handregs_irqs_disabled()to take it into account, so we properly keep track of the local IRQ masking state.We have documentation at the top of
irqflags.hwhich explains the DAIF masking. Since the additional masking with NMIs is related and also covers the IF in DAIF, extend the comment to note what's going on with NMIs.Co-developed-by Mark Brown <[email protected]>Signed-off-by Mark Brown <[email protected]>Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/irqflags.h | 17 +++++++++++++++++ arch/arm64/include/asm/ptrace.h | 3 +++ 2 files changed, 20 insertions(+)arch/arm64/include/asm/irqflags.h +17
-0@@ -20,6 +20,16 @@* always masked and unmasked together, and have no side effects for other * flags. Keeping to this order makes it easier for entry.S to know which * exceptions should be unmasked. +* +* With the addition of the FEAT_NMI extension we gain an additional +* class of superpriority IRQ/FIQ which is separately masked with a +* choice of modes controlled by SCTLR_ELn.{SPINTMASK,NMI}. +* Linux sets SPINTMASK to 0 and NMI to 1 which results in ALLINT.ALLINT +* masking both superpriority interrupts and IRQ/FIQ regardless of the +* I and F settings. Since these superpriority interrupts are being +* used as NMIs we do not include them in the interrupt masking here, +* anything that requires that NMIs be masked needs to explicitly do so, +* but we do check for ALLINT masking IRQs/FIQs. */ /* @@ -104,6 +114,9 @@ static __always_inline unsigned long arch_local_save_flags(void) {arm64_exc_hwstate_t hwstate = { .daif =read_sysreg (daif) }; +if (system_uses_nmi ()) + hwstate.allint =read_sysreg_s (SYS_ALLINT ); +if (system_uses_irq_prio_masking ()) hwstate.pmr =read_sysreg_s (SYS_ICC_PMR_EL1 );@@ -117,6 +130,10 @@ static __always_inline bool arch_irqs_disabled_flags(unsigned long flags)if (hwstate.daif &PSR_I_BIT )return true; +/* SCTLR_EL1.SPINTMASK is clear, so ALLINT masks *all* IRQs/FIQs. */ +if (system_uses_nmi ()) +return !!(hwstate.allint &ALLINT_ALLINT ); +if (system_uses_irq_prio_masking ()) { WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_IRQFLAGS) && hwstate.pmr != GIC_PRIO_IRQOFF &&arch/arm64/include/asm/ptrace.h +0
-0
arch/arm64/include/asm/ptrace.h static __always_inline bool regs_irqs_disabled(const struct pt_regs *regs) · line 210
Quoted 5 lines
arch/arm64/include/asm/ptrace.h +2
-0@@ -206,6 +206,9 @@ static __always_inline bool regs_irqs_disabled(const struct pt_regs *regs)if (regs->pstate &PSR_I_BIT )return true; +if (system_uses_nmi ()) +return !!(regs->pstate &PSR_ALLINT_BIT );
Quoted 4 lines
arch/arm64/include/asm/ptrace.h +1
-0+if (system_uses_irq_prio_masking ()) { WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_IRQFLAGS) && regs->pmr != GIC_PRIO_IRQOFF &&
RFC PATCH v2 34/45 arm64: nmi: Add handling of superpriority interrupts as NMIs
In reply to Vladimir MurzinFrom: Mark Brown <[email protected]>
Our goal with superpriority interrupts is to use them as NMIs, taking advantage of the much smaller regions where they are masked to allow prompt handling of the most time-critical interrupts.
When an interrupt is configured with superpriority, we enter EL1 as we do for any other interrupt. The presence of a superpriority interrupt is indicated by a status bit in ISR_EL1. We check this bit before unmasking interrupts in elX_interrupt(), and if a superpriority interrupt is pending, we handle it as an NMI. Otherwise, normal interrupts are handled as usual.
Since superpriority interrupts are always handled as NMIs, the interrupt controller can rely on in_nmi() to distinguish them from ordinary interrupts.
Enable IPIs to use superpriority interrupts as NMIs, matching the existing pseudo-NMI behaviour.
--- arch/arm64/include/asm/entry-common.h | 7 +++ arch/arm64/kernel/entry-common.c | 83 +++++++++++++++++++++++---- arch/arm64/kernel/smp.c | 2 +- 3 files changed, 79 insertions(+), 13 deletions(-)
arch/arm64/include/asm/entry-common.h +7 -0
@@ -37,6 +37,13 @@ static inline bool arch_irqentry_exit_need_resched(void)if (system_uses_irq_prio_masking () &&read_sysreg (daif))return false; +/* +* If AllInt is set then we must have handled an NMI, so skip +* preemption +*/ +if (system_uses_nmi () &&read_sysreg_s (SYS_ALLINT )) +return false; +/* * Preempting a task from an IRQ means we leave copies of PSTATE * on the stack. cpufeature's enable calls may modify PSTATE, but
arch/arm64/kernel/entry-common.c +71 -12
138 lines not on this page · raw message
arch/arm64/kernel/smp.c +1 -1
@@ -1035,7 +1035,7 @@ static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)static bool ipi_should_be_nmi (enum ipi_msg_type ipi) { -if (!system_uses_irq_prio_masking ()) +if (!system_uses_nmi () && !system_uses_irq_prio_masking ())return false;switch (ipi) {
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 34/45] arm64: nmi: Add handling of superpriority interrupts as NMIs
Quoted 168 lines · Vladimir Murzin
From: Mark Brown <[email protected]>
Our goal with superpriority interrupts is to use them as NMIs, taking advantage of the much smaller regions where they are masked to allow prompt handling of the most time-critical interrupts.
When an interrupt is configured with superpriority, we enter EL1 as we do for any other interrupt. The presence of a superpriority interrupt is indicated by a status bit in
ISR_EL1. We check this bit before unmasking interrupts inelX_interrupt(), and if a superpriority interrupt is pending, we handle it as an NMI. Otherwise, normal interrupts are handled as usual.Since superpriority interrupts are always handled as NMIs, the interrupt controller can rely on
in_nmi()to distinguish them from ordinary interrupts.Enable IPIs to use superpriority interrupts as NMIs, matching the existing pseudo-NMI behaviour.
Signed-off-by Mark Brown <[email protected]>Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/include/asm/entry-common.h | 7 +++ arch/arm64/kernel/entry-common.c | 83 +++++++++++++++++++++++---- arch/arm64/kernel/smp.c | 2 +- 3 files changed, 79 insertions(+), 13 deletions(-)arch/arm64/include/asm/entry-common.h +7
-0@@ -37,6 +37,13 @@ static inline bool arch_irqentry_exit_need_resched(void)if (system_uses_irq_prio_masking () &&read_sysreg (daif))return false; +/* +* If AllInt is set then we must have handled an NMI, so skip +* preemption +*/ +if (system_uses_nmi () &&read_sysreg_s (SYS_ALLINT )) +return false; +/* * Preempting a task from an IRQ means we leave copies of PSTATE * on the stack. cpufeature's enable calls may modify PSTATE, but arch/arm64/kernel/entry-common.c +55
-12@@ -31,6 +31,14 @@#include <asm/sysreg.h> #include <asm/system_misc.h> +static __always_inline bool is_nmi(void) +{ +if (!system_uses_nmi ()) +return false; + +return !!(read_sysreg (isr_el1) &ISR_EL1_IS ); +} +/* * Handle IRQ/context state management when entering from kernel mode. * Before this function is called it is not safe to call regular kernel code, @@ -525,8 +533,8 @@ asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)arm64_debug_exc_context (CRITICAL_CONTEXT ); } -static __always_inline void __el1_pnmi(struct pt_regs *regs, -void (*handler)(struct pt_regs *)) +static __always_inline void __el1_nmi(struct pt_regs *regs, +void (*handler)(struct pt_regs *)) {arm64_exc_hwstate_t hwstate;irqentry_state_t state;@@ -545,7 +553,23 @@ static __always_inline void __el1_irq(struct pt_regs *regs, state =arm64_enter_from_kernel_mode (regs); -arm64_unmask_exc_context (NONMI_CONTEXT ); +/* +* For non-NMI systems, we can switch to NOIRQ context since +* NMIs cannot occur. +* +* For pseudo-NMI, we must keep NONMI context until we can +* determine the interrupt priority, which is done by the IRQ +* handler. +* +* For FEAT_NMI, we already know the interrupt is an IRQ, so we +* can switch to NOIRQ context immediately. NMIs do not +* interfere because they follow a separate handling path and do +* not share the IRQ handling state. +*/ +if (system_uses_irq_prio_masking ()) +arm64_unmask_exc_context (NONMI_CONTEXT ); +else +arm64_unmask_exc_context (NOIRQ_CONTEXT );irq_enter_rcu ();do_interrupt_handler (regs, handler);@@ -565,8 +589,9 @@ static __always_inline void __el1_irq(struct pt_regs *regs, static void noinstr el1_interrupt(struct pt_regs *regs,void (*handler)(struct pt_regs *)) { -if (IS_ENABLED (CONFIG_ARM64_PSEUDO_NMI ) &®s_irqs_disabled (regs)) -__el1_pnmi (regs, handler); +/* Is there a NMI to handle? */ +if (regs_irqs_disabled (regs) ||is_nmi ()) +__el1_nmi (regs, handler);else __el1_irq (regs, handler);@@ -580,6 +605,8 @@ asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs) asmlinkage void noinstr el1h_64_fiq_handler(struct pt_regs *regs) { +WARN_ON_ONCE (system_uses_nmi () && (read_sysreg (isr_el1) &ISR_EL1_FS )); +el1_interrupt (regs, handle_arch_fiq); }@@ -903,20 +930,36 @@ asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)arm64_debug_exc_context (CRITICAL_CONTEXT ); } -static void noinstr el0_interrupt(struct pt_regs *regs, -void (*handler)(struct pt_regs *)) +static __always_inline void __el0_nmi(struct pt_regs *regs, +void (*handler)(struct pt_regs *)) { -arm64_enter_from_user_mode (regs); +irqentry_state_t state; +arm64_exc_hwstate_t hwstate; -arm64_unmask_exc_context (NONMI_CONTEXT ); + state =irqentry_nmi_enter (regs); + hwstate =arm64_unmask_exc_context (NONMI_CONTEXT ); +do_interrupt_handler (regs, handler); +arm64_mask_exc_context (hwstate); +irqentry_nmi_exit (regs, state); +} -if (regs->pc &BIT (55 )) -arm64_apply_bp_hardening (); +static __always_inline void __el0_irq(struct pt_regs *regs, +void (*handler)(struct pt_regs *)) +{ +arm64_enter_from_user_mode (regs); +/* +* For the same reason as in el1_irq() keep NONMI +* context for pesudo-NMI, and switch to NOIRQ +* otherwise +*/ +if (system_uses_irq_prio_masking ()) +arm64_unmask_exc_context (NONMI_CONTEXT ); +else +arm64_unmask_exc_context (NOIRQ_CONTEXT );irq_enter_rcu ();do_interrupt_handler (regs, handler);irq_exit_rcu (); -/* * For the same reason as in el1_irq() we effectivly * have NOIRQ_CONTEXT on return from handler - keep
arch/arm64/kernel/entry-common.c static void noinstr el0_interrupt(struct pt_regs *regs, · line 976
Quoted 10 lines
arch/arm64/kernel/entry-common.c +7
-0@@ -924,6 +967,20 @@ static void noinstr el0_interrupt(struct pt_regs *regs,*/ arm64_debug_exc_context (NOIRQ_CONTEXT );arm64_exit_to_user_mode (regs,arm64_exc_hwstate_of_context (NOIRQ_CONTEXT )); +} + +static void noinstr el0_interrupt(struct pt_regs *regs, +void (*handler)(struct pt_regs *)) +{ +if (regs->pc &BIT (55 )) +arm64_apply_bp_hardening ();
It seems only after enter_from_user_mode() or irqentry_nmi_enter(), it is safe to call normal kernel code.
otherwise LGTM
Quoted 32 lines
arch/arm64/kernel/entry-common.c +9
-0+ +/* Is there a NMI to handle? */ +if (is_nmi ()) +__el0_nmi (regs, handler); +else +__el0_irq (regs, handler); +arm64_debug_exc_context (CRITICAL_CONTEXT ); }@@ -939,6 +996,8 @@ asmlinkage void noinstr el0t_64_irq_handler(struct pt_regs *regs) static void noinstr __el0_fiq_handler_common(struct pt_regs *regs) { +WARN_ON_ONCE (system_uses_nmi () && (read_sysreg (isr_el1) &ISR_EL1_FS )); +el0_interrupt (regs, handle_arch_fiq); }arch/arm64/kernel/smp.c +1
-1@@ -1035,7 +1035,7 @@ static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)static bool ipi_should_be_nmi (enum ipi_msg_type ipi) { -if (!system_uses_irq_prio_masking ()) +if (!system_uses_nmi () && !system_uses_irq_prio_masking ())return false;switch (ipi) {
Re: [RFC PATCH v2 34/45] arm64: nmi: Add handling of superpriority interrupts as NMIs
Quoted 5 lines · Jinjie Ruan
Quoted 5 lines
arch/arm64/kernel/entry-common.c +1
-0@@ -924,6 +967,20 @@ static void noinstr el0_interrupt(struct pt_regs *regs,*/ arm64_debug_exc_context (NOIRQ_CONTEXT );arm64_exit_to_user_mode (regs,arm64_exc_hwstate_of_context (NOIRQ_CONTEXT )); +}
Quoted 8 lines
arch/arm64/kernel/entry-common.c static void noinstr el0_interrupt(struct pt_regs *regs, · line 976
Quoted 6 lines
arch/arm64/kernel/entry-common.c +6
-0+ +static void noinstr el0_interrupt(struct pt_regs *regs, +void (*handler)(struct pt_regs *)) +{ +if (regs->pc &BIT (55 )) +arm64_apply_bp_hardening ();It seems only after
enter_from_user_mode()orirqentry_nmi_enter(), it is safe to call normal kernel code.
I do not think there is issue with that. Here is call chain:
arm64_apply_bp_hardening() // always_inline
-> alternative_has_cap_unlikely() // always_inline
-> cpucap_is_possible() // always_inline -> fn()
- qcom_link_stack_sanitisation // noinstr
- call_hvc_arch_workaround_1 // noinstr
- call_smc_arch_workaround_1 // noinstr
That's also in line with el0_ia(), el0_pc() and el0_softstp() which call arm64_apply_bp_hardening() before enter_from_user_mode()
Cheers
Vladimir
Jinjie Ruan
Reviewed-byRe: [RFC PATCH v2 34/45] arm64: nmi: Add handling of superpriority interrupts as NMIs
Quoted 20 lines · Vladimir Murzin
Quoted 5 lines · Jinjie Ruan
Quoted 5 lines
arch/arm64/kernel/entry-common.c +1
-0@@ -924,6 +967,20 @@ static void noinstr el0_interrupt(struct pt_regs *regs,*/ arm64_debug_exc_context (NOIRQ_CONTEXT );arm64_exit_to_user_mode (regs,arm64_exc_hwstate_of_context (NOIRQ_CONTEXT )); +}Quoted 8 lines
arch/arm64/kernel/entry-common.c static void noinstr el0_interrupt(struct pt_regs *regs, · line 976
Quoted 6 lines
arch/arm64/kernel/entry-common.c +6
-0+ +static void noinstr el0_interrupt(struct pt_regs *regs, +void (*handler)(struct pt_regs *)) +{ +if (regs->pc &BIT (55 )) +arm64_apply_bp_hardening ();It seems only after
enter_from_user_mode()orirqentry_nmi_enter(), it is safe to call normal kernel code.I do not think there is issue with that. Here is call chain:
arm64_apply_bp_hardening() // always_inline -> alternative_has_cap_unlikely() // always_inline -> cpucap_is_possible() // always_inline
Quoted 7 lines
->
fn()
-qcom_link_stack_sanitisation// noinstr
-call_hvc_arch_workaround_1// noinstr
-call_smc_arch_workaround_1// noinstrThat's also in line with
el0_ia(),el0_pc()andel0_softstp()which callarm64_apply_bp_hardening()beforeenter_from_user_mode()
Make sense
Quoted 4 lines
Cheers
Vladimir
RFC PATCH v2 35/45 arm64: suspend: Always initialise PSTATE.ALLINT
In reply to Vladimir MurzinPSTATE.ALLINT is always set to the inverse of SCTLR_ELx.SPINTMASK, regardless of the value of SCTLR_ELx.NMI. SCTLR_ELx.NMI is initialised to 0 by default, so PSTATE.ALLINT does not cause any interrupt masking. With upcoming FEAT_NMI support, SCTLR_ELx.NMI will be set as part of the enable sequence.
However, during CPU suspend/resume, we reinitialise PSTATE to INIT_PSTATE_EL1. INIT_PSTATE_EL1 currently does not set PSR_ALLINT_BIT, but SCTLR_ELx.NMI is restored from the saved value. Since all exceptions are masked during CPU suspend/resume, avoid a state where SCTLR_ELx.NMI is restored to 1 but PSTATE.ALLINT is clear, since that would permit an NMI during resume.
Include PSR_ALLINT_BIT in INIT_PSTATE_EL1 so PSTATE.ALLINT is always set.
--- arch/arm64/include/asm/ptrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/ptrace.h +1 -1
@@ -17,7 +17,7 @@#define CurrentEL_EL2 (2 << 2)#define INIT_PSTATE_EL1 \ - (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL1h) + (PSR_ALLINT_BIT | PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL1h)#include <linux/irqchip/arm-gic-v3-prio.h>
Signature · 1 line
2.34.1
RFC PATCH v2 36/45 arm64/efi: Add ALLINT to IRQ flags checks
In reply to Vladimir MurzinEFI is not expected to modify PSTATE.ALLINT, so check that it remains unchanged across EFI runtime calls.
--- arch/arm64/include/asm/efi.h | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/include/asm/efi.h +4 -0
@@ -78,6 +78,10 @@ do { \pr_err_ratelimited (FW_BUG "DAIF+PMR corrupted (0x%x + 0x%x=>0x%x + 0x%x) by EFI call from %pS\n" , \ __expected.daif, __expected.pmr, __actual.daif, __actual.pmr, \ caller ?:__builtin_return_address (0 )); \ + }else if (system_uses_nmi ()) { \ +pr_err_ratelimited (FW_BUG "DAIF+ALLINT corrupted (0x%x + 0x%x=>0x%x + 0x%x) by EFI call from %pS\n" , \ + __expected.daif, __expected.allint, __actual.daif, __actual.allint, \ + caller ?:__builtin_return_address (0 )); \ }else { \ pr_err_ratelimited(FW_BUG "DAIF corrupted (0x%x=>0x%x) by EFI call from %pS\n", \ __expected.daif, __actual.daif, \
Signature · 1 line
2.34.1
RFC PATCH v2 37/45 arm64: kprobes: Disable NMIs
In reply to Vladimir MurzinKprobes masks all DAIF exceptions to keep things simple and avoid nested exceptions. With FEAT_NMI, that is no longer enough. Mask ALLINT as well to ensure things stay simple.
--- arch/arm64/kernel/probes/kprobes.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
arch/arm64/kernel/probes/kprobes.c +16 -6
@@ -208,17 +208,27 @@ static void __kprobes set_current_kprobe(struct kprobe *p)* the kprobe state is per-CPU and doesn't get migrated. */ static void __kprobes kprobes_save_local_irqflag (struct kprobe_ctlblk *kcb, -struct pt_regs *regs) +struct pt_regs *regs) { - kcb->saved_irqflag = regs->pstate &DAIF_MASK ; - regs->pstate |=DAIF_MASK ; +arm64_exc_hwstate_t next =arm64_exc_hwstate_of_context (CRITICAL_CONTEXT ); + + kcb->saved_irqflag =arm64_inherit_exc_hwstate (regs).flags; + + regs->pstate &= ~(DAIF_MASK |PSR_ALLINT_BIT ); + + regs->pstate |= next.daif; + regs->pstate |= next.allint; }static void __kprobes kprobes_restore_local_irqflag (struct kprobe_ctlblk *kcb, -struct pt_regs *regs) +struct pt_regs *regs) { - regs->pstate &= ~DAIF_MASK ; - regs->pstate |= kcb->saved_irqflag; +arm64_exc_hwstate_t prev = {.flags = kcb->saved_irqflag}; + + regs->pstate &= ~(DAIF_MASK |PSR_ALLINT_BIT ); + + regs->pstate |= prev.daif; + regs->pstate |= prev.allint; } static void __kprobes setup_singlestep(struct kprobe *p,
Signature · 1 line
2.34.1
RFC PATCH v2 38/45 arm64: smp: Abstract SGI and LPI operations
In reply to Vladimir MurzinSGI and LPI backed IPIs require different setup, enable, disable and send operations. These differences are currently handled by repeatedly checking percpu_ipi_descs. As the implementation specific logic grows, these checks make the common IPI code increasingly difficult to follow.
Introduce an operations structure for each implementation to encapsulate the specific of SGI and LPI handling, leaving the common IPI paths generic.
--- arch/arm64/kernel/smp.c | 163 +++++++++++++++++++++++++--------------- 1 file changed, 101 insertions(+), 62 deletions(-)
arch/arm64/kernel/smp.c +101 -62
@@ -75,11 +75,18 @@ static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);#define get_ipi_desc (__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) -static bool percpu_ipi_descs__ro_after_init ; +struct ipi_irq_ops { +void (*setup)(int ipi,int ncpus); +void (*disable)(int cpu,int ipi); +void (*enable)(int cpu,int ipi); +void (*send)(const cpumask_t *mask,unsigned int nr); +}; + +static const struct ipi_irq_ops *ipi_ops__ro_after_init ;static bool crash_stop; -static void ipi_setup (int cpu); +static void ipi_enable (int cpu);#ifdef CONFIG_HOTPLUG_CPU static void ipi_teardown (int cpu);
@@ -240,7 +247,7 @@ asmlinkage notrace void secondary_start_kernel(void)*/ notify_cpu_starting (cpu); -ipi_setup (cpu); +ipi_enable (cpu);numa_add_cpu (cpu);
@@ -916,13 +923,7 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regsstatic void arm64_send_ipi (const cpumask_t *mask,unsigned int nr) { -unsigned int cpu; - -if (!percpu_ipi_descs) -__ipi_send_mask (get_ipi_desc (0 , nr), mask); -else -for_each_cpu (cpu, mask) -__ipi_send_single (get_ipi_desc (cpu, nr), cpu); + ipi_ops->send (mask, nr); }static void arm64_backtrace_ipi (cpumask_t *mask)
@@ -1048,53 +1049,13 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi) } } -static void ipi_setup (int cpu) -{ -int i; - -if (WARN_ON_ONCE (!ipi_irq_base)) -return ; - -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -prepare_percpu_nmi (ipi_irq_base + i); -enable_percpu_nmi (ipi_irq_base + i,0 ); - }else { -enable_percpu_irq (ipi_irq_base + i,0 ); - } - }else { -enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i))); - } - } -} - -#ifdef CONFIG_HOTPLUG_CPU -static void ipi_teardown (int cpu) +static void ipi_sgi_setup (int ipi,int ncpus) { -int i; +int err, irq, cpu; -if (WARN_ON_ONCE (!ipi_irq_base)) +if (WARN_ON_ONCE (ncpus))return ; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -disable_percpu_nmi (ipi_irq_base + i); -teardown_percpu_nmi (ipi_irq_base + i); - }else { -disable_percpu_irq (ipi_irq_base + i); - } - }else { -disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i))); - } - } -} -#endif - -static void ipi_setup_sgi (int ipi) -{ -int err, irq, cpu; irq = ipi_irq_base + ipi;
@@ -1112,7 +1073,50 @@ static void ipi_setup_sgi(int ipi)irq_set_status_flags (irq,IRQ_HIDDEN ); } -static void ipi_setup_lpi (int ipi,int ncpus) +static void ipi_sgi_enable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +prepare_percpu_nmi (ipi_irq_base + ipi); +enable_percpu_nmi (ipi_irq_base + ipi,0 ); + }else { +enable_percpu_irq (ipi_irq_base + ipi,0 ); + } +} + +static void ipi_sgi_disable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +disable_percpu_nmi (ipi_irq_base + ipi); +teardown_percpu_nmi (ipi_irq_base + ipi); + }else { +disable_percpu_irq (ipi_irq_base + ipi); + } +} + +static void ipi_sgi_send (const cpumask_t *mask,unsigned int nr) +{ +__ipi_send_mask (get_ipi_desc (0 , nr), mask); +} + +static const struct ipi_irq_ops ipi_sgi_ops = { + .disable = ipi_sgi_disable, + .enable = ipi_sgi_enable, + .setup = ipi_sgi_setup, + .send = ipi_sgi_send, +}; + + +static void ipi_lpi_enable (int cpu,int ipi) +{ +enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_disable (int cpu,int ipi) +{ +disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_setup (int ipi,int ncpus) {for (int cpu =0 ; cpu < ncpus; cpu++) {int err, irq;
@@ -1132,6 +1136,44 @@ static void ipi_setup_lpi(int ipi, int ncpus) } } +static void ipi_lpi_send (const cpumask_t *mask,unsigned int nr) { +int cpu; + +for_each_cpu (cpu, mask) +__ipi_send_single (get_ipi_desc (cpu, nr), cpu); +} + +static const struct ipi_irq_ops ipi_lpi_ops = { + .disable = ipi_lpi_disable, + .enable = ipi_lpi_enable, + .setup = ipi_lpi_setup, + .send = ipi_lpi_send, +}; + +static void ipi_enable (int cpu) +{ +int ipi; + +if (WARN_ON_ONCE (!ipi_irq_base)) +return ; + +for (ipi =0 ; ipi < nr_ipi; ipi++) + ipi_ops->enable (cpu, ipi); +} + +#ifdef CONFIG_HOTPLUG_CPU +static void ipi_teardown (int cpu) +{ +int ipi; + +if (WARN_ON_ONCE (!ipi_irq_base)) +return ; + +for (ipi =0 ; ipi < nr_ipi; ipi++) + ipi_ops->disable (cpu, ipi); +} +#endif +void __init set_smp_ipi_range_percpu (int ipi_base,int n,int ncpus) {int i;
@@ -1139,18 +1181,15 @@ void __init set_smp_ipi_range_percpu(int ipi_base, int n, int ncpus)WARN_ON (n <MAX_IPI ); nr_ipi =min (n,MAX_IPI ); - percpu_ipi_descs = !!ncpus; ipi_irq_base = ipi_base; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) -ipi_setup_sgi (i); -else -ipi_setup_lpi (i, ncpus); - } + ipi_ops = ncpus ? &ipi_lpi_ops : &ipi_sgi_ops; + +for (i =0 ; i < nr_ipi; i++) + ipi_ops->setup (i, ncpus);/* Setup the boot CPU immediately */ -ipi_setup (smp_processor_id ()); +ipi_enable (smp_processor_id ()); }void arch_smp_send_reschedule (int cpu)
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 38/45] arm64: smp: Abstract SGI and LPI operations
Quoted 9 lines · Vladimir Murzin
SGI and LPI backed IPIs require different setup, enable, disable and send operations. These differences are currently handled by repeatedly checking
percpu_ipi_descs. As the implementation specific logic grows, these checks make the common IPI code increasingly difficult to follow.Introduce an operations structure for each implementation to encapsulate the specific of SGI and LPI handling, leaving the common IPI paths generic.
Quoted 233 lines
Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/smp.c | 163 +++++++++++++++++++++++++--------------- 1 file changed, 101 insertions(+), 62 deletions(-)arch/arm64/kernel/smp.c +101
-62@@ -75,11 +75,18 @@ static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);#define get_ipi_desc (__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) -static bool percpu_ipi_descs__ro_after_init ; +struct ipi_irq_ops { +void (*setup)(int ipi,int ncpus); +void (*disable)(int cpu,int ipi); +void (*enable)(int cpu,int ipi); +void (*send)(const cpumask_t *mask,unsigned int nr); +}; + +static const struct ipi_irq_ops *ipi_ops__ro_after_init ;static bool crash_stop; -static void ipi_setup (int cpu); +static void ipi_enable (int cpu);#ifdef CONFIG_HOTPLUG_CPU static void ipi_teardown (int cpu);@@ -240,7 +247,7 @@ asmlinkage notrace void secondary_start_kernel(void)*/ notify_cpu_starting (cpu); -ipi_setup (cpu); +ipi_enable (cpu);numa_add_cpu (cpu);@@ -916,13 +923,7 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regsstatic void arm64_send_ipi (const cpumask_t *mask,unsigned int nr) { -unsigned int cpu; - -if (!percpu_ipi_descs) -__ipi_send_mask (get_ipi_desc (0 , nr), mask); -else -for_each_cpu (cpu, mask) -__ipi_send_single (get_ipi_desc (cpu, nr), cpu); + ipi_ops->send (mask, nr); }static void arm64_backtrace_ipi (cpumask_t *mask)@@ -1048,53 +1049,13 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi) } } -static void ipi_setup (int cpu) -{ -int i; - -if (WARN_ON_ONCE (!ipi_irq_base)) -return ; - -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -prepare_percpu_nmi (ipi_irq_base + i); -enable_percpu_nmi (ipi_irq_base + i,0 ); - }else { -enable_percpu_irq (ipi_irq_base + i,0 ); - } - }else { -enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i))); - } - } -} - -#ifdef CONFIG_HOTPLUG_CPU -static void ipi_teardown (int cpu) +static void ipi_sgi_setup (int ipi,int ncpus) { -int i; +int err, irq, cpu; -if (WARN_ON_ONCE (!ipi_irq_base)) +if (WARN_ON_ONCE (ncpus))return ; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -disable_percpu_nmi (ipi_irq_base + i); -teardown_percpu_nmi (ipi_irq_base + i); - }else { -disable_percpu_irq (ipi_irq_base + i); - } - }else { -disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i))); - } - } -} -#endif - -static void ipi_setup_sgi (int ipi) -{ -int err, irq, cpu; irq = ipi_irq_base + ipi;@@ -1112,7 +1073,50 @@ static void ipi_setup_sgi(int ipi)irq_set_status_flags (irq,IRQ_HIDDEN ); } -static void ipi_setup_lpi (int ipi,int ncpus) +static void ipi_sgi_enable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +prepare_percpu_nmi (ipi_irq_base + ipi); +enable_percpu_nmi (ipi_irq_base + ipi,0 ); + }else { +enable_percpu_irq (ipi_irq_base + ipi,0 ); + } +} + +static void ipi_sgi_disable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +disable_percpu_nmi (ipi_irq_base + ipi); +teardown_percpu_nmi (ipi_irq_base + ipi); + }else { +disable_percpu_irq (ipi_irq_base + ipi); + } +} + +static void ipi_sgi_send (const cpumask_t *mask,unsigned int nr) +{ +__ipi_send_mask (get_ipi_desc (0 , nr), mask); +} + +static const struct ipi_irq_ops ipi_sgi_ops = { + .disable = ipi_sgi_disable, + .enable = ipi_sgi_enable, + .setup = ipi_sgi_setup, + .send = ipi_sgi_send, +}; + + +static void ipi_lpi_enable (int cpu,int ipi) +{ +enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_disable (int cpu,int ipi) +{ +disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_setup (int ipi,int ncpus) {for (int cpu =0 ; cpu < ncpus; cpu++) {int err, irq;@@ -1132,6 +1136,44 @@ static void ipi_setup_lpi(int ipi, int ncpus) } } +static void ipi_lpi_send (const cpumask_t *mask,unsigned int nr) { +int cpu; + +for_each_cpu (cpu, mask) +__ipi_send_single (get_ipi_desc (cpu, nr), cpu); +} + +static const struct ipi_irq_ops ipi_lpi_ops = { + .disable = ipi_lpi_disable, + .enable = ipi_lpi_enable, + .setup = ipi_lpi_setup, + .send = ipi_lpi_send, +}; + +static void ipi_enable (int cpu) +{ +int ipi; + +if (WARN_ON_ONCE (!ipi_irq_base)) +return ; + +for (ipi =0 ; ipi < nr_ipi; ipi++) + ipi_ops->enable (cpu, ipi); +} + +#ifdef CONFIG_HOTPLUG_CPU +static void ipi_teardown (int cpu) +{ +int ipi; + +if (WARN_ON_ONCE (!ipi_irq_base)) +return ; + +for (ipi =0 ; ipi < nr_ipi; ipi++) + ipi_ops->disable (cpu, ipi); +} +#endif +void __init set_smp_ipi_range_percpu (int ipi_base,int n,int ncpus) {int i;@@ -1139,18 +1181,15 @@ void __init set_smp_ipi_range_percpu(int ipi_base, int n, int ncpus)WARN_ON (n <MAX_IPI ); nr_ipi =min (n,MAX_IPI ); - percpu_ipi_descs = !!ncpus; ipi_irq_base = ipi_base; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) -ipi_setup_sgi (i); -else -ipi_setup_lpi (i, ncpus); - } + ipi_ops = ncpus ? &ipi_lpi_ops : &ipi_sgi_ops; + +for (i =0 ; i < nr_ipi; i++) + ipi_ops->setup (i, ncpus);/* Setup the boot CPU immediately */ -ipi_setup (smp_processor_id ()); +ipi_enable (smp_processor_id ()); }void arch_smp_send_reschedule (int cpu)
Re: [RFC PATCH v2 38/45] arm64: smp: Abstract SGI and LPI operations
In reply to Vladimir MurzinQuoted 6 lines · Vladimir Murzin
SGI and LPI backed IPIs require different setup, enable, disable and send operations. These differences are currently handled by repeatedly checking
percpu_ipi_descs. As the implementation specific logic grows, these checks make the common IPI code increasingly difficult to follow.
Quoted 8 lines
Introduce an operations structure for each implementation to encapsulate the specific of SGI and LPI handling, leaving the common IPI paths generic.
Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/smp.c | 163 +++++++++++++++++++++++++--------------- 1 file changed, 101 insertions(+), 62 deletions(-)
Hi, Vladimir,
I believe that the modifications here do not need to be so extensive. By maintaining the order of the related function definitions consistent with the original, the changes can be reduced to only 126 lines, and the modifications will be easier to review.
arch/arm64/kernel/smp.c | 126
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------
1 file changed, 81 insertions(+), 45 deletions(-)
arch/arm64/kernel/smp.c +0 -0
@@ -75,11 +75,18 @@ static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs,
pcpu_ipi_desc);
#define get_ipi_desc(__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc,
__cpu)->descs[__ipi])
-static bool percpu_ipi_descs __ro_after_init;
+struct ipi_irq_ops {
+ void (*setup)(int ipi, int ncpus); + void (*enable)(int cpu, int ipi); + void (*disable)(int cpu, int ipi); + void (*send)(const cpumask_t *mask, unsigned int nr);
+};
+
+static const struct ipi_irq_ops *ipi_ops __ro_after_init;
static bool crash_stop;
-static void ipi_setup(int cpu);
+static void ipi_enable(int cpu);
#ifdef CONFIG_HOTPLUG_CPU
static void ipi_teardown(int cpu);
@@ -240,7 +247,7 @@ asmlinkage notrace void secondary_start_kernel(void)
*/
notify_cpu_starting(cpu);- ipi_setup(cpu);
+ ipi_enable(cpu);
numa_add_cpu(cpu);
@@ -916,13 +923,7 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs
static void arm64_send_ipi(const cpumask_t *mask, unsigned int nr) {
- unsigned int cpu;
-
- if (!percpu_ipi_descs)
- __ipi_send_mask(get_ipi_desc(0, nr), mask);
- else
- for_each_cpu(cpu, mask)
- __ipi_send_single(get_ipi_desc(cpu, nr), cpu);
+ ipi_ops->send(mask, nr);
}
static void arm64_backtrace_ipi(cpumask_t *mask)
@@ -1048,25 +1049,15 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi)
}
}
-static void ipi_setup(int cpu)
+static void ipi_enable(int cpu)
{
int i;
if (WARN_ON_ONCE(!ipi_irq_base))
return;- for (i = 0; i < nr_ipi; i++) {
- if (!percpu_ipi_descs) {
- if (ipi_should_be_nmi(i)) {
- prepare_percpu_nmi(ipi_irq_base + i);
- enable_percpu_nmi(ipi_irq_base + i, 0);
- } else {
- enable_percpu_irq(ipi_irq_base + i, 0);
- }
- } else {
- enable_irq(irq_desc_get_irq(get_ipi_desc(cpu, i)));
- }
- }
+ for (i = 0; i < nr_ipi; i++)
+ ipi_ops->enable(cpu, i);
}
#ifdef CONFIG_HOTPLUG_CPU
@@ -1077,25 +1068,18 @@ static void ipi_teardown(int cpu)
if (WARN_ON_ONCE(!ipi_irq_base))
return;- for (i = 0; i < nr_ipi; i++) {
- if (!percpu_ipi_descs) {
- if (ipi_should_be_nmi(i)) {
- disable_percpu_nmi(ipi_irq_base + i);
- teardown_percpu_nmi(ipi_irq_base + i);
- } else {
- disable_percpu_irq(ipi_irq_base + i);
- }
- } else {
- disable_irq(irq_desc_get_irq(get_ipi_desc(cpu, i)));
- }
- }
+ for (i = 0; i < nr_ipi; i++)
+ ipi_ops->disable(cpu, i);
}
#endif
-static void ipi_setup_sgi(int ipi)
+static void ipi_sgi_setup(int ipi, int ncpus)
{
int err, irq, cpu;+ if (WARN_ON_ONCE(ncpus))
+ return;
+
irq = ipi_irq_base + ipi;
if (ipi_should_be_nmi(ipi)) {
@@ -1112,7 +1096,39 @@ static void ipi_setup_sgi(int ipi)
irq_set_status_flags(irq, IRQ_HIDDEN);}
-static void ipi_setup_lpi(int ipi, int ncpus)
+static void ipi_sgi_enable(int cpu, int ipi)
+{
+ if (ipi_should_be_nmi(ipi)) {
+ prepare_percpu_nmi(ipi_irq_base + ipi);
+ enable_percpu_nmi(ipi_irq_base + ipi, 0);
+ } else {
+ enable_percpu_irq(ipi_irq_base + ipi, 0);
+ }
+}
+
+static void ipi_sgi_disable(int cpu, int ipi)
+{
+ if (ipi_should_be_nmi(ipi)) {
+ disable_percpu_nmi(ipi_irq_base + ipi);
+ teardown_percpu_nmi(ipi_irq_base + ipi);
+ } else {
+ disable_percpu_irq(ipi_irq_base + ipi);
+ }
+}
+
+static void ipi_sgi_send(const cpumask_t *mask, unsigned int nr) +{
+ __ipi_send_mask(get_ipi_desc(0, nr), mask);
+}
+
+static const struct ipi_irq_ops ipi_sgi_ops = {
+ .setup = ipi_sgi_setup,
+ .enable = ipi_sgi_enable,
+ .disable = ipi_sgi_disable,
+ .send = ipi_sgi_send,
+};
+
+static void ipi_lpi_setup(int ipi, int ncpus)
{
for (int cpu = 0; cpu < ncpus; cpu++) {
int err, irq;
@@ -1132,6 +1148,30 @@ static void ipi_setup_lpi(int ipi, int ncpus)
}}
+static void ipi_lpi_enable(int cpu, int ipi)
+{
+ enable_irq(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));
+}
+
+static void ipi_lpi_disable(int cpu, int ipi)
+{
+ disable_irq(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));
+}
+
+static void ipi_lpi_send(const cpumask_t *mask, unsigned int nr) {
+ int cpu;
+
+ for_each_cpu(cpu, mask)
+ __ipi_send_single(get_ipi_desc(cpu, nr), cpu);
+}
+
+static const struct ipi_irq_ops ipi_lpi_ops = {
+ .setup = ipi_lpi_setup,
+ .enable = ipi_lpi_enable,
+ .disable = ipi_lpi_disable,
+ .send = ipi_lpi_send,
+};
+
void __init set_smp_ipi_range_percpu(int ipi_base, int n, int ncpus)
{
int i;
@@ -1139,18 +1179,14 @@ void __init set_smp_ipi_range_percpu(int
ipi_base, int n, int ncpus)
WARN_ON(n < MAX_IPI);
nr_ipi = min(n, MAX_IPI);- percpu_ipi_descs = !!ncpus;
+ ipi_ops = ncpus ? &ipi_lpi_ops : &ipi_sgi_ops;
ipi_irq_base = ipi_base;- for (i = 0; i < nr_ipi; i++) {
- if (!percpu_ipi_descs)
- ipi_setup_sgi(i);
- else
- ipi_setup_lpi(i, ncpus);
- }
+ for (i = 0; i < nr_ipi; i++)
+ ipi_ops->setup(i, ncpus);
/* Setup the boot CPU immediately */
- ipi_setup(smp_processor_id());
+ ipi_enable(smp_processor_id());
}
Quoted 6 lines
arch/arm64/kernel/smp.c +0
-0
arch/arm64/kernel/smp.c static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc); · line 83
Quoted 10 lines
arch/arm64/kernel/smp.c +6
-1@@ -75,11 +75,18 @@ static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);#define get_ipi_desc (__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) -static bool percpu_ipi_descs__ro_after_init ; +struct ipi_irq_ops { +void (*setup)(int ipi,int ncpus); +void (*disable)(int cpu,int ipi); +void (*enable)(int cpu,int ipi); +void (*send)(const cpumask_t *mask,unsigned int nr); +};
Could the order of different callbacks in ipi_sgi_ops and ipi_sgi_ops consistent with this definition?
Quoted 80 lines
arch/arm64/kernel/smp.c +8
-44+ +static const struct ipi_irq_ops *ipi_ops__ro_after_init ;static bool crash_stop; -static void ipi_setup (int cpu); +static void ipi_enable (int cpu);#ifdef CONFIG_HOTPLUG_CPU static void ipi_teardown (int cpu);@@ -240,7 +247,7 @@ asmlinkage notrace void secondary_start_kernel(void)*/ notify_cpu_starting (cpu); -ipi_setup (cpu); +ipi_enable (cpu);numa_add_cpu (cpu);@@ -916,13 +923,7 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regsstatic void arm64_send_ipi (const cpumask_t *mask,unsigned int nr) { -unsigned int cpu; - -if (!percpu_ipi_descs) -__ipi_send_mask (get_ipi_desc (0 , nr), mask); -else -for_each_cpu (cpu, mask) -__ipi_send_single (get_ipi_desc (cpu, nr), cpu); + ipi_ops->send (mask, nr); }static void arm64_backtrace_ipi (cpumask_t *mask)@@ -1048,53 +1049,13 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi) } } -static void ipi_setup (int cpu) -{ -int i; - -if (WARN_ON_ONCE (!ipi_irq_base)) -return ; - -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -prepare_percpu_nmi (ipi_irq_base + i); -enable_percpu_nmi (ipi_irq_base + i,0 ); - }else { -enable_percpu_irq (ipi_irq_base + i,0 ); - } - }else { -enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i))); - } - } -} - -#ifdef CONFIG_HOTPLUG_CPU -static void ipi_teardown (int cpu) +static void ipi_sgi_setup (int ipi,int ncpus) { -int i; +int err, irq, cpu; -if (WARN_ON_ONCE (!ipi_irq_base)) +if (WARN_ON_ONCE (ncpus))return ; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -disable_percpu_nmi (ipi_irq_base + i); -teardown_percpu_nmi (ipi_irq_base + i); - }else { -disable_percpu_irq (ipi_irq_base + i); - } - }else { -disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i)));
arch/arm64/kernel/smp.c static bool ipi_should_be_nmi(enum ipi_msg_type ipi) · line 1060
Quoted 10 lines
arch/arm64/kernel/smp.c +0
-8- } - } -} -#endif - -static void ipi_setup_sgi (int ipi) -{ -int err, irq, cpu; irq = ipi_irq_base + ipi;
An extra blank line.
Quoted 29 lines
arch/arm64/kernel/smp.c +23
-1@@ -1112,7 +1073,50 @@ static void ipi_setup_sgi(int ipi)irq_set_status_flags (irq,IRQ_HIDDEN ); } -static void ipi_setup_lpi (int ipi,int ncpus) +static void ipi_sgi_enable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +prepare_percpu_nmi (ipi_irq_base + ipi); +enable_percpu_nmi (ipi_irq_base + ipi,0 ); + }else { +enable_percpu_irq (ipi_irq_base + ipi,0 ); + } +} + +static void ipi_sgi_disable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +disable_percpu_nmi (ipi_irq_base + ipi); +teardown_percpu_nmi (ipi_irq_base + ipi); + }else { +disable_percpu_irq (ipi_irq_base + ipi); + } +} + +static void ipi_sgi_send (const cpumask_t *mask,unsigned int nr) +{ +__ipi_send_mask (get_ipi_desc (0 , nr), mask);
arch/arm64/kernel/smp.c static void ipi_setup_sgi(int ipi) · line 1108
Quoted 10 lines
arch/arm64/kernel/smp.c +10
-0+} + +static const struct ipi_irq_ops ipi_sgi_ops = { + .disable = ipi_sgi_disable, + .enable = ipi_sgi_enable, + .setup = ipi_sgi_setup, + .send = ipi_sgi_send, +}; + +
An extra blank line.
Best regards,
Jinjie
Quoted 83 lines
arch/arm64/kernel/smp.c +54
-8+static void ipi_lpi_enable (int cpu,int ipi) +{ +enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_disable (int cpu,int ipi) +{ +disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_setup (int ipi,int ncpus) {for (int cpu =0 ; cpu < ncpus; cpu++) {int err, irq;@@ -1132,6 +1136,44 @@ static void ipi_setup_lpi(int ipi, int ncpus) } } +static void ipi_lpi_send (const cpumask_t *mask,unsigned int nr) { +int cpu; + +for_each_cpu (cpu, mask) +__ipi_send_single (get_ipi_desc (cpu, nr), cpu); +} + +static const struct ipi_irq_ops ipi_lpi_ops = { + .disable = ipi_lpi_disable, + .enable = ipi_lpi_enable, + .setup = ipi_lpi_setup, + .send = ipi_lpi_send, +}; + +static void ipi_enable (int cpu) +{ +int ipi; + +if (WARN_ON_ONCE (!ipi_irq_base)) +return ; + +for (ipi =0 ; ipi < nr_ipi; ipi++) + ipi_ops->enable (cpu, ipi); +} + +#ifdef CONFIG_HOTPLUG_CPU +static void ipi_teardown (int cpu) +{ +int ipi; + +if (WARN_ON_ONCE (!ipi_irq_base)) +return ; + +for (ipi =0 ; ipi < nr_ipi; ipi++) + ipi_ops->disable (cpu, ipi); +} +#endif +void __init set_smp_ipi_range_percpu (int ipi_base,int n,int ncpus) {int i;@@ -1139,18 +1181,15 @@ void __init set_smp_ipi_range_percpu(int ipi_base, int n, int ncpus)WARN_ON (n <MAX_IPI ); nr_ipi =min (n,MAX_IPI ); - percpu_ipi_descs = !!ncpus; ipi_irq_base = ipi_base; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) -ipi_setup_sgi (i); -else -ipi_setup_lpi (i, ncpus); - } + ipi_ops = ncpus ? &ipi_lpi_ops : &ipi_sgi_ops; + +for (i =0 ; i < nr_ipi; i++) + ipi_ops->setup (i, ncpus);/* Setup the boot CPU immediately */ -ipi_setup (smp_processor_id ()); +ipi_enable (smp_processor_id ()); }void arch_smp_send_reschedule (int cpu)
Re: [RFC PATCH v2 38/45] arm64: smp: Abstract SGI and LPI operations
Quoted 18 lines · Jinjie Ruan
Quoted 6 lines · Vladimir Murzin
SGI and LPI backed IPIs require different setup, enable, disable and send operations. These differences are currently handled by repeatedly checking
percpu_ipi_descs. As the implementation specific logic grows, these checks make the common IPI code increasingly difficult to follow.Quoted 8 lines
Introduce an operations structure for each implementation to encapsulate the specific of SGI and LPI handling, leaving the common IPI paths generic.
Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/smp.c | 163 +++++++++++++++++++++++++--------------- 1 file changed, 101 insertions(+), 62 deletions(-)
Quoted 6 lines
Hi, Vladimir,
I believe that the modifications here do not need to be so extensive. By maintaining the order of the related function definitions consistent with the original, the changes can be reduced to only 126 lines, and the modifications will be easier to review.
Hi, Vladimir,
What do you think?
Quoted 456 lines
arch/arm64/kernel/smp.c| 126
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------1 file changed, 81 insertions(+), 45 deletions(-)arch/arm64/kernel/smp.c +9
-2@@ -75,11 +75,18 @@ static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);#define get_ipi_desc (__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) -static bool percpu_ipi_descs__ro_after_init ; +struct ipi_irq_ops { +void (*setup)(int ipi,int ncpus); +void (*enable)(int cpu,int ipi); +void (*disable)(int cpu,int ipi); +void (*send)(const cpumask_t *mask,unsigned int nr); +}; + +static const struct ipi_irq_ops *ipi_ops__ro_after_init ;static bool crash_stop; -static void ipi_setup (int cpu); +static void ipi_enable (int cpu);#ifdef
CONFIG_HOTPLUG_CPU
static voidipi_teardown(int cpu);arch/arm64/kernel/smp.c +1
-1@@ -240,7 +247,7 @@ asmlinkage notrace void secondary_start_kernel(void)*/ notify_cpu_starting (cpu); -ipi_setup (cpu); +ipi_enable (cpu);numa_add_cpu (cpu);@@ -916,13 +923,7 @@ static void __noreturn ipi_cpu_crash_stop(unsignedint cpu, struct
pt_regs*regsstatic void
arm64_send_ipi(const cpumask_t *mask, unsigned int nr){arch/arm64/kernel/smp.c +71
-42-unsigned int cpu; - -if (!percpu_ipi_descs) -__ipi_send_mask (get_ipi_desc (0 , nr), mask); -else -for_each_cpu (cpu, mask) -__ipi_send_single (get_ipi_desc (cpu, nr), cpu); + ipi_ops->send (mask, nr); }static void arm64_backtrace_ipi (cpumask_t *mask)@@ -1048,25 +1049,15 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi) } } -static void ipi_setup (int cpu) +static void ipi_enable (int cpu) {int i;if (WARN_ON_ONCE (!ipi_irq_base))return ; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -prepare_percpu_nmi (ipi_irq_base + i); -enable_percpu_nmi (ipi_irq_base + i,0 ); - }else { -enable_percpu_irq (ipi_irq_base + i,0 ); - } - }else { -enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i))); - } - } +for (i =0 ; i < nr_ipi; i++) + ipi_ops->enable (cpu, i); }#ifdef CONFIG_HOTPLUG_CPU @@ -1077,25 +1068,18 @@ static void ipi_teardown(int cpu)if (WARN_ON_ONCE (!ipi_irq_base))return ; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -disable_percpu_nmi (ipi_irq_base + i); -teardown_percpu_nmi (ipi_irq_base + i); - }else { -disable_percpu_irq (ipi_irq_base + i); - } - }else { -disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i))); - } - } +for (i =0 ; i < nr_ipi; i++) + ipi_ops->disable(cpu, i); }#endif -static void ipi_setup_sgi (int ipi) +static void ipi_sgi_setup (int ipi,int ncpus) {int err, irq, cpu; +if (WARN_ON_ONCE (ncpus)) +return ; + irq = ipi_irq_base + ipi;if (ipi_should_be_nmi (ipi)) {@@ -1112,7 +1096,39 @@ static void ipi_setup_sgi(int ipi)irq_set_status_flags (irq,IRQ_HIDDEN ); } -static void ipi_setup_lpi (int ipi,int ncpus) +static void ipi_sgi_enable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +prepare_percpu_nmi (ipi_irq_base + ipi); +enable_percpu_nmi (ipi_irq_base + ipi,0 ); + }else { +enable_percpu_irq (ipi_irq_base + ipi,0 ); + } +} + +static void ipi_sgi_disable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +disable_percpu_nmi (ipi_irq_base + ipi); +teardown_percpu_nmi (ipi_irq_base + ipi); + }else { +disable_percpu_irq (ipi_irq_base + ipi); + } +} + +static void ipi_sgi_send (const cpumask_t *mask,unsigned int nr) +{ +__ipi_send_mask (get_ipi_desc (0 , nr), mask); +} + +static const struct ipi_irq_ops ipi_sgi_ops = { + .setup = ipi_sgi_setup, + .enable = ipi_sgi_enable, + .disable = ipi_sgi_disable, + .send = ipi_sgi_send, +}; + +static void ipi_lpi_setup (int ipi,int ncpus) {for (int cpu =0 ; cpu < ncpus; cpu++) {int err, irq;@@ -1132,6 +1148,30 @@ static void ipi_setup_lpi(int ipi, int ncpus) } } +static void ipi_lpi_enable (int cpu,int ipi) +{ +enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_disable (int cpu,int ipi) +{ +disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_send (const cpumask_t *mask,unsigned int nr) { +int cpu; + +for_each_cpu (cpu, mask) +__ipi_send_single (get_ipi_desc (cpu, nr), cpu); +} + +static const struct ipi_irq_ops ipi_lpi_ops = { + .setup = ipi_lpi_setup, + .enable = ipi_lpi_enable, + .disable = ipi_lpi_disable, + .send = ipi_lpi_send, +}; +void __init set_smp_ipi_range_percpu (int ipi_base,int n,int ncpus) {int i;@@ -1139,18 +1179,14 @@ void __init set_smp_ipi_range_percpu(int ipi_base, int n, int ncpus)WARN_ON (n <MAX_IPI ); nr_ipi =min (n,MAX_IPI ); - percpu_ipi_descs = !!ncpus; + ipi_ops = ncpus ? &ipi_lpi_ops : &ipi_sgi_ops; ipi_irq_base = ipi_base; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) -ipi_setup_sgi (i); -else -ipi_setup_lpi (i, ncpus); - } +for (i =0 ; i < nr_ipi; i++) + ipi_ops->setup (i, ncpus);/* Setup the boot CPU immediately */ -ipi_setup (smp_processor_id ()); +ipi_enable (smp_processor_id ()); }Quoted 7 lines
arch/arm64/kernel/smp.c +0
-0arch/arm64/kernel/smp.c static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc); · line 83
Quoted 10 lines
arch/arm64/kernel/smp.c +6
-1@@ -75,11 +75,18 @@ static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);#define get_ipi_desc (__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) -static bool percpu_ipi_descs__ro_after_init ; +struct ipi_irq_ops { +void (*setup)(int ipi,int ncpus); +void (*disable)(int cpu,int ipi); +void (*enable)(int cpu,int ipi); +void (*send)(const cpumask_t *mask,unsigned int nr); +};Could the order of different callbacks in
ipi_sgi_opsandipi_sgi_opsconsistent with this definition?Quoted 80 lines
arch/arm64/kernel/smp.c +8
-44+ +static const struct ipi_irq_ops *ipi_ops__ro_after_init ;static bool crash_stop; -static void ipi_setup (int cpu); +static void ipi_enable (int cpu);#ifdef CONFIG_HOTPLUG_CPU static void ipi_teardown (int cpu);@@ -240,7 +247,7 @@ asmlinkage notrace void secondary_start_kernel(void)*/ notify_cpu_starting (cpu); -ipi_setup (cpu); +ipi_enable (cpu);numa_add_cpu (cpu);@@ -916,13 +923,7 @@ static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regsstatic void arm64_send_ipi (const cpumask_t *mask,unsigned int nr) { -unsigned int cpu; - -if (!percpu_ipi_descs) -__ipi_send_mask (get_ipi_desc (0 , nr), mask); -else -for_each_cpu (cpu, mask) -__ipi_send_single (get_ipi_desc (cpu, nr), cpu); + ipi_ops->send (mask, nr); }static void arm64_backtrace_ipi (cpumask_t *mask)@@ -1048,53 +1049,13 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi) } } -static void ipi_setup (int cpu) -{ -int i; - -if (WARN_ON_ONCE (!ipi_irq_base)) -return ; - -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -prepare_percpu_nmi (ipi_irq_base + i); -enable_percpu_nmi (ipi_irq_base + i,0 ); - }else { -enable_percpu_irq (ipi_irq_base + i,0 ); - } - }else { -enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i))); - } - } -} - -#ifdef CONFIG_HOTPLUG_CPU -static void ipi_teardown (int cpu) +static void ipi_sgi_setup (int ipi,int ncpus) { -int i; +int err, irq, cpu; -if (WARN_ON_ONCE (!ipi_irq_base)) +if (WARN_ON_ONCE (ncpus))return ; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) { -if (ipi_should_be_nmi (i)) { -disable_percpu_nmi (ipi_irq_base + i); -teardown_percpu_nmi (ipi_irq_base + i); - }else { -disable_percpu_irq (ipi_irq_base + i); - } - }else { -disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, i)));arch/arm64/kernel/smp.c static bool ipi_should_be_nmi(enum ipi_msg_type ipi) · line 1060
Quoted 10 lines
arch/arm64/kernel/smp.c +0
-8- } - } -} -#endif - -static void ipi_setup_sgi (int ipi) -{ -int err, irq, cpu; irq = ipi_irq_base + ipi;An extra blank line.
Quoted 29 lines
arch/arm64/kernel/smp.c +23
-1@@ -1112,7 +1073,50 @@ static void ipi_setup_sgi(int ipi)irq_set_status_flags (irq,IRQ_HIDDEN ); } -static void ipi_setup_lpi (int ipi,int ncpus) +static void ipi_sgi_enable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +prepare_percpu_nmi (ipi_irq_base + ipi); +enable_percpu_nmi (ipi_irq_base + ipi,0 ); + }else { +enable_percpu_irq (ipi_irq_base + ipi,0 ); + } +} + +static void ipi_sgi_disable (int cpu,int ipi) +{ +if (ipi_should_be_nmi (ipi)) { +disable_percpu_nmi (ipi_irq_base + ipi); +teardown_percpu_nmi (ipi_irq_base + ipi); + }else { +disable_percpu_irq (ipi_irq_base + ipi); + } +} + +static void ipi_sgi_send (const cpumask_t *mask,unsigned int nr) +{ +__ipi_send_mask (get_ipi_desc (0 , nr), mask);arch/arm64/kernel/smp.c static void ipi_setup_sgi(int ipi) · line 1108
Quoted 10 lines
arch/arm64/kernel/smp.c +10
-0+} + +static const struct ipi_irq_ops ipi_sgi_ops = { + .disable = ipi_sgi_disable, + .enable = ipi_sgi_enable, + .setup = ipi_sgi_setup, + .send = ipi_sgi_send, +}; + +An extra blank line.
Best regards,
JinjieQuoted 83 lines
arch/arm64/kernel/smp.c +54
-8+static void ipi_lpi_enable (int cpu,int ipi) +{ +enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_disable (int cpu,int ipi) +{ +disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +} + +static void ipi_lpi_setup (int ipi,int ncpus) {for (int cpu =0 ; cpu < ncpus; cpu++) {int err, irq;@@ -1132,6 +1136,44 @@ static void ipi_setup_lpi(int ipi, int ncpus) } } +static void ipi_lpi_send (const cpumask_t *mask,unsigned int nr) { +int cpu; + +for_each_cpu (cpu, mask) +__ipi_send_single (get_ipi_desc (cpu, nr), cpu); +} + +static const struct ipi_irq_ops ipi_lpi_ops = { + .disable = ipi_lpi_disable, + .enable = ipi_lpi_enable, + .setup = ipi_lpi_setup, + .send = ipi_lpi_send, +}; + +static void ipi_enable (int cpu) +{ +int ipi; + +if (WARN_ON_ONCE (!ipi_irq_base)) +return ; + +for (ipi =0 ; ipi < nr_ipi; ipi++) + ipi_ops->enable (cpu, ipi); +} + +#ifdef CONFIG_HOTPLUG_CPU +static void ipi_teardown (int cpu) +{ +int ipi; + +if (WARN_ON_ONCE (!ipi_irq_base)) +return ; + +for (ipi =0 ; ipi < nr_ipi; ipi++) + ipi_ops->disable (cpu, ipi); +} +#endif +void __init set_smp_ipi_range_percpu (int ipi_base,int n,int ncpus) {int i;@@ -1139,18 +1181,15 @@ void __init set_smp_ipi_range_percpu(int ipi_base, int n, int ncpus)WARN_ON (n <MAX_IPI ); nr_ipi =min (n,MAX_IPI ); - percpu_ipi_descs = !!ncpus; ipi_irq_base = ipi_base; -for (i =0 ; i < nr_ipi; i++) { -if (!percpu_ipi_descs) -ipi_setup_sgi (i); -else -ipi_setup_lpi (i, ncpus); - } + ipi_ops = ncpus ? &ipi_lpi_ops : &ipi_sgi_ops; + +for (i =0 ; i < nr_ipi; i++) + ipi_ops->setup (i, ncpus);/* Setup the boot CPU immediately */ -ipi_setup (smp_processor_id ()); +ipi_enable (smp_processor_id ()); }void arch_smp_send_reschedule (int cpu)
Re: [RFC PATCH v2 38/45] arm64: smp: Abstract SGI and LPI operations
Quoted 18 lines · Jinjie Ruan
Quoted 16 lines · Jinjie Ruan
Quoted 14 lines · Vladimir Murzin
SGI and LPI backed IPIs require different setup, enable, disable and send operations. These differences are currently handled by repeatedly checking
percpu_ipi_descs. As the implementation specific logic grows, these checks make the common IPI code increasingly difficult to follow.Introduce an operations structure for each implementation to encapsulate the specific of SGI and LPI handling, leaving the common IPI paths generic.
Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/smp.c | 163 +++++++++++++++++++++++++--------------- 1 file changed, 101 insertions(+), 62 deletions(-)
Quoted 6 lines
Quoted 6 lines
Hi, Vladimir,
I believe that the modifications here do not need to be so extensive. By maintaining the order of the related function definitions consistent with the original, the changes can be reduced to only 126 lines, and the modifications will be easier to review.
Hi Jinjie,
Quoted 3 lines
Hi, Vladimir,
What do you think?
I think that perhaps we should pay more attention to the end result after applying the patch. However, I understand why diff looks like that and amount of work required to shrink it is close to zero, so in next version stat would look like
arch/arm64/kernel/smp.c | 135 ++++++++++++++++++++++++++-------------- 1 file changed, 88 insertions(+), 47 deletions(-)
Cheers
Vladimir
RFC PATCH v2 39/45 arm64: smp: Fall back to IRQ when IPI NMI request fails
In reply to Vladimir MurzinExisting IPI setup logic relies on ipi_should_be_nmi() to decide whether to request an NMI (if supported) or an ordinary IRQ. If NMI is not supported, using an ordinary IRQ is acceptable.
FEAT_NMI requires complementary support in the interrupt controller. If FEAT_NMI is present but interrupt controller support is absent, request_percpu_nmi() can fail.
One way to prevent this would be to extend ipi_should_be_nmi() to also check for interrupt controller support. However, since falling back to an ordinary IRQ is acceptable, treat ipi_should_be_nmi() as a hint instead. Whether an IPI is actually registered as an NMI is determined by the success or failure of request_percpu_nmi(). If the request fails, fall back to request_irq().
Track which IPIs were actually registered as NMIs and use that state in the per-CPU enable and disable paths. This avoids calling the percpu NMI helpers for an IPI that fell back to regular IRQ handling.
--- arch/arm64/kernel/smp.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
arch/arm64/kernel/smp.c +23 -8
@@ -69,11 +69,13 @@ static int nr_ipi __ro_after_init = NR_IPI;struct ipi_descs {struct irq_desc *descs[MAX_IPI ]; + DECLARE_BITMAP(nmi_bitmap, MAX_IPI); }; static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);#define get_ipi_desc (__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) +#define get_ipi_nmi_bitmap (__cpu) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->nmi_bitmap)struct ipi_irq_ops {void (*setup)(int ipi,int ncpus);
@@ -1051,31 +1053,44 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi)static void ipi_sgi_setup (int ipi,int ncpus) { +bool is_nmi =ipi_should_be_nmi (ipi);int err, irq, cpu;if (WARN_ON_ONCE (ncpus))return ; - irq = ipi_irq_base + ipi; -if (ipi_should_be_nmi (ipi)) { - err =request_percpu_nmi (irq, ipi_handler,"IPI" ,NULL , &irq_stat); -WARN (err,"Could not request IRQ %d as NMI, err=%d\n" , irq, err); - }else { +/* +* Try to request NMI if it should be +*/ +if (is_nmi &&request_percpu_nmi (irq, ipi_handler,"IPI" ,NULL , &irq_stat)) { +/* +* Fallback to regular IRQ +*/ + is_nmi = false; + } + +/* +* Otherwise regular IRQ +*/ +if (!is_nmi) { err =request_percpu_irq (irq, ipi_handler,"IPI" , &irq_stat);WARN (err,"Could not request IRQ %d as IRQ, err=%d\n" , irq, err); } -for_each_possible_cpu (cpu) +for_each_possible_cpu (cpu) {get_ipi_desc (cpu, ipi) =irq_to_desc (irq); +if (is_nmi) +set_bit (ipi,get_ipi_nmi_bitmap (cpu)); + }irq_set_status_flags (irq,IRQ_HIDDEN ); }static void ipi_sgi_enable (int cpu,int ipi) { -if (ipi_should_be_nmi (ipi)) { +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) {prepare_percpu_nmi (ipi_irq_base + ipi);enable_percpu_nmi (ipi_irq_base + ipi,0 ); }else {
@@ -1085,7 +1100,7 @@ static void ipi_sgi_enable(int cpu, int ipi)static void ipi_sgi_disable (int cpu,int ipi) { -if (ipi_should_be_nmi (ipi)) { +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) {disable_percpu_nmi (ipi_irq_base + ipi);teardown_percpu_nmi (ipi_irq_base + ipi); }else {
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 39/45] arm64: smp: Fall back to IRQ when IPI NMI request fails
Quoted 15 lines · Vladimir Murzin
Existing IPI setup logic relies on
ipi_should_be_nmi()to decide whether to request an NMI (if supported) or an ordinary IRQ. If NMI is not supported, using an ordinary IRQ is acceptable.
FEAT_NMIrequires complementary support in the interrupt controller. IfFEAT_NMIis present but interrupt controller support is absent,request_percpu_nmi()can fail.One way to prevent this would be to extend
ipi_should_be_nmi()to also check for interrupt controller support. However, since falling back to an ordinary IRQ is acceptable, treatipi_should_be_nmi()as a hint instead. Whether an IPI is actually registered as an NMI is determined by the success or failure ofrequest_percpu_nmi(). If the request fails, fall back torequest_irq().
Quoted 3 lines
Track which IPIs were actually registered as NMIs and use that state in the per-CPU enable and disable paths. This avoids calling the percpu NMI helpers for an IPI that fell back to regular IRQ handling.
Hi Vladimir,
Should we update the following comment and mention that "regular IPI will also be used when the nmi_bitmap is not set"?
936 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu)
937 {
938 >-------/*
939 >------- * NOTE: though nmi_trigger_cpumask_backtrace() has "nmi_"
in the name,
940 >------- * nothing about it truly needs to be implemented using an
NMI, it's
941 >------- * just that it's _allowed_ to work with NMIs. Ifipi_should_be_nmi()
942 >------- * returned false our backtrace attempt will just use a
regular IPI.
943 >------- */
944 >-------nmi_trigger_cpumask_backtrace(mask, exclude_cpu,arm64_backtrace_ipi);
945 }
otherwise, LGTM
Quoted 85 lines
Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/smp.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)arch/arm64/kernel/smp.c +23
-8@@ -69,11 +69,13 @@ static int nr_ipi __ro_after_init = NR_IPI;struct ipi_descs {struct irq_desc *descs[MAX_IPI ]; + DECLARE_BITMAP(nmi_bitmap, MAX_IPI); }; static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);#define get_ipi_desc (__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) +#define get_ipi_nmi_bitmap (__cpu) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->nmi_bitmap)struct ipi_irq_ops {void (*setup)(int ipi,int ncpus);@@ -1051,31 +1053,44 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi)static void ipi_sgi_setup (int ipi,int ncpus) { +bool is_nmi =ipi_should_be_nmi (ipi);int err, irq, cpu;if (WARN_ON_ONCE (ncpus))return ; - irq = ipi_irq_base + ipi; -if (ipi_should_be_nmi (ipi)) { - err =request_percpu_nmi (irq, ipi_handler,"IPI" ,NULL , &irq_stat); -WARN (err,"Could not request IRQ %d as NMI, err=%d\n" , irq, err); - }else { +/* +* Try to request NMI if it should be +*/ +if (is_nmi &&request_percpu_nmi (irq, ipi_handler,"IPI" ,NULL , &irq_stat)) { +/* +* Fallback to regular IRQ +*/ + is_nmi = false; + } + +/* +* Otherwise regular IRQ +*/ +if (!is_nmi) { err =request_percpu_irq (irq, ipi_handler,"IPI" , &irq_stat);WARN (err,"Could not request IRQ %d as IRQ, err=%d\n" , irq, err); } -for_each_possible_cpu (cpu) +for_each_possible_cpu (cpu) {get_ipi_desc (cpu, ipi) =irq_to_desc (irq); +if (is_nmi) +set_bit (ipi,get_ipi_nmi_bitmap (cpu)); + }irq_set_status_flags (irq,IRQ_HIDDEN ); }static void ipi_sgi_enable (int cpu,int ipi) { -if (ipi_should_be_nmi (ipi)) { +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) {prepare_percpu_nmi (ipi_irq_base + ipi);enable_percpu_nmi (ipi_irq_base + ipi,0 ); }else {@@ -1085,7 +1100,7 @@ static void ipi_sgi_enable(int cpu, int ipi)static void ipi_sgi_disable (int cpu,int ipi) { -if (ipi_should_be_nmi (ipi)) { +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) {disable_percpu_nmi (ipi_irq_base + ipi);teardown_percpu_nmi (ipi_irq_base + ipi); }else {
Re: [RFC PATCH v2 39/45] arm64: smp: Fall back to IRQ when IPI NMI request fails
Quoted 25 lines · Jinjie Ruan
Quoted 15 lines · Vladimir Murzin
Existing IPI setup logic relies on
ipi_should_be_nmi()to decide whether to request an NMI (if supported) or an ordinary IRQ. If NMI is not supported, using an ordinary IRQ is acceptable.
FEAT_NMIrequires complementary support in the interrupt controller. IfFEAT_NMIis present but interrupt controller support is absent,request_percpu_nmi()can fail.One way to prevent this would be to extend
ipi_should_be_nmi()to also check for interrupt controller support. However, since falling back to an ordinary IRQ is acceptable, treatipi_should_be_nmi()as a hint instead. Whether an IPI is actually registered as an NMI is determined by the success or failure ofrequest_percpu_nmi(). If the request fails, fall back torequest_irq().Quoted 3 lines
Track which IPIs were actually registered as NMIs and use that state in the per-CPU enable and disable paths. This avoids calling the percpu NMI helpers for an IPI that fell back to regular IRQ handling.
Hi Vladimir,
Should we update the following comment and mention that "regular IPI will also be used when the
nmi_bitmapis not set"?
Quoted 17 lines
936 void
arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu)
937 {
938 >-------/*
939 >------- * NOTE: thoughnmi_trigger_cpumask_backtrace()has "nmi_"
in the name,
940 >------- * nothing about it truly needs to be implemented using an
NMI, it's
941 >------- * just that it's _allowed_ to work with NMIs. Ifipi_should_be_nmi()
942 >------- * returned false our backtrace attempt will just use a
regular IPI.
943 >------- */
944 >-------nmi_trigger_cpumask_backtrace(mask,exclude_cpu,arm64_backtrace_ipi);
945 }
Something like
- * just that it's _allowed_ to work with NMIs. If ipi_should_be_nmi() - * returned false our backtrace attempt will just use a regular IPI. + * just that it's _allowed_ to work with NMIs. If we failed request + * NMI our backtrace attempt will just use a regular IPI.
Quoted 2 lines
otherwise, LGTM
Reviewed-by Jinjie Ruan <[email protected]>
Cheers
Vladimir
Re: [RFC PATCH v2 39/45] arm64: smp: Fall back to IRQ when IPI NMI request fails
In reply to Vladimir MurzinQuoted 15 lines · Vladimir Murzin
Existing IPI setup logic relies on
ipi_should_be_nmi()to decide whether to request an NMI (if supported) or an ordinary IRQ. If NMI is not supported, using an ordinary IRQ is acceptable.
FEAT_NMIrequires complementary support in the interrupt controller. IfFEAT_NMIis present but interrupt controller support is absent,request_percpu_nmi()can fail.One way to prevent this would be to extend
ipi_should_be_nmi()to also check for interrupt controller support. However, since falling back to an ordinary IRQ is acceptable, treatipi_should_be_nmi()as a hint instead. Whether an IPI is actually registered as an NMI is determined by the success or failure ofrequest_percpu_nmi(). If the request fails, fall back torequest_irq().
Quoted 5 lines
Track which IPIs were actually registered as NMIs and use that state in the per-CPU enable and disable paths. This avoids calling the percpu NMI helpers for an IPI that fell back to regular IRQ handling.
Signed-off-by Vladimir Murzin <[email protected]>
Should we replace ipi_should_be_nmi() with test_bit(ipi, get_ipi_nmi_bitmap(cpu) in smp_send_stop()?
irq_desc_get_irq(get_ipi_desc(cpu, ipi)) has been used more than 3 times, can we define a new macro for use?
git grep "irq_desc_get_irq(get_ipi_desc(cpu, ipi))" arch/arm64/kernel/smp.c:enable_nmi(irq_desc_get_irq(get_ipi_desc(cpu, ipi))); arch/arm64/kernel/smp.c:enable_irq(irq_desc_get_irq(get_ipi_desc(cpu, ipi))); arch/arm64/kernel/smp.c:disable_nmi_nosync(irq_desc_get_irq(get_ipi_desc(cpu, ipi))); arch/arm64/kernel/smp.c:disable_irq(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));
Quoted 83 lines
--- arch/arm64/kernel/smp.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)arch/arm64/kernel/smp.c +23
-8@@ -69,11 +69,13 @@ static int nr_ipi __ro_after_init = NR_IPI;struct ipi_descs {struct irq_desc *descs[MAX_IPI ]; + DECLARE_BITMAP(nmi_bitmap, MAX_IPI); }; static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);#define get_ipi_desc (__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) +#define get_ipi_nmi_bitmap (__cpu) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->nmi_bitmap)struct ipi_irq_ops {void (*setup)(int ipi,int ncpus);@@ -1051,31 +1053,44 @@ static bool ipi_should_be_nmi(enum ipi_msg_type ipi)static void ipi_sgi_setup (int ipi,int ncpus) { +bool is_nmi =ipi_should_be_nmi (ipi);int err, irq, cpu;if (WARN_ON_ONCE (ncpus))return ; - irq = ipi_irq_base + ipi; -if (ipi_should_be_nmi (ipi)) { - err =request_percpu_nmi (irq, ipi_handler,"IPI" ,NULL , &irq_stat); -WARN (err,"Could not request IRQ %d as NMI, err=%d\n" , irq, err); - }else { +/* +* Try to request NMI if it should be +*/ +if (is_nmi &&request_percpu_nmi (irq, ipi_handler,"IPI" ,NULL , &irq_stat)) { +/* +* Fallback to regular IRQ +*/ + is_nmi = false; + } + +/* +* Otherwise regular IRQ +*/ +if (!is_nmi) { err =request_percpu_irq (irq, ipi_handler,"IPI" , &irq_stat);WARN (err,"Could not request IRQ %d as IRQ, err=%d\n" , irq, err); } -for_each_possible_cpu (cpu) +for_each_possible_cpu (cpu) {get_ipi_desc (cpu, ipi) =irq_to_desc (irq); +if (is_nmi) +set_bit (ipi,get_ipi_nmi_bitmap (cpu)); + }irq_set_status_flags (irq,IRQ_HIDDEN ); }static void ipi_sgi_enable (int cpu,int ipi) { -if (ipi_should_be_nmi (ipi)) { +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) {prepare_percpu_nmi (ipi_irq_base + ipi);enable_percpu_nmi (ipi_irq_base + ipi,0 ); }else {@@ -1085,7 +1100,7 @@ static void ipi_sgi_enable(int cpu, int ipi)static void ipi_sgi_disable (int cpu,int ipi) { -if (ipi_should_be_nmi (ipi)) { +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) {disable_percpu_nmi (ipi_irq_base + ipi);teardown_percpu_nmi (ipi_irq_base + ipi); }else {
Re: [RFC PATCH v2 39/45] arm64: smp: Fall back to IRQ when IPI NMI request fails
Quoted 17 lines · Jinjie Ruan
Quoted 15 lines · Vladimir Murzin
Existing IPI setup logic relies on
ipi_should_be_nmi()to decide whether to request an NMI (if supported) or an ordinary IRQ. If NMI is not supported, using an ordinary IRQ is acceptable.
FEAT_NMIrequires complementary support in the interrupt controller. IfFEAT_NMIis present but interrupt controller support is absent,request_percpu_nmi()can fail.One way to prevent this would be to extend
ipi_should_be_nmi()to also check for interrupt controller support. However, since falling back to an ordinary IRQ is acceptable, treatipi_should_be_nmi()as a hint instead. Whether an IPI is actually registered as an NMI is determined by the success or failure ofrequest_percpu_nmi(). If the request fails, fall back torequest_irq().
Quoted 9 lines
Quoted 5 lines
Track which IPIs were actually registered as NMIs and use that state in the per-CPU enable and disable paths. This avoids calling the percpu NMI helpers for an IPI that fell back to regular IRQ handling.
Signed-off-by Vladimir Murzin <[email protected]>Should we replace
ipi_should_be_nmi()with test_bit(ipi,get_ipi_nmi_bitmap(cpu)insmp_send_stop()?
Right. We can do something like
@@ -1312,7 +1328,7 @@ void smp_send_stop(void)
* If CPUs are still online, try an NMI. There's no excuse for this to
* be slow, so we only give them an extra 10 ms to respond. */
- if (num_other_online_cpus() && ipi_should_be_nmi(IPI_CPU_STOP_NMI)) {
+ if (num_other_online_cpus() && ipi_is_nmi(smp_processor_id(), IPI_CPU_STOP_NMI)) {
smp_rmb();
cpumask_copy(&mask, cpu_online_mask);
cpumask_clear_cpu(smp_processor_id(), &mask);where
+#define ipi_is_nmi(__cpu, __ipi) test_bit(__ipi, get_ipi_nmi_bitmap(__cpu))
can be used in other places to simpify the code
Quoted 14 lines
irq_desc_get_irq(get_ipi_desc(cpu, ipi))has been used more than 3 times, can we define a new macro for use?git grep "
irq_desc_get_irq(get_ipi_desc(cpu, ipi))"arch/arm64/kernel/smp.c:enable_nmi(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));arch/arm64/kernel/smp.c:enable_irq(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));arch/arm64/kernel/smp.c:disable_nmi_nosync(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));arch/arm64/kernel/smp.c:disable_irq(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));
I assume you did grep after full series applied, since irq_desc_get_irq(get_ipi_desc(cpu, ipi)) comes from LPI flow.
Anyway, we can easily avoid some duplication and get usage of irq_desc_get_irq(get_ipi_desc(cpu, ipi)) back to two
$ git grep "irq_desc_get_irq(get_ipi_desc(cpu, ipi))" arch/arm64/kernel/smp.c: int irq = irq_desc_get_irq(get_ipi_desc(cpu, ipi)); arch/arm64/kernel/smp.c: int irq = irq_desc_get_irq(get_ipi_desc(cpu, ipi));
Cheers
Vladimir
Re: [RFC PATCH v2 39/45] arm64: smp: Fall back to IRQ when IPI NMI request fails
Quoted 40 lines · Vladimir Murzin
Quoted 17 lines · Jinjie Ruan
Quoted 15 lines · Vladimir Murzin
Existing IPI setup logic relies on
ipi_should_be_nmi()to decide whether to request an NMI (if supported) or an ordinary IRQ. If NMI is not supported, using an ordinary IRQ is acceptable.
FEAT_NMIrequires complementary support in the interrupt controller. IfFEAT_NMIis present but interrupt controller support is absent,request_percpu_nmi()can fail.One way to prevent this would be to extend
ipi_should_be_nmi()to also check for interrupt controller support. However, since falling back to an ordinary IRQ is acceptable, treatipi_should_be_nmi()as a hint instead. Whether an IPI is actually registered as an NMI is determined by the success or failure ofrequest_percpu_nmi(). If the request fails, fall back torequest_irq().Quoted 9 lines
Quoted 5 lines
Track which IPIs were actually registered as NMIs and use that state in the per-CPU enable and disable paths. This avoids calling the percpu NMI helpers for an IPI that fell back to regular IRQ handling.
Signed-off-by Vladimir Murzin <[email protected]>Should we replace
ipi_should_be_nmi()with test_bit(ipi,get_ipi_nmi_bitmap(cpu)insmp_send_stop()?Right. We can do something like
(quoted diff) +1
-1@@ -1312,7 +1328,7 @@ void smp_send_stop(void) * If CPUs are still online, try an NMI. There's no excuse for this to * be slow, so we only give them an extra 10 ms to respond. */ - if (num_other_online_cpus() && ipi_should_be_nmi(IPI_CPU_STOP_NMI)) { + if (num_other_online_cpus() && ipi_is_nmi(smp_processor_id(), IPI_CPU_STOP_NMI)) { smp_rmb(); cpumask_copy(&mask, cpu_online_mask); cpumask_clear_cpu(smp_processor_id(), &mask);
Quoted 5 lines
where
+#define
ipi_is_nmi(__cpu, __ipi)test_bit(__ipi, get_ipi_nmi_bitmap(__cpu))can be used in other places to simpify the code
Agree.
Quoted 16 lines
Quoted 4 lines
irq_desc_get_irq(get_ipi_desc(cpu, ipi))has been used more than 3 times, can we define a new macro for use?Quoted 11 lines
git grep "
irq_desc_get_irq(get_ipi_desc(cpu, ipi))"arch/arm64/kernel/smp.c:enable_nmi(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));arch/arm64/kernel/smp.c:enable_irq(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));arch/arm64/kernel/smp.c:disable_nmi_nosync(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));arch/arm64/kernel/smp.c:disable_irq(irq_desc_get_irq(get_ipi_desc(cpu, ipi)));
Quoted 5 lines
I assume you did grep after full series applied, since
irq_desc_get_irq(get_ipi_desc(cpu, ipi))comes from LPI flow.Anyway, we can easily avoid some duplication and get usage of
irq_desc_get_irq(get_ipi_desc(cpu, ipi))back to two
Anyway, it is always good to avoid repetition.
Quoted 9 lines
$ git grep "irq_desc_get_irq(get_ipi_desc(cpu, ipi))" arch/arm64/kernel/smp.c: int irq = irq_desc_get_irq(get_ipi_desc(cpu, ipi)); arch/arm64/kernel/smp.c: int irq = irq_desc_get_irq(get_ipi_desc(cpu, ipi));Cheers
Vladimir
RFC PATCH v2 40/45 arm64: nmi: Add Kconfig for NMI
In reply to Vladimir MurzinFrom: Mark Brown <[email protected]>
Since NMI handling is in some fairly hot paths we provide a Kconfig option which allows support to be compiled out when not needed.
--- arch/arm64/Kconfig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/arm64/Kconfig +18 -0
@@ -2195,6 +2195,24 @@ endmenu # "ARMv8.7 architectural features" config AS_HAS_MOPS def_bool $(as-instr,.arch_extension mops) +menu "ARMv8.8 architectural features" + +config ARM64_NMI + bool "Enable support for Non-maskable Interrupts (NMI)" + default y + help + Non-maskable interrupts are an architecture and GIC feature + which allow the system to configure some interrupts to be + configured to have superpriority, allowing them to be handled + before other interrupts and masked for shorter periods of time. + + The feature is detected at runtime, and will remain disabled + if the cpu does not implement the feature. It will also be + disabled if pseudo NMIs are enabled at runtime. + +endmenu # "ARMv8.8 architectural features" + + menu "ARMv8.9 architectural features" config ARM64_POE
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 40/45] arm64: nmi: Add Kconfig for NMI
Quoted 17 lines · Vladimir Murzin
From: Mark Brown <[email protected]>
Since NMI handling is in some fairly hot paths we provide a
Kconfigoption which allows support to be compiled out when not needed.Signed-off-by Mark Brown <[email protected]>Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/Kconfig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)arch/arm64/Kconfig +0
-0
arch/arm64/Kconfig endmenu # "ARMv8.7 architectural features" · line 2202
Quoted 8 lines
arch/arm64/Kconfig +5
-0@@ -2195,6 +2195,24 @@ endmenu # "ARMv8.7 architectural features" config AS_HAS_MOPS def_bool $(as-instr,.arch_extension mops) +menu "ARMv8.8 architectural features" + +config ARM64_NMI + bool "Enable support for Non-maskable Interrupts (NMI)" + default y
I think we should select ARM_GIC_V3 here, as FEAT_GICv3_NMI need GICv3
arch/arm64/Kconfig endmenu # "ARMv8.7 architectural features" · line 2204
Quoted 2 lines
arch/arm64/Kconfig +2
-0+ help + Non-maskable interrupts are an architecture and GIC feature
GICv3 feature
Quoted 14 lines
arch/arm64/Kconfig +11
-0+ which allow the system to configure some interrupts to be + configured to have superpriority, allowing them to be handled + before other interrupts and masked for shorter periods of time. + + The feature is detected at runtime, and will remain disabled + if the cpu does not implement the feature. It will also be + disabled if pseudo NMIs are enabled at runtime. + +endmenu # "ARMv8.8 architectural features" + + menu "ARMv8.9 architectural features" config ARM64_POE
Re: [RFC PATCH v2 40/45] arm64: nmi: Add Kconfig for NMI
Quoted 8 lines · Jinjie Ruan
arch/arm64/Kconfig endmenu # "ARMv8.7 architectural features" · line 2202
Quoted 3 lines
arch/arm64/Kconfig +3
-0+config ARM64_NMI + bool "Enable support for Non-maskable Interrupts (NMI)" + default yI think we should select
ARM_GIC_V3here, asFEAT_GICv3_NMIneed GICv3arch/arm64/Kconfig endmenu # "ARMv8.7 architectural features" · line 2204
Quoted 2 lines
arch/arm64/Kconfig +2
-0+ help + Non-maskable interrupts are an architecture and GIC featureGICv3 feature
Not really, GICv5 also supports NMI.
Thanks
Vladimir
Re: [RFC PATCH v2 40/45] arm64: nmi: Add Kconfig for NMI
Quoted 11 lines · Vladimir Murzin
Quoted 8 lines · Jinjie Ruan
arch/arm64/Kconfig endmenu # "ARMv8.7 architectural features" · line 2202
Quoted 3 lines
arch/arm64/Kconfig +3
-0+config ARM64_NMI + bool "Enable support for Non-maskable Interrupts (NMI)" + default yI think we should select
ARM_GIC_V3here, asFEAT_GICv3_NMIneed GICv3arch/arm64/Kconfig endmenu # "ARMv8.7 architectural features" · line 2204
Quoted 2 lines
arch/arm64/Kconfig +2
-0+ help + Non-maskable interrupts are an architecture and GIC featureGICv3 feature
Not really, GICv5 also supports NMI.
Oh, I forgot about this newest interrupt controller.
Quoted 4 lines
Thanks
Vladimir
RFC PATCH v2 41/45 irqchip/gic-v3: Prepare for FEAT_GICv3_NMI support
In reply to Vladimir MurzinFrom: Lorenzo Pieralisi <[email protected]>
Support for FEAT_GICv3_NMI is coming. Currently, the terms "NMI" and "pseudo-NMI" are used interchangeably. Rename helper functions and variables related to pseudo-NMI to make the code paths clearer and more explicit.
--- drivers/irqchip/irq-gic-v3.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/irqchip/irq-gic-v3.c +14 -14
@@ -87,7 +87,7 @@ static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key);#define GIC_LINE_NR min(GICD_TYPER_SPIS(gic_data.rdists.gicd_typer), 1020U)#define GIC_ESPI_NR GICD_TYPER_ESPIS(gic_data.rdists.gicd_typer) -static bool nmi_support_forbidden; +static bool pseudo_nmi_support_forbidden;/* * There are 16 SGIs, though we only actually use 8 in Linux. The other 8 SGIs
@@ -117,7 +117,7 @@ static bool nmi_support_forbidden;* - Figure 4-7 Secure read of the priority field for a Non-secure Group 1 * interrupt. */ -static DEFINE_STATIC_KEY_FALSE (supports_pseudo_nmis); +static DEFINE_STATIC_KEY_FALSE (supports_pseudo_nmi);static u32 gic_get_pribits (void ) {
@@ -185,7 +185,7 @@ static void __init gic_prio_init(void)pr_warn ("Broken GIC integration, security disabled\n" ); }else {pr_warn ("Broken GIC integration, pNMI forbidden\n" ); - nmi_support_forbidden = true; + pseudo_nmi_support_forbidden = true; } }
@@ -512,10 +512,10 @@ static void gic_unmask_irq(struct irq_data *d)gic_poke_irq (d,GICD_ISENABLER ); } -static inline bool gic_supports_nmi (void ) +static inline bool gic_supports_pseudo_nmi (void ) {return IS_ENABLED (CONFIG_ARM64_PSEUDO_NMI ) && -static_branch_likely (&supports_pseudo_nmis); +static_branch_likely (&supports_pseudo_nmi); } static int gic_irq_set_irqchip_state(struct irq_data *d,
@@ -598,7 +598,7 @@ static int gic_irq_nmi_setup(struct irq_data *d) {struct irq_desc *desc =irq_to_desc (d->irq); -if (!gic_supports_nmi ()) +if (!gic_supports_pseudo_nmi ())return -EINVAL ;if (gic_peek_irq (d,GICD_ISENABLER )) {
@@ -626,7 +626,7 @@ static void gic_irq_nmi_teardown(struct irq_data *d) {struct irq_desc *desc =irq_to_desc (d->irq); -if (WARN_ON (!gic_supports_nmi ())) +if (WARN_ON (!gic_supports_pseudo_nmi ()))return ;if (gic_peek_irq (d,GICD_ISENABLER )) {
@@ -804,7 +804,7 @@ static inline void gic_complete_ack(u32 irqnr)static bool gic_rpr_is_nmi_prio (void ) { -if (!gic_supports_nmi ()) +if (!gic_supports_pseudo_nmi ())return false;return unlikely (gic_read_rpr () ==GICV3_PRIO_NMI );
@@ -911,7 +911,7 @@ static void __gic_handle_irq_from_irqsoff(struct pt_regs *regs) static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs) { -if (unlikely (gic_supports_nmi () && !interrupts_enabled (regs))) +if (unlikely (gic_supports_pseudo_nmi () && !interrupts_enabled (regs)))__gic_handle_irq_from_irqsoff (regs);else __gic_handle_irq_from_irqson (regs);
@@ -1162,7 +1162,7 @@ static void gic_cpu_sys_reg_init(void)/* Set priority mask register */ if (!gic_prio_masking_enabled ()) {write_gicreg (DEFAULT_PMR_VALUE ,ICC_PMR_EL1 ); - }else if (gic_supports_nmi ()) { + }else if (gic_supports_pseudo_nmi ()) {/* * Check that all CPUs use the same priority space. *
@@ -1955,15 +1955,15 @@ static const struct gic_quirk gic_quirks[] = { } }; -static void gic_enable_nmi_support (void ) +static void gic_enable_pseudo_nmi (void ) { -if (!gic_prio_masking_enabled () || nmi_support_forbidden) +if (!gic_prio_masking_enabled () || pseudo_nmi_support_forbidden)return ;pr_info ("Pseudo-NMIs enabled using %s ICC_PMR_EL1 synchronisation\n" ,gic_has_relaxed_pmr_sync () ?"relaxed" :"forced" ); -static_branch_enable (&supports_pseudo_nmis); +static_branch_enable (&supports_pseudo_nmi);if (static_branch_likely (&supports_deactivate_key)) gic_eoimode1_chip.flags |=IRQCHIP_SUPPORTS_NMI ;
@@ -2047,7 +2047,7 @@ static int __init gic_init_bases(phys_addr_t dist_phys_base,gic_prio_init ();gic_dist_init ();gic_cpu_init (); -gic_enable_nmi_support (); +gic_enable_pseudo_nmi ();gic_smp_init ();gic_cpu_pm_init ();
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 41/45] irqchip/gic-v3: Prepare for FEAT_GICv3_NMI support
The title "irqchip/gic-v3: Rename *nmi* to *pseudo_nmi*" will be more clear
otherwise LGTM
Quoted 132 lines · Vladimir Murzin
From: Lorenzo Pieralisi <[email protected]>
Support for
FEAT_GICv3_NMIis coming. Currently, the terms "NMI" and "pseudo-NMI" are used interchangeably. Rename helper functions and variables related to pseudo-NMI to make the code paths clearer and more explicit.Signed-off-by Lorenzo Pieralisi <[email protected]>Signed-off-by Mark Brown <[email protected]>Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- drivers/irqchip/irq-gic-v3.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)drivers/irqchip/irq-gic-v3.c +14
-14@@ -87,7 +87,7 @@ static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key);#define GIC_LINE_NR min(GICD_TYPER_SPIS(gic_data.rdists.gicd_typer), 1020U)#define GIC_ESPI_NR GICD_TYPER_ESPIS(gic_data.rdists.gicd_typer) -static bool nmi_support_forbidden; +static bool pseudo_nmi_support_forbidden;/* * There are 16 SGIs, though we only actually use 8 in Linux. The other 8 SGIs @@ -117,7 +117,7 @@ static bool nmi_support_forbidden;* - Figure 4-7 Secure read of the priority field for a Non-secure Group 1 * interrupt. */ -static DEFINE_STATIC_KEY_FALSE (supports_pseudo_nmis); +static DEFINE_STATIC_KEY_FALSE (supports_pseudo_nmi);static u32 gic_get_pribits (void ) {@@ -185,7 +185,7 @@ static void __init gic_prio_init(void)pr_warn ("Broken GIC integration, security disabled\n" ); }else {pr_warn ("Broken GIC integration, pNMI forbidden\n" ); - nmi_support_forbidden = true; + pseudo_nmi_support_forbidden = true; } }@@ -512,10 +512,10 @@ static void gic_unmask_irq(struct irq_data *d)gic_poke_irq (d,GICD_ISENABLER ); } -static inline bool gic_supports_nmi (void ) +static inline bool gic_supports_pseudo_nmi (void ) {return IS_ENABLED (CONFIG_ARM64_PSEUDO_NMI ) && -static_branch_likely (&supports_pseudo_nmis); +static_branch_likely (&supports_pseudo_nmi); } static int gic_irq_set_irqchip_state(struct irq_data *d,@@ -598,7 +598,7 @@ static int gic_irq_nmi_setup(struct irq_data *d) {struct irq_desc *desc =irq_to_desc (d->irq); -if (!gic_supports_nmi ()) +if (!gic_supports_pseudo_nmi ())return -EINVAL ;if (gic_peek_irq (d,GICD_ISENABLER )) {@@ -626,7 +626,7 @@ static void gic_irq_nmi_teardown(struct irq_data *d) {struct irq_desc *desc =irq_to_desc (d->irq); -if (WARN_ON (!gic_supports_nmi ())) +if (WARN_ON (!gic_supports_pseudo_nmi ()))return ;if (gic_peek_irq (d,GICD_ISENABLER )) {@@ -804,7 +804,7 @@ static inline void gic_complete_ack(u32 irqnr)static bool gic_rpr_is_nmi_prio (void ) { -if (!gic_supports_nmi ()) +if (!gic_supports_pseudo_nmi ())return false;return unlikely (gic_read_rpr () ==GICV3_PRIO_NMI );@@ -911,7 +911,7 @@ static void __gic_handle_irq_from_irqsoff(struct pt_regs *regs) static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs) { -if (unlikely (gic_supports_nmi () && !interrupts_enabled (regs))) +if (unlikely (gic_supports_pseudo_nmi () && !interrupts_enabled (regs)))__gic_handle_irq_from_irqsoff (regs);else __gic_handle_irq_from_irqson (regs);@@ -1162,7 +1162,7 @@ static void gic_cpu_sys_reg_init(void)/* Set priority mask register */ if (!gic_prio_masking_enabled ()) {write_gicreg (DEFAULT_PMR_VALUE ,ICC_PMR_EL1 ); - }else if (gic_supports_nmi ()) { + }else if (gic_supports_pseudo_nmi ()) {/* * Check that all CPUs use the same priority space. * @@ -1955,15 +1955,15 @@ static const struct gic_quirk gic_quirks[] = { } }; -static void gic_enable_nmi_support (void ) +static void gic_enable_pseudo_nmi (void ) { -if (!gic_prio_masking_enabled () || nmi_support_forbidden) +if (!gic_prio_masking_enabled () || pseudo_nmi_support_forbidden)return ;pr_info ("Pseudo-NMIs enabled using %s ICC_PMR_EL1 synchronisation\n" ,gic_has_relaxed_pmr_sync () ?"relaxed" :"forced" ); -static_branch_enable (&supports_pseudo_nmis); +static_branch_enable (&supports_pseudo_nmi);if (static_branch_likely (&supports_deactivate_key)) gic_eoimode1_chip.flags |=IRQCHIP_SUPPORTS_NMI ;@@ -2047,7 +2047,7 @@ static int __init gic_init_bases(phys_addr_t dist_phys_base,gic_prio_init ();gic_dist_init ();gic_cpu_init (); -gic_enable_nmi_support (); +gic_enable_pseudo_nmi ();gic_smp_init ();gic_cpu_pm_init ();
Re: [RFC PATCH v2 41/45] irqchip/gic-v3: Prepare for FEAT_GICv3_NMI support
Quoted 2 lines · Jinjie Ruan
The title "irqchip/gic-v3: Rename *nmi* to *pseudo_nmi*" will be more clear
Fair enough. I'll update in the next version.
Quoted 2 lines
otherwise LGTM
Reviewed-by Jinjie Ruan <[email protected]>
Thanks
Vladimir
RFC PATCH v2 42/45 irqchip/gic-v3: Implement FEAT_GICv3_NMI support
In reply to Vladimir MurzinFrom: Lorenzo Pieralisi <[email protected]>
The FEAT_GICv3_NMI GIC feature coupled with the CPU FEAT_NMI enables handling NMI interrupts in HW on aarch64, by adding a superpriority interrupt to the existing GIC priority scheme.
Implement GIC driver support for the FEAT_GICv3_NMI feature.
Check, through the ARM64 capabilitity infrastructure, if support for FEAT_NMI was detected on the core and the system has not overridden the detection and forced pseudo-NMIs enablement.
If FEAT_NMI is detected, it was not overridden (check embedded in the system_uses_nmi() call) and the GIC supports the FEAT_GICv3_NMI feature, initialize NMIs related HW GIC registers and route irq to NMI handling logic.
--- drivers/irqchip/irq-gic-v3.c | 114 +++++++++++++++++++++++++++-- include/linux/irqchip/arm-gic-v3.h | 4 + 2 files changed, 111 insertions(+), 7 deletions(-)
drivers/irqchip/irq-gic-v3.c +107 -7
@@ -64,6 +64,7 @@ struct gic_chip_data {u32 nr_redist_regions;u64 flags;bool has_rss; +bool has_nmi;unsigned int ppi_nr;struct partition_affinity *parts;unsigned int nr_parts;
@@ -89,6 +90,9 @@ static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key);static bool pseudo_nmi_support_forbidden; +static DEFINE_RAW_SPINLOCK (gic_nmi_lock); + +/* * There are 16 SGIs, though we only actually use 8 in Linux. The other 8 SGIs * are potentially stolen by the secure side. Some code, especially code dealing
@@ -252,6 +256,37 @@ enum gic_intid_range { __INVALID_RANGE__ }; +#ifdef CONFIG_ARM64 +#include <asm/cpufeature.h> + +static inline bool gic_supports_v3_3_nmi (void ) +{ +return gic_data.has_nmi &&system_uses_nmi (); +} + +static inline u64 gic_read_nmiar (void ) +{ +u64 irqstat; + + irqstat =read_sysreg_s (SYS_ICC_NMIAR1_EL1 ); + +dsb (sy); + +return irqstat; +} + +#else +static inline bool gic_supports_v3_3_nmi (void ) +{ +return false; +} + +static inline u64 gic_read_nmiar (void ) +{ +BUG (); +} +#endif +static enum gic_intid_range __get_intid_range (irq_hw_number_t hwirq) {switch (hwirq) {
@@ -438,6 +473,8 @@ static u32 convert_offset_index(struct irq_data *d, u32 offset, u32 *index)return GICD_ICFGRnE;case GICD_IROUTER :return GICD_IROUTERnE; +case GICD_INMIR : +return GICD_INMIRnE;default :break ; }
@@ -467,6 +504,42 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)return !!(readl_relaxed (base + offset + (index /32 ) *4 ) & mask); } +static void gic_irq_configure_nmi (struct irq_data *d,bool enable) +{ +void __iomem *base, *addr; +unsigned long val; +u32 offset, index; + + offset =convert_offset_index (d,GICD_INMIR , &index); + +if (gic_irq_in_rdist (d)) + base =gic_data_rdist_sgi_base (); +else + base =gic_dist_base_alias (d); + + addr = base + offset + (index /32 ) *4 ; + +raw_spin_lock (&gic_nmi_lock); + + val =readl_relaxed (addr); + +__assign_bit (index %32 , &val, enable); + +writel_relaxed (val, addr); + +raw_spin_unlock (&gic_nmi_lock); +} + +static void gic_irq_enable_nmi (struct irq_data *d) +{ +gic_irq_configure_nmi (d, true); +} + +static void gic_irq_disable_nmi (struct irq_data *d) +{ +gic_irq_configure_nmi (d, false); +} +static void gic_poke_irq (struct irq_data *d,u32 offset) {void __iomem *base;
@@ -598,7 +671,7 @@ static int gic_irq_nmi_setup(struct irq_data *d) {struct irq_desc *desc =irq_to_desc (d->irq); -if (!gic_supports_pseudo_nmi ()) +if (!gic_supports_pseudo_nmi () && !gic_supports_v3_3_nmi ())return -EINVAL ;if (gic_peek_irq (d,GICD_ISENABLER )) {
@@ -617,7 +690,10 @@ static int gic_irq_nmi_setup(struct irq_data *d)if (!gic_irq_in_rdist (d)) desc->handle_irq = handle_fasteoi_nmi; -gic_irq_set_prio (d, dist_prio_nmi); +if (gic_supports_v3_3_nmi ()) +gic_irq_enable_nmi (d); +else +gic_irq_set_prio (d, dist_prio_nmi);return 0 ; }
@@ -626,7 +702,7 @@ static void gic_irq_nmi_teardown(struct irq_data *d) {struct irq_desc *desc =irq_to_desc (d->irq); -if (WARN_ON (!gic_supports_pseudo_nmi ())) +if (WARN_ON (!gic_supports_pseudo_nmi () && !gic_supports_v3_3_nmi ()))return ;if (gic_peek_irq (d,GICD_ISENABLER )) {
@@ -645,7 +721,10 @@ static void gic_irq_nmi_teardown(struct irq_data *d)if (!gic_irq_in_rdist (d)) desc->handle_irq = handle_fasteoi_irq; -gic_irq_set_prio (d, dist_prio_irq); +if (gic_supports_v3_3_nmi ()) +gic_irq_disable_nmi (d); +else +gic_irq_set_prio (d, dist_prio_irq); }static bool gic_arm64_erratum_2941627_needed (struct irq_data *d)
@@ -836,7 +915,8 @@ static void __gic_handle_nmi(u32 irqnr, struct pt_regs *regs)gic_complete_ack (irqnr);if (generic_handle_domain_nmi (gic_data.domain, irqnr)) { -WARN_ONCE (true,"Unexpected pseudo-NMI (irqnr %u)\n" , irqnr); +WARN_ONCE (true,"Unexpected %sNMI (irqnr %u)\n" , +gic_supports_pseudo_nmi () ?"pseudo-" :"" , irqnr);gic_deactivate_unhandled (irqnr); } }
@@ -911,7 +991,11 @@ static void __gic_handle_irq_from_irqsoff(struct pt_regs *regs) static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs) { -if (unlikely (gic_supports_pseudo_nmi () && !interrupts_enabled (regs))) +if (gic_supports_v3_3_nmi () &&in_nmi ()) { +u32 irqnr =gic_read_nmiar (); + +__gic_handle_nmi (irqnr, regs); + }else if (unlikely (gic_supports_pseudo_nmi () && !interrupts_enabled (regs)))__gic_handle_irq_from_irqsoff (regs);else __gic_handle_irq_from_irqson (regs);
@@ -1964,6 +2048,19 @@ static void gic_enable_pseudo_nmi(void) gic_has_relaxed_pmr_sync() ? "relaxed" : "forced");static_branch_enable (&supports_pseudo_nmi); +} + +static void gic_enable_nmi_support (void ) +{ +if ((!gic_prio_masking_enabled () || pseudo_nmi_support_forbidden) && !gic_supports_v3_3_nmi ()) +return ; + +/* +* Initialize pseudo-NMIs only if GIC driver cannot take advantage +* of core (FEAT_NMI) and GIC (FEAT_GICv3_NMI) in HW +*/ +if (!gic_supports_v3_3_nmi ()) +gic_enable_pseudo_nmi ();if (static_branch_likely (&supports_deactivate_key)) gic_eoimode1_chip.flags |=IRQCHIP_SUPPORTS_NMI ;
@@ -2032,6 +2129,9 @@ static int __init gic_init_bases(phys_addr_t dist_phys_base,irq_domain_update_bus_token (gic_data.domain,DOMAIN_BUS_WIRED ); gic_data.has_rss = !!(typer &GICD_TYPER_RSS ); + gic_data.has_nmi = !!(typer &GICD_TYPER_NMI ); + +pr_info ("Non-maskable interrupt property %s supported\n" , gic_data.has_nmi ?"is" :"not" );if (typer &GICD_TYPER_MBIS ) { err =mbi_init (handle, gic_data.domain);
@@ -2047,7 +2147,7 @@ static int __init gic_init_bases(phys_addr_t dist_phys_base,gic_prio_init ();gic_dist_init ();gic_cpu_init (); -gic_enable_pseudo_nmi (); +gic_enable_nmi_support ();gic_smp_init ();gic_cpu_pm_init ();
include/linux/irqchip/arm-gic-v3.h +4 -0
@@ -30,6 +30,7 @@#define GICD_ICFGR 0x0C00#define GICD_IGRPMODR 0x0D00#define GICD_NSACR 0x0E00 +#define GICD_INMIR 0x0F80#define GICD_IGROUPRnE 0x1000#define GICD_ISENABLERnE 0x1200#define GICD_ICENABLERnE 0x1400
@@ -39,6 +40,7 @@#define GICD_ICACTIVERnE 0x1C00#define GICD_IPRIORITYRnE 0x2000#define GICD_ICFGRnE 0x3000 +#define GICD_INMIRnE 0x3B00#define GICD_IROUTER 0x6000#define GICD_IROUTERnE 0x8000#define GICD_IDREGS 0xFFD0
@@ -83,6 +85,7 @@#define GICD_TYPER_LPIS (1U << 17)#define GICD_TYPER_MBIS (1U << 16)#define GICD_TYPER_ESPI (1U << 8) +#define GICD_TYPER_NMI (1U << 9)#define GICD_TYPER_ID_BITS (typer) ((((typer) >> 19) & 0x1f) + 1)#define GICD_TYPER_NUM_LPIS (typer) ((((typer) >> 11) & 0x1f) + 1)
@@ -238,6 +241,7 @@#define GICR_ICFGR0 GICD_ICFGR#define GICR_IGRPMODR0 GICD_IGRPMODR#define GICR_NSACR GICD_NSACR +#define GICR_INMIR0 GICD_INMIR#define GICR_TYPER_PLPIS (1U << 0)#define GICR_TYPER_VLPIS (1U << 1)
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 42/45] irqchip/gic-v3: Implement FEAT_GICv3_NMI support
Quoted 224 lines · Vladimir Murzin
From: Lorenzo Pieralisi <[email protected]>
The
FEAT_GICv3_NMIGIC feature coupled with the CPUFEAT_NMIenables handling NMI interrupts in HW on aarch64, by adding a superpriority interrupt to the existing GIC priority scheme.Implement GIC driver support for the
FEAT_GICv3_NMIfeature.Check, through the ARM64 capabilitity infrastructure, if support for
FEAT_NMIwas detected on the core and the system has not overridden the detection and forced pseudo-NMIs enablement.If
FEAT_NMIis detected, it was not overridden (check embedded in thesystem_uses_nmi()call) and the GIC supports theFEAT_GICv3_NMIfeature, initialize NMIs related HW GIC registers and route irq to NMI handling logic.Signed-off-by Lorenzo Pieralisi <[email protected]>Signed-off-by Mark Brown <[email protected]>Signed-off-by Ada Couprie Diaz <[email protected]>Signed-off-by Vladimir Murzin <[email protected]>--- drivers/irqchip/irq-gic-v3.c | 114 +++++++++++++++++++++++++++-- include/linux/irqchip/arm-gic-v3.h | 4 + 2 files changed, 111 insertions(+), 7 deletions(-)drivers/irqchip/irq-gic-v3.c +103
-6@@ -64,6 +64,7 @@ struct gic_chip_data {u32 nr_redist_regions;u64 flags;bool has_rss; +bool has_nmi;unsigned int ppi_nr;struct partition_affinity *parts;unsigned int nr_parts;@@ -89,6 +90,9 @@ static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key);static bool pseudo_nmi_support_forbidden; +static DEFINE_RAW_SPINLOCK (gic_nmi_lock); + +/* * There are 16 SGIs, though we only actually use 8 in Linux. The other 8 SGIs * are potentially stolen by the secure side. Some code, especially code dealing @@ -252,6 +256,37 @@ enum gic_intid_range { __INVALID_RANGE__ }; +#ifdef CONFIG_ARM64 +#include <asm/cpufeature.h> + +static inline bool gic_supports_v3_3_nmi (void ) +{ +return gic_data.has_nmi &&system_uses_nmi (); +} + +static inline u64 gic_read_nmiar (void ) +{ +u64 irqstat; + + irqstat =read_sysreg_s (SYS_ICC_NMIAR1_EL1 ); + +dsb (sy); + +return irqstat; +} + +#else +static inline bool gic_supports_v3_3_nmi (void ) +{ +return false; +} + +static inline u64 gic_read_nmiar (void ) +{ +BUG (); +} +#endif +static enum gic_intid_range __get_intid_range (irq_hw_number_t hwirq) {switch (hwirq) {@@ -438,6 +473,8 @@ static u32 convert_offset_index(struct irq_data *d, u32 offset, u32 *index)return GICD_ICFGRnE;case GICD_IROUTER :return GICD_IROUTERnE; +case GICD_INMIR : +return GICD_INMIRnE;default :break ; }@@ -467,6 +504,42 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)return !!(readl_relaxed (base + offset + (index /32 ) *4 ) & mask); } +static void gic_irq_configure_nmi (struct irq_data *d,bool enable) +{ +void __iomem *base, *addr; +unsigned long val; +u32 offset, index; + + offset =convert_offset_index (d,GICD_INMIR , &index); + +if (gic_irq_in_rdist (d)) + base =gic_data_rdist_sgi_base (); +else + base =gic_dist_base_alias (d); + + addr = base + offset + (index /32 ) *4 ; + +raw_spin_lock (&gic_nmi_lock); + + val =readl_relaxed (addr); + +__assign_bit (index %32 , &val, enable); + +writel_relaxed (val, addr); + +raw_spin_unlock (&gic_nmi_lock); +} + +static void gic_irq_enable_nmi (struct irq_data *d) +{ +gic_irq_configure_nmi (d, true); +} + +static void gic_irq_disable_nmi (struct irq_data *d) +{ +gic_irq_configure_nmi (d, false); +} +static void gic_poke_irq (struct irq_data *d,u32 offset) {void __iomem *base;@@ -598,7 +671,7 @@ static int gic_irq_nmi_setup(struct irq_data *d) {struct irq_desc *desc =irq_to_desc (d->irq); -if (!gic_supports_pseudo_nmi ()) +if (!gic_supports_pseudo_nmi () && !gic_supports_v3_3_nmi ())return -EINVAL ;if (gic_peek_irq (d,GICD_ISENABLER )) {@@ -617,7 +690,10 @@ static int gic_irq_nmi_setup(struct irq_data *d)if (!gic_irq_in_rdist (d)) desc->handle_irq = handle_fasteoi_nmi; -gic_irq_set_prio (d, dist_prio_nmi); +if (gic_supports_v3_3_nmi ()) +gic_irq_enable_nmi (d); +else +gic_irq_set_prio (d, dist_prio_nmi);return 0 ; }@@ -626,7 +702,7 @@ static void gic_irq_nmi_teardown(struct irq_data *d) {struct irq_desc *desc =irq_to_desc (d->irq); -if (WARN_ON (!gic_supports_pseudo_nmi ())) +if (WARN_ON (!gic_supports_pseudo_nmi () && !gic_supports_v3_3_nmi ()))return ;if (gic_peek_irq (d,GICD_ISENABLER )) {@@ -645,7 +721,10 @@ static void gic_irq_nmi_teardown(struct irq_data *d)if (!gic_irq_in_rdist (d)) desc->handle_irq = handle_fasteoi_irq; -gic_irq_set_prio (d, dist_prio_irq); +if (gic_supports_v3_3_nmi ()) +gic_irq_disable_nmi (d); +else +gic_irq_set_prio (d, dist_prio_irq); }static bool gic_arm64_erratum_2941627_needed (struct irq_data *d)@@ -836,7 +915,8 @@ static void __gic_handle_nmi(u32 irqnr, struct pt_regs *regs)gic_complete_ack (irqnr);if (generic_handle_domain_nmi (gic_data.domain, irqnr)) { -WARN_ONCE (true,"Unexpected pseudo-NMI (irqnr %u)\n" , irqnr); +WARN_ONCE (true,"Unexpected %sNMI (irqnr %u)\n" , +gic_supports_pseudo_nmi () ?"pseudo-" :"" , irqnr);gic_deactivate_unhandled (irqnr); } }@@ -911,7 +991,11 @@ static void __gic_handle_irq_from_irqsoff(struct pt_regs *regs) static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs) { -if (unlikely (gic_supports_pseudo_nmi () && !interrupts_enabled (regs))) +if (gic_supports_v3_3_nmi () &&in_nmi ()) { +u32 irqnr =gic_read_nmiar (); + +__gic_handle_nmi (irqnr, regs); + }else if (unlikely (gic_supports_pseudo_nmi () && !interrupts_enabled (regs)))__gic_handle_irq_from_irqsoff (regs);else __gic_handle_irq_from_irqson (regs);@@ -1964,6 +2048,19 @@ static void gic_enable_pseudo_nmi(void) gic_has_relaxed_pmr_sync() ? "relaxed" : "forced");static_branch_enable (&supports_pseudo_nmi); +} + +static void gic_enable_nmi_support (void ) +{ +if ((!gic_prio_masking_enabled () || pseudo_nmi_support_forbidden) && !gic_supports_v3_3_nmi ()) +return ; + +/* +* Initialize pseudo-NMIs only if GIC driver cannot take advantage +* of core (FEAT_NMI) and GIC (FEAT_GICv3_NMI) in HW +*/ +if (!gic_supports_v3_3_nmi ()) +gic_enable_pseudo_nmi ();if (static_branch_likely (&supports_deactivate_key)) gic_eoimode1_chip.flags |=IRQCHIP_SUPPORTS_NMI ;
drivers/irqchip/irq-gic-v3.c static int __init gic_init_bases(phys_addr_t dist_phys_base, · line 2134
Quoted 6 lines
drivers/irqchip/irq-gic-v3.c +3
-0@@ -2032,6 +2129,9 @@ static int __init gic_init_bases(phys_addr_t dist_phys_base,irq_domain_update_bus_token (gic_data.domain,DOMAIN_BUS_WIRED ); gic_data.has_rss = !!(typer &GICD_TYPER_RSS ); + gic_data.has_nmi = !!(typer &GICD_TYPER_NMI ); + +pr_info ("Non-maskable interrupt property %s supported\n" , gic_data.has_nmi ?"is" :"not" );
Quoted 48 lines
drivers/irqchip/irq-gic-v3.c +1
-1if (typer &GICD_TYPER_MBIS ) { err =mbi_init (handle, gic_data.domain);@@ -2047,7 +2147,7 @@ static int __init gic_init_bases(phys_addr_t dist_phys_base,gic_prio_init ();gic_dist_init ();gic_cpu_init (); -gic_enable_pseudo_nmi (); +gic_enable_nmi_support ();gic_smp_init ();gic_cpu_pm_init ();include/linux/irqchip/arm-gic-v3.h +4
-0@@ -30,6 +30,7 @@#define GICD_ICFGR 0x0C00#define GICD_IGRPMODR 0x0D00#define GICD_NSACR 0x0E00 +#define GICD_INMIR 0x0F80#define GICD_IGROUPRnE 0x1000#define GICD_ISENABLERnE 0x1200#define GICD_ICENABLERnE 0x1400@@ -39,6 +40,7 @@#define GICD_ICACTIVERnE 0x1C00#define GICD_IPRIORITYRnE 0x2000#define GICD_ICFGRnE 0x3000 +#define GICD_INMIRnE 0x3B00#define GICD_IROUTER 0x6000#define GICD_IROUTERnE 0x8000#define GICD_IDREGS 0xFFD0@@ -83,6 +85,7 @@#define GICD_TYPER_LPIS (1U << 17)#define GICD_TYPER_MBIS (1U << 16)#define GICD_TYPER_ESPI (1U << 8) +#define GICD_TYPER_NMI (1U << 9)#define GICD_TYPER_ID_BITS (typer) ((((typer) >> 19) & 0x1f) + 1)#define GICD_TYPER_NUM_LPIS (typer) ((((typer) >> 11) & 0x1f) + 1)@@ -238,6 +241,7 @@#define GICR_ICFGR0 GICD_ICFGR#define GICR_IGRPMODR0 GICD_IGRPMODR#define GICR_NSACR GICD_NSACR +#define GICR_INMIR0 GICD_INMIR#define GICR_TYPER_PLPIS (1U << 0)#define GICR_TYPER_VLPIS (1U << 1)
RFC PATCH v2 43/45 arm64: smp: Add NMI support for LPI-backed IPIs
In reply to Vladimir MurzinLPI-backed IPIs are currently always requested and managed as regular IRQs. Allow IPIs selected by ipi_should_be_nmi() to be requested as NMIs, falling back to regular IRQs when the NMI request fails.
Track which IPIs were successfully requested as NMIs so that the per-CPU enable and disable paths use the appropriate NMI helpers.
--- arch/arm64/kernel/smp.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)
arch/arm64/kernel/smp.c +29 -5
@@ -1123,16 +1123,24 @@ static const struct ipi_irq_ops ipi_sgi_ops = {static void ipi_lpi_enable (int cpu,int ipi) { -enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) +enable_nmi (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +else +enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); }static void ipi_lpi_disable (int cpu,int ipi) { -disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) +disable_nmi_nosync (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +else +disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); }static void ipi_lpi_setup (int ipi,int ncpus) { +bool is_nmi =ipi_should_be_nmi (ipi); +for (int cpu =0 ; cpu < ncpus; cpu++) {int err, irq;
@@ -1141,13 +1149,29 @@ static void ipi_lpi_setup(int ipi, int ncpus) err =irq_force_affinity (irq,cpumask_of (cpu));WARN (err,"Could not force affinity IRQ %d, err=%d\n" , irq, err); - err =request_irq (irq, ipi_handler,IRQF_NO_AUTOEN ,"IPI" , -NULL ); -WARN (err,"Could not request IRQ %d, err=%d\n" , irq, err); +/* +* Try to request NMI if it should be +*/ +if (is_nmi &&request_nmi (irq, ipi_handler,IRQF_PERCPU |IRQF_NO_AUTOEN ,"IPI" ,NULL )) { +/* +* Fallback to regular IRQ +*/ + is_nmi = false; + } +/* +* Otherwise regular IRQ +*/ +if (!is_nmi) { + err =request_irq (irq, ipi_handler,IRQF_NO_AUTOEN ,"IPI" ,NULL ); +WARN (err,"Could not request IRQ %d, err=%d\n" , irq, err); + }irq_set_status_flags (irq, (IRQ_HIDDEN |IRQ_NO_BALANCING_MASK ));get_ipi_desc (cpu, ipi) =irq_to_desc (irq); + +if (is_nmi) +set_bit (ipi,get_ipi_nmi_bitmap (cpu)); } }
Signature · 1 line
2.34.1
Re: [RFC PATCH v2 43/45] arm64: smp: Add NMI support for LPI-backed IPIs
Quoted 4 lines · Vladimir Murzin
LPI-backed IPIs are currently always requested and managed as regular IRQs. Allow IPIs selected by
ipi_should_be_nmi()to be requested as NMIs, falling back to regular IRQs when the NMI request fails.
It seems FEAT_NMI do not support LPI interrupt.
Quoted 72 lines
Track which IPIs were successfully requested as NMIs so that the per-CPU enable and disable paths use the appropriate NMI helpers.
Signed-off-by Vladimir Murzin <[email protected]>--- arch/arm64/kernel/smp.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)arch/arm64/kernel/smp.c +29
-5@@ -1123,16 +1123,24 @@ static const struct ipi_irq_ops ipi_sgi_ops = {static void ipi_lpi_enable (int cpu,int ipi) { -enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) +enable_nmi (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +else +enable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); }static void ipi_lpi_disable (int cpu,int ipi) { -disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +if (test_bit (ipi,get_ipi_nmi_bitmap (cpu))) +disable_nmi_nosync (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); +else +disable_irq (irq_desc_get_irq (get_ipi_desc (cpu, ipi))); }static void ipi_lpi_setup (int ipi,int ncpus) { +bool is_nmi =ipi_should_be_nmi (ipi); +for (int cpu =0 ; cpu < ncpus; cpu++) {int err, irq;@@ -1141,13 +1149,29 @@ static void ipi_lpi_setup(int ipi, int ncpus) err =irq_force_affinity (irq,cpumask_of (cpu));WARN (err,"Could not force affinity IRQ %d, err=%d\n" , irq, err); - err =request_irq (irq, ipi_handler,IRQF_NO_AUTOEN ,"IPI" , -NULL ); -WARN (err,"Could not request IRQ %d, err=%d\n" , irq, err); +/* +* Try to request NMI if it should be +*/ +if (is_nmi &&request_nmi (irq, ipi_handler,IRQF_PERCPU |IRQF_NO_AUTOEN ,"IPI" ,NULL )) { +/* +* Fallback to regular IRQ +*/ + is_nmi = false; + } +/* +* Otherwise regular IRQ +*/ +if (!is_nmi) { + err =request_irq (irq, ipi_handler,IRQF_NO_AUTOEN ,"IPI" ,NULL ); +WARN (err,"Could not request IRQ %d, err=%d\n" , irq, err); + }irq_set_status_flags (irq, (IRQ_HIDDEN |IRQ_NO_BALANCING_MASK ));get_ipi_desc (cpu, ipi) =irq_to_desc (irq); + +if (is_nmi) +set_bit (ipi,get_ipi_nmi_bitmap (cpu)); } }