On Wed, Apr 15, 2026 at 07:38:52PM +0200, mr-083 wrote: > 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 == 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=subsys0 > # -device nvme,id=ctrl0,subsys=subsys0 > # -device nvme-ns,id=ns0,drive=drv0,bus=ctrl0,nsid=1 > > device_del ns0 # guest receives AEN, removes /dev/nvme0n1 > drive_del drv0 > drive_add 0 file=disk.qcow2,format=qcow2,id=drv0,if=none > device_add nvme-ns,id=ns0,drive=drv0,bus=ctrl0,nsid=1 > # 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 > --- > hw/nvme/ctrl.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ > hw/nvme/ns.c | 8 +++++ > hw/nvme/subsys.c | 2 ++ > 3 files changed, 98 insertions(+) This is useful functionality because PCI hotplug is cumbersome as it requires preallocating PCIe root ports at guest creation time. Users may not know how many devices they will eventually hotplug, and so it's convenient to hotplug multiple namespaces onto an existing NVMe PCI controller. Reviewed-by: Stefan Hajnoczi