From nobody Tue Jan 20 12:10:29 2026 X-Original-To: freebsd-hackers@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 4dwR48698kz6PTDQ for ; Tue, 20 Jan 2026 12:10:48 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "WR4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dwR476Lshz3Tsw for ; Tue, 20 Jan 2026 12:10:47 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Authentication-Results: mx1.freebsd.org; dkim=pass header.d=bsdimp-com.20230601.gappssmtp.com header.s=20230601 header.b=h0OEDEqd; dmarc=none; spf=none (mx1.freebsd.org: domain of wlosh@bsdimp.com has no SPF policy when checking 2607:f8b0:4864:20::531) smtp.mailfrom=wlosh@bsdimp.com; arc=pass ("google.com:s=arc-20240605:i=1") Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-c0ec27cad8cso2235332a12.1 for ; Tue, 20 Jan 2026 04:10:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1768911041; cv=none; d=google.com; s=arc-20240605; b=XR1gNqkJ6PPY2uotWoFudfWLlHyyliewUTbGmXUETOhhkCuOGMBkcrQlgrDdvMyFVl mFcvI88RqVRvvHE+T01Ylg8hbwjVkh6cPXaZgYTO7opzWgUPuJ0z2Dh0N7p/K3922Nqv FqSXf/NutNvgPU6J86OEAQNXCWfv18vR6gi3CdOws0njqYJdw0AduR2OiZCCSE/bwAnB 9vWfqNVto8AnNSSqWqi5opse2459O0wX05HLnVjr33iFQdrJdxXFg4Pl+1YH52t3aqYe yTK5buf/JYy4bFeUGeVlf3FKTG8uSVBCd55014HQ2sbpYmcJfbzpceYay69vHcuTaOZc sOmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=EKXAB7jgHoFvY1ViRSfFnpA3yru+su8W7nZy9IPcAkE=; fh=BGnlq/Bnk5yuHoIOHmeKjbe0R0Wn0sUc6aL/mCAMBkg=; b=O0Ud7bH7Wj+Z8Sn/JbkStOXBqHk80esJSuvIX9zk9GK4eAogaIIXBdiE5OF+w9iZTF 7ZSNrogJg7xL/gM/YSLddoD3cNBEiP9f4AoOFThaGIc3cJOQzASNEk042FSTHdqityYv UyP3sgJhn587jggzK1+mhFmehIeZfoJnFYO4OeDvyASt8IAaMGoGO9CC2jlixR1LeEdB 6R78eSy3TJ5Q+C2RB6n/ov7b67JKuuUceiA8BccNl70dp55BJeAU/Qg50YHTqKk599fR +pnbLJVk0J6hbFY3Pl0BCSeBeVldPmnTnhLTu2nLfouJAWoHj763GIgL/SDavL4KXWnf AkWA==; darn=freebsd.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20230601.gappssmtp.com; s=20230601; t=1768911041; x=1769515841; darn=freebsd.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=EKXAB7jgHoFvY1ViRSfFnpA3yru+su8W7nZy9IPcAkE=; b=h0OEDEqdFPb2/MvsyDw8zh/3TIG9xGftNkpPBVURMRggY1u6+6HY71SxMnRqM6LArd 7KTTGMSfjPSE0EluMv9I8pdX9tFSO6KwNNPKJ9XXbDu1qC7BFr9yttAtJFPL4K8SuU1m 8NXo0tOkkLNxFCoi5Ex6pJz5ih5kOHT1pdljkAQnZLygoduhFdh9jg7NpbfhiMLyXr98 At0e8N49NeBBRvpE2bKYgzoI/FR/Wl2bIRsqvzdYgvn0k+Fd82KVhTWewydFXCHCO72p /iCC+lRfiIwtvfRt8RVFpndE9Gbv6z3VsC/OzsGL8+6lQIrM63E1fEmzwlkS7DVM/Xh6 PUqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768911041; x=1769515841; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EKXAB7jgHoFvY1ViRSfFnpA3yru+su8W7nZy9IPcAkE=; b=KsHsqHElwXnaQm98bhqbqdlPCLy8VE+HQXoKLM+w3ndMOC3QVWrjqYCg6v94p0wf01 X4KoCRiIpCjsJxy3jYr3+X9ir3IPT4tYoEz0SFA6/oyCt0LwNysHvRhx/31ev3xMQtIt dvJiDv+m47wIWq0QKE8aioZ6Z1X5Yk+tSCSt8/CvKR6roDcjfBM6yhQ+4hjrmNW6bavT OfMwteuPY72AZr3WpwCeWlAiFdD/rwmxOM5TEJKfP/zujOEFC7mJ+U0ZgH0HHEKEzJd1 R83QKh2scaYM3xIO5BAiQAJ/k2vcT9rukdlwPT5LAPtcGEATlOEPMSb6Q+ywQhHzOyic uMzQ== X-Gm-Message-State: AOJu0YzIVP0fETxhte0STeGKuPvJs7729OEglZWM+jXy099s6ZwKY9M6 TrQPVUTrJoa4BfaAzLn8NLjlT9Yl252AnwivyalppqSZKRjtQsTyb9OdN1ft3J/VwPCzYi3wopN P8AX+aXW9rOjZouDOltSTMXKT+JWhQtVegY4BcukxRXdfdwgGDPKL6p0= X-Gm-Gg: AZuq6aI2AmMfh8ntgMyoDpzozEaSPJAhxN3oDlhLRFP3rc+33qU3egM9EhK7VTXClDz Hbj6Aw+iy2JEpeewVyILxRIYh5dau+pSbZHXqRjIFC8gGhnaTdlL5kYsNuT/lQrKMxyZBrcbVka 7n/kVMzxLo6DUD0RmX+aZnt1J5I3McMftSEgBUPeTdjVcUzc7oQ6wYRv9DaGiZIC/bIMrWpHTRj Lg6KJpDRwLnB2gNwNR21e+Rsd0ijD8cA6gKlTpNxlSQu0lcT+NY4GpKvmUZUFY98qLitWw= X-Received: by 2002:a17:90b:38ca:b0:34c:3501:d11c with SMTP id 98e67ed59e1d1-352c408870dmr1441621a91.37.1768911040483; Tue, 20 Jan 2026 04:10:40 -0800 (PST) List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@FreeBSD.org MIME-Version: 1.0 References: <20260117175331.4b5eb147.freebsd-listen@fabiankeil.de> <20260118122005.06288476.freebsd-listen@fabiankeil.de> <20260119183249.34199c88.freebsd-listen@fabiankeil.de> In-Reply-To: From: Warner Losh Date: Tue, 20 Jan 2026 05:10:29 -0700 X-Gm-Features: AZwV_QhA2vSnJZUIzZhhN6TjXdvCC6kkhzkSEPqGrpORi7MGo99l8Z7848IWS9w Message-ID: Subject: Re: "(ada0:ahcich0:0:0:0): Periph destroyed", followed by panic To: freebsd-hackers@freebsd.org Content-Type: multipart/alternative; boundary="00000000000035238a0648d0b25f" X-Spamd-Bar: --- X-Spamd-Result: default: False [-3.99 / 15.00]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; ARC_ALLOW(-1.00)[google.com:s=arc-20240605:i=1]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.99)[-0.992]; FORGED_SENDER(0.30)[imp@bsdimp.com,wlosh@bsdimp.com]; R_DKIM_ALLOW(-0.20)[bsdimp-com.20230601.gappssmtp.com:s=20230601]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; RCVD_COUNT_ONE(0.00)[1]; MIME_TRACE(0.00)[0:+,1:+,2:~]; RCPT_COUNT_ONE(0.00)[1]; MISSING_XM_UA(0.00)[]; R_SPF_NA(0.00)[no SPF record]; MLMMJ_DEST(0.00)[freebsd-hackers@freebsd.org]; RCVD_IN_DNSWL_NONE(0.00)[2607:f8b0:4864:20::531:from]; FROM_NEQ_ENVFROM(0.00)[imp@bsdimp.com,wlosh@bsdimp.com]; FROM_HAS_DN(0.00)[]; DMARC_NA(0.00)[bsdimp.com]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_TLS_LAST(0.00)[]; DKIM_TRACE(0.00)[bsdimp-com.20230601.gappssmtp.com:+] X-Rspamd-Queue-Id: 4dwR476Lshz3Tsw --00000000000035238a0648d0b25f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Jan 20, 2026 at 5:05=E2=80=AFAM Warner Losh wrote: > > > On Mon, Jan 19, 2026 at 10:52=E2=80=AFAM Fabian Keil > wrote: > >> Fabian Keil wrote on 2026-01-18 at >> 12:20:05: >> >> > Warner Losh wrote on 2026-01-17 at 10:29:44: >> >> > > You should never pass NULL as the path to xpt_async(). It's a requir= ed >> > > element of the call: the path you are sending the async event to. An= d >> the >> > > path must have either a device or a bus, which that traceback didn't >> have. >> > > Now, how did it come to pass that the bus and/or device was NULL her= e? >> > > That's where the bug is... xpt_create_path always fills in the bus >> (via >> > > xpt_compile_path). One common way for it to become NULL like this i= s >> > > xpt_release_path is called on it. Maybe you can use dtrace to find >> where >> > > that's coming to pass? I'm not seeing anything super quick in the >> code, but >> > > I'm just doing a quick analysis to answer this email + past >> experience... >> >> > While I have not yet been able to reproduce the exact same panic on th= e >> > same system, I ran into a related issue while trying to reproduce the >> > problem in a byhve vm. >> > >> > Running ahcich0-detacher.sh: >> [...] >> > in two terminals at the same time within seconds reliably results in: >> [...] >> > (kgdb) f 8 >> > #8 0xffffffff80369a66 in xpt_async (async_code=3D256, >> path=3D0xfffff8000606eb60, async_arg=3D0x0) at /usr/src/sys/cam/cam_xpt.= c:4321 >> > 4321 xpt_freeze_simq(path->bus->sim, 1); >> [...] >> > (kgdb) p *path >> > $1 =3D {periph =3D 0x0, bus =3D 0x0, target =3D 0x0, device =3D 0x0} >> >> I've come up with a patch that seems to prevent the problem [0]. >> > > I'd use a bool for that patch, not an int. How often do you see 'not > detaching detached device?' It seems like a reasonable roadblock in the > surface... especially since it looks like it can only be true when we > succeed in attach. > > >> > I also once managed to get devctl sleeping "forever" by running only >> > one ahcich0-detacher.sh instance, but haven't been able to reproduce >> > this yet. >> >> Now that the panic is prevented this issue can be reliably triggered >> by running ahcich0-detacher.sh in parallel for a while. >> >> (kgdb) where >> #0 sched_switch (td=3Dtd@entry=3D0xfffff8010a749740, flags=3Dflags@entr= y=3D259) >> at /usr/src/sys/kern/sched_ule.c:2437 >> #1 0xffffffff80a34da3 in mi_switch (flags=3Dflags@entry=3D259) at >> /usr/src/sys/kern/kern_synch.c:548 >> #2 0xffffffff80a88233 in sleepq_switch (wchan=3D0xfffff80006256380, >> pri=3D) at /usr/src/sys/kern/subr_sleepqueue.c:608 >> #3 0xffffffff80a880fc in sleepq_wait (wchan=3D, >> pri=3D) at /usr/src/sys/kern/subr_sleepqueue.c:659 >> #4 0xffffffff80a34311 in _sleep (ident=3Dident@entry=3D0xfffff800062563= 80, >> lock=3Dlock@entry=3D0xfffffe058a9f9400, priority=3Dpriority@entry=3D43, >> wmesg=3D0xffffffff8105a969 "simfree", sbt=3Dsbt@entry=3D0, pr=3Dpr@entry= =3D0, >> flags=3D256) >> at /usr/src/sys/kern/kern_synch.c:223 >> #5 0xffffffff803676c0 in cam_sim_free (sim=3D0xfffff80006256380, >> free_devq=3Dfree_devq@entry=3D1) at /usr/src/sys/cam/cam_sim.c:157 >> #6 0xffffffff804bc6cd in ahci_ch_detach (dev=3D0xfffff800060a2800, >> dev@entry=3D) at >> /usr/src/sys/dev/ahci/ahci.c:969 >> #7 0xffffffff80a63404 in DEVICE_DETACH (dev=3D0xfffff800060a2800) at >> ./device_if.h:234 >> #8 device_detach (dev=3Ddev@entry=3D0xfffff800060a2800) at >> /usr/src/sys/kern/subr_bus.c:2712 >> #9 0xffffffff80a6b307 in devctl2_ioctl (cdev=3D, >> cmd=3D, cmd@entry=3D> available>, data=3D0xfffff8010a7e1400 "ahcich0", >> data@entry=3D, >> fflag=3D, td=3D0xfffff8010a749740, td@entry=3D> variable: value is not available>) at /usr/src/sys/kern/subr_bus.c:5843 >> #10 0xffffffff808d3da1 in devfs_ioctl (ap=3D0xfffffe058835bc58) at >> /usr/src/sys/fs/devfs/devfs_vnops.c:963 >> #11 0xffffffff80f4a9ed in VOP_IOCTL_APV (vop=3D0xffffffff8149bd28 >> , a=3Da@entry=3D0xfffffe058835bc58) at vnode_if.c:1229 >> #12 0xffffffff80b3b030 in VOP_IOCTL (vp=3D, >> command=3D, data=3D, fflag=3D, >> cred=3D, td=3D) at ./vnode_if.h:637 >> #13 0xffffffff80b3b030 in vn_ioctl (fp=3D, com=3D> out>, data=3D, active_cred=3D, td=3D) >> #14 0xffffffff808d446e in devfs_ioctl_f (fp=3D, fp@entry=3D= > reading variable: value is not available>, com=3D, com@entr= y=3D> reading variable: value is not available>, data=3D, >> data@entry=3D, >> cred=3D, cred@entry=3D> available>, td=3D, td@entry=3D> not available>) >> at /usr/src/sys/fs/devfs/devfs_vnops.c:894 >> #15 0xffffffff80aa1ef1 in fo_ioctl (fp=3D0xfffff8000fccadc0, >> com=3D2157462530, data=3D0xfffff8010a7e1400, active_cred=3D= , >> td=3D0xfffff8010a749740) at /usr/src/sys/sys/file.h:379 >> #16 kern_ioctl (td=3Dtd@entry=3D0xfffff8010a749740, fd=3D= , >> com=3Dcom@entry=3D2157462530, data=3Ddata@entry=3D0xfffff8010a7e1400 "ah= cich0") >> at /usr/src/sys/kern/sys_generic.c:810 >> #17 0xffffffff80aa1be3 in sys_ioctl (td=3D0xfffff8010a749740, >> uap=3D0xfffff8010a749b40) at /usr/src/sys/kern/sys_generic.c:715 >> #18 0xffffffff80ef6d9a in syscallenter (td=3D0xfffff8010a749740) at >> /usr/src/sys/amd64/amd64/../../kern/subr_syscall.c:193 >> #19 amd64_syscall (td=3D0xfffff8010a749740, traced=3D0) at >> /usr/src/sys/amd64/amd64/trap.c:1241 >> #20 >> #21 0x0000225fa814557a in ?? () >> Backtrace stopped: Cannot access memory at address 0x225fa68c9038 >> (kgdb) f 5 >> #5 0xffffffff803676c0 in cam_sim_free (sim=3D0xfffff80006256380, >> free_devq=3Dfree_devq@entry=3D1) at /usr/src/sys/cam/cam_sim.c:157 >> 157 error =3D msleep(sim, mtx, PRIBIO, "simfree", 0)= ; >> (kgdb) p *sim >> $1 =3D {sim_action =3D 0xffffffff804bcc80 , sim_poll =3D >> 0xffffffff804bd360 , sim_name =3D 0xffffffff8104741f "ahcich", >> softc =3D 0xfffffe058a9f8000, mtx =3D 0xfffffe058a9f9400, links =3D {tqe= _next =3D >> 0x0, >> tqe_prev =3D 0x0}, path_id =3D 0, unit_number =3D 0, bus_id =3D 0, >> max_tagged_dev_openings =3D 32, max_dev_openings =3D 2, flags =3D 0, dev= q =3D >> 0xfffff80005d94880, refcount =3D 1} >> > > refcount=3D1 means that a reference to the sim wasn't properly released. > We're sleeping here waiting for that to happen. You'll need to find that > ref leak. > > >> (kgdb) p *mtx >> $2 =3D {lock_object =3D {lo_name =3D 0xffffffff81070545 "AHCI channel lo= ck", >> lo_flags =3D 16973824, lo_data =3D 0, lo_witness =3D 0xfffff802bfd7f700}= , >> mtx_lock =3D 0} >> (kgdb) l >> 152 mtx_assert(mtx, MA_OWNED); >> 153 } >> 154 KASSERT(sim->refcount >=3D 1, ("sim->refcount >=3D 1")); >> 155 sim->refcount--; >> 156 if (sim->refcount > 0) { >> 157 error =3D msleep(sim, mtx, PRIBIO, "simfree", 0)= ; >> 158 KASSERT(error =3D=3D 0, ("invalid error value fo= r >> msleep(9)")); >> 159 } >> 160 KASSERT(sim->refcount =3D=3D 0, ("sim->refcount =3D=3D 0= ")); >> 161 if (mtx =3D=3D &cam_sim_free_mtx) /* sim->mtx =3D=3D N= ULL */ >> >> I'm not sure what to do about this. >> > > Sadly, you have to find the leak, or what's holding onto the sim. You wil= l > have to track, likely with dtrace since I don't see any cam debug in it, > calls to cam_sim_hold and cam_sim_release. They are mismatched for some > reason, and you'll have to hunt that down. It looks like they are called > from only one place, though, and that's xpt_bus_register and > xpt_release_bus respectively. You'll need to see what path can cause > xpt_bus_register to be called, but then fail to call the matching > xpt_release_bus. It doesn't look like an error in ahci_ch_attach (it call= s > xpt_bus_deregister which drops the reference with a call to > xpt_release_bus). But it looks like if you are putting the NULL chek and > short circuit, you might have two threads racing: > (1) creation path creating the device and it's past xpt_bus_register, but > not yet to ahci_attached -- there's no giant dropping sleeps here > (2) in the detach path *somehow* that gets to ahci_ch_detach in this > window, maybe because the locking of the channel and updating the channel > are with different mutexes, allowing the race. > This also suggests that the multiple different locks may not be held long > enough. > > Now in theory we take out Giant in the newbus code to prevent this. > However, we drop Giant sometimes when we sleep which can lead to races (t= he > race is already lost before we get into simfree sleep though). If the abo= ve > is the confirmed race you are provoking in your testing (which arguably i= s > a bit artificial, but maybe not SO artificial). It might also be the > absence of a bus_topo_lock() in one of these paths. > > Hopefully that will give you more to chew on and hopefully find the next > layer or two of this onion. > Maybe add a panic() to the case where the channel is NULL. There you know you are actively racing, if that theory holds. Then you can see what other thread is in the attach path and that might help chase it one step further. Warner > Warner > > >> Fabian >> >> [0] < >> https://www.fabiankeil.de/sourcecode/electrobsd/sys-dev-ahci-Don-t-try-t= o-detach-an-already-detached-device-20260119.diff >> > >> >> --00000000000035238a0648d0b25f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Tue, Jan 20,= 2026 at 5:05=E2=80=AFAM Warner Losh <= imp@bsdimp.com> wrote:


On Mon, Jan 19, 2026 at= 10:52=E2=80=AFAM Fabian Keil <freebsd-listen@fabiankeil.de> wrote:
Fabian Keil <freebsd-listen@= fabiankeil.de> wrote on 2026-01-18 at 12:20:05:

> Warner Losh <im= p@bsdimp.com> wrote on 2026-01-17 at 10:29:44:

> > You should never pass NULL as the path to xpt_async(). It's a= required
> > element of the call: the path you are sending the async event to.= And the
> > path must have either a device or a bus, which that traceback did= n't have.
> > Now, how did it come to pass that the bus and/or device was NULL = here?
> > That's where the bug is... xpt_create_path always fills in th= e bus (via
> > xpt_compile_path).=C2=A0 One common way for it to become NULL lik= e this is
> > xpt_release_path is called on it. Maybe you can use dtrace to fin= d where
> > that's coming to pass? I'm not seeing anything super quic= k in the code, but
> > I'm just doing a quick analysis to answer this email + past e= xperience...

> While I have not yet been able to reproduce the exact same panic on th= e
> same system, I ran into a related issue while trying to reproduce the<= br> > problem in a byhve vm.
>
> Running ahcich0-detacher.sh:
[...]
> in two terminals at the same time within seconds reliably results in:<= br> [...]
> (kgdb) f 8
> #8=C2=A0 0xffffffff80369a66 in xpt_async (async_code=3D256, path=3D0xf= ffff8000606eb60, async_arg=3D0x0) at /usr/src/sys/cam/cam_xpt.c:4321
> 4321=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0xpt_freeze_simq(path->bus->sim,= 1);
[...]
> (kgdb) p *path
> $1 =3D {periph =3D 0x0, bus =3D 0x0, target =3D 0x0, device =3D 0x0}
I've come up with a patch that seems to prevent the problem [0].

I'd use a bool for that patch, not an int= . How often do you see 'not detaching detached device?' It seems li= ke a reasonable roadblock in the surface... especially since it looks like = it can only be true when we succeed in attach.
=C2=A0
> I also once managed to get devctl sleeping "forever" by runn= ing only
> one ahcich0-detacher.sh instance, but haven't been able to reprodu= ce
> this yet.

Now that the panic is prevented this issue can be reliably triggered
by running ahcich0-detacher.sh in parallel for a while.

(kgdb) where
#0=C2=A0 sched_switch (td=3Dtd@entry=3D0xfffff8010a749740, flags=3Dflags@en= try=3D259) at /usr/src/sys/kern/sched_ule.c:2437
#1=C2=A0 0xffffffff80a34da3 in mi_switch (flags=3Dflags@entry=3D259) at /us= r/src/sys/kern/kern_synch.c:548
#2=C2=A0 0xffffffff80a88233 in sleepq_switch (wchan=3D0xfffff80006256380, p= ri=3D<optimized out>) at /usr/src/sys/kern/subr_sleepqueue.c:608
#3=C2=A0 0xffffffff80a880fc in sleepq_wait (wchan=3D<unavailable>, pr= i=3D<unavailable>) at /usr/src/sys/kern/subr_sleepqueue.c:659
#4=C2=A0 0xffffffff80a34311 in _sleep (ident=3Dident@entry=3D0xfffff8000625= 6380, lock=3Dlock@entry=3D0xfffffe058a9f9400, priority=3Dpriority@entry=3D4= 3, wmesg=3D0xffffffff8105a969 "simfree", sbt=3Dsbt@entry=3D0, pr= =3Dpr@entry=3D0, flags=3D256)
=C2=A0 =C2=A0 at /usr/src/sys/kern/kern_synch.c:223
#5=C2=A0 0xffffffff803676c0 in cam_sim_free (sim=3D0xfffff80006256380, free= _devq=3Dfree_devq@entry=3D1) at /usr/src/sys/cam/cam_sim.c:157
#6=C2=A0 0xffffffff804bc6cd in ahci_ch_detach (dev=3D0xfffff800060a2800, de= v@entry=3D<error reading variable: value is not available>) at /usr/s= rc/sys/dev/ahci/ahci.c:969
#7=C2=A0 0xffffffff80a63404 in DEVICE_DETACH (dev=3D0xfffff800060a2800) at = ./device_if.h:234
#8=C2=A0 device_detach (dev=3Ddev@entry=3D0xfffff800060a2800) at /usr/src/s= ys/kern/subr_bus.c:2712
#9=C2=A0 0xffffffff80a6b307 in devctl2_ioctl (cdev=3D<optimized out>,= cmd=3D<unavailable>, cmd@entry=3D<error reading variable: value i= s not available>, data=3D0xfffff8010a7e1400 "ahcich0",
=C2=A0 =C2=A0 data@entry=3D<error reading variable: value is not availab= le>, fflag=3D<optimized out>, td=3D0xfffff8010a749740, td@entry=3D= <error reading variable: value is not available>) at /usr/src/sys/ker= n/subr_bus.c:5843
#10 0xffffffff808d3da1 in devfs_ioctl (ap=3D0xfffffe058835bc58) at /usr/src= /sys/fs/devfs/devfs_vnops.c:963
#11 0xffffffff80f4a9ed in VOP_IOCTL_APV (vop=3D0xffffffff8149bd28 <devfs= _specops>, a=3Da@entry=3D0xfffffe058835bc58) at vnode_if.c:1229
#12 0xffffffff80b3b030 in VOP_IOCTL (vp=3D<optimized out>, command=3D= <optimized out>, data=3D<optimized out>, fflag=3D<optimized = out>, cred=3D<optimized out>, td=3D<optimized out>) at ./vno= de_if.h:637
#13 0xffffffff80b3b030 in vn_ioctl (fp=3D<optimized out>, com=3D<o= ptimized out>, data=3D<unavailable>, active_cred=3D<unavailable= >, td=3D<unavailable>)
#14 0xffffffff808d446e in devfs_ioctl_f (fp=3D<unavailable>, fp@entry= =3D<error reading variable: value is not available>, com=3D<unavai= lable>, com@entry=3D<error reading variable: value is not available&g= t;, data=3D<unavailable>,
=C2=A0 =C2=A0 data@entry=3D<error reading variable: value is not availab= le>, cred=3D<unavailable>, cred@entry=3D<error reading variable= : value is not available>, td=3D<unavailable>, td@entry=3D<erro= r reading variable: value is not available>)
=C2=A0 =C2=A0 at /usr/src/sys/fs/devfs/devfs_vnops.c:894
#15 0xffffffff80aa1ef1 in fo_ioctl (fp=3D0xfffff8000fccadc0, com=3D21574625= 30, data=3D0xfffff8010a7e1400, active_cred=3D<unavailable>, td=3D0xff= fff8010a749740) at /usr/src/sys/sys/file.h:379
#16 kern_ioctl (td=3Dtd@entry=3D0xfffff8010a749740, fd=3D<optimized out&= gt;, com=3Dcom@entry=3D2157462530, data=3Ddata@entry=3D0xfffff8010a7e1400 &= quot;ahcich0") at /usr/src/sys/kern/sys_generic.c:810
#17 0xffffffff80aa1be3 in sys_ioctl (td=3D0xfffff8010a749740, uap=3D0xfffff= 8010a749b40) at /usr/src/sys/kern/sys_generic.c:715
#18 0xffffffff80ef6d9a in syscallenter (td=3D0xfffff8010a749740) at /usr/sr= c/sys/amd64/amd64/../../kern/subr_syscall.c:193
#19 amd64_syscall (td=3D0xfffff8010a749740, traced=3D0) at /usr/src/sys/amd= 64/amd64/trap.c:1241
#20 <signal handler called>
#21 0x0000225fa814557a in ?? ()
Backtrace stopped: Cannot access memory at address 0x225fa68c9038
(kgdb) f 5
#5=C2=A0 0xffffffff803676c0 in cam_sim_free (sim=3D0xfffff80006256380, free= _devq=3Dfree_devq@entry=3D1) at /usr/src/sys/cam/cam_sim.c:157
157=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0error =3D msleep(sim, mtx, PRIBIO, "simfree", 0);
(kgdb) p *sim
$1 =3D {sim_action =3D 0xffffffff804bcc80 <ahciaction>, sim_poll =3D = 0xffffffff804bd360 <ahcipoll>, sim_name =3D 0xffffffff8104741f "= ahcich", softc =3D 0xfffffe058a9f8000, mtx =3D 0xfffffe058a9f9400, lin= ks =3D {tqe_next =3D 0x0,
=C2=A0 =C2=A0 tqe_prev =3D 0x0}, path_id =3D 0, unit_number =3D 0, bus_id = =3D 0, max_tagged_dev_openings =3D 32, max_dev_openings =3D 2, flags =3D 0,= devq =3D 0xfffff80005d94880, refcount =3D 1}

refcount=3D1 means that a reference to the sim wasn't properly r= eleased. We're sleeping here waiting for that to happen. You'll nee= d to find that ref leak.
=C2=A0
(kgdb) p *mtx
$2 =3D {lock_object =3D {lo_name =3D 0xffffffff81070545 "AHCI channel = lock", lo_flags =3D 16973824, lo_data =3D 0, lo_witness =3D 0xfffff802= bfd7f700}, mtx_lock =3D 0}
(kgdb) l
152=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0mtx_assert(mtx, MA_OWNED);
153=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
154=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0KASSERT(sim->refcount= >=3D 1, ("sim->refcount >=3D 1"));
155=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sim->refcount--;
156=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (sim->refcount >= ; 0) {
157=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0error =3D msleep(sim, mtx, PRIBIO, "simfree", 0);
158=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0KASSERT(error =3D=3D 0, ("invalid error value for msleep(9)"= ;));
159=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
160=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0KASSERT(sim->refcount= =3D=3D 0, ("sim->refcount =3D=3D 0"));
161=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (mtx =3D=3D &cam_= sim_free_mtx)=C2=A0 =C2=A0/* sim->mtx =3D=3D NULL */

I'm not sure what to do about this.

Sadly, you have to find the leak, or what's holding onto the sim. You = will have to track, likely with dtrace since I don't see any cam debug = in it, calls to cam_sim_hold and cam_sim_release. They are mismatched for s= ome reason, and you'll have to hunt that down. It looks like they are c= alled from only one place, though, and that's xpt_bus_register and xpt_= release_bus respectively. You'll need to see what path can cause xpt_bu= s_register to be called, but then fail to call the matching xpt_release_bus= . It doesn't look like an error in ahci_ch_attach (it calls xpt_bus_der= egister which drops the reference with a call to xpt_release_bus). But it l= ooks like if you are putting the NULL chek and short circuit, you might hav= e two threads racing:
(1) creation path creating the device and i= t's past xpt_bus_register, but not yet to ahci_attached -- there's = no giant dropping=C2=A0sleeps here
(2) in the detach path *someho= w* that gets to ahci_ch_detach in this window, maybe because the locking of= the channel and updating the channel are with different mutexes, allowing = the race.
This also suggests that the multiple different locks ma= y not be held long enough.

Now in theory we take out Giant in the ne= wbus code to prevent this. However, we drop Giant sometimes when we sleep w= hich can lead to races (the race is already lost before we get into simfree= =C2=A0sleep though). If the above is the confirmed race you are provoking i= n your testing (which arguably is a bit artificial, but maybe not SO artifi= cial). It might also be the absence of a bus_topo_lock() in one of these pa= ths.

Hopefully that will give you more to chew on = and hopefully find the next layer or two of this onion.

Maybe add a panic() to the case where the ch= annel is NULL. There you know you are actively racing, if that theory holds= . Then you can see what other thread is in the attach path and that might h= elp chase it one step further.

Warner
= =C2=A0
--00000000000035238a0648d0b25f--