Skip to contentSkip to conversation

hw/block/nvme: coverity fixes

PATCH In review Klaus Jensen 2 participants 6 messages Mar 22, 2021 – Mar 29, 2021

lore
Share & tools
lore
#0 lore raw

v2 cover hw/block/nvme: coverity fixes

From: Klaus Jensen <[email protected]>

Fix two issues reported by coverity (CID 1451080 and 1451082).

v2:
- replace [2/2] with a fix for the bad reference counting noticed by Max

Klaus Jensen (2):
hw/block/nvme: fix resource leak in nvme_dif_rw hw/block/nvme: fix ref counting in nvme_format_ns

 hw/block/nvme-dif.c |  2 +-
 hw/block/nvme.c     | 10 ++++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)
Signature · 1 line
2.31.0
#1 lore raw

PATCH v2 1/2 hw/block/nvme: fix resource leak in nvme_dif_rw

From: Klaus Jensen <[email protected]>

If nvme_map_dptr() fails, nvme_dif_rw() will leak the bounce context. Fix this by using the same error handling as everywhere else in the function.

Reported-by Coverity (CID 1451080)
Fixes 146f720c5563 ("hw/block/nvme: end-to-end data protection")
---
 hw/block/nvme-dif.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/nvme-dif.c +1 -1
index 2038d724bda5..e6f04faafb5f 100644
@@ -432,7 +432,7 @@ uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req)
 
     status = nvme_map_dptr(n, &req->sg, mapped_len, &req->cmd);
     if (status) {
-        return status;
+        goto err;
     }
 
     ctx->data.bounce = g_malloc(len);
Signature · 1 line
2.31.0
#3 lore raw

PATCH v2 2/2 hw/block/nvme: fix ref counting in nvme_format_ns

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

Max noticed that since blk_aio_pwrite_zeroes() may invoke the callback before returning, the callbacks will never see *count == 0 and thus never free the count variable or decrement num_formats causing a CQE to never be posted.

Coverity (CID 1451082) also picked up on the fact that count would not be free'ed if the namespace was of zero size.

Fix both of these issues by explicitly checking *count and finalize for the given namespace if --(*count) is zero. Enqueing a CQE if there are no AIOs outstanding after this case is already handled by nvme_format() by inspecting *num_formats.

Reported-by Coverity (CID 1451082)
Fixes dc04d25e2f3f ("hw/block/nvme: add support for the format nvm command")
---
 hw/block/nvme.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
hw/block/nvme.c +8 -2
index 6842b01ab58b..c54ec3c9523c 100644
@@ -5009,9 +5009,15 @@ static uint16_t nvme_format_ns(NvmeCtrl *n, NvmeNamespace *ns, uint8_t lbaf,
 
     }
 
-    (*count)--;
+    if (--(*count)) {
+        return NVME_NO_COMPLETE;
+    }
 
-    return NVME_NO_COMPLETE;
+    g_free(count);
+    ns->status = 0x0;
+    (*num_formats)--;
+
+    return NVME_SUCCESS;
 }
 
 static uint16_t nvme_format(NvmeCtrl *n, NvmeRequest *req)
Signature · 1 line
2.31.0
#5 lore raw
In reply to Klaus Jensen
Quoted 8 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

Fix two issues reported by coverity (CID 1451080 and 1451082).

v2:
- replace [2/2] with a fix for the bad reference counting noticed by Max

Quoted 8 lines

Klaus Jensen (2):
hw/block/nvme: fix resource leak in nvme_dif_rw hw/block/nvme: fix ref counting in nvme_format_ns

 hw/block/nvme-dif.c |  2 +-
 hw/block/nvme.c     | 10 ++++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)

Gentle ping on this so Peter can get the coverity issues off the list ;)

Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] hw/block/nvme: fix resource leak in nvme_dif_rw

In reply to Klaus Jensen
Quoted 18 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

If nvme_map_dptr() fails, nvme_dif_rw() will leak the bounce context. Fix this by using the same error handling as everywhere else in the function.

Reported-by Coverity (CID 1451080)
Fixes 146f720c5563 ("hw/block/nvme: end-to-end data protection")

---
hw/block/nvme-dif.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/nvme-dif.c +0 -0
index 2038d724bda5..e6f04faafb5f 100644

hw/block/nvme-dif.c uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req) · line 435

Quoted 5 lines
hw/block/nvme-dif.c +1 -1
@@ -432,7 +432,7 @@ uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req)
 
    status = nvme_map_dptr(n, &req->sg, mapped_len, &req->cmd);
    if (status) {
-        return status;
+        goto err;

Looks good to me.

hw/block/nvme-dif.c uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req) · line 438

Quoted 4 lines
hw/block/nvme-dif.c +0 -0
    }
 
    ctx->data.bounce = g_malloc(len);
Quoted 3 lines

2.31.0

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

Re: [PATCH v2 2/2] hw/block/nvme: fix ref counting in nvme_format_ns

In reply to Klaus Jensen
Quoted 33 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

Max noticed that since blk_aio_pwrite_zeroes() may invoke the callback before returning, the callbacks will never see *count == 0 and thus never free the count variable or decrement num_formats causing a CQE to never be posted.

Coverity (CID 1451082) also picked up on the fact that count would not be free'ed if the namespace was of zero size.

Fix both of these issues by explicitly checking *count and finalize for the given namespace if --(*count) is zero. Enqueing a CQE if there are no AIOs outstanding after this case is already handled by nvme_format() by inspecting *num_formats.

Reported-by Coverity (CID 1451082)
Fixes dc04d25e2f3f ("hw/block/nvme: add support for the format nvm command")

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

1 file changed, 8 insertions(+), 2 deletions(-)
hw/block/nvme.c +1 -1
index 6842b01ab58b..c54ec3c9523c 100644
@@ -5009,9 +5009,15 @@ static uint16_t nvme_format_ns(NvmeCtrl *n, NvmeNamespace *ns, uint8_t lbaf,
 
    }
 
-    (*count)--;
+    if (--(*count)) {

hw/block/nvme.c static uint16_t nvme_format_ns(NvmeCtrl *n, NvmeNamespace *ns, uint8_t lbaf, · line 5021

Quoted 10 lines
hw/block/nvme.c +7 -1
+        return NVME_NO_COMPLETE;
+    }
 
-    return NVME_NO_COMPLETE;
+    g_free(count);
+    ns->status = 0x0;
+    (*num_formats)--;
+
+    return NVME_SUCCESS;
 }
Quoted 6 lines
hw/block/nvme.c +0 -0
 static uint16_t nvme_format(NvmeCtrl *n, NvmeRequest *req)
Signature · 1 line
2.31.0