From nobody Sat Nov 23 16:39:37 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 4Xwd3Y6mHQz5dWNf; Sat, 23 Nov 2024 16:39:37 +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 4Xwd3Y69FKz4QCR; Sat, 23 Nov 2024 16:39:37 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732379977; 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=g25o6RjvJ+2vUqd6cBG8DbU+BiZbBgwPNa8Nythqp94=; b=pWaJp2ILujPvr9ZRwfbQGTbQDVSVq1+ixfR+Eu+piGeUy114+b76RFh6dahLvpOKkwjp0f Yeq/nxaPXxJ1Dvv69c0az993B9r/t95ECVQH34HYS2cN//iX72idaVTYDZnmwtlFAog784 +VTWTpq3qbN7krrrbKk5pjj+mUsHEVo9UT6swyl2KxJzKr72NN5yikn0ZN4rgZjQFboArM Aox0Bi7NFY09FGu73aZzHtKnmRWUztzFrEF8razEEMWbKOl1Szbar2XmbswIm19H62xi8X P+Mfcy6bNeR3YHEapTEW4uZ76PKbztY7vhUnzj+bX7k1ZG0mFqYNUTR73Qg41w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732379977; 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=g25o6RjvJ+2vUqd6cBG8DbU+BiZbBgwPNa8Nythqp94=; b=AiiWCIfzHXTQ9AvUJT4St3lYL3RG8JNx3wzWhfiam+fBrwykCUDMx0xIdx2j40lBUKz5o0 M2fpfy/3J+Me/l5RlfosSW3aVSmQhzJ83l/UPoZG2OBuhv+BcFkLt8OKFvyfmQizsrMV7d pyHLpNNIt6StqO1L9OUpL5VLu2rr5cI7AQoCJqpLRMRgx+hS0WdkwSMtddVxseLFdo9Lfx mPmAsF268ajg4eRjhEnf3Jp6NbGqkzjWCUqWZBIHARGMVrgzuzbvxO3v2T6prPg8vwwOck gxbcF0D2b4bPWSe0303AfoKdFS7a9HEHTdeQbW2a36Wc7H6woMzXc1aIn38+Cw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732379977; a=rsa-sha256; cv=none; b=xrDANou/JYMFBXQQQQY0HcVFXshja6+SmYw97WJuavt3iPGCG/puzPd/iRnMnbph0geUIf puJ2CnNsXrugh82GEcfiVeHS/1P0r0Y+wZylX1PEGagnOjVqaDetsxnBum8vGtyrX/kjfa h74d9rlJmvGJnzd5tBRWOmXs7Ti1LueD7x2rDqoV12xdKRXy9RnrdT28odWeDGMMV4NFXg NykN0qTZmxez5Z5XJTgTyGZGIwTODiRsb4KhW6c3ADyAR7GOgU15WOKNdiJP31Gqrd8lVb ottnoaYzEwzuTDphBrCV3m9AfjkPWwG4dl319EtqQNP3WVywoylClrqqdVTiuQ== 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 4Xwd3Y5dh8zH4j; Sat, 23 Nov 2024 16:39:37 +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 4ANGdbNh036173; Sat, 23 Nov 2024 16:39:37 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4ANGdbK1036170; Sat, 23 Nov 2024 16:39:37 GMT (envelope-from git) Date: Sat, 23 Nov 2024 16:39:37 GMT Message-Id: <202411231639.4ANGdbK1036170@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: b4c700fa7cca - main - 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/main X-Git-Reftype: branch X-Git-Commit: b4c700fa7cca7d3e4da84dd417fc3efc42788a52 Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=b4c700fa7cca7d3e4da84dd417fc3efc42788a52 commit b4c700fa7cca7d3e4da84dd417fc3efc42788a52 Author: John Baldwin AuthorDate: 2024-11-23 16:39:02 +0000 Commit: John Baldwin CommitDate: 2024-11-23 16:39:02 +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 --- 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 2dbe1072aa83..32ddcaa9f643 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -2591,7 +2591,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); @@ -5759,17 +5765,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)) {