From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796332859; b=NnCLTSn3GeCQMcFcAHoF3wxvR7QIbmQS9SP4Vv17d6R5f6dtURBoBb1S32CQJODn2/v1 rBwhdTFYhqWRf/Jg1ZGaxwDlXFTy/MmbOpT8Qz0CY8qhCW6dxfGU5TxVpeDR47o37ah31 OV703WP6Lm82A9iQtdoqp/VuD0Uim/8fdawYE7wQZ9OKTvMvtgki59WYrzR+5ztdSFpdW qeTTAbi0ePqtDR0vSkemp+8NuuEduspCiDq8sWMwvpOEHc1udFDoU3BkarLT7u3nNzUY0 2dS/2WZlNx3SOjaErtX4uMhki4StUenVszupnwaRR/scFTFd9OlEu8fWwt7uF21923A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796332859; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=FzML2cMlCQXm5sm8KnQgZKmr3epM3yhpgAEDXrRynCc=; b=hfe93ETB6edBaYBF//pd98fNpURbcpHgZUOFQi8guzBpmP5weI/E5RzbLu5WMjFHgnVv ryBoLuHggV68JA6N1Z+MSZmB6NagIMYby7KFrOFX/JID4qMZDWQBZSbnU3/lxHpsu1qll WCLWegC0tmfz+glDexzzLT+DoVFxjtp3QUzzEKdaHOfKhYQhGu7ioC4n4JHT7EeWMX8AA 2dvBJBBUGDus2OHuXYTFxR2HfoM7MhS48TgbPH51DSLmF21U3pghsdgR9SD/iiXYKXI41 DDWgwt07neyXAr92xeZ3FyjjRErKpnTMBS0f1kUYrfrgooxGOIzxcBIGdgJxX0aQJyQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=G5JVbMRM; arc=none smtp.remote-ip=209.85.216.70 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E5FA606A6AE for ; Thu, 3 Dec 2026 21:20:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9E5FA606A6AE Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=G5JVbMRM Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-3b42ebf632cso200255a91.2 for ; Thu, 03 Dec 2026 13:20:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796332858; x=1796937658; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=FzML2cMlCQXm5sm8KnQgZKmr3epM3yhpgAEDXrRynCc=; b=G5JVbMRM0ne1kcFkX2kXEQs15p+2rZPvGjoaroYp0N99KoGvTogZokxz4/Jej7niFX g1Jx8Od3fCmDXAT97duLZBgQV66RAEAZHh3G/Sai/soBg8+1jS21/41xuuZETKw6rOhI xUzDesuOhU3P5OGllEkTM3uny4ByWLUJd5qMiBmu013gyqc1GPdu41WrHmDIxsulipgv O2zER5JQmpZH594s+wPycgvzT6O6J1vxx4MQNO+SsI+TJDiWQT7T8aL3LwUXT0xcezhS qo0XZYD54vW3cir8K5b4tVppMCPW2cXgw25ye04gt3zmQOZNk/kkhsHnLb8MnZp7vGhi XFxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796332858; x=1796937658; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=FzML2cMlCQXm5sm8KnQgZKmr3epM3yhpgAEDXrRynCc=; b=SLmvxX9cNJqScZEPXsuHEymOzLd1ilrXrzgIYI793ErcApn5Zac4xqq7csr4LB6Zp9 13vbcjPTq3xgEKMySqfLsV+gogS/J428atseT5DBnJPHn1LnmhwvuM+zhQmjlMcaVKrm O+m0ITeA+yZqPokVn/tLkH29BSpXQhZGrJt6X7hyAGXdCtHqttb8y/BpZdKpGr/jmVk1 Uj/tWt4dfXhax25x77RRJDLi9NvXOEIBRCuFGY4HkX+OIwlVCCAG1UHAiLrEYhzjeU1E e1Yymvy/cEa2RHRsbwQWMjHnfAW0GdYVPV5GNSprp2G0K7BDtDZKvBkuptp0yFA6mbnB VixA== X-Forwarded-Encrypted: i=1; AL+hErGnsqTlu2WRXNvncAWsIW94OD1jhTjX6X+MC+4Hn2XX5tEpTJ6wwAlzwqowdUjNmG4f6QKmfSnFJ4CnuwI=@vger.kernel.org X-Gm-Message-State: APuU4Z/PjKGhIlAUHGKHFpOmLLecYHNhA8COt9UuPmWgk1iJ/EVnG5i+ 7OWzcXoKLAzQr64b4Gh7dzlUEn6sVyU4mcmfSFBNHFvlfLunMP1ueM3eFyjNXZgr0rWtoyrd5E5 0QX4rdqqXbSPJPBtuDeNz6A== X-Received: from pjbbg10.prod.google.com ([2002:a17:90b:d8a:b0:3b4:33e:4471]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3e48:b0:3ac:adce:60ad with SMTP id 98e67ed59e1d1-3b41c98a343mr39279638a91.22.1796332857619; Thu, 03 Dec 2026 13:20:57 -0800 (PST) Date: Thu, 3 Dec 2026 21:20:52 +0000 In-Reply-To: <20261203212054.2957430-1-jthoughton@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261203212054.2957430-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261203212054.2957430-4-jthoughton@google.com> Subject: [PATCH v2 3/4] KVM: Annotate concurrent accesses to dirty ring indices From: James Houghton To: Sean Christopherson , Paolo Bonzini Cc: Peter Xu , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+55eaa971f4924b62c9a5@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" 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