hw/nvme queue
From: Klaus Jensen <[email protected]>
Hi,
The following changes since commit b428fe036233cbd15d37e3c027ab6ca4d3661a80:
Merge tag 'pull-target-arm-20260731' of https://gitlab.com/pm215/qemu into staging (2026-07-31 16:19:04 -0400)
are available in the Git repository at:
https://gitlab.com/birkelund/qemu.git tags/pull-nvme-20260803
for you to fetch changes up to 7a34f7b8794b29cd1bd4dfa45f7d8e8daba7cff5:
hw/nvme: fix leak on copy ranges (2026-08-03 15:35:20 -0700)
----------------------------------------------------------------
nvme queue
----------------------------------------------------------------
Klaus Jensen (1):
hw/nvme: fix leak on copy ranges
Minwoo Im (3):
hw/nvme: drop AER requests without aiocb in nvme_del_sq() hw/nvme: factor out nvme_sq_cancel_inflight() hw/nvme: cancel inflight requests on controller reset
hw/nvme/ctrl.c | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-)
[PULL 4/4] hw/nvme: fix leak on copy ranges
From: Klaus Jensen <[email protected]>
The buffer holding the ranges for the copy command is not correctly deallocated.
Fix this.
--- hw/nvme/ctrl.c | 1 + 1 file changed, 1 insertion(+)
hw/nvme/ctrl.c +1 -0
@@ -2811,6 +2811,7 @@ static void nvme_copy_done(NvmeCopyAIOCB *iocb)qemu_iovec_destroy (&iocb->iov);g_free (iocb->bounce); +g_free (iocb->ranges);if (iocb->ret <0 ) {block_acct_failed (stats, &iocb->acct.read);
Signature · 1 line
2.53.0
[PULL 1/4] hw/nvme: drop AER requests without aiocb in nvme_del_sq()
In reply to Klaus JensenFrom: Minwoo Im <[email protected]>
nvme_del_sq() asserted r->aiocb was always set when canceling a queue's inflight requests. A pending Async Event Request has no aiocb (nvme_aer() parks it without issuing any block I/O), so deleting a queue with an outstanding AER trips the assert instead of just dropping the request.
--- hw/nvme/ctrl.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
hw/nvme/ctrl.c +6 -4
@@ -4862,12 +4862,14 @@ static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req) sq = n->sq[qid];while (!QTAILQ_EMPTY (&sq->out_req_list)) { r =QTAILQ_FIRST (&sq->out_req_list); -assert (r->aiocb); r->status =NVME_CMD_ABORT_SQ_DEL ; -blk_aio_cancel (r->aiocb); - } -assert (QTAILQ_EMPTY (&sq->out_req_list)); +if (r->aiocb) { +blk_aio_cancel (r->aiocb); + }else { +QTAILQ_REMOVE (&sq->out_req_list, r, entry); + } + }if (!nvme_check_cqid (n, sq->cqid)) { cq = n->cq[sq->cqid];
Signature · 1 line
2.53.0
[PULL 2/4] hw/nvme: factor out nvme_sq_cancel_inflight()
In reply to Klaus JensenFrom: Minwoo Im <[email protected]>
Factor the cancel-and-wait loop used by nvme_del_sq() into nvme_sq_cancel_inflight(), so it can be reused to drain queues on controller reset.
--- hw/nvme/ctrl.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-)
hw/nvme/ctrl.c +21 -10
@@ -4826,6 +4826,26 @@ static int nvme_init_sq_ioeventfd(NvmeSQueue *sq)return 0 ; } +/* +* A pending Async Event Request has no aiocb (nvme_aer() parks it without +* issuing any block I/O), so there is nothing to cancel; just drop it. +*/ +static void nvme_sq_cancel_inflight (NvmeSQueue *sq,uint16_t status) +{ +NvmeRequest *r; + +while (!QTAILQ_EMPTY (&sq->out_req_list)) { + r =QTAILQ_FIRST (&sq->out_req_list); + r->status = status; + +if (r->aiocb) { +blk_aio_cancel (r->aiocb); + }else { +QTAILQ_REMOVE (&sq->out_req_list, r, entry); + } + } +} +static void nvme_free_sq (NvmeSQueue *sq,NvmeCtrl *n) {uint16_t offset = sq->sqid <<3 ;
@@ -4860,16 +4880,7 @@ static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)trace_pci_nvme_del_sq (qid); sq = n->sq[qid]; -while (!QTAILQ_EMPTY (&sq->out_req_list)) { - r =QTAILQ_FIRST (&sq->out_req_list); - r->status =NVME_CMD_ABORT_SQ_DEL ; - -if (r->aiocb) { -blk_aio_cancel (r->aiocb); - }else { -QTAILQ_REMOVE (&sq->out_req_list, r, entry); - } - } +nvme_sq_cancel_inflight (sq,NVME_CMD_ABORT_SQ_DEL );if (!nvme_check_cqid (n, sq->cqid)) { cq = n->cq[sq->cqid];
Signature · 1 line
2.53.0
[PULL 3/4] hw/nvme: cancel inflight requests on controller reset
In reply to Klaus JensenFrom: Minwoo Im <[email protected]>
nvme_ctrl_reset() freed every SQ/CQ right after nvme_ns_drain(), which only waits out requests on a per-namespace BlockBackend. That is safe as long as the guest first tore down I/O queues gracefully (Delete I/O SQ/CQ), since nvme_del_sq() already cancels and waits for anything left on a queue before freeing it.
A reset that happens without that graceful sequence first (e.g. an abrupt/asynchronous controller reset) can still have commands inflight on blk_aio_*. Freeing sq/cq before those complete leaves their completion callbacks (nvme_rw_cb() and friends) to run against already-freed NvmeRequest/NvmeSQueue/NvmeCQueue memory via nvme_enqueue_req_completion(), causing a use-after-free/segfault.
Run nvme_sq_cancel_inflight() over every queue in nvme_ctrl_reset() before the free loops, so no in-flight blk_aio_* callback can fire after sq/cq memory is freed.
Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3398
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3883
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4068
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4072
--- hw/nvme/ctrl.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
hw/nvme/ctrl.c +12 -0
@@ -8040,6 +8040,18 @@ static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst)nvme_ns_drain (ns); } +/* +* Cancel and wait out every inflight command on every queue first. A +* reset is not required to be preceded by the guest's graceful +* Delete I/O SQ/CQ sequence, so sq/cq must not be freed below while a +* blk_aio_* completion for them could still be in flight. +*/ +for (i =0 ; i < n->num_queues; i++) { +if (n->sq[i] !=NULL ) { +nvme_sq_cancel_inflight (n->sq[i],NVME_CMD_ABORT_SQ_DEL ); + } + } +for (i =0 ; i < n->num_queues; i++) {if (n->sq[i] !=NULL ) {nvme_free_sq (n->sq[i], n);
Signature · 1 line
2.53.0
Stefan Hajnoczi
AppliedApplied, thanks.
Please update the changelog at https://wiki.qemu.org/ChangeLog/11.1 for any user-visible changes.