[PATCH] mm/filemap: stop fault-around when the walk steps back
From: Jose Fernandez (Anthropic)
Date: Thu Oct 29 2026 - 11:51:32 EST
filemap_map_pages() can dereference a PTE pointer outside the page table
it locked. KASAN has reported this as a use-after-free read [1].
Its fault-around loop walks the page cache and moves vmf->pte by the
change in index. A lower index moves it back. xas_find() can step back
to a lower index when a multi-index entry is split during the walk.
[1] fixes xas_find(), but the loop still relies on the walk never
stepping back. Stop the fault-around when the walk steps back. ->fault()
maps the faulting page if the loop did not.
Link: https://lore.kernel.org/20260904121301.200049-1-krystianmkaniewski@xxxxxxxxx [1]
Signed-off-by: Jose Fernandez (Anthropic) <jose.fernandez@xxxxxxxxx>
---
mm/filemap.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/mm/filemap.c b/mm/filemap.c
index b74bc1e5015c..1df776907140 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3968,6 +3968,13 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
unsigned long end;
vm_fault_t map_ret;
+ /* A lower index could move vmf->pte out of its page table. */
+ if (unlikely(xas.xa_index < last_pgoff)) {
+ folio_unlock(folio);
+ folio_put(folio);
+ break;
+ }
+
addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
vmf->pte += xas.xa_index - last_pgoff;
last_pgoff = xas.xa_index;
---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261028-jf-mm-filemap-map-pages-lower-bound-a623ed8a81c9
Best regards,
--
Jose Fernandez (Anthropic) <jose.fernandez@xxxxxxxxx>