All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: Simplify backwards dirty ring batch check
@ 2026-09-10 11:20 Peng Hao
  2026-09-10 14:40 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Peng Hao @ 2026-09-10 11:20 UTC (permalink / raw)
  To: pbonzini, seanjc; +Cc: kvm

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-09-10 15:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-10 11:20 [PATCH] KVM: Simplify backwards dirty ring batch check Peng Hao
2026-09-10 14:40 ` Sean Christopherson
2026-09-10 15:28   ` Paolo Bonzini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.