Re: [kvm PATCH v5 2/4] kvm: x86: Dynamically allocate guest_fpu

Dave Hansen
2018-10-31T14:11:08Z

On 10/31/18 6:26 AM, Marc Orr wrote:
> r = -ENOMEM;
> + x86_fpu_cache = kmem_cache_create_usercopy(
> + "x86_fpu",
> + sizeof(struct fpu),
> + __alignof__(struct fpu),
> + SLAB_ACCOUNT,
> + offsetof(struct fpu, state),
> + sizeof_field(struct fpu, state),
> + NULL);

We should basically never be using sizeof(struct fpu), anywhere. As you
saw, it's about a page in size, but the actual hardware FPU structure
can be as small as ~500 bytes or as big as ~3k. Doing it this way is a
pretty unnecessary waste of memory because sizeof(struct fpu) is sized
for the worst-case (largest) possible XSAVE buffer that we support on
*any* CPU. It will also get way worse if anyone ever throws a bunch
more state into the XSAVE area and we need to make it way bigger.

If you want a kmem cache for this, I'd suggest creating a cache which is
the size of the host XSAVE buffer. That can be found in a variable
called 'fpu_kernel_xstate_size'. I'm assuming here that the guest FPU
is going to support a strict subset of host kernel XSAVE states.

The other alternative is to calculate the actual size of the XSAVE
buffer that the guest needs. You can do that from the values that KVM
sets to limit guest XCR0 values (the name of the control field is
escaping me at the moment).