Re: [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes
From: Jan Kara
Date: Fri Nov 20 2026 - 06:57:52 EST
On Thu 19-11-26 20:10:43, 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:
> - ->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();
As John writes in much more detail, if you're modifying folio contents (and
then dirtying a folio), you're supposed to grab folio pin which should
among other things prevent the reclaim of fs private metadata. At least
that was one of design ideas behind folio pins. Maybe something got broken
or there are paths we missed that modify folios but before coming up with a
different mechanism I'd like to understand why the current scheme doesn't
work...
> 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
> introduce a new helper that helps places deal with the folio refcount
> directly. In that case, we can also deal with GUP. On the other end,
> moar problems:
> - NFS private data itself takes refs on folios. Now private data (that
> we could perhaps release if only we tried) is keeping us from
> releasing that same data. What?
> - ext4 data=journal has some funky usage of buffer_heads. Data journalled
> filesystems will not, in any way, dirty BHs. Instead it will,
> *only if it was DMA pinned*, set the checked flag. ext4 skips
> dropping those buffers if the checked flag is set.
Yes, that code is funky and there's a big comment in front of
ext4_journalled_dirty_folio() trying to explain what's going on. We
attach folio buffers to the transaction on ->page_mkwrite. When the
transaction commits, we must writeprotect the folio again to be notified
about next write so that we can attached buffers to the next transaction.
But what will you do with pins and users modifying folios through them? You
can't just "writeprotect" against an existing pin. So we just note with
folio_set_checked() that this folio has newer data than what is in the
journal and then in a more relaxed context, we do the journaling dance and
record the updated data in the journal. But you don't have to worry about
this too much. Ted plans to rip out data=journal mode in the near future
and then this will go away.
> - ext4 move_extents seems to have suspicious usage of
> filemap_release_folio() as well.
Yeah, that whole code is hairy and I agree these calls are probably
problematic. We should hold invalidate_locks over the whole operation and
should bail if any of the folios has a pin before calling into
filemap_release_folio().
Honza
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR