Skip to contentSkip to conversation

arm64: Drop support for VPIPT i-cache policy

PATCH Applied Marc Zyngier 4 participants 10 messages Dec 4, 2023 – Dec 5, 2023

Subsystems (3)
lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

v3 cover arm64: Drop support for VPIPT i-cache policy

ARMv8.2 introduced support for VPIPT i-caches, the V standing for VMID-tagged. Although this looked like a reasonable idea, no implementation has ever made it into the wild.

Linux has supported this for over 6 years (amusingly, just as the architecture was dropping support for AIVIVT i-caches), but we had no way to even test it, and it is likely that this code was just bit-rotting.

However, in a recent breakthrough (XML drop 2023-09, tagged as d55f5af8e09052abe92a02adf820deea2eaed717), the architecture has finally been purged of this option, making VIPT and PIPT the only two valid options.

This really means this code is just dead code. Nobody will ever come up with such an implementation, and we can just get rid of it.

Most of the impact is on KVM, where we drop a few large comment blocks (and a bit of code), while the core arch code loses the detection code itself.

* From v2:
- Fix reserved naming for RESERVED_AIVIVT
- Collected RBs from Anshuman an Zenghui

Marc Zyngier (3):
KVM: arm64: Remove VPIPT I-cache handling
arm64: Kill detection of VPIPT i-cache policy
arm64: Rename reserved values for CTR_EL0.L1Ip

 arch/arm64/include/asm/cache.h   |  6 ----
 arch/arm64/include/asm/kvm_mmu.h |  7 ----
 arch/arm64/kernel/cpuinfo.c      |  5 ---
 arch/arm64/kvm/hyp/nvhe/pkvm.c   |  2 +-
 arch/arm64/kvm/hyp/nvhe/tlb.c    | 61 --------------------------------
 arch/arm64/kvm/hyp/vhe/tlb.c     | 13 -------
 arch/arm64/tools/sysreg          |  5 +--
 7 files changed, 4 insertions(+), 95 deletions(-)
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH v3 1/3 KVM: arm64: Remove VPIPT I-cache handling

We have some special handling for VPIPT I-cache in critical parts of the cache and TLB maintenance. Remove it.

---
 arch/arm64/include/asm/kvm_mmu.h |  7 ----
 arch/arm64/kvm/hyp/nvhe/pkvm.c   |  2 +-
 arch/arm64/kvm/hyp/nvhe/tlb.c    | 61 --------------------------------
 arch/arm64/kvm/hyp/vhe/tlb.c     | 13 -------
 4 files changed, 1 insertion(+), 82 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +0 -7
index 49e0d4b36bd0..e3e793d0ec30 100644
@@ -243,13 +243,6 @@ static inline size_t __invalidate_icache_max_range(void)
 
 static inline void __invalidate_icache_guest_page(void *va, size_t size)
 {
-	/*
-	 * VPIPT I-cache maintenance must be done from EL2. See comment in the
-	 * nVHE flavor of __kvm_tlb_flush_vmid_ipa().
-	 */
-	if (icache_is_vpipt() && read_sysreg(CurrentEL) != CurrentEL_EL2)
-		return;
-
 	/*
 	 * Blow the whole I-cache if it is aliasing (i.e. VIPT) or the
 	 * invalidation range exceeds our arbitrary limit on invadations by
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
index 9d23a51d7f75..b29f15418c0a 100644
@@ -12,7 +12,7 @@
 #include <nvhe/pkvm.h>
 #include <nvhe/trap_handler.h>
 
-/* Used by icache_is_vpipt(). */
+/* Used by icache_is_aliasing(). */
 unsigned long __icache_flags;
 
 /* Used by kvm_get_vttbr(). */
arch/arm64/kvm/hyp/nvhe/tlb.c +0 -61

84 lines not on this page · raw message

arch/arm64/kvm/hyp/vhe/tlb.c +0 -13
index b636b4111dbf..b32e2940df7d 100644
@@ -216,18 +216,5 @@ void __kvm_flush_vm_context(void)
 {
 	dsb(ishst);
 	__tlbi(alle1is);
-
-	/*
-	 * VIPT and PIPT caches are not affected by VMID, so no maintenance
-	 * is necessary across a VMID rollover.
-	 *
-	 * VPIPT caches constrain lookup and maintenance to the active VMID,
-	 * so we need to invalidate lines with a stale VMID to avoid an ABA
-	 * race after multiple rollovers.
-	 *
-	 */
-	if (icache_is_vpipt())
-		asm volatile("ic ialluis");
-
 	dsb(ish);
 }
Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH v3 2/3 arm64: Kill detection of VPIPT i-cache policy

In reply to Marc Zyngier

Since the kernel will never run on a system with the VPIPT i-cache policy, drop the detection code altogether.

---
 arch/arm64/include/asm/cache.h | 6 ------
 arch/arm64/kernel/cpuinfo.c    | 5 -----
 2 files changed, 11 deletions(-)
arch/arm64/include/asm/cache.h +0 -6
index ceb368d33bf4..06a4670bdb0b 100644
@@ -58,7 +58,6 @@ static inline unsigned int arch_slab_minalign(void)
 #define CTR_L1IP(ctr)		SYS_FIELD_GET(CTR_EL0, L1Ip, ctr)
 
 #define ICACHEF_ALIASING	0
-#define ICACHEF_VPIPT		1
 extern unsigned long __icache_flags;
 
 /*
@@ -70,11 +69,6 @@ static inline int icache_is_aliasing(void)
 	return test_bit(ICACHEF_ALIASING, &__icache_flags);
 }
 
-static __always_inline int icache_is_vpipt(void)
-{
-	return test_bit(ICACHEF_VPIPT, &__icache_flags);
-}
-
 static inline u32 cache_type_cwg(void)
 {
 	return SYS_FIELD_GET(CTR_EL0, CWG, read_cpuid_cachetype());
arch/arm64/kernel/cpuinfo.c +0 -5
index a257da7b56fe..47043c0d95ec 100644
@@ -36,8 +36,6 @@ static struct cpuinfo_arm64 boot_cpu_data;
 static inline const char *icache_policy_str(int l1ip)
 {
 	switch (l1ip) {
-	case CTR_EL0_L1Ip_VPIPT:
-		return "VPIPT";
 	case CTR_EL0_L1Ip_VIPT:
 		return "VIPT";
 	case CTR_EL0_L1Ip_PIPT:
@@ -388,9 +386,6 @@ static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
 	switch (l1ip) {
 	case CTR_EL0_L1Ip_PIPT:
 		break;
-	case CTR_EL0_L1Ip_VPIPT:
-		set_bit(ICACHEF_VPIPT, &__icache_flags);
-		break;
 	case CTR_EL0_L1Ip_VIPT:
 	default:
 		/* Assume aliasing */
Signature · 1 line
2.39.2
author maintainer
#3 lore raw

PATCH v3 3/3 arm64: Rename reserved values for CTR_EL0.L1Ip

In reply to Marc Zyngier

We now have *two* values for CTR_EL0.L1Ip that are reserved. Which makes things a bit awkward. In order to lift the ambiguity, rename RESERVED (0b01) to RESERVED_AIVIVT, and VPIPT (0b00) to RESERVED_VPIPT.

This makes it clear which of these meant what, and I'm sure archeologists will find it useful...

---
 arch/arm64/tools/sysreg | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/tools/sysreg +3 -2
index 96cbeeab4eec..c5af75b23187 100644
@@ -2004,9 +2004,10 @@ Field	27:24	CWG
 Field	23:20	ERG
 Field	19:16	DminLine
 Enum	15:14	L1Ip
-	0b00	VPIPT
+	# This was named as VPIPT in the ARM but now documented as reserved
+	0b00	RESERVED_VPIPT
 	# This is named as AIVIVT in the ARM but documented as reserved
-	0b01	RESERVED
+	0b01	RESERVED_AIVIVT
 	0b10	VIPT
 	0b11	PIPT
 EndEnum
Signature · 1 line
2.39.2
Reviewed-by on v3 3/3
#4 lore raw

Re: [PATCH v3 3/3] arm64: Rename reserved values for CTR_EL0.L1Ip

Quoted 10 lines · Marc Zyngier

We now have *two* values for CTR_EL0.L1Ip that are reserved. Which makes things a bit awkward. In order to lift the ambiguity, rename RESERVED (0b01) to RESERVED_AIVIVT, and VPIPT (0b00) to RESERVED_VPIPT.

This makes it clear which of these meant what, and I'm sure archeologists will find it useful...

Quoted 21 lines
---
 arch/arm64/tools/sysreg | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/tools/sysreg +3 -2
index 96cbeeab4eec..c5af75b23187 100644
@@ -2004,9 +2004,10 @@ Field	27:24	CWG
 Field	23:20	ERG
 Field	19:16	DminLine
 Enum	15:14	L1Ip
-	0b00	VPIPT
+	# This was named as VPIPT in the ARM but now documented as reserved
+	0b00	RESERVED_VPIPT
 	# This is named as AIVIVT in the ARM but documented as reserved
-	0b01	RESERVED
+	0b01	RESERVED_AIVIVT
 	0b10	VIPT
 	0b11	PIPT
 EndEnum
reviewer Acked-by on the v3 series
#5 lore raw
In reply to Marc Zyngier
Quoted 21 lines · Marc Zyngier

ARMv8.2 introduced support for VPIPT i-caches, the V standing for VMID-tagged. Although this looked like a reasonable idea, no implementation has ever made it into the wild.

Linux has supported this for over 6 years (amusingly, just as the architecture was dropping support for AIVIVT i-caches), but we had no way to even test it, and it is likely that this code was just bit-rotting.

However, in a recent breakthrough (XML drop 2023-09, tagged as d55f5af8e09052abe92a02adf820deea2eaed717), the architecture has finally been purged of this option, making VIPT and PIPT the only two valid options.

This really means this code is just dead code. Nobody will ever come up with such an implementation, and we can just get rid of it.

Most of the impact is on KVM, where we drop a few large comment blocks (and a bit of code), while the core arch code loses the detection code itself.

Quoted 8 lines

* From v2:
- Fix reserved naming for RESERVED_AIVIVT
- Collected RBs from Anshuman an Zenghui

Marc Zyngier (3):
KVM: arm64: Remove VPIPT I-cache handling
arm64: Kill detection of VPIPT i-cache policy
arm64: Rename reserved values for CTR_EL0.L1Ip

For the series:

Looking forward, we can/should probably replace __icache_flags with a single ICACHE_NOALIASING or ICACHE_PIPT cpucap, which'd get rid of a bunch of duplicated logic and make that more sound in the case of races around cpu onlining.

Mark.

Quoted 14 lines
 arch/arm64/include/asm/cache.h   |  6 ----
 arch/arm64/include/asm/kvm_mmu.h |  7 ----
 arch/arm64/kernel/cpuinfo.c      |  5 ---
 arch/arm64/kvm/hyp/nvhe/pkvm.c   |  2 +-
 arch/arm64/kvm/hyp/nvhe/tlb.c    | 61 --------------------------------
 arch/arm64/kvm/hyp/vhe/tlb.c     | 13 -------
 arch/arm64/tools/sysreg          |  5 +--
 7 files changed, 4 insertions(+), 95 deletions(-)
Signature · 1 line
2.39.2
author maintainer
#6 lore raw
Quoted 27 lines · Mark Rutland
Quoted 25 lines · Marc Zyngier

ARMv8.2 introduced support for VPIPT i-caches, the V standing for VMID-tagged. Although this looked like a reasonable idea, no implementation has ever made it into the wild.

Linux has supported this for over 6 years (amusingly, just as the architecture was dropping support for AIVIVT i-caches), but we had no way to even test it, and it is likely that this code was just bit-rotting.

However, in a recent breakthrough (XML drop 2023-09, tagged as d55f5af8e09052abe92a02adf820deea2eaed717), the architecture has finally been purged of this option, making VIPT and PIPT the only two valid options.

This really means this code is just dead code. Nobody will ever come up with such an implementation, and we can just get rid of it.

Most of the impact is on KVM, where we drop a few large comment blocks (and a bit of code), while the core arch code loses the detection code itself.

* From v2:
- Fix reserved naming for RESERVED_AIVIVT
- Collected RBs from Anshuman an Zenghui

Quoted 8 lines
Quoted 4 lines

Marc Zyngier (3):
KVM: arm64: Remove VPIPT I-cache handling
arm64: Kill detection of VPIPT i-cache policy
arm64: Rename reserved values for CTR_EL0.L1Ip

For the series:

Thanks.

Quoted 4 lines

Looking forward, we can/should probably replace __icache_flags with a single ICACHE_NOALIASING or ICACHE_PIPT cpucap, which'd get rid of a bunch of duplicated logic and make that more sound in the case of races around cpu onlining.

As long as we refuse VIPT CPUs coming up late (i.e. after we have patched the kernel to set ICACHE_PIPT), it should be doable. I guess we already have this restriction as userspace is able to probe the I-cache policy anyway.

How about the patch below (tested in a guest with a bunch of hacks to expose different L1Ip values)?

Thanks,

	M.

From 8f88afb0b317213dcbf18ea460a508bfccc18568 Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Mon, 4 Dec 2023 18:09:40 +0000
Subject: [PATCH] arm64: Make icache detection a cpu capability

Now that we only have two icache policies, we are in a good position to make the whole detection business more robust.

Let's replace __icache_flags by a single capability (ICACHE_PIPT), and use this if all CPUs are indeed PIPT. It means we can rely on existing logic to mandate that a VIPT CPU coming up late will be denied booting, which is the safe thing to do.

This also leads to some nice cleanups in pKVM, and KVM as a whole can use ARM64_ICACHE_PIPT as a final cap.

---
 arch/arm64/include/asm/cache.h   |  9 ++-------
 arch/arm64/include/asm/kvm_hyp.h |  1 -
 arch/arm64/include/asm/kvm_mmu.h |  2 +-
 arch/arm64/kernel/cpufeature.c   |  7 +++++++
 arch/arm64/kernel/cpuinfo.c      | 34 --------------------------------
 arch/arm64/kvm/arm.c             |  1 -
 arch/arm64/kvm/hyp/nvhe/pkvm.c   |  3 ---
 arch/arm64/tools/cpucaps         |  1 +
 arch/arm64/tools/sysreg          |  2 +-
 9 files changed, 12 insertions(+), 48 deletions(-)
arch/arm64/include/asm/cache.h +2 -7
index 06a4670bdb0b..8ef9522a6151 100644
@@ -37,9 +37,9 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/bitops.h>
 #include <linux/kasan-enabled.h>
 
+#include <asm/cpufeature.h>
 #include <asm/cputype.h>
 #include <asm/mte-def.h>
 #include <asm/sysreg.h>
@@ -55,18 +55,13 @@ static inline unsigned int arch_slab_minalign(void)
 #define arch_slab_minalign() arch_slab_minalign()
 #endif
 
-#define CTR_L1IP(ctr)		SYS_FIELD_GET(CTR_EL0, L1Ip, ctr)
-
-#define ICACHEF_ALIASING	0
-extern unsigned long __icache_flags;
-
 /*
  * Whilst the D-side always behaves as PIPT on AArch64, aliasing is
  * permitted in the I-cache.
  */
 static inline int icache_is_aliasing(void)
 {
-	return test_bit(ICACHEF_ALIASING, &__icache_flags);
+	return !cpus_have_cap(ARM64_ICACHE_PIPT);
 }
 
 static inline u32 cache_type_cwg(void)
arch/arm64/include/asm/kvm_hyp.h +0 -1
index 145ce73fc16c..7ad70f9865fd 100644
@@ -140,7 +140,6 @@ extern u64 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val);
 extern u64 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val);
 extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val);
 
-extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
 
 #endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/include/asm/kvm_mmu.h +1 -1
index e3e793d0ec30..ab86c2f025cf 100644
@@ -248,7 +248,7 @@ static inline void __invalidate_icache_guest_page(void *va, size_t size)
 	 * invalidation range exceeds our arbitrary limit on invadations by
 	 * cache line.
 	 */
-	if (icache_is_aliasing() || size > __invalidate_icache_max_range())
+	if (!cpus_have_final_cap(ARM64_ICACHE_PIPT) || size > __invalidate_icache_max_range())
 		icache_inval_all_pou();
 	else
 		icache_inval_pou((unsigned long)va, (unsigned long)va + size);
arch/arm64/kernel/cpufeature.c +7 -0
index 646591c67e7a..73a37176676e 100644
@@ -2272,6 +2272,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
 		.matches = has_always,
 	},
+	{
+		.desc = "PIPT I-cache policy",
+		.capability = ARM64_ICACHE_PIPT,
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.matches = has_cpuid_feature,
+		ARM64_CPUID_FIELDS(CTR_EL0, L1Ip, PIPT)
+	},
 	{
 		.desc = "GIC system register CPU interface",
 		.capability = ARM64_HAS_GIC_CPUIF_SYSREGS,
arch/arm64/kernel/cpuinfo.c +0 -34
index 47043c0d95ec..a4ea331fb6d4 100644
@@ -33,20 +33,6 @@
 DEFINE_PER_CPU(struct cpuinfo_arm64, cpu_data);
 static struct cpuinfo_arm64 boot_cpu_data;
 
-static inline const char *icache_policy_str(int l1ip)
-{
-	switch (l1ip) {
-	case CTR_EL0_L1Ip_VIPT:
-		return "VIPT";
-	case CTR_EL0_L1Ip_PIPT:
-		return "PIPT";
-	default:
-		return "RESERVED/UNKNOWN";
-	}
-}
-
-unsigned long __icache_flags;
-
 static const char *const hwcap_str[] = {
 	[KERNEL_HWCAP_FP]		= "fp",
 	[KERNEL_HWCAP_ASIMD]		= "asimd",
@@ -378,24 +364,6 @@ static int __init cpuinfo_regs_init(void)
 }
 device_initcall(cpuinfo_regs_init);
 
-static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
-{
-	unsigned int cpu = smp_processor_id();
-	u32 l1ip = CTR_L1IP(info->reg_ctr);
-
-	switch (l1ip) {
-	case CTR_EL0_L1Ip_PIPT:
-		break;
-	case CTR_EL0_L1Ip_VIPT:
-	default:
-		/* Assume aliasing */
-		set_bit(ICACHEF_ALIASING, &__icache_flags);
-		break;
-	}
-
-	pr_info("Detected %s I-cache on CPU%d\n", icache_policy_str(l1ip), cpu);
-}
-
 static void __cpuinfo_store_cpu_32bit(struct cpuinfo_32bit *info)
 {
 	info->reg_id_dfr0 = read_cpuid(ID_DFR0_EL1);
@@ -457,8 +425,6 @@ static void __cpuinfo_store_cpu(struct cpuinfo_arm64 *info)
 
 	if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0))
 		__cpuinfo_store_cpu_32bit(&info->aarch32);
-
-	cpuinfo_detect_icache_policy(info);
 }
 
 void cpuinfo_store_cpu(void)
arch/arm64/kvm/arm.c +0 -1
index e5f75f1f1085..a5f71165cd5c 100644
@@ -2242,7 +2242,6 @@ static void kvm_hyp_init_symbols(void)
 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
 	kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
-	kvm_nvhe_sym(__icache_flags) = __icache_flags;
 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
 }
 
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -3
index b29f15418c0a..187ce5720697 100644
@@ -12,9 +12,6 @@
 #include <nvhe/pkvm.h>
 #include <nvhe/trap_handler.h>
 
-/* Used by icache_is_aliasing(). */
-unsigned long __icache_flags;
-
 /* Used by kvm_get_vttbr(). */
 unsigned int kvm_arm_vmid_bits;
 
arch/arm64/tools/cpucaps +1 -0
index b98c38288a9d..9c6313cd6f5e 100644
@@ -53,6 +53,7 @@ HAS_TLB_RANGE
 HAS_VIRT_HOST_EXTN
 HAS_WFXT
 HW_DBM
+ICACHE_PIPT
 KVM_HVHE
 KVM_PROTECTED_MODE
 MISMATCHED_CACHE_TYPE
arch/arm64/tools/sysreg +1 -1
index c5af75b23187..db8c96841138 100644
@@ -2003,7 +2003,7 @@ Field	28	IDC
 Field	27:24	CWG
 Field	23:20	ERG
 Field	19:16	DminLine
-Enum	15:14	L1Ip
+UnsignedEnum	15:14	L1Ip
 	# This was named as VPIPT in the ARM but now documented as reserved
 	0b00	RESERVED_VPIPT
 	# This is named as AIVIVT in the ARM but documented as reserved
Signature · 5 lines
2.39.2


-- 
Without deviation from the norm, progress is not possible.
reviewer
#7 lore raw
Quoted 45 lines · Marc Zyngier
Quoted 27 lines · Mark Rutland
Quoted 25 lines · Marc Zyngier

ARMv8.2 introduced support for VPIPT i-caches, the V standing for VMID-tagged. Although this looked like a reasonable idea, no implementation has ever made it into the wild.

Linux has supported this for over 6 years (amusingly, just as the architecture was dropping support for AIVIVT i-caches), but we had no way to even test it, and it is likely that this code was just bit-rotting.

However, in a recent breakthrough (XML drop 2023-09, tagged as d55f5af8e09052abe92a02adf820deea2eaed717), the architecture has finally been purged of this option, making VIPT and PIPT the only two valid options.

This really means this code is just dead code. Nobody will ever come up with such an implementation, and we can just get rid of it.

Most of the impact is on KVM, where we drop a few large comment blocks (and a bit of code), while the core arch code loses the detection code itself.

* From v2:
- Fix reserved naming for RESERVED_AIVIVT
- Collected RBs from Anshuman an Zenghui

Quoted 8 lines
Quoted 4 lines

Marc Zyngier (3):
KVM: arm64: Remove VPIPT I-cache handling
arm64: Kill detection of VPIPT i-cache policy
arm64: Rename reserved values for CTR_EL0.L1Ip

For the series:

Thanks.

Quoted 4 lines

Looking forward, we can/should probably replace __icache_flags with a single ICACHE_NOALIASING or ICACHE_PIPT cpucap, which'd get rid of a bunch of duplicated logic and make that more sound in the case of races around cpu onlining.

Quoted 7 lines

As long as we refuse VIPT CPUs coming up late (i.e. after we have patched the kernel to set ICACHE_PIPT), it should be doable. I guess we already have this restriction as userspace is able to probe the I-cache policy anyway.

How about the patch below (tested in a guest with a bunch of hacks to expose different L1Ip values)?

That's roughly what I was thinking; the diff looks good, minor comments below.

Quoted 23 lines

Thanks,

	M.

From 8f88afb0b317213dcbf18ea460a508bfccc18568 Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Mon, 4 Dec 2023 18:09:40 +0000
Subject: [PATCH] arm64: Make icache detection a cpu capability

Now that we only have two icache policies, we are in a good position to make the whole detection business more robust.

Let's replace __icache_flags by a single capability (ICACHE_PIPT), and use this if all CPUs are indeed PIPT. It means we can rely on existing logic to mandate that a VIPT CPU coming up late will be denied booting, which is the safe thing to do.

This also leads to some nice cleanups in pKVM, and KVM as a whole can use ARM64_ICACHE_PIPT as a final cap.

Quoted 11 lines
---
 arch/arm64/include/asm/cache.h   |  9 ++-------
 arch/arm64/include/asm/kvm_hyp.h |  1 -
 arch/arm64/include/asm/kvm_mmu.h |  2 +-
 arch/arm64/kernel/cpufeature.c   |  7 +++++++
 arch/arm64/kernel/cpuinfo.c      | 34 --------------------------------
 arch/arm64/kvm/arm.c             |  1 -
 arch/arm64/kvm/hyp/nvhe/pkvm.c   |  3 ---
 arch/arm64/tools/cpucaps         |  1 +
 arch/arm64/tools/sysreg          |  2 +-
 9 files changed, 12 insertions(+), 48 deletions(-)

Nice diffstat!

[...]

arch/arm64/include/asm/cache.h static inline unsigned int arch_slab_minalign(void) · line 65

Quoted 9 lines

/*
* Whilst the D-side always behaves as PIPT on AArch64, aliasing is
* permitted in the I-cache.
*/
static inline int icache_is_aliasing(void)
{

arch/arm64/include/asm/cache.h +1 -1
-	return test_bit(ICACHEF_ALIASING, &__icache_flags);
+	return !cpus_have_cap(ARM64_ICACHE_PIPT);
 }

It might be nicer to use alternative_has_cap_{likely,unlikely}(...) for consistency with other cap checks, though that won't matter for hyp code and I don't think the likely/unlikely part particularly matters either.

[...]

Quoted 7 lines
arch/arm64/kernel/cpuinfo.c +0 -7
-static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
-{
-	unsigned int cpu = smp_processor_id();
-	u32 l1ip = CTR_L1IP(info->reg_ctr);
-
-	switch (l1ip) {
-	case CTR_EL0_L1Ip_PIPT:

arch/arm64/kernel/cpuinfo.c static int __init cpuinfo_regs_init(void) · line 397

Quoted 10 lines
arch/arm64/kernel/cpuinfo.c +0 -10
-		break;
-	case CTR_EL0_L1Ip_VIPT:
-	default:
-		/* Assume aliasing */
-		set_bit(ICACHEF_ALIASING, &__icache_flags);
-		break;
-	}
-
-	pr_info("Detected %s I-cache on CPU%d\n", icache_policy_str(l1ip), cpu);
-}

Not printing this for each CPU is a nice bonus!

[...]

arch/arm64/tools/sysreg Field 28 IDC · line 2009

Quoted 13 lines
arch/arm64/tools/sysreg +1 -1
index c5af75b23187..db8c96841138 100644
@@ -2003,7 +2003,7 @@ Field	28	IDC
 Field	27:24	CWG
 Field	23:20	ERG
 Field	19:16	DminLine
-Enum	15:14	L1Ip
+UnsignedEnum	15:14	L1Ip
 	# This was named as VPIPT in the ARM but now documented as reserved
 	0b00	RESERVED_VPIPT
 	# This is named as AIVIVT in the ARM but documented as reserved

I was initially surprised by the use of UnsignedEnum, but given PIPT is 0b11, I can see that works. Otherwise, we can keep this as an enum and use a helper that checks for an exact match.

Mark.

author maintainer
#8 lore raw
Quoted 9 lines · Mark Rutland
Quoted 2 lines · Marc Zyngier

How about the patch below (tested in a guest with a bunch of hacks to expose different L1Ip values)?

That's roughly what I was thinking; the diff looks good, minor comments below.

[...]

Quoted 13 lines

arch/arm64/include/asm/cache.h static inline unsigned int arch_slab_minalign(void) · line 65

Quoted 9 lines

/*
* Whilst the D-side always behaves as PIPT on AArch64, aliasing is
* permitted in the I-cache.
*/
static inline int icache_is_aliasing(void)
{

arch/arm64/include/asm/cache.h +1 -1
-	return test_bit(ICACHEF_ALIASING, &__icache_flags);
+	return !cpus_have_cap(ARM64_ICACHE_PIPT);
 }

It might be nicer to use alternative_has_cap_{likely,unlikely}(...) for consistency with other cap checks, though that won't matter for hyp code and I don't think the likely/unlikely part particularly matters either.

Right, this should be marginally better. I was initially worried of the cap not being configured yet, but since we start by assuming VIPT and only flip to PIPT once we have seen all CPUs, this stays correct by construction.

Quoted 11 lines

[...]

Quoted 9 lines
arch/arm64/tools/sysreg +0 -1
index c5af75b23187..db8c96841138 100644
@@ -2003,7 +2003,7 @@ Field	28	IDC
 Field	27:24	CWG
 Field	23:20	ERG
 Field	19:16	DminLine
-Enum	15:14	L1Ip
Quoted 8 lines

arch/arm64/tools/sysreg Field 28 IDC · line 2009

Quoted 4 lines
arch/arm64/tools/sysreg +1 -0
+UnsignedEnum	15:14	L1Ip
 	# This was named as VPIPT in the ARM but now documented as reserved
 	0b00	RESERVED_VPIPT
 	# This is named as AIVIVT in the ARM but documented as reserved

I was initially surprised by the use of UnsignedEnum, but given PIPT is 0b11, I can see that works. Otherwise, we can keep this as an enum and use a helper that checks for an exact match.

Yeah, I hesitated either way, but maybe the helper approach is cleaner. I'll give it a go and repost this patch.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer Applied
#9 lore raw
In reply to Marc Zyngier
Quoted 10 lines · Marc Zyngier

ARMv8.2 introduced support for VPIPT i-caches, the V standing for VMID-tagged. Although this looked like a reasonable idea, no implementation has ever made it into the wild.

Linux has supported this for over 6 years (amusingly, just as the architecture was dropping support for AIVIVT i-caches), but we had no way to even test it, and it is likely that this code was just bit-rotting.

[...]

Applied to arm64 (for-next/rip-vpipt), thanks!

[1/3] KVM: arm64: Remove VPIPT I-cache handling

      https://git.kernel.org/arm64/c/ced242ba9d7c
[2/3] arm64: Kill detection of VPIPT i-cache policy
      https://git.kernel.org/arm64/c/d8e12a0d3715
[3/3] arm64: Rename reserved values for CTR_EL0.L1Ip
      https://git.kernel.org/arm64/c/f35c32ca6839

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev