From nobody Thu Apr 30 00:40:26 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=minio.io ARC-Seal: i=1; a=rsa-sha256; t=1776274778; cv=none; d=zohomail.com; s=zohoarc; b=YJO6L+s8T15HeXKUKFq+CHozVOSS9rWOfPge2Qxgm9yhrySc+I46xTKNPGWeTbTCkZJgIjQ1km35MhMOhjM+l1sSoyRSBS01iSitQg8DSSgRzEM1kn/Ndme8Q/4MXb+KFddmSiLqAem8yTju69peN4p9ats3rozC41C5nBq80jk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776274778; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Fm1uSt8v3N52EDQqCSTygQcIRULkfTeQnbM96/UzDVE=; b=oJCzBCCjsERlLv6pmpihYFm1yykLA/U/kd8N88FmRfCHhr+cnezRSOE4/kCCoPakAogioJVngyVrTRopWzLGAzQzAhmNfj9EoKbDSSFz+9T5c2dN6NAxPvVzkuYPiRaAOYkgNP+QTvVn2T4zCRR8r9r6+/LdFZ7Rem8mvHsolE8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776274778847756.622531844902; Wed, 15 Apr 2026 10:39:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wD4CR-000096-QQ; Wed, 15 Apr 2026 13:38:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wD4CN-00008p-PY for qemu-devel@nongnu.org; Wed, 15 Apr 2026 13:38:47 -0400 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wD4CL-0002ic-LP for qemu-devel@nongnu.org; Wed, 15 Apr 2026 13:38:47 -0400 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-488a14c31eeso57112605e9.0 for ; Wed, 15 Apr 2026 10:38:45 -0700 (PDT) Received: from localhost (2a01cb001411480010f92e9cd3b40a22.ipv6.abo.wanadoo.fr. [2a01:cb00:1411:4800:10f9:2e9c:d3b4:a22]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-488f0ea65b9sm39069835e9.5.2026.04.15.10.38.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 15 Apr 2026 10:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minio.io; s=minio; t=1776274724; x=1776879524; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Fm1uSt8v3N52EDQqCSTygQcIRULkfTeQnbM96/UzDVE=; b=pvcTJWYig9Y7xnLHJRjDa26f7/zCGCFBvCfoXrE0Bp/oCi4Lm02pKuicKkEyykv8Xo ggvlhtwsW6TsH3PJ/1PBBlrvapjI1SLF40mx/50XjDNpEsPkztrgitQYQLbrUcmrxLc8 bmlPizUwZQZnNSJ0zDqa4l3MQnIc4oivZK8Gqoe7EL+eW12QV9FgWzNjJwVzgdbQZECT fWbotsaSetlDStYjD3quLgTngMUZqLFmkIHa4HGw1YFmscLGTpxtJ96z+uFzwwNxvCyO z4+5ViyOJ7ZxV8fOSe6KU9whxV3b0NoiWlXRGA/6Z1gg7U4uCgS68jfWCyuvZxGMhhek CQ0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776274724; x=1776879524; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Fm1uSt8v3N52EDQqCSTygQcIRULkfTeQnbM96/UzDVE=; b=icAFcHgTcRfnCNbnotKaOCkibtg3gNPtuTWuc9fwtwQMr2vk80a5n8T4HXNFxC9Mb/ 3y0kUu68775ws8e8Fo+FjGeq4J3JsouB9NqCgwr5irwsZvPIBrAsGuTuvSGxXNG9+iMN AkHIL7BFtwwGnhxNlLFE9H+54nqLvGTRESA17UeJ0UtiGPAjIA3/8cBQIpaCxxTUwwGp qoujCeXwNX2912DzkexfFR9PjybP1om2ZfNO+eXfy3h3IySxuCUw3z3j/ZgfpUsun88K G+UwD1cJxGveLjVFJau3Qk/3zKynBd5Y2C/33XaXm6goupJRI4XLXstTuLaJpS+494kE DUYg== X-Gm-Message-State: AOJu0YzBs/MaPLpscPhTdtV3FGLOic4Xv4MIH6zmDgU+rvn5tyeoUUGt QOLh0ej8khM5sGJymjp2oOp4yL4IIJCopPf4MOGY82wRmQe8yG6MewyDqP5VdYLhefstlaGfxxC 3Lhp6T+4xxmy1vArWUokNIOtJdV/A3Iinejjvkd1SWEjybDWJUUKgxgcMvwAtrsXjp8GxnhBgxd QY1VasDhc6uQwHkbeYV/BW47vF1tpxmMu7yzXZNIY= X-Gm-Gg: AeBDievTkhRKH/LkzR0T2Mu9547OU9kxPBtqpB/nkSAR82sAk7EA1bqb25/cw97wRFw MLOVpBcI9H7Y5eFeZ8/KfBtZFfYg+TySWRq7JiuFARvtNoiwiZyTN+gjB6ypCS5RIcjgpafVV1b 63pGKyRkV04zGl0cW3Ot/UsydkupaqQcFUoeJIkXW7RKNx2QAfAGl+T91R3lG8qI3c+jyrQvNJ4 ZG1MZ11W/F36pKofAj67/0uSc8ZN3e4n+TQk6SXKfyvwsnk/Ur6BU6Ds+tLP37Mg0saSTJmLIBb qPoCSeGmWFZVjxMexlT1rSP/5F0N+4MCrz6sQfohyzzhzmre+1qOLXgEAOSc+CAvcI1E/XkKIzJ ckaN00Mj3IiyRgxzwmQORp5ihMKf4gwzgvkUh8WqCxkWn46Bl4RCBxu/YDqNDlrWqoAxPbl3qv/ nxiVBCmTtghqhZYxMNCInEJ0K4ezvXBsmRzOwV7Jx/Ie66rlJNSsEWv3oJFoLbYR+9O3o0RGNgB 5MYoQhRJ5JxtpO5mvMX6dDKRHfkBWOmSx85xhi9vsebrpWc1C1BhcK67N2tN6oakwfoFsw= X-Received: by 2002:a05:600c:c10d:b0:488:ae4e:51a5 with SMTP id 5b1f17b1804b1-488d683d633mr217192825e9.15.1776274723436; Wed, 15 Apr 2026 10:38:43 -0700 (PDT) From: mr-083 X-Google-Original-From: mr-083 To: qemu-devel@nongnu.org, qemu-block@nongnu.org Cc: its@irrelevant.dk, kbusch@kernel.org, stefanha@redhat.com, mr-083 Subject: [PATCH v4] hw/nvme: add namespace hotplug support Date: Wed, 15 Apr 2026 19:38:52 +0200 Message-ID: <20260415173852.71135-1-matthieu@min.io> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260409060155.94704-1-matthieu@min.io> References: <20260409060155.94704-1-matthieu@min.io> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::32c; envelope-from=matthieu@minio.io; helo=mail-wm1-x32c.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, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_NAME_MR_MRS=0.998, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @minio.io) X-ZM-MESSAGEID: 1776274783216154100 Add hotplug support for nvme-ns devices on the NvmeBus. This enables NVMe namespace-level hot-add and hot-remove via device_add and device_del with proper Asynchronous Event Notification (AEN), so the guest kernel can react to namespace topology changes. Mark nvme-ns devices as hotpluggable and register the NvmeBus as a hotplug handler with proper plug and unplug callbacks: - plug: attach namespace to all started controllers and send an Asynchronous Event Notification (AEN) with NS_ATTR_CHANGED so the guest kernel rescans namespaces and adds the block device - unplug: drain in-flight I/O, detach from all controllers, send AEN, then unrealize the device. The guest kernel rescans and removes the block device. The plug handler skips controllers that haven't started yet (qs_created =3D=3D false) to avoid interfering with boot-time namespace attachment in nvme_start_ctrl(). The unplug handler drains in-flight I/O via nvme_ns_drain() before detaching the namespace from controllers, so pending requests can complete normally without touching freed state. For symmetry with nvme_ns_realize() which sets subsys->namespaces[nsid], nvme_ns_unrealize() now clears that slot too making the namespace lifecycle complete. Both the controller bus and subsystem bus are configured as hotplug handlers via qbus_set_bus_hotplug_handler() since nvme-ns devices may reparent to the subsystem bus during realize. Example hot-swap sequence using the NVMe subsystem model: # Boot with: -device nvme-subsys,id=3Dsubsys0 # -device nvme,id=3Dctrl0,subsys=3Dsubsys0 # -device nvme-ns,id=3Dns0,drive=3Ddrv0,bus=3Dctrl0,nsid=3D1 device_del ns0 # guest receives AEN, removes /dev/nvme0n1 drive_del drv0 drive_add 0 file=3Ddisk.qcow2,format=3Dqcow2,id=3Ddrv0,if=3Dnone device_add nvme-ns,id=3Dns0,drive=3Ddrv0,bus=3Dctrl0,nsid=3D1 # guest receives AEN, adds /dev/nvme0n1 Tested with Linux 6.1 guest (NVMe driver processes AEN and rescans namespace list automatically). Signed-off-by: Matthieu Reviewed-by: Klaus Jensen Reviewed-by: Stefan Hajnoczi --- hw/nvme/ctrl.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/nvme/ns.c | 8 +++++ hw/nvme/subsys.c | 2 ++ 3 files changed, 98 insertions(+) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index be6c7028cb..2024b0ff75 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -206,6 +206,7 @@ #include "system/hostmem.h" #include "hw/pci/msix.h" #include "hw/pci/pcie_sriov.h" +#include "hw/core/qdev.h" #include "system/spdm-socket.h" #include "migration/vmstate.h" =20 @@ -9293,6 +9294,7 @@ static void nvme_realize(PCIDevice *pci_dev, Error **= errp) } =20 qbus_init(&n->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id); + qbus_set_bus_hotplug_handler(BUS(&n->bus)); =20 if (nvme_init_subsys(n, errp)) { return; @@ -9553,10 +9555,96 @@ static const TypeInfo nvme_info =3D { }, }; =20 +static void nvme_ns_hot_plug(HotplugHandler *hotplug_dev, DeviceState *dev, + Error **errp) +{ + NvmeNamespace *ns =3D NVME_NS(dev); + NvmeSubsystem *subsys =3D ns->subsys; + uint32_t nsid =3D ns->params.nsid; + int i; + + /* + * Attach to all started controllers and notify via AEN. + * Skip controllers that haven't started yet (boot-time realize) =E2= =80=94 + * nvme_start_ctrl() will attach namespaces during controller init. + */ + for (i =3D 0; i < NVME_MAX_CONTROLLERS; i++) { + NvmeCtrl *ctrl =3D nvme_subsys_ctrl(subsys, i); + if (!ctrl || !ctrl->qs_created) { + continue; + } + + if (nvme_csi_supported(ctrl, ns->csi) && !ns->params.detached) { + nvme_attach_ns(ctrl, ns); + nvme_update_dsm_limits(ctrl, ns); + + if (!test_and_set_bit(nsid, ctrl->changed_nsids)) { + nvme_enqueue_event(ctrl, NVME_AER_TYPE_NOTICE, + NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED, + NVME_LOG_CHANGED_NSLIST); + } + } + } +} + +static void nvme_ns_hot_unplug(HotplugHandler *hotplug_dev, DeviceState *d= ev, + Error **errp) +{ + NvmeNamespace *ns =3D NVME_NS(dev); + NvmeSubsystem *subsys =3D ns->subsys; + uint32_t nsid =3D ns->params.nsid; + int i; + + /* + * Drain in-flight I/O before tearing down the namespace. + * This must happen while the namespace is still attached to the + * controllers so any pending requests can complete normally. + */ + nvme_ns_drain(ns); + + /* + * Detach from all controllers and notify the guest via AEN. + * The guest kernel will rescan namespaces and remove the block device. + */ + for (i =3D 0; i < NVME_MAX_CONTROLLERS; i++) { + NvmeCtrl *ctrl =3D nvme_subsys_ctrl(subsys, i); + if (!ctrl || !nvme_ns(ctrl, nsid)) { + continue; + } + + nvme_detach_ns(ctrl, ns); + nvme_update_dsm_limits(ctrl, NULL); + + if (!test_and_set_bit(nsid, ctrl->changed_nsids)) { + nvme_enqueue_event(ctrl, NVME_AER_TYPE_NOTICE, + NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED, + NVME_LOG_CHANGED_NSLIST); + } + } + + /* + * Unrealize: removes from subsystem (in nvme_ns_unrealize), flushes, + * cleans up structures, and removes from QOM. + */ + qdev_unrealize(dev); +} + +static void nvme_bus_class_init(ObjectClass *klass, const void *data) +{ + HotplugHandlerClass *hc =3D HOTPLUG_HANDLER_CLASS(klass); + hc->plug =3D nvme_ns_hot_plug; + hc->unplug =3D nvme_ns_hot_unplug; +} + static const TypeInfo nvme_bus_info =3D { .name =3D TYPE_NVME_BUS, .parent =3D TYPE_BUS, .instance_size =3D sizeof(NvmeBus), + .class_init =3D nvme_bus_class_init, + .interfaces =3D (const InterfaceInfo[]) { + { TYPE_HOTPLUG_HANDLER }, + { } + }, }; =20 static void nvme_register_types(void) diff --git a/hw/nvme/ns.c b/hw/nvme/ns.c index b0106eaa5c..f4f755c6fc 100644 --- a/hw/nvme/ns.c +++ b/hw/nvme/ns.c @@ -719,10 +719,17 @@ void nvme_ns_cleanup(NvmeNamespace *ns) static void nvme_ns_unrealize(DeviceState *dev) { NvmeNamespace *ns =3D NVME_NS(dev); + NvmeSubsystem *subsys =3D ns->subsys; + uint32_t nsid =3D ns->params.nsid; =20 nvme_ns_drain(ns); nvme_ns_shutdown(ns); nvme_ns_cleanup(ns); + + /* Symmetric with nvme_ns_realize() which sets subsys->namespaces[nsid= ]. */ + if (subsys && nsid && subsys->namespaces[nsid] =3D=3D ns) { + subsys->namespaces[nsid] =3D NULL; + } } =20 void nvme_ns_atomic_configure_boundary(bool dn, uint16_t nabsn, @@ -937,6 +944,7 @@ static void nvme_ns_class_init(ObjectClass *oc, const v= oid *data) dc->bus_type =3D TYPE_NVME_BUS; dc->realize =3D nvme_ns_realize; dc->unrealize =3D nvme_ns_unrealize; + dc->hotpluggable =3D true; device_class_set_props(dc, nvme_ns_props); dc->desc =3D "Virtual NVMe namespace"; } diff --git a/hw/nvme/subsys.c b/hw/nvme/subsys.c index 777e1c620f..fa35055d3c 100644 --- a/hw/nvme/subsys.c +++ b/hw/nvme/subsys.c @@ -9,6 +9,7 @@ #include "qemu/osdep.h" #include "qemu/units.h" #include "qapi/error.h" +#include "hw/core/qdev.h" =20 #include "nvme.h" =20 @@ -205,6 +206,7 @@ static void nvme_subsys_realize(DeviceState *dev, Error= **errp) NvmeSubsystem *subsys =3D NVME_SUBSYS(dev); =20 qbus_init(&subsys->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id); + qbus_set_bus_hotplug_handler(BUS(&subsys->bus)); =20 nvme_subsys_setup(subsys, errp); } --=20 2.53.0