Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1363860rdb; Wed, 24 Jan 2024 12:49:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IFidO+neuPPErwHeH87G1loiB3hTh+Dk8WAw2tjhJgvmATnjdAubGfyvPq4HZjjqVOxANZx X-Received: by 2002:a17:902:7c90:b0:1d4:1327:bd15 with SMTP id y16-20020a1709027c9000b001d41327bd15mr1507624pll.32.1706129360198; Wed, 24 Jan 2024 12:49:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706129360; cv=pass; d=google.com; s=arc-20160816; b=kfI6j/DQlUNUbObf68rS9mWnSD1oRULx5XCSFgjcIqsRHbTHxhzrAXPAkMNQ20IxN/ hqNoenRQIdQEhnmRHvtUQUaGitTPWT320TXO7/8a3QkeqKJFqwKiD30VEgOoS41cudWi e+cp84OgNkkjpr4C8XhRTnaAGJ0nxk7n39kUF0H2GSvl6OjwPXcDVrNRyHAAk5Mn4cfm BLFtpr4u4BftGmjAB7PVzYZwlxIF5lvY1ujnbyKGleU5p08YcsEiMd7oSreDBayFVCRb OHfDCykGwfAEpYe3zeXaw2YlJi3qwEjYJRKQNpWF+AmBB75s037Nl1nTV+MTOdNMqB36 +vig== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=lDGHIYOlTA+W628b1kvaMrAADcsMgGzGK6ZEdDqpn+8=; fh=bK24Rju9cuMWcPiBgklg+IpWhXtWnOSEiDJv8+DNMn0=; b=GZCH2yBrJgrxzaGNCwATV/EoUQZC+ek1wwWWUIy+xoCRgiiQHvE0uo1mLKo8tfKBPu uoyp9HHKdN5NC8Ju/csrVumqbKmeBWyzKbnrc+OlT4lEQ5iIt8pDlzv+4nF3SW8qajI+ 1cuvT8QlFVMI/Cmp2q8G/U0nvk9zxL9S+6TAW2MPuJ6GM6n5Y/YUlhKj0O1ivCzgP35X wI+BYqiKROiQ36CMSdJjhYJyNnXJudky2IBXv55+Rbh3NWV33bHPo0gOFR5gFLH2nvCN pjAwZ8hMgtdQWnZwz/Yf7cQUmJlrea9bx79fVdAAft6wlhDkQ4yzKjz5YnTbjYifMweI VEvQ== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=maciej.szmigiero.name); spf=pass (google.com: domain of linux-kernel+bounces-37662-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37662-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id v8-20020a1709029a0800b001d40906a0d5si2424501plp.502.2024.01.24.12.49.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 12:49:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37662-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=maciej.szmigiero.name); spf=pass (google.com: domain of linux-kernel+bounces-37662-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37662-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A52F8B2174E for ; Wed, 24 Jan 2024 20:49:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F3E71350FA; Wed, 24 Jan 2024 20:48:54 +0000 (UTC) Received: from vps-vb.mhejs.net (vps-vb.mhejs.net [37.28.154.113]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECD0C13474A; Wed, 24 Jan 2024 20:48:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.28.154.113 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706129333; cv=none; b=KrH+amEL1VLGQGgx8yvh57wp7NKXLQmEfwnOP5J0VU+83S7x+qAbJW7CIe1i8gT1vqEEI7oHYXOOF1ESHUsLMJY9+1+m7POLVBS1D8lTWvKowu4PmOOhEi/ZXa2kKMVylUoxjjVIyMbtwsyId3uZdHYVIM5OhXuLig5iePXmlKA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706129333; c=relaxed/simple; bh=6FC/0e29kJXWgGL2yi5/vbe6wptoY2zpI5hp51jovMo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=MJCM9CZavBxuUh+xrit494KulpjJvcMHLrghZw2J9yRrXZ4RBqg0wQPGw0HExAoP7aLBXcJt1Q1f0OkDGZLpF0hkjLKQ5ioHs3GlrqMYbONsqRogHSmb7AHs2icdEMrZlD8LDb67SzRxdv14ZHuDa1pS4oOi/5eTQKCoCQUvixM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=maciej.szmigiero.name; spf=pass smtp.mailfrom=maciej.szmigiero.name; arc=none smtp.client-ip=37.28.154.113 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=maciej.szmigiero.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=maciej.szmigiero.name 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 1rSjhi-0003sJ-QU; Wed, 24 Jan 2024 21:18:34 +0100 From: "Maciej S. Szmigiero" To: Paolo Bonzini , Sean Christopherson Cc: Maxim Levitsky , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] KVM: x86: Give a hint when Win2016 might fail to boot due to XSAVES erratum Date: Wed, 24 Jan 2024 21:18:21 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Because KVM CPU caps are initialized from the kernel boot CPU features this makes the XSAVES feature also unavailable for KVM guests in this case. At the same time the XSAVEC feature is left enabled. Unfortunately, having XSAVEC but no XSAVES in CPUID breaks Hyper-V enabled Windows Server 2016 VMs that have more than one vCPU. Let's at least give users hint in the kernel log what could be wrong since these VMs currently simply hang at boot with a black screen - giving no clue what suddenly broke them and how to make them work again. Trigger the kernel message hint based on the particular guest ID written to the Guest OS Identity Hyper-V MSR implemented by KVM. Defer this check to when the L1 Hyper-V hypervisor enables SVM in EFER since we want to limit this message to Hyper-V enabled Windows guests only (Windows session running nested as L2) but the actual Guest OS Identity MSR write is done by L1 and happens before it enables SVM. Fixes: b0563468eeac ("x86/CPU/AMD: Disable XSAVES on AMD family 0x17") Signed-off-by: Maciej S. Szmigiero --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/hyperv.c | 48 +++++++++++++++++++++++++++++++++ arch/x86/kvm/hyperv.h | 3 +++ arch/x86/kvm/x86.c | 4 +++ 4 files changed, 57 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 7bc1daf68741..c4b63be775df 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1145,6 +1145,8 @@ struct kvm_hv { unsigned int synic_auto_eoi_used; struct kvm_hv_syndbg hv_syndbg; + + bool xsaves_xsavec_warned; }; #endif diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 238afd7335e4..41485ae35b23 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1322,6 +1322,54 @@ static bool hv_check_msr_access(struct kvm_vcpu_hv *hv_vcpu, u32 msr) return false; } +#define KVM_HV_WIN2016_GUEST_ID 0x1040a00003839 +#define KVM_HV_WIN2016_GUEST_ID_MASK (~GENMASK_ULL(23, 16)) /* mask out the service version */ + +/* + * Hyper-V enabled Windows Server 2016 SMP VMs fail to boot in !XSAVES && XSAVEC + * configuration. + * Such configuration can result from, for example, AMD Erratum 1386 workaround. + * + * Print a notice so users aren't left wondering what's suddenly gone wrong. + */ +static void kvm_hv_xsaves_xsavec_maybe_warn_unlocked(struct kvm_vcpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + struct kvm_hv *hv = to_kvm_hv(kvm); + + if (hv->xsaves_xsavec_warned) + return; + + if (!vcpu->arch.hyperv_enabled) + return; + + if ((hv->hv_guest_os_id & KVM_HV_WIN2016_GUEST_ID_MASK) != + KVM_HV_WIN2016_GUEST_ID) + return; + + /* UP configurations aren't affected */ + if (atomic_read(&kvm->online_vcpus) < 2) + return; + + if (boot_cpu_has(X86_FEATURE_XSAVES) || + !guest_cpuid_has(vcpu, X86_FEATURE_XSAVEC)) + return; + + pr_notice_ratelimited("Booting SMP Windows KVM VM with !XSAVES && XSAVEC. " + "If it fails to boot try disabling XSAVEC in the VM config.\n"); + + hv->xsaves_xsavec_warned = true; +} + +void kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu) +{ + struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); + + mutex_lock(&hv->hv_lock); + kvm_hv_xsaves_xsavec_maybe_warn_unlocked(vcpu); + mutex_unlock(&hv->hv_lock); +} + static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) { diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index 1dc0b6604526..923e64903da9 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -182,6 +182,8 @@ void kvm_hv_setup_tsc_page(struct kvm *kvm, struct pvclock_vcpu_time_info *hv_clock); void kvm_hv_request_tsc_page_update(struct kvm *kvm); +void kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu); + void kvm_hv_init_vm(struct kvm *kvm); void kvm_hv_destroy_vm(struct kvm *kvm); int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu); @@ -267,6 +269,7 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu); static inline void kvm_hv_setup_tsc_page(struct kvm *kvm, struct pvclock_vcpu_time_info *hv_clock) {} static inline void kvm_hv_request_tsc_page_update(struct kvm *kvm) {} +static inline void kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu) {} static inline void kvm_hv_init_vm(struct kvm *kvm) {} static inline void kvm_hv_destroy_vm(struct kvm *kvm) {} static inline int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 27e23714e960..db0a2c40d749 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1782,6 +1782,10 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) if ((efer ^ old_efer) & KVM_MMU_EFER_ROLE_BITS) kvm_mmu_reset_context(vcpu); + if (guest_cpuid_is_amd_or_hygon(vcpu) && + efer & EFER_SVME) + kvm_hv_xsaves_xsavec_maybe_warn(vcpu); + return 0; }