Skip to contentSkip to conversation

block/nvme: Remove memory leak

PATCH In review Philippe Mathieu-Daudé 2 participants 3 messages Apr 14, 2020 – Apr 15, 2020 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/nvme: Remove memory leak

Fixes bdd6a90a9 ("Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +1 -0
index 7b7c0cc5d6..9f3c7ab819 100644
@@ -163,6 +163,7 @@ static void nvme_init_queue(BlockDriverState *bs, NVMeQueue *q,
     }
     r = qemu_vfio_dma_map(s->vfio, q->queue, bytes, false, &q->iova);
     if (r) {
+        qemu_vfree(q->queue);
         error_setg(errp, "Cannot map queue");
     }
 }
Signature · 1 line
2.21.1
#1 lore raw
Quoted 22 lines · Philippe Mathieu-Daudé
Fixes bdd6a90a9 ("Add VFIO based NVMe driver")
---
 block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
block/nvme.c +1 -0
index 7b7c0cc5d6..9f3c7ab819 100644
@@ -163,6 +163,7 @@ static void nvme_init_queue(BlockDriverState *bs, NVMeQueue *q,
     }
     r = qemu_vfio_dma_map(s->vfio, q->queue, bytes, false, &q->iova);
     if (r) {
+        qemu_vfree(q->queue);
         error_setg(errp, "Cannot map queue");
     }
 }
Signature · 1 line
2.21.1

Are we adding a double free with this change?

IIUC when nvme_init_queue() fails in the nvme_create_queue_pair() we already call the nvme_free_queue_pair() that frees sq.queue and cq.queue.

I think your patch make the code cleaner, freeing the buffer near the allocation during an error, maybe we can also set q->queue to NULL after the qemu_vfree().

Thanks,
Stefano

#2 lore raw
Quoted 26 lines · Stefano Garzarella
Quoted 22 lines · Philippe Mathieu-Daudé
Fixes bdd6a90a9 ("Add VFIO based NVMe driver")
---
  block/nvme.c | 1 +
  1 file changed, 1 insertion(+)
block/nvme.c +1 -0
index 7b7c0cc5d6..9f3c7ab819 100644
@@ -163,6 +163,7 @@ static void nvme_init_queue(BlockDriverState *bs, NVMeQueue *q,
      }
      r = qemu_vfio_dma_map(s->vfio, q->queue, bytes, false, &q->iova);
      if (r) {
+        qemu_vfree(q->queue);
          error_setg(errp, "Cannot map queue");
      }
  }
Signature · 1 line
2.21.1

Are we adding a double free with this change?

Quoted 3 lines

IIUC when nvme_init_queue() fails in the nvme_create_queue_pair() we already call the nvme_free_queue_pair() that frees sq.queue and cq.queue.

Argh you are right, I missed that...

Quoted 8 lines

I think your patch make the code cleaner, freeing the buffer near the allocation during an error, maybe we can also set q->queue to NULL after the qemu_vfree().

Thanks,
Stefano