All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Steven Price" <steven.price@arm.com>,
	kvm@vger.kernel.org, kvmarm@lists.linux.dev
Cc: "Oliver Upton" <oupton@kernel.org>, "Marc Zyngier" <maz@kernel.org>
Subject: Re: [PATCH v6 06/11] KVM: arm64: CCA: Destroy RTTs
Date: Fri, 04 Dec 2026 12:28:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-249789@kernel.org> (raw)
In-Reply-To: <20261204115725.45272-7-steven.price@arm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] KVM: arm64: kvm_destroy_realm() leaks mmu->pgt and
  last_vcpu_ran on error
- [Low] KVM: arm64: mix of guard(mutex) and goto cleanup in
  kvm_destroy_realm()
- [Low] KVM: arm64: mismatched RMI_SUCCESS check in realm_rtt_destroy()
--

commit 4cf622c6d71a9932890c5983b5d74291beb02ebe
Author: Steven Price <steven.price@arm.com>

KVM: arm64: CCA: Destroy RTTs

This commit adds helpers to unmap a Realm's protected stage-2 range and
recursively destroy non-root Realm Translation Tables (RTTs) during Realm
teardown, deferring kvm_free_stage2_pgd() until after the RD is destroyed
and the root RTTs are undelegated.

> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index ee16f5364b9aa..f176ea9184d7c 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c
> @@ -102,6 +102,30 @@ static long realm_rtt_create(struct realm *realm,
>  	return rmi_rtt_create(realm->rd_phys, phys, addr, level);
>  }
>  
> +/*
> + * realm_rtt_destroy - Destroy an RTT at @level for @addr.
> + *
> + * Returns - Result of the RMI_RTT_DESTROY call, and:
> + * @rtt_granule:	RTT granule, if the RTT was destroyed.
> + * @next_addr:		IPA corresponding to the next possible valid entry we
> + *			can target
> + */
> +static long realm_rtt_destroy(struct realm *realm, unsigned long addr,
> +			      int level, phys_addr_t *rtt_granule,
> +			      unsigned long *next_addr)
> +{
> +	unsigned long out_rtt;
> +	long ret;
> +
> +	ret = rmi_rtt_destroy(realm->rd_phys, addr, level,
> +			      &out_rtt, next_addr);
> +
> +	if (ret == RMI_SUCCESS)
> +		*rtt_granule = out_rtt;
> +
> +	return ret;
> +}

[ ... ]

> +	for (addr = start; addr < end; addr = next_addr) {
> +		phys_addr_t rtt_granule;

[ ... ]

> +		ret = realm_rtt_destroy(realm, addr, level,
> +					&rtt_granule, &next_addr);
> +		if (ret < 0)
> +			return ret;
> +
> +		switch (RMI_RESULT_STATUS(ret)) {
> +		case RMI_SUCCESS:
> +			free_rtt(rtt_granule);
> +			break;

[Severity: Low]
This question rests on an RMM ABI detail that could not be confirmed from
the kernel source: can SMC_RMI_RTT_DESTROY return a non-negative RmiResult
with RMI_RESULT_STATUS(ret) == RMI_SUCCESS while any upper bits are
non-zero?

If so, rmi_rtt_destroy() sets *out_rtt (switching on RMI_RESULT_STATUS(ret))
and returns the raw result, but realm_rtt_destroy() skips assigning
*rtt_granule because it tests ret == RMI_SUCCESS.  realm_destroy_rtt_level()
then enters case RMI_SUCCESS and passes the uninitialized stack variable
rtt_granule to free_rtt(), which can corrupt memory or crash during Realm
teardown.  Checking RMI_RESULT_STATUS(ret) == RMI_SUCCESS in
realm_rtt_destroy() would avoid depending on the upper bits being zero.

[ ... ]

> @@ -352,14 +495,32 @@ void kvm_destroy_realm(struct kvm *kvm)
>  	struct realm *realm = &kvm->arch.realm;
>  	size_t pgd_size = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr);
>  
> +	/*
> +	 * Serialize final Realm destruction with Realm creation and stage-2 unmapping,
> +	 * which also access Realm state under config_lock.
> +	 */
> +	guard(mutex)(&kvm->arch.config_lock);
> +
>  	if (!kvm_realm_is_created(kvm))
>  		goto out_free_sro;

[Severity: Low]
This isn't a bug, but kvm_destroy_realm() mixes guard(mutex) with
goto out_free_sro cleanup.  Should it use explicit mutex_lock() and
mutex_unlock() (or scope-based cleanup for realm->sro as well) instead of
mixing the two styles?

>  	kvm_set_realm_state(kvm, REALM_STATE_DYING);
>  
> +	/*
> +	 * REALM_DESTROY requires the realm to be non-live: all RECs must have
> +	 * been destroyed and the root RTTs must be empty. Unmap protected IPAs
> +	 * and destroy any non-root RTTs before tearing down the RD. The root
> +	 * RTT pages are still owned by the RMM at this point, so keep the KVM
> +	 * pgtable alive until after REALM_DESTROY and undelegation.
> +	 */
> +	kvm_realm_unmap_stage2_all(kvm);
> +
>  	if (WARN_ON(rmi_realm_terminate(realm->rd_phys, realm->sro)))
>  		goto out_free_sro;
>  
> +	if (WARN_ON(realm_destroy_rtts(kvm)))
> +		goto out_free_sro;
> +
>  	if (WARN_ON(rmi_realm_destroy(realm->rd_phys, realm->sro)))
>  		goto out_free_sro;
>  	rmi_free_delegated_page(realm->rd_phys);
> @@ -370,6 +531,9 @@ void kvm_destroy_realm(struct kvm *kvm)
>  
>  	kvm_set_realm_state(kvm, REALM_STATE_DEAD);
>  
> +	/* Now that the realm is destroyed, free the entry-level RTTs. */
> +	kvm_free_stage2_pgd(&kvm->arch.mmu);
> +
>  out_free_sro:
>  	kfree(realm->sro);
>  	realm->sro = NULL;

[Severity: Medium]
Does jumping to out_free_sro on teardown errors leak mmu->pgt and
mmu->last_vcpu_ran?

Because kvm_uninit_stage2_mmu() skips kvm_free_stage2_pgd() for created
Realms, kvm_destroy_realm() is the only path that frees the host-only
allocations created in kvm_init_stage2_mmu().  When rmi_realm_terminate(),
realm_destroy_rtts(), rmi_realm_destroy(), or rmi_undelegate_range() fails,
skipping kvm_free_stage2_pgd() intentionally retains the still-delegated
pgt->pgd pages, but it also permanently leaks the non-delegated mmu->pgt
slab object and mmu->last_vcpu_ran per-CPU allocation.

Separating freeing of the host-only mmu->pgt and mmu->last_vcpu_ran
allocations from freeing the delegated pgt->pgd pages would avoid leaking
them on error.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204115725.45272-1-steven.price@arm.com?part=6

  reply	other threads:[~2026-12-04 12:28 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-12-04 11:57 ` [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms Steven Price
2026-12-04 16:07   ` Kohei Enju
2026-12-04 17:05     ` Steven Price
2026-12-04 11:57 ` [PATCH v6 02/11] KVM: arm64: Pass range filter to stage2_unmap_range Steven Price
2026-12-04 11:57 ` [PATCH v6 03/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
2026-12-04 11:57 ` [PATCH v6 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
2026-12-04 12:15   ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 05/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-12-04 12:28   ` sashiko-bot
2026-12-05 11:51     ` Suzuki K Poulose
2026-12-04 11:57 ` [PATCH v6 06/11] KVM: arm64: CCA: Destroy RTTs Steven Price
2026-12-04 12:28   ` sashiko-bot [this message]
2026-12-04 11:57 ` [PATCH v6 07/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half Steven Price
2026-12-04 15:48   ` Kohei Enju
2026-12-04 16:27     ` Steven Price
2026-12-04 11:57 ` [PATCH v6 08/11] KVM: arm64: CCA: Advertise private memory support for Realms Steven Price
2026-12-04 12:23   ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 09/11] KVM: arm64: Select protected VM behaviour by VM flavour Steven Price
2026-12-04 13:21   ` Fuad Tabba
2026-12-04 11:57 ` [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-12-04 12:39   ` sashiko-bot
2026-12-04 15:32   ` Kohei Enju
2026-12-04 16:27     ` Steven Price
2026-12-04 11:57 ` [PATCH v6 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price
2026-12-04 12:44   ` sashiko-bot
2026-12-04 16:30 ` [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Kohei Enju

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-249789@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=steven.price@arm.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.