Re: [PATCH 09/19] kvm: x86: Prepare reallocation check

Paolo Bonzini
2021-12-13T09:16:32Z

On 12/8/21 01:03, Yang Zhong wrote:
> + u64 xcr0 = vcpu->arch.xcr0 & XFEATURE_MASK_USER_DYNAMIC;
> +
> + /* For any state which is enabled dynamically */
> + if ((xfd & xcr0) != xcr0) {
> + u64 request = (xcr0 ^ xfd) & xcr0;
> + struct fpu_guest *guest_fpu = &vcpu->arch.guest_fpu;
> +
> + /*
> + * If requested features haven't been enabled, update
> + * the request bitmap and tell the caller to request
> + * dynamic buffer reallocation.
> + */
> + if ((guest_fpu->user_xfeatures & request) != request) {
> + vcpu->arch.guest_fpu.realloc_request = request;

This should be "|=". If you have

wrmsr(XFD, dynamic-feature-1);
...
wrmsr(XFD, dynamic-feature-2);

then the space for both features has to be allocated.

> + return true;
> + }
> + }
> +

This is just:

struct fpu_guest *guest_fpu = &vcpu->arch.guest_fpu;
u64 xcr0 = vcpu->arch.xcr0 & XFEATURE_MASK_USER_DYNAMIC;
u64 dynamic_enabled = xcr0 & ~xfd;
if (!(dynamic_enabled & ~guest_fpu->user_xfeatures))
return false;

/*
* This should actually not be in guest_fpu, see review of
* patch 2. Also see above regarding "=" vs "|=".
*/
vcpu->arch.guest_fpu.realloc_request |= dynamic_enabled;
return true;

But without documentation I'm not sure why this exit-to-userspace step
is needed:

- if (dynamic_enabled & ~guest_fpu->user_perm) != 0, then this is a
userspace error and you can #GP the guest without any issue. Userspace
is buggy

- if (dynamic_enabled & ~guest_fpu->user_xfeatures) != 0, but the
feature *is* permitted, then it is okay to just go on and reallocate the
guest FPU.

Paolo