[PATCH v2 02/16] ext4: remove unnecessary s_mb_last_start

Baokun Li posted 16 patches 3 months, 2 weeks ago
There is a newer version of this series
[PATCH v2 02/16] ext4: remove unnecessary s_mb_last_start
Posted by Baokun Li 3 months, 2 weeks ago
ac->ac_g_ex.fe_start is only used in ext4_mb_find_by_goal(), but STREAM
ALLOC is activated after ext4_mb_find_by_goal() fails, so there's no need
to update ac->ac_g_ex.fe_start, remove the unnecessary s_mb_last_start.

Signed-off-by: Baokun Li <libaokun1@huawei.com>
---
 fs/ext4/ext4.h    | 1 -
 fs/ext4/mballoc.c | 2 --
 2 files changed, 3 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 9df74123e7e6..cfb60f8fbb63 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1631,7 +1631,6 @@ struct ext4_sb_info {
 	unsigned int s_max_dir_size_kb;
 	/* where last allocation was done - for stream allocation */
 	unsigned long s_mb_last_group;
-	unsigned long s_mb_last_start;
 	unsigned int s_mb_prefetch;
 	unsigned int s_mb_prefetch_limit;
 	unsigned int s_mb_best_avail_max_trim_order;
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 336d65c4f6a2..5cdae3bda072 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -2171,7 +2171,6 @@ static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
 		spin_lock(&sbi->s_md_lock);
 		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
-		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
 		spin_unlock(&sbi->s_md_lock);
 	}
 	/*
@@ -2849,7 +2848,6 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 		/* TBD: may be hot point */
 		spin_lock(&sbi->s_md_lock);
 		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
-		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
 		spin_unlock(&sbi->s_md_lock);
 	}
 
-- 
2.46.1
Re: [PATCH v2 02/16] ext4: remove unnecessary s_mb_last_start
Posted by Jan Kara 3 months, 1 week ago
On Mon 23-06-25 15:32:50, Baokun Li wrote:
> ac->ac_g_ex.fe_start is only used in ext4_mb_find_by_goal(), but STREAM
> ALLOC is activated after ext4_mb_find_by_goal() fails, so there's no need
> to update ac->ac_g_ex.fe_start, remove the unnecessary s_mb_last_start.
> 
> Signed-off-by: Baokun Li <libaokun1@huawei.com>

I'd just note that ac->ac_g_ex.fe_start is also used in
ext4_mb_collect_stats() so this change may impact the statistics gathered
there. OTOH it is questionable whether we even want to account streaming
allocation as a goal hit... Anyway, I'm fine with this, I'd just mention it
in the changelog.

Also one nit below but feel free to add:

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

> @@ -2849,7 +2848,6 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
>  		/* TBD: may be hot point */
>  		spin_lock(&sbi->s_md_lock);
>  		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
> -		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;

Maybe reset ac->ac_g_ex.fe_start to 0 instead of leaving it at some random
value? Just for the sake of defensive programming...

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH v2 02/16] ext4: remove unnecessary s_mb_last_start
Posted by Baokun Li 3 months, 1 week ago
On 2025/6/28 2:15, Jan Kara wrote:
> On Mon 23-06-25 15:32:50, Baokun Li wrote:
>> ac->ac_g_ex.fe_start is only used in ext4_mb_find_by_goal(), but STREAM
>> ALLOC is activated after ext4_mb_find_by_goal() fails, so there's no need
>> to update ac->ac_g_ex.fe_start, remove the unnecessary s_mb_last_start.
>>
>> Signed-off-by: Baokun Li <libaokun1@huawei.com>
> I'd just note that ac->ac_g_ex.fe_start is also used in
> ext4_mb_collect_stats() so this change may impact the statistics gathered
> there. OTOH it is questionable whether we even want to account streaming
> allocation as a goal hit... Anyway, I'm fine with this, I'd just mention it
> in the changelog.
Yes, I missed ext4_mb_collect_stats(). However, instead of explaining
it in the changelog, I think it would be better to move the current
s_bal_goals update to inside or after ext4_mb_find_by_goal().

Then, we could add another variable, such as s_bal_stream_goals, to
represent the hit count for global goals. This kind of statistic would
help us fine-tune the logic for optimizing inode goals and global goals.

What are your thoughts on this?
> Also one nit below but feel free to add:
>
> Reviewed-by: Jan Kara <jack@suse.cz>
Thanks for your review!
>
>> @@ -2849,7 +2848,6 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
>>   		/* TBD: may be hot point */
>>   		spin_lock(&sbi->s_md_lock);
>>   		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
>> -		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
> Maybe reset ac->ac_g_ex.fe_start to 0 instead of leaving it at some random
> value? Just for the sake of defensive programming...
>
> 								Honza

ac->ac_g_ex.fe_start holds the inode goal's start position, not a random
value. It's unused after ext4_mb_find_by_goal() (if s_bal_stream_goals is
added). Thus, I see no need for further modification. We can always re-add
it if future requirements change.


Thanks,
Baokun
Re: [PATCH v2 02/16] ext4: remove unnecessary s_mb_last_start
Posted by Jan Kara 3 months, 1 week ago
On Mon 30-06-25 11:32:16, Baokun Li wrote:
> On 2025/6/28 2:15, Jan Kara wrote:
> > On Mon 23-06-25 15:32:50, Baokun Li wrote:
> > > ac->ac_g_ex.fe_start is only used in ext4_mb_find_by_goal(), but STREAM
> > > ALLOC is activated after ext4_mb_find_by_goal() fails, so there's no need
> > > to update ac->ac_g_ex.fe_start, remove the unnecessary s_mb_last_start.
> > > 
> > > Signed-off-by: Baokun Li <libaokun1@huawei.com>
> > I'd just note that ac->ac_g_ex.fe_start is also used in
> > ext4_mb_collect_stats() so this change may impact the statistics gathered
> > there. OTOH it is questionable whether we even want to account streaming
> > allocation as a goal hit... Anyway, I'm fine with this, I'd just mention it
> > in the changelog.
> Yes, I missed ext4_mb_collect_stats(). However, instead of explaining
> it in the changelog, I think it would be better to move the current
> s_bal_goals update to inside or after ext4_mb_find_by_goal().
> 
> Then, we could add another variable, such as s_bal_stream_goals, to
> represent the hit count for global goals. This kind of statistic would
> help us fine-tune the logic for optimizing inode goals and global goals.
> 
> What are your thoughts on this?

Sure that sounds good to me.

> > > @@ -2849,7 +2848,6 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
> > >   		/* TBD: may be hot point */
> > >   		spin_lock(&sbi->s_md_lock);
> > >   		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
> > > -		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
> > Maybe reset ac->ac_g_ex.fe_start to 0 instead of leaving it at some random
> > value? Just for the sake of defensive programming...
> > 
> ac->ac_g_ex.fe_start holds the inode goal's start position, not a random
> value. It's unused after ext4_mb_find_by_goal() (if s_bal_stream_goals is
> added). Thus, I see no need for further modification. We can always re-add
> it if future requirements change.

Yeah, I was imprecise. It is not a random value. But it is not an offset in
the group we are now setting. Therefore I'd still prefer to reset fe_start
to 0 (or some invalid value like -1 to catch unexpected use).

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH v2 02/16] ext4: remove unnecessary s_mb_last_start
Posted by Baokun Li 3 months, 1 week ago
On 2025/6/30 15:31, Jan Kara wrote:
> On Mon 30-06-25 11:32:16, Baokun Li wrote:
>> On 2025/6/28 2:15, Jan Kara wrote:
>>> On Mon 23-06-25 15:32:50, Baokun Li wrote:
>>>> ac->ac_g_ex.fe_start is only used in ext4_mb_find_by_goal(), but STREAM
>>>> ALLOC is activated after ext4_mb_find_by_goal() fails, so there's no need
>>>> to update ac->ac_g_ex.fe_start, remove the unnecessary s_mb_last_start.
>>>>
>>>> Signed-off-by: Baokun Li <libaokun1@huawei.com>
>>> I'd just note that ac->ac_g_ex.fe_start is also used in
>>> ext4_mb_collect_stats() so this change may impact the statistics gathered
>>> there. OTOH it is questionable whether we even want to account streaming
>>> allocation as a goal hit... Anyway, I'm fine with this, I'd just mention it
>>> in the changelog.
>> Yes, I missed ext4_mb_collect_stats(). However, instead of explaining
>> it in the changelog, I think it would be better to move the current
>> s_bal_goals update to inside or after ext4_mb_find_by_goal().
>>
>> Then, we could add another variable, such as s_bal_stream_goals, to
>> represent the hit count for global goals. This kind of statistic would
>> help us fine-tune the logic for optimizing inode goals and global goals.
>>
>> What are your thoughts on this?
> Sure that sounds good to me.

Ok, I will add a patch to implement that logic in the next version.

>
>>>> @@ -2849,7 +2848,6 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
>>>>    		/* TBD: may be hot point */
>>>>    		spin_lock(&sbi->s_md_lock);
>>>>    		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
>>>> -		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
>>> Maybe reset ac->ac_g_ex.fe_start to 0 instead of leaving it at some random
>>> value? Just for the sake of defensive programming...
>>>
>> ac->ac_g_ex.fe_start holds the inode goal's start position, not a random
>> value. It's unused after ext4_mb_find_by_goal() (if s_bal_stream_goals is
>> added). Thus, I see no need for further modification. We can always re-add
>> it if future requirements change.
> Yeah, I was imprecise. It is not a random value. But it is not an offset in
> the group we are now setting. Therefore I'd still prefer to reset fe_start
> to 0 (or some invalid value like -1 to catch unexpected use).
>
> 								Honza

When ext4_mb_regular_allocator() fails, it might retry and get called
again. In this scenario, we can't reliably determine if ac_g_ex has
already been modified. Therefore, it might be more appropriate to set
ac_g_ex.fe_start to -1 after ext4_mb_find_by_goal() fails. We can then
skip ext4_mb_find_by_goal() when ac_g_ex.fe_start < 0.


Cheers,
Baokun
Re: [PATCH v2 02/16] ext4: remove unnecessary s_mb_last_start
Posted by Ojaswin Mujoo 2 months, 3 weeks ago
On Mon, Jun 30, 2025 at 03:52:58PM +0800, Baokun Li wrote:
> On 2025/6/30 15:31, Jan Kara wrote:
> > On Mon 30-06-25 11:32:16, Baokun Li wrote:
> > > On 2025/6/28 2:15, Jan Kara wrote:
> > > > On Mon 23-06-25 15:32:50, Baokun Li wrote:
> > > > > ac->ac_g_ex.fe_start is only used in ext4_mb_find_by_goal(), but STREAM
> > > > > ALLOC is activated after ext4_mb_find_by_goal() fails, so there's no need
> > > > > to update ac->ac_g_ex.fe_start, remove the unnecessary s_mb_last_start.
> > > > > 
> > > > > Signed-off-by: Baokun Li <libaokun1@huawei.com>
> > > > I'd just note that ac->ac_g_ex.fe_start is also used in
> > > > ext4_mb_collect_stats() so this change may impact the statistics gathered
> > > > there. OTOH it is questionable whether we even want to account streaming
> > > > allocation as a goal hit... Anyway, I'm fine with this, I'd just mention it
> > > > in the changelog.
> > > Yes, I missed ext4_mb_collect_stats(). However, instead of explaining
> > > it in the changelog, I think it would be better to move the current
> > > s_bal_goals update to inside or after ext4_mb_find_by_goal().
> > > 
> > > Then, we could add another variable, such as s_bal_stream_goals, to
> > > represent the hit count for global goals. This kind of statistic would
> > > help us fine-tune the logic for optimizing inode goals and global goals.
> > > 
> > > What are your thoughts on this?
> > Sure that sounds good to me.
> 
> Ok, I will add a patch to implement that logic in the next version.
> 
> > 
> > > > > @@ -2849,7 +2848,6 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
> > > > >    		/* TBD: may be hot point */
> > > > >    		spin_lock(&sbi->s_md_lock);
> > > > >    		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
> > > > > -		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
> > > > Maybe reset ac->ac_g_ex.fe_start to 0 instead of leaving it at some random
> > > > value? Just for the sake of defensive programming...
> > > > 
> > > ac->ac_g_ex.fe_start holds the inode goal's start position, not a random
> > > value. It's unused after ext4_mb_find_by_goal() (if s_bal_stream_goals is
> > > added). Thus, I see no need for further modification. We can always re-add
> > > it if future requirements change.
> > Yeah, I was imprecise. It is not a random value. But it is not an offset in
> > the group we are now setting. Therefore I'd still prefer to reset fe_start
> > to 0 (or some invalid value like -1 to catch unexpected use).
> > 
> > 								Honza
> 
> When ext4_mb_regular_allocator() fails, it might retry and get called
> again. In this scenario, we can't reliably determine if ac_g_ex has
> already been modified. Therefore, it might be more appropriate to set
> ac_g_ex.fe_start to -1 after ext4_mb_find_by_goal() fails. We can then
> skip ext4_mb_find_by_goal() when ac_g_ex.fe_start < 0.

Hmm idk if giving a sort of one-off special meaning to -1 would be right. 

How about resetting the original goal group and goal start in the retry
logic of ext4_mb_new_blocks()? Since we drop preallocations before
retrying, this way we might actually find our goal during the retry
(slim chance though but still).

Regards,
ojaswin
> 
> 
> Cheers,
> Baokun
>