Skip to contentSkip to conversation

hw/nvme fixes

GIT PULL Applied Klaus Jensen 2 participants 9 messages Nov 16

lore
Share & tools
lore
#0 lore raw

From: Klaus Jensen <[email protected]>

Hi,

The following changes since commit 573577bff0138681ddd4ee2410c51bead7b53e74:

Merge tag 'pull-target-arm-20261116' of https://gitlab.com/pm215/qemu into staging (2026-11-16 12:09:43 -0800)

are available in the Git repository at:

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

for you to fetch changes up to c0080f56036249df4a3f56e07e90cb0e56b3fd34:

hw/nvme: fix broadcast fdp events handling (2026-11-16 22:41:27 +0100)

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

----------------------------------------------------------------
Klaus Jensen (4):
hw/nvme: verify source and destination lba size for copy hw/nvme: reject fdp.runs smaller than the lba data size hw/nvme: fix fdp reclaim unit accounting hw/nvme: fix broadcast fdp events handling

Markku Ahvenjärvi (3):
hw/nvme: prevent cq_pending underflow hw/nvme: fix missed CQ updates hw/nvme: clear interrupts on controller reset

 hw/nvme/ctrl.c | 24 +++++++++++++++++++++---
 hw/nvme/ns.c   |  6 ++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
#1 lore raw

[PULL 2/7] hw/nvme: fix missed CQ updates

From: Markku Ahvenjärvi <[email protected]>

When doorbell buffers are enabled, CQ head updates may race with completion posting. When a CQ doorbell leaves pending completions, the event index may become stale and cause the guest to suppress the next MMIO doorbell write. QEMU may then fail to observe that the CQ has been drained. With legacy INTx, this can leave the interrupt asserted indefinitely and cause an interrupt storm.

Update the CQ event index as part of doorbell processing to ensure that subsequent head updates notify QEMU.

Fixes 3f7fe8de3d49 ("hw/nvme: Implement shadow doorbell buffer support")
Fixes 2e53b0b45024 ("hw/nvme: Use ioeventfd to handle doorbell updates")
---
 hw/nvme/ctrl.c | 3 +++
 1 file changed, 3 insertions(+)
hw/nvme/ctrl.c +3 -0
index e591842a4a33..52fa4b327c1f 100644
@@ -4789,6 +4789,7 @@ static void nvme_cq_notifier(EventNotifier *e)
         return;
     }
 
+    nvme_update_cq_eventidx(cq);
     nvme_update_cq_head(cq);
 
     if (cq->tail == cq->head) {
@@ -8633,6 +8634,8 @@ static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
             }
 
             nvme_irq_deassert(n, cq);
+        } else if (n->dbbuf_enabled) {
+            nvme_update_cq_eventidx(cq);
         }
     } else {
         /* Submission queue doorbell write */
Signature · 1 line
2.53.0
#2 lore raw

[PULL 4/7] hw/nvme: verify source and destination lba size for copy

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

Cross-namespace copy requires the source and destination namespace to have the same logical block size. This is not verified, causing a heap out-of-bounds write.

Fix by correctly identifying the invalid operation.

Cc: [email protected]
Reported-by: Feng Xue and XGPT of ThreatBook
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4173

Fixes d522aef88d42 ("hw/nvme: add cross namespace copy support")
---
 hw/nvme/ctrl.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/nvme/ctrl.c +5 -0
index 4a626203cad8..685cbc2f340c 100644
@@ -3278,6 +3278,11 @@ static void nvme_do_copy(NvmeCopyAIOCB *iocb)
         status = NVME_INVALID_FIELD | NVME_DNR;
         goto invalid;
     } else if (snsid != dnsid) {
+        if (sns->lbaf.ds != dns->lbaf.ds) {
+            status = NVME_CMD_INCOMP_NS_OR_FMT | NVME_DNR;
+            goto invalid;
+        }
+
         if (!NVME_ID_NS_DPS_TYPE(sns->id_ns.dps) &&
             !NVME_ID_NS_DPS_TYPE(dns->id_ns.dps)) {
             if (!nvme_copy_matching_ns_format(sns, dns, false)) {
Signature · 1 line
2.53.0
#3 lore raw

[PULL 1/7] hw/nvme: prevent cq_pending underflow

In reply to Klaus Jensen

From: Markku Ahvenjärvi <[email protected]>

cq_pending counts interrupt-enabled completion queues with unconsumed entries. nvme_cq_notifier() and nvme_process_db() decrement it whenever a CQ becomes empty, but they may also process a notification or doorbell write for a CQ that was already empty.

Decrement cq_pending only when the CQ contained pending completions before its head update. Otherwise cq_pending may underflow and prevent legacy INTx from being deasserted.

Fixes 83d7ed5c570d ("hw/nvme: fix pin-based interrupt behavior (again)")
Fixes 2e53b0b45024 ("hw/nvme: Use ioeventfd to handle doorbell updates")
---
 hw/nvme/ctrl.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/nvme/ctrl.c +5 -2
index 10e5d976a84b..e591842a4a33 100644
@@ -4783,6 +4783,7 @@ static void nvme_cq_notifier(EventNotifier *e)
 {
     NvmeCQueue *cq = container_of(e, NvmeCQueue, notifier);
     NvmeCtrl *n = cq->ctrl;
+    bool pending = cq->tail != cq->head;
 
     if (!event_notifier_test_and_clear(e)) {
         return;
@@ -4791,7 +4792,7 @@ static void nvme_cq_notifier(EventNotifier *e)
     nvme_update_cq_head(cq);
 
     if (cq->tail == cq->head) {
-        if (cq->irq_enabled) {
+        if (cq->irq_enabled && pending) {
             n->cq_pending--;
         }
 
@@ -8565,6 +8566,7 @@ static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
 
         uint16_t new_head = val & 0xffff;
         NvmeCQueue *cq;
+        bool pending;
 
         qid = (addr - (0x1000 + (1 << 2))) >> 3;
         if (unlikely(nvme_check_cqid(n, qid))) {
@@ -8619,13 +8621,14 @@ static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
             qemu_bh_schedule(cq->bh);
         }
 
+        pending = cq->tail != cq->head;
         cq->head = new_head;
         if (!qid && n->dbbuf_enabled) {
             stl_le_pci_dma(pci, cq->db_addr, cq->head, MEMTXATTRS_UNSPECIFIED);
         }
 
         if (cq->tail == cq->head) {
-            if (cq->irq_enabled) {
+            if (cq->irq_enabled && pending) {
                 n->cq_pending--;
             }
 
Signature · 1 line
2.53.0
#4 lore raw

[PULL 6/7] hw/nvme: fix fdp reclaim unit accounting

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_do_write_fdp falls back to placement handle zero for a write that has no Data Placement directive or where the placement identifier is invalid. However, it then passes the invalid placement identifier to nvme_update_ruh which results in no update to RUAMW for placement handle zero.

Reset the placement identifier along with the handle in the fallback.

---
 hw/nvme/ctrl.c | 1 +
 1 file changed, 1 insertion(+)
hw/nvme/ctrl.c +1 -0
index 685cbc2f340c..a0d22dacedfa 100644
@@ -3754,6 +3754,7 @@ static void nvme_do_write_fdp(NvmeCtrl *n, NvmeRequest *req, uint64_t slba,
         !nvme_parse_pid(ns, pid, &ph, &rg)) {
         ph = 0;
         rg = 0;
+        pid = 0;
     }
 
     ruhid = ns->fdp.phs[ph];
Signature · 1 line
2.53.0
#5 lore raw

[PULL 5/7] hw/nvme: reject fdp.runs smaller than the lba data size

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

The reclaim unit accounting in nvme_do_write_fdp spins forever if RUNS is configured to be smaller than LBADS.

Reject such an invalid configuration upfront.

---
 hw/nvme/ns.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/nvme/ns.c +6 -0
index 2ae26a9acbd0..37d45bb4d533 100644
@@ -485,6 +485,12 @@ static bool nvme_ns_init_fdp(NvmeNamespace *ns, Error **errp)
     char *r, *p;
     uint16_t *ph;
 
+    if (endgrp->fdp.runs < nvme_l2b(ns, 1)) {
+        error_setg(errp, "fdp.runs must be greater than or equal to the "
+                   "lba data size of the namespace");
+        return false;
+    }
+
     if (!ns->params.fdp.ruhs) {
         ns->fdp.nphs = 1;
         ph = ns->fdp.phs = g_new(uint16_t, 1);
Signature · 1 line
2.53.0
#6 lore raw

[PULL 7/7] hw/nvme: fix broadcast fdp events handling

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

The Set Features handler for the FDP Events feature asserts that a namespace is passed in the command because the feature is namespace-specific. An invalid namespace is rejected prior to reaching the handler, but the "broadcast nsid" (0xFFFFFFFF) is considered valid and causes the assert to abort QEMU.

Reject it with Invalid Field in Command instead as per the specification.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4792

Fixes 73064edfb864 ("hw/nvme: flexible data placement emulation")
---
 hw/nvme/ctrl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +3 -1
index a0d22dacedfa..71c24dc02b67 100644
@@ -6636,7 +6636,9 @@ static uint16_t nvme_set_feature_fdp_events(NvmeCtrl *n, NvmeNamespace *ns,
     g_autofree uint8_t *events = NULL;
     NvmeRuHandle *ruh = NULL;
 
-    assert(ns);
+    if (!ns) {
+        return NVME_INVALID_FIELD | NVME_DNR;
+    }
 
     if (!n->subsys || !n->subsys->endgrp.fdp.enabled) {
         return NVME_FDP_DISABLED | NVME_DNR;
Signature · 1 line
2.53.0
#7 lore raw

[PULL 3/7] hw/nvme: clear interrupts on controller reset

In reply to Klaus Jensen

From: Markku Ahvenjärvi <[email protected]>

Clear stale CQ interrupt state and ensure the interrupt line is deasserted when resetting an NVMe controller.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4142

Fixes 83d7ed5c570d ("hw/nvme: fix pin-based interrupt behavior (again)")
---
 hw/nvme/ctrl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/nvme/ctrl.c +4 -0
index 52fa4b327c1f..4a626203cad8 100644
@@ -8136,6 +8136,10 @@ static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst)
     stl_le_p(&n->bar.intmc, 0);
     stl_le_p(&n->bar.cc, 0);
 
+    n->cq_pending = 0;
+    n->irq_status = 0;
+    nvme_irq_check(n);
+
     n->dbbuf_dbs = 0;
     n->dbbuf_eis = 0;
     n->dbbuf_enabled = false;
Signature · 1 line
2.53.0
#8 lore raw
In reply to Klaus Jensen
Quoted 10 lines · Klaus Jensen

From: Klaus Jensen<[email protected]>

Hi,

The following changes since commit 573577bff0138681ddd4ee2410c51bead7b53e74:

   Merge tag 'pull-target-arm-20261116' ofhttps://gitlab.com/pm215/qemu into staging (2026-11-16 12:09:43 -0800)

are available in the Git repository at:

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

for you to fetch changes up to c0080f56036249df4a3f56e07e90cb0e56b3fd34:

   hw/nvme: fix broadcast fdp events handling (2026-11-16 22:41:27 +0100)

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

Applied, thanks.

r~