From nobody Sat Nov 30 03:34:31 2024 Received: from mr85p00im-ztdg06021701.me.com (mr85p00im-ztdg06021701.me.com [17.58.23.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6397192D61 for ; Thu, 12 Sep 2024 23:45:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726184730; cv=none; b=QCz/XaVu7cRm8zxYmxkcdyUyPcCsV0bCRyT1C+W4ANhStMMh9MFUnP0KI2ChOn6I8vwyXuTMcpqFUftQzrpmg2qeQ4J7oE9mMk+kke0BIZbl3HHpdHNLd5PNGVnb8gLLjX8w5A8tP96+UTNGZa8vMd/do6fGzsJrUItLKgUoyO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726184730; c=relaxed/simple; bh=FL72VzZm1QNC4bDsVSvIwR7iwa2FxHI8m9WiIsoTqYM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NE4L/5y96TrAwWMqNNBAAgLNKBPTEQgJXR44hJc6KvsNlAHQg3hwIFhZbg95ftYDWMi56B0tjfBNRviVnHrNIIVg0kC+rUxIQkxIiiH9Jxn/NW/yoBenrM8ftJkGeCjwUN/dUrDP2xA0+TX2RhKCjPDqYODRGQT9/msZDyAS/AI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=R3rELtJJ; arc=none smtp.client-ip=17.58.23.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="R3rELtJJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1726184728; bh=OWYrCl91sGsazZIoWodu7Wr+r0nEaOLDk990QhU1S18=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To; b=R3rELtJJFdVjz12yxfFyAMUhLtMjHGexM4tBM9ZRK+xPSJNfbEJsjphsPqO69KBfP Eg8C5XBowEH/PM/zCIZQ8wxQZYIUZm+db1yA1OX4PYUfz+AoIbASiLh4Kq7j2Y/Wov gwTbVlVLEgf0T/TmO+sQWrNjRDPTkcHUaTSTCraGjfEdZvGkGi3iUKabutgmu6m789 hkRfPoxHCRhOSaJelFarKgkBojITwQ4bl6y7xSjJ4rbqmoLDFfeJBSMHBg+uGe/ZyQ 75yGPV+gfQLCfl/gdcamQogD92zeOrDgIQbS+T32YJPAQLfXYAyPjqJr3HFvGzQYji 8LNDuUyqfivyw== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06021701.me.com (Postfix) with ESMTPSA id 7A4EA2633439; Thu, 12 Sep 2024 23:45:26 +0000 (UTC) From: Zijun Hu Date: Fri, 13 Sep 2024 07:45:10 +0800 Subject: [PATCH v2 1/2] driver core: bus: Fix drivers_probe_store() giving user wrong scanning result Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240913-bus_match_unlikely-v2-1-5c0c3bfda2f6@quicinc.com> References: <20240913-bus_match_unlikely-v2-0-5c0c3bfda2f6@quicinc.com> In-Reply-To: <20240913-bus_match_unlikely-v2-0-5c0c3bfda2f6@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Zijun Hu , linux-kernel@vger.kernel.org, Zijun Hu X-Mailer: b4 0.14.1 X-Proofpoint-GUID: -VXpWc3Lma8v_L7olFHHRYVJmxr4oFCr X-Proofpoint-ORIG-GUID: -VXpWc3Lma8v_L7olFHHRYVJmxr4oFCr X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-12_10,2024-09-12_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxlogscore=999 clxscore=1015 adultscore=0 suspectscore=0 malwarescore=0 mlxscore=0 bulkscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2409120171 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu drivers_probe_store(), as store() of bus attribute drivers_probe, may give user wrong scanning result as explained below when user scans drivers for a single device synchronously via the attribute: - It wrongly collapses bus_rescan_devices_helper()'s various error return values as -EINVAL, that is not expected since user may want precise scanning result. - It wrongly regards bus_rescan_devices_helper()'s return value 0 as success since the following failed cases also return 0: (1) the device is dead. (2) bus has no driver which match() the device. (3) bus fails to probe() the device with all its drivers. Fixed by giving user precise scanning result or right prompt via static bus_rescan_single_device() implemented for scanning drivers for a single device. Signed-off-by: Zijun Hu --- drivers/base/bus.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 657c93c38b0d..4b5958c5ee7d 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -40,6 +40,24 @@ static struct kset *bus_kset; struct driver_attribute driver_attr_##_name =3D \ __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) =20 +/* + * Bus rescans drivers for a single device, and derives from + * bus_rescan_devices_helper(), but returns scanning result + * as precise as possible. + */ +static int __must_check bus_rescan_single_device(struct device *dev) +{ + int ret; + + if (dev->parent && dev->bus->need_parent_lock) + device_lock(dev->parent); + ret =3D device_attach(dev); + if (dev->parent && dev->bus->need_parent_lock) + device_unlock(dev->parent); + + return ret; +} + static int __must_check bus_rescan_devices_helper(struct device *dev, void *data); =20 @@ -310,13 +328,33 @@ static ssize_t drivers_probe_store(const struct bus_t= ype *bus, const char *buf, size_t count) { struct device *dev; - int err =3D -EINVAL; + int err; + int res; =20 dev =3D bus_find_device_by_name(bus, NULL, buf); if (!dev) return -ENODEV; - if (bus_rescan_devices_helper(dev, NULL) =3D=3D 0) + + res =3D bus_rescan_single_device(dev); + if (res < 0) { + /* + * Give user known -EAGAIN in case of internal + * -EPROBE_DEFER, and propagate error code upwards + * as precise as possible. + */ + err =3D res =3D=3D -EPROBE_DEFER ? -EAGAIN : res; + } else if (res > 0) { err =3D count; + } else { + /* + * Which error code to return for such synchronous + * probing result ? + */ + dev_err(dev, "device '%s' fails to attach a driver\n", + dev_name(dev)); + err =3D count; + } + put_device(dev); return err; } --=20 2.34.1 From nobody Sat Nov 30 03:34:31 2024 Received: from mr85p00im-ztdg06021701.me.com (mr85p00im-ztdg06021701.me.com [17.58.23.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 101DE1C3F16 for ; Thu, 12 Sep 2024 23:45:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726184732; cv=none; b=oeZ1helw+EZbhictl85oyTSafQ2hJYKD4IuUymC186pdmfAfi9HK+RHQMnJuPxJe5kbNE0gln1GrRsUGi5tDNgile2Vf7OvSx23YFwx/czWo2gmt+w7YvCHBeNpGrUydMU4RFa5UPpevIgPkt6mYE9qBp0fqEgEvoQr456kjRn8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726184732; c=relaxed/simple; bh=Cqe+kWq47m2XsK3kmFGisV1NfYVWgyAGcB4tUNsqNEU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tkxQ+Mm5rap1ryHy+T1mr7qWFEIa4A0BJcs8eBhAjuWTGmWMTKlf5pB1xxYBscJP4ArWuGebeI+hfkxrbWcMtqPti/ISxNIDYaZec01brCrCKjrueZz5K8IGVtYwyAg6fPyjiHwbsSUpo+x0+9TXB0t7Whcl5FdVIfS8PIcZjiQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=cIhHpa6V; arc=none smtp.client-ip=17.58.23.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="cIhHpa6V" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1726184730; bh=J79Eu2I+ROINsSW9OWNNnHUMAZ6QbBJ6uVzFELs/tK8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To; b=cIhHpa6VKs7q45D+dZRXXkr2P/AexmMPuozUEK8gvb4PBYJFXG4weF4RCCHTi7icZ eI8sRZ1sUJz5LlOi6eELEy2y8UpP366q7usVZNlh2gwjLeKVfl874Z9W2AAEvDOecE vvxe37qIgzrqb/G4Cgi78+bMNDTt4c/oRhVfD/oGhIGea49t++Hoog5+HDMD0jWT+w +ZmqGBqc+Mfds9XC+rxR3wJwgEx/l94hnJOVi7ZUJyIgXgVlFW555DBvCxGqRPDgtk R+fsH3+0kBbivaEj30UgpwJXkLIz//M9pEfUKQo+fjMHzj9n+NKnb4ci8gKaHxln56 0/hPEjJShPh7Q== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06021701.me.com (Postfix) with ESMTPSA id CAC6426333D0; Thu, 12 Sep 2024 23:45:28 +0000 (UTC) From: Zijun Hu Date: Fri, 13 Sep 2024 07:45:11 +0800 Subject: [PATCH v2 2/2] driver core: bus: Correct API bus_rescan_devices() behavior Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240913-bus_match_unlikely-v2-2-5c0c3bfda2f6@quicinc.com> References: <20240913-bus_match_unlikely-v2-0-5c0c3bfda2f6@quicinc.com> In-Reply-To: <20240913-bus_match_unlikely-v2-0-5c0c3bfda2f6@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Zijun Hu , linux-kernel@vger.kernel.org, Zijun Hu X-Mailer: b4 0.14.1 X-Proofpoint-GUID: TGewEJwZtIE7Y8iAUsjTOARvllsaqNgO X-Proofpoint-ORIG-GUID: TGewEJwZtIE7Y8iAUsjTOARvllsaqNgO X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-12_10,2024-09-12_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxlogscore=999 clxscore=1015 adultscore=0 suspectscore=0 malwarescore=0 mlxscore=0 bulkscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2409120171 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu API bus_rescan_devices() should ideally scan drivers for a bus's devices as many as possible, but it will really stop scanning for remaining devices even if a device encounters inconsequential errors such as -EPROBE_DEFER, -ENODEV and -EBUSY, fixed by ignoring such inconsequential errors and continue to scan drivers for next device. By the way, in order to eliminate risk: - the API's return value is not changed by recording the first error code during scanning which is returned. - API device_reprobe()'s existing logic is not changed as well. Signed-off-by: Zijun Hu --- drivers/base/bus.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 4b5958c5ee7d..fa68acd55bf8 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -58,9 +58,6 @@ static int __must_check bus_rescan_single_device(struct d= evice *dev) return ret; } =20 -static int __must_check bus_rescan_devices_helper(struct device *dev, - void *data); - /** * bus_to_subsys - Turn a struct bus_type into a struct subsys_private * @@ -797,15 +794,23 @@ static int __must_check bus_rescan_devices_helper(str= uct device *dev, void *data) { int ret =3D 0; + int *first_error =3D data; =20 - if (!dev->driver) { - if (dev->parent && dev->bus->need_parent_lock) - device_lock(dev->parent); - ret =3D device_attach(dev); - if (dev->parent && dev->bus->need_parent_lock) - device_unlock(dev->parent); - } - return ret < 0 ? ret : 0; + ret =3D bus_rescan_single_device(dev); + + if (ret >=3D 0) + return 0; + + if (!*first_error) + *first_error =3D ret; + /* + * Ignore these inconsequential errors and continue to + * scan drivers for next device. + */ + if (ret =3D=3D -EPROBE_DEFER || ret =3D=3D -ENODEV || ret =3D=3D -EBUSY) + return 0; + + return ret; } =20 /** @@ -818,7 +823,10 @@ static int __must_check bus_rescan_devices_helper(stru= ct device *dev, */ int bus_rescan_devices(const struct bus_type *bus) { - return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); + int err =3D 0; + + bus_for_each_dev(bus, NULL, &err, bus_rescan_devices_helper); + return err; } EXPORT_SYMBOL_GPL(bus_rescan_devices); =20 @@ -833,9 +841,13 @@ EXPORT_SYMBOL_GPL(bus_rescan_devices); */ int device_reprobe(struct device *dev) { + int ret; + if (dev->driver) device_driver_detach(dev); - return bus_rescan_devices_helper(dev, NULL); + + ret =3D bus_rescan_single_device(dev); + return ret < 0 ? ret : 0; } EXPORT_SYMBOL_GPL(device_reprobe); =20 --=20 2.34.1