Subject: [RFC PATCH v9 1/5] virtio-mmio: move guest page size setting into vm_reset()

From: Sungho Bae <[email-masked]>
Date: Sat, 16 May 2026 10:57:52 +0900

From: Sungho Bae <[email-masked]>

The virtio-mmio legacy spec (Section 4.2.4) requires the driver to
write the guest page size "during initialization, before any queues
are used". Reset is step 1 of the initialization sequence
(Section 3.1), so setting GuestPageSize immediately after the status
register reset in vm_reset() is more proper.

Currently the GuestPageSize write lives in two separate call sites:

 - virtio_mmio_probe(), before register_virtio_device()
 - virtio_mmio_restore(), before virtio_device_restore()

Both of these write the value *before* the reset that happens inside
register_virtio_device()/virtio_device_restore(), so a device
implementation that clears GuestPageSize on reset would lose the
value. QEMU's virtio_mmio_reset() for example zeroes guest_page_shift
on a full device reset.

The current code happens to work because the Linux driver triggers
only a "soft reset" (STATUS register write of 0), and QEMU's
soft-reset path does not clear guest_page_shift. But relying on
this is fragile and not guaranteed by the spec.

Move the GuestPageSize write into vm_reset(), right after the status
reset. This ensures the value is set:

 - at the correct point in the initialization sequence per spec,
 - after every reset (probe, restore, or any future path), and
 - exactly once, removing the duplication.

Fixes: e0c2ce821795 ("virtio_mmio: Restore guest page size on resume")
Signed-off-by: Sungho Bae <[email-masked]>
---
 drivers/virtio/virtio_mmio.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c
index 595c2274fbb5..daa65b269a36 100644
--- a/drivers/virtio/virtio_mmio.c
+++ b/drivers/virtio/virtio_mmio.c
@@ -254,6 +254,16 @@ static void vm_reset(struct virtio_device *vdev)
 
 	/* 0 status means a reset. */
 	writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
+
+	/*
+	 * The virtio-mmio legacy spec requires the driver to write the
+	 * guest page size during initialization, before any queues are
+	 * used.  Since reset is step 1 of initialization (Section 3.1),
+	 * set it here so it is always in place for subsequent queue setup
+	 * in every code path (probe, restore, etc.).
+	 */
+	if (vm_dev->version == 1)
+		writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
 }

@@ -547,9 +557,6 @@ static int virtio_mmio_restore(struct device *dev)
 {
 	struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev);
 
-	if (vm_dev->version == 1)
-		writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
-
 	return virtio_device_restore(&vm_dev->vdev);
 }
 
@@ -619,8 +626,6 @@ static int virtio_mmio_probe(struct platform_device *pdev)
 	vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
 
 	if (vm_dev->version == 1) {
-		writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
-
 		rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
 		/*
 		 * In the legacy case, ensure our coherently-allocated virtio
-- 
2.34.1