From nobody Tue Jan 20 12:05:46 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 4dwQyk2WZWz6PT8N for ; Tue, 20 Jan 2026 12:06:06 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) (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 4dwQyk0m0Lz3Sty for ; Tue, 20 Jan 2026 12:06:06 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-81f3fcdb556so2435832b3a.2 for ; Tue, 20 Jan 2026 04:06:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1768910758; cv=none; d=google.com; s=arc-20240605; b=NAAkPCotuKSdL5HHA6W+EvH3eXwaX6iDlIyKfY6g1AFyRb8tXGra2BQ6j1r5BeFw5G q3fL+Y9/h2HI2ylZqICguLSfGXlc/faebLJPLldMPhvU18SeNQZgIe5lZYhYzwUC1pZu dIQUbVGlHOWmWjIT806DjNUON5fcjglc0YlEuFhAr+E822umhTjNhfwaA4VqJ+Z/Jbv0 aky8PkjBCKbPrVZ+eg5AItYXz0UkPNEkYLnwvdcoD9c5UPpgPCGafs9Z7MKNo1A+JMhk p13DpRkwsfXi19VT/1zhsCATGZqWCiWohPXyRE9Tde3p27gCQxcHbaVvbyyrWseKw8uS T3Rg== 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=pJccDmwq4zsuhKwo+BHIY+HUzUaCVg06MJyBkxkNOGA=; fh=BGnlq/Bnk5yuHoIOHmeKjbe0R0Wn0sUc6aL/mCAMBkg=; b=YxY6zzyKM1C53gquM4r7ArUAJiaQt5iME0yGW8BlwmqXumyCxA4bQFhH8+RImRYHNt ZjjNYAkEO3k2iYwAaj1q5r1lQIvAAlzI6rQ5h0oRZzOPFJZjAbt0HXVshz6sZcpQoNfe uxufqAlBXBLD4XQ9ttq28pX2IwOkVeU90op+bNHTQC71m03u39kWsjwVUrt6kFBIxokG n8e6EbqlwAIP3DWbDyKg1BMKqxerJyvNmVwZzNJGb6+enXAxgNcZnJXQ4dfhBziGf3jA zMtaza0TbWHHQqdss5WHOD+WPEgp4Uq8Jys82CrhB2GAxh2vIWCf3hQ3BINc7vy0JVgR uL9Q==; 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=1768910758; x=1769515558; 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=pJccDmwq4zsuhKwo+BHIY+HUzUaCVg06MJyBkxkNOGA=; b=iW5Jrdtd68OqvlRQeJ5mitiMjMSfkNcQSUOkucVi/irXW7Xv+ZM3JtJXM/YUonodU1 NXG9XTrhHkDG07LwLhaPX9/c5a59aM/2MEYrwwcLVP0y464fnAv4JucIGBlkKQy4y/Xe aWaPIqARVICysMjUh+bn8gyhy0nWi3Pcd4WmfhyUhlZJ2Aohixv7eJJpEiPWNTX8sFir Ept4OP1lp3Ken/IjjA8Hjv8VdyPZOrRBgg8c+U0xo2NCj1+XiBYqzreGzQVlfSCvh1L7 tsD4l7vGtGDpqCK7EQhmLW2jpk8a24XXB9hjXpQf6Uu5umfv9WWke4bIfbUnDoVUcYSL Hfaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768910758; x=1769515558; 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=pJccDmwq4zsuhKwo+BHIY+HUzUaCVg06MJyBkxkNOGA=; b=vQlEZNYKFLVMc3MWSIa+Wu+Z3wU+q4FqUPsxGjZO09sOugBNrspCobCOJNoXWdi6jf rjUbxzPaVuC6xfZ0NMs6Eq3HQy5Usyf/q9qWlk+gKUgP2bNHtu1DnUmsdRJIwO+DZx7o sfvhGStWpEs4nNiuZJ4r17KVt+bK868cE3HB/n0xi5Ph338Wdqasrw6sOx3mVvD7Fzqy WxuboIP9qNQKanTOti+9c5N6LmdgVB1SzO2XK/PoihRySpjI1NOBtywagTQ8zWQxjh5s m3BzxPHE4ABfRyyZfh3Z+ZkDCk15JWMk8YihKts0bF9uBuBPxiYcmNvZyxgvb7XLNcJu OSkA== X-Gm-Message-State: AOJu0Yy+fWl9QnPTzRj5P18k6s8iAJ+f+6JDhMIv1eS1nX9KYczEqeRQ DfV0j2E21oWq9UwCd4ZBGH1WXqdA70LHZBLCnPOKdHdo+3XMckS3NimxFuLeNTOgb4ylbryeAf3 8Grx2oFSyhZcLvEKlCiYeP+ys3HultwM5HDRKwJL4wEQA49Nk4wqX X-Gm-Gg: AY/fxX7cRpx9KriAhNB1ccofYKxOBmsx00+UJK+SYKQ2hZcHwERpQI/a7sDsc/efhOt RBssBJOuFAIzfagDHHU3Y3YJxFyCKbHeXUh2B67uk6y3x8DN7JPS/Fc4FFRvh/D88ktjcfs9GeT TiwhwNjvAOGfCk4zCZU6S0IJhWYHn47KwobT7vvbz57y5PIaXIg2NljNB93CLR3cbRa76dIJJKr DUeXbKknMLjtZ3urhMLHodz8ayoqC5Q+nfvAQRx7pqDsENlToR3XRqN5PX/y/ODYhzNLsU= X-Received: by 2002:a05:6a20:6a20:b0:38d:ee68:2c55 with SMTP id adf61e73a8af0-38dfe590635mr14039805637.15.1768910758064; Tue, 20 Jan 2026 04:05:58 -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: <20260119183249.34199c88.freebsd-listen@fabiankeil.de> From: Warner Losh Date: Tue, 20 Jan 2026 05:05:46 -0700 X-Gm-Features: AZwV_QgFou4GYsgnKQkYJbZVD3LttVQlDLURttv-r3wpTbp4CrmTe45vc72enNA Message-ID: Subject: Re: "(ada0:ahcich0:0:0:0): Periph destroyed", followed by panic To: freebsd-hackers@freebsd.org Content-Type: multipart/alternative; boundary="0000000000005fc5440648d0a102" X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Queue-Id: 4dwQyk0m0Lz3Sty --0000000000005fc5440648d0a102 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 require= d > > > 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 didn'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 the bus (v= ia > > > xpt_compile_path). One common way for it to become NULL like this is > > > 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 the > > 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@entry= =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=3D0xfffff8000625638= 0, > 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@e= ntry=3D reading variable: value is not available>) 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<= error > reading variable: value is not available>, com=3D, com@entry= =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=3D215746= 2530, > data=3D0xfffff8010a7e1400, active_cred=3D, td=3D0xfffff8010a= 749740) > 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 "ahc= ich0") > 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, devq= =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 loc= k", > 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 for > 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 NU= LL */ > > 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 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 calls 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 (the race is already lost before we get into simfree sleep though). If the above is the confirmed race you are provoking in your testing (which arguably is 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. Warner > Fabian > > [0] < > https://www.fabiankeil.de/sourcecode/electrobsd/sys-dev-ahci-Don-t-try-to= -detach-an-already-detached-device-20260119.diff > > > > --0000000000005fc5440648d0a102 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


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.
--0000000000005fc5440648d0a102--