Re: [PATCH -v4 1/4] mm/vmscan: fix anon-only reclaim evicting file pages when swappiness=max

From: Michal Hocko

Date: Tue Jul 28 2026 - 03:25:14 EST


On Tue 28-07-26 06:35:20, Barry Song wrote:
> On Mon, Jul 27, 2026 at 7:37 PM Michal Hocko <mhocko@xxxxxxxx> wrote:
> >
> > On Fri 24-07-26 11:34:32, Ridong wrote:
> > > From: Ridong Chen <chenridong@xxxxxxxxxx>
> > >
> > > As Qi mentioned [1], when swappiness=max (SWAPPINESS_ANON_ONLY) is set,
> > > the reclaim logic is expected to reclaim anonymous pages exclusively.
> > > However, due to the current ordering of checks in get_scan_count(),
> > > file pages may still be evicted if can_reclaim_anon_pages() returns
> > > false, which contradicts the semantics of SWAPPINESS_ANON_ONLY.
> > >
> > > Reproducer in a cgroup holding 64M of file cache, with no swap configured:
> > >
> > > Before (file cache is wrongly evicted):
> > > # cat memory.stat
> > > anon 196608
> > > file 67178496
> > > pgscan_proactive 0
> > > # echo "64M swappiness=max" > memory.reclaim
> > > # cat memory.stat
> > > anon 208896
> > > file 4096 <- page cache evicted
> > > pgsteal_proactive 16400
> > > pgscan_proactive 16400
> > >
> > > After (file cache is left intact):
> > > # cat memory.stat
> > > anon 200704
> > > file 67178496
> > > pgscan_proactive 0
> > > # echo "64M swappiness=max" > memory.reclaim
> > > -bash: echo: write error: Resource temporarily unavailable
> > > # cat memory.stat
> > > anon 208896
> > > file 67178496 <- page cache untouched
> > > pgsteal_proactive 0
> > > pgscan_proactive 0
> > >
> > > Fix this by bailing out early when SWAPPINESS_ANON_ONLY is set and no
> > > anonymous pages are reclaimable, before falling back to file reclaim.
> > >
> > > [1] https://lore.kernel.org/cgroups/7ddf3eee-5fe2-45f7-8614-c8936a039e04@xxxxxxxxx/
> > >
> > > Fixes: 68a1436bde00 ("mm: add swappiness=max arg to memory.reclaim for only anon reclaim")
> > > Suggested-by: Qi Zheng <qi.zheng@xxxxxxxxx>
> > > Acked-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
> > > Acked-by: Johannes Weiner <hannes@xxxxxxxxxxx>
> > > Reviewed-by: Muchun Song <muchun.song@xxxxxxxxx>
> > > Reviewed-by: Qi Zheng <qi.zheng@xxxxxxxxx>
> > > Reviewed-by: Barry Song <baohua@xxxxxxxxxx>
> > > Signed-off-by: Ridong Chen <chenridong@xxxxxxxxxx>
> > > ---
> > > mm/vmscan.c | 24 +++++++++++++++++-------
> > > 1 file changed, 17 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/mm/vmscan.c b/mm/vmscan.c
> > > index 35c3bb15ae96..2c689682b952 100644
> > > --- a/mm/vmscan.c
> > > +++ b/mm/vmscan.c
> > > @@ -2501,6 +2501,23 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
> > > enum scan_balance scan_balance;
> > > enum lru_list lru;
> > >
> > > + /*
> > > + * Proactive reclaim initiated by userspace for anonymous memory only.
> > > + * SWAPPINESS_ANON_ONLY is set only on the proactive reclaim path, so
> > > + * warn if it shows up elsewhere. When anon cannot be reclaimed (e.g.
> > > + * no swap), bail out instead of falling back to evicting file pages,
> > > + * which would violate the anon-only semantics.
> > > + */
> > > + if (swappiness == SWAPPINESS_ANON_ONLY) {
> > > + WARN_ON_ONCE(!sc->proactive);
> > > + if (!can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) {
> > > + memset(nr, 0, sizeof(*nr) * NR_LRU_LISTS);
> > > + return;
> > > + }
> > > + scan_balance = SCAN_ANON;
> > > + goto out;
> >
> > Rather than warning the code would be easier to understand
> > (SWAPPINESS_ANON_ONLY implying sc->proactive is very subtle assumption
> > that might change in the future) I would go with and explicit check.
> >
> > Looking at how the code is structured currently doesn't the following
> > express the intention slightly better?
> >
> > diff --git a/mm/vmscan.c b/mm/vmscan.c
> > index 35c3bb15ae96..4fd38e3f1b05 100644
> > --- a/mm/vmscan.c
> > +++ b/mm/vmscan.c
> > @@ -2503,6 +2503,10 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
> >
> > /* If we have no swap space, do not bother scanning anon folios. */
> > if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) {
> > + if (swappiness == SWAPPINESS_ANON_ONLY && sc->proactive) {
> > + memset(nr, 0, sizeof(*nr) * NR_LRU_LISTS);
> > + return;
> > + }
> > scan_balance = SCAN_FILE;
>
> The question is whether we should allow falling back to SCAN_FILE
> when swappiness == SWAPPINESS_ANON_ONLY and !sc->proactive, if 201 is
> ever allowed for non-proactive reclaim in the future. I guess not,
> since SWAPPINESS_ANON_ONLY literally means reclaiming anonymous
> memory only?

If we follow swappiness==0 then yes we should fallback if we are close
to system OOM. Now arguably SWAPPINESS_ANON_ONLY is a wierd global
policy and it is quite possible this will not be ever allowed. My
argument was not to prepare the existing code for that. I just meant to
structure the code in a way that it current assumptions are more
explicit.

--
Michal Hocko
SUSE Labs