Re: [RFC PATCH 1/3] mm: avoid unnecessary lru drain for wp_can_reuse_anon_folio()

From: Barry Song

Date: Fri Jun 12 2026 - 00:48:54 EST


On Fri, Jun 12, 2026 at 2:10 AM Shakeel Butt <shakeel.butt@xxxxxxxxx> wrote:
>
> On Thu, Jun 11, 2026 at 06:51:22PM +0800, Barry Song (Xiaomi) wrote:
> > We always unconditionally drain the LRU before retrying anon folio
> > reuse in wp_can_reuse_anon_folio(). Instead, assume !LRU anon folios
> > are in lru_cache, and use the refcount to avoid many unnecessary LRU
> > drains.
> >
> > Signed-off-by: Barry Song (Xiaomi) <baohua@xxxxxxxxxx>
> > ---
> > mm/memory.c | 8 +++++++-
> > 1 file changed, 7 insertions(+), 1 deletion(-)
> >
> > diff --git a/mm/memory.c b/mm/memory.c
> > index 56be920c56d7..487a34377a7b 100644
> > --- a/mm/memory.c
> > +++ b/mm/memory.c
> > @@ -4193,12 +4193,18 @@ static bool wp_can_reuse_anon_folio(struct folio *folio,
> > */
> > if (folio_test_ksm(folio) || folio_ref_count(folio) > 3)
> > return false;
> > - if (!folio_test_lru(folio))
> > + if (!folio_test_lru(folio)) {
> > + /*
> > + * Assume folio is on lru_cache and holds a cache reference.
> > + */
> > + if (folio_ref_count(folio) > 2 + folio_test_swapcache(folio))
> > + return false;
>
> In your experiments, how much amount of drains were reduced due to this specific
> check?

This is a very good question. I booted the system into Ubuntu, and after
boot completed I observed:

wp_reuse_skipped_drain: 5542
do_swap_skipped_drain: 0

Then I built the kernel in a 1GB memcg using zRAM swap, and observed:

wp_reuse_skipped_drain: 25017
do_swap_skipped_drain: 43595

So in summary, even without swap-in we can save a significant number of
drains in wp_can_reuse_anon_folio(). With heavy swap-in workloads, most
of the savings come from avoiding drains in do_swap_page(), while we
still see a substantial number of skipped drains in wp_can_reuse_anon_folio().

Best Regards
Barry