Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp551144rdh; Thu, 23 Nov 2023 11:07:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IFeIbqpDvY6G5agRjzYWN5Ikz5dHd7bUAKrLTesoGipmSkUdYygGpUfy7qXsF/aDchC/YoX X-Received: by 2002:a05:6a20:d904:b0:18b:9149:b69 with SMTP id jd4-20020a056a20d90400b0018b91490b69mr645550pzb.6.1700766425659; Thu, 23 Nov 2023 11:07:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700766425; cv=none; d=google.com; s=arc-20160816; b=MiPzqI6uwLbFwf4AWXuK6J2tZerJF6MQcD50yRxm8Kwty5vCAJNKaKEqpVQIAKGbkY Dz4VH3pVWLDMKXuMEhxUPWOxNDVv++M2ABIiEHXcwTLBuDcLNK+zEJXQ0bRx8hYMcIgV 0+fXkRIdRRluSIeKs0JHYdCRbizqGJKLwev0l878/rLv4p42yzvNkmN2NJGgcQ+JEuvZ qRZL4neQvgCLBjcm1TMbCZg0gtneUq1ha6NyDZJDz8Gl3jj1dT6LFB1oLUQraesidt/s QGwiatDSFuRq7y6IaRkFrlQybN0ZHf+YUwV3+ztuL5eJGpjnwwxjnhBT5jYuourNymLy 4FAw== 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 Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id c3-20020a630d03000000b005aaf19c3981si1827531pgl.329.2023.11.23.11.07.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 11:07:05 -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 486958068978; Thu, 23 Nov 2023 11:07:03 -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 Content-Transfer-Encoding: 8bit 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:07:03 -0800 (PST) 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);