Re: [PATCH v1 1/3] mm: process_mrelease: expedite clean file folio reclaim via mmu_gather

From: Minchan Kim

Date: Fri Apr 24 2026 - 17:57:11 EST


On Fri, Apr 24, 2026 at 08:33:03PM +0100, Matthew Wilcox wrote:
> On Tue, Apr 21, 2026 at 04:02:37PM -0700, Minchan Kim wrote:
> > +++ b/mm/swap.c
> > @@ -1043,6 +1043,48 @@ void release_pages(release_pages_arg arg, int nr)
> > }
> > EXPORT_SYMBOL(release_pages);
> >
> > +static inline void free_file_cache(struct folio *folio)
> > +{
> > + if (folio_trylock(folio)) {
> > + mapping_evict_folio(folio_mapping(folio), folio);
>
> If we already know that the folio is for a file (and I think we do?)
> then we can just use folio->mapping here. On the other hand, if it
> could be KSM or something else weird, carry on.

Thanks for the review. It made me think about the shmem corner cases.

Since we already check folio_test_anon(folio) before calling this path,
we know we are dealing with non-anonymous folios.

My specific concern was shmem folios, which are not anonymous but can
be in the swap cache. While mapping_evict_folio() might technically work
for them at this point (since remove_mapping handles it but I might miss),
it feels unintentional and fragile because mapping_evict_folio() is
primarily designed for page cache eviction, not swap cache.

To make this robust and safely adopt your suggestion of using folio->mapping
directly, I think we should handle swap cache folios explicitly
in the main loop like this:

void free_pages_and_caches(struct encoded_page **pages, int nr,
bool try_evict_file_folios)
{
for (int i = 0; i < nr; i++) {
struct folio *folio = page_folio(encoded_page_ptr(pages[i]));

if (folio_test_anon(folio) || folio_test_swapcache(folio))
free_swap_cache(folio);
else if (unlikely(try_evict_file_folios))
free_file_cache(folio);
...
}
}

And then we can use folio->mapping directly in the helper:

static inline void free_file_cache(struct folio *folio)
{
if (folio_trylock(folio)) {
mapping_evict_folio(folio->mapping, folio);
folio_unlock(folio);
}
}

This way, we are guaranteed that anything reaching free_file_cache() is a
non-swapcache file folio, making the direct use of folio->mapping safe.

Please let me know if I am missing something here.