Linux virtualization list
 help / color / mirror / Atom feed
* [PATCH] spi: virtio: move freeze/restore callbacks to virtio_driver
@ 2026-11-29 20:22 Francesco Valla
  2026-11-29 20:27 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Francesco Valla @ 2026-11-29 20:22 UTC (permalink / raw)
  To: Mark Brown; +Cc: virtualization, linux-spi, stable, Francesco Valla

A virtio driver needs to route its freeze and restore callbacks through
the virtio framework rather than generic pm, or the restore callbacks
from its parent (that runs before the driver's) will mark the driver
ready before it can re-create its virtqueues, leading to a dead
communication and violating the virtio specification. At driver side,
this also leads to a hang at first SPI transaction after resume.

Move the freeze/restore operations to the corresponding virtio_driver
callbacks, so that the virtio core takes care of them. This aligns
the SPI driver to what other virtio drivers are doing.

In the restore callback, call virtio_device_ready() before resuming the
SPI controller; in this way, the device is correctly notified of the
readiness before any request can arrive from the driver.

Behavior reproduced and fix tested on QEMU.

Fixes: f98cabe3f6cf ("SPI: Add virtio SPI driver")
Cc: stable@vger.kernel.org
Signed-off-by: Francesco Valla <francesco@valla.it>
---
 drivers/spi/spi-virtio.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/drivers/spi/spi-virtio.c b/drivers/spi/spi-virtio.c
index 2eeb79ff5aa2..c163cfb74910 100644
--- a/drivers/spi/spi-virtio.c
+++ b/drivers/spi/spi-virtio.c
@@ -365,37 +365,38 @@ static void virtio_spi_remove(struct virtio_device *vdev)
 	virtio_spi_del_vq(vdev);
 }
 
-static int virtio_spi_freeze(struct device *dev)
+static int virtio_spi_freeze(struct virtio_device *vdev)
 {
-	struct spi_controller *ctrl = dev_get_drvdata(dev);
-	struct virtio_device *vdev = dev_to_virtio(dev);
+	struct spi_controller *ctrl = dev_get_drvdata(&vdev->dev);
 	int ret;
 
 	ret = spi_controller_suspend(ctrl);
 	if (ret) {
-		dev_warn(dev, "cannot suspend controller (%d)\n", ret);
+		dev_warn(&vdev->dev, "cannot suspend controller (%d)\n", ret);
 		return ret;
 	}
 
 	virtio_spi_del_vq(vdev);
+
 	return 0;
 }
 
-static int virtio_spi_restore(struct device *dev)
+static int virtio_spi_restore(struct virtio_device *vdev)
 {
-	struct spi_controller *ctrl = dev_get_drvdata(dev);
-	struct virtio_device *vdev = dev_to_virtio(dev);
+	struct spi_controller *ctrl = dev_get_drvdata(&vdev->dev);
 	int ret;
 
 	ret = virtio_spi_find_vqs(vdev->priv);
 	if (ret) {
-		dev_err(dev, "problem starting vqueue (%d)\n", ret);
+		dev_err(&vdev->dev, "problem starting vqueue (%d)\n", ret);
 		return ret;
 	}
 
+	virtio_device_ready(vdev);
+
 	ret = spi_controller_resume(ctrl);
 	if (ret)
-		dev_err(dev, "problem resuming controller (%d)\n", ret);
+		dev_err(&vdev->dev, "problem resuming controller (%d)\n", ret);
 
 	return ret;
 }
@@ -406,19 +407,15 @@ static struct virtio_device_id virtio_spi_id_table[] = {
 };
 MODULE_DEVICE_TABLE(virtio, virtio_spi_id_table);
 
-static const struct dev_pm_ops virtio_spi_pm_ops = {
-	.freeze = pm_sleep_ptr(virtio_spi_freeze),
-	.restore = pm_sleep_ptr(virtio_spi_restore),
-};
-
 static struct virtio_driver virtio_spi_driver = {
 	.driver = {
 		.name = KBUILD_MODNAME,
-		.pm = &virtio_spi_pm_ops,
 	},
 	.id_table = virtio_spi_id_table,
 	.probe = virtio_spi_probe,
 	.remove = virtio_spi_remove,
+	.freeze = pm_sleep_ptr(virtio_spi_freeze),
+	.restore = pm_sleep_ptr(virtio_spi_restore),
 };
 module_virtio_driver(virtio_spi_driver);
 

---
base-commit: ca20d5a011d35fc25e328fe31ac47f7aae557c5a
change-id: 20261129-spi-virtio-restore-fix-5d457779bb30

Best regards,
--  
Francesco Valla <francesco@valla.it>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-29 20:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 20:22 [PATCH] spi: virtio: move freeze/restore callbacks to virtio_driver Francesco Valla
2026-11-29 20:27 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox