From nobody Sun Feb 8 06:21:44 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2ACD09454 for ; Sat, 11 Jan 2025 01:04:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557455; cv=none; b=ProfQ5jP7NzhehbiFJnmTx01/vtO7w4HrtEBcSolfPpFvThVyDGDAs1obbMfgEezPpLRkZe51UJGlV/iZPUOSOX69VoVWr+HqCxrbTxQxvNTaKNBVZQL8ms6KXPPEMU+BFk57UKUavz8ZhpULpqidZ+cWCpfD8IpYCzx529buLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557455; c=relaxed/simple; bh=Y87IV5Xpnh+jgd2FxKTBjhTRZsxpUL28iWLdqHeOOJE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oKNCu60ePVUhKwB8vf4GWHvDAaoUzQ3BjQmTBS73diEKUVFYba1MBO4zXG3x1BqG6XiulYo1UuCQ7oDp2+ZI6BuFkjLGKcNfNlbkafgX6xQ3wPFudzhS2nYGxbe6LJxlNiv+sv3EKPbDGsUZZAGOb+4jMcWwKsDLG9Ayie4bJ+o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SLqjOoSr; arc=none smtp.client-ip=209.85.216.73 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SLqjOoSr" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef909597d9so7425471a91.3 for ; Fri, 10 Jan 2025 17:04:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736557453; x=1737162253; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Cy2pnqQ2fSzBgMiJrF6O6bTMaX9oe7O9BUSjAJGBR+0=; b=SLqjOoSryRv1XsjDEu9pvYFmixZ+y5ks07rjgwvUxIAntrzq17skWikXjZE7jSr3rE 7KJAobL/YPvRbgXyjcVI1S9LG9eaOpITDpC2HEwB0N27EHMW7o8Fj8QVLKoUizjcxKpL hc4EXRWI9BDwFyW0IXJGVF7lACrfncT6xOJN7iuMHnBm/nBMTHtXpxqQEkfFQV3lHxyS qD6k7ENdlurjmXlEotNJA9SGISGvAcSGSO4D0C0rOFvY1H2vohHxbVp56gW87UbDsiF0 zom9usmNp3OnXkTAe8aAV9UfKFut00bcYUBRqrgOC3tMlZ/I/UDFOWG7I2rgBpWGOWTz NVfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736557453; x=1737162253; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Cy2pnqQ2fSzBgMiJrF6O6bTMaX9oe7O9BUSjAJGBR+0=; b=McqGgUuf++ZSDWZZQ2bLY8Cp7cw4wEWqAevp4Ir9RUnek38DkY9ejL/DIgKBUqXhLf n1PFB6Eaniyme7KJs3w2OTT6cDyTmtbaTEYflYoQRf9cgO59bTYh8fldEjU9tM3z+dRV Y3FharBu0760n4OYhP7n3u9CAnXTgzbQhBha0LWRHT81j42qsAnirHZH7d6To6bhB4wK z9dJqePhRolHN2f7Sbn2B1lYN8Y8DVajO8oZDWNnQTy5kYL+QjeJYoIn++nwLDkCqBdb +LknJ8jXVdSFO0rrSco9Apq3HkA0NdNLy+HJ16nE5lSP0tHLBMWvwbLjJplnnjdhQd+C jUYQ== X-Forwarded-Encrypted: i=1; AJvYcCXlN1Vyu2eRYtUPF8L/LmWXcFL4iOEff8wX2UWniowqUPqIp0UKF5Ro/n9OqVvUTKD6OBIbbx132vS0E68=@vger.kernel.org X-Gm-Message-State: AOJu0YzaAoYBUpoFckdb831CChmcjH/D8fwQDBOLJZ3rSI+Ok2XF35v/ 8FUIvXMj/WLBWFLYfFwfPwT85BjZF8NroCjULUFqh5A14rhz+R4UVRXkfYkRSnaJ8qKZx/B4rOY pgg== X-Google-Smtp-Source: AGHT+IEiLT8u+IwJHWKAAuUTYAyOIXDxsf9AxyDDoSlDQKImRmj4TEJpD11oowKHMJ/OBwLbI6iHkHS2p5s= X-Received: from pfbln9.prod.google.com ([2002:a05:6a00:3cc9:b0:72a:f9c7:a2ed]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:408e:b0:725:9dc7:4f8b with SMTP id d2e1a72fcca58-72d21f562c6mr17589432b3a.15.1736557453546; Fri, 10 Jan 2025 17:04:13 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 17:04:05 -0800 In-Reply-To: <20250111010409.1252942-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111010409.1252942-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111010409.1252942-2-seanjc@google.com> Subject: [PATCH 1/5] KVM: Bound the number of dirty ring entries in a single reset at INT_MAX From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu , Yan Zhao , Maxim Levitsky , Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Cap the number of ring entries that are reset in a single ioctl to INT_MAX to ensure userspace isn't confused by a wrap into negative space, and so that, in a truly pathological scenario, KVM doesn't miss a TLB flush due to the count wrapping to zero. While the size of the ring is fixed at 0x10000 entries and KVM (currently) supports at most 4096, userspace is allowed to harvest entries from the ring while the reset is in-progress, i.e. it's possible for the ring to always harvested entries. Opportunistically return an actual error code from the helper so that a future fix to handle pending signals can gracefully return -EINTR. Cc: Peter Xu Cc: Yan Zhao Cc: Maxim Levitsky Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking") Signed-off-by: Sean Christopherson --- include/linux/kvm_dirty_ring.h | 8 +++++--- virt/kvm/dirty_ring.c | 10 +++++----- virt/kvm/kvm_main.c | 9 ++++++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/linux/kvm_dirty_ring.h b/include/linux/kvm_dirty_ring.h index 4862c98d80d3..82829243029d 100644 --- a/include/linux/kvm_dirty_ring.h +++ b/include/linux/kvm_dirty_ring.h @@ -49,9 +49,10 @@ static inline int kvm_dirty_ring_alloc(struct kvm_dirty_= ring *ring, } =20 static inline int kvm_dirty_ring_reset(struct kvm *kvm, - struct kvm_dirty_ring *ring) + struct kvm_dirty_ring *ring, + int *nr_entries_reset) { - return 0; + return -ENOENT; } =20 static inline void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, @@ -81,7 +82,8 @@ int kvm_dirty_ring_alloc(struct kvm_dirty_ring *ring, int= index, u32 size); * called with kvm->slots_lock held, returns the number of * processed pages. */ -int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring); +int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring, + int *nr_entries_reset); =20 /* * returns =3D0: successfully pushed diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c index 7bc74969a819..2faf894dec5a 100644 --- a/virt/kvm/dirty_ring.c +++ b/virt/kvm/dirty_ring.c @@ -104,19 +104,19 @@ static inline bool kvm_dirty_gfn_harvested(struct kvm= _dirty_gfn *gfn) return smp_load_acquire(&gfn->flags) & KVM_DIRTY_GFN_F_RESET; } =20 -int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring) +int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring, + int *nr_entries_reset) { u32 cur_slot, next_slot; u64 cur_offset, next_offset; unsigned long mask; - int count =3D 0; struct kvm_dirty_gfn *entry; bool first_round =3D true; =20 /* This is only needed to make compilers happy */ cur_slot =3D cur_offset =3D mask =3D 0; =20 - while (true) { + while (likely((*nr_entries_reset) < INT_MAX)) { entry =3D &ring->dirty_gfns[ring->reset_index & (ring->size - 1)]; =20 if (!kvm_dirty_gfn_harvested(entry)) @@ -129,7 +129,7 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_di= rty_ring *ring) kvm_dirty_gfn_set_invalid(entry); =20 ring->reset_index++; - count++; + (*nr_entries_reset)++; /* * Try to coalesce the reset operations when the guest is * scanning pages in the same slot. @@ -166,7 +166,7 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_di= rty_ring *ring) =20 trace_kvm_dirty_ring_reset(ring); =20 - return count; + return 0; } =20 void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 9d54473d18e3..2d63b4d46ccb 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4877,15 +4877,18 @@ static int kvm_vm_ioctl_reset_dirty_pages(struct kv= m *kvm) { unsigned long i; struct kvm_vcpu *vcpu; - int cleared =3D 0; + int cleared =3D 0, r; =20 if (!kvm->dirty_ring_size) return -EINVAL; =20 mutex_lock(&kvm->slots_lock); =20 - kvm_for_each_vcpu(i, vcpu, kvm) - cleared +=3D kvm_dirty_ring_reset(vcpu->kvm, &vcpu->dirty_ring); + kvm_for_each_vcpu(i, vcpu, kvm) { + r =3D kvm_dirty_ring_reset(vcpu->kvm, &vcpu->dirty_ring, &cleared); + if (r) + break; + } =20 mutex_unlock(&kvm->slots_lock); =20 --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Sun Feb 8 06:21:44 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3E502033A for ; Sat, 11 Jan 2025 01:04:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557457; cv=none; b=LHscUBe3wGjMVe7iVrjshlrpAMzBxdcLb4mfBzr8h5Q4CBaHzQIZsogvgexz8cfgBiUJkS4uZ+wjC6zNLveXTJXZT3yYmD53sCb2PV+ZskjSmoXmDhtB6NyIITXCXfm4Iyn6vL0yUHdeyxhO/LsjDSWc84hobKUgB6VCnRI7GVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557457; c=relaxed/simple; bh=xIq2EpqRyA2AXMVcTKTfGnLziwxJNZwMY0SpruaP6dQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fHgcBV8gpZ+YhEEMzq9zI5a/IZ64ZrTV65ZR+cEzFloR3/xfDUD7BFKXrY4COATYVrOTLXD+hx4VkgxhF/EwcqCu6icqPfbyQ+AGioxcMif3/Ft6XOJtJYZCMrjNf7A0G5g7IkPRLzwnVXLYrXxOMiiP4v6gXSNWO80IKIRqjE0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4qs5oJlp; arc=none smtp.client-ip=209.85.216.73 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4qs5oJlp" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ee46799961so6714185a91.2 for ; Fri, 10 Jan 2025 17:04:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736557455; x=1737162255; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=qLONhXo3aW3WBH6fEqaAtaxz8PcaBZClYQxjCE2fW5k=; b=4qs5oJlp1NssvfORJ4Fz8n0E7/9Q9YF6XBJ3KFSFnXvaaAWsBWw1SVWB7aW1eRxyck Wn65+z/O5Lf/+namTAQj9Lb95a2TFGMpZNE+B93P4SwWgV9s1ytC2WTpktBQTUrDjuWu MRDtshiYsP7ay+wgdPVxE/xxIpU6kTv6pzGbAo2N+Ys1aETlClp+FqZrchyifISTaZiZ /H+lxtQByZMBz6nbxJ2OFA69N9NanXwdSR8CdNFKfjuT3GqGAVhtI7/HD3MzZsFzNqsQ TWBQ2RB7ojnF3/PIp7AR5JEFoKOc4mjiiCKKwq9gasQ1lBQgF0R0448AUghuwSc+uVEP YSGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736557455; x=1737162255; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qLONhXo3aW3WBH6fEqaAtaxz8PcaBZClYQxjCE2fW5k=; b=EM3N35SwW04CdUsRUHcMqLnCGpZU62H8CtEUiUXYHRxFHH4UW5/LcRXB4vYM35e17s p9Te6l4BQQp0xmqGpSsDmcFP3RFadZ3ZDZJ/XVqfkobTKeAS6ppe3/J5/eV0pO2ESNpU M/hecEouaqi8rveikl98i8mud8TWq7XNirOmGE0IhOvRXHG5+LVj+miGdwWBCFKW8Uu6 5fihoBJPeo61c+w55Z3Apdk+/0QRRezz9SA+36tcEpEl7hrY5tyO+TsIxhohFdmo4o74 YKNEu4b2fmOzMUZ081F/5LeHnTTy8w+NrYiC7uFRks6RNmWI5/O1FrUg+aIPK8g3Cne2 nINA== X-Forwarded-Encrypted: i=1; AJvYcCXFvyHlu4/7MvvCP9DwY44jsMYO6UhOYbhfGQc+xn4Yook65NyIIXWSegtI6KUhGPCcPXW68hrCfpCTW7o=@vger.kernel.org X-Gm-Message-State: AOJu0Yyn7i5ga24G+Z4bRdy8QnnryW0AEYYPMPopXrvMS8sJu318vfpP o9ohzB4m9msIqhLvbG49rE4w6o53xNn6jN2A0an0dpjgpPZCICYwt9oHmrjyV5w9FczyfrgBC9t zTA== X-Google-Smtp-Source: AGHT+IHH0Fmds1PmmVVZr37IVwKQmveAAVRjINELYisNKzj4RspyqLxQGG7qVO+G/iH8KfvqVTQcx1AsZ7I= X-Received: from pjbqi17.prod.google.com ([2002:a17:90b:2751:b0:2ea:5084:5297]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2b8e:b0:2ee:d433:7c54 with SMTP id 98e67ed59e1d1-2f548eceae7mr17545829a91.19.1736557455348; Fri, 10 Jan 2025 17:04:15 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 17:04:06 -0800 In-Reply-To: <20250111010409.1252942-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111010409.1252942-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111010409.1252942-3-seanjc@google.com> Subject: [PATCH 2/5] KVM: Bail from the dirty ring reset flow if a signal is pending From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu , Yan Zhao , Maxim Levitsky , Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Abort a dirty ring reset if the current task has a pending signal, as the hard limit of INT_MAX entries doesn't ensure KVM will respond to a signal in a timely fashion. Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking") Signed-off-by: Sean Christopherson --- virt/kvm/dirty_ring.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c index 2faf894dec5a..a81ad17d5eef 100644 --- a/virt/kvm/dirty_ring.c +++ b/virt/kvm/dirty_ring.c @@ -117,6 +117,9 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_di= rty_ring *ring, cur_slot =3D cur_offset =3D mask =3D 0; =20 while (likely((*nr_entries_reset) < INT_MAX)) { + if (signal_pending(current)) + return -EINTR; + entry =3D &ring->dirty_gfns[ring->reset_index & (ring->size - 1)]; =20 if (!kvm_dirty_gfn_harvested(entry)) --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Sun Feb 8 06:21:44 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F41D54765 for ; Sat, 11 Jan 2025 01:04:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557459; cv=none; b=Ofq6VZUVW37w/+hj7drnc2rBacqKI76VkwX3wTDQd4RgMvNUG02x5eqMvISMtTza/sVrAQeclQK8RPQLYRDt4M7D6rHZkW/stNpZag7/OzljfdoWuiO+ED6Xzvqz1j29TnJiMf1ZfpdJNgDPODQf6wlZCJTopDtc7bWq89omHXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557459; c=relaxed/simple; bh=38yKh50jmTM2QnQCwQHA2pD1K0ArDB2ne9BY9y0yDsE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kD82Y1ULmcNMddQeRnphT4SQU7X+XKQf468SYxnCs+kc5Mz/Vc7koPg+WJnIARogGlrx7CcRY07kaZMHvIOn/5AciY0saBvaiNnEgnz26vahnfpXsF33HGDM3W4spGTNUAsUuq5wkghFwJzieaTqvLMHF64XF1vLIFC80Zkqu/E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OtwLxFBh; arc=none smtp.client-ip=209.85.216.74 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OtwLxFBh" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef9e4c5343so6698840a91.0 for ; Fri, 10 Jan 2025 17:04:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736557457; x=1737162257; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=qztCq3yN4c7V5VVWefuCXzG4z+KIAvAw68bTrwIGEiM=; b=OtwLxFBhXTN+tJ+2KUpBBqdXmvLd6QivviqhooqBIdpPjzAQnYEbmbLWXTQ9OKKAhZ h0PMoIdDfPxwSd4KEOmmwsQXCGZmb+s+5LUFFIqPq0kmHUK3gKlMT/7oZtwcS2OMmdCu fJKGsKXxyoCtAxaspOiRLZ95rwhrWXwyFL9Rr3QO8LLMcLC1oekmaPZ3Jse/NZmc4gFP qCAKEV0Q+pAU76dvkHUW/p7t++udkCGmsHuthpVWKbm1PfqhORHzxQyRORavYKTC3LU3 +A4WvGYoqIkrAYcMm6tiMEW+Uw04OReJlLPcrpSiJWcP4I/8eX5xNBVU1lxKO5MPXJ/G 9Ocg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736557457; x=1737162257; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qztCq3yN4c7V5VVWefuCXzG4z+KIAvAw68bTrwIGEiM=; b=E30TIHmqZJiiBw0o9Tq4ZQmMg3tQ83/Qui45Hhd5aCgIXajfmeLO+9Y+4ozRelM8nc ZdmnLffxNjDp28cKbVBocdWSABxB/A8FurkpTSpsx/knpEmCx6Ik/pEaPtpePdQbt3nr IQ5xKhzeLgRu9lMj69q2sExm15B92L77RJnpORxwMxM/DkrJSxUD9TBqDudhBHjfCdJI 7TYqRzwUB9cwQrggLPZat5Yyo0YU7+oH/jcqdDBnHJ+sTFu4sLtnfPoesxGfcBORHdWz c4bisQFYl/r6xFV8Z7agacOWo7lLu4TOc1gpxeyVtLI7+BM+8L52Ke72TsUbs+gYAp4C X4bw== X-Forwarded-Encrypted: i=1; AJvYcCU6+r0XOf0EV+aGQgIEa5yJYiqGmBu3K4CB8MG4mfZrpoO/OnTfKk6KDGZfjdLDAubEbSI688GBjNtE5Z0=@vger.kernel.org X-Gm-Message-State: AOJu0YwDrcSUX6Ay5Pz1RoXdG+66G1SkJ4lCSpiUfLtVSJG7BdnvdPqA OCo/VJXtBrxVejqUJwYBaO59q/yiEhyea2B+szyyRT72MbMgrnFhb4gyjbOQPRe9uc2N5+GkqbP UwA== X-Google-Smtp-Source: AGHT+IGWC2DYlQkU/EcPOOmwCPjaBsLhvhJQuYZtuDOFD2C754CM0kGzgUC90dVegSd+WxYgpA7SBB0o1w0= X-Received: from pjc4.prod.google.com ([2002:a17:90b:2f44:b0:2f2:ea3f:34c3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:274e:b0:2f4:4003:f3ea with SMTP id 98e67ed59e1d1-2f5490f19c7mr19708701a91.33.1736557457046; Fri, 10 Jan 2025 17:04:17 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 17:04:07 -0800 In-Reply-To: <20250111010409.1252942-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111010409.1252942-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111010409.1252942-4-seanjc@google.com> Subject: [PATCH 3/5] KVM: Conditionally reschedule when resetting the dirty ring From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu , Yan Zhao , Maxim Levitsky , Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When resetting a dirty ring, conditionally reschedule on each iteration after the first. The recently introduced hard limit mitigates the issue of an endless reset, but isn't sufficient to completely prevent RCU stalls, soft lockups, etc., nor is the hard limit intended to guard against such badness. Note! Take care to check for reschedule even in the "continue" paths, as a pathological scenario (or malicious userspace) could dirty the same gfn over and over, i.e. always hit the continue path. rcu: INFO: rcu_sched self-detected stall on CPU rcu: 4-....: (5249 ticks this GP) idle=3D51e4/1/0x4000000000000000 softi= rq=3D309/309 fqs=3D2563 rcu: (t=3D5250 jiffies g=3D-319 q=3D608 ncpus=3D24) CPU: 4 UID: 1000 PID: 1067 Comm: dirty_log_test Tainted: G L = 6.13.0-rc3-17fa7a24ea1e-HEAD-vm #814 Tainted: [L]=3DSOFTLOCKUP Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:kvm_arch_mmu_enable_log_dirty_pt_masked+0x26/0x200 [kvm] Call Trace: kvm_reset_dirty_gfn.part.0+0xb4/0xe0 [kvm] kvm_dirty_ring_reset+0x58/0x220 [kvm] kvm_vm_ioctl+0x10eb/0x15d0 [kvm] __x64_sys_ioctl+0x8b/0xb0 do_syscall_64+0x5b/0x160 entry_SYSCALL_64_after_hwframe+0x4b/0x53 Tainted: [L]=3DSOFTLOCKUP Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:kvm_arch_mmu_enable_log_dirty_pt_masked+0x17/0x200 [kvm] Call Trace: kvm_reset_dirty_gfn.part.0+0xb4/0xe0 [kvm] kvm_dirty_ring_reset+0x58/0x220 [kvm] kvm_vm_ioctl+0x10eb/0x15d0 [kvm] __x64_sys_ioctl+0x8b/0xb0 do_syscall_64+0x5b/0x160 entry_SYSCALL_64_after_hwframe+0x4b/0x53 Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking") Signed-off-by: Sean Christopherson --- virt/kvm/dirty_ring.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c index a81ad17d5eef..37eb2b7142bd 100644 --- a/virt/kvm/dirty_ring.c +++ b/virt/kvm/dirty_ring.c @@ -133,6 +133,16 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_d= irty_ring *ring, =20 ring->reset_index++; (*nr_entries_reset)++; + + /* + * While the size of each ring is fixed, it's possible for the + * ring to be constantly re-dirtied/harvested while the reset + * is in-progress (the hard limit exists only to guard against + * wrapping the count into negative space). + */ + if (!first_round) + cond_resched(); + /* * Try to coalesce the reset operations when the guest is * scanning pages in the same slot. --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Sun Feb 8 06:21:44 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E73B021106 for ; Sat, 11 Jan 2025 01:04:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557460; cv=none; b=qWwaMkFr3n42/MHMmoxq1u4M8GlKZUBgejyrr7WGMePDPRrSzIi7trycFsfv98PV4tc1XaCPW3RuS+CqKc8cG1PUb7liynRQVXEFQWfd8wPM79hlMWFkC6+idd/k0B4tQzxH8g+jXkOiGr6RjFp9r/AqRlEDz1qkUSOK1ijjVHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557460; c=relaxed/simple; bh=4AOojhK7RgZpfHVFyZZg/F1b+66In+n0W7FklHWokKs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UxocSp4FgQYrQt24NfnqaStjfLNjLsz9UtQd4STyypGba7pdhE1qYwd1EcCgq0bkXZdTDpaDM0AINN5mZEtVJ2Txke1jZ6IEJKqCESG/eSaEt6EIEdLblEoEPzPe4+ky6TuVX0728Jmf9C7vv+0tTzdUyG4OkzrKKP6MKkR1Fls= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SS4Dk6LS; arc=none smtp.client-ip=209.85.216.74 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SS4Dk6LS" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef9da03117so6730253a91.1 for ; Fri, 10 Jan 2025 17:04:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736557458; x=1737162258; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=oGi7slrEyYAnwuVNpr6ZrhtkCDMVeK/CUwkbMoeq/cE=; b=SS4Dk6LSfD/sZVtJuIMAVMzMhrSGOGVePoGpUZYs9dQbksms/LORsyTEjwcBrCiPsm Zx7QTXq1sVxb/+vcWoutAybzoOisOCfZ9MnwaZYq7SP6twNqXyf522SM4BIbVdZ4z3MN zf3yj1qjBK4Z60Fv/IoxZNu1gzAzsK6Z4AilPcbZV58NKEHEZD4j284X7kW1wcRw1Ttt 6Wb4NMMzHwag04ebjlaDTguwP/Vn4VYkMM90SojZYjHg12YeMX40p5/zAlwK3rPyFxqy rC8f3Jsl5EbxgU+E/U03ZGZcvwyyz7W09BBQCIG4+Q9r8CPciGEpcPS0lzKsPbCWK1fe E7PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736557458; x=1737162258; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oGi7slrEyYAnwuVNpr6ZrhtkCDMVeK/CUwkbMoeq/cE=; b=m6jeYkwd2Kcj5F+Qe2SSguvJn0oWNpAygwRJRqe/h4boy9LyOjkwlpDhE9yQgvuuSa IPmq0J0qHm0I9k8cXX+WeZCjVgBQJ/zYeLP5uFczjrRMsTiLhe63lq+Ksx5qXTuUV6uK sPl4MXpsP+p9nG+ws6wBR2Jl1sntnbKkgKq/KM1/2cleprG8QF+LZv/cCpz1KSZEnnsI yU2e91zwSMoYglumIhE6FLJeVzDitXUF+taJOQE0aAuw41fv+ZvYnDUOwZvUV3WShF/u m7T8pNZDVmiLyfjCPyPb7cfaXqEC4p2U3ROIroFyW2070IfurJ8DPyzt8rzXsNfoHJ3O xcUw== X-Forwarded-Encrypted: i=1; AJvYcCWKZcm4Nwy3t+A3eeKpBFl78MY8tiMraw7ORVVNc+Z6dSXmkSZTcN/D3eTvLfNdh4dX41yP2lTKrYmDGpI=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2PifrEyhLGlVfYSoJMofl8jLO1w39yXkqNDiej2EnbNDlAmIc wXeUQsFTYopcF5YXeqGYjXLBJVDi0mtWbEo24orzwigM89rvx6tY6cCFCBE3PUS6duVyt3x5cH4 wSA== X-Google-Smtp-Source: AGHT+IEU0JMvjhrXcRzpNyyxrvcQqODJ1wZPu5PG2qjeWRbJZlS0/3S5feFaFOwVGoqKiMB61Iex5Pfl42k= X-Received: from pfwz22.prod.google.com ([2002:a05:6a00:1d96:b0:725:e37e:7451]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3e13:b0:725:eacf:cfda with SMTP id d2e1a72fcca58-72d21fe0263mr18801122b3a.17.1736557458526; Fri, 10 Jan 2025 17:04:18 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 17:04:08 -0800 In-Reply-To: <20250111010409.1252942-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111010409.1252942-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111010409.1252942-5-seanjc@google.com> Subject: [PATCH 4/5] KVM: Check for empty mask of harvested dirty ring entries in caller From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu , Yan Zhao , Maxim Levitsky , Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When resetting a dirty ring, explicitly check that there is work to be done before calling kvm_reset_dirty_gfn(), e.g. if no harvested entries are found and/or on the loop's first iteration, and delete the extremely misleading comment "This is only needed to make compilers happy". KVM absolutely relies on mask to be zero-initialized, i.e. the comment is an outright lie. Furthermore, the compiler is right to complain that KVM is calling a function with uninitialized data, as there are no guarantees the implementation details of kvm_reset_dirty_gfn() will be visible to kvm_dirty_ring_reset(). While the flaw could be fixed by simply deleting (or rewording) the comment, and duplicating the check is unfortunate, checking mask in the caller will allow for additional cleanups. Opportunisticaly drop the zero-initialization of cur_slot and cur_offset. If a bug were introduced where either the slot or offset was consumed before mask is set to a non-zero value, then it is highly desirable for the compiler (or some other sanitizer) to yell. Cc: Peter Xu Cc: Yan Zhao Cc: Maxim Levitsky Signed-off-by: Sean Christopherson --- virt/kvm/dirty_ring.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c index 37eb2b7142bd..95ab0e3cf9da 100644 --- a/virt/kvm/dirty_ring.c +++ b/virt/kvm/dirty_ring.c @@ -55,9 +55,6 @@ static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot= , u64 offset, u64 mask) struct kvm_memory_slot *memslot; int as_id, id; =20 - if (!mask) - return; - as_id =3D slot >> 16; id =3D (u16)slot; =20 @@ -109,13 +106,10 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_= dirty_ring *ring, { u32 cur_slot, next_slot; u64 cur_offset, next_offset; - unsigned long mask; + unsigned long mask =3D 0; struct kvm_dirty_gfn *entry; bool first_round =3D true; =20 - /* This is only needed to make compilers happy */ - cur_slot =3D cur_offset =3D mask =3D 0; - while (likely((*nr_entries_reset) < INT_MAX)) { if (signal_pending(current)) return -EINTR; @@ -163,14 +157,31 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_= dirty_ring *ring, continue; } } - kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); + + /* + * Reset the slot for all the harvested entries that have been + * gathered, but not yet fully processed. + */ + if (mask) + kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); + + /* + * The current slot was reset or this is the first harvested + * entry, (re)initialize the metadata. + */ cur_slot =3D next_slot; cur_offset =3D next_offset; mask =3D 1; first_round =3D false; } =20 - kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); + /* + * Perform a final reset if there are harvested entries that haven't + * been processed. The loop only performs a reset when an entry can't + * be coalesced, i.e. always leaves at least one entry pending. + */ + if (mask) + kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); =20 /* * The request KVM_REQ_DIRTY_RING_SOFT_FULL will be cleared --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Sun Feb 8 06:21:44 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC8871474DA for ; Sat, 11 Jan 2025 01:04:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557463; cv=none; b=OUS2hWeG8hURajzJYEJ9uJqf9lXlqL0WEQZtQHiZjNB4gn2mXKviaiG5OUaz19r4UHbmSuMdV6A4Y+kkE7w6BZv86Pi1ZzWmtboVhz8u88cF7RxsxvbO504R5AaYBW+BJsUWiagdkIRXA3uHyS4SE4anJiOi0pcqfTcgfaEQ/Qk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736557463; c=relaxed/simple; bh=GuHaUoJc3kSQRY79m3B6lcu5nI5McGM/FGvOckPh/E4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UOJw82ny790k/O1+F6etxjD/DKgGoUcSIBrDY11tf+tHOuuTCgxIdRW1krzDsUtkE/sKUVf90IHN0zGzXmFSUeRloptGNMlv9BUUKtetJZN9BQHkeGNLFZSrceECFVrmJYORiUNMVbL7BsX98+DD+DQGH5PW3tpyXMOAmgGqv/M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=A6ZI9l3A; arc=none smtp.client-ip=209.85.216.74 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="A6ZI9l3A" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef6ef9ba3fso4775895a91.2 for ; Fri, 10 Jan 2025 17:04:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736557460; x=1737162260; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=tVYbCX2RqPz2ig5eCEENys/M+ZhhahZJXVbEX0SbPgo=; b=A6ZI9l3AManGIewequpKdftymqIyiAKM8gSb7o4oxl+z8KJ5jPV3Pn3YUM6CTJejSW zizpj1S01JQnas30amkG2ucciGB2wdF5Vcb3YwlCDE+m3aAPjzVqpO/8NEnmGVDfLs98 m+sKTIl82YnGSpYwFKBbA7xmTtPVGO7XTpVV5TwThk90I5ie3oOCNOV5VqAbXmZJOHyI 1u0ytJZLS1flfT7YwzwJHXvD9EQKn/EZyUQqx+pVJ8lEYlOibV0nzNXqHmwrplY8g7IB GUUmpZ0mPLE30uAya38B/FngIOH9e+Jq7PaZi1ZQnCUghwkZP15kOfwkZpE5Z+gK5kD1 0DHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736557460; x=1737162260; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tVYbCX2RqPz2ig5eCEENys/M+ZhhahZJXVbEX0SbPgo=; b=FHQuA4HRcwLuSRZrcpsa1a9DNEQO9tHX4EvEr+qSFe/a5vLIX3aiUMH22g9oWUMSi7 TwXe6J9l4a0+ZNtsur94d3Yufbf7caTmEMoKtS1XVIxtBtdKlcharm1tFJu7IY9cf94V jy3mFmHpP7jA1CKdPF3vM+FWIIoKxxhRDAOGGVs79VfzBvJnCvc5Hvpu4ErkBrd4Lii7 u4dakJOtqPodzloBk3KD92TBv+PmGCbj6BOEyW2nD3vil09iuxCIVUQf+rr+cpuHQJGe uNxdv3JIrHwWQtHAg/uPcUHTDooOuPxFFd+xNYi9kBz6kZ0109vo1y2XUIaFCEbj1xJl 9xIQ== X-Forwarded-Encrypted: i=1; AJvYcCVLMURpmI03iqApC8n/5SzrCTsK0ozGoIUn1T4xU3rGBCJ37Y475ythSTwhsPU4UUqewLZO8LvK045F+aE=@vger.kernel.org X-Gm-Message-State: AOJu0Yx8D+5h8q3w0B/Hmdjn0XCn+uIKiIK7cvrIVEUIXeGKciObUSrS e99WRbct8fb4oDwMSbckB0ed6VZxHYPiyTw4cF/ABWtZixBYTujD6kk5zdifF3+7Ggu1nuVTet0 VlA== X-Google-Smtp-Source: AGHT+IFuOLVPsL+7D49WGbUBlqnRDugnmoXi0Idkmj5v+pibXtrs67/9IO5GA1O0JQ4pwUp/7WG4rrkB8Ok= X-Received: from pjbsw3.prod.google.com ([2002:a17:90b:2c83:b0:2f4:3ea1:9033]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:c2c7:b0:2ee:e518:c1d8 with SMTP id 98e67ed59e1d1-2f548f1c3f0mr18718339a91.30.1736557460283; Fri, 10 Jan 2025 17:04:20 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 17:04:09 -0800 In-Reply-To: <20250111010409.1252942-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111010409.1252942-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111010409.1252942-6-seanjc@google.com> Subject: [PATCH 5/5] KVM: Use mask of harvested dirty ring entries to coalesce dirty ring resets From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu , Yan Zhao , Maxim Levitsky , Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use "mask" instead of a dedicated boolean to track whether or not there is at least one to-be-reset entry for the current slot+offset. In the body of the loop, mask is zero only on the first iteration, i.e. !mask is equivalent to first_round. Opportunstically combine the adjacent "if (mask)" statements into a single if-statement. No function change intended. Cc: Peter Xu Cc: Yan Zhao Cc: Maxim Levitsky Signed-off-by: Sean Christopherson --- virt/kvm/dirty_ring.c | 60 +++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c index 95ab0e3cf9da..9b23f86ff7b6 100644 --- a/virt/kvm/dirty_ring.c +++ b/virt/kvm/dirty_ring.c @@ -108,7 +108,6 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_di= rty_ring *ring, u64 cur_offset, next_offset; unsigned long mask =3D 0; struct kvm_dirty_gfn *entry; - bool first_round =3D true; =20 while (likely((*nr_entries_reset) < INT_MAX)) { if (signal_pending(current)) @@ -128,42 +127,42 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_= dirty_ring *ring, ring->reset_index++; (*nr_entries_reset)++; =20 - /* - * While the size of each ring is fixed, it's possible for the - * ring to be constantly re-dirtied/harvested while the reset - * is in-progress (the hard limit exists only to guard against - * wrapping the count into negative space). - */ - if (!first_round) + if (mask) { + /* + * While the size of each ring is fixed, it's possible + * for the ring to be constantly re-dirtied/harvested + * while the reset is in-progress (the hard limit exists + * only to guard against the count becoming negative). + */ cond_resched(); =20 - /* - * Try to coalesce the reset operations when the guest is - * scanning pages in the same slot. - */ - if (!first_round && next_slot =3D=3D cur_slot) { - s64 delta =3D next_offset - cur_offset; + /* + * Try to coalesce the reset operations when the guest + * is scanning pages in the same slot. + */ + if (next_slot =3D=3D cur_slot) { + s64 delta =3D next_offset - cur_offset; =20 - if (delta >=3D 0 && delta < BITS_PER_LONG) { - mask |=3D 1ull << delta; - continue; - } + if (delta >=3D 0 && delta < BITS_PER_LONG) { + mask |=3D 1ull << delta; + continue; + } =20 - /* Backwards visit, careful about overflows! */ - if (delta > -BITS_PER_LONG && delta < 0 && - (mask << -delta >> -delta) =3D=3D mask) { - cur_offset =3D next_offset; - mask =3D (mask << -delta) | 1; - continue; + /* Backwards visit, careful about overflows! */ + if (delta > -BITS_PER_LONG && delta < 0 && + (mask << -delta >> -delta) =3D=3D mask) { + cur_offset =3D next_offset; + mask =3D (mask << -delta) | 1; + continue; + } } - } =20 - /* - * Reset the slot for all the harvested entries that have been - * gathered, but not yet fully processed. - */ - if (mask) + /* + * Reset the slot for all the harvested entries that + * have been gathered, but not yet fully processed. + */ kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); + } =20 /* * The current slot was reset or this is the first harvested @@ -172,7 +171,6 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_di= rty_ring *ring, cur_slot =3D next_slot; cur_offset =3D next_offset; mask =3D 1; - first_round =3D false; } =20 /* --=20 2.47.1.613.gc27f4b7a9f-goog