Re: [RFC PATCH] mm: allow smaller large folios to use lru_cache

From: Barry Song

Date: Mon Jul 06 2026 - 06:50:07 EST


On Thu, Jul 2, 2026 at 9:00 PM David Hildenbrand (Arm) <david@xxxxxxxxxx> wrote:
[...]
> > diff --git a/include/linux/swap.h b/include/linux/swap.h
> > index 8f0f68e245ba..fce1d84aa2c8 100644
> > --- a/include/linux/swap.h
> > +++ b/include/linux/swap.h
> > @@ -322,9 +322,9 @@ static inline bool folio_may_be_lru_cached(struct folio *folio)
> > /*
> > * Holding PMD-sized folios in per-CPU LRU cache unbalances accounting.
> > * Holding small numbers of low-order mTHP folios in per-CPU LRU cache
> > - * will be sensible, but nobody has implemented and tested that yet.
> > + * will be sensible.
> > */
> > - return !folio_test_large(folio);
> > + return folio_order(folio) < PAGE_ALLOC_COSTLY_ORDER;
> > }
> >
> > extern atomic_t lru_disable_count;
>
> Not that __wp_can_reuse_large_anon_folio() currently assumes that large folios
> are not in the LRU cache.

Thanks for pointing this out. I assume we could do something
similar to wp_can_reuse_anon_folio(). It won't always help
drain lru_cache, but it could help in some cases?

diff --git a/mm/memory.c b/mm/memory.c
index a29cd38ad547..db82ac8057ac 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4143,6 +4143,12 @@ static bool
__wp_can_reuse_large_anon_folio(struct folio *folio,
folio_unlock(folio);
}

+ if (folio_may_be_lru_cached(folio) && !folio_test_lru(folio)) {
+ if (folio_ref_count(folio) > folio_large_mapcount(folio) + 1)
+ return false;
+ lru_add_drain();
+ }
+
if (folio_large_mapcount(folio) != folio_ref_count(folio))
return false;

Best Regards
Barry