Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

virtio: break and reset virtio devices on device_shutdown()

Hongyu reported a hang on kexec in a VM. QEMU reported invalid memory
accesses during the hang.

Invalid read at addr 0x102877002, size 2, region '(null)', reason: rejected
Invalid write at addr 0x102877A44, size 2, region '(null)', reason: rejected
...

It was traced down to virtio-console. Kexec works fine if virtio-console
is not in use.

The issue is that virtio-console continues to write to the MMIO even after
underlying virtio-pci device is reset.

Additionally, Eric noticed that IOMMUs are reset before devices, if
devices are not reset on shutdown they continue to poke at guest memory
and get errors from the IOMMU. Some devices get wedged then.

The problem can be solved by breaking all virtio devices on virtio
bus shutdown, then resetting them.

Reported-by: Eric Auger <eauger@redhat.com>
Reported-by: Hongyu Ning <hongyu.ning@linux.intel.com>
Message-ID: <c1dbc7dbad9b445245d3348f19e6742b0be07347.1740094946.git.mst@redhat.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

+29
+29
drivers/virtio/virtio.c
··· 395 395 return dev->config->get_vq_affinity(dev, irq_vec); 396 396 } 397 397 398 + static void virtio_dev_shutdown(struct device *_d) 399 + { 400 + struct virtio_device *dev = dev_to_virtio(_d); 401 + struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); 402 + 403 + /* 404 + * Stop accesses to or from the device. 405 + * We only need to do it if there's a driver - no accesses otherwise. 406 + */ 407 + if (!drv) 408 + return; 409 + 410 + /* 411 + * Some devices get wedged if you kick them after they are 412 + * reset. Mark all vqs as broken to make sure we don't. 413 + */ 414 + virtio_break_device(dev); 415 + /* 416 + * Guarantee that any callback will see vq->broken as true. 417 + */ 418 + virtio_synchronize_cbs(dev); 419 + /* 420 + * As IOMMUs are reset on shutdown, this will block device access to memory. 421 + * Some devices get wedged if this happens, so reset to make sure it does not. 422 + */ 423 + dev->config->reset(dev); 424 + } 425 + 398 426 static const struct bus_type virtio_bus = { 399 427 .name = "virtio", 400 428 .match = virtio_dev_match, ··· 431 403 .probe = virtio_dev_probe, 432 404 .remove = virtio_dev_remove, 433 405 .irq_get_affinity = virtio_irq_get_affinity, 406 + .shutdown = virtio_dev_shutdown, 434 407 }; 435 408 436 409 int __register_virtio_driver(struct virtio_driver *driver, struct module *owner)