Re: [PATCH v5 9/9] mm/collapse: separate scanning a PTE table from collapsing it
From: Lance Yang
Date: Thu Oct 15 2026 - 01:44:48 EST
On Wed, Oct 14, 2026 at 06:11:42PM +0100, Kiryl Shutsemau (Meta) wrote:
>A collapse is two jobs. One reads a PTE table under mmap_lock and decides
>whether the range is worth collapsing. The other allocates, isolates,
>copies and flushes, and wants the lock given up first.
>
>collapse_single_pmd() did both, so the boundary between them was somewhere
>in the middle of a function.
>
>Give each half its own function:
>
> - collapse_scan_pmd() scans one table. It picks the side:
> collapse_scan_anon(), the anonymous scan that used to carry the
> collapse_scan_pmd() name, or collapse_scan_file().
>
> - collapse_pmd() does the collapse the scan asked for, and is handed
> what the scan returned. SCAN_SUCCEED means there is something to
> collapse. SCAN_PTE_MAPPED_HUGEPAGE means the page cache already
> holds the PMD folio and only the PTE table is left to retract.
> Anything else is why there is nothing to do. It calls
> collapse_anon(), formerly mthp_collapse(), or collapse_file().
>
>For now collapse_single_pmd() stays, as the two of them with the
>mmap_lock drop in between, so its callers see what they saw before.
>collapse_control_init() sets a control up before its first scan.
>
>What the scan found and the collapse needs travels in collapse_control.
>For an anonymous table that is the orders and the referenced and
>swapped-out counts. collapse_anon() and collapse_huge_page() now read
>them from there instead of taking them as arguments.
>
>For a file it is the file itself and the offset in it. A file collapse
>works on the page cache and never sees a VMA, so the scan takes the
>reference while it still has one and the collapse gives it back.
>
>The file scan moves under mmap_lock with the anonymous one, where before
>the lock was given up first. The lock is now held over the page cache
>walk, an RCU walk over one table's worth of slots with no PTL, and taken
>fewer times. collapse_scan_mm_slot() ends its walk whenever the lock was
>dropped, so a refused file table used to cost khugepaged an unlock, a
>trip back through khugepaged_do_scan(), a relock and a VMA lookup. Now
>only a table that goes on to be collapsed does.
>
>Assisted-by: LLM
>Acked-by: Zi Yan <ziy@xxxxxxxxxx>
>Signed-off-by: Kiryl Shutsemau (Meta) <kas@xxxxxxxxxx>
>---
Reviewed-by: Lance Yang <lance.yang@xxxxxxxxx>