Skip to contentSkip to conversation

hw/nvme: select first free NSID for legacy drive configuration

PATCH In review Hannes Reinecke 2 participants 3 messages Sep 9, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/nvme: select first free NSID for legacy drive configuration

If a legacy 'drive' argument is passed to the controller we cannot assume that '1' will be a free NSID, as the subsys might already have attached a namespace to this NSID. So select the first free one.

---
 hw/nvme/ctrl.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +8 -1
index 757cdff038..2c69031ca9 100644
@@ -6546,8 +6546,15 @@ static void nvme_realize(PCIDevice *pci_dev, Error **errp)
 
     /* setup a namespace if the controller drive property was given */
     if (n->namespace.blkconf.blk) {
+        int i;
         ns = &n->namespace;
-        ns->params.nsid = 1;
+        for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
+            if (nvme_ns(n, i) || nvme_subsys_ns(n->subsys, i)) {
+                continue;
+            }
+            ns->params.nsid = i;
+            break;
+        }
 
         if (nvme_ns_setup(ns, errp)) {
             return;
Signature · 1 line
2.26.2
#1 lore raw
Quoted 34 lines · Hannes Reinecke

If a legacy 'drive' argument is passed to the controller we cannot assume that '1' will be a free NSID, as the subsys might already have attached a namespace to this NSID. So select the first free one.

---
 hw/nvme/ctrl.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +8 -1
index 757cdff038..2c69031ca9 100644
@@ -6546,8 +6546,15 @@ static void nvme_realize(PCIDevice *pci_dev, Error **errp)
 
     /* setup a namespace if the controller drive property was given */
     if (n->namespace.blkconf.blk) {
+        int i;
         ns = &n->namespace;
-        ns->params.nsid = 1;
+        for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
+            if (nvme_ns(n, i) || nvme_subsys_ns(n->subsys, i)) {
+                continue;
+            }
+            ns->params.nsid = i;
+            break;
+        }
 
         if (nvme_ns_setup(ns, errp)) {
             return;
Signature · 1 line
2.26.2

Did you actually hit this?

Because then then property checking logic is bad... The device is not supposed to allow nvme,drive= combined with a subsystem property. In nvme_check_constraints():

  if (n->namespace.blkconf.blk && n->subsys) {
    /* error out */
    return;
  }
#2 lore raw
Quoted 38 lines · Klaus Jensen
Quoted 34 lines · Hannes Reinecke

If a legacy 'drive' argument is passed to the controller we cannot assume that '1' will be a free NSID, as the subsys might already have attached a namespace to this NSID. So select the first free one.

---
 hw/nvme/ctrl.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +8 -1
index 757cdff038..2c69031ca9 100644
@@ -6546,8 +6546,15 @@ static void nvme_realize(PCIDevice *pci_dev, Error **errp)
 
     /* setup a namespace if the controller drive property was given */
     if (n->namespace.blkconf.blk) {
+        int i;
         ns = &n->namespace;
-        ns->params.nsid = 1;
+        for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
+            if (nvme_ns(n, i) || nvme_subsys_ns(n->subsys, i)) {
+                continue;
+            }
+            ns->params.nsid = i;
+            break;
+        }
 
         if (nvme_ns_setup(ns, errp)) {
             return;
Signature · 1 line
2.26.2

Did you actually hit this?

Quoted 10 lines

Because then then property checking logic is bad... The device is not supposed to allow nvme,drive= combined with a subsystem property. In nvme_check_constraints():

  if (n->namespace.blkconf.blk && n->subsys) {
    /* error out */
    return;
  }

Ah. Missed that.
Do ignore my patch then.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		           Kernel Storage Architect
[email protected]			                  +49 911 74053 688
SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), GF: Felix Imendörffer