* [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
* [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
` (7 subsequent siblings)
8 siblings, 0 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
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 related [flat|nested] 12+ 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-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
` (6 subsequent siblings)
8 siblings, 1 reply; 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
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 related [flat|nested] 12+ 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
` (5 subsequent siblings)
8 siblings, 0 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
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 related [flat|nested] 12+ 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
` (4 subsequent siblings)
8 siblings, 0 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
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 related [flat|nested] 12+ 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 20:11 ` sashiko-bot
2026-11-20 0:55 ` [PATCH RFC 0/5] mm: GUP dirty + ->release_folio() fixes John Hubbard
` (3 subsequent siblings)
8 siblings, 1 reply; 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
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 related [flat|nested] 12+ 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
2026-11-20 11:57 ` Jan Kara
` (2 subsequent siblings)
8 siblings, 0 replies; 12+ 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] 12+ 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
` (5 preceding siblings ...)
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
8 siblings, 0 replies; 12+ messages in thread
From: Jan Kara @ 2026-11-20 11:57 UTC (permalink / raw)
To: Pedro Falcato
Cc: David Hildenbrand, Andrew Morton, Zi Yan, Matthew Wilcox (Oracle),
Jan Kara, Lorenzo Stoakes, John Hubbard, Peter Xu,
Johannes Weiner, 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 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@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 12+ 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
` (6 preceding siblings ...)
2026-11-20 11:57 ` Jan Kara
@ 2026-11-20 17:39 ` Christoph Hellwig
2026-11-20 20:11 ` sashiko-bot
8 siblings, 0 replies; 12+ messages in thread
From: Christoph Hellwig @ 2026-11-20 17:39 UTC (permalink / raw)
To: Pedro Falcato
Cc: David Hildenbrand, Andrew Morton, Zi Yan, Matthew Wilcox (Oracle),
Jan Kara, Lorenzo Stoakes, John Hubbard, Peter Xu,
Johannes Weiner, 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 Thu, Nov 19, 2026 at 08:10:43PM +0000, Pedro Falcato wrote:
> - 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();
Writes through GUP are known broken in this an in other ways. The
fix is not to create band aids, but to phase out the remaining
writers through GUP. Which is really long overdue, and I'm starting
to get really pissed at the maintainers not fixing that after years
of knowing the bugs and having the infrastructure to fix it.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH RFC 2/5] mm/huge_memory: fix filemap_release_folio() interaction with GUP dirty
2026-11-19 20:10 ` [PATCH RFC 2/5] mm/huge_memory: " Pedro Falcato
@ 2026-11-20 20:11 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-20 20:11 UTC (permalink / raw)
To: Pedro Falcato, David Hildenbrand, Andrew Morton, Zi Yan,
Matthew Wilcox (Oracle), Jan Kara, Lorenzo Stoakes, John Hubbard,
Peter Xu, Johannes Weiner
Cc: linux-ext4, 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, Kairui Song, Qi Zheng, Shakeel Butt,
Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Michal Hocko,
tytso, Chuck Lever, Jeff Layton, Anna Schumaker
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm/huge_memory: Fixes trailer cites d824ec2a1546 instead of
6a3edd293956
--
commit 30321b8e2452fd3a099b0078bb5c7a8478e790ce
Author: Pedro Falcato <pfalcato@suse.de>
> mm/huge_memory: fix filemap_release_folio() interaction with GUP dirty
>
> 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>
[Severity: Low]
d824ec2a1546 ("mm: do not reclaim private data from pinned page") only
modified shrink_folio_list() in mm/vmscan.c, whereas the
filemap_release_folio() call in the THP split path was added by
6a3edd293956 ("mm: release private data before split THP"). Should the
Fixes tag be:
Fixes: 6a3edd293956 ("mm: release private data before split THP")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de?part=2
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH RFC 5/5] mm/filemap: refuse releasing maybe_dma_pinned folios
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
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-20 20:11 UTC (permalink / raw)
To: Pedro Falcato, David Hildenbrand, Andrew Morton, Zi Yan,
Matthew Wilcox (Oracle), Jan Kara, Lorenzo Stoakes, John Hubbard,
Peter Xu, Johannes Weiner
Cc: linux-ext4, 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, Kairui Song, Qi Zheng, Shakeel Butt,
Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Michal Hocko,
tytso, Chuck Lever, Jeff Layton, Anna Schumaker
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm/filemap: filemap_release_folio() dma-pin check breaks
folio_invalidate()
- [Medium] mm/filemap: VM_WARN_ON_FOLIO on mapped folio hit via
mext_move_extent()
- [Medium] mm/filemap: VM_WARN_ON_FOLIO in filemap_release_folio()
precedes needs_release
--
commit 38a290b76cc80ed81b7d5a26e8b50ef48dfba24c
Author: Pedro Falcato <pfalcato@suse.de>
mm/filemap: refuse releasing maybe_dma_pinned folios
This commit updates filemap_release_folio() to warn if the folio is mapped
and refuse to release filesystem metadata when folio_maybe_dma_pinned() is
true.
> diff --git a/mm/filemap.c b/mm/filemap.c
> index f9e34144c53c7..4fa75663b7f3e 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);
[Severity: Medium]
Can EXT4_IOC_MOVE_EXT hit this VM_WARN_ON_FOLIO() on a memory-mapped file?
In fs/ext4/move_extent.c, mext_move_extent() locks the origin and donor
folios and calls filemap_release_folio() on them without unmapping them
from user page tables or checking folio_mapped() first:
fs/ext4/move_extent.c:mext_move_extent() {
...
if (!filemap_release_folio(folio[0], 0) ||
!filemap_release_folio(folio[1], 0)) {
ret = -EBUSY;
goto unlock;
}
...
}
If either file is memory-mapped with resident PTEs in the moved range, an
unprivileged EXT4_IOC_MOVE_EXT ioctl triggers this warning when
CONFIG_DEBUG_VM is enabled.
> if (!folio_needs_release(folio))
> return true;
[Severity: Medium]
Should VM_WARN_ON_FOLIO(folio_mapped(folio), folio) come after the
!folio_needs_release(folio) check to match filemap_may_release_folio()?
filemap_may_release_folio() returns true when !folio_needs_release(folio)
before checking folio_mapped(folio):
mm/internal.h:filemap_may_release_folio() {
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;
...
}
Callers such as collapse_file() (which calls try_to_unmap() without
TTU_SYNC) and __folio_freeze_split_file() (which calls unmap_folio())
can leave a folio mapped if unmapping races with zap_pte_range(), then
rely on filemap_may_release_folio() to gate filemap_release_folio().
For a folio that does not need release, filemap_may_release_folio()
returns true and filemap_release_folio() then triggers
VM_WARN_ON_FOLIO(folio_mapped(folio), folio).
> + if (folio_maybe_dma_pinned(folio))
> + return false;
> if (folio_test_writeback(folio))
> return false;
[Severity: High]
Does refusing to release a DMA-pinned folio here break truncation via
folio_invalidate() and leak the folio and its buffer_heads?
During full-folio truncation, truncate_cleanup_folio() calls
folio_invalidate() before removing the folio from the page cache.
block_invalidate_folio() and gfs2_invalidate_folio() first discard all
buffers on the folio and then call filemap_release_folio(folio, 0) to
free the buffer_heads and clear PG_private.
If filemap_release_folio() returns false because the folio is DMA-pinned,
truncation still removes the folio from the page cache and sets
folio->mapping = NULL, leaving PG_private and the discarded buffer_heads
attached. When the DMA pin holder later unpins the folio as dirty (for
example via unpin_user_pages_dirty_lock()), folio_mark_dirty() sees
folio_mapping(folio) == NULL and calls noop_dirty_folio(), setting
PG_dirty on the orphaned folio while PG_private holds a reference. When
shrink_folio_list() later scans this folio on the file LRU, it sees
folio_test_dirty(folio) and jumps to activate_locked before reaching the
orphaned buffer release path, and because folio->mapping is NULL nothing
can ever write it back or clear PG_dirty, permanently leaking the folio
and its buffer_heads on the file LRU.
On GFS2, gfs2_invalidate_folio() -> gfs2_discard() also leaves struct
gfs2_bufdata attached at bh->b_private to be freed by gfs2_release_folio();
once truncation clears folio->mapping, any later filemap_release_folio()
call sees mapping == NULL and falls back to try_to_free_buffers() instead
of mapping->a_ops->release_folio, permanently leaking struct gfs2_bufdata
even if the folio was not dirtied. In ext4's mpage_release_unused_pages(),
a failed delayed allocation calls block_invalidate_folio() and then
folio_clear_uptodate(folio), relying on filemap_release_folio() ->
try_to_free_buffers() -> folio_cancel_dirty() to clear PG_dirty, so
returning false leaves a pinned folio in the page cache with PG_dirty set
and PG_uptodate clear.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de?part=5
^ permalink raw reply [flat|nested] 12+ 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
` (7 preceding siblings ...)
2026-11-20 17:39 ` Christoph Hellwig
@ 2026-11-20 20:11 ` sashiko-bot
8 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-20 20:11 UTC (permalink / raw)
To: Pedro Falcato; +Cc: linux-ext4, tytso
Sashiko has finished reviewing this 5-patch series:
- Patches 1, 3, and 4: no issues found.
- Patch 2 ("mm/huge_memory: fix filemap_release_folio() interact..."): 1
potential issue (1 Low)
- Patch 5 ("mm/filemap: refuse releasing maybe_dma_pinned folios"): 3
potential issues (1 High, 2 Medium)
Detailed reviews have been sent as separate replies to patches 2 and 5.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de
^ 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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox