Skip to contentSkip to conversation

kvmtool: virtio: fix endian for big endian hosts

PATCH In review Ben Dooks 2 participants 6 messages Jan 15, 2025 – Jan 17, 2025 KVM

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

PATCH 1/1 kvmtool: virtio: fix endian for big endian hosts

When running on a big endian host, the virtio mmio-modern.c correctly sets all reads to return little endian values. However the header uses a 4 byte char for the magic value, which is always going to be in the correct endian regardless of host endian.

To make the simplest change, simply avoid endian convresion of the read of the magic value. This fixes the following bug from the guest:

[    0.592838] virtio-mmio 10020000.virtio: Wrong magic value 0x76697274!
---
 virtio/mmio-modern.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
virtio/mmio-modern.c +4 -1
index 6c0bb38..fd9c0cb 100644
@@ -66,7 +66,10 @@ static void virtio_mmio_config_in(struct kvm_cpu *vcpu,
 		return;
 	}
 
-	*data = cpu_to_le32(val);
+	if (addr != VIRTIO_MMIO_MAGIC_VALUE)
+		*data = cpu_to_le32(val);
+	else
+		*data = val;
 }
 
 static void virtio_mmio_config_out(struct kvm_cpu *vcpu,
Signature · 1 line
2.37.2.352.g3c44437643
#1 lore raw
Quoted 34 lines · Ben Dooks

When running on a big endian host, the virtio mmio-modern.c correctly sets all reads to return little endian values. However the header uses a 4 byte char for the magic value, which is always going to be in the correct endian regardless of host endian.

To make the simplest change, simply avoid endian convresion of the read of the magic value. This fixes the following bug from the guest:

[    0.592838] virtio-mmio 10020000.virtio: Wrong magic value 0x76697274!
---
 virtio/mmio-modern.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
virtio/mmio-modern.c +4 -1
index 6c0bb38..fd9c0cb 100644
@@ -66,7 +66,10 @@ static void virtio_mmio_config_in(struct kvm_cpu *vcpu,
 		return;
 	}
 
-	*data = cpu_to_le32(val);
+	if (addr != VIRTIO_MMIO_MAGIC_VALUE)
+		*data = cpu_to_le32(val);
+	else
+		*data = val;
 }
 
 static void virtio_mmio_config_out(struct kvm_cpu *vcpu,
Signature · 1 line
2.37.2.352.g3c44437643

I think vendor_id should also have the same issue, but drivers don't notice because they all use VIRTIO_DEV_ANY_ID. So how about the change below instead?

Thanks,
drew

include/kvm/virtio-mmio.h +1 -1
index b428b8d32f48..133817c1dc44 100644
@@ -18,7 +18,7 @@ struct virtio_mmio_ioevent_param {
 };
 
 struct virtio_mmio_hdr {
-       char    magic[4];
+       u32     magic;
        u32     version;
        u32     device_id;
        u32     vendor_id;
virtio/mmio.c +3 -2
index fae73b52dae0..782268e8f842 100644
@@ -6,6 +6,7 @@
 #include "kvm/irq.h"
 #include "kvm/fdt.h"
 
+#include <linux/byteorder.h>
 #include <linux/virtio_mmio.h>
 #include <string.h>
 
@@ -168,10 +169,10 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
                return r;
 
        vmmio->hdr = (struct virtio_mmio_hdr) {
-               .magic          = {'v', 'i', 'r', 't'},
+               .magic          = le32_to_cpu(0x74726976), /* 'virt' */
                .version        = legacy ? 1 : 2,
                .device_id      = subsys_id,
-               .vendor_id      = 0x4d564b4c , /* 'LKVM' */
+               .vendor_id      = le32_to_cpu(0x4d564b4c), /* 'LKVM' */
                .queue_num_max  = 256,
        };
author
#2 lore raw
Quoted 69 lines · Andrew Jones
Quoted 34 lines · Ben Dooks

When running on a big endian host, the virtio mmio-modern.c correctly sets all reads to return little endian values. However the header uses a 4 byte char for the magic value, which is always going to be in the correct endian regardless of host endian.

To make the simplest change, simply avoid endian convresion of the read of the magic value. This fixes the following bug from the guest:

[    0.592838] virtio-mmio 10020000.virtio: Wrong magic value 0x76697274!
---
  virtio/mmio-modern.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
virtio/mmio-modern.c +4 -1
index 6c0bb38..fd9c0cb 100644
@@ -66,7 +66,10 @@ static void virtio_mmio_config_in(struct kvm_cpu *vcpu,
  		return;
  	}
 
-	*data = cpu_to_le32(val);
+	if (addr != VIRTIO_MMIO_MAGIC_VALUE)
+		*data = cpu_to_le32(val);
+	else
+		*data = val;
  }
 
  static void virtio_mmio_config_out(struct kvm_cpu *vcpu,
Signature · 1 line
2.37.2.352.g3c44437643

I think vendor_id should also have the same issue, but drivers don't notice because they all use VIRTIO_DEV_ANY_ID. So how about the change below instead?

Thanks,
drew

include/kvm/virtio-mmio.h +1 -1
index b428b8d32f48..133817c1dc44 100644
@@ -18,7 +18,7 @@ struct virtio_mmio_ioevent_param {
  };
 
  struct virtio_mmio_hdr {
-       char    magic[4];
+       u32     magic;
         u32     version;
         u32     device_id;
         u32     vendor_id;
virtio/mmio.c +1 -0
index fae73b52dae0..782268e8f842 100644
@@ -6,6 +6,7 @@
  #include "kvm/irq.h"
  #include "kvm/fdt.h"
 
+#include <linux/byteorder.h>
  #include <linux/virtio_mmio.h>
  #include <string.h>
 

virtio/mmio.c int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev, · line 172

Quoted 5 lines
virtio/mmio.c +1 -1
@@ -168,10 +169,10 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
                 return r;
 
         vmmio->hdr = (struct virtio_mmio_hdr) {
-               .magic          = {'v', 'i', 'r', 't'},
+               .magic          = le32_to_cpu(0x74726976), /* 'virt' */

just doing the change of magic type and then doing

	.magic = 0x74726976;

should work, as then magic is in host order amd will get converted to le32 in the IO code. Don't think vendor_id suffers as it was converted from string to hex.

Quoted 7 lines
virtio/mmio.c +1 -1
                 .version        = legacy ? 1 : 2,
                 .device_id      = subsys_id,
-               .vendor_id      = 0x4d564b4c , /* 'LKVM' */
+               .vendor_id      = le32_to_cpu(0x4d564b4c), /* 'LKVM' */
                 .queue_num_max  = 256,
         };
 
Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html
#3 lore raw
Quoted 76 lines · Ben Dooks
Quoted 67 lines · Andrew Jones
Quoted 32 lines · Ben Dooks

When running on a big endian host, the virtio mmio-modern.c correctly sets all reads to return little endian values. However the header uses a 4 byte char for the magic value, which is always going to be in the correct endian regardless of host endian.

To make the simplest change, simply avoid endian convresion of the read of the magic value. This fixes the following bug from the guest:

[    0.592838] virtio-mmio 10020000.virtio: Wrong magic value 0x76697274!
---
  virtio/mmio-modern.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
virtio/mmio-modern.c +4 -1
index 6c0bb38..fd9c0cb 100644
@@ -66,7 +66,10 @@ static void virtio_mmio_config_in(struct kvm_cpu *vcpu,
  		return;
  	}
-	*data = cpu_to_le32(val);
+	if (addr != VIRTIO_MMIO_MAGIC_VALUE)
+		*data = cpu_to_le32(val);
+	else
+		*data = val;
  }
  static void virtio_mmio_config_out(struct kvm_cpu *vcpu,
Signature · 1 line
2.37.2.352.g3c44437643

I think vendor_id should also have the same issue, but drivers don't notice because they all use VIRTIO_DEV_ANY_ID. So how about the change below instead?

Thanks,
drew

include/kvm/virtio-mmio.h +1 -1
index b428b8d32f48..133817c1dc44 100644
@@ -18,7 +18,7 @@ struct virtio_mmio_ioevent_param {
  };
 
  struct virtio_mmio_hdr {
-       char    magic[4];
+       u32     magic;
         u32     version;
         u32     device_id;
         u32     vendor_id;
virtio/mmio.c +1 -0
index fae73b52dae0..782268e8f842 100644
@@ -6,6 +6,7 @@
  #include "kvm/irq.h"
  #include "kvm/fdt.h"
 
+#include <linux/byteorder.h>
  #include <linux/virtio_mmio.h>
  #include <string.h>
 

virtio/mmio.c int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev, · line 172

Quoted 5 lines
virtio/mmio.c +1 -1
@@ -168,10 +169,10 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
                 return r;
 
         vmmio->hdr = (struct virtio_mmio_hdr) {
-               .magic          = {'v', 'i', 'r', 't'},
+               .magic          = le32_to_cpu(0x74726976), /* 'virt' */

just doing the change of magic type and then doing

Quoted 5 lines
	.magic = 0x74726976;

should work, as then magic is in host order amd will get converted to le32 in the IO code. Don't think vendor_id suffers as it was converted from string to hex.

Oh, right. I overthought that one. I prefer the magic in hex better than the special casing in virtio_mmio_config_in()

Thanks,
drew

Quoted 15 lines
Quoted 8 lines
virtio/mmio.c +1 -1
                 .version        = legacy ? 1 : 2,
                 .device_id      = subsys_id,
-               .vendor_id      = 0x4d564b4c , /* 'LKVM' */
+               .vendor_id      = le32_to_cpu(0x4d564b4c), /* 'LKVM' */
                 .queue_num_max  = 256,
         };
 
Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html
author
#4 lore raw
Quoted 83 lines · Andrew Jones
Quoted 76 lines · Ben Dooks
Quoted 67 lines · Andrew Jones
Quoted 32 lines · Ben Dooks

When running on a big endian host, the virtio mmio-modern.c correctly sets all reads to return little endian values. However the header uses a 4 byte char for the magic value, which is always going to be in the correct endian regardless of host endian.

To make the simplest change, simply avoid endian convresion of the read of the magic value. This fixes the following bug from the guest:

[    0.592838] virtio-mmio 10020000.virtio: Wrong magic value 0x76697274!
---
   virtio/mmio-modern.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
virtio/mmio-modern.c +4 -1
index 6c0bb38..fd9c0cb 100644
@@ -66,7 +66,10 @@ static void virtio_mmio_config_in(struct kvm_cpu *vcpu,
   		return;
   	}
-	*data = cpu_to_le32(val);
+	if (addr != VIRTIO_MMIO_MAGIC_VALUE)
+		*data = cpu_to_le32(val);
+	else
+		*data = val;
   }
   static void virtio_mmio_config_out(struct kvm_cpu *vcpu,
Signature · 1 line
2.37.2.352.g3c44437643

I think vendor_id should also have the same issue, but drivers don't notice because they all use VIRTIO_DEV_ANY_ID. So how about the change below instead?

Thanks,
drew

include/kvm/virtio-mmio.h +1 -1
index b428b8d32f48..133817c1dc44 100644
@@ -18,7 +18,7 @@ struct virtio_mmio_ioevent_param {
   };
 
   struct virtio_mmio_hdr {
-       char    magic[4];
+       u32     magic;
          u32     version;
          u32     device_id;
          u32     vendor_id;
virtio/mmio.c +1 -0
index fae73b52dae0..782268e8f842 100644
@@ -6,6 +6,7 @@
   #include "kvm/irq.h"
   #include "kvm/fdt.h"
 
+#include <linux/byteorder.h>
   #include <linux/virtio_mmio.h>
   #include <string.h>
 

virtio/mmio.c int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev, · line 172

Quoted 5 lines
virtio/mmio.c +1 -1
@@ -168,10 +169,10 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
                  return r;
 
          vmmio->hdr = (struct virtio_mmio_hdr) {
-               .magic          = {'v', 'i', 'r', 't'},
+               .magic          = le32_to_cpu(0x74726976), /* 'virt' */

just doing the change of magic type and then doing

Quoted 5 lines
	.magic = 0x74726976;

should work, as then magic is in host order amd will get converted to le32 in the IO code. Don't think vendor_id suffers as it was converted from string to hex.

Quoted 5 lines

Oh, right. I overthought that one. I prefer the magic in hex better than the special casing in virtio_mmio_config_in()

Thanks,
drew

Ok, will wait a few days to see if anyone else has a comment.

I assume you're ok with me re-doing my patch?

Thanks for the review.

Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html
#5 lore raw
Quoted 87 lines · Ben Dooks
Quoted 86 lines · Andrew Jones
Quoted 76 lines · Ben Dooks
Quoted 67 lines · Andrew Jones
Quoted 32 lines · Ben Dooks

When running on a big endian host, the virtio mmio-modern.c correctly sets all reads to return little endian values. However the header uses a 4 byte char for the magic value, which is always going to be in the correct endian regardless of host endian.

To make the simplest change, simply avoid endian convresion of the read of the magic value. This fixes the following bug from the guest:

[    0.592838] virtio-mmio 10020000.virtio: Wrong magic value 0x76697274!
---
   virtio/mmio-modern.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
virtio/mmio-modern.c +4 -1
index 6c0bb38..fd9c0cb 100644
@@ -66,7 +66,10 @@ static void virtio_mmio_config_in(struct kvm_cpu *vcpu,
   		return;
   	}
-	*data = cpu_to_le32(val);
+	if (addr != VIRTIO_MMIO_MAGIC_VALUE)
+		*data = cpu_to_le32(val);
+	else
+		*data = val;
   }
   static void virtio_mmio_config_out(struct kvm_cpu *vcpu,
Signature · 1 line
2.37.2.352.g3c44437643

I think vendor_id should also have the same issue, but drivers don't notice because they all use VIRTIO_DEV_ANY_ID. So how about the change below instead?

Thanks,
drew

include/kvm/virtio-mmio.h +1 -1
index b428b8d32f48..133817c1dc44 100644
@@ -18,7 +18,7 @@ struct virtio_mmio_ioevent_param {
   };
 
   struct virtio_mmio_hdr {
-       char    magic[4];
+       u32     magic;
          u32     version;
          u32     device_id;
          u32     vendor_id;
virtio/mmio.c +1 -0
index fae73b52dae0..782268e8f842 100644
@@ -6,6 +6,7 @@
   #include "kvm/irq.h"
   #include "kvm/fdt.h"
 
+#include <linux/byteorder.h>
   #include <linux/virtio_mmio.h>
   #include <string.h>
 

virtio/mmio.c int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev, · line 172

Quoted 5 lines
virtio/mmio.c +1 -1
@@ -168,10 +169,10 @@ int virtio_mmio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
                  return r;
 
          vmmio->hdr = (struct virtio_mmio_hdr) {
-               .magic          = {'v', 'i', 'r', 't'},
+               .magic          = le32_to_cpu(0x74726976), /* 'virt' */

just doing the change of magic type and then doing

Quoted 5 lines
	.magic = 0x74726976;

should work, as then magic is in host order amd will get converted to le32 in the IO code. Don't think vendor_id suffers as it was converted from string to hex.

Oh, right. I overthought that one. I prefer the magic in hex better than the special casing in virtio_mmio_config_in()

Quoted 6 lines
Quoted 2 lines

Thanks,
drew

Ok, will wait a few days to see if anyone else has a comment.

I assume you're ok with me re-doing my patch?

yup, thanks

Quoted 9 lines

Thanks for the review.

Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html