Skip to contentSkip to conversation

virt/kvm: Replace unsafe shift check with __builtin_clzll()

PATCH In review Peng Hao 2 participants 2 messages Dec 23, 2025 KVM

lore
Share & tools
lore

Peng Hao

author
#0 lore raw

PATCH 1/1 virt/kvm: Replace unsafe shift check with __builtin_clzll()

The (mask << x >> x) == mask pattern to check for shift overflow is considered undefined behavior by modern compilers and can be optimized away incorrectly.

Replace it with a safe and explicit check using __builtin_clzll() to ensure the left shift does not discard any bits. This avoids UB and is more robust.

---
 virt/kvm/dirty_ring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/dirty_ring.c +1 -1
index 02bc6b00d76c..926a4c0115d1 100644
@@ -171,7 +171,7 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
 
 				/* Backwards visit, careful about overflows! */
 				if (delta > -BITS_PER_LONG && delta < 0 &&
-				(mask << -delta >> -delta) == mask) {
+				(unsigned long)-delta <= __builtin_clzll(mask)) {
 					cur_offset = next_offset;
 					mask = (mask << -delta) | 1;
 					continue;
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 3 lines · Peng Hao

The (mask << x >> x) == mask pattern to check for shift overflow is considered undefined behavior by modern compilers and can be optimized away incorrectly.

No, not for unsigned integers.

If anything, you could track clz(mask) in a variable, which should be a tiny bit more efficient:

virt/kvm/dirty_ring.c +12 -11
index 02bc6b00d76c..30be665ec9b6 100644
@@ -118,7 +118,8 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
 	 * a sequence of gfns X, X-1, ... X-N-1 can be batched.
 	 */
 	u32 cur_slot, next_slot;
-	u64 cur_offset, next_offset;
+	u64 cur_offset, next_offset, min_offset;
+	int zeroes = 0;
 	unsigned long mask = 0;
 	struct kvm_dirty_gfn *entry;
 
@@ -164,16 +165,15 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
 			if (next_slot == cur_slot) {
 				s64 delta = next_offset - cur_offset;
 
-				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;
+				if (delta >= -zeroes && delta < BITS_PER_LONG) {
+					if (delta >= 0) {
+						mask |= 1ull << delta;
+						zeroes = BITS_PER_LONG - 1 - delta;
+					} else {
+						cur_offset = next_offset;
+						mask = (mask << -delta) | 1;
+						zeroes += delta;
+					}
 					continue;
 				}
 			}
@@ -192,6 +192,7 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
 		cur_slot = next_slot;
 		cur_offset = next_offset;
 		mask = 1;
+		zeroes = BITS_PER_LONG - 1;
 	}
 
 	/*

Review

Quoted 4 lines

/* Backwards visit, careful about overflows! */ if (delta > -BITS_PER_LONG && delta < 0 &&

(quoted diff) +1 -1
-				(mask << -delta >> -delta) == mask) {
+				(unsigned long)-delta <= __builtin_clzll(mask)) {

As an aside, the first term of the && is "-delta < BITS_PER_LONG", so it is redundant with the new term that you've added.

"delta < 0" can also be removed, since (unsigned long) -delta will be very large for positive delta. So you could have written this as simply "if ((unsigned long)-delta <= __builtin_clzll(mask))".

Paolo