kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <sean.j.christopherson@intel.com>
To: Dave Hansen <dave.hansen@intel.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Derek Yerger <derek@djy.llc>,
	kernel@najdan.com, Thomas Lambertz <mail@thomaslambertz.de>,
	Rik van Riel <riel@surriel.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Borislav Petkov <bp@suse.de>,
	Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [PATCH 1/4] KVM: x86: Handle TIF_NEED_FPU_LOAD in kvm_{load,put}_guest_fpu()
Date: Fri, 17 Jan 2020 10:43:34 -0800	[thread overview]
Message-ID: <20200117184333.GF7175@linux.intel.com> (raw)
In-Reply-To: <4d5dca91-8dbc-9ff3-b67a-2fa963da29cf@intel.com>

On Fri, Jan 17, 2020 at 10:31:53AM -0800, Dave Hansen wrote:
> On 1/16/20 10:26 PM, Sean Christopherson wrote:
> > Handle TIF_NEED_FPU_LOAD similar to how fpu__copy() handles the flag
> > when duplicating FPU state to a new task struct.  TIF_NEED_FPU_LOAD can
> > be set any time control is transferred out of KVM, be it voluntarily,
> > e.g. if I/O is triggered during a KVM call to get_user_pages, or
> > involuntarily, e.g. if softirq runs after an IRQ occurs.  Therefore,
> > KVM must account for TIF_NEED_FPU_LOAD whenever it is (potentially)
> > accessing CPU FPU state.
> > 
> > Fixes: 5f409e20b7945 ("x86/fpu: Defer FPU state load until return to userspace")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
> > ---
> >  arch/x86/kvm/x86.c | 27 ++++++++++++++++++++++++---
> >  1 file changed, 24 insertions(+), 3 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index cf917139de6b..0c7211491f98 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -8476,8 +8476,20 @@ static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
> >  {
> >  	fpregs_lock();
> >  
> > -	copy_fpregs_to_fpstate(vcpu->arch.user_fpu);
> > -	/* PKRU is separately restored in kvm_x86_ops->run.  */
> > +	/*
> > +	 * If userspace's FPU state is not resident in the CPU registers, just
> > +	 * memcpy() from current, else save CPU state directly to user_fpu.
> > +	 */
> > +	if (test_thread_flag(TIF_NEED_FPU_LOAD))
> > +		memcpy(&vcpu->arch.user_fpu->state, &current->thread.fpu.state,
> > +		       fpu_kernel_xstate_size);
> > +	else
> > +		copy_fpregs_to_fpstate(vcpu->arch.user_fpu);
> > +
> > +	/*
> > +	 * Load guest's FPU state to the CPU registers.  PKRU is separately
> > +	 * loaded in kvm_x86_ops->run.
> > +	 */
> >  	__copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
> >  				~XFEATURE_MASK_PKRU);
> 
> Nit: it took me a minute to realize that there is both:
> 
> 	vcpu->arch.user_fpu
> and
> 	vcpu->arch.guest_fpu
> 
> It might help readability to have local variables for those, or at least
> a comment to help differentiate the two.

Or even better, add a helper to wrap the logic instead of copy+paste, e.g.:

static void kvm_save_current_fpu(struct fpu *fpu)
{
	if (test_thread_flag(TIF_NEED_FPU_LOAD))
		memcpy(&fpu->state, &current->thread.fpu.state,
		       fpu_kernel_xstate_size);
	else
		copy_fpregs_to_fpstate(fpu);
}

> 
> 
> > @@ -8492,7 +8504,16 @@ static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
> >  {
> >  	fpregs_lock();
> >  
> > -	copy_fpregs_to_fpstate(vcpu->arch.guest_fpu);
> > +	/*
> > +	 * If guest's FPU state is not resident in the CPU registers, just
> > +	 * memcpy() from current, else save CPU state directly to guest_fpu.
> > +	 */
> > +	if (test_thread_flag(TIF_NEED_FPU_LOAD))
> > +		memcpy(&vcpu->arch.guest_fpu->state, &current->thread.fpu.state,
> > +		       fpu_kernel_xstate_size);
> > +	else
> > +		copy_fpregs_to_fpstate(vcpu->arch.guest_fpu);
> > +
> >  	copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
> >  
> >  	fpregs_mark_activate();
> 
> This also makes me wonder if we want to have copy_fpregs_to_fpstate()
> check for TIF_NEED_FPU_LOAD and complain if it's set.

  reply	other threads:[~2020-01-17 18:43 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-17  6:26 [PATCH 0/4] KVM: x86: TIF_NEED_FPU_LOAD bug fixes Sean Christopherson
2020-01-17  6:26 ` [PATCH 1/4] KVM: x86: Handle TIF_NEED_FPU_LOAD in kvm_{load,put}_guest_fpu() Sean Christopherson
2020-01-17 18:31   ` Dave Hansen
2020-01-17 18:43     ` Sean Christopherson [this message]
2020-01-17  6:26 ` [PATCH 2/4] KVM: x86: Ensure guest's FPU state is loaded when accessing for emulation Sean Christopherson
2020-01-17  6:26 ` [PATCH 3/4] KVM: x86: Revert "KVM: X86: Fix fpu state crash in kvm guest" Sean Christopherson
2020-03-04  7:41   ` Liu, Jing2
2020-03-04  7:58     ` Paolo Bonzini
2020-01-17  6:26 ` [PATCH 4/4] KVM: x86: Remove unused ctxt param from emulator's FPU accessors Sean Christopherson
2020-03-04  7:38 ` [PATCH 0/4] KVM: x86: TIF_NEED_FPU_LOAD bug fixes Liu, Jing2
2020-03-04 15:24   ` Sean Christopherson

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=20200117184333.GF7175@linux.intel.com \
    --to=sean.j.christopherson@intel.com \
    --cc=bigeasy@linutronix.de \
    --cc=bp@suse.de \
    --cc=dave.hansen@intel.com \
    --cc=derek@djy.llc \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kernel@najdan.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mail@thomaslambertz.de \
    --cc=pbonzini@redhat.com \
    --cc=riel@surriel.com \
    --cc=tglx@linutronix.de \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.com \
    /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;
as well as URLs for NNTP newsgroup(s).