Skip to contentSkip to conversation

target/i386: Clear xsave pkru bit when KVM XCR0 not support

PATCH In review Yuchen 3 participants 5 messages May 17, 2023 – May 20, 2023 2 messages not in the archive

lore
Share & tools
lore

Yuchen

author
#0 lore raw

PATCH 1/1 target/i386: Clear xsave pkru bit when KVM XCR0 not support

Migrating guest from Intel new CPU (as Gold 6230) to old CPU (as E5-2650 v4) will pause on the destination host. Because old CPU not support xsave pkru feature, and KVM KVM_SET_XSAVE ioctl return EINVAL.

This kernel commit introduces the problem:
ea4d6938d4c0 x86/fpu: Replace KVMs home brewed FPU copy from user

---
target/i386/xsave_helper.c | 8 ++++++++

1 file changed, 8 insertions(+)
target/i386/xsave_helper.c +2 -0
index 996e9f3bfe..64e2b969fe 100644
@@ -6,6 +6,8 @@
 #include "cpu.h"
+static bool has_xsave_pkru;
+

void x86_cpu_xsave_all_areas(X86CPU *cpu, void *buf, uint32_t buflen)

{
     CPUX86State *env = &cpu->env;
@@ -47,6 +49,9 @@ void x86_cpu_xsave_all_areas(X86CPU *cpu, void *buf, uint32_t buflen)
         stq_p(xmm + 8, env->xmm_regs[i].ZMM_Q(1));
     }
+    if (!has_xsave_pkru) {
+        env->xstate_bv &= ~XSTATE_PKRU_MASK;
+    }
     header->xstate_bv = env->xstate_bv;
     e = &x86_ext_save_areas[XSTATE_YMM_BIT];
@@ -181,6 +186,9 @@ void x86_cpu_xrstor_all_areas(X86CPU *cpu, const void *buf, uint32_t buflen)
         env->xmm_regs[i].ZMM_Q(1) = ldq_p(xmm + 8);
     }
+    if (xsave->header.xstate_bv & XSTATE_PKRU_MASK) {
+        has_xsave_pkru = true;
+    }
     env->xstate_bv = header->xstate_bv;
     e = &x86_ext_save_areas[XSTATE_YMM_BIT];

--
2.34.1
-------------------------------------------------------------------------------------------------------------------------------------
?????????????????????????????????
???????????????????????????????????????? ????????????????????????????????????????
???
This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!

#1 lore raw
Quoted 10 lines · Yuchen

Migrating guest from Intel new CPU (as Gold 6230) to old CPU (as E5-2650 v4) will pause on the destination host. Because old CPU not support xsave pkru feature, and KVM KVM_SET_XSAVE ioctl return EINVAL.

This kernel commit introduces the problem:

ea4d6938d4c0 x86/fpu: Replace KVMs home brewed FPU copy from user

Would this work instead?

target/i386/xsave_helper.c +1 -1
index 996e9f3bfef5..d3e5edad2ecd 100644
@@ -47,7 +47,7 @@ void x86_cpu_xsave_all_areas(X86CPU *cpu, void *buf, uint32_t buflen)
         stq_p(xmm + 8, env->xmm_regs[i].ZMM_Q(1));
     }
 
-    header->xstate_bv = env->xstate_bv;
+    header->xstate_bv = env->xstate_bv & x86_cpu_xsave_xcr0_components(cpu);
 
     e = &x86_ext_save_areas[XSTATE_YMM_BIT];
     if (e->size && e->offset) {

Paolo

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

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

author
#4 lore raw

Paolo, thanks,
The kernel patch can solve this problem. But it is difficult to upgrade the kernel in some production environments, and upgrading qemu is easy. This patch is just to sync qemu with kvm XSAVE features, no negative impact. At the same time, it increases the compatibility of qemu to kernel and improves the robustness of qemu.

Quoted 77 lines

-----邮件原件-----
发件人: Paolo Bonzini <[email protected]>
发送时间: 2023年5月20日 1:37
收件人: yuchen (Cloud) <[email protected]>
主题: Re: 回复: [PATCH] target/i386: Clear xsave pkru bit when KVM XCR0 not support

Quoted 1 line · Yuchen

Yes, because x86_cpu_xsave_all_areas() can get the correct XSAVE

features.

If you tested it, I can post the patch as a workaround. However, the kernel bug has been fixed too.

Paolo

Quoted 4 lines
Quoted 4 lines

-----邮件原件-----
发件人: Paolo Bonzini <[email protected]>
发送时间: 2023年5月18日 1:08
收件人: yuchen (Cloud) <[email protected]>;

[email protected]

Quoted 3 lines
Quoted 3 lines

抄送: [email protected]; [email protected]; chengchiwen (Cloud) <[email protected]>
主题: Re: [PATCH] target/i386: Clear xsave pkru bit when KVM XCR0

not

Quoted 15 lines
Quoted 15 lines

support

Quoted 3 lines · Yuchen

Migrating guest from Intel new CPU (as Gold 6230) to old CPU (as E5-2650 v4) will pause on the destination host. Because old CPU not support xsave pkru feature, and KVM KVM_SET_XSAVE ioctl return

EINVAL.

Quoted 4 lines

This kernel commit introduces the problem:

ea4d6938d4c0 x86/fpu: Replace KVMs home brewed FPU copy from

user

Quoted 2 lines
Quoted 7 lines

target/i386/xsave_helper.c void x86_cpu_xsave_all_areas(X86CPU *cpu,

Quoted 7 lines

Would this work instead?

target/i386/xsave_helper.c +0 -0
index 996e9f3bfef5..d3e5edad2ecd 100644
@@ -47,7 +47,7 @@ void x86_cpu_xsave_all_areas(X86CPU *cpu,

void

Quoted 8 lines
Quoted 8 lines
target/i386/xsave_helper.c +2 -1
 *buf, uint32_t buflen)
           stq_p(xmm + 8, env->xmm_regs[i].ZMM_Q(1));
       }
 
-    header->xstate_bv = env->xstate_bv;
+    header->xstate_bv = env->xstate_bv &
+ x86_cpu_xsave_xcr0_components(cpu);
 
Quoted 8 lines
Quoted 4 lines
       e = &x86_ext_save_areas[XSTATE_YMM_BIT];
       if (e->size && e->offset) {

Paolo

----------------------------------------------------------------------
---------------------------------------------------------------
本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地址

中列出

Quoted 1 line

的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部

或部分地泄露、复制、

Quoted 1 line

或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮

件通知发件人并删除本

Quoted 8 lines

邮件!
This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!

author
#5 lore raw

Paolo,
I debugged and found, x86_cpu_xsave_xcr0_components() retuen 0, although it can solve problem, but XCR0 bit 0 also is 0,
this is not correct. Because Intel manual Requirements : " XCR0.X87 (bit 0): This bit 0 must be 1. An attempt to write 0 to this bit causes a #GP exception. " (2.6 EXTENDED CONTROL REGISTERS (INCLUDING XCR0)).

Quoted 104 lines

-----邮件原件-----
发件人: yuchen (Cloud)
发送时间: 2023年5月20日 9:58
收件人: 'Paolo Bonzini' <[email protected]>; '[email protected]' <[email protected]>
抄送: '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; 'Dongli Zhang' <[email protected]> 主题: 回复: 回复: [PATCH] target/i386: Clear xsave pkru bit when KVM XCR0 not support

Paolo, thanks,
The kernel patch can solve this problem. But it is difficult to upgrade the kernel in some production environments, and upgrading qemu is easy. This patch is just to sync qemu with kvm XSAVE features, no negative impact.
At the same time, it increases the compatibility of qemu to kernel and improves the robustness of qemu.

Quoted 14 lines

-----邮件原件-----
发件人: Paolo Bonzini <[email protected]>
发送时间: 2023年5月20日 1:37
收件人: yuchen (Cloud) <[email protected]>
主题: Re: 回复: [PATCH] target/i386: Clear xsave pkru bit when KVM XCR0 not support

Quoted 1 line · Yuchen

Yes, because x86_cpu_xsave_all_areas() can get the correct XSAVE

features.

If you tested it, I can post the patch as a workaround. However, the kernel bug has been fixed too.

Quoted 8 lines

Paolo

Quoted 4 lines
Quoted 4 lines

-----邮件原件-----
发件人: Paolo Bonzini <[email protected]>
发送时间: 2023年5月18日 1:08
收件人: yuchen (Cloud) <[email protected]>;

[email protected]

Quoted 1 line
Quoted 1 line

抄送: [email protected]; [email protected]; chengchiwen

(Cloud)

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
主题: Re: [PATCH] target/i386: Clear xsave pkru bit when KVM XCR0

not

Quoted 6 lines
Quoted 6 lines

support

Quoted 3 lines · Yuchen

Migrating guest from Intel new CPU (as Gold 6230) to old CPU (as E5-2650 v4) will pause on the destination host. Because old CPU not support xsave pkru feature, and KVM KVM_SET_XSAVE ioctl

return

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

EINVAL.

Quoted 4 lines

This kernel commit introduces the problem:

ea4d6938d4c0 x86/fpu: Replace KVMs home brewed FPU copy

from

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

user

Quoted 2 lines

Would this work instead?

Quoted 24 lines
Quoted 6 lines

target/i386/xsave_helper.c void x86_cpu_xsave_all_areas(X86CPU *cpu,

Quoted 6 lines
(quoted diff) +0 -0

b/target/i386/xsave_helper.c index 996e9f3bfef5..d3e5edad2ecd 100644

target/i386/xsave_helper.c +0 -0
@@ -47,7 +47,7 @@ void x86_cpu_xsave_all_areas(X86CPU *cpu,

void

Quoted 17 lines
Quoted 11 lines
target/i386/xsave_helper.c +2 -1
 *buf, uint32_t buflen)
           stq_p(xmm + 8, env->xmm_regs[i].ZMM_Q(1));
       }
 
-    header->xstate_bv = env->xstate_bv;
+    header->xstate_bv = env->xstate_bv &
+ x86_cpu_xsave_xcr0_components(cpu);
 
       e = &x86_ext_save_areas[XSTATE_YMM_BIT];
       if (e->size && e->offset) {
Quoted 1 line

Paolo

--------------------------------------------------------------------
--
---------------------------------------------------------------
本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地

址

Quoted 2 lines

中列出

Quoted 1 line

的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全

部

Quoted 2 lines

或部分地泄露、复制、

Quoted 1 line

或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或

邮

Quoted 10 lines

件通知发件人并删除本

Quoted 9 lines

邮件!
This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended
recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!

#6 lore raw
In reply to Yuchen

Hi Yuchen,

Quoted 7 lines · Yuchen

Migrating guest from Intel new CPU (as Gold 6230) to old CPU (as E5-2650 v4) will pause on the destination host. Because old CPU not support xsave pkru feature, and KVM KVM_SET_XSAVE ioctl return EINVAL.

This kernel commit introduces the problem:
ea4d6938d4c0 x86/fpu: Replace KVMs home brewed FPU copy from user

This kernel commit issue should be resolved by the below kernel commit.

x86/kvm/fpu: Limit guest user_xfeatures to supported bits of XCR0

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ad856280ddea3401e1f5060ef20e6de9f6122c76

Since the old target server does not support pkru, I assume the VM's cpu type should not support pkru. Therefore, the pkru should never be migrated away from source server.

Dongli Zhang

Quoted 47 lines

---
target/i386/xsave_helper.c | 8 ++++++++

1 file changed, 8 insertions(+)
target/i386/xsave_helper.c +8 -1
index 996e9f3bfe..64e2b969fe 100644
@@ -6,6 +6,8 @@
 #include "cpu.h"
+static bool has_xsave_pkru;
+
 void x86_cpu_xsave_all_areas(X86CPU *cpu, void *buf, uint32_t buflen)
 {
     CPUX86State *env = &cpu->env;
@@ -47,6 +49,9 @@ void x86_cpu_xsave_all_areas(X86CPU *cpu, void *buf, uint32_t buflen)
         stq_p(xmm + 8, env->xmm_regs[i].ZMM_Q(1));
     }
+    if (!has_xsave_pkru) {
+        env->xstate_bv &= ~XSTATE_PKRU_MASK;
+    }
     header->xstate_bv = env->xstate_bv;
     e = &x86_ext_save_areas[XSTATE_YMM_BIT];
@@ -181,6 +186,9 @@ void x86_cpu_xrstor_all_areas(X86CPU *cpu, const void *buf, uint32_t buflen)
         env->xmm_regs[i].ZMM_Q(1) = ldq_p(xmm + 8);
     }
+    if (xsave->header.xstate_bv & XSTATE_PKRU_MASK) {
+        has_xsave_pkru = true;
+    }
     env->xstate_bv = header->xstate_bv;
     e = &x86_ext_save_areas[XSTATE_YMM_BIT];
--

2.34.1
-------------------------------------------------------------------------------------------------------------------------------------
?????????????????????????????????
???????????????????????????????????????? ????????????????????????????????????????
???
This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!