From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.211 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796560570; b=plVSGvSZ4TrOI/ZPp4oBzuC8BvtNYznMNr8mok7ZQ+N3sGtxbBcCBbc+23LNh0DlGis6 Ge7f3nKfab4CZ+RhQFmHpj6x/SyjBwpiJ/CdR3ObU+iSqUIsVcb5aXb8dAQJ7QxrGFH4e pw/BqCQWeL/9nuN/GwJzhJP8TT6/GF6ld41wqdQxgQvHF3AADyrhTenlrGMrCBHkeD0X4 LgLv5zNLHw3Xg8VKQIOGIdRd0sZ6oOnORPu1eL75rC1Q1MCXtpM6yCLNm/HZ3SYIf3qbw oFHSu1XxwxyNZisEZHT+OrC3VDnkeU3z0l35PY2rBW0R0OmneMRn64UIKz94VdogMiw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796560570; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To; bh=13X0O6EcTcutDfWcIay8MGJp03v3DNAsCVHyqgiEk5Q=; b=SpjX+F4J4IffaxXfuFsDoGUwao+sdKI9CByib0x7pCinAbWacwsnKNtpK75aJCUCFnjG nFQsGROHpHp/3KFZc1bVj3BBf2m1/FjgxPC3VZCeaQJkSoQKzIoNkD6ReorNg2i39P+90 YxSDRUrQn5QY4lLFGPElT/aGupnlgdEEmMiJm0PA5Obf6rvBzWorfbHJMedWwy29I9duu EMG7YaMdtnVqkC0LW61sseTi8j4mYS5tfC11JhsyZk3FZ/UTVOfg0lPSJupQPU93Fk+vA 3DOLYW4MWqNXaDgS5MV3EB7qvjM4iE/5RNajJUGletyGZD6sGEXWcI5agHXVpGwYLWw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="Pf1tF+/v"; arc=none smtp.remote-ip=91.218.175.211 Received: from mta0.migadu.com (out-211.mta0.migadu.com [91.218.175.211]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9571606A31F for ; Sun, 6 Dec 2026 12:36:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D9571606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Pf1tF+/v X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=dy4vcAvCXTB3skcR2rTopWeZ/THNQfVDz2YsYhGyOu8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796560566; v=1; x=1797165366; b=Pf1tF+/v3rjF4wpdJxyPTmScp22L3dSTlBMPpcoNTtKxVnLpe2XHkm9RORpblVAymMXw6gaI uejvLLUN6JTfsXm4/mO5i840w4FAPBtVYq4xI4Q5o8ZmsfnnyNDfYcYFmJZ/CJSdM0A9nPLsF+m ClKDrn4B5HLaT+KzonoJbj9M= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id db4020b640e3ce6e; Sun, 06 Dec 2026 12:36:06 +0000 X-Mizu-Trace-ID: db4020b640e3ce6e X-Migadu-Flow: FLOW_OUT Date: Sun, 6 Dec 2026 20:35:54 +0800 From: Peng Fan To: "Michael S. Tsirkin" 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: References: <20261205-virtio-reset-v1-1-c2203390b005@nxp.com> <20261205073228-mutt-send-email-mst@kernel.org> <20261206062620-mutt-send-email-mst@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20261206062620-mutt-send-email-mst@kernel.org> Hi Michael, On Sun, Dec 06, 2026 at 06:33:58AM -0500, Michael S. Tsirkin wrote: >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 >> >> >> >> 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? For example after virtio-input driver probe fails, the driver and device are not bound. User could write device name to 'bind' to trigger probe again. > >> > >> >> 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? >> >> 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. Sorry. I use b4 to manage the patch, I intented to put more info in cover- letter (mainly the test code) with some background info, so the commit message was duplicated in cover-letter. I will drop that. > >> 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. virtio_dev_probe() is the probe/bind path. it runs once per device bind. The function already does several config accesses, so I think one additional get_status() read is negligible. Or I may miss-understand something? Thanks Peng > >> @@ -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