From: Paolo Bonzini <pbonzini@redhat.com>
To: Thomas Gleixner <tglx@linutronix.de>,
Yang Zhong <yang.zhong@intel.com>,
x86@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, mingo@redhat.com, bp@alien8.de,
dave.hansen@linux.intel.com
Cc: seanjc@google.com, jun.nakajima@intel.com, kevin.tian@intel.com,
jing2.liu@linux.intel.com, jing2.liu@intel.com
Subject: Re: [PATCH 02/19] x86/fpu: Prepare KVM for dynamically enabled states
Date: Mon, 13 Dec 2021 13:45:49 +0100 [thread overview]
Message-ID: <16c938e2-2427-c8dd-94a1-eba8f967283b@redhat.com> (raw)
In-Reply-To: <878rwovhnh.ffs@tglx>
On 12/13/21 13:00, Thomas Gleixner wrote:
> On Mon, Dec 13 2021 at 10:12, Paolo Bonzini wrote:
>> On 12/8/21 01:03, Yang Zhong wrote:
>>> - user_xfeatures
>>>
>>> Track which features are currently enabled for the vCPU
>>
>> Please rename to alloc_xfeatures
>
> That name makes no sense at all. This has nothing to do with alloc.
Isn't that the features for which space is currently allocated?
fpstate_realloc does
+ if (guest_fpu) {
+ newfps->is_guest = true;
+ newfps->is_confidential = curfps->is_confidential;
+ guest_fpu->user_xfeatures |= xfeatures;
+ }
+
and kvm_check_guest_realloc_fpstate does
+ if ((guest_fpu->user_xfeatures & request) != request) {
+ vcpu->arch.guest_fpu.realloc_request |= request;
+ return true;
+ }
Reading "user_xfeatures" in there is cryptic, it seems like it's
something related to the userspace thread or group that has invoked the
KVM ioctl. If it's renamed to alloc_xfeatures, then this:
+ missing = request & ~guest_fpu->alloc_xfeatures;
+ if (missing) {
+ vcpu->arch.guest_fpu.realloc_request |= missing;
+ return true;
+ }
makes it obvious that the allocation is for features that are requested
but haven't been allocated in the xstate yet.
>>> - user_perm
>>>
>>> Copied from guest_perm of the group leader thread. The first
>>> vCPU which does the copy locks the guest_perm
>>
>> Please rename to perm_xfeatures.
>
> All of that is following the naming conventions in the FPU code related
> to permissions etc.
perm or guest_perm is just fine as well; but user_perm is not (there's
no preexisting reference to user_perm in the code, in fact, as far as I
can see).
>>> - realloc_request
>>>
>>> KVM sets this field to request dynamically-enabled features
>>> which require reallocation of @fpstate
>>
>> This field should be in vcpu->arch, and there is no need for
>> fpu_guest_realloc_fpstate. Rename __xfd_enable_feature to
>> fpu_enable_xfd_feature and add it to the public API, then just do
>>
>> if (unlikely(vcpu->arch.xfd_realloc_request)) {
>> u64 request = vcpu->arch.xfd_realloc_request;
>> ret = fpu_enable_xfd(request, enter_guest);
>> }
>>
>> to kvm_put_guest_fpu.
>
> Why? Yet another export of FPU internals just because?
It's one function more and one field less. I prefer another export of
FPU internals, to a write to a random field with undocumented invariants.
For example, why WARN_ON_ONCE if enter_guest == true? If you enter the
guest after the host has restored MSR_IA32_XFD with KVM_SET_MSR, the
WARN_ON_ONCE can actually fire. It would be just fine to skip the
reallocation until enter_guest == false, which is you actually XSAVE
into the guest_fpu.
As an aside, realloc_request (if it survives, see below) shouldn't be
added until patch 6.
> Also what clears the reallocation request and what is the @enter_guest
> argument supposed to help with?
Nothing---make it
if (unlikely(vcpu->arch.xfd_realloc_request)) {
u64 request = vcpu->arch.xfd_realloc_request;
ret = fpu_enable_xfd(request, &vcpu->arch.guest_fpu);
if (!ret)
vcpu->arch.xfd_realloc_request = 0;
}
but in fact, I'm not sure why the request has to be delayed at all. The
obvious implementation of a write to XFD, after all the validity checks,
is just
/* This function just calls xfd_enable_feature. */
r = fpu_guest_alloc_xfeatures(&vcpu->arch.guest_fpu,
vcpu->arch.xcr0 & ~xfd);
/*
* An error means that userspace has screwed up by not doing
* arch_prctl(ARCH_REQ_XCOMP_GUEST_PERM). If we are here coming
* from a ioctl fail it, if the guest has done WRMSR or XSETBV
* it will inject a #GP.
*/
if (r < 0)
return 1;
vcpu->arch.xfd = xfd;
with none of the kvm_check_guest_realloc_fpstate or KVM_EXIT_FPU_REALLOC
business. No field and a simple, self-contained external API. The same
code works for __kvm_set_xcr as well, just with "xcr0 & ~vcpu->arch.xfd"
as the second argument instead.
(Maybe I'm missing why KVM_EXIT_FPU_REALLOC is needed, but I'm not
ashamed to say that, given that new userspace API was added with
documentation or tests. The only comment in the code is:
+ /*
+ * Check if fpstate reallocate is required. If yes, then
+ * let the fpu core do reallocation and update xfd;
+ * otherwise, update xfd here.
+ */
+ if (kvm_check_guest_realloc_fpstate(vcpu, data)) {
+ vcpu->run->exit_reason = KVM_EXIT_FPU_REALLOC;
+ vcpu->arch.complete_userspace_io =
+ kvm_skip_emulated_instruction;
+ return KVM_MSR_RET_USERSPACE;
+ }
+
which has nothing to do with the actual content of either
kvm_check_guest_realloc_fpstate or the "then" branch. Userspace can
just do ARCH_REQ_XCOMP_GUEST_PERM based on the guest CPUID, before
KVM_SET_CPUID2, KVM_SET_MSR or KVM_SET_XCR).
Paolo
next prev parent reply other threads:[~2021-12-13 12:45 UTC|newest]
Thread overview: 80+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-08 0:03 [PATCH 00/19] AMX Support in KVM Yang Zhong
2021-12-08 0:03 ` [PATCH 01/19] x86/fpu: Extend prctl() with guest permissions Yang Zhong
2021-12-14 0:16 ` Thomas Gleixner
2021-12-08 0:03 ` [PATCH 02/19] x86/fpu: Prepare KVM for dynamically enabled states Yang Zhong
2021-12-13 9:12 ` Paolo Bonzini
2021-12-13 12:00 ` Thomas Gleixner
2021-12-13 12:45 ` Paolo Bonzini [this message]
2021-12-13 19:50 ` Thomas Gleixner
2021-12-08 0:03 ` [PATCH 03/19] kvm: x86: Fix xstate_required_size() to follow XSTATE alignment rule Yang Zhong
2021-12-08 0:03 ` [PATCH 04/19] kvm: x86: Check guest xstate permissions when KVM_SET_CPUID2 Yang Zhong
2021-12-08 0:03 ` [PATCH 05/19] x86/fpu: Move xfd initialization out of __fpstate_reset() to the callers Yang Zhong
2021-12-10 22:33 ` Thomas Gleixner
2021-12-08 0:03 ` [PATCH 06/19] x86/fpu: Add reallocation mechanims for KVM Yang Zhong
2021-12-08 0:03 ` [PATCH 07/19] kvm: x86: Propagate fpstate reallocation error to userspace Yang Zhong
2021-12-10 15:44 ` Paolo Bonzini
2021-12-08 0:03 ` [PATCH 08/19] x86/fpu: Move xfd_update_state() to xstate.c and export symbol Yang Zhong
2021-12-10 22:44 ` Thomas Gleixner
2021-12-08 0:03 ` [PATCH 09/19] kvm: x86: Prepare reallocation check Yang Zhong
2021-12-13 9:16 ` Paolo Bonzini
2021-12-14 7:06 ` Tian, Kevin
2021-12-14 10:16 ` Paolo Bonzini
2021-12-14 14:41 ` Liu, Jing2
2021-12-15 7:09 ` Tian, Kevin
2021-12-08 0:03 ` [PATCH 10/19] kvm: x86: Emulate WRMSR of guest IA32_XFD Yang Zhong
2021-12-10 16:02 ` Paolo Bonzini
2021-12-13 7:51 ` Liu, Jing2
2021-12-13 9:01 ` Paolo Bonzini
2021-12-14 10:26 ` Yang Zhong
2021-12-14 11:24 ` Paolo Bonzini
2021-12-10 23:09 ` Thomas Gleixner
2021-12-13 15:06 ` Paolo Bonzini
2021-12-13 19:45 ` Thomas Gleixner
2021-12-13 21:23 ` Thomas Gleixner
2021-12-14 7:16 ` Tian, Kevin
2021-12-08 0:03 ` [PATCH 11/19] kvm: x86: Check fpstate reallocation in XSETBV emulation Yang Zhong
2021-12-08 0:03 ` [PATCH 12/19] x86/fpu: Prepare KVM for bringing XFD state back in-sync Yang Zhong
2021-12-10 23:11 ` Thomas Gleixner
2021-12-08 0:03 ` [PATCH 13/19] kvm: x86: Disable WRMSR interception for IA32_XFD on demand Yang Zhong
2021-12-08 7:23 ` Liu, Jing2
2021-12-08 0:03 ` [PATCH 14/19] x86/fpu: Prepare for KVM XFD_ERR handling Yang Zhong
2021-12-10 16:16 ` Paolo Bonzini
2021-12-10 23:20 ` Thomas Gleixner
2021-12-08 0:03 ` [PATCH 15/19] kvm: x86: Save and restore guest XFD_ERR properly Yang Zhong
2021-12-10 16:23 ` Paolo Bonzini
2021-12-10 22:01 ` Paolo Bonzini
2021-12-12 13:10 ` Yang Zhong
2021-12-11 0:10 ` Thomas Gleixner
2021-12-11 1:31 ` Paolo Bonzini
2021-12-11 3:23 ` Tian, Kevin
2021-12-11 13:10 ` Thomas Gleixner
2021-12-11 3:07 ` Tian, Kevin
2021-12-11 13:29 ` Thomas Gleixner
2021-12-12 1:50 ` Tian, Kevin
2021-12-12 9:10 ` Paolo Bonzini
2021-12-08 0:03 ` [PATCH 16/19] kvm: x86: Introduce KVM_{G|S}ET_XSAVE2 ioctl Yang Zhong
2021-12-10 16:25 ` Paolo Bonzini
2021-12-10 16:30 ` Paolo Bonzini
2021-12-10 22:13 ` Paolo Bonzini
2021-12-13 8:23 ` Wang, Wei W
2021-12-13 9:24 ` Paolo Bonzini
2021-12-14 6:06 ` Wang, Wei W
2021-12-14 6:18 ` Paolo Bonzini
2021-12-15 2:39 ` Wang, Wei W
2021-12-15 13:42 ` Paolo Bonzini
2021-12-16 8:25 ` Wang, Wei W
2021-12-16 10:28 ` Paolo Bonzini
2021-12-20 17:54 ` State Component 18 and Palette 1 (Re: [PATCH 16/19] kvm: x86: Introduce KVM_{G|S}ET_XSAVE2 ioctl) Nakajima, Jun
2021-12-22 14:44 ` Paolo Bonzini
2021-12-22 23:47 ` Nakajima, Jun
2021-12-22 14:52 ` Dave Hansen
2021-12-22 23:51 ` Nakajima, Jun
2021-12-13 10:10 ` [PATCH 16/19] kvm: x86: Introduce KVM_{G|S}ET_XSAVE2 ioctl Thomas Gleixner
2021-12-13 10:43 ` Paolo Bonzini
2021-12-13 12:40 ` Thomas Gleixner
2021-12-08 0:03 ` [PATCH 17/19] docs: virt: api.rst: Document the new KVM_{G, S}ET_XSAVE2 ioctls Yang Zhong
2021-12-08 0:03 ` [PATCH 18/19] kvm: x86: AMX XCR0 support for guest Yang Zhong
2021-12-10 16:30 ` Paolo Bonzini
2021-12-08 0:03 ` [PATCH 19/19] kvm: x86: Add AMX CPUIDs support Yang Zhong
2021-12-10 21:52 ` Paolo Bonzini
2021-12-11 21:20 ` [PATCH 00/19] AMX Support in KVM 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=16c938e2-2427-c8dd-94a1-eba8f967283b@redhat.com \
--to=pbonzini@redhat.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=jing2.liu@intel.com \
--cc=jing2.liu@linux.intel.com \
--cc=jun.nakajima@intel.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
--cc=yang.zhong@intel.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®