Re: [PATCH] khugepaged: convert redundant check to WARN_ON

From: David Hildenbrand (Arm)

Date: Tue Feb 24 2026 - 05:51:37 EST


On 2/23/26 05:40, Dev Jain wrote:
>
> On 20/02/26 4:03 am, Andrew Morton wrote:
>> On Thu, 19 Feb 2026 11:18:27 +0530 Dev Jain <dev.jain@xxxxxxx> wrote:
>>
>>> Claim: folio_order(folio) == HPAGE_PMD_ORDER => folio->index == start.
>>>
>>> Proof: Both loops in hpage_collapse_scan_file and collapse_file, which
>>> iterate on the xarray, have the invariant that
>>> start <= folio->index < start + HPAGE_PMD_NR ... (i)
>>> A folio is always naturally aligned in the pagecache, therefore
>>> folio_order == HPAGE_PMD_ORDER => IS_ALIGNED(folio->index, HPAGE_PMD_NR) == true ... (ii)
>>> thp_vma_allowable_order -> thp_vma_suitable_order requires that the virtual
>>> offsets in the VMA are aligned to the order,
>>> => IS_ALIGNED(start, HPAGE_PMD_NR) == true ... (iii)
>>>
>>> Combining (i), (ii) and (iii), the claim is proven.
>>>
>>> Therefore, convert this to a VM_WARN_ON.
>>>
>>> ...
>>>
>>> --- a/mm/khugepaged.c
>>> +++ b/mm/khugepaged.c
>>> @@ -2000,8 +2000,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
>>> * we locked the first folio, then a THP might be there already.
>>> * This will be discovered on the first iteration.
>>> */
>>> - if (folio_order(folio) == HPAGE_PMD_ORDER &&
>>> - folio->index == start) {
>>> + if (folio_order(folio) == HPAGE_PMD_ORDER) {
>>> + VM_WARN_ON(folio->index != start);
>> It's a bad sad to remove unneeded code by retaining that code and
>> adding even more code.
>>
>> Perhaps add a comment reminding us to remove this altogether at a later
>> date?
>
> But then shouldn't I remove it just now :)

I'd say, remove it directly. Even if "folio->index != start", both
functions should do the right thing.

While at it, we could heavily simplify the comments to "If there is
already a PMD-sized THP, we can only try collapsing the PTE table".

Or sth, like that.

--
Cheers,

David