Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: arm64: nv: Fix guest stage-2 mapping leak on sub-block unmap
@ 2026-11-19 18:51 Colton Lewis
  2026-11-20  6:35 ` Wei-Lin Chang
  2026-11-20 11:13 ` Marc Zyngier
  0 siblings, 2 replies; 3+ messages in thread
From: Colton Lewis @ 2026-11-19 18:51 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton
  Cc: Wei-Lin Chang, Joey Gouly, Suzuki K Poulose, Zenghui Yu,
	Fuad Tabba, Steffen Eiden, kvmarm, linux-arm-kernel, kvm,
	linux-kernel, Colton Lewis

When a shadow stage-2 block mapping (for example, 2M at PMD level) is
partially invalidated by a smaller guest unmap such as a 4K TLBI
IPAS2E1IS, kvm_pgtable_stage2_unmap() shatters and clears the entire
block descriptor in mmu->pgt. However, kvm_remove_guest_s2_mappings()
only removes a struct kvm_guest_s2_mapping node when its entire
[nested_start, nested.last] span lies within the unmap range. As a
result, every sub-block invalidation followed by a re-fault leaks a
stale block-sized tracking node in mmu->guest_s2_mappings and
kvm->arch.mmu.guest_s2_mappings until the shadow MMU is destroyed,
allowing an L1 guest to grow host kernel memory and degrade subsequent
TLBI IPAS2E1IS walks to O(N) under write_lock(&kvm->mmu_lock).

Conversely, unconditionally removing any overlapping tracking node on a
sub-block unmap would be unsafe when a shadow block mapping has been
split into a child table (such as on a page-granule fault under dirty
logging or a racing shared walk returning -EAGAIN), because sibling
sub-page PTEs outside the unmapped range remain live in mmu->pgt and
still rely on the block-sized tracking node for host MMU notifier
unmaps.

Since stage2_unmap_walker() unlinks empty child page tables in
KVM_PGTABLE_WALK_TABLE_POST as soon as their last valid entry is
cleared, check mmu->pgt via kvm_pgtable_get_leaf() on partial-overlap
candidates: when no valid entry or split sub-table remains at or below
the recorded mapping granule (!kvm_pte_valid(pte) &&
kvm_granule_size(level) >= map_size), remove and free the tracking node;
otherwise retain it to cover surviving split sub-entries.

In addition, suppress exact-match duplicate insertions in
kvm_record_guest_s2_mapping() when concurrent faults or -EAGAIN retries
record an identical (nested, canonical) range on the same shadow MMU.

Fixes: 5491980c1a41 ("KVM: arm64: nv: Track guest stage-2 mapping removal")
Fixes: c2778d14cd2f ("KVM: arm64: nv: Track guest stage-2 mapping creation")
Signed-off-by: Colton Lewis <coltonlewis@google.com>
---
 arch/arm64/kvm/nested.c | 68 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 61 insertions(+), 7 deletions(-)

diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 4d83281a7a28b..2b1c85de5b719 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -896,6 +896,30 @@ static unsigned long s2_mapping_to_nested_start(struct kvm_guest_s2_mapping *map
 	return mapping->nested.start & ~S2_MMU_IDX_MASK;
 }
 
+static bool guest_s2_mapping_exists(struct kvm_s2_mmu *mmu,
+				    struct kvm_guest_s2_mapping *mapping)
+{
+	struct interval_tree_node *node;
+
+	for (node = interval_tree_iter_first(&mmu->guest_s2_mappings,
+					     mapping->nested.start,
+					     mapping->nested.last);
+	     node;
+	     node = interval_tree_iter_next(node, mapping->nested.start,
+					    mapping->nested.last)) {
+		struct kvm_guest_s2_mapping *curr;
+
+		curr = container_of(node, struct kvm_guest_s2_mapping, nested);
+		if (curr->nested.start == mapping->nested.start &&
+		    curr->nested.last == mapping->nested.last &&
+		    curr->canonical.start == mapping->canonical.start &&
+		    curr->canonical.last == mapping->canonical.last)
+			return true;
+	}
+
+	return false;
+}
+
 void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,
 				 gpa_t nested_ipa, size_t map_size,
 				 struct kvm_guest_s2_mapping *mapping)
@@ -915,9 +939,32 @@ void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,
 
 	tag_s2_mapping_mmu(mapping, mmu);
 
-	guard(spinlock)(&kvm->arch.guest_s2_tracking_lock);
-	interval_tree_insert(&mapping->nested, &mmu->guest_s2_mappings);
-	interval_tree_insert(&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings);
+	scoped_guard(spinlock, &kvm->arch.guest_s2_tracking_lock) {
+		if (!guest_s2_mapping_exists(mmu, mapping)) {
+			interval_tree_insert(&mapping->nested,
+					     &mmu->guest_s2_mappings);
+			interval_tree_insert(&mapping->canonical,
+					     &kvm->arch.mmu.guest_s2_mappings);
+			mapping = NULL;
+		}
+	}
+
+	kfree(mapping);
+}
+
+static bool guest_s2_mapping_unmapped(struct kvm_s2_mmu *mmu,
+				      gpa_t nested_start, size_t map_size)
+{
+	kvm_pte_t pte;
+	s8 level;
+
+	if (!mmu->pgt)
+		return true;
+
+	if (kvm_pgtable_get_leaf(mmu->pgt, nested_start, &pte, &level, 0))
+		return false;
+
+	return !kvm_pte_valid(pte) && kvm_granule_size(level) >= map_size;
 }
 
 void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa,
@@ -937,17 +984,24 @@ void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa,
 	node = interval_tree_iter_first(&mmu->guest_s2_mappings, nipa, nipa_end);
 	while (node) {
 		unsigned long nested_start;
+		size_t map_size;
 
 		next = interval_tree_iter_next(node, nipa, nipa_end);
 		mapping = container_of(node, struct kvm_guest_s2_mapping,
 				       nested);
 		/*
-		 * Tracking must be conservative on removal, only remove
-		 * mappings that are within the unmap range.
+		 * A sub-block unmap tears down an unsplit shadow block entry
+		 * in mmu->pgt, whereas a split block may still have valid
+		 * sub-page entries outside [nipa, nipa_end]. Remove the
+		 * tracking entry when it lies within the unmap range or no
+		 * mappings remain across its span in mmu->pgt.
 		 */
 		nested_start = s2_mapping_to_nested_start(mapping);
-		if (nipa <= nested_start && nipa_end >= mapping->nested.last) {
-			interval_tree_remove(&mapping->nested, &mmu->guest_s2_mappings);
+		map_size = mapping->nested.last - nested_start + 1;
+		if ((nipa <= nested_start && nipa_end >= mapping->nested.last) ||
+		    guest_s2_mapping_unmapped(mmu, nested_start, map_size)) {
+			interval_tree_remove(&mapping->nested,
+					     &mmu->guest_s2_mappings);
 			interval_tree_remove(&mapping->canonical,
 					     &kvm->arch.mmu.guest_s2_mappings);
 			kfree(mapping);

base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
-- 
2.56.1.809.ga0b61035b5-goog



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] KVM: arm64: nv: Fix guest stage-2 mapping leak on sub-block unmap
  2026-11-19 18:51 [PATCH] KVM: arm64: nv: Fix guest stage-2 mapping leak on sub-block unmap Colton Lewis
@ 2026-11-20  6:35 ` Wei-Lin Chang
  2026-11-20 11:13 ` Marc Zyngier
  1 sibling, 0 replies; 3+ messages in thread
From: Wei-Lin Chang @ 2026-11-20  6:35 UTC (permalink / raw)
  To: Colton Lewis, Marc Zyngier, Oliver Upton
  Cc: Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, kvmarm, linux-arm-kernel, kvm, linux-kernel

On Thu, Nov 19, 2026 at 06:51:05PM +0000, Colton Lewis wrote:
> When a shadow stage-2 block mapping (for example, 2M at PMD level) is
> partially invalidated by a smaller guest unmap such as a 4K TLBI
> IPAS2E1IS, kvm_pgtable_stage2_unmap() shatters and clears the entire

"shatters" sounds like it breaks the block into page mappings, so maybe
just "clears the entire block.."?

> block descriptor in mmu->pgt. However, kvm_remove_guest_s2_mappings()

nit: I think "the shadow page table" is clearer than "mmu->pgt"

> only removes a struct kvm_guest_s2_mapping node when its entire
> [nested_start, nested.last] span lies within the unmap range. As a
> result, every sub-block invalidation followed by a re-fault leaks a

Doesn't the block tracking node get leaked regardless of whether a
re-fault happens?

> stale block-sized tracking node in mmu->guest_s2_mappings and
> kvm->arch.mmu.guest_s2_mappings until the shadow MMU is destroyed,
> allowing an L1 guest to grow host kernel memory and degrade subsequent
> TLBI IPAS2E1IS walks to O(N) under write_lock(&kvm->mmu_lock).
> 
> Conversely, unconditionally removing any overlapping tracking node on a
> sub-block unmap would be unsafe when a shadow block mapping has been
> split into a child table (such as on a page-granule fault under dirty
> logging or a racing shared walk returning -EAGAIN), because sibling
> sub-page PTEs outside the unmapped range remain live in mmu->pgt and

What are sub-page PTEs? :P

> still rely on the block-sized tracking node for host MMU notifier
> unmaps.
> 
> Since stage2_unmap_walker() unlinks empty child page tables in
> KVM_PGTABLE_WALK_TABLE_POST as soon as their last valid entry is
> cleared, check mmu->pgt via kvm_pgtable_get_leaf() on partial-overlap
> candidates: when no valid entry or split sub-table remains at or below
 
You use "or" here.                ^^

> the recorded mapping granule (!kvm_pte_valid(pte) &&

but this is an AND?                                 ^^
so I don't quite understand.

> kvm_granule_size(level) >= map_size), remove and free the tracking node;
> otherwise retain it to cover surviving split sub-entries.

"sub-entries" doesn't really make sense to me..
Maybe "... to cover other entries within the range"?

For this third paragraph can you improve it by stating how the problem
is fixed upfront? The first two paragraphs describe the problem, then
you started to talk about stage2_unmap_walker all of a sudden (which I'm
not sure how much it helps), and the fix itself is not emphasised. The
combination makes it pretty difficult to get what the patch is doing.

> 
> In addition, suppress exact-match duplicate insertions in
> kvm_record_guest_s2_mapping() when concurrent faults or -EAGAIN retries
> record an identical (nested, canonical) range on the same shadow MMU.

I believe this is better split into its own patch, it's doing another
thing.

> 
> Fixes: 5491980c1a41 ("KVM: arm64: nv: Track guest stage-2 mapping removal")
> Fixes: c2778d14cd2f ("KVM: arm64: nv: Track guest stage-2 mapping creation")

Since there isn't a real bug in the original implementation do we need
these tags?

> Signed-off-by: Colton Lewis <coltonlewis@google.com>
> ---
>  arch/arm64/kvm/nested.c | 68 ++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 61 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
> index 4d83281a7a28b..2b1c85de5b719 100644
> --- a/arch/arm64/kvm/nested.c
> +++ b/arch/arm64/kvm/nested.c
> @@ -896,6 +896,30 @@ static unsigned long s2_mapping_to_nested_start(struct kvm_guest_s2_mapping *map
>  	return mapping->nested.start & ~S2_MMU_IDX_MASK;
>  }
>  
> +static bool guest_s2_mapping_exists(struct kvm_s2_mmu *mmu,
> +				    struct kvm_guest_s2_mapping *mapping)
> +{
> +	struct interval_tree_node *node;
> +
> +	for (node = interval_tree_iter_first(&mmu->guest_s2_mappings,
> +					     mapping->nested.start,
> +					     mapping->nested.last);
> +	     node;
> +	     node = interval_tree_iter_next(node, mapping->nested.start,
> +					    mapping->nested.last)) {
> +		struct kvm_guest_s2_mapping *curr;
> +
> +		curr = container_of(node, struct kvm_guest_s2_mapping, nested);
> +		if (curr->nested.start == mapping->nested.start &&
> +		    curr->nested.last == mapping->nested.last &&
> +		    curr->canonical.start == mapping->canonical.start &&
> +		    curr->canonical.last == mapping->canonical.last)
> +			return true;
> +	}
> +
> +	return false;
> +}
> +
>  void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,
>  				 gpa_t nested_ipa, size_t map_size,
>  				 struct kvm_guest_s2_mapping *mapping)
> @@ -915,9 +939,32 @@ void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,
>  
>  	tag_s2_mapping_mmu(mapping, mmu);
>  
> -	guard(spinlock)(&kvm->arch.guest_s2_tracking_lock);
> -	interval_tree_insert(&mapping->nested, &mmu->guest_s2_mappings);
> -	interval_tree_insert(&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings);
> +	scoped_guard(spinlock, &kvm->arch.guest_s2_tracking_lock) {
> +		if (!guest_s2_mapping_exists(mmu, mapping)) {
> +			interval_tree_insert(&mapping->nested,
> +					     &mmu->guest_s2_mappings);
> +			interval_tree_insert(&mapping->canonical,
> +					     &kvm->arch.mmu.guest_s2_mappings);
> +			mapping = NULL;
> +		}
> +	}
> +
> +	kfree(mapping);

In my understanding kfree() can be called while holding locks, so we
don't need the extra level of indentation.

> +}
> +
> +static bool guest_s2_mapping_unmapped(struct kvm_s2_mmu *mmu,
> +				      gpa_t nested_start, size_t map_size)
> +{
> +	kvm_pte_t pte;
> +	s8 level;
> +
> +	if (!mmu->pgt)
> +		return true;
> +
> +	if (kvm_pgtable_get_leaf(mmu->pgt, nested_start, &pte, &level, 0))
> +		return false;
> +
> +	return !kvm_pte_valid(pte) && kvm_granule_size(level) >= map_size;
>  }
>  
>  void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa,
> @@ -937,17 +984,24 @@ void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa,
>  	node = interval_tree_iter_first(&mmu->guest_s2_mappings, nipa, nipa_end);
>  	while (node) {
>  		unsigned long nested_start;
> +		size_t map_size;
>  
>  		next = interval_tree_iter_next(node, nipa, nipa_end);
>  		mapping = container_of(node, struct kvm_guest_s2_mapping,
>  				       nested);
>  		/*
> -		 * Tracking must be conservative on removal, only remove
> -		 * mappings that are within the unmap range.
> +		 * A sub-block unmap tears down an unsplit shadow block entry
> +		 * in mmu->pgt, whereas a split block may still have valid

Does "split block" make sense? Either it is a block mapping or a page
mapping.

> +		 * sub-page entries outside [nipa, nipa_end]. Remove the

Same "sub-page entries" wording issue here.

Thanks,
Wei-Lin Chang

> +		 * tracking entry when it lies within the unmap range or no
> +		 * mappings remain across its span in mmu->pgt.
>  		 */
>  		nested_start = s2_mapping_to_nested_start(mapping);
> -		if (nipa <= nested_start && nipa_end >= mapping->nested.last) {
> -			interval_tree_remove(&mapping->nested, &mmu->guest_s2_mappings);
> +		map_size = mapping->nested.last - nested_start + 1;
> +		if ((nipa <= nested_start && nipa_end >= mapping->nested.last) ||
> +		    guest_s2_mapping_unmapped(mmu, nested_start, map_size)) {
> +			interval_tree_remove(&mapping->nested,
> +					     &mmu->guest_s2_mappings);
>  			interval_tree_remove(&mapping->canonical,
>  					     &kvm->arch.mmu.guest_s2_mappings);
>  			kfree(mapping);
> 
> base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
> -- 
> 2.56.1.809.ga0b61035b5-goog
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] KVM: arm64: nv: Fix guest stage-2 mapping leak on sub-block unmap
  2026-11-19 18:51 [PATCH] KVM: arm64: nv: Fix guest stage-2 mapping leak on sub-block unmap Colton Lewis
  2026-11-20  6:35 ` Wei-Lin Chang
@ 2026-11-20 11:13 ` Marc Zyngier
  1 sibling, 0 replies; 3+ messages in thread
From: Marc Zyngier @ 2026-11-20 11:13 UTC (permalink / raw)
  To: Colton Lewis
  Cc: Oliver Upton, Wei-Lin Chang, Joey Gouly, Suzuki K Poulose,
	Zenghui Yu, Fuad Tabba, Steffen Eiden, kvmarm, linux-arm-kernel,
	kvm, linux-kernel

On Thu, 19 Nov 2026 18:51:05 +0000,
Colton Lewis <coltonlewis@google.com> wrote:
> 
> When a shadow stage-2 block mapping (for example, 2M at PMD level) is
> partially invalidated by a smaller guest unmap such as a 4K TLBI
> IPAS2E1IS,

What does that mean? If we have a 2MB shadow mapping, then the guest
cannot have something smaller in its own PTs. And how do you get a "4K
TLBI IPAS2E1IS"? TTL == {4kB, L3}? If that's the case, we have every
right to *ignore* this TLBI, because it cannot possibly reflect the
original state of the PTs. If this is an unqualified address (no TTL),
then the whole block *must* go.

There is no such thing as "partial invalidation".

> kvm_pgtable_stage2_unmap() shatters and clears the entire
> block descriptor in mmu->pgt.

Shatters? Or clears the entire block? It cannot be "and" because the
two are polar opposites.

> However, kvm_remove_guest_s2_mappings()
> only removes a struct kvm_guest_s2_mapping node when its entire
> [nested_start, nested.last] span lies within the unmap range.

What do you call "the unmap range"? What we have removed from the
shadow S2? What the guest gives us as part of the TLBI instruction?
Something else?

> As a
> result, every sub-block invalidation followed by a re-fault leaks a
> stale block-sized tracking node in mmu->guest_s2_mappings and
> kvm->arch.mmu.guest_s2_mappings until the shadow MMU is destroyed,

Leaks? A leak is something that is still allocated, but not linked to
an existing data structure, and therefore not recoverable. This looks
to me like exact opposite of a leak...

> allowing an L1 guest to grow host kernel memory and degrade subsequent
> TLBI IPAS2E1IS walks to O(N) under write_lock(&kvm->mmu_lock).

Honestly, this wall of text is not an explanation. That's just a whole
lot of LLM gunk that you have generously copy pasted.

>
> Conversely, unconditionally removing any overlapping tracking node on a
> sub-block unmap would be unsafe when a shadow block mapping has been
> split into a child table (such as on a page-granule fault under dirty
> logging or a racing shared walk returning -EAGAIN), because sibling
> sub-page PTEs outside the unmapped range remain live in mmu->pgt and
> still rely on the block-sized tracking node for host MMU notifier
> unmaps.

-ENOPARSE. Sub-page PTEs sounds like a great idea.

I stopped reading here. Please come back with an actual description of
the problem that you will have produced yourself, or provide the
original LLM report (it cannot be much worse than the above).

	M.

-- 
Jazz isn't dead. It just smells funny.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-20 11:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 18:51 [PATCH] KVM: arm64: nv: Fix guest stage-2 mapping leak on sub-block unmap Colton Lewis
2026-11-20  6:35 ` Wei-Lin Chang
2026-11-20 11:13 ` Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox