Skip to content

Commit aee42f3

Browse files
mstsirkingregkh
authored andcommitted
virtio: break and reset virtio devices on device_shutdown()
[ Upstream commit 8bd2fa0 ] 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 <[email protected]> Reported-by: Hongyu Ning <[email protected]> Message-ID: <c1dbc7dbad9b445245d3348f19e6742b0be07347.1740094946.git.mst@redhat.com> Tested-by: Eric Auger <[email protected]> Acked-by: Jason Wang <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 2acbf48 commit aee42f3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

drivers/virtio/virtio.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,34 @@ static const struct cpumask *virtio_irq_get_affinity(struct device *_d,
395395
return dev->config->get_vq_affinity(dev, irq_vec);
396396
}
397397

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+
398426
static const struct bus_type virtio_bus = {
399427
.name = "virtio",
400428
.match = virtio_dev_match,
@@ -403,6 +431,7 @@ static const struct bus_type virtio_bus = {
403431
.probe = virtio_dev_probe,
404432
.remove = virtio_dev_remove,
405433
.irq_get_affinity = virtio_irq_get_affinity,
434+
.shutdown = virtio_dev_shutdown,
406435
};
407436

408437
int __register_virtio_driver(struct virtio_driver *driver, struct module *owner)

0 commit comments

Comments
 (0)