Skip to contentSkip to conversation

x86/sev: Add ENCODE_FRAME_POINTER after stack changed for #VC

PATCH Applied Lai Jiangshan 3 participants 8 messages Mar 16, 2022 – May 18, 2022

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

PATCH 1/1 x86/sev: Add ENCODE_FRAME_POINTER after stack changed for #VC

From: Lai Jiangshan <[email protected]>

In idtentry_vc(), the real frame is changed after it switches to new stack, ENCODE_FRAME_POINTER should be called in case for unwinder.

Cc Joerg Roedel <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 arch/x86/entry/entry_64.S | 1 +
 1 file changed, 1 insertion(+)
arch/x86/entry/entry_64.S +1 -0
index e20eabaa56b8..f99f2881ed83 100644
@@ -540,6 +540,7 @@ SYM_CODE_START(\asmsym)
 	call	vc_switch_off_ist
 	movq	%rax, %rsp		/* Switch to new stack */
 
+	ENCODE_FRAME_POINTER
 	UNWIND_HINT_REGS
 
 	/* Update pt_regs */
Signature · 1 line
2.19.1.6.gb485710b
#1 lore raw
Quoted 17 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

In idtentry_vc(), the real frame is changed after it switches to new stack, ENCODE_FRAME_POINTER should be called in case for unwinder.

Cc Joerg Roedel <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 arch/x86/entry/entry_64.S | 1 +
 1 file changed, 1 insertion(+)
arch/x86/entry/entry_64.S +0 -0
index e20eabaa56b8..f99f2881ed83 100644

arch/x86/entry/entry_64.S SYM_CODE_START(\asmsym) · line 546

Quoted 7 lines
arch/x86/entry/entry_64.S +1 -0
@@ -540,6 +540,7 @@ SYM_CODE_START(\asmsym)
 	call	vc_switch_off_ist
 	movq	%rax, %rsp		/* Switch to new stack */
 
+	ENCODE_FRAME_POINTER
 	UNWIND_HINT_REGS
 
 	/* Update pt_regs */

Looks right to me. Any chance you have the ability to test this scenario by unwinding from #VC with CONFIG_FRAME_POINTER?

Signature · 1 line
Josh
#2 lore raw
Quoted 22 lines · Josh Poimboeuf
Quoted 20 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

In idtentry_vc(), the real frame is changed after it switches to new stack, ENCODE_FRAME_POINTER should be called in case for unwinder.

Cc Joerg Roedel <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 arch/x86/entry/entry_64.S | 1 +
 1 file changed, 1 insertion(+)
arch/x86/entry/entry_64.S +0 -0
index e20eabaa56b8..f99f2881ed83 100644
@@ -540,6 +540,7 @@ SYM_CODE_START(\asmsym)
      call    vc_switch_off_ist
      movq    %rax, %rsp              /* Switch to new stack */
 
Quoted 8 lines

arch/x86/entry/entry_64.S SYM_CODE_START(\asmsym) · line 546

Quoted 4 lines
arch/x86/entry/entry_64.S +1 -0
+     ENCODE_FRAME_POINTER
      UNWIND_HINT_REGS
 
      /* Update pt_regs */

Looks right to me. Any chance you have the ability to test this scenario by unwinding from #VC with CONFIG_FRAME_POINTER?

I don't have the hardware to test it. Actually, it was you to inspire me to find this in the conversation of my patchset when I tried to search all the code of switching of %rsp to see how ENCODE_FRAME_POINTER and UNWIND_HINT_REGS are used once more.

Quoted 3 lines

--
Josh

#3 lore raw
Quoted 32 lines · Lai Jiangshan
Quoted 22 lines · Josh Poimboeuf
Quoted 20 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

In idtentry_vc(), the real frame is changed after it switches to new stack, ENCODE_FRAME_POINTER should be called in case for unwinder.

Cc Joerg Roedel <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 arch/x86/entry/entry_64.S | 1 +
 1 file changed, 1 insertion(+)
arch/x86/entry/entry_64.S +0 -0
index e20eabaa56b8..f99f2881ed83 100644
@@ -540,6 +540,7 @@ SYM_CODE_START(\asmsym)
      call    vc_switch_off_ist
      movq    %rax, %rsp              /* Switch to new stack */
 
Quoted 8 lines

arch/x86/entry/entry_64.S SYM_CODE_START(\asmsym) · line 546

Quoted 4 lines
arch/x86/entry/entry_64.S +1 -0
+     ENCODE_FRAME_POINTER
      UNWIND_HINT_REGS
 
      /* Update pt_regs */

Looks right to me. Any chance you have the ability to test this scenario by unwinding from #VC with CONFIG_FRAME_POINTER?

Quoted 4 lines

I don't have the hardware to test it. Actually, it was you to inspire me to find this in the conversation of my patchset when I tried to search all the code of switching of %rsp to see how ENCODE_FRAME_POINTER and UNWIND_HINT_REGS are used once more.

Ok, maybe Joerg can test it. It looks obviously right to me though. Since it fixes a real frame pointer unwinding bug, it probably needs:

Fixes a13644f3a53d ("x86/entry/64: Add entry code for #VC handler")
Signature · 1 line
Josh
#4 lore raw
Quoted 1 line · Josh Poimboeuf

Ok, maybe Joerg can test it. It looks obviously right to me though.

It looks also obviously right to me.

Hello, Joerg

Any feedback?

Quoted 5 lines

Since it fixes a real frame pointer unwinding bug, it probably needs:

Fixes a13644f3a53d ("x86/entry/64: Add entry code for #VC handler")

Hello, Borislav

Can you have a look at the patch, please?

Thanks
Lai

#5 lore raw
Quoted 15 lines · Lai Jiangshan
Quoted 1 line · Josh Poimboeuf

Ok, maybe Joerg can test it. It looks obviously right to me though.

It looks also obviously right to me.

Hello, Joerg

Any feedback?

Quoted 4 lines

Since it fixes a real frame pointer unwinding bug, it probably needs:

Fixes a13644f3a53d ("x86/entry/64: Add entry code for #VC handler")
Quoted 8 lines
Quoted 1 line

Hello, Borislav

Can you have a look at the patch, please?

Thanks
Lai

Hello

Ping.

Thanks
Lai

#6 lore raw

+ Brijesh
+ Tom

Quoted 27 lines · Lai Jiangshan
Quoted 15 lines · Lai Jiangshan
Quoted 1 line · Josh Poimboeuf

Ok, maybe Joerg can test it. It looks obviously right to me though.

It looks also obviously right to me.

Hello, Joerg

Any feedback?

Quoted 4 lines

Since it fixes a real frame pointer unwinding bug, it probably needs:

Fixes a13644f3a53d ("x86/entry/64: Add entry code for #VC handler")
Quoted 8 lines
Quoted 1 line

Hello, Borislav

Can you have a look at the patch, please?

Thanks
Lai

Quoted 6 lines

Hello

Ping.

Thanks
Lai

Hello, Joerg, Brijesh, Tom

Ping.

Thanks
Lai

merge bot Applied
#7 lore raw

[tip: x86/sev] x86/sev: Annotate stack change in the #VC handler

In reply to Lai Jiangshan
The following commit has been merged into the x86/sev branch of tip:

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

Commit-ID:     c42b145181aafd59ed31ccd879493389e3ea5a08
Gitweb:        https://git.kernel.org/tip/c42b145181aafd59ed31ccd879493389e3ea5a08
Author:        Lai Jiangshan <[email protected]>
AuthorDate:    Wed, 16 Mar 2022 12:16:12 +08:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 18 May 2022 20:36:03 +02:00

x86/sev: Annotate stack change in the #VC handler

In idtentry_vc(), vc_switch_off_ist() determines a safe stack to switch to, off of the IST stack. Annotate the new stack switch with ENCODE_FRAME_POINTER in case UNWINDER_FRAME_POINTER is used.

A stack walk before looks like this:

  CPU: 0 PID: 0 Comm: swapper Not tainted 5.18.0-rc7+ #2
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  Call Trace:
   <TASK>
   dump_stack_lvl
   dump_stack
   kernel_exc_vmm_communication
   asm_exc_vmm_communication
   ? native_read_msr
   ? __x2apic_disable.part.0
   ? x2apic_setup
   ? cpu_init
   ? trap_init
   ? start_kernel
   ? x86_64_start_reservations
   ? x86_64_start_kernel
   ? secondary_startup_64_no_verify
   </TASK>

and with the fix, the stack dump is exact:

  CPU: 0 PID: 0 Comm: swapper Not tainted 5.18.0-rc7+ #3
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  Call Trace:
   <TASK>
   dump_stack_lvl
   dump_stack
   kernel_exc_vmm_communication
   asm_exc_vmm_communication
  RIP: 0010:native_read_msr
  Code: ...
  < snipped regs >
   ? __x2apic_disable.part.0
   x2apic_setup
   cpu_init
   trap_init
   start_kernel
   x86_64_start_reservations
   x86_64_start_kernel
   secondary_startup_64_no_verify
   </TASK>

[ bp: Test in a SEV-ES guest and rewrite the commit message to
explain what exactly this does. ]

Fixes a13644f3a53d ("x86/entry/64: Add entry code for #VC handler")
---
 arch/x86/entry/entry_64.S | 1 +
 1 file changed, 1 insertion(+)
arch/x86/entry/entry_64.S +1 -0
index 4faac48..f7bd800 100644
@@ -505,6 +505,7 @@ SYM_CODE_START(\asmsym)
 	call	vc_switch_off_ist
 	movq	%rax, %rsp		/* Switch to new stack */
 
+	ENCODE_FRAME_POINTER
 	UNWIND_HINT_REGS
 
 	/* Update pt_regs */