Skip to content

Commit 7657ea9

Browse files
rananta468Marc Zyngier
authored andcommitted
KVM: arm64: Use TLBI range-based instructions for unmap
The current implementation of the stage-2 unmap walker traverses the given range and, as a part of break-before-make, performs TLB invalidations with a DSB for every PTE. A multitude of this combination could cause a performance bottleneck on some systems. Hence, if the system supports FEAT_TLBIRANGE, defer the TLB invalidations until the entire walk is finished, and then use range-based instructions to invalidate the TLBs in one go. Condition deferred TLB invalidation on the system supporting FWB, as the optimization is entirely pointless when the unmap walker needs to perform CMOs. Rename stage2_put_pte() to stage2_unmap_put_pte() as the function now serves the stage-2 unmap walker specifically, rather than acting generic. Signed-off-by: Raghavendra Rao Ananta <rananta@google.com> Reviewed-by: Shaoqin Huang <shahuang@redhat.com> Signed-off-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20230811045127.3308641-15-rananta@google.com
1 parent defc8cc commit 7657ea9

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

arch/arm64/kvm/hyp/pgtable.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,16 +831,36 @@ static void stage2_make_pte(const struct kvm_pgtable_visit_ctx *ctx, kvm_pte_t n
831831
smp_store_release(ctx->ptep, new);
832832
}
833833

834-
static void stage2_put_pte(const struct kvm_pgtable_visit_ctx *ctx, struct kvm_s2_mmu *mmu,
835-
struct kvm_pgtable_mm_ops *mm_ops)
834+
static bool stage2_unmap_defer_tlb_flush(struct kvm_pgtable *pgt)
836835
{
837836
/*
838-
* Clear the existing PTE, and perform break-before-make with
839-
* TLB maintenance if it was valid.
837+
* If FEAT_TLBIRANGE is implemented, defer the individual
838+
* TLB invalidations until the entire walk is finished, and
839+
* then use the range-based TLBI instructions to do the
840+
* invalidations. Condition deferred TLB invalidation on the
841+
* system supporting FWB as the optimization is entirely
842+
* pointless when the unmap walker needs to perform CMOs.
843+
*/
844+
return system_supports_tlb_range() && stage2_has_fwb(pgt);
845+
}
846+
847+
static void stage2_unmap_put_pte(const struct kvm_pgtable_visit_ctx *ctx,
848+
struct kvm_s2_mmu *mmu,
849+
struct kvm_pgtable_mm_ops *mm_ops)
850+
{
851+
struct kvm_pgtable *pgt = ctx->arg;
852+
853+
/*
854+
* Clear the existing PTE, and perform break-before-make if it was
855+
* valid. Depending on the system support, defer the TLB maintenance
856+
* for the same until the entire unmap walk is completed.
840857
*/
841858
if (kvm_pte_valid(ctx->old)) {
842859
kvm_clear_pte(ctx->ptep);
843-
kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ctx->addr, ctx->level);
860+
861+
if (!stage2_unmap_defer_tlb_flush(pgt))
862+
kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu,
863+
ctx->addr, ctx->level);
844864
}
845865

846866
mm_ops->put_page(ctx->ptep);
@@ -1098,7 +1118,7 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,
10981118
* block entry and rely on the remaining portions being faulted
10991119
* back lazily.
11001120
*/
1101-
stage2_put_pte(ctx, mmu, mm_ops);
1121+
stage2_unmap_put_pte(ctx, mmu, mm_ops);
11021122

11031123
if (need_flush && mm_ops->dcache_clean_inval_poc)
11041124
mm_ops->dcache_clean_inval_poc(kvm_pte_follow(ctx->old, mm_ops),
@@ -1112,13 +1132,19 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,
11121132

11131133
int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
11141134
{
1135+
int ret;
11151136
struct kvm_pgtable_walker walker = {
11161137
.cb = stage2_unmap_walker,
11171138
.arg = pgt,
11181139
.flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
11191140
};
11201141

1121-
return kvm_pgtable_walk(pgt, addr, size, &walker);
1142+
ret = kvm_pgtable_walk(pgt, addr, size, &walker);
1143+
if (stage2_unmap_defer_tlb_flush(pgt))
1144+
/* Perform the deferred TLB invalidations */
1145+
kvm_tlb_flush_vmid_range(pgt->mmu, addr, size);
1146+
1147+
return ret;
11221148
}
11231149

11241150
struct stage2_attr_data {

0 commit comments

Comments
 (0)