From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796212175; b=3SyJQxIrjuuCDkwVT1tmLRFKAadIEw5JN4eLKi3srEm/uHwJsCXJPX9ERZgmRR+nRnwi 8IW5Qt/k0BOITkAUEIen+ydctuFhLZZjpwJMYlauYIZu0kPs+jkefCdMJZYZRXieYuT4Z LSJLjIIHGqEuKGsQQk8uBNLZvHHz9GUh85D0d6o6H8FdriCOA7rP+PjAwnk2jqp2BhjoO K3mHVHJp4/uP0lSmZbLAhbMtG0oXh2y5hAD8SX9y1FRNeTxU2hI/VbjdM/MqxF470652W tmKM+RZ1XlVTNEp8mcSG+uHf28P0rp8hfFNGv+04owa4VpP6NJ5ki2e3StiJ9cLvT6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796212175; h=DMARC-Filter:Received:Received:DKIM-Signature:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:From: Content-Language:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=1ZvZd51b9TkuCcRjPLm/CJEhFtyGzJOgqKDjOpRkaBA=; b=4OG93t0wY/hPH10VXoWH+xpLEEgNIUX0Sde5kw0hXo0F6aqoPGlz4dD+r1XrlqnX9gxQ FoHlERkl2m1hekt239nlb6F/cf7YI59zi9iG15pt3itUt2m7BL265t5Ux6K7oLQKU6leq NpiG2BIklg/EvlZt1Lerk59IQksMgX+hLcraPyAY9NBywzD9OaA8S0ZX/LK7J9tIRXAeu lFvIxPg1EmKF0dZpVdCotA5o4h6RCLgj7JcsyVsIoEI7vrvlRvW/b8pFzS6qrfO+8Pj9I rvFJCn6E5yAESwWfBcNoAwYlq1RzYovfjx4wM9m0jWMv7XRpOGmWlDJJG50gqe4wMeA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=kgMWl2Aq; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 15132606A31A; Wed, 2 Dec 2026 11:49:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 15132606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=kgMWl2Aq Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1F6F21570; Wed, 2 Dec 2026 03:49:30 -0800 (PST) Received: from [10.57.53.204] (unknown [10.57.53.204]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6B8CF3F7D8; Wed, 2 Dec 2026 03:49:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796212172; bh=NY2nEnQaEtalk+ySl8ixjYr0hNQy/XuXTn9aBEXezNo=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=kgMWl2AqidrRYv5JShYoVp2ZKA0shZtmu8DgF2l4VvJD9hRoDF+2b4W9P0s6bvhh+ HtdS5Ms2wOyS7R39AuRXK0sFGgrtuHlDZfoMIjHBke4/GZ9vyAcvtcXV+gB1oNZNO3 QIZFAneJjBlHDBZpIO/yxKHTUBea27s+gk2lDUG8= Message-ID: Date: Wed, 2 Dec 2026 11:49:25 +0000 X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 06/11] KVM: arm64: CCA: Destroy RTTs To: Suzuki K Poulose , kvm@vger.kernel.org, kvmarm@lists.linux.dev Cc: Catalin Marinas , Marc Zyngier , Will Deacon , James Morse , Oliver Upton , Zenghui Yu , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Joey Gouly , Alexandru Elisei , Christoffer Dall , Fuad Tabba , linux-coco@lists.linux.dev, Ganapatrao Kulkarni , Gavin Shan , Shanker Donthineni , Alper Gun , "Aneesh Kumar K . V" , Emi Kisanuki , Vishal Annapurve , WeiLin.Chang@arm.com, Lorenzo Pieralisi , Kohei Enju References: <20261130142534.219108-1-steven.price@arm.com> <20261130142534.219108-7-steven.price@arm.com> <78571dbf-2237-462d-b34d-a1045a9250ad@arm.com> From: Steven Price Content-Language: en-GB In-Reply-To: <78571dbf-2237-462d-b34d-a1045a9250ad@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 01/12/2026 23:17, Suzuki K Poulose wrote: > 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 >> >> --- >> 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() Ah, yes that's a good change. Thanks for the suggestion. Steve > > 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; >