Kernel KVM virtualization development
 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 v4 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
Date: Mon, 23 Nov 2026 11:12:46 +0000	[thread overview]
Message-ID: <sashiko-outbox-229102@kernel.org> (raw)
In-Reply-To: <20261123104233.95710-8-steven.price@arm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] KVM: arm64: cca: direct-reclaim allocation in
  realm_unmap_cache_topup()
- [Medium] KVM: arm64: cca: stale err_level used after
  realm_unmap_cache_topup() failure
- [Low] KVM: arm64: cca: possible RTT creation for unmapped entry in
  realm_unmap_*()
--

commit 4c7e030072ac67c12f0f2603fad3d39821c47426
Author: Steven Price <steven.price@arm.com>

KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

This commit adds RMI Realm Translation Table creation, folding, and stage-2
unmapping helpers for CCA Realms, including filtered range unmapping for
shared and private memory.

> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index 435174ac3d87f..b34f5320b50b6 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c

[ ... ]

> @@ -172,6 +391,208 @@ 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 (!mc || 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;
> +}

[Severity: Medium]
When __kvm_mmu_topup_memory_cache() fails, does returning false cause
realm_unmap_shared_range() and realm_unmap_private_range() to pass a stale
err_level to realm_create_rtt_levels()?

Because kvm->mmu_lock was dropped and re-acquired, another thread can fold
the RTT at err_level before the lock is retaken.  The callers only retry
from addr when realm_unmap_cache_topup() returns true; on failure they fall
through to realm_create_rtt_levels(realm, addr, err_level, level, mc).
alloc_rtt() can still succeed from a partially topped-up cache or via
alloc_delegated_granule()'s GFP_ATOMIC fallback (which sets __GFP_HIGH and
force-charges memcg), causing realm_rtt_create() at err_level + 1 to fail
with RMI_ERROR_RTT at an ancestor level < err_level.  That triggers the
WARN() in realm_create_rtt_levels() and returns -ENXIO, hitting
WARN_ON(ret != -ENOMEM) in realm_unmap_shared_range() or KVM_BUG_ON(1, kvm)
in realm_unmap_private_range().

> +static void realm_unmap_shared_range(struct kvm *kvm,
> +				     unsigned long start,
> +				     unsigned long end,
> +				     bool may_block,
> +				     struct kvm_mmu_memory_cache *mc)
> +{
> +	struct realm *realm = &kvm->arch.realm;
> +	unsigned long rd = realm->rd_phys;
> +	unsigned long next_addr, addr;
> +	unsigned long shared_bit = BIT(realm->ia_bits - 1);
> +
> +	if (start >= end)
> +		return;
> +
> +	/*
> +	 * Callers may pass either an IPA range or its shared alias. Add the
> +	 * shared bit only for the former. Use addition so an exclusive end at
> +	 * shared_bit advances to the end of the IPA space.
> +	 */
> +	if (start < shared_bit) {
> +		start += shared_bit;
> +		end += shared_bit;
> +	}
> +
> +	for (addr = start; addr < end; addr = next_addr) {
> +		long ret;
> +
> +		ret = rmi_rtt_unprot_unmap(rd, addr, end, RMI_ADDR_TYPE_NONE,
> +					   0, &next_addr, NULL, NULL);
> +		if (WARN_ON(ret < 0))
> +			return;
> +
> +		switch (RMI_RESULT_STATUS(ret)) {
> +		case RMI_SUCCESS:
> +			break;
> +		case RMI_ERROR_RTT: {
> +			int err_level = RMI_RESULT_DATA_LEVEL(ret);
> +			int level = find_map_level(realm, addr, end);
> +
> +			if (WARN_ON(err_level < realm->start_level ||
> +				    err_level > KVM_PGTABLE_LAST_LEVEL))
> +				return;
> +
> +			if (err_level >= level) {
> +				/* Nothing present, so skip */
> +				next_addr = ALIGN(addr + 1,
> +						  kvm_granule_size(err_level));
> +				break;
> +			}

[Severity: Low]
This question rests on an RMM architectural detail that could not be
confirmed from the kernel source: can SMC_RMI_RTT_UNPROT_UNMAP or
SMC_RMI_RTT_DATA_UNMAP return RMI_ERROR_RTT with err_level < level when the
RTT walk stops at an unassigned ancestor entry (where neither a child RTT
nor a block mapping is present at err_level), rather than advancing
next_addr and returning RMI_SUCCESS?

If so, when unmapping a sub-block range (such as a single page, where
find_map_level() returns KVM_PGTABLE_LAST_LEVEL) across an unmapped ancestor
entry, both realm_unmap_shared_range() and realm_unmap_private_range() only
skip when err_level >= level.  For err_level < level, they treat the entry
as a block mapping to split, allocating and delegating intermediate RTTs via
realm_create_rtt_levels() only to fold them back at the end of the function,
and if that allocation fails in realm_unmap_private_range() (for example
under GFP_ATOMIC when may_block is false), it hits KVM_BUG_ON(1, kvm) even
though nothing was mapped.

[ ... ]

> +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 cannot take slots_lock, which protects the
> +	 * eager split cache. Use a cache owned by this invalidation instead.
> +	 */
> +	struct kvm_mmu_memory_cache cache = { .gfp_zero = __GFP_ZERO };

[Severity: Medium]
Can topping up this cache from an MMU notifier callback enter direct reclaim
and trigger a lockdep splat on __mmu_notifier_invalidate_range_start_map?

When mmu_notifier_invalidate_range_start() invokes
kvm_mmu_notifier_invalidate_range_start() -> ... ->
kvm_realm_unmap_range_filter() with may_block set, it holds
__mmu_notifier_invalidate_range_start_map.  Because cache.gfp_custom is left
at 0, realm_unmap_cache_topup() -> __kvm_mmu_topup_memory_cache() allocates
with GFP_KERNEL_ACCOUNT (__GFP_DIRECT_RECLAIM | __GFP_FS), which enters
fs_reclaim_acquire() and acquires __mmu_notifier_invalidate_range_start_map
recursively (and risks reclaim recursion deadlock under memory pressure).
Setting cache.gfp_custom to GFP_NOWAIT | __GFP_ACCOUNT avoids direct reclaim
in the notifier path.

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

  reply	other threads:[~2026-11-23 11:12 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 10:42 [PATCH v4 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-11-23 10:42 ` [PATCH v4 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot Steven Price
2026-11-23 11:04   ` sashiko-bot
2026-11-23 10:42 ` [PATCH v4 02/11] KVM: arm64: CCA: Introduce Realms Steven Price
2026-11-23 10:42 ` [PATCH v4 03/11] KVM: arm64: Pass range filter to stage2_unmap_range Steven Price
2026-11-23 10:42 ` [PATCH v4 04/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
2026-11-23 11:23   ` Suzuki K Poulose
2026-11-23 10:42 ` [PATCH v4 05/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
2026-11-23 10:42 ` [PATCH v4 06/11] KVM: arm64: CCA: Destroy RTTs Steven Price
2026-11-23 11:00   ` sashiko-bot
2026-11-23 10:42 ` [PATCH v4 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-11-23 11:12   ` sashiko-bot [this message]
2026-11-23 10:42 ` [PATCH v4 08/11] KVM: Allow architectures to reject memslot changes before invalidation Steven Price
2026-11-23 10:42 ` [PATCH v4 09/11] KVM: arm64: CCA: Prevent memslot modifications after Realm creation Steven Price
2026-11-23 10:42 ` [PATCH v4 10/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-11-23 11:14   ` sashiko-bot
2026-11-23 10:42 ` [PATCH v4 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-229102@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox