Skip to contentSkip to conversation

thermal: intel: hfi: Prework for the virtualization of HFI

PATCH In review Ricardo Neri 3 participants 21 messages Feb 3, 2024 – Feb 6, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover thermal: intel: hfi: Prework for the virtualization of HFI

Zhao Liu will soon post a patchset to virtualize the Hardware Feedback Interface (HFI) and Intel Thread Director (ITD) for the benefit of virtual machines that make use of ITD for scheduling. His experiments show up to 14% improvement in performance in some workloads and configurations. This series lays the foundation for his patchset. I will share Zhao's patchset when available.

Patches 1-3 reorganize portions of the HFI driver to facilitate the implementation of virtual HFI tables. In my opinion, this reorganization is valuable on its own.

Patches 4-6 introduce the concept of ITD classes and enable ITD.

Patches 7-9 add support to reset the ITD classification history of the current task to be used during context switch.

Several patches of the series were cherry-picked from my last submission to support IPC classes of tasks for scheduling [1], but this series does not touch the scheduler. I have kept the Reviewed-by and Acked-by tags of the cherry-picked patches.

In this iteration, the virtualization of HFI requires that HFI and ITD are unconditionally enabled in the bare-metal system. This conflicts with a recent patchset from Stanislaw [2] that only enables HFI if there are user space entities listening to thermal netlink events. I am not sure how to resolve this conflict. Please see patch 6 for a discussion.

Patches apply cleanly on Rafael's master and testing branches at the time of posting.

Thanks and BR,
Ricardo

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Ricardo Neri (6):
thermal: intel: hfi: Introduce Intel Thread Director classes x86/cpufeatures: Add the Intel Thread Director feature definitions
thermal: intel: hfi: Enable Intel Thread Director x86/cpufeatures: Add feature bit for HRESET
x86/hreset: Configure history reset
x86/cpu: Introduce interface to reset hardware history

Zhao Liu (2):
thermal: intel: hfi: Relocate bit definitions of HFI registers
thermal: intel: hfi: Introduce the hfi_table structure

Zhuocheng Ding (1):
thermal: intel: hfi: Move selected data structures to a header file

 MAINTAINERS                        |   1 +
 arch/x86/include/asm/cpufeatures.h |   2 +
 arch/x86/include/asm/hfi.h         |  85 +++++++++++++++
 arch/x86/include/asm/hreset.h      |  30 ++++++
 arch/x86/include/asm/msr-index.h   |  13 +++
 arch/x86/kernel/cpu/common.c       |  31 +++++-
 arch/x86/kernel/cpu/cpuid-deps.c   |   1 +
 drivers/thermal/intel/intel_hfi.c  | 165 +++++++++++++++--------------
 8 files changed, 245 insertions(+), 83 deletions(-)
 create mode 100644 arch/x86/include/asm/hfi.h
 create mode 100644 arch/x86/include/asm/hreset.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/9 thermal: intel: hfi: Relocate bit definitions of HFI registers

From: Zhao Liu <[email protected]>

KVM needs the definition of several HFI registers for the virtualization of HFI. Move the necessary definitions to msr-index.h

While here, use BIT_ULL() and GENMASK_ULL() since the relevant registers have 64 bits. Also, remove the "_BIT" suffix for consistency in naming.

No functional changes.

Cc Len Brown <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Srinivas Pandruvada <[email protected]>
Cc Stanislaw Gruszka <[email protected]>
Cc Zhuocheng Ding <[email protected]>
---
 arch/x86/include/asm/msr-index.h  |  4 ++++
 drivers/thermal/intel/intel_hfi.c | 10 +++-------
 2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/msr-index.h +4 -0
index f1bd7b91b3c6..46983fb0b5b3 100644
@@ -1143,7 +1143,11 @@
 
 /* Hardware Feedback Interface */
 #define MSR_IA32_HW_FEEDBACK_PTR        0x17d0
+#define HW_FEEDBACK_PTR_VALID           BIT_ULL(0)
+#define HW_FEEDBACK_PTR_RESERVED_MASK   GENMASK_ULL(11, 1)
+
 #define MSR_IA32_HW_FEEDBACK_CONFIG     0x17d1
+#define HW_FEEDBACK_CONFIG_HFI_ENABLE   BIT_ULL(0)
 
 /* x2APIC locked status */
 #define MSR_IA32_XAPIC_DISABLE_STATUS	0xBD
drivers/thermal/intel/intel_hfi.c +3 -7
index 3b04c6ec4fca..9aaca74bdfa3 100644
@@ -48,10 +48,6 @@
 
 #include "../thermal_netlink.h"
 
-/* Hardware Feedback Interface MSR configuration bits */
-#define HW_FEEDBACK_PTR_VALID_BIT		BIT(0)
-#define HW_FEEDBACK_CONFIG_HFI_ENABLE_BIT	BIT(0)
-
 /* CPUID detection and enumeration definitions for HFI */
 
 #define CPUID_HFI_LEAF 6
@@ -356,7 +352,7 @@ static void hfi_enable(void)
 	u64 msr_val;
 
 	rdmsrl(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
-	msr_val |= HW_FEEDBACK_CONFIG_HFI_ENABLE_BIT;
+	msr_val |= HW_FEEDBACK_CONFIG_HFI_ENABLE;
 	wrmsrl(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
 }
 
@@ -366,7 +362,7 @@ static void hfi_set_hw_table(struct hfi_instance *hfi_instance)
 	u64 msr_val;
 
 	hw_table_pa = virt_to_phys(hfi_instance->hw_table);
-	msr_val = hw_table_pa | HW_FEEDBACK_PTR_VALID_BIT;
+	msr_val = hw_table_pa | HW_FEEDBACK_PTR_VALID;
 	wrmsrl(MSR_IA32_HW_FEEDBACK_PTR, msr_val);
 }
 
@@ -377,7 +373,7 @@ static void hfi_disable(void)
 	int i;
 
 	rdmsrl(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
-	msr_val &= ~HW_FEEDBACK_CONFIG_HFI_ENABLE_BIT;
+	msr_val &= ~HW_FEEDBACK_CONFIG_HFI_ENABLE;
 	wrmsrl(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
 
 	/*
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/9 thermal: intel: hfi: Introduce the hfi_table structure

In reply to Ricardo Neri

From: Zhao Liu <[email protected]>

The virtualization of HFI requires to parse the HFI table of the host system. Instead of exposing several pointers to the various section of the table, create a single data structure that describes the table and can be shared more cleanly.

A separate data structure that represents an HFI table improves readability as it makes it clear that the table is one of several attributes of an HFI instance.

No functional changes.

---
 drivers/thermal/intel/intel_hfi.c | 47 ++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 19 deletions(-)
drivers/thermal/intel/intel_hfi.c +28 -19
index 9aaca74bdfa3..eeabdf072efd 100644
@@ -97,12 +97,25 @@ struct hfi_hdr {
 } __packed;
 
 /**
- * struct hfi_instance - Representation of an HFI instance (i.e., a table)
- * @local_table:	Base of the local copy of the HFI table
+ * struct hfi_table - Representation of an HFI table
+ * @base_addr:		Base address of the local copy of the HFI table
  * @timestamp:		Timestamp of the last update of the local table.
  *			Located at the base of the local table.
  * @hdr:		Base address of the header of the local table
  * @data:		Base address of the data of the local table
+ */
+struct hfi_table {
+	union {
+		void			*base_addr;
+		u64			*timestamp;
+	};
+	void			*hdr;
+	void			*data;
+};
+
+/**
+ * struct hfi_instance - Representation of an HFI instance (i.e., a table)
+ * @local_table:	Local copy of HFI table for this instance
  * @cpus:		CPUs represented in this HFI table instance
  * @hw_table:		Pointer to the HFI table of this instance
  * @update_work:	Delayed work to process HFI updates
@@ -112,12 +125,7 @@ struct hfi_hdr {
  * A set of parameters to parse and navigate a specific HFI table.
  */
 struct hfi_instance {
-	union {
-		void			*local_table;
-		u64			*timestamp;
-	};
-	void			*hdr;
-	void			*data;
+	struct hfi_table	local_table;
 	cpumask_var_t		cpus;
 	void			*hw_table;
 	struct delayed_work	update_work;
@@ -175,7 +183,7 @@ static void get_hfi_caps(struct hfi_instance *hfi_instance,
 		s16 index;
 
 		index = per_cpu(hfi_cpu_info, cpu).index;
-		caps = hfi_instance->data + index * hfi_features.cpu_stride;
+		caps = hfi_instance->local_table.data + index * hfi_features.cpu_stride;
 		cpu_caps[i].cpu = cpu;
 
 		/*
@@ -292,7 +300,7 @@ void intel_hfi_process_event(__u64 pkg_therm_status_msr_val)
 	 * where a lagging CPU entered the locked region.
 	 */
 	new_timestamp = *(u64 *)hfi_instance->hw_table;
-	if (*hfi_instance->timestamp == new_timestamp) {
+	if (*hfi_instance->local_table.timestamp == new_timestamp) {
 		thermal_clear_package_intr_status(PACKAGE_LEVEL, PACKAGE_THERM_STATUS_HFI_UPDATED);
 		raw_spin_unlock(&hfi_instance->event_lock);
 		return;
@@ -304,7 +312,7 @@ void intel_hfi_process_event(__u64 pkg_therm_status_msr_val)
 	 * Copy the updated table into our local copy. This includes the new
 	 * timestamp.
 	 */
-	memcpy(hfi_instance->local_table, hfi_instance->hw_table,
+	memcpy(hfi_instance->local_table.base_addr, hfi_instance->hw_table,
 	       hfi_features.nr_table_pages << PAGE_SHIFT);
 
 	/*
@@ -339,11 +347,12 @@ static void init_hfi_cpu_index(struct hfi_cpu_info *info)
 static void init_hfi_instance(struct hfi_instance *hfi_instance)
 {
 	/* The HFI header is below the time-stamp. */
-	hfi_instance->hdr = hfi_instance->local_table +
-			    sizeof(*hfi_instance->timestamp);
+	hfi_instance->local_table.hdr = hfi_instance->local_table.base_addr +
+					sizeof(*hfi_instance->local_table.timestamp);
 
 	/* The HFI data starts below the header. */
-	hfi_instance->data = hfi_instance->hdr + hfi_features.hdr_size;
+	hfi_instance->local_table.data = hfi_instance->local_table.hdr +
+					 hfi_features.hdr_size;
 }
 
 /* Caller must hold hfi_instance_lock. */
@@ -439,7 +448,7 @@ void intel_hfi_online(unsigned int cpu)
 	 * if needed.
 	 */
 	mutex_lock(&hfi_instance_lock);
-	if (hfi_instance->hdr)
+	if (hfi_instance->local_table.hdr)
 		goto enable;
 
 	/*
@@ -459,9 +468,9 @@ void intel_hfi_online(unsigned int cpu)
 	 * Allocate memory to keep a local copy of the table that
 	 * hardware generates.
 	 */
-	hfi_instance->local_table = kzalloc(hfi_features.nr_table_pages << PAGE_SHIFT,
-					    GFP_KERNEL);
-	if (!hfi_instance->local_table)
+	hfi_instance->local_table.base_addr = kzalloc(hfi_features.nr_table_pages << PAGE_SHIFT,
+						      GFP_KERNEL);
+	if (!hfi_instance->local_table.base_addr)
 		goto free_hw_table;
 
 	init_hfi_instance(hfi_instance);
@@ -512,7 +521,7 @@ void intel_hfi_offline(unsigned int cpu)
 	if (!hfi_instance)
 		return;
 
-	if (!hfi_instance->hdr)
+	if (!hfi_instance->local_table.hdr)
 		return;
 
 	mutex_lock(&hfi_instance_lock);
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/9 thermal: intel: hfi: Move selected data structures to a header file

In reply to Ricardo Neri

From: Zhuocheng Ding <[email protected]>

The virtualization of HFI needs the definitions of HFI capabilities and the HFI table. Move these definitions into a header file under arch/x86. No changes in the relocated structures.

---
 MAINTAINERS                       |  1 +
 arch/x86/include/asm/hfi.h        | 70 +++++++++++++++++++++++++++++++
 drivers/thermal/intel/intel_hfi.c | 66 +----------------------------
 3 files changed, 72 insertions(+), 65 deletions(-)
 create mode 100644 arch/x86/include/asm/hfi.h
MAINTAINERS +1 -0
index 8999497011a2..8bc873ba82fa 100644
@@ -21788,6 +21788,7 @@ L:	[email protected]
 S:	Supported
 Q:	https://patchwork.kernel.org/project/linux-pm/list/
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal
+F:	arch/x86/include/asm/hfi.h
 F:	Documentation/ABI/testing/sysfs-class-thermal
 F:	Documentation/admin-guide/thermal/
 F:	Documentation/devicetree/bindings/thermal/
arch/x86/include/asm/hfi.h new file +70 -0

70 lines not on this page · raw message

drivers/thermal/intel/intel_hfi.c +1 -65

92 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

PATCH 4/9 thermal: intel: hfi: Introduce Intel Thread Director classes

In reply to Ricardo Neri

On Intel hybrid parts, each type of CPU has specific performance and energy efficiency capabilities. The Intel Thread Director technology extends the Hardware Feedback Interface (HFI) to provide performance and energy efficiency data for advanced classes of instructions.

Add support to parse per-class capabilities.

---
Patch cherry-picked from the IPC classes patchset.
---

---
 arch/x86/include/asm/hfi.h        |  8 +++++++-
 drivers/thermal/intel/intel_hfi.c | 22 +++++++++++++++++-----
 2 files changed, 24 insertions(+), 6 deletions(-)
arch/x86/include/asm/hfi.h +7 -1
index ed8a548a376e..02ee56dbaeb6 100644
@@ -28,7 +28,8 @@ union cpuid6_edx {
  * @perf_updated:	Hardware updated performance capabilities
  * @ee_updated:		Hardware updated energy efficiency capabilities
  *
- * Properties of the data in an HFI table.
+ * Properties of the data in an HFI table. There exists one header per each
+ * HFI class.
  */
 struct hfi_hdr {
 	u8	perf_updated;
@@ -54,16 +55,21 @@ struct hfi_table {
 
 /**
  * struct hfi_features - Supported HFI features
+ * @nr_classes:		Number of classes supported
  * @nr_table_pages:	Size of the HFI table in 4KB pages
  * @cpu_stride:		Stride size to locate the capability data of a logical
  *			processor within the table (i.e., row stride)
+ * @class_stride:	Stride size to locate a class within the capability
+ *			data of a logical processor or the HFI table header
  * @hdr_size:		Size of the table header
  *
  * Parameters and supported features that are common to all HFI instances
  */
 struct hfi_features {
+	unsigned int	nr_classes;
 	size_t		nr_table_pages;
 	unsigned int	cpu_stride;
+	unsigned int	class_stride;
 	unsigned int	hdr_size;
 };
 
drivers/thermal/intel/intel_hfi.c +17 -5
index ee8950a60f72..3c399f3d059f 100644
@@ -57,7 +57,7 @@
  * @ee_cap:		Energy efficiency capability
  *
  * Capabilities of a logical processor in the HFI table. These capabilities are
- * unitless.
+ * unitless and specific to each HFI class.
  */
 struct hfi_cpu_data {
 	u8	perf_cap;
@@ -277,8 +277,8 @@ static void init_hfi_cpu_index(struct hfi_cpu_info *info)
 }
 
 /*
- * The format of the HFI table depends on the number of capabilities that the
- * hardware supports. Keep a data structure to navigate the table.
+ * The format of the HFI table depends on the number of capabilities and classes
+ * that the hardware supports. Keep a data structure to navigate the table.
  */
 static void init_hfi_instance(struct hfi_instance *hfi_instance)
 {
@@ -498,18 +498,30 @@ static __init int hfi_parse_features(void)
 	/* The number of 4KB pages required by the table */
 	hfi_features.nr_table_pages = edx.split.table_pages + 1;
 
+	/*
+	 * Capability fields of an HFI class are grouped together. Classes are
+	 * contiguous in memory.  Hence, use the number of supported features to
+	 * locate a specific class.
+	 */
+	hfi_features.class_stride = nr_capabilities;
+
+	/* For now, use only one class of the HFI table */
+	hfi_features.nr_classes = 1;
+
 	/*
 	 * The header contains change indications for each supported feature.
 	 * The size of the table header is rounded up to be a multiple of 8
 	 * bytes.
 	 */
-	hfi_features.hdr_size = DIV_ROUND_UP(nr_capabilities, 8) * 8;
+	hfi_features.hdr_size = DIV_ROUND_UP(nr_capabilities *
+					     hfi_features.nr_classes, 8) * 8;
 
 	/*
 	 * Data of each logical processor is also rounded up to be a multiple
 	 * of 8 bytes.
 	 */
-	hfi_features.cpu_stride = DIV_ROUND_UP(nr_capabilities, 8) * 8;
+	hfi_features.cpu_stride = DIV_ROUND_UP(nr_capabilities *
+					       hfi_features.nr_classes, 8) * 8;
 
 	return 0;
 }
Signature · 1 line
2.25.1
#5 lore raw

PATCH 5/9 x86/cpufeatures: Add the Intel Thread Director feature definitions

In reply to Ricardo Neri

Intel Thread Director (ITD) provides hardware resources to classify the current task. The classification reflects the instructions per cycle of such current task.

ITD extends the Hardware Feedback Interface table to provide performance and energy efficiency capabilities for each of the supported classes of tasks.

Cc Len Brown <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Srinivas Pandruvada <[email protected]>
Cc Stanislaw Gruszka <[email protected]>
Cc Zhao Liu <[email protected]>
Cc Zhuocheng Ding <[email protected]>

---
Patch cherry-picked from the IPC classes patchset.
---

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 arch/x86/kernel/cpu/cpuid-deps.c   | 1 +
 2 files changed, 2 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index fdf723b6f6d0..8104f4791abd 100644
@@ -360,6 +360,7 @@
 #define X86_FEATURE_HWP_EPP		(14*32+10) /* HWP Energy Perf. Preference */
 #define X86_FEATURE_HWP_PKG_REQ		(14*32+11) /* HWP Package Level Request */
 #define X86_FEATURE_HFI			(14*32+19) /* Hardware Feedback Interface */
+#define X86_FEATURE_ITD			(14*32+23) /* Intel Thread Director */
 
 /* AMD SVM Feature Identification, CPUID level 0x8000000a (EDX), word 15 */
 #define X86_FEATURE_NPT			(15*32+ 0) /* Nested Page Table support */
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
index e462c1d3800a..2ab036125a56 100644
@@ -82,6 +82,7 @@ static const struct cpuid_dep cpuid_deps[] = {
 	{ X86_FEATURE_XFD,			X86_FEATURE_XGETBV1   },
 	{ X86_FEATURE_AMX_TILE,			X86_FEATURE_XFD       },
 	{ X86_FEATURE_SHSTK,			X86_FEATURE_XSAVES    },
+	{ X86_FEATURE_ITD,			X86_FEATURE_HFI       },
 	{}
 };
 
Signature · 1 line
2.25.1
#6 lore raw

PATCH 6/9 thermal: intel: hfi: Enable Intel Thread Director

In reply to Ricardo Neri

Enable Intel Thread Director (ITD) from the CPU hotplug callback: globally from CPU0 and then enable the thread-classification hardware in each logical processor individually.

Also, initialize the number of classes supported.

Currently, a bare-metal machine does not use ITD, but KVM uses the attributes of the bare-metal machine to virtualize HFI.

---
Discussion:

This patch conflicts with a patchset from Stanislaw Gruszka to enable HFI only if there are user space entities listening to the thermal netlink events. ITD requires that HFI is enabled to function. ITD needs to be unconditionally enabled for virtual machines.

Options to resolve this conflict include a command-line argument for users wanting to virtualize HFI or a CONFIG_ option for the same effect. QEMU could also learn to listen to thermal netlink event. A blunter option is to unconditionally enable HFI when KVM is enabled at build time.
---
Patch cherry-picked from the IPC classes patchset.
---

---
 arch/x86/include/asm/hfi.h        |  9 ++++++
 arch/x86/include/asm/msr-index.h  |  6 ++++
 drivers/thermal/intel/intel_hfi.c | 52 +++++++++++++++++++++++++++++--
 3 files changed, 65 insertions(+), 2 deletions(-)
arch/x86/include/asm/hfi.h +9 -0
index 02ee56dbaeb6..b7fda3e0e8c8 100644
@@ -23,6 +23,15 @@ union cpuid6_edx {
 	u32 full;
 };
 
+union cpuid6_ecx {
+	struct {
+		u32	dont_care0:8;
+		u32	nr_classes:8;
+		u32	dont_care1:16;
+	} split;
+	u32 full;
+};
+
 /**
  * struct hfi_hdr - Header of the HFI table
  * @perf_updated:	Hardware updated performance capabilities
arch/x86/include/asm/msr-index.h +6 -0
index 46983fb0b5b3..d74932a0778d 100644
@@ -1148,6 +1148,12 @@
 
 #define MSR_IA32_HW_FEEDBACK_CONFIG     0x17d1
 #define HW_FEEDBACK_CONFIG_HFI_ENABLE   BIT_ULL(0)
+#define HW_FEEDBACK_CONFIG_ITD_ENABLE   BIT_ULL(1)
+
+#define MSR_IA32_HW_FEEDBACK_THREAD_CONFIG 0x17d4
+#define HW_FEEDBACK_THREAD_CONFIG_ENABLE BIT_ULL(0)
+
+#define MSR_IA32_HW_FEEDBACK_CHAR       0x17d2
 
 /* x2APIC locked status */
 #define MSR_IA32_XAPIC_DISABLE_STATUS	0xBD
drivers/thermal/intel/intel_hfi.c +50 -2

105 lines not on this page · raw message

Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH 6/9] thermal: intel: hfi: Enable Intel Thread Director

Quoted 20 lines · Ricardo Neri

Enable Intel Thread Director (ITD) from the CPU hotplug callback: globally from CPU0 and then enable the thread-classification hardware in each logical processor individually.

Also, initialize the number of classes supported.

Currently, a bare-metal machine does not use ITD, but KVM uses the attributes of the bare-metal machine to virtualize HFI.

Quoted 7 lines

---
Discussion:

This patch conflicts with a patchset from Stanislaw Gruszka to enable HFI only if there are user space entities listening to the thermal netlink events. ITD requires that HFI is enabled to function. ITD needs to be unconditionally enabled for virtual machines.

Why unconditionally? From what I can tell from KVM patches (please correct me if I'm wrong) guests need to be modified to utilize HFI/ITD. Do we also have to enable HFI/ITD if no such guest run on virtual machine ?

Quoted 4 lines

Options to resolve this conflict include a command-line argument for users wanting to virtualize HFI or a CONFIG_ option for the same effect. QEMU could also learn to listen to thermal netlink event. A blunter option is to unconditionally enable HFI when KVM is enabled at build time.

In general similar principle should be applied - do not enable if not needed. We should be able to get information from KVM when there is actual need. QEMU registering to thermal events seems to be odd for me, and I think there must be better solution.

Regards
Stanislaw

Quoted 166 lines

---
Patch cherry-picked from the IPC classes patchset.
---

---
 arch/x86/include/asm/hfi.h        |  9 ++++++
 arch/x86/include/asm/msr-index.h  |  6 ++++
 drivers/thermal/intel/intel_hfi.c | 52 +++++++++++++++++++++++++++++--
 3 files changed, 65 insertions(+), 2 deletions(-)
arch/x86/include/asm/hfi.h +9 -0
index 02ee56dbaeb6..b7fda3e0e8c8 100644
@@ -23,6 +23,15 @@ union cpuid6_edx {
 	u32 full;
 };
 
+union cpuid6_ecx {
+	struct {
+		u32	dont_care0:8;
+		u32	nr_classes:8;
+		u32	dont_care1:16;
+	} split;
+	u32 full;
+};
+
 /**
  * struct hfi_hdr - Header of the HFI table
  * @perf_updated:	Hardware updated performance capabilities
arch/x86/include/asm/msr-index.h +6 -0
index 46983fb0b5b3..d74932a0778d 100644
@@ -1148,6 +1148,12 @@
 
 #define MSR_IA32_HW_FEEDBACK_CONFIG     0x17d1
 #define HW_FEEDBACK_CONFIG_HFI_ENABLE   BIT_ULL(0)
+#define HW_FEEDBACK_CONFIG_ITD_ENABLE   BIT_ULL(1)
+
+#define MSR_IA32_HW_FEEDBACK_THREAD_CONFIG 0x17d4
+#define HW_FEEDBACK_THREAD_CONFIG_ENABLE BIT_ULL(0)
+
+#define MSR_IA32_HW_FEEDBACK_CHAR       0x17d2
 
 /* x2APIC locked status */
 #define MSR_IA32_XAPIC_DISABLE_STATUS	0xBD
drivers/thermal/intel/intel_hfi.c +50 -2
index 3c399f3d059f..b69fa234b317 100644
@@ -33,6 +33,7 @@
 #include <linux/percpu-defs.h>
 #include <linux/printk.h>
 #include <linux/processor.h>
+#include <linux/sched/topology.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/suspend.h>
@@ -298,6 +299,10 @@ static void hfi_enable(void)
 
 	rdmsrl(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
 	msr_val |= HW_FEEDBACK_CONFIG_HFI_ENABLE;
+
+	if (cpu_feature_enabled(X86_FEATURE_ITD))
+		msr_val |= HW_FEEDBACK_CONFIG_ITD_ENABLE;
+
 	wrmsrl(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
 }
 
@@ -319,6 +324,10 @@ static void hfi_disable(void)
 
 	rdmsrl(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
 	msr_val &= ~HW_FEEDBACK_CONFIG_HFI_ENABLE;
+
+	if (cpu_feature_enabled(X86_FEATURE_ITD))
+		msr_val &= ~HW_FEEDBACK_CONFIG_ITD_ENABLE;
+
 	wrmsrl(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
 
 	/*
@@ -337,6 +346,30 @@ static void hfi_disable(void)
 	}
 }
 
+static void hfi_enable_itd_classification(void)
+{
+	u64 msr_val;
+
+	if (!cpu_feature_enabled(X86_FEATURE_ITD))
+		return;
+
+	rdmsrl(MSR_IA32_HW_FEEDBACK_THREAD_CONFIG, msr_val);
+	msr_val |= HW_FEEDBACK_THREAD_CONFIG_ENABLE;
+	wrmsrl(MSR_IA32_HW_FEEDBACK_THREAD_CONFIG, msr_val);
+}
+
+static void hfi_disable_itd_classification(void)
+{
+	u64 msr_val;
+
+	if (!cpu_feature_enabled(X86_FEATURE_ITD))
+		return;
+
+	rdmsrl(MSR_IA32_HW_FEEDBACK_THREAD_CONFIG, msr_val);
+	msr_val &= ~HW_FEEDBACK_THREAD_CONFIG_ENABLE;
+	wrmsrl(MSR_IA32_HW_FEEDBACK_THREAD_CONFIG, msr_val);
+}
+
 /**
  * intel_hfi_online() - Enable HFI on @cpu
  * @cpu:	CPU in which the HFI will be enabled
@@ -377,6 +410,8 @@ void intel_hfi_online(unsigned int cpu)
 
 	init_hfi_cpu_index(info);
 
+	hfi_enable_itd_classification();
+
 	/*
 	 * Now check if the HFI instance of the package/die of @cpu has been
 	 * initialized (by checking its header). In such case, all we have to
@@ -460,6 +495,8 @@ void intel_hfi_offline(unsigned int cpu)
 	if (!hfi_instance->local_table.hdr)
 		return;
 
+	hfi_disable_itd_classification();
+
 	mutex_lock(&hfi_instance_lock);
 	cpumask_clear_cpu(cpu, hfi_instance->cpus);
 
@@ -505,8 +542,14 @@ static __init int hfi_parse_features(void)
 	 */
 	hfi_features.class_stride = nr_capabilities;
 
-	/* For now, use only one class of the HFI table */
-	hfi_features.nr_classes = 1;
+	if (cpu_feature_enabled(X86_FEATURE_ITD)) {
+		union cpuid6_ecx ecx;
+
+		ecx.full = cpuid_ecx(CPUID_HFI_LEAF);
+		hfi_features.nr_classes = ecx.split.nr_classes;
+	} else {
+		hfi_features.nr_classes = 1;
+	}
 
 	/*
 	 * The header contains change indications for each supported feature.
@@ -535,11 +578,16 @@ static void hfi_do_enable(void)
 	/* No locking needed. There is no concurrency with CPU online. */
 	hfi_set_hw_table(hfi_instance);
 	hfi_enable();
+
+	hfi_enable_itd_classification();
 }
 
 static int hfi_do_disable(void)
 {
 	/* No locking needed. There is no concurrency with CPU offline. */
+
+	hfi_disable_itd_classification();
+
 	hfi_disable();
 
 	return 0;
Signature · 1 line
2.25.1
#8 lore raw

Re: [PATCH 6/9] thermal: intel: hfi: Enable Intel Thread Director

Quoted 24 lines · Stanislaw Gruszka
Quoted 23 lines · Ricardo Neri

Enable Intel Thread Director (ITD) from the CPU hotplug callback: globally from CPU0 and then enable the thread-classification hardware in each logical processor individually.

Also, initialize the number of classes supported.

Currently, a bare-metal machine does not use ITD, but KVM uses the attributes of the bare-metal machine to virtualize HFI.

---
Discussion:

Quoted 8 lines
Quoted 4 lines

This patch conflicts with a patchset from Stanislaw Gruszka to enable HFI only if there are user space entities listening to the thermal netlink events. ITD requires that HFI is enabled to function. ITD needs to be unconditionally enabled for virtual machines.

Why unconditionally? From what I can tell from KVM patches (please correct me if I'm wrong) guests need to be modified to utilize HFI/ITD. Do we also have to enable HFI/ITD if no such guest run on virtual machine ?

You are correct. ITD needs to be enabled unconditionally iff we can't know when a guest machine is running.

Quoted 9 lines
Quoted 5 lines

Options to resolve this conflict include a command-line argument for users wanting to virtualize HFI or a CONFIG_ option for the same effect. QEMU could also learn to listen to thermal netlink event. A blunter option is to unconditionally enable HFI when KVM is enabled at build time.

In general similar principle should be applied - do not enable if not needed. We should be able to get information from KVM when there is actual need.

Agreed, Zhao had suggested to enable/disable when virtual machines are built/destroyed.

Quoted 2 lines

QEMU registering to thermal events seems to be odd for me, and I think there must be better solution.

Right.

#9 lore raw

PATCH 7/9 x86/cpufeatures: Add feature bit for HRESET

In reply to Ricardo Neri

The HRESET instruction isolates the classification of individual tasks when they run sequentially on the same logical processor. It resets the classification history that the logical processor maintains.

Cc Len Brown <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Srinivas Pandruvada <[email protected]>
Cc Stanislaw Gruszka <[email protected]>
Cc Zhao Liu <[email protected]>
Cc Zhuocheng Ding <[email protected]>

---
Patch cherry-picked from the IPC classes patchset.
---

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 arch/x86/include/asm/msr-index.h   | 3 +++
 2 files changed, 4 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 8104f4791abd..3b42479c049d 100644
@@ -326,6 +326,7 @@
 #define X86_FEATURE_FSRC		(12*32+12) /* "" Fast short REP {CMPSB,SCASB} */
 #define X86_FEATURE_LKGS		(12*32+18) /* "" Load "kernel" (userspace) GS */
 #define X86_FEATURE_AMX_FP16		(12*32+21) /* "" AMX fp16 Support */
+#define X86_FEATURE_HRESET		(12*32+22) /* Hardware history reset instruction */
 #define X86_FEATURE_AVX_IFMA            (12*32+23) /* "" Support for VPMADD52[H,L]UQ */
 #define X86_FEATURE_LAM			(12*32+26) /* Linear Address Masking */
 
arch/x86/include/asm/msr-index.h +3 -0
index d74932a0778d..65b1bfb9c304 100644
@@ -1155,6 +1155,9 @@
 
 #define MSR_IA32_HW_FEEDBACK_CHAR       0x17d2
 
+/* Hardware History Reset  */
+#define MSR_IA32_HW_HRESET_ENABLE	0x17da
+
 /* x2APIC locked status */
 #define MSR_IA32_XAPIC_DISABLE_STATUS	0xBD
 #define LEGACY_XAPIC_DISABLED		BIT(0) /*
Signature · 1 line
2.25.1
maintainer
#10 lore raw

Re: [PATCH 7/9] x86/cpufeatures: Add feature bit for HRESET

arch/x86/include/asm/cpufeatures.h line 329

Quoted 9 lines · Ricardo Neri
arch/x86/include/asm/cpufeatures.h +1 -0
index 8104f4791abd..3b42479c049d 100644
@@ -326,6 +326,7 @@
 #define X86_FEATURE_FSRC		(12*32+12) /* "" Fast short REP {CMPSB,SCASB} */
 #define X86_FEATURE_LKGS		(12*32+18) /* "" Load "kernel" (userspace) GS */
 #define X86_FEATURE_AMX_FP16		(12*32+21) /* "" AMX fp16 Support */
+#define X86_FEATURE_HRESET		(12*32+22) /* Hardware history reset instruction */
  #define X86_FEATURE_HRESET		(12*32+22) /* "" Hardware history reset instruction */

unless this really needs to be visible in /proc/cpuinfo:

Documentation/arch/x86/cpuinfo.rst

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#11 lore raw

Re: [PATCH 7/9] x86/cpufeatures: Add feature bit for HRESET

Quoted 13 lines · Borislav Petkov

arch/x86/include/asm/cpufeatures.h line 329

Quoted 9 lines · Ricardo Neri
arch/x86/include/asm/cpufeatures.h +1 -0
index 8104f4791abd..3b42479c049d 100644
@@ -326,6 +326,7 @@
 #define X86_FEATURE_FSRC		(12*32+12) /* "" Fast short REP {CMPSB,SCASB} */
 #define X86_FEATURE_LKGS		(12*32+18) /* "" Load "kernel" (userspace) GS */
 #define X86_FEATURE_AMX_FP16		(12*32+21) /* "" AMX fp16 Support */
+#define X86_FEATURE_HRESET		(12*32+22) /* Hardware history reset instruction */
  #define X86_FEATURE_HRESET		(12*32+22) /* "" Hardware history reset instruction */
Quoted 3 lines

unless this really needs to be visible in /proc/cpuinfo:

Documentation/arch/x86/cpuinfo.rst

Good point. There is no need to expose HRESET in /proc/cpuinfo. I will implement this change.

#12 lore raw

PATCH 8/9 x86/hreset: Configure history reset

In reply to Ricardo Neri

Configure the MSR that controls the behavior of HRESET on each logical processor.

Cc Len Brown <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Srinivas Pandruvada <[email protected]>
Cc Stanislaw Gruszka <[email protected]>
Cc Zhao Liu <[email protected]>
Cc Zhuocheng Ding <[email protected]>

---
Patch cherry-picked from the IPC classes patchset
---

---
 arch/x86/kernel/cpu/common.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/common.c +22 -1
index 0b97bcde70c6..bce8719b47c9 100644
@@ -381,6 +381,26 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)
 	cr4_clear_bits(X86_CR4_UMIP);
 }
 
+static u32 hardware_history_features __ro_after_init;
+
+static __always_inline void setup_hreset(struct cpuinfo_x86 *c)
+{
+	if (!cpu_feature_enabled(X86_FEATURE_HRESET))
+		return;
+
+	/*
+	 * Use on all CPUs the hardware history features that the boot
+	 * CPU supports.
+	 */
+	if (c == &boot_cpu_data)
+		hardware_history_features = cpuid_ebx(0x20);
+
+	if (!hardware_history_features)
+		return;
+
+	wrmsrl(MSR_IA32_HW_HRESET_ENABLE, hardware_history_features);
+}
+
 /* These bits should not change their value after CPU init is finished. */
 static const unsigned long cr4_pinned_mask =
 	X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP |
@@ -1872,10 +1892,11 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	/* Disable the PN if appropriate */
 	squash_the_stupid_serial_number(c);
 
-	/* Set up SMEP/SMAP/UMIP */
+	/* Set up SMEP/SMAP/UMIP/HRESET */
 	setup_smep(c);
 	setup_smap(c);
 	setup_umip(c);
+	setup_hreset(c);
 
 	/* Enable FSGSBASE instructions if available. */
 	if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
Signature · 1 line
2.25.1
maintainer
#13 lore raw

Re: [PATCH 8/9] x86/hreset: Configure history reset

arch/x86/kernel/cpu/common.c static __always_inline void setup_umip(struct cpuinfo_x86 *c) · line 396

Quoted 11 lines · Ricardo Neri
arch/x86/kernel/cpu/common.c +11 -0
+static __always_inline void setup_hreset(struct cpuinfo_x86 *c)
+{
+	if (!cpu_feature_enabled(X86_FEATURE_HRESET))
+		return;
+
+	/*
+	 * Use on all CPUs the hardware history features that the boot
+	 * CPU supports.
+	 */
+	if (c == &boot_cpu_data)
+		hardware_history_features = cpuid_ebx(0x20);

That's not what this does - that sets hardware_history_features on the BSP.

Why isn't this whole thing called in bsp_init_intel()?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#14 lore raw

Re: [PATCH 8/9] x86/hreset: Configure history reset

Quoted 15 lines · Borislav Petkov

arch/x86/kernel/cpu/common.c static __always_inline void setup_umip(struct cpuinfo_x86 *c) · line 396

Quoted 11 lines · Ricardo Neri
arch/x86/kernel/cpu/common.c +11 -0
+static __always_inline void setup_hreset(struct cpuinfo_x86 *c)
+{
+	if (!cpu_feature_enabled(X86_FEATURE_HRESET))
+		return;
+
+	/*
+	 * Use on all CPUs the hardware history features that the boot
+	 * CPU supports.
+	 */
+	if (c == &boot_cpu_data)
+		hardware_history_features = cpuid_ebx(0x20);

That's not what this does - that sets hardware_history_features on the BSP.

I meant to say that we will use the features that the BSP supports and use them to configure all other CPUs. I will reword the comment to make this clear.

Quoted 2 lines

Why isn't this whole thing called in bsp_init_intel()?

The register MSR_IA32_HW_HRESET_ENABLE needs to be configured on each CPU.

I can set hardware_history_features from bsp_init_intel() but I still need to call setup_hreset() on every CPU.

maintainer
#15 lore raw

Re: [PATCH 8/9] x86/hreset: Configure history reset

Quoted 2 lines · Ricardo Neri

I can set hardware_history_features from bsp_init_intel() but I still need to call setup_hreset() on every CPU.

init_intel() runs on every CPU.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#16 lore raw

Re: [PATCH 8/9] x86/hreset: Configure history reset

Quoted 5 lines · Borislav Petkov
Quoted 2 lines · Ricardo Neri

I can set hardware_history_features from bsp_init_intel() but I still need to call setup_hreset() on every CPU.

init_intel() runs on every CPU.

I see. Yes, this looks like the place to setup HRESET. Thank you for the pointer!

#17 lore raw

PATCH 9/9 x86/cpu: Introduce interface to reset hardware history

In reply to Ricardo Neri

KVM needs an interface to reset the history of vCPU at context switch. When called, hardware will start the classification of the next task from scratch.

Cc Len Brown <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Srinivas Pandruvada <[email protected]>
Cc Stanislaw Gruszka <[email protected]>
Cc Zhao Liu <[email protected]>
Cc Zhuocheng Ding <[email protected]>

---
Patch cherry-picked from the IPC classes patchset. Removed calls to reset_hardware_history() from context switch. Now KVM will call it directly when needed.
---
* Measurements of the cost of the HRESET instruction

Methodology:
I created a tight loop with interrupts and preemption disabled. I recorded the value of the TSC counter before and after executing HRESET or RDTSC. I repeated the measurement 100,000 times. I performed the experiment using an Alder Lake S system. I set the frequency of the CPUs at a fixed value.

The table below compares the cost of HRESET with RDTSC (expressed in the elapsed TSC count). The cost of the two instructions is comparable.

PCore ECore
Frequency (GHz) 5.0 3.8
HRESET (avg) 28.5 44.7
HRESET (stdev %) 3.6 2.3
RDTSC (avg) 25.2 35.7
RDTSC (stdev %) 3.9 2.6

---
 arch/x86/include/asm/hreset.h | 30 ++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/common.c  |  8 ++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 arch/x86/include/asm/hreset.h
arch/x86/include/asm/hreset.h new file +30 -0
new file mode 100644
index 000000000000..d68ca2fb8642
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_HRESET_H
+
+/**
+ * HRESET - History reset. Available since binutils v2.36.
+ *
+ * Request the processor to reset the history of task classification on the
+ * current logical processor. The history components to be
+ * reset are specified in %eax. Only bits specified in CPUID(0x20).EBX
+ * and enabled in the IA32_HRESET_ENABLE MSR can be selected.
+ *
+ * The assembly code looks like:
+ *
+ *	hreset %eax
+ *
+ * The corresponding machine code looks like:
+ *
+ *	F3 0F 3A F0 ModRM Imm
+ *
+ * The value of ModRM is 0xc0 to specify %eax register addressing.
+ * The ignored immediate operand is set to 0.
+ *
+ * The instruction is documented in the Intel SDM.
+ */
+
+#define __ASM_HRESET  ".byte 0xf3, 0xf, 0x3a, 0xf0, 0xc0, 0x0"
+
+void reset_hardware_history(void);
+
+#endif /* _ASM_X86_HRESET_H */
arch/x86/kernel/cpu/common.c +8 -0
index bce8719b47c9..ab9809520164 100644
@@ -57,6 +57,7 @@
 #include <asm/mce.h>
 #include <asm/msr.h>
 #include <asm/cacheinfo.h>
+#include <asm/hreset.h>
 #include <asm/memtype.h>
 #include <asm/microcode.h>
 #include <asm/intel-family.h>
@@ -383,6 +384,13 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)
 
 static u32 hardware_history_features __ro_after_init;
 
+void reset_hardware_history(void)
+{
+	asm_inline volatile (ALTERNATIVE("", __ASM_HRESET, X86_FEATURE_HRESET)
+			     : : "a" (hardware_history_features) : "memory");
+}
+EXPORT_SYMBOL(reset_hardware_history);
+
 static __always_inline void setup_hreset(struct cpuinfo_x86 *c)
 {
 	if (!cpu_feature_enabled(X86_FEATURE_HRESET))
Signature · 1 line
2.25.1
maintainer
#18 lore raw

Re: [PATCH 9/9] x86/cpu: Introduce interface to reset hardware history

arch/x86/kernel/cpu/common.c static __always_inline void setup_umip(struct cpuinfo_x86 *c) · line 391

Quoted 5 lines · Ricardo Neri
arch/x86/kernel/cpu/common.c +5 -0
+void reset_hardware_history(void)
+{
+	asm_inline volatile (ALTERNATIVE("", __ASM_HRESET, X86_FEATURE_HRESET)
+			     : : "a" (hardware_history_features) : "memory");
+}

This thing belongs in the header too, and it should be __always_inline.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#19 lore raw

Re: [PATCH 9/9] x86/cpu: Introduce interface to reset hardware history

Quoted 8 lines · Borislav Petkov

arch/x86/kernel/cpu/common.c static __always_inline void setup_umip(struct cpuinfo_x86 *c) · line 391

Quoted 5 lines · Ricardo Neri
arch/x86/kernel/cpu/common.c +5 -0
+void reset_hardware_history(void)
+{
+	asm_inline volatile (ALTERNATIVE("", __ASM_HRESET, X86_FEATURE_HRESET)
+			     : : "a" (hardware_history_features) : "memory");
+}

This thing belongs in the header too, and it should be __always_inline.

Thanks a lot for your feedback Boris!

Sure, I can move this code to the header ad use __always_inline.

BR,
Ricardo

#20 lore raw
In reply to Ricardo Neri
Quoted 6 lines · Ricardo Neri

Zhao Liu will soon post a patchset to virtualize the Hardware Feedback Interface (HFI) and Intel Thread Director (ITD) for the benefit of virtual machines that make use of ITD for scheduling. His experiments show up to 14% improvement in performance in some workloads and configurations. This series lays the foundation for his patchset. I will share Zhao's patchset when available.

Here are Zhao and Zhuocheng's patches:

https://lore.kernel.org/lkml/[email protected]/