Skip to content

RFC iommu/virtio: fix remove

Cam Miller linux-iommu lkml virtualization 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest3 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3iommu/virtio: Fix out-of-order teardown in viommu_remove()drivers/iommu/virtio-iommu.c+1 -10 replies
2/3iommu/virtio: Prevent driver unbind via sysfsdrivers/iommu/virtio-iommu.c+4 -10 replies
3/3iommu/virtio: Add device links for endpointsdrivers/iommu/virtio-iommu.c+3 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC linux v1 0/3] 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