linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next v2 0/3] Fix two potential memory leak
@ 2022-09-21  6:40 Ye Bin
  2022-09-21  6:40 ` [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode() Ye Bin
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Ye Bin @ 2022-09-21  6:40 UTC (permalink / raw)
  To: tytso, adilger.kernel, linux-ext4; +Cc: linux-kernel, jack, Ye Bin

Diff v2 vs v1:
Add new patch "ext4: update 'state->fc_regions_size' after successful memory allocation"

Ye Bin (3):
  ext4: fix potential memory leak in ext4_fc_record_modified_inode()
  ext4: fix potential memory leak in ext4_fc_record_regions()
  ext4: update 'state->fc_regions_size' after successful memory
    allocation

 fs/ext4/fast_commit.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

-- 
2.31.1


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

* [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode()
  2022-09-21  6:40 [PATCH -next v2 0/3] Fix two potential memory leak Ye Bin
@ 2022-09-21  6:40 ` Ye Bin
  2022-09-21 11:31   ` Jan Kara
  2022-09-21  6:40 ` [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions() Ye Bin
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Ye Bin @ 2022-09-21  6:40 UTC (permalink / raw)
  To: tytso, adilger.kernel, linux-ext4; +Cc: linux-kernel, jack, Ye Bin

As krealloc may return NULL, in this case 'state->fc_modified_inodes'
may not be freed by krealloc, but 'state->fc_modified_inodes' already
set NULL. Then will lead to 'state->fc_modified_inodes' memory leak.

Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 fs/ext4/fast_commit.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
index 88575f3d0dc5..29a22ee0adb8 100644
--- a/fs/ext4/fast_commit.c
+++ b/fs/ext4/fast_commit.c
@@ -1486,13 +1486,15 @@ static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
 		if (state->fc_modified_inodes[i] == ino)
 			return 0;
 	if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) {
-		state->fc_modified_inodes = krealloc(
-				state->fc_modified_inodes,
+		int *fc_modified_inodes;
+
+		fc_modified_inodes = krealloc(state->fc_modified_inodes,
 				sizeof(int) * (state->fc_modified_inodes_size +
 				EXT4_FC_REPLAY_REALLOC_INCREMENT),
 				GFP_KERNEL);
-		if (!state->fc_modified_inodes)
+		if (!fc_modified_inodes)
 			return -ENOMEM;
+		state->fc_modified_inodes = fc_modified_inodes;
 		state->fc_modified_inodes_size +=
 			EXT4_FC_REPLAY_REALLOC_INCREMENT;
 	}
-- 
2.31.1


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

* [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions()
  2022-09-21  6:40 [PATCH -next v2 0/3] Fix two potential memory leak Ye Bin
  2022-09-21  6:40 ` [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode() Ye Bin
@ 2022-09-21  6:40 ` Ye Bin
  2022-09-21 11:32   ` Jan Kara
  2022-09-21  6:40 ` [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation Ye Bin
  2022-09-30  3:19 ` [PATCH -next v2 0/3] Fix two potential memory leak Theodore Ts'o
  3 siblings, 1 reply; 9+ messages in thread
From: Ye Bin @ 2022-09-21  6:40 UTC (permalink / raw)
  To: tytso, adilger.kernel, linux-ext4; +Cc: linux-kernel, jack, Ye Bin

As krealloc may return NULL, in this case 'state->fc_regions' may not be
freed by krealloc, but 'state->fc_regions' already set NULL. Then will
lead to 'state->fc_regions' memory leak.

Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 fs/ext4/fast_commit.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
index 29a22ee0adb8..f5b0fc50ed47 100644
--- a/fs/ext4/fast_commit.c
+++ b/fs/ext4/fast_commit.c
@@ -1679,15 +1679,17 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
 	if (replay && state->fc_regions_used != state->fc_regions_valid)
 		state->fc_regions_used = state->fc_regions_valid;
 	if (state->fc_regions_used == state->fc_regions_size) {
+		struct ext4_fc_alloc_region *fc_regions;
+
 		state->fc_regions_size +=
 			EXT4_FC_REPLAY_REALLOC_INCREMENT;
-		state->fc_regions = krealloc(
-					state->fc_regions,
-					state->fc_regions_size *
-					sizeof(struct ext4_fc_alloc_region),
-					GFP_KERNEL);
-		if (!state->fc_regions)
+		fc_regions = krealloc(state->fc_regions,
+				      state->fc_regions_size *
+				      sizeof(struct ext4_fc_alloc_region),
+				      GFP_KERNEL);
+		if (!fc_regions)
 			return -ENOMEM;
+		state->fc_regions = fc_regions;
 	}
 	region = &state->fc_regions[state->fc_regions_used++];
 	region->ino = ino;
-- 
2.31.1


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

* [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation
  2022-09-21  6:40 [PATCH -next v2 0/3] Fix two potential memory leak Ye Bin
  2022-09-21  6:40 ` [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode() Ye Bin
  2022-09-21  6:40 ` [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions() Ye Bin
@ 2022-09-21  6:40 ` Ye Bin
  2022-09-21 11:32   ` Jan Kara
  2022-09-30  3:19 ` [PATCH -next v2 0/3] Fix two potential memory leak Theodore Ts'o
  3 siblings, 1 reply; 9+ messages in thread
From: Ye Bin @ 2022-09-21  6:40 UTC (permalink / raw)
  To: tytso, adilger.kernel, linux-ext4; +Cc: linux-kernel, jack, Ye Bin

To avoid to 'state->fc_regions_size' mismatch with 'state->fc_regions'
when fail to reallocate 'fc_reqions',only update 'state->fc_regions_size'
after 'state->fc_regions' is allocated successfully.

Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 fs/ext4/fast_commit.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
index f5b0fc50ed47..694ab0627395 100644
--- a/fs/ext4/fast_commit.c
+++ b/fs/ext4/fast_commit.c
@@ -1681,14 +1681,15 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
 	if (state->fc_regions_used == state->fc_regions_size) {
 		struct ext4_fc_alloc_region *fc_regions;
 
-		state->fc_regions_size +=
-			EXT4_FC_REPLAY_REALLOC_INCREMENT;
 		fc_regions = krealloc(state->fc_regions,
-				      state->fc_regions_size *
-				      sizeof(struct ext4_fc_alloc_region),
+				      sizeof(struct ext4_fc_alloc_region) *
+				      (state->fc_regions_size +
+				       EXT4_FC_REPLAY_REALLOC_INCREMENT),
 				      GFP_KERNEL);
 		if (!fc_regions)
 			return -ENOMEM;
+		state->fc_regions_size +=
+			EXT4_FC_REPLAY_REALLOC_INCREMENT;
 		state->fc_regions = fc_regions;
 	}
 	region = &state->fc_regions[state->fc_regions_used++];
-- 
2.31.1


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

* Re: [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode()
  2022-09-21  6:40 ` [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode() Ye Bin
@ 2022-09-21 11:31   ` Jan Kara
  0 siblings, 0 replies; 9+ messages in thread
From: Jan Kara @ 2022-09-21 11:31 UTC (permalink / raw)
  To: Ye Bin; +Cc: tytso, adilger.kernel, linux-ext4, linux-kernel, jack

On Wed 21-09-22 14:40:38, Ye Bin wrote:
> As krealloc may return NULL, in this case 'state->fc_modified_inodes'
> may not be freed by krealloc, but 'state->fc_modified_inodes' already
> set NULL. Then will lead to 'state->fc_modified_inodes' memory leak.
> 
> Signed-off-by: Ye Bin <yebin10@huawei.com>

Still looks good to me. I think this didn't change since last time, did it?
Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/ext4/fast_commit.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
> index 88575f3d0dc5..29a22ee0adb8 100644
> --- a/fs/ext4/fast_commit.c
> +++ b/fs/ext4/fast_commit.c
> @@ -1486,13 +1486,15 @@ static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
>  		if (state->fc_modified_inodes[i] == ino)
>  			return 0;
>  	if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) {
> -		state->fc_modified_inodes = krealloc(
> -				state->fc_modified_inodes,
> +		int *fc_modified_inodes;
> +
> +		fc_modified_inodes = krealloc(state->fc_modified_inodes,
>  				sizeof(int) * (state->fc_modified_inodes_size +
>  				EXT4_FC_REPLAY_REALLOC_INCREMENT),
>  				GFP_KERNEL);
> -		if (!state->fc_modified_inodes)
> +		if (!fc_modified_inodes)
>  			return -ENOMEM;
> +		state->fc_modified_inodes = fc_modified_inodes;
>  		state->fc_modified_inodes_size +=
>  			EXT4_FC_REPLAY_REALLOC_INCREMENT;
>  	}
> -- 
> 2.31.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation
  2022-09-21  6:40 ` [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation Ye Bin
@ 2022-09-21 11:32   ` Jan Kara
  2022-09-21 16:38     ` Damien Guibouret
  0 siblings, 1 reply; 9+ messages in thread
From: Jan Kara @ 2022-09-21 11:32 UTC (permalink / raw)
  To: Ye Bin; +Cc: tytso, adilger.kernel, linux-ext4, linux-kernel, jack

On Wed 21-09-22 14:40:40, Ye Bin wrote:
> To avoid to 'state->fc_regions_size' mismatch with 'state->fc_regions'
> when fail to reallocate 'fc_reqions',only update 'state->fc_regions_size'
> after 'state->fc_regions' is allocated successfully.
> 
> Signed-off-by: Ye Bin <yebin10@huawei.com>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/ext4/fast_commit.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
> index f5b0fc50ed47..694ab0627395 100644
> --- a/fs/ext4/fast_commit.c
> +++ b/fs/ext4/fast_commit.c
> @@ -1681,14 +1681,15 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
>  	if (state->fc_regions_used == state->fc_regions_size) {
>  		struct ext4_fc_alloc_region *fc_regions;
>  
> -		state->fc_regions_size +=
> -			EXT4_FC_REPLAY_REALLOC_INCREMENT;
>  		fc_regions = krealloc(state->fc_regions,
> -				      state->fc_regions_size *
> -				      sizeof(struct ext4_fc_alloc_region),
> +				      sizeof(struct ext4_fc_alloc_region) *
> +				      (state->fc_regions_size +
> +				       EXT4_FC_REPLAY_REALLOC_INCREMENT),
>  				      GFP_KERNEL);
>  		if (!fc_regions)
>  			return -ENOMEM;
> +		state->fc_regions_size +=
> +			EXT4_FC_REPLAY_REALLOC_INCREMENT;
>  		state->fc_regions = fc_regions;
>  	}
>  	region = &state->fc_regions[state->fc_regions_used++];
> -- 
> 2.31.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions()
  2022-09-21  6:40 ` [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions() Ye Bin
@ 2022-09-21 11:32   ` Jan Kara
  0 siblings, 0 replies; 9+ messages in thread
From: Jan Kara @ 2022-09-21 11:32 UTC (permalink / raw)
  To: Ye Bin; +Cc: tytso, adilger.kernel, linux-ext4, linux-kernel, jack

On Wed 21-09-22 14:40:39, Ye Bin wrote:
> As krealloc may return NULL, in this case 'state->fc_regions' may not be
> freed by krealloc, but 'state->fc_regions' already set NULL. Then will
> lead to 'state->fc_regions' memory leak.
> 
> Signed-off-by: Ye Bin <yebin10@huawei.com>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/ext4/fast_commit.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
> index 29a22ee0adb8..f5b0fc50ed47 100644
> --- a/fs/ext4/fast_commit.c
> +++ b/fs/ext4/fast_commit.c
> @@ -1679,15 +1679,17 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
>  	if (replay && state->fc_regions_used != state->fc_regions_valid)
>  		state->fc_regions_used = state->fc_regions_valid;
>  	if (state->fc_regions_used == state->fc_regions_size) {
> +		struct ext4_fc_alloc_region *fc_regions;
> +
>  		state->fc_regions_size +=
>  			EXT4_FC_REPLAY_REALLOC_INCREMENT;
> -		state->fc_regions = krealloc(
> -					state->fc_regions,
> -					state->fc_regions_size *
> -					sizeof(struct ext4_fc_alloc_region),
> -					GFP_KERNEL);
> -		if (!state->fc_regions)
> +		fc_regions = krealloc(state->fc_regions,
> +				      state->fc_regions_size *
> +				      sizeof(struct ext4_fc_alloc_region),
> +				      GFP_KERNEL);
> +		if (!fc_regions)
>  			return -ENOMEM;
> +		state->fc_regions = fc_regions;
>  	}
>  	region = &state->fc_regions[state->fc_regions_used++];
>  	region->ino = ino;
> -- 
> 2.31.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation
  2022-09-21 11:32   ` Jan Kara
@ 2022-09-21 16:38     ` Damien Guibouret
  0 siblings, 0 replies; 9+ messages in thread
From: Damien Guibouret @ 2022-09-21 16:38 UTC (permalink / raw)
  To: Jan Kara, Ye Bin; +Cc: tytso, adilger.kernel, linux-ext4, linux-kernel

Hello,

That's fine for me.

Regards,

Damien

Le 21/09/2022 à 13:32, Jan Kara a écrit :
> On Wed 21-09-22 14:40:40, Ye Bin wrote:
>> To avoid to 'state->fc_regions_size' mismatch with 'state->fc_regions'
>> when fail to reallocate 'fc_reqions',only update 'state->fc_regions_size'
>> after 'state->fc_regions' is allocated successfully.
>>
>> Signed-off-by: Ye Bin <yebin10@huawei.com>
> 
> Looks good. Feel free to add:
> 
> Reviewed-by: Jan Kara <jack@suse.cz>
> 
> 								Honza
> 
>> ---
>>   fs/ext4/fast_commit.c | 9 +++++----
>>   1 file changed, 5 insertions(+), 4 deletions(-)
>>
>> diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
>> index f5b0fc50ed47..694ab0627395 100644
>> --- a/fs/ext4/fast_commit.c
>> +++ b/fs/ext4/fast_commit.c
>> @@ -1681,14 +1681,15 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
>>   	if (state->fc_regions_used == state->fc_regions_size) {
>>   		struct ext4_fc_alloc_region *fc_regions;
>>   
>> -		state->fc_regions_size +=
>> -			EXT4_FC_REPLAY_REALLOC_INCREMENT;
>>   		fc_regions = krealloc(state->fc_regions,
>> -				      state->fc_regions_size *
>> -				      sizeof(struct ext4_fc_alloc_region),
>> +				      sizeof(struct ext4_fc_alloc_region) *
>> +				      (state->fc_regions_size +
>> +				       EXT4_FC_REPLAY_REALLOC_INCREMENT),
>>   				      GFP_KERNEL);
>>   		if (!fc_regions)
>>   			return -ENOMEM;
>> +		state->fc_regions_size +=
>> +			EXT4_FC_REPLAY_REALLOC_INCREMENT;
>>   		state->fc_regions = fc_regions;
>>   	}
>>   	region = &state->fc_regions[state->fc_regions_used++];
>> -- 
>> 2.31.1
>>

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

* Re: [PATCH -next v2 0/3] Fix two potential memory leak
  2022-09-21  6:40 [PATCH -next v2 0/3] Fix two potential memory leak Ye Bin
                   ` (2 preceding siblings ...)
  2022-09-21  6:40 ` [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation Ye Bin
@ 2022-09-30  3:19 ` Theodore Ts'o
  3 siblings, 0 replies; 9+ messages in thread
From: Theodore Ts'o @ 2022-09-30  3:19 UTC (permalink / raw)
  To: linux-ext4, adilger.kernel, yebin10; +Cc: Theodore Ts'o, linux-kernel, jack

On Wed, 21 Sep 2022 14:40:37 +0800, Ye Bin wrote:
> Diff v2 vs v1:
> Add new patch "ext4: update 'state->fc_regions_size' after successful memory allocation"
> 
> Ye Bin (3):
>   ext4: fix potential memory leak in ext4_fc_record_modified_inode()
>   ext4: fix potential memory leak in ext4_fc_record_regions()
>   ext4: update 'state->fc_regions_size' after successful memory
>     allocation
> 
> [...]

Applied, thanks!

[1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode()
      commit: 3edc135aaf975c6022f69c47e55f4e55981aff10
[2/3] ext4: fix potential memory leak in ext4_fc_record_regions()
      commit: 87429b6248196338ae578d51ae95f2e3094f8a50
[3/3] ext4: update 'state->fc_regions_size' after successful memory allocation
      commit: 24e597348a4676fb1e573faaea05eb9e6a4b422c

Best regards,
-- 
Theodore Ts'o <tytso@mit.edu>

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

end of thread, other threads:[~2022-09-30  3:21 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-21  6:40 [PATCH -next v2 0/3] Fix two potential memory leak Ye Bin
2022-09-21  6:40 ` [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode() Ye Bin
2022-09-21 11:31   ` Jan Kara
2022-09-21  6:40 ` [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions() Ye Bin
2022-09-21 11:32   ` Jan Kara
2022-09-21  6:40 ` [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation Ye Bin
2022-09-21 11:32   ` Jan Kara
2022-09-21 16:38     ` Damien Guibouret
2022-09-30  3:19 ` [PATCH -next v2 0/3] Fix two potential memory leak Theodore Ts'o

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