* [PATCH] virtio: reset the device when probe fails
@ 2026-12-05 10:27 Peng Fan
2026-12-05 12:35 ` Michael S. Tsirkin
0 siblings, 1 reply; 4+ messages in thread
From: Peng Fan @ 2026-12-05 10:27 UTC (permalink / raw)
To: Michael S. Tsirkin, Jason Wang, Eugenio Pérez, Xuan Zhuo
Cc: virtualization, linux-kernel, Peng Fan
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>
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] virtio: reset the device when probe fails
2026-12-05 10:27 [PATCH] virtio: reset the device when probe fails Peng Fan
@ 2026-12-05 12:35 ` Michael S. Tsirkin
2026-12-06 11:02 ` Peng Fan
0 siblings, 1 reply; 4+ messages in thread
From: Michael S. Tsirkin @ 2026-12-05 12:35 UTC (permalink / raw)
To: Peng Fan
Cc: Jason Wang, Eugenio Pérez, Xuan Zhuo, virtualization,
linux-kernel, Peng Fan
On Sat, Dec 05, 2026 at 06:27:26PM +0800, Peng Fan wrote:
> 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,
what is a 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.
why is this repeated?
>
> 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);
This is the bug, is it not? If you call virtio_device_ready you must
reset?
>
> 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>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] virtio: reset the device when probe fails
2026-12-05 12:35 ` Michael S. Tsirkin
@ 2026-12-06 11:02 ` Peng Fan
2026-12-06 11:33 ` Michael S. Tsirkin
0 siblings, 1 reply; 4+ messages in thread
From: Peng Fan @ 2026-12-06 11:02 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: Jason Wang, Eugenio Pérez, Xuan Zhuo, virtualization,
linux-kernel, Peng Fan
On Sat, Dec 05, 2026 at 07:35:04AM -0500, Michael S. Tsirkin wrote:
>On Sat, Dec 05, 2026 at 06:27:26PM +0800, Peng Fan wrote:
>> 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,
>
>what is a sysfs bind?
It is something below:
ret = writef("/sys/bus/virtio/drivers/virtio_probe_test/bind", "%s\n", dev);
>
>> 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.
>
>why is this repeated?
Hope I not missunderstand. If you are asking a repeated reset, the reset in
register_virtio_device() happens only when the virtio device is first registered:
/* in register_virtio_device() */
virtio_reset_device(dev);
...
device_add(&dev->dev); /* triggers the initial probe */
A later sysfs bind does not re-run register_virtio_device(); it goes through
driver_probe_device() -> virtio_dev_probe() only. So if the first probe failed
after virtio_find_vqs() left queue_enable set, the next bind has no reset to
clear it.
The added reset is only in virtio_dev_probe()’s error path, so it is not on
the normal bind path and not repeated per bind - it runs once when a probe
fails. Successful probes are unchanged.
Maybe the commit message wording is confusing.
BTW: Sashiko reported an issue in current patch, I think I may need to update
patch as below(not tested):
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -274,6 +274,21 @@ static int virtio_dev_probe(struct device *_d)
u64 device_features;
u64 driver_features;
u64 driver_features_legacy;
+ unsigned int status;
+
+ /*
+ * Driver initialisation must start from a freshly reset, acknowledged
+ * device. A previous probe may have failed and
+ * left VIRTIO_CONFIG_S_FAILED set: FAILED is sticky, only a reset
+ * clears it. The device may also have been unbound without being
+ * reset. Start over in both cases -- the reset additionally clears
+ * any queue_enable left behind by the failed probe.
+ */
+ status = dev->config->get_status(dev);
+ if (status != VIRTIO_CONFIG_S_ACKNOWLEDGE) {
+ virtio_reset_device(dev);
+ virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
+ }
/* We have a driver! */
virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
@@ -362,6 +377,18 @@ 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);
+ /*
+ * Status is 0 after the reset: re-acknowledge before flagging the
+ * failure, otherwise the status field jumps straight from 0 to
+ * FAILED and the next probe has to build on an unacknowledged,
+ * failed device. Same order as virtio_device_restore_priv().
+ */
+ virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
return err;
}
>
>>
>> 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);
>
>This is the bug, is it not? If you call virtio_device_ready you must
>reset?
ah, right. That test hook was only a minimal trigger for the core error path,
not a model of correct driver behavior, so I've dropped it.
A real path in virtio_input:
err = virtinput_init_vqs(vi); /* find_vqs(): queue_enable = 1 */
...
vi->idev = input_allocate_device(); /* can fail */
if (!vi->idev) {
err = -ENOMEM;
goto err_input_alloc;
}
...
err_input_alloc:
vdev->config->del_vqs(vdev); /* no reset */
On modern virtio-pci, del_vqs() does not clear queue_enable, so a later bind
fails in setup_vq().
I replaced my test with a failslab reproducer that fails
input_allocate_device() in the real virtio_input driver.
Hope it is clear now.
Thanks
Peng
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] virtio: reset the device when probe fails
2026-12-06 11:02 ` Peng Fan
@ 2026-12-06 11:33 ` Michael S. Tsirkin
0 siblings, 0 replies; 4+ messages in thread
From: Michael S. Tsirkin @ 2026-12-06 11:33 UTC (permalink / raw)
To: Peng Fan
Cc: Jason Wang, Eugenio Pérez, Xuan Zhuo, virtualization,
linux-kernel, Peng Fan
On Sun, Dec 06, 2026 at 07:02:52PM +0800, Peng Fan wrote:
> On Sat, Dec 05, 2026 at 07:35:04AM -0500, Michael S. Tsirkin wrote:
> >On Sat, Dec 05, 2026 at 06:27:26PM +0800, Peng Fan wrote:
> >> 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,
> >
> >what is a sysfs bind?
>
> It is something below:
> ret = writef("/sys/bus/virtio/drivers/virtio_probe_test/bind", "%s\n", dev);
why are you trying to do this?
> >
> >> 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.
> >
> >why is this repeated?
>
> Hope I not missunderstand. If you are asking a repeated reset,
I am asking why did you repeat the same message twice in the commit log.
> the reset in
> register_virtio_device() happens only when the virtio device is first registered:
> /* in register_virtio_device() */
> virtio_reset_device(dev);
> ...
> device_add(&dev->dev); /* triggers the initial probe */
>
> A later sysfs bind does not re-run register_virtio_device(); it goes through
> driver_probe_device() -> virtio_dev_probe() only. So if the first probe failed
> after virtio_find_vqs() left queue_enable set, the next bind has no reset to
> clear it.
>
> The added reset is only in virtio_dev_probe()’s error path, so it is not on
> the normal bind path and not repeated per bind - it runs once when a probe
> fails. Successful probes are unchanged.
>
> Maybe the commit message wording is confusing.
>
> BTW: Sashiko reported an issue in current patch, I think I may need to update
> patch as below(not tested):
>
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -274,6 +274,21 @@ static int virtio_dev_probe(struct device *_d)
> u64 device_features;
> u64 driver_features;
> u64 driver_features_legacy;
> + unsigned int status;
> +
> + /*
> + * Driver initialisation must start from a freshly reset, acknowledged
> + * device. A previous probe may have failed and
> + * left VIRTIO_CONFIG_S_FAILED set: FAILED is sticky, only a reset
> + * clears it. The device may also have been unbound without being
> + * reset. Start over in both cases -- the reset additionally clears
> + * any queue_enable left behind by the failed probe.
> + */
> + status = dev->config->get_status(dev);
> + if (status != VIRTIO_CONFIG_S_ACKNOWLEDGE) {
> + virtio_reset_device(dev);
> + virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
> + }
>
> /* We have a driver! */
> virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
This adds an extra status read on data path.
> @@ -362,6 +377,18 @@ 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);
> + /*
> + * Status is 0 after the reset: re-acknowledge before flagging the
> + * failure, otherwise the status field jumps straight from 0 to
> + * FAILED and the next probe has to build on an unacknowledged,
> + * failed device. Same order as virtio_device_restore_priv().
> + */
> + virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
> virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
> return err;
> }
>
> >
> >>
> >> 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);
> >
> >This is the bug, is it not? If you call virtio_device_ready you must
> >reset?
>
> ah, right. That test hook was only a minimal trigger for the core error path,
> not a model of correct driver behavior, so I've dropped it.
>
> A real path in virtio_input:
>
> err = virtinput_init_vqs(vi); /* find_vqs(): queue_enable = 1 */
> ...
> vi->idev = input_allocate_device(); /* can fail */
> if (!vi->idev) {
> err = -ENOMEM;
> goto err_input_alloc;
> }
> ...
> err_input_alloc:
> vdev->config->del_vqs(vdev); /* no reset */
>
> On modern virtio-pci, del_vqs() does not clear queue_enable, so a later bind
> fails in setup_vq().
>
> I replaced my test with a failslab reproducer that fails
> input_allocate_device() in the real virtio_input driver.
>
> Hope it is clear now.
>
> Thanks
> Peng
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-06 11:34 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 10:27 [PATCH] virtio: reset the device when probe fails Peng Fan
2026-12-05 12:35 ` Michael S. Tsirkin
2026-12-06 11:02 ` Peng Fan
2026-12-06 11:33 ` 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