All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Usama Arif <usama.arif@linux.dev>
Cc: 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>,
	 linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
	linux-kernel@vger.kernel.org,  stable@vger.kernel.org
Subject: Re: [PATCH] KVM: arm64: Top up the mmu memcache on user fault for NV
Date: Fri, 27 Nov 2026 17:26:47 +0000	[thread overview]
Message-ID: <awm5PZYWxVOXdBfm@gremlin> (raw)
In-Reply-To: <20261127152237.108363-1-usama.arif@linux.dev>

On Fri, Nov 27, 2026 at 07:22:36AM -0800, Usama Arif wrote:
> On Thu, 26 Nov 2026 17:28:29 +0000 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> 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 nested virtualisation adds another edge case - the mapping size of
> > a shadow S2 is clamped to that of the guest's own S2. If the guest
> > hypervisor replaces a block mapping with a table, a permission fault on the
> > shadow block has to be mapped at PAGE_SIZE, which requires a page table to
> > collapse the block into.
> >
> > 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, also top up the memcache for permission faults taken on a
> > shadow S2.
> >
> > Note that dirty logging had a similar issue, addressed in commit
> > 56fd1fc526e6 ("KVM: arm64: Top up stage-2 memcache for dirty logging
> > faults").
> >
> > 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>
> > ---
> >  arch/arm64/kvm/mmu.c | 8 +++++---
> >  1 file changed, 5 insertions(+), 3 deletions(-)
> >
> > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> > index a0d4981ebb99..e09a8f2c0482 100644
> > --- a/arch/arm64/kvm/mmu.c
> > +++ b/arch/arm64/kvm/mmu.c
> > @@ -2256,16 +2256,18 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
> >
> >  	/*
> >  	 * 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 so normally don't require allocations from the memcache. One
> > +	 * 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.
> > +	 * With nested, a shadow stage-2 block may also need to be collapsed
> > +	 * into a table if the guest's own stage-2 mapping has become smaller.
> >  	 */
> >  	memcache = get_mmu_memcache(s2fd->vcpu);
> >  	if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
> > -	    is_protected_kvm_enabled()) {
> > +	    is_protected_kvm_enabled() || s2fd->nested) {
> >  		ret = topup_mmu_memcache(s2fd->mmu, memcache);
> >  		if (ret)
> >  			return ret;
> >
>
> Thanks for the fix!
>
> On the narrow condition being the smaller backport: I don't think it
> is. The hunk already conflicts on 7.3, where topup_mmu_memcache() still
> takes the vCPU, and the condition looks different on 7.2 and older, so
> every stable tree needs a small manual backport either way.
>
> A permission fault can also allocate outside NV. For example, take an
> exec permission fault on a THP block, where the VMM replaces that
> memory with 64K hugetlb pages before the fault looks up the VMA:
>
>   - the MMU notifier unmaps the block, and mmu_seq is read afterwards
>   - kvm_s2_resolve_vma_size() forces PAGE_SIZE for the CONT_PTE hugetlb
>   - PAGE_SIZE != perm_fault_granule, so kvm_pgtable_stage2_map() runs
>     and allocates a table, with no top-up
>
> That one is narrow and VMM-driven, but topping up for every fault would
> cover it too, and it costs a single comparison when the cache already
> holds enough pages.
>

Reads a little schloppy ;) But I think you're right yeah.

Good bloody lord @ 64 KiB hugetlb pages being a thing... obscene :)

Anyway, I'll send an unconditional v2, it's really not that much delta in
general.

--
Cheers, Lorenzo

      reply	other threads:[~2026-11-27 17:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 17:28 [PATCH] KVM: arm64: Top up the mmu memcache on user fault for NV Lorenzo Stoakes (ARM)
2026-11-27 11:58 ` Leonardo Bras
2026-11-27 13:09   ` Lorenzo Stoakes (ARM)
2026-11-30 12:03     ` Leonardo Bras
2026-11-30 16:52       ` Lorenzo Stoakes (ARM)
2026-12-01 14:30         ` Leonardo Bras
2026-12-01 15:53           ` Lorenzo Stoakes (ARM)
2026-12-01 16:02             ` Leonardo Bras
2026-11-27 15:22 ` Usama Arif
2026-11-27 17:26   ` Lorenzo Stoakes (ARM) [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=awm5PZYWxVOXdBfm@gremlin \
    --to=ljs@kernel.org \
    --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=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=usama.arif@linux.dev \
    --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.