Re: [PATCH 12/32] mm/khugepaged: check PMD support with the folios locked
From: David Hildenbrand (Arm)
Date: Wed Dec 02 2026 - 10:30:26 EST
On 12/2/26 10:42, Baolin Wang wrote:
>
>
> On 11/29/26 6:06 PM, David Hildenbrand (Arm) wrote:
>> 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.
>>
>> This has low urgency, as chattr +j requires CAP_SYS_RESOURCE.
>>
>> 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 ]
>> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>> ---
>> mm/khugepaged.c | 26 ++++++++++++++++----------
>> 1 file changed, 16 insertions(+), 10 deletions(-)
>>
>> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
>> index c25f732f2bf84..091e4136ba037 100644
>> --- a/mm/khugepaged.c
>> +++ b/mm/khugepaged.c
>> @@ -2300,13 +2300,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);
>> @@ -2542,9 +2535,22 @@ 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.
>> + *
>> + * 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.
>> + */
>> + if (!is_shmem && !mapping_pmd_folio_support(mapping))
>> + result = SCAN_FAIL;
>
> I sent a patch[1] earlier to clean up the shmem comments. While you are
> touching this part, could you remove those obsolete comments and check
> for shmem?
Ack, that makes sense. Was a bit careful about including a cleanup in the fix
first, but both relevant commits went into 7.2, so it's just fine.
Thanks!
--
Cheers,
David