Re: [RFC PATCH 00/57] mm/collapse: rebuild collapse on migration primitives

From: David Hildenbrand (Arm)

Date: Mon Sep 14 2026 - 11:41:39 EST


Following up on some older threads (sorry, the last month was insane)

[...]

>> Ideally, we'd get b), in small, nice-to-review chunks that incrementally improve
>> the code without inflating it heavily or moving everything around.
>
> It is not a cleanup and I would rather not sell it as one. It replaces a
> mechanism, so judged as (b) it fails by construction.
>
> I believe the end result is much cleaner. But I might be biased. :)
>

It should start with cleanups, though, and not moving stuff around and rewriting
it in a rather uncontrolled manner.

I saw that you started sending out cleanups, good. :)

>> The current locking is nasty, so anything that moves us one step closer into
>> something that is not only simpler but also more scalable is nice. I am a bit
>> concerned with the churn in the series as is.
>>
>> After this series, mm/collapse.c itself is way larger than just mm/khugepaged.c
>> originally, which raises some eyebrows.
>
> Line count is a poor proxy for simplicity or scalability. What the
> engine changes is the serialization model, and that is the part collapse
> needs changed: the PMD granularity and the exclusion both come out of
> the locking.
>
> Incremental does not reach it, though. The old mechanism is correct
> because it holds mmap_write_lock, the anon_vma write lock and a reference
> from the LRU; the engine is correct because the sources are frozen behind
> migration entries. There is no halfway state that is correct under both,
> so the switch lands as one patch.
>
> What can be incremental is everything around it: the engine goes in beside
> the old mechanism, patch 25 points the anon path at it, and 28 removes what
> it replaces. Until 28 both are in the tree with only one of them
> reachable, so the switch can be reverted on its own.

What I am saying is that there must be a way where we slowly move into that
direction.

We try to move in small, controlled increments where possible.

Huge rewrites are not helpful, neither for reviewers nor for maintainers nor for
consumers.

Sure, at some point there will be a switch, and bigger changes but what has been
presented in this RFC was way too much and way too radical for one series.

Nobody can really review 39 patches of RFC.

Often it's more helpful to propose an overall design idea, and then discuss if
and how to get there.

(and the design idea should not read like schlop otherwise nobody will have an
interest in engaging with it)

>
>> We should also be aware that people are proposing file/shmem mTHP collapse, so
>> ideally what we refactor would naturally unify some of these code paths.
>>
>> I am wondering whether shmem mTHP collapse should come first. (I'm hoping that
>> shmem mTHP collapse can unify some of the anon+file collapse code in a nice way,
>> to similarly just look like a cleanup while enabling a new scenario.
>
> mTHP collapse as it stands has limited usability: PMD-aligned windows only,
> and one VMA has to own the PMD. Bolting file collapse onto the same
> structure adds to the debt instead of paying it down.
>
> It would fit the new design. The frame -- scan, candidate selection, the
> round and its passes -- has nothing anon-specific in it; what is
> anon-specific sits in the freeze (folio_test_anon(), PageAnonExclusive())
> and the unshare in the fault-in pass. A file source would bring its own
> check, freeze, copy and install.
>
> I am not sure it should, though.
>
> Do we want to find file collapse candidates by walking the virtual
> address space at all?

In guest_memfd, we recently discussed that we actually would want a mechanism to
collapse even without walking the VA space .... but maybe guest_memfd is just
too special, not sure.

You still need a mmap to identify the candidate file IIRC.

>
> collapse_file() already works on the mapping -- it builds the folio in
> the page cache and then repairs every mapping through
> retract_page_tables() -- so the VMA walk only picks which inode range to
> try, and it reaches only what a registered mm maps right now. Large
> folios buy more than TLB reach: fewer page cache entries, cheaper
> writeback, natural locking batch, etc. Those apply whether the file is
> mapped or not, and going at the inode directly would reach them.
>
>> Agreed, I think we really should unify+cleanup the existing code first before
>> doing more drastic changes.
>>
>> Having a series that throws all of khugepaged.c into a mixer and pours something
>> new into collapse.c is ... concerning :)
>
> The moving around is patches 29-35 and the tracing after them. None of it
> is needed for the engine: 1-28 add it, switch the anon path over and delete
> the old mechanism, without moving anything else out of khugepaged.c. If
> the churn is the problem, v2 can stop there and the moves can come later as
> their own series.
>

The churn is always the problem.

Small, controlled increments please.

--
Cheers,

David