* [PATCH v2 1/3] virtio: document virtio_reset_device
[not found] <20220114215641.239984-1-mst@redhat.com>
@ 2022-01-14 21:57 ` Michael S. Tsirkin
2022-01-14 21:57 ` [PATCH v2 2/3] virtio_console: break out of buf poll on remove Michael S. Tsirkin
2022-01-14 21:57 ` [PATCH v2 3/3] virtio_mem: break device " Michael S. Tsirkin
2 siblings, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2022-01-14 21:57 UTC (permalink / raw)
To: linux-kernel; +Cc: virtualization
Looks like most callers get driver/device removal wrong.
Document what's expected of callers.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
drivers/virtio/virtio.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 2ed6e2451fd8..631a346a3aa6 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -201,6 +201,22 @@ static int virtio_finalize_features(struct virtio_device *dev)
return 0;
}
+/**
+ * virtio_reset_device - quiesce device for removal
+ * @dev: the device to reset
+ *
+ * Prevents device from sending interrupts and accessing memory.
+ *
+ * Generally used for cleanup during driver / device removal.
+ *
+ * Once this has been invoked, caller must ensure that
+ * virtqueue_notify / virtqueue_kick are not in progress.
+ *
+ * Note: this guarantees that vq callbacks are not in progress, however caller
+ * is responsible for preventing access from other contexts, such as a system
+ * call/workqueue/bh. Invoking virtio_break_device then flushing any such
+ * contexts is one way to handle that.
+ * */
void virtio_reset_device(struct virtio_device *dev)
{
dev->config->reset(dev);
--
MST
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 2/3] virtio_console: break out of buf poll on remove
[not found] <20220114215641.239984-1-mst@redhat.com>
2022-01-14 21:57 ` [PATCH v2 1/3] virtio: document virtio_reset_device Michael S. Tsirkin
@ 2022-01-14 21:57 ` Michael S. Tsirkin
2022-01-14 21:57 ` [PATCH v2 3/3] virtio_mem: break device " Michael S. Tsirkin
2 siblings, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2022-01-14 21:57 UTC (permalink / raw)
To: linux-kernel; +Cc: Greg Kroah-Hartman, virtualization, Arnd Bergmann, Amit Shah
A common pattern for device reset is currently:
vdev->config->reset(vdev);
.. cleanup ..
reset prevents new interrupts from arriving and waits for interrupt
handlers to finish.
However if - as is common - the handler queues a work request which is
flushed during the cleanup stage, we have code adding buffers / trying
to get buffers while device is reset. Not good.
This was reproduced by running
modprobe virtio_console
modprobe -r virtio_console
in a loop.
Fix this up by calling virtio_break_device + flush before reset.
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1786239
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
drivers/char/virtio_console.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index 2359889a35a0..e3c430539a17 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -1957,6 +1957,13 @@ static void virtcons_remove(struct virtio_device *vdev)
list_del(&portdev->list);
spin_unlock_irq(&pdrvdata_lock);
+ /* Device is going away, exit any polling for buffers */
+ virtio_break_device(vdev);
+ if (use_multiport(portdev))
+ flush_work(&portdev->control_work);
+ else
+ flush_work(&portdev->config_work);
+
/* Disable interrupts for vqs */
virtio_reset_device(vdev);
/* Finish up work that's lined up */
--
MST
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 3/3] virtio_mem: break device on remove
[not found] <20220114215641.239984-1-mst@redhat.com>
2022-01-14 21:57 ` [PATCH v2 1/3] virtio: document virtio_reset_device Michael S. Tsirkin
2022-01-14 21:57 ` [PATCH v2 2/3] virtio_console: break out of buf poll on remove Michael S. Tsirkin
@ 2022-01-14 21:57 ` Michael S. Tsirkin
2 siblings, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2022-01-14 21:57 UTC (permalink / raw)
To: linux-kernel; +Cc: virtualization
A common pattern for device reset is currently:
vdev->config->reset(vdev);
.. cleanup ..
reset prevents new interrupts from arriving and waits for interrupt
handlers to finish.
However if - as is common - the handler queues a work request which is
flushed during the cleanup stage, we have code adding buffers / trying
to get buffers while device is reset. Not good.
This was reproduced by running
modprobe virtio_console
modprobe -r virtio_console
in a loop, and this reasoning seems to apply to virtio mem though
I could not reproduce it there.
Fix this up by calling virtio_break_device + flush before reset.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
drivers/virtio/virtio_mem.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 38becd8d578c..33b8a118a3ae 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -2888,6 +2888,8 @@ static void virtio_mem_remove(struct virtio_device *vdev)
virtio_mem_deinit_hotplug(vm);
/* reset the device and cleanup the queues */
+ virtio_break_device(vdev);
+ flush_work(&vm->wq);
virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
--
MST
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-01-14 21:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20220114215641.239984-1-mst@redhat.com>
2022-01-14 21:57 ` [PATCH v2 1/3] virtio: document virtio_reset_device Michael S. Tsirkin
2022-01-14 21:57 ` [PATCH v2 2/3] virtio_console: break out of buf poll on remove Michael S. Tsirkin
2022-01-14 21:57 ` [PATCH v2 3/3] virtio_mem: break device " Michael S. Tsirkin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).