Skip to content

Commit 9f90dd4

Browse files
committed
Merge: virtio: break and reset virtio devices on device_shutdown()
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/6675 JIRA: https://issues.redhat.com/browse/RHEL-331 UPSTREAM: yes When vIOMMU is 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. Signed-off-by: Eric Auger <eric.auger@redhat.com> v2 -> v3: - added "virtgpu: don't reset on shutdown" fixing the regression observed with virtio-gpu v1 -> v2: - forced to respin because commit "virtio: hookup irq_get_affinity callback" has landed and caused conflict with v1 Approved-by: José Expósito <jexposit@redhat.com> Approved-by: Laurent Vivier <lvivier@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Augusto Caringi <acaringi@redhat.com>
2 parents 3ccda4a + d347784 commit 9f90dd4

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

drivers/gpu/drm/virtio/virtgpu_drv.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ static void virtio_gpu_remove(struct virtio_device *vdev)
123123
drm_dev_put(dev);
124124
}
125125

126+
static void virtio_gpu_shutdown(struct virtio_device *vdev)
127+
{
128+
/*
129+
* drm does its own synchronization on shutdown.
130+
* Do nothing here, opt out of device reset.
131+
*/
132+
}
133+
126134
static void virtio_gpu_config_changed(struct virtio_device *vdev)
127135
{
128136
struct drm_device *dev = vdev->priv;
@@ -157,6 +165,7 @@ static struct virtio_driver virtio_gpu_driver = {
157165
.id_table = id_table,
158166
.probe = virtio_gpu_probe,
159167
.remove = virtio_gpu_remove,
168+
.shutdown = virtio_gpu_shutdown,
160169
.config_changed = virtio_gpu_config_changed
161170
};
162171

drivers/virtio/virtio.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,40 @@ static const struct cpumask *virtio_irq_get_affinity(struct device *_d,
347347
return dev->config->get_vq_affinity(dev, irq_vec);
348348
}
349349

350+
static void virtio_dev_shutdown(struct device *_d)
351+
{
352+
struct virtio_device *dev = dev_to_virtio(_d);
353+
struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
354+
355+
/*
356+
* Stop accesses to or from the device.
357+
* We only need to do it if there's a driver - no accesses otherwise.
358+
*/
359+
if (!drv)
360+
return;
361+
362+
/* If the driver has its own shutdown method, use that. */
363+
if (drv->shutdown) {
364+
drv->shutdown(dev);
365+
return;
366+
}
367+
368+
/*
369+
* Some devices get wedged if you kick them after they are
370+
* reset. Mark all vqs as broken to make sure we don't.
371+
*/
372+
virtio_break_device(dev);
373+
/*
374+
* Guarantee that any callback will see vq->broken as true.
375+
*/
376+
virtio_synchronize_cbs(dev);
377+
/*
378+
* As IOMMUs are reset on shutdown, this will block device access to memory.
379+
* Some devices get wedged if this happens, so reset to make sure it does not.
380+
*/
381+
dev->config->reset(dev);
382+
}
383+
350384
static struct bus_type virtio_bus = {
351385
.name = "virtio",
352386
.match = virtio_dev_match,
@@ -355,6 +389,7 @@ static struct bus_type virtio_bus = {
355389
.probe = virtio_dev_probe,
356390
.remove = virtio_dev_remove,
357391
.irq_get_affinity = virtio_irq_get_affinity,
392+
.shutdown = virtio_dev_shutdown,
358393
};
359394

360395
int register_virtio_driver(struct virtio_driver *driver)

include/linux/virtio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ size_t virtio_max_dma_size(const struct virtio_device *vdev);
178178
* changes; may be called in interrupt context.
179179
* @freeze: optional function to call during suspend/hibernation.
180180
* @restore: optional function to call on resume.
181+
* @shutdown: synchronize with the device on shutdown. If provided, replaces
182+
* the virtio core implementation.
181183
*/
182184
struct virtio_driver {
183185
struct device_driver driver;
@@ -195,6 +197,7 @@ struct virtio_driver {
195197
int (*freeze)(struct virtio_device *dev);
196198
int (*restore)(struct virtio_device *dev);
197199
#endif
200+
void (*shutdown)(struct virtio_device *dev);
198201
};
199202

200203
static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)

0 commit comments

Comments
 (0)