hw/nvme: drop AER requests without aiocb in nvme_del_sq()
PATCH 1/3 hw/nvme: drop AER requests without aiocb in nvme_del_sq()
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
@@ -4858,12 +4858,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.34.1
PATCH 2/3 hw/nvme: factor out nvme_sq_cancel_inflight()
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
@@ -4822,6 +4822,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 ;
@@ -4856,16 +4876,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.34.1
PATCH 3/3 hw/nvme: cancel inflight requests on controller reset
In reply to Minwoo Imnvme_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.
--- hw/nvme/ctrl.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
hw/nvme/ctrl.c +12 -0
@@ -8035,6 +8035,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.34.1
Re: [PATCH 3/3] hw/nvme: cancel inflight requests on controller reset
Quoted 35 lines · Minwoo Im
nvme_ctrl_reset()freed every SQ/CQ right afternvme_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), sincenvme_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 vianvme_enqueue_req_completion(), causing a use-after-free/segfault.Run
nvme_sq_cancel_inflight()over every queue innvme_ctrl_reset()before the free loops, so no in-flight blk_aio_* callback can fire after sq/cq memory is freed.Signed-off-by Minwoo Im <[email protected]>--- hw/nvme/ctrl.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)hw/nvme/ctrl.c +5
-0@@ -8035,6 +8035,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.
hw/nvme/ctrl.c static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst) · line 8052
Quoted 10 lines
hw/nvme/ctrl.c +7
-0+*/ +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);
BTW, can't the newly added loop be fold into the existing loop, like
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);
nvme_free_sq(n->sq[i], n);
}
}Thanks,
/mjt
Quoted 38 lines · Minwoo Im
nvme_del_sq()assertedr->aiocbwas 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.Signed-off-by Minwoo Im <[email protected]>--- hw/nvme/ctrl.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)hw/nvme/ctrl.c +6
-4@@ -4858,12 +4858,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.34.1
Thanks,