From: Thomas Gleixner <tglx@linutronix.de>
To: "Liu, Jing2" <jing2.liu@intel.com>, Paolo Bonzini <pbonzini@redhat.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
"x86@kernel.org" <x86@kernel.org>,
"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"Nakajima, Jun" <jun.nakajima@intel.com>,
Dave Hansen <dave.hansen@linux.intel.com>,
Arjan van de Ven <arjan@linux.intel.com>,
Jing Liu <jing2.liu@linux.intel.com>,
"seanjc@google.com" <seanjc@google.com>,
"Cooper, Andrew" <andrew.cooper3@citrix.com>,
"Liu, Jing2" <jing2.liu@intel.com>,
"Bae, Chang Seok" <chang.seok.bae@intel.com>
Subject: Re: Thoughts of AMX KVM support based on latest kernel
Date: Tue, 16 Nov 2021 13:18:10 +0100 [thread overview]
Message-ID: <87k0h85m65.ffs@tglx> (raw)
In-Reply-To: <BYAPR11MB325685AB8E3DFD245846F854A9939@BYAPR11MB3256.namprd11.prod.outlook.com>
Jing,
On Wed, Nov 10 2021 at 13:01, Jing2 Liu wrote:
> Triggering of a reallocation request and error handling
>
> First, we want to avoid weird guest failures at runtime due to (more likely)
> permission failures of a reallocation request, checking the permissions of the
> vcpu (for the extend features) at kvm_vcpu_ioctl_set_cpuid2() time, when
> QEMU wants to advertise the extended features (e.g. AMX) for the first
> time.
That's the right thing to do. If there is no permission for the guest
granted via the prctl() extension I suggested then exposing AMX should
be rejected.
> We have no idea at vcpu_create() time whether QEMU wants to enable AMX
> or not at that time. If kvm_vcpu_ioctl_set_cpuid2() succeeds, then there is
> no need to further check permission in reallocation path.
That's correct.
> Upon detection (interception) of an attempt by a vcpu to write to XCR0 (XSETBV)
> and XFD (WRMSR), we check if the write is valid, and we start passthrough of
> the XFD MSRs if the dynamic feature[i] meets the condition
> XCR0[i]=1 && XFD[i]=0. And we make a reallocation request to the FPU core.
>
> We simplify the KVM implementation by assuming that the reallocation
> request was successful when the vcpu comes back to KVM. For such VM exit
> handling that requires a buffer-reallocation request, we don't resume the
> guest immediately. Instead, we go back to the userspace, to rely on the
> userspace VMM (e.g. QEMU) for handling error cases. The actual reallocation
> happens when control is transferred from KVM to the kernel (FPU core). If
> no error, QEMU will come back to KVM by repeating vcpu_ioctl_run().
>
> Potential failures there are due to lack of memory. But this would not be
> interesting cases; the host should have more resource problems at that
> time if that is the case.
Indeed.
> One of potential drawbacks of the Option 2 might be additional
> checks in the host, although we can minimize the impact by having
> CONFIG_KVM_TBD. We believe that the case
> "XFD != 0 and XINUSE != 0" should be very infrequent.
I really don't like the idea of having an extra check in switch_to().
Can we start simple and do something like the uncompiled below and see
how much overhead it creates?
Thanks,
tglx
---
diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/xstate.h
index 0f8b90ab18c9..6175a78e0be8 100644
--- a/arch/x86/include/asm/fpu/xstate.h
+++ b/arch/x86/include/asm/fpu/xstate.h
@@ -122,4 +122,12 @@ static __always_inline __pure bool fpu_state_size_dynamic(void)
}
#endif
+void fpu_update_guest_xfd_state(void);
+
+static inline void kvm_update_guest_xfd_state(void)
+{
+ if (fpu_state_size_dynamic())
+ fpu_update_guest_xfd_state();
+}
+
#endif
diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 8ea306b1bf8e..161db48c9052 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -199,6 +199,17 @@ void fpu_reset_from_exception_fixup(void)
}
#if IS_ENABLED(CONFIG_KVM)
+void fpu_update_guest_xfd_state(void)
+{
+ u64 xfd;
+
+ /* FIXME: Add debug */
+ rdmsrl(MSR_IA32_XFD, xfd);
+ current->thread.fpu.fpstate->xfd = xfd;
+ __this_cpu_write(xfd_state, xfd);
+}
+EXPORT_SYMBOL_GPL(fpu_update_guest_xfd_state);
+
static void __fpstate_reset(struct fpstate *fpstate);
bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2686f2edb47c..9425fdbb4806 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9576,6 +9576,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
vcpu->arch.last_vmentry_cpu = vcpu->cpu;
vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
+ kvm_update_guest_xfd_state();
+
vcpu->mode = OUTSIDE_GUEST_MODE;
smp_wmb();
next prev parent reply other threads:[~2021-11-16 12:18 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-10 13:01 Thoughts of AMX KVM support based on latest kernel Liu, Jing2
2021-11-16 12:18 ` Thomas Gleixner [this message]
2021-11-16 16:05 ` Sean Christopherson
2021-11-16 18:55 ` Thomas Gleixner
2021-11-16 19:49 ` Paolo Bonzini
2021-11-16 20:14 ` Sean Christopherson
2021-11-16 20:36 ` Thomas Gleixner
2021-11-16 22:11 ` Nakajima, Jun
2021-11-17 7:39 ` Paolo Bonzini
2021-11-16 20:12 ` Sean Christopherson
2021-11-16 23:35 ` Thomas Gleixner
2021-11-16 19:20 ` Thomas Gleixner
2021-11-17 4:52 ` Nakajima, Jun
2021-11-17 7:31 ` Paolo Bonzini
2021-11-17 10:15 ` Tian, Kevin
2021-11-17 12:24 ` Thomas Gleixner
2021-11-17 12:53 ` Paolo Bonzini
2021-11-18 23:17 ` Nakajima, Jun
2021-11-19 10:13 ` Thomas Gleixner
2021-11-19 15:41 ` Nakajima, Jun
2021-12-08 0:50 ` Rewording of Setting IA32_XFD[18] (Re: Thoughts of AMX KVM support based on latest kernel) Nakajima, Jun
2021-12-08 13:57 ` Paolo Bonzini
2021-12-10 21:53 ` Thomas Gleixner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87k0h85m65.ffs@tglx \
--to=tglx@linutronix.de \
--cc=andrew.cooper3@citrix.com \
--cc=arjan@linux.intel.com \
--cc=chang.seok.bae@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=jing2.liu@intel.com \
--cc=jing2.liu@linux.intel.com \
--cc=jun.nakajima@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.