Re: [PATCH v3] mm/memfd_luo: don't use memfd_pin_folios()

From: Mike Rapoport

Date: Tue Nov 24 2026 - 10:48:51 EST


On Tue, Nov 24, 2026 at 02:51:59PM +0000, Pratyush Yadav wrote:
> On Mon, Nov 23 2026, Mike Rapoport wrote:
>
> > Hi,
> >
> >> memfd_luo takes a snapshot of the folios in the file's mapping. To make
> >> sure the snapshot does not go out of sync, it pins the folios in the
> >> file via memfd_pin_folios(). The pinning also migrates the folios out of
> >> MIGRATE_MOVABLE to make sure doesn't land in KHO boot memory or in a CMA
> >> region.
> >>
> >> memfd_pin_folios() does not lock the folios. So it can race with
> >> shmem_writeout(). A folio returned by memfd_pin_folios() might already
> >> be in swap cache. This happens to work but is fragile. memfd_luo looks
> >> at folio flags and index on a folio that isn't in the memfd's page
> >> cache.
> >>
> >> In addition, memfd_pin_folios() needs the caller to allocate an array of
> >> folios. memfd_luo does not really need that since it will vmalloc
> >> folios_ser and store the information there anyway. The folios array is
> >> unnecessary overhead.
> >>
> >> Finally, memfd_pin_folios() fills in holes in the memfd. This makes
> >> preserving sparse memfds very expensive in both compute and memory.
> >> While this patch doesn't yet stop filling in the holes, it sets up the
> >> infrastructure to make it simpler.
> >>
> >> Fix all this by walking the page cache directly using shmem_get_folio()
> >> with SGP_CACHE. This returns a locked folio with an elevated refcount.
> >> memfd_luo holds that reference until the memfd is unpreserved. This
> >> blocks compaction, migration, etc.
> >>
> >> There is still a need to migrate folios out of MIGRATE_MOVABLE page
> >> blocks. This is because those might be CMA or KHO scratch. memfd_luo
> >> will hold a longterm reference to the folios so it will block migration
> >> in that pageblock. Introduce memfd_luo_migrate_folio() to do this.
> [...]
> >> +/*
> >> + * Look up the folio at @index, allocating it if needed. Preservation holds a
> >> + * longterm reference on the folio, so folios that are not longterm pinnable are
> >> + * migrated out first, retrying a few times if someone else has them isolated.
> >> + *
> >> + * On success, returns 0 with *@foliop locked and holding a reference.
> >> + */
> >> +static int memfd_luo_get_folio(struct inode *inode, pgoff_t index,
> >> + struct folio **foliop)
> >> +{
> >> + struct folio *folio;
> >> + int err, retry;
> >> +
> >> + for (retry = 0; retry < MEMFD_LUO_MIGRATE_RETRIES; retry++) {
> >> + err = shmem_get_folio(inode, index, 0, &folio, SGP_CACHE);
> >> + if (err)
> >> + return err;
> >> +
> >> + if (likely(folio_is_longterm_pinnable(folio))) {
> >> + *foliop = folio;
> >> + return 0;
> >> + }
> >> +
> >> + folio_unlock(folio);
> >> + err = memfd_luo_migrate_folio(folio);
> >> + /* Someone else might have it isolated, retry a bit. */
> >> + if (err && err != -EBUSY)
> >> + return err;
> >> + }
> >> +
> >> + return -EBUSY;
> >
> > In v2 we did another shmem_get_folio() after a successfull migration,
> > now even if the last iteration migrates successfully we still return
> > -EBUSY and don't update *foloip.
>
> Argh, right... Good catch!
>
> How about this:
>
> for (retry = 0; ; retry++) {
> err = shmem_get_folio(inode, index, 0, &folio, SGP_CACHE);
> if (err)
> return err;
>
> if (likely(folio_is_longterm_pinnable(folio))) {
> *foliop = folio;
> return 0;
> }
>
> folio_unlock(folio);
> if (retry == MEMFD_LUO_MIGRATE_RETRIES) {
> folio_put(folio);
> return -EBUSY;
> }
>
> err = memfd_luo_migrate_folio(folio);
> /* Someone else might have it isolated, retry a bit. */
> if (err && err != -EBUSY)
> return err;
> }
>
> /* Should never get here. */
> return -EINVAL;
>
> Or, I suppose I can go back to the original implementation.
>
> What do you think?

v2 was better I think, but it still had that

if (err == -EBUSY && ++busy_retries < MEMFD_LUO_MIGRATE_RETRIES)
continue;
if (err)
return err;

which I don't really like.

I came up with a variant below that IMHO makes things more explicit:

static int memfd_luo_get_folio(struct inode *inode, pgoff_t index,
struct folio **foliop)
{
int err, retries = 0;
struct folio *folio;

for (;;) {
if (retries > MEMFD_LUO_MIGRATE_RETRIES)
return -EBUSY;

err = shmem_get_folio(inode, index, 0, &folio, SGP_CACHE);
if (err)
return err;

if (likely(folio_is_longterm_pinnable(folio)))
break;

folio_unlock(folio);
err = memfd_luo_migrate_folio(folio);
if (err) {
if (err != -EBUSY)
return err;
retries++;
}
}

*foliop = folio;
return 0;
}

It does an iteration more than v2 with the same MEMFD_LUO_MIGRATE_RETRIES,
but that's not really important.

Your call really, I can live with either of them.

> --
> Regards,
> Pratyush Yadav

--
Sincerely yours,
Mike.