Re: [PATCH v2] mm/mglru: skip oom killing for order != 0 allocs in kswapd

From: Barry Song

Date: Thu Nov 26 2026 - 06:51:19 EST


On Thu, Nov 26, 2026 at 5:56 PM Bingfang Guo <bfguo@xxxxxxxxxx> wrote:
>
> Hi, Barry!
>
> On Thu, Nov 26, 2026 at 08:40:44AM +0800, Barry Song wrote:
> > On Wed, Nov 25, 2026 at 5:32 PM Bingfang Guo <bfguo@xxxxxxxxxx> wrote:
> > [...]
> > > You are right. Maybe we can start from skipping aging in reclaim path if the
> > > youngest gen is still too young? And maybe for this path, min_ttl can have a
> > > default value? Is that a good idea?
> > >
> > > diff --git a/mm/vmscan.c b/mm/vmscan.c
> > > index d891ae46baf49..7f9b14b772d14 100644
> > > --- a/mm/vmscan.c
> > > +++ b/mm/vmscan.c
> > > @@ -5136,8 +5136,11 @@ static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq,
> > > return false;
> > >
> > > + /* avoid excessive aging for huge allocations */
> > > + if (unlikely(sc->order)) {
> > > + int gen = lru_gen_from_seq(max_seq);
> > > + unsigned long birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
> > > + return !time_is_before_jiffies(birth + min_ttl);
> > > + }
> >
> > I'm not convinced that we need to couple the aging decision with
> > min_ttl protection.
> >
> > This has been a long-standing problem: high-order allocations can
> > trigger excessive reclaim and still fail to obtain contiguous
> > memory.
> >
> > I've tried to address this twice, but haven't found a satisfactory
> > solution yet:
> >
> > https://lore.kernel.org/linux-mm/20260422021842.78495-1-baohua@xxxxxxxxxx/
> >
> > Johannes shared some thoughts on this here:
> >
> > https://lore.kernel.org/linux-mm/aejtfpBUu7gNV6Tk@xxxxxxxxxxx/
> >
> > Do you have any traces showing how many pages kswapd scans and
> > reclaims while kcompactd makes little progress toward satisfying
> > order-9 allocations?
>
> We encountered this problem when running a stability test on a 6G
> VM with 2 nodes. It populates the system with hwpoisoned pages
> per 511 pages so order-9 migrations should mostly fail. Then it
> tries to allocate order-9 pages. So the results might not quite
> reflect real scenarios, but I think that applies to unmigratable
> pages such as slab as well.
>
> When the system OOMs and kills all the processes, we have about
> 4.8 GiB memory free and:
>
> PGSCAN_KSWAPD : 162,406,932
> PGSTEAL_KSWAPD : 91,233,588
> COMPACTMIGRATE_SCANNED : 6,542,604,811 (kcompactd: 4,041,105,566, 61.8%)
> COMPACTFREE_SCANNED : 541,265,798 (kcompactd: 348,448,520, 64.4%)
> COMPACTISOLATED : 174,688,832 (2.67% of migrated)
> PGMIGRATE_SUCCESS : 100,824,439
> PGMIGRATE_FAIL : 2,022,069
>
> So seems in this case, compaction fail rate is quite high and we
> have really many scanned pages compared to the actually used size
> (about 1G).
>
> >
> > Basically, I think we need some form of throttling to prevent
> > kswapd from freeing more and more memory when there is already
> > plenty of free memory, yet compaction is making little progress
> > toward satisfying order-9 allocations.
> >
>
> Yes! And I've read your and Johannes's previous discussions. It
> also seems weird to me to keep reclaiming even if there are
> plenty of pages available there. And it's quite tricky to balance
> between that and concurrency...
>
> Looking into shrink path in kswapd,
>
> sc->nr_to_reclaim += max(high_wmark_pages(z: zone), SWAP_CLUSTER_MAX);
>
> I think this is too much, for this case. So I think maybe we can
> still reclaim for scalability, but reduce its target. At least we
> should take reclaimable memory size and free memory size into
> consideration, and rescale nr_to_reclaim.

So, conceptually, is this along the lines of what we have in mind?
I'd also be interested in hearing Johannes's thoughts on this.

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 81797b71228f..0144fe4be738 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -7450,17 +7450,31 @@ static bool prepare_kswapd_sleep(pg_data_t
*pgdat, int order,
* This is used to determine if the scanning priority needs to be raised.
*/
static bool kswapd_shrink_node(pg_data_t *pgdat,
- struct scan_control *sc)
+ struct scan_control *sc,
+ int highest_zoneidx)
{
struct zone *zone;
int z;
unsigned long nr_reclaimed = sc->nr_reclaimed;
+ bool can_compact = sc->order > 0;

/* 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) {
+ unsigned long mark = high_wmark_pages(zone);
+
+ if (can_compact && !compaction_suitable(zone,
sc->order, mark, highest_zoneidx))
+ can_compact = false;
sc->nr_to_reclaim += max(high_wmark_pages(zone),
SWAP_CLUSTER_MAX);
}
+ /*
+ * If all eligible zones are already suitable for compaction,
+ * limit further reclaim to avoid over-reclaiming due to
+ * fragmentation.
+ */
+ if (can_compact)
+ sc->nr_to_reclaim = min(sc->nr_to_reclaim,
compact_gap(sc->order));

/*
* Historically care was taken to put equal pressure on all zones but
@@ -7637,7 +7651,7 @@ static int balance_pgdat(pg_data_t *pgdat, int
order, int highest_zoneidx)
* enough pages are already being scanned that the high
* watermark would be met at 100% efficiency.
*/
- if (kswapd_shrink_node(pgdat, &sc))
+ if (kswapd_shrink_node(pgdat, &sc, highest_zoneidx))
raise_priority = false;

/*

Thanks,
Barry