Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
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 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
Date: Tue, 1 Dec 2026 23:15:27 +0000	[thread overview]
Message-ID: <54c43dbb-1f39-4bd9-986c-e4bfe71d118d@arm.com> (raw)
In-Reply-To: <20261130142534.219108-8-steven.price@arm.com>

On 30/11/2026 14:25, Steven Price wrote:
> Add the RMI RTT creation and unmap helpers needed by CCA Realm stage-2
> memory operations. REC exit handling and guest requested RIPAS changes
> follow in the vCPU series.
> 

minor nit :
Can this patch come before the Destroy RTTs ? If so we could use
kvm_realm_unmap_range_filter() directly from 
kvm_realm_stage2_unmap_all() instead of looping through 
kvm_stage2_unmap_range_filter()



> Signed-off-by: Steven Price <steven.price@arm.com>
> 
> ---
> Changes since v4:
> - Drop RTT folding during unmap.
> - Avoid cache topups during shared unmap; use atomic RTT splits and
>    fall back to unmapping the whole block if a split fails.
> 
> Changes since v3:
> - Use the VTCR-derived Realm start level for RTT operations.
> 
> Changes since v2:
> - Use a local MMU cache during notifier invalidation, handle
>    failed RTT splits, and validate RMI levels and cache state.
> 
> Changes since v1:
> - Rework RTT creation and folding around missing or existing RTTs.
>    Use a shared RMI block-size helper and handle alignment, partial
>    progress and errors in range operations.
> ---
>   arch/arm64/include/asm/kvm_rmi.h |   8 +
>   arch/arm64/kvm/rmi.c             | 312 +++++++++++++++++++++++++++++++
>   drivers/firmware/arm_rmm/rmi.c   |  10 -
>   include/linux/arm-rmi-cmds.h     |   8 +
>   4 files changed, 328 insertions(+), 10 deletions(-)
> 
> diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h
> index ec6b06ffc28b..d23ec620195b 100644
> --- a/arch/arm64/include/asm/kvm_rmi.h
> +++ b/arch/arm64/include/asm/kvm_rmi.h
> @@ -82,4 +82,12 @@ int kvm_init_realm(struct kvm *kvm);
>   void kvm_destroy_realm(struct kvm *kvm);
>   void kvm_realm_unmap_stage2_all(struct kvm *kvm);
>   
> +enum kvm_gfn_range_filter;
> +
> +void kvm_realm_unmap_range_filter(struct kvm *kvm,
> +				  unsigned long ipa,
> +				  unsigned long size,
> +				  bool may_block,
> +				  enum kvm_gfn_range_filter attr_filter);
> +
>   #endif /* __ASM_KVM_RMI_H */
> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index 140c7f9ab2c2..4690df0a5d29 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c
> @@ -16,6 +16,76 @@ u32 kvm_rmm_ipa_limit(void)
>   	return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
>   }
>   
> +static int find_map_level(struct realm *realm,
> +			  unsigned long start,
> +			  unsigned long end)
> +{
> +	int start_level = realm->start_level;
> +	int level = KVM_PGTABLE_LAST_LEVEL;
> +
> +	while (level > start_level) {
> +		unsigned long map_size = kvm_granule_size(level - 1);
> +
> +		if (!IS_ALIGNED(start, map_size) ||
> +		    (start + map_size) > end)
> +			break;
> +
> +		level--;
> +	}
> +
> +	return level;
> +}
> +
> +static int undelegate_range_desc(unsigned long desc)
> +{
> +	unsigned long size = rmi_addr_block_size_to_bytes(RMI_ADDR_RANGE_BLOCK_SIZE(desc));
> +	unsigned long count = RMI_ADDR_RANGE_COUNT(desc);
> +	unsigned long addr = RMI_ADDR_RANGE_ADDR(desc);
> +	unsigned long state = RMI_ADDR_RANGE_STATE(desc);
> +
> +	if (state == RMI_OP_MEM_UNDELEGATED)
> +		return 0;
> +
> +	if (size * count == 0)
> +		return 0;
> +
> +	return rmi_undelegate_range(addr, size * count);
> +}
> +
> +static phys_addr_t alloc_delegated_granule(struct kvm_mmu_memory_cache *mc)
> +{
> +	phys_addr_t phys;
> +	void *virt;
> +
> +	if (mc && kvm_mmu_memory_cache_nr_free_objects(mc)) {
> +		virt = kvm_mmu_memory_cache_alloc(mc);
> +	} else {
> +		virt = (void *)__get_free_page(GFP_ATOMIC | __GFP_ZERO |
> +					       __GFP_ACCOUNT);
> +	}
> +
> +	if (!virt)
> +		return PHYS_ADDR_MAX;
> +
> +	phys = virt_to_phys(virt);
> +	if (rmi_delegate_page(phys)) {
> +		free_page((unsigned long)virt);
> +		return PHYS_ADDR_MAX;
> +	}
> +
> +	return phys;
> +}
> +
> +static phys_addr_t alloc_rtt(struct kvm_mmu_memory_cache *mc)
> +{
> +	phys_addr_t phys = alloc_delegated_granule(mc);
> +
> +	if (phys != PHYS_ADDR_MAX)
> +		kvm_account_pgtable_pages(phys_to_virt(phys), 1);
> +
> +	return phys;
> +}
> +
>   static void free_rtt(phys_addr_t phys)
>   {
>   	kvm_account_pgtable_pages(phys_to_virt(phys), -1);
> @@ -23,6 +93,15 @@ static void free_rtt(phys_addr_t phys)
>   		kvm_account_pgtable_pages(phys_to_virt(phys), 1);
>   }
>   
> +static long realm_rtt_create(struct realm *realm,
> +			     unsigned long addr,
> +			     int level,
> +			     phys_addr_t phys)
> +{
> +	addr = ALIGN_DOWN(addr, kvm_granule_size(level - 1));
> +	return rmi_rtt_create(realm->rd_phys, phys, addr, level);
> +}
> +
>   /*
>    * realm_rtt_destroy - Destroy an RTT at @level for @addr.
>    *
> @@ -47,6 +126,47 @@ static long realm_rtt_destroy(struct realm *realm, unsigned long addr,
>   	return ret;
>   }
>   
> +static int realm_create_rtt_levels(struct realm *realm,
> +				   unsigned long ipa,
> +				   int level,
> +				   int max_level,
> +				   struct kvm_mmu_memory_cache *mc)
> +{
> +	long ret = 0;
> +	phys_addr_t rtt = PHYS_ADDR_MAX;
> +
> +	while (level++ < max_level) {
> +		if (rtt == PHYS_ADDR_MAX)
> +			rtt = alloc_rtt(mc);
> +		if (rtt == PHYS_ADDR_MAX)
> +			return -ENOMEM;
> +
> +		ret = realm_rtt_create(realm, ipa, level, rtt);
> +		if (ret < 0)
> +			break;
> +
> +		if (RMI_RESULT_STATUS(ret) == RMI_ERROR_RTT &&
> +		    RMI_RESULT_DATA_LEVEL(ret) == level - 1) {
> +			/* The RTT already exists, reuse the granule at the next level. */
> +			ret = 0;
> +			continue;
> +		}
> +
> +		if (ret) {
> +			WARN(1, "Failed to create RTT at level %d: %ld\n",
> +			     level, ret);
> +			ret = -ENXIO;
> +			break;
> +		}
> +		rtt = PHYS_ADDR_MAX;
> +	}
> +
> +	if (rtt != PHYS_ADDR_MAX)
> +		free_rtt(rtt);
> +
> +	return ret;
> +}
> +
>   static int realm_destroy_rtt_level(struct realm *realm, int level,
>   				   unsigned long start, unsigned long end)
>   {
> @@ -166,6 +286,198 @@ void kvm_realm_unmap_stage2_all(struct kvm *kvm)
>   	realm->stage2_unmapped = true;
>   }
>   
> +static bool realm_unmap_cache_topup(struct kvm *kvm,
> +				    struct kvm_mmu_memory_cache *mc, int needed)
> +{
> +	int ret;
> +
> +	if (kvm_mmu_memory_cache_nr_free_objects(mc) >= needed)
> +		return false;
> +
> +	lockdep_assert_held_write(&kvm->mmu_lock);
> +	write_unlock(&kvm->mmu_lock);
> +	ret = __kvm_mmu_topup_memory_cache(mc, KVM_PGTABLE_LAST_LEVEL, needed);
> +	write_lock(&kvm->mmu_lock);
> +
> +	return !ret;
> +}

...

> +
> +void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start,
> +				  unsigned long size, bool may_block,
> +				  enum kvm_gfn_range_filter attr_filter)
> +{
> +	/*
> +	 * MMU notifier callbacks only invalidate shared mappings, which can
> +	 * fall back to unmapping a larger block if an atomic split fails.
> +	 * Private invalidation uses a local cache instead of the eager split
> +	 * cache protected by slots_lock.
> +	 */
> +	struct kvm_mmu_memory_cache cache = { .gfp_zero = __GFP_ZERO };

Is it better to keep a cache in struct realm that can be used here
instead of the local cache ? That could be reused for RMI_POPULATE
and even SET_RIPAS.


Suzuki


  reply	other threads:[~2026-12-01 23:15 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
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 [this message]
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=54c43dbb-1f39-4bd9-986c-e4bfe71d118d@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