hw/nvme fixes
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(-)
[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.
--- hw/nvme/ctrl.c | 3 +++ 1 file changed, 3 insertions(+)
hw/nvme/ctrl.c +3 -0
@@ -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
[PULL 4/7] hw/nvme: verify source and destination lba size for copy
In reply to Klaus JensenFrom: 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
--- hw/nvme/ctrl.c | 5 +++++ 1 file changed, 5 insertions(+)
hw/nvme/ctrl.c +5 -0
@@ -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
[PULL 1/7] hw/nvme: prevent cq_pending underflow
In reply to Klaus JensenFrom: 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.
--- hw/nvme/ctrl.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
hw/nvme/ctrl.c +5 -2
@@ -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
[PULL 6/7] hw/nvme: fix fdp reclaim unit accounting
In reply to Klaus JensenFrom: 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
@@ -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
[PULL 5/7] hw/nvme: reject fdp.runs smaller than the lba data size
In reply to Klaus JensenFrom: 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
@@ -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
[PULL 7/7] hw/nvme: fix broadcast fdp events handling
In reply to Klaus JensenFrom: 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
--- hw/nvme/ctrl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +3 -1
@@ -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
[PULL 3/7] hw/nvme: clear interrupts on controller reset
In reply to Klaus JensenFrom: 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
--- hw/nvme/ctrl.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/nvme/ctrl.c +4 -0
@@ -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
Richard Henderson
AppliedQuoted 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-20261116for 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~