From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796332856; b=CmtHWcodhy916T7lfH1jcaaWmrKX5kE7Tl4XucMc9y2/QTX1jSKVBBJSsKBhNjli4E4S EIao+F3VwUBGJItTn0smr3IcGq4qz+VZAcGhBHtGBBlFXSrH/BEuRCvrhwQqRNj2OiK6m TTBAvHEPFkNJCMueHt4cayqYwzWWQGT9evn1WhtmzXdioFIJqo4FpFzdXh8bubs1NCQbT kldsNJ5YFTKv37CSIxz8i2MtQZagTqBBJQWQqWuHT5PUr4hUde2Stv/9UXyI7OI/1XenB gAsWqsqZ8s4kI4hTAn8kA5KmPnJx5E4ei4cv6TE1JxnpeF9u9LczUT7BrjdF0b8FohA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796332856; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=62wA3w/yhb4gSCU5v7A3ATJTUPe6RNoOmUvQpZ1hbI4=; b=XoFIjss0tBYWO/5dcv2xJgi0TWJR8+c8iavrbJriK+mOiIj4pn0B3epKW+7N/NXj4D5w JP9XYugC7DXBgwnzOzRQKG+FphwCUfRLUkqJPWi8uf21p0v4ugSqBH92rd4wt9cMQffHb LX42EzrlX5yivATKoL38LbaBlBmX5YbnYGs+UM4g/Ya2a1XoUbvroumVFXGWoq/DMmo8V V/X5STxj7WWNuzb8duuRA+3BKJB0uPm2WB5ucUR0869a7ad08lRE0bNFc8FQGzlUXQlR0 edbjKwtYW+mMhdlUaaRJseCVmpYlll6qHbipU8WdX5/gHCv+aabdsgBSkxM0H+h9hHg== 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=EJCpRxAe; arc=none smtp.remote-ip=209.85.210.199 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (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 EF2F7606A322 for ; Thu, 3 Dec 2026 21:20:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EF2F7606A322 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=EJCpRxAe Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-8a065e2933fso300573b3a.3 for ; Thu, 03 Dec 2026 13:20:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796332855; x=1796937655; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=62wA3w/yhb4gSCU5v7A3ATJTUPe6RNoOmUvQpZ1hbI4=; b=EJCpRxAefcw5J/SLhuthErJ1xEyhQBr1rnuou4LhUcCNrZvtbUOZ83V8lVg9xomR63 H6tJuvGyr/vn/JW1at6mHsiCTOHmlz2TlhFfsiPyMeqtnM77X8l8Qac/Z5DXf5zeVioL G+qX34Lkm7axP8WvZwKiyWn4r2mJK1KxFIksCbntjmegMwSSoBgRX1L/dMMbTyjZ7Lqa zZ6e4xiYlOLyUB54rijS8/yZEKuy5tYMgV9asJ9h5aeCZ+xXpVh4B4MgSrwJH3sdAEJj LxAg2Jpuu/7F8Lg0JqLVSdXNH8xEvavM3RW84QQt5TqCDMObkdG/lMlslJCPuqrApvz0 4vCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796332855; x=1796937655; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=62wA3w/yhb4gSCU5v7A3ATJTUPe6RNoOmUvQpZ1hbI4=; b=BkWNhWDXCtBoJpEq++X/uqNKrN/BVc/mD3t4MW1u01a0+b24KnnkL9sGl2fHDGNT/f Myv6b/oTH0vMtWKfmjcmWYqZv3giGqGm5tVPWpMAEemYrfMuSYNf+gDkkhdBJ+Taf9z1 k0tqaq2T8unkJJQJnI5MO5QFyuXBHLQkhnhhbe5J4jZ5dE6ObotANzq0yOHU0MFxwGPn ozYiG1tJz2AgekyQ1CXR0bWIRNUDuoNbO1Yvl7eWU2HFP+L8uNctQrrPSyYvlGsOl1lO AWlcXjZAiKy8BaeahBShO8CrcaOBW3Ngoq9krB279AHzGW/awr7G7DuVZ/mwqDjht7z7 QhdA== X-Forwarded-Encrypted: i=1; AL+hErHBriKCEKRNalHeO4vQqeCYsVQCnBC7Ilkobk3j3tq7ZIseLqLjSMjQG8eO+knTGxSv7wMILP2SWzW78ho=@vger.kernel.org X-Gm-Message-State: APuU4Z+Lyr+CpSkCWO/mhEYfz4+Me1uuvzASv1zlcQWPI/au0wutFjHw AR3hbTWPhPeDUMVbc9gveOXZScS79FbmKr+kpzHP9DGtnWkZtugacDNlPGt5/j9m0RB63wOseeT QfxCL+Y2OMvsoBmo85kjYxg== X-Received: from pfme2.prod.google.com ([2002:aa7:98c2:0:b0:8b5:d577:733d]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:9511:b0:89f:e64f:c34a with SMTP id d2e1a72fcca58-8de2e3dc0bemr114016b3a.46.1796332855035; Thu, 03 Dec 2026 13:20:55 -0800 (PST) Date: Thu, 3 Dec 2026 21:20:49 +0000 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 X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261203212054.2957430-1-jthoughton@google.com> Subject: [PATCH v2 0/4] KVM: Don't let dirty ring resets pass dirty_index 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" Fix a userspace-triggerable WARN in kvm_dirty_ring_push() reported by syzbot[1], and tidy up the memory ordering and annotations around the dirty ring indices while in the area. Patch 1 is the fix. The dirty ring is mapped writable into userspace, so userspace can set KVM_DIRTY_GFN_F_RESET on entries that KVM never pushed. KVM_RESET_DIRTY_RINGS then advances reset_index past dirty_index, the u32 math in kvm_dirty_ring_used() underflows, and the next push hits WARN_ON_ONCE(kvm_dirty_ring_full(ring)). Stop resetting entries once reset_index catches up to dirty_index, and bump dirty_index before publishing the DIRTY flag, so that a VMM that reaps the ring while the vCPU is running never has a legitimately harvested entry skipped. Patch 2 converts the DIRTY side of the flags handshake with userspace to smp_store_release(), to match the acquire/release semantics already used for the RESET side. No functional change intended. Patch 3 is optional. It marks the remaining concurrent accesses to the ring indices: WRITE_ONCE() for reset_index, which the vCPU reads with READ_ONCE(), and READ_ONCE() for the reads of the other task's index in the kvm_dirty_ring_push and kvm_dirty_ring_reset tracepoints. I'm not aware of any KCSAN reports or miscompiles, so this mostly documents the concurrency and ensures the compiler can't tear or refetch the accesses. Feel free to drop it if that is appropriate. Patch 4 adds a regression test for patch 1 to dirty_log_test. It fails without patch 1 (KVM resets two entries it never pushed), and passes with the series applied, including with a minimally sized ring so that the vCPU repeatedly exits with KVM_EXIT_DIRTY_RING_FULL while the ring is being reaped. v2: - Rebase onto v7.4-rc5. - Selftest: clear the bogus RESET flags after verifying that KVM doesn't reset them, so that they can't race with KVM pushing to the entries while the ring is being reaped[2]. v1: https://lore.kernel.org/kvm/20261117212913.3634487-1-jthoughton@google.com [1] https://lore.kernel.org/kvm/6afbad4d.f3cfa7c9.844a4.0002.GAE@google.com [2] https://lore.kernel.org/kvm/sashiko-outbox-219706@kernel.org James Houghton (4): KVM: Don't reset dirty ring entries that KVM hasn't pushed KVM: Use smp_store_release() to mark dirty ring entries as dirtied KVM: Annotate concurrent accesses to dirty ring indices KVM: selftests: Verify KVM doesn't reset unused dirty ring entries include/trace/events/kvm.h | 4 ++-- tools/testing/selftests/kvm/dirty_log_test.c | 25 ++++++++++++++++++++ virt/kvm/dirty_ring.c | 23 +++++++++++++----- 3 files changed, 44 insertions(+), 8 deletions(-) base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.56.1.915.g69d5ec7298-goog