arm64: sdei: Use alternative patching to select the conduit in sdei exit
Subsystems (2)
PATCH RESEND v2 1/2 arm64: sdei: Use alternative patching to select the conduit in sdei exit
Patch in the conduit instruction for SDEI instead of branching between the possible conduits. The sdei driver's conduit initialization is moved earlier in the initialization flow, into setup_arch(), so that it is ready in time for alternative patching.
--- arch/arm64/include/asm/sdei.h | 13 +++---- arch/arm64/kernel/entry.S | 26 ++++++------- arch/arm64/kernel/sdei.c | 30 ++++++++++++--- arch/arm64/kernel/setup.c | 3 ++ drivers/firmware/arm_sdei.c | 71 +++++++++++++++++++---------------- include/linux/arm_sdei.h | 6 ++- 6 files changed, 88 insertions(+), 61 deletions(-)
arch/arm64/include/asm/sdei.h +5 -8
@@ -3,10 +3,6 @@#ifndef __ASM_SDEI_H#define __ASM_SDEI_H -/* Values for sdei_exit_mode */ -#define SDEI_EXIT_HVC 0 -#define SDEI_EXIT_SMC 1 -#define SDEI_STACK_SIZE IRQ_STACK_SIZE#ifndef __ASSEMBLY__
@@ -17,8 +13,6 @@#include <asm/virt.h> -extern unsigned long sdei_exit_mode; -/* Software Delegated Exception entry point from firmware*/ asmlinkage void __sdei_asm_handler(unsigned long event_num, unsigned long arg,unsigned long pc,unsigned long pstate);
@@ -40,8 +34,11 @@ asmlinkage unsigned long __sdei_handler(struct pt_regs *regs,unsigned long do_sdei_event (struct pt_regs *regs,struct sdei_registered_event *arg); -unsigned long sdei_arch_get_entry_point (int conduit); -#define sdei_arch_get_entry_point (x) sdei_arch_get_entry_point(x) +unsigned long sdei_arch_get_entry_point (void ); +#define sdei_arch_get_entry_point sdei_arch_get_entry_point + +void __init sdei_patch_conduit (struct alt_instr *alt,__le32 *origptr, +__le32 *updptr,int nr_inst);#endif /* __ASSEMBLY__ */ #endif /* __ASM_SDEI_H */
arch/arm64/kernel/entry.S +12 -14
@@ -914,13 +914,10 @@ NOKPROBE(call_on_irq_stack) #include <asm/sdei.h> #include <uapi/linux/arm_sdei.h> -.macro sdei_handler_exit exit_mode - /* On success, this call never returns... */ - cmp \exit_mode, #SDEI_EXIT_SMC - b.ne 99f - smc #0 - b . -99: hvc #0 +.macro sdei_handler_exit +alternative_cb ARM64_ALWAYS_SYSTEM, sdei_patch_conduit + nop // Patched to SMC/HVC #0 +alternative_cb_end b . .endm
@@ -955,7 +952,6 @@ NOKPROBE(__sdei_asm_entry_trampoline) * Make the exit call and restore the original ttbr1_el1 * * x0 & x1: setup for the exit API call - * x2: exit_mode * x4: struct sdei_registered_event argument from registration time. */ SYM_CODE_START(__sdei_asm_exit_trampoline)
@@ -964,7 +960,7 @@ SYM_CODE_START(__sdei_asm_exit_trampoline) tramp_unmap_kernel tmp=x4 -1: sdei_handler_exit exit_mode=x2 +1: sdei_handler_exit SYM_CODE_END(__sdei_asm_exit_trampoline) NOKPROBE(__sdei_asm_exit_trampoline) .popsection // .entry.tramp.text
@@ -1070,14 +1066,16 @@ SYM_CODE_START(__sdei_asm_handler) mov_q x3, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME csel x0, x2, x3, ls - ldr_l x2, sdei_exit_mode - -alternative_if_not ARM64_UNMAP_KERNEL_AT_EL0 - sdei_handler_exit exit_mode=x2 +#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 +alternative_if ARM64_UNMAP_KERNEL_AT_EL0 + b 1f alternative_else_nop_endif +#endif + + sdei_handler_exit #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 - tramp_alias dst=x5, sym=__sdei_asm_exit_trampoline, tmp=x3 +1: tramp_alias dst=x5, sym=__sdei_asm_exit_trampoline, tmp=x3 br x5 #endif SYM_CODE_END(__sdei_asm_handler)
arch/arm64/kernel/sdei.c +25 -5
@@ -20,8 +20,6 @@#include <asm/sysreg.h> #include <asm/vmap_stack.h> -unsigned long sdei_exit_mode; -/* * VMAP'd stacks checking for stack overflow on exception using sp as a scratch * register, meaning SDEI has to switch to its own stack. We need two stacks as
@@ -162,7 +160,7 @@ static int init_sdei_scs(void)return err; } -unsigned long sdei_arch_get_entry_point (int conduit) +unsigned long sdei_arch_get_entry_point (void ) {/* * SDEI works between adjacent exception levels. If we booted at EL1 we
@@ -181,8 +179,6 @@ unsigned long sdei_arch_get_entry_point(int conduit)if (init_sdei_scs ()) goto out_err_free_stacks; - sdei_exit_mode = (conduit ==SMCCC_CONDUIT_HVC ) ?SDEI_EXIT_HVC :SDEI_EXIT_SMC ; -#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 if (arm64_kernel_unmapped_at_el0 ()) {unsigned long offset;
@@ -262,3 +258,27 @@ unsigned long __kprobes do_sdei_event(struct pt_regs *regs,return vbar +0x480 ; } + +/* +* Patch in the sdei conduit instruction. +*/ +void __init sdei_patch_conduit (struct alt_instr *alt,__le32 *origptr, +__le32 *updptr,int nr_inst) +{ +u32 insn; + +BUG_ON (nr_inst !=1 );/* NOP -> HVC/SMC */ + +switch (sdei_get_conduit ()) { +case SMCCC_CONDUIT_HVC : + insn =aarch64_insn_get_hvc_value (); +break ; +case SMCCC_CONDUIT_SMC : + insn =aarch64_insn_get_smc_value (); +break ; +default : +return ; + } + + *updptr =cpu_to_le32 (insn); +}
arch/arm64/kernel/setup.c +3 -0
@@ -7,6 +7,7 @@*/ #include <linux/acpi.h> +#include <linux/arm_sdei.h> #include <linux/export.h> #include <linux/kernel.h> #include <linux/stddef.h>
@@ -365,6 +366,8 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)else psci_acpi_init (); +sdei_conduit_init (); +init_bootcpu_ops ();smp_init_cpus ();smp_build_mpidr_hash ();
drivers/firmware/arm_sdei.c +38 -33
97 lines not on this page · raw message
include/linux/arm_sdei.h +5 -1
@@ -13,7 +13,7 @@/* Arch code should override this to set the entry point from firmware... */ #ifndef sdei_arch_get_entry_point -#define sdei_arch_get_entry_point (conduit) (0) +#define sdei_arch_get_entry_point () (0)#endif /*
@@ -22,6 +22,8 @@*/ typedef int (sdei_event_callback )(u32 event,struct pt_regs *regs,void *arg); +enum arm_smccc_conduit sdei_get_conduit (void ); +/* * Register your callback to claim an event. The event must be described * by firmware.
@@ -47,10 +49,12 @@ int sdei_unregister_ghes(struct ghes *ghes);int sdei_mask_local_cpu (void );int sdei_unmask_local_cpu (void );void __init sdei_init (void ); +void __init sdei_conduit_init (void );#else static inline int sdei_mask_local_cpu (void ) {return 0 ; }static inline int sdei_unmask_local_cpu (void ) {return 0 ; }static inline void sdei_init (void ) { } +static inline void sdei_conduit_init (void ) { }#endif /* CONFIG_ARM_SDE_INTERFACE */
Signature · 1 line
2.39.2
PATCH RESEND v2 2/2 arm64: sdei: abort running SDEI handlers during crash
Interrupts are blocked in SDEI context, per the SDEI spec: "The client interrupts cannot preempt the event handler." If we crashed in the SDEI handler-running context (as with ACPI's AGDI) then we need to clean up the SDEI state before proceeding to the crash kernel so that the crash kernel can have working interrupts.
Track the active SDEI handler per-cpu so that we can COMPLETE_AND_RESUME the handler, discarding the interrupted context.
--- arch/arm64/include/asm/sdei.h | 8 ++++++++ arch/arm64/kernel/entry.S | 24 ++++++++++++++++++++++++ arch/arm64/kernel/sdei.c | 3 +++ arch/arm64/kernel/smp.c | 27 +++++++++++++++++++++++---- 4 files changed, 58 insertions(+), 4 deletions(-)
arch/arm64/include/asm/sdei.h +8 -0
@@ -13,6 +13,11 @@#include <asm/virt.h> +#ifdef CONFIG_ARM_SDE_INTERFACE +DECLARE_PER_CPU(struct sdei_registered_event *, sdei_active_normal_event); +DECLARE_PER_CPU(struct sdei_registered_event *, sdei_active_critical_event); +#endif +/* Software Delegated Exception entry point from firmware*/ asmlinkage void __sdei_asm_handler(unsigned long event_num, unsigned long arg,unsigned long pc,unsigned long pstate);
@@ -23,6 +28,9 @@ asmlinkage void __sdei_asm_entry_trampoline(unsigned long event_num,unsigned long pc, unsigned long pstate); +/* Abort a running handler. Context is discarded. */ +void sdei_handler_abort (void ); +/* * The above entry point does the minimum to call C code. This function does * anything else, before calling the driver.
arch/arm64/kernel/entry.S +24 -0
@@ -1003,6 +1003,12 @@ SYM_CODE_START(__sdei_asm_handler) ldrb w4, [x19, #SDEI_EVENT_PRIORITY] #endif + cbnz w4, 1f + adr_this_cpu dst=x5, sym=sdei_active_normal_event, tmp=x6 + b 2f +1: adr_this_cpu dst=x5, sym=sdei_active_critical_event, tmp=x6 +2: str x19, [x5] + #ifdef CONFIG_VMAP_STACK /* * entry.S may have been using sp as a scratch register, find whether
@@ -1066,6 +1072,13 @@ SYM_CODE_START(__sdei_asm_handler) mov_q x3, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME csel x0, x2, x3, ls + ldrb w3, [x4, #SDEI_EVENT_PRIORITY] + cbnz w3, 1f + adr_this_cpu dst=x5, sym=sdei_active_normal_event, tmp=x6 + b 2f +1: adr_this_cpu dst=x5, sym=sdei_active_critical_event, tmp=x6 +2: str xzr, [x5] + #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 alternative_if ARM64_UNMAP_KERNEL_AT_EL0 b 1f
@@ -1080,4 +1093,15 @@ alternative_else_nop_endif #endif SYM_CODE_END(__sdei_asm_handler) NOKPROBE(__sdei_asm_handler) + +SYM_CODE_START(sdei_handler_abort) + mov_q x0, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME + adr x1, 1f + sdei_handler_exit + // exit the handler and jump to the next instruction. + // Exit will stomp x0-x17, PSTATE, ELR_ELx, and SPSR_ELx. +1: ret +SYM_CODE_END(sdei_handler_abort) +NOKPROBE(sdei_handler_abort) + #endif /* CONFIG_ARM_SDE_INTERFACE */
arch/arm64/kernel/sdei.c +3 -0
@@ -45,6 +45,9 @@ DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr); DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);#endif +DEFINE_PER_CPU(struct sdei_registered_event *, sdei_active_normal_event); +DEFINE_PER_CPU(struct sdei_registered_event *, sdei_active_critical_event); +static void _free_sdei_stack (unsigned long *__percpu *ptr,int cpu) {unsigned long *p;
arch/arm64/kernel/smp.c +23 -4
@@ -1047,10 +1047,8 @@ void crash_smp_send_stop(void)* If this cpu is the only one alive at this point in time, online or * not, there are no stop messages to be sent around, so just back out. */ -if (num_other_online_cpus () ==0 ) { -sdei_mask_local_cpu (); -return ; - } +if (num_other_online_cpus () ==0 ) + goto skip_ipi;cpumask_copy (&mask, cpu_online_mask);cpumask_clear_cpu (smp_processor_id (), &mask);
@@ -1069,7 +1067,28 @@ void crash_smp_send_stop(void)pr_warn ("SMP: failed to stop secondary CPUs %*pbl\n" ,cpumask_pr_args (&mask)); +skip_ipi:sdei_mask_local_cpu (); + +#ifdef CONFIG_ARM_SDE_INTERFACE +/* +* If the crash happened in an SDEI event handler then we need to +* finish the handler with the firmware so that we can have working +* interrupts in the crash kernel. +*/ +if (__this_cpu_read (sdei_active_critical_event)) { +pr_warn ("SMP: stopped CPUS from SDEI critical event handler " +"context, attempting to finish handler.\n" ); +sdei_handler_abort (); +__this_cpu_write (sdei_active_critical_event,NULL ); + } +if (__this_cpu_read (sdei_active_normal_event)) { +pr_warn ("SMP: stopped CPUS from SDEI normal event handler " +"context, attempting to finish handler.\n" ); +sdei_handler_abort (); +__this_cpu_write (sdei_active_normal_event,NULL ); + } +#endif }bool smp_crash_stop_failed (void )
Signature · 1 line
2.39.2