2015-11-30 08:33:42

by Michael S. Tsirkin

[permalink] [raw]
Subject: [PATCH] tools/virtio: fix byteswap logic

commit cf561f0d2eb74574ad9985a2feab134267a9d298 ("virtio: introduce
virtio_is_little_endian() helper") changed byteswap logic to
skip feature bit checks for LE platforms, but didn't
update tools/virtio, so vring_bench started failing.

Update the copy under tools/virtio/ (TODO: find a way to avoid this code
duplication).

Cc: Greg Kurz <[email protected]>
Signed-off-by: Michael S. Tsirkin <[email protected]>
---
tools/virtio/linux/virtio_config.h | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/tools/virtio/linux/virtio_config.h b/tools/virtio/linux/virtio_config.h
index 806d683..57a6964 100644
--- a/tools/virtio/linux/virtio_config.h
+++ b/tools/virtio/linux/virtio_config.h
@@ -40,33 +40,39 @@ static inline void __virtio_clear_bit(struct virtio_device *vdev,
#define virtio_has_feature(dev, feature) \
(__virtio_test_bit((dev), feature))

+static inline bool virtio_is_little_endian(struct virtio_device *vdev)
+{
+ return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
+ virtio_legacy_is_little_endian();
+}
+
+/* Memory accessors */
static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
{
- return __virtio16_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
+ return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
}

static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
{
- return __cpu_to_virtio16(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
+ return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
}

static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
{
- return __virtio32_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
+ return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
}

static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
{
- return __cpu_to_virtio32(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
+ return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
}

static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
{
- return __virtio64_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
+ return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
}

static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
{
- return __cpu_to_virtio64(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
+ return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
}
-
--
MST


2015-11-30 09:39:05

by Gerg Kurz

[permalink] [raw]
Subject: Re: [PATCH] tools/virtio: fix byteswap logic

On Mon, 30 Nov 2015 10:33:34 +0200
"Michael S. Tsirkin" <[email protected]> wrote:

> commit cf561f0d2eb74574ad9985a2feab134267a9d298 ("virtio: introduce
> virtio_is_little_endian() helper") changed byteswap logic to
> skip feature bit checks for LE platforms, but didn't
> update tools/virtio, so vring_bench started failing.
>

Oops... yet another oversight...

> Update the copy under tools/virtio/ (TODO: find a way to avoid this code
> duplication).
>
> Cc: Greg Kurz <[email protected]>
> Signed-off-by: Michael S. Tsirkin <[email protected]>
> ---

Reviewed-by: Greg Kurz <[email protected]>

> tools/virtio/linux/virtio_config.h | 20 +++++++++++++-------
> 1 file changed, 13 insertions(+), 7 deletions(-)
>
> diff --git a/tools/virtio/linux/virtio_config.h b/tools/virtio/linux/virtio_config.h
> index 806d683..57a6964 100644
> --- a/tools/virtio/linux/virtio_config.h
> +++ b/tools/virtio/linux/virtio_config.h
> @@ -40,33 +40,39 @@ static inline void __virtio_clear_bit(struct virtio_device *vdev,
> #define virtio_has_feature(dev, feature) \
> (__virtio_test_bit((dev), feature))
>
> +static inline bool virtio_is_little_endian(struct virtio_device *vdev)
> +{
> + return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
> + virtio_legacy_is_little_endian();
> +}
> +
> +/* Memory accessors */
> static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
> {
> - return __virtio16_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
> }
>
> static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
> {
> - return __cpu_to_virtio16(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
> }
>
> static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
> {
> - return __virtio32_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
> }
>
> static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
> {
> - return __cpu_to_virtio32(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
> }
>
> static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
> {
> - return __virtio64_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
> }
>
> static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
> {
> - return __cpu_to_virtio64(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
> }
> -

2015-11-30 10:02:23

by Cornelia Huck

[permalink] [raw]
Subject: Re: [PATCH] tools/virtio: fix byteswap logic

On Mon, 30 Nov 2015 10:33:34 +0200
"Michael S. Tsirkin" <[email protected]> wrote:

> commit cf561f0d2eb74574ad9985a2feab134267a9d298 ("virtio: introduce
> virtio_is_little_endian() helper") changed byteswap logic to
> skip feature bit checks for LE platforms, but didn't
> update tools/virtio, so vring_bench started failing.
>
> Update the copy under tools/virtio/ (TODO: find a way to avoid this code
> duplication).

Not to mention that it is easy to miss the code duplication...

>
> Cc: Greg Kurz <[email protected]>
> Signed-off-by: Michael S. Tsirkin <[email protected]>
> ---
> tools/virtio/linux/virtio_config.h | 20 +++++++++++++-------
> 1 file changed, 13 insertions(+), 7 deletions(-)

Reviewed-by: Cornelia Huck <[email protected]>

2015-12-04 19:46:13

by Venkatesh Srinivas

[permalink] [raw]
Subject: Re: [PATCH] tools/virtio: fix byteswap logic

On Mon, Nov 30, 2015 at 10:33:34AM +0200, Michael S. Tsirkin wrote:
> commit cf561f0d2eb74574ad9985a2feab134267a9d298 ("virtio: introduce
> virtio_is_little_endian() helper") changed byteswap logic to
> skip feature bit checks for LE platforms, but didn't
> update tools/virtio, so vring_bench started failing.
>
> Update the copy under tools/virtio/ (TODO: find a way to avoid this code
> duplication).
>
> Cc: Greg Kurz <[email protected]>
> Signed-off-by: Michael S. Tsirkin <[email protected]>
> ---
> tools/virtio/linux/virtio_config.h | 20 +++++++++++++-------
> 1 file changed, 13 insertions(+), 7 deletions(-)
>
> diff --git a/tools/virtio/linux/virtio_config.h b/tools/virtio/linux/virtio_config.h
> index 806d683..57a6964 100644
> --- a/tools/virtio/linux/virtio_config.h
> +++ b/tools/virtio/linux/virtio_config.h
> @@ -40,33 +40,39 @@ static inline void __virtio_clear_bit(struct virtio_device *vdev,
> #define virtio_has_feature(dev, feature) \
> (__virtio_test_bit((dev), feature))
>
> +static inline bool virtio_is_little_endian(struct virtio_device *vdev)
> +{
> + return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
> + virtio_legacy_is_little_endian();
> +}
> +
> +/* Memory accessors */
> static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
> {
> - return __virtio16_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
> }
>
> static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
> {
> - return __cpu_to_virtio16(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
> }
>
> static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
> {
> - return __virtio32_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
> }
>
> static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
> {
> - return __cpu_to_virtio32(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
> }
>
> static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
> {
> - return __virtio64_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
> }
>
> static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
> {
> - return __cpu_to_virtio64(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
> + return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
> }

Tested patch with vring-bench. (I did something similar when reviving it).

Tested-by: Venkatesh Srinivas <[email protected]>

-- vs;