Skip to contentSkip to conversation

backport "KVM: arm64: limit PMU version to PMUv3 for ARMv8.1"

PATCH Applied Zenghui Yu 2 participants 8 messages Nov 28, 2023 – Nov 30, 2023

Subsystems (3)
lore
Share & tools
lore

Zenghui Yu

author reviewer
#0 lore raw

cover backport "KVM: arm64: limit PMU version to PMUv3 for ARMv8.1"

We need to backport patch #1 as well because it introduced a helper used by patch #2.

Andrew Murray (2):
arm64: cpufeature: Extract capped perfmon fields
KVM: arm64: limit PMU version to PMUv3 for ARMv8.1

 arch/arm64/include/asm/cpufeature.h | 23 +++++++++++++++++++++++
 arch/arm64/include/asm/sysreg.h     |  6 ++++++
 arch/arm64/kvm/sys_regs.c           | 10 ++++++++++
 3 files changed, 39 insertions(+)
Signature · 1 line
2.33.0
author reviewer
#1 lore raw

PATCH 1/2 arm64: cpufeature: Extract capped perfmon fields

From: Andrew Murray <[email protected]>

commit 8e35aa642ee4dab01b16cc4b2df59d1936f3b3c2 upstream.

When emulating ID registers there is often a need to cap the version bits of a feature such that the guest will not use features that the host is not aware of. For example, when KVM mediates access to the PMU by emulating register accesses.

Let's add a helper that extracts a performance monitors ID field and caps the version to a given value.

Fields that identify the version of the Performance Monitors Extension do not follow the standard ID scheme, and instead follow the scheme described in ARM DDI 0487E.a page D13-2825 "Alternative ID scheme used for the Performance Monitors Extension version". The value 0xF means an IMPLEMENTATION DEFINED PMU is present, and values 0x0-OxE can be treated the same as an unsigned field with 0x0 meaning no PMU is present.

---
 arch/arm64/include/asm/cpufeature.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/arm64/include/asm/cpufeature.h +23 -0
index 05f41d8f7db3..a3cc478d2570 100644
@@ -422,6 +422,29 @@ cpuid_feature_extract_unsigned_field(u64 features, int field)
 	return cpuid_feature_extract_unsigned_field_width(features, field, 4);
 }
 
+/*
+ * Fields that identify the version of the Performance Monitors Extension do
+ * not follow the standard ID scheme. See ARM DDI 0487E.a page D13-2825,
+ * "Alternative ID scheme used for the Performance Monitors Extension version".
+ */
+static inline u64 __attribute_const__
+cpuid_feature_cap_perfmon_field(u64 features, int field, u64 cap)
+{
+	u64 val = cpuid_feature_extract_unsigned_field(features, field);
+	u64 mask = GENMASK_ULL(field + 3, field);
+
+	/* Treat IMPLEMENTATION DEFINED functionality as unimplemented */
+	if (val == 0xf)
+		val = 0;
+
+	if (val > cap) {
+		features &= ~mask;
+		features |= (cap << field) & mask;
+	}
+
+	return features;
+}
+
 static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp)
 {
 	return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);
Signature · 1 line
2.33.0
maintainer Applied
#2 lore raw

Patch "arm64: cpufeature: Extract capped perfmon fields" has been added to the 4.19-stable tree

This is a note to let you know that I've just added the patch titled

arm64: cpufeature: Extract capped perfmon fields

to the 4.19-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
arm64-cpufeature-extract-capped-perfmon-fields.patch
and it can be found in the queue-4.19 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.

From [email protected] Tue Nov 28 07:47:05 2023
From: Zenghui Yu <[email protected]>
Date: Tue, 28 Nov 2023 15:46:32 +0800
Subject: arm64: cpufeature: Extract capped perfmon fields
To: <[email protected]>, <[email protected]>, <[email protected]>
Cc: <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, Zenghui Yu <[email protected]>
Message-ID: <[email protected]>

From: Andrew Murray <[email protected]>

commit 8e35aa642ee4dab01b16cc4b2df59d1936f3b3c2 upstream.

When emulating ID registers there is often a need to cap the version bits of a feature such that the guest will not use features that the host is not aware of. For example, when KVM mediates access to the PMU by emulating register accesses.

Let's add a helper that extracts a performance monitors ID field and caps the version to a given value.

Fields that identify the version of the Performance Monitors Extension do not follow the standard ID scheme, and instead follow the scheme described in ARM DDI 0487E.a page D13-2825 "Alternative ID scheme used for the Performance Monitors Extension version". The value 0xF means an IMPLEMENTATION DEFINED PMU is present, and values 0x0-OxE can be treated the same as an unsigned field with 0x0 meaning no PMU is present.

---
 arch/arm64/include/asm/cpufeature.h |   23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/arm64/include/asm/cpufeature.h +23 -0
@@ -422,6 +422,29 @@ cpuid_feature_extract_unsigned_field(u64
 	return cpuid_feature_extract_unsigned_field_width(features, field, 4);
 }
 
+/*
+ * Fields that identify the version of the Performance Monitors Extension do
+ * not follow the standard ID scheme. See ARM DDI 0487E.a page D13-2825,
+ * "Alternative ID scheme used for the Performance Monitors Extension version".
+ */
+static inline u64 __attribute_const__
+cpuid_feature_cap_perfmon_field(u64 features, int field, u64 cap)
+{
+	u64 val = cpuid_feature_extract_unsigned_field(features, field);
+	u64 mask = GENMASK_ULL(field + 3, field);
+
+	/* Treat IMPLEMENTATION DEFINED functionality as unimplemented */
+	if (val == 0xf)
+		val = 0;
+
+	if (val > cap) {
+		features &= ~mask;
+		features |= (cap << field) & mask;
+	}
+
+	return features;
+}
+
 static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp)
 {
 	return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);

Patches currently in stable-queue which might be from [email protected] are

queue-4.19/arm64-cpufeature-extract-capped-perfmon-fields.patch queue-4.19/kvm-arm64-limit-pmu-version-to-pmuv3-for-armv8.1.patch

author reviewer
#3 lore raw

PATCH 2/2 KVM: arm64: limit PMU version to PMUv3 for ARMv8.1

In reply to Zenghui Yu

From: Andrew Murray <[email protected]>

commit c854188ea01062f5a5fd7f05658feb1863774eaa upstream.

We currently expose the PMU version of the host to the guest via emulation of the DFR0_EL1 and AA64DFR0_EL1 debug feature registers. However many of the features offered beyond PMUv3 for 8.1 are not supported in KVM. Examples of this include support for the PMMIR registers (added in PMUv3 for ARMv8.4) and 64-bit event counters added in (PMUv3 for ARMv8.5).

Let's trap the Debug Feature Registers in order to limit PMUVer/PerfMon in the Debug Feature Registers to PMUv3 for ARMv8.1 to avoid unexpected behaviour.

Both ID_AA64DFR0.PMUVer and ID_DFR0.PerfMon follow the "Alternative ID scheme used for the Performance Monitors Extension version" where 0xF means an IMPLEMENTATION DEFINED PMU is implemented, and values 0x0-0xE are treated as with an unsigned field (with 0x0 meaning no PMU is present). As we don't expect to expose an IMPLEMENTATION DEFINED PMU, and our cap is below 0xF, we can treat these fields as unsigned when applying the cap.

[Mark: make field names consistent, use perfmon cap]
[[email protected]: adjust the context in read_id_reg()]
---
 arch/arm64/include/asm/sysreg.h |  6 ++++++
 arch/arm64/kvm/sys_regs.c       | 10 ++++++++++
 2 files changed, 16 insertions(+)
arch/arm64/include/asm/sysreg.h +6 -0
index 22266c7e2cc1..0a8342de5796 100644
@@ -622,6 +622,12 @@
 #define ID_AA64DFR0_TRACEVER_SHIFT	4
 #define ID_AA64DFR0_DEBUGVER_SHIFT	0
 
+#define ID_AA64DFR0_PMUVER_8_1		0x4
+
+#define ID_DFR0_PERFMON_SHIFT		24
+
+#define ID_DFR0_PERFMON_8_1		0x4
+
 #define ID_ISAR5_RDM_SHIFT		24
 #define ID_ISAR5_CRC32_SHIFT		16
 #define ID_ISAR5_SHA2_SHIFT		12
arch/arm64/kvm/sys_regs.c +10 -0
index f06629bf2be1..2799e7e9915a 100644
@@ -1049,6 +1049,16 @@ static u64 read_id_reg(struct sys_reg_desc const *r, bool raz)
 			kvm_debug("LORegions unsupported for guests, suppressing\n");
 
 		val &= ~(0xfUL << ID_AA64MMFR1_LOR_SHIFT);
+	} else if (id == SYS_ID_AA64DFR0_EL1) {
+		/* Limit guests to PMUv3 for ARMv8.1 */
+		val = cpuid_feature_cap_perfmon_field(val,
+						ID_AA64DFR0_PMUVER_SHIFT,
+						ID_AA64DFR0_PMUVER_8_1);
+	} else if (id == SYS_ID_DFR0_EL1) {
+		/* Limit guests to PMUv3 for ARMv8.1 */
+		val = cpuid_feature_cap_perfmon_field(val,
+						ID_DFR0_PERFMON_SHIFT,
+						ID_DFR0_PERFMON_8_1);
 	}
 
 	return val;
Signature · 1 line
2.33.0
maintainer Applied
#4 lore raw

Patch "KVM: arm64: limit PMU version to PMUv3 for ARMv8.1" has been added to the 4.19-stable tree

This is a note to let you know that I've just added the patch titled

KVM: arm64: limit PMU version to PMUv3 for ARMv8.1

to the 4.19-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:

     kvm-arm64-limit-pmu-version-to-pmuv3-for-armv8.1.patch

and it can be found in the queue-4.19 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.

From [email protected] Tue Nov 28 07:47:10 2023
From: Zenghui Yu <[email protected]>
Date: Tue, 28 Nov 2023 15:46:33 +0800
Subject: KVM: arm64: limit PMU version to PMUv3 for ARMv8.1
To: <[email protected]>, <[email protected]>, <[email protected]>
Cc: <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, Zenghui Yu <[email protected]>
Message-ID: <[email protected]>

From: Andrew Murray <[email protected]>

commit c854188ea01062f5a5fd7f05658feb1863774eaa upstream.

We currently expose the PMU version of the host to the guest via emulation of the DFR0_EL1 and AA64DFR0_EL1 debug feature registers. However many of the features offered beyond PMUv3 for 8.1 are not supported in KVM. Examples of this include support for the PMMIR registers (added in PMUv3 for ARMv8.4) and 64-bit event counters added in (PMUv3 for ARMv8.5).

Let's trap the Debug Feature Registers in order to limit PMUVer/PerfMon in the Debug Feature Registers to PMUv3 for ARMv8.1 to avoid unexpected behaviour.

Both ID_AA64DFR0.PMUVer and ID_DFR0.PerfMon follow the "Alternative ID scheme used for the Performance Monitors Extension version" where 0xF means an IMPLEMENTATION DEFINED PMU is implemented, and values 0x0-0xE are treated as with an unsigned field (with 0x0 meaning no PMU is present). As we don't expect to expose an IMPLEMENTATION DEFINED PMU, and our cap is below 0xF, we can treat these fields as unsigned when applying the cap.

[Mark: make field names consistent, use perfmon cap]
[[email protected]: adjust the context in read_id_reg()]
---
 arch/arm64/include/asm/sysreg.h |    6 ++++++
 arch/arm64/kvm/sys_regs.c       |   10 ++++++++++
 2 files changed, 16 insertions(+)
arch/arm64/include/asm/sysreg.h +6 -0
@@ -622,6 +622,12 @@
 #define ID_AA64DFR0_TRACEVER_SHIFT	4
 #define ID_AA64DFR0_DEBUGVER_SHIFT	0
 
+#define ID_AA64DFR0_PMUVER_8_1		0x4
+
+#define ID_DFR0_PERFMON_SHIFT		24
+
+#define ID_DFR0_PERFMON_8_1		0x4
+
 #define ID_ISAR5_RDM_SHIFT		24
 #define ID_ISAR5_CRC32_SHIFT		16
 #define ID_ISAR5_SHA2_SHIFT		12
arch/arm64/kvm/sys_regs.c +10 -0
@@ -1049,6 +1049,16 @@ static u64 read_id_reg(struct sys_reg_de
 			kvm_debug("LORegions unsupported for guests, suppressing\n");
 
 		val &= ~(0xfUL << ID_AA64MMFR1_LOR_SHIFT);
+	} else if (id == SYS_ID_AA64DFR0_EL1) {
+		/* Limit guests to PMUv3 for ARMv8.1 */
+		val = cpuid_feature_cap_perfmon_field(val,
+						ID_AA64DFR0_PMUVER_SHIFT,
+						ID_AA64DFR0_PMUVER_8_1);
+	} else if (id == SYS_ID_DFR0_EL1) {
+		/* Limit guests to PMUv3 for ARMv8.1 */
+		val = cpuid_feature_cap_perfmon_field(val,
+						ID_DFR0_PERFMON_SHIFT,
+						ID_DFR0_PERFMON_8_1);
 	}
 
 	return val;

Patches currently in stable-queue which might be from [email protected] are

queue-4.19/arm64-cpufeature-extract-capped-perfmon-fields.patch queue-4.19/kvm-arm64-limit-pmu-version-to-pmuv3-for-armv8.1.patch

maintainer
#5 lore raw
In reply to Zenghui Yu
Quoted 6 lines · Zenghui Yu

We need to backport patch #1 as well because it introduced a helper used by patch #2.

Andrew Murray (2):
arm64: cpufeature: Extract capped perfmon fields
KVM: arm64: limit PMU version to PMUv3 for ARMv8.1

We can not just take these in an old stable tree and not newer ones as that would mean you could upgrade and have a regression. Please provide backports for all applicable stable trees and we will be glad to take them.

thanks,

greg k-h

author reviewer
#6 lore raw
Quoted 4 lines · Greg KH
Quoted 3 lines · Zenghui Yu

We need to backport patch #1 as well because it introduced a helper used by patch #2.

Quoted 8 lines
Quoted 3 lines

Andrew Murray (2):
arm64: cpufeature: Extract capped perfmon fields
KVM: arm64: limit PMU version to PMUv3 for ARMv8.1

We can not just take these in an old stable tree and not newer ones as that would mean you could upgrade and have a regression. Please provide backports for all applicable stable trees and we will be glad to take them.

Thanks for the heads up! "for-5.4" patches sent now.

Zenghui

maintainer
#7 lore raw
Quoted 9 lines · Zenghui Yu
Quoted 8 lines · Greg KH
Quoted 6 lines · Zenghui Yu

We need to backport patch #1 as well because it introduced a helper used by patch #2.

Andrew Murray (2):
arm64: cpufeature: Extract capped perfmon fields
KVM: arm64: limit PMU version to PMUv3 for ARMv8.1

Quoted 6 lines
Quoted 4 lines

We can not just take these in an old stable tree and not newer ones as that would mean you could upgrade and have a regression. Please provide backports for all applicable stable trees and we will be glad to take them.

Thanks for the heads up! "for-5.4" patches sent now.

Great, all now queued up, thanks.

greg k-h