* [PATCH 1/4] KVM: Don't reset dirty ring entries that KVM hasn't pushed
2026-11-17 21:29 [PATCH 0/4] KVM: Don't let dirty ring resets pass dirty_index James Houghton
@ 2026-11-17 21:29 ` James Houghton
2026-11-17 21:29 ` [PATCH 2/4] KVM: Use smp_store_release() to mark dirty ring entries as dirtied James Houghton
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: James Houghton @ 2026-11-17 21:29 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: Peter Xu, kvm, linux-kernel, syzbot+55eaa971f4924b62c9a5
When resetting a vCPU's dirty ring, stop if reset_index has caught up to
dirty_index instead of relying solely on the RESET flag of the next
entry. The ring is mapped writable into userspace, so userspace can set
KVM_DIRTY_GFN_F_RESET on entries that KVM never pushed, in which case
KVM advances reset_index past dirty_index. The u32 math in
kvm_dirty_ring_used() then underflows, and the next push hits the
WARN_ON_ONCE() in kvm_dirty_ring_push():
WARNING: virt/kvm/dirty_ring.c:225 at kvm_dirty_ring_push+0x2b2/0x5e0
Call Trace:
make_spte+0x71a/0xc80
kvm_tdp_mmu_map+0x140e/0x1f40
kvm_tdp_page_fault+0x2f7/0x3a0
kvm_mmu_page_fault+0x1f8/0xc00
vcpu_run+0x4da1/0x7890
kvm_arch_vcpu_ioctl_run+0xe83/0x18b0
kvm_vcpu_ioctl+0xa61/0xfd0
Userspace that scribbles on its own dirty ring gets to keep the pieces,
but KVM shouldn't WARN, and keeping reset_index from passing dirty_index
is trivial.
To avoid skipping entries that were legitimately harvested while the
vCPU is running, bump dirty_index before publishing the DIRTY flag in
kvm_dirty_ring_push(), and check dirty_index after the load-acquire of
the RESET flag in kvm_dirty_ring_reset(). If KVM observes the RESET
flag for an entry, i.e. userspace observed the DIRTY flag, then KVM is
guaranteed to also observe the updated dirty_index.
Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking")
Reported-by: syzbot+55eaa971f4924b62c9a5@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/6afbad4d.f3cfa7c9.844a4.0002.GAE@google.com
Cc: stable@vger.kernel.org
Cc: Peter Xu <peterx@redhat.com>
Signed-off-by: James Houghton <jthoughton@google.com>
---
virt/kvm/dirty_ring.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
index 572b854edf74..8fab75dace76 100644
--- a/virt/kvm/dirty_ring.c
+++ b/virt/kvm/dirty_ring.c
@@ -140,6 +140,16 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
if (!kvm_dirty_gfn_harvested(entry))
break;
+ /*
+ * Don't reset entries that KVM hasn't pushed, e.g. if userspace
+ * set the RESET flag on an unused entry. Check the number of
+ * used entries after checking the flag, so that dirty_index is
+ * up-to-date for entries that were harvested (pairs with the
+ * ordering in kvm_dirty_ring_push()).
+ */
+ if (!kvm_dirty_ring_used(ring))
+ break;
+
next_slot = READ_ONCE(entry->slot);
next_offset = READ_ONCE(entry->offset);
@@ -229,12 +239,14 @@ void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset)
entry->slot = slot;
entry->offset = offset;
/*
- * Make sure the data is filled in before we publish this to
- * the userspace program. There's no paired kernel-side reader.
+ * Bump dirty_index before publishing the entry, so that
+ * kvm_dirty_ring_reset() sees the new dirty_index if it sees the entry
+ * as harvested, and make sure the data is filled in before publishing
+ * the entry to userspace.
*/
+ WRITE_ONCE(ring->dirty_index, ring->dirty_index + 1);
smp_wmb();
kvm_dirty_gfn_set_dirtied(entry);
- ring->dirty_index++;
trace_kvm_dirty_ring_push(ring, slot, offset);
if (kvm_dirty_ring_soft_full(ring))
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
--
2.56.1.708.g77570a6a1d-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 2/4] KVM: Use smp_store_release() to mark dirty ring entries as dirtied
2026-11-17 21:29 [PATCH 0/4] KVM: Don't let dirty ring resets pass dirty_index James Houghton
2026-11-17 21:29 ` [PATCH 1/4] KVM: Don't reset dirty ring entries that KVM hasn't pushed James Houghton
@ 2026-11-17 21:29 ` James Houghton
2026-11-17 21:29 ` [PATCH 3/4] KVM: Annotate concurrent accesses to dirty ring indices James Houghton
2026-11-17 21:29 ` [PATCH 4/4] KVM: selftests: Verify KVM doesn't reset unused dirty ring entries James Houghton
3 siblings, 0 replies; 7+ messages in thread
From: James Houghton @ 2026-11-17 21:29 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: Peter Xu, kvm, linux-kernel, syzbot+55eaa971f4924b62c9a5
Publish dirty ring entries to userspace with smp_store_release() in
kvm_dirty_gfn_set_dirtied(), instead of an smp_wmb() in
kvm_dirty_ring_push() followed by a plain store in the helper.
Setting the DIRTY flag is the first half of a handshake with userspace:
userspace load-acquires the DIRTY flag, harvests the entry, and
store-releases the RESET flag, which KVM then load-acquires in
kvm_dirty_gfn_harvested(). Commit 8929bc965964 ("KVM: Use acquire/release
semantics when accessing dirty ring GFN state") converted KVM's side of
the RESET flag to acquire/release, but left the DIRTY side split between
a barrier in the caller and a plain store in the helper. As a result, the
helper doesn't convey that its store must be ordered, and the store isn't
marked even though userspace can access the flags concurrently.
Now that kvm_dirty_ring_reset() relies on this handshake to observe an
up-to-date dirty_index, make the ordering explicit and self-contained in
the helper, so that all three flag accessors use matching acquire/release
semantics.
No functional change intended.
Signed-off-by: James Houghton <jthoughton@google.com>
---
virt/kvm/dirty_ring.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
index 8fab75dace76..d0693ef6e479 100644
--- a/virt/kvm/dirty_ring.c
+++ b/virt/kvm/dirty_ring.c
@@ -95,7 +95,7 @@ static inline void kvm_dirty_gfn_set_invalid(struct kvm_dirty_gfn *gfn)
static inline void kvm_dirty_gfn_set_dirtied(struct kvm_dirty_gfn *gfn)
{
- gfn->flags = KVM_DIRTY_GFN_F_DIRTY;
+ smp_store_release(&gfn->flags, KVM_DIRTY_GFN_F_DIRTY);
}
static inline bool kvm_dirty_gfn_harvested(struct kvm_dirty_gfn *gfn)
@@ -245,7 +245,6 @@ void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset)
* the entry to userspace.
*/
WRITE_ONCE(ring->dirty_index, ring->dirty_index + 1);
- smp_wmb();
kvm_dirty_gfn_set_dirtied(entry);
trace_kvm_dirty_ring_push(ring, slot, offset);
--
2.56.1.708.g77570a6a1d-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 3/4] KVM: Annotate concurrent accesses to dirty ring indices
2026-11-17 21:29 [PATCH 0/4] KVM: Don't let dirty ring resets pass dirty_index James Houghton
2026-11-17 21:29 ` [PATCH 1/4] KVM: Don't reset dirty ring entries that KVM hasn't pushed James Houghton
2026-11-17 21:29 ` [PATCH 2/4] KVM: Use smp_store_release() to mark dirty ring entries as dirtied James Houghton
@ 2026-11-17 21:29 ` James Houghton
2026-11-17 21:29 ` [PATCH 4/4] KVM: selftests: Verify KVM doesn't reset unused dirty ring entries James Houghton
3 siblings, 0 replies; 7+ messages in thread
From: James Houghton @ 2026-11-17 21:29 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: Peter Xu, kvm, linux-kernel, syzbot+55eaa971f4924b62c9a5
A vCPU's dirty ring indices are accessed concurrently by two tasks: the
vCPU pushes entries and updates dirty_index, while KVM_RESET_DIRTY_RINGS
updates reset_index, and can run at any time (VMMs commonly reap the ring
from a separate thread while the vCPU is running).
Use WRITE_ONCE() to update reset_index, which the vCPU reads with
READ_ONCE() in kvm_dirty_ring_used(). KCSAN doesn't assume that a plain
read-modify-write is atomic, so it can report the increment as a data
race.
Use READ_ONCE() to read the other task's index in the kvm_dirty_ring_push
and kvm_dirty_ring_reset tracepoints, so that the compiler can't tear or
refetch the loads. The values are only used for tracing, but the
indices are part of the ring's concurrency design, so mark the reads the
same way as kvm_dirty_ring_used().
Signed-off-by: James Houghton <jthoughton@google.com>
---
include/trace/events/kvm.h | 4 ++--
virt/kvm/dirty_ring.c | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h
index 07013f88e655..477a9d3705ed 100644
--- a/include/trace/events/kvm.h
+++ b/include/trace/events/kvm.h
@@ -316,7 +316,7 @@ TRACE_EVENT(kvm_dirty_ring_push,
TP_fast_assign(
__entry->index = ring->index;
__entry->dirty_index = ring->dirty_index;
- __entry->reset_index = ring->reset_index;
+ __entry->reset_index = READ_ONCE(ring->reset_index);
__entry->slot = slot;
__entry->offset = offset;
),
@@ -340,7 +340,7 @@ TRACE_EVENT(kvm_dirty_ring_reset,
TP_fast_assign(
__entry->index = ring->index;
- __entry->dirty_index = ring->dirty_index;
+ __entry->dirty_index = READ_ONCE(ring->dirty_index);
__entry->reset_index = ring->reset_index;
),
diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
index d0693ef6e479..cd52f136fdf8 100644
--- a/virt/kvm/dirty_ring.c
+++ b/virt/kvm/dirty_ring.c
@@ -156,7 +156,7 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
/* Update the flags to reflect that this GFN is reset */
kvm_dirty_gfn_set_invalid(entry);
- ring->reset_index++;
+ WRITE_ONCE(ring->reset_index, ring->reset_index + 1);
(*nr_entries_reset)++;
if (mask) {
--
2.56.1.708.g77570a6a1d-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 4/4] KVM: selftests: Verify KVM doesn't reset unused dirty ring entries
2026-11-17 21:29 [PATCH 0/4] KVM: Don't let dirty ring resets pass dirty_index James Houghton
` (2 preceding siblings ...)
2026-11-17 21:29 ` [PATCH 3/4] KVM: Annotate concurrent accesses to dirty ring indices James Houghton
@ 2026-11-17 21:29 ` James Houghton
2026-11-17 21:37 ` sashiko-bot
3 siblings, 1 reply; 7+ messages in thread
From: James Houghton @ 2026-11-17 21:29 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: Peter Xu, kvm, linux-kernel, syzbot+55eaa971f4924b62c9a5
Before running the vCPU in the dirty ring test, mark the first two
(unused) entries of the ring as harvested and verify that
KVM_RESET_DIRTY_RINGS doesn't reset any entries, i.e. that KVM doesn't
advance reset_index beyond dirty_index.
Signed-off-by: James Houghton <jthoughton@google.com>
---
tools/testing/selftests/kvm/dirty_log_test.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index 8189eaac713c..96a40488eccb 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -375,6 +375,21 @@ static void dirty_ring_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot,
"with collected (%u)", cleared, count);
}
+static void dirty_ring_reset_unused_entries(struct kvm_vcpu *vcpu)
+{
+ struct kvm_dirty_gfn *dirty_gfns = vcpu_map_dirty_ring(vcpu);
+
+ /*
+ * Mark unused entries as harvested, and verify KVM doesn't reset any
+ * entries, as KVM hasn't pushed anything to the ring. KVM will
+ * overwrite the flags when it pushes to the entries.
+ */
+ dirty_gfn_set_collected(&dirty_gfns[0]);
+ dirty_gfn_set_collected(&dirty_gfns[1]);
+
+ TEST_ASSERT_EQ(kvm_vm_reset_dirty_ring(vcpu->vm), 0);
+}
+
static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu)
{
struct kvm_run *run = vcpu->run;
@@ -687,6 +702,9 @@ static void run_test(enum vm_guest_mode mode, void *arg)
TEST_ASSERT_EQ(vcpu_stop, false);
+ if (host_log_mode == LOG_MODE_DIRTY_RING)
+ dirty_ring_reset_unused_entries(vcpu);
+
kvm_pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu);
for (iteration = 1; iteration <= p->iterations; iteration++) {
--
2.56.1.708.g77570a6a1d-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 4/4] KVM: selftests: Verify KVM doesn't reset unused dirty ring entries
2026-11-17 21:29 ` [PATCH 4/4] KVM: selftests: Verify KVM doesn't reset unused dirty ring entries James Houghton
@ 2026-11-17 21:37 ` sashiko-bot
2026-11-17 21:55 ` James Houghton
0 siblings, 1 reply; 7+ messages in thread
From: sashiko-bot @ 2026-11-17 21:37 UTC (permalink / raw)
To: James Houghton; +Cc: kvm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: selftests: clear RESET flags in
dirty_ring_reset_unused_entries()
--
commit 494a32b29217257f8a046ec9364c749fef56211c
Author: James Houghton <jthoughton@google.com>
KVM: selftests: Verify KVM doesn't reset unused dirty ring entries
This commit adds a helper to dirty_log_test that marks the first two unused
dirty ring entries as harvested before running the vCPU and asserts that
KVM_RESET_DIRTY_RINGS resets zero entries.
> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
> index 8189eaac713cd..96a40488eccb0 100644
> --- a/tools/testing/selftests/kvm/dirty_log_test.c
> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> @@ -375,6 +375,21 @@ static void dirty_ring_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot,
> "with collected (%u)", cleared, count);
> }
>
> +static void dirty_ring_reset_unused_entries(struct kvm_vcpu *vcpu)
> +{
> + struct kvm_dirty_gfn *dirty_gfns = vcpu_map_dirty_ring(vcpu);
> +
> + /*
> + * Mark unused entries as harvested, and verify KVM doesn't reset any
> + * entries, as KVM hasn't pushed anything to the ring. KVM will
> + * overwrite the flags when it pushes to the entries.
> + */
> + dirty_gfn_set_collected(&dirty_gfns[0]);
> + dirty_gfn_set_collected(&dirty_gfns[1]);
> +
> + TEST_ASSERT_EQ(kvm_vm_reset_dirty_ring(vcpu->vm), 0);
> +}
[Severity: Low]
Should dirty_ring_reset_unused_entries() clear flags back to 0 on
dirty_gfns[0] and dirty_gfns[1] after kvm_vm_reset_dirty_ring() returns?
When kvm_dirty_ring_used(ring) is 0, kvm_dirty_ring_reset() breaks out of
its loop before calling kvm_dirty_gfn_set_invalid(entry), leaving both
entries with KVM_DIRTY_GFN_F_RESET set when vcpu_worker starts. In
kvm_dirty_ring_push(), KVM increments ring->dirty_index before overwriting
entry->flags with KVM_DIRTY_GFN_F_DIRTY:
virt/kvm/dirty_ring.c:kvm_dirty_ring_push() {
...
WRITE_ONCE(ring->dirty_index, ring->dirty_index + 1);
kvm_dirty_gfn_set_dirtied(entry);
...
}
If the host reaping loop runs while the vCPU is pushing its first two dirty
ring entries (for example when reap_i is 0), kvm_dirty_ring_reset() can
observe the stale KVM_DIRTY_GFN_F_RESET flag together with the incremented
ring->dirty_index and reset an entry that userspace never collected:
host vcpu_worker
---- -----------
dirty_ring_collect_dirty_pages()
dirty_ring_collect_one()
/* flags == KVM_DIRTY_GFN_F_RESET,
stops with count == 0 */
kvm_dirty_ring_push()
ring->dirty_index++
kvm_vm_reset_dirty_ring()
kvm_dirty_ring_reset()
kvm_dirty_gfn_harvested(entry)
kvm_dirty_ring_used(ring) > 0
ring->reset_index++
cleared++
kvm_dirty_gfn_set_dirtied(entry)
TEST_ASSERT(cleared == count) <-- fails (1 != 0)
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117212913.3634487-1-jthoughton@google.com?part=4
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 4/4] KVM: selftests: Verify KVM doesn't reset unused dirty ring entries
2026-11-17 21:37 ` sashiko-bot
@ 2026-11-17 21:55 ` James Houghton
0 siblings, 0 replies; 7+ messages in thread
From: James Houghton @ 2026-11-17 21:55 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, sashiko-bot
Cc: jthoughton, kvm, sashiko-reviews
On 17 November 2026, sashiko-bot@kernel.org wrote:
> [Severity: Low]
> Should dirty_ring_reset_unused_entries() clear flags back to 0 on
> dirty_gfns[0] and dirty_gfns[1] after kvm_vm_reset_dirty_ring() returns?
>
> When kvm_dirty_ring_used(ring) is 0, kvm_dirty_ring_reset() breaks out of
> its loop before calling kvm_dirty_gfn_set_invalid(entry), leaving both
> entries with KVM_DIRTY_GFN_F_RESET set when vcpu_worker starts. In
> kvm_dirty_ring_push(), KVM increments ring->dirty_index before overwriting
> entry->flags with KVM_DIRTY_GFN_F_DIRTY:
[...]
> If the host reaping loop runs while the vCPU is pushing its first two dirty
> ring entries (for example when reap_i is 0), kvm_dirty_ring_reset() can
> observe the stale KVM_DIRTY_GFN_F_RESET flag together with the incremented
> ring->dirty_index and reset an entry that userspace never collected:
Yeah, this is possible. Sashiko's suggestion is the right one, I think.
Sean/Paolo, if nothing else in the series needs a v2, would you mind squashing
in the below when applying? Otherwise I'll fold it into v2.
Thanks!
---
diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index 96a4048..b03ce06 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -381,13 +381,20 @@ static void dirty_ring_reset_unused_entries(struct kvm_vcpu *vcpu)
/*
* Mark unused entries as harvested, and verify KVM doesn't reset any
- * entries, as KVM hasn't pushed anything to the ring. KVM will
- * overwrite the flags when it pushes to the entries.
+ * entries, as KVM hasn't pushed anything to the ring.
*/
dirty_gfn_set_collected(&dirty_gfns[0]);
dirty_gfn_set_collected(&dirty_gfns[1]);
TEST_ASSERT_EQ(kvm_vm_reset_dirty_ring(vcpu->vm), 0);
+
+ /*
+ * Clear the bogus RESET flags, otherwise a reset that runs while KVM
+ * is pushing to the entries could reset them before userspace has
+ * collected them.
+ */
+ dirty_gfns[0].flags = 0;
+ dirty_gfns[1].flags = 0;
}
static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu)
^ permalink raw reply related [flat|nested] 7+ messages in thread