Re: [PATCH v2] KVM: arm64: Unconditionally top up the mmu memcache on user fault
From: Usama Arif
Date: Fri Dec 04 2026 - 09:22:36 EST
On 03/12/2026 10:45, Lorenzo Stoakes (ARM) wrote:
> The S2 user fault path tops up the MMU memcache (used for page table
> pre-allocation as the point at which page tables are installed may not
> allocate) except when:
>
> * It's a permission fault AND
> * dirty page logging is disabled AND
> * pKVM is disabled
>
> At which point it is assumed no page table will be allocated.
>
> However this is not safe - if the mapping size differs from that at which
> the permission fault was taken, we must install a new mapping, which may
> require a page table. This can happen when:
>
> * A guest hypervisor replaces a block in its own S2 with a table without
> issuing a TLBI (likely due to a bug) - the shadow S2 mapping size is
> clamped to that of the guest's S2, so the shadow block has to be
> collapsed.
>
> * A guest hypervisor does issue the TLBI, but races with another vCPU
> which has already taken a permission fault on the shadow block.
>
> * Nested virtualisation isn't in use at all, but the VMM replaces a THP
> with 64K hugetlb pages while a permission fault on it is in flight.
>
> With the memcache empty, kvm_mmu_memory_cache_alloc() hits its
> guest-triggerable WARN_ON(!nobjs) and falls back to a GFP_ATOMIC allocation
> under mmu_lock, with a BUG_ON() if that fails.
>
> Therefore, top up the memcache unconditionally. This is cheap, as the top
> up does nothing if the cache already holds enough objects.
>
> Note that dirty logging had a similar issue, addressed in
> commit 56fd1fc526e6 ("KVM: arm64: Top up stage-2 memcache for dirty
> logging faults").
>
> Local testing with dirty logging disabled indicated no performance
> impact.
>
> kvm_page_table_test was modified to take 65,536 permission faults with
> dirty logging disabled, and run on an Apple M2 with the kernel under test
> running as an NV guest. The median time across ~70 runs was 1.059s without
> this patch and 1.062s with it.
>
> A control stage in the same runs, whose code this patch does not touch,
> showed a similar level of variation so this is clearly within noise levels.
>
> Fixes: 7d894834a305 ("KVM: arm64: Add usage of stage 2 fault lookup level in user_mem_abort()")
> Fixes: fd276e71d1e7 ("KVM: arm64: nv: Handle shadow stage 2 page faults")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> Reviewed-by: Leonardo Bras <leo.bras@xxxxxxx>
> ---
> v2:
> * Switched to always topping up as per Usama.
> * Got performance numbers as per Leo.
> * Tweaked commit message to be more succinct.
> * Retitled the patch as it is no longer NV-specific.
> * Added a Fixes tag for the non-NV case, which predates NV support.
>
> v1:
> https://lore.kernel.org/r/20261126-fix-nv-cache-topup-v1-1-a22c936872fe@xxxxxxxxxx
> ---
> arch/arm64/kvm/mmu.c | 19 +++----------------
> 1 file changed, 3 insertions(+), 16 deletions(-)
>
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index a0d4981ebb99..4ef32bedb327 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -2248,28 +2248,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
> static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
> struct kvm_s2_fault_result *result)
> {
> - bool perm_fault = kvm_s2_fault_is_perm(s2fd);
> struct kvm_s2_fault_vma_info s2vi = {};
> enum kvm_pgtable_prot prot;
> void *memcache;
> int ret;
>
> - /*
> - * Permission faults just need to update the existing leaf entry,
> - * and so normally don't require allocations from the memcache. The
> - * only exception to this is when dirty logging is enabled at runtime
> - * and a fault needs to collapse a block entry into a table.
> - * Under pKVM a permission fault can also collapse pages into a block,
> - * which needs a fresh mapping object, and the hypervisor requires the
> - * min-pages memcache even when the install allocates nothing.
> - */
> memcache = get_mmu_memcache(s2fd->vcpu);
> - if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
> - is_protected_kvm_enabled()) {
> - ret = topup_mmu_memcache(s2fd->mmu, memcache);
> - if (ret)
> - return ret;
> - }
> + ret = topup_mmu_memcache(s2fd->mmu, memcache);
> + if (ret)
> + return ret;
>
LGTM! Thanks for the fix!
Acked-by: Usama Arif <usama.arif@xxxxxxxxx>
> ret = kvm_s2_fault_pin_pfn(s2fd, &s2vi);
> if (ret == -EHWPOISON) {
>
> ---
> base-commit: 012e4bc01e339a65dc7c59181b5efb33b171d12c
> change-id: 20261126-fix-nv-cache-topup-9ab1fd829014
>
> Cheers,