osdep: Fix ROUND_UP(64-bit, 32-bit)
PATCH 1/1 osdep: Fix ROUND_UP(64-bit, 32-bit)
When using bit-wise operations that exploit the power-of-two nature of the second argument of ROUND_UP(), we still need to ensure that the mask is as wide as the first argument (done by using addition of 0 to force proper arithmetic promotion). Unpatched, ROUND_UP(2ULL*1024*1024*1024*1024, 512) produces 0, instead of the intended 2TiB.
Broken since its introduction in commit 292c8e50 (v1.5.0).
---
I did not audit to see how many potential users of ROUND_UP are actually passing in different sized types where the first argument can be larger than UINT32_MAX; I stumbled across the problem when iotests 190 started failing on a patch where I added a new use. We can either be conservative and put this on qemu-stable no matter what, or go through the effort of an audit to see what might be broken (many callers in the block layer, but not just there).
--- include/qemu/osdep.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/qemu/osdep.h +3 -3
@@ -189,13 +189,13 @@ extern int daemon(int, int);/* Round number up to multiple. Requires that d be a power of 2 (see * QEMU_ALIGN_UP for a safer but slower version on arbitrary -* numbers) */ +* numbers); works even if d is a smaller type than n. */ #ifndef ROUND_UP -#define ROUND_UP (n,d) (((n) + (d) - 1) & -(d)) +#define ROUND_UP (n, d) (((n) + (d) - 1) & -((n) - (n) + (d)))#endif #ifndef DIV_ROUND_UP -#define DIV_ROUND_UP (n,d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d))#endif /*
Signature · 1 line
2.13.5
Quoted 37 lines · Eric Blake
When using bit-wise operations that exploit the power-of-two nature of the second argument of
ROUND_UP(), we still need to ensure that the mask is as wide as the first argument (done by using addition of 0 to force proper arithmetic promotion). Unpatched,ROUND_UP(2ULL*1024*1024*1024*1024, 512)produces 0, instead of the intended 2TiB.Broken since its introduction in commit
292c8e50(v1.5.0).Signed-off-by Eric Blake <[email protected]>---
I did not audit to see how many potential users ofROUND_UPare actually passing in different sized types where the first argument can be larger thanUINT32_MAX; I stumbled across the problem when iotests 190 started failing on a patch where I added a new use. We can either be conservative and put this on qemu-stable no matter what, or go through the effort of an audit to see what might be broken (many callers in the block layer, but not just there).--- include/qemu/osdep.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)include/qemu/osdep.h +1
-1@@ -189,13 +189,13 @@ extern int daemon(int, int);/* Round number up to multiple. Requires that d be a power of 2 (see * QEMU_ALIGN_UP for a safer but slower version on arbitrary -* numbers) */ +* numbers); works even if d is a smaller type than n. */ #ifndef ROUND_UP
include/qemu/osdep.h extern int daemon(int, int); · line 201
Quoted 10 lines
include/qemu/osdep.h +2
-2-#define ROUND_UP (n,d) (((n) + (d) - 1) & -(d)) +#define ROUND_UP (n, d) (((n) + (d) - 1) & -((n) - (n) + (d)))#endif #ifndef DIV_ROUND_UP -#define DIV_ROUND_UP (n,d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d))#endif /*
Paolo
Quoted 30 lines · Eric Blake
When using bit-wise operations that exploit the power-of-two nature of the second argument of
ROUND_UP(), we still need to ensure that the mask is as wide as the first argument (done by using addition of 0 to force proper arithmetic promotion). Unpatched,ROUND_UP(2ULL*1024*1024*1024*1024, 512)produces 0, instead of the intended 2TiB.Broken since its introduction in commit
292c8e50(v1.5.0).Signed-off-by Eric Blake <[email protected]>---
I did not audit to see how many potential users ofROUND_UPare actually passing in different sized types where the first argument can be larger thanUINT32_MAX; I stumbled across the problem when iotests 190 started failing on a patch where I added a new use. We can either be conservative and put this on qemu-stable no matter what, or go through the effort of an audit to see what might be broken (many callers in the block layer, but not just there).--- include/qemu/osdep.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)include/qemu/osdep.h +0
-0
include/qemu/osdep.h extern int daemon(int, int); · line 195
Quoted 9 lines
include/qemu/osdep.h +2
-2@@ -189,13 +189,13 @@ extern int daemon(int, int);/* Round number up to multiple. Requires that d be a power of 2 (see * QEMU_ALIGN_UP for a safer but slower version on arbitrary -* numbers) */ +* numbers); works even if d is a smaller type than n. */ #ifndef ROUND_UP -#define ROUND_UP (n,d) (((n) + (d) - 1) & -(d)) +#define ROUND_UP (n, d) (((n) + (d) - 1) & -((n) - (n) + (d)))#endif
Another way to widen the mask as necessary would be:
(((n) + (d) - 1) & -(0 ? (n) : (d)))
include/qemu/osdep.h extern int daemon(int, int); · line 196
Quoted 1 line
include/qemu/osdep.h +0
-0From the C standard,
6.5.15 Conditional operator
5 If both the second and third operands have arithmetic type, the result
type that would be determined by the usual arithmetic conversions, were they applied to those two operands, is the type of the result. [...]
Using the conditional operator is *perhaps* better than addition and subtraction, because it would keep the previous trait of ROUND_UP that "n" is evaluated only once:
6.5.15 Conditional operator
4 The first operand is evaluated; there is a sequence point after its
evaluation. The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below. [...]
Although, I admit that invoking ROUND_UP with any side effects in the arguments would be crazy to begin with...
include/qemu/osdep.h extern int daemon(int, int); · line 199
Quoted 5 lines
include/qemu/osdep.h +1
-1#ifndef DIV_ROUND_UP -#define DIV_ROUND_UP (n,d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d))#endif
This looks like an independent whitespace fix; should it be in this patch?
Thanks
Laszlo
Quoted 14 lines · Laszlo Ersek
Quoted 12 lines · Eric Blake
When using bit-wise operations that exploit the power-of-two nature of the second argument of
ROUND_UP(), we still need to ensure that the mask is as wide as the first argument (done by using addition of 0 to force proper arithmetic promotion). Unpatched,ROUND_UP(2ULL*1024*1024*1024*1024, 512)produces 0, instead of the intended 2TiB.Broken since its introduction in commit
292c8e50(v1.5.0).Signed-off-by Eric Blake <[email protected]>
Quoted 6 lines
Quoted 2 lines
+#define
ROUND_UP(n, d)(((n) + (d) - 1) & -((n) - (n) + (d)))
#endifAnother way to widen the mask as necessary would be:
(((n) + (d) - 1) & -(0 ? (n) : (d)))
Oh, I like that even better!
Quoted 6 lines
include/qemu/osdep.h extern int daemon(int, int); · line 199
Quoted 4 lines
#ifndef
DIV_ROUND_UPinclude/qemu/osdep.h +1
-1-#define DIV_ROUND_UP (n,d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d))#endif This looks like an independent whitespace fix; should it be in this patch?
checkpatch complained about the pre-patch spacing in ROUND_UP, and DIV_ROUND_UP had the same issue 2 lines later. But you're right that it's not strictly necessary (or that I should at least call it out in the commit message).
v2 coming up.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org