Skip to content

Commit 7d82410

Browse files
gkurzmstsirkin
authored andcommitted
virtio: add explicit big-endian support to memory accessors
The current memory accessors logic is: - little endian if little_endian - native endian (i.e. no byteswap) if !little_endian If we want to fully support cross-endian vhost, we also need to be able to convert to big endian. Instead of changing the little_endian argument to some 3-value enum, this patch changes the logic to: - little endian if little_endian - big endian if !little_endian The native endian case is handled by all users with a trivial helper. This patch doesn't change any functionality, nor it does add overhead. Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Cornelia Huck <cornelia.huck@de.ibm.com> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
1 parent ab27c07 commit 7d82410

File tree

6 files changed

+24
-15
lines changed

6 files changed

+24
-15
lines changed

drivers/net/macvtap.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ struct macvtap_queue {
5151

5252
static inline bool macvtap_is_little_endian(struct macvtap_queue *q)
5353
{
54-
return q->flags & MACVTAP_VNET_LE;
54+
return q->flags & MACVTAP_VNET_LE ||
55+
virtio_legacy_is_little_endian();
5556
}
5657

5758
static inline u16 macvtap16_to_cpu(struct macvtap_queue *q, __virtio16 val)

drivers/net/tun.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ struct tun_struct {
208208

209209
static inline bool tun_is_little_endian(struct tun_struct *tun)
210210
{
211-
return tun->flags & TUN_VNET_LE;
211+
return tun->flags & TUN_VNET_LE ||
212+
virtio_legacy_is_little_endian();
212213
}
213214

214215
static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)

drivers/vhost/vhost.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
175175

176176
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
177177
{
178-
return vhost_has_feature(vq, VIRTIO_F_VERSION_1);
178+
return vhost_has_feature(vq, VIRTIO_F_VERSION_1) ||
179+
virtio_legacy_is_little_endian();
179180
}
180181

181182
/* Memory accessors */

include/linux/virtio_byteorder.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,61 @@
33
#include <linux/types.h>
44
#include <uapi/linux/virtio_types.h>
55

6-
/*
7-
* Low-level memory accessors for handling virtio in modern little endian and in
8-
* compatibility native endian format.
9-
*/
6+
static inline bool virtio_legacy_is_little_endian(void)
7+
{
8+
#ifdef __LITTLE_ENDIAN
9+
return true;
10+
#else
11+
return false;
12+
#endif
13+
}
1014

1115
static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
1216
{
1317
if (little_endian)
1418
return le16_to_cpu((__force __le16)val);
1519
else
16-
return (__force u16)val;
20+
return be16_to_cpu((__force __be16)val);
1721
}
1822

1923
static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
2024
{
2125
if (little_endian)
2226
return (__force __virtio16)cpu_to_le16(val);
2327
else
24-
return (__force __virtio16)val;
28+
return (__force __virtio16)cpu_to_be16(val);
2529
}
2630

2731
static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
2832
{
2933
if (little_endian)
3034
return le32_to_cpu((__force __le32)val);
3135
else
32-
return (__force u32)val;
36+
return be32_to_cpu((__force __be32)val);
3337
}
3438

3539
static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
3640
{
3741
if (little_endian)
3842
return (__force __virtio32)cpu_to_le32(val);
3943
else
40-
return (__force __virtio32)val;
44+
return (__force __virtio32)cpu_to_be32(val);
4145
}
4246

4347
static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
4448
{
4549
if (little_endian)
4650
return le64_to_cpu((__force __le64)val);
4751
else
48-
return (__force u64)val;
52+
return be64_to_cpu((__force __be64)val);
4953
}
5054

5155
static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
5256
{
5357
if (little_endian)
5458
return (__force __virtio64)cpu_to_le64(val);
5559
else
56-
return (__force __virtio64)val;
60+
return (__force __virtio64)cpu_to_be64(val);
5761
}
5862

5963
#endif /* _LINUX_VIRTIO_BYTEORDER */

include/linux/virtio_config.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
207207

208208
static inline bool virtio_is_little_endian(struct virtio_device *vdev)
209209
{
210-
return virtio_has_feature(vdev, VIRTIO_F_VERSION_1);
210+
return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
211+
virtio_legacy_is_little_endian();
211212
}
212213

213214
/* Memory accessors */

include/linux/vringh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ static inline void vringh_notify(struct vringh *vrh)
228228

229229
static inline bool vringh_is_little_endian(const struct vringh *vrh)
230230
{
231-
return vrh->little_endian;
231+
return vrh->little_endian ||
232+
virtio_legacy_is_little_endian();
232233
}
233234

234235
static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)

0 commit comments

Comments
 (0)