mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe()
@ 2025-11-05  3:53 Zilin Guan
  2025-11-05  8:53 ` Qu Wenruo
  2025-11-05 10:26 ` Markus Elfring
  0 siblings, 2 replies; 7+ messages in thread
From: Zilin Guan @ 2025-11-05  3:53 UTC (permalink / raw)
  To: clm; +Cc: dsterba, linux-btrfs, linux-kernel, jianhao.xu, Zilin Guan

scrub_raid56_parity_stripe() allocates a bio with bio_alloc(), but
fails to release it on some error paths, leading to a potential
memory leak.

Add the missing bio_put() calls to properly drop the bio reference
in those error cases.

Fixes: 1009254bf22a3 ("btrfs: scrub: use scrub_stripe to implement RAID56 P/Q scrub")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
 fs/btrfs/scrub.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 651b11884f82..ba20d9286a34 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -2203,6 +2203,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
 	ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
 			      &length, &bioc, NULL, NULL);
 	if (ret < 0) {
+		bio_put(bio);
 		btrfs_put_bioc(bioc);
 		btrfs_bio_counter_dec(fs_info);
 		goto out;
@@ -2212,6 +2213,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
 	btrfs_put_bioc(bioc);
 	if (!rbio) {
 		ret = -ENOMEM;
+		bio_put(bio);
 		btrfs_bio_counter_dec(fs_info);
 		goto out;
 	}
-- 
2.34.1


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

* Re: [PATCH] btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe()
  2025-11-05  3:53 [PATCH] btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe() Zilin Guan
@ 2025-11-05  8:53 ` Qu Wenruo
  2025-11-05 10:26 ` Markus Elfring
  1 sibling, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2025-11-05  8:53 UTC (permalink / raw)
  To: Zilin Guan, clm; +Cc: dsterba, linux-btrfs, linux-kernel, jianhao.xu



在 2025/11/5 14:23, Zilin Guan 写道:
> scrub_raid56_parity_stripe() allocates a bio with bio_alloc(), but
> fails to release it on some error paths, leading to a potential
> memory leak.
> 
> Add the missing bio_put() calls to properly drop the bio reference
> in those error cases.
> 
> Fixes: 1009254bf22a3 ("btrfs: scrub: use scrub_stripe to implement RAID56 P/Q scrub")
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Merged into for-next branch.

Thanks,
Qu
> ---
>   fs/btrfs/scrub.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
> index 651b11884f82..ba20d9286a34 100644
> --- a/fs/btrfs/scrub.c
> +++ b/fs/btrfs/scrub.c
> @@ -2203,6 +2203,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
>   	ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
>   			      &length, &bioc, NULL, NULL);
>   	if (ret < 0) {
> +		bio_put(bio);
>   		btrfs_put_bioc(bioc);
>   		btrfs_bio_counter_dec(fs_info);
>   		goto out;
> @@ -2212,6 +2213,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
>   	btrfs_put_bioc(bioc);
>   	if (!rbio) {
>   		ret = -ENOMEM;
> +		bio_put(bio);
>   		btrfs_bio_counter_dec(fs_info);
>   		goto out;
>   	}


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

* Re: [PATCH] btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe()
  2025-11-05  3:53 [PATCH] btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe() Zilin Guan
  2025-11-05  8:53 ` Qu Wenruo
@ 2025-11-05 10:26 ` Markus Elfring
  2025-11-06  2:12   ` Qu Wenruo
  1 sibling, 1 reply; 7+ messages in thread
From: Markus Elfring @ 2025-11-05 10:26 UTC (permalink / raw)
  To: Zilin Guan, linux-btrfs, Chris Mason; +Cc: LKML, David Sterba, Jianhao Xu

…
> Add the missing bio_put() calls to properly drop the bio reference
> in those error cases.

How do you think about to use additional labels?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.18-rc4#n526

Regards,
Markus

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

* Re: [PATCH] btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe()
  2025-11-05 10:26 ` Markus Elfring
@ 2025-11-06  2:12   ` Qu Wenruo
  2025-11-06  6:55     ` Markus Elfring
  0 siblings, 1 reply; 7+ messages in thread
From: Qu Wenruo @ 2025-11-06  2:12 UTC (permalink / raw)
  To: Markus Elfring, Zilin Guan, linux-btrfs, Chris Mason
  Cc: LKML, David Sterba, Jianhao Xu



在 2025/11/5 20:56, Markus Elfring 写道:
> …
>> Add the missing bio_put() calls to properly drop the bio reference
>> in those error cases.
> 
> How do you think about to use additional labels?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.18-rc4#n526

I believe the current hot fix is fine.

As in the long run we're going to use on-stack bio for this particular 
call site.

Thanks,
Qu

> 
> Regards,
> Markus
> 


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

* Re: btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe()
  2025-11-06  2:12   ` Qu Wenruo
@ 2025-11-06  6:55     ` Markus Elfring
  2025-11-06  7:02       ` Qu Wenruo
  0 siblings, 1 reply; 7+ messages in thread
From: Markus Elfring @ 2025-11-06  6:55 UTC (permalink / raw)
  To: Qu Wenruo, Zilin Guan, linux-btrfs, Chris Mason
  Cc: LKML, David Sterba, Jianhao Xu

>> …
>>> Add the missing bio_put() calls to properly drop the bio reference
>>> in those error cases.
>>
>> How do you think about to use additional labels?
>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.18-rc4#n526
> 
> I believe the current hot fix is fine.
…

Would you like to care a bit more for avoidance of duplicate source code
(also according to better exception handling)?

Regards,
Markus

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

* Re: btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe()
  2025-11-06  6:55     ` Markus Elfring
@ 2025-11-06  7:02       ` Qu Wenruo
  2025-11-06  7:19         ` Markus Elfring
  0 siblings, 1 reply; 7+ messages in thread
From: Qu Wenruo @ 2025-11-06  7:02 UTC (permalink / raw)
  To: Markus Elfring, Zilin Guan, linux-btrfs, Chris Mason
  Cc: LKML, David Sterba, Jianhao Xu



在 2025/11/6 17:25, Markus Elfring 写道:
>>> …
>>>> Add the missing bio_put() calls to properly drop the bio reference
>>>> in those error cases.
>>>
>>> How do you think about to use additional labels?
>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.18-rc4#n526
>>
>> I believe the current hot fix is fine.
> …
> 
> Would you like to care a bit more for avoidance of duplicate source code
> (also according to better exception handling)?

That is handled in this patch, a dedicated cleanup/refactor:

https://lore.kernel.org/linux-btrfs/2d2cfb7729a65d88ea8b9d6408611d0cc76e1ab7.1762398098.git.wqu@suse.com/T/#u

To be honest, doing that extra tag inside the original 
scrub_raid56_parity_stripe() is not that helpful, that function is doing 
a lot of different works, and @bio is only allocated for the last two 
stages (using cached stripes and to wait for the scrub of parity stripe).

Doing a dedicated tag for less than half of the code is not going to 
improve readability.

It's better to extract the unrelated behavior into a dedicated helper so 
that it's way straight forward.

Thanks,
Qu

> 
> Regards,
> Markus
> 


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

* Re: btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe()
  2025-11-06  7:02       ` Qu Wenruo
@ 2025-11-06  7:19         ` Markus Elfring
  0 siblings, 0 replies; 7+ messages in thread
From: Markus Elfring @ 2025-11-06  7:19 UTC (permalink / raw)
  To: Qu Wenruo, linux-btrfs
  Cc: Qu Wenruo, LKML, Chris Mason, David Sterba, Jianhao Xu, Zilin Guan

> That is handled in this patch, a dedicated cleanup/refactor:
> 
> https://lore.kernel.org/linux-btrfs/2d2cfb7729a65d88ea8b9d6408611d0cc76e1ab7.1762398098.git.wqu@suse.com/T/#u

Thanks for another bit of collateral evolution.
[PATCH] btrfs: extract the parity scrub code into a helper

Regards,
Markus

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

end of thread, other threads:[~2025-11-06  7:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-05  3:53 [PATCH] btrfs: scrub: fix memory leak in scrub_raid56_parity_stripe() Zilin Guan
2025-11-05  8:53 ` Qu Wenruo
2025-11-05 10:26 ` Markus Elfring
2025-11-06  2:12   ` Qu Wenruo
2025-11-06  6:55     ` Markus Elfring
2025-11-06  7:02       ` Qu Wenruo
2025-11-06  7:19         ` Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®