Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 6.6.y+6.1.y 0/2] KVM: x86/mmu: Check write tracking in all address spaces
@ 2026-08-26 20:16 Artem Dinaburg
  2026-08-26 20:16 ` [PATCH 6.6.y 1/2] " Artem Dinaburg
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Artem Dinaburg @ 2026-08-26 20:16 UTC (permalink / raw)
  To: stable; +Cc: kvm, linux-kernel, seanjc, pbonzini, kimjw04271234,
	Artem Dinaburg

Please queue upstream commit 0f38453cdb2e17566ccb7c0f3dabd5bd21caca26
("KVM: x86/mmu: Check write tracking in all address spaces") for the
stable trees. It is in mainline as of v7.2-rc6.

The fix is absent by code state from 7.1.10, 6.18.46, 6.12.105, 6.6.153
and 6.1.184.

7.1.y, 6.18.y, 6.12.y: please cherry-pick the upstream commit directly.
No patch is included for these trees. Commit 0f38453cdb2e applies and
compiles unmodified on all three, so there is nothing to adapt:

  git cherry-pick -x 0f38453cdb2e17566ccb7c0f3dabd5bd21caca26

6.6.y and 6.1.y need adapted backports; they are patches 1/2 and 2/2 of
this series. On 6.6 the upstream commit applies but does not build,
because KVM_MAX_NR_ADDRESS_SPACES and kvm_arch_nr_memslot_as_ids() do
not exist there. On 6.1 the upstream commit conflicts: the function is
still kvm_slot_page_track_is_active() and the tracking array is
mode-indexed gfn_track[].

5.15.y and 5.10.y are not covered by this submission. Those trees were
not assessed.

Jinu Kim (2):
  [6.6.y] KVM: x86/mmu: Check write tracking in all address spaces
  [6.1.y] KVM: x86/mmu: Check write tracking in all address spaces

Each patch touches only arch/x86/kvm/mmu/page_track.c:
  1/2 (6.6.y): 26 insertions(+), 6 deletions(-)
  2/2 (6.1.y): 22 insertions(+), 6 deletions(-)

-- 
2.43.0

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

* [PATCH 6.6.y 1/2] KVM: x86/mmu: Check write tracking in all address spaces
  2026-08-26 20:16 [PATCH 6.6.y+6.1.y 0/2] KVM: x86/mmu: Check write tracking in all address spaces Artem Dinaburg
@ 2026-08-26 20:16 ` Artem Dinaburg
  2026-08-26 20:16 ` [PATCH 6.1.y 2/2] " Artem Dinaburg
  2026-08-28  1:46 ` [PATCH 6.6.y+6.1.y 0/2] " Sasha Levin
  2 siblings, 0 replies; 4+ messages in thread
From: Artem Dinaburg @ 2026-08-26 20:16 UTC (permalink / raw)
  To: stable; +Cc: kvm, linux-kernel, seanjc, pbonzini, kimjw04271234,
	Artem Dinaburg

From: Jinu Kim <kimjw04271234@gmail.com>

commit 0f38453cdb2e17566ccb7c0f3dabd5bd21caca26 upstream.

kvm_gfn_is_write_tracked() checks only the supplied memslot, but page
tracking is per-address-space and shadow pages are shared across all
address spaces.  With SMM, a GFN can therefore be write-tracked in one
address space and appear untracked through the other.

Check the supplied slot first, then the slot for the other address space.
This ensures all callers honor write tracking regardless of the active
address space.  In particular, it prevents mmu_try_to_unsync_pages() from
marking an upper-level shadow page unsync and eventually triggering the
BUG in pte_list_remove().

Fixes: 699023e23965 ("KVM: x86: add SMM to the MMU role, support SMRAM address space")
Assisted-by: Codex:GPT-5
Signed-off-by: Jinu Kim <kimjw04271234@gmail.com>
Message-ID: <20260721103512.2136240-2-kimjw04271234@gmail.com>
[invert direction of the conditional. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[ artem: adapt to 6.6, which has neither KVM_MAX_NR_ADDRESS_SPACES nor
  kvm_arch_nr_memslot_as_ids(); use KVM_ADDRESS_SPACE_NUM instead. That
  macro is CONFIG_KVM_SMM-conditional in 6.6, so the guard on
  KVM_ADDRESS_SPACE_NUM > 1 around the peer-slot lookup is retained ]
Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
---
Target tree: linux-6.6.y (stable).

Verified: applies to v6.6.153; arch/x86/kvm/mmu/page_track.o builds clean
with CONFIG_KVM_SMM=y and with CONFIG_KVM_SMM=n.

 arch/x86/kvm/mmu/page_track.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c
index c87da11f3a04..2615074a4bff 100644
--- a/arch/x86/kvm/mmu/page_track.c
+++ b/arch/x86/kvm/mmu/page_track.c
@@ -117,13 +117,23 @@ void __kvm_write_track_remove_gfn(struct kvm *kvm,
 	kvm_mmu_gfn_allow_lpage(slot, gfn);
 }
 
-/*
- * check if the corresponding access on the specified guest page is tracked.
- */
+static bool __kvm_gfn_is_write_tracked(const struct kvm_memory_slot *slot,
+				       gfn_t gfn)
+{
+	int index;
+
+	if (!slot)
+		return false;
+
+	index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
+	return !!READ_ONCE(slot->arch.gfn_write_track[index]);
+}
+
+/* check if write access is tracked on the specified guest page.  */
 bool kvm_gfn_is_write_tracked(struct kvm *kvm,
 			      const struct kvm_memory_slot *slot, gfn_t gfn)
 {
-	int index;
+	const struct kvm_memory_slot *other_slot;
 
 	if (!slot)
 		return false;
@@ -131,8 +141,18 @@ bool kvm_gfn_is_write_tracked(struct kvm *kvm,
 	if (!kvm_page_track_write_tracking_enabled(kvm))
 		return false;
 
-	index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
-	return !!READ_ONCE(slot->arch.gfn_write_track[index]);
+	BUILD_BUG_ON(KVM_ADDRESS_SPACE_NUM > 2);
+
+	if (__kvm_gfn_is_write_tracked(slot, gfn))
+		return true;
+
+	if (KVM_ADDRESS_SPACE_NUM > 1) {
+		other_slot = __gfn_to_memslot(__kvm_memslots(kvm, slot->as_id ^ 1), gfn);
+		if (__kvm_gfn_is_write_tracked(other_slot, gfn))
+			return true;
+	}
+
+	return false;
 }
 
 #ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING
-- 
2.43.0


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

* [PATCH 6.1.y 2/2] KVM: x86/mmu: Check write tracking in all address spaces
  2026-08-26 20:16 [PATCH 6.6.y+6.1.y 0/2] KVM: x86/mmu: Check write tracking in all address spaces Artem Dinaburg
  2026-08-26 20:16 ` [PATCH 6.6.y 1/2] " Artem Dinaburg
@ 2026-08-26 20:16 ` Artem Dinaburg
  2026-08-28  1:46 ` [PATCH 6.6.y+6.1.y 0/2] " Sasha Levin
  2 siblings, 0 replies; 4+ messages in thread
From: Artem Dinaburg @ 2026-08-26 20:16 UTC (permalink / raw)
  To: stable; +Cc: kvm, linux-kernel, seanjc, pbonzini, kimjw04271234,
	Artem Dinaburg

From: Jinu Kim <kimjw04271234@gmail.com>

commit 0f38453cdb2e17566ccb7c0f3dabd5bd21caca26 upstream.

kvm_gfn_is_write_tracked() checks only the supplied memslot, but page
tracking is per-address-space and shadow pages are shared across all
address spaces.  With SMM, a GFN can therefore be write-tracked in one
address space and appear untracked through the other.

Check the supplied slot first, then the slot for the other address space.
This ensures all callers honor write tracking regardless of the active
address space.  In particular, it prevents mmu_try_to_unsync_pages() from
marking an upper-level shadow page unsync and eventually triggering the
BUG in pte_list_remove().

Fixes: 699023e23965 ("KVM: x86: add SMM to the MMU role, support SMRAM address space")
Assisted-by: Codex:GPT-5
Signed-off-by: Jinu Kim <kimjw04271234@gmail.com>
Message-ID: <20260721103512.2136240-2-kimjw04271234@gmail.com>
[invert direction of the conditional. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[ artem: adapt to 6.1's kvm_slot_page_track_is_active() and mode-indexed
  gfn_track[]; KVM_ADDRESS_SPACE_NUM is unconditionally 2 on x86 in 6.1,
  so the peer-slot lookup needs no guard ]
Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
---
Target tree: linux-6.1.y (stable).

Verified: applies to v6.1.184; arch/x86/kvm/ builds clean with x86_64
defconfig plus CONFIG_KVM=m, CONFIG_KVM_INTEL=m and CONFIG_KVM_AMD=m,
gcc 13.3.0.

 arch/x86/kvm/mmu/page_track.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c
index 2e09d1b6249f..51d5a912c553 100644
--- a/arch/x86/kvm/mmu/page_track.c
+++ b/arch/x86/kvm/mmu/page_track.c
@@ -171,14 +171,25 @@ void kvm_slot_page_track_remove_page(struct kvm *kvm,
 }
 EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
 
-/*
- * check if the corresponding access on the specified guest page is tracked.
- */
+static bool __kvm_slot_page_track_is_active(const struct kvm_memory_slot *slot,
+					    gfn_t gfn,
+					    enum kvm_page_track_mode mode)
+{
+	int index;
+
+	if (!slot)
+		return false;
+
+	index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
+	return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
+}
+
+/* check if write access is tracked on the specified guest page. */
 bool kvm_slot_page_track_is_active(struct kvm *kvm,
 				   const struct kvm_memory_slot *slot,
 				   gfn_t gfn, enum kvm_page_track_mode mode)
 {
-	int index;
+	const struct kvm_memory_slot *other_slot;
 
 	if (WARN_ON(!page_track_mode_is_valid(mode)))
 		return false;
@@ -190,8 +201,13 @@ bool kvm_slot_page_track_is_active(struct kvm *kvm,
 	    !kvm_page_track_write_tracking_enabled(kvm))
 		return false;
 
-	index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
-	return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
+	BUILD_BUG_ON(KVM_ADDRESS_SPACE_NUM > 2);
+
+	if (__kvm_slot_page_track_is_active(slot, gfn, mode))
+		return true;
+
+	other_slot = __gfn_to_memslot(__kvm_memslots(kvm, slot->as_id ^ 1), gfn);
+	return __kvm_slot_page_track_is_active(other_slot, gfn, mode);
 }
 
 void kvm_page_track_cleanup(struct kvm *kvm)
-- 
2.43.0


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

* Re: [PATCH 6.6.y+6.1.y 0/2] KVM: x86/mmu: Check write tracking in all address spaces
  2026-08-26 20:16 [PATCH 6.6.y+6.1.y 0/2] KVM: x86/mmu: Check write tracking in all address spaces Artem Dinaburg
  2026-08-26 20:16 ` [PATCH 6.6.y 1/2] " Artem Dinaburg
  2026-08-26 20:16 ` [PATCH 6.1.y 2/2] " Artem Dinaburg
@ 2026-08-28  1:46 ` Sasha Levin
  2 siblings, 0 replies; 4+ messages in thread
From: Sasha Levin @ 2026-08-28  1:46 UTC (permalink / raw)
  To: stable
  Cc: Sasha Levin, kvm, linux-kernel, seanjc, pbonzini, kimjw04271234,
	Artem Dinaburg

> 7.1.y, 6.18.y, 6.12.y: please cherry-pick the upstream commit directly.
> No patch is included for these trees.
>
> 6.6.y and 6.1.y need adapted backports; they are patches 1/2 and 2/2 of
> this series.

Queued for 7.1, 6.18, 6.12, 6.6 and 6.1, thanks.

-- 
Thanks,
Sasha

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

end of thread, other threads:[~2026-08-28  1:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-26 20:16 [PATCH 6.6.y+6.1.y 0/2] KVM: x86/mmu: Check write tracking in all address spaces Artem Dinaburg
2026-08-26 20:16 ` [PATCH 6.6.y 1/2] " Artem Dinaburg
2026-08-26 20:16 ` [PATCH 6.1.y 2/2] " Artem Dinaburg
2026-08-28  1:46 ` [PATCH 6.6.y+6.1.y 0/2] " Sasha Levin

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