[PATCH 8/8] mm/khugepaged: fix and flesh out try_to_unmap_flush() comment
From: Pedro Falcato
Date: Mon Jul 20 2026 - 11:00:24 EST
Instead of vaguely handwaving that it should probably be done, flesh out
any correctness concerns with try_to_unmap batching, flushing and its
interaction with the rest of collapse_file().
Signed-off-by: Pedro Falcato <pfalcato@xxxxxxx>
---
mm/khugepaged.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index a09e4e4e1943..1999e094df20 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2560,15 +2560,19 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
xa_unlocked:
/*
- * If collapse is successful, flush must be done now before copying.
- * If collapse is unsuccessful, does flush actually need to be done?
- * Do it anyway, to clear the state.
+ * try_to_unmap() flush must be done now before copying, regardless
+ * of success or not. In case of success, folios are about to be
+ * copied and collapsed onto a single large folio. In that case,
+ * stale TLB entries need to be flushed out, so no racing write may
+ * get lost. In case of failure, stale TLB entries need to be flushed
+ * out before putting the folio (which can possibly free it).
*/
try_to_unmap_flush();
if (result == SCAN_SUCCEED && nr_none &&
!shmem_charge(mapping->host, nr_none))
result = SCAN_FAIL;
+
if (result != SCAN_SUCCEED) {
nr_none = 0;
goto rollback;
--
2.55.0