Skip to contentSkip to conversation

iommu/virtio: fix remove

RFC In review Cam Miller 3 participants 8 messages Nov 24 – Nov 25

Subsystems (3)
lore
Share & tools
lore
author
#3 lore raw

RFC 2/3 iommu/virtio: Prevent driver unbind via sysfs

In reply to Cam Miller

Set suppress_bind_attrs on the virtio-iommu driver to prevent userspace from triggering driver unbind via sysfs.

Unbinding the IOMMU driver from beneath live endpoint devices would leave those devices with dangling domain pointers. This would not be a recoverable operation.

Note that suppress_bind_attrs covers only the driver bind/unbind path, not PCI-level device removal triggered via the sysfs remove file.

Fixes edcd69ab9a32 ("iommu: Add virtio-iommu driver")
---
 drivers/iommu/virtio-iommu.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iommu/virtio-iommu.c +4 -1
index a08474eeed58..dd10c3e98a3d 100644
@@ -1285,7 +1285,10 @@ static struct virtio_device_id id_table[] = {
 MODULE_DEVICE_TABLE(virtio, id_table);
 
 static struct virtio_driver virtio_iommu_drv = {
-	.driver.name		= KBUILD_MODNAME,
+	.driver		= {
+		.name		= KBUILD_MODNAME,
+		.suppress_bind_attrs     = true,
+	},
 	.id_table		= id_table,
 	.feature_table		= features,
 	.feature_table_size	= ARRAY_SIZE(features),
Signature · 1 line
2.54.0
author
#1 lore raw

RFC 1/3 iommu/virtio: Fix out-of-order teardown in viommu_remove()

In reply to Cam Miller

Fix bug in viommu_remove() by swapping out-of-order IOMMU unregistration and sysfs removal steps.

Other IOMMU drivers all call iommu_device_unregister() before iommu_device_sysfs_remove(). Before this change, viommu_remove() had incorrectly reversed this ordering, leading to the following kernel oops.

  BUG: kernel NULL pointer dereference, address: 0000000000000030
  ...snip...
  RIP: 0010:sysfs_remove_link_from_group+0x10/0x40
  ...snip..
  Call Trace:
   <TASK>
  ...snip...
   ? sysfs_remove_link_from_group+0x10/0x40
   iommu_deinit_device+0x2a/0xe0
   __iommu_group_remove_device+0xb0/0xe0
   ? __pfx_remove_iommu_group+0x10/0x10
   remove_iommu_group+0x35/0x60
   bus_for_each_dev+0x83/0xd0
   iommu_device_unregister+0x3d/0xb0
   viommu_remove+0x25/0x60
   virtio_dev_remove+0x3c/0x90
  ...snip...

iommu_device_sysfs_remove() will set iommu->dev = NULL after tearing down the corresponding sysfs entry. Then iommu_device_unregister() will iterate over every endpoint device attached to the IOMMU and reference the null iommu->dev pointer, specifically via call sysfs_remove_link_from_group(&iommu->dev->kobj, ...).

Fixes edcd69ab9a32 ("iommu: Add virtio-iommu driver")
---
 drivers/iommu/virtio-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/virtio-iommu.c +1 -1
index 587fc13197f1..a08474eeed58 100644
@@ -1254,8 +1254,8 @@ static void viommu_remove(struct virtio_device *vdev)
 {
 	struct viommu_dev *viommu = vdev->priv;
 
-	iommu_device_sysfs_remove(&viommu->iommu);
 	iommu_device_unregister(&viommu->iommu);
+	iommu_device_sysfs_remove(&viommu->iommu);
 
 	/* Stop all virtqueues */
 	virtio_reset_device(vdev);
Signature · 1 line
2.54.0
author
#5 lore raw

RFC 3/3 iommu/virtio: Add device links for endpoints

In reply to Cam Miller

Make each endpoint a consumer of the virtio-iommu device, so that the driver core unbinds each endpoint driver before viommu_remove() runs.

Before this change, removing a virtio-iommu (for example via "echo 1 > /sys/bus/pci/devices/<dev>/remove") tears down the IOMMU groups of all its endpoints while their drivers are still bound. The endpoints keep stale IOVAs and their later dma_free_contiguous() leads to WARNs in __iommu_group_free_device(), endpoint IO hanging, and crash at shutdown.

Make each endpoint a consumer of the viommu with device_link_add(), as arm-smmu and other IOMMU drivers do. The driver core then unbinds the endpoint drivers before viommu_remove() runs, so that their DMA teardown will still see an intact domain.

Fixes edcd69ab9a32 ("iommu: Add virtio-iommu driver")
---
 drivers/iommu/virtio-iommu.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iommu/virtio-iommu.c +3 -0
index dd10c3e98a3d..a84a653f269e 100644
@@ -1044,6 +1044,9 @@ static struct iommu_device *viommu_probe_device(struct device *dev)
 			goto err_free_dev;
 	}
 
+	if (!device_link_add(dev, viommu->dev, DL_FLAG_AUTOREMOVE_SUPPLIER))
+		dev_dbg(dev, "could not add device link\n");
+
 	return &viommu->iommu;
 
 err_free_dev:
Signature · 1 line
2.54.0

Cam Miller

author
#0 lore raw

cover iommu/virtio: fix remove

This series allows for hot-removing a virtio-iommu device via sysfs to succeed, by fixing 2 separate day 1 bugs.

Patch 1 addresses a NULL pointer dereference due to a pair of out-of-order calls. Patch 2 prevents driver unbind via suppress_bind_attrs. Patch 3 unbinds IOMMU endpoint drivers before removing the IOMMU.

Bugs
----

Before patch 1, attempting a removal caused the following kernel crash on x86.

  BUG: kernel NULL pointer dereference, address: 0000000000000030
  --- snip ---
  RIP: 0010:sysfs_remove_link_from_group+0x10/0x40
  --- snip ---
  Call Trace:
   <TASK>
  --- snip ---
   ? sysfs_remove_link_from_group+0x10/0x40
   iommu_deinit_device+0x2a/0xe0
   __iommu_group_remove_device+0xb0/0xe0
   ? __pfx_remove_iommu_group+0x10/0x10
   remove_iommu_group+0x35/0x60
   bus_for_each_dev+0x83/0xd0
   iommu_device_unregister+0x3d/0xb0
   viommu_remove+0x25/0x60
   virtio_dev_remove+0x3c/0x90
  --- snip ---

After resolving this bug with patch 1, a 2nd bug was exposed. Live endpoints remain active after unregistering the IOMMU. All their IOVAs become stale, causing page faults.

The following warning catches this case.

  WARNING: drivers/iommu/iommu.c:743 at __iommu_group_free_device.constprop.0+0xb9/0xe0, CPU#1:
bash/1250
  --- snip ---
  Call Trace:
   <TASK>
   __iommu_group_remove_device+0x95/0xe0
   ? __pfx_remove_iommu_group+0x10/0x10
   remove_iommu_group+0x39/0x60
   bus_for_each_dev+0x85/0xd0
   iommu_device_unregister+0x3d/0xb0
   viommu_remove+0x1d/0x60
  --- snip ---

As a consequence, endpoint drivers attempting IO hang.

igb 0000:01:00.0 enp1s0: NETDEV WATCHDOG: CPU: 5: transmit queue 2 timed out 5120 ms igb 0000:01:00.0 enp1s0: Reset adapter
igb 0000:01:00.0 enp1s0: igb: enp1s0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX/TX
clocksource: Watchdog remote CPU 7 read timed out
igb 0000:01:00.0: Detected Tx Unit Hang

Shutting down the system at this point leads to a kernel panic.

  BUG: unable to handle page fault for address: ffffee9903ffffc0
  --- snip ---
  RIP: 0010:dma_free_contiguous+0x59/0xb0
  --- snip ---
  Call Trace:
   <TASK>
   __igb_close.isra.0+0xb0/0x1c0 [igb]
   __igb_shutdown.isra.0+0x27a/0x310 [igb]
   igb_shutdown+0x2f/0x90 [igb]
   pci_device_shutdown+0x35/0x60
   device_shutdown+0x118/0x1b0
   kernel_power_off+0x35/0x80
   __do_sys_reboot+0x1d4/0x240
   do_syscall_64+0xbe/0x470
   ? vfs_writev+0xe8/0x3c0
   ? do_writev+0x7f/0x110
   ? do_writev+0x7f/0x110
   ? do_syscall_64+0xfa/0x470
   ? do_user_addr_fault+0x2ae/0x7b0
   ? irqentry_exit+0x40/0x730
   ? do_syscall_64+0x37/0x470
   ? __irq_exit_rcu+0x4c/0x110
   entry_SYSCALL_64_after_hwframe+0x76/0x7e
  --- snip ---

Patch 3 resolves this bug using device links. These links ensure that the IOMMU endpoints get unbound before viommu_remove() runs.

Testing
-------

I tested this patch using a libvirt-managed q35 qemu machine (i.e. x86 guest).

I used default-bus-bypass-iommu=true and placed root disk (as well as other devices) on the default bus. This way, access to the root filesystem was not affected by the IOMMU removal.

Libvirt domain XML:

<domain type='kvm'

    xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
    <os><type arch='x86_64' machine='pc-q35-9.2'>hvm</type></os>
    ...
    <!-- second host bridge, not bypassed -->
    <controller type='pci' index='15' model='pcie-expander-bus'>
      <target busNr='32'/>
    </controller>
    <controller type='pci' index='16' model='pcie-root-port'>
      <address type='pci' bus='0x0f' slot='0x00' function='0x0'/>
    </controller>
    <!-- test endpoint, behind the root port -->
    <interface type='user'>
      <model type='igb'/>
      <address type='pci' bus='0x10' slot='0x00' function='0x0'/>
    </interface>
    <iommu model='virtio'>
      <address type='pci' bus='0x00' slot='0x04' function='0x0'/>
    </iommu>
    <qemu:commandline>
      <qemu:arg value='-machine'/>
      <qemu:arg value='default-bus-bypass-iommu=true'/>
    </qemu:commandline>

</domain>

Guest topology (abridged lspci -tv):

-+-[0000:00]-+-00.0 Intel Corporation 82G33/G31/P35/P31 Express DRAM Controller

   |           +-01.0  Red Hat, Inc. QEMU PCIe Expander bridge
   |           +-02.3-[04]----00.0  Red Hat, Inc. Virtio 1.0 block device   <- root disk
   |           +-04.0  Red Hat, Inc. Virtio 1.0 IOMMU
   |           ...

\-[0000:20]---00.0-[21]----00.0 Intel Corporation 82576 Gigabit Network Connection

Device links created:

  # ls /sys/class/devlink/ | grep virtio0
  virtio:virtio0--pci:0000:20:00.0
  virtio:virtio0--pci:0000:21:00.0
  # cat /sys/class/devlink/virtio:virtio0--pci:0000:21:00.0/status
  active

Removal:

  # echo 1 > /sys/bus/pci/devices/0000:00:04.0/remove
  [  146.791872] igb 0000:21:00.0: removed PHC on enp33s0
  [  147.538710] virtio_iommu virtio0: device removed

I also tested removal with DMA in-flight. I removed the IOMMU while running iperf3 from the guest to the host (SLIRP backend).

  # iperf3 -c 10.0.2.2 -t 120 -i 5 &
  ...
  [  5]  10.01-15.01  sec   109 MBytes   183 Mbits/sec    0   68.4 KBytes
  [  5]  15.01-20.01  sec   110 MBytes   184 Mbits/sec    0   68.4 KBytes
  # echo 1 > /sys/bus/pci/devices/0000:00:04.0/remove
  [  5]  30.00-35.01  sec  65.1 MBytes   109 Mbits/sec    1   1.43 KBytes
  [  5]  35.01-40.01  sec  0.00 Bytes  0.00 bits/sec    0   1.43 KBytes
  ...
  # dmesg | tail -n4
  [  234.227669] clocksource: Watchdog remote CPU 15 read timed out
  [  260.920027] igb 0000:21:00.0: removed PHC on enp33s0
  [  261.578453] virtio_iommu virtio0: device removed

The transfer stops because the NIC is unbound, as expected.

Rebinding an endpoint after the removal succeeds.

  # echo 0000:21:00.0 > /sys/bus/pci/drivers/igb/bind
  igb 0000:21:00.0: deferred probe timeout, ignoring dependency
  igb 0000:21:00.0: added PHC on eth0
  igb 0000:21:00.0 enp33s0: igb: enp33s0 NIC Link is Up 1000 Mbps Full Duplex

Rebooting after removal completes cleanly, resolving the oops. After reboot, the links are active again and everything works.

Thank you.

Cam Miller (3):
iommu/virtio: Fix out-of-order teardown in viommu_remove() iommu/virtio: Prevent driver unbind via sysfs iommu/virtio: Add device links for endpoints

 drivers/iommu/virtio-iommu.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
Signature · 1 line
2.54.0
#7 lore raw
Sashiko has reviewed this 3-patch series and found no issues.

Sashiko has reviewed this 3-patch series and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Reviewed-by on v1 1/3
#2 lore raw

Re: [RFC linux v1 1/3] iommu/virtio: Fix out-of-order teardown in viommu_remove()

In reply to Cam Miller
Quoted 43 lines · Cam Miller

Fix bug in viommu_remove() by swapping out-of-order IOMMU unregistration and sysfs removal steps.

Other IOMMU drivers all call iommu_device_unregister() before iommu_device_sysfs_remove(). Before this change, viommu_remove() had incorrectly reversed this ordering, leading to the following kernel oops.

  BUG: kernel NULL pointer dereference, address: 0000000000000030
  ...snip...
  RIP: 0010:sysfs_remove_link_from_group+0x10/0x40
  ...snip..
  Call Trace:
   <TASK>
  ...snip...
   ? sysfs_remove_link_from_group+0x10/0x40
   iommu_deinit_device+0x2a/0xe0
   __iommu_group_remove_device+0xb0/0xe0
   ? __pfx_remove_iommu_group+0x10/0x10
   remove_iommu_group+0x35/0x60
   bus_for_each_dev+0x83/0xd0
   iommu_device_unregister+0x3d/0xb0
   viommu_remove+0x25/0x60
   virtio_dev_remove+0x3c/0x90
  ...snip...

iommu_device_sysfs_remove() will set iommu->dev = NULL after tearing down the corresponding sysfs entry. Then iommu_device_unregister() will iterate over every endpoint device attached to the IOMMU and reference the null iommu->dev pointer, specifically via call sysfs_remove_link_from_group(&iommu->dev->kobj, ...).

Fixes edcd69ab9a32 ("iommu: Add virtio-iommu driver")
---
 drivers/iommu/virtio-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/virtio-iommu.c +0 -0
index 587fc13197f1..a08474eeed58 100644

drivers/iommu/virtio-iommu.c static void viommu_remove(struct virtio_device *vdev) · line 1261

Quoted 9 lines
drivers/iommu/virtio-iommu.c +1 -1
@@ -1254,8 +1254,8 @@ static void viommu_remove(struct virtio_device *vdev)
 {
 	struct viommu_dev *viommu = vdev->priv;
 
-	iommu_device_sysfs_remove(&viommu->iommu);
 	iommu_device_unregister(&viommu->iommu);
+	iommu_device_sysfs_remove(&viommu->iommu);
 
 	/* Stop all virtqueues */
 	virtio_reset_device(vdev);

Looks good to me. Thanks!

Reviewed-by on v1 2/3
#4 lore raw

Re: [RFC linux v1 2/3] iommu/virtio: Prevent driver unbind via sysfs

In reply to Cam Miller
Quoted 22 lines · Cam Miller

Set suppress_bind_attrs on the virtio-iommu driver to prevent userspace from triggering driver unbind via sysfs.

Unbinding the IOMMU driver from beneath live endpoint devices would leave those devices with dangling domain pointers. This would not be a recoverable operation.

Note that suppress_bind_attrs covers only the driver bind/unbind path, not PCI-level device removal triggered via the sysfs remove file.

Fixes edcd69ab9a32 ("iommu: Add virtio-iommu driver")
---
 drivers/iommu/virtio-iommu.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iommu/virtio-iommu.c +0 -0
index a08474eeed58..dd10c3e98a3d 100644
@@ -1285,7 +1285,10 @@ static struct virtio_device_id id_table[] = {
 MODULE_DEVICE_TABLE(virtio, id_table);

drivers/iommu/virtio-iommu.c static struct virtio_device_id id_table[] = { · line 1294

Quoted 10 lines
drivers/iommu/virtio-iommu.c +4 -1
 
 static struct virtio_driver virtio_iommu_drv = {
-	.driver.name		= KBUILD_MODNAME,
+	.driver		= {
+		.name		= KBUILD_MODNAME,
+		.suppress_bind_attrs     = true,
+	},
 	.id_table		= id_table,
 	.feature_table		= features,
 	.feature_table_size	= ARRAY_SIZE(features),

Makes sense to me and this is also in line with other IOMMU drivers like arm-smmu-v3 that use the same attribute.

Thanks,
Niklas

Reviewed-by on v1 3/3
#6 lore raw

Re: [RFC linux v1 3/3] iommu/virtio: Add device links for endpoints

In reply to Cam Miller
Quoted 26 lines · Cam Miller

Make each endpoint a consumer of the virtio-iommu device, so that the driver core unbinds each endpoint driver before viommu_remove() runs.

Before this change, removing a virtio-iommu (for example via "echo 1 > /sys/bus/pci/devices/<dev>/remove") tears down the IOMMU groups of all its endpoints while their drivers are still bound. The endpoints keep stale IOVAs and their later dma_free_contiguous() leads to WARNs in __iommu_group_free_device(), endpoint IO hanging, and crash at shutdown.

Make each endpoint a consumer of the viommu with device_link_add(), as arm-smmu and other IOMMU drivers do. The driver core then unbinds the endpoint drivers before viommu_remove() runs, so that their DMA teardown will still see an intact domain.

Fixes edcd69ab9a32 ("iommu: Add virtio-iommu driver")
---
 drivers/iommu/virtio-iommu.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iommu/virtio-iommu.c +0 -0
index dd10c3e98a3d..a84a653f269e 100644

drivers/iommu/virtio-iommu.c static struct iommu_device *viommu_probe_device(struct device *dev) · line 1052

Quoted 9 lines
drivers/iommu/virtio-iommu.c +3 -0
@@ -1044,6 +1044,9 @@ static struct iommu_device *viommu_probe_device(struct device *dev)
 			goto err_free_dev;
 	}
 
+	if (!device_link_add(dev, viommu->dev, DL_FLAG_AUTOREMOVE_SUPPLIER))
+		dev_dbg(dev, "could not add device link\n");
+
 	return &viommu->iommu;
 
 err_free_dev:

Looks good to me and like the unbind suppression is also already used by other IOMMU drivers in this case e.g. arm-smmu in a similar fashion.

Thanks,
Niklas