All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Nathan Chen" <nathanc@nvidia.com>, kvmarm@lists.linux.dev
Cc: "Oliver Upton" <oupton@kernel.org>, "Marc Zyngier" <maz@kernel.org>
Subject: Re: [RFC PATCH 4/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand
Date: Tue, 24 Nov 2026 21:34:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-232050@kernel.org> (raw)
In-Reply-To: <20261124211218.2751086-5-nathanc@nvidia.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] KVM: arm64: nv: premature pool growth in get_s2_mmu_nested()
  clock sweep
- [Medium] KVM: arm64: nv: missing __GFP_NORETRY in init_nested_s2_mmu()
  on pool grow
- [Medium] KVM: arm64: nv: skipped baseline MMU allocation in
  kvm_vcpu_init_nested()
--

commit ed87347a8e1063feec4d2776535ef6edc4da64e2
Author: Nathan Chen <nathanc@nvidia.com>

KVM: arm64: nv: Grow the shadow S2 MMU pool on demand

This commit grows a nested-capable VM's shadow stage-2 MMU pool on demand
when all free MMUs in the pool have been accessed since the last victim
sweep, bounded by a per-VM limit and a host-wide VMID budget.

> diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
> index 44dc6780fe6fe..d530a37716be6 100644
> --- a/arch/arm64/kvm/nested.c
> +++ b/arch/arm64/kvm/nested.c
[ ... ]
> @@ -91,16 +139,25 @@ static int init_nested_s2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)
>  	return kvm_init_stage2_mmu(kvm, mmu, kvm_get_pa_bits(kvm));
>  }
>  
> -/* Add a block of @nr shadow S2 MMUs to the pool */
> -static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr)
> +/*
> + * Add a block of @nr shadow S2 MMUs to the pool, reallocating the
> + * pointer table if it is full. Only the table moves, never the MMUs, so
> + * cached hw_mmu pointers stay valid. Runtime walkers of the table hold
> + * mmu_lock, and kvm_arch_flush_shadow_all() only runs once no vcpu can
> + * enter the guest, so it can't race with a grow.
> + */
> +static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr, bool grow)
>  {
> +	gfp_t gfp = GFP_KERNEL_ACCOUNT | (grow ? __GFP_NORETRY : 0);
>  	size_t size = kvm->arch.nested_mmus_size;
> +	size_t cap = kvm->arch.nested_mmus_capacity;
> +	struct kvm_s2_mmu **table = NULL;
>  	struct nested_s2_mmu_block *blk;
>  	int i, ret = 0;
>  
>  	lockdep_assert_held(&kvm->arch.config_lock);
>  
> -	blk = kvzalloc_flex(*blk, mmus, nr, GFP_KERNEL_ACCOUNT);
> +	blk = kvzalloc_flex(*blk, mmus, nr, gfp);
>  	if (!blk)
>  		return -ENOMEM;
>  
>  	for (i = 0; i < nr; i++) {
>  		ret = init_nested_s2_mmu(kvm, &blk->mmus[i]);
>  		if (ret)
>  			goto out_free;

[Severity: Medium]
Should init_nested_s2_mmu() -> kvm_init_stage2_mmu() also receive @gfp so
that its allocations carry __GFP_NORETRY when @grow is true?

kvzalloc_flex() and kvmalloc_objs() use __GFP_NORETRY on grow, but
kvm_init_stage2_mmu() still allocates each struct kvm_pgtable and stage-2
PGD (up to 16 contiguous pages per MMU in kvm_host_zalloc_pages_exact())
with hardcoded GFP_KERNEL_ACCOUNT.  When a VM near its memcg limit
triggers kvm_nested_s2_grow(), the small block allocation can succeed
while the PGD allocations enter full reclaim and trigger the memcg OOM
killer instead of failing fast and setting nested_mmus_no_grow.

[ ... ]

> @@ -155,7 +270,7 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
>  	num_mmus = atomic_read(&kvm->online_vcpus) * S2_MMU_PER_VCPU;
>  
>  	if (num_mmus > kvm->arch.nested_mmus_size) {
> -		int ret = kvm_nested_add_s2_mmus(kvm, S2_MMU_PER_VCPU);
> +		int ret = kvm_nested_add_s2_mmus(kvm, S2_MMU_PER_VCPU, false);

[Severity: Medium]
Should kvm_vcpu_init_nested() compare num_mmus against the baseline MMU
count (nested_mmus_size - nested_mmus_grown) rather than
nested_mmus_size?

If an already-initialized vcpu runs and triggers kvm_nested_s2_grow()
before the remaining vcpus run KVM_ARM_VCPU_INIT, nested_mmus_size
already includes nested_mmus_grown.  Later kvm_vcpu_init_nested() calls
then see num_mmus <= nested_mmus_size and skip allocating those vcpus'
baseline S2_MMU_PER_VCPU MMUs, leaving the pool smaller by
S2_MMU_PER_VCPU per late-initialized vcpu and keeping their baseline MMUs
charged against the global nested_s2_mmus_grown budget.

[ ... ]

> @@ -842,6 +957,7 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
>  {
>  	struct kvm *kvm = vcpu->kvm;
>  	struct kvm_s2_mmu *s2_mmu;
> +	bool in_use = true;
>  	int i, victim = -1;
>  
>  	lockdep_assert_held_write(&vcpu->kvm->mmu_lock);
> @@ -859,8 +975,8 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
>  	 * hasn't been used since the search last went past it (such as
>  	 * one left behind by a torn down guest), is recycled straight
>  	 * away. Other free MMUs lose their accessed bit and get a second
> -	 * chance. If every free MMU was used since the last pass, the
> -	 * first one found is recycled.
> +	 * chance. If every free MMU was used since the last pass, they
> +	 * are all in concurrent use and the pool is too small.
>  	 */
>  	for (i = kvm->arch.nested_mmus_next;
>  	     i < (kvm->arch.nested_mmus_size + kvm->arch.nested_mmus_next);
> @@ -873,6 +989,7 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
>  
>  		if (!kvm_s2_mmu_valid(mmu) || !mmu->accessed) {
>  			victim = i;
> +			in_use = false;
>  			break;
>  		}
>  
> @@ -891,6 +1008,14 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
>  	if (kvm_s2_mmu_valid(s2_mmu))
>  		s2_mmu->pending_unmap = true;
>  
> +	/* Every free MMU is in concurrent use: the pool is too small */
> +	if (in_use && !READ_ONCE(kvm->arch.nested_mmus_no_grow) &&
> +	    kvm->arch.nested_mmus_size < S2_MMU_MAX_PER_VM &&
> +	    nested_s2_mmus_budget_left()) {
> +		WRITE_ONCE(kvm->arch.nested_mmus_grow, true);
> +		kvm_make_request(KVM_REQ_NESTED_S2_GROW, vcpu);
> +	}

[Severity: Medium]
Does setting s2_mmu->accessed = true on a lookup miss cause
get_s2_mmu_nested() to grow the pool even when old MMUs are never reused?

When the loop picks an invalid or unaccessed slot at nested_mmus_next, it
breaks on the first iteration, advances nested_mmus_next to victim + 1
without sweeping other slots, and sets s2_mmu->accessed = true at the out
label:

arch/arm64/kvm/nested.c:get_s2_mmu_nested() {
    ...
out:
	atomic_inc(&s2_mmu->refcnt);
	s2_mmu->accessed = true;
    ...
}

Each newly allocated MMU therefore sits right behind the clock hand with
accessed == true before any lookup_s2_mmu() hit has occurred.  After the
initial 2 * online_vcpus misses fill the invalid slots, the very first
recycle sees accessed == true on every slot and triggers
KVM_REQ_NESTED_S2_GROW even if the guest only ran one nested VM at a
time.  After a grow resets nested_mmus_next = size, consuming the new
invalid slots and stepping one by one through the unaccessed slots leaves
at least size + online_vcpus - 1 slots with accessed == true from
allocation misses alone, so a single active reference or lookup hit on
the remaining slot triggers another grow cycle, inflating the pool toward
S2_MMU_MAX_PER_VM (4096) and slowing linear scans over nested_mmus under
mmu_lock.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124211218.2751086-1-nathanc@nvidia.com?part=4

  reply	other threads:[~2026-11-24 21:34 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 21:12 [RFC PATCH 0/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 1/6] KVM: arm64: nv: Allocate shadow S2 MMUs in blocks Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 2/6] KVM: arm64: nv: Pick the shadow S2 MMU to recycle with a clock Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 3/6] KVM: arm64: Add kvm_arm_vmid_nr_usable() Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 4/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Nathan Chen
2026-11-24 21:34   ` sashiko-bot [this message]
2026-11-24 21:12 ` [RFC PATCH 5/6] KVM: arm64: nv: Park grown MMUs on ALLE1 and reactivate them " Nathan Chen
2026-11-24 21:36   ` sashiko-bot
2026-11-24 21:12 ` [RFC PATCH 6/6] KVM: arm64: nv: Park shadow S2 MMUs emptied by a guest range invalidation Nathan Chen
2026-11-24 21:42   ` sashiko-bot
2026-11-24 22:29 ` [RFC PATCH 0/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Oliver Upton
2026-11-25 22:36   ` Nathan Chen

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=sashiko-outbox-232050@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=nathanc@nvidia.com \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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.