The alloc_resource methods of ahci, mvs and siis carve the registers of a channel (and for ahci of the enclosure management device) out of the controller's own rman with rman_reserve_resource(), which leaves the type of the new resource unset. Since 9dbf5b0e6876 (D44131) the release_resource methods select on rman_get_type(), so they return EINVAL for these resources and they are never released.
When the controller is detached, rman_fini() then fails with EBUSY and leaves the rman on the global list, while the softc containing it is freed. The next read of hw.bus.rman, for example by devinfo -r, uses the stale rman and panics in sysctl_rman().
Set the type when the resource is allocated. The three drivers share this code; the panic was seen and the fix tested with ahci only.
Fixes: 9dbf5b0e6876 ("new-bus: Remove the 'rid' and 'type' arguments from BUS_RELEASE_RESOURCE")
This was found on a keelOS host (FreeBSD stable/15), where an AHCI controller is detached at runtime to be handed to ppt and the device tree is read with devinfo right after. The analysis and the change were made with an AI assistant (Claude) under my direction; I checked the code paths against the source and ran the tests below on real hardware.
Signed-off-by: Wanpeng Qian <wanpengqian@gmail.com>
Sponsored by: keelos.dev