Skip to contentSkip to conversation

KVM: arm64: Address soft lockups due to I-cache CMOs

PATCH Applied Oliver Upton 4 participants 8 messages Sep 20, 2023 – Sep 22, 2023

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

cover KVM: arm64: Address soft lockups due to I-cache CMOs

Small series to address the soft lockups that Gavin hits when running KVM guests w/ hugepages on an Ampere Altra Max machine. While I absolutely loathe "fixing" the issue of slow I-cache CMOs in this way, I can't really think of an alternative.

Oliver Upton (2):
arm64: tlbflush: Rename MAX_TLBI_OPS
KVM: arm64: Avoid soft lockups due to I-cache maintenance

 arch/arm64/include/asm/kvm_mmu.h  | 37 ++++++++++++++++++++++++++-----
 arch/arm64/include/asm/tlbflush.h |  8 +++----
 2 files changed, 35 insertions(+), 10 deletions(-)

base-commit: ce9ecca0238b140b88f43859b211c9fdfd8e5b70

Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#1 lore raw

PATCH 1/2 arm64: tlbflush: Rename MAX_TLBI_OPS

Perhaps unsurprisingly, I-cache invalidations suffer from performance issues similar to TLB invalidations on certain systems. TLB and I-cache maintenance all result in DVM on the mesh, which is where the real bottleneck lies.

Rename the heuristic to point the finger at DVM, such that it may be reused for limiting I-cache invalidations.

---
 arch/arm64/include/asm/tlbflush.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/tlbflush.h +4 -4
index b149cf9f91bc..3431d37e5054 100644
@@ -333,7 +333,7 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
  * This is meant to avoid soft lock-ups on large TLB flushing ranges and not
  * necessarily a performance improvement.
  */
-#define MAX_TLBI_OPS	PTRS_PER_PTE
+#define MAX_DVM_OPS	PTRS_PER_PTE
 
 /*
  * __flush_tlb_range_op - Perform TLBI operation upon a range
@@ -413,12 +413,12 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma,
 
 	/*
 	 * When not uses TLB range ops, we can handle up to
-	 * (MAX_TLBI_OPS - 1) pages;
+	 * (MAX_DVM_OPS - 1) pages;
 	 * When uses TLB range ops, we can handle up to
 	 * (MAX_TLBI_RANGE_PAGES - 1) pages.
 	 */
 	if ((!system_supports_tlb_range() &&
-	     (end - start) >= (MAX_TLBI_OPS * stride)) ||
+	     (end - start) >= (MAX_DVM_OPS * stride)) ||
 	    pages >= MAX_TLBI_RANGE_PAGES) {
 		flush_tlb_mm(vma->vm_mm);
 		return;
@@ -451,7 +451,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end
 {
 	unsigned long addr;
 
-	if ((end - start) > (MAX_TLBI_OPS * PAGE_SIZE)) {
+	if ((end - start) > (MAX_DVM_OPS * PAGE_SIZE)) {
 		flush_tlb_all();
 		return;
 	}
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by Tested-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] arm64: tlbflush: Rename MAX_TLBI_OPS

Quoted 5 lines · Oliver Upton

Perhaps unsurprisingly, I-cache invalidations suffer from performance issues similar to TLB invalidations on certain systems. TLB and I-cache maintenance all result in DVM on the mesh, which is where the real bottleneck lies.

Quoted 8 lines

Rename the heuristic to point the finger at DVM, such that it may be reused for limiting I-cache invalidations.

---
  arch/arm64/include/asm/tlbflush.h | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 37 lines
arch/arm64/include/asm/tlbflush.h +4 -4
index b149cf9f91bc..3431d37e5054 100644
@@ -333,7 +333,7 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
   * This is meant to avoid soft lock-ups on large TLB flushing ranges and not
   * necessarily a performance improvement.
   */
-#define MAX_TLBI_OPS	PTRS_PER_PTE
+#define MAX_DVM_OPS	PTRS_PER_PTE
 
  /*
   * __flush_tlb_range_op - Perform TLBI operation upon a range
@@ -413,12 +413,12 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma,
 
  	/*
  	 * When not uses TLB range ops, we can handle up to
-	 * (MAX_TLBI_OPS - 1) pages;
+	 * (MAX_DVM_OPS - 1) pages;
  	 * When uses TLB range ops, we can handle up to
  	 * (MAX_TLBI_RANGE_PAGES - 1) pages.
  	 */
  	if ((!system_supports_tlb_range() &&
-	     (end - start) >= (MAX_TLBI_OPS * stride)) ||
+	     (end - start) >= (MAX_DVM_OPS * stride)) ||
  	    pages >= MAX_TLBI_RANGE_PAGES) {
  		flush_tlb_mm(vma->vm_mm);
  		return;
@@ -451,7 +451,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end
  {
  	unsigned long addr;
 
-	if ((end - start) > (MAX_TLBI_OPS * PAGE_SIZE)) {
+	if ((end - start) > (MAX_DVM_OPS * PAGE_SIZE)) {
  		flush_tlb_all();
  		return;
  	}
maintainer Acked-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] arm64: tlbflush: Rename MAX_TLBI_OPS

In reply to Oliver Upton
Quoted 52 lines · Oliver Upton

Perhaps unsurprisingly, I-cache invalidations suffer from performance issues similar to TLB invalidations on certain systems. TLB and I-cache maintenance all result in DVM on the mesh, which is where the real bottleneck lies.

Rename the heuristic to point the finger at DVM, such that it may be reused for limiting I-cache invalidations.

---
 arch/arm64/include/asm/tlbflush.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/tlbflush.h +4 -4
index b149cf9f91bc..3431d37e5054 100644
@@ -333,7 +333,7 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
  * This is meant to avoid soft lock-ups on large TLB flushing ranges and not
  * necessarily a performance improvement.
  */
-#define MAX_TLBI_OPS	PTRS_PER_PTE
+#define MAX_DVM_OPS	PTRS_PER_PTE
 
 /*
  * __flush_tlb_range_op - Perform TLBI operation upon a range
@@ -413,12 +413,12 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma,
 
 	/*
 	 * When not uses TLB range ops, we can handle up to
-	 * (MAX_TLBI_OPS - 1) pages;
+	 * (MAX_DVM_OPS - 1) pages;
 	 * When uses TLB range ops, we can handle up to
 	 * (MAX_TLBI_RANGE_PAGES - 1) pages.
 	 */
 	if ((!system_supports_tlb_range() &&
-	     (end - start) >= (MAX_TLBI_OPS * stride)) ||
+	     (end - start) >= (MAX_DVM_OPS * stride)) ||
 	    pages >= MAX_TLBI_RANGE_PAGES) {
 		flush_tlb_mm(vma->vm_mm);
 		return;
@@ -451,7 +451,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end
 {
 	unsigned long addr;
 
-	if ((end - start) > (MAX_TLBI_OPS * PAGE_SIZE)) {
+	if ((end - start) > (MAX_DVM_OPS * PAGE_SIZE)) {
 		flush_tlb_all();
 		return;
 	}
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog

Will

#4 lore raw

PATCH 2/2 KVM: arm64: Avoid soft lockups due to I-cache maintenance

In reply to Oliver Upton

Gavin reports of soft lockups on his Ampere Altra Max machine when backing KVM guests with hugetlb pages. Upon further investigation, it was found that the system is unable to keep up with parallel I-cache invalidations done by KVM's stage-2 fault handler.

This is ultimately an implementation problem. I-cache maintenance instructions are available at EL0, so nothing stops a malicious userspace from hammering a system with CMOs and cause it to fall over. "Fixing" this problem in KVM is nothing more than slapping a bandage over a much deeper problem.

Anyway, the kernel already has a heuristic for limiting TLB invalidations to avoid soft lockups. Reuse that logic to limit I-cache CMOs done by KVM to map executable pages on systems without FEAT_DIC. While at it, restructure __invalidate_icache_guest_page() to improve readability and squeeze our new condition into the existing branching structure.

---
 arch/arm64/include/asm/kvm_mmu.h | 37 ++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +31 -6
index 96a80e8f6226..a425ecdd7be0 100644
@@ -224,16 +224,41 @@ static inline void __clean_dcache_guest_page(void *va, size_t size)
 	kvm_flush_dcache_to_poc(va, size);
 }
 
+static inline size_t __invalidate_icache_max_range(void)
+{
+	u8 iminline;
+	u64 ctr;
+
+	asm volatile(ALTERNATIVE_CB("movz %0, #0\n"
+				    "movk %0, #0, lsl #16\n"
+				    "movk %0, #0, lsl #32\n"
+				    "movk %0, #0, lsl #48\n",
+				    ARM64_ALWAYS_SYSTEM,
+				    kvm_compute_final_ctr_el0)
+		     : "=r" (ctr));
+
+	iminline = SYS_FIELD_GET(CTR_EL0, IminLine, ctr) + 2;
+	return MAX_DVM_OPS << iminline;
+}
+
 static inline void __invalidate_icache_guest_page(void *va, size_t size)
 {
-	if (icache_is_aliasing()) {
-		/* any kind of VIPT cache */
+	/*
+	 * 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
+	 * cache line.
+	 */
+	if (icache_is_aliasing() || size > __invalidate_icache_max_range())
 		icache_inval_all_pou();
-	} else if (read_sysreg(CurrentEL) != CurrentEL_EL1 ||
-		   !icache_is_vpipt()) {
-		/* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */
+	else
 		icache_inval_pou((unsigned long)va, (unsigned long)va + size);
-	}
 }
 
 void kvm_set_way_flush(struct kvm_vcpu *vcpu);
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by Tested-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] KVM: arm64: Avoid soft lockups due to I-cache maintenance

Quoted 18 lines · Oliver Upton

Gavin reports of soft lockups on his Ampere Altra Max machine when backing KVM guests with hugetlb pages. Upon further investigation, it was found that the system is unable to keep up with parallel I-cache invalidations done by KVM's stage-2 fault handler.

This is ultimately an implementation problem. I-cache maintenance instructions are available at EL0, so nothing stops a malicious userspace from hammering a system with CMOs and cause it to fall over. "Fixing" this problem in KVM is nothing more than slapping a bandage over a much deeper problem.

Anyway, the kernel already has a heuristic for limiting TLB invalidations to avoid soft lockups. Reuse that logic to limit I-cache CMOs done by KVM to map executable pages on systems without FEAT_DIC. While at it, restructure __invalidate_icache_guest_page() to improve readability and squeeze our new condition into the existing branching structure.

Quoted 6 lines
---
  arch/arm64/include/asm/kvm_mmu.h | 37 ++++++++++++++++++++++++++------
  1 file changed, 31 insertions(+), 6 deletions(-)
Quoted 52 lines
arch/arm64/include/asm/kvm_mmu.h +31 -6
index 96a80e8f6226..a425ecdd7be0 100644
@@ -224,16 +224,41 @@ static inline void __clean_dcache_guest_page(void *va, size_t size)
  	kvm_flush_dcache_to_poc(va, size);
  }
 
+static inline size_t __invalidate_icache_max_range(void)
+{
+	u8 iminline;
+	u64 ctr;
+
+	asm volatile(ALTERNATIVE_CB("movz %0, #0\n"
+				    "movk %0, #0, lsl #16\n"
+				    "movk %0, #0, lsl #32\n"
+				    "movk %0, #0, lsl #48\n",
+				    ARM64_ALWAYS_SYSTEM,
+				    kvm_compute_final_ctr_el0)
+		     : "=r" (ctr));
+
+	iminline = SYS_FIELD_GET(CTR_EL0, IminLine, ctr) + 2;
+	return MAX_DVM_OPS << iminline;
+}
+
  static inline void __invalidate_icache_guest_page(void *va, size_t size)
  {
-	if (icache_is_aliasing()) {
-		/* any kind of VIPT cache */
+	/*
+	 * 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
+	 * cache line.
+	 */
+	if (icache_is_aliasing() || size > __invalidate_icache_max_range())
  		icache_inval_all_pou();
-	} else if (read_sysreg(CurrentEL) != CurrentEL_EL1 ||
-		   !icache_is_vpipt()) {
-		/* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */
+	else
  		icache_inval_pou((unsigned long)va, (unsigned long)va + size);
-	}
  }
 
  void kvm_set_way_flush(struct kvm_vcpu *vcpu);
maintainer Reviewed-by on the v1 series
#6 lore raw
In reply to Oliver Upton
Quoted 5 lines · Oliver Upton

Small series to address the soft lockups that Gavin hits when running KVM guests w/ hugepages on an Ampere Altra Max machine. While I absolutely loathe "fixing" the issue of slow I-cache CMOs in this way, I can't really think of an alternative.

I don't think there is any, unfortunately. I don't think this change is inherently bad (we should have added something like this a long while ago), but it scares me that these systems can apparently be DoS'd from userspace or a guest...

Anyway:

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author Applied
#7 lore raw
In reply to Oliver Upton
Quoted 10 lines · Oliver Upton

Small series to address the soft lockups that Gavin hits when running KVM guests w/ hugepages on an Ampere Altra Max machine. While I absolutely loathe "fixing" the issue of slow I-cache CMOs in this way, I can't really think of an alternative.

Oliver Upton (2):
arm64: tlbflush: Rename MAX_TLBI_OPS
KVM: arm64: Avoid soft lockups due to I-cache maintenance

[...]

Applied to kvmarm/next, thanks!

[1/2] arm64: tlbflush: Rename MAX_TLBI_OPS

      https://git.kernel.org/kvmarm/kvmarm/c/ec1c3b9ff160
[2/2] KVM: arm64: Avoid soft lockups due to I-cache maintenance
      https://git.kernel.org/kvmarm/kvmarm/c/909b583f81b5

--
Best,
Oliver