git: c8d63d63679f - main - nvme: fix panic if we boot w/o a namespace

From: Warner Losh <imp_at_FreeBSD.org>
Date: Wed, 18 Feb 2026 16:55:11 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=c8d63d63679fc0d8a67b33499a2a2edd444c8e2e

commit c8d63d63679fc0d8a67b33499a2a2edd444c8e2e
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2026-02-17 08:00:34 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2026-02-18 16:39:28 +0000

    nvme: fix panic if we boot w/o a namespace
    
    If we format a drive, and then crash, we'll come back up. nvme_sim_ns
    device won't attach because we don't have a namespace. Some drives (all?
    I couldn't find it in the standard) send an AER with a namespace change,
    which causes a NULL dereference because s_sim wasn't initialized because
    we didn't attach. So, if we get into the ns_changed routine, bail early
    if we didn't attach. We'll attach later, and deal with the ns correctly
    if it's really there, or not attach one if it's not.
    
    Sponsored by:           Netflix
---
 sys/dev/nvme/nvme_sim.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/sys/dev/nvme/nvme_sim.c b/sys/dev/nvme/nvme_sim.c
index 8b861cf3aa71..a59823847c39 100644
--- a/sys/dev/nvme/nvme_sim.c
+++ b/sys/dev/nvme/nvme_sim.c
@@ -405,6 +405,16 @@ nvme_sim_ns_added(device_t dev, struct nvme_namespace *ns)
 	struct nvme_sim_softc *sc = device_get_softc(dev);
 	union ccb *ccb;
 
+	/*
+	 * If we have no namespaces, then we both do not attach the nvme_sim_ns
+	 * device. And then get a ns changed AER as well to tell us about it
+	 * (which is how we get here). If there's no device attached, then
+	 * there's nothing to do. sc->s_sim will be NULL as well (since it
+	 * only gets set when we attach).
+	 */
+	if (!device_is_attached(dev))
+		return (0);
+
 	/*
 	 * We map the NVMe namespace idea onto the CAM unit LUN. For each new
 	 * namespace, scan or rescan the path to enumerate it.