From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5EAEC33CB3 for ; Fri, 17 Jan 2020 18:43:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BAE2A2082F for ; Fri, 17 Jan 2020 18:43:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729308AbgAQSng (ORCPT ); Fri, 17 Jan 2020 13:43:36 -0500 Received: from mga12.intel.com ([192.55.52.136]:35316 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726897AbgAQSnf (ORCPT ); Fri, 17 Jan 2020 13:43:35 -0500 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Jan 2020 10:43:34 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,331,1574150400"; d="scan'208";a="257916106" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.202]) by fmsmga002.fm.intel.com with ESMTP; 17 Jan 2020 10:43:34 -0800 Date: Fri, 17 Jan 2020 10:43:34 -0800 From: Sean Christopherson To: Dave Hansen Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Derek Yerger , kernel@najdan.com, Thomas Lambertz , Rik van Riel , Sebastian Andrzej Siewior , Borislav Petkov , Thomas Gleixner Subject: Re: [PATCH 1/4] KVM: x86: Handle TIF_NEED_FPU_LOAD in kvm_{load,put}_guest_fpu() Message-ID: <20200117184333.GF7175@linux.intel.com> References: <20200117062628.6233-1-sean.j.christopherson@intel.com> <20200117062628.6233-2-sean.j.christopherson@intel.com> <4d5dca91-8dbc-9ff3-b67a-2fa963da29cf@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4d5dca91-8dbc-9ff3-b67a-2fa963da29cf@intel.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > > --- > > 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, ¤t->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, ¤t->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, ¤t->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.