Skip to contentSkip to conversation

util/vfio-helper: Fix endianess when enabling Bus Master

PATCH In review Farhan Ali 3 participants 11 messages Dec 17, 2025 – Jan 5

lore
Share & tools
lore

Farhan Ali

author
#0 lore raw

PATCH 1/1 util/vfio-helper: Fix endianess when enabling Bus Master

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

---
 util/vfio-helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/vfio-helpers.c +1 -1
index fdff042ab4..3c8284284f 100644
@@ -443,7 +443,7 @@ static int qemu_vfio_init_pci(QEMUVFIOState *s, const char *device,
     if (ret) {
         goto fail;
     }
-    pci_cmd |= PCI_COMMAND_MASTER;
+    pci_cmd = cpu_to_le16(le16_to_cpu(pci_cmd) | PCI_COMMAND_MASTER);
     ret = qemu_vfio_pci_write_config(s, &pci_cmd, sizeof(pci_cmd), PCI_COMMAND);
     if (ret) {
         goto fail;
Signature · 1 line
2.43.0
#2 lore raw
In reply to Farhan Ali

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

How did you find the issue ?

Thanks,

C.

Quoted 18 lines
---
  util/vfio-helpers.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
util/vfio-helpers.c +1 -1
index fdff042ab4..3c8284284f 100644
@@ -443,7 +443,7 @@ static int qemu_vfio_init_pci(QEMUVFIOState *s, const char *device,
      if (ret) {
          goto fail;
      }
-    pci_cmd |= PCI_COMMAND_MASTER;
+    pci_cmd = cpu_to_le16(le16_to_cpu(pci_cmd) | PCI_COMMAND_MASTER);
      ret = qemu_vfio_pci_write_config(s, &pci_cmd, sizeof(pci_cmd), PCI_COMMAND);
      if (ret) {
          goto fail;
#3 lore raw
Quoted 8 lines · Cédric Le Goater

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

How did you find the issue ?

I presumed using VFIO on s390x hosts...

Quoted 28 lines

Thanks,

C.

Quoted 19 lines

---
  util/vfio-helpers.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
util/vfio-helpers.c +1 -1
index fdff042ab4..3c8284284f 100644
@@ -443,7 +443,7 @@ static int qemu_vfio_init_pci(QEMUVFIOState *s, 
 const char *device,
       if (ret) {
           goto fail;
       }
-    pci_cmd |= PCI_COMMAND_MASTER;
+    pci_cmd = cpu_to_le16(le16_to_cpu(pci_cmd) | PCI_COMMAND_MASTER);
       ret = qemu_vfio_pci_write_config(s, &pci_cmd, sizeof(pci_cmd), PCI_COMMAND);
       if (ret) {

          goto fail;

#4 lore raw
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Cédric Le Goater

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

How did you find the issue ?

I presumed using VFIO on s390x hosts...

yes. I have been using a nvme drive for over 2y in an s390x VM and never saw any issue.

C.

Quoted 29 lines
Quoted 28 lines

Thanks,

C.

Quoted 18 lines

---
  util/vfio-helpers.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
util/vfio-helpers.c +1 -1
index fdff042ab4..3c8284284f 100644
@@ -443,7 +443,7 @@ static int qemu_vfio_init_pci(QEMUVFIOState *s, const char *device,
       if (ret) {
           goto fail;
       }
-    pci_cmd |= PCI_COMMAND_MASTER;
+    pci_cmd = cpu_to_le16(le16_to_cpu(pci_cmd) | PCI_COMMAND_MASTER);
       ret = qemu_vfio_pci_write_config(s, &pci_cmd, sizeof(pci_cmd), PCI_COMMAND);
       if (ret) {
           goto fail;
author
#5 lore raw
Quoted 9 lines · Cédric Le Goater
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Cédric Le Goater

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

How did you find the issue ?

I presumed using VFIO on s390x hosts...

yes. I have been using a nvme drive for over 2y in an s390x VM and never saw any issue.

C.

Hi Cedric,

I have recently been experimenting with QEMU NVMe userspace driver and PCI instructions from userspace [1]. On some of our test systems I noticed the driver being hung. After some investigation we realized it was due to this issue.

[1] https://lore.kernel.org/all/[email protected]/

Thanks

Farhan

Quoted 32 lines
Quoted 30 lines
Quoted 29 lines

Thanks,

C.

Quoted 19 lines

---
  util/vfio-helpers.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
util/vfio-helpers.c +1 -1
index fdff042ab4..3c8284284f 100644
@@ -443,7 +443,7 @@ static int qemu_vfio_init_pci(QEMUVFIOState *s, 
 const char *device,
       if (ret) {
           goto fail;
       }
-    pci_cmd |= PCI_COMMAND_MASTER;
+    pci_cmd = cpu_to_le16(le16_to_cpu(pci_cmd) | PCI_COMMAND_MASTER);
       ret = qemu_vfio_pci_write_config(s, &pci_cmd, sizeof(pci_cmd), PCI_COMMAND);
       if (ret) {

          goto fail;

#6 lore raw

Hello Farhan,

Quoted 15 lines · Farhan Ali
Quoted 13 lines · Cédric Le Goater
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Cédric Le Goater

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

How did you find the issue ?

I presumed using VFIO on s390x hosts...

Quoted 8 lines
Quoted 5 lines

yes. I have been using a nvme drive for over 2y in an s390x VM and never saw any issue.

C.

Hi Cedric,

I have recently been experimenting with QEMU NVMe userspace driver and PCI instructions from userspace [1]. On some of our test systems I noticed the driver being hung. After some investigation we realized it was due to this issue.

Do you think you could provide a test case for it ?

Also, I think the LE conversion should be done in routines qemu_vfio_pci_read_config() and qemu_vfio_pci_write_config().

Thanks,

C.

Quoted 40 lines

[1] https://lore.kernel.org/all/[email protected]/

Thanks

Farhan

Quoted 31 lines
Quoted 29 lines
Quoted 28 lines

Thanks,

C.

Quoted 18 lines

---
  util/vfio-helpers.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
util/vfio-helpers.c +1 -1
index fdff042ab4..3c8284284f 100644
@@ -443,7 +443,7 @@ static int qemu_vfio_init_pci(QEMUVFIOState *s, const char *device,
       if (ret) {
           goto fail;
       }
-    pci_cmd |= PCI_COMMAND_MASTER;
+    pci_cmd = cpu_to_le16(le16_to_cpu(pci_cmd) | PCI_COMMAND_MASTER);
       ret = qemu_vfio_pci_write_config(s, &pci_cmd, sizeof(pci_cmd), PCI_COMMAND);
       if (ret) {
           goto fail;
#7 lore raw
Quoted 23 lines · Cédric Le Goater

Hello Farhan,

Quoted 20 lines · Farhan Ali
Quoted 18 lines · Cédric Le Goater
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Cédric Le Goater

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

How did you find the issue ?

I presumed using VFIO on s390x hosts...

yes. I have been using a nvme drive for over 2y in an s390x VM and never saw any issue.

C.

Quoted 8 lines
Quoted 3 lines

Hi Cedric,

I have recently been experimenting with QEMU NVMe userspace driver and PCI instructions from userspace [1]. On some of our test systems I noticed the driver being hung. After some investigation we realized it was due to this issue.

Do you think you could provide a test case for it ?

Also, I think the LE conversion should be done in routines qemu_vfio_pci_read_config() and qemu_vfio_pci_write_config().

These config routines take a void buffer, so have no clue of endianness size.

Maybe we want intermediary qemu_vfio_pci_ld16_config() and qemu_vfio_pci_st16_config() helpers?

author
#8 lore raw
Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Cédric Le Goater

Hello Farhan,

Quoted 18 lines · Farhan Ali
Quoted 16 lines · Cédric Le Goater
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Cédric Le Goater

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

How did you find the issue ?

I presumed using VFIO on s390x hosts...

yes. I have been using a nvme drive for over 2y in an s390x VM and never saw any issue.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

C.

Hi Cedric,

I have recently been experimenting with QEMU NVMe userspace driver and PCI instructions from userspace [1]. On some of our test systems I noticed the driver being hung. After some investigation we realized it was due to this issue.

Do you think you could provide a test case for it ?

@Cedric,

Just wanted to understand what you had in mind here? The test case would use VFIO and would need an NVMe device, looking through QEMU testcases I couldn't find an example of using VFIO. If there are examples or other frameworks that is used to test this could you point me to them?

Quoted 7 lines
Quoted 3 lines

Also, I think the LE conversion should be done in routines qemu_vfio_pci_read_config() and qemu_vfio_pci_write_config().

These config routines take a void buffer, so have no clue of endianness size.

Yup, exactly.

Quoted 3 lines

Maybe we want intermediary qemu_vfio_pci_ld16_config() and qemu_vfio_pci_st16_config() helpers?

I did think of adding in additional helper functions. But since qemu_vfio_pci_read_config()/qemu_vfio_pci_write_config() is only called in this one place, wasn't sure if it made sense in adding them.

Another approach I thought of was limiting reads/writes to 32 bit similar to vfio_pci_write_config()/vfio_pci_read_config() does today. Can we do this here as well?

Thanks

Farhan

#9 lore raw
Quoted 43 lines · Farhan Ali
Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Cédric Le Goater

Hello Farhan,

Quoted 18 lines · Farhan Ali
Quoted 16 lines · Cédric Le Goater
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Cédric Le Goater

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

How did you find the issue ?

I presumed using VFIO on s390x hosts...

yes. I have been using a nvme drive for over 2y in an s390x VM and never saw any issue.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

C.

Hi Cedric,

I have recently been experimenting with QEMU NVMe userspace driver and PCI instructions from userspace [1]. On some of our test systems I noticed the driver being hung. After some investigation we realized it was due to this issue.

Do you think you could provide a test case for it ?

@Cedric,

Just wanted to understand what you had in mind here? The test case would use VFIO and would need an NVMe device, looking through QEMU testcases I couldn't find an example of using VFIO. If there are examples or other frameworks that is used to test this could you point me to them?

Quoted 7 lines
Quoted 3 lines

Also, I think the LE conversion should be done in routines qemu_vfio_pci_read_config() and qemu_vfio_pci_write_config().

These config routines take a void buffer, so have no clue of endianness size.

Quoted 8 lines

Yup, exactly.

Quoted 3 lines

Maybe we want intermediary qemu_vfio_pci_ld16_config() and qemu_vfio_pci_st16_config() helpers?

I did think of adding in additional helper functions. But since qemu_vfio_pci_read_config()/qemu_vfio_pci_write_config() is only called in this one place, wasn't sure if it made sense in adding them.

Another approach I thought of was limiting reads/writes to 32 bit similar to vfio_pci_write_config()/vfio_pci_read_config() does today. Can we do this here as well?

I don't see why we couldn't. The vfio helper routines under util/ use the same kernel interface as the vfio-pci device.

Ideally, both should use a common low-level interface but that seems a bit complex for this fix.

Thanks,

C.

author
#10 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Cédric Le Goater

Hello Farhan,

Quoted 18 lines · Farhan Ali
Quoted 16 lines · Cédric Le Goater
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Cédric Le Goater

Hello,

Quoted 3 lines · Farhan Ali

The VFIO pread/pwrite functions use little-endian data format. When enabling the Bus Master bit, the value must be correctly converted from the CPU's native endianess to little-endian format.

How did you find the issue ?

I presumed using VFIO on s390x hosts...

yes. I have been using a nvme drive for over 2y in an s390x VM and never saw any issue.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

C.

Hi Cedric,

I have recently been experimenting with QEMU NVMe userspace driver and PCI instructions from userspace [1]. On some of our test systems I noticed the driver being hung. After some investigation we realized it was due to this issue.

Do you think you could provide a test case for it ?

@Cedric,

Just wanted to understand what you had in mind here? The test case would use VFIO and would need an NVMe device, looking through QEMU testcases I couldn't find an example of using VFIO. If there are examples or other frameworks that is used to test this could you point me to them?

Quoted 6 lines
Quoted 3 lines

Also, I think the LE conversion should be done in routines qemu_vfio_pci_read_config() and qemu_vfio_pci_write_config().

These config routines take a void buffer, so have no clue of endianness size.

Yup, exactly.

Quoted 4 lines

Maybe we want intermediary qemu_vfio_pci_ld16_config() and qemu_vfio_pci_st16_config() helpers?

I did think of adding in additional helper functions. But since qemu_vfio_pci_read_config()/qemu_vfio_pci_write_config() is only called in this one place, wasn't sure if it made sense in adding them.

Another approach I thought of was limiting reads/writes to 32 bit similar to vfio_pci_write_config()/vfio_pci_read_config() does today. Can we do this here as well?

Thanks

Farhan