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

From: Pratyush Yadav

Date: Tue Nov 24 2026 - 13:27:48 EST


On Tue, Nov 24 2026, Mike Rapoport wrote:

> On Tue, Nov 24, 2026 at 04:56:50PM +0000, Pratyush Yadav wrote:
>> On Tue, Nov 24 2026, Mike Rapoport wrote:
>>
>> > On Tue, Nov 24, 2026 at 02:51:59PM +0000, Pratyush Yadav wrote:
>> >> On Mon, Nov 23 2026, Mike Rapoport wrote:
>> [...]
>> > 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++;
>> > }
>> > }
>>
>> This has a super theoretical corner case where the migration can
>> succeed, then the folio can migrate back to CMA by the time we do
>> shmem_get_folio(). So we can loop infinitely.
>
> Oops :)
>
>> I think the if (retries > MEMFD_LUO_MIGRATE_RETRIES) should be after the
>> folio_is_longterm_pinnable() check and retries should increment after
>
> Maybe after folio_unlock() then?

Right. Sounds good.

>
>> every migration, not only failed ones.
>>
>> Apart from that, your variant looks nicer.
>>
>> Does that sound good?
[...]

--
Regards,
Pratyush Yadav