Re: [RFC PATCH v2] mm: Improve pgdat_balanced() to avoid over-reclamation for higher-order allocation

From: Barry Song

Date: Wed Apr 22 2026 - 17:19:28 EST


On Wed, Apr 22, 2026 at 11:47 PM Johannes Weiner <hannes@xxxxxxxxxxx> wrote:
>
> Hi Barry,
>
> On Wed, Apr 22, 2026 at 10:18:42AM +0800, Barry Song (Xiaomi) wrote:
> > We may encounter cases where the system still has plenty of free
> > memory, but cannot satisfy higher-order allocations. On phones, we
> > have observed that bursty network transfers can cause devices to
> > heat up. Baolin and Kairui have seen similar behavior on servers.
> >
> > Currently, kswapd behaves as follows: when a higher-order allocation
> > is issued with __GFP_KSWAPD_RECLAIM, pgdat_balanced() returns false
> > because __zone_watermark_ok() fails if no suitable higher-order
> > pages exist, even when free memory is well above the high watermark.
> > As a result, kswapd_shrink_node() sets an excessively large
> > sc->nr_to_reclaim and attempts aggressive reclamation:
> >
> > for_each_managed_zone_pgdat(zone, pgdat, z, sc->reclaim_idx) {
> > sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
> > }
> >
> > We have an opportunity to re-evaluate the balance by resetting
> > sc->order to 0 after shrink_node() with the following code
> > in kswapd_shrink_node():
> > /*
> > * Fragmentation may mean that the system cannot be rebalanced for
> > * high-order allocations. If twice the allocation size has been
> > * reclaimed then recheck watermarks only at order-0 to prevent
> > * excessive reclaim.
> > */
> > if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
> > sc->order = 0;
> >
> > But we have actually scanned and over-reclaimed far more than
> > compact_gap(sc->order).
>
> Do you have traces for how much it overshoots?

I previously worked for a company that had some data. I’m no
longer engaged with them, but I’m trying to recollect the data
on a new platform I recently moved to. My impression is that
when over-reclamation occurs, the amount of free memory is
actually much higher than what compaction_suitable() would
indicate.

Will present the data once recollection is complete with the
help from Wenchao.

>
> > If higher-order allocations continue, we may see persistently high
> > kswapd CPU utilization coexisting with plenty of free memory in the
> > system.
> >
> > We may want to evaluate the situation earlier at the beginning.
> > If there is plenty of free memory, we could avoid triggering
> > reclamation with an excessively large sc->nr_to_reclaim value
> > and instead prefer compaction.
> >
> > Cc: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
> > Cc: Johannes Weiner <hannes@xxxxxxxxxxx>
> > Cc: David Hildenbrand <david@xxxxxxxxxx>
> > Cc: Michal Hocko <mhocko@xxxxxxxxxx>
> > Cc: Qi Zheng <zhengqi.arch@xxxxxxxxxxxxx>
> > Cc: Shakeel Butt <shakeel.butt@xxxxxxxxx>
> > Cc: Lorenzo Stoakes <ljs@xxxxxxxxxx>
> > Cc: Kairui Song <kasong@xxxxxxxxxxx>
> > Cc: Axel Rasmussen <axelrasmussen@xxxxxxxxxx>
> > Cc: Yuanchu Xie <yuanchu@xxxxxxxxxx>
> > Cc: Wei Xu <weixugc@xxxxxxxxxx>
> > Co-developed-by: Wang Lian <wanglian@xxxxxxxxxx>
> > Co-developed-by: Kunwu Chan <chentao@xxxxxxxxxx>
> > Signed-off-by: Barry Song (Xiaomi) <baohua@xxxxxxxxxx>
> > ---
> > -RFC v1 was "mm: net: disable kswapd for high-order network
> > buffer allocation":
> > https://lore.kernel.org/linux-mm/20251013101636.69220-1-21cnbao@xxxxxxxxx/
> >
> > mm/vmscan.c | 7 +++++++
> > 1 file changed, 7 insertions(+)
> >
> > diff --git a/mm/vmscan.c b/mm/vmscan.c
> > index bd1b1aa12581..4f9668aa8eef 100644
> > --- a/mm/vmscan.c
> > +++ b/mm/vmscan.c
> > @@ -6964,6 +6964,13 @@ static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
> > if (__zone_watermark_ok(zone, order, mark, highest_zoneidx,
> > 0, free_pages))
> > return true;
> > + /*
> > + * Free pages may be well above the watermark, but if
> > + * higher-order pages are unavailable, kswapd may still
> > + * trigger excessive reclamation.
> > + */
> > + if (order && compaction_suitable(zone, order, mark, highest_zoneidx))
> > + return true;
>
> I've tried this in the past, but it was regressing huge page requests
> under memory pressure and with higher levels of concurrency:
>
> https://lore.kernel.org/linux-mm/20250411182156.GE366747@xxxxxxxxxxx/
>
> The compaction gap is sized for a single allocation, but
> kswapd/kcompactd are a shared resource for potentially hundreds or
> thousands of incoming requests. So if there is high demand for
> contiguous memory this isn't enough - kswapd gives up too early,
> kcompactd efficiency drops, you get storms of direct
> reclaim/compaction, and still poor allocation success rates. Continued
> kswapd wakeups mean that there is ongoing unsatisfied demand. The
> system has to keep moving forward.

I guess one possibility is to check whether the gap is much
larger than compaction_gap(), to ensure there are sufficient
gaps.

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 4f9668aa8eef..428ec7266aab 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -6969,6 +6969,7 @@ static bool pgdat_balanced(pg_data_t *pgdat, int
order, int highest_zoneidx)
* higher-order pages are unavailable, kswapd may still
* trigger excessive reclamation.
*/
+ mark += 2 * num_possible_cpus() * compact_gap(order);
if (order && compaction_suitable(zone, order, mark,
highest_zoneidx))
return true;
}

On the other hand, “ongoing unsatisfied demand” may not always
make sense. For example, network code may first try higher
orders with the kswapd flag but without the direct reclamation
flag, and then quickly fall back to order-0 allocations if the
higher-order allocation fails. In many cases, higher orders do
not improve network performance, while kswapd performs
unnecessary reclamation and may further trigger more refaults.

>
> That said, it's well possible that we're overshooting that progress
> buffer due to running reclaim scans with a high order. It might be a
> better idea to look into that?

I could investigate this further, but I suspect it is mainly
because we set a very large nr_to_reclaim, assuming we want to
reach the high watermark, while we may already have been above
the high watermark in kswapd_shrink_node()?

/* Reclaim a number of pages proportional to the number of zones */
sc->nr_to_reclaim = 0;
for_each_managed_zone_pgdat(zone, pgdat, z, sc->reclaim_idx) {
sc->nr_to_reclaim += max(high_wmark_pages(zone),
SWAP_CLUSTER_MAX);
}

A rough look suggests another place where high-order may affect
scanning is here:

static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc)
{
int i;
enum zone_watermarks mark;

if (sc->nr_reclaimed >= max(sc->nr_to_reclaim, compact_gap(sc->order)))
return true;
...
}

So maybe we set something like the following instead, if we are
actually above the high watermark and are only short of
high-order pages?

sc->nr_to_reclaim = min(sc->nr_to_reclaim, 2 * compact_gap(sc->order))

Anyway, let me take a closer look.

Thanks
Barry