Re: [PATCH] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list()

From: Yajun Deng

Date: Sun Jan 11 2026 - 09:06:27 EST




> 2026年1月11日 08:10,Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> 写道:
>
> On Fri, 9 Jan 2026 18:51:21 +0800 Yajun Deng <yajun.deng@xxxxxxxxx> wrote:
>
>> In move_to_free_list(), when a page block changes its migration type,
>> we need to update free page counts for both the old and new types.
>> Originally, this was done by two calls to account_freepages(), which
>> updates NR_FREE_PAGES and also type-specific counters. However, this
>> causes NR_FREE_PAGES to be updated twice, while the net change is zero
>> in most cases.
>>
>> This patch introduces a new function account_freepages_both() that
>> updates the statistics for both old and new migration types in one go.
>> It avoids the double update of NR_FREE_PAGES by computing the net change
>> only when the isolation status changes.
>>
>> The optimization avoid duplicate NR_FREE_PAGES updates in
>> move_to_free_list().
>
> Seems nice and LGTM.
>
>> +static inline void account_freepages_both(struct zone *zone, int nr_pages,
>> + int old_mt, int new_mt)
>> +{
>> + lockdep_assert_held(&zone->lock);
>> +
>> + bool old_isolated = is_migrate_isolate(old_mt);
>> + bool new_isolated = is_migrate_isolate(new_mt);
>
> We do permit C99 definition ordering nowadays, but I do think our eyes
> and brains prefer the old-school style.
>
> So here I'd personally prefer
>
> bool old_isolated = is_migrate_isolate(old_mt);
> bool new_isolated = is_migrate_isolate(new_mt);
>
> lockdep_assert_held(&zone->lock);
>
>
> Or simply remove the assertion - it doesn't look useful to me. If we
> aren't holding zone->lock here then the kernel is so screwed up we
> should all just go home.
>
>
Okay, I’ll remove the assertion.

Thanks.
>