From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796332857; b=FoAC9RSyMbhzo3zQnoAMJLO5wH2Z9TMgoxdmizXheNpCgkMQN3CluSR1USzafmFfkZKm awXE2szcYYmetV4JAiq/mE2CtJ11REfun5IrHcWwMS1jsAkXL65FSOsylmSGIWaZ5jGOu LGbCeMNSbeSf39c0gfgGSiyIYX0num7D9Dj0zoiBxoV5MEGbI6uJo8gE1cFdckESAQ4Gd k5/DpKlWu8eDVIzmEAUcQ2YwepiOFvkyQzoubINrr2O69pqLpSjiYNYnEtIgQJpGSwN7D aV9mqagJndxqPGaj+r4b+H2gt+dQh6g2pAq5+qa0qCSY/C9wAtBNxag7grXtyWNXaug== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796332857; 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=Oj9HltxrC2ykMnsqQC4uIIB6KiJvEakWIM/HKarh42E=; b=R0+ePKQQjb5EtBbS2ycp/G4b0sNYRJcwXRMCMZo68ugUAfds3ckL2PZY3fN3HshoRmmJ 0NTyOas7u9IiuHNFMPlasGd/sl10Y1kxX94x1DMY2e/Jet0YOxNE733bvw+CKOiknyjtA z1mf6rWmPq/yjvVVKuZcwYDsXcJP6exJ9Kd36HI4wzdxqiIEgT+PhAW+8zaNP3g5UHPwH +CQsFxheG3y8vNgNTwqtHy6yxFi7u8KDzQwgu8bN2YXYgBTGL9lm03MW73H2r+EKeqyfs hzIQPE3KE3WGetvTTzjLWtdbMt9DukLAfE0TOPzcGrcmSNoiMC+s7PMNP0U0Le6Ivkg== 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=C414xvqG; arc=none smtp.remote-ip=209.85.214.197 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (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 BD238606A332 for ; Thu, 3 Dec 2026 21:20:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BD238606A332 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=C414xvqG Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2f0717e54b0so2858985ad.0 for ; Thu, 03 Dec 2026 13:20:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796332856; x=1796937656; 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=Oj9HltxrC2ykMnsqQC4uIIB6KiJvEakWIM/HKarh42E=; b=C414xvqGpCA4R1B7EYrPex9/P1JGsULRneni4L40J1rI9RHTmG8ey1Ni0KOoobUapx VVKFJq7yBA0xsjvw1fvp1lwfpWVRmJ7REwq7at9KHN/otFcogKlvEf1styJmIkUUTXpn 8oQSUatGN2Z5yi6n4Lhnp54JevmxQoF8zjFxbm8m72iQkh/hl21h57k0Tk/V1CQB92Qc E1r3W6WBe6BIDvKHzctcpzXe5LSiY5/GGi9tXnQToCrvXpypdSh03iqRzpmfxnbE8RPg 9ifIjGIFLf6l1kMc1jgjuhvqUumqYe/5o3EHvXnuoX4mR5aVUU+fGcOMrAEBkTOPMSzb 4IKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796332856; x=1796937656; 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=Oj9HltxrC2ykMnsqQC4uIIB6KiJvEakWIM/HKarh42E=; b=gWsKsWGfSt8H3MafmjMdD4C0jcKqSWZSA7osJqX0pQafkQb8ImSG4Z9yLvB6IilYoA tvOOI6xNXTW8aoLJYqbDBY08a8O3mLGKgjDxuFMbDt2O/T7GiWhVDkUKWpduSSOjTjOX qKQjid10a1XbT4UtxbtzVfdzoasx8f4SMp7RLpFunjW0UU162uvlW98eRyRWM4B9yNE6 rt/6K1TevK9XCxn9NYKhmRa34+snyfe9zirPbMLSuMak0EBwlD+tD/OqEzwS0zWE6ugD EYBbmSOhI1JB6KKTsCFP1MCPNQVCLodL5VLID+LqSVfRZI81UtX5UGva8i+q9AvOkYZZ ouUg== X-Forwarded-Encrypted: i=1; AL+hErFtnuhBm3GBoOpMk0zeD/Tbo8sNyZ37W/PtQqQ14EbJVpHuSWfUbw+jLXy1wpSq3prgqBvSbkyw11xovUU=@vger.kernel.org X-Gm-Message-State: APuU4Z90kJ32ByIHPkF1L8Bw+m1t0W+oq3io6KthuJRp8q29F/8F40w0 JBJ5aOquIZ4s9+n2XGV4r1/IQI8Q7IqTwCYiGfgIk8ODzDAEnBFtjSNV09f4aOtMuh6XnzqFvbg pMZ+pct51k00QtWSuW3XUDQ== X-Received: from ploq12.prod.google.com ([2002:a17:903:1aac:b0:2f1:5369:ae89]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e807:b0:2ef:ee1b:8e52 with SMTP id d9443c01a7336-2f03f1a4a23mr480839545ad.32.1796332855794; Thu, 03 Dec 2026 13:20:55 -0800 (PST) Date: Thu, 3 Dec 2026 21:20:50 +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-2-jthoughton@google.com> Subject: [PATCH v2 1/4] KVM: Don't reset dirty ring entries that KVM hasn't pushed 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" 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