From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Peter Xu <peterx@redhat.com>, Yan Zhao <yan.y.zhao@intel.com>,
Maxim Levitsky <mlevitsk@redhat.com>,
Sean Christopherson <seanjc@google.com>
Subject: [PATCH v2 5/5] KVM: Use mask of harvested dirty ring entries to coalesce dirty ring resets
Date: Thu, 8 May 2025 07:10:12 -0700 [thread overview]
Message-ID: <20250508141012.1411952-6-seanjc@google.com> (raw)
In-Reply-To: <20250508141012.1411952-1-seanjc@google.com>
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 <peterx@redhat.com>
Cc: Yan Zhao <yan.y.zhao@intel.com>
Cc: Maxim Levitsky <mlevitsk@redhat.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
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 a3434be8f00d..934828d729e5 100644
--- a/virt/kvm/dirty_ring.c
+++ b/virt/kvm/dirty_ring.c
@@ -121,7 +121,6 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
u64 cur_offset, next_offset;
unsigned long mask = 0;
struct kvm_dirty_gfn *entry;
- bool first_round = true;
while (likely((*nr_entries_reset) < INT_MAX)) {
if (signal_pending(current))
@@ -141,42 +140,42 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
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)
+ 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();
- /*
- * Try to coalesce the reset operations when the guest is
- * scanning pages in the same slot.
- */
- if (!first_round && next_slot == cur_slot) {
- s64 delta = next_offset - cur_offset;
+ /*
+ * Try to coalesce the reset operations when the guest
+ * is scanning pages in the same slot.
+ */
+ if (next_slot == cur_slot) {
+ s64 delta = next_offset - cur_offset;
- if (delta >= 0 && delta < BITS_PER_LONG) {
- mask |= 1ull << delta;
- continue;
- }
+ if (delta >= 0 && delta < BITS_PER_LONG) {
+ mask |= 1ull << delta;
+ continue;
+ }
- /* Backwards visit, careful about overflows! */
- if (delta > -BITS_PER_LONG && delta < 0 &&
- (mask << -delta >> -delta) == mask) {
- cur_offset = next_offset;
- mask = (mask << -delta) | 1;
- continue;
+ /* Backwards visit, careful about overflows! */
+ if (delta > -BITS_PER_LONG && delta < 0 &&
+ (mask << -delta >> -delta) == mask) {
+ cur_offset = next_offset;
+ mask = (mask << -delta) | 1;
+ continue;
+ }
}
- }
- /*
- * 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);
+ }
/*
* The current slot was reset or this is the first harvested
@@ -185,7 +184,6 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
cur_slot = next_slot;
cur_offset = next_offset;
mask = 1;
- first_round = false;
}
/*
--
2.49.0.1015.ga840276032-goog
next prev parent reply other threads:[~2025-05-08 14:10 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-08 14:10 [PATCH v2 0/5] KVM: Dirty ring fixes and cleanups Sean Christopherson
2025-05-08 14:10 ` [PATCH v2 1/5] KVM: Bound the number of dirty ring entries in a single reset at INT_MAX Sean Christopherson
2025-05-13 1:25 ` Binbin Wu
2025-05-08 14:10 ` [PATCH v2 2/5] KVM: Bail from the dirty ring reset flow if a signal is pending Sean Christopherson
2025-05-08 14:10 ` [PATCH v2 3/5] KVM: Conditionally reschedule when resetting the dirty ring Sean Christopherson
2025-05-12 22:02 ` James Houghton
2025-05-13 14:13 ` Sean Christopherson
2025-05-13 22:27 ` James Houghton
2025-05-14 14:24 ` Sean Christopherson
2025-05-08 14:10 ` [PATCH v2 4/5] KVM: Check for empty mask of harvested dirty ring entries in caller Sean Christopherson
2025-05-13 9:17 ` Binbin Wu
2025-05-13 12:51 ` Gupta, Pankaj
2025-05-08 14:10 ` Sean Christopherson [this message]
2025-05-12 22:33 ` [PATCH v2 5/5] KVM: Use mask of harvested dirty ring entries to coalesce dirty ring resets James Houghton
2025-05-13 12:16 ` Gupta, Pankaj
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=20250508141012.1411952-6-seanjc@google.com \
--to=seanjc@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mlevitsk@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=yan.y.zhao@intel.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