Skip to contentSkip to conversation

hw/nvme: zns/zrwa fixes

PATCH In review Klaus Jensen 2 participants 7 messages Nov 18 – Nov 19

lore
Share & tools
lore
#0 lore raw

cover hw/nvme: zns/zrwa fixes

Three fixes for zns/zrwa issues reported in #4520.

---
Klaus Jensen (3):
hw/nvme: fix open zone zrwa allocation
hw/nvme: bound zrwa flush by the write area and zone write boundary hw/nvme: advance zone write pointer only once the write is committed

 hw/nvme/ctrl.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)

---
base-commit: 37c23beb36286b9fb44849088522480bcdfe376d
change-id: 20261118-fix-4520-fea33bc104ed

Best regards,

Signature · 1 line
Klaus Jensen <[email protected]>
#1 lore raw

PATCH 1/3 hw/nvme: fix open zone zrwa allocation

From: Klaus Jensen <[email protected]>

nvme_zrm_open_flags incorrectly allocates a ZRWA resource regardless of the zone state. A ZRWA resource shall only be allocated if

a) there are available ZRWA resources; and
b) the zone is empty.

Fix the state machine and error out early.

Fixes e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
---
 hw/nvme/ctrl.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/nvme/ctrl.c +12 -4
index 5e8788a257da..16860ccd9520 100644
@@ -2038,10 +2038,19 @@ enum {
 static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
                                     NvmeZone *zone, int flags)
 {
+    NvmeZoneState state = nvme_get_zone_state(zone);
+
+    bool zrwaa =
+        (flags & NVME_ZRM_ZRWA) && !(zone->d.za & NVME_ZA_ZRWA_VALID);
+
     int act = 0;
     uint16_t status;
 
-    switch (nvme_get_zone_state(zone)) {
+    if (zrwaa && state != NVME_ZONE_STATE_EMPTY) {
+        return NVME_INVALID_ZONE_OP | NVME_DNR;
+    }
+
+    switch (state) {
     case NVME_ZONE_STATE_EMPTY:
         act = 1;
 
@@ -2051,8 +2060,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
         if (n->params.auto_transition_zones) {
             nvme_zrm_auto_transition_zone(ns);
         }
-        status = nvme_zns_check_resources(ns, act, 1,
-                                          (flags & NVME_ZRM_ZRWA) ? 1 : 0);
+        status = nvme_zns_check_resources(ns, act, 1, zrwaa);
         if (status) {
             return status;
         }
@@ -2080,7 +2088,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
         /* fallthrough */
 
     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
-        if (flags & NVME_ZRM_ZRWA) {
+        if (zrwaa) {
             ns->zns.numzrwa--;
 
             zone->d.za |= NVME_ZA_ZRWA_VALID;
Signature · 1 line
2.53.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] hw/nvme: fix open zone zrwa allocation

Quoted 68 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_zrm_open_flags incorrectly allocates a ZRWA resource regardless of the zone state. A ZRWA resource shall only be allocated if

a) there are available ZRWA resources; and
b) the zone is empty.

Fix the state machine and error out early.

Fixes e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
---
 hw/nvme/ctrl.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/nvme/ctrl.c +12 -4
index 5e8788a257da..16860ccd9520 100644
@@ -2038,10 +2038,19 @@ enum {
 static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
                                     NvmeZone *zone, int flags)
 {
+    NvmeZoneState state = nvme_get_zone_state(zone);
+
+    bool zrwaa =
+        (flags & NVME_ZRM_ZRWA) && !(zone->d.za & NVME_ZA_ZRWA_VALID);
+
     int act = 0;
     uint16_t status;
 
-    switch (nvme_get_zone_state(zone)) {
+    if (zrwaa && state != NVME_ZONE_STATE_EMPTY) {
+        return NVME_INVALID_ZONE_OP | NVME_DNR;
+    }
+
+    switch (state) {
     case NVME_ZONE_STATE_EMPTY:
         act = 1;
 
@@ -2051,8 +2060,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
         if (n->params.auto_transition_zones) {
             nvme_zrm_auto_transition_zone(ns);
         }
-        status = nvme_zns_check_resources(ns, act, 1,
-                                          (flags & NVME_ZRM_ZRWA) ? 1 : 0);
+        status = nvme_zns_check_resources(ns, act, 1, zrwaa);
         if (status) {
             return status;
         }
@@ -2080,7 +2088,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
         /* fallthrough */
 
     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
-        if (flags & NVME_ZRM_ZRWA) {
+        if (zrwaa) {
             ns->zns.numzrwa--;
 
             zone->d.za |= NVME_ZA_ZRWA_VALID;
Signature · 2 lines
-- 
2.53.0
#3 lore raw

PATCH 2/3 hw/nvme: bound zrwa flush by the write area and zone write boundary

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_zone_mgmt_send_zrwa_flush incorrectly accepts a flush up to and including one LBA past the zone random write area. Fix the comparison.

Additionally, there is no check that the last LBA to be flushed falls within the zone boundary (i.e., the *capacity* of the zone). That is, if a zone has a small capacity than size, the write pointer can be pushed past it. Fix with a check on the zone write boundary.

Fixes e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
---
 hw/nvme/ctrl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +2 -1
index 16860ccd9520..35e2c57851ae 100644
@@ -4279,7 +4279,8 @@ static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl *n, NvmeZone *zone,
         return NVME_INVALID_FIELD | NVME_DNR;
     }
 
-    if (elba < wp || elba > wp + ns->zns.zrwas) {
+    if (elba < wp || elba >= wp + ns->zns.zrwas ||
+        elba >= nvme_zone_wr_boundary(zone)) {
         return NVME_ZONE_BOUNDARY_ERROR | NVME_DNR;
     }
 
Signature · 1 line
2.53.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] hw/nvme: bound zrwa flush by the write area and zone write boundary

Quoted 38 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_zone_mgmt_send_zrwa_flush incorrectly accepts a flush up to and including one LBA past the zone random write area. Fix the comparison.

Additionally, there is no check that the last LBA to be flushed falls within the zone boundary (i.e., the *capacity* of the zone). That is, if a zone has a small capacity than size, the write pointer can be pushed past it. Fix with a check on the zone write boundary.

Fixes e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
---
 hw/nvme/ctrl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +2 -1
index 16860ccd9520..35e2c57851ae 100644
@@ -4279,7 +4279,8 @@ static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl *n, NvmeZone *zone,
         return NVME_INVALID_FIELD | NVME_DNR;
     }
 
-    if (elba < wp || elba > wp + ns->zns.zrwas) {
+    if (elba < wp || elba >= wp + ns->zns.zrwas ||
+        elba >= nvme_zone_wr_boundary(zone)) {
         return NVME_ZONE_BOUNDARY_ERROR | NVME_DNR;
     }
 
Signature · 2 lines
-- 
2.53.0
#5 lore raw

PATCH 3/3 hw/nvme: advance zone write pointer only once the write is committed

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_do_write stages the updated zone write pointer before data is mapped. If that fails, the write pointer update is already staged and subsequent writes will be rejected if attempted at the original position.

Move the staging down.

Fixes 975b64665048 ("hw/block/nvme: refactor zone resource management")
---
 hw/nvme/ctrl.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
hw/nvme/ctrl.c +13 -6
index 35e2c57851ae..5755aad1fa72 100644
@@ -3800,7 +3800,7 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
     uint64_t data_size = nvme_l2b(ns, nlb);
     uint64_t mapped_size = data_size;
     uint64_t data_offset;
-    NvmeZone *zone;
+    NvmeZone *zone = NULL;
     NvmeZonedResult *res = (NvmeZonedResult *)&req->cqe;
     BlockBackend *blk = ns->blkconf.blk;
     uint16_t status;
@@ -3893,10 +3893,6 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
         if (status) {
             goto invalid;
         }
-
-        if (!(zone->d.za & NVME_ZA_ZRWA_VALID)) {
-            zone->w_ptr += nlb;
-        }
     } else if (ns->endgrp && ns->endgrp->fdp.enabled) {
         nvme_do_write_fdp(n, req, slba, nlb);
     }
@@ -3904,7 +3900,13 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
     data_offset = nvme_l2b(ns, slba);
 
     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
-        return nvme_dif_rw(n, req);
+        status = nvme_dif_rw(n, req);
+
+        if (status != NVME_NO_COMPLETE) {
+            return status;
+        }
+
+        goto out;
     }
 
     if (!wrz) {
@@ -3922,6 +3924,11 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
                                            req);
     }
 
+out:
+    if (ns->params.zoned && !(zone->d.za & NVME_ZA_ZRWA_VALID)) {
+        zone->w_ptr += nlb;
+    }
+
     return NVME_NO_COMPLETE;
 
 invalid:
Signature · 1 line
2.53.0
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] hw/nvme: advance zone write pointer only once the write is committed

Quoted 74 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_do_write stages the updated zone write pointer before data is mapped. If that fails, the write pointer update is already staged and subsequent writes will be rejected if attempted at the original position.

Move the staging down.

Fixes 975b64665048 ("hw/block/nvme: refactor zone resource management")
---
 hw/nvme/ctrl.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
hw/nvme/ctrl.c +13 -6
index 35e2c57851ae..5755aad1fa72 100644
@@ -3800,7 +3800,7 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
     uint64_t data_size = nvme_l2b(ns, nlb);
     uint64_t mapped_size = data_size;
     uint64_t data_offset;
-    NvmeZone *zone;
+    NvmeZone *zone = NULL;
     NvmeZonedResult *res = (NvmeZonedResult *)&req->cqe;
     BlockBackend *blk = ns->blkconf.blk;
     uint16_t status;
@@ -3893,10 +3893,6 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
         if (status) {
             goto invalid;
         }
-
-        if (!(zone->d.za & NVME_ZA_ZRWA_VALID)) {
-            zone->w_ptr += nlb;
-        }
     } else if (ns->endgrp && ns->endgrp->fdp.enabled) {
         nvme_do_write_fdp(n, req, slba, nlb);
     }
@@ -3904,7 +3900,13 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
     data_offset = nvme_l2b(ns, slba);
 
     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
-        return nvme_dif_rw(n, req);
+        status = nvme_dif_rw(n, req);
+
+        if (status != NVME_NO_COMPLETE) {
+            return status;
+        }
+
+        goto out;
     }
 
     if (!wrz) {
@@ -3922,6 +3924,11 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
                                            req);
     }
 
+out:
+    if (ns->params.zoned && !(zone->d.za & NVME_ZA_ZRWA_VALID)) {
+        zone->w_ptr += nlb;
+    }
+
     return NVME_NO_COMPLETE;
 
 invalid:
Signature · 2 lines
-- 
2.53.0