[PATCH v2 0/8] mm/khugepaged: collapse_file() cleanups
From: Pedro Falcato
Date: Wed Nov 11 2026 - 13:09:56 EST
Here are a bunch of cleanups for collapse_file() that attempt to aid
code readability and understanding. This is part 1 out of N, and only
attempts to address the main collapse loop. The exit path is still a
mess but I figure it's much easier to review if I send this batch as-is,
versus sending the whole thing.
Tested using the khugepaged shmem and file selftests.
Based on top of latest mm-core/for-next.
Note that some review comments from v1 went unaddressed
(Nico's larger restructuring, some small Lorenzo suggestions) for a couple
of small reasons. The main one being that I want to get this v2 out before
going insane :)) I agreed to work with Nico, and I'll either send out some
of his patches as a followup, or he will.
In any case, I'm tired of this function and I hope this kicks off some
discussion :)
Cc: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
Cc: "Liam R. Howlett" <liam@xxxxxxxxxxxxx>
Cc: Nico Pache <nico.pache@xxxxxxxxx>
Cc: Ryan Roberts <ryan.roberts@xxxxxxx>
Cc: Dev Jain <dev.jain@xxxxxxx>
Cc: Barry Song <baohua@xxxxxxxxxx>
Cc: Lance Yang <lance.yang@xxxxxxxxx>
Cc: Usama Arif <usama.arif@xxxxxxxxx>
Cc: Lorenzo Stoakes <ljs@xxxxxxxxxx>
Cc: Kiryl Shutsemau <kas@xxxxxxxxxx>
Cc: linux-mm@xxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Changes in v2:
- Rebased on current mm-core/for-next, which contains a number of fixes to
the amazingly simple and awesome collapse_file() function.
- Fix the "bad unlock balance" reported by syzbot ci (syzbot ci, Lorenzo)
- 1/8:
- Fold in v1 5/8 (kerneldoc); new helpers get their kerneldoc in
the patch that introduces them. (Lorenzo)
- Group the input fields of collapse_file_state and make end and
is_shmem const; is_shmem is now a bool. (Lorenzo)
- Wrap the prepare_collapse_file_folio() declaration to fit in 80
columns. (Lorenzo)
- Keep the original LRU drain behaviour (drain only after a
successful shmem_get_folio()) (Lorenzo)
- 2/8: New patch. Turn value entries into NULL before calling
prepare_collapse_file_folio().
- 3/8 (v1 2/8):
- Rename collapse_read_folio() to maybe_read_folio(). (Lorenzo)
- Replace the compound condition with an early-return guard.
(Lorenzo)
- Comment on why the xa_lock is dropped. (Lorenzo)
- Expand the commit message. (Lorenzo)
- 5/8 (v1 4/8): Drop a duplicate blank line.
- 6/8: Add a commit message, noting that errors before try_to_unmap()
no longer call try_to_unmap_flush() themselves.
- 7/8: Explain in the commit message why the refcount and dirty checks
don't need the i_pages lock. The flush added by 854e5ff8b3ce now
happens at out_putback.
- 8/8: Retitle (drop "fix"), since 854e5ff8b3ce already reworded the
comment; drop a stray blank line.
Link to v1: https://lore.kernel.org/all/20260720142913.846902-1-pfalcato@xxxxxxx/
Pedro Falcato (8):
mm/khugepaged: separate out windy folio logic from collapse_file
mm/khugepaged: sanitize the folio passed to file folio collapse logic
mm/khugepaged: factor out page cache folio reading
mm/khugepaged: factor out and simplify dirty/writeback handling
mm/khugepaged: simplify prepare folio locking and exit paths
mm/khugepaged: hoist isolation into collapse_isolate_folio()
mm/khugepaged: hoist more code into collapse_isolate_folio()
mm/khugepaged: flesh out try_to_unmap_flush() comment
mm/khugepaged.c | 461 ++++++++++++++++++++++++++++++------------------
1 file changed, 286 insertions(+), 175 deletions(-)
base-commit: 8d21e3d3faf19f461f6d0380fe8a1f5bdba97f25
--
2.56.0