All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes
@ 2026-11-19 20:10 Pedro Falcato
  2026-11-19 20:10 ` [PATCH RFC 1/5] mm/khugepaged: fix filemap_release_folio() interaction with GUP dirty Pedro Falcato
                   ` (8 more replies)
  0 siblings, 9 replies; 12+ messages in thread
From: Pedro Falcato @ 2026-11-19 20:10 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton, Zi Yan, Matthew Wilcox (Oracle),
	Jan Kara, Lorenzo Stoakes, John Hubbard, Peter Xu,
	Johannes Weiner
  Cc: linux-ext4, linux-nfs, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, Baolin Wang,
	Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
	Usama Arif, Kiryl Shutsemau, linux-mm, linux-kernel, Kairui Song,
	Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu,
	Baoquan He, Michal Hocko, linux-fsdevel, Pedro Falcato, stable

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();

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.
 - ext4 move_extents seems to have suspicious usage of
   filemap_release_folio() as well.

Many of these have suspicious Fixes: (which are definitely wrong). I
targeted these patches to complement Jan's original fix. Anything before
that (or which is missing that patch) is hopelessly broken anyway.
Proper Fixes: can be added, but many of them go back quite far.

Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
Pedro Falcato (5):
      mm/khugepaged: fix filemap_release_folio() interaction with GUP dirty
      mm/huge_memory: fix filemap_release_folio() interaction with GUP dirty
      mm/vmscan: avoid releasing FS metadata for !FOLL_PIN gup folios
      mm/vmscan: be more careful when releasing FS metadata if buffer_heads_over_limit
      mm/filemap: refuse releasing maybe_dma_pinned folios

 mm/filemap.c     |  3 +++
 mm/huge_memory.c | 22 +++++++++++-----------
 mm/internal.h    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 mm/khugepaged.c  | 19 +++++++++++++------
 mm/vmscan.c      | 18 ++++++------------
 5 files changed, 81 insertions(+), 29 deletions(-)
---
base-commit: 9663aa0da7070d958e333477ce0720512e9fa016
change-id: 20261116-gup-dirty-what-b73da748f5d1

-- 



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-20 20:11 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 20:10 [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes Pedro Falcato
2026-11-19 20:10 ` [PATCH RFC 1/5] mm/khugepaged: fix filemap_release_folio() interaction with GUP dirty Pedro Falcato
2026-11-19 20:10 ` [PATCH RFC 2/5] mm/huge_memory: " Pedro Falcato
2026-11-20 20:11   ` sashiko-bot
2026-11-19 20:10 ` [PATCH RFC 3/5] mm/vmscan: avoid releasing FS metadata for !FOLL_PIN gup folios Pedro Falcato
2026-11-19 20:10 ` [PATCH RFC 4/5] mm/vmscan: be more careful when releasing FS metadata if buffer_heads_over_limit Pedro Falcato
2026-11-19 20:10 ` [PATCH RFC 5/5] mm/filemap: refuse releasing maybe_dma_pinned folios Pedro Falcato
2026-11-20 20:11   ` sashiko-bot
2026-11-20  0:55 ` [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes John Hubbard
2026-11-20 11:57 ` Jan Kara
2026-11-20 17:39 ` Christoph Hellwig
2026-11-20 20:11 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.