public inbox for linux-nvme@lists.infradead.org
 help / color / mirror / Atom feed
From: Maurizio Lombardi <mlombard@redhat.com>
To: Sagi Grimberg <sagi@grimberg.me>
Cc: linux-nvme@lists.infradead.org, kbusch@kernel.org, axboe@fb.com,
	hch@lst.de
Subject: Re: [PATCH] nvme-tcp: fix memory leak when freeing a queue
Date: Wed, 3 Nov 2021 11:45:16 +0100	[thread overview]
Message-ID: <20211103104516.GD4840@raketa> (raw)
In-Reply-To: <a609e935-f529-d87e-a950-33367694aef5@grimberg.me>

On Wed, Nov 03, 2021 at 11:23:45AM +0200, Sagi Grimberg wrote:
> 
> > Release the page frag cache when tearing down the io queues
> > 
> > Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
> > ---
> >   drivers/nvme/host/tcp.c | 6 ++++++
> >   1 file changed, 6 insertions(+)
> > 
> > diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> > index 07156ea9d1a8..999417626b15 100644
> > --- a/drivers/nvme/host/tcp.c
> > +++ b/drivers/nvme/host/tcp.c
> > @@ -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;
> > +	}
> 
> 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".

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



  reply	other threads:[~2021-11-03 10:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-03  8:18 [PATCH] nvme-tcp: fix memory leak when freeing a queue Maurizio Lombardi
2021-11-03  9:23 ` Sagi Grimberg
2021-11-03 10:45   ` Maurizio Lombardi [this message]
2021-11-03 11:03     ` Maurizio Lombardi
2021-11-09  8:13       ` Christoph Hellwig
2021-11-09 15:37         ` John Meneghini
2021-11-09 15:54         ` Sagi Grimberg
2021-11-11 10:00 ` Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211103104516.GD4840@raketa \
    --to=mlombard@redhat.com \
    --cc=axboe@fb.com \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox