Skip to contentSkip to conversation

virtio: fix vring_align() on 64-bit win32 platforms

PATCH Superseded Andrew Baumann 3 participants 4 messages Mar 21, 2017 – Mar 22, 2017

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio: fix vring_align() on 64-bit win32 platforms

"long" is 32-bits on win32, but we need to promote it to a 64-bit hwaddr before negating, or else the top half of the address is truncated

---
 include/hw/virtio/virtio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/virtio/virtio.h +1 -1
index 15efcf2..a0a8543 100644
@@ -34,7 +34,7 @@ struct VirtQueue;
 static inline hwaddr vring_align(hwaddr addr,
                                              unsigned long align)
 {
-    return (addr + align - 1) & ~(align - 1);
+    return (addr + align - 1) & ~(hwaddr)(align - 1);
 }
 
 typedef struct VirtQueue VirtQueue;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 13 lines · Andrew Baumann

"long" is 32-bits on win32, but we need to promote it to a 64-bit hwaddr before negating, or else the top half of the address is truncated

---
 include/hw/virtio/virtio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/virtio/virtio.h +0 -0
index 15efcf2..a0a8543 100644

include/hw/virtio/virtio.h struct VirtQueue; · line 37

Quoted 5 lines
include/hw/virtio/virtio.h +1 -1
@@ -34,7 +34,7 @@ struct VirtQueue;
 static inline hwaddr vring_align(hwaddr addr,
                                              unsigned long align)
 {
-    return (addr + align - 1) & ~(align - 1);
+    return (addr + align - 1) & ~(hwaddr)(align - 1);

Why not just use the QEMU_ALIGN_DOWN macro, instead of open-coding it?

(Hmm - a good BiteSized task might be to come up with a Coccinelle script to help replace all open-coded rounding functions with appropriate macro calls instead)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 12 lines

From: Eric Blake [mailto:[email protected]]
Sent: Tuesday, 21 March 2017 15:52

Quoted 8 lines · Andrew Baumann

"long" is 32-bits on win32, but we need to promote it to a 64-bit hwaddr before negating, or else the top half of the address is truncated

---
 include/hw/virtio/virtio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

include/hw/virtio/virtio.h struct VirtQueue; · line 37

Quoted 10 lines
include/hw/virtio/virtio.h +1 -1
index 15efcf2..a0a8543 100644
@@ -34,7 +34,7 @@ struct VirtQueue;
 static inline hwaddr vring_align(hwaddr addr,
                                              unsigned long align)
 {
-    return (addr + align - 1) & ~(align - 1);
+    return (addr + align - 1) & ~(hwaddr)(align - 1);

Why not just use the QEMU_ALIGN_DOWN macro, instead of open-coding it?

Well, this code is aligning up, but yes the ALIGN_UP macro looks like it should also avoid the type promotion problem. This patch is just the minimally-invasive change after discovering the bug.

Let me know if you want me to spin another patch with the macro.

Andrew

#3 lore raw

Am 22.03.2017 um 00:06 schrieb Andrew Baumann:

Quoted 25 lines
Quoted 12 lines

From: Eric Blake [mailto:[email protected]]
Sent: Tuesday, 21 March 2017 15:52

Quoted 8 lines · Andrew Baumann

"long" is 32-bits on win32, but we need to promote it to a 64-bit hwaddr before negating, or else the top half of the address is truncated

---
 include/hw/virtio/virtio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

include/hw/virtio/virtio.h struct VirtQueue; · line 37

Quoted 10 lines
include/hw/virtio/virtio.h +1 -1
index 15efcf2..a0a8543 100644
@@ -34,7 +34,7 @@ struct VirtQueue;
 static inline hwaddr vring_align(hwaddr addr,
                                              unsigned long align)
 {
-    return (addr + align - 1) & ~(align - 1);
+    return (addr + align - 1) & ~(hwaddr)(align - 1);

Why not just use the QEMU_ALIGN_DOWN macro, instead of open-coding it?

Quoted 5 lines

Well, this code is aligning up, but yes the ALIGN_UP macro looks like it should also avoid the type promotion problem. This patch is just the minimally-invasive change after discovering the bug.

Let me know if you want me to spin another patch with the macro.

Andrew

Yes, please use QEMU_ALIGN_UP in an updated patch. This is a bug fix needed for v2.9.0.

Fixing all other code locations which round up or down with Coccinelle is a separate task, nothing which is needed for the next QEMU version.

Thanks,
Stefan