Re: [PATCH] mm:fix duplicate accounting of free pages in should_reclaim_retry()

From: liuqiqi
Date: Mon Aug 25 2025 - 03:10:49 EST


Duplicate accounting of free pages in should_reclaim_retry() effects:
The number of retry in the __alloc_pages_slowpath() function has increased.
The execution time of the kswapd process has increased.

static inline struct page *
__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
struct alloc_context *ac)
{
......
retry:
/*
* Deal with possible cpuset update races or zonelist updates to avoid
* infinite retries.
*/
......
if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags,
did_some_progress > 0, &no_progress_loops))
goto retry;

The test program: continuously allocates 1k-sized memory through kmalloc();
counts the number of retry and the execution time of the kswapd process;
the test results also confirm this.

> Thanks. Does this have any significant runtime effects?

> In the zone_reclaimable_pages() function, if the page counts for
> NR_ZONE_INACTIVE_FILE, NR_ZONE_ACTIVE_FILE, NR_ZONE_INACTIVE_ANON,
> and NR_ZONE_ACTIVE_ANON are all zero,
> the function returns the number of free pages as the result.
>
> In this case, when should_reclaim_retry() calculates reclaimable pages,
> it will inadvertently double-count the free pages in its accounting.
>
> static inline bool
> should_reclaim_retry(gfp_t gfp_mask, unsigned order,
> struct alloc_context *ac, int alloc_flags,
> bool did_some_progress, int *no_progress_loops)
> {
> ...
> available = reclaimable = zone_reclaimable_pages(zone);
> available += zone_page_state_snapshot(zone, NR_FREE_PAGES);

https://lore.kernel.org/all/20250817120016.8dcc091c5b7114d6993a29ae@xxxxxxxxxxxxxxxxxxxx/

---
Best Regards