From nobody Sun Dec 01 04:57:30 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 4Y1F5k58vTz5gCsF; Sun, 01 Dec 2024 04:57:30 +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 4Y1F5k2Ldvz4B6J; Sun, 1 Dec 2024 04:57:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733029050; 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=1aSVJizZ821KFwwIwqFQvb9TTxBGiE2xhc0d/RCTCTw=; b=VqeYpbVmgSFL38yS02yZ+L7pZOK0Waml9YXoiF74l+YF/p37Zv7N+O3pDRvRMP8j17kvqX WldZaf9+UsR5GkmKVuo1uGQd/2gANWS9EtKV70FGw4v6U7v+QqIM7RRPvD5n+bH/aWYgbf Sf9wLW5ND9yaFnVrASDRj1MTsn0jh+iic7XGIOMU83LnXbA/AyWiY9c9T3mJrxCt8OuRZV 3jT6Tg3B2gH0ICZq6MVI4TOG+iMW6ClBpOsWbcIVeOQnn14mWGgacp8jzvT/a8WMFzSVoM Khd0TJKfXf5a98tCLfdZWEmTZ5kRDsPH4c4qHOH/jLvXAfSr6VFp+1RKU8iCfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733029050; 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=1aSVJizZ821KFwwIwqFQvb9TTxBGiE2xhc0d/RCTCTw=; b=akK3ThxXPPZEY0UgXS75tGJ0amlG7+jQR7gly4koG4ffg4lk2FEBCxudm7VZrOHr5VybTY 3ZMIM5Oi/I0bdqaV6AR3HuTKkGwRozTmzWbh8sMz68gaw1nmpDGpt6AKlIqg5MI2OSCfuD 8F+JrpitwekS89+fNhfkqKO80dL/wV9dC3KLDQL/lRQcuitpob+/EsBjFixG38LUlImGzc CHgOJOaKU5KVQlnQnwak4+vfTYvAjrEbZQzzQTWl+z6ZDcRCh9EcX0GRJ7X2LoKGkwRJEQ Q2LpC2lrqdJkQGHD19cSip63972LHtuG1x+W6RxpqXi3wUFsunuQkcINYnuPCQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733029050; a=rsa-sha256; cv=none; b=alBQpGTfEKDSMwJ7IgzbMjs6bNVCEc7D7c45UqWjsaWEGZyP4vSlCRxlBuqLNnnoB1mdXD U+mDTpqBkw9WACTGgIYt/ZQddPtV7BBO2ZMVVKtq4pYzRF+tCxzzNf8wN6mVKmZ+xPHFuo Out0uiZSXYD/hBIkyGtH7lYEl1z6SFacHCRXyglD6pkCJFu8dm1+yKMtHco/uzlimKQDku AOXfpLHNiMJbkVH65BRJ3E5F4aJnl1Lm0ZYIhqPRMn8MYCCkgaaggyJ6IctN10uC+S3l/5 OWqo4m6TTyRyxXzpMAdAdiqKYP24tUf5I4nHyqgYZPNqlchEWL5l3pKJ4siOrA== 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 4Y1F5k1z88zf7S; Sun, 1 Dec 2024 04:57:30 +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 4B14vURL096112; Sun, 1 Dec 2024 04:57:30 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4B14vUsD096109; Sun, 1 Dec 2024 04:57:30 GMT (envelope-from git) Date: Sun, 1 Dec 2024 04:57:30 GMT Message-Id: <202412010457.4B14vUsD096109@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: d53146682254 - stable/14 - 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/14 X-Git-Reftype: branch X-Git-Commit: d5314668225469e2b59ee929ad9717e63271ab0a Auto-Submitted: auto-generated The branch stable/14 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=d5314668225469e2b59ee929ad9717e63271ab0a commit d5314668225469e2b59ee929ad9717e63271ab0a Author: John Baldwin AuthorDate: 2024-11-23 16:39:02 +0000 Commit: John Baldwin CommitDate: 2024-12-01 02:46:08 +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 d22dd4868ff6..f08a7718e071 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -2579,7 +2579,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); @@ -5720,17 +5726,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)) {