Re: [PATCH] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts
From: Lorenzo Stoakes (ARM)
Date: Thu Nov 26 2026 - 11:53:48 EST
On Thu, Nov 26, 2026 at 04:30:24PM +0000, Lorenzo Stoakes (ARM) wrote:
> I also think actually there's an existing problem with the top-up right now in
> user_mem_abort() that needs addressing too, actually, that this change exposes
> and will cause bugs if done as above.
>
> So right now top-up is conditional there:
>
> if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
> is_protected_kvm_enabled()) {
> ret = topup_mmu_memcache(s2fd->mmu, memcache);
> if (ret)
> return ret;
> }
>
> Inverted is 'permission fault && !dirty-logging && !pKVM'. So the question is
> whether a permission fault not needing a new page table is correct.
>
> So working through, the actual decision about a page table allocation is done
> in:
>
> user_mem_abort()
> -> kvm_s2_fault_map()
> -> kvm_pgtable_stage2_map()
> -> ... -> stage2_map_walk_leaf()
> -> stage2_map_walker_try_leaf()
> -> stage2_leaf_mapping_allowed()
> -> kvm_block_mapping_supported() [ if false, allocate a page table ]
>
> static bool kvm_block_mapping_supported(const struct kvm_pgtable_visit_ctx *ctx, u64 phys)
> {
> u64 granule = kvm_granule_size(ctx->level);
>
> ...
>
> if (granule > (ctx->end - ctx->addr))
> return false;
>
> ...
> }
>
> But page table size can vary in NV between host and guest (guest must be >=
> host) and it gets clamped to the smaller of the two in kvm_s2_resolve_vma_size():
>
> static short kvm_s2_resolve_vma_size(const struct kvm_s2_fault_desc *s2fd,
> struct kvm_s2_fault_vma_info *s2vi,
> struct vm_area_struct *vma)
> {
> ...
> if (s2fd->nested) {
> ...
> /*
> * If we're about to create a shadow stage 2 entry, then we
> * can only create a block mapping if the guest stage 2 page
> * table uses at least as big a mapping.
> */
> max_map_size = min(kvm_s2_trans_size(s2fd->nested), max_map_size);
> ...
> }
> }
>
> This sets s2vi->vma_pagesize based on this value:
>
> static int kvm_s2_fault_get_vma_info(const struct kvm_s2_fault_desc *s2fd,
> struct kvm_s2_fault_vma_info *s2vi)
> {
> ...
> s2vi->vma_pagesize = BIT(kvm_s2_resolve_vma_size(s2fd, s2vi, vma));
> ...
> }
>
> And the fault map uses this in the mapping size, which defines ctx->addr, end
> above in kvm_block_mapping_supported():
>
> static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
> const struct kvm_s2_fault_vma_info *s2vi,
> enum kvm_pgtable_prot prot,
> void *memcache,
> struct kvm_s2_fault_result *result)
> {
> ...
> mapping_size = s2vi->vma_pagesize;
> ...
> ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, gfn_to_gpa(gfn), mapping_size,
> __pfn_to_phys(pfn), prot,
> memcache, flags);
> ...
> }
>
> So basically, if the host has smaller page table size than the guest, you need
> an extra page table level.
>
> So simple solution is to add nested to the check:
>
> if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
> is_protected_kvm_enabled() || s2fd->nested) {
> ret = topup_mmu_memcache(s2fd->mmu, memcache);
> if (ret)
> return ret;
> }
>
> But honestly I wonder at this point if it's truly worth doing all these checks?
> Maybe safer to just topup unconditionally.
>
> It'll only top up once before the cache is full anyway, so it's low cost. And
> would make more consistent with other fault paths too.
Given this is pre-existing, let me send a patch to fix this separately.
--
Cheers, Lorenzo