From 0fee55b8725e6069e90528504d14abf4cbdcd240 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Mon, 19 Jan 2026 12:02:45 +0100 Subject: [PATCH] sys/dev/ahci: Don't try to detach an already detached device Prevents: (kgdb) where #0 __curthread () at /usr/src/sys/amd64/include/pcpu_aux.h:57 #1 doadump (textdump=textdump@entry=1) at /usr/src/sys/kern/kern_shutdown.c:405 #2 0xffffffff80a20360 in kern_reboot (howto=260) at /usr/src/sys/kern/kern_shutdown.c:523 #3 0xffffffff80a20869 in vpanic (fmt=0xffffffff80f8238a "%s", ap=ap@entry=0xfffffe00a2de6930) at /usr/src/sys/kern/kern_shutdown.c:967 #4 0xffffffff80a206a3 in panic (fmt=) at /usr/src/sys/kern/kern_shutdown.c:891 #5 0xffffffff80ec27c0 in trap_fatal (frame=, eva=) at /usr/src/sys/amd64/amd64/trap.c:1000 #6 0xffffffff80ec27c0 in trap_pfault (frame=0xfffffe00a2de69b0, usermode=false, signo=, ucode=) #7 #8 0xffffffff80369a66 in xpt_async (async_code=256, path=0xfffff8000606eb60, async_arg=0x0) at /usr/src/sys/cam/cam_xpt.c:4321 #9 0xffffffff804b8d23 in ahci_ch_detach (dev=0xfffff800039d7800) at /usr/src/sys/dev/ahci/ahci.c:952 #10 0xffffffff80a5d050 in DEVICE_DETACH (dev=0xfffff800039d7800) at ./device_if.h:234 #11 device_detach (dev=dev@entry=0xfffff800039d7800) at /usr/src/sys/kern/subr_bus.c:2712 #12 0xffffffff80a64cc2 in devctl2_ioctl (cdev=, cmd=, data=0xfffff80108617700 "ahcich0", fflag=, td=) at /usr/src/sys/kern/subr_bus.c:5843 #13 0xffffffff808cd19b in devfs_ioctl (ap=0xfffffe00a2de6c58) at /usr/src/sys/fs/devfs/devfs_vnops.c:963 #14 0xffffffff80b25348 in VOP_IOCTL (vp=, command=, data=, fflag=, cred=, td=) at ./vnode_if.h:633 #15 0xffffffff80b25348 in vn_ioctl (fp=, com=0, data=0x2, active_cred=0x0, td=0xfffffe0012063d00) #16 0xffffffff808cd80e in devfs_ioctl_f (fp=0xfffff8000606eb60, com=0, data=0x2, cred=0x0, td=0xfffffe0012063d00) at /usr/src/sys/fs/devfs/devfs_vnops.c:894 #17 0xffffffff80a93556 in fo_ioctl (fp=0xfffff8000cd2feb0, com=0, data=0xfffff80108617700, active_cred=0x0, td=0xfffff8000cdc4740) at /usr/src/sys/sys/file.h:379 #18 kern_ioctl (td=td@entry=0xfffff8000cdc4740, fd=, com=0, com@entry=2157462530, data=data@entry=0xfffff80108617700 "ahcich0") at /usr/src/sys/kern/sys_generic.c:810 #19 0xffffffff80a93267 in sys_ioctl (td=, uap=) at /usr/src/sys/kern/sys_generic.c:715 #20 0xffffffff80ec3107 in syscallenter (td=0xfffff8000cdc4740) at /usr/src/sys/amd64/amd64/../../kern/subr_syscall.c:193 #21 amd64_syscall (td=0xfffff8000cdc4740, traced=0) at /usr/src/sys/amd64/amd64/trap.c:1241 #22 #23 0x00000047b214757a in ?? () Backtrace stopped: Cannot access memory at address 0x47af775a58 (kgdb) f 8 #8 0xffffffff80369a66 in xpt_async (async_code=256, path=0xfffff8000606eb60, async_arg=0x0) at /usr/src/sys/cam/cam_xpt.c:4321 4321 xpt_freeze_simq(path->bus->sim, 1); (kgdb) l 4316 } 4317 if (path != NULL) { 4318 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD) 4319 xpt_freeze_devq(path, 1); 4320 else 4321 xpt_freeze_simq(path->bus->sim, 1); 4322 } 4323 xpt_action(ccb); 4324 } 4325 (kgdb) p *path $1 = {periph = 0x0, bus = 0x0, target = 0x0, device = 0x0} Obtained from: ElectroBSD --- sys/dev/ahci/ahci.c | 15 ++++++++++++--- sys/dev/ahci/ahci.h | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/sys/dev/ahci/ahci.c b/sys/dev/ahci/ahci.c index 80cbdefd1e29..7f2b6e2be079 100644 --- a/sys/dev/ahci/ahci.c +++ b/sys/dev/ahci/ahci.c @@ -729,16 +729,22 @@ ahci_attached(device_t dev, struct ahci_channel *ch) mtx_unlock(&ctlr->ch_mtx); } -void +int ahci_detached(device_t dev, struct ahci_channel *ch) { struct ahci_controller *ctlr = device_get_softc(dev); + int device_detached = 0; mtx_lock(&ctlr->ch_mtx); mtx_lock(&ch->mtx); - ctlr->ch[ch->unit] = NULL; + if (ctlr->ch[ch->unit] != NULL) { + ctlr->ch[ch->unit] = NULL; + device_detached = 1; + } mtx_unlock(&ch->mtx); mtx_unlock(&ctlr->ch_mtx); + + return (device_detached); } struct ahci_channel * @@ -947,7 +953,10 @@ ahci_ch_detach(device_t dev) { struct ahci_channel *ch = device_get_softc(dev); - ahci_detached(device_get_parent(dev), ch); + if (!ahci_detached(device_get_parent(dev), ch)) { + device_printf(dev, "Not detaching already detached device.\n"); + return (ENXIO); + } mtx_lock(&ch->mtx); xpt_async(AC_LOST_DEVICE, ch->path, NULL); /* Forget about reset. */ diff --git a/sys/dev/ahci/ahci.h b/sys/dev/ahci/ahci.h index 04d0cccc31f9..94549ed62a22 100644 --- a/sys/dev/ahci/ahci.h +++ b/sys/dev/ahci/ahci.h @@ -671,6 +671,6 @@ void ahci_free_mem(device_t dev); /* Functions to allow AHCI EM to access other channels. */ void ahci_attached(device_t dev, struct ahci_channel *ch); -void ahci_detached(device_t dev, struct ahci_channel *ch); +int ahci_detached(device_t dev, struct ahci_channel *ch); struct ahci_channel * ahci_getch(device_t dev, int n); void ahci_putch(struct ahci_channel *ch); -- 2.52.0