* [PATCH v2] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list()
@ 2026-01-12 12:16 Yajun Deng
2026-01-12 14:50 ` Johannes Weiner
2026-01-12 14:53 ` Joshua Hahn
0 siblings, 2 replies; 4+ messages in thread
From: Yajun Deng @ 2026-01-12 12:16 UTC (permalink / raw)
To: akpm, vbabka, surenb, mhocko, jackmanb, hannes, ziy
Cc: linux-mm, linux-kernel, Yajun Deng, Joshua Hahn
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 adds a condition that updates the NR_FREE_PAGES only if one of
the two types is the isolate type. This avoids NR_FREE_PAGES being
updates twice.
The optimization avoid duplicate NR_FREE_PAGES updates in
move_to_free_list().
Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
Suggested-by: Joshua Hahn <joshua.hahnjy@gmail.com>
---
v2: remove account_freepages_both().
v1: https://lore.kernel.org/all/20260109105121.328780-1-yajun.deng@linux.dev/
---
mm/page_alloc.c | 38 +++++++++++++++++++++++++-------------
1 file changed, 25 insertions(+), 13 deletions(-)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index ebfa07632995..d56e94eb4914 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -812,6 +812,16 @@ compaction_capture(struct capture_control *capc, struct page *page,
}
#endif /* CONFIG_COMPACTION */
+static inline void account_specific_freepages(struct zone *zone, int nr_pages,
+ int migratetype)
+{
+ if (is_migrate_cma(migratetype))
+ __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages);
+ else if (migratetype == MIGRATE_HIGHATOMIC)
+ WRITE_ONCE(zone->nr_free_highatomic,
+ zone->nr_free_highatomic + nr_pages);
+}
+
static inline void account_freepages(struct zone *zone, int nr_pages,
int migratetype)
{
@@ -822,11 +832,7 @@ static inline void account_freepages(struct zone *zone, int nr_pages,
__mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages);
- if (is_migrate_cma(migratetype))
- __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages);
- else if (migratetype == MIGRATE_HIGHATOMIC)
- WRITE_ONCE(zone->nr_free_highatomic,
- zone->nr_free_highatomic + nr_pages);
+ account_specific_freepages(zone, nr_pages, migratetype);
}
/* Used for pages not on another list */
@@ -861,6 +867,8 @@ static inline void move_to_free_list(struct page *page, struct zone *zone,
{
struct free_area *area = &zone->free_area[order];
int nr_pages = 1 << order;
+ bool old_isolated = is_migrate_isolate(old_mt);
+ bool new_isolated = is_migrate_isolate(new_mt);
/* Free page moving can fail, so it happens before the type update */
VM_WARN_ONCE(get_pageblock_migratetype(page) != old_mt,
@@ -869,14 +877,18 @@ static inline void move_to_free_list(struct page *page, struct zone *zone,
list_move_tail(&page->buddy_list, &area->free_list[new_mt]);
- account_freepages(zone, -nr_pages, old_mt);
- account_freepages(zone, nr_pages, new_mt);
-
- if (order >= pageblock_order &&
- is_migrate_isolate(old_mt) != is_migrate_isolate(new_mt)) {
- if (!is_migrate_isolate(old_mt))
- nr_pages = -nr_pages;
- __mod_zone_page_state(zone, NR_FREE_PAGES_BLOCKS, nr_pages);
+ if (!old_isolated)
+ account_specific_freepages(zone, -nr_pages, old_mt);
+ if (!new_isolated)
+ account_specific_freepages(zone, nr_pages, new_mt);
+
+ /* Only update NR_FREE_PAGES if one of them is isolate type */
+ if (old_isolated != new_isolated) {
+ nr_pages = old_isolated ? nr_pages : -nr_pages;
+ __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages);
+ if (order >= pageblock_order)
+ __mod_zone_page_state(zone, NR_FREE_PAGES_BLOCKS,
+ nr_pages);
}
}
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v2] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list()
2026-01-12 12:16 [PATCH v2] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list() Yajun Deng
@ 2026-01-12 14:50 ` Johannes Weiner
2026-01-13 7:13 ` Yajun Deng
2026-01-12 14:53 ` Joshua Hahn
1 sibling, 1 reply; 4+ messages in thread
From: Johannes Weiner @ 2026-01-12 14:50 UTC (permalink / raw)
To: Yajun Deng
Cc: akpm, vbabka, surenb, mhocko, jackmanb, ziy, linux-mm,
linux-kernel, Joshua Hahn
On Mon, Jan 12, 2026 at 08:16:14PM +0800, Yajun Deng 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 adds a condition that updates the NR_FREE_PAGES only if one of
> the two types is the isolate type. This avoids NR_FREE_PAGES being
> updates twice.
>
> The optimization avoid duplicate NR_FREE_PAGES updates in
> move_to_free_list().
>
> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
> Suggested-by: Joshua Hahn <joshua.hahnjy@gmail.com>
I'm not a fan of this.
The code ends up more complicated, more lines, and fragile because the
accounting decisions are now spread out over multiple places (again).
Is it worth it? move_to_free_list() is used in page isolation, which
has to do the accounting anyway; and migratetype fallbacks, which we
are trying to avoid as much as possible. So this path shouldn't be all
that hot to begin with.
Simplicity & maintainability trumps here, IMO, unless you have hard
data showing this is worth the pain.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list()
2026-01-12 14:50 ` Johannes Weiner
@ 2026-01-13 7:13 ` Yajun Deng
0 siblings, 0 replies; 4+ messages in thread
From: Yajun Deng @ 2026-01-13 7:13 UTC (permalink / raw)
To: Johannes Weiner
Cc: akpm, vbabka, surenb, mhocko, jackmanb, ziy, linux-mm,
linux-kernel, Joshua Hahn
January 12, 2026 at 10:50 PM, "Johannes Weiner" <hannes@cmpxchg.org mailto:hannes@cmpxchg.org?to=%22Johannes%20Weiner%22%20%3Channes%40cmpxchg.org%3E > wrote:
>
> On Mon, Jan 12, 2026 at 08:16:14PM +0800, Yajun Deng 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 adds a condition that updates the NR_FREE_PAGES only if one of
> > the two types is the isolate type. This avoids NR_FREE_PAGES being
> > updates twice.
> >
> > The optimization avoid duplicate NR_FREE_PAGES updates in
> > move_to_free_list().
> >
> > Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
> > Suggested-by: Joshua Hahn <joshua.hahnjy@gmail.com>
> >
> I'm not a fan of this.
>
> The code ends up more complicated, more lines, and fragile because the
> accounting decisions are now spread out over multiple places (again).
>
How about V1? It will introduce account_freepages_both().
https://lore.kernel.org/all/20260109105121.328780-1-yajun.deng@linux.dev/
> Is it worth it? move_to_free_list() is used in page isolation, which
> has to do the accounting anyway; and migratetype fallbacks, which we
> are trying to avoid as much as possible. So this path shouldn't be all
> that hot to begin with.
>
Not all cases are of the isolation type. There are indeed duplicate caculations.
move_freepages_block() will be called in __isolate_free_page().
Both old_mt and new_mt are mergeable type in this case.
> Simplicity & maintainability trumps here, IMO, unless you have hard
> data showing this is worth the pain.
>
I'm tring to do it, but I haven't yet.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list()
2026-01-12 12:16 [PATCH v2] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list() Yajun Deng
2026-01-12 14:50 ` Johannes Weiner
@ 2026-01-12 14:53 ` Joshua Hahn
1 sibling, 0 replies; 4+ messages in thread
From: Joshua Hahn @ 2026-01-12 14:53 UTC (permalink / raw)
To: Yajun Deng
Cc: akpm, vbabka, surenb, mhocko, jackmanb, hannes, ziy, linux-mm,
linux-kernel
On Mon, 12 Jan 2026 20:16:14 +0800 Yajun Deng <yajun.deng@linux.dev> 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 adds a condition that updates the NR_FREE_PAGES only if one of
> the two types is the isolate type. This avoids NR_FREE_PAGES being
> updates twice.
>
> The optimization avoid duplicate NR_FREE_PAGES updates in
> move_to_free_list().
>
> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
> Suggested-by: Joshua Hahn <joshua.hahnjy@gmail.com>
Hello Yajun,
LGTM. Thank you for kindly accepting my suggestion!
Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-01-13 7:13 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-12 12:16 [PATCH v2] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list() Yajun Deng
2026-01-12 14:50 ` Johannes Weiner
2026-01-13 7:13 ` Yajun Deng
2026-01-12 14:53 ` Joshua Hahn
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox