Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] mm/filemap: two smaller fixes
@ 2026-11-29  9:05 David Hildenbrand (Arm)
  2026-11-29  9:05 ` [PATCH 1/2] mm/filemap: avoid retrying faults on uffd-wp markers David Hildenbrand (Arm)
  2026-11-29  9:05 ` [PATCH 2/2] mm/filemap: update the folio order range atomically David Hildenbrand (Arm)
  0 siblings, 2 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-29  9:05 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle), Jan Kara, Andrew Morton, Ying Huang,
	Kefeng Wang, ZhangPeng, Hannes Reinecke, Christian Brauner,
	Luis Chamberlain, John Garry, Zhang Yi, Baokun Li, Ojaswin Mujoo,
	Theodore Ts'o
  Cc: linux-fsdevel, linux-mm, David Hildenbrand (Arm), stable,
	Chris Mason

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.

Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
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,
--  
David Hildenbrand (Arm) <david@kernel.org>



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

* [PATCH 1/2] mm/filemap: avoid retrying faults on uffd-wp markers
  2026-11-29  9:05 [PATCH 0/2] mm/filemap: two smaller fixes David Hildenbrand (Arm)
@ 2026-11-29  9:05 ` David Hildenbrand (Arm)
  2026-11-29  9:05 ` [PATCH 2/2] mm/filemap: update the folio order range atomically David Hildenbrand (Arm)
  1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-29  9:05 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle), Jan Kara, Andrew Morton, Ying Huang,
	Kefeng Wang, ZhangPeng, Hannes Reinecke, Christian Brauner,
	Luis Chamberlain, John Garry, Zhang Yi, Baokun Li, Ojaswin Mujoo,
	Theodore Ts'o
  Cc: linux-fsdevel, linux-mm, David Hildenbrand (Arm), stable

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.

Fixes: 58f327f2ce80 ("filemap: avoid unnecessary major faults in filemap_fault()")
Cc: stable@vger.kernel.org
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 mm/filemap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 5f8c180d6124d..cc547063695ec 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -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;
 

-- 
2.56.1



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

* [PATCH 2/2] mm/filemap: update the folio order range atomically
  2026-11-29  9:05 [PATCH 0/2] mm/filemap: two smaller fixes David Hildenbrand (Arm)
  2026-11-29  9:05 ` [PATCH 1/2] mm/filemap: avoid retrying faults on uffd-wp markers David Hildenbrand (Arm)
@ 2026-11-29  9:05 ` David Hildenbrand (Arm)
  1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-29  9:05 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle), Jan Kara, Andrew Morton, Ying Huang,
	Kefeng Wang, ZhangPeng, Hannes Reinecke, Christian Brauner,
	Luis Chamberlain, John Garry, Zhang Yi, Baokun Li, Ojaswin Mujoo,
	Theodore Ts'o
  Cc: linux-fsdevel, linux-mm, David Hildenbrand (Arm), Chris Mason,
	stable

From: Chris Mason <mason@kernel.org>

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.

Fixes: 3c20917120ce ("block/bdev: enable large folio support for large logical block sizes")
Fixes: 58fd191f99f3 ("ext4: make data=journal support large block size")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: adjust comment and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 include/linux/pagemap.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 73af18a373670..0363ce1b1e5ab 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -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)
 {

-- 
2.56.1



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

end of thread, other threads:[~2026-11-29  9:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29  9:05 [PATCH 0/2] mm/filemap: two smaller fixes David Hildenbrand (Arm)
2026-11-29  9:05 ` [PATCH 1/2] mm/filemap: avoid retrying faults on uffd-wp markers David Hildenbrand (Arm)
2026-11-29  9:05 ` [PATCH 2/2] mm/filemap: update the folio order range atomically David Hildenbrand (Arm)

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