mm/filemap: two smaller fixes
Subsystems (4)
cover mm/filemap: two smaller fixes
Hi,
here are two fairly small pagecache-related fixes.
I was surprised to learn under which circumstances ext4 can change the maximum folio order of a file, but I guess we just have to support that. I have a related khugepaged fix coming up.
I ran some targeted tests, including reproducing the hang in #1, without surprises.
---
Chris Mason (1):
mm/filemap: update the folio order range atomically
David Hildenbrand (Arm) (1):
mm/filemap: avoid retrying faults on uffd-wp markers
include/linux/pagemap.h | 11 +++++------ mm/filemap.c | 8 ++++---- 2 files changed, 9 insertions(+), 10 deletions(-)
---
base-commit: c5941b1abeffba401f7f9cbc8c4f0c5da75dcba5
change-id: 20261129-filemap_fixes-f6281263b46c
Best regards,
Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
PATCH 1/2 mm/filemap: avoid retrying faults on uffd-wp markers
filemap_fault_recheck_pte_none() avoids unnecessary major faults in mlocked mappings by checking whether an apparently empty PTE was only temporarily cleared. If the PTE is no longer empty, it returns VM_FAULT_NOPAGE to retry the fault before populating the page cache.
However, filemap_fault() can also be reached with an uffd-wp marker in vmf->orig_pte. If the page is absent from the page cache, the recheck treats the nonempty marker as a reason to retry, but the marker remains unchanged.
Consequently, the fault repeats indefinitely.
This can be reproduced by mapping a fresh sparse regular file, applying mlock2(MLOCK_ONFAULT), registering for async uffd-wp and write-protecting the unpopulated mapping. A read from the mapping then fails to complete.
Let's fix it by checking pte_present() instead, allowing marker faults to populate the page cache while still detecting temporarily cleared mappings, including NUMA hinting PTEs, and by renaming the helper accordingly.
This has low urgency, as it requires the unusual combination of MLOCK_ONFAULT and async uffd-wp.
--- mm/filemap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/filemap.c +4 -4
@@ -3513,7 +3513,7 @@ static struct file *do_async_mmap_readahead(struct vm_fault *vmf,return fpin; } -static vm_fault_t filemap_fault_recheck_pte_none (struct vm_fault *vmf) +static vm_fault_t filemap_fault_recheck_pte_present (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma;vm_fault_t ret =0 ;
@@ -3544,11 +3544,11 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)if (unlikely (!ptep))return VM_FAULT_NOPAGE ; -if (unlikely (!pte_none (ptep_get_lockless (ptep)))) { +if (unlikely (pte_present (ptep_get_lockless (ptep)))) { ret =VM_FAULT_NOPAGE ; }else {spin_lock (vmf->ptl); -if (unlikely (!pte_none (ptep_get (ptep)))) +if (unlikely (pte_present (ptep_get (ptep)))) ret =VM_FAULT_NOPAGE ;spin_unlock (vmf->ptl); }
@@ -3613,7 +3613,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) mapping_locked = true; } }else { - ret =filemap_fault_recheck_pte_none (vmf); + ret =filemap_fault_recheck_pte_present (vmf);if (unlikely (ret))return ret;
Signature · 1 line
2.56.1
+ mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch added to mm-new branch
The patch titled Subject: mm/filemap: avoid retrying faults on uffd-wp markers has been added to the -mm mm-new branch. Its filename is mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch
The patch titled
Subject: mm/filemap: avoid retrying faults on uffd-wp markers
has been added to the -mm mm-new branch. Its filename is
mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: "David Hildenbrand (Arm)" <[email protected]>
Subject: mm/filemap: avoid retrying faults on uffd-wp markers
Date: Sun, 29 Nov 2026 10:05:09 +0100
Patch series "mm/filemap: two smaller fixes".
Two fairly small pagecache-related fixes.
I was surprised to learn under which circumstances ext4 can change the maximum folio order of a file, but I guess we just have to support that. I have a related khugepaged fix coming up.
This patch (of 2):
filemap_fault_recheck_pte_none() avoids unnecessary major faults in mlocked mappings by checking whether an apparently empty PTE was only temporarily cleared. If the PTE is no longer empty, it returns VM_FAULT_NOPAGE to retry the fault before populating the page cache.
However, filemap_fault() can also be reached with an uffd-wp marker in vmf->orig_pte. If the page is absent from the page cache, the recheck treats the nonempty marker as a reason to retry, but the marker remains unchanged.
Consequently, the fault repeats indefinitely.
This can be reproduced by mapping a fresh sparse regular file, applying mlock2(MLOCK_ONFAULT), registering for async uffd-wp and write-protecting the unpopulated mapping. A read from the mapping then fails to complete.
Let's fix it by checking pte_present() instead, allowing marker faults to populate the page cache while still detecting temporarily cleared mappings, including NUMA hinting PTEs, and by renaming the helper accordingly.
This has low urgency, as it requires the unusual combination of MLOCK_ONFAULT and async uffd-wp.
mm/filemap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/filemap.c renamed from mm/filemap.c~mm-filemap-avoid-retrying-faults-on-uffd-wp-markers +4 -4
@@ -3529,7 +3529,7 @@ static struct file *do_async_mmap_readahreturn fpin; } -static vm_fault_t filemap_fault_recheck_pte_none (struct vm_fault *vmf) +static vm_fault_t filemap_fault_recheck_pte_present (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma;vm_fault_t ret =0 ;
@@ -3560,11 +3560,11 @@ static vm_fault_t filemap_fault_recheck_if (unlikely (!ptep))return VM_FAULT_NOPAGE ; -if (unlikely (!pte_none (ptep_get_lockless (ptep)))) { +if (unlikely (pte_present (ptep_get_lockless (ptep)))) { ret =VM_FAULT_NOPAGE ; }else {spin_lock (vmf->ptl); -if (unlikely (!pte_none (ptep_get (ptep)))) +if (unlikely (pte_present (ptep_get (ptep)))) ret =VM_FAULT_NOPAGE ;spin_unlock (vmf->ptl); }
@@ -3629,7 +3629,7 @@ vm_fault_t filemap_fault(struct vm_fault mapping_locked = true; } }else { - ret =filemap_fault_recheck_pte_none (vmf); + ret =filemap_fault_recheck_pte_present (vmf);if (unlikely (ret))return ret;
_
Patches currently in -mm which might be from [email protected] are
mm-hugetlb-charge-the-copy-made-at-fork-to-the-parent.patch mm-mprotect-dont-skip-uffd-marker-install-when-racing-with-pte-table-reclaim.patch mm-userfaultfd-prevent-poison-marker-loss.patch mm-hugetlb-compare-against-persistent-count-when-freeing-lowmem-folios.patch mm-hugetlb-preserve-reservations-when-shrinking-on-highmem.patch mm-hugetlb-account-temporary-folios-separately.patch mm-hugetlb-account-temporary-folios-separately-fix.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation-fix.patch mm-memfd-recheck-the-page-cache-before-allocating-a-hugetlb-folio.patch mm-hugetlb-count-again-if-the-reserve-map-shrank.patch mm-hugetlb-account-restored-reservations-before-freeing-folios.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush.patch mm-userfaultfd-reject-dax-mappings.patch uprobes-skip-mappings-not-managed-by-core-mm.patch mm-memory-store-the-pfn-of-cow-pfnmap-mappings-in-the-anon-page-offset.patch mm-hugetlb-always-mark-new-page-cache-folios-migratable.patch mm-memfd-mark-new-hugetlb-folios-migratable.patch mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.patch hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.patch mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.patch fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.patch mm-hugetlb-use-the-vma-lock-to-stabilize-flags-for-pmd-sharing.patch rfc-mm-hugetlb_cgroup-charge-faults-to-mm-owner.patch mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch
PATCH 2/2 mm/filemap: update the folio order range atomically
In reply to David Hildenbrand (Arm)From: Chris Mason <[email protected]>
mapping_set_folio_order_range() updates mapping->flags with a plain load and store, assuming that nobody can modify the flags concurrently. However, set_blocksize() and ext4_change_inode_journal_flag() call it on active inodes, and none of their locks protects from concurrent mm_take_all_locks():
set_blocksize() mm_take_all_locks()
=============== ===================
load mapping->flags
test_and_set_bit(AS_MM_ALL_LOCKS)
down_write(i_mmap_rwsem)
store mapping->flags
mm_drop_all_locks()
bit is clear, no up_write()Consequently, we can lose concurrent setting/clearing of AS_MM_ALL_LOCKS, resulting in i_mmap_rwsem staying write-locked forever or being released by another task.
Let's fix it by updating the order bits atomically, and document what callers on an active inode still have to ensure.
This has medium urgency, as an unsuspecting admin can trigger it, leaving i_mmap_rwsem locked forever.
--- include/linux/pagemap.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
include/linux/pagemap.h +5 -6
@@ -411,8 +411,8 @@ static inline size_t mapping_max_folio_size_supported(void)* something the core cannot know). * Do not tune it based on, eg, i_size. * -* Context: This should not be called while the inode is active as it -* is non-atomic. +* Context: Safe to be called while the inode is active. However, the page +* cache must be empty and must be kept empty while setting the order range. */ static inline void mapping_set_folio_order_range(struct address_space *mapping, unsigned int min,
@@ -430,8 +430,8 @@ static inline void mapping_set_folio_order_range(struct address_space *mapping,if (max < min) max = min; - mapping->flags = (mapping->flags & ~AS_FOLIO_ORDER_MASK ) | - (min <<AS_FOLIO_ORDER_MIN ) | (max <<AS_FOLIO_ORDER_MAX ); +set_mask_bits (&mapping->flags,AS_FOLIO_ORDER_MASK , + (min <<AS_FOLIO_ORDER_MIN ) | (max <<AS_FOLIO_ORDER_MAX )); } static inline void mapping_set_folio_min_order(struct address_space *mapping,
@@ -448,8 +448,7 @@ static inline void mapping_set_folio_min_order(struct address_space *mapping,* indicate that the VFS can use large folios to cache the contents of * the file. * -* Context: This should not be called while the inode is active as it -* is non-atomic. +* Context: See mapping_set_folio_order_range(). */ static inline void mapping_set_large_folios (struct address_space *mapping) {
Signature · 1 line
2.56.1
Re: [PATCH 2/2] mm/filemap: update the folio order range atomically
Quoted 27 lines · David Hildenbrand (Arm)
From: Chris Mason <[email protected]>
mapping_set_folio_order_range()updatesmapping->flagswith a plain load and store, assuming that nobody can modify the flags concurrently. However,set_blocksize()andext4_change_inode_journal_flag()call it on active inodes, and none of their locks protects from concurrentmm_take_all_locks():set_blocksize() mm_take_all_locks() =============== =================== load mapping->flags test_and_set_bit(AS_MM_ALL_LOCKS) down_write(i_mmap_rwsem) store mapping->flags mm_drop_all_locks() bit is clear, no up_write()Consequently, we can lose concurrent setting/clearing of
AS_MM_ALL_LOCKS, resulting ini_mmap_rwsemstaying write-locked forever or being released by another task.Let's fix it by updating the order bits atomically, and document what callers on an active inode still have to ensure.
This has medium urgency, as an unsuspecting admin can trigger it, leaving
i_mmap_rwsemlocked forever.
Quoted 6 lines
Fixes 3c20917120ce ("block/bdev: enable large folio support for large logical block sizes")Fixes 58fd191f99f3 ("ext4: make data=journal support large block size")Signed-off-by Chris Mason <[email protected]>[ david: adjust comment and description ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
Yeah, I wanted to do this for a while but didn't get to it. Feel free to add:
Honza
Quoted 44 lines
--- include/linux/pagemap.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)include/linux/pagemap.h +5
-6@@ -411,8 +411,8 @@ static inline size_t mapping_max_folio_size_supported(void)* something the core cannot know). * Do not tune it based on, eg, i_size. * -* Context: This should not be called while the inode is active as it -* is non-atomic. +* Context: Safe to be called while the inode is active. However, the page +* cache must be empty and must be kept empty while setting the order range. */ static inline void mapping_set_folio_order_range(struct address_space *mapping, unsigned int min,@@ -430,8 +430,8 @@ static inline void mapping_set_folio_order_range(struct address_space *mapping,if (max < min) max = min; - mapping->flags = (mapping->flags & ~AS_FOLIO_ORDER_MASK ) | - (min <<AS_FOLIO_ORDER_MIN ) | (max <<AS_FOLIO_ORDER_MAX ); +set_mask_bits (&mapping->flags,AS_FOLIO_ORDER_MASK , + (min <<AS_FOLIO_ORDER_MIN ) | (max <<AS_FOLIO_ORDER_MAX )); } static inline void mapping_set_folio_min_order(struct address_space *mapping,@@ -448,8 +448,7 @@ static inline void mapping_set_folio_min_order(struct address_space *mapping,* indicate that the VFS can use large folios to cache the contents of * the file. * -* Context: This should not be called while the inode is active as it -* is non-atomic. +* Context: See mapping_set_folio_order_range(). */ static inline void mapping_set_large_folios (struct address_space *mapping) {Signature · 2 lines
-- 2.56.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
+ mm-filemap-update-the-folio-order-range-atomically.patch added to mm-new branch
In reply to David Hildenbrand (Arm)The patch titled Subject: mm/filemap: update the folio order range atomically has been added to the -mm mm-new branch. Its filename is mm-filemap-update-the-folio-order-range-atomically.patch
The patch titled
Subject: mm/filemap: update the folio order range atomically
has been added to the -mm mm-new branch. Its filename is
mm-filemap-update-the-folio-order-range-atomically.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-filemap-update-the-folio-order-range-atomically.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Chris Mason <[email protected]>
Subject: mm/filemap: update the folio order range atomically
Date: Sun, 29 Nov 2026 10:05:10 +0100
mapping_set_folio_order_range() updates mapping->flags with a plain load and store, assuming that nobody can modify the flags concurrently. However, set_blocksize() and ext4_change_inode_journal_flag() call it on active inodes, and none of their locks protects from concurrent mm_take_all_locks():
set_blocksize() mm_take_all_locks()
=============== ===================
load mapping->flags
test_and_set_bit(AS_MM_ALL_LOCKS)
down_write(i_mmap_rwsem)
store mapping->flags
mm_drop_all_locks()
bit is clear, no up_write()Consequently, we can lose concurrent setting/clearing of AS_MM_ALL_LOCKS, resulting in i_mmap_rwsem staying write-locked forever or being released by another task.
Let's fix it by updating the order bits atomically, and document what callers on an active inode still have to ensure.
This has medium urgency, as an unsuspecting admin can trigger it, leaving i_mmap_rwsem locked forever.
include/linux/pagemap.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
include/linux/pagemap.h renamed from include/linux/pagemap.h~mm-filemap-update-the-folio-order-range-atomically +5 -6
@@ -411,8 +411,8 @@ static inline size_t mapping_max_folio_s* something the core cannot know). * Do not tune it based on, eg, i_size. * -* Context: This should not be called while the inode is active as it -* is non-atomic. +* Context: Safe to be called while the inode is active. However, the page +* cache must be empty and must be kept empty while setting the order range. */ static inline void mapping_set_folio_order_range(struct address_space *mapping, unsigned int min,
@@ -430,8 +430,8 @@ static inline void mapping_set_folio_ordif (max < min) max = min; - mapping->flags = (mapping->flags & ~AS_FOLIO_ORDER_MASK ) | - (min <<AS_FOLIO_ORDER_MIN ) | (max <<AS_FOLIO_ORDER_MAX ); +set_mask_bits (&mapping->flags,AS_FOLIO_ORDER_MASK , + (min <<AS_FOLIO_ORDER_MIN ) | (max <<AS_FOLIO_ORDER_MAX )); } static inline void mapping_set_folio_min_order(struct address_space *mapping,
@@ -448,8 +448,7 @@ static inline void mapping_set_folio_min* indicate that the VFS can use large folios to cache the contents of * the file. * -* Context: This should not be called while the inode is active as it -* is non-atomic. +* Context: See mapping_set_folio_order_range(). */ static inline void mapping_set_large_folios (struct address_space *mapping) {
_
Patches currently in -mm which might be from [email protected] are
mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch mm-filemap-update-the-folio-order-range-atomically.patch