]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
arm64: sdei: abort running SDEI handlers during crash
authorD Scott Phillips <scott@os.amperecomputing.com>
Tue, 27 Jun 2023 00:29:39 +0000 (17:29 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 19 Sep 2023 10:22:52 +0000 (12:22 +0200)
commit 5cd474e57368f0957c343bb21e309cf82826b1ef upstream.

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.

Fixes: f5df26961853 ("arm64: kernel: Add arch-specific SDEI entry code and CPU masking")
Signed-off-by: D Scott Phillips <scott@os.amperecomputing.com>
Cc: stable@vger.kernel.org
Reviewed-by: James Morse <james.morse@arm.com>
Tested-by: Mihai Carabas <mihai.carabas@oracle.com>
Link: https://lore.kernel.org/r/20230627002939.2758-1-scott@os.amperecomputing.com
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/arm64/include/asm/sdei.h
arch/arm64/kernel/entry.S
arch/arm64/kernel/sdei.c
arch/arm64/kernel/smp.c
drivers/firmware/arm_sdei.c
include/linux/arm_sdei.h

index 7bea1d705dd6408cf53c41b9491d8b68b1851730..6bb0258fb4aa1bef0c90ef01b6a748d5bf402ad0 100644 (file)
@@ -17,6 +17,9 @@
 
 #include <asm/virt.h>
 
+DECLARE_PER_CPU(struct sdei_registered_event *, sdei_active_normal_event);
+DECLARE_PER_CPU(struct sdei_registered_event *, sdei_active_critical_event);
+
 extern unsigned long sdei_exit_mode;
 
 /* Software Delegated Exception entry point from firmware*/
@@ -29,6 +32,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.
index c56b262a2749db1d0c50bee640e81f91cda577af..bdc5f744249bbbe1b4b00b5bdcb2871e9bc93127 100644 (file)
@@ -1005,9 +1005,13 @@ SYM_CODE_START(__sdei_asm_handler)
 
        mov     x19, x1
 
-#if defined(CONFIG_VMAP_STACK) || defined(CONFIG_SHADOW_CALL_STACK)
+       /* Store the registered-event for crash_smp_send_stop() */
        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
        /*
@@ -1072,6 +1076,14 @@ SYM_CODE_START(__sdei_asm_handler)
 
        ldr_l   x2, sdei_exit_mode
 
+       /* Clear the registered-event seen by crash_smp_send_stop() */
+       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]
+
 alternative_if_not ARM64_UNMAP_KERNEL_AT_EL0
        sdei_handler_exit exit_mode=x2
 alternative_else_nop_endif
@@ -1082,4 +1094,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
+       ldr_l   x2, sdei_exit_mode
+       sdei_handler_exit exit_mode=x2
+       // 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 */
index 47f77d1234cb6b3e5a6839fa9ce38437ef63c950..532611d07bdcb38764d240ac1630db68138df75e 100644 (file)
@@ -47,6 +47,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;
index 3beaa6640ab35727929f688b6ffc0144614a7078..bc29cc044a4d7d10a1d3651fa35a1a2be5b73c7b 100644 (file)
@@ -1073,10 +1073,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);
@@ -1095,7 +1093,9 @@ 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();
+       sdei_handler_abort();
 }
 
 bool smp_crash_stop_failed(void)
index f9040bd6108125993fd69ae7b8a24edc9b1e571b..285fe7ad490d1ddd904e0dd73518350bc135ef8c 100644 (file)
@@ -1095,3 +1095,22 @@ int sdei_event_handler(struct pt_regs *regs,
        return err;
 }
 NOKPROBE_SYMBOL(sdei_event_handler);
+
+void sdei_handler_abort(void)
+{
+       /*
+        * 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("still in SDEI critical event 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("still in SDEI normal event context, attempting to finish handler.\n");
+               __sdei_handler_abort();
+               __this_cpu_write(sdei_active_normal_event, NULL);
+       }
+}
index 14dc461b0e829e182ace14a4af0ea8ef037465c1..255701e1251b4ac242456693f998e3940a36851c 100644 (file)
@@ -47,10 +47,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 sdei_handler_abort(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_handler_abort(void) { }
 #endif /* CONFIG_ARM_SDE_INTERFACE */