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 Signed-off-by: James Houghton --- 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)) -- 2.56.1.915.g69d5ec7298-goog 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 --- 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.915.g69d5ec7298-goog 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 --- 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.915.g69d5ec7298-goog 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. Clear the flags afterwards, so that the bogus RESET flags can't race with KVM pushing to the entries. Signed-off-by: James Houghton --- tools/testing/selftests/kvm/dirty_log_test.c | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c index 8189eaac713c..b03ce06ce100 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -375,6 +375,28 @@ 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. + */ + 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) { struct kvm_run *run = vcpu->run; @@ -687,6 +709,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.915.g69d5ec7298-goog