Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

khugepaged: remove redundant index check for pmd-folios

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, remove this check.
While at it, simplify the comments.

Link: https://lkml.kernel.org/r/20260227143501.1488110-1-dev.jain@arm.com
Signed-off-by: Dev Jain <dev.jain@arm.com>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Lance Yang <lance.yang@linux.dev>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Anshuman Khandual <anshuman.khandual@arm.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Nico Pache <npache@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Dev Jain and committed by
Andrew Morton
22aa3321 1745ccbd

+4 -10
+4 -10
mm/khugepaged.c
··· 2023 2023 * we locked the first folio, then a THP might be there already. 2024 2024 * This will be discovered on the first iteration. 2025 2025 */ 2026 - if (folio_order(folio) == HPAGE_PMD_ORDER && 2027 - folio->index == start) { 2028 - /* Maybe PMD-mapped */ 2026 + if (folio_order(folio) == HPAGE_PMD_ORDER) { 2029 2027 result = SCAN_PTE_MAPPED_HUGEPAGE; 2030 2028 goto out_unlock; 2031 2029 } ··· 2351 2353 continue; 2352 2354 } 2353 2355 2354 - if (folio_order(folio) == HPAGE_PMD_ORDER && 2355 - folio->index == start) { 2356 - /* Maybe PMD-mapped */ 2356 + if (folio_order(folio) == HPAGE_PMD_ORDER) { 2357 2357 result = SCAN_PTE_MAPPED_HUGEPAGE; 2358 2358 /* 2359 - * For SCAN_PTE_MAPPED_HUGEPAGE, further processing 2360 - * by the caller won't touch the page cache, and so 2361 - * it's safe to skip LRU and refcount checks before 2362 - * returning. 2359 + * PMD-sized THP implies that we can only try 2360 + * retracting the PTE table. 2363 2361 */ 2364 2362 folio_put(folio); 2365 2363 break;