Skip to contentSkip to conversation

dovetail: Track the owner of a trap notification by exception frame

PATCH Superseded Jan Kiszka 1 participant 1 message Nov 9 1 message not in the archive

Subsystems (5)
Arm (32-bit) x86 RISC-V arm64 +1 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Jan Kiszka

author
#1 lore raw

PATCH 1/4 dovetail: Track the owner of a trap notification by exception frame

From: Jan Kiszka <[email protected]>

Trap notifications are guarded by _TLF_OOBTRAP: oob_trap_notify() only calls into the companion core if the flag is clear, and sets it; oob_trap_unwind() calls the exit handler and clears the flag whenever it is set.

This does not cope with nested traps. A trap handler running on behalf of an oob thread may trigger another trap before it reaches its own exit annotation, e.g. a user access from an emulation handler faulting in a not yet resident page on ARM:

do_alignment() <- notify, _TLF_OOBTRAP set
do_alignment_ldmstm()
ldrt/strt to user address <- translation fault (SVC mode)
do_DataAbort()
do_page_fault()
dovetail_fault_entry() <- notify suppressed, fine
handle_mm_fault()
dovetail_fault_exit() <- unwind fires: wrong trap number,
wrong frame, and the core may switch us back to oob
...emulation continues oob...
dovetail_fault_exit() <- no-op, flag already clear

Since _TLF_OOBTRAP is per-thread and the thread does not change across nesting levels, the flag alone cannot tell which trap occurrence owns the notification. The exception frame can: every nesting level pushes its own pt_regs on the kernel stack, and the frame is already passed to both the notify and unwind helpers.

Record the owning frame in the task struct when notifying, and only unwind when the frame passed to oob_trap_unwind() matches. Inner trap exits become no-ops, the outer exit unwinds with the trap number and frame the core was notified with. oob_trap_regs can in fact encode the semantic of the _TLF_OOBTRAP flag, thus drop it.

A forking parent can never be inside a trap handler, so neither the flag nor the owner frame can legitimately be set when a new task inherits its state. Add a debug assertion for this in inband_task_init(), which turns an unbalanced trap entry into a loud warning on the next fork instead of silently propagating the stuck state to the children.

Assisted-by opencode:Fable-5.1
---
 arch/arm/include/asm/thread_info.h   |  1 -
 arch/arm64/include/asm/thread_info.h |  1 -
 arch/riscv/include/asm/thread_info.h |  1 -
 arch/x86/include/asm/thread_info.h   |  2 --
 include/linux/dovetail.h             | 12 ++++++++++--
 include/linux/sched.h                |  5 +++++
 kernel/dovetail.c                    | 10 ++++++++--
 7 files changed, 23 insertions(+), 9 deletions(-)
arch/arm/include/asm/thread_info.h +0 -1
index 23cae59037c1e..12ab7e49b7b76 100644
@@ -196,7 +196,6 @@ extern int vfp_restore_user_hwstate(struct user_vfp *,
 #define _TLF_OOB		0x0001
 #define _TLF_DOVETAIL		0x0002
 #define _TLF_OFFSTAGE		0x0004
-#define _TLF_OOBTRAP		0x0008
  #endif /* __KERNEL__ */
 #endif /* __ASM_ARM_THREAD_INFO_H */
arch/arm64/include/asm/thread_info.h +0 -1
index 48563d782a99a..934b080246146 100644
@@ -144,7 +144,6 @@ void arch_setup_new_exec(void);
 #define _TLF_OOB			0x0001
 #define _TLF_DOVETAIL			0x0002
 #define _TLF_OFFSTAGE			0x0004
-#define _TLF_OOBTRAP			0x0008
 #define _TLF_KERNEL_FPU_PREEMPTED	0x0010	/* Dovetail: kernel mode FPSIMD section preempted by OOB */
  #endif /* __ASM_THREAD_INFO_H */
arch/riscv/include/asm/thread_info.h +0 -1
index 820a596d9bf65..11164e12317fa 100644
@@ -140,6 +140,5 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src);
 #define _TLF_OOB		0x0001
 #define _TLF_DOVETAIL		0x0002
 #define _TLF_OFFSTAGE		0x0004
-#define _TLF_OOBTRAP		0x0008
  #endif /* _ASM_RISCV_THREAD_INFO_H */
arch/x86/include/asm/thread_info.h +0 -2
index 6da425bc87712..0c9cdf60e3f12 100644
@@ -218,13 +218,11 @@ static inline int arch_within_stack_frames(const void * const stack,
 #define TS_OOB			0x0010	/* Thread is running out-of-band */
 #define TS_DOVETAIL		0x0020  /* Dovetail notifier enabled */
 #define TS_OFFSTAGE		0x0040	/* Thread is in-flight to OOB context */
-#define TS_OOBTRAP		0x0080	/* Handling a trap from OOB context */
 #define TS_KERNEL_FPU_PREEMPTED	0x0100	/* Dovetail: kernel mode FPU section preempted by OOB */
  #define _TLF_OOB			TS_OOB
 #define _TLF_DOVETAIL			TS_DOVETAIL
 #define _TLF_OFFSTAGE			TS_OFFSTAGE
-#define _TLF_OOBTRAP			TS_OOBTRAP
 #define _TLF_KERNEL_FPU_PREEMPTED	TS_KERNEL_FPU_PREEMPTED
  #ifndef __ASSEMBLER__
include/linux/dovetail.h +8 -1
index 1140c5b7c99f3..645d1eec32aa6 100644
@@ -62,10 +62,18 @@ int pipeline_syscall(unsigned int nr, struct pt_regs *regs);
 void __oob_trap_notify(unsigned int exception,
 		       struct pt_regs *regs);
 +/*
+ * Trap notifications do not nest: a trap handler running on behalf
+ * of an oob thread may itself trigger a fault (e.g. a user access
+ * from an emulation handler), landing into another annotated trap
+ * handler. Only the outermost trap occurrence notifies the companion
+ * core, which is told about the exit when that same occurrence
+ * unwinds.
+ */
 static __always_inline void oob_trap_notify(unsigned int exception,
 					struct pt_regs *regs)
 {
-	if (running_oob() && !test_thread_local_flags(_TLF_OOBTRAP))
+	if (running_oob() && !current->oob_trap_regs)
 		__oob_trap_notify(exception, regs);
 }
 @@ -75,7 +83,7 @@ void __oob_trap_unwind(unsigned int exception,

static __always_inline void oob_trap_unwind(unsigned int exception,
struct pt_regs *regs)
{
- if (test_thread_local_flags(_TLF_OOBTRAP))
+ if (current->oob_trap_regs == regs)
__oob_trap_unwind(exception, regs);
}
diff --git a/include/linux/sched.h b/include/linux/sched.h
index c4291b7f7b6ea..3012926e8e355 100644

include/linux/sched.h +5 -0
@@ -74,6 +74,7 @@ struct perf_event_context;
 struct perf_ctx_data;
 struct pid_namespace;
 struct pipe_inode_info;
+struct pt_regs;
 struct rcu_node;
 struct reclaim_state;
 struct root_domain;
@@ -1302,6 +1303,10 @@ struct task_struct {
 #ifdef CONFIG_IRQ_PIPELINE
 	unsigned long			stall_bits;
 #endif
+#ifdef CONFIG_DOVETAIL
+	/* Exception frame owning the pending oob trap notification. */
+	struct pt_regs			*oob_trap_regs;
+#endif
  #ifdef CONFIG_LOCKDEP
 # define MAX_LOCK_DEPTH			48UL
kernel/dovetail.c +8 -2
index 16ac66e4529f5..f196a9815dabd 100644
@@ -22,6 +22,12 @@ void inband_task_init(struct task_struct *p)
 {
 	struct thread_info *ti = task_thread_info(p);
 
+	/*
+	 * A forking parent cannot be inside a trap handler. If oob_trap_regs
+	 * is non-NULL, some trap entry was not balanced by a matching exit.
+	 */
+	WARN_ON_ONCE(dovetail_debug() && p->oob_trap_regs);
+
 	clear_ti_local_flags(ti, _TLF_DOVETAIL|_TLF_OOB|_TLF_OFFSTAGE);
 	arch_inband_task_init(p);
 }
@@ -284,7 +290,7 @@ noinstr void __oob_trap_notify(unsigned int exception,
 	 * to the in-band stage, may return with hard irqs on.
 	 */
 	if (dovetail_enabled) {
-		set_thread_local_flags(_TLF_OOBTRAP);
+		current->oob_trap_regs = regs;
 		flags = hard_local_irq_save();
 		instrumentation_begin();
 		handle_oob_trap_entry(exception, regs);
@@ -306,7 +312,7 @@ noinstr void __oob_trap_unwind(unsigned int exception, struct pt_regs *regs)
 	 * stage. Must return with hard irqs off.
 	 */
 	hard_local_irq_disable();
-	clear_thread_local_flags(_TLF_OOBTRAP);
+	current->oob_trap_regs = NULL;
 	instrumentation_begin();
 	handle_oob_trap_exit(exception, regs);
 	instrumentation_end();
Signature · 1 line
2.47.3