nvme-tcp: fix memory leak when freeing a queue
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
@@ -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
Sagi Grimberg
maintainerQuoted 21 lines
Release the page frag cache when tearing down the io queues
Signed-off-by Maurizio Lombardi <[email protected]>--- drivers/nvme/host/tcp.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/nvme/host/tcp.c +1
-0@@ -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.
Quoted 29 lines · Sagi Grimberg
Quoted 19 lines
Release the page frag cache when tearing down the io queues
Signed-off-by Maurizio Lombardi <[email protected]>--- drivers/nvme/host/tcp.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/nvme/host/tcp.c +1
-0@@ -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_queueis already protected against re-entry with theNVME_TCP_Q_ALLOCATEDbit.
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
Quoted 8 lines
Quoted 7 lines
Thanks Maurizio,
Why do we need to the pf_cache.va protection?
nvme_tcp_free_queueis already protected against re-entry with theNVME_TCP_Q_ALLOCATEDbit.
Quoted 4 lines
It's not the re-entry what worried me, I thought that
nvme_tcp_free_queue()might be called beforepage_frag_alloc()had the chance to initialize thepf_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
Christoph Hellwig
maintainerQuoted 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_queueis already protected against re-entry with theNVME_TCP_Q_ALLOCATEDbit.
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 beforepage_frag_alloc()had the chance to initialize thepf_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 callnvme_tcp_destroy_io_queues()and iI guess that in that case thepf_cacheis not initialized, so theif(pf_cache.va)protection is necessary.
Sagi, does this look good to you now?
John Meneghini
Reviewed-byLooks 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_queueis already protected against re-entry with theNVME_TCP_Q_ALLOCATEDbit.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 beforepage_frag_alloc()had the chance to initialize thepf_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 callnvme_tcp_destroy_io_queues()and iI guess that in that case thepf_cacheis not initialized, so theif(pf_cache.va)protection is necessary.Sagi, does this look good to you now?
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_queueis already protected against re-entry with theNVME_TCP_Q_ALLOCATEDbit.It's not the re-entry what worried me, I thought that
nvme_tcp_free_queue()might be called beforepage_frag_alloc()had the chance to initialize thepf_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 callnvme_tcp_destroy_io_queues()and iI guess that in that case thepf_cacheis not initialized, so theif(pf_cache.va)protection is necessary.Sagi, does this look good to you now?
Yes,
Thanks,
applied to nvme-5.16.