Skip to contentSkip to conversation

KVM: arm64: Remove redundant check for S2FWB

PATCH In review Jing Zhang 3 participants 5 messages Feb 5, 2021 – Mar 8, 2021

Subsystems (2)
lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

PATCH 1/1 KVM: arm64: Remove redundant check for S2FWB

Remove redundant check for CPU feature S2FWB in dcache flush code to save some CPU cycles for every memslot flush and unmapping. And move the S2FWB check to outer functions to avoid future redundancy and keep consistent with other usage like in access_dcsw and kvm_arch_prepare_memory_region.

---
 arch/arm64/kvm/hyp/pgtable.c | 9 ++-------
 arch/arm64/kvm/mmu.c         | 3 ++-
 2 files changed, 4 insertions(+), 8 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +2 -7
index bdf8e55ed308..afd57564b1cb 100644
@@ -642,9 +642,6 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 
 static void stage2_flush_dcache(void *addr, u64 size)
 {
-	if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
-		return;
-
 	__flush_dcache_area(addr, size);
 }
 
@@ -670,7 +667,8 @@ static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
 
 		if (page_count(virt_to_page(childp)) != 1)
 			return 0;
-	} else if (stage2_pte_cacheable(pte)) {
+	} else if (stage2_pte_cacheable(pte) &&
+			!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
 		need_flush = true;
 	}
 
@@ -846,9 +844,6 @@ int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 		.flags	= KVM_PGTABLE_WALK_LEAF,
 	};
 
-	if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
-		return 0;
-
 	return kvm_pgtable_walk(pgt, addr, size, &walker);
 }
 
arch/arm64/kvm/mmu.c +2 -1
index 7d2257cc5438..53130ed23304 100644
@@ -1458,7 +1458,8 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
 	 * If switching it off, need to clean the caches.
 	 * Clean + invalidate does the trick always.
 	 */
-	if (now_enabled != was_enabled)
+	if (now_enabled != was_enabled &&
+			!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
 		stage2_flush_vm(vcpu->kvm);
 
 	/* Caches are now on, stop trapping VM ops (until a S/W op) */

base-commit: a8ac864a7d6dbc2fc43081b1eecd9e0183065d47

Signature · 6 lines
2.30.0.365.g02bc693789-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
maintainer
#1 lore raw

Hi Jing,

Quoted 2 lines · Jing Zhang

Remove redundant check for CPU feature S2FWB in dcache flush code to save some CPU cycles for every memslot flush and unmapping.

What CPU cycles? This is only a static branch. Can you actually measure the overhead? What does it represent in the face of a full memslot unmapping?

Thanks,

        M.
Signature · 5 lines
Jazz is not dead. It just smells funny...
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
author
#2 lore raw

Hi Marc,

Thanks for the comment.

Quoted 10 lines · Marc Zyngier

Hi Jing,

Quoted 2 lines · Jing Zhang

Remove redundant check for CPU feature S2FWB in dcache flush code to save some CPU cycles for every memslot flush and unmapping.

What CPU cycles? This is only a static branch. Can you actually measure the overhead? What does it represent in the face of a full memslot unmapping?

For CPU cycles, I mean CPU time spent for S2FWB check.
For memslot unmapping, there is actually no improvement, just move the check to the stage2_unmap_walker since we removed the S2FWB check in stage2_flush_dcache.
The saving is from the code path of memslot flush. The S2FWB check was in stage2_flush_dcache, in which case, for a memslot flush, the check was done for every page. Now it will save some CPU time if we do the check at a higher level, like in kvm_toggle_cache, access_dcsw, kvm_arch_prepare_memory_region.
The redundant check is as follows (Only the first check is necessary):
kvm_arch_prepare_memory_region -> S2FWB check -> stage2_flush_memslot -> kvm_pgtable_stage2_flush -> S2FWB check -> stage2_flush_walker -> S2FWB check -> __flush_dcache_area

Quoted 6 lines

Thanks,

         M.

--
Jazz is not dead. It just smells funny...

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#3 lore raw
In reply to Jing Zhang
Quoted 37 lines · Jing Zhang

Remove redundant check for CPU feature S2FWB in dcache flush code to save some CPU cycles for every memslot flush and unmapping. And move the S2FWB check to outer functions to avoid future redundancy and keep consistent with other usage like in access_dcsw and kvm_arch_prepare_memory_region.

---
 arch/arm64/kvm/hyp/pgtable.c | 9 ++-------
 arch/arm64/kvm/mmu.c         | 3 ++-
 2 files changed, 4 insertions(+), 8 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +2 -4
index bdf8e55ed308..afd57564b1cb 100644
@@ -642,9 +642,6 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 
 static void stage2_flush_dcache(void *addr, u64 size)
 {
-	if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
-		return;
-
 	__flush_dcache_area(addr, size);
 }
 
@@ -670,7 +667,8 @@ static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
 
 		if (page_count(virt_to_page(childp)) != 1)
 			return 0;
-	} else if (stage2_pte_cacheable(pte)) {
+	} else if (stage2_pte_cacheable(pte) &&
+			!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
 		need_flush = true;
 	}
 

arch/arm64/kvm/hyp/pgtable.c int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size) · line 848

Quoted 8 lines
arch/arm64/kvm/hyp/pgtable.c +0 -3
@@ -846,9 +844,6 @@ int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 		.flags	= KVM_PGTABLE_WALK_LEAF,
 	};
 
-	if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
-		return 0;
-
 	return kvm_pgtable_walk(pgt, addr, size, &walker);
 }

I think we should leave pgtable.c as it is: there's no benefit from this change on the unmap path, and the other path involves the case where the caller has asked for a flush and we can elide it.

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 1463

Quoted 12 lines
arch/arm64/kvm/mmu.c +2 -1
index 7d2257cc5438..53130ed23304 100644
@@ -1458,7 +1458,8 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
 	 * If switching it off, need to clean the caches.
 	 * Clean + invalidate does the trick always.
 	 */
-	if (now_enabled != was_enabled)
+	if (now_enabled != was_enabled &&
+			!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
 		stage2_flush_vm(vcpu->kvm);

This change looks fine, but I don't grok the justification in your follow-up email. You say:

  | The saving is from the code path of memslot flush. The S2FWB check was
  | in stage2_flush_dcache, in which case, for a memslot flush, the check
  | was done for every page.

but I don't see where this is called for every page. It looks to me like it's called for every pgd in the range, which is a very different kettle of frogs.

What am I missing?

Will
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author
#4 lore raw

Hi Will,

Quoted 42 lines · Will Deacon
Quoted 40 lines · Jing Zhang

Remove redundant check for CPU feature S2FWB in dcache flush code to save some CPU cycles for every memslot flush and unmapping. And move the S2FWB check to outer functions to avoid future redundancy and keep consistent with other usage like in access_dcsw and kvm_arch_prepare_memory_region.

---
 arch/arm64/kvm/hyp/pgtable.c | 9 ++-------
 arch/arm64/kvm/mmu.c         | 3 ++-
 2 files changed, 4 insertions(+), 8 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +2 -4
index bdf8e55ed308..afd57564b1cb 100644
@@ -642,9 +642,6 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 
 static void stage2_flush_dcache(void *addr, u64 size)
 {
-     if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
-             return;
-
      __flush_dcache_area(addr, size);
 }
 
@@ -670,7 +667,8 @@ static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
 
              if (page_count(virt_to_page(childp)) != 1)
                      return 0;
-     } else if (stage2_pte_cacheable(pte)) {
+     } else if (stage2_pte_cacheable(pte) &&
+                     !cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
              need_flush = true;
      }
 
@@ -846,9 +844,6 @@ int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
              .flags  = KVM_PGTABLE_WALK_LEAF,
      };
 
Quoted 9 lines

arch/arm64/kvm/hyp/pgtable.c int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size) · line 848

Quoted 5 lines
arch/arm64/kvm/hyp/pgtable.c +0 -3
-     if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
-             return 0;
-
      return kvm_pgtable_walk(pgt, addr, size, &walker);
 }

I think we should leave pgtable.c as it is: there's no benefit from this change on the unmap path, and the other path involves the case where the caller has asked for a flush and we can elide it.

Agreed.

Quoted 17 lines

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 1463

Quoted 13 lines
arch/arm64/kvm/mmu.c +2 -1
index 7d2257cc5438..53130ed23304 100644
@@ -1458,7 +1458,8 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
       * If switching it off, need to clean the caches.
       * Clean + invalidate does the trick always.
       */
-     if (now_enabled != was_enabled)
+     if (now_enabled != was_enabled &&
+                     !cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
              stage2_flush_vm(vcpu->kvm);

This change looks fine, but I don't grok the justification in your follow-up email. You say:

Quoted 8 lines
  | The saving is from the code path of memslot flush. The S2FWB check was
  | in stage2_flush_dcache, in which case, for a memslot flush, the check
  | was done for every page.

but I don't see where this is called for every page. It looks to me like it's called for every pgd in the range, which is a very different kettle of frogs.

What am I missing?

You are right. It is called for every pgd in the range instead of for every page.

Quoted 2 lines

Will

Thanks,
Jing
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm