From: Suzuki K Poulose <suzuki.poulose@arm.com>
To: Steven Price <steven.price@arm.com>,
kvm@vger.kernel.org, kvmarm@lists.linux.dev
Cc: Catalin Marinas <catalin.marinas@arm.com>,
Marc Zyngier <maz@kernel.org>, Will Deacon <will@kernel.org>,
James Morse <james.morse@arm.com>,
Oliver Upton <oliver.upton@linux.dev>,
Zenghui Yu <yuzenghui@huawei.com>,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, Joey Gouly <joey.gouly@arm.com>,
Alexandru Elisei <alexandru.elisei@arm.com>,
Christoffer Dall <christoffer.dall@arm.com>,
Fuad Tabba <tabba@google.com>,
linux-coco@lists.linux.dev,
Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com>,
Gavin Shan <gshan@redhat.com>,
Shanker Donthineni <sdonthineni@nvidia.com>,
Alper Gun <alpergun@google.com>,
"Aneesh Kumar K . V" <aneesh.kumar@kernel.org>,
Emi Kisanuki <fj0570is@fujitsu.com>,
Vishal Annapurve <vannapurve@google.com>,
WeiLin.Chang@arm.com, Lorenzo Pieralisi <lpieralisi@kernel.org>,
Kohei Enju <enju.kohei@fujitsu.com>
Subject: Re: [PATCH v5 06/11] KVM: arm64: CCA: Destroy RTTs
Date: Tue, 1 Dec 2026 23:17:38 +0000 [thread overview]
Message-ID: <78571dbf-2237-462d-b34d-a1045a9250ad@arm.com> (raw)
In-Reply-To: <20261130142534.219108-7-steven.price@arm.com>
On 30/11/2026 14:25, Steven Price wrote:
> The RMM owns the stage 2 page tables for a realm, and KVM must request
> that the RMM creates/destroys entries as necessary. The physical pages
> to store the page tables are delegated to the realm as required, and can
> be undelegated when no longer used.
>
> Creating new RTTs is the easy part, destroying them is a little more
> tricky. The result of realm_rtt_destroy() can be used to effectively
> walk the tree and destroy the entries (undelegating pages that were
> given to the realm).
>
> REALM_DESTROY requires no RECs, live mappings, or non-root RTTs. Unmap
> the protected IPA half, then use REALM_TERMINATE to move the Realm to
> ZOMBIE. Destroy non-root RTTs before destroying the RD. Unprotected
> entries are not live and need no explicit unmap. Undelegate the root
> RTTs only after the RD has gone away.
>
> Signed-off-by: Steven Price <steven.price@arm.com>
>
> ---
> Changes since v4:
> - Clarify Realm liveness and drop the stale start-level explanation.
> - Fold the RTT destroy wrapper, use a scoped mutex guard, and unmap
> only private mappings during teardown.
>
> Changes since v3:
> - Match VTCR's minimum of two stage-2 levels for Realm RTTs.
> - Use explicit config_lock cleanup in Realm destruction.
>
> Changes since v2:
> - Use the actual stage-2 start level and correct the RTT skip
> granularity. Free SRO state on final teardown errors.
>
> Changes since v1:
> - Tear down created Realms in both NEW and ACTIVE states. Fix the
> RTT start level, page accounting on failed undelegation, and
> handling of RTT granules returned by RMI.
> ---
> arch/arm64/include/asm/kvm_mmu.h | 5 +
> arch/arm64/include/asm/kvm_rmi.h | 3 +
> arch/arm64/kvm/mmu.c | 23 ++++-
> arch/arm64/kvm/rmi.c | 171 +++++++++++++++++++++++++++++++
> 4 files changed, 198 insertions(+), 4 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
> index 32392abf2e21..6ae9d07fefee 100644
> --- a/arch/arm64/include/asm/kvm_mmu.h
> +++ b/arch/arm64/include/asm/kvm_mmu.h
> @@ -182,6 +182,11 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
> int create_hyp_stack(phys_addr_t phys_addr, unsigned long *haddr);
> void __init free_hyp_pgds(void);
>
> +enum kvm_gfn_range_filter;
> +
> +void kvm_stage2_unmap_range_filter(struct kvm_s2_mmu *mmu, phys_addr_t start,
> + u64 size, bool may_block,
> + enum kvm_gfn_range_filter filter);
If we bring the Patch 7 before this one, we could avoid this and
directly call kvm_realm_unmap_stage2_range_filter() or even
realm_unmap_private_range()
Otherwise, looks good to me
Suzuki
> void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
> u64 size, bool may_block);
> void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end);
> diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h
> index 4ac1af622872..ec6b06ffc28b 100644
> --- a/arch/arm64/include/asm/kvm_rmi.h
> +++ b/arch/arm64/include/asm/kvm_rmi.h
> @@ -58,6 +58,7 @@ enum realm_state {
> * @state: The lifetime state machine for the realm
> * @ia_bits: Number of valid Input Address bits in the IPA
> * @start_level: Starting level of the Realm Translation Table
> + * @stage2_unmapped: The Realm's protected stage-2 mappings have been removed
> */
> struct realm {
> phys_addr_t rd_phys;
> @@ -72,11 +73,13 @@ struct realm {
> enum realm_state state;
> unsigned int ia_bits;
> int start_level;
> + bool stage2_unmapped;
> };
>
> u32 kvm_rmm_ipa_limit(void);
>
> int kvm_init_realm(struct kvm *kvm);
> void kvm_destroy_realm(struct kvm *kvm);
> +void kvm_realm_unmap_stage2_all(struct kvm *kvm);
>
> #endif /* __ASM_KVM_RMI_H */
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index e4bfd25bda45..c2cb231ea65b 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -403,9 +403,9 @@ static int kvm_pgtable_stage2_unmap_tracked(struct kvm_pgtable *pgt, u64 addr, u
> * be called while holding mmu_lock otherwise another faulting VCPU may
> * come in and mess with things behind our backs.
> */
> -static void kvm_stage2_unmap_range_filter(struct kvm_s2_mmu *mmu, phys_addr_t start,
> - u64 size, bool may_block,
> - enum kvm_gfn_range_filter filter)
> +void kvm_stage2_unmap_range_filter(struct kvm_s2_mmu *mmu, phys_addr_t start,
> + u64 size, bool may_block,
> + enum kvm_gfn_range_filter filter)
> {
I
> struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
>
> @@ -1149,9 +1149,24 @@ 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);
> +
> + guard(mutex)(&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);
> + kvm_realm_unmap_stage2_all(kvm);
> + }
> +}
> +
> 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);
> }
>
> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index 6a8bff1d8d46..140c7f9ab2c2 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c
> @@ -16,19 +16,187 @@ u32 kvm_rmm_ipa_limit(void)
> return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
> }
>
> +static void free_rtt(phys_addr_t phys)
> +{
> + kvm_account_pgtable_pages(phys_to_virt(phys), -1);
> + if (rmi_free_delegated_page(phys))
> + kvm_account_pgtable_pages(phys_to_virt(phys), 1);
> +}
> +
> +/*
> + * 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;
> +}
> +
> +static int realm_destroy_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_destroy_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 destroy the RTT range for the full IPA
> + * space, after everything is unmapped. Also we
> + * descend down only if we cannot destroy 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;
> + if (WARN_ON(level == KVM_PGTABLE_LAST_LEVEL))
> + return -EBUSY;
> +
> + /*
> + * The table has active entries in it, recurse deeper
> + * and destroy the RTTs.
> + */
> + next_addr = ALIGN(addr + 1, map_size);
> + ret = realm_destroy_rtt_level(realm,
> + level + 1,
> + addr,
> + next_addr);
> + if (ret)
> + return ret;
> + /*
> + * Now that the child RTTs are destroyed,
> + * retry at this level.
> + */
> + next_addr = addr;
> + break;
> + default:
> + WARN_ON(1);
> + return -ENXIO;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static int realm_destroy_rtts(struct kvm *kvm)
> +{
> + struct realm *realm = &kvm->arch.realm;
> + unsigned int ia_bits = realm->ia_bits;
> +
> + lockdep_assert_held(&kvm->arch.config_lock);
> + if (WARN_ON_ONCE(!realm->stage2_unmapped))
> + return -EINVAL;
> +
> + /* Root RTTs remain until after the RD has been destroyed. */
> + return realm_destroy_rtt_level(realm, realm->start_level + 1,
> + 0, 1UL << ia_bits);
> +}
> +
> +void kvm_realm_unmap_stage2_all(struct kvm *kvm)
> +{
> + struct realm *realm = &kvm->arch.realm;
> +
> + lockdep_assert_held(&kvm->arch.config_lock);
> +
> + if (realm->stage2_unmapped)
> + return;
> +
> + write_lock(&kvm->mmu_lock);
> + kvm_stage2_unmap_range_filter(&kvm->arch.mmu, 0,
> + BIT(realm->ia_bits - 1), true,
> + KVM_FILTER_PRIVATE);
> + write_unlock(&kvm->mmu_lock);
> +
> + realm->stage2_unmapped = true;
> +}
> +
> 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;
>
> 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);
> @@ -39,6 +207,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;
next prev parent reply other threads:[~2026-12-01 23:17 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 14:25 [PATCH v5 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-11-30 14:25 ` [PATCH v5 01/11] KVM: arm64: guest_memfd: Drop mmap requirement for memslots Steven Price
2026-11-30 15:40 ` Suzuki K Poulose
2026-11-30 15:41 ` Steven Price
2026-11-30 14:25 ` [PATCH v5 02/11] KVM: arm64: CCA: Introduce Realms Steven Price
2026-11-30 14:25 ` [PATCH v5 03/11] KVM: arm64: Pass range filter to stage2_unmap_range Steven Price
2026-11-30 14:25 ` [PATCH v5 04/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
2026-11-30 14:25 ` [PATCH v5 05/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
2026-12-01 16:56 ` Suzuki K Poulose
2026-11-30 14:25 ` [PATCH v5 06/11] KVM: arm64: CCA: Destroy RTTs Steven Price
2026-12-01 23:17 ` Suzuki K Poulose [this message]
2026-11-30 14:25 ` [PATCH v5 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-12-01 23:15 ` Suzuki K Poulose
2026-11-30 14:25 ` [PATCH v5 08/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half Steven Price
2026-12-01 18:39 ` Fuad Tabba
2026-11-30 14:25 ` [PATCH v5 09/11] KVM: arm64: CCA: Advertise private memory support for Realms Steven Price
2026-12-01 23:23 ` Suzuki K Poulose
2026-11-30 14:25 ` [PATCH v5 10/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-12-01 19:55 ` Fuad Tabba
2026-11-30 14:25 ` [PATCH v5 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=78571dbf-2237-462d-b34d-a1045a9250ad@arm.com \
--to=suzuki.poulose@arm.com \
--cc=WeiLin.Chang@arm.com \
--cc=alexandru.elisei@arm.com \
--cc=alpergun@google.com \
--cc=aneesh.kumar@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=christoffer.dall@arm.com \
--cc=enju.kohei@fujitsu.com \
--cc=fj0570is@fujitsu.com \
--cc=gankulkarni@os.amperecomputing.com \
--cc=gshan@redhat.com \
--cc=james.morse@arm.com \
--cc=joey.gouly@arm.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=lpieralisi@kernel.org \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=sdonthineni@nvidia.com \
--cc=steven.price@arm.com \
--cc=tabba@google.com \
--cc=vannapurve@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox