2017-11-16 16:26:32

by Quan Xu

[permalink] [raw]
Subject: Re: [PATCH 1/2] x86,kvm: move qemu/guest FPU switching out to vcpu_run



On 2017-11-16 20:18, Paolo Bonzini wrote:
> On 16/11/2017 13:12, Quan Xu wrote:
>> However it seems there is still some gap..
>>
>> as Rik said, "at context switch time, the context switch code will save
>> the guest FPU state to current->thread.fpu when the VCPU thread is scheduled out."
> By "guest FPU state" Rik means "guest FPU with host PKRU".
  :( actually it is host_pkru, just with different names..

> Guest PKRU is always stored in vcpu->arch.pkru rather than in the guest
> FPU state, so guest PKRU will never be in current->thread.fpu.state either.
>
> KVM_GET_XSAVE will the guest FPU state with vcpu->arch.pkru and
> migration will work properly.
  agreed, I fix it.. that's why I concern.. there are so much methods to
  write PKRU with host_pkru/guest_pkru..

 after migration, KVM_GET|SET_XSAVE restore the right pkru.. but we
introduce
 another method:

-- When the VCPU thread is scheduled back in, the context
switch code will restore current->thread.fpu to the FPU
registers.


there is still a window to restore current->thread.fpu to the FPU
registers before enter guest mode and

preempt_disable().

on target machine, after migration, the pkru value is source_host_pkru
in current->thread.fpu.

in case,

            source_host_pkru_value != guest_pkru_value,
            target_host_pkru_value == guest_pkru_value..

source_host_pkru_value may be restored to PKRU.. make pkru status inconsistent..


thanks

Quan
Alibaba Cloud
> Thanks,
>
> Paolo
>
>> after preempt_enable() in vcpu_enter_guest(), the vcpu thread is
>> scheduled out,
>> in kvm_x86_ops->run, the PKRU has been restored with host_pkru (IF
>> guest_pkru != host_pkru)..
>> instead of guest_pkru..
>>
>> then the PKRU is host_pkru, how to save guest_pkru current->thread.fpu?
>>
>> as mentioned, all this _assumes_ that the pkru is in
>> current->thread.fpu.state as well.
>>
>>
>> thanks,
>>
>> Quan
>> Alibaba Cloud
>>
>>> Context switching will only happen in vcpu_enter_guest() after
>>> preempt_enable() for a preemptible kernel, or in vcpu_run via
>>> cond_resched() for a non-preemptible kernel.
>>>
>>> Thanks,
>>>
>>> Paolo
>>>
>>>> VM migration again, in case,
>>>>             source_host_pkru_value != guest_pkru_value,
>>>>             target_host_pkru_value == guest_pkru_value..
>>>>
>>>> the pkru status would be inconsistent..
>


From 1584237323722403554@xxx Thu Nov 16 15:34:06 +0000 2017
X-GM-THRID: 1584017174910331026
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread


2017-11-16 15:34:06

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH 1/2] x86,kvm: move qemu/guest FPU switching out to vcpu_run

On 16/11/2017 14:35, Quan Xu wrote:
> but we introduce another method:
>
>       -- When the VCPU thread is scheduled back in, the context
>          switch code will restore current->thread.fpu to the FPU
>          registers.
>
>
> there is still a window to restore current->thread.fpu to the FPU
> registers before enter guest mode and
>
> preempt_disable().

That will always use the host PKRU. The guest PKRU is _never_ visible
to the context switch code, because it's only ever used in a section
that runs with preemption disabled.

It's actually much simpler than before.

Paolo

> on target machine, after migration, the pkru value is source_host_pkru
> in current->thread.fpu.
>
> in case,
>
>             source_host_pkru_value != guest_pkru_value,
>             target_host_pkru_value == guest_pkru_value..
>
> source_host_pkru_value may be restored to PKRU.. make pkru status
> inconsistent..


From 1584234050009989527@xxx Thu Nov 16 14:42:04 +0000 2017
X-GM-THRID: 1584017174910331026
X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread