mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Price <steven.price@arm.com>
To: Suzuki K Poulose <suzuki.poulose@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 v2 07/12] KVM: arm64: CCA: Tear down RTTs
Date: Fri, 13 Nov 2026 16:40:12 +0000	[thread overview]
Message-ID: <adc28bdc-ced8-4627-8ec7-23b767e7d445@arm.com> (raw)
In-Reply-To: <020d220a-6f21-46a9-a0d6-51e56a7b3a5a@arm.com>

On 13/11/2026 16:26, Suzuki K Poulose wrote:
> On 13/11/2026 15:05, Steven Price wrote:
>> On 13/11/2026 01:04, Suzuki K Poulose wrote:
>>> On 11/11/2026 14:57, 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, tearing down 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).
>>>>
>>>> Tear down a created Realm whether it is still new or has been
>>>> activated.
>>>> Unmap the Realm IPA space once during stage-2 teardown. Defer
>>>> destruction of non-root RTTs until final Realm destruction, after
>>>> REALM_TERMINATE and before destroying the RD; root RTTs remain until
>>>> the
>>>> RD has gone away.
>>>>
>>>> Signed-off-by: Steven Price <steven.price@arm.com>
>>>>
> 
> ...
> 
>>>>      #endif /* __ASM_KVM_RMI_H */
>>>> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
>>>> index 9df3f8d5b4ef..be014f6c00e7 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;
>>>>    }
> 
> ...
> 
>>>>    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);
>>>
>>> We added kvm_free_stage2_pgd() from kvm_destroy_realm() for Realms in
>>> the previous patch. Should we move that here since, this is where we
>>>
>>> 1. Deal with tearing down stage2
>>> 2. And more importantly we are skipping it for Realms.
>>
>> I'm not quite sure what you're asking for here. We can't free the stage2
>> PGD while the realm is existing (because the RMM has the pages) - so we
>> defer it.
> 
> I understand. My point is, we are deferring the stage2 free for Realm
> in this patch, without explicitly showing where it is done. We have
> introduced that as part of kvm_destroy_realm() in Patch 6.
> 
> My recommendation is to add kvm_free_stage2_pgd() to kvm_destroy_realm()
> in this patch, where we are showing when this is deferred to. e.g.,
> this patch functionally leaves the reader with no clue when this is
> actually freed.
> 
> Does that make sense ?

Ah, yes move the code between patches - I thought you were suggesting
somehow moving the code between functions (hence I was very confused).

Yes, I can move the kvm_destroy_realm() code changes into this patch.

Thanks,
Steve

> 
>>
>>>>        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 69c628cfb6c7..b904e6541fb0 100644
>>>> --- a/arch/arm64/kvm/rmi.c
>>>> +++ b/arch/arm64/kvm/rmi.c
>>>> @@ -11,16 +11,193 @@
>>>>    #include <asm/kvm_pgtable.h>
>>>>    #include <asm/virt.h>
>>>>    +static inline unsigned long rmi_rtt_level_mapsize(int level)
>>>> +{
>>>> +    if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
>>>> +        return PAGE_SIZE;
>>>> +
>>>> +    return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
>>>> +}
>>>> +
>>>
>>> We agreed to use kvm_granule_size() instead of this ?
>>>
>>> https://lore.kernel.org/all/cb183290-3fee-4535-aa3e-5cc172c2323a@arm.com
>>
>> That was a screw-up when I was squashing fixes back in - the next patch
>> removes the code. I'll fix this up in the next version so it never
>> appears. Thanks for pointing it out.
>>
>>>>    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)
>>>> +{
>>>> +    return 4 - max(2, stage2_pgtable_levels(realm->ia_bits));
>>>
>>> I don't think we need this "max()". ia_bits is guaranteed to be
>>>> = 32bit (ARM64_MIN_PARANGE_BITS, see kvm_init_ipa_range()), implying
>>> that we have at least 2 levels on all page sizes.
>>
>> Ah, very true - Sashiko complained, but I hadn't noticed
>> ARM64_MIN_PARANGE_BITS provided an effective minimum. I'll remove the
>> max() and add a comment.
>>
>>>> +}
>>>> +
>>>> +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);
>>>
>>> Is there a reason why remove the account before freeing ? Could we do
>>> this after we have reclaimed it ?
>>
>> Sashiko again:
>>
>>> Does calling kvm_account_pgtable_pages() after rmi_free_delegated_page()
>>> access the page after it has already been freed and uncharged from its
>>> memcg?
>>
>> Basically the problem is that rmi_free_delegated_page() frees the page
>> back to the allocator. So doing the account after freeing means that
>> technically the page could have been reused.
> 
> Thanks, for the explanation, that makes sense.
> 
> Cheers
> Suzuki
> 


  reply	other threads:[~2026-11-13 16:40 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 14:57 [PATCH v2 00/12] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-11-11 14:57 ` [PATCH v2 01/12] KVM: arm64: CCA: Reserve the RMM mode Steven Price
2026-11-11 14:57 ` [PATCH v2 02/12] KVM: arm64: guest_memfd: Check mmap support when preparing memslot Steven Price
2026-11-11 15:21   ` Suzuki K Poulose
2026-11-12 10:43     ` Steven Price
2026-11-12 16:35       ` Suzuki K Poulose
2026-11-12 16:57         ` Suzuki K Poulose
2026-11-11 14:57 ` [PATCH v2 03/12] KVM: arm64: CCA: Introduce Realms Steven Price
2026-11-11 14:57 ` [PATCH v2 04/12] KVM: arm64: CCA: Check RMI support and features at init Steven Price
2026-11-12 16:39   ` Suzuki K Poulose
2026-11-13 11:49     ` Steven Price
2026-11-11 14:57 ` [PATCH v2 05/12] KVM: arm64: Handle attributes in stage2_unmap_range Steven Price
2026-11-11 14:57 ` [PATCH v2 06/12] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
2026-11-11 14:57 ` [PATCH v2 07/12] KVM: arm64: CCA: Tear down RTTs Steven Price
2026-11-13  1:04   ` Suzuki K Poulose
2026-11-13 15:05     ` Steven Price
2026-11-13 16:26       ` Suzuki K Poulose
2026-11-13 16:40         ` Steven Price [this message]
2026-11-11 14:57 ` [PATCH v2 08/12] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-11-11 14:57 ` [PATCH v2 09/12] KVM: Allow architectures to reject memslot changes before invalidation Steven Price
2026-11-11 14:57 ` [PATCH v2 10/12] KVM: arm64: CCA: Prevent memslot modifications after Realm creation Steven Price
2026-11-11 14:57 ` [PATCH v2 11/12] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-11-11 14:57 ` [PATCH v2 12/12] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price
2026-11-12  9:08 ` [PATCH v2 00/12] KVM: arm64: CCA: Prepare Realm memory management Kohei Enju
2026-11-12  9:41   ` 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=adc28bdc-ced8-4627-8ec7-23b767e7d445@arm.com \
    --to=steven.price@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=suzuki.poulose@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

all inboxes | Powered by JetHome®