[RFC PATCH 2/2] KVM: arm64: Improve splitting performance by using SKIP return values
Leonardo Bras
leo.bras at arm.com
Fri May 15 12:59:03 PDT 2026
Splitting an S2 pagetable is needed when using dirty-bit tracking.
Currently, when splitting, all the child and sibling nodes will be walked,
with the walker just returning earlier if there is nothing to do. This
means all pagetable entries in the splitting range get a callback from the
walker function, even if it was just split, or it's a level-3 entry.
Optimize splitting in two cases:
- If a level-3 entry is walked, it means the parent level-2 entry is split,
so avoid walking all level-3 siblings.
- If a split just succeeded in an table entry, it means all children nodes
are already split, so skip walking this entry's children.
Optimization measured on a 1GB VM, running on the model, splitting all at
the beginning (no manual protect):
- Memory was already split (4k pages): -97.33% runtime (-172ms) - 20 runs
- THP backed memory: -19.82% runtime (-153ms) - 10 runs
- 1x1GB hugetlb memory: -20.65% runtime (-150ms) - 10 runs
(Above runtime is measured on kvm_mmu_split_huge_pages(), using
ktime_get_real_ns() before and after function call)
Signed-off-by: Leonardo Bras <leo.bras at arm.com>
---
arch/arm64/kvm/hyp/pgtable.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index 4e43339522bb..164c5bcd6026 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -1502,23 +1502,27 @@ static int stage2_split_walker(const struct kvm_pgtable_visit_ctx *ctx,
struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
struct kvm_mmu_memory_cache *mc = ctx->arg;
struct kvm_s2_mmu *mmu;
kvm_pte_t pte = ctx->old, new, *childp;
enum kvm_pgtable_prot prot;
s8 level = ctx->level;
bool force_pte;
int nr_pages;
u64 phys;
- /* No huge-pages exist at the last level */
+ /*
+ * No huge-pages exist at the last level
+ * Also, if one PTE exist in the last level, the whole block is already
+ * split, so skip walking it's siblings.
+ */
if (level == KVM_PGTABLE_LAST_LEVEL)
- return 0;
+ return SKIP_SIBLINGS;
/* We only split valid block mappings */
if (!kvm_pte_valid(pte))
return 0;
nr_pages = stage2_block_get_nr_page_tables(level);
if (nr_pages < 0)
return nr_pages;
if (mc->nobjs >= nr_pages) {
@@ -1554,21 +1558,23 @@ static int stage2_split_walker(const struct kvm_pgtable_visit_ctx *ctx,
return -EAGAIN;
}
/*
* Note, the contents of the page table are guaranteed to be made
* visible before the new PTE is assigned because stage2_make_pte()
* writes the PTE using smp_store_release().
*/
new = kvm_init_table_pte(childp, mm_ops);
stage2_make_pte(ctx, new);
- return 0;
+
+ /* All child entries are already split, so skip walking them */
+ return SKIP_CHILDREN;
}
int kvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size,
struct kvm_mmu_memory_cache *mc)
{
struct kvm_pgtable_walker walker = {
.cb = stage2_split_walker,
.flags = KVM_PGTABLE_WALK_LEAF,
.arg = mc,
};
--
2.54.0
More information about the linux-arm-kernel
mailing list