jail_attach_jd passed PD_DEREF to do_jail_attach, assuming it would take care of freeing the held prison. This is not true, as do_jail_attach immediately cleared that flag, leaving the jail stock in dying state when it is later removed. This is largely a documentation problem: add a comment block to do_jail_attach that lays out what it expects and what it returns. And now sys_jail_attach_fd does the right thing, calling prison_deref itself to remove its reference.
Also relax do_jail_attach's requirement, as a locked prison isn't necessary unless the lock is the only hold (it will just hold and unlock). This allows kern_jail_set (which holds a reference) to skip the prison_lock_xlock call.