hw/block/nvme: coverity fixes
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
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.
--- hw/block/nvme-dif.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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; } ctx->data.bounce =g_malloc (len);
Signature · 1 line
2.31.0
Re: [PATCH v2 1/2] hw/block/nvme: fix resource leak in nvme_dif_rw
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")Signed-off-by Klaus Jensen <[email protected]>---
hw/block/nvme-dif.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/block/nvme-dif.c +0
-0
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
PATCH v2 2/2 hw/block/nvme: fix ref counting in nvme_format_ns
In reply to Klaus JensenFrom: 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.
--- hw/block/nvme.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
hw/block/nvme.c +8 -2
@@ -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
Re: [PATCH v2 2/2] hw/block/nvme: fix ref counting in nvme_format_ns
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 decrementnum_formatscausing 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 Max Reitz <[email protected]>Reported-by Coverity (CID 1451082)Fixes dc04d25e2f3f ("hw/block/nvme: add support for the format nvm command")Signed-off-by Klaus Jensen <[email protected]>---
hw/block/nvme.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)hw/block/nvme.c +1
-1@@ -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
-0static uint16_t nvme_format (NvmeCtrl *n,NvmeRequest *req)Signature · 1 line
2.31.0
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 innvme_dif_rwhw/block/nvme: fix ref counting innvme_format_nshw/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 ;)