Skip to contentSkip to conversation

hw/block/nvme: add broadcast nsid support flush command

PATCH Applied Klaus Jensen 3 participants 9 messages Jan 25, 2021 – Feb 11, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/block/nvme: add broadcast nsid support flush command

From: Gollu Appalanaidu <[email protected]>

Add support for using the broadcast nsid to issue a flush on all namespaces through a single command.

---
 include/block/nvme.h  |   8 +++
 hw/block/nvme.c       | 123 +++++++++++++++++++++++++++++++++++++++---
 hw/block/trace-events |   2 +
 3 files changed, 126 insertions(+), 7 deletions(-)
include/block/nvme.h +8 -0
index e4b918064df9..0e2e94c7ebbb 100644
@@ -1024,6 +1024,14 @@ enum NvmeIdCtrlOncs {
     NVME_ONCS_TIMESTAMP     = 1 << 6,
 };
 
+enum NvmeIdctrlVwc {
+    NVME_VWC_PRESENT                    = 1 << 0,
+    NVME_VWC_NSID_BROADCAST_NO_SUPPORT  = 0 << 1,
+    NVME_VWC_NSID_BROADCAST_RESERVED    = 1 << 1,
+    NVME_VWC_NSID_BROADCAST_CTRL_SPEC   = 2 << 1,
+    NVME_VWC_NSID_BROADCAST_SUPPORT     = 3 << 1,
+};
+
 enum NvmeIdCtrlFrmw {
     NVME_FRMW_SLOT1_RO = 1 << 0,
 };
hw/block/nvme.c +116 -7

153 lines not on this page · raw message

hw/block/trace-events +2 -0
index 8e249ea910aa..ce2a35c34ea6 100644
@@ -43,6 +43,8 @@ pci_nvme_admin_cmd(uint16_t cid, uint16_t sqid, uint8_t opcode, const char *opna
 pci_nvme_read(uint16_t cid, uint32_t nsid, uint32_t nlb, uint64_t count, uint64_t lba) "cid %"PRIu16" nsid %"PRIu32" nlb %"PRIu32" count %"PRIu64" lba 0x%"PRIx64""
 pci_nvme_write(uint16_t cid, const char *verb, uint32_t nsid, uint32_t nlb, uint64_t count, uint64_t lba) "cid %"PRIu16" opname '%s' nsid %"PRIu32" nlb %"PRIu32" count %"PRIu64" lba 0x%"PRIx64""
 pci_nvme_rw_cb(uint16_t cid, const char *blkname) "cid %"PRIu16" blk '%s'"
+pci_nvme_flush(uint16_t cid, uint32_t nsid) "cid %"PRIu16" nsid %"PRIu32""
+pci_nvme_aio_flush_cb(uint16_t cid, const char *blkname) "cid %"PRIu16" blk '%s'"
 pci_nvme_block_status(int64_t offset, int64_t bytes, int64_t pnum, int ret, bool zeroed) "offset %"PRId64" bytes %"PRId64" pnum %"PRId64" ret 0x%x zeroed %d"
 pci_nvme_dsm(uint16_t cid, uint32_t nsid, uint32_t nr, uint32_t attr) "cid %"PRIu16" nsid %"PRIu32" nr %"PRIu32" attr 0x%"PRIx32""
 pci_nvme_dsm_deallocate(uint16_t cid, uint32_t nsid, uint64_t slba, uint32_t nlb) "cid %"PRIu16" nsid %"PRIu32" slba %"PRIu64" nlb %"PRIu32""
Signature · 1 line
2.30.0
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Klaus Jensen

From: Gollu Appalanaidu <[email protected]>

Add support for using the broadcast nsid to issue a flush on all namespaces through a single command.

Quoted 7 lines
---
 include/block/nvme.h  |   8 +++
 hw/block/nvme.c       | 123 +++++++++++++++++++++++++++++++++++++++---
 hw/block/trace-events |   2 +
 3 files changed, 126 insertions(+), 7 deletions(-)
#3 lore raw
In reply to Klaus Jensen
Quoted 37 lines · Klaus Jensen
hw/block/nvme.c +29 -4
@@ -1644,10 +1679,56 @@ static uint16_t nvme_compare(NvmeCtrl *n, NvmeRequest *req)
 
 static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req)
 {
-    block_acct_start(blk_get_stats(req->ns->blkconf.blk), &req->acct, 0,
-                     BLOCK_ACCT_FLUSH);
-    req->aiocb = blk_aio_flush(req->ns->blkconf.blk, nvme_rw_cb, req);
-    return NVME_NO_COMPLETE;
+    uint32_t nsid = le32_to_cpu(req->cmd.nsid);
+    uintptr_t *num_flushes = (uintptr_t *)&req->opaque;
+    uint16_t status;
+    struct nvme_aio_flush_ctx *ctx;
+    NvmeNamespace *ns;
+
+    trace_pci_nvme_flush(nvme_cid(req), nsid);
+
+    if (nsid != NVME_NSID_BROADCAST) {
+        req->ns = nvme_ns(n, nsid);
+        if (unlikely(!req->ns)) {
+            return NVME_INVALID_FIELD | NVME_DNR;
+        }
+
+        block_acct_start(blk_get_stats(req->ns->blkconf.blk), &req->acct, 0,
+                         BLOCK_ACCT_FLUSH);
+        req->aiocb = blk_aio_flush(req->ns->blkconf.blk, nvme_rw_cb, req);
+        return NVME_NO_COMPLETE;
+    }
+
+    /* 1-initialize; see comment in nvme_dsm */
+    *num_flushes = 1;
+
+    for (int i = 1; i <= n->num_namespaces; i++) {
+        ns = nvme_ns(n, i);
+        if (!ns) {
+            continue;
+        }
+

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

Quoted 10 lines
hw/block/nvme.c +10 -0
+        ctx = g_new(struct nvme_aio_flush_ctx, 1);
+        ctx->req = req;
+        ctx->ns = ns;
+
+        (*num_flushes)++;
+
+        block_acct_start(blk_get_stats(ns->blkconf.blk), &ctx->acct, 0,
+                         BLOCK_ACCT_FLUSH);
+        req->aiocb = blk_aio_flush(ns->blkconf.blk, nvme_aio_flush_cb, ctx);
+    }

Overwriting req->aiocb with the most recent flush request doesn't seem right.

This whole implementation would be much simpler with the synchronous blk_flush() routine instead of the AIO equivalent. This is not really a performant feature, so I don't think it's critical to get these operations happening in parallel. What do you think?

#4 lore raw
Quoted 43 lines · Keith Busch
Quoted 42 lines · Klaus Jensen
hw/block/nvme.c +34 -4
@@ -1644,10 +1679,56 @@ static uint16_t nvme_compare(NvmeCtrl *n, NvmeRequest *req)
 
 static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req)
 {
-    block_acct_start(blk_get_stats(req->ns->blkconf.blk), &req->acct, 0,
-                     BLOCK_ACCT_FLUSH);
-    req->aiocb = blk_aio_flush(req->ns->blkconf.blk, nvme_rw_cb, req);
-    return NVME_NO_COMPLETE;
+    uint32_t nsid = le32_to_cpu(req->cmd.nsid);
+    uintptr_t *num_flushes = (uintptr_t *)&req->opaque;
+    uint16_t status;
+    struct nvme_aio_flush_ctx *ctx;
+    NvmeNamespace *ns;
+
+    trace_pci_nvme_flush(nvme_cid(req), nsid);
+
+    if (nsid != NVME_NSID_BROADCAST) {
+        req->ns = nvme_ns(n, nsid);
+        if (unlikely(!req->ns)) {
+            return NVME_INVALID_FIELD | NVME_DNR;
+        }
+
+        block_acct_start(blk_get_stats(req->ns->blkconf.blk), &req->acct, 0,
+                         BLOCK_ACCT_FLUSH);
+        req->aiocb = blk_aio_flush(req->ns->blkconf.blk, nvme_rw_cb, req);
+        return NVME_NO_COMPLETE;
+    }
+
+    /* 1-initialize; see comment in nvme_dsm */
+    *num_flushes = 1;
+
+    for (int i = 1; i <= n->num_namespaces; i++) {
+        ns = nvme_ns(n, i);
+        if (!ns) {
+            continue;
+        }
+
+        ctx = g_new(struct nvme_aio_flush_ctx, 1);
+        ctx->req = req;
+        ctx->ns = ns;
+
+        (*num_flushes)++;
Quoted 9 lines

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

Quoted 5 lines
hw/block/nvme.c +5 -0
+
+        block_acct_start(blk_get_stats(ns->blkconf.blk), &ctx->acct, 0,
+                         BLOCK_ACCT_FLUSH);
+        req->aiocb = blk_aio_flush(ns->blkconf.blk, nvme_aio_flush_cb, ctx);
+    }

Overwriting req->aiocb with the most recent flush request doesn't seem right.

It doesn't really matter if this[1] patch is merged, but it is wrong and to align with the other multi-aio commands, this should just ignore the returned aiocb.

Quoted 4 lines

This whole implementation would be much simpler with the synchronous blk_flush() routine instead of the AIO equivalent. This is not really a performant feature, so I don't think it's critical to get these operations happening in parallel. What do you think?

It would definitely be simpler, but I believe that if there is a lot to flush, then we won't just block the nvme device. We are holding the Big QEMU Lock and will block most other devices as well.

[1]: hw/block/nvme: drain namespaces on sq deletion

#5 lore raw
Quoted 9 lines · Klaus Jensen
Quoted 4 lines · Keith Busch

This whole implementation would be much simpler with the synchronous blk_flush() routine instead of the AIO equivalent. This is not really a performant feature, so I don't think it's critical to get these operations happening in parallel. What do you think?

It would definitely be simpler, but I believe that if there is a lot to flush, then we won't just block the nvme device. We are holding the Big QEMU Lock and will block most other devices as well.

Hm, I feel like you may have told me this same explanation for a different patch. :) Okay, I'm convinced: this is the way.

#6 lore raw
Quoted 7 lines · Keith Busch
Quoted 6 lines · Klaus Jensen
Quoted 4 lines · Keith Busch

This whole implementation would be much simpler with the synchronous blk_flush() routine instead of the AIO equivalent. This is not really a performant feature, so I don't think it's critical to get these operations happening in parallel. What do you think?

Quoted 7 lines
Quoted 3 lines

It would definitely be simpler, but I believe that if there is a lot to flush, then we won't just block the nvme device. We are holding the Big QEMU Lock and will block most other devices as well.

Hm, I feel like you may have told me this same explanation for a different patch. :) Okay, I'm convinced: this is the way.

Is that an Acked-by? ;)

And yes, I might have used that argument for Copy, can't remember ;)