Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp989740rdb; Sun, 18 Feb 2024 23:52:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUu/8L2shIBKRuVzGTL+W9EcoypuR9sN6Lf9rAvx15+d3Wj4P3+h64ZShc+MFf5g0R1gJZUsPjc6YQyo0/BSvcGGq9lExMtc0n6EK2/yA== X-Google-Smtp-Source: AGHT+IFwLv0Qso1jgcUKaMapZyRQTbZLMdsHD+JGRjDkaqiNWK2ohwhQ72qQfczQRWYINRFL4v2t X-Received: by 2002:aa7:ce0c:0:b0:564:5a23:4ec with SMTP id d12-20020aa7ce0c000000b005645a2304ecmr2708231edv.32.1708329160304; Sun, 18 Feb 2024 23:52:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708329160; cv=pass; d=google.com; s=arc-20160816; b=YIxL6+P1Jp8jYqr+OCiN4QrGrx52yVMI2xptl/9c5SDz4CEOIixqcFec6CIEW6GZuF xd192gjrkrud/TfSyUipODiALtQYTUhdLsb4RM+j/Dgu06vOWzFZZgwTtBwCZYKV/8UL Cm/RN2lcp2LmsFv3iJx9HGuTyvoWj+hjZivOm88OJfGrYwIsn00kPgvx0dOyojao9VVK As0+WEegbgQoafAUDctHG8HHfC7J6n/ffmhB4RtW+4fBmC3bQTQLWicYP/2DvuYcVBq+ rxDH2JSOXbs6OKCmeIefMDqU3ur4+ShvTcP2iQqIF01v/5APE/mW0zm0aQPhy5+luiaH GyVQ== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=oN+fiXiPYDGijIT75yrMMb08Pibpw2y5VTJ47AsAXWg=; fh=6bYtQoKQxNJSYrJA1a9vseXH6qHZpRYO7L/7krtpXA0=; b=pCVGJtX37ASR3lCCAszWfXFbgPxJ0CMJqFIXxwm8qJR5zJoxQL2EvatZyvoKfrcImA VQwQGSC3e+WC2MAHmm3j4vXZsCfoWEL8XCH70+0debDH6VYA/bTAH8ibUqjuqnsL7KJ/ LF6yuQeokeat2D5lHE+QU1a8E6z8vhDJAOCZpcr6Q2Fo2liLaxFvBRqs+Oop9eGJ0BYT z0JCmNyYd65fSoniLaXQv97qjLdubh9nJz32Ym/+o6WAdVu+mqFAq78OLJhuiJQmGzoA go6w3KynxhfYiIPKq/xOrzSfEhNwH8sPi4FCYXmQXsRZoRTfHpFDCF/y5qQdNOudMpME gMOg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ElChzFnt; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-70874-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70874-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id b25-20020aa7df99000000b00564703ff732si730009edy.494.2024.02.18.23.52.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 23:52:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70874-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ElChzFnt; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-70874-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70874-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 1336C1F21D11 for ; Mon, 19 Feb 2024 07:52:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A5DCE2E856; Mon, 19 Feb 2024 07:47:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ElChzFnt" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 B72ED20B28; Mon, 19 Feb 2024 07:47:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328871; cv=none; b=Vffat+IDOhyJq+W8vDTC/poDGfPJ8YkmFVvoa1WKpZ848yuGJpVHNe7jYFz+T8aTrjAP7rjWdUmNHDNZYSubW7Jh+wh/DdBFjiGdbdwDfM79qSWsWfbkFtA+U3RLoayO6fFoFztEGNbPISQXTosbKosWf5PYXw6Xp2LRTOtDylQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328871; c=relaxed/simple; bh=iFPryYlo0x9QbRdEVDhSTPJscWPbWs0HWCWU5SaS5aI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Nu1U1CjUbYB1X1QN7/kLQOyCys4NaNXi1Rl1UEVNxCzyr76Qvru4X2l9nDzJdK4kZolGIedjYUYw5nUAvPwWFT26xTNWEY3vzalOohHlB+eLnMBWuXWHcF4POPxLjOScjSFyOs/DkBqO1NpiQWQ5cXiMvaVmoO9zWpxeBDYQkck= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ElChzFnt; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708328869; x=1739864869; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iFPryYlo0x9QbRdEVDhSTPJscWPbWs0HWCWU5SaS5aI=; b=ElChzFntSW8JIgRMagpDXFfz68uUhmLpaoNhDhZ6daODrzQuIueRspVG yQfcacagbk3DHalM6g4atYJTNNvxQQ9EiVFDqw3YG38DkRyjtDiZ1lwk+ cqvdFHBVVuLHAJ2C9ZuGd6pwUzVUiq5eGpIGM0CiunHmNdJwo4XniiBES 3AVtD6XO14MyKsfIP7KfBKtKYKTAgFWqWIsrMtmfhJoFkBI9WbvvmSAbZ rN/8KQWKDi4pm0TLkcbXPOGbbg1PNcJQPGXKmfHjkTVEJl423h/7zLMKx 0e+J0AL/0e6jX6axeh2/ybOkNSPuRPUi7VNjsqVUuuIa+C94653Js+sOR Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="2535068" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="2535068" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2024 23:47:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="826966081" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="826966081" Received: from jf.jf.intel.com (HELO jf.intel.com) ([10.165.9.183]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2024 23:47:43 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, x86@kernel.org, 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 v10 08/27] KVM: x86: Rework cpuid_get_supported_xcr0() to operate on vCPU data Date: Sun, 18 Feb 2024 23:47:14 -0800 Message-ID: <20240219074733.122080-9-weijiang.yang@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240219074733.122080-1-weijiang.yang@intel.com> References: <20240219074733.122080-1-weijiang.yang@intel.com> 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: 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 Reviewed-by: Maxim Levitsky Reviewed-by: Chao Gao --- 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 adba49afb5fe..d57a6255b19f 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) { #ifdef CONFIG_KVM_HYPERV @@ -361,8 +361,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.43.0