RE: [PATCH 13/19] kvm: x86: Disable WRMSR interception for IA32_XFD on demand

Liu, Jing2
2021-12-08T07:23:27Z

On 12/8/2021 8:03 AM, Yang Zhong wrote:
> From: Jing Liu <jing2.liu@intel.com>
>
> Always intercepting IA32_XFD causes non-negligible overhead when this
> register is updated frequently in the guest.
>
> Disable WRMSR interception to IA32_XFD after fpstate reallocation is
> completed. There are three options for when to disable the
> interception:
>
> 1) When emulating the 1st WRMSR which requires reallocation,
> disable interception before exiting to userapce with the
> assumption that the userspace VMM should not bounch back to
> the kernel if reallocation fails. However it's not good to
> design kernel based on application behavior. If due to bug
> the vCPU thread comes back to the kernel after reallocation
> fails, XFD passthrough may lead to host memory corruption
> when doing XSAVES for guest fpstate which has a smaller size
> than what guest XFD allows.
>
> 2) Disable interception when coming back from the userspace VMM
> (for the 1st WRMSR which triggers reallocation). Re-check
> whether fpstate size can serve the new guest XFD value. Disable
> interception only when the check succeeds. This requires KVM
> to store guest XFD value in some place and then compare it
> to guest_fpu::user_xfeatures in the completion handler.

For option 2), we are considering that fpstate->size can be used to indicate
if reallocation is successful. Because once one of the XFD features (today,
it's AMX) is enabled, kernel need reallocate full size, otherwise, KVM has no
chance to reallocate for other XFD features later since it's non-trapped (to
avoid WRMSR VM EXITs due to guest toggling XFD).

Then KVM doesn't need to store guest XFD value in some place. And kernel
fpu core may need an API to tell guest permitted size for KVM.

Thanks,
Jing

>
> 3) Disable interception at the 2nd WRMSR which enables dynamic
> XSTATE features. If guest_fpu::user_xfeatures already includes
> bits for dynamic features set in guest XFD value, disable
> interception.
>