Skip to contentSkip to conversation

entry: Provide generic implementation for host and guest entry/exit work

PATCH Applied Thomas Gleixner 9 participants 64 messages Oct 23, 2019 – Feb 27, 2020

Subsystems (8)

v2 of 5 Applied Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover entry: Provide generic implementation for host and guest entry/exit work

When working on a way to move out the posix cpu timer expiry out of the timer interrupt context, I noticed that KVM is not handling pending task work before entering a guest. A quick hack was to add that to the x86 KVM handling loop. The discussion ended with a request to make this a generic infrastructure possible with also moving the per arch implementations of the enter from and return to user space handling generic.

https://lore.kernel.org/r/[email protected]

The series implements the syscall enter/exit and the general exit to userspace work handling along with the pre guest enter functionality.

Changes vs. RFC version:

- Dropped ARM64 conversion as requested by ARM64 folks

- Addressed various review comments (Peter, Andy, Mike, Paolo, Josh, Miroslav)

- Picked up, fixed and completed Peter's patch which makes interrupt enable/disable symmetric in trap handlers

- Completed the removal of irq disabling / irq tracing from low level ASM code

- Moved KVM specific parts of the enter guest mode handling to KVM

    (Paolo)

The series is also available from git:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git WIP/core.entry

Thanks,

tglx

RFC version: https://lore.kernel.org/r/[email protected]

---
 /Makefile                             |    3 
 arch/Kconfig                          |    3 
 arch/x86/Kconfig                      |    1 
 arch/x86/entry/calling.h              |   12 +
 arch/x86/entry/common.c               |  264 ++------------------------------
 arch/x86/entry/entry_32.S             |   41 ----
 arch/x86/entry/entry_64.S             |   32 ---
 arch/x86/entry/entry_64_compat.S      |   30 ---
 arch/x86/include/asm/irqflags.h       |    8 
 arch/x86/include/asm/paravirt.h       |    9 -
 arch/x86/include/asm/signal.h         |    1 
 arch/x86/include/asm/thread_info.h    |    9 -
 arch/x86/kernel/signal.c              |    2 
 arch/x86/kernel/traps.c               |   33 ++--
 arch/x86/kvm/x86.c                    |   17 --
 arch/x86/mm/fault.c                   |    7 
 b/arch/x86/include/asm/entry-common.h |  104 ++++++++++++
 b/arch/x86/kvm/Kconfig                |    1 
 b/include/linux/entry-common.h        |  280 ++++++++++++++++++++++++++++++++++
 b/kernel/entry/common.c               |  184 ++++++++++++++++++++++
 include/linux/kvm_host.h              |   64 +++++++
 kernel/Makefile                       |    1 
 virt/kvm/Kconfig                      |    3 
 23 files changed, 735 insertions(+), 374 deletions(-)
#1 lore raw

PATCH v2 1/17 x86/entry/32: Remove unused resume_userspace label

The C reimplementation of SYSENTER left that unused ENTRY() label around. Remove it.

Fixes 5f310f739b4c ("x86/entry/32: Re-implement SYSENTER using the new C path")
---
 arch/x86/entry/entry_32.S |    1 -
 1 file changed, 1 deletion(-)
arch/x86/entry/entry_32.S +0 -1
@@ -825,7 +825,6 @@ END(ret_from_fork)
 	cmpl	$USER_RPL, %eax
 	jb	restore_all_kernel		# not returning to v8086 or userspace
 
-ENTRY(resume_userspace)
 	DISABLE_INTERRUPTS(CLBR_ANY)
 	TRACE_IRQS_OFF
 	movl	%esp, %eax
#5 lore raw

PATCH v2 2/17 x86/entry/64: Remove pointless jump in paranoid_exit

In reply to Thomas Gleixner

Jump directly to restore_regs_and_return_to_kernel instead of making a pointless extra jump through .Lparanoid_exit_restore

---
 arch/x86/entry/entry_64.S |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/entry/entry_64.S +1 -2
@@ -1272,12 +1272,11 @@ ENTRY(paranoid_exit)
 	/* Always restore stashed CR3 value (see paranoid_entry) */
 	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
 	SWAPGS_UNSAFE_STACK
-	jmp	.Lparanoid_exit_restore
+	jmp	restore_regs_and_return_to_kernel
 .Lparanoid_exit_no_swapgs:
 	TRACE_IRQS_IRETQ_DEBUG
 	/* Always restore stashed CR3 value (see paranoid_entry) */
 	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
-.Lparanoid_exit_restore:
 	jmp restore_regs_and_return_to_kernel
 END(paranoid_exit)
 
#9 lore raw

PATCH v2 3/17 x86/traps: Remove pointless irq enable from do_spurious_interrupt_bug()

In reply to Thomas Gleixner

That function returns immediately after conditionally reenabling interrupts which is more than pointless and requires the ASM code to disable interrupts again.

---
 arch/x86/kernel/traps.c |    1 -
 1 file changed, 1 deletion(-)
arch/x86/kernel/traps.c +0 -1
@@ -871,7 +871,6 @@ do_simd_coprocessor_error(struct pt_regs
 dotraplinkage void
 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
 {
-	cond_local_irq_enable(regs);
 }
 
 dotraplinkage void
#17 lore raw

PATCH v2 4/17 x86/entry: Make DEBUG_ENTRY_ASSERT_IRQS_OFF available for 32bit

In reply to Thomas Gleixner

Move the interrupt state verification debug macro to common code and fixup the irqflags and paravirt components so it can be used in 32bit code later.

---
 arch/x86/entry/calling.h        |   12 ++++++++++++
 arch/x86/entry/entry_64.S       |   12 ------------
 arch/x86/include/asm/irqflags.h |    8 ++++++--
 arch/x86/include/asm/paravirt.h |    9 +++++----
 4 files changed, 23 insertions(+), 18 deletions(-)
arch/x86/entry/calling.h +12 -0
@@ -366,3 +366,15 @@ For 32-bit we have the following convent
 #else
 #define GET_CR2_INTO(reg) _ASM_MOV %cr2, reg
 #endif
+
+.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
+#ifdef CONFIG_DEBUG_ENTRY
+	push %_ASM_AX
+	SAVE_FLAGS(CLBR_EAX)
+	test $X86_EFLAGS_IF, %_ASM_AX
+	jz .Lokay_\@
+	ud2
+.Lokay_\@:
+	pop %_ASM_AX
+#endif
+.endm
arch/x86/entry/entry_64.S +0 -12
@@ -387,18 +387,6 @@ ENTRY(spurious_entries_start)
     .endr
 END(spurious_entries_start)
 
-.macro DEBUG_ENTRY_ASSERT_IRQS_OFF
-#ifdef CONFIG_DEBUG_ENTRY
-	pushq %rax
-	SAVE_FLAGS(CLBR_RAX)
-	testl $X86_EFLAGS_IF, %eax
-	jz .Lokay_\@
-	ud2
-.Lokay_\@:
-	popq %rax
-#endif
-.endm
-
 /*
  * Enters the IRQ stack if we're not already using it.  NMI-safe.  Clobbers
  * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
arch/x86/include/asm/irqflags.h +6 -2
@@ -126,11 +126,15 @@ static inline notrace unsigned long arch
 #define ENABLE_INTERRUPTS(x)	sti
 #define DISABLE_INTERRUPTS(x)	cli
 
-#ifdef CONFIG_X86_64
 #ifdef CONFIG_DEBUG_ENTRY
-#define SAVE_FLAGS(x)		pushfq; popq %rax
+# ifdef CONFIG_X86_64
+#  define SAVE_FLAGS(x)		pushfq; popq %rax
+# else
+#  define SAVE_FLAGS(x)		pushfl; popl %eax
+# endif
 #endif
 
+#ifdef CONFIG_X86_64
 #define SWAPGS	swapgs
 /*
  * Currently paravirt can't handle swapgs nicely when we
arch/x86/include/asm/paravirt.h +5 -4
@@ -904,6 +904,11 @@ extern void default_banner(void);
 		  ANNOTATE_RETPOLINE_SAFE;				\
 		  jmp PARA_INDIRECT(pv_ops+PV_CPU_usergs_sysret64);)
 
+#endif /* CONFIG_PARAVIRT_XXL */
+#endif	/* CONFIG_X86_64 */
+
+#ifdef CONFIG_PARAVIRT_XXL
+
 #ifdef CONFIG_DEBUG_ENTRY
 #define SAVE_FLAGS(clobbers)                                        \
 	PARA_SITE(PARA_PATCH(PV_IRQ_save_fl),			    \
@@ -912,10 +917,6 @@ extern void default_banner(void);
 		  call PARA_INDIRECT(pv_ops+PV_IRQ_save_fl);	    \
 		  PV_RESTORE_REGS(clobbers | CLBR_CALLEE_SAVE);)
 #endif
-#endif /* CONFIG_PARAVIRT_XXL */
-#endif	/* CONFIG_X86_64 */
-
-#ifdef CONFIG_PARAVIRT_XXL
 
 #define GET_CR2_INTO_AX							\
 	PARA_SITE(PARA_PATCH(PV_MMU_read_cr2),				\
#20 lore raw

PATCH v2 5/17 x86/traps: Make interrupt enable/disable symmetric in C code

In reply to Thomas Gleixner

Traps enable interrupts conditionally but rely on the ASM return code to disable them again. That results in redundant interrupt disable and trace calls.

Make the trap handlers disable interrupts before returning to avoid that, which allows simplification of the ASM entry code.

---
 arch/x86/kernel/traps.c |   32 +++++++++++++++++++++-----------
 arch/x86/mm/fault.c     |    7 +++++--
 2 files changed, 26 insertions(+), 13 deletions(-)
arch/x86/kernel/traps.c +21 -11
@@ -276,6 +276,7 @@ static void do_error_trap(struct pt_regs
 			NOTIFY_STOP) {
 		cond_local_irq_enable(regs);
 		do_trap(trapnr, signr, str, regs, error_code, sicode, addr);
+		cond_local_irq_disable(regs);
 	}
 }
 
@@ -501,6 +502,7 @@ dotraplinkage void do_bounds(struct pt_r
 		die("bounds", regs, error_code);
 	}
 
+	cond_local_irq_disable(regs);
 	return;
 
 exit_trap:
@@ -512,6 +514,7 @@ dotraplinkage void do_bounds(struct pt_r
 	 * time..
 	 */
 	do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, 0, NULL);
+	cond_local_irq_disable(regs);
 }
 
 dotraplinkage void
@@ -525,19 +528,19 @@ do_general_protection(struct pt_regs *re
 
 	if (static_cpu_has(X86_FEATURE_UMIP)) {
 		if (user_mode(regs) && fixup_umip_exception(regs))
-			return;
+			goto exit_trap;
 	}
 
 	if (v8086_mode(regs)) {
 		local_irq_enable();
 		handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
-		return;
+		goto exit_trap;
 	}
 
 	tsk = current;
 	if (!user_mode(regs)) {
 		if (fixup_exception(regs, X86_TRAP_GP, error_code, 0))
-			return;
+			goto exit_trap;
 
 		tsk->thread.error_code = error_code;
 		tsk->thread.trap_nr = X86_TRAP_GP;
@@ -549,12 +552,12 @@ do_general_protection(struct pt_regs *re
 		 */
 		if (!preemptible() && kprobe_running() &&
 		    kprobe_fault_handler(regs, X86_TRAP_GP))
-			return;
+			goto exit_trap;
 
 		if (notify_die(DIE_GPF, desc, regs, error_code,
 			       X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
 			die(desc, regs, error_code);
-		return;
+		goto exit_trap;
 	}
 
 	tsk->thread.error_code = error_code;
@@ -563,6 +566,8 @@ do_general_protection(struct pt_regs *re
 	show_signal(tsk, SIGSEGV, "", desc, regs, error_code);
 
 	force_sig(SIGSEGV);
+exit_trap:
+	cond_local_irq_disable(regs);
 }
 NOKPROBE_SYMBOL(do_general_protection);
 
@@ -783,9 +788,7 @@ dotraplinkage void do_debug(struct pt_re
 	if (v8086_mode(regs)) {
 		handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
 					X86_TRAP_DB);
-		cond_local_irq_disable(regs);
-		debug_stack_usage_dec();
-		goto exit;
+		goto exit_irq;
 	}
 
 	if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
@@ -802,6 +805,8 @@ dotraplinkage void do_debug(struct pt_re
 	si_code = get_si_code(tsk->thread.debugreg6);
 	if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
 		send_sigtrap(regs, error_code, si_code);
+
+exit_irq:
 	cond_local_irq_disable(regs);
 	debug_stack_usage_dec();
 
@@ -827,7 +832,7 @@ static void math_error(struct pt_regs *r
 
 	if (!user_mode(regs)) {
 		if (fixup_exception(regs, trapnr, error_code, 0))
-			return;
+			goto exit_trap;
 
 		task->thread.error_code = error_code;
 		task->thread.trap_nr = trapnr;
@@ -835,7 +840,7 @@ static void math_error(struct pt_regs *r
 		if (notify_die(DIE_TRAP, str, regs, error_code,
 					trapnr, SIGFPE) != NOTIFY_STOP)
 			die(str, regs, error_code);
-		return;
+		goto exit_trap;
 	}
 
 	/*
@@ -849,10 +854,12 @@ static void math_error(struct pt_regs *r
 	si_code = fpu__exception_code(fpu, trapnr);
 	/* Retry when we get spurious exceptions: */
 	if (!si_code)
-		return;
+		goto exit_trap;
 
 	force_sig_fault(SIGFPE, si_code,
 			(void __user *)uprobe_get_trap_addr(regs));
+exit_trap:
+	cond_local_irq_disable(regs);
 }
 
 dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
@@ -888,6 +895,8 @@ do_device_not_available(struct pt_regs *
 
 		info.regs = regs;
 		math_emulate(&info);
+
+		cond_local_irq_disable(regs);
 		return;
 	}
 #endif
@@ -918,6 +927,7 @@ dotraplinkage void do_iret_error(struct
 		do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
 			ILL_BADSTK, (void __user *)NULL);
 	}
+	local_irq_disable();
 }
 #endif
 
arch/x86/mm/fault.c +5 -2
@@ -1500,10 +1500,13 @@ static noinline void
 		return;
 
 	/* Was the fault on kernel-controlled part of the address space? */
-	if (unlikely(fault_in_kernel_space(address)))
+	if (unlikely(fault_in_kernel_space(address))) {
 		do_kern_addr_fault(regs, hw_error_code, address);
-	else
+	} else {
 		do_user_addr_fault(regs, hw_error_code, address);
+		if (regs->flags & X86_EFLAGS_IF)
+			local_irq_disable();
+	}
 }
 NOKPROBE_SYMBOL(__do_page_fault);
 
#25 lore raw

PATCH v2 6/17 x86/entry/32: Remove redundant interrupt disable

In reply to Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls and the misnomed preempt_stop macro. As a consequence ret_from_exception and ret_from_intr collapse.

Add a debug check to verify that interrupts are disabled depending on CONFIG_DEBUG_ENTRY.

---
 arch/x86/entry/entry_32.S |   21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
arch/x86/entry/entry_32.S +6 -15
@@ -63,12 +63,6 @@
  * enough to patch inline, increasing performance.
  */
 
-#ifdef CONFIG_PREEMPTION
-# define preempt_stop(clobbers)	DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
-#else
-# define preempt_stop(clobbers)
-#endif
-
 .macro TRACE_IRQS_IRET
 #ifdef CONFIG_TRACE_IRQFLAGS
 	testl	$X86_EFLAGS_IF, PT_EFLAGS(%esp)     # interrupts off?
@@ -809,8 +803,7 @@ END(ret_from_fork)
 	# userspace resumption stub bypassing syscall exit tracing
 	ALIGN
 ret_from_exception:
-	preempt_stop(CLBR_ANY)
-ret_from_intr:
+	DEBUG_ENTRY_ASSERT_IRQS_OFF
 #ifdef CONFIG_VM86
 	movl	PT_EFLAGS(%esp), %eax		# mix EFLAGS and CS
 	movb	PT_CS(%esp), %al
@@ -825,8 +818,6 @@ END(ret_from_fork)
 	cmpl	$USER_RPL, %eax
 	jb	restore_all_kernel		# not returning to v8086 or userspace
 
-	DISABLE_INTERRUPTS(CLBR_ANY)
-	TRACE_IRQS_OFF
 	movl	%esp, %eax
 	call	prepare_exit_to_usermode
 	jmp	restore_all
@@ -1084,7 +1075,7 @@ ENTRY(entry_INT80_32)
 
 restore_all_kernel:
 #ifdef CONFIG_PREEMPTION
-	DISABLE_INTERRUPTS(CLBR_ANY)
+	/* Interrupts are disabled and debug-checked */
 	cmpl	$0, PER_CPU_VAR(__preempt_count)
 	jnz	.Lno_preempt
 	testl	$X86_EFLAGS_IF, PT_EFLAGS(%esp)	# interrupts off (exception path) ?
@@ -1189,7 +1180,7 @@ END(spurious_entries_start)
 	TRACE_IRQS_OFF
 	movl	%esp, %eax
 	call	smp_spurious_interrupt
-	jmp	ret_from_intr
+	jmp	ret_from_exception
 ENDPROC(common_spurious)
 #endif
 
@@ -1207,7 +1198,7 @@ ENDPROC(common_spurious)
 	TRACE_IRQS_OFF
 	movl	%esp, %eax
 	call	do_IRQ
-	jmp	ret_from_intr
+	jmp	ret_from_exception
 ENDPROC(common_interrupt)
 
 #define BUILD_INTERRUPT3(name, nr, fn)			\
@@ -1219,7 +1210,7 @@ ENTRY(name)						\
 	TRACE_IRQS_OFF					\
 	movl	%esp, %eax;				\
 	call	fn;					\
-	jmp	ret_from_intr;				\
+	jmp	ret_from_exception;				\
 ENDPROC(name)
 
 #define BUILD_INTERRUPT(name, nr)		\
@@ -1366,7 +1357,7 @@ ENTRY(xen_do_upcall)
 #ifndef CONFIG_PREEMPTION
 	call	xen_maybe_preempt_hcall
 #endif
-	jmp	ret_from_intr
+	jmp	ret_from_exception
 ENDPROC(xen_hypervisor_callback)
 
 /*
#28 lore raw

PATCH v2 7/17 x86/entry/64: Remove redundant interrupt disable

In reply to Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls.

Add debug checks where appropriate.

---
 arch/x86/entry/entry_64.S |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
arch/x86/entry/entry_64.S +3 -6
@@ -595,8 +595,7 @@ END(common_spurious)
 	call	do_IRQ	/* rdi points to pt_regs */
 	/* 0(%rsp): old RSP */
 ret_from_intr:
-	DISABLE_INTERRUPTS(CLBR_ANY)
-	TRACE_IRQS_OFF
+	DEBUG_ENTRY_ASSERT_IRQS_OFF
 
 	LEAVE_IRQ_STACK
 
@@ -1252,8 +1251,7 @@ END(paranoid_entry)
  */
 ENTRY(paranoid_exit)
 	UNWIND_HINT_REGS
-	DISABLE_INTERRUPTS(CLBR_ANY)
-	TRACE_IRQS_OFF_DEBUG
+	DEBUG_ENTRY_ASSERT_IRQS_OFF
 	testl	%ebx, %ebx			/* swapgs needed? */
 	jnz	.Lparanoid_exit_no_swapgs
 	TRACE_IRQS_IRETQ
@@ -1356,8 +1354,7 @@ END(error_entry)
 
 ENTRY(error_exit)
 	UNWIND_HINT_REGS
-	DISABLE_INTERRUPTS(CLBR_ANY)
-	TRACE_IRQS_OFF
+	DEBUG_ENTRY_ASSERT_IRQS_OFF
 	testb	$3, CS(%rsp)
 	jz	retint_kernel
 	jmp	retint_user
#39 lore raw

PATCH v2 8/17 x86/entry: Move syscall irq tracing to C code

In reply to Thomas Gleixner

Interrupt state tracing can be safely done in C code. The few stack operations in assembly do not need to be covered.

Remove the now pointless indirection via .Lsyscall_32_done and jump to swapgs_restore_regs_and_return_to_usermode directly.

---
 arch/x86/entry/common.c          |   10 ++++++++++
 arch/x86/entry/entry_32.S        |   17 -----------------
 arch/x86/entry/entry_64.S        |    6 ------
 arch/x86/entry/entry_64_compat.S |   30 ++++--------------------------
 4 files changed, 14 insertions(+), 49 deletions(-)
arch/x86/entry/common.c +10 -0
@@ -218,6 +218,9 @@ static void exit_to_usermode_loop(struct
 	user_enter_irqoff();
 
 	mds_user_clear_cpu_buffers();
+
+	/* The return to usermode reenables interrupts. Tell the tracer */
+	trace_hardirqs_on();
 }
 
 #define SYSCALL_EXIT_WORK_FLAGS				\
@@ -279,6 +282,9 @@ static void syscall_slow_exit_work(struc
 {
 	struct thread_info *ti;
 
+	/* User to kernel transition disabled interrupts. */
+	trace_hardirqs_off();
+
 	enter_from_user_mode();
 	local_irq_enable();
 	ti = current_thread_info();
@@ -351,6 +357,7 @@ static __always_inline void do_syscall_3
 /* Handles int $0x80 */
 __visible void do_int80_syscall_32(struct pt_regs *regs)
 {
+	trace_hardirqs_off();
 	enter_from_user_mode();
 	local_irq_enable();
 	do_syscall_32_irqs_on(regs);
@@ -367,6 +374,9 @@ static __always_inline void do_syscall_3
 	unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
 		vdso_image_32.sym_int80_landing_pad;
 
+	/* User to kernel transition disabled interrupts. */
+	trace_hardirqs_off();
+
 	/*
 	 * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
 	 * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
arch/x86/entry/entry_32.S +0 -17
@@ -924,12 +924,6 @@ ENTRY(entry_SYSENTER_32)
 	jnz	.Lsysenter_fix_flags
 .Lsysenter_flags_fixed:
 
-	/*
-	 * User mode is traced as though IRQs are on, and SYSENTER
-	 * turned them off.
-	 */
-	TRACE_IRQS_OFF
-
 	movl	%esp, %eax
 	call	do_fast_syscall_32
 	/* XEN PV guests always use IRET path */
@@ -939,8 +933,6 @@ ENTRY(entry_SYSENTER_32)
 	STACKLEAK_ERASE
 
 /* Opportunistic SYSEXIT */
-	TRACE_IRQS_ON			/* User mode traces as IRQs on. */
-
 	/*
 	 * Setup entry stack - we keep the pointer in %eax and do the
 	 * switch after almost all user-state is restored.
@@ -1039,12 +1031,6 @@ ENTRY(entry_INT80_32)
 
 	SAVE_ALL pt_regs_ax=$-ENOSYS switch_stacks=1	/* save rest */
 
-	/*
-	 * User mode is traced as though IRQs are on, and the interrupt gate
-	 * turned them off.
-	 */
-	TRACE_IRQS_OFF
-
 	movl	%esp, %eax
 	call	do_int80_syscall_32
 .Lsyscall_32_done:
@@ -1052,11 +1038,8 @@ ENTRY(entry_INT80_32)
 	STACKLEAK_ERASE
 
 restore_all:
-	TRACE_IRQS_IRET
 	SWITCH_TO_ENTRY_STACK
-.Lrestore_all_notrace:
 	CHECK_AND_APPLY_ESPFIX
-.Lrestore_nocheck:
 	/* Switch back to user CR3 */
 	SWITCH_TO_USER_CR3 scratch_reg=%eax
 
arch/x86/entry/entry_64.S +0 -6
@@ -167,15 +167,11 @@ GLOBAL(entry_SYSCALL_64_after_hwframe)
 
 	PUSH_AND_CLEAR_REGS rax=$-ENOSYS
 
-	TRACE_IRQS_OFF
-
 	/* IRQs are off. */
 	movq	%rax, %rdi
 	movq	%rsp, %rsi
 	call	do_syscall_64		/* returns with IRQs disabled */
 
-	TRACE_IRQS_IRETQ		/* we're about to change IF */
-
 	/*
 	 * Try to use SYSRET instead of IRET if we're returning to
 	 * a completely clean 64-bit userspace context.  If we're not,
@@ -342,7 +338,6 @@ ENTRY(ret_from_fork)
 	UNWIND_HINT_REGS
 	movq	%rsp, %rdi
 	call	syscall_return_slowpath	/* returns with IRQs disabled */
-	TRACE_IRQS_ON			/* user mode is traced as IRQS on */
 	jmp	swapgs_restore_regs_and_return_to_usermode
 
 1:
@@ -606,7 +601,6 @@ END(common_spurious)
 GLOBAL(retint_user)
 	mov	%rsp,%rdi
 	call	prepare_exit_to_usermode
-	TRACE_IRQS_IRETQ
 
 GLOBAL(swapgs_restore_regs_and_return_to_usermode)
 #ifdef CONFIG_DEBUG_ENTRY
arch/x86/entry/entry_64_compat.S +4 -26
@@ -129,17 +129,11 @@ ENTRY(entry_SYSENTER_compat)
 	jnz	.Lsysenter_fix_flags
 .Lsysenter_flags_fixed:
 
-	/*
-	 * User mode is traced as though IRQs are on, and SYSENTER
-	 * turned them off.
-	 */
-	TRACE_IRQS_OFF
-
 	movq	%rsp, %rdi
 	call	do_fast_syscall_32
 	/* XEN PV guests always use IRET path */
-	ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
-		    "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
+	ALTERNATIVE "testl %eax, %eax; jz swapgs_restore_regs_and_return_to_usermode", \
+		    "jmp swapgs_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
 	jmp	sysret32_from_system_call
 
 .Lsysenter_fix_flags:
@@ -247,17 +241,11 @@ GLOBAL(entry_SYSCALL_compat_after_hwfram
 	pushq   $0			/* pt_regs->r15 = 0 */
 	xorl	%r15d, %r15d		/* nospec   r15 */
 
-	/*
-	 * User mode is traced as though IRQs are on, and SYSENTER
-	 * turned them off.
-	 */
-	TRACE_IRQS_OFF
-
 	movq	%rsp, %rdi
 	call	do_fast_syscall_32
 	/* XEN PV guests always use IRET path */
-	ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
-		    "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
+	ALTERNATIVE "testl %eax, %eax; jz swapgs_restore_regs_and_return_to_usermode", \
+		    "jmp swapgs_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
 
 	/* Opportunistic SYSRET */
 sysret32_from_system_call:
@@ -266,7 +254,6 @@ GLOBAL(entry_SYSCALL_compat_after_hwfram
 	 * stack. So let's erase the thread stack right now.
 	 */
 	STACKLEAK_ERASE
-	TRACE_IRQS_ON			/* User mode traces as IRQs on. */
 	movq	RBX(%rsp), %rbx		/* pt_regs->rbx */
 	movq	RBP(%rsp), %rbp		/* pt_regs->rbp */
 	movq	EFLAGS(%rsp), %r11	/* pt_regs->flags (in r11) */
@@ -403,17 +390,8 @@ ENTRY(entry_INT80_compat)
 	xorl	%r15d, %r15d		/* nospec   r15 */
 	cld
 
-	/*
-	 * User mode is traced as though IRQs are on, and the interrupt
-	 * gate turned them off.
-	 */
-	TRACE_IRQS_OFF
-
 	movq	%rsp, %rdi
 	call	do_int80_syscall_32
-.Lsyscall_32_done:
 
-	/* Go back to user mode. */
-	TRACE_IRQS_ON
 	jmp	swapgs_restore_regs_and_return_to_usermode
 END(entry_INT80_compat)
#47 lore raw

PATCH v2 9/17 x86/entry: Remove _TIF_NOHZ from _TIF_WORK_SYSCALL_ENTRY

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]>

Evaluating _TIF_NOHZ to decide whether to use the slow syscall entry path is not only pointless, it's actually counterproductive:

1) Context tracking code is invoked unconditionally before that flag is evaluated.

2) If the flag is set the slow path is invoked for nothing due to #1

Remove it.

---
 arch/x86/include/asm/thread_info.h |    8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
arch/x86/include/asm/thread_info.h +2 -6
@@ -133,14 +133,10 @@ struct thread_info {
 #define _TIF_X32		(1 << TIF_X32)
 #define _TIF_FSCHECK		(1 << TIF_FSCHECK)
 
-/*
- * work to do in syscall_trace_enter().  Also includes TIF_NOHZ for
- * enter_from_user_mode()
- */
+/* Work to do before invoking the actual syscall. */
 #define _TIF_WORK_SYSCALL_ENTRY	\
 	(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU | _TIF_SYSCALL_AUDIT |	\
-	 _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT |	\
-	 _TIF_NOHZ)
+	 _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT)
 
 /* flags to check in __switch_to() */
 #define _TIF_WORK_CTXSW_BASE						\
#49 lore raw

PATCH v2 10/17 entry: Provide generic syscall entry functionality

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]>

On syscall entry certain work needs to be done conditionally like tracing, seccomp etc. This code is duplicated in all architectures.

Provide a generic version.

---
V2: Fix function documentation (Mike)
Add comment about return value (Andy)

---
 arch/Kconfig                 |    3 
 include/linux/entry-common.h |  132 +++++++++++++++++++++++++++++++++++++++++++
 kernel/Makefile              |    1 
 kernel/entry/Makefile        |    3 
 kernel/entry/common.c        |   33 ++++++++++
 5 files changed, 172 insertions(+)
arch/Kconfig +3 -0
@@ -27,6 +27,9 @@ config HAVE_IMA_KEXEC
 config HOTPLUG_SMT
 	bool
 
+config GENERIC_ENTRY
+       bool
+
 config OPROFILE
 	tristate "OProfile system profiling"
 	depends on PROFILING
include/linux/entry-common.h new file +132 -0

132 lines not on this page · raw message

kernel/Makefile +1 -0
@@ -43,6 +43,7 @@ obj-y += irq/
 obj-y += rcu/
 obj-y += livepatch/
 obj-y += dma/
+obj-y += entry/
 
 obj-$(CONFIG_CHECKPOINT_RESTORE) += kcmp.o
 obj-$(CONFIG_FREEZER) += freezer.o
kernel/entry/Makefile new file +3 -0
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_GENERIC_ENTRY) += common.o
kernel/entry/common.c new file +33 -0
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/context_tracking.h>
+#include <linux/entry-common.h>
+
+#define CREATE_TRACE_POINTS
+#include <trace/events/syscalls.h>
+
+long core_syscall_enter_from_usermode(struct pt_regs *regs, long syscall)
+{
+	unsigned long ti_work = READ_ONCE(current_thread_info()->flags);
+	unsigned long ret = 0;
+
+	if (ti_work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) {
+		ret = arch_syscall_enter_tracehook(regs);
+		if (ret || (ti_work & _TIF_SYSCALL_EMU))
+			return -1L;
+	}
+
+	/* Do seccomp after ptrace, to catch any tracer changes. */
+	if (ti_work & _TIF_SECCOMP) {
+		ret = arch_syscall_enter_seccomp(regs);
+		if (ret == -1L)
+			return ret;
+	}
+
+	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
+		trace_sys_enter(regs, syscall);
+
+	arch_syscall_enter_audit(regs);
+
+	return ret ? : syscall;
+}
#50 lore raw

PATCH v2 11/17 x86/entry: Use generic syscall entry function

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]>

Replace the syscall entry work handling with the generic version, Provide the necessary helper inlines to handle the real architecture specific parts, e.g. audit and seccomp invocations.

---
 arch/x86/Kconfig                    |    1 
 arch/x86/entry/common.c             |  108 +++---------------------------------
 arch/x86/include/asm/entry-common.h |   59 +++++++++++++++++++
 arch/x86/include/asm/thread_info.h  |    5 -
 4 files changed, 70 insertions(+), 103 deletions(-)
arch/x86/Kconfig +1 -0
@@ -111,6 +111,7 @@ config X86
 	select GENERIC_CPU_AUTOPROBE
 	select GENERIC_CPU_VULNERABILITIES
 	select GENERIC_EARLY_IOREMAP
+	select GENERIC_ENTRY
 	select GENERIC_FIND_FIRST_BIT
 	select GENERIC_IOMAP
 	select GENERIC_IRQ_EFFECTIVE_AFF_MASK	if SMP
arch/x86/entry/common.c +10 -98

154 lines not on this page · raw message

arch/x86/include/asm/entry-common.h new file +59 -0

59 lines not on this page · raw message

arch/x86/include/asm/thread_info.h +0 -5
@@ -133,11 +133,6 @@ struct thread_info {
 #define _TIF_X32		(1 << TIF_X32)
 #define _TIF_FSCHECK		(1 << TIF_FSCHECK)
 
-/* Work to do before invoking the actual syscall. */
-#define _TIF_WORK_SYSCALL_ENTRY	\
-	(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU | _TIF_SYSCALL_AUDIT |	\
-	 _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT)
-
 /* flags to check in __switch_to() */
 #define _TIF_WORK_CTXSW_BASE						\
 	(_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP|		\
#51 lore raw

PATCH v2 12/17 entry: Provide generic syscall exit function

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]>

Like syscall entry all architectures have similar and pointlessly different code to handle pending work before returning from a syscall to user space.

Provide a generic version.

---
 include/linux/entry-common.h |   31 ++++++++++++++++++++++++
 kernel/entry/common.c        |   55 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)
include/linux/entry-common.h +31 -0
@@ -46,6 +46,17 @@
 	 _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_EMU |			\
 	 ARCH_SYSCALL_ENTER_WORK)
 
+/*
+ * TIF flags handled in syscall_exit_to_usermode()
+ */
+#ifndef ARCH_SYSCALL_EXIT_WORK
+# define ARCH_SYSCALL_EXIT_WORK		(0)
+#endif
+
+#define SYSCALL_EXIT_WORK						\
+	(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT |			\
+	 _TIF_SYSCALL_TRACEPOINT | ARCH_SYSCALL_EXIT_WORK)
+
 /**
  * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry()
  * @regs:	Pointer to currents pt_regs
@@ -129,4 +140,24 @@ static inline long syscall_enter_from_us
 	return syscall;
 }
 
+/**
+ * arch_syscall_exit_tracehook - Wrapper around tracehook_report_syscall_exit()
+ *
+ * Defaults to tracehook_report_syscall_exit(). Can be replaced by
+ * architecture specific code.
+ *
+ * Invoked from syscall_exit_to_usermode()
+ */
+static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step);
+
+#ifndef arch_syscall_exit_tracehook
+static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step)
+{
+	tracehook_report_syscall_exit(regs, step);
+}
+#endif
+
+/* Common syscall exit function */
+void syscall_exit_to_usermode(struct pt_regs *regs, long syscall, long retval);
+
 #endif
kernel/entry/common.c +55 -0

58 lines not on this page · raw message

#52 lore raw

PATCH v2 13/17 x86/entry: Use generic syscall exit functionality

In reply to Thomas Gleixner

Replace the x86 variant with the generic version.

---
 arch/x86/entry/common.c             |   44 ------------------------------------
 arch/x86/include/asm/entry-common.h |    2 +
 2 files changed, 3 insertions(+), 43 deletions(-)
arch/x86/entry/common.c +1 -43
@@ -142,55 +142,13 @@ static void exit_to_usermode_loop(struct
 	trace_hardirqs_on();
 }
 
-#define SYSCALL_EXIT_WORK_FLAGS				\
-	(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT |	\
-	 _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)
-
-static void syscall_slow_exit_work(struct pt_regs *regs, u32 cached_flags)
-{
-	bool step;
-
-	audit_syscall_exit(regs);
-
-	if (cached_flags & _TIF_SYSCALL_TRACEPOINT)
-		trace_sys_exit(regs, regs->ax);
-
-	/*
-	 * If TIF_SYSCALL_EMU is set, we only get here because of
-	 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
-	 * We already reported this syscall instruction in
-	 * syscall_trace_enter().
-	 */
-	step = unlikely(
-		(cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU))
-		== _TIF_SINGLESTEP);
-	if (step || cached_flags & _TIF_SYSCALL_TRACE)
-		tracehook_report_syscall_exit(regs, step);
-}
-
 /*
  * Called with IRQs on and fully valid regs.  Returns with IRQs off in a
  * state such that we can immediately switch to user mode.
  */
 __visible inline void syscall_return_slowpath(struct pt_regs *regs)
 {
-	struct thread_info *ti = current_thread_info();
-	u32 cached_flags = READ_ONCE(ti->flags);
-
-	CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
-
-	if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
-	    WARN(irqs_disabled(), "syscall %ld left IRQs disabled", regs->orig_ax))
-		local_irq_enable();
-
-	rseq_syscall(regs);
-
-	/*
-	 * First do one-time work.  If these work items are enabled, we
-	 * want to run them exactly once per syscall exit with IRQs on.
-	 */
-	if (unlikely(cached_flags & SYSCALL_EXIT_WORK_FLAGS))
-		syscall_slow_exit_work(regs, cached_flags);
+	syscall_exit_to_usermode(regs, regs->orig_ax, regs->ax);
 
 	local_irq_disable();
 	prepare_exit_to_usermode(regs);
arch/x86/include/asm/entry-common.h +2 -0
@@ -5,6 +5,8 @@
 #include <linux/seccomp.h>
 #include <linux/audit.h>
 
+#define ARCH_SYSCALL_EXIT_WORK		(_TIF_SINGLESTEP)
+
 static inline long arch_syscall_enter_seccomp(struct pt_regs *regs)
 {
 #ifdef CONFIG_SECCOMP
#53 lore raw

PATCH v2 14/17 entry: Provide generic exit to usermode functionality

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]>

Provide a generic facility to handle the exit to usermode work. That's aimed to replace the pointlessly different copies in each architecture.

---
V2: Move lockdep and address limit check right to the end of the return
sequence. (PeterZ)

---
 include/linux/entry-common.h |  105 +++++++++++++++++++++++++++++++++++++++++++
 kernel/entry/common.c        |   82 +++++++++++++++++++++++++++++++++
 2 files changed, 187 insertions(+)
include/linux/entry-common.h +105 -0
@@ -34,6 +34,30 @@
 # define _TIF_AUDIT			(0)
 #endif
 
+#ifndef _TIF_UPROBE
+# define _TIF_UPROBE			(0)
+#endif
+
+#ifndef _TIF_PATCH_PENDING
+# define _TIF_PATCH_PENDING		(0)
+#endif
+
+#ifndef _TIF_NOTIFY_RESUME
+# define _TIF_NOTIFY_RESUME		(0)
+#endif
+
+/*
+ * TIF flags handled in exit_to_usermode()
+ */
+#ifndef ARCH_EXIT_TO_USERMODE_WORK
+# define ARCH_EXIT_TO_USERMODE_WORK	(0)
+#endif
+
+#define EXIT_TO_USERMODE_WORK						\
+	(_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE |		\
+	 _TIF_NEED_RESCHED | _TIF_PATCH_PENDING |			\
+	 ARCH_EXIT_TO_USERMODE_WORK)
+
 /*
  * TIF flags handled in syscall_enter_from_usermode()
  */
@@ -58,6 +82,87 @@
 	 _TIF_SYSCALL_TRACEPOINT | ARCH_SYSCALL_EXIT_WORK)
 
 /**
+ * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
+ * @ti_work:	Cached TIF flags gathered with interrupts disabled
+ *
+ * Defaults to local_irq_enable(). Can be supplied by architecture specific
+ * code.
+ */
+static inline void local_irq_enable_exit_to_user(unsigned long ti_work);
+
+#ifndef local_irq_enable_exit_to_user
+static inline void local_irq_enable_exit_to_user(unsigned long ti_work)
+{
+	local_irq_enable();
+}
+#endif
+
+/**
+ * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable()
+ *
+ * Defaults to local_irq_disable(). Can be supplied by architecture specific
+ * code.
+ */
+static inline void local_irq_disable_exit_to_user(void);
+
+#ifndef local_irq_disable_exit_to_user
+static inline void local_irq_disable_exit_to_user(void)
+{
+	local_irq_disable();
+}
+#endif
+
+/**
+ * arch_exit_to_usermode_work - Architecture specific TIF work for
+ *				exit to user mode.
+ * @regs:	Pointer to currents pt_regs
+ * @ti_work:	Cached TIF flags gathered with interrupts disabled
+ *
+ * Invoked from exit_to_usermode() with interrupt disabled
+ *
+ * Defaults to NOOP. Can be supplied by architecture specific code.
+ */
+static inline void arch_exit_to_usermode_work(struct pt_regs *regs,
+					      unsigned long ti_work);
+
+#ifndef arch_exit_to_usermode_work
+static inline void arch_exit_to_usermode_work(struct pt_regs *regs,
+					      unsigned long ti_work)
+{
+}
+#endif
+
+/**
+ * arch_exit_to_usermode - Architecture specific preparation for
+ *			   exit to user mode.
+ * @regs:	Pointer to currents pt_regs
+ * @ti_work:	Cached TIF flags gathered with interrupts disabled
+ *
+ * Invoked from exit_to_usermode() with interrupt disabled as the last
+ * function before return.
+ */
+static inline void arch_exit_to_usermode(struct pt_regs *regs,
+					 unsigned long ti_work);
+
+#ifndef arch_exit_to_usermode
+static inline void arch_exit_to_usermode(struct pt_regs *regs,
+					 unsigned long ti_work)
+{
+}
+#endif
+
+/* Common exit to usermode function to handle TIF work */
+asmlinkage __visible void exit_to_usermode(struct pt_regs *regs);
+
+/**
+ * arch_do_signal -  Architecture specific signal delivery function
+ * @regs:	Pointer to currents pt_regs
+ *
+ * Invoked from exit_to_usermode()
+ */
+void arch_do_signal(struct pt_regs *regs);
+
+/**
  * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry()
  * @regs:	Pointer to currents pt_regs
  *
kernel/entry/common.c +82 -0

96 lines not on this page · raw message

#56 lore raw

PATCH v2 15/17 x86/entry: Use generic exit to usermode

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]>

Replace the x86 specific exit to usermode code with the generic implementation.

---
 arch/x86/entry/common.c             |  110 ------------------------------------
 arch/x86/entry/entry_32.S           |    2 
 arch/x86/entry/entry_64.S           |    2 
 arch/x86/include/asm/entry-common.h |   47 ++++++++++++++-
 arch/x86/include/asm/signal.h       |    1 
 arch/x86/kernel/signal.c            |    2 
 6 files changed, 51 insertions(+), 113 deletions(-)
arch/x86/entry/common.c +3 -107
@@ -15,15 +15,9 @@
 #include <linux/smp.h>
 #include <linux/errno.h>
 #include <linux/ptrace.h>
-#include <linux/tracehook.h>
-#include <linux/audit.h>
 #include <linux/signal.h>
 #include <linux/export.h>
-#include <linux/context_tracking.h>
-#include <linux/user-return-notifier.h>
 #include <linux/nospec.h>
-#include <linux/uprobes.h>
-#include <linux/livepatch.h>
 #include <linux/syscalls.h>
 #include <linux/uaccess.h>
 
@@ -47,101 +41,6 @@
 static inline void enter_from_user_mode(void) {}
 #endif
 
-#define EXIT_TO_USERMODE_LOOP_FLAGS				\
-	(_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE |	\
-	 _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY | _TIF_PATCH_PENDING)
-
-static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
-{
-	/*
-	 * In order to return to user mode, we need to have IRQs off with
-	 * none of EXIT_TO_USERMODE_LOOP_FLAGS set.  Several of these flags
-	 * can be set at any time on preemptible kernels if we have IRQs on,
-	 * so we need to loop.  Disabling preemption wouldn't help: doing the
-	 * work to clear some of the flags can sleep.
-	 */
-	while (true) {
-		/* We have work to do. */
-		local_irq_enable();
-
-		if (cached_flags & _TIF_NEED_RESCHED)
-			schedule();
-
-		if (cached_flags & _TIF_UPROBE)
-			uprobe_notify_resume(regs);
-
-		if (cached_flags & _TIF_PATCH_PENDING)
-			klp_update_patch_state(current);
-
-		/* deal with pending signal delivery */
-		if (cached_flags & _TIF_SIGPENDING)
-			do_signal(regs);
-
-		if (cached_flags & _TIF_NOTIFY_RESUME) {
-			clear_thread_flag(TIF_NOTIFY_RESUME);
-			tracehook_notify_resume(regs);
-			rseq_handle_notify_resume(NULL, regs);
-		}
-
-		if (cached_flags & _TIF_USER_RETURN_NOTIFY)
-			fire_user_return_notifiers();
-
-		/* Disable IRQs and retry */
-		local_irq_disable();
-
-		cached_flags = READ_ONCE(current_thread_info()->flags);
-
-		if (!(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
-			break;
-	}
-}
-
-/* Called with IRQs disabled. */
-__visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
-{
-	struct thread_info *ti = current_thread_info();
-	u32 cached_flags;
-
-	addr_limit_user_check();
-
-	lockdep_assert_irqs_disabled();
-	lockdep_sys_exit();
-
-	cached_flags = READ_ONCE(ti->flags);
-
-	if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
-		exit_to_usermode_loop(regs, cached_flags);
-
-	/* Reload ti->flags; we may have rescheduled above. */
-	cached_flags = READ_ONCE(ti->flags);
-
-	fpregs_assert_state_consistent();
-	if (unlikely(cached_flags & _TIF_NEED_FPU_LOAD))
-		switch_fpu_return();
-
-#ifdef CONFIG_COMPAT
-	/*
-	 * Compat syscalls set TS_COMPAT.  Make sure we clear it before
-	 * returning to user mode.  We need to clear it *after* signal
-	 * handling, because syscall restart has a fixup for compat
-	 * syscalls.  The fixup is exercised by the ptrace_syscall_32
-	 * selftest.
-	 *
-	 * We also need to clear TS_REGS_POKED_I386: the 32-bit tracer
-	 * special case only applies after poking regs and before the
-	 * very next return to user mode.
-	 */
-	ti->status &= ~(TS_COMPAT|TS_I386_REGS_POKED);
-#endif
-
-	user_enter_irqoff();
-
-	mds_user_clear_cpu_buffers();
-
-	/* The return to usermode reenables interrupts. Tell the tracer */
-	trace_hardirqs_on();
-}
-
 /*
  * Called with IRQs on and fully valid regs.  Returns with IRQs off in a
  * state such that we can immediately switch to user mode.
@@ -149,9 +48,6 @@ static void exit_to_usermode_loop(struct
 __visible inline void syscall_return_slowpath(struct pt_regs *regs)
 {
 	syscall_exit_to_usermode(regs, regs->orig_ax, regs->ax);
-
-	local_irq_disable();
-	prepare_exit_to_usermode(regs);
 }
 
 #ifdef CONFIG_X86_64
@@ -179,7 +75,7 @@ static void exit_to_usermode_loop(struct
 #endif
 	}
 
-	syscall_return_slowpath(regs);
+	syscall_exit_to_usermode(regs, regs->orig_ax, regs->ax);
 }
 #endif
 
@@ -223,7 +119,7 @@ static __always_inline void do_syscall_3
 #endif /* CONFIG_IA32_EMULATION */
 	}
 
-	syscall_return_slowpath(regs);
+	syscall_exit_to_usermode(regs, regs->orig_ax, regs->ax);
 }
 
 /* Handles int $0x80 */
@@ -278,7 +174,7 @@ static __always_inline void do_syscall_3
 		/* User code screwed up. */
 		local_irq_disable();
 		regs->ax = -EFAULT;
-		prepare_exit_to_usermode(regs);
+		exit_to_usermode(regs);
 		return 0;	/* Keep it simple: use IRET. */
 	}
 
arch/x86/entry/entry_32.S +1 -1
@@ -819,7 +819,7 @@ END(ret_from_fork)
 	jb	restore_all_kernel		# not returning to v8086 or userspace
 
 	movl	%esp, %eax
-	call	prepare_exit_to_usermode
+	call	exit_to_usermode
 	jmp	restore_all
 END(ret_from_exception)
 
arch/x86/entry/entry_64.S +1 -1
@@ -600,7 +600,7 @@ END(common_spurious)
 	/* Interrupt came from user space */
 GLOBAL(retint_user)
 	mov	%rsp,%rdi
-	call	prepare_exit_to_usermode
+	call	exit_to_usermode
 
 GLOBAL(swapgs_restore_regs_and_return_to_usermode)
 #ifdef CONFIG_DEBUG_ENTRY
arch/x86/include/asm/entry-common.h +45 -2

56 lines not on this page · raw message

arch/x86/include/asm/signal.h +0 -1
@@ -35,7 +35,6 @@ typedef sigset_t compat_sigset_t;
 #endif /* __ASSEMBLY__ */
 #include <uapi/asm/signal.h>
 #ifndef __ASSEMBLY__
-extern void do_signal(struct pt_regs *regs);
 
 #define __ARCH_HAS_SA_RESTORER
 
arch/x86/kernel/signal.c +1 -1
@@ -808,7 +808,7 @@ static inline unsigned long get_nr_resta
  * want to handle. Thus you cannot kill init even with a SIGKILL even by
  * mistake.
  */
-void do_signal(struct pt_regs *regs)
+void arch_do_signal(struct pt_regs *regs)
 {
 	struct ksignal ksig;
 
#57 lore raw

PATCH v2 16/17 kvm/workpending: Provide infrastructure for work before entering a guest

In reply to Thomas Gleixner

Entering a guest is similar to exiting to user space. Pending work like handling signals, rescheduling, task work etc. needs to be handled before that.

Provide generic infrastructure to avoid duplication of the same handling code all over the place.

The kvm_exit code is split up into a KVM specific part and a generic builtin core part to avoid multiple exports for the actual work functions. The exit to guest mode handling is slightly different from the exit to usermode handling, e.g. vs. rseq, so a separate function is used.

---
V2: Moved KVM specific functions to kvm (Paolo)
Added lockdep assert (Andy)
Dropped live patching from enter guest mode work (Miroslav)

---
 include/linux/entry-common.h |   12 ++++++++
 include/linux/kvm_host.h     |   64 +++++++++++++++++++++++++++++++++++++++++++
 kernel/entry/common.c        |   14 +++++++++
 virt/kvm/Kconfig             |    3 ++
 4 files changed, 93 insertions(+)
include/linux/entry-common.h +12 -0
@@ -265,4 +265,16 @@ static inline void arch_syscall_exit_tra
 /* Common syscall exit function */
 void syscall_exit_to_usermode(struct pt_regs *regs, long syscall, long retval);
 
+/* KVM exit to guest mode */
+
+void core_exit_to_guestmode_work(unsigned long ti_work);
+
+#ifndef ARCH_EXIT_TO_GUESTMODE_WORK
+# define ARCH_EXIT_TO_GUESTMODE_WORK	(0)
+#endif
+
+#define EXIT_TO_GUESTMODE_WORK						\
+	(_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_RESUME |	\
+	 ARCH_EXIT_TO_GUESTMODE_WORK)
+
 #endif
include/linux/kvm_host.h +64 -0

74 lines not on this page · raw message

kernel/entry/common.c +14 -0
@@ -8,6 +8,20 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>
 
+#ifdef CONFIG_KVM_EXIT_TO_GUEST_WORK
+void core_exit_to_guestmode_work(unsigned long ti_work)
+{
+	if (ti_work & _TIF_NEED_RESCHED)
+		schedule();
+
+	if (ti_work & _TIF_NOTIFY_RESUME) {
+		clear_thread_flag(TIF_NOTIFY_RESUME);
+		tracehook_notify_resume(NULL);
+	}
+}
+EXPORT_SYMBOL_GPL(core_exit_to_guestmode_work);
+#endif
+
 static unsigned long core_exit_to_usermode_work(struct pt_regs *regs,
 						unsigned long ti_work)
 {
virt/kvm/Kconfig +3 -0
@@ -60,3 +60,6 @@ config HAVE_KVM_VCPU_RUN_PID_CHANGE
 
 config HAVE_KVM_NO_POLL
        bool
+
+config KVM_EXIT_TO_GUEST_WORK
+       bool
#59 lore raw

PATCH v2 17/17 x86/kvm: Use generic exit to guest work function

In reply to Thomas Gleixner

Use the generic infrastructure to check for and handle pending work before entering into guest mode.

---
 arch/x86/kvm/Kconfig |    1 +
 arch/x86/kvm/x86.c   |   17 +++++------------
 2 files changed, 6 insertions(+), 12 deletions(-)
arch/x86/kvm/Kconfig +1 -0
@@ -42,6 +42,7 @@ config KVM
 	select HAVE_KVM_MSI
 	select HAVE_KVM_CPU_RELAX_INTERCEPT
 	select HAVE_KVM_NO_POLL
+	select KVM_EXIT_TO_GUEST_WORK
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
 	select KVM_VFIO
 	select SRCU
arch/x86/kvm/x86.c +5 -12
@@ -52,6 +52,7 @@
 #include <linux/irqbypass.h>
 #include <linux/sched/stat.h>
 #include <linux/sched/isolation.h>
+#include <linux/entry-common.h>
 #include <linux/mem_encrypt.h>
 
 #include <trace/events/kvm.h>
@@ -8115,8 +8116,8 @@ static int vcpu_enter_guest(struct kvm_v
 	if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
 		kvm_x86_ops->sync_pir_to_irr(vcpu);
 
-	if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
-	    || need_resched() || signal_pending(current)) {
+	if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
+	    exit_to_guestmode_work_pending()) {
 		vcpu->mode = OUTSIDE_GUEST_MODE;
 		smp_wmb();
 		local_irq_enable();
@@ -8309,17 +8310,9 @@ static int vcpu_run(struct kvm_vcpu *vcp
 
 		kvm_check_async_pf_completion(vcpu);
 
-		if (signal_pending(current)) {
-			r = -EINTR;
-			vcpu->run->exit_reason = KVM_EXIT_INTR;
-			++vcpu->stat.signal_exits;
+		r = exit_to_guestmode(kvm, vcpu);
+		if (r)
 			break;
-		}
-		if (need_resched()) {
-			srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
-			cond_resched();
-			vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
-		}
 	}
 
 	srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Reviewed-by on v2 1/17
#2 lore raw

Re: [patch V2 01/17] x86/entry/32: Remove unused resume_userspace label

In reply to Thomas Gleixner
Quoted 7 lines · Thomas Gleixner

The C reimplementation of SYSENTER left that unused ENTRY() label around. Remove it.

Fixes 5f310f739b4c ("x86/entry/32: Re-implement SYSENTER using the new C path")

---

Reviewed-by on v2 5/17
#21 lore raw

Re: [patch V2 05/17] x86/traps: Make interrupt enable/disable symmetric in C code

In reply to Thomas Gleixner
Quoted 10 lines · Thomas Gleixner

Traps enable interrupts conditionally but rely on the ASM return code to disable them again. That results in redundant interrupt disable and trace calls.

Make the trap handlers disable interrupts before returning to avoid that, which allows simplification of the ASM entry code.

---

Reviewed-by on v2 6/17
#26 lore raw

Re: [patch V2 06/17] x86/entry/32: Remove redundant interrupt disable

In reply to Thomas Gleixner
Quoted 10 lines · Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls and the misnomed preempt_stop macro. As a consequence ret_from_exception and ret_from_intr collapse.

Add a debug check to verify that interrupts are disabled depending on CONFIG_DEBUG_ENTRY.

---

One nit below.

Quoted 15 lines
 arch/x86/entry/entry_32.S |   21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
arch/x86/entry/entry_32.S +1 -1
@@ -1207,7 +1198,7 @@ ENDPROC(common_spurious)
 	TRACE_IRQS_OFF
 	movl	%esp, %eax
 	call	do_IRQ
-	jmp	ret_from_intr
+	jmp	ret_from_exception
 ENDPROC(common_interrupt)
 
 #define BUILD_INTERRUPT3(name, nr, fn)			\

arch/x86/entry/entry_32.S ENTRY(name) \ · line 1213

Quoted 5 lines
arch/x86/entry/entry_32.S +1 -1
@@ -1219,7 +1210,7 @@ ENTRY(name)						\
 	TRACE_IRQS_OFF					\
 	movl	%esp, %eax;				\
 	call	fn;					\
-	jmp	ret_from_intr;				\
+	jmp	ret_from_exception;				\

This backslash is now unaligned.

Quoted 14 lines
arch/x86/entry/entry_32.S +1 -1
 ENDPROC(name)
 
 #define BUILD_INTERRUPT(name, nr)		\
@@ -1366,7 +1357,7 @@ ENTRY(xen_do_upcall)
 #ifndef CONFIG_PREEMPTION
 	call	xen_maybe_preempt_hcall
 #endif
-	jmp	ret_from_intr
+	jmp	ret_from_exception
 ENDPROC(xen_hypervisor_callback)
 
 /*
Reviewed-by on v2 7/17
#29 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

In reply to Thomas Gleixner
Quoted 8 lines · Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls.

Add debug checks where appropriate.

---

maintainer Acked-by on the v2 series
#61 lore raw
In reply to Thomas Gleixner
Quoted 16 lines · Thomas Gleixner
 /Makefile                             |    3 
 arch/Kconfig                          |    3 
 arch/x86/Kconfig                      |    1 
 arch/x86/entry/calling.h              |   12 +
 arch/x86/entry/common.c               |  264 ++------------------------------
 arch/x86/entry/entry_32.S             |   41 ----
 arch/x86/entry/entry_64.S             |   32 ---
 arch/x86/entry/entry_64_compat.S      |   30 ---
 arch/x86/include/asm/irqflags.h       |    8 
 arch/x86/include/asm/paravirt.h       |    9 -
 arch/x86/include/asm/signal.h         |    1 
 arch/x86/include/asm/thread_info.h    |    9 -
 arch/x86/kernel/signal.c              |    2 
 arch/x86/kernel/traps.c               |   33 ++--
 arch/x86/kvm/x86.c                    |   17 --
 arch/x86/mm/fault.c                   |    7 
Quoted 8 lines
 b/arch/x86/include/asm/entry-common.h |  104 ++++++++++++
 b/arch/x86/kvm/Kconfig                |    1 
 b/include/linux/entry-common.h        |  280 ++++++++++++++++++++++++++++++++++
 b/kernel/entry/common.c               |  184 ++++++++++++++++++++++
 include/linux/kvm_host.h              |   64 +++++++
 kernel/Makefile                       |    1 
 virt/kvm/Kconfig                      |    3 
 23 files changed, 735 insertions(+), 374 deletions(-)

This looks really nice; esp. the cleaned up interrupt state.

#60 lore raw

Re: [patch V2 17/17] x86/kvm: Use generic exit to guest work function

In reply to Thomas Gleixner
Quoted 30 lines · Thomas Gleixner

Use the generic infrastructure to check for and handle pending work before entering into guest mode.

---
 arch/x86/kvm/Kconfig |    1 +
 arch/x86/kvm/x86.c   |   17 +++++------------
 2 files changed, 6 insertions(+), 12 deletions(-)
arch/x86/kvm/Kconfig +1 -0
@@ -42,6 +42,7 @@ config KVM
 	select HAVE_KVM_MSI
 	select HAVE_KVM_CPU_RELAX_INTERCEPT
 	select HAVE_KVM_NO_POLL
+	select KVM_EXIT_TO_GUEST_WORK
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
 	select KVM_VFIO
 	select SRCU
arch/x86/kvm/x86.c +1 -0
@@ -52,6 +52,7 @@
 #include <linux/irqbypass.h>
 #include <linux/sched/stat.h>
 #include <linux/sched/isolation.h>
+#include <linux/entry-common.h>
 #include <linux/mem_encrypt.h>
 
 #include <trace/events/kvm.h>

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_v · line 8120

Quoted 7 lines
arch/x86/kvm/x86.c +2 -2
@@ -8115,8 +8116,8 @@ static int vcpu_enter_guest(struct kvm_v
 	if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
 		kvm_x86_ops->sync_pir_to_irr(vcpu);
 
-	if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
-	    || need_resched() || signal_pending(current)) {
+	if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
+	    exit_to_guestmode_work_pending()) {

The terms EXIT_TO_GUEST and exit_to_guestmode are very confusing, as they're inverted from the usual virt terminology of VM-Enter (enter guest) and VM-Exit (exit guest). The conflict is most obvious here, with the above "vcpu->mode == EXITING_GUEST_MODE", which is checking to see if the vCPU is being forced to exit *from* guest mode because was kicked by some other part of KVM.

Maybe XFER_TO_GUEST? I.e. avoid entry/exit entirely, so that neither the entry code or KVM ends up with a confusing name.

arch/x86/kvm/x86.c static int vcpu_run(struct kvm_vcpu *vcp · line 8313

Quoted 12 lines
arch/x86/kvm/x86.c +1 -4
 		vcpu->mode = OUTSIDE_GUEST_MODE;
 		smp_wmb();
 		local_irq_enable();
@@ -8309,17 +8310,9 @@ static int vcpu_run(struct kvm_vcpu *vcp
 
 		kvm_check_async_pf_completion(vcpu);
 
-		if (signal_pending(current)) {
-			r = -EINTR;
-			vcpu->run->exit_reason = KVM_EXIT_INTR;
-			++vcpu->stat.signal_exits;
+		r = exit_to_guestmode(kvm, vcpu);

Ditto here. If the run loop is stripped down to the core functionality, it effectively looks like:

	for (;;) {
		r = vcpu_enter_guest(vcpu);
		if (r <= 0)
			break;

		...

		r = exit_to_guestmode(kvm, vcpu);
		if (r)
			break;
	}

Appending _handle_work to the function would also be helpful so that it's somewhat clear the function isn't related to the core vcpu_enter_guest() functionality, e.g.:

	for (;;) {
		r = vcpu_enter_guest(vcpu);
		if (r <= 0)
			break;

		...

		r = xfer_to_guestmode_handle_work(kvm, vcpu);
		if (r)
			break;
	}
Quoted 13 lines
arch/x86/kvm/x86.c +1 -6
+		if (r)
 			break;
-		}
-		if (need_resched()) {
-			srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
-			cond_resched();
-			vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
-		}
 	}
 
 	srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
#58 lore raw

Re: [patch V2 16/17] kvm/workpending: Provide infrastructure for work before entering a guest

In reply to Thomas Gleixner
Quoted 16 lines · Thomas Gleixner

Entering a guest is similar to exiting to user space. Pending work like handling signals, rescheduling, task work etc. needs to be handled before that.

Provide generic infrastructure to avoid duplication of the same handling code all over the place.

The kvm_exit code is split up into a KVM specific part and a generic builtin core part to avoid multiple exports for the actual work functions. The exit to guest mode handling is slightly different from the exit to usermode handling, e.g. vs. rseq, so a separate function is used.

---

include/linux/kvm_host.h static inline int kvm_arch_vcpu_run_pid_ · line 1409

Quoted 3 lines
include/linux/kvm_host.h +3 -0
+/**
+ * exit_to_guestmode - Check and handle pending work which needs to be
+ *		       handled before returning to guest mode

Nit: I'd prefer "transferring" or "transitioning" over "returning". KVM could bail out of the very first run of a guest in order to handle work, in which case the kernel isn't technically returning to guest mode as it's never been there. The comment might trip up VMX folks that understand the difference between VMLAUNCH and VMRESUME, but not the purpose of this code.

Quoted 15 lines

+ * @kvm: Pointer to the guest instance
+ * @vcpu: Pointer to current's VCPU data
+ *
+ * Returns: 0 or an error code
+ */

include/linux/kvm_host.h +10 -0
+static inline int exit_to_guestmode(struct kvm *kvm, struct kvm_vcpu *vcpu)
+{
+	unsigned long ti_work = READ_ONCE(current_thread_info()->flags);
+	int r = 0;
+
+	if (unlikely(ti_work & EXIT_TO_GUESTMODE_WORK)) {
+		if (ti_work & _TIF_SIGPENDING) {
+			vcpu->run->exit_reason = KVM_EXIT_INTR;
+			vcpu->stat.signal_exits++;
+			return -EINTR;

include/linux/kvm_host.h static inline int kvm_arch_vcpu_run_pid_ · line 1434

Quoted 10 lines
include/linux/kvm_host.h +10 -0
+		}
+		core_exit_to_guestmode_work(ti_work);
+		r = arch_exit_to_guestmode_work(kvm, vcpu, ti_work);
+	}
+	return r;
+}
+
+/**
+ * _exit_to_guestmode_work_pending - Check if work is pending which needs to be
+ *				     handled before returning to guest mode

Same pedantic comment on "returning".

Quoted 15 lines

+ *
+ * Returns: True if work pending, False otherwise.
+ */

include/linux/kvm_host.h +11 -0
+static inline bool exit_to_guestmode_work_pending(void)
+{
+	unsigned long ti_work = READ_ONCE(current_thread_info()->flags);
+
+	lockdep_assert_irqs_disabled();
+
+	return !!(ti_work & EXIT_TO_GUESTMODE_WORK);
+
+}
+#endif /* CONFIG_KVM_EXIT_TO_GUEST_WORK */
+
 #endif
#62 lore raw
In reply to Thomas Gleixner
Quoted 3 lines · Thomas Gleixner

The series is also available from git:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git WIP/core.entry

Actually

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git WIP.core/entry

:-)

Signature · 1 line
Josh
maintainer
#40 lore raw

Re: [patch V2 08/17] x86/entry: Move syscall irq tracing to C code

In reply to Thomas Gleixner
Quoted 6 lines · Thomas Gleixner

Interrupt state tracing can be safely done in C code. The few stack operations in assembly do not need to be covered.

Remove the now pointless indirection via .Lsyscall_32_done and jump to swapgs_restore_regs_and_return_to_usermode directly.

This doesn't look right.

Review static void syscall_slow_exit_work(struc · line 287

Quoted 8 lines

#define SYSCALL_EXIT_WORK_FLAGS \

(quoted diff) +3 -0
@@ -279,6 +282,9 @@ static void syscall_slow_exit_work(struc
 {
        struct thread_info *ti;
 
+       /* User to kernel transition disabled interrupts. */
+       trace_hardirqs_off();
+

So you just traced IRQs off, but...

Review static void syscall_slow_exit_work(struc · line 289

Quoted 2 lines
(quoted diff) +0 -0
        enter_from_user_mode();
        local_irq_enable();

Now they're on and traced on again?

I also don't see how your patch handles the fastpath case.

How about the attached patch instead?

commit e154af0d7ff2d605f155f5aca059e3e835e426d4

Author: Andy Lutomirski <[email protected]>
Date:   Fri Aug 2 10:30:44 2019 -0700

x86/entry: Move exit-to-usermode irqflag tracing to prepare_exit_to_usermode()

prepare_exit_to_usermode() can easily handle irqflag tracing. Move the logic there and remove it from the entry asm.

arch/x86/entry/common.c +7 -0
index cc49380ef8ab..f4ce0cf2fb74 100644
@@ -217,6 +217,13 @@ __visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
 
 	user_enter_irqoff();
 
+	/*
+	 * The actual return to usermode will almost certainly turn IRQs on.
+	 * Trace it here to simplify the asm code.
+	 */
+	if (likely(regs->flags & X86_EFLAGS_IF))
+		trace_hardirqs_on();
+
 	mds_user_clear_cpu_buffers();
 }
 
arch/x86/entry/entry_32.S +0 -1
index f83ca5aa8b77..c703c29bebb1 100644
@@ -1062,7 +1062,6 @@ ENTRY(entry_INT80_32)
 	STACKLEAK_ERASE
 
 restore_all:
-	TRACE_IRQS_IRET
 	SWITCH_TO_ENTRY_STACK
 .Lrestore_all_notrace:
 	CHECK_AND_APPLY_ESPFIX
arch/x86/entry/entry_64.S +0 -3
index 3abae80d4902..056419a0e76f 100644
@@ -172,8 +172,6 @@ GLOBAL(entry_SYSCALL_64_after_hwframe)
 	movq	%rsp, %rsi
 	call	do_syscall_64		/* returns with IRQs disabled */
 
-	TRACE_IRQS_IRETQ		/* we're about to change IF */
-
 	/*
 	 * Try to use SYSRET instead of IRET if we're returning to
 	 * a completely clean 64-bit userspace context.  If we're not,
@@ -617,7 +615,6 @@ ret_from_intr:
 .Lretint_user:
 	mov	%rsp,%rdi
 	call	prepare_exit_to_usermode
-	TRACE_IRQS_IRETQ
 
 GLOBAL(swapgs_restore_regs_and_return_to_usermode)
 #ifdef CONFIG_DEBUG_ENTRY
arch/x86/entry/entry_64_compat.S +0 -2
index 435df637f392..3502f38bde01 100644
@@ -254,7 +254,6 @@ sysret32_from_system_call:
 	 * stack. So let's erase the thread stack right now.
 	 */
 	STACKLEAK_ERASE
-	TRACE_IRQS_ON			/* User mode traces as IRQs on. */
 	movq	RBX(%rsp), %rbx		/* pt_regs->rbx */
 	movq	RBP(%rsp), %rbp		/* pt_regs->rbp */
 	movq	EFLAGS(%rsp), %r11	/* pt_regs->flags (in r11) */
@@ -396,6 +395,5 @@ ENTRY(entry_INT80_compat)
 .Lsyscall_32_done:
 
 	/* Go back to user mode. */
-	TRACE_IRQS_ON
 	jmp	swapgs_restore_regs_and_return_to_usermode
 END(entry_INT80_compat)
#11 lore raw

Re: [patch V2 03/17] x86/traps: Remove pointless irq enable from do_spurious_interrupt_bug()

In reply to Thomas Gleixner
Quoted 11 lines · Thomas Gleixner

That function returns immediately after conditionally reenabling interrupts which is more than pointless and requires the ASM code to disable interrupts again.

---
 arch/x86/kernel/traps.c |    1 -
 1 file changed, 1 deletion(-)

arch/x86/kernel/traps.c do_simd_coprocessor_error(struct pt_regs · line 874

Quoted 5 lines
arch/x86/kernel/traps.c +0 -1
@@ -871,7 +871,6 @@ do_simd_coprocessor_error(struct pt_regs
 dotraplinkage void
 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
 {
-	cond_local_irq_enable(regs);
 }

I think we can just remove this handler altogether. The Intel and AMD manuals say vector 15 (X86_TRAP_SPURIOUS) is reserved.

Signature · 1 line
Josh
maintainer
#54 lore raw

Re: [patch V2 14/17] entry: Provide generic exit to usermode functionality

In reply to Thomas Gleixner
Quoted 7 lines · Thomas Gleixner

From: Thomas Gleixner <[email protected]>

Provide a generic facility to handle the exit to usermode work. That's aimed to replace the pointlessly different copies in each architecture.

Quoted 6 lines

/**
+ * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
+ * @ti_work: Cached TIF flags gathered with interrupts disabled
+ *
+ * Defaults to local_irq_enable(). Can be supplied by architecture specific
+ * code.

What did you have in mind here?

maintainer
#41 lore raw

Re: [patch V2 08/17] x86/entry: Move syscall irq tracing to C code

In reply to Andy Lutomirski
Quoted 22 lines · Andy Lutomirski
Quoted 6 lines · Thomas Gleixner

Interrupt state tracing can be safely done in C code. The few stack operations in assembly do not need to be covered.

Remove the now pointless indirection via .Lsyscall_32_done and jump to swapgs_restore_regs_and_return_to_usermode directly.

This doesn't look right.

Review static void syscall_slow_exit_work(struc · line 287

Quoted 8 lines

#define SYSCALL_EXIT_WORK_FLAGS \

(quoted diff) +3 -0
@@ -279,6 +282,9 @@ static void syscall_slow_exit_work(struc
 {
        struct thread_info *ti;
 
+       /* User to kernel transition disabled interrupts. */
+       trace_hardirqs_off();
+

So you just traced IRQs off, but...

Quoted 8 lines

Review static void syscall_slow_exit_work(struc · line 289

Quoted 2 lines
(quoted diff) +0 -0
        enter_from_user_mode();
        local_irq_enable();

Now they're on and traced on again?

I also don't see how your patch handles the fastpath case.

How about the attached patch instead?

Ignore the attached patch. You have this in your do_exit_to_usermode() later in the series. But I'm still quite confused by this patch.

#22 lore raw

Re: [patch V2 05/17] x86/traps: Make interrupt enable/disable symmetric in C code

In reply to Thomas Gleixner
Quoted 5 lines · Thomas Gleixner
arch/x86/mm/fault.c +0 -0
@@ -1500,10 +1500,13 @@ static noinline void
 		return;
 

arch/x86/mm/fault.c static noinline void · line 1509

Quoted 10 lines
arch/x86/mm/fault.c +5 -2
 	/* Was the fault on kernel-controlled part of the address space? */
-	if (unlikely(fault_in_kernel_space(address)))
+	if (unlikely(fault_in_kernel_space(address))) {
 		do_kern_addr_fault(regs, hw_error_code, address);
-	else
+	} else {
 		do_user_addr_fault(regs, hw_error_code, address);
+		if (regs->flags & X86_EFLAGS_IF)
+			local_irq_disable();
+	}

The corresponding irq enable is in do_user_addr_fault(), why not do the disable there?

Signature · 1 line
Josh
#30 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

In reply to Thomas Gleixner
Quoted 5 lines · Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls.

Add debug checks where appropriate.

This seems a little scary. Does anybody other than Andy actually run with CONFIG_DEBUG_ENTRY? What happens if somebody accidentally leaves irqs enabled? How do we know you found all the leaks?

Signature · 1 line
Josh
#12 lore raw

Re: [patch V2 03/17] x86/traps: Remove pointless irq enable from do_spurious_interrupt_bug()

In reply to Josh Poimboeuf
Quoted 9 lines · Josh Poimboeuf
Quoted 8 lines · Thomas Gleixner

That function returns immediately after conditionally reenabling interrupts which is more than pointless and requires the ASM code to disable interrupts again.

---
 arch/x86/kernel/traps.c |    1 -
 1 file changed, 1 deletion(-)
Quoted 11 lines

arch/x86/kernel/traps.c do_simd_coprocessor_error(struct pt_regs · line 874

Quoted 8 lines
arch/x86/kernel/traps.c +0 -1
@@ -871,7 +871,6 @@ do_simd_coprocessor_error(struct pt_regs
 dotraplinkage void
 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
 {
-	cond_local_irq_enable(regs);
 }

I think we can just remove this handler altogether. The Intel and AMD manuals say vector 15 (X86_TRAP_SPURIOUS) is reserved.

Right, but this has history. Pentium Pro Erratum:

PROBLEM: If the APIC subsystem is configured in mixed mode with Virtual Wire mode implemented through the local APIC, an interrupt vector of 0Fh (Intel reserved encoding) may be generated by the local APIC (Int 15). This vector may be generated upon receipt of a spurious interrupt (an interrupt which is removed before the system receives the INTA sequence) instead of the programmed 8259 spurious interrupt vector.

IMPLICATION: The spurious interrupt vector programmed in the 8259 is normally handled by an operating system’s spurious interrupt handler. However, a vector of 0Fh is unknown to some operating systems, which would crash if this erratum occurred.

Initially (2.1.) there was a printk() in that handler, which later got ifdeffed out (2.1.54).

So I rather keep that thing at least as long as we support PPro :) Even if we ditch that the handler is not really hurting anyone.

Thanks,

tglx

#13 lore raw

Re: [patch V2 03/17] x86/traps: Remove pointless irq enable from do_spurious_interrupt_bug()

Quoted 37 lines · Thomas Gleixner
Quoted 9 lines · Josh Poimboeuf
Quoted 8 lines · Thomas Gleixner

That function returns immediately after conditionally reenabling interrupts which is more than pointless and requires the ASM code to disable interrupts again.

---
 arch/x86/kernel/traps.c |    1 -
 1 file changed, 1 deletion(-)
Quoted 11 lines

arch/x86/kernel/traps.c do_simd_coprocessor_error(struct pt_regs · line 874

Quoted 8 lines
arch/x86/kernel/traps.c +0 -1
@@ -871,7 +871,6 @@ do_simd_coprocessor_error(struct pt_regs
 dotraplinkage void
 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
 {
-	cond_local_irq_enable(regs);
 }

I think we can just remove this handler altogether. The Intel and AMD manuals say vector 15 (X86_TRAP_SPURIOUS) is reserved.

Right, but this has history. Pentium Pro Erratum:

PROBLEM: If the APIC subsystem is configured in mixed mode with Virtual Wire mode implemented through the local APIC, an interrupt vector of 0Fh (Intel reserved encoding) may be generated by the local APIC (Int 15). This vector may be generated upon receipt of a spurious interrupt (an interrupt which is removed before the system receives the INTA sequence) instead of the programmed 8259 spurious interrupt vector.

IMPLICATION: The spurious interrupt vector programmed in the 8259 is normally handled by an operating system’s spurious interrupt handler. However, a vector of 0Fh is unknown to some operating systems, which would crash if this erratum occurred.

Quoted 5 lines

Initially (2.1.) there was a printk() in that handler, which later got ifdeffed out (2.1.54).

So I rather keep that thing at least as long as we support PPro :) Even if we ditch that the handler is not really hurting anyone.

Ah. I guess we could remove the idtentry for 64-bit then? Anyway the above would be a good comment for the function.

Signature · 1 line
Josh
#43 lore raw

Re: [patch V2 08/17] x86/entry: Move syscall irq tracing to C code

In reply to Andy Lutomirski
Quoted 21 lines · Andy Lutomirski
Quoted 6 lines · Thomas Gleixner

Interrupt state tracing can be safely done in C code. The few stack operations in assembly do not need to be covered.

Remove the now pointless indirection via .Lsyscall_32_done and jump to swapgs_restore_regs_and_return_to_usermode directly.

This doesn't look right.

Review static void syscall_slow_exit_work(struc · line 287

Quoted 8 lines

#define SYSCALL_EXIT_WORK_FLAGS \

(quoted diff) +3 -0
@@ -279,6 +282,9 @@ static void syscall_slow_exit_work(struc
 {
        struct thread_info *ti;
 
+       /* User to kernel transition disabled interrupts. */
+       trace_hardirqs_off();
+

So you just traced IRQs off, but...

Quoted 4 lines

Review static void syscall_slow_exit_work(struc · line 289

Quoted 2 lines
(quoted diff) +0 -0
        enter_from_user_mode();
        local_irq_enable();

Now they're on and traced on again?

Yes, because that's what actually happens.

usermode
syscall <- Disables interrupts, but tracing thinks they are on
entry_SYSCALL_64
....
call do_syscall_64

trace_hardirqs_off() <- So before calling anything else, we have to tell
the tracer that interrupts are on, which we did so far in the ASM code between entry_SYSCALL_64 and 'call do_syscall_64'. I'm merily lifting this to C-code.

enter_from_user_mode()
local_irq_enable()

Quoted 1 line

I also don't see how your patch handles the fastpath case.

Hmm?

All syscalls return through:

syscall_return_slowpath()
local_irq_disable()
prepare_exit_to_usermode()
user_enter_irqoff()
mds_user_clear_cpu_buffers()
trace_hardirqs_on()

What am I missing?

Quoted 1 line

How about the attached patch instead?

      	    	^^^^^^ Groan.

arch/x86/entry/common.c __visible inline void prepare_exit_to_usermode(struct pt_regs *regs) · line 222

Quoted 6 lines
arch/x86/entry/common.c +3 -0
 	user_enter_irqoff();
 
+	/*
+	 * The actual return to usermode will almost certainly turn IRQs on.
+	 * Trace it here to simplify the asm code.

Why would we return to user from a syscall or interrupt with interrupts traced as disabled? Also the existing ASM is inconsistent vs. that:

ENTRY(entry_SYSENTER_32) TRACE_IRQS_ON

ENTRY(entry_INT80_32) TRACE_IRQS_IRET

ENTRY(entry_SYSCALL_64) TRACE_IRQS_IRET

ENTRY(ret_from_fork) TRACE_IRQS_ON

GLOBAL(retint_user) TRACE_IRQS_IRETQ

ENTRY(entry_SYSCALL_compat) TRACE_IRQS_ON

ENTRY(entry_INT80_compat) TRACE_IRQS_ON

arch/x86/entry/common.c __visible inline void prepare_exit_to_usermode(struct pt_regs *regs) · line 225

Quoted 3 lines
arch/x86/entry/common.c +3 -0
+	 */
+	if (likely(regs->flags & X86_EFLAGS_IF))
+		trace_hardirqs_on();

My variant does this unconditionally and after
mds_user_clear_cpu_buffers().

Quoted 2 lines
	mds_user_clear_cpu_buffers();
}

And your ASM changes keep still all the TRACE_IRQS_OFF invocations in the various syscall entry pathes, which is what I removed and put as the first thing into the C functions.

Confused.

Thanks,

tglx

#14 lore raw

Re: [patch V2 03/17] x86/traps: Remove pointless irq enable from do_spurious_interrupt_bug()

In reply to Josh Poimboeuf
Quoted 38 lines · Josh Poimboeuf
Quoted 37 lines · Thomas Gleixner
Quoted 9 lines · Josh Poimboeuf
Quoted 8 lines · Thomas Gleixner

That function returns immediately after conditionally reenabling interrupts which is more than pointless and requires the ASM code to disable interrupts again.

---
 arch/x86/kernel/traps.c |    1 -
 1 file changed, 1 deletion(-)
Quoted 11 lines

arch/x86/kernel/traps.c do_simd_coprocessor_error(struct pt_regs · line 874

Quoted 8 lines
arch/x86/kernel/traps.c +0 -1
@@ -871,7 +871,6 @@ do_simd_coprocessor_error(struct pt_regs
 dotraplinkage void
 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
 {
-	cond_local_irq_enable(regs);
 }

I think we can just remove this handler altogether. The Intel and AMD manuals say vector 15 (X86_TRAP_SPURIOUS) is reserved.

Right, but this has history. Pentium Pro Erratum:

PROBLEM: If the APIC subsystem is configured in mixed mode with Virtual Wire mode implemented through the local APIC, an interrupt vector of 0Fh (Intel reserved encoding) may be generated by the local APIC (Int 15). This vector may be generated upon receipt of a spurious interrupt (an interrupt which is removed before the system receives the INTA sequence) instead of the programmed 8259 spurious interrupt vector.

IMPLICATION: The spurious interrupt vector programmed in the 8259 is normally handled by an operating system’s spurious interrupt handler. However, a vector of 0Fh is unknown to some operating systems, which would crash if this erratum occurred.

Quoted 8 lines
Quoted 5 lines

Initially (2.1.) there was a printk() in that handler, which later got ifdeffed out (2.1.54).

So I rather keep that thing at least as long as we support PPro :) Even if we ditch that the handler is not really hurting anyone.

Ah. I guess we could remove the idtentry for 64-bit then? Anyway the above would be a good comment for the function.

That doesn't buy much. Who knows how many other CPUs issue vector 15 occasionally and will then crash and burn. Better safe than sorry :)

Thanks,

tglx

#55 lore raw

Re: [patch V2 14/17] entry: Provide generic exit to usermode functionality

In reply to Andy Lutomirski
Quoted 8 lines · Andy Lutomirski
Quoted 5 lines · Thomas Gleixner

From: Thomas Gleixner <[email protected]>

Provide a generic facility to handle the exit to usermode work. That's aimed to replace the pointlessly different copies in each architecture.

Quoted 8 lines
Quoted 6 lines

/**
+ * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
+ * @ti_work: Cached TIF flags gathered with interrupts disabled
+ *
+ * Defaults to local_irq_enable(). Can be supplied by architecture specific
+ * code.

What did you have in mind here?

Look at the previous version which had the ARM64 conversion. ARM64 does magic different stuff vs. local_irq_enable() in the exit path. It's not using the regular one. I'm happy to ditch that :)

Thanks,

tglx

#23 lore raw

Re: [patch V2 05/17] x86/traps: Make interrupt enable/disable symmetric in C code

In reply to Josh Poimboeuf
Quoted 6 lines · Josh Poimboeuf
Quoted 5 lines · Thomas Gleixner
arch/x86/mm/fault.c +0 -0
@@ -1500,10 +1500,13 @@ static noinline void
 		return;
 
Quoted 13 lines

arch/x86/mm/fault.c static noinline void · line 1509

Quoted 10 lines
arch/x86/mm/fault.c +5 -2
 	/* Was the fault on kernel-controlled part of the address space? */
-	if (unlikely(fault_in_kernel_space(address)))
+	if (unlikely(fault_in_kernel_space(address))) {
 		do_kern_addr_fault(regs, hw_error_code, address);
-	else
+	} else {
 		do_user_addr_fault(regs, hw_error_code, address);
+		if (regs->flags & X86_EFLAGS_IF)
+			local_irq_disable();
+	}

The corresponding irq enable is in do_user_addr_fault(), why not do the disable there?

Yeah, will do. Was just as lazy as Peter and did not want to touch the gazillion of returns. :)

Thanks,

tglx

#42 lore raw

Re: [patch V2 08/17] x86/entry: Move syscall irq tracing to C code

In reply to Andy Lutomirski
Quoted 28 lines · Andy Lutomirski
Quoted 27 lines · Andy Lutomirski
Quoted 6 lines · Thomas Gleixner

Interrupt state tracing can be safely done in C code. The few stack operations in assembly do not need to be covered.

Remove the now pointless indirection via .Lsyscall_32_done and jump to swapgs_restore_regs_and_return_to_usermode directly.

This doesn't look right.

Review static void syscall_slow_exit_work(struc · line 287

Quoted 8 lines

#define SYSCALL_EXIT_WORK_FLAGS \

(quoted diff) +3 -0
@@ -279,6 +282,9 @@ static void syscall_slow_exit_work(struc
 {
        struct thread_info *ti;
 
+       /* User to kernel transition disabled interrupts. */
+       trace_hardirqs_off();
+

So you just traced IRQs off, but...

Review static void syscall_slow_exit_work(struc · line 289

Quoted 2 lines
(quoted diff) +0 -0
        enter_from_user_mode();
        local_irq_enable();

Now they're on and traced on again?

Quoted 7 lines
Quoted 3 lines

I also don't see how your patch handles the fastpath case.

How about the attached patch instead?

Ignore the attached patch. You have this in your do_exit_to_usermode() later in the series. But I'm still quite confused by this patch.

What's confusing you? It basically does:

ENTRY(syscall/int80)

- TRACE_IRQS_OFF
call C-syscall*()
- TRACE_IRQS_ON/IRET

and

C-syscall*()

+ trace_hardirqs_off() <- first action

	....
	prepare_exit_to_usermode()	<- last action
	return

and

prepare_exit_to_usermode()

	....

+ trace_hardirqs_on() <- last action
return

So this is exactly the same as the ASM today.

The only change is that I made it do unconditionally trace_hardirqs_on() for consistency reasons.

I tried to split it into bits and pieces, but failed to come up with something sensible. Let me try again tomorrow.

Thanks,

tglx

#31 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

In reply to Josh Poimboeuf
Quoted 9 lines · Josh Poimboeuf
Quoted 5 lines · Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls.

Add debug checks where appropriate.

This seems a little scary. Does anybody other than Andy actually run with CONFIG_DEBUG_ENTRY?

I do.

Quoted 2 lines

What happens if somebody accidentally leaves irqs enabled? How do we know you found all the leaks?

For the DO_ERROR() ones that's trivial:

#define DO_ERROR(trapnr, signr, sicode, addr, str, name) \
dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
{ \
do_error_trap(regs, error_code, str, trapnr, signr, sicode, addr); \
+ lockdep_assert_irqs_disabled(); \
}

DO_ERROR(X86_TRAP_DE, SIGFPE, FPE_INTDIV, IP, "divide error", divide_error)

Now for the rest we surely could do:

dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)

{
	__do_bounds(regs, error_code);
	lockdep_assert_irqs_disabled();
}

and move the existing body into a static function so independent of any (future) return path there the lockdep assert will be invoked.

Thanks,

tglx

maintainer
#32 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

Quoted 31 lines · Thomas Gleixner
Quoted 9 lines · Josh Poimboeuf
Quoted 5 lines · Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls.

Add debug checks where appropriate.

This seems a little scary. Does anybody other than Andy actually run with CONFIG_DEBUG_ENTRY?

I do.

Quoted 2 lines

What happens if somebody accidentally leaves irqs enabled? How do we know you found all the leaks?

For the DO_ERROR() ones that's trivial:

 #define DO_ERROR(trapnr, signr, sicode, addr, str, name)                  \
 dotraplinkage void do_##name(struct pt_regs *regs, long error_code)       \
 {                                                                         \
        do_error_trap(regs, error_code, str, trapnr, signr, sicode, addr); \

+ lockdep_assert_irqs_disabled(); \
}

DO_ERROR(X86_TRAP_DE, SIGFPE, FPE_INTDIV, IP, "divide error", divide_error)

Now for the rest we surely could do:

Quoted 9 lines

dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)

{
        __do_bounds(regs, error_code);
        lockdep_assert_irqs_disabled();
}

and move the existing body into a static function so independent of any (future) return path there the lockdep assert will be invoked.

If we do this, can we macro-ize it:

DEFINE_IDTENTRY_HANDLER(do_bounds)
{
...
}

If you do this, please don't worry about the weird ones that take cr2 as a third argument. Once your series lands, I will send a follow-up to get rid of it. It's 2/3 written already.

maintainer Acked-by
#44 lore raw

Re: [patch V2 08/17] x86/entry: Move syscall irq tracing to C code

In reply to Andy Lutomirski
Quoted 10 lines · Andy Lutomirski
Quoted 6 lines · Thomas Gleixner

Interrupt state tracing can be safely done in C code. The few stack operations in assembly do not need to be covered.

Remove the now pointless indirection via .Lsyscall_32_done and jump to swapgs_restore_regs_and_return_to_usermode directly.

This doesn't look right.

Well, I feel a bit silly. I read this:

Quoted 3 lines

Review static void syscall_slow_exit_work(struc

Quoted 3 lines

#define SYSCALL_EXIT_WORK_FLAGS \

(quoted diff) +0 -0
@@ -279,6 +282,9 @@ static void syscall_slow_exit_work(struc

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

and I applied the diff in my head to the wrong function, and I didn't notice that it didn't really apply there. Oddly, gitweb gets this right:

https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?h=WIP.core/entry&id=e3158f93138ded84eb44fa97606197f6adcf9366

Looking at the actual code:

with one minor caveat: you are making a subtle and mostly irrelevant semantic change: with your patch, user mode will be traced as IRQs on even if a nasty user has used iopl() to turn off interrupts. This is probably a good thing, but I think you should mention it in the changelog.

FWIW, the rest of the series looks pretty good, too.

maintainer
#45 lore raw

Re: [patch V2 08/17] x86/entry: Move syscall irq tracing to C code

Quoted 15 lines · Andy Lutomirski
Quoted 10 lines · Andy Lutomirski
Quoted 6 lines · Thomas Gleixner

Interrupt state tracing can be safely done in C code. The few stack operations in assembly do not need to be covered.

Remove the now pointless indirection via .Lsyscall_32_done and jump to swapgs_restore_regs_and_return_to_usermode directly.

This doesn't look right.

Well, I feel a bit silly. I read this:

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Review static void syscall_slow_exit_work(struc

Quoted 2 lines

#define SYSCALL_EXIT_WORK_FLAGS \

(quoted diff) +0 -0
@@ -279,6 +282,9 @@ static void syscall_slow_exit_work(struc

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

and I applied the diff in my head to the wrong function, and I didn't notice that it didn't really apply there. Oddly, gitweb gets this

I had the same when reviewing these patches; I was almost going to ask tglx about it on IRC when the penny dropped.

#33 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

In reply to Andy Lutomirski
Quoted 30 lines · Andy Lutomirski
Quoted 17 lines · Thomas Gleixner
Quoted 2 lines · Josh Poimboeuf

What happens if somebody accidentally leaves irqs enabled? How do we know you found all the leaks?

For the DO_ERROR() ones that's trivial:

 #define DO_ERROR(trapnr, signr, sicode, addr, str, name)                  \
 dotraplinkage void do_##name(struct pt_regs *regs, long error_code)       \
 {                                                                         \
        do_error_trap(regs, error_code, str, trapnr, signr, sicode, addr); \

+ lockdep_assert_irqs_disabled(); \
}

DO_ERROR(X86_TRAP_DE, SIGFPE, FPE_INTDIV, IP, "divide error", divide_error)

Now for the rest we surely could do:

Quoted 9 lines

dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)

{
        __do_bounds(regs, error_code);
        lockdep_assert_irqs_disabled();
}

and move the existing body into a static function so independent of any (future) return path there the lockdep assert will be invoked.

If we do this, can we macro-ize it:

Quoted 8 lines

DEFINE_IDTENTRY_HANDLER(do_bounds)
{
...
}

If you do this, please don't worry about the weird ones that take cr2 as a third argument. Once your series lands, I will send a follow-up to get rid of it. It's 2/3 written already.

I spent quite some time digging deeper into this. Finding all corner cases which eventually enable interrupts from an exception handler is not as trivial as it looked in the first place. Especially the fault handler is a nightmare. Also PeterZ's approach of doing

	   if (regs->eflags & IF)
	   	local_irq_disable();

is doomed due to sys_iopl(). See below.

I'm tempted to do pretty much the same thing as the syscall rework did as a first step:

- Move the actual handler invocation to C

- Do the irq tracing on entry in C

- Move irq disable before return to ASM

Peter gave me some half finished patches which pretty much do that by copying half of the linux/syscalls.h macro maze into the entry code. That's one possible solution, but TBH it sucks big times.

We have the following variants:

do_divide_error(struct pt_regs *regs, long error_code); do_debug(struct pt_regs *regs, long error_code); do_nmi(struct pt_regs *regs, long error_code); do_int3(struct pt_regs *regs, long error_code); do_overflow(struct pt_regs *regs, long error_code); do_bounds(struct pt_regs *regs, long error_code); do_invalid_op(struct pt_regs *regs, long error_code); do_device_not_available(struct pt_regs *regs, long error_code); do_coprocessor_segment_overrun(struct pt_regs *regs, long error_code); do_invalid_TSS(struct pt_regs *regs, long error_code); do_segment_not_present(struct pt_regs *regs, long error_code); do_stack_segment(struct pt_regs *regs, long error_code); do_general_protection(struct pt_regs *regs, long error_code); do_spurious_interrupt_bug(struct pt_regs *regs, long error_code); do_coprocessor_error(struct pt_regs *regs, long error_code); do_alignment_check(struct pt_regs *regs, long error_code); do_machine_check(struct pt_regs *regs, long error_code); do_simd_coprocessor_error(struct pt_regs *regs, long error_code); do_iret_error(struct pt_regs *regs, long error_code); do_mce(struct pt_regs *regs, long error_code);

do_async_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address); do_double_fault(struct pt_regs *regs, long error_code, unsigned long address); do_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address);

So if we can remove the third argument then we can spare most of the macro maze and just have one common function without bells and whistels. The other option would be to extend all handlers to have three arguments, i.e. add 'long unused', which is not pretty either.

What's your plan with cr2? Stash it in pt_regs or something else?

Once we have the interesting parts in C then we can revisit the elimination of the unconditional irq disable because in C it's way simpler to do diagnostics, but I'm not entirely sure whether it's worth it.

A related issue is the inconsistency of the irq disabled tracing in the return to user path. As I pointed out in the other mail, the various syscall implementations do that differently. The exception handlers do it always conditional, regular interrupts as well. For regular interrupts that does not make sense as they can by all means never return to an interrupt disabled context.

The interesting bells and whistels result from sys_iopl(). If user space has been granted iopl(level = 3) it gains cli/sti priviledges. When the application has interrupts disabled in userspace:

- invocation of a syscall

- any exception (aside of NMI/MCE) which conditionally enables interrupts depending on user_mode(regs) and therefor can be preempted and schedule

is just undefined behaviour and I personally consider it to be a plain bug.

Just for the record: This results in running a resulting or even completely unrelated signal handler with interrupts disabled as well.

Whatever we decide it is, leaving it completely inconsistent is not a solution at all. The options are:

1) Always do conditional tracing depending on the user_regs->eflags.IF state.

2) #1 + warn once when syscalls and exceptions (except NMI/MCE) happen

      and user_regs->eflags.IF is cleared.

3a) #2 + enforce signal handling to run with interrupts enabled.

3b) #2 + set regs->eflags.IF. So the state is always correct from the
kernel POV. Of course that changes existing behaviour, but its changing undefined and inconsistent behaviour.

4) Let iopl(level) return -EPERM if level == 3.

Yeah, I know it's not possible due to regressions (DPKD uses iopl(3)), but TBH that'd be the sanest option of all.

Of course the infinite wisdom of hardware designers tied IN, INS, OUT, OUTS and CLI/STI together on IOPL so we cannot even distangle them in any way.

The only way out would be to actually use a full 8K sized I/O bitmap, but that's a massive pain as it has to be copied on every context switch.

Really pretty options to chose from ...

Thanks,

tglx

#46 lore raw

Re: [patch V2 08/17] x86/entry: Move syscall irq tracing to C code

In reply to Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Andy Lutomirski
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines · Thomas Gleixner

Interrupt state tracing can be safely done in C code. The few stack operations in assembly do not need to be covered.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Remove the now pointless indirection via .Lsyscall_32_done and jump to swapgs_restore_regs_and_return_to_usermode directly.

This doesn't look right.

Well, I feel a bit silly. I read this:

Happened to me before. Don't worry.

Quoted 11 lines
Quoted 8 lines
Quoted 3 lines

Review static void syscall_slow_exit_work(struc

Quoted 3 lines

#define SYSCALL_EXIT_WORK_FLAGS \

(quoted diff) +0 -0
@@ -279,6 +282,9 @@ static void syscall_slow_exit_work(struc

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

and I applied the diff in my head to the wrong function, and I didn't notice that it didn't really apply there. Oddly, gitweb gets this

I had the same when reviewing these patches; I was almost going to ask tglx about it on IRC when the penny dropped.

diff is weird at times.

Thanks,

tglx

#34 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

In reply to Thomas Gleixner
Quoted 2 lines · Thomas Gleixner

Whatever we decide it is, leaving it completely inconsistent is not a solution at all. The options are:

Actually there is also:

0) Always do unconditional trace_irqs_on().

But that does not allow to actually trace the real return flags state which might be useful to diagnose crap which results from user space CLI.

Quoted 31 lines

1) Always do conditional tracing depending on the user_regs->eflags.IF state.

2) #1 + warn once when syscalls and exceptions (except NMI/MCE) happen

      and user_regs->eflags.IF is cleared.

3a) #2 + enforce signal handling to run with interrupts enabled.

3b) #2 + set regs->eflags.IF. So the state is always correct from the
kernel POV. Of course that changes existing behaviour, but its changing undefined and inconsistent behaviour.

4) Let iopl(level) return -EPERM if level == 3.

Yeah, I know it's not possible due to regressions (DPKD uses iopl(3)), but TBH that'd be the sanest option of all.

Of course the infinite wisdom of hardware designers tied IN, INS, OUT, OUTS and CLI/STI together on IOPL so we cannot even distangle them in any way.

The only way out would be to actually use a full 8K sized I/O bitmap, but that's a massive pain as it has to be copied on every context switch.

Really pretty options to chose from ...

Thanks,

tglx

maintainer
#35 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

In reply to Thomas Gleixner
Quoted 1 line · Thomas Gleixner

is just undefined behaviour and I personally consider it to be a plain bug.

I concur.

Quoted 27 lines

Just for the record: This results in running a resulting or even completely unrelated signal handler with interrupts disabled as well.

Whatever we decide it is, leaving it completely inconsistent is not a solution at all. The options are:

1) Always do conditional tracing depending on the user_regs->eflags.IF state.

2) #1 + warn once when syscalls and exceptions (except NMI/MCE) happen

      and user_regs->eflags.IF is cleared.

3a) #2 + enforce signal handling to run with interrupts enabled.

3b) #2 + set regs->eflags.IF. So the state is always correct from the
kernel POV. Of course that changes existing behaviour, but its changing undefined and inconsistent behaviour.

4) Let iopl(level) return -EPERM if level == 3.

Yeah, I know it's not possible due to regressions (DPKD uses iopl(3)), but TBH that'd be the sanest option of all.

Of course the infinite wisdom of hardware designers tied IN, INS, OUT, OUTS and CLI/STI together on IOPL so we cannot even distangle them in any way.

Quoted 5 lines

The only way out would be to actually use a full 8K sized I/O bitmap, but that's a massive pain as it has to be copied on every context switch.

Really pretty options to chose from ...

If 4 is out (and I'm afraid it might be), then I'm on record for liking 3b.

maintainer
#36 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

In reply to Thomas Gleixner
Quoted 46 lines · Thomas Gleixner
Quoted 30 lines · Andy Lutomirski
Quoted 17 lines · Thomas Gleixner
Quoted 2 lines · Josh Poimboeuf

What happens if somebody accidentally leaves irqs enabled? How do we know you found all the leaks?

For the DO_ERROR() ones that's trivial:

 #define DO_ERROR(trapnr, signr, sicode, addr, str, name)                  \
 dotraplinkage void do_##name(struct pt_regs *regs, long error_code)       \
 {                                                                         \
        do_error_trap(regs, error_code, str, trapnr, signr, sicode, addr); \

+ lockdep_assert_irqs_disabled(); \
}

DO_ERROR(X86_TRAP_DE, SIGFPE, FPE_INTDIV, IP, "divide error", divide_error)

Now for the rest we surely could do:

Quoted 9 lines

dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)

{
        __do_bounds(regs, error_code);
        lockdep_assert_irqs_disabled();
}

and move the existing body into a static function so independent of any (future) return path there the lockdep assert will be invoked.

If we do this, can we macro-ize it:

Quoted 8 lines

DEFINE_IDTENTRY_HANDLER(do_bounds)
{
...
}

If you do this, please don't worry about the weird ones that take cr2 as a third argument. Once your series lands, I will send a follow-up to get rid of it. It's 2/3 written already.

I spent quite some time digging deeper into this. Finding all corner cases which eventually enable interrupts from an exception handler is not as trivial as it looked in the first place. Especially the fault handler is a nightmare. Also PeterZ's approach of doing

Quoted 4 lines
           if (regs->eflags & IF)
                local_irq_disable();

is doomed due to sys_iopl(). See below.

I missed something in the discussion. What breaks? Can you check user_mode(regs) too?

Quoted 41 lines

I'm tempted to do pretty much the same thing as the syscall rework did as a first step:

- Move the actual handler invocation to C

- Do the irq tracing on entry in C

- Move irq disable before return to ASM

Peter gave me some half finished patches which pretty much do that by copying half of the linux/syscalls.h macro maze into the entry code. That's one possible solution, but TBH it sucks big times.

We have the following variants:

do_divide_error(struct pt_regs *regs, long error_code); do_debug(struct pt_regs *regs, long error_code); do_nmi(struct pt_regs *regs, long error_code); do_int3(struct pt_regs *regs, long error_code); do_overflow(struct pt_regs *regs, long error_code); do_bounds(struct pt_regs *regs, long error_code); do_invalid_op(struct pt_regs *regs, long error_code); do_device_not_available(struct pt_regs *regs, long error_code); do_coprocessor_segment_overrun(struct pt_regs *regs, long error_code); do_invalid_TSS(struct pt_regs *regs, long error_code); do_segment_not_present(struct pt_regs *regs, long error_code); do_stack_segment(struct pt_regs *regs, long error_code); do_general_protection(struct pt_regs *regs, long error_code); do_spurious_interrupt_bug(struct pt_regs *regs, long error_code); do_coprocessor_error(struct pt_regs *regs, long error_code); do_alignment_check(struct pt_regs *regs, long error_code); do_machine_check(struct pt_regs *regs, long error_code); do_simd_coprocessor_error(struct pt_regs *regs, long error_code); do_iret_error(struct pt_regs *regs, long error_code); do_mce(struct pt_regs *regs, long error_code);

do_async_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address); do_double_fault(struct pt_regs *regs, long error_code, unsigned long address); do_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address);

Quoted 6 lines

So if we can remove the third argument then we can spare most of the macro maze and just have one common function without bells and whistels. The other option would be to extend all handlers to have three arguments, i.e. add 'long unused', which is not pretty either.

What's your plan with cr2? Stash it in pt_regs or something else?

Just read it from CR2. I added a new idtentry macro arg called "entry_work", and setting it to 0 causes the enter_from_user_mode to be skipped. Then C code calls enter_from_user_mode() after reading CR2 (and DR7). WIP code is here:

https://git.kernel.org/pub/scm/linux/kernel/git/luto/linux.git/log/?h=x86/idtentry

The idea is that, if everything is converted, then we get rid of the entry_work=1 case, which is easier if there's a macro.

So my suggestion is to use a macro for the 2-arg version and open-code all the 3-arg cases. Then, when the dust settles, we get rid of the third arg and they can use the macro.

Quoted 7 lines

The interesting bells and whistels result from sys_iopl(). If user space has been granted iopl(level = 3) it gains cli/sti priviledges. When the application has interrupts disabled in userspace:

- invocation of a syscall

Quoted 8 lines

- any exception (aside of NMI/MCE) which conditionally enables interrupts depending on user_mode(regs) and therefor can be preempted and schedule

is just undefined behaviour and I personally consider it to be a plain bug.

Just for the record: This results in running a resulting or even completely unrelated signal handler with interrupts disabled as well.

I am seriously tempted to say that the solution is to remove iopl(), at least on 64-bit kernels. Doing STI in user mode is BS :)

Otherwise we need to give it semantics, no? I personally have no actual problem with the fact that an NMI can cause scheduling to happen. Big fscking deal.

Quoted 6 lines

Whatever we decide it is, leaving it completely inconsistent is not a solution at all. The options are:

1) Always do conditional tracing depending on the user_regs->eflags.IF state.

I'm okay with always tracing like user mode means IRQs on or doing it "correctly". I consider the former to be simpler and therefore quite possibly better.

Quoted 15 lines

2) #1 + warn once when syscalls and exceptions (except NMI/MCE) happen

      and user_regs->eflags.IF is cleared.

3a) #2 + enforce signal handling to run with interrupts enabled.

3b) #2 + set regs->eflags.IF. So the state is always correct from the
kernel POV. Of course that changes existing behaviour, but its changing undefined and inconsistent behaviour.

4) Let iopl(level) return -EPERM if level == 3.

Yeah, I know it's not possible due to regressions (DPKD uses iopl(3)), but TBH that'd be the sanest option of all.

Quoted 8 lines

Of course the infinite wisdom of hardware designers tied IN, INS, OUT, OUTS and CLI/STI together on IOPL so we cannot even distangle them in any way.

The only way out would be to actually use a full 8K sized I/O bitmap, but that's a massive pain as it has to be copied on every context switch.

Hmm. This actually doesn't seem that bad. We already have a TIF_ flag to optimize this. So basically iopl() would effectively become ioperm(everything on).

#37 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

Quoted 6 lines · Andy Lutomirski
Quoted 5 lines · Thomas Gleixner

I spent quite some time digging deeper into this. Finding all corner cases which eventually enable interrupts from an exception handler is not as trivial as it looked in the first place. Especially the fault handler is a nightmare. Also PeterZ's approach of doing

Quoted 6 lines
Quoted 4 lines
           if (regs->eflags & IF)
                local_irq_disable();

is doomed due to sys_iopl(). See below.

I missed something in the discussion. What breaks?

Assume user space has issued CLI then the above check is giving the wrong answer because it assumes that all faults in user mode have IF set.

Quoted 1 line

Can you check user_mode(regs) too?

Yes, but I still hate it with a passion :)

Quoted 7 lines
Quoted 1 line

What's your plan with cr2? Stash it in pt_regs or something else?

Just read it from CR2. I added a new idtentry macro arg called "entry_work", and setting it to 0 causes the enter_from_user_mode to be skipped. Then C code calls enter_from_user_mode() after reading CR2 (and DR7). WIP code is here:

Quoted 8 lines

https://git.kernel.org/pub/scm/linux/kernel/git/luto/linux.git/log/?h=x86/idtentry

The idea is that, if everything is converted, then we get rid of the entry_work=1 case, which is easier if there's a macro.

So my suggestion is to use a macro for the 2-arg version and open-code all the 3-arg cases. Then, when the dust settles, we get rid of the third arg and they can use the macro.

I'll have a look tomorrow with brain awake.

Quoted 10 lines
Quoted 10 lines

The interesting bells and whistels result from sys_iopl(). If user space has been granted iopl(level = 3) it gains cli/sti priviledges. When the application has interrupts disabled in userspace:

- invocation of a syscall

- any exception (aside of NMI/MCE) which conditionally enables interrupts depending on user_mode(regs) and therefor can be preempted and schedule

Quoted 7 lines
Quoted 4 lines

is just undefined behaviour and I personally consider it to be a plain bug.

Just for the record: This results in running a resulting or even completely unrelated signal handler with interrupts disabled as well.

I am seriously tempted to say that the solution is to remove iopl(), at least on 64-bit kernels. Doing STI in user mode is BS :)

STI would be halfways sane. CLI is the problem. And yes I agree it's BS :)

Quoted 3 lines

Otherwise we need to give it semantics, no? I personally have no actual problem with the fact that an NMI can cause scheduling to happen. Big fscking deal.

Right, I don't care either. I do neither care that any exception/syscall which hits a user space CLI region might schedule. It's been that way forever.

But giving this semantics is insanely hard at least if you want sensible, useful, consistent and understandable semantics. I know that's overrated.

Quoted 30 lines
Quoted 5 lines

Whatever we decide it is, leaving it completely inconsistent is not a solution at all. The options are:

1) Always do conditional tracing depending on the user_regs->eflags.IF state.

I'm okay with always tracing like user mode means IRQs on or doing it "correctly". I consider the former to be simpler and therefore quite possibly better.

Quoted 20 lines

2) #1 + warn once when syscalls and exceptions (except NMI/MCE) happen

      and user_regs->eflags.IF is cleared.

3a) #2 + enforce signal handling to run with interrupts enabled.

3b) #2 + set regs->eflags.IF. So the state is always correct from the
kernel POV. Of course that changes existing behaviour, but its changing undefined and inconsistent behaviour.

4) Let iopl(level) return -EPERM if level == 3.

Yeah, I know it's not possible due to regressions (DPKD uses iopl(3)), but TBH that'd be the sanest option of all.

Of course the infinite wisdom of hardware designers tied IN, INS, OUT, OUTS and CLI/STI together on IOPL so we cannot even distangle them in any way.

Quoted 7 lines
Quoted 3 lines

The only way out would be to actually use a full 8K sized I/O bitmap, but that's a massive pain as it has to be copied on every context switch.

Hmm. This actually doesn't seem that bad. We already have a TIF_ flag to optimize this. So basically iopl() would effectively become ioperm(everything on).

Yes, and the insane user space would:

1) Pay the latency price for copying 8K bitmap on every context switch IN

2) Inflict latency on the next task due to requiring memset of 8K bitmap on every context switch OUT

3) #GP when issuing CLI/STI

I personally have no problem with that. #1 and #3 are sane and as iopl() requires CAP_RAW_IO it's not available to Joe User, so the sysadmin is responsible for eventual issues resulting from #2.

Though the no-regression hammer might pound on #3 as it breaks random engineering trainwrecks from hell.

#1/#2 could be easily mitigated though.

      struct tss_struct {
      	struct x86_hw_tss       x86_tss;
	unsigned long           io_bitmap[IO_BITMAP_LONGS + 1];
      };

and x86_tss has

    u16	io_bitmap_base;

which is either set to

INVALID_IO_BITMAP_OFFSET ( 0x8000 )

or

IO_BITMAP_OFFSET
(offsetof(struct tss_struct, io_bitmap) - offsetof(struct tss_struct, x86_tss))

So we could add

	unsigned long           io_bitmap_all[IO_BITMAP_LONGS + 1];

and just set the base to this one.

But that involves also upping __KERNEL_TSS_LIMIT. Too tired to think about the implications of that right now.

Thanks,

tglx

#63 lore raw
In reply to Thomas Gleixner

Hi Thomas,

Quoted 7 lines · Thomas Gleixner

When working on a way to move out the posix cpu timer expiry out of the timer interrupt context, I noticed that KVM is not handling pending task work before entering a guest. A quick hack was to add that to the x86 KVM handling loop. The discussion ended with a request to make this a generic infrastructure possible with also moving the per arch implementations of the enter from and return to user space handling generic.

Quoted 8 lines

https://lore.kernel.org/r/[email protected]

The series implements the syscall enter/exit and the general exit to userspace work handling along with the pre guest enter functionality.

Changes vs. RFC version:

- Dropped ARM64 conversion as requested by ARM64 folks

If you fancy another crack at arm64 on your way back from Lyon, we've now got more of the asm->C conversion queued up here:

https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/log/?h=for-next/entry-s-to-c

No worries if not, but figured it was worth letting you know anyway.

Will

Reviewed-by on v2 1/17
#3 lore raw

Re: [patch V2 01/17] x86/entry/32: Remove unused resume_userspace label

In reply to Thomas Gleixner
Quoted 10 lines · Thomas Gleixner

The C reimplementation of SYSENTER left that unused ENTRY() label around. Remove it.

Fixes 5f310f739b4c ("x86/entry/32: Re-implement SYSENTER using the new C path")
---
  arch/x86/entry/entry_32.S |    1 -
  1 file changed, 1 deletion(-)

alex.

Reviewed-by on v2 2/17
#7 lore raw

Re: [patch V2 02/17] x86/entry/64: Remove pointless jump in paranoid_exit

In reply to Thomas Gleixner
Quoted 8 lines · Thomas Gleixner

Jump directly to restore_regs_and_return_to_kernel instead of making a pointless extra jump through .Lparanoid_exit_restore

---
  arch/x86/entry/entry_64.S |    3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)

alex.

Reviewed-by on v2 3/17
#15 lore raw

Re: [patch V2 03/17] x86/traps: Remove pointless irq enable from do_spurious_interrupt_bug()

In reply to Thomas Gleixner
Quoted 8 lines · Thomas Gleixner

That function returns immediately after conditionally reenabling interrupts which is more than pointless and requires the ASM code to disable interrupts again.

---
  arch/x86/kernel/traps.c |    1 -
  1 file changed, 1 deletion(-)

alex.

Reviewed-by on v2 4/17
#19 lore raw

Re: [patch V2 04/17] x86/entry: Make DEBUG_ENTRY_ASSERT_IRQS_OFF available for 32bit

In reply to Thomas Gleixner
Quoted 11 lines · Thomas Gleixner

Move the interrupt state verification debug macro to common code and fixup the irqflags and paravirt components so it can be used in 32bit code later.

---
  arch/x86/entry/calling.h        |   12 ++++++++++++
  arch/x86/entry/entry_64.S       |   12 ------------
  arch/x86/include/asm/irqflags.h |    8 ++++++--
  arch/x86/include/asm/paravirt.h |    9 +++++----
  4 files changed, 23 insertions(+), 18 deletions(-)

alex.

Reviewed-by on v2 5/17
#24 lore raw

Re: [patch V2 05/17] x86/traps: Make interrupt enable/disable symmetric in C code

In reply to Thomas Gleixner
Quoted 7 lines · Thomas Gleixner

Traps enable interrupts conditionally but rely on the ASM return code to disable them again. That results in redundant interrupt disable and trace calls.

Make the trap handlers disable interrupts before returning to avoid that, which allows simplification of the ASM entry code.

Quoted 7 lines
---
  arch/x86/kernel/traps.c |   32 +++++++++++++++++++++-----------
  arch/x86/mm/fault.c     |    7 +++++--
  2 files changed, 26 insertions(+), 13 deletions(-)

alex.

Reviewed-by on v2 6/17
#27 lore raw

Re: [patch V2 06/17] x86/entry/32: Remove redundant interrupt disable

In reply to Thomas Gleixner
Quoted 5 lines · Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls and the misnomed preempt_stop macro. As a consequence ret_from_exception and ret_from_intr collapse.

Quoted 8 lines

Add a debug check to verify that interrupts are disabled depending on CONFIG_DEBUG_ENTRY.

---
  arch/x86/entry/entry_32.S |   21 ++++++---------------
  1 file changed, 6 insertions(+), 15 deletions(-)

alex.

Reviewed-by on v2 7/17
#38 lore raw

Re: [patch V2 07/17] x86/entry/64: Remove redundant interrupt disable

In reply to Thomas Gleixner
Quoted 11 lines · Thomas Gleixner

Now that the trap handlers return with interrupts disabled, the unconditional disabling of interrupts in the low level entry code can be removed along with the trace calls.

Add debug checks where appropriate.

---
  arch/x86/entry/entry_64.S |    9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)

alex.

#8 lore raw

[tip: x86/asm] x86/entry/64: Remove pointless jump in paranoid_exit

In reply to Thomas Gleixner
The following commit has been merged into the x86/asm branch of tip:

The following commit has been merged into the x86/asm branch of tip:

Commit-ID:     45c08383141794a7e9b26f35d491b74f33ac469e
Gitweb:        https://git.kernel.org/tip/45c08383141794a7e9b26f35d491b74f33ac469e
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Wed, 23 Oct 2019 14:27:07 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Sat, 16 Nov 2019 12:55:55 +01:00

x86/entry/64: Remove pointless jump in paranoid_exit

Jump directly to restore_regs_and_return_to_kernel instead of making a pointless extra jump through .Lparanoid_exit_restore

---
 arch/x86/entry/entry_64.S | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/entry/entry_64.S +1 -2
index d58c012..76942cb 100644
@@ -1273,12 +1273,11 @@ SYM_CODE_START_LOCAL(paranoid_exit)
 	/* Always restore stashed CR3 value (see paranoid_entry) */
 	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
 	SWAPGS_UNSAFE_STACK
-	jmp	.Lparanoid_exit_restore
+	jmp	restore_regs_and_return_to_kernel
 .Lparanoid_exit_no_swapgs:
 	TRACE_IRQS_IRETQ_DEBUG
 	/* Always restore stashed CR3 value (see paranoid_entry) */
 	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
-.Lparanoid_exit_restore:
 	jmp restore_regs_and_return_to_kernel
 SYM_CODE_END(paranoid_exit)
 
#4 lore raw

[tip: x86/asm] x86/entry/32: Remove unused resume_userspace label

In reply to Thomas Gleixner
The following commit has been merged into the x86/asm branch of tip:

The following commit has been merged into the x86/asm branch of tip:

Commit-ID:     df1a7524741b6c094786032e12a21a448321d9f6
Gitweb:        https://git.kernel.org/tip/df1a7524741b6c094786032e12a21a448321d9f6
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Wed, 23 Oct 2019 14:27:06 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Sat, 16 Nov 2019 12:55:55 +01:00

x86/entry/32: Remove unused resume_userspace label

The C reimplementation of SYSENTER left that unused ENTRY() label around. Remove it.

---
 arch/x86/entry/entry_32.S | 1 -
 1 file changed, 1 deletion(-)
arch/x86/entry/entry_32.S +0 -1
index a987b62..4bbcc5e 100644
@@ -824,7 +824,6 @@ ret_from_intr:
 	cmpl	$USER_RPL, %eax
 	jb	restore_all_kernel		# not returning to v8086 or userspace
 
-SYM_INNER_LABEL_ALIGN(resume_userspace, SYM_L_LOCAL)
 	DISABLE_INTERRUPTS(CLBR_ANY)
 	TRACE_IRQS_OFF
 	movl	%esp, %eax
#48 lore raw

Re: [patch V2 09/17] x86/entry: Remove _TIF_NOHZ from _TIF_WORK_SYSCALL_ENTRY

In reply to Thomas Gleixner
Quoted 5 lines · Thomas Gleixner

From: Thomas Gleixner <[email protected]>

Evaluating _TIF_NOHZ to decide whether to use the slow syscall entry path is not only pointless, it's actually counterproductive:

Quoted 8 lines

1) Context tracking code is invoked unconditionally before that flag is evaluated.

2) If the flag is set the slow path is invoked for nothing due to #1

Remove it.

I'm borrowing this patch for a series of mine. But if you apply it in the meantime, that would be even better :-)

Thanks!

#16 lore raw

[tip: x86/entry] x86/traps: Remove pointless irq enable from do_spurious_interrupt_bug()

In reply to Thomas Gleixner
The following commit has been merged into the x86/entry branch of tip:

The following commit has been merged into the x86/entry branch of tip:

Commit-ID:     e039dd815941e785203142261397da6ec64d20cc
Gitweb:        https://git.kernel.org/tip/e039dd815941e785203142261397da6ec64d20cc
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 25 Feb 2020 22:36:40 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 27 Feb 2020 14:48:39 +01:00

x86/traps: Remove pointless irq enable from do_spurious_interrupt_bug()

That function returns immediately after conditionally reenabling interrupts which is more than pointless and requires the ASM code to disable interrupts again.

---
 arch/x86/kernel/traps.c | 1 -
 1 file changed, 1 deletion(-)
arch/x86/kernel/traps.c +0 -1
index 6ef00eb..474b8cb 100644
@@ -862,7 +862,6 @@ do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
 dotraplinkage void
 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
 {
-	cond_local_irq_enable(regs);
 }
 
 dotraplinkage void