{"id":2221319,"url":"http://patchwork.ozlabs.org/api/1.0/patches/2221319/?format=json","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/1.0/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260409070114.11313-2-matthieu@min.io>","date":"2026-04-09T07:01:10","name":"[1/2] hw/nvme: add namespace hotplug support","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"089a6e1b9f1796a90356ea16f66613834e6a7dec","submitter":{"id":93098,"url":"http://patchwork.ozlabs.org/api/1.0/people/93098/?format=json","name":"mr-083","email":"matthieu@minio.io"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260409070114.11313-2-matthieu@min.io/mbox/","series":[{"id":499254,"url":"http://patchwork.ozlabs.org/api/1.0/series/499254/?format=json","date":"2026-04-09T07:01:10","name":"NVMe namespace hotplug and drive reconnection support","version":1,"mbox":"http://patchwork.ozlabs.org/series/499254/mbox/"}],"check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2221319/checks/","tags":{},"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n secure) header.d=minio.io header.i=@minio.io header.a=rsa-sha256\n header.s=minio header.b=POVnGdR5;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4frtCd1Vqbz1yD3\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 09 Apr 2026 18:20:13 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wAkbo-0006S5-T1; Thu, 09 Apr 2026 04:19:28 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <matthieu@minio.io>) id 1wAjOI-00053r-MD\n for qemu-devel@nongnu.org; Thu, 09 Apr 2026 03:01:28 -0400","from mail-wm1-x330.google.com ([2a00:1450:4864:20::330])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <matthieu@minio.io>) id 1wAjOF-00076K-VY\n for qemu-devel@nongnu.org; Thu, 09 Apr 2026 03:01:25 -0400","by mail-wm1-x330.google.com with SMTP id\n 5b1f17b1804b1-482f454be5bso4628075e9.0\n for <qemu-devel@nongnu.org>; Thu, 09 Apr 2026 00:01:17 -0700 (PDT)","from localhost\n (2a01cb00141148004df9ec80d3fa1cac.ipv6.abo.wanadoo.fr.\n [2a01:cb00:1411:4800:4df9:ec80:d3fa:1cac])\n by smtp.gmail.com with UTF8SMTPSA id\n 5b1f17b1804b1-488cd190685sm16134045e9.29.2026.04.09.00.01.14\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Thu, 09 Apr 2026 00:01:15 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=minio.io; s=minio; t=1775718076; x=1776322876; darn=nongnu.org;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=ef/IDwXK0GaH8OkKz0gZMKyBYmbEr0v7UmTk+WfXTa0=;\n b=POVnGdR5TieptfyFxKGfwntPb68mZGvjbufIvZDDFrfyxoQE9edRvNKK+oNwdjxF6h\n OJS0v/I1YppsVm6n5weqDQWYcS6YtuBEld7N3JA5h2VTEWuicKZHxxGkq2ptBq5msdbm\n 2tQwOc1+9vx0VkyNQDGwGHCVWPI3JttTITj8lIoa3nxtdsIxvdxS0ZFw5kYPmqIQWSnp\n hjfW/oQ0VdJ+2rz5kPw12Skdw8HZ4XjoHe8d6zF8Zwwkj6yx+wZNpU7tehg6c9bnSxM3\n 3xWLEuIyznwwXC9L5zkskK05lkdqYv2wtiHsiNNchc8qnZlIqPEi3KjfxZN6rfpZrjo6\n uEZQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1775718076; x=1776322876;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=ef/IDwXK0GaH8OkKz0gZMKyBYmbEr0v7UmTk+WfXTa0=;\n b=ccE0wS0DkJPzdGzizYFWJlx6UzIKjd04tEgUD5hrhv5CUSr6/Obavmwn1cj4M0ngsX\n 5k398U5G7lWwX6lpTwupslKYQapZcPLBSlO5JKs6Vy4Jje56YKN8dVpa8AX0DjaQc0uY\n j5+KUr6xwy+SBT8PYSJyXM531e2a1Po9s5wti5PNT4IUkIO+sHAylXYmr2lRNPOtZ7io\n qLHc6zsaRJgL1CZpUXI47WfggdgqXMeIU8xaNLHE9vbJXwWHN/rFNfcTi/oEwT1A5qs6\n bqLc02BD/XtkqKVs4RLg11/yvpIlTeS6HOBDG6/7Lf2iBK0tiH7NAB01+mFYs+Na0er3\n LN2g==","X-Gm-Message-State":"AOJu0YwwqiQzj/nhUmlvDvgoCXs4XWIiNtMZ0ZwPAU8p2YDI/Cm0NfIG\n p6m8moS1kr2F6OeT2CZoVL1dfjUkLfnxgAIYhq4UPgtOj6wjibc1yTq7O8+KiAeFOiAHh/ToOQR\n ox9wU6A8ZGXxo7blDsYWq8zu5reWh8kJ0wC/DDpkkWMtQZ4oCchzE0Yb7xCfZfAwp6PNllkzeTN\n w4bURRduQg55hZ6ICplZqdGall26pOBjVxhxsyOjE=","X-Gm-Gg":"AeBDiesLU/u10twg0ViNloqIRexjZHybnXHQMm9JwaYEzQBiReA3weucVHu/1XNHlS1\n uPMnJjW4RVyVx3qVMjb3l1itrXcFvF+7X2uaDFwCYb4Lx1KslBjjom5qPcOknY7jwXOqvUrxRqB\n 9AC6FHBrgZO5OAmwN6jWKZpYT6kXq5TnT41ShdJ1xuRDH3GHjI3qRZ4VdYfvLlLG5lNgWfIePyZ\n MLGKRk10E68kRwSJMl4iOyM4p1mw1FJkGqr5jnh/SyILuc5G7Ca7SgEStuHfFr0LKRzrrTkL1nQ\n 74bde5wGpAEt+fhTp2PGMm9CKhisPweAWzGxCfd1JHSuY1Y55FK23imZbzmTKVOH9q40KBxdN1E\n 3GUdBKG8aJsBLACsc11hBMzSqz8bvx03+1+WXPXUOEQLbUPxO/dUIjeZwqXker7IFvufTFZbDrS\n Mpi3lblGQveBw07j+2bF+njnmhuzu/AQEa4trBTYSqW6OByvjRgpqgCMYwL3URllXBPUZ0lLtqo\n 6HZXaSgXMWOnBrwM3cKNwBRzg/yLTId4pdmiYcl6gDV7Gl/5qYxv0OJlHbv5g==","X-Received":"by 2002:a05:600c:a012:b0:488:aa33:dc8f with SMTP id\n 5b1f17b1804b1-488cd25a23cmr26399515e9.0.1775718075571;\n Thu, 09 Apr 2026 00:01:15 -0700 (PDT)","From":"mr-083 <matthieu@minio.io>","X-Google-Original-From":"mr-083 <matthieu@min.io>","To":"qemu-devel@nongnu.org,\n\tqemu-block@nongnu.org","Cc":"its@irrelevant.dk, kbusch@kernel.org, stefanha@redhat.com,\n mr-083 <matthieu@min.io>","Subject":"[PATCH 1/2] hw/nvme: add namespace hotplug support","Date":"Thu,  9 Apr 2026 09:01:10 +0200","Message-ID":"<20260409070114.11313-2-matthieu@min.io>","X-Mailer":"git-send-email 2.53.0","In-Reply-To":"<20260409070114.11313-1-matthieu@min.io>","References":"<20260409070114.11313-1-matthieu@min.io>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Received-SPF":"pass client-ip=2a00:1450:4864:20::330;\n envelope-from=matthieu@minio.io; helo=mail-wm1-x330.google.com","X-Spam_score_int":"-10","X-Spam_score":"-1.1","X-Spam_bar":"-","X-Spam_report":"(-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n HK_NAME_MR_MRS=0.998,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,\n SPF_PASS=-0.001 autolearn=no autolearn_force=no","X-Spam_action":"no action","X-Mailman-Approved-At":"Thu, 09 Apr 2026 04:19:22 -0400","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"qemu development <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"},"content":"Add hotplug support for nvme-ns devices on the NvmeBus. This enables\nnamespace-level hot-swap without removing the NVMe controller, which\nis how physical NVMe drives behave when hot-swapped in the same PCIe\nslot.\n\nMark nvme-ns devices as hotpluggable and register the NvmeBus as a\nhotplug handler with proper plug and unplug callbacks:\n\n- plug: attach namespace to all started controllers and send an\n  Asynchronous Event Notification (AEN) with NS_ATTR_CHANGED so\n  the guest kernel rescans namespaces\n- unplug: detach from all controllers, send AEN, remove from\n  subsystem, then unrealize the device\n\nThe plug handler skips controllers that haven't started yet\n(qs_created == false) to avoid interfering with boot-time namespace\nattachment in nvme_start_ctrl().\n\nBoth the controller bus and subsystem bus are configured as hotplug\nhandlers via qbus_set_bus_hotplug_handler() since nvme-ns devices\nmay reparent to the subsystem bus during realize.\n\nSigned-off-by: Matthieu Receveur <matthieu@min.io>\n---\n hw/nvme/ctrl.c   | 85 ++++++++++++++++++++++++++++++++++++++++++++++++\n hw/nvme/ns.c     |  1 +\n hw/nvme/subsys.c |  2 ++\n 3 files changed, 88 insertions(+)","diff":"diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c\nindex be6c7028cb..5502e4ea2b 100644\n--- a/hw/nvme/ctrl.c\n+++ b/hw/nvme/ctrl.c\n@@ -206,6 +206,7 @@\n #include \"system/hostmem.h\"\n #include \"hw/pci/msix.h\"\n #include \"hw/pci/pcie_sriov.h\"\n+#include \"hw/core/qdev.h\"\n #include \"system/spdm-socket.h\"\n #include \"migration/vmstate.h\"\n \n@@ -9293,6 +9294,7 @@ static void nvme_realize(PCIDevice *pci_dev, Error **errp)\n     }\n \n     qbus_init(&n->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id);\n+    qbus_set_bus_hotplug_handler(BUS(&n->bus));\n \n     if (nvme_init_subsys(n, errp)) {\n         return;\n@@ -9553,10 +9555,93 @@ static const TypeInfo nvme_info = {\n     },\n };\n \n+static void nvme_ns_hot_plug(HotplugHandler *hotplug_dev, DeviceState *dev,\n+                              Error **errp)\n+{\n+    NvmeNamespace *ns = NVME_NS(dev);\n+    NvmeSubsystem *subsys = ns->subsys;\n+    uint32_t nsid = ns->params.nsid;\n+    int i;\n+\n+    /*\n+     * Attach to all started controllers and notify via AEN.\n+     * Skip controllers that haven't started yet (boot-time realize) —\n+     * nvme_start_ctrl() will attach namespaces during controller init.\n+     */\n+    for (i = 0; i < NVME_MAX_CONTROLLERS; i++) {\n+        NvmeCtrl *ctrl = nvme_subsys_ctrl(subsys, i);\n+        if (!ctrl || !ctrl->qs_created) {\n+            continue;\n+        }\n+\n+        if (nvme_csi_supported(ctrl, ns->csi) && !ns->params.detached) {\n+            nvme_attach_ns(ctrl, ns);\n+            nvme_update_dsm_limits(ctrl, ns);\n+\n+            if (!test_and_set_bit(nsid, ctrl->changed_nsids)) {\n+                nvme_enqueue_event(ctrl, NVME_AER_TYPE_NOTICE,\n+                                   NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED,\n+                                   NVME_LOG_CHANGED_NSLIST);\n+            }\n+        }\n+    }\n+}\n+\n+static void nvme_ns_hot_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,\n+                               Error **errp)\n+{\n+    NvmeNamespace *ns = NVME_NS(dev);\n+    NvmeSubsystem *subsys = ns->subsys;\n+    uint32_t nsid = ns->params.nsid;\n+    int i;\n+\n+    /*\n+     * Detach from all controllers and notify the guest via AEN.\n+     * Must happen before unrealize to avoid use-after-free when the\n+     * guest sends I/O to a freed namespace.\n+     */\n+    for (i = 0; i < NVME_MAX_CONTROLLERS; i++) {\n+        NvmeCtrl *ctrl = nvme_subsys_ctrl(subsys, i);\n+        if (!ctrl || !nvme_ns(ctrl, nsid)) {\n+            continue;\n+        }\n+\n+        nvme_detach_ns(ctrl, ns);\n+        nvme_update_dsm_limits(ctrl, NULL);\n+\n+        if (!test_and_set_bit(nsid, ctrl->changed_nsids)) {\n+            nvme_enqueue_event(ctrl, NVME_AER_TYPE_NOTICE,\n+                               NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED,\n+                               NVME_LOG_CHANGED_NSLIST);\n+        }\n+    }\n+\n+    /* Remove from subsystem namespace list. */\n+    subsys->namespaces[nsid] = NULL;\n+\n+    /*\n+     * Unrealize: drain I/O, flush, cleanup structures, remove from QOM.\n+     * nvme_ns_unrealize() handles drain/shutdown/cleanup internally.\n+     */\n+    qdev_unrealize(dev);\n+}\n+\n+static void nvme_bus_class_init(ObjectClass *klass, const void *data)\n+{\n+    HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);\n+    hc->plug = nvme_ns_hot_plug;\n+    hc->unplug = nvme_ns_hot_unplug;\n+}\n+\n static const TypeInfo nvme_bus_info = {\n     .name = TYPE_NVME_BUS,\n     .parent = TYPE_BUS,\n     .instance_size = sizeof(NvmeBus),\n+    .class_init = nvme_bus_class_init,\n+    .interfaces = (const InterfaceInfo[]) {\n+        { TYPE_HOTPLUG_HANDLER },\n+        { }\n+    },\n };\n \n static void nvme_register_types(void)\ndiff --git a/hw/nvme/ns.c b/hw/nvme/ns.c\nindex b0106eaa5c..eb628c0734 100644\n--- a/hw/nvme/ns.c\n+++ b/hw/nvme/ns.c\n@@ -937,6 +937,7 @@ static void nvme_ns_class_init(ObjectClass *oc, const void *data)\n     dc->bus_type = TYPE_NVME_BUS;\n     dc->realize = nvme_ns_realize;\n     dc->unrealize = nvme_ns_unrealize;\n+    dc->hotpluggable = true;\n     device_class_set_props(dc, nvme_ns_props);\n     dc->desc = \"Virtual NVMe namespace\";\n }\ndiff --git a/hw/nvme/subsys.c b/hw/nvme/subsys.c\nindex 777e1c620f..fa35055d3c 100644\n--- a/hw/nvme/subsys.c\n+++ b/hw/nvme/subsys.c\n@@ -9,6 +9,7 @@\n #include \"qemu/osdep.h\"\n #include \"qemu/units.h\"\n #include \"qapi/error.h\"\n+#include \"hw/core/qdev.h\"\n \n #include \"nvme.h\"\n \n@@ -205,6 +206,7 @@ static void nvme_subsys_realize(DeviceState *dev, Error **errp)\n     NvmeSubsystem *subsys = NVME_SUBSYS(dev);\n \n     qbus_init(&subsys->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id);\n+    qbus_set_bus_hotplug_handler(BUS(&subsys->bus));\n \n     nvme_subsys_setup(subsys, errp);\n }\n","prefixes":["1/2"]}