From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796474114; b=nxmRAGlQ8rZmNssTBegOf6Nz7uKHP/rkYQ3ypykocU3RcTNmRWrWHA5ZcJZMhAxM+eux v6nHsQf9eraKqII+Pv9kJ5spgjeYUW34FoYPkGh64hI4E6ERn1baTnk2IhlXaD4PFB89z p7Mxj2Ga5OXtav3g8UJqJzCv5YDggc2UiLK0poZNbGwYMZQm0ugXFCC4GfCQLTSBvq5q8 CeGQRtlD0XRon7S8ymeAYx6LcadDbKfQiLRECiYMYWm/xWVInDHEE+OQSObxxbtRPwup2 yCgqYbS2/isIB8dKxpV8Pi5BBApZrQLr9xM3fDIuCCLe8IrINrU2dN5+yd1wEQxkHfw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796474114; h=DMARC-Filter:DKIM-Signature:X-MC-Rep:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:In-Reply-To:X-Mimecast-Spam-Score:X-Mimecast-MFC-PROC-ID: X-Mimecast-Originator:Content-Type:Content-Disposition; bh=zewzjsG51vXBnDDEA1mXRMxbl2g3CW2fSn1qSXJRNW8=; b=B8qjXV7ziUUMLli10+7MNZkgk3DnqLQkf9Rkclu1HobpMeOv8rfe4XoyiEuD56vxiLZr 3Ed4WHNg3IHuN4V4zcgfyC1aWvPm64CQHJqx35nqtiyNeeX6bFEancgOrq9YUoGzn68l8 FNYlYP30/7OAVd8p9fa3mBYYgtBVW6hpT1MZ/hCgmPRkw8gMW7VjIP08yOnjXbr2oZwWC a2ktvRGc/M5XPMo5frHHBD8ConLyW3krDhBArpyEermGCAhhgHNQgF3K+JIu6N0R1oTX9 Mvt7fDBTZ8IKKay3A9IubmpmgqpYdiKGbIv80+74wOS8pnv5ZfLecwaDnYIs9WF2bmQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b="BsWeb7r/"; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9119606A322 for ; Sat, 5 Dec 2026 12:35:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D9119606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BsWeb7r/ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796474111; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=zewzjsG51vXBnDDEA1mXRMxbl2g3CW2fSn1qSXJRNW8=; b=BsWeb7r/e0ae3FRciJgXyAADgJQTngQG1Ug8XcigMmfT+2bWLBotp6YmhfZCCi/rz+pYeo ETllZpim0w+BsAUs4t8QegqE0+vBKKMqr7iASCw2IZEhtJQTC+dA1XJJmrKC0Hrg/x4X7U x7nYo5c5dQpcvKWRjNHoBiIn3HxPuQM= X-MC-Rep: binary re single Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-317-P_mqc_cUOBarTgqKIyjDvA-1; Sat, 5 Dec 2026 12:35:09 +0000 X-MC-Unique: P_mqc_cUOBarTgqKIyjDvA-1 X-Mimecast-MFC-AGG-ID: P_mqc_cUOBarTgqKIyjDvA_1796474108 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4bb68d624baso16287635e9.2 for ; Sat, 05 Dec 2026 04:35:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796474108; x=1797078908; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=zewzjsG51vXBnDDEA1mXRMxbl2g3CW2fSn1qSXJRNW8=; b=j1n0/jqQiZkKQOAS8AUNWgLZA9BcKer6cKCu8d6EsQ6jKOBwAwf25bnNz/nEWjHgbB qXfEQQU5XjA3fsfy4/cxvfoG2vzoBTohimtM6R/U09qd6o1ufmghh9b8Pe/cnYkxHbLP LRS3Es3sYIrceZrEyih6qDouGotxdW95Q4Ki7K+j1y6PZb7QWSxnn0h6Gr/emE8GgzsT /Bpd2E8n585NKqzYxQZtHcOAPpeqPoK+6csToPwrGTjPnv9SzjhvZbjgC86PWrbvo8L7 1J/rndCFM/mzyfqkIDlL7u0NHoT/CbihvOtpFLR8Cl9n6JUIsrqd5Ng3QILkwaaRaAIO iWEA== X-Forwarded-Encrypted: i=1; AL+hErHcqy6LixqfcWPqcwZOAqMLixMrzPOXa5Kn/aMuwnXxXUKixtYwax5/DvVACJgD3cg+7LEwA6lL6jBAacBJRA==@lists.linux.dev X-Gm-Message-State: APuU4Z95DWcKllPoUa2JFW3VZwZ3hkR0W5ldooMNdVHMHl79FOHkzsAc CeN8BLU7F12AHUbiLfFM1AMYXOUKCcN9LipcrdNn8zz2vD8rr3aFo+8N0UzpFRldET6fv971nwj dXx1omFMOR09rZ4PVocy0dKmZSJu1LU+gVmNB/YFpjs+q2PjyWo9YGf5NoilF6KXABR8G5xQi52 sU X-Gm-Gg: AZFdTZdtDO2fAkse9i/oMb7Tiy9KGM2OO4qYkftvWXLvkFF8EC2Z3KpyyJIZ/b9lFm2 2jN+vFG08JZm9F4m+r/0xGXVTC1swvBO8YI2DiJyndcnkA3Q2jao+VVqw/+nmRl5voRjGcmZa+N o+1PLX6fnyxSgrlsS29+eOFydnsUDKnASXWzchocIY9Ms3qe4WildC4IE0VqUvgiw1GIAXf/mih ju/nqo9CI9jPA4gl+QYKIKRih4T/MJ6Eq/ltedSdN26l7d8kQiJx8pao2qhQetUr31r/jjl4E+3 tX/N/7Oo6rG2JS57nuBF0dkFiN/99ZI5cxTcMMdSM50HJE9PY0yucmjRM60Gt/6Sq6h2OvL5PzE 6kN7gQxWki7OIpTIWgP+i2zO9Z3w/j0PmZ+ETweSHtRw= X-Received: by 2002:a05:600c:314a:b0:4b7:6aac:7406 with SMTP id 5b1f17b1804b1-4bbeb6ecd1emr87626645e9.3.1796474108140; Sat, 05 Dec 2026 04:35:08 -0800 (PST) X-Received: by 2002:a05:600c:314a:b0:4b7:6aac:7406 with SMTP id 5b1f17b1804b1-4bbeb6ecd1emr87625905e9.3.1796474107539; Sat, 05 Dec 2026 04:35:07 -0800 (PST) Received: from redhat.com ([2a06:c701:73fa:9a00:793e:eac1:d749:dcc7]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d1737db151sm10676773f8f.34.2026.12.05.04.35.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 04:35:06 -0800 (PST) Date: Sat, 5 Dec 2026 07:35:04 -0500 From: "Michael S. Tsirkin" To: Peng Fan Cc: Jason Wang , Eugenio =?iso-8859-1?Q?P=E9rez?= , Xuan Zhuo , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Peng Fan Subject: Re: [PATCH] virtio: reset the device when probe fails Message-ID: <20261205073228-mutt-send-email-mst@kernel.org> References: <20261205-virtio-reset-v1-1-c2203390b005@nxp.com> X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 In-Reply-To: <20261205-virtio-reset-v1-1-c2203390b005@nxp.com> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: t-SHJdxKtu03WZrfDfdlDMFmCh0lq4W1ikEchg95JyI_1796474108 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Dec 05, 2026 at 06:27:26PM +0800, Peng Fan wrote: > From: Peng Fan > > 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 > --- > 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