Re: [PATCH 12/32] mm/khugepaged: check PMD support with the folios locked
From: Jose A. Perez de Azpillaga
Date: Sun Nov 29 2026 - 15:27:42 EST
On 11/29/26 11:06 AM, 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>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
--
cheers,
jose a. p-a