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 v3 06/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
Date: Mon, 16 Nov 2026 17:21:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-217428@kernel.org> (raw)
In-Reply-To: <20261116164358.142785-7-steven.price@arm.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] KVM: arm64: cca: broken split-failure fallback in
  realm_unmap_*_range()
- [Medium] KVM: arm64: cca: stale err_level after
  realm_unmap_cache_topup() failure
- [Medium] KVM: arm64: cca: get_start_level() misses 2-level minimum on
  64K pages
- [Medium] KVM: arm64: KVM_BUG_ON() on -ENOMEM in
  realm_unmap_private_range()
- [Low] KVM: arm64: cca: possible RTT creation for unmapped entry in
  realm_unmap_*()
--

commit 453fd020abf481912e2fee6d2f403740b481af92
Author: Steven Price <steven.price@arm.com>

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

This commit adds RMI RTT creation, folding, and unmap helpers for CCA
Realm stage-2 shared and private address ranges.

> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index 74b89da4e8015..838f95c201ada 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c

[ ... ]

> @@ -154,12 +291,94 @@ static int realm_tear_down_rtt_range(struct realm *realm,
>  					 start, end);
>  }
>  
> +/*
> + * Returns 0 on successful fold, a negative value on error, a positive value if
> + * we were not able to fold all tables at this level.
> + */
> +static int realm_fold_rtt_level(struct realm *realm, int level,
> +				unsigned long start, unsigned long end)
> +{
> +	int not_folded = 0;
> +	ssize_t map_size;
> +	unsigned long addr, next_addr;
> +
> +	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
> +		return -EINVAL;

[Severity: Medium]
On a CONFIG_ARM64_64K_PAGES kernel, can realm_fold_rtt_level() be called
with level == 4 and trigger this WARN_ON()?

For a 32-bit or 33-bit IPA, stage2_pgtable_levels(realm->ia_bits) returns
1 on 64K pages, so get_start_level() returns 4 - 1 = 3
(KVM_PGTABLE_LAST_LEVEL) even though kvm_get_vtcr() clamps stage-2 tables
to at least 2 levels (start_level = 2).  When realm_unmap_shared_range()
or realm_unmap_private_range() then calls realm_fold_rtt_level(realm,
get_start_level(realm) + 1, start, end), level is 4, triggering the
warning and skipping RTT folding (and similarly in
realm_tear_down_rtt_range()).

[ ... ]

> @@ -189,6 +408,208 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)
>  	return 0;
>  }
>  
> +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;
> +}
> +
> +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 < get_start_level(realm) ||
> +				    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 architecture 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 exists), rather than advancing next_addr across
the unmapped ancestor and returning RMI_SUCCESS?

If yes, unmapping a sub-block range (such as a single page where level is
KVM_PGTABLE_LAST_LEVEL) in an unpopulated region will fall through to
realm_create_rtt_levels() in both realm_unmap_shared_range() and
realm_unmap_private_range(), allocating and delegating intermediate RTTs
only to fold them again at the end of the function, and hitting
KVM_BUG_ON(1, kvm) in realm_unmap_private_range() if that allocation
fails.

> +
> +			if (may_block && realm_unmap_cache_topup(kvm, mc,
> +								 level - err_level)) {
> +				next_addr = addr;
> +				break;
> +			}
> +
> +			ret = realm_create_rtt_levels(realm, addr, err_level,
> +						      level, mc);

[Severity: Medium]
Can realm_create_rtt_levels() run with a stale err_level if
__kvm_mmu_topup_memory_cache() fails inside realm_unmap_cache_topup()?

When the cache has fewer than needed objects and topup fails with
-ENOMEM, realm_unmap_cache_topup() still drops and re-acquires
kvm->mmu_lock before returning false.  If another thread folds or modifies
the RTTs while mmu_lock is dropped, continuing into
realm_create_rtt_levels() with the pre-unlock err_level (in both
realm_unmap_shared_range() and realm_unmap_private_range()) can fail with
RMI_ERROR_RTT at an ancestor level and trigger the WARN() in
realm_create_rtt_levels().

> +			if (!ret) {
> +				/* Retry with the RTT levels in place. */
> +				next_addr = addr;
> +				break;
> +			}
> +			WARN_ON(ret != -ENOMEM);
> +
> +			/* Splitting failed; unmap the entire existing block. */
> +			next_addr = ALIGN_DOWN(addr, kvm_granule_size(err_level));
> +			end = max(end, next_addr + kvm_granule_size(err_level));
> +			break;

[Severity: Medium]
If realm_create_rtt_levels() fails partway through a multi-level split
(for example, after creating a level-2 RTT to split a 1 GiB level-1 block,
then failing with -ENOMEM at level 3), doesn't this fallback use the
initial err_level and widen the unmap to the full 1 GiB block instead of
the 2 MiB block at the last level created?

Also, when next_addr is aligned down below start, start is not updated, so
the realm_fold_rtt_level() call after the loop starts at the original
start and skips folding RTTs in [next_addr, start).  Both issues also
apply to the fallback in realm_unmap_private_range().

[ ... ]

> +static void realm_unmap_private_range(struct kvm *kvm,
> +				      unsigned long start,
> +				      unsigned long end,
> +				      bool may_block,
> +				      struct kvm_mmu_memory_cache *mc)
> +{

[ ... ]

> +			ret = realm_create_rtt_levels(realm, addr, err_level,
> +						      level, mc);
> +			if (!ret) {
> +				next_addr = addr;
> +				break;
> +			}
> +
> +			/* A failed split cannot leave the backing pages mapped. */\n> +			KVM_BUG_ON(1, kvm);

[Severity: Medium]
Can an RTT allocation failure (-ENOMEM) in realm_create_rtt_levels()
trigger a host WARN_ON_ONCE() via KVM_BUG_ON()?

Both __kvm_mmu_topup_memory_cache() and alloc_delegated_granule() use
__GFP_ACCOUNT, so a host VMM hitting its memcg limit or host memory
pressure can cause realm_create_rtt_levels() to return -ENOMEM.  Unlike
realm_unmap_shared_range(), which guards its warning with WARN_ON(ret !=
-ENOMEM), KVM_BUG_ON(1, kvm) unconditionally warns even on -ENOMEM; using
WARN_ON(ret != -ENOMEM) and marking the VM dead or bugged without warning
on allocation failure would avoid a user-triggerable kernel warning.

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

  reply	other threads:[~2026-11-16 17:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 16:43 [PATCH v3 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-11-16 16:43 ` [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot Steven Price
2026-11-16 16:43 ` [PATCH v3 02/11] KVM: arm64: CCA: Introduce Realms Steven Price
2026-11-16 16:43 ` [PATCH v3 03/11] KVM: arm64: Handle attributes in stage2_unmap_range Steven Price
2026-11-16 16:43 ` [PATCH v3 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
2026-11-16 16:43 ` [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs Steven Price
2026-11-16 17:08   ` sashiko-bot
2026-11-16 16:43 ` [PATCH v3 06/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-11-16 17:21   ` sashiko-bot [this message]
2026-11-16 16:43 ` [PATCH v3 07/11] KVM: Allow architectures to reject memslot changes before invalidation Steven Price
2026-11-16 16:43 ` [PATCH v3 08/11] KVM: arm64: CCA: Prevent memslot modifications after Realm creation Steven Price
2026-11-16 16:43 ` [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-11-16 17:31   ` sashiko-bot
2026-11-16 16:43 ` [PATCH v3 10/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
2026-11-16 16:43 ` [PATCH v3 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-217428@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