From: Usama Arif <usama.arif@linux.dev>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
Fuad Tabba <fuad.tabba@linux.dev>,
Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Christoffer Dall <christoffer.dall@linaro.org>,
Jintack Lim <jintack.lim@linaro.org>,
Leonardo Bras <leo.bras@arm.com>,
Wei-Lin Chang <weilin.chang@arm.com>,
Yanan Wang <wangyanan55@huawei.com>
Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v2] KVM: arm64: Unconditionally top up the mmu memcache on user fault
Date: Fri, 4 Dec 2026 14:22:26 +0000 [thread overview]
Message-ID: <0220d594-041c-41f7-b8ba-84cfaffda1af@linux.dev> (raw)
In-Reply-To: <20261203-fix-nv-cache-topup-v2-1-afc8fc1f87a2@kernel.org>
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@vger.kernel.org
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Reviewed-by: Leonardo Bras <leo.bras@arm.com>
> ---
> 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@kernel.org
> ---
> 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@linux.dev>
> ret = kvm_s2_fault_pin_pfn(s2fd, &s2vi);
> if (ret == -EHWPOISON) {
>
> ---
> base-commit: 012e4bc01e339a65dc7c59181b5efb33b171d12c
> change-id: 20261126-fix-nv-cache-topup-9ab1fd829014
>
> Cheers,
prev parent reply other threads:[~2026-12-04 14:22 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 18:45 [PATCH v2] KVM: arm64: Unconditionally top up the mmu memcache on user fault Lorenzo Stoakes (ARM)
2026-12-04 13:39 ` Leonardo Bras
2026-12-04 14:22 ` Usama Arif [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0220d594-041c-41f7-b8ba-84cfaffda1af@linux.dev \
--to=usama.arif@linux.dev \
--cc=catalin.marinas@arm.com \
--cc=christoffer.dall@linaro.org \
--cc=fuad.tabba@linux.dev \
--cc=jintack.lim@linaro.org \
--cc=joey.gouly@arm.com \
--cc=kvmarm@lists.linux.dev \
--cc=leo.bras@arm.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=stable@vger.kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=wangyanan55@huawei.com \
--cc=weilin.chang@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.