Skip to contentSkip to conversation

hw/block/nvme: store aiocb in compare

PATCH In review Klaus Jensen 3 participants 7 messages Apr 8, 2021 – Apr 9, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 hw/block/nvme: store aiocb in compare

From: Klaus Jensen <[email protected]>

nvme_compare() fails to store the aiocb from the blk_aio_preadv() call. Fix this.

Fixes 0a384f923f51 ("hw/block/nvme: add compare command")
Cc Gollu Appalanaidu <[email protected]>
---
 hw/block/nvme.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/block/nvme.c +2 -1
index 6b1f056a0ebc..94bc373260be 100644
@@ -2837,7 +2837,8 @@ static uint16_t nvme_compare(NvmeCtrl *n, NvmeRequest *req)
 
     block_acct_start(blk_get_stats(blk), &req->acct, data_len,
                      BLOCK_ACCT_READ);
-    blk_aio_preadv(blk, offset, &ctx->data.iov, 0, nvme_compare_data_cb, req);
+    req->aiocb = blk_aio_preadv(blk, offset, &ctx->data.iov, 0,
+                                nvme_compare_data_cb, req);
 
     return NVME_NO_COMPLETE;
 }
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/2 hw/block/nvme: drain namespaces on sq deletion

From: Klaus Jensen <[email protected]>

For most commands, when issuing an AIO, the BlockAIOCB is stored in the NvmeRequest aiocb pointer when the AIO is issued. The main use of this is cancelling AIOs when deleting submission queues (it is currently not used for Abort).

However, some commands like Dataset Management Zone Management Send (zone reset) may involve more than one AIO and here the AIOs are issued without saving a reference to the BlockAIOCB. This is a problem since nvme_del_sq() will attempt to cancel outstanding AIOs, potentially with an invalid BlockAIOCB since the aiocb pointer is not NULL'ed when the request structure is recycled.

Fix this by

1. making sure the aiocb pointer is NULL'ed when requests are recycled
2. only attempt to cancel the AIO if the aiocb is non-NULL
3. if any AIOs could not be cancelled, drain all aio as a last resort.

Fixes dc04d25e2f3f ("hw/block/nvme: add support for the format nvm command")
Fixes c94973288cd9 ("hw/block/nvme: add broadcast nsid support flush command")
Fixes e4e430b3d6ba ("hw/block/nvme: add simple copy command")
Fixes 5f5dc4c6a942 ("hw/block/nvme: zero out zones on reset")
Fixes 2605257a26b8 ("hw/block/nvme: add the dataset management command")
Cc Gollu Appalanaidu <[email protected]>
Cc Minwoo Im <[email protected]>
---
 hw/block/nvme.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
hw/block/nvme.c +21 -2
index 94bc373260be..3c4297e38a52 100644
@@ -470,6 +470,7 @@ static void nvme_req_clear(NvmeRequest *req)
 {
     req->ns = NULL;
     req->opaque = NULL;
+    req->aiocb = NULL;
     memset(&req->cqe, 0x0, sizeof(req->cqe));
     req->status = NVME_SUCCESS;
 }
@@ -3681,6 +3682,7 @@ static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)
     NvmeSQueue *sq;
     NvmeCQueue *cq;
     uint16_t qid = le16_to_cpu(c->qid);
+    int nsid;
 
     if (unlikely(!qid || nvme_check_sqid(n, qid))) {
         trace_pci_nvme_err_invalid_del_sq(qid);
@@ -3692,9 +3694,26 @@ 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);
-        blk_aio_cancel(r->aiocb);
+        if (r->aiocb) {
+            blk_aio_cancel(r->aiocb);
+        }
     }
+
+    /*
+     * Drain all namespaces if there are still outstanding requests that we
+     * could not cancel explicitly.
+     */
+    if (!QTAILQ_EMPTY(&sq->out_req_list)) {
+        for (nsid = 1; nsid <= NVME_MAX_NAMESPACES; nsid++) {
+            NvmeNamespace *ns = nvme_ns(n, nsid);
+            if (ns) {
+                nvme_ns_drain(ns);
+            }
+        }
+    }
+
+    assert(QTAILQ_EMPTY(&sq->out_req_list));
+
     if (!nvme_check_cqid(n, sq->cqid)) {
         cq = n->cq[sq->cqid];
         QTAILQ_REMOVE(&cq->sq_list, sq, entry);
Signature · 1 line
2.31.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] hw/block/nvme: drain namespaces on sq deletion

Quoted 45 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

For most commands, when issuing an AIO, the BlockAIOCB is stored in the NvmeRequest aiocb pointer when the AIO is issued. The main use of this is cancelling AIOs when deleting submission queues (it is currently not used for Abort).

However, some commands like Dataset Management Zone Management Send (zone reset) may involve more than one AIO and here the AIOs are issued without saving a reference to the BlockAIOCB. This is a problem since nvme_del_sq() will attempt to cancel outstanding AIOs, potentially with an invalid BlockAIOCB since the aiocb pointer is not NULL'ed when the request structure is recycled.

Fix this by

1. making sure the aiocb pointer is NULL'ed when requests are recycled
2. only attempt to cancel the AIO if the aiocb is non-NULL
3. if any AIOs could not be cancelled, drain all aio as a last resort.

Fixes dc04d25e2f3f ("hw/block/nvme: add support for the format nvm command")
Fixes c94973288cd9 ("hw/block/nvme: add broadcast nsid support flush command")
Fixes e4e430b3d6ba ("hw/block/nvme: add simple copy command")
Fixes 5f5dc4c6a942 ("hw/block/nvme: zero out zones on reset")
Fixes 2605257a26b8 ("hw/block/nvme: add the dataset management command")
Cc Gollu Appalanaidu <[email protected]>
Cc Minwoo Im <[email protected]>
---
 hw/block/nvme.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
hw/block/nvme.c +1 -0
index 94bc373260be..3c4297e38a52 100644
@@ -470,6 +470,7 @@ static void nvme_req_clear(NvmeRequest *req)
 {
     req->ns = NULL;
     req->opaque = NULL;
+    req->aiocb = NULL;
     memset(&req->cqe, 0x0, sizeof(req->cqe));
     req->status = NVME_SUCCESS;
 }

hw/block/nvme.c static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req) · line 3685

Quoted 4 lines
hw/block/nvme.c +1 -0
@@ -3681,6 +3682,7 @@ static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)
     NvmeSQueue *sq;
     NvmeCQueue *cq;
     uint16_t qid = le16_to_cpu(c->qid);
+    int nsid;

Even we don't have fully supported number of namespaces in this device (0xFFFFFFFF), can we have this one with uint32_t ?

Otherwise, looks good to me.

#3 lore raw

Re: [PATCH 2/2] hw/block/nvme: drain namespaces on sq deletion

Quoted 45 lines · Minwoo Im
Quoted 44 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

For most commands, when issuing an AIO, the BlockAIOCB is stored in the NvmeRequest aiocb pointer when the AIO is issued. The main use of this is cancelling AIOs when deleting submission queues (it is currently not used for Abort).

However, some commands like Dataset Management Zone Management Send (zone reset) may involve more than one AIO and here the AIOs are issued without saving a reference to the BlockAIOCB. This is a problem since nvme_del_sq() will attempt to cancel outstanding AIOs, potentially with an invalid BlockAIOCB since the aiocb pointer is not NULL'ed when the request structure is recycled.

Fix this by

1. making sure the aiocb pointer is NULL'ed when requests are recycled
2. only attempt to cancel the AIO if the aiocb is non-NULL
3. if any AIOs could not be cancelled, drain all aio as a last resort.

Fixes dc04d25e2f3f ("hw/block/nvme: add support for the format nvm command")
Fixes c94973288cd9 ("hw/block/nvme: add broadcast nsid support flush command")
Fixes e4e430b3d6ba ("hw/block/nvme: add simple copy command")
Fixes 5f5dc4c6a942 ("hw/block/nvme: zero out zones on reset")
Fixes 2605257a26b8 ("hw/block/nvme: add the dataset management command")
Cc Gollu Appalanaidu <[email protected]>
Cc Minwoo Im <[email protected]>
---
 hw/block/nvme.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
hw/block/nvme.c +1 -0
index 94bc373260be..3c4297e38a52 100644
@@ -470,6 +470,7 @@ static void nvme_req_clear(NvmeRequest *req)
 {
     req->ns = NULL;
     req->opaque = NULL;
+    req->aiocb = NULL;
     memset(&req->cqe, 0x0, sizeof(req->cqe));
     req->status = NVME_SUCCESS;
 }
Quoted 9 lines

hw/block/nvme.c static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req) · line 3685

Quoted 5 lines
hw/block/nvme.c +1 -0
@@ -3681,6 +3682,7 @@ static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)
     NvmeSQueue *sq;
     NvmeCQueue *cq;
     uint16_t qid = le16_to_cpu(c->qid);
+    int nsid;

Even we don't have fully supported number of namespaces in this device (0xFFFFFFFF), can we have this one with uint32_t ?

Sure!

Quoted 4 lines

Otherwise, looks good to me.

Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] hw/block/nvme: drain namespaces on sq deletion

In reply to Klaus Jensen
Quoted 72 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

For most commands, when issuing an AIO, the BlockAIOCB is stored in the NvmeRequest aiocb pointer when the AIO is issued. The main use of this is cancelling AIOs when deleting submission queues (it is currently not used for Abort).

However, some commands like Dataset Management Zone Management Send (zone reset) may involve more than one AIO and here the AIOs are issued without saving a reference to the BlockAIOCB. This is a problem since nvme_del_sq() will attempt to cancel outstanding AIOs, potentially with an invalid BlockAIOCB since the aiocb pointer is not NULL'ed when the request structure is recycled.

Fix this by

1. making sure the aiocb pointer is NULL'ed when requests are recycled
2. only attempt to cancel the AIO if the aiocb is non-NULL
3. if any AIOs could not be cancelled, drain all aio as a last resort.

Fixes dc04d25e2f3f ("hw/block/nvme: add support for the format nvm command")
Fixes c94973288cd9 ("hw/block/nvme: add broadcast nsid support flush command")
Fixes e4e430b3d6ba ("hw/block/nvme: add simple copy command")
Fixes 5f5dc4c6a942 ("hw/block/nvme: zero out zones on reset")
Fixes 2605257a26b8 ("hw/block/nvme: add the dataset management command")
Cc Gollu Appalanaidu <[email protected]>
Cc Minwoo Im <[email protected]>

---
hw/block/nvme.c | 23 +++++++++++++++++++++--

1 file changed, 21 insertions(+), 2 deletions(-)
hw/block/nvme.c +15 -2
index 94bc373260be..3c4297e38a52 100644
@@ -470,6 +470,7 @@ static void nvme_req_clear(NvmeRequest *req)
 {
    req->ns = NULL;
    req->opaque = NULL;
+    req->aiocb = NULL;
    memset(&req->cqe, 0x0, sizeof(req->cqe));
    req->status = NVME_SUCCESS;
 }
@@ -3681,6 +3682,7 @@ static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)
    NvmeSQueue *sq;
    NvmeCQueue *cq;
    uint16_t qid = le16_to_cpu(c->qid);
+    int nsid;
 
    if (unlikely(!qid || nvme_check_sqid(n, qid))) {
        trace_pci_nvme_err_invalid_del_sq(qid);
@@ -3692,9 +3694,26 @@ 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);
-        blk_aio_cancel(r->aiocb);
+        if (r->aiocb) {
+            blk_aio_cancel(r->aiocb);
+        }
    }
+
+    /*
+     * Drain all namespaces if there are still outstanding requests that we
+     * could not cancel explicitly.
+     */
+    if (!QTAILQ_EMPTY(&sq->out_req_list)) {
+        for (nsid = 1; nsid <= NVME_MAX_NAMESPACES; nsid++) {
+            NvmeNamespace *ns = nvme_ns(n, nsid);
+            if (ns) {
+                nvme_ns_drain(ns);

hw/block/nvme.c static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)

Quoted 10 lines
hw/block/nvme.c +6 -1
+            }
+        }
+    }
+
+    assert(QTAILQ_EMPTY(&sq->out_req_list));
+
    if (!nvme_check_cqid(n, sq->cqid)) {
        cq = n->cq[sq->cqid];
        QTAILQ_REMOVE(&cq->sq_list, sq, entry);
--

LTM.

Quoted 3 lines

2.31.1

Reviewed-by on v1 1/2
#5 lore raw
In reply to Klaus Jensen
Quoted 17 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

nvme_compare() fails to store the aiocb from the blk_aio_preadv() call. Fix this.

Fixes 0a384f923f51 ("hw/block/nvme: add compare command")
Cc Gollu Appalanaidu <[email protected]>

---
hw/block/nvme.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
hw/block/nvme.c +0 -0
index 6b1f056a0ebc..94bc373260be 100644

hw/block/nvme.c static uint16_t nvme_compare(NvmeCtrl *n, NvmeRequest *req)

Quoted 10 lines
hw/block/nvme.c +2 -2
@@ -2837,7 +2837,8 @@ static uint16_t nvme_compare(NvmeCtrl *n, NvmeRequest *req)
 
    block_acct_start(blk_get_stats(blk), &req->acct, data_len,
                     BLOCK_ACCT_READ);
-    blk_aio_preadv(blk, offset, &ctx->data.iov, 0, nvme_compare_data_cb, req);
+    req->aiocb = blk_aio_preadv(blk, offset, &ctx->data.iov, 0,
+                                nvme_compare_data_cb, req);
 
    return NVME_NO_COMPLETE;
 }
--
Quoted 3 lines

2.31.1