[PATCH 7/7] ext4: set the type of max_zeroout to unsigned int to avoid overflow

Baokun Li posted 7 patches 2 years ago
There is a newer version of this series
[PATCH 7/7] ext4: set the type of max_zeroout to unsigned int to avoid overflow
Posted by Baokun Li 2 years ago
The max_zeroout is of type int and the s_extent_max_zeroout_kb is of
type uint, and the s_extent_max_zeroout_kb can be freely modified via
the sysfs interface. When the block size is 1024, max_zeroout may
overflow, so declare it as unsigned int to avoid overflow.

Signed-off-by: Baokun Li <libaokun1@huawei.com>
---
 fs/ext4/extents.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 01299b55a567..8653b13e8248 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -3425,10 +3425,8 @@ static int ext4_ext_convert_to_initialized(handle_t *handle,
 	struct ext4_extent zero_ex1, zero_ex2;
 	struct ext4_extent *ex, *abut_ex;
 	ext4_lblk_t ee_block, eof_block;
-	unsigned int ee_len, depth, map_len = map->m_len;
-	int allocated = 0, max_zeroout = 0;
-	int err = 0;
-	int split_flag = EXT4_EXT_DATA_VALID2;
+	unsigned int ee_len, depth, map_len = map->m_len, max_zeroout = 0;
+	int err = 0, allocated = 0, split_flag = EXT4_EXT_DATA_VALID2;
 
 	ext_debug(inode, "logical block %llu, max_blocks %u\n",
 		  (unsigned long long)map->m_lblk, map_len);
-- 
2.31.1
Re: [PATCH 7/7] ext4: set the type of max_zeroout to unsigned int to avoid overflow
Posted by Jan Kara 1 year, 12 months ago
On Fri 26-01-24 16:57:16, Baokun Li wrote:
> The max_zeroout is of type int and the s_extent_max_zeroout_kb is of
> type uint, and the s_extent_max_zeroout_kb can be freely modified via
> the sysfs interface. When the block size is 1024, max_zeroout may
> overflow, so declare it as unsigned int to avoid overflow.
> 
> Signed-off-by: Baokun Li <libaokun1@huawei.com>
> ---
>  fs/ext4/extents.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
> index 01299b55a567..8653b13e8248 100644
> --- a/fs/ext4/extents.c
> +++ b/fs/ext4/extents.c
> @@ -3425,10 +3425,8 @@ static int ext4_ext_convert_to_initialized(handle_t *handle,
>  	struct ext4_extent zero_ex1, zero_ex2;
>  	struct ext4_extent *ex, *abut_ex;
>  	ext4_lblk_t ee_block, eof_block;
> -	unsigned int ee_len, depth, map_len = map->m_len;
> -	int allocated = 0, max_zeroout = 0;
> -	int err = 0;
> -	int split_flag = EXT4_EXT_DATA_VALID2;
> +	unsigned int ee_len, depth, map_len = map->m_len, max_zeroout = 0;
> +	int err = 0, allocated = 0, split_flag = EXT4_EXT_DATA_VALID2;

Honestly, I prefer if we keep unrelated variables on different lines,
especially when they have initializers. I find the code more readable that
way. So in this case:

	int err = 0;
	int split_flag = EXT4_EXT_DATA_VALID2;
	int allocated = 0;
	unsigned int max_zeroout = 0;

But otherwise the fix looks good!

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH 7/7] ext4: set the type of max_zeroout to unsigned int to avoid overflow
Posted by Baokun Li 1 year, 11 months ago
On 2024/2/14 0:38, Jan Kara wrote:
> On Fri 26-01-24 16:57:16, Baokun Li wrote:
>> The max_zeroout is of type int and the s_extent_max_zeroout_kb is of
>> type uint, and the s_extent_max_zeroout_kb can be freely modified via
>> the sysfs interface. When the block size is 1024, max_zeroout may
>> overflow, so declare it as unsigned int to avoid overflow.
>>
>> Signed-off-by: Baokun Li <libaokun1@huawei.com>
>> ---
>>   fs/ext4/extents.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
>> index 01299b55a567..8653b13e8248 100644
>> --- a/fs/ext4/extents.c
>> +++ b/fs/ext4/extents.c
>> @@ -3425,10 +3425,8 @@ static int ext4_ext_convert_to_initialized(handle_t *handle,
>>   	struct ext4_extent zero_ex1, zero_ex2;
>>   	struct ext4_extent *ex, *abut_ex;
>>   	ext4_lblk_t ee_block, eof_block;
>> -	unsigned int ee_len, depth, map_len = map->m_len;
>> -	int allocated = 0, max_zeroout = 0;
>> -	int err = 0;
>> -	int split_flag = EXT4_EXT_DATA_VALID2;
>> +	unsigned int ee_len, depth, map_len = map->m_len, max_zeroout = 0;
>> +	int err = 0, allocated = 0, split_flag = EXT4_EXT_DATA_VALID2;
> Honestly, I prefer if we keep unrelated variables on different lines,
> especially when they have initializers. I find the code more readable that
> way. So in this case:
>
> 	int err = 0;
> 	int split_flag = EXT4_EXT_DATA_VALID2;
> 	int allocated = 0;
> 	unsigned int max_zeroout = 0;
>
> But otherwise the fix looks good!
>
> 								Honza
Totally agree! I will replace it in the next version.

Thanks!
-- 
With Best Regards,
Baokun Li
.
Re: [PATCH 7/7] ext4: set the type of max_zeroout to unsigned int to avoid overflow
Posted by Zhang Yi 2 years ago
On 2024/1/26 16:57, Baokun Li wrote:
> The max_zeroout is of type int and the s_extent_max_zeroout_kb is of
> type uint, and the s_extent_max_zeroout_kb can be freely modified via
> the sysfs interface. When the block size is 1024, max_zeroout may
> overflow, so declare it as unsigned int to avoid overflow.
> 
> Signed-off-by: Baokun Li <libaokun1@huawei.com>

Looks good to me.

Reviewed-by: Zhang Yi <yi.zhang@huawei.com>

> ---
>  fs/ext4/extents.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
> index 01299b55a567..8653b13e8248 100644
> --- a/fs/ext4/extents.c
> +++ b/fs/ext4/extents.c
> @@ -3425,10 +3425,8 @@ static int ext4_ext_convert_to_initialized(handle_t *handle,
>  	struct ext4_extent zero_ex1, zero_ex2;
>  	struct ext4_extent *ex, *abut_ex;
>  	ext4_lblk_t ee_block, eof_block;
> -	unsigned int ee_len, depth, map_len = map->m_len;
> -	int allocated = 0, max_zeroout = 0;
> -	int err = 0;
> -	int split_flag = EXT4_EXT_DATA_VALID2;
> +	unsigned int ee_len, depth, map_len = map->m_len, max_zeroout = 0;
> +	int err = 0, allocated = 0, split_flag = EXT4_EXT_DATA_VALID2;
>  
>  	ext_debug(inode, "logical block %llu, max_blocks %u\n",
>  		  (unsigned long long)map->m_lblk, map_len);
>