All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steven Price <steven.price@arm.com>
To: sashiko-reviews@lists.linux.dev, kvm@vger.kernel.org,
	kvmarm@lists.linux.dev
Cc: Oliver Upton <oupton@kernel.org>, Marc Zyngier <maz@kernel.org>
Subject: Re: [PATCH v5 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
Date: Thu, 3 Dec 2026 09:49:21 +0000	[thread overview]
Message-ID: <3bf953e1-04b7-4eba-81e8-5fe042a68e2e@arm.com> (raw)
In-Reply-To: <sashiko-outbox-241438@kernel.org>

On 30/11/2026 14:53, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] KVM: arm64: cca: stale err_level and KVM_BUG_ON() on OOM in
>   private unmap
> --
> 
> commit ecf125967f6f8c4f0a2799914ef908db3d675dea
> Author: Steven Price <steven.price@arm.com>
> 
> KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
> 
> This commit adds RMI Realm Translation Table (RTT) creation and shared/private
> range unmap helpers for CCA Realm stage-2 memory operations.
> 
>> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
>> index 140c7f9ab2c20..4690df0a5d29a 100644
>> --- a/arch/arm64/kvm/rmi.c
>> +++ b/arch/arm64/kvm/rmi.c
> 
> [ ... ]
> 
>> @@ -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;
>> +}
>> +
>> +static void realm_unmap_shared_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) {
>> +				/* 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;
>> +		}
>> +		default:
>> +			WARN_ON(1);
>> +			return;
>> +		}
>> +
>> +		if (may_block)
>> +			cond_resched_rwlock_write(&kvm->mmu_lock);
>> +	}
>> +}
>> +
>> +static void realm_unmap_private_range(struct kvm *kvm,
>> +				      unsigned long start,
>> +				      unsigned long end,
>> +				      bool may_block,
>> +				      struct kvm_mmu_memory_cache *mc)
>> +{
> 
> [ ... ]
> 
>> +		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 this RTT entry. */
>> +				next_addr = ALIGN(addr + 1,
>> +						  kvm_granule_size(err_level));
>> +				break;
>> +			}
>> +
>> +			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);
>> +			if (!ret) {
>> +				next_addr = addr;
>> +				break;
>> +			}
>> +
>> +			/* A failed split cannot leave the backing pages mapped. */
>> +			KVM_BUG_ON(1, kvm);
>> +			next_addr = ALIGN_DOWN(addr, kvm_granule_size(err_level));
>> +			end = max(end, next_addr + kvm_granule_size(err_level));
>> +			break;
> 
> [Severity: Medium]
> Does an allocation failure during an RTT split in realm_unmap_private_range()
> cause a stale err_level to be used and trigger a host kernel warning via
> KVM_BUG_ON(1, kvm)?
> 
> When realm_unmap_cache_topup() needs to allocate objects, it drops and
> re-acquires kvm->mmu_lock around __kvm_mmu_topup_memory_cache(), which uses
> GFP_KERNEL_ACCOUNT.  If that allocation fails (for example under a memory
> cgroup limit), realm_unmap_cache_topup() returns false even though
> kvm->mmu_lock was released, so realm_unmap_private_range() proceeds directly
> to realm_create_rtt_levels() with the pre-unlock err_level instead of
> retrying rmi_rtt_data_unmap().
> 
> If realm_create_rtt_levels() then fails with -ENOMEM, KVM_BUG_ON(1, kvm)
> emits a host WARN_ON_ONCE() splat on a __GFP_ACCOUNT allocation failure
> instead of warning only when ret != -ENOMEM and marking the VM dead with
> kvm_vm_dead(kvm).
> 
> Also, if realm_create_rtt_levels() creates an intermediate RTT level before
> failing at a deeper level during a multi-level split, the fallback in both
> realm_unmap_private_range() and realm_unmap_shared_range() widens next_addr
> and end using kvm_granule_size(err_level) rather than the granule size of
> the last level reached.
> 

I can see Sashiko is going to keep complaining about this...

While I don't agree this is actually a problem, I don't really like the 
return value from realm_unmap_cache_topup(). So I propose the following 
diff (on top of this patch) which adds a "lock_dropped" output flag and 
retries the walk in that case. This also adds a "try_topup" flag so we 
don't get stuck in an infinite loop if the memory isn't available.

----8<-----
diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
index 1345e35ea206..fc55bc25b762 100644
--- a/arch/arm64/kvm/rmi.c
+++ b/arch/arm64/kvm/rmi.c
@@ -331,20 +331,23 @@ 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)
+static int realm_unmap_cache_topup(struct kvm *kvm,
+				   struct kvm_mmu_memory_cache *mc,
+				   unsigned int needed, bool *lock_dropped)
 {
 	int ret;
 
+	*lock_dropped = false;
 	if (kvm_mmu_memory_cache_nr_free_objects(mc) >= needed)
-		return false;
+		return 0;
 
 	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;
+	*lock_dropped = true;
+	return ret;
 }
 
 static void realm_unmap_shared_range(struct kvm *kvm,
@@ -542,6 +545,7 @@ static void realm_unmap_private_range(struct kvm *kvm,
 	struct realm *realm = &kvm->arch.realm;
 	unsigned long rd = realm->rd_phys;
 	unsigned long next_addr, addr;
+	bool try_topup = may_block;
 	long ret;
 
 	for (addr = start; addr < end; addr = next_addr) {
@@ -575,10 +579,18 @@ static void realm_unmap_private_range(struct kvm *kvm,
 				break;
 			}
 
-			if (may_block && realm_unmap_cache_topup(kvm, mc,
-								 level - err_level)) {
-				next_addr = addr;
-				break;
+			if (try_topup) {
+				bool lock_dropped;
+
+				if (realm_unmap_cache_topup(kvm, mc,
+							    level - err_level,
+							    &lock_dropped))
+					try_topup = false;
+				if (lock_dropped) {
+					/* Retry the RTT walk with mmu_lock held. */
+					next_addr = addr;
+					break;
+				}
 			}
 
 			ret = realm_create_rtt_levels(realm, addr, err_level,


  reply	other threads:[~2026-12-03  9:49 UTC|newest]

Thread overview: 31+ 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 14:58   ` sashiko-bot
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-12-02 11:49     ` Steven Price
2026-11-30 14:25 ` [PATCH v5 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-11-30 14:53   ` sashiko-bot
2026-12-03  9:49     ` Steven Price [this message]
2026-12-01 23:15   ` Suzuki K Poulose
2026-12-02 11:49     ` Steven Price
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-11-30 14:58   ` sashiko-bot
2026-12-03  9:35     ` Steven Price
2026-12-03 10:32     ` Suzuki K Poulose
2026-12-01 19:55   ` Fuad Tabba
2026-12-03  9:49     ` Steven Price
2026-11-30 14:25 ` [PATCH v5 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price
2026-12-02  4:45 ` [PATCH v5 00/11] KVM: arm64: CCA: Prepare Realm memory management Kohei Enju
2026-12-03  9:24   ` 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=3bf953e1-04b7-4eba-81e8-5fe042a68e2e@arm.com \
    --to=steven.price@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.