Re: [PATCH 7.2 v4 01/12] mm/khugepaged: remove READ_ONLY_THP_FOR_FS check

Zi Yan
2026-04-24T14:49:46Z

On 24 Apr 2026, at 8:40, David Hildenbrand (Arm) wrote:

> On 4/24/26 04:49, Zi Yan wrote:
>> collapse_file() requires FSes supporting large folio with at least
>> PMD_ORDER, so replace the READ_ONLY_THP_FOR_FS check with that.
>> MADV_COLLAPSE ignores shmem huge config, so exclude the check for shmem.
>>
>> While at it, replace VM_BUG_ON with VM_WARN_ON_ONCE.
>>
>> Add a helper function mapping_pmd_thp_support() for FSes supporting large
>> folio with at least PMD_ORDER.
>>
>> Signed-off-by: Zi Yan <ziy@nvidia.com>
>> Reviewed-by: Lance Yang <lance.yang@linux.dev>
>> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>> ---
>> include/linux/pagemap.h | 9 +++++++++
>> mm/khugepaged.c | 10 ++++++++--
>> 2 files changed, 17 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
>> index 31a848485ad9..5b4313d91137 100644
>> --- a/include/linux/pagemap.h
>> +++ b/include/linux/pagemap.h
>> @@ -513,6 +513,15 @@ static inline bool mapping_large_folio_support(const struct address_space *mappi
>> return mapping_max_folio_order(mapping) > 0;
>> }
>>
>> +static inline bool mapping_pmd_thp_support(const struct address_space *mapping)
>> +{
>> + /* AS_FOLIO_ORDER is only reasonable for pagecache folios */
>> + VM_WARN_ON_ONCE((unsigned long)mapping & FOLIO_MAPPING_ANON);
>> +
>> + return mapping_max_folio_order(mapping) >= PMD_ORDER;
>> +}
>
> Given mapping_large_folio_support(), I wonder whether we should call that
> mapping_pmd_folio_support() ?

OK, will rename it.

>
>> +
>> +
>> /* Return the maximum folio size for this pagecache mapping, in bytes. */
>> static inline size_t mapping_max_folio_size(const struct address_space *mapping)
>> {
>> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
>> index 7d48d4fbd5f3..79f051eb6195 100644
>> --- a/mm/khugepaged.c
>> +++ b/mm/khugepaged.c
>> @@ -2235,8 +2235,14 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
>> int nr_none = 0;
>> bool is_shmem = shmem_file(file);
>>
>> - VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
>> - VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
>> + /*
>> + * 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_thp_support(mapping));
>
>
> When we always make shmem to set the mapping to enable large folios, can we then
> drop this special casing no?

Yes, I added the TODO above it to remind us. I have not changed the code
here to avoid a dependency on Baolin’s patch[1]. I will do that once
Baolin’s patch gets into mm-stable.

[1] https://lore.kernel.org/all/b2c7deee259a94b0d00a7c320d8d24d2c421f761.1776908112.git.baolin.wang@linux.alibaba.com/

>
>> + VM_WARN_ON_ONCE(start & (HPAGE_PMD_NR - 1));
>>
>> result = alloc_charge_folio(&new_folio, mm, cc, HPAGE_PMD_ORDER);
>> if (result != SCAN_SUCCEED)
>
>
> --
> Cheers,
>
> David

Best Regards,
Yan, Zi