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 6/6] KVM: arm64: nv: Park shadow S2 MMUs emptied by a guest range invalidation
Date: Tue, 24 Nov 2026 21:42:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-232055@kernel.org> (raw)
In-Reply-To: <20261124211218.2751086-7-nathanc@nvidia.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] KVM: arm64: nv: stale guest_s2_mappings left by
  s2_mmu_unmap_range()
--

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

KVM: arm64: nv: Park shadow S2 MMUs emptied by a guest range invalidation

This commit exports kvm_nested_s2_park() and calls it from
s2_mmu_unmap_range() so that grown, unreferenced shadow stage-2 MMUs
emptied by a guest TLBI range invalidation are immediately parked.

> diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
> index df81adf962599..99dcd2549b08a 100644
> --- a/arch/arm64/kvm/sys_regs.c
> +++ b/arch/arm64/kvm/sys_regs.c
> @@ -4123,6 +4123,14 @@ static void s2_mmu_unmap_range(struct kvm_s2_mmu *mmu,
>  	 * the TLBI.
>  	 */
>  	kvm_stage2_unmap_range(mmu, info->range.start, info->range.size, true);
> +
> +	/*
> +	 * The guest hypervisor may have unmapped everything this MMU held,
> +	 * e.g. all memory of a nested VM being torn down. If it is grown and
> +	 * now empty, return it to the budget rather than leave it for a
> +	 * recycle to unmap again.
> +	 */
> +	kvm_nested_s2_park(kvm_s2_mmu_to_kvm(mmu), mmu);
>  }

[Severity: High]
Can calling kvm_nested_s2_park() after a partial range invalidation
(TLBI RIPAS2E1IS) leave stale kvm_guest_s2_mapping entries in
mmu->guest_s2_mappings and kvm->arch.mmu.guest_s2_mappings?

When a guest TLBI RIPAS2E1IS unmaps a range smaller than a recorded block
mapping (or unmaps a block across multiple sub-block ranges),
stage2_unmap_walker() unmaps the entire overlapping block PTE and can leave
mmu->pgt empty, while kvm_remove_guest_s2_mappings() only frees mappings
completely enclosed within that single unmap range:

arch/arm64/kvm/nested.c:kvm_remove_guest_s2_mappings() {
    ...
		nested_start = s2_mapping_to_nested_start(mapping);
		if (nipa <= nested_start && nipa_end >= mapping->nested.last) {
			interval_tree_remove(&mapping->nested, &mmu->guest_s2_mappings);
			interval_tree_remove(&mapping->canonical,
					     &kvm->arch.mmu.guest_s2_mappings);
			kfree(mapping);
		}
    ...
}

Because kvm_nested_s2_park() only checks kvm_s2_mmu_empty(mmu) (which walks
mmu->pgt), it marks the MMU invalid via kvm_init_nested_s2_mmu() and clears
pending_unmap without draining mmu->guest_s2_mappings.  Later full-VMID
invalidations skip the invalidated MMU, and reactivating it in
get_s2_mmu_nested() sees !kvm_s2_mmu_valid(s2_mmu) and skips setting
pending_unmap.  The leftover mappings leak across park/grow cycles and let
kvm_nested_unmap_cipa_range() unmap unrelated nested IPAs after the MMU is
reused for a new nested context.

Should kvm_nested_s2_park() call
kvm_remove_guest_s2_mappings(mmu, 0, kvm_phys_size(mmu)) before parking the
MMU?

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

  reply	other threads:[~2026-11-24 21:42 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
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 [this message]
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-232055@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.