kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: James Houghton <jthoughton@google.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Xu <peterx@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 syzbot+55eaa971f4924b62c9a5@syzkaller.appspotmail.com
Subject: [PATCH 3/4] KVM: Annotate concurrent accesses to dirty ring indices
Date: Tue, 17 Nov 2026 21:29:12 +0000	[thread overview]
Message-ID: <20261117212913.3634487-4-jthoughton@google.com> (raw)
In-Reply-To: <20261117212913.3634487-1-jthoughton@google.com>

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


  parent reply	other threads:[~2026-11-17 21:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117212913.3634487-4-jthoughton@google.com \
    --to=jthoughton@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=seanjc@google.com \
    --cc=syzbot+55eaa971f4924b62c9a5@syzkaller.appspotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).