Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5895548pxu; Wed, 23 Dec 2020 08:03:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJz42WxjlM8htO22v1DfqxDTWBo+/1wBZwojPjFbl2hBKLYnOLOmsCGdY3gHNvgrriT0p/4p X-Received: by 2002:a17:906:b7cc:: with SMTP id fy12mr24812426ejb.44.1608739425378; Wed, 23 Dec 2020 08:03:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608739425; cv=none; d=google.com; s=arc-20160816; b=NGUf6j3Z5dzmG8tOmLXjIOMm8JFCVr0p6dmGkWzmGBIaHND9UnNwGScRemj2cYONBM aGUyUBU+uMA9oGBwju6jxKpT1DovTxAoQmPa2XRZbR3C7cb3XdRhmwAdhspomaRew8A7 f7piqe2uYNncUk1On0vMhovIWpiF8zbXe9FqkUfe+c1+kYCXr4Bp0dW3ajI/77iL1g5n TTlAyuv1VxwTeV4VUeEzffE95Bn8VV5Ahi0ALfQgeyWvLPPDYil2l1nw4ma5k+rKsTRU 9RvrqOamYgzaqW+y+07DJisl8PTS0wMJT5RFtstuC3ncTDtwW479aukrbDnTXPht2Dey e9+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:ironport-sdr:ironport-sdr; bh=4bPknZ6P99CRIlqVOh7PSD2HiaCqaeZgiBDlGEGMnrA=; b=oc+OIR/9/9aHcS//a+852LFywcltoZ7mPLKD6PVyS51zPGYFzl8RK59ml4VpF/rrD+ 4PwlZU1zQ1Nn9RViUlfm+WtmJ/tGjYeog/bUSJXM8bQ3GSrG/GZ5Z7ntNybJ+I+/+P4Y clev/bEb+ytoQbUNJtXUa0AX5rK0xrJ7LM9d/IIFc5OMmXkWbvAKGq7F60G0Ifk77HPv VByKsA7Nl9aUw2yc7vhVbbrttw6rPT3gARSNb9HE3XRLt7QZV125NIGhYPe0ThpKa/TT f9Rly88hkyur+ndwKhs5YJLJ2H81+rLb8TSUFSR0z7LjSHnU0ZVQmjDkotruY/wpH/MJ yOYA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c20si14128727ejr.339.2020.12.23.08.03.23; Wed, 23 Dec 2020 08:03:45 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728402AbgLWQCW (ORCPT + 99 others); Wed, 23 Dec 2020 11:02:22 -0500 Received: from mga01.intel.com ([192.55.52.88]:1574 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728058AbgLWQCW (ORCPT ); Wed, 23 Dec 2020 11:02:22 -0500 IronPort-SDR: aVOR/DHJpbLUIEtwuIf5WhC22ddTqymsyD8mlp2+5FqlztWNJO9SoKbNg94GhFYv/kNEgskK5J n1KsDxSGwFxw== X-IronPort-AV: E=McAfee;i="6000,8403,9844"; a="194483184" X-IronPort-AV: E=Sophos;i="5.78,441,1599548400"; d="scan'208";a="194483184" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Dec 2020 08:01:40 -0800 IronPort-SDR: 1XSvzBXp0Fq7V9+HAcYCNMBzL0Bomh+nP20y493IPL/Hp1o9VRVnrfhje+ZdA2/3L7Yl1qJfeL nwj956lp2v8g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,441,1599548400"; d="scan'208";a="458027979" Received: from chang-linux-3.sc.intel.com ([172.25.66.175]) by fmsmga001.fm.intel.com with ESMTP; 23 Dec 2020 08:01:40 -0800 From: "Chang S. Bae" To: bp@suse.de, luto@kernel.org, tglx@linutronix.de, mingo@kernel.org, x86@kernel.org Cc: len.brown@intel.com, dave.hansen@intel.com, jing2.liu@intel.com, ravi.v.shankar@intel.com, linux-kernel@vger.kernel.org, chang.seok.bae@intel.com, kvm@vger.kernel.org Subject: [PATCH v3 10/21] x86/fpu/xstate: Update xstate save function to support dynamic xstate Date: Wed, 23 Dec 2020 07:57:06 -0800 Message-Id: <20201223155717.19556-11-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201223155717.19556-1-chang.seok.bae@intel.com> References: <20201223155717.19556-1-chang.seok.bae@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org copy_xregs_to_kernel() used to save all user states in a kernel buffer. When the dynamic user state is enabled, it becomes conditional which state to be saved. fpu->state_mask can indicate which state components are reserved to be saved in XSAVE buffer. Use it as XSAVE's instruction mask to select states. KVM used to save all xstate via copy_xregs_to_kernel(). Update KVM to set a valid fpu->state_mask, which will be necessary to correctly handle dynamic state buffers. No functional change until the kernel supports dynamic user states. Signed-off-by: Chang S. Bae Reviewed-by: Len Brown Cc: x86@kernel.org Cc: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org --- Changes from v2: * Updated the changelog to clarify the KVM code changes. --- arch/x86/include/asm/fpu/internal.h | 3 +-- arch/x86/kernel/fpu/core.c | 2 +- arch/x86/kvm/x86.c | 11 ++++++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h index 67ffd1d7c95e..d409a6ae0c38 100644 --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -332,9 +332,8 @@ static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate) /* * Save processor xstate to xsave area. */ -static inline void copy_xregs_to_kernel(struct xregs_state *xstate) +static inline void copy_xregs_to_kernel(struct xregs_state *xstate, u64 mask) { - u64 mask = xfeatures_mask_all; u32 lmask = mask; u32 hmask = mask >> 32; int err; diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 8b9d3ec9ac46..5a12e4b22db2 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -99,7 +99,7 @@ int copy_fpregs_to_fpstate(struct fpu *fpu) if (likely(use_xsave())) { struct xregs_state *xsave = &xstate->xsave; - copy_xregs_to_kernel(xsave); + copy_xregs_to_kernel(xsave, fpu->state_mask); /* * AVX512 state is tracked here because its use is diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4aecfba04bd3..93b5bacad67a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9214,15 +9214,20 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu) static void kvm_save_current_fpu(struct fpu *fpu) { + struct fpu *src_fpu = ¤t->thread.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, + if (test_thread_flag(TIF_NEED_FPU_LOAD)) { + memcpy(&fpu->state, &src_fpu->state, fpu_kernel_xstate_min_size); - else + } else { + if (fpu->state_mask != src_fpu->state_mask) + fpu->state_mask = src_fpu->state_mask; copy_fpregs_to_fpstate(fpu); + } } /* Swap (qemu) user FPU context for the guest FPU context. */ -- 2.17.1