mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peng Fan <peng.fan@linux.dev>
To: "Michael S. Tsirkin" <mst@redhat.com>,
	"Jason Wang" <jasowangio@gmail.com>,
	"Eugenio Pérez" <eperezma@redhat.com>,
	"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>
Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org,
	 Peng Fan <peng.fan@nxp.com>
Subject: [PATCH] virtio: reset the device when probe fails
Date: Sat, 05 Dec 2026 18:27:26 +0800	[thread overview]
Message-ID: <20261205-virtio-reset-v1-1-c2203390b005@nxp.com> (raw)

From: Peng Fan <peng.fan@nxp.com>

A driver probe can fail after the transport has already enabled the
virtqueues. virtio-pci only clears queue_enable on a device reset, and the
modern delete path does not clear it, so a failed probe leaves the queues
enabled. Because register_virtio_device()'s reset is not repeated on a
later sysfs bind, setup_vq() then rejects the still-enabled queue and the
device can never be bound again.

Reset the device in virtio_dev_probe()'s error path before marking it
FAILED, so a failed probe leaves the device in a clean state for rebind.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
A driver probe can fail after the transport has already enabled the
virtqueues. virtio-pci only clears queue_enable on a device reset, and the
modern delete path does not clear it, so a failed probe leaves the queues
enabled. Because register_virtio_device()'s reset is not repeated on a
later sysfs bind, setup_vq() then rejects the still-enabled queue and the
device can never be bound again.

Reset the device in virtio_dev_probe()'s error path before marking it
FAILED, so a failed probe leaves the device in a clean state for rebind.

My test code as below, two parts: a test ko, and a userspace program.

---------------Driver-------------------------
// SPDX-License-Identifier: GPL-2.0

static bool fail_first = true;

static int vpt_probe(struct virtio_device *vdev)
{
	struct virtqueue_info info = {
		.name = "vptest",
		.callback = NULL,
	};
	struct virtqueue *vq;
	int ret;

	ret = virtio_find_vqs(vdev, 1, &vq, &info, NULL);
	if (ret) {
		pr_err("VTTEST find_vqs failed: %d\n", ret);
		return ret;
	}

	virtio_device_ready(vdev);

	if (fail_first) {
		fail_first = false;
		pr_info("VTTEST injected probe failure after device_ready\n");
		vdev->config->del_vqs(vdev);
		return -EIO;
	}

	pr_info("VTTEST probe succeeded\n");
	return 0;
}

static void vpt_remove(struct virtio_device *vdev)
{
	vdev->config->del_vqs(vdev);
}

static const struct virtio_device_id vpt_id_table[] = {
	{ VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
	{ 0 },
};
MODULE_DEVICE_TABLE(virtio, vpt_id_table);

static struct virtio_driver vpt_driver = {
	.driver.name = "virtio_probe_test",
	.id_table = vpt_id_table,
	.probe = vpt_probe,
	.remove = vpt_remove,
};
module_virtio_driver(vpt_driver);

MODULE_DESCRIPTION("virtio probe-failure rebind test");
MODULE_LICENSE("GPL");

-----------------Userspace code snippet------------------------
static int scenario_probe_rebind(void)
{
	char dev[128];
	int ret;

	if (find_virtio("virtio_input", dev, sizeof(dev))) {
		vlog("VTTEST probe_rebind SKIP: no bound virtio_input\n");
		return 0;
	}
	vlog("VTTEST probe_rebind device=%s\n", dev);

	ret = writef("/sys/bus/virtio/drivers/virtio_input/unbind", "%s\n", dev);
	vlog("VTTEST probe_rebind unbind rc=%d\n", ret);
	usleep(200000);

	ret = insmod_file("/test/virtio_probe_test.ko");
	vlog("VTTEST probe_rebind insmod rc=%d\n", ret);
	usleep(500000);

	if (has_driver(dev, "virtio_probe_test")) {
		vlog("VTTEST probe_rebind FAIL: first probe unexpectedly bound\n");
		return 0;
	}

	ret = writef("/sys/bus/virtio/drivers/virtio_probe_test/bind",
		     "%s\n", dev);
	vlog("VTTEST probe_rebind rebind rc=%d\n", ret);
	usleep(500000);

	if (has_driver(dev, "virtio_probe_test"))
		vlog("VTTEST probe_rebind PASS\n");
	else
		vlog("VTTEST probe_rebind FAIL: rebind did not bind\n");

	return 0;
}
---
 drivers/virtio/virtio.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index c8be1a3f479d..bf3ae7210456 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -362,6 +362,12 @@ static int virtio_dev_probe(struct device *_d)
 	return 0;
 
 err:
+	/*
+	 * Reset the device so a failed probe does not leave enabled queues
+	 * behind (virtio-pci queue_enable can only be cleared by reset), which
+	 * would otherwise make a later rebind fail in setup_vq().
+	 */
+	virtio_reset_device(dev);
 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
 	return err;
 

---
base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
change-id: 20261205-virtio-reset-20cdadf66a6f

Best regards,
-- 
Peng Fan <peng.fan@nxp.com>


             reply	other threads:[~2026-12-05 10:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 10:27 Peng Fan [this message]
2026-12-05 12:35 ` Michael S. Tsirkin
2026-12-06 11:02   ` Peng Fan
2026-12-06 11:33     ` Michael S. Tsirkin
2026-12-06 12:35       ` Peng Fan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205-virtio-reset-v1-1-c2203390b005@nxp.com \
    --to=peng.fan@linux.dev \
    --cc=eperezma@redhat.com \
    --cc=jasowangio@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=peng.fan@nxp.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®