[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL v2 78/88] virtio-iommu: Clear IOMMUDevice when VFIO device is unpl
From: |
Michael S. Tsirkin |
Subject: |
[PULL v2 78/88] virtio-iommu: Clear IOMMUDevice when VFIO device is unplugged |
Date: |
Tue, 2 Jul 2024 16:20:20 -0400 |
From: Cédric Le Goater <clg@redhat.com>
When a VFIO device is hoplugged in a VM using virtio-iommu, IOMMUPciBus
and IOMMUDevice cache entries are created in the .get_address_space()
handler of the machine IOMMU device. However, these entries are never
destroyed, not even when the VFIO device is detached from the machine.
This can lead to an assert if the device is reattached again.
When reattached, the .get_address_space() handler reuses an
IOMMUDevice entry allocated when the VFIO device was first attached.
virtio_iommu_set_host_iova_ranges() is called later on from the
.set_iommu_device() handler an fails with an assert on 'probe_done'
because the device appears to have been already probed when this is
not the case.
The IOMMUDevice entry is allocated in pci_device_iommu_address_space()
called from under vfio_realize(), the VFIO PCI realize handler. Since
pci_device_unset_iommu_device() is called from vfio_exitfn(), a sub
function of the PCIDevice unrealize() handler, it seems that the
.unset_iommu_device() handler is the best place to release resources
allocated at realize time. Clear the IOMMUDevice cache entry there to
fix hotplug.
Fixes: 817ef10da23c ("virtio-iommu: Implement set|unset]_iommu_device()
callbacks")
Signed-off-by: Cédric Le Goater <clg@redhat.com>
Message-Id: <20240701101453.203985-1-clg@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
hw/virtio/virtio-iommu.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c
index ed7426afc7..7c54c6b5e2 100644
--- a/hw/virtio/virtio-iommu.c
+++ b/hw/virtio/virtio-iommu.c
@@ -467,6 +467,26 @@ static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus,
void *opaque,
return &sdev->as;
}
+static void virtio_iommu_device_clear(VirtIOIOMMU *s, PCIBus *bus, int devfn)
+{
+ IOMMUPciBus *sbus = g_hash_table_lookup(s->as_by_busptr, bus);
+ IOMMUDevice *sdev;
+
+ if (!sbus) {
+ return;
+ }
+
+ sdev = sbus->pbdev[devfn];
+ if (!sdev) {
+ return;
+ }
+
+ g_list_free_full(sdev->resv_regions, g_free);
+ sdev->resv_regions = NULL;
+ g_free(sdev);
+ sbus->pbdev[devfn] = NULL;
+}
+
static gboolean hiod_equal(gconstpointer v1, gconstpointer v2)
{
const struct hiod_key *key1 = v1;
@@ -650,6 +670,7 @@ virtio_iommu_unset_iommu_device(PCIBus *bus, void *opaque,
int devfn)
}
g_hash_table_remove(viommu->host_iommu_devices, &key);
+ virtio_iommu_device_clear(viommu, bus, devfn);
}
static const PCIIOMMUOps virtio_iommu_ops = {
--
MST
- [PULL v2 68/88] tests/qtest/bios-tables-test.c: Set "arch" for aarch64 tests, (continued)
- [PULL v2 68/88] tests/qtest/bios-tables-test.c: Set "arch" for aarch64 tests, Michael S. Tsirkin, 2024/07/02
- [PULL v2 69/88] tests/qtest/bios-tables-test.c: Set "arch" for x86 tests, Michael S. Tsirkin, 2024/07/02
- [PULL v2 70/88] tests/data/acpi: Move x86 ACPI tables under x86/${machine} path, Michael S. Tsirkin, 2024/07/02
- [PULL v2 71/88] tests/data/acpi/virt: Move ARM64 ACPI tables under aarch64/${machine} path, Michael S. Tsirkin, 2024/07/02
- [PULL v2 73/88] pc-bios/meson.build: Add support for RISC-V in unpack_edk2_blobs, Michael S. Tsirkin, 2024/07/02
- [PULL v2 74/88] tests/data/acpi/rebuild-expected-aml.sh: Add RISC-V, Michael S. Tsirkin, 2024/07/02
- [PULL v2 72/88] meson.build: Add RISC-V to the edk2-target list, Michael S. Tsirkin, 2024/07/02
- [PULL v2 76/88] hw/cxl/events: Mark cxl-add-dynamic-capacity and cxl-release-dynamic-capcity unstable, Michael S. Tsirkin, 2024/07/02
- [PULL v2 77/88] virtio: remove virtio_tswap16s() call in vring_packed_event_read(), Michael S. Tsirkin, 2024/07/02
- [PULL v2 75/88] hw/cxl/events: Improve QMP interfaces and documentation for add/release dynamic capacity., Michael S. Tsirkin, 2024/07/02
- [PULL v2 78/88] virtio-iommu: Clear IOMMUDevice when VFIO device is unplugged,
Michael S. Tsirkin <=
- [PULL v2 80/88] hw/ppc/spapr_pci: Do not create DT for disabled PCI device, Michael S. Tsirkin, 2024/07/02
- [PULL v2 79/88] hw/pci: Rename has_power to enabled, Michael S. Tsirkin, 2024/07/02
- [PULL v2 85/88] pcie_sriov: Release VFs failed to realize, Michael S. Tsirkin, 2024/07/02
- [PULL v2 82/88] pcie_sriov: Do not manually unrealize, Michael S. Tsirkin, 2024/07/02
- [PULL v2 83/88] pcie_sriov: Ensure VF function number does not overflow, Michael S. Tsirkin, 2024/07/02
- [PULL v2 84/88] pcie_sriov: Reuse SR-IOV VF device instances, Michael S. Tsirkin, 2024/07/02
- [PULL v2 87/88] pcie_sriov: Register VFs after migration, Michael S. Tsirkin, 2024/07/02
- [PULL v2 81/88] hw/ppc/spapr_pci: Do not reject VFs created after a PF, Michael S. Tsirkin, 2024/07/02
- [PULL v2 86/88] pcie_sriov: Remove num_vfs from PCIESriovPF, Michael S. Tsirkin, 2024/07/02
- [PULL v2 88/88] hw/pci: Replace -1 with UINT32_MAX for romsize, Michael S. Tsirkin, 2024/07/02