Skip to contentSkip to conversation

ARM: dovetail: Fix VFP exception trap notification

PATCH In review Jan Kiszka 2 participants 3 messages Nov 9 – Dec 3 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
author
#3 lore raw

PATCH 4/4 ARM: dovetail: Fix VFP exception trap notification

In reply to a message not in the archive

From: Jan Kiszka <[email protected]>

The trap annotation of the VFP bounce path predates upstream
commit c0b5195bad63 ("ARM: 9426/1: vfp: Move sending signals outside of vfp_state_hold()ed section.") which turned vfp_raise_exceptions() into returning the si_code,
deferring vfp_raise_sigfpe() to the exit of VFP_bounce() after
vfp_state_release(). The Dovetail hunk was kept in place verbatim,
which is now wrong in three ways:

- for VFP_EXCEPTION_ERROR, SIGFPE is sent twice: once from the annotated block in vfp_raise_exceptions(), once more from the exit path of VFP_bounce() via the returned si_code,

- the deferred signals for regular FP exceptions enabled in FPSCR (divide by zero, overflow, ...) are not annotated at all, so an oob thread sends them from the oob stage,

- the annotated block runs within the vfp_state_hold() section, which under Dovetail is a hard_preempt_disable() section. If the core demotes the thread to in-band from there, hard_preempt_enable() pairs an oob-stage hard_preempt_disable() which did not touch the preemption count with an in-band preempt_enable_no_resched(), unbalancing it. Besides, that was the very reason for the upstream change on PREEMPT_RT.

Move the notification to the exit path of VFP_bounce(), after the VFP state was released: if any signal is to be raised, have the companion core demote the thread, or skip the signal if the core decided to handle the trap from the oob stage. This mirrors what arm64 does for ARM64_TRAP_FPE in do_fpsimd_exc().

vfp_raise_exceptions() does not need the register frame anymore, restore its upstream signature.

Assisted-by opencode:Fable-5.1
---
 arch/arm/vfp/vfpmodule.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
arch/arm/vfp/vfpmodule.c +19 -8
index 4bbe92c619f2d..5589eb76379b5 100644
@@ -283,7 +283,7 @@ static void vfp_panic(char *reason, u32 inst)
 /*
  * Process bitmask of exception conditions.
  */
-static int vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
+static int vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr)
 {
 	int si_code = 0;
 
@@ -291,10 +291,6 @@ static int vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_r
 
 	if (exceptions == VFP_EXCEPTION_ERROR) {
 		vfp_panic("unhandled bounce", inst);
-		if (mark_cond_trap_entry(ARM_TRAP_VFP, regs)) {
-			vfp_raise_sigfpe(FPE_FLTINV, regs);
-			mark_trap_exit(ARM_TRAP_VFP, regs);
-		}
 		return FPE_FLTINV;
 	}
 
@@ -437,7 +433,7 @@ static void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs, unsigned lo
 		 * unallocated VFP instruction but with FPSCR.IXE set and not
 		 * on VFP subarch 1.
 		 */
-		si_code = vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
+		si_code = vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr);
 		goto exit;
 	}
 
@@ -462,7 +458,7 @@ static void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs, unsigned lo
 	 */
 	exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
 	if (exceptions)
-		si_code2 = vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
+		si_code2 = vfp_raise_exceptions(exceptions, trigger, orig_fpscr);
 
 	/*
 	 * If there isn't a second FP instruction, exit now. Note that
@@ -476,13 +472,28 @@ static void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs, unsigned lo
 emulate:
 	exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
 	if (exceptions)
-		si_code = vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
+		si_code = vfp_raise_exceptions(exceptions, trigger, orig_fpscr);
 exit:
 	vfp_state_release(flags);
+
+	if (!si_code && !si_code2)
+		return;
+
+	/*
+	 * Dovetail: the companion core may handle the FP exception of
+	 * an oob thread entirely from the oob stage, in which case it
+	 * does not demote us to in-band and we must not raise the
+	 * signal. Otherwise, we run in-band from this point.
+	 */
+	if (!mark_cond_trap_entry(ARM_TRAP_VFP, regs))
+		return;
+
 	if (si_code2)
 		vfp_raise_sigfpe(si_code2, regs);
 	if (si_code)
 		vfp_raise_sigfpe(si_code, regs);
+
+	mark_trap_exit(ARM_TRAP_VFP, regs);
 }
 
 static void vfp_enable(void *unused)
Signature · 1 line
2.47.3

Jan Kiszka

author
#1 lore raw

PATCH 3/4 ARM: dovetail: Notify the core about undefined instruction traps from user mode

In reply to a message not in the archive

From: Jan Kiszka <[email protected]>

Unlike the kernel mode entry (__und_svc), the user mode undefined instruction path does not inform the companion core at all. This is intended for the undef hooks which must stay oob-capable, like the lazy VFP state restore in vfp_support_entry(), but leaves the in-band fallbacks of that path unannotated, so an oob thread may run them from the oob stage:

- do_undefinstr() raising SIGILL when no hook claimed the instruction or the instruction could not be read from user memory,

- swp_handler() emulating SWP{B}, which accesses user memory (and may fault in pages), takes the mmap lock and raises SIGSEGV on errors,

- uprobe_trap_handler(), which depends on the in-band return to user path for running the probe handlers, a path an oob thread skips.

Annotate them. do_undefinstr() uses the conditional form, since the companion core may want to handle an undefined instruction of an oob thread entirely from the oob stage, in which case it does not demote the thread and we must not inject the in-band signal (this is the behavior arm64 implements in do_el0_undef()). Kernel mode faults are already covered by the notification issued from __und_svc, don't notify twice.

swp_handler() and uprobe_trap_handler() are restricted to user mode by their hook descriptors and always need the in-band stage, so they use the strict form like break_trap() does.

Assisted-by opencode:Fable-5.1
---
 arch/arm/kernel/swp_emulate.c  | 22 ++++++++++++++++++----
 arch/arm/kernel/traps.c        | 12 ++++++++++++
 arch/arm/probes/uprobes/core.c | 10 ++++++++++
 3 files changed, 40 insertions(+), 4 deletions(-)
arch/arm/kernel/swp_emulate.c +18 -4
index fdce83c95acb0..39b45c598f7f0 100644
@@ -23,6 +23,7 @@
 #include <linux/sched/mm.h>
 #include <linux/syscalls.h>
 #include <linux/perf_event.h>
+#include <linux/dovetail.h>
 
 #include <asm/opcodes.h>
 #include <asm/system_info.h>
@@ -161,6 +162,14 @@ static int swp_handler(struct pt_regs *regs, unsigned int instr)
 {
 	unsigned int address, destreg, data, type;
 	unsigned int res = 0;
+	int ret = 0;
+
+	/*
+	 * Dovetail: emulating the access may fault on user memory,
+	 * and failures are reported by signal. We need to run
+	 * in-band for this, have the companion core demote us.
+	 */
+	mark_trap_entry(ARM_TRAP_UNDEFINSTR, regs);
 
 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc);
 
@@ -171,12 +180,14 @@ static int swp_handler(struct pt_regs *regs, unsigned int instr)
 	case ARM_OPCODE_CONDTEST_FAIL:
 		/* Condition failed - return to next instruction */
 		regs->ARM_pc += 4;
-		return 0;
+		goto out;
 	case ARM_OPCODE_CONDTEST_UNCOND:
 		/* If unconditional encoding - not a SWP, undef */
-		return -EFAULT;
+		ret = -EFAULT;
+		goto out;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
+		goto out;
 	}
 
 	if (current->pid != previous_pid) {
@@ -220,7 +231,10 @@ static int swp_handler(struct pt_regs *regs, unsigned int instr)
 		set_segfault(regs, address);
 	}
 
-	return 0;
+out:
+	mark_trap_exit(ARM_TRAP_UNDEFINSTR, regs);
+
+	return ret;
 }
 
 /*
arch/arm/kernel/traps.c +12 -0
index 8ca5ba26d2598..059a1dc900431 100644
@@ -483,6 +483,16 @@ asmlinkage void do_undefinstr(struct pt_regs *regs)
 		return;
 
 die_sig:
+	/*
+	 * Dovetail: the companion core may handle an undefined
+	 * instruction raised by an oob thread entirely from the oob
+	 * stage, in which case it does not demote us to in-band and
+	 * we must not apply the in-band fixup. The kernel mode case
+	 * was notified from __und_svc already.
+	 */
+	if (user_mode(regs) &&
+	    !mark_cond_trap_entry(ARM_TRAP_UNDEFINSTR, regs))
+		return;
 #ifdef CONFIG_DEBUG_USER
 	if (user_debug & UDBG_UNDEFINED) {
 		pr_info("%s (%d): undefined instruction: pc=%px\n",
@@ -493,6 +503,8 @@ asmlinkage void do_undefinstr(struct pt_regs *regs)
 #endif
 	arm_notify_die("Oops - undefined instruction", regs,
 		       SIGILL, ILL_ILLOPC, pc, 0, 6);
+	if (user_mode(regs))
+		mark_trap_exit(ARM_TRAP_UNDEFINSTR, regs);
 }
 NOKPROBE_SYMBOL(do_undefinstr)
 
arch/arm/probes/uprobes/core.c +10 -0
index 0e1c6b9e7e54a..06b3b5e37e73d 100644
@@ -10,6 +10,7 @@
 #include <linux/sched.h>
 #include <linux/uprobes.h>
 #include <linux/notifier.h>
+#include <linux/dovetail.h>
 
 #include <asm/opcodes.h>
 #include <asm/traps.h>
@@ -185,6 +186,13 @@ static int uprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
 {
 	unsigned long flags;
 
+	/*
+	 * Dovetail: uprobe handling runs from the in-band return to
+	 * user path, which an oob thread would skip. Have the
+	 * companion core demote us first.
+	 */
+	mark_trap_entry(ARM_TRAP_BREAK, regs);
+
 	local_irq_save(flags);
 	instr &= 0x0fffffff;
 	if (instr == (UPROBE_SWBP_ARM_INSN & 0x0fffffff))
@@ -193,6 +201,8 @@ static int uprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
 		uprobe_post_sstep_notifier(regs);
 	local_irq_restore(flags);
 
+	mark_trap_exit(ARM_TRAP_BREAK, regs);
+
 	return 0;
 }
 
Signature · 1 line
2.47.3
#2 lore raw

Re: [dovetail][PATCH 3/4] ARM: dovetail: Notify the core about undefined instruction traps from user mode

Quoted 103 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

Unlike the kernel mode entry (__und_svc), the user mode undefined instruction path does not inform the companion core at all. This is intended for the undef hooks which must stay oob-capable, like the lazy VFP state restore in vfp_support_entry(), but leaves the in-band fallbacks of that path unannotated, so an oob thread may run them from the oob stage:

- do_undefinstr() raising SIGILL when no hook claimed the instruction or the instruction could not be read from user memory,

- swp_handler() emulating SWP{B}, which accesses user memory (and may fault in pages), takes the mmap lock and raises SIGSEGV on errors,

- uprobe_trap_handler(), which depends on the in-band return to user path for running the probe handlers, a path an oob thread skips.

Annotate them. do_undefinstr() uses the conditional form, since the companion core may want to handle an undefined instruction of an oob thread entirely from the oob stage, in which case it does not demote the thread and we must not inject the in-band signal (this is the behavior arm64 implements in do_el0_undef()). Kernel mode faults are already covered by the notification issued from __und_svc, don't notify twice.

swp_handler() and uprobe_trap_handler() are restricted to user mode by their hook descriptors and always need the in-band stage, so they use the strict form like break_trap() does.

Assisted-by opencode:Fable-5.1
---
 arch/arm/kernel/swp_emulate.c  | 22 ++++++++++++++++++----
 arch/arm/kernel/traps.c        | 12 ++++++++++++
 arch/arm/probes/uprobes/core.c | 10 ++++++++++
 3 files changed, 40 insertions(+), 4 deletions(-)
arch/arm/kernel/swp_emulate.c +18 -4
index fdce83c95acb0..39b45c598f7f0 100644
@@ -23,6 +23,7 @@
 #include <linux/sched/mm.h>
 #include <linux/syscalls.h>
 #include <linux/perf_event.h>
+#include <linux/dovetail.h>
 
 #include <asm/opcodes.h>
 #include <asm/system_info.h>
@@ -161,6 +162,14 @@ static int swp_handler(struct pt_regs *regs, unsigned int instr)
 {
 	unsigned int address, destreg, data, type;
 	unsigned int res = 0;
+	int ret = 0;
+
+	/*
+	 * Dovetail: emulating the access may fault on user memory,
+	 * and failures are reported by signal. We need to run
+	 * in-band for this, have the companion core demote us.
+	 */
+	mark_trap_entry(ARM_TRAP_UNDEFINSTR, regs);
 
 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc);
 
@@ -171,12 +180,14 @@ static int swp_handler(struct pt_regs *regs, unsigned int instr)
 	case ARM_OPCODE_CONDTEST_FAIL:
 		/* Condition failed - return to next instruction */
 		regs->ARM_pc += 4;
-		return 0;
+		goto out;
 	case ARM_OPCODE_CONDTEST_UNCOND:
 		/* If unconditional encoding - not a SWP, undef */
-		return -EFAULT;
+		ret = -EFAULT;
+		goto out;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
+		goto out;
 	}
 
 	if (current->pid != previous_pid) {
@@ -220,7 +231,10 @@ static int swp_handler(struct pt_regs *regs, unsigned int instr)
 		set_segfault(regs, address);
 	}
 
-	return 0;
+out:
+	mark_trap_exit(ARM_TRAP_UNDEFINSTR, regs);
+
+	return ret;
 }
 
 /*
arch/arm/kernel/traps.c +0 -0
index 8ca5ba26d2598..059a1dc900431 100644
@@ -483,6 +483,16 @@ asmlinkage void do_undefinstr(struct pt_regs *regs)
 		return;
 
 die_sig:

arch/arm/kernel/traps.c asmlinkage void do_undefinstr(struct pt_regs *regs) · line 495

Quoted 10 lines
arch/arm/kernel/traps.c +10 -0
+	/*
+	 * Dovetail: the companion core may handle an undefined
+	 * instruction raised by an oob thread entirely from the oob
+	 * stage, in which case it does not demote us to in-band and
+	 * we must not apply the in-band fixup. The kernel mode case
+	 * was notified from __und_svc already.
+	 */
+	if (user_mode(regs) &&
+	    !mark_cond_trap_entry(ARM_TRAP_UNDEFINSTR, regs))
+		return;

Jan, that is the patch referenced in the other thread. That is most likely where the AI put the mark_cond_trap_entry() pattern from.

Question - especially regarding the comment above, which is right - targeting Philippe:

Why do we have that split? In supervisor mode (exception over kernel) we notify the co-kernel from assembly already (__und_svc), while we delay the notification to do_undefinstr() over user mode.

Quoted 49 lines
arch/arm/kernel/traps.c +2 -0
 #ifdef CONFIG_DEBUG_USER
 	if (user_debug & UDBG_UNDEFINED) {
 		pr_info("%s (%d): undefined instruction: pc=%px\n",
@@ -493,6 +503,8 @@ asmlinkage void do_undefinstr(struct pt_regs *regs)
 #endif
 	arm_notify_die("Oops - undefined instruction", regs,
 		       SIGILL, ILL_ILLOPC, pc, 0, 6);
+	if (user_mode(regs))
+		mark_trap_exit(ARM_TRAP_UNDEFINSTR, regs);
 }
 NOKPROBE_SYMBOL(do_undefinstr)
 
arch/arm/probes/uprobes/core.c +10 -0
index 0e1c6b9e7e54a..06b3b5e37e73d 100644
@@ -10,6 +10,7 @@
 #include <linux/sched.h>
 #include <linux/uprobes.h>
 #include <linux/notifier.h>
+#include <linux/dovetail.h>
 
 #include <asm/opcodes.h>
 #include <asm/traps.h>
@@ -185,6 +186,13 @@ static int uprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
 {
 	unsigned long flags;
 
+	/*
+	 * Dovetail: uprobe handling runs from the in-band return to
+	 * user path, which an oob thread would skip. Have the
+	 * companion core demote us first.
+	 */
+	mark_trap_entry(ARM_TRAP_BREAK, regs);
+
 	local_irq_save(flags);
 	instr &= 0x0fffffff;
 	if (instr == (UPROBE_SWBP_ARM_INSN & 0x0fffffff))
@@ -193,6 +201,8 @@ static int uprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
 		uprobe_post_sstep_notifier(regs);
 	local_irq_restore(flags);
 
+	mark_trap_exit(ARM_TRAP_BREAK, regs);
+
 	return 0;
 }
 
Signature · 1 line
2.47.3