Re: [patch 13/31] x86/fpu: Move KVMs FPU swapping to FPU core

Thomas Gleixner
2021-10-14T19:14:29Z

Paolo,

On Thu, Oct 14 2021 at 17:01, Paolo Bonzini wrote:
> On 14/10/21 16:09, Thomas Gleixner wrote:
>> On Thu, Oct 14 2021 at 11:01, Paolo Bonzini wrote:
>>> On 14/10/21 10:02, Liu, Jing2 wrote:
>>> Based on the input from Andy and Thomas, the new way would be like this:
>>>
>>> 1) host_fpu must always be checked for reallocation in
>>> kvm_load_guest_fpu (or in the FPU functions that it calls, that depends
>>> on the rest of Thomas's patches). That's because arch_prctl can enable
>>> AMX for QEMU at any point after KVM_CREATE_VCPU.
>>
>> No.
>>
>> 1) QEMU starts
>> 2) QEMU requests permissions via prctl()
>> 3) QEMU creates vCPU threads
>>
>> Doing it the other way around makes no sense at all and wont work.
>
> Sure, but KVM needs to do something that makes sense even for userspaces
> that are not QEMU.
>
> For example, there could be a program that uses AMX *itself* and does
> not expose it to the guest. In that case, the arch_prctl can come at
> the point AMX is needed, which can be after the program creates vCPU
> threads. That's for host_fpu.

That wont affect the vCPU threads unless they start to use AMX in user
space themself. Which means they have the default buffer and their vCPU
user/guest FPU's too.

The prctl() sets the permission nothing else. As long as they don't use
AMX their XFD[18] stays set. Only when they start using AMX in user
space themself they trigger #NM which allocates a larger buffer for the
thread.

So then the point where it matters is fpu_swap_kvm_fpu() and that's
preemptible context so we can do allocations before fiddling with the
buffers. Not rocket science.

And that has nothing to do with the whole XCR0/XFD/XFD_ERR/#NM guest
mess.

> For the guest_fpu, I agree that the arch_prctl must come before creating
> vCPUs.

Good :)

>> vcpu_create()
>>
>> fpu_init_fpstate_user(guest_fpu, supported_xcr0)
>>
>> That will (it does not today) do:
>>
>> guest_fpu::__state_perm = supported_xcr0 & xstate_get_group_perm();
>>
>> The you have the information you need right in the guest FPU.
>
> Good, I wasn't aware of the APIs that will be there.

Me neither, but that's a pretty obvious consequence of the work I'm
doing for AMX. So I made it up for you. :)

>> This unconditionally calls into that allocation for every XCR0/XFD
>> trap ?
>
> Calls into the function, but doesn't necessarily allocate anything.

Sure.

> What you wrote below looks correct to me, thanks.
>
> Paolo
>

Properly quoting mail is hard, right?

>> Also you really should not wait until _all_ dynamic states are cleared
>> in guest XFD. Because a guest which has bit 18 and 19 available but only > uses one of them is going to trap on every other context switch due to
>> XFD writes.

Thanks,

tglx