mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/2] KVM: arm64: nv: Fix stale and duplicate guest S2 tracking entries
@ 2026-12-02  0:13 Colton Lewis
  2026-12-02  0:13 ` [PATCH v3 1/2] KVM: arm64: nv: Remove unmapped guest stage-2 mapping entries Colton Lewis
  2026-12-02  0:13 ` [PATCH v3 2/2] KVM: arm64: nv: Avoid duplicate " Colton Lewis
  0 siblings, 2 replies; 3+ messages in thread
From: Colton Lewis @ 2026-12-02  0:13 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton
  Cc: Wei-Lin Chang, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, kvmarm, linux-arm-kernel, kvm, linux-kernel

When kvm_pgtable_stage2_unmap_tracked() is called with a size smaller
than a tracked block mapping (such as from a TLBI RIPAS2E1IS range or a
TLBI IPAS2E1IS with a 4kB TTL hint on a 2MB block),
kvm_pgtable_stage2_unmap() clears the entire block in the shadow stage-2
page table while kvm_remove_guest_s2_mappings() leaves its stale
tracking entry behind in guest_s2_mappings.

Patch 1 checks nested_start in the shadow page table with
kvm_pgtable_get_leaf() on partial overlaps in
kvm_remove_guest_s2_mappings() and removes the tracking entry once no
valid PTE or child table remains at its tracked granule size, leaving
compute_tlb_inval_range() untouched so this series is independent of
Marc's sub-page TTL fix [1].

Patch 2 avoids inserting identical duplicate entries in
kvm_record_guest_s2_mapping().

Tool-generated content:
- Initial bug discovery: automated LLM-assisted audit of the arm64
  nested stage-2 tracking code.
- Patch and text drafting: interactive Gemini session (patches, commit
  messages, and cover letter).

Changes since v2:
- Drop the compute_tlb_inval_range() changes and return to the v1
  kvm_remove_guest_s2_mappings() approach (Marc Zyngier).
- Simplify the Patch 1 commit message and comments per v1 review
  (Wei-Lin Chang).

Link to v1: https://lore.kernel.org/r/20261119185105.626343-1-coltonlewis@google.com
Link to v2: https://lore.kernel.org/r/20261123232647.3733149-1-coltonlewis@google.com
[1] https://lore.kernel.org/r/20261123182312.4842-1-maz@kernel.org

Colton Lewis (2):
  KVM: arm64: nv: Remove unmapped guest stage-2 mapping entries
  KVM: arm64: nv: Avoid duplicate guest stage-2 mapping entries

 arch/arm64/kvm/nested.c | 66 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 61 insertions(+), 5 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.56.1.911.g883d8bcb20-goog


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

* [PATCH v3 1/2] KVM: arm64: nv: Remove unmapped guest stage-2 mapping entries
  2026-12-02  0:13 [PATCH v3 0/2] KVM: arm64: nv: Fix stale and duplicate guest S2 tracking entries Colton Lewis
@ 2026-12-02  0:13 ` Colton Lewis
  2026-12-02  0:13 ` [PATCH v3 2/2] KVM: arm64: nv: Avoid duplicate " Colton Lewis
  1 sibling, 0 replies; 3+ messages in thread
From: Colton Lewis @ 2026-12-02  0:13 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton
  Cc: Wei-Lin Chang, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, kvmarm, linux-arm-kernel, kvm, linux-kernel

kvm_pgtable_stage2_unmap_tracked() can be called with a size smaller than
a tracked block mapping (for example, from a TLBI RIPAS2E1IS range or a
TLBI IPAS2E1IS with a 4kB TTL hint on a 2MB block).
kvm_pgtable_stage2_unmap() clears the entire block in the shadow stage-2
page table, but kvm_remove_guest_s2_mappings() only removes tracking
entries fully enclosed by the unmap range, leaving a stale
kvm_guest_s2_mapping entry behind in guest_s2_mappings.

Partially overlapping entries cannot be removed unconditionally, because
the tracked range may instead contain smaller page mappings in a child
table (such as under dirty logging) that remain valid outside the
unmapped range.

On a partial overlap, look up nested_start in the shadow page table with
kvm_pgtable_get_leaf() and remove the tracking entry when no valid PTE or
child table remains at its tracked granule size (!kvm_pte_valid(pte) &&
kvm_granule_size(level) >= map_size). Since kvm_pgtable_stage2_unmap()
has already unlinked any empty child tables, a single lookup at
nested_start is enough to tell whether the entire tracked range is empty.

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

diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 78d5b18dbd2d6..3dcf2ea54873e 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -928,6 +928,27 @@ void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,
 	interval_tree_insert(&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings);
 }
 
+/*
+ * Each tracked range [nested_start, nested_start + map_size - 1] is aligned
+ * to one host stage-2 granule. After kvm_pgtable_stage2_unmap() unlinks any
+ * empty child tables, an invalid leaf at or above that granule level means
+ * neither a block mapping nor a child table remains in the tracked range.
+ */
+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,
 				  size_t size)
 {
@@ -944,18 +965,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;
+		unsigned long nested_start, nested_end;
+		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.
+		 * Remove the tracking entry if it is fully enclosed by the
+		 * unmap range, or if a partial unmap left no shadow mappings
+		 * anywhere in its tracked range.
 		 */
 		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);
+		nested_end = mapping->nested.last;
+		map_size = nested_end - nested_start + 1;
+		if ((nipa <= nested_start && nipa_end >= nested_end) ||
+		    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);
-- 
2.56.1.911.g883d8bcb20-goog


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

* [PATCH v3 2/2] KVM: arm64: nv: Avoid duplicate guest stage-2 mapping entries
  2026-12-02  0:13 [PATCH v3 0/2] KVM: arm64: nv: Fix stale and duplicate guest S2 tracking entries Colton Lewis
  2026-12-02  0:13 ` [PATCH v3 1/2] KVM: arm64: nv: Remove unmapped guest stage-2 mapping entries Colton Lewis
@ 2026-12-02  0:13 ` Colton Lewis
  1 sibling, 0 replies; 3+ messages in thread
From: Colton Lewis @ 2026-12-02  0:13 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton
  Cc: Wei-Lin Chang, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, kvmarm, linux-arm-kernel, kvm, linux-kernel

kvm_record_guest_s2_mapping() unconditionally inserts a new entry into
both guest_s2_mappings interval trees. Parallel stage-2 faults on the
same IPA or retries after -EAGAIN can therefore insert identical
entries for the same nested and canonical IPA range.

Check mmu->guest_s2_mappings under guest_s2_tracking_lock before
inserting, and free the new entry if an identical one is already
present.

Signed-off-by: Colton Lewis <coltonlewis@google.com>
---
 arch/arm64/kvm/nested.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 3dcf2ea54873e..fafa7f5738776 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -904,6 +904,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)
@@ -924,6 +948,11 @@ 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);
+	if (guest_s2_mapping_exists(mmu, mapping)) {
+		kfree(mapping);
+		return;
+	}
+
 	interval_tree_insert(&mapping->nested, &mmu->guest_s2_mappings);
 	interval_tree_insert(&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings);
 }
-- 
2.56.1.911.g883d8bcb20-goog


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

end of thread, other threads:[~2026-12-02  0:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  0:13 [PATCH v3 0/2] KVM: arm64: nv: Fix stale and duplicate guest S2 tracking entries Colton Lewis
2026-12-02  0:13 ` [PATCH v3 1/2] KVM: arm64: nv: Remove unmapped guest stage-2 mapping entries Colton Lewis
2026-12-02  0:13 ` [PATCH v3 2/2] KVM: arm64: nv: Avoid duplicate " Colton Lewis

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

all inboxes | Powered by JetHome®