public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Rik van Riel <riel@redhat.com>
To: Wanpeng Li <kernellwp@gmail.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>, kvm <kvm@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	David Hildenbrand <david@redhat.com>,
	Christian Borntraeger <borntraeger@de.ibm.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Radim Krcmar <rkrcmar@redhat.com>
Subject: Re: [PATCH 1/2] x86,kvm: move qemu/guest FPU switching out to vcpu_run
Date: Wed, 15 Nov 2017 09:40:46 -0500	[thread overview]
Message-ID: <1510756846.21121.289.camel@redhat.com> (raw)
In-Reply-To: <CANRm+Cxet5FDQWy+aNb21KZBCqbLiRvZM1T6G64j=S7b8_9kfw@mail.gmail.com>

On Wed, 2017-11-15 at 12:33 +0800, Wanpeng Li wrote:
> 2017-11-15 11:03 GMT+08:00 Rik van Riel <riel@redhat.com>:
> > On Wed, 2017-11-15 at 08:47 +0800, Wanpeng Li wrote:
> > > 2017-11-15 5:54 GMT+08:00  <riel@redhat.com>:
> > > > From: Rik van Riel <riel@redhat.com>
> > > > 
> > > > Currently, every time a VCPU is scheduled out, the host kernel
> > > > will
> > > > first save the guest FPU/xstate context, then load the qemu
> > > > userspace
> > > > FPU context, only to then immediately save the qemu userspace
> > > > FPU
> > > > context back to memory. When scheduling in a VCPU, the same
> > > > extraneous
> > > > FPU loads and saves are done.
> > > > 
> > > > This could be avoided by moving from a model where the guest
> > > > FPU is
> > > > loaded and stored with preemption disabled, to a model where
> > > > the
> > > > qemu userspace FPU is swapped out for the guest FPU context for
> > > > the duration of the KVM_RUN ioctl.
> > > 
> > > What will happen if CONFIG_PREEMPT is enabled?
> > 
> > The scheduler will save the guest FPU context when a
> > VCPU thread is preempted, and restore it when it is
> > scheduled back in.
> 
> I mean all the involved processes will use fpu. Before patch if
> kernel
> preempt occur:
> 
> context_switch
>   -> prepare_task_switch
>         -> fire_sched_out_preempt_notifiers
>               -> kvm_sched_out
>                     -> kvm_arch_vcpu_put
>                           -> kvm_put_guest_fpu
>                                -> copy_fpregs_to_fpstate(&vcpu-
> >arch.guest_fpu)
>                                     save xsave area to guest fpu
> buffer
>                                -> __kernel_fpu_end
>                                      ->
> copy_kernel_to_fpregs(&current->thread.fpu.state)
>                                          restore prev vCPU qemu
> userspace FPU to the xsave area
>   -> switch_to
>         -> __switch_to
>             -> switch_fpu_prepare
>                   -> copy_fpregs_to_fpstate => save xsave area to
> prev
> vCPU qemu userspace FPU
>             -> switch_fpu_finish
>                   -> copy_kernel_to_fpgregs => restore next task FPU
> to xsave area
> 
> 
> After the patch:
> 
> context_switch
>   -> prepare_task_switch
>         -> fire_sched_out_preempt_notifiers
>               -> kvm_sched_out
> 
>  -> switch_to
>         -> __switch_to
>             -> switch_fpu_prepare
>                   -> copy_fpregs_to_fpstate         => Oops
>                   save xsave area to prev vCPU qemu userspace FPU,
> actually the guest FPU buffer is loaded in xsave area, you transmit
> guest FPU in xsave area into the prev vCPU qemu userspace FPU

When entering kvm_arch_vcpu_ioctl_run we save the qemu userspace
FPU context in &vcpu->arch.user_fpu, and we restore that before
leaving kvm_arch_vcpu_ioctl_run.

Userspace should always see the userspace FPU context, no?

Am I overlooking anything?

  reply	other threads:[~2017-11-15 14:40 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-14 21:54 [PATCH v2 0/2] x86,kvm: move qemu/guest FPU switching out to kvm_arch_vcpu_ioctl_run riel
2017-11-14 21:54 ` [PATCH 1/2] x86,kvm: move qemu/guest FPU switching out to vcpu_run riel
2017-11-15  0:47   ` Wanpeng Li
2017-11-15  3:03     ` Rik van Riel
2017-11-15  4:33       ` Wanpeng Li
2017-11-15 14:40         ` Rik van Riel [this message]
2017-11-16  5:54           ` Wanpeng Li
2017-11-15  6:53   ` quan.xu04
2017-11-15 14:43     ` Rik van Riel
2017-11-15 15:09       ` Paolo Bonzini
2017-11-16  2:50       ` Quan Xu
2017-11-16  4:21         ` Rik van Riel
2017-11-16  5:06           ` Quan Xu
2017-11-16 10:21             ` Paolo Bonzini
2017-11-16 12:12               ` Quan Xu
2017-11-16 12:18                 ` Paolo Bonzini
2017-11-16 13:35                   ` Quan Xu
2017-11-16 13:39                     ` Paolo Bonzini
     [not found]                       ` <d67cef62-3165-50cd-36e8-be7c555dc79e@gmail.com>
2017-11-16 17:50                         ` Paolo Bonzini
2017-11-17  2:54                           ` Quan Xu
2017-11-14 21:54 ` [PATCH 2/2] x86,kvm: remove KVM emulator get_fpu / put_fpu riel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1510756846.21121.289.camel@redhat.com \
    --to=riel@redhat.com \
    --cc=borntraeger@de.ibm.com \
    --cc=david@redhat.com \
    --cc=kernellwp@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox