mirror of https://lore.kernel.org/lkml/
 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
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ 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] 7+ messages in thread

* [PATCH RFC 1/5] mm/khugepaged: fix filemap_release_folio() interaction with GUP dirty
  2026-11-19 20:10 [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes Pedro Falcato
@ 2026-11-19 20:10 ` Pedro Falcato
  2026-11-19 20:10 ` [PATCH RFC 2/5] mm/huge_memory: " Pedro Falcato
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ 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

Whether a folio can be "released" (drop attached buffer_heads, iomap
state, etc) is a complicated matter.

Namely, the act of calling pin_user_pages()/get_user_pages(FOLL_WRITE)
and later calling folio_mark_dirty() on these same folios relies on
on these structures to have been set up by ->page_mkwrite() or
->write_begin() elsewhere. ->dirty_folio() implementations usually
cannot set these up themselves, as ->dirty_folio() is not allowed to
block.

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

So, what happens if filemap_release_folio() is randomly called on
folios?

              T0                             |       T1
    [get_user_pages(), etc]
       ->page_mkwrite() called here
	 ->private is ok
         folio is marked as dirty, initially.
   *write finishes*
					      |   *some op*
					      | filemap_release_folio()
   folio_lock();
   folio_mark_dirty();
     ->dirty_folio()
          dirty_folio expects ->private
   folio_unlock();

In, e.g. ext4's case, this results in a WARN_ON(!folio_buffers(folio))
and very likely data loss, as block_dirty_folio() will not find any
buffers.

Thus, introduce a new helper to help figure out if
filemap_release_folio() can be called. filemap_may_release_folio()
mainly takes into account the refcount to help figure out if it's safe
to release. The NFS ->private refcount funkyness will be handled in a
separate patch.

As a part of this change, move try_to_unmap() back to before calling
filemap_may_release_folio(). This is required for mappings which don't
require write notification, and as such the dirty=1 -> folio dirty=1
transition is actually meaningful.

Reported-by: David Hildenbrand <david@kernel.org>
Cc: stable@vger.kernel.org
Fixes: d824ec2a1546 ("mm: do not reclaim private data from pinned page")
Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
 mm/internal.h   | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 mm/khugepaged.c | 19 +++++++++++++------
 2 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/mm/internal.h b/mm/internal.h
index 85cf7becf858..4c22d27ace96 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -718,6 +718,54 @@ static inline bool folio_needs_release(struct folio *folio)
 		(mapping && mapping_release_always(mapping));
 }
 
+static inline bool filemap_may_release_folio(struct folio *folio, int extra_refs)
+{
+	/*
+	 * Whether we may "release a folio" (drop attached buffers,
+	 * etc) is a bit more complex than what you could be hoping for.
+	 * There are two types of dirtying users in the kernel:
+	 * 1) ones which take the regular page fault/write(2) paths.
+	 * These regular paths (which fall into ->page_mkwrite or ->write_begin)
+	 * take enough care to e.g establish buffers and attach them to the folio.
+	 *
+	 * 2) ones that don't necessarily do that.
+	 * Essentially, the act of writing to a pinned/GUP'd page relies on the
+	 * above (for GUP-like operations, ->page_mkwrite). However, the folio
+	 * does not stay locked all the time. It is released somewhere in between
+	 * get_user_pages() and whatever it is you're doing with the page (e.g IO
+	 * submission). These users rely on the following logic:
+	 *
+	 *    [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();
+	 *
+	 * Thus, it's not possible to call filemap_release_folio() if _anyone_
+	 * has a reference we don't expect, that can plausibly dirty the folio.
+	 * Additionally, we can't have mappings for these. These checks are
+	 * racy, but the folio lock guarantees that whomever is e.g starting IO
+	 * on these pages will inevitably wait for us to finish.
+	 */
+	VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
+	if (!folio_needs_release(folio))
+		return true;
+
+	/* There's no way to know whether these are write-mapped, so give up. */
+	if (folio_mapped(folio))
+		return false;
+	if (folio_ref_count(folio) != extra_refs + folio_expected_ref_count(folio))
+		return false;
+	return true;
+}
+
 extern unsigned long highest_memmap_pfn;
 
 /*
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 964f78c3856b..1f1b93371a7e 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2396,6 +2396,15 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 			goto out_unlock;
 		}
 
+		if (!folio_isolate_lru(folio)) {
+			result = SCAN_DEL_PAGE_LRU;
+			goto out_unlock;
+		}
+
+		if (folio_mapped(folio))
+			try_to_unmap(folio,
+					TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH);
+
 		if (!is_shmem && (folio_test_dirty(folio) ||
 				  folio_test_writeback(folio))) {
 			/*
@@ -2404,11 +2413,13 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 			 * since first write.
 			 */
 			result = SCAN_PAGE_DIRTY_OR_WRITEBACK;
+			folio_putback_lru(folio);
 			goto out_unlock;
 		}
 
-		if (!folio_isolate_lru(folio)) {
-			result = SCAN_DEL_PAGE_LRU;
+		if (!filemap_may_release_folio(folio, 2)) {
+			result = SCAN_PAGE_COUNT;
+			folio_putback_lru(folio);
 			goto out_unlock;
 		}
 
@@ -2418,10 +2429,6 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 			goto out_unlock;
 		}
 
-		if (folio_mapped(folio))
-			try_to_unmap(folio,
-					TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH);
-
 		xas_lock_irq(&xas);
 
 		VM_BUG_ON_FOLIO(folio != xa_load(xas.xa, index), folio);

-- 
2.56.1


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

* [PATCH RFC 2/5] mm/huge_memory: fix filemap_release_folio() interaction with GUP dirty
  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 ` Pedro Falcato
  2026-11-19 20:10 ` [PATCH RFC 3/5] mm/vmscan: avoid releasing FS metadata for !FOLL_PIN gup folios Pedro Falcato
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ 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

filemap_release_folio() can only be called safely on:
 - folios that are unmapped and
 - folios are not dirty and
 - folios that don't have elevated refcounts

Anything else can plausibly be pinned for DMA/writing, which interacts
very poorly with ->release_folio().

Fix it by adding a filemap_may_release_folio() check, plus move unmap_folio()
outside the i_mmap_read_lock(). unmap_folio() does not need the
i_mmap_read_lock, since the folio is locked anyway.

Reported-by: David Hildenbrand <david@kernel.org>
Cc: stable@vger.kernel.org
Fixes: d824ec2a1546 ("mm: do not reclaim private data from pinned page")
Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
 mm/huge_memory.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 79d400ea9553..7c619c45cceb 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3630,13 +3630,13 @@ void vma_adjust_trans_huge(struct vm_area_struct *vma,
  * still have failed, but remap_anon_folio() must be called afterwards,
  * for anon folios.
  */
-static int unmap_folio(struct folio *folio)
+static int unmap_folio(struct folio *folio, bool locked)
 {
-	enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SYNC |
-		TTU_BATCH_FLUSH;
+	enum ttu_flags ttu_flags = TTU_SYNC | TTU_BATCH_FLUSH;
 
 	VM_BUG_ON_FOLIO(!folio_test_large(folio), folio);
-
+	if (locked)
+		ttu_flags |= TTU_RMAP_LOCKED;
 	/* Racy check if we can split the page, before we split PMDs */
 	if (folio_expected_ref_count(folio) != folio_ref_count(folio) - 1)
 		return -EAGAIN;
@@ -4170,7 +4170,7 @@ static int __folio_freeze_split_anon(struct folio *folio,
 		if (!anon_vma)
 			return -EBUSY;
 		anon_vma_lock_write(anon_vma);
-		ret = unmap_folio(folio);
+		ret = unmap_folio(folio, true);
 		if (ret)
 			goto out_unlock;
 	}
@@ -4271,8 +4271,13 @@ static int __folio_freeze_split_file(struct folio *folio,
 	memcg = get_mem_cgroup_from_folio(folio);
 	old_memcg = set_active_memcg(memcg);
 
+	ret = unmap_folio(folio, false);
+	if (ret)
+		goto fail_free;
+
 	gfp = current_gfp_context(mapping_gfp_mask(mapping) & GFP_RECLAIM_MASK);
-	if (!filemap_release_folio(folio, gfp)) {
+	if (!filemap_may_release_folio(folio, 1) ||
+	    !filemap_release_folio(folio, gfp)) {
 		ret = -EBUSY;
 		goto fail_free;
 	}
@@ -4306,10 +4311,6 @@ static int __folio_freeze_split_file(struct folio *folio,
 	if (shmem_mapping(mapping))
 		end = shmem_fallocend(mapping->host, end);
 
-	ret = unmap_folio(folio);
-	if (ret)
-		goto fail_mmap_unlock;
-
 	xas_lock_irq(&xas);
 
 	/*
@@ -4384,7 +4385,6 @@ static int __folio_freeze_split_file(struct folio *folio,
 	lruvec_unlock(lruvec);
 fail:
 	xas_unlock_irq(&xas);
-fail_mmap_unlock:
 	if (nr_shmem_dropped)
 		shmem_uncharge(mapping->host, nr_shmem_dropped);
 	/*

-- 
2.56.1


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

* [PATCH RFC 3/5] mm/vmscan: avoid releasing FS metadata for !FOLL_PIN gup folios
  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-19 20:10 ` 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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ 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

The check added in commit d824ec2a1546
("mm: do not reclaim private data from pinned page")
is correct. However, it only handles pages pinned for DMA, using
pin_user_pages(). It completely skips any other get_user_pages() user,
that does not pass FOLL_PIN.

Replace the folio_maybe_dma_pinned() check with
filemap_may_release_folio().

Reported-by: David Hildenbrand <david@kernel.org>
Cc: stable@vger.kernel.org
Fixes: d824ec2a1546 ("mm: do not reclaim private data from pinned page")
Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
 mm/vmscan.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 6a1af9b92349..d80bbb607a15 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1464,16 +1464,6 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 			}
 		}
 
-		/*
-		 * Folio is unmapped now so it cannot be newly pinned anymore.
-		 * No point in trying to reclaim folio if it is pinned.
-		 * Furthermore we don't want to reclaim underlying fs metadata
-		 * if the folio is pinned and thus potentially modified by the
-		 * pinning process as that may upset the filesystem.
-		 */
-		if (folio_maybe_dma_pinned(folio))
-			goto activate_locked;
-
 		mapping = folio_mapping(folio);
 		if (folio_test_dirty(folio)) {
 			if (folio_is_file_lru(folio)) {
@@ -1566,7 +1556,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 		 * the folio on the LRU so it is swappable.
 		 */
 		if (folio_needs_release(folio)) {
-			if (!filemap_release_folio(folio, sc->gfp_mask))
+			if (!filemap_may_release_folio(folio, 1) ||
+			    !filemap_release_folio(folio, sc->gfp_mask))
 				goto activate_locked;
 			if (!mapping && folio_ref_count(folio) == 1) {
 				folio_unlock(folio);

-- 
2.56.1


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

* [PATCH RFC 4/5] mm/vmscan: be more careful when releasing FS metadata if buffer_heads_over_limit
  2026-11-19 20:10 [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes Pedro Falcato
                   ` (2 preceding siblings ...)
  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 ` Pedro Falcato
  2026-11-19 20:10 ` [PATCH RFC 5/5] mm/filemap: refuse releasing maybe_dma_pinned folios Pedro Falcato
  2026-11-20  0:55 ` [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes John Hubbard
  5 siblings, 0 replies; 7+ 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

Namely, don't call ->release_folio() for mapped or referenced folios.
Since this is the active list, there's no actual point in doing
unmapping. In fact, that would be counterproductive for active folios.

So, gate on !mapped && may_release_folio.

Reported-by: David Hildenbrand <david@kernel.org>
Cc: stable@vger.kernel.org
Fixes: d824ec2a1546 ("mm: do not reclaim private data from pinned page")
Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
 mm/vmscan.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index d80bbb607a15..986742d7fb6a 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -2217,7 +2217,10 @@ static void shrink_active_list(unsigned long nr_to_scan,
 		if (unlikely(buffer_heads_over_limit)) {
 			if (folio_needs_release(folio) &&
 			    folio_trylock(folio)) {
-				filemap_release_folio(folio, 0);
+				if (!folio_mapped(folio) &&
+				    filemap_may_release_folio(folio, 1)) {
+					filemap_release_folio(folio, 0);
+				}
 				folio_unlock(folio);
 			}
 		}

-- 
2.56.1


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

* [PATCH RFC 5/5] mm/filemap: refuse releasing maybe_dma_pinned folios
  2026-11-19 20:10 [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes Pedro Falcato
                   ` (3 preceding siblings ...)
  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 ` Pedro Falcato
  2026-11-20  0:55 ` [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes John Hubbard
  5 siblings, 0 replies; 7+ 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

Some callers, like folio_unmap_invalidate(), call filemap_release_folio()
without any care for folio refcounts. This is on purpose. As
folio_unmap_invalidate() states, invalidate_inode_pages2() needs stronger
guarantees, and "can't afford to leave folios behind because
shrink_folio_list() has a temp ref on them". Ok.

Use folio_maybe_dma_pinned() universally, in filemap_release_folio(). This
has a much lower false positive rate, and should be ok enough for most
serious cases.

While here, properly assert that no folio passed to release_folio is
mapped.

Cc: stable@vger.kernel.org
Fixes: d824ec2a1546 ("mm: do not reclaim private data from pinned page")
Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
 mm/filemap.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/filemap.c b/mm/filemap.c
index f9e34144c53c..4fa75663b7f3 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -4571,8 +4571,11 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp)
 	struct address_space * const mapping = folio->mapping;
 
 	BUG_ON(!folio_test_locked(folio));
+	VM_WARN_ON_FOLIO(folio_mapped(folio), folio);
 	if (!folio_needs_release(folio))
 		return true;
+	if (folio_maybe_dma_pinned(folio))
+		return false;
 	if (folio_test_writeback(folio))
 		return false;
 

-- 
2.56.1


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

* Re: [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes
  2026-11-19 20:10 [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes Pedro Falcato
                   ` (4 preceding siblings ...)
  2026-11-19 20:10 ` [PATCH RFC 5/5] mm/filemap: refuse releasing maybe_dma_pinned folios Pedro Falcato
@ 2026-11-20  0:55 ` John Hubbard
  5 siblings, 0 replies; 7+ messages in thread
From: John Hubbard @ 2026-11-20  0:55 UTC (permalink / raw)
  To: Pedro Falcato, David Hildenbrand, Andrew Morton, Zi Yan,
	Matthew Wilcox (Oracle),
	Jan Kara, Lorenzo Stoakes, 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, stable

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.

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

> 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

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.


thanks,
-- 
John Hubbard


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

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

Thread overview: 7+ 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-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  0:55 ` [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes John Hubbard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®