2022-09-21 06:32:35

by Ye Bin

[permalink] [raw]
Subject: [PATCH -next v2 0/3] Fix two potential memory leak

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


2022-09-21 06:32:37

by Ye Bin

[permalink] [raw]
Subject: [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions()

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 <[email protected]>
---
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

2022-09-21 06:32:38

by Ye Bin

[permalink] [raw]
Subject: [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation

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 <[email protected]>
---
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

2022-09-21 06:33:43

by Ye Bin

[permalink] [raw]
Subject: [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode()

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 <[email protected]>
---
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

2022-09-21 11:33:26

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation

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 <[email protected]>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <[email protected]>

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 <[email protected]>
SUSE Labs, CR

2022-09-21 11:34:17

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode()

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 <[email protected]>

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

Reviewed-by: Jan Kara <[email protected]>

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 <[email protected]>
SUSE Labs, CR

2022-09-21 11:45:25

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions()

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 <[email protected]>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <[email protected]>

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 <[email protected]>
SUSE Labs, CR

2022-09-21 16:49:33

by Damien Guibouret

[permalink] [raw]
Subject: Re: [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation

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 <[email protected]>
>
> Looks good. Feel free to add:
>
> Reviewed-by: Jan Kara <[email protected]>
>
> 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
>>

2022-09-30 03:22:33

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH -next v2 0/3] Fix two potential memory leak

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 <[email protected]>