qemu: fix hot remove assigned device
RFC PATCH 1/1 qemu: fix hot remove assigned device
When hot remove an assigned device, segmentation fault was triggered by qemu_free(&pci_dev->qdev) in pci_unregister_device(). pci_register_device() doesn't initialize or set pci_dev->qdev. For an assigned device, qdev variable isn't touched at all. So segmentation fault happens when to free a non-initialized qdev.
Paul,
you introduced the code to free qdev in pci_unregiser_device. Did you miss something?
Following patch changes the code back to free pci_dev, and fixes the hot remove issue.
--- hw/pci.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
hw/pci.c +1 -1
@@ -377,7 +377,7 @@ int pci_unregister_device(PCIDevice *pci_dev)qemu_free_irqs (pci_dev->irq); pci_irq_index--; pci_dev->bus->devices[pci_dev->devfn] =NULL ; -qdev_free (&pci_dev->qdev); +qemu_free (pci_dev);return 0 ; }
Signature · 1 line
1.6.0.4
Quoted 5 lines · Weidong Han
When hot remove an assigned device, segmentation fault was triggered by
qemu_free(&pci_dev->qdev)inpci_unregister_device().pci_register_device()doesn't initialize or setpci_dev->qdev. For an assigned device, qdev variable isn't touched at all. So segmentation fault happens when to free a non-initialized qdev.
Better would be to just disable hot remove for devices still using the legacy pci_register_device API.
Paul
Quoted 10 lines · Paul Brook
Quoted 5 lines · Weidong Han
When hot remove an assigned device, segmentation fault was triggered by
qemu_free(&pci_dev->qdev)inpci_unregister_device().pci_register_device()doesn't initialize or setpci_dev->qdev. For an assigned device, qdev variable isn't touched at all. So segmentation fault happens when to free a non-initialized qdev.Better would be to just disable hot remove for devices still using the legacy
pci_register_deviceAPI.
PCI passthrough uses pci_register_device to register assigned device to qemu. Is there newer API to do so?
Regards,
Weidong
Quoted 8 lines · Weidong
Quoted 7 lines · Paul Brook
Quoted 5 lines · Weidong Han
When hot remove an assigned device, segmentation fault was triggered by
qemu_free(&pci_dev->qdev)inpci_unregister_device().pci_register_device()doesn't initialize or setpci_dev->qdev. For an assigned device, qdev variable isn't touched at all. So segmentation fault happens when to free a non-initialized qdev.
Quoted 5 lines
Quoted 2 lines
Better would be to just disable hot remove for devices still using the legacy
pci_register_deviceAPI.PCI passthrough uses
pci_register_deviceto register assigned device to qemu. Is there newer API to do so?
Yes. See e.g. LSI scsi emulation.
Paul
Quoted 8 lines · Paul Brook
Quoted 7 lines · Weidong
Quoted 6 lines · Paul Brook
Quoted 5 lines · Weidong Han
When hot remove an assigned device, segmentation fault was triggered by
qemu_free(&pci_dev->qdev)inpci_unregister_device().pci_register_device()doesn't initialize or setpci_dev->qdev. For an assigned device, qdev variable isn't touched at all. So segmentation fault happens when to free a non-initialized qdev.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Better would be to just disable hot remove for devices still using the legacy
pci_register_deviceAPI.PCI passthrough uses
pci_register_deviceto register assigned device to qemu. Is there newer API to do so?Yes. See e.g. LSI scsi emulation.
Well. Except that you can't (yet) register pci config read/write callbacks using the qdev-based API.
cheers,
Gerd
Quoted 11 lines · Gerd Hoffmann
Quoted 10 lines · Paul Brook
Quoted 9 lines · Weidong
Quoted 8 lines · Paul Brook
Quoted 5 lines · Weidong Han
When hot remove an assigned device, segmentation fault was triggered by
qemu_free(&pci_dev->qdev)inpci_unregister_device().pci_register_device()doesn't initialize or setpci_dev->qdev. For an assigned device, qdev variable isn't touched at all. So segmentation fault happens when to free a non-initialized qdev.Better would be to just disable hot remove for devices still using the legacy
pci_register_deviceAPI.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
PCI passthrough uses
pci_register_deviceto register assigned device to qemu. Is there newer API to do so?Yes. See e.g. LSI scsi emulation.
Well. Except that you can't (yet) register pci config read/write callbacks using the qdev-based API.
So pci passthrough have to use pci_register_device now. I cooked a new patch (post below) to fix this issue. Thanks.
When hot remove an assigned device, segmentation fault was triggered by qdev_free(&pci_dev->qdev) in pci_unregister_device(). pci_register_device() doesn't initialize or set pci_dev->qdev. For an assigned device, qdev variable isn't touched at all. So segmentation fault happens when to free a non-initialized qdev.
This patch adds a parameter in pci_unregister_device to check if it's an assigned device. For assgined device, free pci_dev instead of qdev. No changes for other devices.
--- hw/device-assignment.c | 2 +- hw/pci-hotplug.c | 2 +- hw/pci.c | 8 ++++++-- hw/pci.h | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-)
hw/device-assignment.c +1 -1
@@ -581,7 +581,7 @@ static void free_assigned_device(AssignedDevInfo *adev) dev->real_device.config_fd =0 ; } -pci_unregister_device (&dev->dev); +pci_unregister_device (&dev->dev,1 );#ifdef KVM_CAP_IRQ_ROUTING free_dev_irq_entries (dev);#endif
hw/pci-hotplug.c +1 -1
@@ -271,6 +271,6 @@ void pci_device_hot_remove_success(int pcibus, int slot)break ; } -pci_unregister_device (d); +pci_unregister_device (d,0 ); }
hw/pci.c +6 -2
@@ -363,7 +363,7 @@ static void pci_unregister_io_regions(PCIDevice *pci_dev) } } -int pci_unregister_device (PCIDevice *pci_dev) +int pci_unregister_device (PCIDevice *pci_dev,int assigned) {int ret =0 ;
@@ -377,7 +377,11 @@ int pci_unregister_device(PCIDevice *pci_dev)qemu_free_irqs (pci_dev->irq); pci_irq_index--; pci_dev->bus->devices[pci_dev->devfn] =NULL ; -qdev_free (&pci_dev->qdev); + +if (assigned) +qemu_free (pci_dev); +else +qdev_free (&pci_dev->qdev);return 0 ; }
hw/pci.h +1 -1
@@ -199,7 +199,7 @@ PCIDevice *pci_register_device(PCIBus *bus, const char *name, int instance_size, int devfn, PCIConfigReadFunc *config_read, PCIConfigWriteFunc *config_write); -int pci_unregister_device (PCIDevice *pci_dev); +int pci_unregister_device (PCIDevice *pci_dev,int assigned); void pci_register_io_region(PCIDevice *pci_dev, int region_num, uint32_t size, int type,
Signature · 1 line
1.6.0.4
Quoted 9 lines · Weidong
hw/pci.c +1
-1-int pci_unregister_device (PCIDevice *pci_dev) +int pci_unregister_device (PCIDevice *pci_dev,int assigned) {int ret =0 ;@@ -377,7 +377,11 @@ int pci_unregister_device(PCIDevice *pci_dev)qemu_free_irqs (pci_dev->irq); pci_irq_index--;
hw/pci.c int pci_unregister_device(PCIDevice *pci_dev) · line 387
Quoted 10 lines
hw/pci.c +5
-1pci_dev->bus->devices[pci_dev->devfn] =NULL ; -qdev_free (&pci_dev->qdev); + +if (assigned) +qemu_free (pci_dev); +else +qdev_free (&pci_dev->qdev);return 0 ; }
Can you check pci_dev->qdev instead of assigned? A little less ugly.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Weidong
hw/pci.c +1
-1-int pci_unregister_device (PCIDevice *pci_dev) +int pci_unregister_device (PCIDevice *pci_dev,int assigned) {int ret =0 ;
Quoted 15 lines
hw/pci.c int pci_unregister_device(PCIDevice *pci_dev) · line 387
Quoted 13 lines
hw/pci.c +5
-1@@ -377,7 +377,11 @@ int pci_unregister_device(PCIDevice *pci_dev)qemu_free_irqs (pci_dev->irq); pci_irq_index--; pci_dev->bus->devices[pci_dev->devfn] =NULL ; -qdev_free (&pci_dev->qdev); + +if (assigned) +qemu_free (pci_dev); +else +qdev_free (&pci_dev->qdev);return 0 ; }Can you check
pci_dev->qdevinstead of assigned? A little less ugly.
I tried to find an easy and clean way to check it, but I found the members of struct PCIDevice and DeviceState were not suitable for this checking, and qdev is not pointer in struct PCIDevice.
Regards,
Weidong
Quoted 2 lines · Weidong
I tried to find an easy and clean way to check it, but I found the members of struct PCIDevice and DeviceState were not suitable for this checking, and qdev is not pointer in struct PCIDevice.
Yes, of course. I applied the patch, thanks.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 7 lines · Weidong
Quoted 2 lines · Avi Kivity
Can you check
pci_dev->qdevinstead of assigned? A little less ugly.I tried to find an easy and clean way to check it, but I found the members of struct PCIDevice and DeviceState were not suitable for this checking, and qdev is not pointer in struct PCIDevice.
Well, certain DeviceState pointers (type for example) are set only in case the device was created using the qdev api. I think you certainly should get away without adding a new parameter to pci_unregister_device.
Also I've just sent a patch to the qemu-devel fixing the qdev register API for pci devices, allowing to register config space callbacks. Once this is merged you can switch pci passthrough to the new qdev API.
cheers,
Gerd
Quoted 14 lines · Gerd Hoffmann
Quoted 7 lines · Weidong
Quoted 2 lines · Avi Kivity
Can you check
pci_dev->qdevinstead of assigned? A little less ugly.I tried to find an easy and clean way to check it, but I found the members of struct PCIDevice and DeviceState were not suitable for this checking, and qdev is not pointer in struct PCIDevice.
Well, certain DeviceState pointers (type for example) are set only in case the device was created using the qdev api. I think you certainly should get away without adding a new parameter to
pci_unregister_device.
Quoted 5 lines
Also I've just sent a patch to the qemu-devel fixing the qdev register API for pci devices, allowing to register config space callbacks. Once this is merged you can switch pci passthrough to the new qdev API.
Good. Extending the qdev APIs is the most elegant solution. Thanks.
Regards,
Weidong