[PATCH] KVM: arm64: Shrink the stage-2 memcache to the page-table depth
From: Usama Arif
Date: Wed Nov 25 2026 - 15:55:15 EST
When a stage-2 fault may need new page tables, KVM first tops up the
vCPU's memcache: if it holds fewer pages than the fault might need, it
is refilled to KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, which arm64 sets to
40. Pages never return to the cache, and unused ones stay there until
the vCPU is destroyed.
The cache only has to cover a single map: every fault that can allocate
tables tops it up first, and a map allocates at most one table per
level below the preallocated root (kvm_mmu_cache_min_pages()). That is
at most three pages with 4 KiB and 16 KiB pages, and two with 64 KiB
pages.
With 64 KiB pages, the 40-page cache is 2.5 MiB per vCPU. As every
last-level table covers 512 MiB, an 8 GiB guest needs just 17 table
pages to map all of its memory, while its four vCPU caches get filled
with 160. On a production host with 64 KiB pages running 101 VMs with
four vCPUs each, the caches were still 86% full. The memory used:
before after
per-vCPU capacity 2.5 MiB 256 KiB
all 404 vCPUs 869 MiB <= 101 MiB
Let's size the cache to KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL,
which is 4 pages and covers the deepest walk with one page to spare. On
the host above, it frees at least 768 MiB. Refills get more frequent,
but no more than four pages per vCPU ever sit unused. With 4 KiB pages,
the cache shrinks from 160 KiB to 16 KiB per vCPU.
Move the two level definitions to asm/kvm_types.h, so that the generic
top-up code in kvm_main.c can see them. kvm_pgtable.h still gets them
through linux/kvm_types.h.
The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
but it is topped up before every page it maps. The pKVM hypervisor
memcache and the eager split cache don't use this capacity.
Suggested-by: Leonardo Bras <leo.bras@xxxxxxx>
Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
---
arch/arm64/include/asm/kvm_pgtable.h | 3 ---
arch/arm64/include/asm/kvm_types.h | 7 ++++++-
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h
index eda440354a55ec..691e2eb63939f5 100644
--- a/arch/arm64/include/asm/kvm_pgtable.h
+++ b/arch/arm64/include/asm/kvm_pgtable.h
@@ -19,9 +19,6 @@
struct kvm_s2_mmu;
-#define KVM_PGTABLE_FIRST_LEVEL -1
-#define KVM_PGTABLE_LAST_LEVEL 3
-
/*
* The largest supported block sizes for KVM (no 52-bit PA support):
* - 4K (level 1): 1GB
diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
index 9a126b9e2d7c99..5b61285d317e28 100644
--- a/arch/arm64/include/asm/kvm_types.h
+++ b/arch/arm64/include/asm/kvm_types.h
@@ -2,7 +2,12 @@
#ifndef _ASM_ARM64_KVM_TYPES_H
#define _ASM_ARM64_KVM_TYPES_H
-#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
+#define KVM_PGTABLE_FIRST_LEVEL -1
+#define KVM_PGTABLE_LAST_LEVEL 3
+
+/* One page per level below the root covers kvm_mmu_cache_min_pages(). */
+#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE \
+ (KVM_PGTABLE_LAST_LEVEL - KVM_PGTABLE_FIRST_LEVEL)
#endif /* _ASM_ARM64_KVM_TYPES_H */
--
2.53.1-Meta
> Thanks!
> Leo