Skip to contentSkip to conversation

virtio-pci: fix migration for pci bus master

PATCH Superseded Michael S. Tsirkin 3 participants 14 messages Sep 14, 2014 – Oct 13, 2014

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-pci: fix migration for pci bus master

Current support for bus master (clearing OK bit) together with the need to support guests which do not enable PCI bus mastering, leads to extra state in VIRTIO_PCI_FLAG_BUS_MASTER_BUG bit, which isn't robust in case of cross-version migration for the case when guests use the device before setting DRIVER_OK.

Rip out VIRTIO_PCI_FLAG_BUS_MASTER_BUG and implement a simpler
work-around: treat clearing of PCI_COMMAND as a virtio reset. Old guests never touch this bit so they will work.

As reset clears device status, DRIVER and MASTER bits are now in sync, so we can fix up cross-version migration simply by synchronising them, without need to detect a buggy guest explicitly.

Drop tracking VIRTIO_PCI_FLAG_BUS_MASTER_BUG completely.

As reset makes the device quiescent, in the future we'll be able to drop checking OK bit in a bunch of places.

---
 hw/virtio/virtio-pci.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
hw/virtio/virtio-pci.c +20 -19
index a827cd4..f560814 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -480,13 +469,18 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
+    uint8_t cmd = proxy->pci_dev.config[PCI_COMMAND];
+
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        (cmd & PCI_COMMAND_MASTER)) {
+        /* Bus driver disables bus mastering - make it act
+         * as a kind of reset to render the device quiescent. */
         virtio_pci_stop_ioeventfd(proxy);
-        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
+        virtio_reset(vdev);
+        msix_unuse_all_vectors(&proxy->pci_dev);
     }
 }
 
@@ -895,11 +889,19 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+        /* Linux before 2.6.34 drives the device without enabling
+           the PCI device bus master bit. Enable it automatically
+           for the guest. This is a PCI spec violation but so is
+           initiating DMA with bus master bit clear.
+           Note: this only makes a difference when migrating
+           across QEMU versions from an old QEMU, as for new QEMU
+           bus master and driver bits are always in sync.
+           TODO: consider enabling conditionally for compat machine types. */
+        if (vdev->status & (VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                            VIRTIO_CONFIG_S_DRIVER)) {
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,7 +1042,6 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
Signature · 1 line
MST
#1 lore raw
Quoted 18 lines · On Sun, 14 Sep 2014 21:30:36 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Current support for bus master (clearing OK bit) together with the need to support guests which do not enable PCI bus mastering, leads to extra state in VIRTIO_PCI_FLAG_BUS_MASTER_BUG bit, which isn't robust in case of cross-version migration for the case when guests use the device before setting DRIVER_OK.

Rip out VIRTIO_PCI_FLAG_BUS_MASTER_BUG and implement a simpler
work-around: treat clearing of PCI_COMMAND as a virtio reset. Old guests never touch this bit so they will work.

As reset clears device status, DRIVER and MASTER bits are now in sync, so we can fix up cross-version migration simply by synchronising them, without need to detect a buggy guest explicitly.

Drop tracking VIRTIO_PCI_FLAG_BUS_MASTER_BUG completely.

Quoted 7 lines

As reset makes the device quiescent, in the future we'll be able to drop checking OK bit in a bunch of places.

---

Hi Michael,

I am not quite sure how to test this patch with my pseries based setup... Migrating from qemu-2.1 to qemu-master ?

Cheers.

--
Greg

Quoted 86 lines
 hw/virtio/virtio-pci.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
hw/virtio/virtio-pci.c +20 -19
index a827cd4..f560814 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -480,13 +469,18 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
+    uint8_t cmd = proxy->pci_dev.config[PCI_COMMAND];
+
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        (cmd & PCI_COMMAND_MASTER)) {
+        /* Bus driver disables bus mastering - make it act
+         * as a kind of reset to render the device quiescent. */
         virtio_pci_stop_ioeventfd(proxy);
-        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
+        virtio_reset(vdev);
+        msix_unuse_all_vectors(&proxy->pci_dev);
     }
 }
 
@@ -895,11 +889,19 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+        /* Linux before 2.6.34 drives the device without enabling
+           the PCI device bus master bit. Enable it automatically
+           for the guest. This is a PCI spec violation but so is
+           initiating DMA with bus master bit clear.
+           Note: this only makes a difference when migrating
+           across QEMU versions from an old QEMU, as for new QEMU
+           bus master and driver bits are always in sync.
+           TODO: consider enabling conditionally for compat machine types. */
+        if (vdev->status & (VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                            VIRTIO_CONFIG_S_DRIVER)) {
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,7 +1042,6 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
#2 lore raw
Quoted 31 lines · Greg Kurz
Quoted 18 lines · On Sun, 14 Sep 2014 21:30:36 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Current support for bus master (clearing OK bit) together with the need to support guests which do not enable PCI bus mastering, leads to extra state in VIRTIO_PCI_FLAG_BUS_MASTER_BUG bit, which isn't robust in case of cross-version migration for the case when guests use the device before setting DRIVER_OK.

Rip out VIRTIO_PCI_FLAG_BUS_MASTER_BUG and implement a simpler
work-around: treat clearing of PCI_COMMAND as a virtio reset. Old guests never touch this bit so they will work.

As reset clears device status, DRIVER and MASTER bits are now in sync, so we can fix up cross-version migration simply by synchronising them, without need to detect a buggy guest explicitly.

Drop tracking VIRTIO_PCI_FLAG_BUS_MASTER_BUG completely.

Quoted 7 lines

As reset makes the device quiescent, in the future we'll be able to drop checking OK bit in a bunch of places.

---

Hi Michael,

Quoted 7 lines

I am not quite sure how to test this patch with my pseries based setup... Migrating from qemu-2.1 to qemu-master ?

Cheers.

--
Greg

Exactly. And back! Pls don't forget to specify the 2.1 machine type. Thanks!

Quoted 86 lines
Quoted 86 lines
 hw/virtio/virtio-pci.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
hw/virtio/virtio-pci.c +20 -19
index a827cd4..f560814 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -480,13 +469,18 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
+    uint8_t cmd = proxy->pci_dev.config[PCI_COMMAND];
+
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        (cmd & PCI_COMMAND_MASTER)) {
+        /* Bus driver disables bus mastering - make it act
+         * as a kind of reset to render the device quiescent. */
         virtio_pci_stop_ioeventfd(proxy);
-        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
+        virtio_reset(vdev);
+        msix_unuse_all_vectors(&proxy->pci_dev);
     }
 }
 
@@ -895,11 +889,19 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+        /* Linux before 2.6.34 drives the device without enabling
+           the PCI device bus master bit. Enable it automatically
+           for the guest. This is a PCI spec violation but so is
+           initiating DMA with bus master bit clear.
+           Note: this only makes a difference when migrating
+           across QEMU versions from an old QEMU, as for new QEMU
+           bus master and driver bits are always in sync.
+           TODO: consider enabling conditionally for compat machine types. */
+        if (vdev->status & (VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                            VIRTIO_CONFIG_S_DRIVER)) {
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,7 +1042,6 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
#3 lore raw
Quoted 35 lines · On Wed, 17 Sep 2014 20:39:25 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 34 lines · Greg Kurz
Quoted 18 lines · On Sun, 14 Sep 2014 21:30:36 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Current support for bus master (clearing OK bit) together with the need to support guests which do not enable PCI bus mastering, leads to extra state in VIRTIO_PCI_FLAG_BUS_MASTER_BUG bit, which isn't robust in case of cross-version migration for the case when guests use the device before setting DRIVER_OK.

Rip out VIRTIO_PCI_FLAG_BUS_MASTER_BUG and implement a simpler
work-around: treat clearing of PCI_COMMAND as a virtio reset. Old guests never touch this bit so they will work.

As reset clears device status, DRIVER and MASTER bits are now in sync, so we can fix up cross-version migration simply by synchronising them, without need to detect a buggy guest explicitly.

Drop tracking VIRTIO_PCI_FLAG_BUS_MASTER_BUG completely.

Quoted 7 lines

As reset makes the device quiescent, in the future we'll be able to drop checking OK bit in a bunch of places.

---

Hi Michael,

I am not quite sure how to test this patch with my pseries based setup... Migrating from qemu-2.1 to qemu-master ?

Quoted 8 lines
Quoted 4 lines

Cheers.

--
Greg

Exactly. And back! Pls don't forget to specify the 2.1 machine type. Thanks!

Michael,

Nikunj and I had started to investigate the pseries breakage: the QEMU originated reset brought by this patch clears the vq and breaks SLOF. This isn't a surprise since reset should always come from the driver, not the device.

Since commit 45363e46aeebfc99753389649eac7c7fc22bfe52 has reverted this patch, QEMU works again for pseries and virtio. :)

So back to the initial issue, I've tried to migrate a pseries-2.1 guest running rhel65, from QEMU v2.1.2 to QEMU master, back and forth, several times and it always succeeded... what symptom this patch was expected to fix ?

Cheers.

--
Greg

Quoted 87 lines
Quoted 86 lines
Quoted 86 lines
 hw/virtio/virtio-pci.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
hw/virtio/virtio-pci.c +20 -19
index a827cd4..f560814 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -480,13 +469,18 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
+    uint8_t cmd = proxy->pci_dev.config[PCI_COMMAND];
+
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        (cmd & PCI_COMMAND_MASTER)) {
+        /* Bus driver disables bus mastering - make it act
+         * as a kind of reset to render the device quiescent. */
         virtio_pci_stop_ioeventfd(proxy);
-        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
+        virtio_reset(vdev);
+        msix_unuse_all_vectors(&proxy->pci_dev);
     }
 }
 
@@ -895,11 +889,19 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+        /* Linux before 2.6.34 drives the device without enabling
+           the PCI device bus master bit. Enable it automatically
+           for the guest. This is a PCI spec violation but so is
+           initiating DMA with bus master bit clear.
+           Note: this only makes a difference when migrating
+           across QEMU versions from an old QEMU, as for new QEMU
+           bus master and driver bits are always in sync.
+           TODO: consider enabling conditionally for compat machine types. */
+        if (vdev->status & (VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                            VIRTIO_CONFIG_S_DRIVER)) {
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,7 +1042,6 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
#4 lore raw
Quoted 57 lines · Greg Kurz
Quoted 35 lines · On Wed, 17 Sep 2014 20:39:25 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 34 lines · Greg Kurz
Quoted 18 lines · On Sun, 14 Sep 2014 21:30:36 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Current support for bus master (clearing OK bit) together with the need to support guests which do not enable PCI bus mastering, leads to extra state in VIRTIO_PCI_FLAG_BUS_MASTER_BUG bit, which isn't robust in case of cross-version migration for the case when guests use the device before setting DRIVER_OK.

Rip out VIRTIO_PCI_FLAG_BUS_MASTER_BUG and implement a simpler
work-around: treat clearing of PCI_COMMAND as a virtio reset. Old guests never touch this bit so they will work.

As reset clears device status, DRIVER and MASTER bits are now in sync, so we can fix up cross-version migration simply by synchronising them, without need to detect a buggy guest explicitly.

Drop tracking VIRTIO_PCI_FLAG_BUS_MASTER_BUG completely.

Quoted 7 lines

As reset makes the device quiescent, in the future we'll be able to drop checking OK bit in a bunch of places.

---

Hi Michael,

I am not quite sure how to test this patch with my pseries based setup... Migrating from qemu-2.1 to qemu-master ?

Quoted 8 lines
Quoted 4 lines

Cheers.

--
Greg

Exactly. And back! Pls don't forget to specify the 2.1 machine type. Thanks!

Michael,

Nikunj and I had started to investigate the pseries breakage: the QEMU originated reset brought by this patch clears the vq and breaks SLOF. This isn't a surprise since reset should always come from the driver, not the device.

Since commit 45363e46aeebfc99753389649eac7c7fc22bfe52 has reverted this patch, QEMU works again for pseries and virtio. :)

Quoted 96 lines

So back to the initial issue, I've tried to migrate a pseries-2.1 guest running rhel65, from QEMU v2.1.2 to QEMU master, back and forth, several times and it always succeeded... what symptom this patch was expected to fix ?

Cheers.

--
Greg

Quoted 87 lines
Quoted 86 lines
Quoted 86 lines
 hw/virtio/virtio-pci.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
hw/virtio/virtio-pci.c +20 -19
index a827cd4..f560814 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -480,13 +469,18 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
+    uint8_t cmd = proxy->pci_dev.config[PCI_COMMAND];
+
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        (cmd & PCI_COMMAND_MASTER)) {
+        /* Bus driver disables bus mastering - make it act
+         * as a kind of reset to render the device quiescent. */
         virtio_pci_stop_ioeventfd(proxy);
-        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
+        virtio_reset(vdev);
+        msix_unuse_all_vectors(&proxy->pci_dev);
     }
 }
 
@@ -895,11 +889,19 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+        /* Linux before 2.6.34 drives the device without enabling
+           the PCI device bus master bit. Enable it automatically
+           for the guest. This is a PCI spec violation but so is
+           initiating DMA with bus master bit clear.
+           Note: this only makes a difference when migrating
+           across QEMU versions from an old QEMU, as for new QEMU
+           bus master and driver bits are always in sync.
+           TODO: consider enabling conditionally for compat machine types. */
+        if (vdev->status & (VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                            VIRTIO_CONFIG_S_DRIVER)) {
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,7 +1042,6 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {

It's a race condition: you have to catch it migrating in the window before DRIVER_OK is set.

#5 lore raw
Quoted 152 lines · On Mon, 6 Oct 2014 19:26:21 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 151 lines · Greg Kurz
Quoted 35 lines · On Wed, 17 Sep 2014 20:39:25 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 34 lines · Greg Kurz
Quoted 18 lines · On Sun, 14 Sep 2014 21:30:36 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Current support for bus master (clearing OK bit) together with the need to support guests which do not enable PCI bus mastering, leads to extra state in VIRTIO_PCI_FLAG_BUS_MASTER_BUG bit, which isn't robust in case of cross-version migration for the case when guests use the device before setting DRIVER_OK.

Rip out VIRTIO_PCI_FLAG_BUS_MASTER_BUG and implement a simpler
work-around: treat clearing of PCI_COMMAND as a virtio reset. Old guests never touch this bit so they will work.

As reset clears device status, DRIVER and MASTER bits are now in sync, so we can fix up cross-version migration simply by synchronising them, without need to detect a buggy guest explicitly.

Drop tracking VIRTIO_PCI_FLAG_BUS_MASTER_BUG completely.

Quoted 7 lines

As reset makes the device quiescent, in the future we'll be able to drop checking OK bit in a bunch of places.

---

Hi Michael,

I am not quite sure how to test this patch with my pseries based setup... Migrating from qemu-2.1 to qemu-master ?

Quoted 8 lines
Quoted 4 lines

Cheers.

--
Greg

Exactly. And back! Pls don't forget to specify the 2.1 machine type. Thanks!

Michael,

Nikunj and I had started to investigate the pseries breakage: the QEMU originated reset brought by this patch clears the vq and breaks SLOF. This isn't a surprise since reset should always come from the driver, not the device.

Since commit 45363e46aeebfc99753389649eac7c7fc22bfe52 has reverted this patch, QEMU works again for pseries and virtio. :)

So back to the initial issue, I've tried to migrate a pseries-2.1 guest running rhel65, from QEMU v2.1.2 to QEMU master, back and forth, several times and it always succeeded... what symptom this patch was expected to fix ?

Cheers.

--
Greg

Quoted 85 lines
Quoted 85 lines
Quoted 85 lines
 hw/virtio/virtio-pci.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
hw/virtio/virtio-pci.c +20 -19
index a827cd4..f560814 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -480,13 +469,18 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
+    uint8_t cmd = proxy->pci_dev.config[PCI_COMMAND];
+
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        (cmd & PCI_COMMAND_MASTER)) {
+        /* Bus driver disables bus mastering - make it act
+         * as a kind of reset to render the device quiescent. */
         virtio_pci_stop_ioeventfd(proxy);
-        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
+        virtio_reset(vdev);
+        msix_unuse_all_vectors(&proxy->pci_dev);
     }
 }
 
@@ -895,11 +889,19 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+        /* Linux before 2.6.34 drives the device without enabling
+           the PCI device bus master bit. Enable it automatically
+           for the guest. This is a PCI spec violation but so is
+           initiating DMA with bus master bit clear.
+           Note: this only makes a difference when migrating
+           across QEMU versions from an old QEMU, as for new QEMU
+           bus master and driver bits are always in sync.
+           TODO: consider enabling conditionally for compat machine types. */
+        if (vdev->status & (VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                            VIRTIO_CONFIG_S_DRIVER)) {
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,7 +1042,6 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

hw/virtio/virtio-pci.c static void virtio_pci_reset(DeviceState *qdev) · line 1047

Quoted 1 line
hw/virtio/virtio-pci.c +0 -0
 static Property virtio_pci_properties[] = {

It's a race condition: you have to catch it migrating in the window before DRIVER_OK is set.

Ok I see... when DRIVER and MASTER bits are not in sync. I'll patch QEMU to trigger a migration in this window then.

Thanks.

--
Greg

#6 lore raw
Quoted 158 lines · Greg Kurz
Quoted 156 lines · On Mon, 6 Oct 2014 19:26:21 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 153 lines · Greg Kurz
Quoted 35 lines · On Wed, 17 Sep 2014 20:39:25 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 34 lines · Greg Kurz
Quoted 18 lines · On Sun, 14 Sep 2014 21:30:36 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Current support for bus master (clearing OK bit) together with the need to support guests which do not enable PCI bus mastering, leads to extra state in VIRTIO_PCI_FLAG_BUS_MASTER_BUG bit, which isn't robust in case of cross-version migration for the case when guests use the device before setting DRIVER_OK.

Rip out VIRTIO_PCI_FLAG_BUS_MASTER_BUG and implement a simpler
work-around: treat clearing of PCI_COMMAND as a virtio reset. Old guests never touch this bit so they will work.

As reset clears device status, DRIVER and MASTER bits are now in sync, so we can fix up cross-version migration simply by synchronising them, without need to detect a buggy guest explicitly.

Drop tracking VIRTIO_PCI_FLAG_BUS_MASTER_BUG completely.

Quoted 7 lines

As reset makes the device quiescent, in the future we'll be able to drop checking OK bit in a bunch of places.

---

Hi Michael,

I am not quite sure how to test this patch with my pseries based setup... Migrating from qemu-2.1 to qemu-master ?

Quoted 8 lines
Quoted 4 lines

Cheers.

--
Greg

Exactly. And back! Pls don't forget to specify the 2.1 machine type. Thanks!

Michael,

Nikunj and I had started to investigate the pseries breakage: the QEMU originated reset brought by this patch clears the vq and breaks SLOF. This isn't a surprise since reset should always come from the driver, not the device.

Since commit 45363e46aeebfc99753389649eac7c7fc22bfe52 has reverted this patch, QEMU works again for pseries and virtio. :)

So back to the initial issue, I've tried to migrate a pseries-2.1 guest running rhel65, from QEMU v2.1.2 to QEMU master, back and forth, several times and it always succeeded... what symptom this patch was expected to fix ?

Cheers.

--
Greg

Quoted 87 lines
Quoted 86 lines
Quoted 86 lines
 hw/virtio/virtio-pci.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
hw/virtio/virtio-pci.c +20 -19
index a827cd4..f560814 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -480,13 +469,18 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
+    uint8_t cmd = proxy->pci_dev.config[PCI_COMMAND];
+
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
         !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        (cmd & PCI_COMMAND_MASTER)) {
+        /* Bus driver disables bus mastering - make it act
+         * as a kind of reset to render the device quiescent. */
         virtio_pci_stop_ioeventfd(proxy);
-        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
+        virtio_reset(vdev);
+        msix_unuse_all_vectors(&proxy->pci_dev);
     }
 }
 
@@ -895,11 +889,19 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+        /* Linux before 2.6.34 drives the device without enabling
+           the PCI device bus master bit. Enable it automatically
+           for the guest. This is a PCI spec violation but so is
+           initiating DMA with bus master bit clear.
+           Note: this only makes a difference when migrating
+           across QEMU versions from an old QEMU, as for new QEMU
+           bus master and driver bits are always in sync.
+           TODO: consider enabling conditionally for compat machine types. */
+        if (vdev->status & (VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                            VIRTIO_CONFIG_S_DRIVER)) {
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,7 +1042,6 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
Quoted 8 lines
Quoted 3 lines

It's a race condition: you have to catch it migrating in the window before DRIVER_OK is set.

Ok I see... when DRIVER and MASTER bits are not in sync. I'll patch QEMU to trigger a migration in this window then.

Thanks.

BTW I reverted that patch, and to fix migration, I'm thinking about applying the following patch on top of master.

Would appreciate testing cross-version migration (2.1 to master) with this patch applied.

hw/virtio/virtio-pci.h +5 -0
index 1cea157..8873b6d 100644
@@ -53,6 +53,11 @@ typedef struct VirtioBusClass VirtioPCIBusClass;
 #define VIRTIO_PCI_BUS_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
 
+/* Need to activate work-arounds for buggy guests at vmstate load. */
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT  0
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
+    (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
+
 /* Performance improves when virtqueue kick processing is decoupled from the
  * vcpu thread using ioeventfd for some devices. */
 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
include/hw/i386/pc.h +5 -0
index bae023a..e07b6c4 100644
@@ -312,6 +312,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "intel-hda",\
             .property = "old_msi_addr",\
             .value    = "on",\
+        },\
+        {\
+            .driver   = "virtio-pci",\
+            .property = "virtio-pci-bus-master-bug-migration",\
+            .value    = "on",\
         }
 
 #define PC_COMPAT_2_0 \
hw/virtio/virtio-pci.c +11 -18
index a827cd4..a499a3c 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -483,8 +472,7 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
-        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
         virtio_pci_stop_ioeventfd(proxy);
         virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
     }
@@ -895,11 +883,15 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        /* Old QEMU versions did not set bus master enable on status write.
+         * Detect DRIVER set and enable it.
+         */
+        if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
+            (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,10 +1032,11 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
+    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
+                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
     DEFINE_PROP_END_OF_LIST(),
 };
#7 lore raw
Quoted 5 lines · On Mon, 6 Oct 2014 20:25:04 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

[...]

BTW I reverted that patch, and to fix migration, I'm thinking about applying the following patch on top of master.

Michael,

I could force the migration issue with a rhel65 guest thanks to the following patch, applied to hw/virtio/virtio-pci.c in QEMU v2.1.

@@ -271,6 +272,16 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)

     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     hwaddr pa;

+ if ((vdev->status == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER))
+ && (!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER))
+ && getenv("MIG_BUG"))
+ {
+ fprintf(stderr, "\n\n\n\tMIGRATE !\n\n\n");
+ qmp_migrate(getenv("MIG_BUG"), false, false, false, false, false,
+ false, NULL);
+ unsetenv("MIG_BUG");
+ }
+

     switch (addr) {
     case VIRTIO_PCI_GUEST_FEATURES:
         /* Guest does not negotiate properly?  We have to assume nothing. */

Indeed, the destination QEMU master hangs because bus master isn't set.

Quoted 3 lines

Would appreciate testing cross-version migration (2.1 to master) with this patch applied.

I had first to to enable the property for pseries of course. I could then migrate QEMU v2.1 to QEMU master and back to QEMU v2.1, in the window where we have DRIVER enabled and MASTER disabled, without experiencing the hang.

Your fix works as expected (just a remark below).

Quoted 24 lines
hw/virtio/virtio-pci.h +5 -0
index 1cea157..8873b6d 100644
@@ -53,6 +53,11 @@ typedef struct VirtioBusClass VirtioPCIBusClass;
 #define VIRTIO_PCI_BUS_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
 
+/* Need to activate work-arounds for buggy guests at vmstate load. */
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT  0
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
+    (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
+
 /* Performance improves when virtqueue kick processing is decoupled from the
  * vcpu thread using ioeventfd for some devices. */
 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
include/hw/i386/pc.h +0 -0
index bae023a..e07b6c4 100644
@@ -312,6 +312,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "intel-hda",\

include/hw/i386/pc.h bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *); · line 322

Quoted 10 lines
include/hw/i386/pc.h +5 -0
             .property = "old_msi_addr",\
             .value    = "on",\
+        },\
+        {\
+            .driver   = "virtio-pci",\
+            .property = "virtio-pci-bus-master-bug-migration",\
+            .value    = "on",\
         }
 
 #define PC_COMPAT_2_0 \

FWIW, the issue does not occur with intel targets, at least not in my test case (booting rhel6 on a virtio-blk disk). I see bus master is set early (bios ?) and never unset...

If you decide to apply for intel anyway, shouldn't the enablement be in a separate patch ?

Will you resend or would you like me to do it, along with the pseries enablement part ? In this case, I would need your SoB for the present patch.

Thanks.

--
Greg

Quoted 73 lines
hw/virtio/virtio-pci.c +11 -18
index a827cd4..a499a3c 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -483,8 +472,7 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
-        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
         virtio_pci_stop_ioeventfd(proxy);
         virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
     }
@@ -895,11 +883,15 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        /* Old QEMU versions did not set bus master enable on status write.
+         * Detect DRIVER set and enable it.
+         */
+        if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
+            (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,10 +1032,11 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
+    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
+                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
     DEFINE_PROP_END_OF_LIST(),
 };
#8 lore raw
Quoted 87 lines · Greg Kurz
Quoted 5 lines · On Mon, 6 Oct 2014 20:25:04 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

[...]

BTW I reverted that patch, and to fix migration, I'm thinking about applying the following patch on top of master.

Michael,

I could force the migration issue with a rhel65 guest thanks to the following patch, applied to hw/virtio/virtio-pci.c in QEMU v2.1.

(quoted diff) +10 -0
@@ -271,6 +272,16 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     hwaddr pa;
 
+    if ((vdev->status == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER))
+        && (!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER))
+        && getenv("MIG_BUG"))
+    {
+        fprintf(stderr, "\n\n\n\tMIGRATE !\n\n\n");
+        qmp_migrate(getenv("MIG_BUG"), false, false, false, false, false,
+                        false, NULL);
+        unsetenv("MIG_BUG");
+    }
+
     switch (addr) {
     case VIRTIO_PCI_GUEST_FEATURES:
         /* Guest does not negotiate properly?  We have to assume nothing. */

Indeed, the destination QEMU master hangs because bus master isn't set.

Quoted 3 lines

Would appreciate testing cross-version migration (2.1 to master) with this patch applied.

I had first to to enable the property for pseries of course. I could then migrate QEMU v2.1 to QEMU master and back to QEMU v2.1, in the window where we have DRIVER enabled and MASTER disabled, without experiencing the hang.

Your fix works as expected (just a remark below).

Quoted 24 lines
hw/virtio/virtio-pci.h +5 -0
index 1cea157..8873b6d 100644
@@ -53,6 +53,11 @@ typedef struct VirtioBusClass VirtioPCIBusClass;
 #define VIRTIO_PCI_BUS_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
 
+/* Need to activate work-arounds for buggy guests at vmstate load. */
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT  0
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
+    (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
+
 /* Performance improves when virtqueue kick processing is decoupled from the
  * vcpu thread using ioeventfd for some devices. */
 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
include/hw/i386/pc.h +0 -0
index bae023a..e07b6c4 100644
@@ -312,6 +312,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "intel-hda",\

include/hw/i386/pc.h bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *); · line 322

Quoted 10 lines
include/hw/i386/pc.h +5 -0
             .property = "old_msi_addr",\
             .value    = "on",\
+        },\
+        {\
+            .driver   = "virtio-pci",\
+            .property = "virtio-pci-bus-master-bug-migration",\
+            .value    = "on",\
         }
 
 #define PC_COMPAT_2_0 \

FWIW, the issue does not occur with intel targets, at least not in my test case (booting rhel6 on a virtio-blk disk). I see bus master is set early (bios ?) and never unset...

If you decide to apply for intel anyway, shouldn't the enablement be in a separate patch ?

Quoted 8 lines

Will you resend or would you like me to do it, along with the pseries enablement part ? In this case, I would need your SoB for the present patch.

Thanks.

--
Greg

AFAIK pseries doesn't support cross-version migration, does it?

Quoted 73 lines
Quoted 73 lines
hw/virtio/virtio-pci.c +11 -18
index a827cd4..a499a3c 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -483,8 +472,7 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
-        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
         virtio_pci_stop_ioeventfd(proxy);
         virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
     }
@@ -895,11 +883,15 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        /* Old QEMU versions did not set bus master enable on status write.
+         * Detect DRIVER set and enable it.
+         */
+        if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
+            (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,10 +1032,11 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
+    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
+                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
     DEFINE_PROP_END_OF_LIST(),
 };
#9 lore raw
Quoted 92 lines · On Mon, 13 Oct 2014 12:01:07 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 91 lines · Greg Kurz
Quoted 5 lines · On Mon, 6 Oct 2014 20:25:04 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

[...]

BTW I reverted that patch, and to fix migration, I'm thinking about applying the following patch on top of master.

Michael,

I could force the migration issue with a rhel65 guest thanks to the following patch, applied to hw/virtio/virtio-pci.c in QEMU v2.1.

(quoted diff) +10 -0
@@ -271,6 +272,16 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     hwaddr pa;
 
+    if ((vdev->status == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER))
+        && (!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER))
+        && getenv("MIG_BUG"))
+    {
+        fprintf(stderr, "\n\n\n\tMIGRATE !\n\n\n");
+        qmp_migrate(getenv("MIG_BUG"), false, false, false, false, false,
+                        false, NULL);
+        unsetenv("MIG_BUG");
+    }
+
     switch (addr) {
     case VIRTIO_PCI_GUEST_FEATURES:
         /* Guest does not negotiate properly?  We have to assume nothing. */

Indeed, the destination QEMU master hangs because bus master isn't set.

Quoted 3 lines

Would appreciate testing cross-version migration (2.1 to master) with this patch applied.

I had first to to enable the property for pseries of course. I could then migrate QEMU v2.1 to QEMU master and back to QEMU v2.1, in the window where we have DRIVER enabled and MASTER disabled, without experiencing the hang.

Your fix works as expected (just a remark below).

Quoted 24 lines
hw/virtio/virtio-pci.h +5 -0
index 1cea157..8873b6d 100644
@@ -53,6 +53,11 @@ typedef struct VirtioBusClass VirtioPCIBusClass;
 #define VIRTIO_PCI_BUS_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
 
+/* Need to activate work-arounds for buggy guests at vmstate load. */
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT  0
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
+    (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
+
 /* Performance improves when virtqueue kick processing is decoupled from the
  * vcpu thread using ioeventfd for some devices. */
 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
include/hw/i386/pc.h +0 -0
index bae023a..e07b6c4 100644
@@ -312,6 +312,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "intel-hda",\

include/hw/i386/pc.h bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *); · line 322

Quoted 10 lines
include/hw/i386/pc.h +5 -0
             .property = "old_msi_addr",\
             .value    = "on",\
+        },\
+        {\
+            .driver   = "virtio-pci",\
+            .property = "virtio-pci-bus-master-bug-migration",\
+            .value    = "on",\
         }
 
 #define PC_COMPAT_2_0 \

FWIW, the issue does not occur with intel targets, at least not in my test case (booting rhel6 on a virtio-blk disk). I see bus master is set early (bios ?) and never unset...

If you decide to apply for intel anyway, shouldn't the enablement be in a separate patch ?

Will you resend or would you like me to do it, along with the pseries enablement part ? In this case, I would need your SoB for the present patch.

Quoted 7 lines
Quoted 4 lines

Thanks.

--
Greg

AFAIK pseries doesn't support cross-version migration, does it?

(Cc'ing Alexander and Alexey)

It is still at an early stage, but we have that:

commit 6026db4501f773caaa2895cde7f93022960c7169

Author: Alexey Kardashevskiy <[email protected]>
Date:   Wed Jun 25 14:08:45 2014 +1000

spapr: Define a 2.1 pseries machine

Quoted 76 lines
Quoted 73 lines
Quoted 73 lines
hw/virtio/virtio-pci.c +11 -18
index a827cd4..a499a3c 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -483,8 +472,7 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
-        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
         virtio_pci_stop_ioeventfd(proxy);
         virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
     }
@@ -895,11 +883,15 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        /* Old QEMU versions did not set bus master enable on status write.
+         * Detect DRIVER set and enable it.
+         */
+        if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
+            (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,10 +1032,11 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
+    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
+                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
     DEFINE_PROP_END_OF_LIST(),
 };
#12 lore raw
In reply to Greg Kurz
Quoted 78 lines · Greg Kurz
Quoted 5 lines · On Mon, 6 Oct 2014 20:25:04 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

[...]

BTW I reverted that patch, and to fix migration, I'm thinking about applying the following patch on top of master.

Michael,

I could force the migration issue with a rhel65 guest thanks to the following patch, applied to hw/virtio/virtio-pci.c in QEMU v2.1.

(quoted diff) +10 -0
@@ -271,6 +272,16 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     hwaddr pa;
 
+    if ((vdev->status == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER))
+        && (!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER))
+        && getenv("MIG_BUG"))
+    {
+        fprintf(stderr, "\n\n\n\tMIGRATE !\n\n\n");
+        qmp_migrate(getenv("MIG_BUG"), false, false, false, false, false,
+                        false, NULL);
+        unsetenv("MIG_BUG");
+    }
+
     switch (addr) {
     case VIRTIO_PCI_GUEST_FEATURES:
         /* Guest does not negotiate properly?  We have to assume nothing. */

Indeed, the destination QEMU master hangs because bus master isn't set.

Quoted 3 lines

Would appreciate testing cross-version migration (2.1 to master) with this patch applied.

I had first to to enable the property for pseries of course. I could then migrate QEMU v2.1 to QEMU master and back to QEMU v2.1, in the window where we have DRIVER enabled and MASTER disabled, without experiencing the hang.

Your fix works as expected (just a remark below).

Quoted 33 lines
hw/virtio/virtio-pci.h +5 -0
index 1cea157..8873b6d 100644
@@ -53,6 +53,11 @@ typedef struct VirtioBusClass VirtioPCIBusClass;
 #define VIRTIO_PCI_BUS_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
 
+/* Need to activate work-arounds for buggy guests at vmstate load. */
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT  0
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
+    (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
+
 /* Performance improves when virtqueue kick processing is decoupled from the
  * vcpu thread using ioeventfd for some devices. */
 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
include/hw/i386/pc.h +5 -0
index bae023a..e07b6c4 100644
@@ -312,6 +312,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "intel-hda",\
             .property = "old_msi_addr",\
             .value    = "on",\
+        },\
+        {\
+            .driver   = "virtio-pci",\
+            .property = "virtio-pci-bus-master-bug-migration",\
+            .value    = "on",\
         }
 
Quoted 4 lines

include/hw/i386/pc.h bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *); · line 322

Quoted 1 line
include/hw/i386/pc.h +0 -0
 #define PC_COMPAT_2_0 \

FWIW, the issue does not occur with intel targets, at least not in my test case (booting rhel6 on a virtio-blk disk).

This will reproduce with rhel5 though.

Quoted 2 lines

I see bus
master is set early (bios ?) and never unset...

IIUC if you don't boot from device, it won't be set, and will not be set with older guests.

Quoted 85 lines

If you decide to apply for intel anyway, shouldn't the enablement be in a separate patch ?

Will you resend or would you like me to do it, along with the pseries enablement part ? In this case, I would need your SoB for the present patch.

Thanks.

--
Greg

Quoted 73 lines
hw/virtio/virtio-pci.c +11 -18
index a827cd4..a499a3c 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -483,8 +472,7 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
-        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
         virtio_pci_stop_ioeventfd(proxy);
         virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
     }
@@ -895,11 +883,15 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        /* Old QEMU versions did not set bus master enable on status write.
+         * Detect DRIVER set and enable it.
+         */
+        if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
+            (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,10 +1032,11 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
+    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
+                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
     DEFINE_PROP_END_OF_LIST(),
 };
#10 lore raw
In reply to Greg Kurz
Quoted 95 lines · Greg Kurz
Quoted 92 lines · On Mon, 13 Oct 2014 12:01:07 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 91 lines · Greg Kurz
Quoted 5 lines · On Mon, 6 Oct 2014 20:25:04 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

[...]

BTW I reverted that patch, and to fix migration, I'm thinking about applying the following patch on top of master.

Michael,

I could force the migration issue with a rhel65 guest thanks to the following patch, applied to hw/virtio/virtio-pci.c in QEMU v2.1.

(quoted diff) +10 -0
@@ -271,6 +272,16 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     hwaddr pa;
 
+    if ((vdev->status == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER))
+        && (!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER))
+        && getenv("MIG_BUG"))
+    {
+        fprintf(stderr, "\n\n\n\tMIGRATE !\n\n\n");
+        qmp_migrate(getenv("MIG_BUG"), false, false, false, false, false,
+                        false, NULL);
+        unsetenv("MIG_BUG");
+    }
+
     switch (addr) {
     case VIRTIO_PCI_GUEST_FEATURES:
         /* Guest does not negotiate properly?  We have to assume nothing. */

Indeed, the destination QEMU master hangs because bus master isn't set.

Quoted 3 lines

Would appreciate testing cross-version migration (2.1 to master) with this patch applied.

I had first to to enable the property for pseries of course. I could then migrate QEMU v2.1 to QEMU master and back to QEMU v2.1, in the window where we have DRIVER enabled and MASTER disabled, without experiencing the hang.

Your fix works as expected (just a remark below).

Quoted 24 lines
hw/virtio/virtio-pci.h +5 -0
index 1cea157..8873b6d 100644
@@ -53,6 +53,11 @@ typedef struct VirtioBusClass VirtioPCIBusClass;
 #define VIRTIO_PCI_BUS_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
 
+/* Need to activate work-arounds for buggy guests at vmstate load. */
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT  0
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
+    (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
+
 /* Performance improves when virtqueue kick processing is decoupled from the
  * vcpu thread using ioeventfd for some devices. */
 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
include/hw/i386/pc.h +0 -0
index bae023a..e07b6c4 100644
@@ -312,6 +312,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "intel-hda",\

include/hw/i386/pc.h bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *); · line 322

Quoted 10 lines
include/hw/i386/pc.h +5 -0
             .property = "old_msi_addr",\
             .value    = "on",\
+        },\
+        {\
+            .driver   = "virtio-pci",\
+            .property = "virtio-pci-bus-master-bug-migration",\
+            .value    = "on",\
         }
 
 #define PC_COMPAT_2_0 \

FWIW, the issue does not occur with intel targets, at least not in my test case (booting rhel6 on a virtio-blk disk). I see bus master is set early (bios ?) and never unset...

If you decide to apply for intel anyway, shouldn't the enablement be in a separate patch ?

Will you resend or would you like me to do it, along with the pseries enablement part ? In this case, I would need your SoB for the present patch.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Thanks.

--
Greg

AFAIK pseries doesn't support cross-version migration, does it?

We're trying to make sure we don't break cross-version migration for the pseries machines. If nothing else, at least as a learning exercise.

Alex

#11 lore raw
Quoted 103 lines · Alexander Graf
Quoted 100 lines · Greg Kurz
Quoted 97 lines · On Mon, 13 Oct 2014 12:01:07 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 95 lines · Greg Kurz
Quoted 5 lines · On Mon, 6 Oct 2014 20:25:04 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

[...]

BTW I reverted that patch, and to fix migration, I'm thinking about applying the following patch on top of master.

Michael,

I could force the migration issue with a rhel65 guest thanks to the following patch, applied to hw/virtio/virtio-pci.c in QEMU v2.1.

(quoted diff) +10 -0
@@ -271,6 +272,16 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     hwaddr pa;
 
+    if ((vdev->status == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER))
+        && (!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER))
+        && getenv("MIG_BUG"))
+    {
+        fprintf(stderr, "\n\n\n\tMIGRATE !\n\n\n");
+        qmp_migrate(getenv("MIG_BUG"), false, false, false, false, false,
+                        false, NULL);
+        unsetenv("MIG_BUG");
+    }
+
     switch (addr) {
     case VIRTIO_PCI_GUEST_FEATURES:
         /* Guest does not negotiate properly?  We have to assume nothing. */

Indeed, the destination QEMU master hangs because bus master isn't set.

Quoted 3 lines

Would appreciate testing cross-version migration (2.1 to master) with this patch applied.

I had first to to enable the property for pseries of course. I could then migrate QEMU v2.1 to QEMU master and back to QEMU v2.1, in the window where we have DRIVER enabled and MASTER disabled, without experiencing the hang.

Your fix works as expected (just a remark below).

Quoted 24 lines
hw/virtio/virtio-pci.h +5 -0
index 1cea157..8873b6d 100644
@@ -53,6 +53,11 @@ typedef struct VirtioBusClass VirtioPCIBusClass;
 #define VIRTIO_PCI_BUS_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
 
+/* Need to activate work-arounds for buggy guests at vmstate load. */
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT  0
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
+    (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
+
 /* Performance improves when virtqueue kick processing is decoupled from the
  * vcpu thread using ioeventfd for some devices. */
 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
include/hw/i386/pc.h +0 -0
index bae023a..e07b6c4 100644
@@ -312,6 +312,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "intel-hda",\

include/hw/i386/pc.h bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *); · line 322

Quoted 10 lines
include/hw/i386/pc.h +5 -0
             .property = "old_msi_addr",\
             .value    = "on",\
+        },\
+        {\
+            .driver   = "virtio-pci",\
+            .property = "virtio-pci-bus-master-bug-migration",\
+            .value    = "on",\
         }
 
 #define PC_COMPAT_2_0 \

FWIW, the issue does not occur with intel targets, at least not in my test case (booting rhel6 on a virtio-blk disk). I see bus master is set early (bios ?) and never unset...

If you decide to apply for intel anyway, shouldn't the enablement be in a separate patch ?

Will you resend or would you like me to do it, along with the pseries enablement part ? In this case, I would need your SoB for the present patch.

Thanks.

--
Greg

Quoted 7 lines
Quoted 1 line
Quoted 1 line

AFAIK pseries doesn't support cross-version migration, does it?

We're trying to make sure we don't break cross-version migration for the pseries machines. If nothing else, at least as a learning exercise.

Alex

I see.
In that case, I think we need a common header, where we can add device compatibility defines. With PC and PSERIES would then pull it in.

Let's not duplicate code.

I'll try to come up with a patch.

Signature · 1 line
MST
#13 lore raw
In reply to Michael S. Tsirkin
Quoted 84 lines · On Mon, 13 Oct 2014 14:09:54 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 78 lines · Greg Kurz
Quoted 5 lines · On Mon, 6 Oct 2014 20:25:04 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

[...]

BTW I reverted that patch, and to fix migration, I'm thinking about applying the following patch on top of master.

Michael,

I could force the migration issue with a rhel65 guest thanks to the following patch, applied to hw/virtio/virtio-pci.c in QEMU v2.1.

(quoted diff) +10 -0
@@ -271,6 +272,16 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     hwaddr pa;
 
+    if ((vdev->status == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER))
+        && (!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER))
+        && getenv("MIG_BUG"))
+    {
+        fprintf(stderr, "\n\n\n\tMIGRATE !\n\n\n");
+        qmp_migrate(getenv("MIG_BUG"), false, false, false, false, false,
+                        false, NULL);
+        unsetenv("MIG_BUG");
+    }
+
     switch (addr) {
     case VIRTIO_PCI_GUEST_FEATURES:
         /* Guest does not negotiate properly?  We have to assume nothing. */

Indeed, the destination QEMU master hangs because bus master isn't set.

Quoted 3 lines

Would appreciate testing cross-version migration (2.1 to master) with this patch applied.

I had first to to enable the property for pseries of course. I could then migrate QEMU v2.1 to QEMU master and back to QEMU v2.1, in the window where we have DRIVER enabled and MASTER disabled, without experiencing the hang.

Your fix works as expected (just a remark below).

Quoted 33 lines
hw/virtio/virtio-pci.h +5 -0
index 1cea157..8873b6d 100644
@@ -53,6 +53,11 @@ typedef struct VirtioBusClass VirtioPCIBusClass;
 #define VIRTIO_PCI_BUS_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
 
+/* Need to activate work-arounds for buggy guests at vmstate load. */
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT  0
+#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
+    (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
+
 /* Performance improves when virtqueue kick processing is decoupled from the
  * vcpu thread using ioeventfd for some devices. */
 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
include/hw/i386/pc.h +5 -0
index bae023a..e07b6c4 100644
@@ -312,6 +312,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "intel-hda",\
             .property = "old_msi_addr",\
             .value    = "on",\
+        },\
+        {\
+            .driver   = "virtio-pci",\
+            .property = "virtio-pci-bus-master-bug-migration",\
+            .value    = "on",\
         }
 
Quoted 4 lines

include/hw/i386/pc.h bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *); · line 322

Quoted 1 line
include/hw/i386/pc.h +0 -0
 #define PC_COMPAT_2_0 \

FWIW, the issue does not occur with intel targets, at least not in my test case (booting rhel6 on a virtio-blk disk).

Quoted 8 lines

This will reproduce with rhel5 though.

Quoted 2 lines

I see bus
master is set early (bios ?) and never unset...

IIUC if you don't boot from device, it won't be set, and will not be set with older guests.

Not sure to follow... you mean that if I boot from memory ? Like, passing -kernel and friends to QEMU ?

Quoted 86 lines
Quoted 85 lines

If you decide to apply for intel anyway, shouldn't the enablement be in a separate patch ?

Will you resend or would you like me to do it, along with the pseries enablement part ? In this case, I would need your SoB for the present patch.

Thanks.

--
Greg

Quoted 73 lines
hw/virtio/virtio-pci.c +11 -18
index a827cd4..a499a3c 100644
@@ -86,9 +86,6 @@
  * 12 is historical, and due to x86 page size. */
 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
-/* Flags track per-device state like workarounds for quirks in older guests. */
-#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
-
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 
@@ -323,14 +320,6 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
                                      proxy->pci_dev.config[PCI_COMMAND] |
                                      PCI_COMMAND_MASTER, 1);
         }
-
-        /* Linux before 2.6.34 sets the device as OK without enabling
-           the PCI device bus master bit. In this case we need to disable
-           some safety checks. */
-        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
-            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
-        }
         break;
     case VIRTIO_MSI_CONFIG_VECTOR:
         msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
@@ -483,8 +472,7 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
     pci_default_write_config(pci_dev, address, val, len);
 
     if (range_covers_byte(address, len, PCI_COMMAND) &&
-        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
-        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
+        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
         virtio_pci_stop_ioeventfd(proxy);
         virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
     }
@@ -895,11 +883,15 @@ static void virtio_pci_vmstate_change(DeviceState *d, bool running)
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
 
     if (running) {
-        /* Try to find out if the guest has bus master disabled, but is
-           in ready state. Then we have a buggy guest OS. */
-        if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        /* Old QEMU versions did not set bus master enable on status write.
+         * Detect DRIVER set and enable it.
+         */
+        if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
+            (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
-            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
+            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
+                                     proxy->pci_dev.config[PCI_COMMAND] |
+                                     PCI_COMMAND_MASTER, 1);
         }
         virtio_pci_start_ioeventfd(proxy);
     } else {
@@ -1040,10 +1032,11 @@ static void virtio_pci_reset(DeviceState *qdev)
     virtio_pci_stop_ioeventfd(proxy);
     virtio_bus_reset(bus);
     msix_unuse_all_vectors(&proxy->pci_dev);
-    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 }
 
 static Property virtio_pci_properties[] = {
+    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
+                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
     DEFINE_PROP_END_OF_LIST(),
 };