diff mbox series

[f2fs-dev,1/2,v2] f2fs: fix wrong calculation of block age

Message ID 20230116030850.20260-1-qixiaoyu1@xiaomi.com (mailing list archive)
State Superseded
Headers show
Series [f2fs-dev,1/2,v2] f2fs: fix wrong calculation of block age | expand

Commit Message

qixiaoyu1 Jan. 16, 2023, 3:08 a.m. UTC
Currently we wrongly calculate the new block age to
old * LAST_AGE_WEIGHT / 100.

Fix it to new * (100 - LAST_AGE_WEIGHT) / 100
                + old * LAST_AGE_WEIGHT / 100.

Signed-off-by: qixiaoyu1 <qixiaoyu1@xiaomi.com>
Signed-off-by: xiongping1 <xiongping1@xiaomi.com>
---
Change log v1 -> v2:
 - fix udiv

 fs/f2fs/extent_cache.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

Comments

Chao Yu Jan. 28, 2023, 3:35 a.m. UTC | #1
On 2023/1/16 11:08, qixiaoyu1 wrote:
> Currently we wrongly calculate the new block age to
> old * LAST_AGE_WEIGHT / 100.
> 
> Fix it to new * (100 - LAST_AGE_WEIGHT) / 100
>                  + old * LAST_AGE_WEIGHT / 100.
> 
> Signed-off-by: qixiaoyu1 <qixiaoyu1@xiaomi.com>
> Signed-off-by: xiongping1 <xiongping1@xiaomi.com>
> ---
> Change log v1 -> v2:
>   - fix udiv
> 
>   fs/f2fs/extent_cache.c | 7 ++-----
>   1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/f2fs/extent_cache.c b/fs/f2fs/extent_cache.c
> index 342af24b2f8c..ad5533f178fd 100644
> --- a/fs/f2fs/extent_cache.c
> +++ b/fs/f2fs/extent_cache.c
> @@ -874,11 +874,8 @@ void f2fs_update_read_extent_tree_range_compressed(struct inode *inode,
>   static unsigned long long __calculate_block_age(unsigned long long new,
>   						unsigned long long old)
>   {
> -	unsigned long long diff;
> -
> -	diff = (new >= old) ? new - (new - old) : new + (old - new);
> -
> -	return div_u64(diff * LAST_AGE_WEIGHT, 100);
> +	return div_u64(new, 100) * (100 - LAST_AGE_WEIGHT)
> +		+ div_u64(old, 100) * LAST_AGE_WEIGHT;

How about updating as below to avoid lossing accuracy if new is less than 100?

return div_u64(new * (100 - LAST_AGE_WEIGHT), 100) +
		div_u64(old * LAST_AGE_WEIGHT, 100);

Thanks,

>   }
>   
>   /* This returns a new age and allocated blocks in ei */
qixiaoyu1 Jan. 29, 2023, 11:18 a.m. UTC | #2
On Sat, Jan 28, 2023 at 11:35:34AM +0800, Chao Yu wrote:
> On 2023/1/16 11:08, qixiaoyu1 wrote:
> > Currently we wrongly calculate the new block age to
> > old * LAST_AGE_WEIGHT / 100.
> > 
> > Fix it to new * (100 - LAST_AGE_WEIGHT) / 100
> >                  + old * LAST_AGE_WEIGHT / 100.
> > 
> > Signed-off-by: qixiaoyu1 <qixiaoyu1@xiaomi.com>
> > Signed-off-by: xiongping1 <xiongping1@xiaomi.com>
> > ---
> > Change log v1 -> v2:
> >   - fix udiv
> > 
> >   fs/f2fs/extent_cache.c | 7 ++-----
> >   1 file changed, 2 insertions(+), 5 deletions(-)
> > 
> > diff --git a/fs/f2fs/extent_cache.c b/fs/f2fs/extent_cache.c
> > index 342af24b2f8c..ad5533f178fd 100644
> > --- a/fs/f2fs/extent_cache.c
> > +++ b/fs/f2fs/extent_cache.c
> > @@ -874,11 +874,8 @@ void f2fs_update_read_extent_tree_range_compressed(struct inode *inode,
> >   static unsigned long long __calculate_block_age(unsigned long long new,
> >   						unsigned long long old)
> >   {
> > -	unsigned long long diff;
> > -
> > -	diff = (new >= old) ? new - (new - old) : new + (old - new);
> > -
> > -	return div_u64(diff * LAST_AGE_WEIGHT, 100);
> > +	return div_u64(new, 100) * (100 - LAST_AGE_WEIGHT)
> > +		+ div_u64(old, 100) * LAST_AGE_WEIGHT;
> 
> How about updating as below to avoid lossing accuracy if new is less than 100?
> 
> return div_u64(new * (100 - LAST_AGE_WEIGHT), 100) +
> 		div_u64(old * LAST_AGE_WEIGHT, 100);
> 
> Thanks,
> 

We want to avoid overflow by doing the division first. To keep the accuracy, how
about updating as below:

	res = div_u64_rem(new, 100, &rem_new) * (100 - LAST_AGE_WEIGHT)
		+ div_u64_rem(old, 100, &rem_old) * LAST_AGE_WEIGHT;
	res += rem_new * (100 - LAST_AGE_WEIGHT) / 100 + rem_old * LAST_AGE_WEIGHT / 100;
	return res;

Thanks,

> >   }
> >   /* This returns a new age and allocated blocks in ei */
qixiaoyu1 Feb. 1, 2023, 12:23 p.m. UTC | #3
> > 
> > How about updating as below to avoid lossing accuracy if new is less than 100?
> > 
> > return div_u64(new * (100 - LAST_AGE_WEIGHT), 100) +
> > 		div_u64(old * LAST_AGE_WEIGHT, 100);
> > 
> > Thanks,
> > 
> 
> We want to avoid overflow by doing the division first. To keep the accuracy, how
> about updating as below:
> 
> 	res = div_u64_rem(new, 100, &rem_new) * (100 - LAST_AGE_WEIGHT)
> 		+ div_u64_rem(old, 100, &rem_old) * LAST_AGE_WEIGHT;
> 	res += rem_new * (100 - LAST_AGE_WEIGHT) / 100 + rem_old * LAST_AGE_WEIGHT / 100;
> 	return res;
> 
> Thanks,
> 

Friendly ping

> > >   }
> > >   /* This returns a new age and allocated blocks in ei */
Chao Yu Feb. 1, 2023, 12:57 p.m. UTC | #4
On 2023/2/1 20:23, qixiaoyu wrote:
>>>
>>> How about updating as below to avoid lossing accuracy if new is less than 100?
>>>
>>> return div_u64(new * (100 - LAST_AGE_WEIGHT), 100) +
>>> 		div_u64(old * LAST_AGE_WEIGHT, 100);
>>>
>>> Thanks,
>>>
>>
>> We want to avoid overflow by doing the division first. To keep the accuracy, how

Alright,

>> about updating as below:
>>
>> 	res = div_u64_rem(new, 100, &rem_new) * (100 - LAST_AGE_WEIGHT)
>> 		+ div_u64_rem(old, 100, &rem_old) * LAST_AGE_WEIGHT;
>> 	res += rem_new * (100 - LAST_AGE_WEIGHT) / 100 + rem_old * LAST_AGE_WEIGHT / 100;
>> 	return res;

if (rem_new)
	res += rem_new * (100 - LAST_AGE_WEIGHT) / 100;
if (rem_old)
	res += rem_old * LAST_AGE_WEIGHT / 100;

Otherwise, it looks fine to me. :)

Thanks,

>>
>> Thanks,
>>
> 
> Friendly ping
> 
>>>>    }
>>>>    /* This returns a new age and allocated blocks in ei */
qixiaoyu1 Feb. 1, 2023, 2:04 p.m. UTC | #5
On Wed, Feb 01, 2023 at 08:57:33PM +0800, Chao Yu wrote:
> On 2023/2/1 20:23, qixiaoyu wrote:
> > > > 
> > > > How about updating as below to avoid lossing accuracy if new is less than 100?
> > > > 
> > > > return div_u64(new * (100 - LAST_AGE_WEIGHT), 100) +
> > > > 		div_u64(old * LAST_AGE_WEIGHT, 100);
> > > > 
> > > > Thanks,
> > > > 
> > > 
> > > We want to avoid overflow by doing the division first. To keep the accuracy, how
> 
> Alright,
> 
> > > about updating as below:
> > > 
> > > 	res = div_u64_rem(new, 100, &rem_new) * (100 - LAST_AGE_WEIGHT)
> > > 		+ div_u64_rem(old, 100, &rem_old) * LAST_AGE_WEIGHT;
> > > 	res += rem_new * (100 - LAST_AGE_WEIGHT) / 100 + rem_old * LAST_AGE_WEIGHT / 100;
> > > 	return res;
> 
> if (rem_new)
> 	res += rem_new * (100 - LAST_AGE_WEIGHT) / 100;
> if (rem_old)
> 	res += rem_old * LAST_AGE_WEIGHT / 100;
> 
> Otherwise, it looks fine to me. :)
> 
> Thanks,
> 

Thank you! I will update v3 soon :)

> > > 
> > > Thanks,
> > > 
> > 
> > Friendly ping
> > 
> > > > >    }
> > > > >    /* This returns a new age and allocated blocks in ei */
diff mbox series

Patch

diff --git a/fs/f2fs/extent_cache.c b/fs/f2fs/extent_cache.c
index 342af24b2f8c..ad5533f178fd 100644
--- a/fs/f2fs/extent_cache.c
+++ b/fs/f2fs/extent_cache.c
@@ -874,11 +874,8 @@  void f2fs_update_read_extent_tree_range_compressed(struct inode *inode,
 static unsigned long long __calculate_block_age(unsigned long long new,
 						unsigned long long old)
 {
-	unsigned long long diff;
-
-	diff = (new >= old) ? new - (new - old) : new + (old - new);
-
-	return div_u64(diff * LAST_AGE_WEIGHT, 100);
+	return div_u64(new, 100) * (100 - LAST_AGE_WEIGHT)
+		+ div_u64(old, 100) * LAST_AGE_WEIGHT;
 }
 
 /* This returns a new age and allocated blocks in ei */