Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts

From: Leonardo Bras

Date: Wed Nov 25 2026 - 12:15:10 EST


On Tue, Nov 24, 2026 at 07:28:42AM -0800, Usama Arif wrote:
> When a stage-2 fault may need new page tables, KVM first tops up the
> vCPU's page cache: 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. Unused pages stay in the cache until the vCPU is destroyed.
>
> With 64 KiB pages, that is 2.5 MiB per vCPU, and most of it is never
> used. Stage-2 has at most three levels and the root is preallocated, so
> a fault needs at most two new table pages (kvm_mmu_cache_min_pages()).
> 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 MB 512 KB
> all 404 vCPUs 869 MB <= 202 MB
>
> Let's lower the capacity to 8 with 64 KiB pages, In the production
> usecase above, it frees at least 667 MB. That still covers the two-page
> minimum and leaves room for several table allocations per refill. 4 KB
> and 16 KB pages keep 40.
>
> The temporary cache in kvm_phys_addr_ioremap() uses the same capacity,
> but it is topped up before every page it maps, so 8 is sufficient there
> as well. The pKVM hypervisor memcache and the eager split cache don't
> use this capacity.
>
> Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
> ---
> arch/arm64/include/asm/kvm_types.h | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h
> index 9a126b9e2d7c99..28568da9ea4a7b 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
>
> +/* Must cover kvm_mmu_cache_min_pages(), at most 2 with 64 KiB pages. */
> +#ifdef CONFIG_ARM64_64K_PAGES
> +#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 8
> +#else
> #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
> +#endif
>
> #endif /* _ASM_ARM64_KVM_TYPES_H */
>
> --
> 2.53.1-Meta
>

Hi Usama,

Have you tried to reproduce this issue in v7.4-rc1 or newer?

IIUC Lorenzo Stoakes introduced a change that would cause the memcache to
skip topup if the cache contains a minimum of ~4 pages, so it can possibly
improve that issue.

Although I can see that when it's below that minimum it fills the whole
capacity (40) of the cache.

In any case, Lorenzo, could you advise?

CC:Lorenzo Stoakes (ARM) (ljs@xxxxxxxxxx)

Thanks,
Leo