Linux-NVME Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] nvme-multipath: Fix memory leak with ana_log_buf
@ 2020-02-20 20:29 Logan Gunthorpe
  2020-02-20 21:12 ` Sagi Grimberg
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Logan Gunthorpe @ 2020-02-20 20:29 UTC (permalink / raw)
  To: linux-kernel, linux-nvme
  Cc: Keith Busch, Jens Axboe, Logan Gunthorpe, Christoph Hellwig,
	Sagi Grimberg

kmemleak reports a memory leak with the ana_log_buf allocated by
nvme_mpath_init():

unreferenced object 0xffff888120e94000 (size 8208):
  comm "nvme", pid 6884, jiffies 4295020435 (age 78786.312s)
    hex dump (first 32 bytes):
      00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00  ................
      01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00  ................
    backtrace:
      [<00000000e2360188>] kmalloc_order+0x97/0xc0
      [<0000000079b18dd4>] kmalloc_order_trace+0x24/0x100
      [<00000000f50c0406>] __kmalloc+0x24c/0x2d0
      [<00000000f31a10b9>] nvme_mpath_init+0x23c/0x2b0
      [<000000005802589e>] nvme_init_identify+0x75f/0x1600
      [<0000000058ef911b>] nvme_loop_configure_admin_queue+0x26d/0x280
      [<00000000673774b9>] nvme_loop_create_ctrl+0x2a7/0x710
      [<00000000f1c7a233>] nvmf_dev_write+0xc66/0x10b9
      [<000000004199f8d0>] __vfs_write+0x50/0xa0
      [<0000000065466fef>] vfs_write+0xf3/0x280
      [<00000000b0db9a8b>] ksys_write+0xc6/0x160
      [<0000000082156b91>] __x64_sys_write+0x43/0x50
      [<00000000c34fbb6d>] do_syscall_64+0x77/0x2f0
      [<00000000bbc574c9>] entry_SYSCALL_64_after_hwframe+0x49/0xbe

nvme_mpath_init() is called by nvme_init_identify() which is called in
multiple places (nvme_reset_work(), nvme_passthru_end(), etc). This
means nvme_mpath_init() may be called multiple times before
nvme_mpath_uninit() (which is only called on nvme_free_ctrl()).

When nvme_mpath_init() is called multiple times, it overwrites the
ana_log_buf pointer with a new allocation, thus leaking the previous
allocation.

To fix this, free ana_log_buf before allocating a new one.

Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
---
 drivers/nvme/host/multipath.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 797c18337d96..a11900cf3a36 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -715,6 +715,7 @@ int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 	}
 
 	INIT_WORK(&ctrl->ana_work, nvme_ana_work);
+	kfree(ctrl->ana_log_buf);
 	ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
 	if (!ctrl->ana_log_buf) {
 		error = -ENOMEM;

base-commit: 11a48a5a18c63fd7621bb050228cebf13566e4d8
-- 
2.20.1


_______________________________________________
linux-nvme mailing list
linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nvme-multipath: Fix memory leak with ana_log_buf
  2020-02-20 20:29 [PATCH] nvme-multipath: Fix memory leak with ana_log_buf Logan Gunthorpe
@ 2020-02-20 21:12 ` Sagi Grimberg
  2020-02-20 21:14   ` Logan Gunthorpe
  2020-02-21 14:15 ` Christoph Hellwig
  2020-02-21 14:54 ` Keith Busch
  2 siblings, 1 reply; 5+ messages in thread
From: Sagi Grimberg @ 2020-02-20 21:12 UTC (permalink / raw)
  To: Logan Gunthorpe, linux-kernel, linux-nvme
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig

Reviewed-by: Sagi Grimberg <sagi@grimberg.me>

Keith, can you CC stable when applying?

_______________________________________________
linux-nvme mailing list
linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nvme-multipath: Fix memory leak with ana_log_buf
  2020-02-20 21:12 ` Sagi Grimberg
@ 2020-02-20 21:14   ` Logan Gunthorpe
  0 siblings, 0 replies; 5+ messages in thread
From: Logan Gunthorpe @ 2020-02-20 21:14 UTC (permalink / raw)
  To: Sagi Grimberg, linux-kernel, linux-nvme
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig



On 2020-02-20 2:12 p.m., Sagi Grimberg wrote:
> Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
> 
> Keith, can you CC stable when applying?

Oh, yeah, I should have added the tag:

Fixes: 0d0b660f214d ("nvme: add ANA support")

_______________________________________________
linux-nvme mailing list
linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nvme-multipath: Fix memory leak with ana_log_buf
  2020-02-20 20:29 [PATCH] nvme-multipath: Fix memory leak with ana_log_buf Logan Gunthorpe
  2020-02-20 21:12 ` Sagi Grimberg
@ 2020-02-21 14:15 ` Christoph Hellwig
  2020-02-21 14:54 ` Keith Busch
  2 siblings, 0 replies; 5+ messages in thread
From: Christoph Hellwig @ 2020-02-21 14:15 UTC (permalink / raw)
  To: Logan Gunthorpe
  Cc: Sagi Grimberg, linux-kernel, linux-nvme, Jens Axboe, Keith Busch,
	Christoph Hellwig

On Thu, Feb 20, 2020 at 01:29:53PM -0700, Logan Gunthorpe wrote:
> kmemleak reports a memory leak with the ana_log_buf allocated by
> nvme_mpath_init():

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

_______________________________________________
linux-nvme mailing list
linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nvme-multipath: Fix memory leak with ana_log_buf
  2020-02-20 20:29 [PATCH] nvme-multipath: Fix memory leak with ana_log_buf Logan Gunthorpe
  2020-02-20 21:12 ` Sagi Grimberg
  2020-02-21 14:15 ` Christoph Hellwig
@ 2020-02-21 14:54 ` Keith Busch
  2 siblings, 0 replies; 5+ messages in thread
From: Keith Busch @ 2020-02-21 14:54 UTC (permalink / raw)
  To: Logan Gunthorpe
  Cc: Jens Axboe, Sagi Grimberg, linux-kernel, linux-nvme, Christoph Hellwig

On Thu, Feb 20, 2020 at 01:29:53PM -0700, Logan Gunthorpe wrote:
> kmemleak reports a memory leak with the ana_log_buf allocated by
> nvme_mpath_init():
> 
> unreferenced object 0xffff888120e94000 (size 8208):
>   comm "nvme", pid 6884, jiffies 4295020435 (age 78786.312s)
>     hex dump (first 32 bytes):
>       00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00  ................
>       01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00  ................
>     backtrace:
>       [<00000000e2360188>] kmalloc_order+0x97/0xc0
>       [<0000000079b18dd4>] kmalloc_order_trace+0x24/0x100
>       [<00000000f50c0406>] __kmalloc+0x24c/0x2d0
>       [<00000000f31a10b9>] nvme_mpath_init+0x23c/0x2b0
>       [<000000005802589e>] nvme_init_identify+0x75f/0x1600
>       [<0000000058ef911b>] nvme_loop_configure_admin_queue+0x26d/0x280
>       [<00000000673774b9>] nvme_loop_create_ctrl+0x2a7/0x710
>       [<00000000f1c7a233>] nvmf_dev_write+0xc66/0x10b9
>       [<000000004199f8d0>] __vfs_write+0x50/0xa0
>       [<0000000065466fef>] vfs_write+0xf3/0x280
>       [<00000000b0db9a8b>] ksys_write+0xc6/0x160
>       [<0000000082156b91>] __x64_sys_write+0x43/0x50
>       [<00000000c34fbb6d>] do_syscall_64+0x77/0x2f0
>       [<00000000bbc574c9>] entry_SYSCALL_64_after_hwframe+0x49/0xbe
> 
> nvme_mpath_init() is called by nvme_init_identify() which is called in
> multiple places (nvme_reset_work(), nvme_passthru_end(), etc). This
> means nvme_mpath_init() may be called multiple times before
> nvme_mpath_uninit() (which is only called on nvme_free_ctrl()).
> 
> When nvme_mpath_init() is called multiple times, it overwrites the
> ana_log_buf pointer with a new allocation, thus leaking the previous
> allocation.
> 
> To fix this, free ana_log_buf before allocating a new one.
> 
> Signed-off-by: Logan Gunthorpe <logang@deltatee.com>

Thanks, applied to 5.6-rc3 with the reviews and the fixes tag.

_______________________________________________
linux-nvme mailing list
linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-20 20:29 [PATCH] nvme-multipath: Fix memory leak with ana_log_buf Logan Gunthorpe
2020-02-20 21:12 ` Sagi Grimberg
2020-02-20 21:14   ` Logan Gunthorpe
2020-02-21 14:15 ` Christoph Hellwig
2020-02-21 14:54 ` Keith Busch

Linux-NVME Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-nvme/0 linux-nvme/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-nvme linux-nvme/ https://lore.kernel.org/linux-nvme \
		linux-nvme@lists.infradead.org
	public-inbox-index linux-nvme

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.infradead.lists.linux-nvme


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git