[PATCH 08/16] hw/virtio: Replace HOST_BIG_ENDIAN #ifdef with runtime if() check

Philippe Mathieu-Daudé posted 16 patches 1 month ago
Maintainers: Richard Henderson <richard.henderson@linaro.org>, Helge Deller <deller@gmx.de>, Gerd Hoffmann <kraxel@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Dmitry Osipenko <dmitry.osipenko@collabora.com>, Stefano Garzarella <sgarzare@redhat.com>, Laurent Vivier <laurent@vivier.eu>, Jason Wang <jasowang@redhat.com>, Peter Maydell <peter.maydell@linaro.org>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Aurelien Jarno <aurelien@aurel32.net>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Aleksandar Rikalo <arikalo@gmail.com>, Nicholas Piggin <npiggin@gmail.com>, Chinmay Rath <rathc@linux.ibm.com>, Harsh Prateek Bora <harshpb@linux.ibm.com>, Paolo Bonzini <pbonzini@redhat.com>, Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <dbarboza@ventanamicro.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, David Hildenbrand <david@redhat.com>, Ilya Leoshkevich <iii@linux.ibm.com>, Thomas Huth <thuth@redhat.com>, Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>, Artyom Tarasenko <atar4qemu@gmail.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>
[PATCH 08/16] hw/virtio: Replace HOST_BIG_ENDIAN #ifdef with runtime if() check
Posted by Philippe Mathieu-Daudé 1 month ago
Replace compile-time #ifdef with a runtime check to ensure all code
paths are built and tested. This reduces build-time configuration
complexity and improves maintainability.

No functional change intended.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/hw/virtio/virtio-access.h | 6 +-----
 hw/virtio/vhost.c                 | 7 +++----
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/include/hw/virtio/virtio-access.h b/include/hw/virtio/virtio-access.h
index 07aae69042a..80328912ad3 100644
--- a/include/hw/virtio/virtio-access.h
+++ b/include/hw/virtio/virtio-access.h
@@ -149,11 +149,7 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr)
 
 static inline uint16_t virtio_tswap16(VirtIODevice *vdev, uint16_t s)
 {
-#if HOST_BIG_ENDIAN
-    return virtio_access_is_big_endian(vdev) ? s : bswap16(s);
-#else
-    return virtio_access_is_big_endian(vdev) ? bswap16(s) : s;
-#endif
+    return HOST_BIG_ENDIAN ^ virtio_access_is_big_endian(vdev) ? s : bswap16(s);
 }
 
 static inline uint16_t virtio_lduw_phys_cached(VirtIODevice *vdev,
diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index 266a11514a1..6343477b42f 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -1168,11 +1168,10 @@ static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
     if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
         return false;
     }
-#if HOST_BIG_ENDIAN
-    return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
-#else
+    if (HOST_BIG_ENDIAN) {
+        return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
+    }
     return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
-#endif
 }
 
 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
-- 
2.51.0


Re: [PATCH 08/16] hw/virtio: Replace HOST_BIG_ENDIAN #ifdef with runtime if() check
Posted by Farhan Ali 1 month ago
On 10/10/2025 6:42 AM, Philippe Mathieu-Daudé wrote:
> Replace compile-time #ifdef with a runtime check to ensure all code
> paths are built and tested. This reduces build-time configuration
> complexity and improves maintainability.
>
> No functional change intended.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/hw/virtio/virtio-access.h | 6 +-----
>   hw/virtio/vhost.c                 | 7 +++----
>   2 files changed, 4 insertions(+), 9 deletions(-)
>
> diff --git a/include/hw/virtio/virtio-access.h b/include/hw/virtio/virtio-access.h
> index 07aae69042a..80328912ad3 100644
> --- a/include/hw/virtio/virtio-access.h
> +++ b/include/hw/virtio/virtio-access.h
> @@ -149,11 +149,7 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr)
>   
>   static inline uint16_t virtio_tswap16(VirtIODevice *vdev, uint16_t s)
>   {
> -#if HOST_BIG_ENDIAN
> -    return virtio_access_is_big_endian(vdev) ? s : bswap16(s);
> -#else
> -    return virtio_access_is_big_endian(vdev) ? bswap16(s) : s;
> -#endif
> +    return HOST_BIG_ENDIAN ^ virtio_access_is_big_endian(vdev) ? s : bswap16(s);

This patch breaks virtio devices(at least input/net devices) on s390x. I 
am not sure if ^ is the right check here? Changing the logic back to how 
it was fixes it for me.

Thanks

Farhan

>   }
>   
>   static inline uint16_t virtio_lduw_phys_cached(VirtIODevice *vdev,
> diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
> index 266a11514a1..6343477b42f 100644
> --- a/hw/virtio/vhost.c
> +++ b/hw/virtio/vhost.c
> @@ -1168,11 +1168,10 @@ static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
>       if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
>           return false;
>       }
> -#if HOST_BIG_ENDIAN
> -    return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
> -#else
> +    if (HOST_BIG_ENDIAN) {
> +        return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
> +    }
>       return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
> -#endif
>   }
>   
>   static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,

Re: [PATCH 08/16] hw/virtio: Replace HOST_BIG_ENDIAN #ifdef with runtime if() check
Posted by Lei Yang 1 month ago
Hi Philippe Mathieu-Daudé

According to my test result this change introduced a minor bug, qemu
output error messages "qemu-system-x86_64: virtio-blk missing headers"
reproduced command:
/usr/local/bin/qemu-system-x86_64 -M q35 -m 8G -smp 8 -cpu host
-enable-kvm -device VGA,bus=pcie.0,addr=0x2 -drive
file=///home/fedora-40.qcow2,media=disk,if=virtio -device
virtio-net-pci,mac=00:11:22:33:44:00,netdev=netdev1,id=net1,bus=pcie.0,addr=0x7
 -netdev tap,id=netdev1,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown
-vnc :0  -monitor stdio
QEMU 10.1.50 monitor - type 'help' for more information
(qemu) qemu-system-x86_64: virtio-blk missing headers

Thanks
Lei

On Sat, Oct 11, 2025 at 3:25 AM Farhan Ali <alifm@linux.ibm.com> wrote:
>
>
> On 10/10/2025 6:42 AM, Philippe Mathieu-Daudé wrote:
> > Replace compile-time #ifdef with a runtime check to ensure all code
> > paths are built and tested. This reduces build-time configuration
> > complexity and improves maintainability.
> >
> > No functional change intended.
> >
> > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> > ---
> >   include/hw/virtio/virtio-access.h | 6 +-----
> >   hw/virtio/vhost.c                 | 7 +++----
> >   2 files changed, 4 insertions(+), 9 deletions(-)
> >
> > diff --git a/include/hw/virtio/virtio-access.h b/include/hw/virtio/virtio-access.h
> > index 07aae69042a..80328912ad3 100644
> > --- a/include/hw/virtio/virtio-access.h
> > +++ b/include/hw/virtio/virtio-access.h
> > @@ -149,11 +149,7 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr)
> >
> >   static inline uint16_t virtio_tswap16(VirtIODevice *vdev, uint16_t s)
> >   {
> > -#if HOST_BIG_ENDIAN
> > -    return virtio_access_is_big_endian(vdev) ? s : bswap16(s);
> > -#else
> > -    return virtio_access_is_big_endian(vdev) ? bswap16(s) : s;
> > -#endif
> > +    return HOST_BIG_ENDIAN ^ virtio_access_is_big_endian(vdev) ? s : bswap16(s);
>
> This patch breaks virtio devices(at least input/net devices) on s390x. I
> am not sure if ^ is the right check here? Changing the logic back to how
> it was fixes it for me.
>
> Thanks
>
> Farhan
>
> >   }
> >
> >   static inline uint16_t virtio_lduw_phys_cached(VirtIODevice *vdev,
> > diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
> > index 266a11514a1..6343477b42f 100644
> > --- a/hw/virtio/vhost.c
> > +++ b/hw/virtio/vhost.c
> > @@ -1168,11 +1168,10 @@ static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
> >       if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
> >           return false;
> >       }
> > -#if HOST_BIG_ENDIAN
> > -    return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
> > -#else
> > +    if (HOST_BIG_ENDIAN) {
> > +        return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
> > +    }
> >       return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
> > -#endif
> >   }
> >
> >   static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
>