Skip to contentSkip to conversation

device-assignment: Reset device on system reset

PATCH Superseded Alex Williamson 3 participants 8 messages Mar 17, 2011 – Apr 3, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device-assignment: Reset device on system reset

On system reset, we currently try to quiesce DMA by clearing the command register. This assumes that nothing re-enables bus master support without first de-programming the device. Use a bigger hammer to help the guest not shoot itself by issuing a function reset via sysfs on each system reset.

---

 hw/device-assignment.c |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)
hw/device-assignment.c +22 -2
index db82e73..f1c2a3c 100644
@@ -1693,13 +1693,33 @@ static const VMStateDescription vmstate_assigned_device = {
 
 static void reset_assigned_device(DeviceState *dev)
 {
-    PCIDevice *d = DO_UPCAST(PCIDevice, qdev, dev);
+    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
+    AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
+    char reset_file[64];
+    const char reset[] = "1";
+    int fd, ret;
+
+    snprintf(reset_file, sizeof(reset_file),
+             "/sys/bus/pci/devices/0000:%02x:%02x.%01x/reset",
+             adev->host.bus, adev->host.dev, adev->host.func);
+
+    /*
+     * Issue a device reset via pci-sysfs.  Note that we use write(2) here
+     * and ignore the return value because some kernels have a bug that
+     * returns 0 rather than bytes written on success, sending us into an
+     * infinite retry loop using other write mechanisms.
+     */
+    fd = open(reset_file, O_WRONLY);
+    if (fd != -1) {
+        ret = write(fd, reset, strlen(reset));
+        close(fd);
+    }
 
     /*
      * When a 0 is written to the command register, the device is logically
      * disconnected from the PCI bus. This avoids further DMA transfers.
      */
-    assigned_dev_pci_write_config(d, PCI_COMMAND, 0, 2);
+    assigned_dev_pci_write_config(pci_dev, PCI_COMMAND, 0, 2);
 }
 
 static int assigned_initfn(struct PCIDevice *pci_dev)
#1 lore raw

hw/device-assignment.c static const VMStateDescription vmstate_assigned_device = { · line 1704

Quoted 12 lines · * Alex Williamson ([email protected]) wrote:

static void reset_assigned_device(DeviceState *dev) {

hw/device-assignment.c +9 -1
-    PCIDevice *d = DO_UPCAST(PCIDevice, qdev, dev);
+    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
+    AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
+    char reset_file[64];
+    const char reset[] = "1";
+    int fd, ret;
+
+    snprintf(reset_file, sizeof(reset_file),
+             "/sys/bus/pci/devices/0000:%02x:%02x.%01x/reset",
+             adev->host.bus, adev->host.dev, adev->host.func);

need to consider segment: %04x:..., adev->host.seg, ...

hw/device-assignment.c static const VMStateDescription vmstate_assigned_device = { · line 1716

Quoted 11 lines
hw/device-assignment.c +11 -0
+    /*
+     * Issue a device reset via pci-sysfs.  Note that we use write(2) here
+     * and ignore the return value because some kernels have a bug that
+     * returns 0 rather than bytes written on success, sending us into an
+     * infinite retry loop using other write mechanisms.
+     */
+    fd = open(reset_file, O_WRONLY);
+    if (fd != -1) {
+        ret = write(fd, reset, strlen(reset));
+        close(fd);
+    }

This will probably fail when it's managed by libvirt. I expect it will need some file ownership and security label mgmt added to device assignement path I expect.

#2 lore raw
Quoted 14 lines · Chris Wright

hw/device-assignment.c static const VMStateDescription vmstate_assigned_device = { · line 1704

Quoted 12 lines · * Alex Williamson ([email protected]) wrote:

static void reset_assigned_device(DeviceState *dev) {

hw/device-assignment.c +9 -1
-    PCIDevice *d = DO_UPCAST(PCIDevice, qdev, dev);
+    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
+    AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
+    char reset_file[64];
+    const char reset[] = "1";
+    int fd, ret;
+
+    snprintf(reset_file, sizeof(reset_file),
+             "/sys/bus/pci/devices/0000:%02x:%02x.%01x/reset",
+             adev->host.bus, adev->host.dev, adev->host.func);
Quoted 1 line

need to consider segment: %04x:..., adev->host.seg, ...

Shoot, I ported this over from a code base w/o seg support. Thanks for the catch.

Quoted 12 lines

hw/device-assignment.c static const VMStateDescription vmstate_assigned_device = { · line 1716

Quoted 11 lines
hw/device-assignment.c +11 -0
+    /*
+     * Issue a device reset via pci-sysfs.  Note that we use write(2) here
+     * and ignore the return value because some kernels have a bug that
+     * returns 0 rather than bytes written on success, sending us into an
+     * infinite retry loop using other write mechanisms.
+     */
+    fd = open(reset_file, O_WRONLY);
+    if (fd != -1) {
+        ret = write(fd, reset, strlen(reset));
+        close(fd);
+    }
Quoted 3 lines

This will probably fail when it's managed by libvirt. I expect it will need some file ownership and security label mgmt added to device assignement path I expect.

Already posted a patch for adding file rights, seems to be sufficient:

https://www.redhat.com/archives/libvir-list/2011-March/msg00823.html

Thanks,

Alex

#3 lore raw
Quoted 8 lines · * Alex Williamson ([email protected]) wrote:
Quoted 7 lines · Chris Wright
Quoted 5 lines · * Alex Williamson ([email protected]) wrote:
hw/device-assignment.c +5 -0
+    fd = open(reset_file, O_WRONLY);
+    if (fd != -1) {
+        ret = write(fd, reset, strlen(reset));
+        close(fd);
+    }
Quoted 7 lines
Quoted 3 lines

This will probably fail when it's managed by libvirt. I expect it will need some file ownership and security label mgmt added to device assignement path I expect.

Already posted a patch for adding file rights, seems to be sufficient:

https://www.redhat.com/archives/libvir-list/2011-March/msg00823.html

Awesome, I missed that path, thanks Alex!

thanks,
-chris

#4 lore raw

PATCH v2 1/1 device-assignment: Reset device on system reset

In reply to Alex Williamson

On system reset, we currently try to quiesce DMA by clearing the command register. This assumes that nothing re-enables bus master support without first de-programming the device. Use a bigger hammer to help the guest not shoot itself by issuing a function reset via sysfs on each system reset.

---

v2: Fix segment support

 hw/device-assignment.c |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)
hw/device-assignment.c +22 -2
index db82e73..4997b6e 100644
@@ -1693,13 +1693,33 @@ static const VMStateDescription vmstate_assigned_device = {
 
 static void reset_assigned_device(DeviceState *dev)
 {
-    PCIDevice *d = DO_UPCAST(PCIDevice, qdev, dev);
+    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
+    AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
+    char reset_file[64];
+    const char reset[] = "1";
+    int fd, ret;
+
+    snprintf(reset_file, sizeof(reset_file),
+             "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/reset",
+             adev->host.seg, adev->host.bus, adev->host.dev, adev->host.func);
+
+    /*
+     * Issue a device reset via pci-sysfs.  Note that we use write(2) here
+     * and ignore the return value because some kernels have a bug that
+     * returns 0 rather than bytes written on success, sending us into an
+     * infinite retry loop using other write mechanisms.
+     */
+    fd = open(reset_file, O_WRONLY);
+    if (fd != -1) {
+        ret = write(fd, reset, strlen(reset));
+        close(fd);
+    }
 
     /*
      * When a 0 is written to the command register, the device is logically
      * disconnected from the PCI bus. This avoids further DMA transfers.
      */
-    assigned_dev_pci_write_config(d, PCI_COMMAND, 0, 2);
+    assigned_dev_pci_write_config(pci_dev, PCI_COMMAND, 0, 2);
 }
 
 static int assigned_initfn(struct PCIDevice *pci_dev)
Acked-by on v2 1/1
#5 lore raw
Quoted 7 lines · * Alex Williamson ([email protected]) wrote:

On system reset, we currently try to quiesce DMA by clearing the command register. This assumes that nothing re-enables bus master support without first de-programming the device. Use a bigger hammer to help the guest not shoot itself by issuing a function reset via sysfs on each system reset.

Looks good.

#6 lore raw
Quoted 7 lines · Chris Wright
Quoted 6 lines · * Alex Williamson ([email protected]) wrote:

On system reset, we currently try to quiesce DMA by clearing the command register. This assumes that nothing re-enables bus master support without first de-programming the device. Use a bigger hammer to help the guest not shoot itself by issuing a function reset via sysfs on each system reset.

Quoted 5 lines
Quoted 1 line

Looks good.

Applied, thanks.

#7 lore raw
In reply to Alex Williamson
Quoted 6 lines · Alex Williamson

On system reset, we currently try to quiesce DMA by clearing the command register. This assumes that nothing re-enables bus master support without first de-programming the device. Use a bigger hammer to help the guest not shoot itself by issuing a function reset via sysfs on each system reset.

Quoted 6 lines

---

v2: Fix segment support

 hw/device-assignment.c |   24 ++++++++++++++++++++++--

Applied, thanks.