From nobody Sun Dec 01 04:58:11 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Y1F6W6n3zz5gDMx; Sun, 01 Dec 2024 04:58:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y1F6W3lXtz4DHT; Sun, 1 Dec 2024 04:58:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733029091; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1E9CDmdhvrwlATGtaM/RU+9Kh7k8MDGLC0eYBL0wyzM=; b=GWAqZo0ytO+EpOHcyueZ1SM9iZLP1fm0Vw0253Zi3rpSguaLW+iMUwFrzpS17v0W4erhxx ufAAqG7iIE3lXuHDOS5DWEW/ZfAnfCF99rH3pp1dR53rnkndzyDKvtYLF+66Gv/PqOkJ+R WAnPi8QKzSpVrMvQclk9ZB8Rg5rHjkceFRCG25dW5qQVED/sGQUJO+TZEYu2EII+jFeNKF 2zNYD8Sb7TAz405OmQnzzUF262YpGJoYMalxB16Z7lVYDDRRjPOufCHvtfut64TmeS7fMf TVN77bBO8+atBA8xVsfdeMzUaxVRegm9/nIA8U0BWO91a9WNPrdn9SH4tCR8Cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733029091; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1E9CDmdhvrwlATGtaM/RU+9Kh7k8MDGLC0eYBL0wyzM=; b=M/DoDW4L5RfU5s5Lgyech6NrwVjVhwUR7QqEUAZLRsblUBGRPBMAq9cirgv70C0K7a+cUZ auIpFPkTjjIHktSbaZa8BS0Qq+t/ePlGpHi0KkbZhyOKKxyVl9bl1Mrepm1y9XmXbadUMa Ba0QGJ2h0TFwYlL8ADOcnhKDQswjPgirBPK4XDd7y1j3HZmXeiosjo/oDDaa8JyHE2TIIm vYmxnLTS/pfdnJobRjrBs7Rq4dk71xrzEk88B6yPe7ckGCN6C1m6QC5EZs1eO6oWhzx4/Z vqzfIkjy+ZHh/F6tbDCS9GDganFuYFj+uqaT6xB0ejZZoOD67gY88Dj/9WOuWw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733029091; a=rsa-sha256; cv=none; b=mcGdjZI6bPnO6Q4za8yeHIBKcspe7RMixTpNu8TNfbk0p9DPPc6pk41BKZ8gTftcWiXpk8 xyfkrvUT/oLrq/iIwZmguYRi3sTbLLKkQHPNGBKTASKIsnUjVzxc2Z/0BW5BcS46YHO2C3 wWOr4ZJ2MGtrn5tuJil68dZpUbC1a6lvMrxTzK/lOV7ZARr8jBfTZM0KlprNMKntdbwt7c DJOD+9xNbVXfRuiDU3nHO/67PY0rh1RifSFfpWMY8ADeTGfGps3NGZ94DUYmZGz9KYBH80 ayQvkkvdXex04M+EvGxO+slYMcJb2O4x1lvOKYUsIyA7r7Xly24jt/X6a/JOGA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Y1F6W3KnDzfLT; Sun, 1 Dec 2024 04:58:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4B14wBNU098160; Sun, 1 Dec 2024 04:58:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4B14wBPY098157; Sun, 1 Dec 2024 04:58:11 GMT (envelope-from git) Date: Sun, 1 Dec 2024 04:58:11 GMT Message-Id: <202412010458.4B14wBPY098157@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: 9ee1bb61d76a - stable/13 - new-bus: Fix some shortcomings in disabling devices via hints List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 9ee1bb61d76a9a74983ae13985b66485d16f49dd Auto-Submitted: auto-generated The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=9ee1bb61d76a9a74983ae13985b66485d16f49dd commit 9ee1bb61d76a9a74983ae13985b66485d16f49dd Author: John Baldwin AuthorDate: 2024-11-23 16:39:02 +0000 Commit: John Baldwin CommitDate: 2024-11-30 21:51:22 +0000 new-bus: Fix some shortcomings in disabling devices via hints A device can be disabled via a hint after it is probed (but before it is attached). The initial version of this marked the device disabled, but left the device "alive" meaning that dev->driver and dev->desc were untouched and still pointed into the driver that probed the device. If that driver lives in a kernel module that is later unloaded, device_detach() called from devclass_delete_driver() doesn't do anything (the device's state is DS_ALIVE). In particular, it doesn't call device_set_driver(dev, NULL) to disassociate the device from the driver that is being unloaded. There are several places where these stale pointers can be tripped over. After kldunload, invoking the sysctl to fetch device info can dereference dev->desc and dev->driver causing panics. Even without kldunload, a system suspend request will call the device_suspend and device_resume DEVMETHODs of the driver in question even though the device is not attached which can cause some excitement. To clean this up, more fully detach a device that is disabled by a hint by clearing the driver and setting the state to DS_NOTPRESENT. However, to keep the device name+unit combination reserved, leave the device attached to its devclass. This requires a change to 'devctl enable' handling to deal with this updated state. It now checks for a non-NULL devclass to determine if a disabled device is in this state and if so it clears the hint. However, it also now clears the devclass before attaching the device. This gives all drivers an opportunity to attach to the now-enabled device. Reported by: adrian Discussed with: imp Reviewed by: imp MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D47691 (cherry picked from commit b4c700fa7cca7d3e4da84dd417fc3efc42788a52) --- sys/kern/subr_bus.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index aec5b4c615fd..0add4e019043 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -3018,7 +3018,13 @@ device_attach(device_t dev) int error; if (resource_disabled(dev->driver->name, dev->unit)) { + /* + * Mostly detach the device, but leave it attached to + * the devclass to reserve the name and unit. + */ device_disable(dev); + (void)device_set_driver(dev, NULL); + dev->state = DS_NOTPRESENT; if (bootverbose) device_printf(dev, "disabled via hints entry\n"); return (ENXIO); @@ -5858,17 +5864,20 @@ devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, * attach the device rather than doing a full probe. */ device_enable(dev); - if (device_is_alive(dev)) { + if (dev->devclass != NULL) { /* * If the device was disabled via a hint, clear * the hint. */ - if (resource_disabled(dev->driver->name, dev->unit)) - resource_unset_value(dev->driver->name, + if (resource_disabled(dev->devclass->name, dev->unit)) + resource_unset_value(dev->devclass->name, dev->unit, "disabled"); - error = device_attach(dev); - } else - error = device_probe_and_attach(dev); + + /* Allow any drivers to rebid. */ + if (!(dev->flags & DF_FIXEDCLASS)) + devclass_delete_device(dev->devclass, dev); + } + error = device_probe_and_attach(dev); break; case DEV_DISABLE: if (!device_is_enabled(dev)) {