From: Peng Hao <flyingpenghao@gmail.com>
To: pbonzini@redhat.com, seanjc@google.com
Cc: kvm@vger.kernel.org
Subject: [PATCH] KVM: Simplify backwards dirty ring batch check
Date: Thu, 10 Sep 2026 19:20:27 +0800 [thread overview]
Message-ID: <20260910112027.34581-1-flyingpeng@tencent.com> (raw)
The backwards coalescing path shifts mask left and back right to check
whether moving the batch base would discard set bits. The shift fits if
the distance is no greater than the number of leading zeroes in mask.
Use that directly. Convert delta to u64 before negating it so that a
positive delta, including one that wrapped to S64_MIN, is rejected by the
unsigned comparison without invoking signed overflow. Use __builtin_clzl()
to match the unsigned long type of mask on both 32-bit and 64-bit builds.
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
virt/kvm/dirty_ring.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
index 572b854edf74..8f39047ae7ab 100644
--- a/virt/kvm/dirty_ring.c
+++ b/virt/kvm/dirty_ring.c
@@ -170,9 +170,8 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
continue;
}
- /* Backwards visit, careful about overflows! */
- if (delta > -BITS_PER_LONG && delta < 0 &&
- (mask << -delta >> -delta) == mask) {
+ /* Backwards visit, but do not discard set bits. */
+ if (-(u64)delta <= __builtin_clzl(mask)) {
cur_offset = next_offset;
mask = (mask << -delta) | 1;
continue;
--
2.43.7
next reply other threads:[~2026-09-10 11:20 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-10 11:20 Peng Hao [this message]
2026-09-10 14:40 ` [PATCH] KVM: Simplify backwards dirty ring batch check Sean Christopherson
2026-09-10 15:28 ` Paolo Bonzini
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=20260910112027.34581-1-flyingpeng@tencent.com \
--to=flyingpenghao@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.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