Skip to contentSkip to conversation

x86/entry: Clean up entry code

PATCH Superseded Lai Jiangshan 3 participants 22 messages Mar 15, 2022 – Mar 18, 2022

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover x86/entry: Clean up entry code

From: Lai Jiangshan <[email protected]>

This patchset moves the stack-switch code to the place where error_entry() return, unravels error_entry() from XENpv and makes entry_INT80_compat use idtentry macro.

This patchset is highly related to XENpv, because it does the extra cleanup to convert SWAPGS to swapgs after major cleanup is done.

The patches are the third try to pick patches from the patchset
https://lore.kernel.org/lkml/[email protected]/
which converts ASM code to C code. These patches are prepared for that purpose. But this patchset has it own value: it simplifies the stack switch, avoids leaving the old stack inside a function call, and separates XENpv code with native code without adding new code.

Changed from V2:
Make the patch of folding int80 thing as the first patch Add more changelog in "Switch the stack after error_entry() returns"

Changed from V1
Squash cleanup patches converting SWAPGS to swapgs into one patch

Use my official email address (Ant Group). The work is backed by my company and I was incorrectly misunderstood that [email protected] is the only portal for opensource work in the corporate group.

[V2]: https://lore.kernel.org/lkml/[email protected]/
[V1]: https://lore.kernel.org/lkml/[email protected]/

Lai Jiangshan (7):
x86/entry: Use idtentry macro for entry_INT80_compat x86/traps: Move pt_regs only in fixup_bad_iret() x86/entry: Switch the stack after error_entry() returns x86/entry: move PUSH_AND_CLEAR_REGS out of error_entry x86/entry: Move cld to the start of idtentry x86/entry: Don't call error_entry for XENPV x86/entry: Convert SWAPGS to swapgs and remove the definition of
SWAPGS

 arch/x86/entry/entry_64.S        |  61 +++++++++++++-----
 arch/x86/entry/entry_64_compat.S | 105 +------------------------------
 arch/x86/include/asm/idtentry.h  |  47 ++++++++++++++
 arch/x86/include/asm/irqflags.h  |   8 ---
 arch/x86/include/asm/proto.h     |   4 --
 arch/x86/include/asm/traps.h     |   2 +-
 arch/x86/kernel/traps.c          |  17 ++---
 7 files changed, 100 insertions(+), 144 deletions(-)
Signature · 1 line
2.19.1.6.gb485710b
#1 lore raw

PATCH v3 1/7 x86/entry: Use idtentry macro for entry_INT80_compat

From: Lai Jiangshan <[email protected]>

entry_INT80_compat is identical to idtentry macro except a special handling for %rax in the prolog.

Add the prolog to idtentry and use idtentry for entry_INT80_compat.

---
 arch/x86/entry/entry_64.S        |  18 ++++++
 arch/x86/entry/entry_64_compat.S | 103 -------------------------------
 arch/x86/include/asm/idtentry.h  |  47 ++++++++++++++
 arch/x86/include/asm/proto.h     |   4 --
 4 files changed, 65 insertions(+), 107 deletions(-)
arch/x86/entry/entry_64.S +18 -0
index cb5b7348b9b8..47ed97cbf46c 100644
@@ -360,6 +360,24 @@ SYM_CODE_START(\asmsym)
 		pushq	$-1			/* ORIG_RAX: no syscall to restart */
 	.endif
 
+	.if \vector == IA32_SYSCALL_VECTOR
+		/*
+		 * User tracing code (ptrace or signal handlers) might assume
+		 * that the saved RAX contains a 32-bit number when we're
+		 * invoking a 32-bit syscall.  Just in case the high bits are
+		 * nonzero, zero-extend the syscall number.  (This could almost
+		 * certainly be deleted with no ill effects.)
+		 */
+		movl	%eax, %eax
+
+		/*
+		 * do_int80_syscall_32() expects regs->orig_ax to be user ax,
+		 * and regs->ax to be $-ENOSYS.
+		 */
+		movq	%rax, (%rsp)
+		movq	$-ENOSYS, %rax
+	.endif
+
 	.if \vector == X86_TRAP_BP
 		/*
 		 * If coming from kernel space, create a 6-word gap to allow the
arch/x86/entry/entry_64_compat.S +0 -103

106 lines not on this page · raw message

arch/x86/include/asm/idtentry.h +47 -0

65 lines not on this page · raw message

arch/x86/include/asm/proto.h +0 -4
index feed36d44d04..c4d331fe65ff 100644
@@ -28,10 +28,6 @@ void entry_SYSENTER_compat(void);
 void __end_entry_SYSENTER_compat(void);
 void entry_SYSCALL_compat(void);
 void entry_SYSCALL_compat_safe_stack(void);
-void entry_INT80_compat(void);
-#ifdef CONFIG_XEN_PV
-void xen_entry_INT80_compat(void);
-#endif
 #endif
 
 void x86_configure_nx(void);
Signature · 1 line
2.19.1.6.gb485710b
#2 lore raw

Re: [PATCH V3 1/7] x86/entry: Use idtentry macro for entry_INT80_compat

Quoted 6 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

entry_INT80_compat is identical to idtentry macro except a special handling for %rax in the prolog.

Add the prolog to idtentry and use idtentry for entry_INT80_compat.

I'm struggling to see the Xen case. Currently it has:

arch/x86/entry/entry_64_compat.S sysret32_from_system_call: · line 371

Quoted 2 lines
arch/x86/entry/entry_64_compat.S +0 -2
-	/* In the Xen PV case we already run on the thread stack. */
-	ALTERNATIVE "", "jmp .Lint80_keep_stack", X86_FEATURE_XENPV

But idtentry's error_entry() does not have that afaict.

#3 lore raw

PATCH v3 2/7 x86/traps: Move pt_regs only in fixup_bad_iret()

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

fixup_bad_iret() and sync_regs() have similar arguments and do similar work that copies full or partial pt_regs to a place and switches stack after return. They are quite the same, but fixup_bad_iret() not only copies the pt_regs but also the return address of error_entry() while sync_regs() copies the pt_regs only and the return address of error_entry() was preserved and handled in ASM code.

This patch makes fixup_bad_iret() work like sync_regs() and the handling of the return address of error_entry() is moved in ASM code.

It removes the need to use the struct bad_iret_stack, simplifies fixup_bad_iret() and makes the ASM error_entry() call fixup_bad_iret() as the same as calling sync_regs() which adds readability because the calling patterns are exactly the same.

It is prepared for later patch to do the stack switch after the error_entry() which simplifies the code further.

---
 arch/x86/entry/entry_64.S    |  5 ++++-
 arch/x86/include/asm/traps.h |  2 +-
 arch/x86/kernel/traps.c      | 17 ++++++-----------
 3 files changed, 11 insertions(+), 13 deletions(-)
arch/x86/entry/entry_64.S +4 -1
index 47ed97cbf46c..37505331b7f1 100644
@@ -1073,9 +1073,12 @@ SYM_CODE_START_LOCAL(error_entry)
 	 * Pretend that the exception came from user mode: set up pt_regs
 	 * as if we faulted immediately after IRET.
 	 */
-	mov	%rsp, %rdi
+	popq	%r12				/* save return addr in %12 */
+	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
 	call	fixup_bad_iret
 	mov	%rax, %rsp
+	ENCODE_FRAME_POINTER
+	pushq	%r12
 	jmp	.Lerror_entry_from_usermode_after_swapgs
 SYM_CODE_END(error_entry)
 
arch/x86/include/asm/traps.h +1 -1
index 35317c5c551d..47ecfff2c83d 100644
@@ -13,7 +13,7 @@
 #ifdef CONFIG_X86_64
 asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs);
 asmlinkage __visible notrace
-struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s);
+struct pt_regs *fixup_bad_iret(struct pt_regs *bad_regs);
 void __init trap_init(void);
 asmlinkage __visible noinstr struct pt_regs *vc_switch_off_ist(struct pt_regs *eregs);
 #endif
arch/x86/kernel/traps.c +6 -11
index 1563fb995005..9fe9cd9d3eeb 100644
@@ -892,13 +892,8 @@ asmlinkage __visible noinstr struct pt_regs *vc_switch_off_ist(struct pt_regs *r
 }
 #endif
 
-struct bad_iret_stack {
-	void *error_entry_ret;
-	struct pt_regs regs;
-};
-
 asmlinkage __visible noinstr
-struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
+struct pt_regs *fixup_bad_iret(struct pt_regs *bad_regs)
 {
 	/*
 	 * This is called from entry_64.S early in handling a fault
@@ -908,19 +903,19 @@ struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
 	 * just below the IRET frame) and we want to pretend that the
 	 * exception came from the IRET target.
 	 */
-	struct bad_iret_stack tmp, *new_stack =
-		(struct bad_iret_stack *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
+	struct pt_regs tmp, *new_stack =
+		(struct pt_regs *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
 
 	/* Copy the IRET target to the temporary storage. */
-	__memcpy(&tmp.regs.ip, (void *)s->regs.sp, 5*8);
+	__memcpy(&tmp.ip, (void *)bad_regs->sp, 5*8);
 
 	/* Copy the remainder of the stack from the current stack. */
-	__memcpy(&tmp, s, offsetof(struct bad_iret_stack, regs.ip));
+	__memcpy(&tmp, bad_regs, offsetof(struct pt_regs, ip));
 
 	/* Update the entry stack */
 	__memcpy(new_stack, &tmp, sizeof(tmp));
 
-	BUG_ON(!user_mode(&new_stack->regs));
+	BUG_ON(!user_mode(new_stack));
 	return new_stack;
 }
 #endif
Signature · 1 line
2.19.1.6.gb485710b
#4 lore raw

PATCH v3 3/7 x86/entry: Switch the stack after error_entry() returns

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

error_entry() calls sync_regs() to settle/copy the pt_regs and switches the stack directly after sync_regs(). But error_entry() itself is also a function call, the switching has to handle the return address of it together, which causes the work complicated and tangly.

Switching to the stack after error_entry() makes the code simpler and intuitive.

The behavior/logic is unchanged:
1) (opt) feed fixup_bad_iret() with the pt_regs pushed by ASM code
2) (opt) fixup_bad_iret() moves the partial pt_regs up
3) feed sync_regs() with the pt_regs pushed by ASM code or returned by fixup_bad_iret()
4) sync_regs() copies the whole pt_regs to kernel stack if needed
5) after error_entry() and switching %rsp, it is in kernel stack with the pt_regs

Changes only in calling:
Old code switches to copied pt_regs immediately twice in error_entry() while new code switches to the copied pt_regs only once after error_entry() returns.
It is correct since sync_regs() doesn't need to be called close to the pt_regs it handles.

Old code stashes the return-address of error_entry() in a scratch register and new code doesn't stash it.
It relies on the fact that fixup_bad_iret() and sync_regs() don't corrupt the return-address of error_entry() on the stack. But even the old code also relies on the fact that fixup_bad_iret() and sync_regs() don't corrupt the return-address of themselves. They are the same reliances and are assured.

After this change, error_entry() will not do fancy things with the stack except when in the prolog which will be fixed in the next patch ("move PUSH_AND_CLEAR_REGS out of error_entry"). This patch and the next patch can't be swapped because the next patch relies on this patch's stopping fiddling with the return-address of error_entry(), otherwise the objtool would complain.

---
 arch/x86/entry/entry_64.S | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
arch/x86/entry/entry_64.S +6 -10
index 37505331b7f1..7768cdd0c7ed 100644
@@ -326,6 +326,8 @@ SYM_CODE_END(ret_from_fork)
 .macro idtentry_body cfunc has_error_code:req
 
 	call	error_entry
+	movq	%rax, %rsp			/* switch stack settled by sync_regs() */
+	ENCODE_FRAME_POINTER
 	UNWIND_HINT_REGS
 
 	movq	%rsp, %rdi			/* pt_regs pointer into 1st argument*/
@@ -1014,14 +1016,10 @@ SYM_CODE_START_LOCAL(error_entry)
 	/* We have user CR3.  Change to kernel CR3. */
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
 
+	leaq	8(%rsp), %rdi			/* arg0 = pt_regs pointer */
 .Lerror_entry_from_usermode_after_swapgs:
 	/* Put us onto the real thread stack. */
-	popq	%r12				/* save return addr in %12 */
-	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
 	call	sync_regs
-	movq	%rax, %rsp			/* switch stack */
-	ENCODE_FRAME_POINTER
-	pushq	%r12
 	RET
 
 	/*
@@ -1053,6 +1051,7 @@ SYM_CODE_START_LOCAL(error_entry)
 	 */
 .Lerror_entry_done_lfence:
 	FENCE_SWAPGS_KERNEL_ENTRY
+	leaq	8(%rsp), %rax			/* return pt_regs pointer */
 	RET
 
 .Lbstep_iret:
@@ -1073,12 +1072,9 @@ SYM_CODE_START_LOCAL(error_entry)
 	 * Pretend that the exception came from user mode: set up pt_regs
 	 * as if we faulted immediately after IRET.
 	 */
-	popq	%r12				/* save return addr in %12 */
-	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
+	leaq	8(%rsp), %rdi			/* arg0 = pt_regs pointer */
 	call	fixup_bad_iret
-	mov	%rax, %rsp
-	ENCODE_FRAME_POINTER
-	pushq	%r12
+	mov	%rax, %rdi
 	jmp	.Lerror_entry_from_usermode_after_swapgs
 SYM_CODE_END(error_entry)
 
Signature · 1 line
2.19.1.6.gb485710b
#5 lore raw

Re: [PATCH V3 3/7] x86/entry: Switch the stack after error_entry() returns

Quoted 2 lines · Lai Jiangshan

.Lerror_entry_from_usermode_after_swapgs:
/* Put us onto the real thread stack. */

This comment is no longer correct.

Signature · 1 line
Josh
#6 lore raw

Re: [PATCH V3 3/7] x86/entry: Switch the stack after error_entry() returns

Quoted 6 lines · Josh Poimboeuf
Quoted 2 lines · Lai Jiangshan

.Lerror_entry_from_usermode_after_swapgs:

      /* Put us onto the real thread stack. */

This comment is no longer correct.

Thanks for the review.

I thought "us" is the pt_regs. But it can be everything including the "running environment" and "running stack".

I will change "us" to "the pt_regs"

Quoted 4 lines

--
Josh

#7 lore raw

Re: [PATCH V3 3/7] x86/entry: Switch the stack after error_entry() returns

In reply to Lai Jiangshan
Quoted 87 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

error_entry() calls sync_regs() to settle/copy the pt_regs and switches the stack directly after sync_regs(). But error_entry() itself is also a function call, the switching has to handle the return address of it together, which causes the work complicated and tangly.

Switching to the stack after error_entry() makes the code simpler and intuitive.

The behavior/logic is unchanged:
1) (opt) feed fixup_bad_iret() with the pt_regs pushed by ASM code
2) (opt) fixup_bad_iret() moves the partial pt_regs up
3) feed sync_regs() with the pt_regs pushed by ASM code or returned by fixup_bad_iret()
4) sync_regs() copies the whole pt_regs to kernel stack if needed
5) after error_entry() and switching %rsp, it is in kernel stack with the pt_regs

Changes only in calling:
Old code switches to copied pt_regs immediately twice in error_entry() while new code switches to the copied pt_regs only once after error_entry() returns.
It is correct since sync_regs() doesn't need to be called close to the pt_regs it handles.

Old code stashes the return-address of error_entry() in a scratch register and new code doesn't stash it.
It relies on the fact that fixup_bad_iret() and sync_regs() don't corrupt the return-address of error_entry() on the stack. But even the old code also relies on the fact that fixup_bad_iret() and sync_regs() don't corrupt the return-address of themselves. They are the same reliances and are assured.

After this change, error_entry() will not do fancy things with the stack except when in the prolog which will be fixed in the next patch ("move PUSH_AND_CLEAR_REGS out of error_entry"). This patch and the next patch can't be swapped because the next patch relies on this patch's stopping fiddling with the return-address of error_entry(), otherwise the objtool would complain.

---
 arch/x86/entry/entry_64.S | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
arch/x86/entry/entry_64.S +4 -5
index 37505331b7f1..7768cdd0c7ed 100644
@@ -326,6 +326,8 @@ SYM_CODE_END(ret_from_fork)
 .macro idtentry_body cfunc has_error_code:req
 
 	call	error_entry
+	movq	%rax, %rsp			/* switch stack settled by sync_regs() */
+	ENCODE_FRAME_POINTER
 	UNWIND_HINT_REGS
 
 	movq	%rsp, %rdi			/* pt_regs pointer into 1st argument*/
@@ -1014,14 +1016,10 @@ SYM_CODE_START_LOCAL(error_entry)
 	/* We have user CR3.  Change to kernel CR3. */
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
 
+	leaq	8(%rsp), %rdi			/* arg0 = pt_regs pointer */
 .Lerror_entry_from_usermode_after_swapgs:
 	/* Put us onto the real thread stack. */
-	popq	%r12				/* save return addr in %12 */
-	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
 	call	sync_regs
-	movq	%rax, %rsp			/* switch stack */
-	ENCODE_FRAME_POINTER
-	pushq	%r12
 	RET
 
 	/*
@@ -1053,6 +1051,7 @@ SYM_CODE_START_LOCAL(error_entry)
 	 */
 .Lerror_entry_done_lfence:
 	FENCE_SWAPGS_KERNEL_ENTRY
+	leaq	8(%rsp), %rax			/* return pt_regs pointer */
 	RET
 
 .Lbstep_iret:
@@ -1073,12 +1072,9 @@ SYM_CODE_START_LOCAL(error_entry)
 	 * Pretend that the exception came from user mode: set up pt_regs
 	 * as if we faulted immediately after IRET.
 	 */

arch/x86/entry/entry_64.S SYM_CODE_START_LOCAL(error_entry) · line 1079

Quoted 10 lines
arch/x86/entry/entry_64.S +2 -5
-	popq	%r12				/* save return addr in %12 */
-	movq	%rsp, %rdi			/* arg0 = pt_regs pointer */
+	leaq	8(%rsp), %rdi			/* arg0 = pt_regs pointer */
 	call	fixup_bad_iret
-	mov	%rax, %rsp
-	ENCODE_FRAME_POINTER
-	pushq	%r12
+	mov	%rax, %rdi
 	jmp	.Lerror_entry_from_usermode_after_swapgs
 SYM_CODE_END(error_entry)

So the new Changelog doesn't seem to help me much. But looking at both fixup_bad_iret() and sync_regs(), they both have:

__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1

as hard-coded destination. Now, fixup_bad_iret() sets up a complete ptregs there and then returns a pointer to this stack.

sync_regs otoh, does a straight up pt_regs sized copy from arg0 to this new stack.

Therefore it appears to me that doing sync_regs() after fixup_bad_iret() is a complete NO-OP and only confuses things further.

Would not something like the below clarify things?

arch/x86/entry/entry_64.S +7 -2
@@ -1004,6 +1004,7 @@ SYM_CODE_START_LOCAL(error_entry)
 .Lerror_entry_from_usermode_after_swapgs:
 	/* Put us onto the real thread stack. */
 	call	sync_regs
+.Lerror_entry_from_usermode_after_sync_regs:
 	RET
 
 	/*
@@ -1058,8 +1059,12 @@ SYM_CODE_START_LOCAL(error_entry)
 	 */
 	leaq	8(%rsp), %rdi			/* arg0 = pt_regs pointer */
 	call	fixup_bad_iret
-	mov	%rax, %rdi
-	jmp	.Lerror_entry_from_usermode_after_swapgs
+	/*
+	 * fixup_bad_iret() will have setup pt_regs on the thread stack, and
+	 * returns a pointer to that stack exactly like sync_regs() would've
+	 * done. As such, calling sync_regs again makes no sense.
+	 */
+	jmp	.Lerror_entry_from_usermode_after_sync_regs
 SYM_CODE_END(error_entry)
 
 SYM_CODE_START_LOCAL(error_return)
#8 lore raw

Re: [PATCH V3 3/7] x86/entry: Switch the stack after error_entry() returns

Quoted 7 lines · Peter Zijlstra
Quoted 2 lines

SYM_CODE_END(error_entry)

So the new Changelog doesn't seem to help me much. But looking at both fixup_bad_iret() and sync_regs(), they both have:

__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1

For a long time in old days (before KPTI), tss.sp0 does be the kernel thread stack, but now is not the kernel thread stack, rather it is the trampoline stack (or entry stack) in the cpu entry area.

And bad IRET can happen when doing IRET to return to userspace and it is also the trampoline stack. So fixup_bad_iret() is really just moving the lower partial pt_regs up to concat with user IRET frame head in the entry stack.

So fixup_bad_iret() will NOT have setup pt_regs on the thread stack. And sync_regs() is needed after fixup_bad_iret()

The patch
https://lore.kernel.org/lkml/[email protected]/
tried changing fixup_bad_iret() to copy the pt_regs directly to the kernel stack.

And in the V1 of the patchset that converting ASM to code also tried it:
https://lore.kernel.org/lkml/[email protected]/

And in the current patchset, it focuses on ASM code only. I don't think we need to change it. It would be much simpler to change the behavior of fixup_bad_iret() when error_entry() is converted to C.

Quoted 37 lines

as hard-coded destination. Now, fixup_bad_iret() sets up a complete ptregs there and then returns a pointer to this stack.

sync_regs otoh, does a straight up pt_regs sized copy from arg0 to this new stack.

Therefore it appears to me that doing sync_regs() after fixup_bad_iret() is a complete NO-OP and only confuses things further.

Would not something like the below clarify things?

arch/x86/entry/entry_64.S +7 -2
@@ -1004,6 +1004,7 @@ SYM_CODE_START_LOCAL(error_entry)
 .Lerror_entry_from_usermode_after_swapgs:
        /* Put us onto the real thread stack. */
        call    sync_regs
+.Lerror_entry_from_usermode_after_sync_regs:
        RET
 
        /*
@@ -1058,8 +1059,12 @@ SYM_CODE_START_LOCAL(error_entry)
         */
        leaq    8(%rsp), %rdi                   /* arg0 = pt_regs pointer */
        call    fixup_bad_iret
-       mov     %rax, %rdi
-       jmp     .Lerror_entry_from_usermode_after_swapgs
+       /*
+        * fixup_bad_iret() will have setup pt_regs on the thread stack, and
+        * returns a pointer to that stack exactly like sync_regs() would've
+        * done. As such, calling sync_regs again makes no sense.
+        */
+       jmp     .Lerror_entry_from_usermode_after_sync_regs
 SYM_CODE_END(error_entry)
 
 SYM_CODE_START_LOCAL(error_return)
#9 lore raw

Re: [PATCH V3 3/7] x86/entry: Switch the stack after error_entry() returns

Quoted 9 lines · Lai Jiangshan
Quoted 7 lines · Peter Zijlstra
Quoted 2 lines

SYM_CODE_END(error_entry)

So the new Changelog doesn't seem to help me much. But looking at both fixup_bad_iret() and sync_regs(), they both have:

__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1

Yes, I can't read :/ Functions don't both use that.

Your patch is correct, not switching the stack in between runs sync_regs from another stack, but the end result should be the same.

#10 lore raw

PATCH v3 4/7 x86/entry: move PUSH_AND_CLEAR_REGS out of error_entry

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

Moving PUSH_AND_CLEAR_REGS out of error_entry doesn't change any functionality.

It makes error_entry() do not fiddle with the stack.

It will enlarge the size:

size arch/x86/entry/entry_64.o.before:
text data bss dec hex filename
17916 384 0 18300 477c arch/x86/entry/entry_64.o

size --format=SysV arch/x86/entry/entry_64.o.before:

.entry.text                      5528      0
.orc_unwind                      6456      0
.orc_unwind_ip                   4304      0

size arch/x86/entry/entry_64.o.after:
text data bss dec hex filename
26868 384 0 27252 6a74 arch/x86/entry/entry_64.o

size --format=SysV arch/x86/entry/entry_64.o.after:

.entry.text                      8200      0
.orc_unwind                     10224      0
.orc_unwind_ip                   6816      0

But .entry.text in x86_64 is 2M aligned, enlarging it to 8.2k doesn't enlarge the final text size.

The tables .orc_unwind[_ip] are enlarged due to it adds many pushes.

It is prepared for not calling error_entry() from XENPV in later patch and for future converting the whole error_entry into C code.

---
 arch/x86/entry/entry_64.S | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/entry/entry_64.S +3 -2
index 7768cdd0c7ed..903da9119e7a 100644
@@ -325,6 +325,9 @@ SYM_CODE_END(ret_from_fork)
  */
 .macro idtentry_body cfunc has_error_code:req
 
+	PUSH_AND_CLEAR_REGS
+	ENCODE_FRAME_POINTER
+
 	call	error_entry
 	movq	%rax, %rsp			/* switch stack settled by sync_regs() */
 	ENCODE_FRAME_POINTER
@@ -1002,8 +1005,6 @@ SYM_CODE_END(paranoid_exit)
 SYM_CODE_START_LOCAL(error_entry)
 	UNWIND_HINT_FUNC
 	cld
-	PUSH_AND_CLEAR_REGS save_ret=1
-	ENCODE_FRAME_POINTER 8
 	testb	$3, CS+8(%rsp)
 	jz	.Lerror_kernelspace
 
Signature · 1 line
2.19.1.6.gb485710b
#11 lore raw

Re: [PATCH V3 4/7] x86/entry: move PUSH_AND_CLEAR_REGS out of error_entry

Quoted 22 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

Moving PUSH_AND_CLEAR_REGS out of error_entry doesn't change any functionality.

It makes error_entry() do not fiddle with the stack.

It will enlarge the size:

size arch/x86/entry/entry_64.o.before:
text data bss dec hex filename
17916 384 0 18300 477c arch/x86/entry/entry_64.o

size --format=SysV arch/x86/entry/entry_64.o.before:

.entry.text                      5528      0
.orc_unwind                      6456      0
.orc_unwind_ip                   4304      0

size arch/x86/entry/entry_64.o.after:
text data bss dec hex filename
26868 384 0 27252 6a74 arch/x86/entry/entry_64.o

Quoted 7 lines

size --format=SysV arch/x86/entry/entry_64.o.after:

.entry.text                      8200      0
.orc_unwind                     10224      0
.orc_unwind_ip                   6816      0

But .entry.text in x86_64 is 2M aligned, enlarging it to 8.2k doesn't enlarge the final text size.

So I don't much care about the orc data, that's only used when unwinding, a relatively rare occasion, anyway. But the text increase does bother me a little, this can blow up I$ misses on syscall heavy workloads.

#12 lore raw

Re: [PATCH V3 4/7] x86/entry: move PUSH_AND_CLEAR_REGS out of error_entry

Quoted 5 lines · Peter Zijlstra

So I don't much care about the orc data, that's only used when unwinding, a relatively rare occasion, anyway. But the text increase does bother me a little, this can blow up I$ misses on syscall heavy workloads.

It has nothing to do with the syscall.

It does add a lot in I$. But it is fortunate that the footprint is the same (before and after the patch) for a single exception vector. For example heavy #PF workloads.

Footprint will increase after this patch when two or more exception vectors happen heavily at the same time, for example when #PF and timer are occurring heavily.

We can put PUSH_AND_CLEAR_REGS in a single function to reduce the footprint for heavy multi-exception workloads. What do you think?

#13 lore raw

PATCH v3 5/7 x86/entry: Move cld to the start of idtentry

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

Make it next to CLAC

---
 arch/x86/entry/entry_64.S | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/entry/entry_64.S +5 -3
index 903da9119e7a..e4a07276fd1c 100644
@@ -360,6 +360,7 @@ SYM_CODE_START(\asmsym)
 	UNWIND_HINT_IRET_REGS offset=\has_error_code*8
 	ENDBR
 	ASM_CLAC
+	cld
 
 	.if \has_error_code == 0
 		pushq	$-1			/* ORIG_RAX: no syscall to restart */
@@ -446,6 +447,7 @@ SYM_CODE_START(\asmsym)
 	UNWIND_HINT_IRET_REGS
 	ENDBR
 	ASM_CLAC
+	cld
 
 	pushq	$-1			/* ORIG_RAX: no syscall to restart */
 
@@ -502,6 +504,7 @@ SYM_CODE_START(\asmsym)
 	UNWIND_HINT_IRET_REGS
 	ENDBR
 	ASM_CLAC
+	cld
 
 	/*
 	 * If the entry is from userspace, switch stacks and treat it as
@@ -564,6 +567,7 @@ SYM_CODE_START(\asmsym)
 	UNWIND_HINT_IRET_REGS offset=8
 	ENDBR
 	ASM_CLAC
+	cld
 
 	/* paranoid_entry returns GS information for paranoid_exit in EBX. */
 	call	paranoid_entry
@@ -886,7 +890,6 @@ SYM_CODE_END(xen_failsafe_callback)
  */
 SYM_CODE_START_LOCAL(paranoid_entry)
 	UNWIND_HINT_FUNC
-	cld
 	PUSH_AND_CLEAR_REGS save_ret=1
 	ENCODE_FRAME_POINTER 8
 
@@ -1004,7 +1007,6 @@ SYM_CODE_END(paranoid_exit)
  */
 SYM_CODE_START_LOCAL(error_entry)
 	UNWIND_HINT_FUNC
-	cld
 	testb	$3, CS+8(%rsp)
 	jz	.Lerror_kernelspace
 
@@ -1138,6 +1140,7 @@ SYM_CODE_START(asm_exc_nmi)
 	 */
 
 	ASM_CLAC
+	cld
 
 	/* Use %rdx as our temp variable throughout */
 	pushq	%rdx
@@ -1157,7 +1160,6 @@ SYM_CODE_START(asm_exc_nmi)
 	 */
 
 	swapgs
-	cld
 	FENCE_SWAPGS_USER_ENTRY
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
 	movq	%rsp, %rdx
Signature · 1 line
2.19.1.6.gb485710b
#14 lore raw

PATCH v3 6/7 x86/entry: Don't call error_entry for XENPV

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

When in XENPV, it is already in the task stack, and it can't fault for native_iret() nor native_load_gs_index() since XENPV uses its own pvops for iret and load_gs_index(). And it doesn't need to switch CR3. So there is no reason to call error_entry() in XENPV.

---
 arch/x86/entry/entry_64.S | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/entry/entry_64.S +11 -2
index e4a07276fd1c..ec885c2107de 100644
@@ -328,8 +328,17 @@ SYM_CODE_END(ret_from_fork)
 	PUSH_AND_CLEAR_REGS
 	ENCODE_FRAME_POINTER
 
-	call	error_entry
-	movq	%rax, %rsp			/* switch stack settled by sync_regs() */
+	/*
+	 * Call error_entry and switch stack settled by sync_regs().
+	 *
+	 * When in XENPV, it is already in the task stack, and it can't fault
+	 * for native_iret() nor native_load_gs_index() since XENPV uses its
+	 * own pvops for iret and load_gs_index().  And it doesn't need to
+	 * switch CR3.  So it can skip invoking error_entry().
+	 */
+	ALTERNATIVE "call error_entry; movq %rax, %rsp", \
+		"", X86_FEATURE_XENPV
+
 	ENCODE_FRAME_POINTER
 	UNWIND_HINT_REGS
 
Signature · 1 line
2.19.1.6.gb485710b
#15 lore raw

Re: [PATCH V3 6/7] x86/entry: Don't call error_entry for XENPV

Quoted 10 lines · Lai Jiangshan

+++ b/arch/x86/entry/entry_64.S

arch/x86/entry/entry_64.S +3 -2
@@ -328,8 +328,17 @@ SYM_CODE_END(ret_from_fork)
 	PUSH_AND_CLEAR_REGS
 	ENCODE_FRAME_POINTER
 
-	call	error_entry
-	movq	%rax, %rsp			/* switch stack settled by sync_regs() */
+	/*
+	 * Call error_entry and switch stack settled by sync_regs().
+	 *

arch/x86/entry/entry_64.S SYM_CODE_END(ret_from_fork) · line 343

Quoted 10 lines
arch/x86/entry/entry_64.S +8 -0
+	 * When in XENPV, it is already in the task stack, and it can't fault
+	 * for native_iret() nor native_load_gs_index() since XENPV uses its
+	 * own pvops for iret and load_gs_index().  And it doesn't need to
+	 * switch CR3.  So it can skip invoking error_entry().
+	 */
+	ALTERNATIVE "call error_entry; movq %rax, %rsp", \
+		"", X86_FEATURE_XENPV
+
 	ENCODE_FRAME_POINTER
 	UNWIND_HINT_REGS

The second ENCODE_FRAME_POINTER is only needed for the stack-switching (non-XENPV) case. The second UNWIND_HINT_REGS shouldn't be needed at all, since the first one still applies after the call.

How about something more readable like this?

.macro idtentry_body cfunc has_error_code:req

PUSH_AND_CLEAR_REGS
ENCODE_FRAME_POINTER

#ifdef CONFIG_XEN_PV
/*
* When in XENPV, it is already in the task stack, and it can't fault
* for native_iret() nor native_load_gs_index() since XENPV uses its
* own pvops for iret and load_gs_index(). And it doesn't need to
* switch CR3. So it can skip invoking error_entry().
*/
ALTERNATIVE "", "jmp 1f", X86_FEATURE_XENPV
#endif

call error_entry
movq %rax, %rsp
ENCODE_FRAME_POINTER
1:
movq %rsp, %rdi /* pt_regs pointer into 1st argument*/

Signature · 1 line
Josh
#16 lore raw

Re: [PATCH V3 6/7] x86/entry: Don't call error_entry for XENPV

In reply to Lai Jiangshan
Quoted 26 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

When in XENPV, it is already in the task stack, and it can't fault for native_iret() nor native_load_gs_index() since XENPV uses its own pvops for iret and load_gs_index(). And it doesn't need to switch CR3. So there is no reason to call error_entry() in XENPV.

---
 arch/x86/entry/entry_64.S | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/entry/entry_64.S +4 -2
index e4a07276fd1c..ec885c2107de 100644
@@ -328,8 +328,17 @@ SYM_CODE_END(ret_from_fork)
 	PUSH_AND_CLEAR_REGS
 	ENCODE_FRAME_POINTER
 
-	call	error_entry
-	movq	%rax, %rsp			/* switch stack settled by sync_regs() */
+	/*
+	 * Call error_entry and switch stack settled by sync_regs().
+	 *
+	 * When in XENPV, it is already in the task stack, and it can't fault

arch/x86/entry/entry_64.S SYM_CODE_END(ret_from_fork) · line 344

Quoted 10 lines
arch/x86/entry/entry_64.S +7 -0
+	 * for native_iret() nor native_load_gs_index() since XENPV uses its
+	 * own pvops for iret and load_gs_index().  And it doesn't need to
+	 * switch CR3.  So it can skip invoking error_entry().
+	 */
+	ALTERNATIVE "call error_entry; movq %rax, %rsp", \
+		"", X86_FEATURE_XENPV
+
 	ENCODE_FRAME_POINTER
 	UNWIND_HINT_REGS
 

Oooh, here we go, this is the answer to my question for patch #1, a note in the changelog might be nice. Something like:

"This looses a Xen PV optimization, which will be restored in a later patch. The superfluous stack switch is just that."

#17 lore raw

Re: [PATCH V3 6/7] x86/entry: Don't call error_entry for XENPV

Quoted 42 lines · Peter Zijlstra
Quoted 26 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

When in XENPV, it is already in the task stack, and it can't fault for native_iret() nor native_load_gs_index() since XENPV uses its own pvops for iret and load_gs_index(). And it doesn't need to switch CR3. So there is no reason to call error_entry() in XENPV.

---
 arch/x86/entry/entry_64.S | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/entry/entry_64.S +4 -2
index e4a07276fd1c..ec885c2107de 100644
@@ -328,8 +328,17 @@ SYM_CODE_END(ret_from_fork)
      PUSH_AND_CLEAR_REGS
      ENCODE_FRAME_POINTER
 
-     call    error_entry
-     movq    %rax, %rsp                      /* switch stack settled by sync_regs() */
+     /*
+      * Call error_entry and switch stack settled by sync_regs().
+      *
+      * When in XENPV, it is already in the task stack, and it can't fault

arch/x86/entry/entry_64.S SYM_CODE_END(ret_from_fork) · line 344

Quoted 10 lines
arch/x86/entry/entry_64.S +7 -0
+      * for native_iret() nor native_load_gs_index() since XENPV uses its
+      * own pvops for iret and load_gs_index().  And it doesn't need to
+      * switch CR3.  So it can skip invoking error_entry().
+      */
+     ALTERNATIVE "call error_entry; movq %rax, %rsp", \
+             "", X86_FEATURE_XENPV
+
      ENCODE_FRAME_POINTER
      UNWIND_HINT_REGS
 

Oooh, here we go, this is the answer to my question for patch #1, a note in the changelog might be nice. Something like:

Quoted 2 lines

"This looses a Xen PV optimization, which will be restored in a later patch. The superfluous stack switch is just that."

In V2, the change of int80 thing is after this patch. Maybe that order of patches is more natural. I'm sorry to reorder them.

#18 lore raw

PATCH v3 7/7 x86/entry: Convert SWAPGS to swapgs and remove the definition of SWAPGS

In reply to Lai Jiangshan

From: Lai Jiangshan <[email protected]>

XENPV doesn't use swapgs_restore_regs_and_return_to_usermode(), error_entry() and entry_SYSENTER_compat(), so the PV-awared SWAPGS in them can be changed to swapgs. There is no user of the SWAPGS anymore after this change.

---
 arch/x86/entry/entry_64.S        | 6 +++---
 arch/x86/entry/entry_64_compat.S | 2 +-
 arch/x86/include/asm/irqflags.h  | 8 --------
 3 files changed, 4 insertions(+), 12 deletions(-)
arch/x86/entry/entry_64.S +3 -3
index ec885c2107de..e20eabaa56b8 100644
@@ -1023,7 +1023,7 @@ SYM_CODE_START_LOCAL(error_entry)
 	 * We entered from user mode or we're pretending to have entered
 	 * from user mode due to an IRET fault.
 	 */
-	SWAPGS
+	swapgs
 	FENCE_SWAPGS_USER_ENTRY
 	/* We have user CR3.  Change to kernel CR3. */
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
@@ -1055,7 +1055,7 @@ SYM_CODE_START_LOCAL(error_entry)
 	 * gsbase and proceed.  We'll fix up the exception and land in
 	 * .Lgs_change's error handler with kernel gsbase.
 	 */
-	SWAPGS
+	swapgs
 
 	/*
 	 * Issue an LFENCE to prevent GS speculation, regardless of whether it is a
@@ -1076,7 +1076,7 @@ SYM_CODE_START_LOCAL(error_entry)
 	 * We came from an IRET to user mode, so we have user
 	 * gsbase and CR3.  Switch to kernel gsbase and CR3:
 	 */
-	SWAPGS
+	swapgs
 	FENCE_SWAPGS_USER_ENTRY
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
 
arch/x86/entry/entry_64_compat.S +1 -1
index 2b7496a36f65..6866151bbef3 100644
@@ -50,7 +50,7 @@ SYM_CODE_START(entry_SYSENTER_compat)
 	UNWIND_HINT_EMPTY
 	ENDBR
 	/* Interrupts are off on entry. */
-	SWAPGS
+	swapgs
 
 	pushq	%rax
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
arch/x86/include/asm/irqflags.h +0 -8
index 111104d1c2cd..7793e52d6237 100644
@@ -137,14 +137,6 @@ static __always_inline void arch_local_irq_restore(unsigned long flags)
 	if (!arch_irqs_disabled_flags(flags))
 		arch_local_irq_enable();
 }
-#else
-#ifdef CONFIG_X86_64
-#ifdef CONFIG_XEN_PV
-#define SWAPGS	ALTERNATIVE "swapgs", "", X86_FEATURE_XENPV
-#else
-#define SWAPGS	swapgs
-#endif
-#endif
 #endif /* !__ASSEMBLY__ */
 
 #endif
Signature · 1 line
2.19.1.6.gb485710b
#19 lore raw
In reply to Lai Jiangshan
Quoted 9 lines · Lai Jiangshan

Lai Jiangshan (7):
x86/entry: Use idtentry macro for entry_INT80_compat x86/traps: Move pt_regs only in fixup_bad_iret() x86/entry: Switch the stack after error_entry() returns x86/entry: move PUSH_AND_CLEAR_REGS out of error_entry x86/entry: Move cld to the start of idtentry x86/entry: Don't call error_entry for XENPV x86/entry: Convert SWAPGS to swapgs and remove the definition of
SWAPGS

So AFAICT these patches are indeed correct.

I do however worry a little bit about the I$ impact of patch 4, and there's a few niggles, but otherwise looks good.

I'd love for some of the other x86 people to also look at this, but a tentative ACK on this.

#20 lore raw
Quoted 12 lines · Peter Zijlstra
Quoted 9 lines · Lai Jiangshan

Lai Jiangshan (7):
x86/entry: Use idtentry macro for entry_INT80_compat x86/traps: Move pt_regs only in fixup_bad_iret() x86/entry: Switch the stack after error_entry() returns x86/entry: move PUSH_AND_CLEAR_REGS out of error_entry x86/entry: Move cld to the start of idtentry x86/entry: Don't call error_entry for XENPV x86/entry: Convert SWAPGS to swapgs and remove the definition of
SWAPGS

Quoted 8 lines

So AFAICT these patches are indeed correct.

I do however worry a little bit about the I$ impact of patch 4, and there's a few niggles, but otherwise looks good.

I'd love for some of the other x86 people to also look at this, but a tentative ACK on this.

Also, I forgot to mention; they no longer apply cleanly because I sprinked ENDBR all over the place. Mostly trivial to fixup though.

#21 lore raw
Quoted 19 lines · Peter Zijlstra
Quoted 17 lines · Peter Zijlstra
Quoted 9 lines · Lai Jiangshan

Lai Jiangshan (7):
x86/entry: Use idtentry macro for entry_INT80_compat x86/traps: Move pt_regs only in fixup_bad_iret() x86/entry: Switch the stack after error_entry() returns x86/entry: move PUSH_AND_CLEAR_REGS out of error_entry x86/entry: Move cld to the start of idtentry x86/entry: Don't call error_entry for XENPV x86/entry: Convert SWAPGS to swapgs and remove the definition of
SWAPGS

So AFAICT these patches are indeed correct.

I do however worry a little bit about the I$ impact of patch 4, and there's a few niggles, but otherwise looks good.

Quoted 6 lines
Quoted 3 lines

I'd love for some of the other x86 people to also look at this, but a tentative ACK on this.

Also, I forgot to mention; they no longer apply cleanly because I sprinked ENDBR all over the place. Mostly trivial to fixup though.

They can still be applied to the newest tip/master which already has sprinked ENDBR. Is there a more proper branch for me to rebase the patches onto?