linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block/genhd: Fix memory leak in error path of __alloc_disk_node()
@ 2019-11-27  2:40 Keita Suzuki
  2019-12-04  9:56 ` Naohiro Aota
  2019-12-11  9:10 ` [PATCH RESEND] " Keita Suzuki
  0 siblings, 2 replies; 3+ messages in thread
From: Keita Suzuki @ 2019-11-27  2:40 UTC (permalink / raw)
  To: Jens Axboe
  Cc: linux-block, linux-kernel, keitasuzuki.park, takafumi.kubota1012

'disk->part_tbl' is malloced in disk_expand_part_tbl() and should be
freed before leaving from the error handling cases. However, current code
does not free this, causing a memory leak. Add disk_replace_part_tbl()
before freeing 'disk'.

I have tested this by randomly causing failures to the target code,
and verified on kmemleak that this memory leak does occur.

unreferenced object 0xffff888006dad500 (size 64):
  comm "systemd-udevd", pid 116, jiffies 4294895558 (age 121.716s)
  hex dump (first 32 bytes):
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<00000000eec79bf3>] disk_expand_part_tbl+0xab/0x170
    [<00000000624e7d03>] __alloc_disk_node+0xb1/0x1c0
    [<00000000ca3f4185>] 0xffffffffc01b8584
    [<000000006f88a6ee>] do_one_initcall+0x8b/0x2a4
    [<0000000016058199>] do_init_module+0xfd/0x380
    [<00000000b6fde336>] load_module+0x3fae/0x4240
    [<00000000c523d013>] __do_sys_finit_module+0x11a/0x1b0
    [<00000000f07bba26>] do_syscall_64+0x6d/0x1e0
    [<00000000979467fd>] entry_SYSCALL_64_after_hwframe+0x44/0xa9

Signed-off-by: Keita Suzuki <keitasuzuki.park@sslab.ics.keio.ac.jp>
---
 block/genhd.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/block/genhd.c b/block/genhd.c
index ff6268970ddc..8c4b63d7f507 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -1504,6 +1504,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id)
 		 */
 		seqcount_init(&disk->part0.nr_sects_seq);
 		if (hd_ref_init(&disk->part0)) {
+			disk_replace_part_tbl(disk, NULL);
 			hd_free_part(&disk->part0);
 			kfree(disk);
 			return NULL;
-- 
2.17.1


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

* Re: [PATCH] block/genhd: Fix memory leak in error path of __alloc_disk_node()
  2019-11-27  2:40 [PATCH] block/genhd: Fix memory leak in error path of __alloc_disk_node() Keita Suzuki
@ 2019-12-04  9:56 ` Naohiro Aota
  2019-12-11  9:10 ` [PATCH RESEND] " Keita Suzuki
  1 sibling, 0 replies; 3+ messages in thread
From: Naohiro Aota @ 2019-12-04  9:56 UTC (permalink / raw)
  To: Keita Suzuki; +Cc: Jens Axboe, linux-block, linux-kernel, takafumi.kubota1012

Looks good,
Reviewed-by: Naohiro Aota <naohiro.aota@wdc.com>

I think you can also add:

Fixes: 6c71013ecb7e ("block: partition: convert percpu ref")

On Wed, Nov 27, 2019 at 02:40:57AM +0000, Keita Suzuki wrote:
>'disk->part_tbl' is malloced in disk_expand_part_tbl() and should be
>freed before leaving from the error handling cases. However, current code
>does not free this, causing a memory leak. Add disk_replace_part_tbl()
>before freeing 'disk'.
>
>I have tested this by randomly causing failures to the target code,
>and verified on kmemleak that this memory leak does occur.
>
>unreferenced object 0xffff888006dad500 (size 64):
>  comm "systemd-udevd", pid 116, jiffies 4294895558 (age 121.716s)
>  hex dump (first 32 bytes):
>    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>  backtrace:
>    [<00000000eec79bf3>] disk_expand_part_tbl+0xab/0x170
>    [<00000000624e7d03>] __alloc_disk_node+0xb1/0x1c0
>    [<00000000ca3f4185>] 0xffffffffc01b8584
>    [<000000006f88a6ee>] do_one_initcall+0x8b/0x2a4
>    [<0000000016058199>] do_init_module+0xfd/0x380
>    [<00000000b6fde336>] load_module+0x3fae/0x4240
>    [<00000000c523d013>] __do_sys_finit_module+0x11a/0x1b0
>    [<00000000f07bba26>] do_syscall_64+0x6d/0x1e0
>    [<00000000979467fd>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
>
>Signed-off-by: Keita Suzuki <keitasuzuki.park@sslab.ics.keio.ac.jp>
>---
> block/genhd.c | 1 +
> 1 file changed, 1 insertion(+)
>
>diff --git a/block/genhd.c b/block/genhd.c
>index ff6268970ddc..8c4b63d7f507 100644
>--- a/block/genhd.c
>+++ b/block/genhd.c
>@@ -1504,6 +1504,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id)
> 		 */
> 		seqcount_init(&disk->part0.nr_sects_seq);
> 		if (hd_ref_init(&disk->part0)) {
>+			disk_replace_part_tbl(disk, NULL);
> 			hd_free_part(&disk->part0);
> 			kfree(disk);
> 			return NULL;
>-- 
>2.17.1
>

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

* [PATCH RESEND] block/genhd: Fix memory leak in error path of __alloc_disk_node()
  2019-11-27  2:40 [PATCH] block/genhd: Fix memory leak in error path of __alloc_disk_node() Keita Suzuki
  2019-12-04  9:56 ` Naohiro Aota
@ 2019-12-11  9:10 ` Keita Suzuki
  1 sibling, 0 replies; 3+ messages in thread
From: Keita Suzuki @ 2019-12-11  9:10 UTC (permalink / raw)
  Cc: keitasuzuki.park, takafumi.kubota1012, naohiro.aota, Jens Axboe,
	linux-block, linux-kernel

'disk->part_tbl' is malloced in disk_expand_part_tbl() and should be
freed before leaving from the error handling cases. However, current code
does not free this, causing a memory leak. Add disk_replace_part_tbl()
before freeing 'disk'.

I have tested this by randomly causing failures to the target code,
and verified on kmemleak that this memory leak does occur.

unreferenced object 0xffff888006dad500 (size 64):
  comm "systemd-udevd", pid 116, jiffies 4294895558 (age 121.716s)
  hex dump (first 32 bytes):
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<00000000eec79bf3>] disk_expand_part_tbl+0xab/0x170
    [<00000000624e7d03>] __alloc_disk_node+0xb1/0x1c0
    [<00000000ca3f4185>] 0xffffffffc01b8584
    [<000000006f88a6ee>] do_one_initcall+0x8b/0x2a4
    [<0000000016058199>] do_init_module+0xfd/0x380
    [<00000000b6fde336>] load_module+0x3fae/0x4240
    [<00000000c523d013>] __do_sys_finit_module+0x11a/0x1b0
    [<00000000f07bba26>] do_syscall_64+0x6d/0x1e0
    [<00000000979467fd>] entry_SYSCALL_64_after_hwframe+0x44/0xa9

Fixes: 6c71013ecb7e ("block: partition: convert percpu ref")

Signed-off-by: Keita Suzuki <keitasuzuki.park@sslab.ics.keio.ac.jp>
Reviewed-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 block/genhd.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/block/genhd.c b/block/genhd.c
index ff6268970ddc..8c4b63d7f507 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -1504,6 +1504,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id)
 		 */
 		seqcount_init(&disk->part0.nr_sects_seq);
 		if (hd_ref_init(&disk->part0)) {
+			disk_replace_part_tbl(disk, NULL);
 			hd_free_part(&disk->part0);
 			kfree(disk);
 			return NULL;
-- 
2.17.1


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

end of thread, other threads:[~2019-12-11  9:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-27  2:40 [PATCH] block/genhd: Fix memory leak in error path of __alloc_disk_node() Keita Suzuki
2019-12-04  9:56 ` Naohiro Aota
2019-12-11  9:10 ` [PATCH RESEND] " Keita Suzuki

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).