[PATCH v2 12/31] mm/khugepaged: check PMD support with the folios locked
From: David Hildenbrand (Arm)
Date: Fri Dec 04 2026 - 07:54:19 EST
From: Chris Mason <mason@xxxxxxxxxx>
A collapse that races with chattr +j on an ext4 file can store a PMD
folio in a mapping whose maximum folio order no longer allows it, as
the maximum folio order can change once the page cache is empty.
ext4_change_inode_journal_flag() empties the page cache and then lowers
the maximum folio order under the invalidate_lock. But collapsing only
checks early, in file_thp_enabled() under the mmap_lock, and
collapse_file() merely has a VM_WARN_ON_ONCE() that the same race
triggers:
madvise_collapse() ext4_change_inode_journal_flag()
================== ================================
mmap_read_lock()
file_thp_enabled()
mmap_read_unlock()
filemap_invalidate_lock()
truncate_pagecache()
ext4_set_inode_mapping_order()
filemap_invalidate_unlock()
collapse_file()
page_cache_sync_readahead()
xas_store(new_folio)
Consequently, on a nodelalloc mount, a write fault on that PMD folio runs
out of journal credits, resulting in SIGBUS and an ext4 file system error.
Let's fix it by checking again once we have the page cache folios locked,
stabilizing the maximum supported folio order. A MADV_COLLAPSE that loses
the race now fails with -EINVAL, like one that starts after the chattr.
While at it, drop the shmem related TODO+comment and just check shmem
as well, as proposed by [1]. This change was made possible by
commit eb92d97f7e6a ("Revert "tmpfs: don't enable large folios if not
supported""), which went into 7.2 along with the commit we are fixing.
This has low urgency, as chattr +j requires CAP_SYS_RESOURCE.
Link: https://lore.kernel.org/all/12bac4614aa4b37490e92051d484f4f2cb215076.1789701677.git.baolin.wang@xxxxxxxxxxxxxxxxx [1]
Fixes: 8a088263a097 ("mm/huge_memory: remove READ_ONLY_THP_FOR_FS from file_thp_enabled()")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
[ david: rework code; adjust comment and description ]
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/khugepaged.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index a2d13d989a558..de00e0d2a6d5f 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2301,13 +2301,6 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
bool readahead_done = false;
bool any_non_shmem_dirty = false;
- /*
- * MADV_COLLAPSE ignores shmem huge config, so do not check shmem
- *
- * TODO: once shmem always calls mapping_set_large_folios() on its
- * mapping, the shmem check can be removed.
- */
- VM_WARN_ON_ONCE(!is_shmem && !mapping_pmd_folio_support(mapping));
VM_WARN_ON_ONCE(start & (HPAGE_PMD_NR - 1));
result = alloc_charge_folio(&new_folio, mm, cc, HPAGE_PMD_ORDER);
@@ -2543,9 +2536,17 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
*/
try_to_unmap_flush();
- if (result == SCAN_SUCCEED && nr_none &&
- !shmem_charge(mapping->host, nr_none))
- result = SCAN_FAIL;
+ if (result == SCAN_SUCCEED) {
+ /*
+ * A filesystem can lower the maximum folio order only once the
+ * page cache is empty. Recheck now that all old folios are
+ * locked and still in the page cache.
+ */
+ if (!mapping_pmd_folio_support(mapping))
+ result = SCAN_FAIL;
+ else if (nr_none && !shmem_charge(mapping->host, nr_none))
+ result = SCAN_FAIL;
+ }
if (result != SCAN_SUCCEED) {
nr_none = 0;
goto rollback;
--
2.56.1