public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] KVM: x86: Fix breakage in KVM_SET_XSAVE's ABI
@ 2023-09-28  0:19 Sean Christopherson
  2023-09-28  0:19 ` [PATCH 1/5] x86/fpu: Allow caller to constrain xfeatures when copying to uabi buffer Sean Christopherson
                   ` (7 more replies)
  0 siblings, 8 replies; 14+ messages in thread
From: Sean Christopherson @ 2023-09-28  0:19 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	Sean Christopherson, Paolo Bonzini, Shuah Khan, Nathan Chancellor,
	Nick Desaulniers
  Cc: linux-kernel, kvm, linux-kselftest, llvm, Tyler Stachecki,
	Leonardo Bras

Rework how KVM limits guest-unsupported xfeatures to effectively hide
only when saving state for userspace (KVM_GET_XSAVE), i.e. to let userspace
load all host-supported xfeatures (via KVM_SET_XSAVE) irrespective of
what features have been exposed to the guest.

The effect on KVM_SET_XSAVE was knowingly done by commit ad856280ddea
("x86/kvm/fpu: Limit guest user_xfeatures to supported bits of XCR0"):

    As a bonus, it will also fail if userspace tries to set fpu features
    (with the KVM_SET_XSAVE ioctl) that are not compatible to the guest
    configuration.  Such features will never be returned by KVM_GET_XSAVE
    or KVM_GET_XSAVE2.

Peventing userspace from doing stupid things is usually a good idea, but in
this case restricting KVM_SET_XSAVE actually exacerbated the problem that
commit ad856280ddea was fixing.  As reported by Tyler, rejecting KVM_SET_XSAVE
for guest-unsupported xfeatures breaks live migration from a kernel without
commit ad856280ddea, to a kernel with ad856280ddea.  I.e. from a kernel that
saves guest-unsupported xfeatures to a kernel that doesn't allow loading
guest-unuspported xfeatures.

To make matters even worse, QEMU doesn't terminate if KVM_SET_XSAVE fails,
and so the end result is that the live migration results (possibly silent)
guest data corruption instead of a failed migration.

Patch 1 refactors the FPU code to let KVM pass in a mask of which xfeatures
to save, patch 2 fixes KVM by passing in guest_supported_xcr0 instead of
modifying user_xfeatures directly.

Patches 3-5 are regression tests.

I have no objection if anyone wants patches 1 and 2 squashed together, I
split them purely to make review easier.

Note, this doesn't fix the scenario where a guest is migrated from a "bad"
to a "good" kernel and the target host doesn't support the over-saved set
of xfeatures.  I don't see a way to safely handle that in the kernel without
an opt-in, which more or less defeats the purpose of handling it in KVM.

Sean Christopherson (5):
  x86/fpu: Allow caller to constrain xfeatures when copying to uabi
    buffer
  KVM: x86: Constrain guest-supported xfeatures only at KVM_GET_XSAVE{2}
  KVM: selftests: Touch relevant XSAVE state in guest for state test
  KVM: selftests: Load XSAVE state into untouched vCPU during state test
  KVM: selftests: Force load all supported XSAVE state in state test

 arch/x86/include/asm/fpu/api.h                |   3 +-
 arch/x86/kernel/fpu/core.c                    |   5 +-
 arch/x86/kernel/fpu/xstate.c                  |  12 +-
 arch/x86/kernel/fpu/xstate.h                  |   3 +-
 arch/x86/kvm/cpuid.c                          |   8 --
 arch/x86/kvm/x86.c                            |  37 +++---
 .../selftests/kvm/include/x86_64/processor.h  |  23 ++++
 .../testing/selftests/kvm/x86_64/state_test.c | 110 +++++++++++++++++-
 8 files changed, 168 insertions(+), 33 deletions(-)


base-commit: 5804c19b80bf625c6a9925317f845e497434d6d3
-- 
2.42.0.582.g8ccd20d70d-goog


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-10-12 14:46 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-28  0:19 [PATCH 0/5] KVM: x86: Fix breakage in KVM_SET_XSAVE's ABI Sean Christopherson
2023-09-28  0:19 ` [PATCH 1/5] x86/fpu: Allow caller to constrain xfeatures when copying to uabi buffer Sean Christopherson
2023-09-28  0:19 ` [PATCH 2/5] KVM: x86: Constrain guest-supported xfeatures only at KVM_GET_XSAVE{2} Sean Christopherson
2023-09-28 14:09   ` Dave Hansen
2023-09-28  0:19 ` [PATCH 3/5] KVM: selftests: Touch relevant XSAVE state in guest for state test Sean Christopherson
2023-09-28  0:19 ` [PATCH 4/5] KVM: selftests: Load XSAVE state into untouched vCPU during " Sean Christopherson
2023-09-28  0:19 ` [PATCH 5/5] KVM: selftests: Force load all supported XSAVE state in " Sean Christopherson
2023-10-04  7:11 ` [PATCH 0/5] KVM: x86: Fix breakage in KVM_SET_XSAVE's ABI Leonardo Bras
2023-10-04 12:21   ` Tyler Stachecki
2023-10-04 14:51     ` Sean Christopherson
2023-10-04 15:29       ` Tyler Stachecki
2023-10-04 16:54         ` Sean Christopherson
2023-10-05  1:29 ` Sean Christopherson
2023-10-12 14:45 ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox