From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796556849; b=G7zTKPtTg3aJgAhOZ85wp1917e+uVBjgXJtw80VzxpBLLwz6JBXLWT77CUc64RScYqe2 GKLqqEGX4O/XIVn9WIJTxE/6VTnKAHqfhIrupzYr2vbHK4B042wkYJf0cwNoYsukaxA+0 yZOngqGgwKpvPD1E/HxuoTTO93z68sMqlOe57t7ZT7geKTl/qpjnf1vl/kveep85eW+oN xMQhCEIODdxkC7OXFifOfmm/UDknoFwQ3bdO/9Xt1Q6n7XoepcwL617KQQI1X9sc5hIzK 9bVovH24zNz2WzeZafyntb4U4R9vNlTGIFg0FmRMC7Hnmgxe/5WM3JlDFwQymQelR1Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796556849; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature: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:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To; bh=5fDjS/Ytgre/ddisWLC+ayUAXiq43x3yH6s5DzeDrgk=; b=TnzSOw0JPQt0aCLayMtLDkues+rtkuiEPiRwK39w4Z1t95VhRWbLcPLOKTwg8qG1HIyU q57rjUYL0CBanaQ6Xi0uib1ZNYEEj2+beJJylY1YGviuGXPO8hRiTI13sBELDYkuV4cHD YGKC32fHIJzuzeFGaGGBM0gCEkb7b6Q4IxWx9EHjh6ApgptZbBo6y9BR2hM2g/TA03p4n bWByBzxNY4X70PLmmzmqBfZXx5+Lz7XW/ll2CZglf4naZXoMhrJGCx5OtbuX5MA5Le2IT eFwYneKBm/7r+EVrxJ1GiAlvn2i8dTwEcybVVAdq9BZXHGSiN84SresZYLVOF5tcpwA== 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=F6qJkVEO; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=BKPAnXAS; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 15769606A321 for ; Sun, 6 Dec 2026 11:34:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 15769606A321 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=F6qJkVEO; dkim=pass (2048-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=BKPAnXAS DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796556847; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5fDjS/Ytgre/ddisWLC+ayUAXiq43x3yH6s5DzeDrgk=; b=F6qJkVEOLk9OW9npD6S6J6wpeEKbWQEk2CK51uyZORWSs2NC9EFv/cHBqvPJwN89Gl3Apo Dq8yG/mTZT5yRrqqihHLeOqYThF1zZHMj8OttOemOJ8vqsosSo/nnBiwu7UsP7qVxxtk7V CRyFYlphg5AIosz3cgUIzCfduDp48+Q= 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-686-SUROwfr6PPCbILV02WF-IQ-1; Sun, 6 Dec 2026 11:34:04 +0000 X-MC-Unique: SUROwfr6PPCbILV02WF-IQ-1 X-Mimecast-MFC-AGG-ID: SUROwfr6PPCbILV02WF-IQ_1796556843 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4ba01393800so13416405e9.1 for ; Sun, 06 Dec 2026 03:34:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796556843; x=1797161643; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=5fDjS/Ytgre/ddisWLC+ayUAXiq43x3yH6s5DzeDrgk=; b=BKPAnXASCniJPLBnQ60C74uV2o0XUAeB48WTwKBYTwQPFX9WVQIFcM/d3Pv0wndv8e rev4ZgqJMfufwZTMWfvgibq14vTVVaxPAPYDcPtGaRgA9ZEx7Nqi+oQAWKD5kx/ST8nK KqVRELqs0yqEgnb1QQ63DY/I7REbBCWMkIxL5FUSM3AFhoQLoFop/zT+MJRknd0mHA3J xGnlFofFL2cqvURtNnWY61HOItk5o3GfTHkYiWhfIY3tolNnpaKLGbvL9mjbNey6aS27 mfoaveWZx0WoDo3W75dVsXrsweH0atAPtFF5xz2TodXgkWsHHt6VBmr084wNn5+eJX1x 2I7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796556843; x=1797161643; h=in-reply-to:content-transfer-encoding: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=5fDjS/Ytgre/ddisWLC+ayUAXiq43x3yH6s5DzeDrgk=; b=UiOxJwfjnBeakOBB+6R+W+5Wd9VBtVlK+DdugobskVi/L7OPFKu5mZNQIauK+bSD77 Izv83x1TKa7k9ygI8kPdloevIdV4TAQgLOdXu+eEi2TrzwUyNQL58kQs1KiYz3Ky12dt f9KgRkNi0fGsaew47fLniWlXksnQm4gjvxK5/BH/ARlKVq44Ovog/363Hqpdsx8GOJHD cWgWiWNgK6A2AUp9E7u2Um57gSLT6CI5he/4NRDt4nptZKb8XAxpdvjoLxDlaMfbw43w giJE3lZdsT67U6LlVodNW33iZi/wrESDeoSjIf0ojvWGMUbZ5WIKKX22G4e78OQnbLz6 9wQg== X-Forwarded-Encrypted: i=1; AL+hErGGvTQaNvvrzM1Ofs+pDKYsmK5TQCpwiXgdCnqLqkjXeuVt2qOA8UI/nbDP3AFAgCQ3dNxarFe5R1VUIZY=@vger.kernel.org X-Gm-Message-State: APuU4Z9RSEYRDCIYk9BAZWdmfT62Rb9G73PyMyWvfKSAWozPPco5qvvG gcGMS7U6pLNkY0QzyMp3DV5wh+bKaPTLZXVHX7l5D1qYiuiUK5qSkEE7aPpcgsx9txU3Ns89jHw v5JJH4VW3Fhnb5OmOX2/deXOoR89ifys6xj1FQzxqIkO6IzBwdIbGxUB/jMlVOdVa+w== X-Gm-Gg: AZFdTZcseYKRvBenOcJNUqi4KZ9kYorz8PyaRDYLMybB3+1fRt1P387pod1y7jVj85m wL3/WG00aXGlxVZaJBYq0cBo8VcQk1axXZyHzww86j6b9A8O7mDvI/cw+4FjqFcvPhMf2eiFW/P d79n/3DLugEWxJAChIgf6FbqAKELaSgRv+OtmltEzSkh3QWtOc7K9xvtW3csMUySReArY5zkPkT 23Y+EHX+kv5fdnDxkBeXgsCx19fw5OJt1pO2GkDnKpHWm6tKCcj61toqOJxyJB7G0ls9MAoRKo6 WOmljSo+IxN/KfPHxKzX28q/ws4zxdC3FvDSvl/ybq1lpzlJ5vWPetPp6kMcVY9ZODciWlUbpCh r9znwT7wuTzjvQUmEhwbkyZCHpIC1//Kp6Aj4Yuv9 X-Received: by 2002:a05:600c:a086:b0:4a2:526b:d410 with SMTP id 5b1f17b1804b1-4bbeb8c8e84mr137852545e9.13.1796556842885; Sun, 06 Dec 2026 03:34:02 -0800 (PST) X-Received: by 2002:a05:600c:a086:b0:4a2:526b:d410 with SMTP id 5b1f17b1804b1-4bbeb8c8e84mr137852055e9.13.1796556842335; Sun, 06 Dec 2026 03:34:02 -0800 (PST) Received: from redhat.com ([2a0d:6fc0:150c:9500:cd8:4b56:5c18:ebdc]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d17435e113sm17072607f8f.52.2026.12.06.03.34.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 03:34:01 -0800 (PST) Date: Sun, 6 Dec 2026 06:33:58 -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: <20261206062620-mutt-send-email-mst@kernel.org> References: <20261205-virtio-reset-v1-1-c2203390b005@nxp.com> <20261205073228-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: 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? > > > >> 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. > 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