linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block: Fix possible memory leak for rq_wb on add_disk failure
@ 2022-10-29  7:13 Chen Zhongjin
  2022-10-30  9:34 ` Christoph Hellwig
  2022-10-31 13:32 ` Jens Axboe
  0 siblings, 2 replies; 3+ messages in thread
From: Chen Zhongjin @ 2022-10-29  7:13 UTC (permalink / raw)
  To: linux-kernel, linux-block; +Cc: axboe, mcgrof, hare, chenzhongjin

kmemleak reported memory leaks in device_add_disk():

kmemleak: 3 new suspected memory leaks

unreferenced object 0xffff88800f420800 (size 512):
  comm "modprobe", pid 4275, jiffies 4295639067 (age 223.512s)
  hex dump (first 32 bytes):
    04 00 00 00 08 00 00 00 01 00 00 00 00 00 00 00  ................
    00 e1 f5 05 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<00000000d3662699>] kmalloc_trace+0x26/0x60
    [<00000000edc7aadc>] wbt_init+0x50/0x6f0
    [<0000000069601d16>] wbt_enable_default+0x157/0x1c0
    [<0000000028fc393f>] blk_register_queue+0x2a4/0x420
    [<000000007345a042>] device_add_disk+0x6fd/0xe40
    [<0000000060e6aab0>] nbd_dev_add+0x828/0xbf0 [nbd]
    ...

It is because the memory allocated in wbt_enable_default() is not
released in device_add_disk() error path.
Normally, these memory are freed in:

del_gendisk()
  rq_qos_exit()
    rqos->ops->exit(rqos);
      wbt_exit()

So rq_qos_exit() is called to free the rq_wb memory for wbt_init().
However in the error path of device_add_disk(), only
blk_unregister_queue() is called and make rq_wb memory leaked.

Add rq_qos_exit() to the error path to fix it.

Fixes: 83cbce957446 ("block: add error handling for device_add_disk / add_disk")
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
---
 block/genhd.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/block/genhd.c b/block/genhd.c
index fee90eb98b4a..0f9769db2de8 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -527,6 +527,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
 		bdi_unregister(disk->bdi);
 out_unregister_queue:
 	blk_unregister_queue(disk);
+	rq_qos_exit(disk->queue);
 out_put_slave_dir:
 	kobject_put(disk->slave_dir);
 out_put_holder_dir:
-- 
2.17.1


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

* Re: [PATCH] block: Fix possible memory leak for rq_wb on add_disk failure
  2022-10-29  7:13 [PATCH] block: Fix possible memory leak for rq_wb on add_disk failure Chen Zhongjin
@ 2022-10-30  9:34 ` Christoph Hellwig
  2022-10-31 13:32 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2022-10-30  9:34 UTC (permalink / raw)
  To: Chen Zhongjin; +Cc: linux-kernel, linux-block, axboe, mcgrof, hare

Looks good:

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

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

* Re: [PATCH] block: Fix possible memory leak for rq_wb on add_disk failure
  2022-10-29  7:13 [PATCH] block: Fix possible memory leak for rq_wb on add_disk failure Chen Zhongjin
  2022-10-30  9:34 ` Christoph Hellwig
@ 2022-10-31 13:32 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2022-10-31 13:32 UTC (permalink / raw)
  To: linux-kernel, Chen Zhongjin, linux-block; +Cc: mcgrof, hare

On Sat, 29 Oct 2022 15:13:55 +0800, Chen Zhongjin wrote:
> kmemleak reported memory leaks in device_add_disk():
> 
> kmemleak: 3 new suspected memory leaks
> 
> unreferenced object 0xffff88800f420800 (size 512):
>   comm "modprobe", pid 4275, jiffies 4295639067 (age 223.512s)
>   hex dump (first 32 bytes):
>     04 00 00 00 08 00 00 00 01 00 00 00 00 00 00 00  ................
>     00 e1 f5 05 00 00 00 00 00 00 00 00 00 00 00 00  ................
>   backtrace:
>     [<00000000d3662699>] kmalloc_trace+0x26/0x60
>     [<00000000edc7aadc>] wbt_init+0x50/0x6f0
>     [<0000000069601d16>] wbt_enable_default+0x157/0x1c0
>     [<0000000028fc393f>] blk_register_queue+0x2a4/0x420
>     [<000000007345a042>] device_add_disk+0x6fd/0xe40
>     [<0000000060e6aab0>] nbd_dev_add+0x828/0xbf0 [nbd]
>     ...
> 
> [...]

Applied, thanks!

[1/1] block: Fix possible memory leak for rq_wb on add_disk failure
      (no commit info)

Best regards,
-- 
Jens Axboe



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

end of thread, other threads:[~2022-10-31 13:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-29  7:13 [PATCH] block: Fix possible memory leak for rq_wb on add_disk failure Chen Zhongjin
2022-10-30  9:34 ` Christoph Hellwig
2022-10-31 13:32 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).