Skip to contentSkip to conversation

block/nvme: Fix a memory leak in nvme_free_queue_pair()

PATCH In review Philippe Mathieu-Daudé 3 participants 17 messages Oct 6, 2021 – Nov 2, 2021 Block layer

lore
Share & tools
lore
#0 lore raw

cover block/nvme: Fix a memory leak in nvme_free_queue_pair()

Hi,

While searching for a memory corruption in the NVMe block driver, Valgrind revealed one leak. It is not what I'm looking for, but worth fixing (at least one warning removed from the huge valgrind report).

Philippe Mathieu-Daudé (5):
block/nvme: Automatically free qemu_memalign() with QEMU_AUTO_VFREE block/nvme: Display CQ/SQ pointer in nvme_free_queue_pair() block/nvme: Extract nvme_free_queue() from nvme_free_queue_pair() block/nvme: Pass BDRVNVMeState* handle to nvme_free_queue_pair() block/nvme: Fix memory leak from nvme_init_queue()

 block/nvme.c       | 31 +++++++++++++++++--------------
 block/trace-events |  2 +-
 2 files changed, 18 insertions(+), 15 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/5 block/nvme: Automatically free qemu_memalign() with QEMU_AUTO_VFREE

Since commit 4d324c0bf65 ("introduce QEMU_AUTO_VFREE") buffers allocated by qemu_memalign() can automatically freed when using the QEMU_AUTO_VFREE macro. Use it to simplify a bit.

---
 block/nvme.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
block/nvme.c +4 -7
index 1cc7b62bb4b..fefcc04abe6 100644
@@ -514,10 +514,10 @@ static bool nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
 {
     BDRVNVMeState *s = bs->opaque;
     bool ret = false;
-    union {
+    QEMU_AUTO_VFREE union {
         NvmeIdCtrl ctrl;
         NvmeIdNs ns;
-    } *id;
+    } *id = NULL;
     NvmeLBAF *lbaf;
     uint16_t oncs;
     int r;
@@ -595,7 +595,6 @@ static bool nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
     s->blkshift = lbaf->ds;
 out:
     qemu_vfio_dma_unmap(s->vfio, id);
-    qemu_vfree(id);
 
     return ret;
 }
@@ -1219,7 +1218,7 @@ static int nvme_co_prw(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 {
     BDRVNVMeState *s = bs->opaque;
     int r;
-    uint8_t *buf = NULL;
+    QEMU_AUTO_VFREE uint8_t *buf = NULL;
     QEMUIOVector local_qiov;
     size_t len = QEMU_ALIGN_UP(bytes, qemu_real_host_page_size);
     assert(QEMU_IS_ALIGNED(offset, s->page_size));
@@ -1246,7 +1245,6 @@ static int nvme_co_prw(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
     if (!r && !is_write) {
         qemu_iovec_from_buf(qiov, 0, buf, bytes);
     }
-    qemu_vfree(buf);
     return r;
 }
 
@@ -1365,7 +1363,7 @@ static int coroutine_fn nvme_co_pdiscard(BlockDriverState *bs,
     BDRVNVMeState *s = bs->opaque;
     NVMeQueuePair *ioq = s->queues[INDEX_IO(0)];
     NVMeRequest *req;
-    NvmeDsmRange *buf;
+    QEMU_AUTO_VFREE NvmeDsmRange *buf = NULL;
     QEMUIOVector local_qiov;
     int ret;
 
@@ -1440,7 +1438,6 @@ static int coroutine_fn nvme_co_pdiscard(BlockDriverState *bs,
     trace_nvme_dsm_done(s, offset, bytes, ret);
 out:
     qemu_iovec_destroy(&local_qiov);
-    qemu_vfree(buf);
     return ret;
 
 }
Signature · 1 line
2.31.1
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] block/nvme: Automatically free qemu_memalign() with QEMU_AUTO_VFREE

Quoted 8 lines · Philippe Mathieu-Daudé

Since commit 4d324c0bf65 ("introduce QEMU_AUTO_VFREE") buffers allocated by qemu_memalign() can automatically freed when using the QEMU_AUTO_VFREE macro. Use it to simplify a bit.

---
 block/nvme.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
#3 lore raw

PATCH 2/5 block/nvme: Display CQ/SQ pointer in nvme_free_queue_pair()

In reply to Philippe Mathieu-Daudé

For debugging purpose it is helpful to know the CQ/SQ pointers. We already have a trace event in nvme_free_queue_pair(), extend it to report these pointer addresses.

---
 block/nvme.c       | 2 +-
 block/trace-events | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
block/nvme.c +1 -1
index fefcc04abe6..0c94799a541 100644
@@ -185,7 +185,7 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
 
 static void nvme_free_queue_pair(NVMeQueuePair *q)
 {
-    trace_nvme_free_queue_pair(q->index, q);
+    trace_nvme_free_queue_pair(q->index, q, &q->cq, &q->sq);
     if (q->completion_bh) {
         qemu_bh_delete(q->completion_bh);
     }
block/trace-events +1 -1
index f2d0a9b62a7..491e765c611 100644
@@ -157,7 +157,7 @@ nvme_dsm_done(void *s, int64_t offset, int64_t bytes, int ret) "s %p offset 0x%"
 nvme_dma_map_flush(void *s) "s %p"
 nvme_free_req_queue_wait(void *s, unsigned q_index) "s %p q #%u"
 nvme_create_queue_pair(unsigned q_index, void *q, size_t size, void *aio_context, int fd) "index %u q %p size %zu aioctx %p fd %d"
-nvme_free_queue_pair(unsigned q_index, void *q) "index %u q %p"
+nvme_free_queue_pair(unsigned q_index, void *q, void *cq, void *sq) "index %u q %p cq %p sq %p"
 nvme_cmd_map_qiov(void *s, void *cmd, void *req, void *qiov, int entries) "s %p cmd %p req %p qiov %p entries %d"
 nvme_cmd_map_qiov_pages(void *s, int i, uint64_t page) "s %p page[%d] 0x%"PRIx64
 nvme_cmd_map_qiov_iov(void *s, int i, void *page, int pages) "s %p iov[%d] %p pages %d"
Signature · 1 line
2.31.1
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] block/nvme: Display CQ/SQ pointer in nvme_free_queue_pair()

Quoted 9 lines · Philippe Mathieu-Daudé

For debugging purpose it is helpful to know the CQ/SQ pointers. We already have a trace event in nvme_free_queue_pair(), extend it to report these pointer addresses.

---
 block/nvme.c       | 2 +-
 block/trace-events | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
#5 lore raw

PATCH 3/5 block/nvme: Extract nvme_free_queue() from nvme_free_queue_pair()

In reply to Philippe Mathieu-Daudé

Instead of duplicating code, extract the common helper to free a single queue.

---
 block/nvme.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
block/nvme.c +7 -2
index 0c94799a541..e4f336d79c2 100644
@@ -183,15 +183,20 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
     return r == 0;
 }
 
+static void nvme_free_queue(NVMeQueue *q)
+{
+    qemu_vfree(q->queue);
+}
+
 static void nvme_free_queue_pair(NVMeQueuePair *q)
 {
     trace_nvme_free_queue_pair(q->index, q, &q->cq, &q->sq);
     if (q->completion_bh) {
         qemu_bh_delete(q->completion_bh);
     }
+    nvme_free_queue(&q->sq);
+    nvme_free_queue(&q->cq);
     qemu_vfree(q->prp_list_pages);
-    qemu_vfree(q->sq.queue);
-    qemu_vfree(q->cq.queue);
     qemu_mutex_destroy(&q->lock);
     g_free(q);
 }
Signature · 1 line
2.31.1
#7 lore raw

PATCH 4/5 block/nvme: Pass BDRVNVMeState* handle to nvme_free_queue_pair()

In reply to Philippe Mathieu-Daudé

In the next commit we want to access BDRVNVMeState from nvme_free_queue_pair() callee. Pass it along first.

---
 block/nvme.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
block/nvme.c +7 -7
index e4f336d79c2..6e476f54b9f 100644
@@ -183,19 +183,19 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
     return r == 0;
 }
 
-static void nvme_free_queue(NVMeQueue *q)
+static void nvme_free_queue(BDRVNVMeState *s, NVMeQueue *q)
 {
     qemu_vfree(q->queue);
 }
 
-static void nvme_free_queue_pair(NVMeQueuePair *q)
+static void nvme_free_queue_pair(BDRVNVMeState *s, NVMeQueuePair *q)
 {
     trace_nvme_free_queue_pair(q->index, q, &q->cq, &q->sq);
     if (q->completion_bh) {
         qemu_bh_delete(q->completion_bh);
     }
-    nvme_free_queue(&q->sq);
-    nvme_free_queue(&q->cq);
+    nvme_free_queue(s, &q->sq);
+    nvme_free_queue(s, &q->cq);
     qemu_vfree(q->prp_list_pages);
     qemu_mutex_destroy(&q->lock);
     g_free(q);
@@ -270,7 +270,7 @@ static NVMeQueuePair *nvme_create_queue_pair(BDRVNVMeState *s,
 
     return q;
 fail:
-    nvme_free_queue_pair(q);
+    nvme_free_queue_pair(s, q);
     return NULL;
 }
 
@@ -693,7 +693,7 @@ static bool nvme_add_io_queue(BlockDriverState *bs, Error **errp)
     s->queue_count++;
     return true;
 out_error:
-    nvme_free_queue_pair(q);
+    nvme_free_queue_pair(s, q);
     return false;
 }
 
@@ -918,7 +918,7 @@ static void nvme_close(BlockDriverState *bs)
     BDRVNVMeState *s = bs->opaque;
 
     for (unsigned i = 0; i < s->queue_count; ++i) {
-        nvme_free_queue_pair(s->queues[i]);
+        nvme_free_queue_pair(s, s->queues[i]);
     }
     g_free(s->queues);
     aio_set_event_notifier(bdrv_get_aio_context(bs),
Signature · 1 line
2.31.1
#9 lore raw

PATCH 5/5 block/nvme: Fix memory leak from nvme_init_queue()

In reply to Philippe Mathieu-Daudé

nvme_create_queue_pair() allocates resources with qemu_vfio_dma_map(), but we never release them. Do it in nvme_free_queue() which is called from nvme_free_queue_pair().

Reported by valgrind:

==252858== 520,192 bytes in 1 blocks are still reachable in loss record 8,293 of 8,302

  ==252858==    at 0x4846803: memalign (vg_replace_malloc.c:1265)
  ==252858==    by 0x484691F: posix_memalign (vg_replace_malloc.c:1429)
  ==252858==    by 0xB8AFE4: qemu_try_memalign (oslib-posix.c:210)
  ==252858==    by 0xA9E315: nvme_create_queue_pair (nvme.c:229)
  ==252858==    by 0xAA0125: nvme_init (nvme.c:799)
  ==252858==    by 0xAA081C: nvme_file_open (nvme.c:953)
  ==252858==    by 0xA23DDD: bdrv_open_driver (block.c:1550)
  ==252858==    by 0xA24806: bdrv_open_common (block.c:1827)
  ==252858==    by 0xA2889B: bdrv_open_inherit (block.c:3747)
  ==252858==    by 0xA28DE4: bdrv_open (block.c:3840)
  ==252858==    by 0x9E0F8E: bds_tree_init (blockdev.c:675)
  ==252858==    by 0x9E7C74: qmp_blockdev_add (blockdev.c:3551)
Fixes bdd6a90a9e5 ("block: Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +1 -0
index 6e476f54b9f..903c8ffa060 100644
@@ -185,6 +185,7 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
 
 static void nvme_free_queue(BDRVNVMeState *s, NVMeQueue *q)
 {
+    qemu_vfio_dma_unmap(s->vfio, q->queue);
     qemu_vfree(q->queue);
 }
 
Signature · 1 line
2.31.1
#10 lore raw

Re: [PATCH 5/5] block/nvme: Fix memory leak from nvme_init_queue()

Quoted 7 lines · Philippe Mathieu-Daudé

nvme_create_queue_pair() allocates resources with qemu_vfio_dma_map(), but we never release them. Do it in nvme_free_queue() which is called from nvme_free_queue_pair().

Reported by valgrind:

==252858== 520,192 bytes in 1 blocks are still reachable in loss record 8,293 of 8,302

BTW the "8302" number is kinda depressing...

Good news, with this patch I'm now at 8301.

Quoted 32 lines
  ==252858==    at 0x4846803: memalign (vg_replace_malloc.c:1265)
  ==252858==    by 0x484691F: posix_memalign (vg_replace_malloc.c:1429)
  ==252858==    by 0xB8AFE4: qemu_try_memalign (oslib-posix.c:210)
  ==252858==    by 0xA9E315: nvme_create_queue_pair (nvme.c:229)
  ==252858==    by 0xAA0125: nvme_init (nvme.c:799)
  ==252858==    by 0xAA081C: nvme_file_open (nvme.c:953)
  ==252858==    by 0xA23DDD: bdrv_open_driver (block.c:1550)
  ==252858==    by 0xA24806: bdrv_open_common (block.c:1827)
  ==252858==    by 0xA2889B: bdrv_open_inherit (block.c:3747)
  ==252858==    by 0xA28DE4: bdrv_open (block.c:3840)
  ==252858==    by 0x9E0F8E: bds_tree_init (blockdev.c:675)
  ==252858==    by 0x9E7C74: qmp_blockdev_add (blockdev.c:3551)
Fixes bdd6a90a9e5 ("block: Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +1 -0
index 6e476f54b9f..903c8ffa060 100644
@@ -185,6 +185,7 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
 
 static void nvme_free_queue(BDRVNVMeState *s, NVMeQueue *q)
 {
+    qemu_vfio_dma_unmap(s->vfio, q->queue);
     qemu_vfree(q->queue);
 }
 
#11 lore raw

Re: [PATCH 5/5] block/nvme: Fix memory leak from nvme_init_queue()

In reply to Philippe Mathieu-Daudé
Quoted 31 lines · Philippe Mathieu-Daudé

nvme_create_queue_pair() allocates resources with qemu_vfio_dma_map(), but we never release them. Do it in nvme_free_queue() which is called from nvme_free_queue_pair().

Reported by valgrind:

==252858== 520,192 bytes in 1 blocks are still reachable in loss record 8,293 of 8,302

  ==252858==    at 0x4846803: memalign (vg_replace_malloc.c:1265)
  ==252858==    by 0x484691F: posix_memalign (vg_replace_malloc.c:1429)
  ==252858==    by 0xB8AFE4: qemu_try_memalign (oslib-posix.c:210)
  ==252858==    by 0xA9E315: nvme_create_queue_pair (nvme.c:229)
  ==252858==    by 0xAA0125: nvme_init (nvme.c:799)
  ==252858==    by 0xAA081C: nvme_file_open (nvme.c:953)
  ==252858==    by 0xA23DDD: bdrv_open_driver (block.c:1550)
  ==252858==    by 0xA24806: bdrv_open_common (block.c:1827)
  ==252858==    by 0xA2889B: bdrv_open_inherit (block.c:3747)
  ==252858==    by 0xA28DE4: bdrv_open (block.c:3840)
  ==252858==    by 0x9E0F8E: bds_tree_init (blockdev.c:675)
  ==252858==    by 0x9E7C74: qmp_blockdev_add (blockdev.c:3551)
Fixes bdd6a90a9e5 ("block: Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +0 -0
index 6e476f54b9f..903c8ffa060 100644

block/nvme.c static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q, · line 190

Quoted 6 lines
block/nvme.c +1 -0
@@ -185,6 +185,7 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
 
 static void nvme_free_queue(BDRVNVMeState *s, NVMeQueue *q)
 {
+    qemu_vfio_dma_unmap(s->vfio, q->queue);
     qemu_vfree(q->queue);
 }

I can't figure out the issue. qemu_vfree(q->queue) was already called before this patch. How does adding qemu_vfio_dma_unmap() help with the valgrind report in the commit description?

Stefan

#12 lore raw

Re: [PATCH 5/5] block/nvme: Fix memory leak from nvme_init_queue()

Quoted 33 lines · Stefan Hajnoczi
Quoted 32 lines · Philippe Mathieu-Daudé

nvme_create_queue_pair() allocates resources with qemu_vfio_dma_map(), but we never release them. Do it in nvme_free_queue() which is called from nvme_free_queue_pair().

Reported by valgrind:

==252858== 520,192 bytes in 1 blocks are still reachable in loss record 8,293 of 8,302

  ==252858==    at 0x4846803: memalign (vg_replace_malloc.c:1265)
  ==252858==    by 0x484691F: posix_memalign (vg_replace_malloc.c:1429)
  ==252858==    by 0xB8AFE4: qemu_try_memalign (oslib-posix.c:210)
  ==252858==    by 0xA9E315: nvme_create_queue_pair (nvme.c:229)
  ==252858==    by 0xAA0125: nvme_init (nvme.c:799)
  ==252858==    by 0xAA081C: nvme_file_open (nvme.c:953)
  ==252858==    by 0xA23DDD: bdrv_open_driver (block.c:1550)
  ==252858==    by 0xA24806: bdrv_open_common (block.c:1827)
  ==252858==    by 0xA2889B: bdrv_open_inherit (block.c:3747)
  ==252858==    by 0xA28DE4: bdrv_open (block.c:3840)
  ==252858==    by 0x9E0F8E: bds_tree_init (blockdev.c:675)
  ==252858==    by 0x9E7C74: qmp_blockdev_add (blockdev.c:3551)
Fixes bdd6a90a9e5 ("block: Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +0 -0
index 6e476f54b9f..903c8ffa060 100644
@@ -185,6 +185,7 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
 
Quoted 9 lines

block/nvme.c static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q, · line 190

Quoted 5 lines
block/nvme.c +1 -0
 static void nvme_free_queue(BDRVNVMeState *s, NVMeQueue *q)
 {
+    qemu_vfio_dma_unmap(s->vfio, q->queue);
     qemu_vfree(q->queue);
 }

I can't figure out the issue. qemu_vfree(q->queue) was already called before this patch. How does adding qemu_vfio_dma_unmap() help with the valgrind report in the commit description?

You are right, I think I didn't select the correct record between the 8302 reported by valgrind. I will revisit, thanks.

#13 lore raw

Re: [PATCH 5/5] block/nvme: Fix memory leak from nvme_init_queue()

Am 07.10.2021 um 15:34 hat Philippe Mathieu-Daudé geschrieben:

Quoted 44 lines · Stefan Hajnoczi
Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 32 lines

nvme_create_queue_pair() allocates resources with qemu_vfio_dma_map(), but we never release them. Do it in nvme_free_queue() which is called from nvme_free_queue_pair().

Reported by valgrind:

==252858== 520,192 bytes in 1 blocks are still reachable in loss record 8,293 of 8,302

  ==252858==    at 0x4846803: memalign (vg_replace_malloc.c:1265)
  ==252858==    by 0x484691F: posix_memalign (vg_replace_malloc.c:1429)
  ==252858==    by 0xB8AFE4: qemu_try_memalign (oslib-posix.c:210)
  ==252858==    by 0xA9E315: nvme_create_queue_pair (nvme.c:229)
  ==252858==    by 0xAA0125: nvme_init (nvme.c:799)
  ==252858==    by 0xAA081C: nvme_file_open (nvme.c:953)
  ==252858==    by 0xA23DDD: bdrv_open_driver (block.c:1550)
  ==252858==    by 0xA24806: bdrv_open_common (block.c:1827)
  ==252858==    by 0xA2889B: bdrv_open_inherit (block.c:3747)
  ==252858==    by 0xA28DE4: bdrv_open (block.c:3840)
  ==252858==    by 0x9E0F8E: bds_tree_init (blockdev.c:675)
  ==252858==    by 0x9E7C74: qmp_blockdev_add (blockdev.c:3551)
Fixes bdd6a90a9e5 ("block: Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +0 -0
index 6e476f54b9f..903c8ffa060 100644
@@ -185,6 +185,7 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
 
Quoted 9 lines

block/nvme.c static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q, · line 190

Quoted 5 lines
block/nvme.c +1 -0
 static void nvme_free_queue(BDRVNVMeState *s, NVMeQueue *q)
 {
+    qemu_vfio_dma_unmap(s->vfio, q->queue);
     qemu_vfree(q->queue);
 }

I can't figure out the issue. qemu_vfree(q->queue) was already called before this patch. How does adding qemu_vfio_dma_unmap() help with the valgrind report in the commit description?

Quoted 2 lines

You are right, I think I didn't select the correct record between the 8302 reported by valgrind. I will revisit, thanks.

Should we still merge (parts of) this series for 6.2? Or does this mean that we don't want it at all?

Kevin

#14 lore raw

Re: [PATCH 5/5] block/nvme: Fix memory leak from nvme_init_queue()

Quoted 45 lines · Kevin Wolf

Am 07.10.2021 um 15:34 hat Philippe Mathieu-Daudé geschrieben:

Quoted 44 lines · Stefan Hajnoczi
Quoted 42 lines · Philippe Mathieu-Daudé
Quoted 31 lines

nvme_create_queue_pair() allocates resources with qemu_vfio_dma_map(), but we never release them. Do it in nvme_free_queue() which is called from nvme_free_queue_pair().

Reported by valgrind:

==252858== 520,192 bytes in 1 blocks are still reachable in loss record 8,293 of 8,302

  ==252858==    at 0x4846803: memalign (vg_replace_malloc.c:1265)
  ==252858==    by 0x484691F: posix_memalign (vg_replace_malloc.c:1429)
  ==252858==    by 0xB8AFE4: qemu_try_memalign (oslib-posix.c:210)
  ==252858==    by 0xA9E315: nvme_create_queue_pair (nvme.c:229)
  ==252858==    by 0xAA0125: nvme_init (nvme.c:799)
  ==252858==    by 0xAA081C: nvme_file_open (nvme.c:953)
  ==252858==    by 0xA23DDD: bdrv_open_driver (block.c:1550)
  ==252858==    by 0xA24806: bdrv_open_common (block.c:1827)
  ==252858==    by 0xA2889B: bdrv_open_inherit (block.c:3747)
  ==252858==    by 0xA28DE4: bdrv_open (block.c:3840)
  ==252858==    by 0x9E0F8E: bds_tree_init (blockdev.c:675)
  ==252858==    by 0x9E7C74: qmp_blockdev_add (blockdev.c:3551)
Fixes bdd6a90a9e5 ("block: Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +0 -0
index 6e476f54b9f..903c8ffa060 100644

block/nvme.c static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q, · line 190

Quoted 6 lines
block/nvme.c +1 -0
@@ -185,6 +185,7 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
 
 static void nvme_free_queue(BDRVNVMeState *s, NVMeQueue *q)
 {
+    qemu_vfio_dma_unmap(s->vfio, q->queue);
     qemu_vfree(q->queue);
 }

I can't figure out the issue. qemu_vfree(q->queue) was already called before this patch. How does adding qemu_vfio_dma_unmap() help with the valgrind report in the commit description?

Quoted 5 lines
Quoted 2 lines

You are right, I think I didn't select the correct record between the 8302 reported by valgrind. I will revisit, thanks.

Should we still merge (parts of) this series for 6.2? Or does this mean that we don't want it at all?

Patches #1-4 are cleanups welcome for 6.2 :) However we do not want #5.

#15 lore raw

Re: [PATCH 5/5] block/nvme: Fix memory leak from nvme_init_queue()

Am 02.11.2021 um 13:36 hat Philippe Mathieu-Daudé geschrieben:

Quoted 46 lines · Kevin Wolf
Quoted 45 lines

Am 07.10.2021 um 15:34 hat Philippe Mathieu-Daudé geschrieben:

Quoted 44 lines · Stefan Hajnoczi
Quoted 42 lines · Philippe Mathieu-Daudé
Quoted 31 lines

nvme_create_queue_pair() allocates resources with qemu_vfio_dma_map(), but we never release them. Do it in nvme_free_queue() which is called from nvme_free_queue_pair().

Reported by valgrind:

==252858== 520,192 bytes in 1 blocks are still reachable in loss record 8,293 of 8,302

  ==252858==    at 0x4846803: memalign (vg_replace_malloc.c:1265)
  ==252858==    by 0x484691F: posix_memalign (vg_replace_malloc.c:1429)
  ==252858==    by 0xB8AFE4: qemu_try_memalign (oslib-posix.c:210)
  ==252858==    by 0xA9E315: nvme_create_queue_pair (nvme.c:229)
  ==252858==    by 0xAA0125: nvme_init (nvme.c:799)
  ==252858==    by 0xAA081C: nvme_file_open (nvme.c:953)
  ==252858==    by 0xA23DDD: bdrv_open_driver (block.c:1550)
  ==252858==    by 0xA24806: bdrv_open_common (block.c:1827)
  ==252858==    by 0xA2889B: bdrv_open_inherit (block.c:3747)
  ==252858==    by 0xA28DE4: bdrv_open (block.c:3840)
  ==252858==    by 0x9E0F8E: bds_tree_init (blockdev.c:675)
  ==252858==    by 0x9E7C74: qmp_blockdev_add (blockdev.c:3551)
Fixes bdd6a90a9e5 ("block: Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +0 -0
index 6e476f54b9f..903c8ffa060 100644

block/nvme.c static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q, · line 190

Quoted 6 lines
block/nvme.c +1 -0
@@ -185,6 +185,7 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
 
 static void nvme_free_queue(BDRVNVMeState *s, NVMeQueue *q)
 {
+    qemu_vfio_dma_unmap(s->vfio, q->queue);
     qemu_vfree(q->queue);
 }

I can't figure out the issue. qemu_vfree(q->queue) was already called before this patch. How does adding qemu_vfio_dma_unmap() help with the valgrind report in the commit description?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

You are right, I think I didn't select the correct record between the 8302 reported by valgrind. I will revisit, thanks.

Should we still merge (parts of) this series for 6.2? Or does this mean that we don't want it at all?

Patches #1-4 are cleanups welcome for 6.2 :) However we do not want #5.

Thanks. Patch 4 doesn't seem to make sense without 5 (and definitely not without rewriting the commit message), but I'm applying patches 1-3.

Kevin

#16 lore raw

Re: [PATCH 5/5] block/nvme: Fix memory leak from nvme_init_queue()

Quoted 10 lines · Kevin Wolf

Am 02.11.2021 um 13:36 hat Philippe Mathieu-Daudé geschrieben:

Quoted 6 lines · Kevin Wolf
Quoted 3 lines

Should we still merge (parts of) this series for 6.2? Or does this mean that we don't want it at all?

Patches #1-4 are cleanups welcome for 6.2 :) However we do not want #5.

Thanks. Patch 4 doesn't seem to make sense without 5 (and definitely not without rewriting the commit message), but I'm applying patches 1-3.

Oops indeed. Thank you, that helps.