Skip to contentSkip to conversation

hw/nvme fixes

GIT PULL Applied Klaus Jensen 2 participants 7 messages Nov 20

lore
Share & tools
lore
#1 lore raw

[PULL 1/5] hw/nvme: report data transfer error on failed dma

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_tx discards the MemTxResult returned by dma_buf_read/write and only inspects the residual. dma_buf_rw already accounts failed transactions against the residual, so a full-length transfer to or from unmapped memory results in a zero residual and an apparently successful transfer.

Check the memory transaction result instead and complete the command with a Data Transfer Error on error and add a new trace event that actually describe the event correctly.

Fixes f02b664aad8f ("hw/dma: Let dma_buf_read() / dma_buf_write() propagate MemTxResult")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4700

Reported-by Karl Mehltretter
---
 hw/nvme/ctrl.c       | 10 ++++++++--
 hw/nvme/trace-events |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)
hw/nvme/ctrl.c +8 -2
index 71c24dc02b67..5e8788a257da 100644
@@ -1380,11 +1380,17 @@ static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, void *ptr, uint32_t len,
     if (sg->flags & NVME_SG_DMA) {
         const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
         dma_addr_t residual;
+        MemTxResult res;
 
         if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
-            dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
+            res = dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
         } else {
-            dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
+            res = dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
+        }
+
+        if (unlikely(res != MEMTX_OK)) {
+            trace_pci_nvme_err_data_transfer(res);
+            return NVME_DATA_TRAS_ERROR;
         }
 
         if (unlikely(residual)) {
hw/nvme/trace-events +1 -0
index f97a6a11f360..7cc4d1975afb 100644
@@ -139,6 +139,7 @@ pci_nvme_err_addr_write(uint64_t addr) "addr 0x%"PRIx64""
 pci_nvme_err_cfs(void) "controller fatal status"
 pci_nvme_err_aio(uint16_t cid, const char *errname, uint16_t status) "cid %"PRIu16" err '%s' status 0x%"PRIx16""
 pci_nvme_err_copy_invalid_format(uint8_t format) "format 0x%"PRIx8""
+pci_nvme_err_data_transfer(int result) "result %d"
 pci_nvme_err_invalid_sgld(uint16_t cid, uint8_t typ) "cid %"PRIu16" type 0x%"PRIx8""
 pci_nvme_err_invalid_num_sgld(uint16_t cid, uint8_t typ) "cid %"PRIu16" type 0x%"PRIx8""
 pci_nvme_err_invalid_sgl_excess_length(uint32_t residual) "residual %"PRIu32""
Signature · 1 line
2.53.0
#2 lore raw

[PULL 4/5] 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
#0 lore raw

From: Klaus Jensen <[email protected]>

Hi,

The following changes since commit 08ed97c33c3818ef51a7c852347a28e9f66807b5:

tests/tcg/i386: compile test-i386-blendv.c with -msse4.1 (2026-11-19 13:54:13 -0800)

are available in the Git repository at:

https://gitlab.com/birkelund/qemu.git tags/pull-nvme-20261120

for you to fetch changes up to 7e203726411b7a28b2d020339d6267c7c7d4134e:

hw/nvme: advance zone write pointer only once the write is committed (2026-11-20 11:32:57 +0100)

----------------------------------------------------------------
nvme queue

----------------------------------------------------------------
Klaus Jensen (5):
hw/nvme: report data transfer error on failed dma hw/nvme: fix zone descriptor extension allocation overflow 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       | 48 +++++++++++++++++++++++++++++++++++-------------
 hw/nvme/ns.c         |  4 ++--
 hw/nvme/nvme.h       |  3 ++-
 hw/nvme/trace-events |  1 +
 4 files changed, 40 insertions(+), 16 deletions(-)
#3 lore raw

[PULL 3/5] 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
#4 lore raw

[PULL 5/5] 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
#5 lore raw

[PULL 2/5] hw/nvme: fix zone descriptor extension allocation overflow

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_ns_zoned_init_state sizes the zone descriptor extension array with 32-bit arithmetic. A large enough zone extension size and number of zones will wrap, causing either null pointer deref on Set Zone Descriptor Extension or an out-of-bounds write.

nvme_get_zd_extension has the same problem.

Fix the initialization by using g_malloc0_n and compute the per-zone offset using 64-bit arithmetic.

Fixes 1a9290ade33a ("hw/block/nvme: Support Zone Descriptor Extensions")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4730

Reported-by Karl Mehltretter
---
 hw/nvme/ns.c   | 4 ++--
 hw/nvme/nvme.h | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
hw/nvme/ns.c +2 -2
index 37d45bb4d533..b18fcab43b44 100644
@@ -314,8 +314,8 @@ static void nvme_ns_zoned_init_state(NvmeNamespace *ns)
 
     ns->zone_array = g_new0(NvmeZone, ns->num_zones);
     if (ns->params.zd_extension_size) {
-        ns->zd_extensions = g_malloc0(ns->params.zd_extension_size *
-                                      ns->num_zones);
+        ns->zd_extensions = g_malloc0_n(ns->num_zones,
+                                        ns->params.zd_extension_size);
     }
 
     QTAILQ_INIT(&ns->exp_open_zones);
hw/nvme/nvme.h +2 -1
index a4b2c01a2cc4..1bfe85c10de0 100644
@@ -363,7 +363,8 @@ static inline bool nvme_wp_is_valid(NvmeZone *zone)
 static inline uint8_t *nvme_get_zd_extension(NvmeNamespace *ns,
                                              uint32_t zone_idx)
 {
-    return &ns->zd_extensions[zone_idx * ns->params.zd_extension_size];
+    return ns->zd_extensions + (uint64_t)zone_idx *
+        ns->params.zd_extension_size;
 }
 
 static inline void nvme_aor_inc_open(NvmeNamespace *ns)
Signature · 1 line
2.53.0
#6 lore raw
In reply to Klaus Jensen
Quoted 10 lines · Klaus Jensen

From: Klaus Jensen<[email protected]>

Hi,

The following changes since commit 08ed97c33c3818ef51a7c852347a28e9f66807b5:

   tests/tcg/i386: compile test-i386-blendv.c with -msse4.1 (2026-11-19 13:54:13 -0800)

are available in the Git repository at:

Quoted 8 lines
   https://gitlab.com/birkelund/qemu.git tags/pull-nvme-20261120

for you to fetch changes up to 7e203726411b7a28b2d020339d6267c7c7d4134e:

hw/nvme: advance zone write pointer only once the write is committed (2026-11-20 11:32:57 +0100)

----------------------------------------------------------------
nvme queue

Applied, thanks.

r~