[PATCH RFC 2/5] mm/huge_memory: fix filemap_release_folio() interaction with GUP dirty

From: Pedro Falcato

Date: Thu Nov 19 2026 - 15:12:00 EST


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@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Fixes: d824ec2a1546 ("mm: do not reclaim private data from pinned page")
Signed-off-by: Pedro Falcato <pfalcato@xxxxxxx>
---
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