Skip to contentSkip to conversation

x86/Xen/32: xen_iret_crit_fixup adjustments

PATCH Applied Jan Beulich 4 participants 13 messages Nov 11, 2019 – Nov 20, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover x86/Xen/32: xen_iret_crit_fixup adjustments

The first patch here fixes another regression from 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs"), besides the one already addressed by
https://lists.xenproject.org/archives/html/xen-devel/2019-10/msg01988.html.
The second patch is a minimal bit of cleanup on top.

1: make xen_iret_crit_fixup independent of frame layout 2: simplify xen_iret_crit_fixup's ring check

Jan

#1 lore raw

PATCH 1/2 x86/Xen/32: make xen_iret_crit_fixup independent of frame layout

Now that SS:ESP always get saved by SAVE_ALL, this also needs to be accounted for in xen_iret_crit_fixup. Otherwise the old_ax value gets interpreted as EFLAGS, and hence VM86 mode appears to be active all the time, leading to random "vm86_32: no user_vm86: BAD" log messages alongside processes randomly crashing.

Since following the previous model (sitting after SAVE_ALL) would further complicate the code _and_ retain the dependency of xen_iret_crit_fixup on frame manipulations done by entry_32.S, switch things around and do the adjustment ahead of SAVE_ALL.

Fixes 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs")
arch/x86/entry/entry_32.S +9 -13
@@ -1341,11 +1341,6 @@ END(spurious_interrupt_bug)
 
 #ifdef CONFIG_XEN_PV
 ENTRY(xen_hypervisor_callback)
-	pushl	$-1				/* orig_ax = -1 => not a system call */
-	SAVE_ALL
-	ENCODE_FRAME_POINTER
-	TRACE_IRQS_OFF
-
 	/*
 	 * Check to see if we got the event in the critical
 	 * region in xen_iret_direct, after we've reenabled
@@ -1353,16 +1348,17 @@ ENTRY(xen_hypervisor_callback)
 	 * iret instruction's behaviour where it delivers a
 	 * pending interrupt when enabling interrupts:
 	 */
-	movl	PT_EIP(%esp), %eax
-	cmpl	$xen_iret_start_crit, %eax
+	cmpl	$xen_iret_start_crit, (%esp)
 	jb	1f
-	cmpl	$xen_iret_end_crit, %eax
+	cmpl	$xen_iret_end_crit, (%esp)
 	jae	1f
-
-	jmp	xen_iret_crit_fixup
-
-ENTRY(xen_do_upcall)
-1:	mov	%esp, %eax
+	call	xen_iret_crit_fixup
+1:
+	pushl	$-1				/* orig_ax = -1 => not a system call */
+	SAVE_ALL
+	ENCODE_FRAME_POINTER
+	TRACE_IRQS_OFF
+	mov	%esp, %eax
 	call	xen_evtchn_do_upcall
 #ifndef CONFIG_PREEMPTION
 	call	xen_maybe_preempt_hcall
arch/x86/xen/xen-asm_32.S +26 -44

102 lines not on this page · raw message

maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] x86/Xen/32: make xen_iret_crit_fixup independent of frame layout

Quoted 6 lines · Jan Beulich

Now that SS:ESP always get saved by SAVE_ALL, this also needs to be accounted for in xen_iret_crit_fixup. Otherwise the old_ax value gets interpreted as EFLAGS, and hence VM86 mode appears to be active all the time, leading to random "vm86_32: no user_vm86: BAD" log messages alongside processes randomly crashing.

Quoted 7 lines

Since following the previous model (sitting after SAVE_ALL) would further complicate the code _and_ retain the dependency of xen_iret_crit_fixup on frame manipulations done by entry_32.S, switch things around and do the adjustment ahead of SAVE_ALL.

Fixes 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs")

Juergen

merge bot Applied
#3 lore raw

[tip: x86/urgent] x86/xen/32: Make xen_iret_crit_fixup() independent of frame layout

In reply to Jan Beulich
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     29b810f5a5ec127d3143770098e05981baa3eb77
Gitweb:        https://git.kernel.org/tip/29b810f5a5ec127d3143770098e05981baa3eb77
Author:        Jan Beulich <[email protected]>
AuthorDate:    Mon, 11 Nov 2019 15:32:12 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 19 Nov 2019 21:58:28 +01:00

x86/xen/32: Make xen_iret_crit_fixup() independent of frame layout

Now that SS:ESP always get saved by SAVE_ALL, this also needs to be accounted for in xen_iret_crit_fixup(). Otherwise the old_ax value gets interpreted as EFLAGS, and hence VM86 mode appears to be active all the time, leading to random "vm86_32: no user_vm86: BAD" log messages alongside processes randomly crashing.

Since following the previous model (sitting after SAVE_ALL) would further complicate the code _and_ retain the dependency of xen_iret_crit_fixup() on frame manipulations done by entry_32.S, switch things around and do the adjustment ahead of SAVE_ALL.

Fixes 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs")
Cc Stable Team <[email protected]>
---
 arch/x86/entry/entry_32.S | 22 +++++--------
 arch/x86/xen/xen-asm_32.S | 66 +++++++++++++-------------------------
 2 files changed, 33 insertions(+), 55 deletions(-)
arch/x86/entry/entry_32.S +9 -13
index 3f847d8..019dbac 100644
@@ -1341,11 +1341,6 @@ END(spurious_interrupt_bug)
 
 #ifdef CONFIG_XEN_PV
 ENTRY(xen_hypervisor_callback)
-	pushl	$-1				/* orig_ax = -1 => not a system call */
-	SAVE_ALL
-	ENCODE_FRAME_POINTER
-	TRACE_IRQS_OFF
-
 	/*
 	 * Check to see if we got the event in the critical
 	 * region in xen_iret_direct, after we've reenabled
@@ -1353,16 +1348,17 @@ ENTRY(xen_hypervisor_callback)
 	 * iret instruction's behaviour where it delivers a
 	 * pending interrupt when enabling interrupts:
 	 */
-	movl	PT_EIP(%esp), %eax
-	cmpl	$xen_iret_start_crit, %eax
+	cmpl	$xen_iret_start_crit, (%esp)
 	jb	1f
-	cmpl	$xen_iret_end_crit, %eax
+	cmpl	$xen_iret_end_crit, (%esp)
 	jae	1f
-
-	jmp	xen_iret_crit_fixup
-
-ENTRY(xen_do_upcall)
-1:	mov	%esp, %eax
+	call	xen_iret_crit_fixup
+1:
+	pushl	$-1				/* orig_ax = -1 => not a system call */
+	SAVE_ALL
+	ENCODE_FRAME_POINTER
+	TRACE_IRQS_OFF
+	mov	%esp, %eax
 	call	xen_evtchn_do_upcall
 #ifndef CONFIG_PREEMPTION
 	call	xen_maybe_preempt_hcall
arch/x86/xen/xen-asm_32.S +24 -42

100 lines not on this page · raw message

#4 lore raw

PATCH 2/2 x86/Xen/32: simplify xen_iret_crit_fixup's ring check

In reply to Jan Beulich

This can be had with two instead of six insns, by just checking the high CS.RPL bit.

Also adjust the comment - there would be no #GP in the mentioned cases, as there's no segment limit violation or alike. Instead there'd be #PF, but that one reports the target EIP of said branch, not the address of the branch insn itself.

---
An alternative would be to keep using SEGMENT_RPL_MASK, but follow it with "jpe".

arch/x86/xen/xen-asm_32.S +4 -11
@@ -153,22 +153,15 @@ hyper_iret:
  * it's still on stack), we need to restore its value here.
  */
 ENTRY(xen_iret_crit_fixup)
-	pushl %ecx
 	/*
 	 * Paranoia: Make sure we're really coming from kernel space.
 	 * One could imagine a case where userspace jumps into the
 	 * critical range address, but just before the CPU delivers a
-	 * GP, it decides to deliver an interrupt instead.  Unlikely?
-	 * Definitely.  Easy to avoid?  Yes.  The Intel documents
-	 * explicitly say that the reported EIP for a bad jump is the
-	 * jump instruction itself, not the destination, but some
-	 * virtual environments get this wrong.
+	 * PF, it decides to deliver an interrupt instead.  Unlikely?
+	 * Definitely.  Easy to avoid?  Yes.
 	 */
-	movl 3*4(%esp), %ecx		/* nested CS */
-	andl $SEGMENT_RPL_MASK, %ecx
-	cmpl $USER_RPL, %ecx
-	popl %ecx
-	je 2f
+	testb $2, 2*4(%esp)		/* nested CS */
+	jnz 2f
 
 	/*
 	 * If eip is before iret_restore_end then stack
maintainer Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] x86/Xen/32: simplify xen_iret_crit_fixup's ring check

Quoted 9 lines · Jan Beulich

This can be had with two instead of six insns, by just checking the high CS.RPL bit.

Also adjust the comment - there would be no #GP in the mentioned cases, as there's no segment limit violation or alike. Instead there'd be #PF, but that one reports the target EIP of said branch, not the address of the branch insn itself.

Juergen

merge bot Applied
#6 lore raw

[tip: x86/urgent] x86/xen/32: Simplify ring check in xen_iret_crit_fixup()

In reply to Jan Beulich
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     922eea2ce5c799228d9ff1be9890e6873ce8fff6
Gitweb:        https://git.kernel.org/tip/922eea2ce5c799228d9ff1be9890e6873ce8fff6
Author:        Jan Beulich <[email protected]>
AuthorDate:    Mon, 11 Nov 2019 15:32:59 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 19 Nov 2019 21:58:28 +01:00

x86/xen/32: Simplify ring check in xen_iret_crit_fixup()

This can be had with two instead of six insns, by just checking the high CS.RPL bit.

Also adjust the comment - there would be no #GP in the mentioned cases, as there's no segment limit violation or alike. Instead there'd be #PF, but that one reports the target EIP of said branch, not the address of the branch insn itself.

---
 arch/x86/xen/xen-asm_32.S | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
arch/x86/xen/xen-asm_32.S +4 -11
index 392e033..cd17777 100644
@@ -153,22 +153,15 @@ hyper_iret:
  * it's still on stack), we need to restore its value here.
  */
 ENTRY(xen_iret_crit_fixup)
-	pushl %ecx
 	/*
 	 * Paranoia: Make sure we're really coming from kernel space.
 	 * One could imagine a case where userspace jumps into the
 	 * critical range address, but just before the CPU delivers a
-	 * GP, it decides to deliver an interrupt instead.  Unlikely?
-	 * Definitely.  Easy to avoid?  Yes.  The Intel documents
-	 * explicitly say that the reported EIP for a bad jump is the
-	 * jump instruction itself, not the destination, but some
-	 * virtual environments get this wrong.
+	 * PF, it decides to deliver an interrupt instead.  Unlikely?
+	 * Definitely.  Easy to avoid?  Yes.
 	 */
-	movl 3*4(%esp), %ecx		/* nested CS */
-	andl $SEGMENT_RPL_MASK, %ecx
-	cmpl $USER_RPL, %ecx
-	popl %ecx
-	je 2f
+	testb $2, 2*4(%esp)		/* nested CS */
+	jnz 2f
 
 	/*
 	 * If eip is before iret_restore_end then stack
#7 lore raw

Ping: [PATCH 0/2] x86/Xen/32: xen_iret_crit_fixup adjustments

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

The first patch here fixes another regression from 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs"), besides the one already addressed by
https://lists.xenproject.org/archives/html/xen-devel/2019-10/msg01988.html.
The second patch is a minimal bit of cleanup on top.

1: make xen_iret_crit_fixup independent of frame layout 2: simplify xen_iret_crit_fixup's ring check

Seeing that the other regression fix has been taken into -tip, what is the situation here? Should 5.4 really ship with this still unfixed?

Jan

#8 lore raw

Re: Ping: [PATCH 0/2] x86/Xen/32: xen_iret_crit_fixup adjustments

Quoted 7 lines · Jan Beulich
Quoted 6 lines · Jan Beulich

The first patch here fixes another regression from 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs"), besides the one already addressed by
https://lists.xenproject.org/archives/html/xen-devel/2019-10/msg01988.html.
The second patch is a minimal bit of cleanup on top.

Quoted 5 lines
Quoted 2 lines

1: make xen_iret_crit_fixup independent of frame layout 2: simplify xen_iret_crit_fixup's ring check

Seeing that the other regression fix has been taken into -tip, what is the situation here? Should 5.4 really ship with this still unfixed?

I am still unable to boot a 32-bit guest with those patches, crashing in int3_exception_notify with regs->sp zero.

When I revert to 3c88c692c287 the guest actually boots so my (?) problem was introduced somewhere in-between.

-boris

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

Re: Ping: [PATCH 0/2] x86/Xen/32: xen_iret_crit_fixup adjustments

Quoted 14 lines · Boris Ostrovsky
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Jan Beulich

The first patch here fixes another regression from 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs"), besides the one already addressed by
https://lists.xenproject.org/archives/html/xen-devel/2019-10/msg01988.html.
The second patch is a minimal bit of cleanup on top.

Quoted 5 lines
Quoted 2 lines

1: make xen_iret_crit_fixup independent of frame layout 2: simplify xen_iret_crit_fixup's ring check

Seeing that the other regression fix has been taken into -tip, what is the situation here? Should 5.4 really ship with this still unfixed?

Quoted 5 lines

I am still unable to boot a 32-bit guest with those patches, crashing in int3_exception_notify with regs->sp zero.

When I revert to 3c88c692c287 the guest actually boots so my (?) problem was introduced somewhere in-between.

Nevermind this. I didn't read your patches correctly.

-boris

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Re: Ping: [PATCH 0/2] x86/Xen/32: xen_iret_crit_fixup adjustments

Quoted 14 lines · Boris Ostrovsky
Quoted 13 lines · Boris Ostrovsky
Quoted 12 lines · Jan Beulich
Quoted 8 lines · Jan Beulich

The first patch here fixes another regression from 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs"), besides the one already addressed by
https://lists.xenproject.org/archives/html/xen-devel/2019-10/msg01988.html.
The second patch is a minimal bit of cleanup on top.

1: make xen_iret_crit_fixup independent of frame layout 2: simplify xen_iret_crit_fixup's ring check

Seeing that the other regression fix has been taken into -tip, what is the situation here? Should 5.4 really ship with this still unfixed?

Quoted 6 lines
Quoted 5 lines

I am still unable to boot a 32-bit guest with those patches, crashing in int3_exception_notify with regs->sp zero.

When I revert to 3c88c692c287 the guest actually boots so my (?) problem was introduced somewhere in-between.

Nevermind this. I didn't read your patches correctly.

BTW, I'd rather this not go into 5.4 this late. 3c88c692c287 has been there since 5.2 and noone complained.

-boris

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

Re: [Xen-devel] Ping: [PATCH 0/2] x86/Xen/32: xen_iret_crit_fixup adjustments

Quoted 18 lines · Boris Ostrovsky
Quoted 17 lines · Boris Ostrovsky
Quoted 16 lines · Boris Ostrovsky
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Jan Beulich

The first patch here fixes another regression from 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs"), besides the one already addressed by
https://lists.xenproject.org/archives/html/xen-devel/2019-10/msg01988.html.
The second patch is a minimal bit of cleanup on top.

Quoted 5 lines
Quoted 2 lines

1: make xen_iret_crit_fixup independent of frame layout 2: simplify xen_iret_crit_fixup's ring check

Seeing that the other regression fix has been taken into -tip, what is the situation here? Should 5.4 really ship with this still unfixed?

I am still unable to boot a 32-bit guest with those patches, crashing in int3_exception_notify with regs->sp zero.

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

When I revert to 3c88c692c287 the guest actually boots so my (?) problem was introduced somewhere in-between.

Nevermind this. I didn't read your patches correctly.

BTW, I'd rather this not go into 5.4 this late. 3c88c692c287 has been there since 5.2 and noone complained.

Afaict the issues were introduced in 5.3, and my first patch (including a note [complaint if you will] of the second issue) was sent around 5.4-rc2. This has been blocking osstest's linux-linus forever since, so even without my mail everyone could have been aware by paying attention to the flight reports (the bisection ones, unfortunately, are pretty useless here, as in cases like this one they seem to tend to point at huge merge commits).

Jan

#12 lore raw

Re: [Xen-devel] Ping: [PATCH 0/2] x86/Xen/32: xen_iret_crit_fixup adjustments

In reply to Boris Ostrovsky
Quoted 15 lines · Boris Ostrovsky
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Jan Beulich

The first patch here fixes another regression from 3c88c692c287 ("x86/stackframe/32: Provide consistent pt_regs"), besides the one already addressed by
https://lists.xenproject.org/archives/html/xen-devel/2019-10/msg01988.html.
The second patch is a minimal bit of cleanup on top.

Quoted 5 lines
Quoted 2 lines

1: make xen_iret_crit_fixup independent of frame layout 2: simplify xen_iret_crit_fixup's ring check

Seeing that the other regression fix has been taken into -tip, what is the situation here? Should 5.4 really ship with this still unfixed?

Quoted 5 lines

I am still unable to boot a 32-bit guest with those patches, crashing in int3_exception_notify with regs->sp zero.

When I revert to 3c88c692c287 the guest actually boots so my (?) problem was introduced somewhere in-between.

In order to even get as far as the patches here taking effect you first need "x86/stackframe/32: repair 32-bit Xen PV" (which is what "the other regression fix" in my ping refers to).

Jan