Skip to contentSkip to conversation

nvme-tcp: fix memory leak when freeing a queue

PATCH Applied Maurizio Lombardi 4 participants 8 messages Nov 3, 2021 – Nov 11, 2021 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-tcp: fix memory leak when freeing a queue

Release the page frag cache when tearing down the io queues

---
 drivers/nvme/host/tcp.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/nvme/host/tcp.c +6 -0
index 07156ea9d1a8..999417626b15 100644
@@ -1229,6 +1229,7 @@ static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
 
 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
 {
+	struct page *page;
 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
 
@@ -1238,6 +1239,11 @@ static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
 	if (queue->hdr_digest || queue->data_digest)
 		nvme_tcp_free_crypto(queue);
 
+	if (queue->pf_cache.va) {
+		page = virt_to_head_page(queue->pf_cache.va);
+		__page_frag_cache_drain(page, queue->pf_cache.pagecnt_bias);
+		queue->pf_cache.va = NULL;
+	}
 	sock_release(queue->sock);
 	kfree(queue->pdu);
 	mutex_destroy(&queue->send_mutex);
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 21 lines

Release the page frag cache when tearing down the io queues

---
  drivers/nvme/host/tcp.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/nvme/host/tcp.c +1 -0
index 07156ea9d1a8..999417626b15 100644
@@ -1229,6 +1229,7 @@ static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
 
  static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
  {
+	struct page *page;
  	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
 

drivers/nvme/host/tcp.c static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid) · line 1246

Quoted 8 lines
drivers/nvme/host/tcp.c +5 -0
@@ -1238,6 +1239,11 @@ static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
  	if (queue->hdr_digest || queue->data_digest)
  		nvme_tcp_free_crypto(queue);
 
+	if (queue->pf_cache.va) {
+		page = virt_to_head_page(queue->pf_cache.va);
+		__page_frag_cache_drain(page, queue->pf_cache.pagecnt_bias);
+		queue->pf_cache.va = NULL;
+	}

Thanks Maurizio,

Why do we need to the pf_cache.va protection? nvme_tcp_free_queue is already protected against re-entry with the NVME_TCP_Q_ALLOCATED bit.

#2 lore raw
Quoted 29 lines · Sagi Grimberg
Quoted 19 lines

Release the page frag cache when tearing down the io queues

---
  drivers/nvme/host/tcp.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/nvme/host/tcp.c +1 -0
index 07156ea9d1a8..999417626b15 100644
@@ -1229,6 +1229,7 @@ static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
  static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
  {
+	struct page *page;
  	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  	struct nvme_tcp_queue *queue = &ctrl->queues[qid];

drivers/nvme/host/tcp.c static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid) · line 1245

Quoted 7 lines
drivers/nvme/host/tcp.c +5 -0
@@ -1238,6 +1239,11 @@ static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
  	if (queue->hdr_digest || queue->data_digest)
  		nvme_tcp_free_crypto(queue);
+	if (queue->pf_cache.va) {
+		page = virt_to_head_page(queue->pf_cache.va);
+		__page_frag_cache_drain(page, queue->pf_cache.pagecnt_bias);
+		queue->pf_cache.va = NULL;
+	}

Thanks Maurizio,

Quoted 4 lines

Why do we need to the pf_cache.va protection? nvme_tcp_free_queue is already protected against re-entry with the NVME_TCP_Q_ALLOCATED bit.

It's not the re-entry what worried me, I thought that nvme_tcp_free_queue() might be called before page_frag_alloc() had the chance to initialize the pf_cache, triggering a NULL pointer dereference. I am doing some tests right now and it seems not to be possible so maybe we can drop the "if".

However, we still need to set pf_cache.va to NULL because the queues might be re-used in the error code path and we have to prevent page_frag_alloc() from dereferencing a freed cache page and corrupting the memory.

Maurizio

#3 lore raw
Quoted 8 lines
Quoted 7 lines

Thanks Maurizio,

Why do we need to the pf_cache.va protection? nvme_tcp_free_queue is already protected against re-entry with the NVME_TCP_Q_ALLOCATED bit.

Quoted 4 lines

It's not the re-entry what worried me, I thought that nvme_tcp_free_queue() might be called before page_frag_alloc() had the chance to initialize the pf_cache, triggering a NULL pointer dereference. I am doing some tests right now and it seems not to be possible so maybe we can drop the "if".

Oh wait, if nvme_tcp_setup_ctrl() fails it could call nvme_tcp_destroy_io_queues() and iI guess that in that case the pf_cache is not initialized, so the if(pf_cache.va) protection is necessary.

Maurizio

#4 lore raw
Quoted 8 lines · Maurizio Lombardi
Quoted 8 lines
Quoted 7 lines

Thanks Maurizio,

Why do we need to the pf_cache.va protection? nvme_tcp_free_queue is already protected against re-entry with the NVME_TCP_Q_ALLOCATED bit.

Quoted 8 lines
Quoted 4 lines

It's not the re-entry what worried me, I thought that nvme_tcp_free_queue() might be called before page_frag_alloc() had the chance to initialize the pf_cache, triggering a NULL pointer dereference. I am doing some tests right now and it seems not to be possible so maybe we can drop the "if".

Oh wait, if nvme_tcp_setup_ctrl() fails it could call nvme_tcp_destroy_io_queues() and iI guess that in that case the pf_cache is not initialized, so the if(pf_cache.va) protection is necessary.

Sagi, does this look good to you now?

Reviewed-by
#5 lore raw

Looks good to me.

Quoted 20 lines · Christoph Hellwig
Quoted 8 lines · Maurizio Lombardi
Quoted 8 lines
Quoted 7 lines

Thanks Maurizio,

Why do we need to the pf_cache.va protection? nvme_tcp_free_queue is already protected against re-entry with the NVME_TCP_Q_ALLOCATED bit.

Quoted 8 lines
Quoted 4 lines

It's not the re-entry what worried me, I thought that nvme_tcp_free_queue() might be called before page_frag_alloc() had the chance to initialize the pf_cache, triggering a NULL pointer dereference. I am doing some tests right now and it seems not to be possible so maybe we can drop the "if".

Oh wait, if nvme_tcp_setup_ctrl() fails it could call nvme_tcp_destroy_io_queues() and iI guess that in that case the pf_cache is not initialized, so the if(pf_cache.va) protection is necessary.

Sagi, does this look good to you now?

maintainer Reviewed-by
#6 lore raw
In reply to Christoph Hellwig
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 7 lines

Thanks Maurizio,

Why do we need to the pf_cache.va protection? nvme_tcp_free_queue is already protected against re-entry with the NVME_TCP_Q_ALLOCATED bit.

It's not the re-entry what worried me, I thought that nvme_tcp_free_queue() might be called before page_frag_alloc() had the chance to initialize the pf_cache, triggering a NULL pointer dereference. I am doing some tests right now and it seems not to be possible so maybe we can drop the "if".

Quoted 5 lines
Quoted 3 lines

Oh wait, if nvme_tcp_setup_ctrl() fails it could call nvme_tcp_destroy_io_queues() and iI guess that in that case the pf_cache is not initialized, so the if(pf_cache.va) protection is necessary.

Sagi, does this look good to you now?

Yes,