Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp893667rdh; Thu, 23 Nov 2023 23:59:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IGA/VMYVwjA5iK+w1p05+0mP9s5KoCLIq4urGg0HEA++zG8PdRf6vBJyAvFsMFIJIGkv9yl X-Received: by 2002:a05:6808:3319:b0:3af:9838:8494 with SMTP id ca25-20020a056808331900b003af98388494mr2653567oib.20.1700812775465; Thu, 23 Nov 2023 23:59:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700812775; cv=none; d=google.com; s=arc-20160816; b=RvpwA8iUviwYlmqKGk4N9h0H27TZZgfBEWWL9W0G4+p5bnH1MPJIWHFLT7efCiL2i/ D3yG1vb/Nao0CLfk9elDQLF5DMpZd4UsqffhVpNLkoqiOVPoeoUfhQ4QHThnIpm8knQh CS+x43cbT5VsytwPvn6JJCNz83nAqSTpm2t3PAdBBbhWyQl8d8SVlQARsb5Jx57mIv1j m/PACUbnd5dgQEj+GnJepOWhJcuKUkT4h3qO3NtUcIZH6gOIAMJdiD5hfnYONlcnGIo7 bxf2yi121N+k90Wv4f7kEqGsnJqUEi5l6IaMRGmX8yzyWliBRa92ScByner5jmbboP36 TNow== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hvbibyv9gMZrYN6NtBPC0XdLuEfYMUkmbOvcEPoIoV4=; fh=uy0EBGgYIm8+MgsxUvKHUXUo3s9z4H9hdLwRv8YoeJU=; b=w5uEul9lPHSYDV48WO8twBHEywFEubEYPJeiV50fqeIllWz05zEdNLmtdLwKS5BGXY 9PE9oE71y6J0wfOPhF2ZISOblo5KQIFRMBjqUy387ecfqRzDZAQKLN3TV6i2ZEv1OTRC u+JrlVei4qJQXhTUfoEBpu5Gzud4GnwT3JvGsfuaGjTuva0enEOrGdZDfS1IHBpscRK4 8ih7n2g81mz1dsit/1RYYw9UH5sNz3MfXHZjSamibmxObORNkHoahUw2S37iKWthgIm9 0HwiwZ+qx/YR7Y/rb+DYDH0yLmYN98fdGDroGUUj3w4WNklMXkBgYbCqxcwTDE+ikY0u CNnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lor0KWP3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id f5-20020a631f05000000b005bd39be60b3si2893213pgf.390.2023.11.23.23.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 23:59:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lor0KWP3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 16BE0805B210; Thu, 23 Nov 2023 23:59:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345028AbjKXH7M (ORCPT + 99 others); Fri, 24 Nov 2023 02:59:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232071AbjKXH6g (ORCPT ); Fri, 24 Nov 2023 02:58:36 -0500 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0ADF61731; Thu, 23 Nov 2023 23:58:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700812723; x=1732348723; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PTRYAwAe8AAOFrWsNP3TkNOOKSAeaA8d5OvgMc5L7Ms=; b=lor0KWP31kE5W9nsM8bSqp0fa6z9AJ2HZ68IhATFl9HcxVStaRU5YKYA BlY3Mm05iXNR6NMc8YvqpSilyY6A51tS5WI+zf8osF6725xr5YwhoUeT1 9U3dj4PWeCNnDIO9MMBc57F+4Qb/CQYS7q5+m6J59Ylgm67CjAsCS8Z4I 88TTSN3vmqsJ0YOOxFBnC+Jp44sL31SHmkfD9yQMTC6GuhQa983QscjQL 9+V9RJGcjGrmB3mnmmf4pL9mGcLxc/4MN2ypONVfEZq75u+cgNwANKehT TQhqtNuCwIy/33NdQwiLBC9fUTBtQTSXbfvTgE8K8ufJpO583zlRfw5Hc w==; X-IronPort-AV: E=McAfee;i="6600,9927,10902"; a="458872308" X-IronPort-AV: E=Sophos;i="6.04,223,1695711600"; d="scan'208";a="458872308" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Nov 2023 23:58:38 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10902"; a="833629813" X-IronPort-AV: E=Sophos;i="6.04,223,1695711600"; d="scan'208";a="833629813" Received: from unknown (HELO embargo.jf.intel.com) ([10.165.9.183]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Nov 2023 23:58:37 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, mlevitsk@redhat.com, john.allen@amd.com, weijiang.yang@intel.com Subject: [PATCH v7 08/26] KVM: x86: Rework cpuid_get_supported_xcr0() to operate on vCPU data Date: Fri, 24 Nov 2023 00:53:12 -0500 Message-Id: <20231124055330.138870-9-weijiang.yang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20231124055330.138870-1-weijiang.yang@intel.com> References: <20231124055330.138870-1-weijiang.yang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 morse.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 (morse.vger.email [0.0.0.0]); Thu, 23 Nov 2023 23:59:28 -0800 (PST) From: Sean Christopherson Rework and rename cpuid_get_supported_xcr0() to explicitly operate on vCPU state, i.e. on a vCPU's CPUID state, now that the only usage of the helper is to retrieve a vCPU's already-set CPUID. Prior to commit 275a87244ec8 ("KVM: x86: Don't adjust guest's CPUID.0x12.1 (allowed SGX enclave XFRM)"), KVM incorrectly fudged guest CPUID at runtime, which in turn necessitated massaging the incoming CPUID state for KVM_SET_CPUID{2} so as not to run afoul of kvm_cpuid_check_equal(). I.e. KVM also invoked cpuid_get_supported_xcr0() with the incoming CPUID state, and thus without an explicit vCPU object. Opportunistically move the helper below kvm_update_cpuid_runtime() to make it harder to repeat the mistake of querying supported XCR0 for runtime updates. No functional change intended. Signed-off-by: Sean Christopherson Signed-off-by: Yang Weijiang --- arch/x86/kvm/cpuid.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index dda6fc4cfae8..d0315e469d92 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -247,21 +247,6 @@ void kvm_update_pv_runtime(struct kvm_vcpu *vcpu) vcpu->arch.pv_cpuid.features = best->eax; } -/* - * Calculate guest's supported XCR0 taking into account guest CPUID data and - * KVM's supported XCR0 (comprised of host's XCR0 and KVM_SUPPORTED_XCR0). - */ -static u64 cpuid_get_supported_xcr0(struct kvm_cpuid_entry2 *entries, int nent) -{ - struct kvm_cpuid_entry2 *best; - - best = cpuid_entry2_find(entries, nent, 0xd, 0); - if (!best) - return 0; - - return (best->eax | ((u64)best->edx << 32)) & kvm_caps.supported_xcr0; -} - static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries, int nent) { @@ -312,6 +297,21 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime); +/* + * Calculate guest's supported XCR0 taking into account guest CPUID data and + * KVM's supported XCR0 (comprised of host's XCR0 and KVM_SUPPORTED_XCR0). + */ +static u64 vcpu_get_supported_xcr0(struct kvm_vcpu *vcpu) +{ + struct kvm_cpuid_entry2 *best; + + best = kvm_find_cpuid_entry_index(vcpu, 0xd, 0); + if (!best) + return 0; + + return (best->eax | ((u64)best->edx << 32)) & kvm_caps.supported_xcr0; +} + static bool kvm_cpuid_has_hyperv(struct kvm_cpuid_entry2 *entries, int nent) { struct kvm_cpuid_entry2 *entry; @@ -357,8 +357,7 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) kvm_apic_set_version(vcpu); } - vcpu->arch.guest_supported_xcr0 = - cpuid_get_supported_xcr0(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent); + vcpu->arch.guest_supported_xcr0 = vcpu_get_supported_xcr0(vcpu); kvm_update_pv_runtime(vcpu); -- 2.27.0