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 v3 05/11] KVM: arm64: CCA: Tear down RTTs
Date: Mon, 16 Nov 2026 17:08:53 +0000	[thread overview]
Message-ID: <sashiko-outbox-217410@kernel.org> (raw)
In-Reply-To: <20261116164358.142785-6-steven.price@arm.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] KVM: arm64: unclamped stage-2 levels in get_start_level() on
  64K pages
- [Medium] KVM: arm64: CCA: unlocked kvm_realm_is_created() check in
  kvm_uninit_stage2_mmu()
- [Low] KVM: arm64: mix of guard() and goto cleanup in
  kvm_destroy_realm()
- [Low] KVM: arm64: possible skipped parent retry in
  realm_tear_down_rtt_level()
--

commit 80a462fb8b46e04c72060d069b1296caf72d83ff
Author: Steven Price <steven.price@arm.com>

KVM: arm64: CCA: Tear down RTTs

This commit adds stage-2 unmapping and Realm Translation Table (RTT)
teardown for Arm CCA Realms during stage-2 MMU uninitialization and final
Realm destruction.

> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index ac72db3d4505f..9baf2352d2a8f 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -1145,9 +1145,25 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
>  	return err;
>  }
>  
> +static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
> +{
> +	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
> +
> +	mutex_lock(&kvm->arch.config_lock);
> +	if (kvm_realm_state(kvm) == REALM_STATE_NEW ||
> +	    kvm_realm_state(kvm) == REALM_STATE_ACTIVE) {
> +		kvm_set_realm_state(kvm, REALM_STATE_DYING);
> +		WARN_ON(kvm_realm_teardown_stage2(kvm));
> +	}
> +	mutex_unlock(&kvm->arch.config_lock);
> +}
> +
>  void kvm_uninit_stage2_mmu(struct kvm *kvm)
>  {
> -	kvm_free_stage2_pgd(&kvm->arch.mmu);
> +	if (kvm_realm_is_created(kvm))
> +		kvm_realm_uninit_stage2(&kvm->arch.mmu);
> +	else
> +		kvm_free_stage2_pgd(&kvm->arch.mmu);
>  	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
>  }

[Severity: Medium]
Can kvm_uninit_stage2_mmu() race with Realm creation by checking
kvm_realm_is_created() outside kvm->arch.config_lock?

If kvm_mmu_notifier_release() -> kvm_arch_flush_shadow_all() runs before
or concurrently with Realm creation, kvm_uninit_stage2_mmu() takes the
else branch without holding config_lock, freeing mmu->pgt and clearing
mmu->pgd_phys while leaving realm.state at REALM_STATE_NONE.  Checking
kvm_vm_is_realm(kvm) here and handling all realm.state transitions inside
kvm_realm_uninit_stage2() under config_lock (including moving
REALM_STATE_NONE to REALM_STATE_DEAD and freeing the PGD) would avoid the
race.

> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index 57f59b618a96e..74b89da4e8015 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c
> @@ -16,22 +16,213 @@ u32 kvm_rmm_ipa_limit(void)
>  	return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
>  }
>  
> +static int get_start_level(struct realm *realm)
> +{
> +	/*
> +	 * kvm_init_ipa_range() enforces a minimum of 32 IPA bits
> +	 * (ARM64_MIN_PARANGE_BITS).
> +	 */
> +	return 4 - stage2_pgtable_levels(realm->ia_bits);
> +}

[Severity: High]
Does get_start_level() return 3 instead of 2 for a 32-bit or 33-bit IPA
Realm on CONFIG_ARM64_64K_PAGES?

stage2_pgtable_levels(ipa) evaluates ARM64_HW_PGTABLE_LEVELS((ipa) - 4),
which is 1 for 32 or 33 IPA bits with 64K pages, whereas kvm_get_vtcr()
clamps the stage-2 levels to at least 2:

arch/arm64/kvm/hyp/pgtable.c:kvm_get_vtcr() {
    ...
	lvls = stage2_pgtable_levels(phys_shift);
	if (lvls < 2)
		lvls = 2;
    ...
}

When get_start_level() returns 3, realm_tear_down_rtt_range() calls
realm_tear_down_rtt_level() with level = 4 (> KVM_PGTABLE_LAST_LEVEL),
triggering WARN_ON() and returning -EINVAL, which causes
kvm_destroy_realm() to abort and leak the Realm Descriptor, RTT granules
and stage-2 PGD pages.

[ ... ]

> +static int realm_tear_down_rtt_level(struct realm *realm, int level,
> +				     unsigned long start, unsigned long end)
> +{
> +	ssize_t map_size;
> +	unsigned long addr, next_addr;
> +
> +	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
> +		return -EINVAL;
> +
> +	map_size = kvm_granule_size(level - 1);
> +	if (WARN_ON(!IS_ALIGNED(start, map_size)))
> +		return -EINVAL;
> +
> +	for (addr = start; addr < end; addr = next_addr) {
> +		phys_addr_t rtt_granule;
> +		long ret;
> +		unsigned long align_addr = ALIGN(addr, map_size);
> +
> +		next_addr = ALIGN(addr + 1, map_size);
> +
> +		if (next_addr > end || align_addr != addr) {
> +			/*
> +			 * The target range is smaller than what this level
> +			 * covers, recurse deeper.
> +			 */
> +			ret = realm_tear_down_rtt_level(realm,
> +							level + 1,
> +							addr,
> +							min(next_addr, end));
> +			if (ret)
> +				return ret;
> +			continue;
> +		}
> +
> +		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;
> +		case RMI_ERROR_RTT:
> +			if (next_addr > addr) {
> +				/* Missing RTT, skip */
> +				break;
> +			}
> +			/*
> +			 * We tear down the RTT range for the full IPA
> +			 * space, after everything is unmapped. Also we
> +			 * descend down only if we cannot tear down a
> +			 * top level RTT. Thus RMM must be able to walk
> +			 * to the requested level. e.g., a block mapping
> +			 * exists at L1 or L2.
> +			 */
> +			if (WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level))
> +				return -EBUSY;

[Severity: Low]
This question rests on an Arm CCA RMM specification detail that could not
be confirmed from the kernel source: when SMC_RMI_RTT_DESTROY fails with
RMI_ERROR_RTT because the RTT at @level still has live child table
entries, can the RMM return out_top > addr (with addr < next_addr <
addr + map_size) if the initial entries in that RTT are non-live, or report
RMI_RESULT_DATA_LEVEL(ret) == level - 1?

If out_top advances to the first live entry inside the RTT at @level, the
if (next_addr > addr) check treats the RTT as missing and advances addr to
that unaligned next_addr; the next iteration then sees align_addr != addr,
destroys the remaining child RTTs at level + 1, and continues to the next
map_size boundary without retrying realm_rtt_destroy() on the parent RTT
at @level, leaving it live and causing rmi_realm_destroy() to fail.  If
instead the walk terminates at level - 1 with next_addr == addr,
WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level) fires and aborts teardown
with -EBUSY.

[ ... ]

>  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 RMM teardown with Realm creation and stage-2 teardown,
> +	 * which also access Realm state under config_lock.
> +	 */
> +	guard(mutex)(&kvm->arch.config_lock);
> +
>  	if (!kvm_realm_is_created(kvm))
>  		goto out_free_sro;
>  
>  	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 the IPA space
> +	 * 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.
> +	 */
> +	realm_unmap_stage2(kvm);
> +
>  	if (realm->rd_phys) {
>  		phys_addr_t rd_phys = realm->rd_phys;
>  
>  		if (WARN_ON(rmi_realm_terminate(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(rd_phys, realm->sro)))
>  			goto out_free_sro;
>  		rmi_free_delegated_page(rd_phys);
> @@ -44,6 +235,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: Low]
This isn't a bug, but kvm_destroy_realm() mixes guard(mutex) with goto
out_free_sro cleanup.  Using __free(kfree) for realm->sro (or explicit
mutex_lock() and mutex_unlock()) would avoid mixing scope-based cleanup
and goto unwinding in the same function.

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

  reply	other threads:[~2026-11-16 17:08 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 16:43 [PATCH v3 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-11-16 16:43 ` [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot Steven Price
2026-11-17 14:05   ` Fuad Tabba
2026-11-16 16:43 ` [PATCH v3 02/11] KVM: arm64: CCA: Introduce Realms Steven Price
2026-11-17 14:07   ` Fuad Tabba
2026-11-16 16:43 ` [PATCH v3 03/11] KVM: arm64: Handle attributes in stage2_unmap_range Steven Price
2026-11-17 14:16   ` Fuad Tabba
2026-11-16 16:43 ` [PATCH v3 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
2026-11-16 16:43 ` [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs Steven Price
2026-11-16 17:08   ` sashiko-bot [this message]
2026-11-16 16:43 ` [PATCH v3 06/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-11-16 17:21   ` sashiko-bot
2026-11-16 16:43 ` [PATCH v3 07/11] KVM: Allow architectures to reject memslot changes before invalidation Steven Price
2026-11-17 14:31   ` Fuad Tabba
2026-11-16 16:43 ` [PATCH v3 08/11] KVM: arm64: CCA: Prevent memslot modifications after Realm creation Steven Price
2026-11-17 14:42   ` Fuad Tabba
2026-11-16 16:43 ` [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-11-16 17:31   ` sashiko-bot
2026-11-16 19:09     ` Suzuki K Poulose
2026-11-16 16:43 ` [PATCH v3 10/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
2026-11-17 15:02   ` Fuad Tabba
2026-11-16 16:43 ` [PATCH v3 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price

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-217410@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.