Skip to contentSkip to conversation

hw/nvme: fix namespace attachment on controller hotplug

PATCH Superseded Klaus Jensen 2 participants 5 messages Sep 24, 2021 – Nov 16, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hw/nvme: fix namespace attachment on controller hotplug

From: Klaus Jensen <[email protected]>

First patch from Hannes fixes the subsystem registration process such that shared (but non-detached) namespaces are automatically attached to hotplugged controllers.

The second patch changes the default for 'shared' such that namespaces are shared by default and will thus by default be attached to hotplugged controllers. This adds a compat property for older machine versions and updates the documentation to reflect this.

Hannes Reinecke (1):
hw/nvme: reattach subsystem namespaces on hotplug

Klaus Jensen (1):
hw/nvme: change nvme-ns 'shared' default

 docs/system/devices/nvme.rst | 24 ++++++++++++++----------
 hw/core/machine.c            |  4 +++-
 hw/nvme/ns.c                 |  8 +-------
 hw/nvme/subsys.c             | 10 +++++++++-
 4 files changed, 27 insertions(+), 19 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH 1/2 hw/nvme: reattach subsystem namespaces on hotplug

From: Hannes Reinecke <[email protected]>

With commit 5ffbaeed16 ("hw/nvme: fix controller hot unplugging") namespaces get moved from the controller to the subsystem if one is specified.
That keeps the namespaces alive after a controller hot-unplug, but after a controller hotplug we have to reconnect the namespaces from the subsystem to the controller.

Fixes 5ffbaeed16 ("hw/nvme: fix controller hot unplugging")
Cc Klaus Jensen <[email protected]>
[k.jensen: only attach to shared and non-detached namespaces]
---
 hw/nvme/subsys.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
hw/nvme/subsys.c +9 -1
index 93c35950d69d..6b2e4c975f5b 100644
@@ -14,7 +14,7 @@
 int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp)
 {
     NvmeSubsystem *subsys = n->subsys;
-    int cntlid;
+    int cntlid, nsid;
 
     for (cntlid = 0; cntlid < ARRAY_SIZE(subsys->ctrls); cntlid++) {
         if (!subsys->ctrls[cntlid]) {
@@ -29,12 +29,20 @@ int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp)
 
     subsys->ctrls[cntlid] = n;
 
+    for (nsid = 1; nsid < ARRAY_SIZE(subsys->namespaces); nsid++) {
+        NvmeNamespace *ns = subsys->namespaces[nsid];
+        if (ns && ns->params.shared && !ns->params.detached) {
+            nvme_attach_ns(n, ns);
+        }
+    }
+
     return cntlid;
 }
 
 void nvme_subsys_unregister_ctrl(NvmeSubsystem *subsys, NvmeCtrl *n)
 {
     subsys->ctrls[n->cntlid] = NULL;
+    n->cntlid = -1;
 }
 
 static void nvme_subsys_setup(NvmeSubsystem *subsys)
Signature · 1 line
2.33.0
#2 lore raw

PATCH 2/2 hw/nvme: change nvme-ns 'shared' default

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

Change namespaces to be shared namespaces by default (parameter shared=on). Keep shared=off for older machine types.

---
 docs/system/devices/nvme.rst | 24 ++++++++++++++----------
 hw/core/machine.c            |  4 +++-
 hw/nvme/ns.c                 |  8 +-------
 3 files changed, 18 insertions(+), 18 deletions(-)
docs/system/devices/nvme.rst +14 -10
index bff72d1c24d0..a1c0db01f6d5 100644
@@ -110,28 +110,32 @@ multipath I/O.
 This will create an NVM subsystem with two controllers. Having controllers
 linked to an ``nvme-subsys`` device allows additional ``nvme-ns`` parameters:
 
-``shared`` (default: ``off``)
+``shared`` (default: ``on`` since 6.2)
   Specifies that the namespace will be attached to all controllers in the
-  subsystem. If set to ``off`` (the default), the namespace will remain a
-  private namespace and may only be attached to a single controller at a time.
+  subsystem. If set to ``off``, the namespace will remain a private namespace
+  and may only be attached to a single controller at a time. Shared namespaces
+  are always automatically attached to all controllers (also when controllers
+  are hotplugged).
 
 ``detached`` (default: ``off``)
   If set to ``on``, the namespace will be be available in the subsystem, but
-  not attached to any controllers initially.
+  not attached to any controllers initially. A shared namespace with this set
+  to ``on`` will never be automatically attached to controllers.
 
 Thus, adding
 
 .. code-block:: console
 
    -drive file=nvm-1.img,if=none,id=nvm-1
-   -device nvme-ns,drive=nvm-1,nsid=1,shared=on
+   -device nvme-ns,drive=nvm-1,nsid=1
    -drive file=nvm-2.img,if=none,id=nvm-2
-   -device nvme-ns,drive=nvm-2,nsid=3,detached=on
+   -device nvme-ns,drive=nvm-2,nsid=3,shared=off,detached=on
 
-will cause NSID 1 will be a shared namespace (due to ``shared=on``) that is
-initially attached to both controllers. NSID 3 will be a private namespace
-(i.e. only attachable to a single controller at a time) and will not be
-attached to any controller initially (due to ``detached=on``).
+will cause NSID 1 will be a shared namespace that is initially attached to both
+controllers. NSID 3 will be a private namespace due to ``shared=off`` and only
+attachable to a single controller at a time. Additionally it will not be
+attached to any controller initially (due to ``detached=on``) or to hotplugged
+controllers.
 
 Optional Features
 =================
hw/core/machine.c +3 -1
index 067f42b528fd..5e2fa3e392b9 100644
@@ -37,7 +37,9 @@
 #include "hw/virtio/virtio.h"
 #include "hw/virtio/virtio-pci.h"
 
-GlobalProperty hw_compat_6_1[] = {};
+GlobalProperty hw_compat_6_1[] = {
+    { "nvme-ns", "shared", "off" },
+};
 const size_t hw_compat_6_1_len = G_N_ELEMENTS(hw_compat_6_1);
 
 GlobalProperty hw_compat_6_0[] = {
hw/nvme/ns.c +1 -7
index b7cf1494e75b..8b5f98c76180 100644
@@ -465,12 +465,6 @@ static void nvme_ns_realize(DeviceState *dev, Error **errp)
                        "linked to an nvme-subsys device");
             return;
         }
-
-        if (ns->params.shared) {
-            error_setg(errp, "shared requires that the nvme device is "
-                       "linked to an nvme-subsys device");
-            return;
-        }
     } else {
         /*
          * If this namespace belongs to a subsystem (through a link on the
@@ -532,7 +526,7 @@ static void nvme_ns_realize(DeviceState *dev, Error **errp)
 static Property nvme_ns_props[] = {
     DEFINE_BLOCK_PROPERTIES(NvmeNamespace, blkconf),
     DEFINE_PROP_BOOL("detached", NvmeNamespace, params.detached, false),
-    DEFINE_PROP_BOOL("shared", NvmeNamespace, params.shared, false),
+    DEFINE_PROP_BOOL("shared", NvmeNamespace, params.shared, true),
     DEFINE_PROP_UINT32("nsid", NvmeNamespace, params.nsid, 0),
     DEFINE_PROP_UUID("uuid", NvmeNamespace, params.uuid),
     DEFINE_PROP_UINT64("eui64", NvmeNamespace, params.eui64, 0),
Signature · 1 line
2.33.0
#3 lore raw
In reply to Klaus Jensen
Quoted 14 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

First patch from Hannes fixes the subsystem registration process such that shared (but non-detached) namespaces are automatically attached to hotplugged controllers.

The second patch changes the default for 'shared' such that namespaces are shared by default and will thus by default be attached to hotplugged controllers. This adds a compat property for older machine versions and updates the documentation to reflect this.

Hannes Reinecke (1):
hw/nvme: reattach subsystem namespaces on hotplug

Quoted 9 lines

Klaus Jensen (1):
hw/nvme: change nvme-ns 'shared' default

 docs/system/devices/nvme.rst | 24 ++++++++++++++----------
 hw/core/machine.c            |  4 +++-
 hw/nvme/ns.c                 |  8 +-------
 hw/nvme/subsys.c             | 10 +++++++++-
 4 files changed, 27 insertions(+), 19 deletions(-)

Ping :)

Reviewed-by on the v1 series
#4 lore raw
In reply to Klaus Jensen
Quoted 10 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

First patch from Hannes fixes the subsystem registration process such that shared (but non-detached) namespaces are automatically attached to hotplugged controllers.

The second patch changes the default for 'shared' such that namespaces are shared by default and will thus by default be attached to hotplugged controllers. This adds a compat property for older machine versions and updates the documentation to reflect this.

Series looks good.