All of lore.kernel.org
 help / color / mirror / Atom feed
* [PULL 0/5] hw/nvme fixes
@ 2026-11-20 12:29 Klaus Jensen
  2026-11-20 12:29 ` [PULL 1/5] hw/nvme: report data transfer error on failed dma Klaus Jensen
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Klaus Jensen @ 2026-11-20 12:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Klaus Jensen

From: Klaus Jensen <k.jensen@samsung.com>

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(-)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PULL 1/5] hw/nvme: report data transfer error on failed dma
  2026-11-20 12:29 [PULL 0/5] hw/nvme fixes Klaus Jensen
@ 2026-11-20 12:29 ` Klaus Jensen
  2026-11-20 12:29 ` [PULL 2/5] hw/nvme: fix zone descriptor extension allocation overflow Klaus Jensen
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Klaus Jensen @ 2026-11-20 12:29 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Klaus Jensen, qemu-stable, Jesper Wendel Devantier,
	Keith Busch, Klaus Jensen, qemu-block

From: Klaus Jensen <k.jensen@samsung.com>

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.

Cc: qemu-stable@nongnu.org
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
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
 hw/nvme/ctrl.c       | 10 ++++++++--
 hw/nvme/trace-events |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 71c24dc02b67..5e8788a257da 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -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)) {
diff --git a/hw/nvme/trace-events b/hw/nvme/trace-events
index f97a6a11f360..7cc4d1975afb 100644
--- a/hw/nvme/trace-events
+++ b/hw/nvme/trace-events
@@ -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""
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PULL 2/5] hw/nvme: fix zone descriptor extension allocation overflow
  2026-11-20 12:29 [PULL 0/5] hw/nvme fixes Klaus Jensen
  2026-11-20 12:29 ` [PULL 1/5] hw/nvme: report data transfer error on failed dma Klaus Jensen
@ 2026-11-20 12:29 ` Klaus Jensen
  2026-11-20 12:29 ` [PULL 3/5] hw/nvme: fix open zone zrwa allocation Klaus Jensen
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Klaus Jensen @ 2026-11-20 12:29 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Klaus Jensen, qemu-stable, Jesper Wendel Devantier,
	Keith Busch, Klaus Jensen, qemu-block

From: Klaus Jensen <k.jensen@samsung.com>

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.

Cc: qemu-stable@nongnu.org
Fixes: 1a9290ade33a ("hw/block/nvme: Support Zone Descriptor Extensions")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4730
Reported-by: Karl Mehltretter
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
 hw/nvme/ns.c   | 4 ++--
 hw/nvme/nvme.h | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/hw/nvme/ns.c b/hw/nvme/ns.c
index 37d45bb4d533..b18fcab43b44 100644
--- a/hw/nvme/ns.c
+++ b/hw/nvme/ns.c
@@ -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);
diff --git a/hw/nvme/nvme.h b/hw/nvme/nvme.h
index a4b2c01a2cc4..1bfe85c10de0 100644
--- a/hw/nvme/nvme.h
+++ b/hw/nvme/nvme.h
@@ -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)
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PULL 3/5] hw/nvme: fix open zone zrwa allocation
  2026-11-20 12:29 [PULL 0/5] hw/nvme fixes Klaus Jensen
  2026-11-20 12:29 ` [PULL 1/5] hw/nvme: report data transfer error on failed dma Klaus Jensen
  2026-11-20 12:29 ` [PULL 2/5] hw/nvme: fix zone descriptor extension allocation overflow Klaus Jensen
@ 2026-11-20 12:29 ` Klaus Jensen
  2026-11-20 12:29 ` [PULL 4/5] hw/nvme: bound zrwa flush by the write area and zone write boundary Klaus Jensen
  2026-11-20 12:29 ` [PULL 5/5] hw/nvme: advance zone write pointer only once the write is committed Klaus Jensen
  4 siblings, 0 replies; 6+ messages in thread
From: Klaus Jensen @ 2026-11-20 12:29 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Klaus Jensen, qemu-stable, Jesper Wendel Devantier,
	Keith Busch, Klaus Jensen, qemu-block

From: Klaus Jensen <k.jensen@samsung.com>

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.

Cc: qemu-stable@nongnu.org
Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
Fixes: e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
 hw/nvme/ctrl.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 5e8788a257da..16860ccd9520 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -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;
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PULL 4/5] hw/nvme: bound zrwa flush by the write area and zone write boundary
  2026-11-20 12:29 [PULL 0/5] hw/nvme fixes Klaus Jensen
                   ` (2 preceding siblings ...)
  2026-11-20 12:29 ` [PULL 3/5] hw/nvme: fix open zone zrwa allocation Klaus Jensen
@ 2026-11-20 12:29 ` Klaus Jensen
  2026-11-20 12:29 ` [PULL 5/5] hw/nvme: advance zone write pointer only once the write is committed Klaus Jensen
  4 siblings, 0 replies; 6+ messages in thread
From: Klaus Jensen @ 2026-11-20 12:29 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Klaus Jensen, qemu-stable, Jesper Wendel Devantier,
	Keith Busch, Klaus Jensen, qemu-block

From: Klaus Jensen <k.jensen@samsung.com>

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.

Cc: qemu-stable@nongnu.org
Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
Fixes: e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
 hw/nvme/ctrl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 16860ccd9520..35e2c57851ae 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -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;
     }
 
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PULL 5/5] hw/nvme: advance zone write pointer only once the write is committed
  2026-11-20 12:29 [PULL 0/5] hw/nvme fixes Klaus Jensen
                   ` (3 preceding siblings ...)
  2026-11-20 12:29 ` [PULL 4/5] hw/nvme: bound zrwa flush by the write area and zone write boundary Klaus Jensen
@ 2026-11-20 12:29 ` Klaus Jensen
  4 siblings, 0 replies; 6+ messages in thread
From: Klaus Jensen @ 2026-11-20 12:29 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Klaus Jensen, qemu-stable, Jesper Wendel Devantier,
	Keith Busch, Klaus Jensen, qemu-block

From: Klaus Jensen <k.jensen@samsung.com>

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.

Cc: qemu-stable@nongnu.org
Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
Fixes: 975b64665048 ("hw/block/nvme: refactor zone resource management")
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
 hw/nvme/ctrl.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 35e2c57851ae..5755aad1fa72 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -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:
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-20 12:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 12:29 [PULL 0/5] hw/nvme fixes Klaus Jensen
2026-11-20 12:29 ` [PULL 1/5] hw/nvme: report data transfer error on failed dma Klaus Jensen
2026-11-20 12:29 ` [PULL 2/5] hw/nvme: fix zone descriptor extension allocation overflow Klaus Jensen
2026-11-20 12:29 ` [PULL 3/5] hw/nvme: fix open zone zrwa allocation Klaus Jensen
2026-11-20 12:29 ` [PULL 4/5] hw/nvme: bound zrwa flush by the write area and zone write boundary Klaus Jensen
2026-11-20 12:29 ` [PULL 5/5] hw/nvme: advance zone write pointer only once the write is committed Klaus Jensen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.