Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp1315865rdb; Sat, 3 Feb 2024 01:08:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPHAvDZedZbNdMtXcfOSCjrvF11YeP8Y9W/mSK1Cp54lbV5GLa/5W8nVu1YpLKiS59bqrn X-Received: by 2002:ac8:720e:0:b0:42b:ee47:2369 with SMTP id a14-20020ac8720e000000b0042bee472369mr702873qtp.64.1706951329555; Sat, 03 Feb 2024 01:08:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706951329; cv=pass; d=google.com; s=arc-20160816; b=EFTHM9aecr/5G2D+iEfLhbvx9vhmnLDL/z3/XMt44BuTEoa1eYhmWbILrilbQj3yMT s7JUSU3ToTnuVhjFY857mVz+GirnInyxY7WRnqOTdGRSu76wBo806SGqBpng7s2KL7R3 d2VdEcQxBn8KJvlqoSs7Vynrs2ZQlt0Xeunh2C8aRY9zPgKOvlON0aW/6QFrJAANnENZ mlj+m9QL3Ye4aje5cSbX10dihT0qIBd18n8yYTQBHBelZd0jLGKwAHhWpfKVhW0fPp87 nEOhP7K9JsTaReQ9G6RcAZNM0ZpDwoD8SJTOLY7NcJwfQ6xTo3IX99ohxnlLZQZvdvPM tsag== 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=4agYm69xH4LKJyeFvVAzCn8d6azyIyiZ21I+ZraxP1Q=; fh=ZKPjFkzq2IJjOKIs3cSoIa23JXBPBeGD+M4ctHBIuCQ=; b=yAj9+3B4/uTlCQncBhFZIkkVQ4onPX4+c/huC5EN0VRvAxb0L6up8VSYcNij48Vb5c l3mfeBtYyaJOSREyTqtjkP6Q8BnjuK2Jig756iYmPzGAdqL6zY9Hsz0An8XaTB04D5Zm nJv9wwFgTBxYz4wsZRyzg6WmQN7XVMW+sdRbKaTM+WbhQuO5SbFBTmYDKylwZSLL/sjQ y0bvk3dJaEA35wVl4V3HaqdNJ9Xd/6Ou/3OIPV9Fx0S5HTwTwPxdUYcj5MN0e9Z08UHp ATtimJCtpWR6X2Lcw6K+xJsuKcA4xZvcsyzIebSYDNqeZfIVc2TR3yCj0pQNt9PM2OI0 44Dg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VdseGHXE; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-51001-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51001-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCXjwyyBEC8TUTUYeYH+rHLyTw10TQVXfTtPrtDiiKDGwW5mL5I1RE8L9NguBQrIWR3spkrBxBZaXGZv027eOJWzJu5gwcRsKoUWgDtAGA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id b19-20020ac87fd3000000b0042a9aa32409si3974234qtk.130.2024.02.03.01.08.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Feb 2024 01:08:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-51001-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VdseGHXE; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-51001-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51001-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 459311C297BB for ; Sat, 3 Feb 2024 09:08:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DC07067C56; Sat, 3 Feb 2024 09:02:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="VdseGHXE" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (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 5AA4A6775C; Sat, 3 Feb 2024 09:01:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706950919; cv=none; b=iZRycaHAchAEpswdDobEl4fyuNXW9+zAdVpXLUTWe2iAJR34Y3MA9BVOct9Gjsc65w+O8t8ibpetkY49GxTMGJyy5gpxEnloZuLbze/INW7e08KuHjhHlLrk0Jsf5KTn9t2MqK+CQUaL+RQ5ps+GLtKoo6MNOPR4QqJprFPSuL4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706950919; c=relaxed/simple; bh=VCv51ZKM2GW64nR2mDsrfM6GGX/jccl5W1ec8CqBGLo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=S2kbMQ2Pg0UMjRQGIWZnoF4GC46BEIxmzZfRw7+yLKbglE1Rk4CigkDS+97btCWk3CtwG0VVr4RKa0Mb6U9PghIjhYJpTBN1jYdgFaMpu5hNVeTQW3uZGw35tlLx4NtqYbH+ncRo7eqoTnJI9jA563+pB+GG3Jte3Tdcl2CRwcs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=VdseGHXE; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706950919; x=1738486919; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VCv51ZKM2GW64nR2mDsrfM6GGX/jccl5W1ec8CqBGLo=; b=VdseGHXEhR8GTHqm6waOSbYG2+7VoEhbB9O/wxaW1kEGyVt1b+3sgjc1 TAr5g2JuGxAhRNF4T68573D6axKhLa2mIwPQqnfXNSXRhfyQNu5TJf2+n 93q73yd5QQANXA0NDgDk6FifxLWyDHYSb7MI+jWD8D/Ppsh2oGgEARBl1 wk57uC24NnmCdnXfSfLzAX+u6zRgx7uWuSBz7IObkrWQmt/Km5yXJJzRm nKwbVrnfy4Km7ebDJG1frSXMcv+N2jcjZRBpHFVKtg6Uj/iUiRbow5PbH QqqDteyW8ykwHwi7574//y7cnpZ0yyJ28zGd3XfhxP1PzW9P46Tc55AOT w==; X-IronPort-AV: E=McAfee;i="6600,9927,10971"; a="4132204" X-IronPort-AV: E=Sophos;i="6.05,240,1701158400"; d="scan'208";a="4132204" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2024 01:01:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,240,1701158400"; d="scan'208";a="291576" Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.36]) by fmviesa009.fm.intel.com with ESMTP; 03 Feb 2024 01:01:52 -0800 From: Zhao Liu To: Paolo Bonzini , Sean Christopherson , "Rafael J . Wysocki" , Daniel Lezcano , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , kvm@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Cc: Ricardo Neri , Len Brown , Zhang Rui , Zhenyu Wang , Zhuocheng Ding , Dapeng Mi , Yanting Jiang , Yongwei Ma , Vineeth Pillai , Suleiman Souhlal , Masami Hiramatsu , David Dai , Saravana Kannan , Zhao Liu Subject: [RFC 23/26] KVM: x86: Expose ITD feature bit and related info in CPUID Date: Sat, 3 Feb 2024 17:12:11 +0800 Message-Id: <20240203091214.411862-24-zhao1.liu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240203091214.411862-1-zhao1.liu@linux.intel.com> References: <20240203091214.411862-1-zhao1.liu@linux.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: Zhao Liu The Guest's Intel Thread Director (ITD) feature bit is not only dependent on the Host ITD's enablement, but is also based on the Guest's HFI feature bit. When the Host supports both HFI and ITD, try to support HFI and ITD for the Guest. If Host doesn't support ITD, we won't allow Guest to enable HFI or ITD. Tested-by: Yanting Jiang Co-developed-by: Zhuocheng Ding Signed-off-by: Zhuocheng Ding Signed-off-by: Zhao Liu --- arch/x86/kvm/cpuid.c | 55 +++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 4da8f3319917..9e78398f29dc 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -137,7 +137,7 @@ static int kvm_check_hfi_cpuid(struct kvm_vcpu *vcpu, { struct hfi_features hfi_features; struct kvm_cpuid_entry2 *best = NULL; - bool has_hfi; + bool has_hfi, has_itd; int nr_classes, ret; union cpuid6_ecx ecx; union cpuid6_edx edx; @@ -148,9 +148,14 @@ static int kvm_check_hfi_cpuid(struct kvm_vcpu *vcpu, return 0; has_hfi = cpuid_entry_has(best, X86_FEATURE_HFI); - if (!has_hfi) + has_itd = cpuid_entry_has(best, X86_FEATURE_ITD); + if (!has_hfi && !has_itd) return 0; + /* ITD must base on HFI. */ + if (!has_hfi && has_itd) + return -EINVAL; + /* * Only the platform with 1 HFI instance (i.e., client platform) * can enable HFI in Guest. For more information, please refer to @@ -159,11 +164,11 @@ static int kvm_check_hfi_cpuid(struct kvm_vcpu *vcpu, if (intel_hfi_max_instances() != 1) return -EINVAL; - /* - * Currently we haven't supported ITD. HFI is the default feature - * with 1 class. - */ - nr_classes = 1; + /* Guest's ITD must base on Host's ITD enablement. */ + if (!cpu_feature_enabled(X86_FEATURE_ITD) && has_itd) + return -EINVAL; + + nr_classes = has_itd ? 4 : 1; ret = intel_hfi_build_virt_features(&hfi_features, nr_classes, vcpu->kvm->created_vcpus); @@ -718,11 +723,13 @@ void kvm_set_cpu_caps(void) if (boot_cpu_has(X86_FEATURE_PTS)) kvm_cpu_cap_set(X86_FEATURE_PTS); /* - * Set HFI based on hardware capability. Only when the Host has + * Set HFI/ITD based on hardware capability. Only when the Host has * the valid HFI instance, KVM can build the virtual HFI table. */ - if (intel_hfi_enabled()) + if (intel_hfi_enabled()) { kvm_cpu_cap_set(X86_FEATURE_HFI); + kvm_cpu_cap_set(X86_FEATURE_ITD); + } } kvm_cpu_cap_mask(CPUID_7_0_EBX, @@ -1069,19 +1076,35 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) entry->ebx = 0; - if (kvm_cpu_cap_has(X86_FEATURE_HFI)) { + /* + * When Host enables ITD, we will expose ITD and HFI, + * otherwise, HFI/ITD will not be exposed to Guest. + * ITD is an extension of HFI, so after KVM supports ITD + * emulation, HFI-related info in 0x6 leaf should be consistent + * with the Host, that is, use the Host's ITD info, except + * for the HFI index. + * + * HFI table size is related to the HFI table indexes, but + * this item will be checked in kvm_check_cpuid() after + * KVM_SET_CPUID/KVM_SET_CPUID2. + */ + if (kvm_cpu_cap_has(X86_FEATURE_ITD)) { union cpuid6_ecx ecx; union cpuid6_edx edx; + union cpuid6_ecx *host_ecx = (union cpuid6_ecx *)&entry->ecx; + union cpuid6_edx *host_edx = (union cpuid6_edx *)&entry->edx; ecx.full = 0; edx.full = 0; - /* Number of supported HFI classes */ - ecx.split.nr_classes = 1; - /* HFI supports performance and energy efficiency capabilities. */ - edx.split.capabilities.split.performance = 1; - edx.split.capabilities.split.energy_efficiency = 1; + /* Number of supported HFI/ITD classes. */ + ecx.split.nr_classes = host_ecx->split.nr_classes; + /* HFI/ITD supports performance and energy efficiency capabilities. */ + edx.split.capabilities.split.performance = + host_edx->split.capabilities.split.performance; + edx.split.capabilities.split.energy_efficiency = + host_edx->split.capabilities.split.energy_efficiency; /* As default, keep the same HFI table size as host. */ - edx.split.table_pages = ((union cpuid6_edx)entry->edx).split.table_pages; + edx.split.table_pages = host_edx->split.table_pages; /* * Default HFI index = 0. User should be careful that * the index differ for each CPUs. -- 2.34.1