Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2638222pxb; Tue, 12 Oct 2021 10:23:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTY/btwMC7cOLxA2T0bO5pXUpDQPdzf+RzUtdBgHJnBlDI9ngvqoynwkwTFmBZqnSz4EeM X-Received: by 2002:a63:df06:: with SMTP id u6mr23993795pgg.148.1634059424232; Tue, 12 Oct 2021 10:23:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634059424; cv=none; d=google.com; s=arc-20160816; b=vlXT8ddO93Fo60KVC3pm0lAGmxEbTiQ1Z/ScKaesxvn+yw/UDtjzZX32FDBGR+Sn45 g6rjnJNMhBH8G/HNV95kvM++ECx6j7VtKthy8GH5hBZ8BnkN7k29M4nwl8+C8ovSmvhv a4Xod7Jqs5Rn09OVzagp2lA3RwvhQWxBy43DLMWDO3YrtC2ccIQyuuXnaiCgRdrqPWEd yVWadHitqDpi4NsMOzsi2ncY7HoYUrEJ2WOZupa4gAPVtVnq8akUPiqNtpg15W1Oosc9 0eDco6lgsP2HXbZQT++vEwXZgUyY5OPVXxDrZ1sWDeKlbcQ8/zSoJs7NEg0vFQH9urO6 jGIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=RJ4hKggkAg1oAa2rnQG+wzzAGkhAl0EY2OE5++4GObU=; b=z3HHqMHgrKBDukqywSld3A9FBwvvCPfyUmoIbiwF7p2bQEeuq+LFrBj9pL+dzlFp7u PP0CNsi5HKNxE3N/NnD3V8J+zU8qAdiMLwYZdT/qbTtKBLUi5X92VNd4kVXmrX+FlTou i/bKNG87W2p0Xf5Xy1kKjMCu6SDFxGQWF8EkE24RTkUiFc01M/ojzoM0mmU7MU3vlsAC qyE4woYkCEiueaUAhMpjNEMr7Mk4I/DvPg6uCW7v+fhImBnwZODDXhsYWNsK6WPlZUl3 6HClm46JZgSPfVLtq+GioqUReOrI1pCMTv7ue4Gs211BcFkJPwotL1L2V9EGBIBRXcpB P1Ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GRHXfUIL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v20si4850033pjn.155.2021.10.12.10.23.31; Tue, 12 Oct 2021 10:23:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GRHXfUIL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232387AbhJLRYY (ORCPT + 99 others); Tue, 12 Oct 2021 13:24:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:27546 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232374AbhJLRYU (ORCPT ); Tue, 12 Oct 2021 13:24:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634059337; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RJ4hKggkAg1oAa2rnQG+wzzAGkhAl0EY2OE5++4GObU=; b=GRHXfUILnFuISMr9lA5EomcRJdnuTIxlaniKfz6pIXXvLk4naKRYRKutKEJl2i7HkkuD9h 0ICfhZwg7IfE8IkzZOi68mfu5bmHLK8OWRQ1d0vJoHQs1QI/ufadZKFXO65+bgWWLw3okZ RpmO+9CUTm/+1wBLRuEzLMIQqAbggBM= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-490-LxXC8qj-OQODeSM4DL22Ng-1; Tue, 12 Oct 2021 13:22:16 -0400 X-MC-Unique: LxXC8qj-OQODeSM4DL22Ng-1 Received: by mail-wr1-f72.google.com with SMTP id 41-20020adf812c000000b00160dfbfe1a2so12672801wrm.3 for ; Tue, 12 Oct 2021 10:22:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=RJ4hKggkAg1oAa2rnQG+wzzAGkhAl0EY2OE5++4GObU=; b=h4170DKNXOao8zweR0q2g2j9M1BCZLMxHD1NQqoVZp0wf6q/E3EFEu33ryDReYtYHY FpmBfmCR942eKT9D3CG9G/6nAIUAehXkMGq9OnPXXFNTdAyEsjd299daaC7F2x40+XoR CQ0dHziAYw1nKWctxadDxmfrHnRBs3Fs8yBbOSPMDs5rMbOrR+4SElIClkYKqXXSmysy Xr2g+G8MdNRxSCvHy9QQyMqjsyck17H1O/ZGQRr8Kk84tJHXTtz4jxpbsXEQbS5aVXoN jZHqBF/4OulMV1jyCN/M59TRFhkUzWft843cd6MbbJLgjsxzzFL0TsTd3LzelV5NyzWr o7FA== X-Gm-Message-State: AOAM533Hg7ickqpKKCozrVErCiUq1nyHIi2Y7I4f6ZYlQl8p6Ccuycdk vCzgiwk5hMqA5qCZiv0jkhGHybrhxwoKYpQvZoZfwwJ5IlpJUzqTQ9WuvzkOdK0z65BoHygoY3u x/7+zWwl1Z6QaSbXwpV7AnSNz X-Received: by 2002:adf:a390:: with SMTP id l16mr33839911wrb.104.1634059335294; Tue, 12 Oct 2021 10:22:15 -0700 (PDT) X-Received: by 2002:adf:a390:: with SMTP id l16mr33839870wrb.104.1634059334928; Tue, 12 Oct 2021 10:22:14 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id v23sm3002264wmj.4.2021.10.12.10.22.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 Oct 2021 10:22:13 -0700 (PDT) Message-ID: <8a5762ab-18d5-56f8-78a6-c722a2f387c5@redhat.com> Date: Tue, 12 Oct 2021 19:22:12 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [patch 13/31] x86/fpu: Move KVMs FPU swapping to FPU core Content-Language: en-US To: Thomas Gleixner , LKML Cc: x86@kernel.org, "Chang S. Bae" , Dave Hansen , Arjan van de Ven , kvm@vger.kernel.org References: <20211011215813.558681373@linutronix.de> <20211011223611.069324121@linutronix.de> From: Paolo Bonzini In-Reply-To: <20211011223611.069324121@linutronix.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12/10/21 02:00, Thomas Gleixner wrote: > Swapping the host/guest FPU is directly fiddling with FPU internals which > requires 5 exports. The upcoming support of dymanically enabled states > would even need more. > > Implement a swap function in the FPU core code and export that instead. > > Signed-off-by: Thomas Gleixner > Cc: kvm@vger.kernel.org > Cc: Paolo Bonzini > --- > arch/x86/include/asm/fpu/api.h | 8 +++++ > arch/x86/include/asm/fpu/internal.h | 15 +--------- > arch/x86/kernel/fpu/core.c | 30 ++++++++++++++++++--- > arch/x86/kernel/fpu/init.c | 1 > arch/x86/kernel/fpu/xstate.c | 1 > arch/x86/kvm/x86.c | 51 +++++++----------------------------- > arch/x86/mm/extable.c | 2 - > 7 files changed, 48 insertions(+), 60 deletions(-) > > --- a/arch/x86/include/asm/fpu/api.h > +++ b/arch/x86/include/asm/fpu/api.h > @@ -12,6 +12,8 @@ > #define _ASM_X86_FPU_API_H > #include > > +#include > + > /* > * Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It > * disables preemption so be careful if you intend to use it for long periods > @@ -108,4 +110,10 @@ extern int cpu_has_xfeatures(u64 xfeatur > > static inline void update_pasid(void) { } > > +/* FPSTATE related functions which are exported to KVM */ > +extern void fpu_init_fpstate_user(struct fpu *fpu); > + > +/* KVM specific functions */ > +extern void fpu_swap_kvm_fpu(struct fpu *save, struct fpu *rstor, u64 restore_mask); > + > #endif /* _ASM_X86_FPU_API_H */ > --- a/arch/x86/include/asm/fpu/internal.h > +++ b/arch/x86/include/asm/fpu/internal.h > @@ -74,14 +74,8 @@ static __always_inline __pure bool use_f > return static_cpu_has(X86_FEATURE_FXSR); > } > > -/* > - * fpstate handling functions: > - */ > - > extern union fpregs_state init_fpstate; > - > extern void fpstate_init_user(union fpregs_state *state); > -extern void fpu_init_fpstate_user(struct fpu *fpu); > > #ifdef CONFIG_MATH_EMULATION > extern void fpstate_init_soft(struct swregs_state *soft); > @@ -381,12 +375,7 @@ static inline int os_xrstor_safe(struct > return err; > } > > -extern void __restore_fpregs_from_fpstate(union fpregs_state *fpstate, u64 mask); > - > -static inline void restore_fpregs_from_fpstate(union fpregs_state *fpstate) > -{ > - __restore_fpregs_from_fpstate(fpstate, xfeatures_mask_fpstate()); > -} > +extern void restore_fpregs_from_fpstate(union fpregs_state *fpstate, u64 mask); > > extern bool copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size); > > @@ -467,7 +456,7 @@ static inline void fpregs_restore_userre > */ > mask = xfeatures_mask_restore_user() | > xfeatures_mask_supervisor(); > - __restore_fpregs_from_fpstate(&fpu->state, mask); > + restore_fpregs_from_fpstate(&fpu->state, mask); > > fpregs_activate(fpu); > fpu->last_cpu = cpu; > --- a/arch/x86/kernel/fpu/core.c > +++ b/arch/x86/kernel/fpu/core.c > @@ -124,9 +124,8 @@ void save_fpregs_to_fpstate(struct fpu * > asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave)); > frstor(&fpu->state.fsave); > } > -EXPORT_SYMBOL(save_fpregs_to_fpstate); > > -void __restore_fpregs_from_fpstate(union fpregs_state *fpstate, u64 mask) > +void restore_fpregs_from_fpstate(union fpregs_state *fpstate, u64 mask) > { > /* > * AMD K7/K8 and later CPUs up to Zen don't save/restore > @@ -151,7 +150,31 @@ void __restore_fpregs_from_fpstate(union > frstor(&fpstate->fsave); > } > } > -EXPORT_SYMBOL_GPL(__restore_fpregs_from_fpstate); > + > +#if IS_ENABLED(CONFIG_KVM) > +void fpu_swap_kvm_fpu(struct fpu *save, struct fpu *rstor, u64 restore_mask) > +{ > + fpregs_lock(); > + > + if (save) { > + if (test_thread_flag(TIF_NEED_FPU_LOAD)) { > + memcpy(&save->state, ¤t->thread.fpu.state, > + fpu_kernel_xstate_size); > + } else { > + save_fpregs_to_fpstate(save); > + } > + } > + > + if (rstor) { > + restore_mask &= xfeatures_mask_fpstate(); > + restore_fpregs_from_fpstate(&rstor->state, restore_mask); > + } > + > + fpregs_mark_activate(); > + fpregs_unlock(); > +} > +EXPORT_SYMBOL_GPL(fpu_swap_kvm_fpu); > +#endif > > void kernel_fpu_begin_mask(unsigned int kfpu_mask) > { > @@ -459,7 +482,6 @@ void fpregs_mark_activate(void) > fpu->last_cpu = smp_processor_id(); > clear_thread_flag(TIF_NEED_FPU_LOAD); > } > -EXPORT_SYMBOL_GPL(fpregs_mark_activate); > > /* > * x87 math exception handling: > --- a/arch/x86/kernel/fpu/init.c > +++ b/arch/x86/kernel/fpu/init.c > @@ -136,7 +136,6 @@ static void __init fpu__init_system_gene > * components into a single, continuous memory block: > */ > unsigned int fpu_kernel_xstate_size __ro_after_init; > -EXPORT_SYMBOL_GPL(fpu_kernel_xstate_size); > > /* Get alignment of the TYPE. */ > #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test) > --- a/arch/x86/kernel/fpu/xstate.c > +++ b/arch/x86/kernel/fpu/xstate.c > @@ -65,7 +65,6 @@ static short xsave_cpuid_features[] __in > * XSAVE buffer, both supervisor and user xstates. > */ > u64 xfeatures_mask_all __ro_after_init; > -EXPORT_SYMBOL_GPL(xfeatures_mask_all); > > static unsigned int xstate_offsets[XFEATURE_MAX] __ro_after_init = > { [ 0 ... XFEATURE_MAX - 1] = -1}; > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -68,7 +68,9 @@ > #include > #include > #include > -#include /* Ugh! */ > +#include > +#include > +#include > #include > #include > #include > @@ -9899,58 +9901,27 @@ static int complete_emulated_mmio(struct > return 0; > } > > -static void kvm_save_current_fpu(struct fpu *fpu) > -{ > - /* > - * If the target FPU state is not resident in the CPU registers, just > - * memcpy() from current, else save CPU state directly to the target. > - */ > - if (test_thread_flag(TIF_NEED_FPU_LOAD)) > - memcpy(&fpu->state, ¤t->thread.fpu.state, > - fpu_kernel_xstate_size); > - else > - save_fpregs_to_fpstate(fpu); > -} > - > /* Swap (qemu) user FPU context for the guest FPU context. */ > static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) > { > - fpregs_lock(); > - > - kvm_save_current_fpu(vcpu->arch.user_fpu); > - > /* > - * Guests with protected state can't have it set by the hypervisor, > - * so skip trying to set it. > + * Guest with protected state have guest_fpu == NULL which makes > + * the swap only safe the host state. Exclude PKRU from restore as > + * it is restored separately in kvm_x86_ops.run(). > */ > - if (vcpu->arch.guest_fpu) > - /* PKRU is separately restored in kvm_x86_ops.run. */ > - __restore_fpregs_from_fpstate(&vcpu->arch.guest_fpu->state, > - ~XFEATURE_MASK_PKRU); > - > - fpregs_mark_activate(); > - fpregs_unlock(); > - > + fpu_swap_kvm_fpu(vcpu->arch.user_fpu, vcpu->arch.guest_fpu, > + ~XFEATURE_MASK_PKRU); > trace_kvm_fpu(1); > } > > /* When vcpu_run ends, restore user space FPU context. */ > static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) > { > - fpregs_lock(); > - > /* > - * Guests with protected state can't have it read by the hypervisor, > - * so skip trying to save it. > + * Guest with protected state have guest_fpu == NULL which makes > + * swap only restore the host state. > */ > - if (vcpu->arch.guest_fpu) > - kvm_save_current_fpu(vcpu->arch.guest_fpu); > - > - restore_fpregs_from_fpstate(&vcpu->arch.user_fpu->state); > - > - fpregs_mark_activate(); > - fpregs_unlock(); > - > + fpu_swap_kvm_fpu(vcpu->arch.guest_fpu, vcpu->arch.user_fpu, ~0ULL); > ++vcpu->stat.fpu_reload; > trace_kvm_fpu(0); > } > --- a/arch/x86/mm/extable.c > +++ b/arch/x86/mm/extable.c > @@ -47,7 +47,7 @@ static bool ex_handler_fprestore(const s > WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.", > (void *)instruction_pointer(regs)); > > - __restore_fpregs_from_fpstate(&init_fpstate, xfeatures_mask_fpstate()); > + restore_fpregs_from_fpstate(&init_fpstate, xfeatures_mask_fpstate()); > return true; > } > > Reviewed-by: Paolo Bonzini