Re: [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes

From: Pedro Falcato

Date: Tue Nov 24 2026 - 08:38:01 EST


Hi John, Christoph, Jan,

Thank you for your replies. Since you largely share the same point, I'll
combine this into a single reply.

On Thu, Nov 19, 2026 at 04:55:30PM -0800, John Hubbard wrote:
> On 11/19/26 12:10 PM, Pedro Falcato wrote:
> > Sooo.... It turns out GUP dirty and ->release_folio() interact really
> > weirdly. This was initially reported by some AI agent, and David let me
> > know.
> >
> > The basic issue is:
>
> The basic issue is covered in a series of LWN articles that span 5 or
> 6 years:
>
> https://lwn.net/Kernel/Index/#Memory_management-get_user_pages
>
> Probably good to read and absorb that. There's a lot.
>

Thanks. I was aware of some of this, but it's good to take a fresh look again :)

> The problem remains not fully solved. pin_user_pages() is a building
> block, but we need to fully coordinate with filesystems in order to
> finish. It's good that you are looking at this.
>
> > - ->dirty_folio() relies on buffers/IFS/etc being attached to the
> > folios. It _cannot_ attach them itself, as dirty_folio() cannot
> > block.
> > - certain MM paths (reclaim, khugepaged, folio split, etc) are very
> > trigger-happy when it comes to asking filesystems to release them. In
> > most cases, barring any dirty folios, buffers, it will just drop
> > them.
> > - there is no problem in usual IO submission, but issues start to pop
> > up with GUP, where the code flow (roughly) becomes:
> > [get_user_pages(), etc]
> > ->page_mkwrite() called here, or before
> > ->private correctly populated with whatever the filesystem
> > expects.
> > folio is marked as dirty, initially.
> > *write finishes*
> > folio_lock();
> > folio_mark_dirty();
> > ->dirty_folio()
> > dirty_folio expects ->private to be populated by
> > ->page_mkwrite(), ->write_begin().
> > folio_unlock();
> >
> > What happens if any filemap_release_folio() runs (and succeeds) between
> > the initial dirtying (plus eventually writeback) and the write to the
> > folio completes? Welp, it's unpredictable. ext4 for example will
> > WARN_ONCE, and likely drop data (see ext4_dirty_folio()).
> >
> > So, what can we do about it? It's complicated, hence why this is an RFC.
> > For the possible solutions:
> > - ->dirty_folio() attaching this FS information itself is not really
> > possible in many cases. ->dirty_folio() can't block, nor is it even
> > guaranteed to hold the folio lock while being called.
> > - Can pincount help? Kind of. Things like folio_maybe_dma_pinned() are
> > rough, and don't deal with the usage of get_user_pages(FOLL_WRITE).
> > pincount itself isn't available in the common order-0 case, and not
> > available on order-1 as well on 32-bit, but who cares about that^W^W^W^W^W
> > - Could get_user_pages() ban FOLL_WRITE? Mayhaps. I would not be
> > opposed to that, but that's probably a larger change than ideal for stable,
> > and I'm not familiar enough with GUP requirements to decide to go that way.
> > This would require migrating all FOLL_WRITE users to
> > pin_user_pages().
> > - Can the whole system be reworked? Probably, but not for stable.
> >
> > This patch series is really rough but kind-of works. Instead of using
> > folio_maybe_dma_pinned(), which only interacts with pin_user_pages(), I
>
> pin_user_pages() was specifically introduced for this exact class of
> problem. get_user_pages() is now meant for other, much more limited cases.
> I've attempted to document that, here:
>
> https://docs.kernel.org/core-api/pin_user_pages.html

Ok, so I guess you want me to drop the hacks to make GUP FOLL_WRITE not
screw itself over? To be clear, the rest is still broken, even with pin_user_pages().
It just means we get nicer looking fixes, and I can drop patch 3.

It's unclear (to me) whether we care about any straggling get_user_pages
FOLL_WRITE users. A quick look around doesn't find me many; most seem to have
been converted to pin_user_pages() (though, admittedly, I don't know how many
of these are converted in stable; do we care?). Perhaps piling on a patch
to simply disallow get_user_pages(FOLL_WRITE) is ok? Giving Lorenzo's idea
a go again :)

FWIW I asked my least-favourite chatbot about get_user_pages(FOLL_WRITE) users,
and it found me a couple of cases. But little enough that it sounded fixable.
But, again, I don't know if this reflects stable, or whether we really care
that much.

> > ---
> > base-commit: 9663aa0da7070d958e333477ce0720512e9fa016
> > change-id: 20261116-gup-dirty-what-b73da748f5d1
>
> A git alias to wrap "git format-patch" with a sed(1) invocation is a great way
> to avoid accidentally "leaking" gerrit change-id's into a posting, fwiw.

I appreciate the advice, but this is gunk that b4 adds to every patchset ;)

--
Pedro