From patchwork Thu Nov 23 19:06:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej S. Szmigiero" X-Patchwork-Id: 169086 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp656346vqx; Thu, 23 Nov 2023 11:07:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IGC5eydVltnkfppkTmgztdBGy2RVP0tAd1B0xbDfjc2TvhPMpIpdod0gF4Qq8aceR1HxJWJ X-Received: by 2002:a05:6a20:12c5:b0:188:f1dd:62aa with SMTP id v5-20020a056a2012c500b00188f1dd62aamr471838pzg.44.1700766422165; Thu, 23 Nov 2023 11:07:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700766422; cv=none; d=google.com; s=arc-20160816; b=xxqHh0f+JhYhxMG4T9UY5dWE8+gx5894lhUhXYVV3ShZ4gNudeMnMu8OimUQNKpkVb KAtbbdm5/m+4y6vYSIY13QEBjXzz2NYYlo50qxmdLk6yKvRBFw8ic0XK4Te4gxXfm4ir JJLZ9GD+C+zm2zNJ2Eljhd8Ey6GFjZop+u/jQfKs3bqu7HK2P4OgBL8DCYfNzvwSBSNF WCQ0CDHifXlVmsjDaDQiMn7szgvcjC2tfpHttBDJK2SXFnxUxjoZyaCxKyBk9wtWc8fL jT9ozxw01gyOmkiX8kpw4RaL1MM3fYJL55Untn7xK7kKRtDR68IFLmg8/nM8yUkP8V31 xiIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=Dt2yJA2lKk2GXuE04IFX0ZReIwL8SIiJu1FD1m8mI00=; fh=z0BYfbZeUyOxhLCLtxp9QBm0KwQEubyRlouTvJNgb4g=; b=dPybkzde/ca60qAcxOyllVql+OszOl5XrCGMQGJ9gBEDtc9/Cl1nxiSqw7yFDz61Ld 6BD39Fs7cFnBpKYbFJmGtnA9pg78azWV3tvfSRrVoOsGHqUB7vHjP++9wn89BWMHF9JW S5VIBG5rzKrmkc9nuvdakZsQBWCbwcULSxQqasl53QPmtcSZUznT2pXlBk67TvDyMGnJ A7hlSQEYF5ANoQpoK3wMS7JLE62IxRc2WEmiKEj2GHlZWIgQcbfih++FBGZN/CQTh75l A3kxDT8kETz9gqpFmKkTOfMT48lLdtgkMKLAuY33Z5ExZaylFEY0pB0EhOup7fun8gti aXrg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id q21-20020a056a00151500b006c6a9c10e15si1843247pfu.46.2023.11.23.11.07.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 11:07:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 563988068978; Thu, 23 Nov 2023 11:06:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230072AbjKWTGp (ORCPT + 99 others); Thu, 23 Nov 2023 14:06:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229462AbjKWTGn (ORCPT ); Thu, 23 Nov 2023 14:06:43 -0500 Received: from vps-vb.mhejs.net (vps-vb.mhejs.net [37.28.154.113]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04B679E; Thu, 23 Nov 2023 11:06:47 -0800 (PST) Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r6F2B-0007vm-O5; Thu, 23 Nov 2023 20:06:43 +0100 From: "Maciej S. Szmigiero" To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] KVM: x86: Allow XSAVES on CPUs where host doesn't use it due to an errata Date: Thu, 23 Nov 2023 20:06:37 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Thu, 23 Nov 2023 11:06:55 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783382851841183970 X-GMAIL-MSGID: 1783382851841183970 From: "Maciej S. Szmigiero" Since commit b0563468eeac ("x86/CPU/AMD: Disable XSAVES on AMD family 0x17") kernel unconditionally clears the XSAVES CPU feature bit on Zen1/2 CPUs. Since KVM CPU caps are initialized from the kernel boot CPU features this makes the XSAVES feature also unavailable for KVM guests in this case, even though they might want to decide on their own whether they are affected by this errata. Allow KVM guests to make such decision by setting the XSAVES KVM CPU capability bit based on the actual CPU capability. This fixes booting Hyper-V enabled Windows Server 2016 VMs with more than one vCPU on Zen1/2 CPUs. Signed-off-by: Maciej S. Szmigiero --- arch/x86/kvm/cpuid.c | 16 ++++++++++++++++ arch/x86/kvm/svm/svm.c | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index dda6fc4cfae8..a8820460163a 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -679,6 +679,22 @@ void kvm_set_cpu_caps(void) F(AMX_COMPLEX) ); + /* + * It is possible that CPU supports XSAVES but the host kernel decided + * not to use it, for example due to AMD Erratum 1386, and cleared the + * relevant CPU feature bit. + * + * In such case let the guest decide on it own whether to make use of + * this feature. + */ + if (boot_cpu_data.cpuid_level >= XSTATE_CPUID) { + unsigned int eax, ebx, ecx, edx; + + cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx); + if (eax & F(XSAVES)) + kvm_cpu_cap_set(X86_FEATURE_XSAVES); + } + kvm_cpu_cap_mask(CPUID_D_1_EAX, F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd ); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 712146312358..3cc36710eb21 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4306,9 +4306,12 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) * whether it's advertised to the guest so that KVM context switches * XSS on VM-Enter/VM-Exit. Failure to do so would effectively give * the guest read/write access to the host's XSS. + * + * Make sure to check for XSAVES in KVM CPU capabilities, since the + * boot CPU feature bit might be disabled due to Erratum 1386. */ if (boot_cpu_has(X86_FEATURE_XSAVE) && - boot_cpu_has(X86_FEATURE_XSAVES) && + kvm_cpu_cap_has(X86_FEATURE_XSAVES) && guest_cpuid_has(vcpu, X86_FEATURE_XSAVE)) kvm_governed_feature_set(vcpu, X86_FEATURE_XSAVES);