Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp60081pxb; Mon, 25 Oct 2021 03:56:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweGRrGRe3egRaHAFanb8p+QupZPYXne692ieMByt06GHqWfKBT7HFp4+Xyzba7ARQlLinH X-Received: by 2002:a17:906:942:: with SMTP id j2mr21957776ejd.303.1635159363763; Mon, 25 Oct 2021 03:56:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635159363; cv=none; d=google.com; s=arc-20160816; b=1HuxNXJ3DfIAjpA+M9GPML3XZNdbpgluhdo0/gOHEoNhmriu4IhmJnhCda4gSgLY5J mJZF6A6WJdtQffl3wubHnRbquDAkw96Y8ZbjkrB+3PuC4OcSR2cOPIPxRyeRAmR3uM4e lrCWv5SW2MKi8rUhpLfyMrpS6drNkNLOZT27m3cEBYnCcplJh/vI5+riVgyBkP6OZsVm i5IqchytuHqhGSq3HACZ7L2oWhxWV/J/XBlkALY7F394nyBVMx1eN6qED043Qgy9U0kq PldA7hg5WJU8uaG7zp62lZ+AYZKXxElRSOS3sMVFnRPKTFQ6eITISQlix5715aeU6dEV pw/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=dAUS7NTV2o3+sFQ/jLHYjUDBGUyYYhF1lkpl3CpbYfk=; b=NbTYhP09D4GsHl4UAr+7MlPWLbpa3SHrqyGkmq2eMa0ai2AHKOSJ9iSVZBMk96hnOJ +n7gET8IXnVKkx03yaOom1013ZiyfaisJtTbFwF4917XF/i4PY/x14cTsX6fwkKptlHn 2/r8DmLea8LGWWwHOWzLIgS9DQASgJAaHolVjW4upUcqfpc+RuTza9ZsWD1E6tSiTM6N sM/pWrkYnfpgH2yiF8qTvNyMiWhb6UnJP/3By71lZSMYiGxFXQPDE6rWDedqwB766gA0 evaR5yCrtFj/ixa7P7FpfnABeV/B7bmAXNS3Qxnx79BBNj2UEu32kAe1YYJfUbvKAAgY 5nPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=iYgYqdyF; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o2si22084568ejd.219.2021.10.25.03.55.40; Mon, 25 Oct 2021 03:56:03 -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=@linutronix.de header.s=2020 header.b=iYgYqdyF; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232298AbhJYIj2 (ORCPT + 99 others); Mon, 25 Oct 2021 04:39:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232458AbhJYIjU (ORCPT ); Mon, 25 Oct 2021 04:39:20 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CDA9C061232; Mon, 25 Oct 2021 01:36:37 -0700 (PDT) Date: Mon, 25 Oct 2021 08:36:34 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1635150995; h=from:from:sender:sender:reply-to: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=dAUS7NTV2o3+sFQ/jLHYjUDBGUyYYhF1lkpl3CpbYfk=; b=iYgYqdyFHU4/6lf6lsu1MaGbm7x6GJqSMg4s0j5nTrXdXBB8yR6Zidl7ONAazUYGonOfNQ p3272p+FGISlY7PK2iSy2foCYntsoAQ6XmYvJViB8ZR9u1X3I3beTXcdC1QN24rNEi+gW9 jLu/kdy1T+sVrwRAblxc5as80mFj3+DjKAGzWkR4uQWwXIKzFr9zx6mJr5pr863je5qqJa AL+pl8JciZVMy5v43tS7a0EAzJhRovfSk0/yebdnef5sf1srFcHp6IXLZJ9MtcRRk1OkrM q7328kTkuFcqZzwWXipZeDeC4mHgSVy0om07sVdB67OGohY3AcmOfeARXBtqjQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1635150995; h=from:from:sender:sender:reply-to: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=dAUS7NTV2o3+sFQ/jLHYjUDBGUyYYhF1lkpl3CpbYfk=; b=bBbnvWKzYQDGAKEKsYfg+XeYrtKvdZwcuR+q/1SDl+vqdQGaTElTtU6qrSahuGpiVSVkGT uaYAr3WNcNpIkSBw== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/fpu] x86/fpu: Provide infrastructure for KVM FPU cleanup Cc: Thomas Gleixner , Borislav Petkov , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20211022185312.954684740@linutronix.de> References: <20211022185312.954684740@linutronix.de> MIME-Version: 1.0 Message-ID: <163515099487.626.4497497790299786860.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/fpu branch of tip: Commit-ID: b35633854ccb5cb0129e1cd160d55112f94cbdce Gitweb: https://git.kernel.org/tip/b35633854ccb5cb0129e1cd160d55112f94cbdce Author: Thomas Gleixner AuthorDate: Fri, 22 Oct 2021 20:55:51 +02:00 Committer: Borislav Petkov CommitterDate: Mon, 25 Oct 2021 10:22:07 +02:00 x86/fpu: Provide infrastructure for KVM FPU cleanup For the upcoming AMX support it's necessary to do a proper integration with KVM. Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run(). With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds. Provide: - An allocator which initializes the state properly - A replacement for the existing FPU swap mechanim Aside of the reduced memory footprint, this also makes state switching more efficient when TIF_FPU_NEED_LOAD is set. It does not require a memcpy as the state is already correct in the to be swapped out fpstate. The existing interfaces will be removed once KVM is converted over. [ bp: Include vmalloc.h explicitly to prevent build failures due to include files cleanups, courtesy of Stephen Rothwell: https://lore.kernel.org/r/20211025151144.552c60ca@canb.auug.org.au ] Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov Link: https://lkml.kernel.org/r/20211022185312.954684740@linutronix.de --- arch/x86/include/asm/fpu/api.h | 13 +++++- arch/x86/kernel/fpu/core.c | 86 ++++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/fpu/api.h b/arch/x86/include/asm/fpu/api.h index 9ce8314..de85bca 100644 --- a/arch/x86/include/asm/fpu/api.h +++ b/arch/x86/include/asm/fpu/api.h @@ -135,9 +135,22 @@ extern void fpu_init_fpstate_user(struct fpu *fpu); extern void fpstate_clear_xstate_component(struct fpstate *fps, unsigned int xfeature); /* KVM specific functions */ +extern bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu); +extern void fpu_free_guest_fpstate(struct fpu_guest *gfpu); +extern int fpu_swap_kvm_fpstate(struct fpu_guest *gfpu, bool enter_guest); extern void fpu_swap_kvm_fpu(struct fpu *save, struct fpu *rstor, u64 restore_mask); extern int fpu_copy_kvm_uabi_to_fpstate(struct fpu *fpu, const void *buf, u64 xcr0, u32 *pkru); extern void fpu_copy_fpstate_to_kvm_uabi(struct fpu *fpu, void *buf, unsigned int size, u32 pkru); +static inline void fpstate_set_confidential(struct fpu_guest *gfpu) +{ + gfpu->fpstate->is_confidential = true; +} + +static inline bool fpstate_is_confidential(struct fpu_guest *gfpu) +{ + return gfpu->fpstate->is_confidential; +} + #endif /* _ASM_X86_FPU_API_H */ diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 0fb9def..3c6b177 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -16,6 +16,7 @@ #include #include +#include #include "context.h" #include "internal.h" @@ -176,6 +177,75 @@ void fpu_reset_from_exception_fixup(void) } #if IS_ENABLED(CONFIG_KVM) +static void __fpstate_reset(struct fpstate *fpstate); + +bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu) +{ + struct fpstate *fpstate; + unsigned int size; + + size = fpu_user_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64); + fpstate = vzalloc(size); + if (!fpstate) + return false; + + __fpstate_reset(fpstate); + fpstate_init_user(fpstate); + fpstate->is_valloc = true; + fpstate->is_guest = true; + + gfpu->fpstate = fpstate; + return true; +} +EXPORT_SYMBOL_GPL(fpu_alloc_guest_fpstate); + +void fpu_free_guest_fpstate(struct fpu_guest *gfpu) +{ + struct fpstate *fps = gfpu->fpstate; + + if (!fps) + return; + + if (WARN_ON_ONCE(!fps->is_valloc || !fps->is_guest || fps->in_use)) + return; + + gfpu->fpstate = NULL; + vfree(fps); +} +EXPORT_SYMBOL_GPL(fpu_free_guest_fpstate); + +int fpu_swap_kvm_fpstate(struct fpu_guest *guest_fpu, bool enter_guest) +{ + struct fpstate *guest_fps = guest_fpu->fpstate; + struct fpu *fpu = ¤t->thread.fpu; + struct fpstate *cur_fps = fpu->fpstate; + + fpregs_lock(); + if (!cur_fps->is_confidential && !test_thread_flag(TIF_NEED_FPU_LOAD)) + save_fpregs_to_fpstate(fpu); + + /* Swap fpstate */ + if (enter_guest) { + fpu->__task_fpstate = cur_fps; + fpu->fpstate = guest_fps; + guest_fps->in_use = true; + } else { + guest_fps->in_use = false; + fpu->fpstate = fpu->__task_fpstate; + fpu->__task_fpstate = NULL; + } + + cur_fps = fpu->fpstate; + + if (!cur_fps->is_confidential) + restore_fpregs_from_fpstate(cur_fps, XFEATURE_MASK_FPSTATE); + + fpregs_mark_activate(); + fpregs_unlock(); + return 0; +} +EXPORT_SYMBOL_GPL(fpu_swap_kvm_fpstate); + void fpu_swap_kvm_fpu(struct fpu *save, struct fpu *rstor, u64 restore_mask) { fpregs_lock(); @@ -352,16 +422,20 @@ void fpstate_init_user(struct fpstate *fpstate) fpstate_init_fstate(fpstate); } +static void __fpstate_reset(struct fpstate *fpstate) +{ + /* Initialize sizes and feature masks */ + fpstate->size = fpu_kernel_cfg.default_size; + fpstate->user_size = fpu_user_cfg.default_size; + fpstate->xfeatures = fpu_kernel_cfg.default_features; + fpstate->user_xfeatures = fpu_user_cfg.default_features; +} + void fpstate_reset(struct fpu *fpu) { /* Set the fpstate pointer to the default fpstate */ fpu->fpstate = &fpu->__fpstate; - - /* Initialize sizes and feature masks */ - fpu->fpstate->size = fpu_kernel_cfg.default_size; - fpu->fpstate->user_size = fpu_user_cfg.default_size; - fpu->fpstate->xfeatures = fpu_kernel_cfg.default_features; - fpu->fpstate->user_xfeatures = fpu_user_cfg.default_features; + __fpstate_reset(fpu->fpstate); } #if IS_ENABLED(CONFIG_KVM)