Fix two potential memory leak
v2 cover 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(-)
Signature · 1 line
2.31.1
PATCH 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.
--- fs/ext4/fast_commit.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ext4/fast_commit.c +5 -3
@@ -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 ; }
Signature · 1 line
2.31.1
Re: [PATCH -next v2 1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode()
Quoted 5 lines · 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 <[email protected]>
Still looks good to me. I think this didn't change since last time, did it? Feel free to add:
Honza
Quoted 30 lines
--- fs/ext4/fast_commit.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)fs/ext4/fast_commit.c +5
-3@@ -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 ; }Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v2 2/3 ext4: fix potential memory leak in ext4_fc_record_regions()
In reply to Ye BinAs 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.
--- fs/ext4/fast_commit.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
fs/ext4/fast_commit.c +8 -6
@@ -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;
Signature · 1 line
2.31.1
Re: [PATCH -next v2 2/3] ext4: fix potential memory leak in ext4_fc_record_regions()
Quoted 5 lines · 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 <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 35 lines
--- fs/ext4/fast_commit.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)fs/ext4/fast_commit.c +8
-6@@ -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;Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v2 3/3 ext4: update 'state->fc_regions_size' after successful memory allocation
In reply to Ye BinTo 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.
--- fs/ext4/fast_commit.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
fs/ext4/fast_commit.c +5 -4
@@ -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++];
Signature · 1 line
2.31.1
Re: [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation
Quoted 5 lines · 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 <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 31 lines
--- fs/ext4/fast_commit.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)fs/ext4/fast_commit.c +5
-4@@ -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++];Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
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 :
Quoted 44 lines · Ye Bin
Quoted 5 lines
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
Quoted 31 lines
--- fs/ext4/fast_commit.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)fs/ext4/fast_commit.c +5
-4@@ -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++];Signature · 1 line
2.31.1
Quoted 10 lines · 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 inext4_fc_record_modified_inode()
ext4: fix potential memory leak inext4_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: 24e597348a4676fb1e573faaea05eb9e6a4b422cBest regards,
Signature · 1 line
Theodore Ts'o <[email protected]>