Re: [PATCH 16/19] kvm: x86: Introduce KVM_{G|S}ET_XSAVE2 ioctl

Paolo Bonzini
2021-12-13T09:24:19Z

On 12/13/21 09:23, Wang, Wei W wrote:
> On Saturday, December 11, 2021 6:13 AM, Paolo Bonzini wrote:
>>
>> By the way, I think KVM_SET_XSAVE2 is not needed. Instead:
>>
>> - KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2) should return the size of the
>> buffer that is passed to KVM_GET_XSAVE2
>>
>> - KVM_GET_XSAVE2 should fill in the buffer expecting that its size is
>> whatever KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2) passes
>>
>> - KVM_SET_XSAVE can just expect a buffer that is bigger than 4k if the
>> save states recorded in the header point to offsets larger than 4k.
>
> I think one issue is that KVM_SET_XSAVE works with "struct kvm_xsave" (hardcoded 4KB buffer),
> including kvm_vcpu_ioctl_x86_set_xsave. The states obtained via KVM_GET_XSAVE2 will be made
> using "struct kvm_xsave2".
>
> Did you mean that we could add a new code path under KVM_SET_XSAVE to make it work with
> the new "struct kvm_xsave2"?

There is no need for struct kvm_xsave2, because there is no need for a
"size" argument.

- KVM_GET_XSAVE2 *is* needed, and it can expect a buffer as big as the
return value of KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)

- but KVM_SET_XSAVE2 is not needed, because KVM_SET_XSAVE can use
copy_from_user to read the XSTATE_BV, use it to deduce the size of the
buffer, and use copy_from_user to read the full size of the buffer.

For this to work you can redefine struct kvm_xsave to

struct kvm_xsave {
__u32 region[1024];

/*
* KVM_GET_XSAVE only uses 4096 bytes and only returns
* user save states up to save state 17 (TILECFG).
*
* For KVM_GET_XSAVE2, the total size of region + extra
* must be the size that is communicated by
* KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2).
*
* KVM_SET_XSAVE uses the extra field if the struct was
* returned by KVM_GET_XSAVE2.
*/
__u32 extra[];
}

Paolo