[PATCH v2 0/3] mm/khugepaged: bound mmap_lock hold time when walking an mm's VMAs
From: Lorenzo Stoakes (ARM)
Date: Thu Nov 05 2026 - 15:59:50 EST
This series introduces a couple small changes to khugepaged to improve its
behaviour both in the face of mm->mmap_lock contention and to reduce that
lock contention when possible.
Looking at the first change - if the mmap lock is contended, an attempted
mmap trylock in collapse_scan_mm_slot() will result in the scan being
aborted wherever it happens to be, and when next the mm is processed it
starts from the beginning again.
If an mm has a lot of ineligible VMAs and is undergoing significant
mmap_lock contention, collapsing the eligible VMAs there can take a very
long time.
Patch 2 of the series fixes this (patch 1 is a prerequisite) by keeping
track of where khugepaged was in the mm before aborting the scan.
This issue was noticed during some work in which an mm was instantiated
with a large number of small VMAs under significant mmap_lock contention,
during which collapse was observed to take 10.8s. With this patch applied
it took 3.6s to collapse the same mm.
Finally, patch 3 uses this to have khugepaged release + reacquire the mmap
read lock if it is contended after walking a large number of VMAs.
This was also observed as part of the previously mentioned work in the form
of mmap lock stalls, which patch 3 resolves. The reader stealing the lock
back from a queued writer on re-acquisition is addressed separately by the
rwsem change at [0].
Importantly, none of the changes introduces any regressions for khugepaged
anywhere. It is all upside, and implemented conservatively.
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
---
v2:
* Removed mmap_lock_nosteal patch (was 3/5) and sent a general rwsem patch
separately, as per Peter.
* Removed what was previously 4/5 as with 3/5 gone it was no longer
necessary.
* Put 2/3 commit message on a diet, as per David.
* Updated 3/3 to count every VMA rather than only skipped ones as per David.
* Updated 3/3 to use a sensibly calculated maximum VMA scan count renamed to
KHUGEPAGED_MAX_VMA_SCAN_BATCH as per David.
* Updated 3/3 to only release the mmap lock if it is contended.
* Updated 3/3 to resume from the per-mm cursor after re-acquiring the lock
rather than the now stale VMA.
* Updated 3/3 to treat a VMA whose aligned end wraps the address space as
the last in the mm, as per Lance.
* Fixed 3/3 KHUGEPAGED_MAX_VMA_SCAN_BATCH to derive from the pages_to_scan
budget passed to collapse_scan_mm_slot().
v1:
https://lore.kernel.org/r/20261103-khugepaged-latency-fix-v1-0-a07ab16ac860@xxxxxxxxxx
[0]: https://lore.kernel.org/all/20261105-rwsem-dont-steal-from-queued-writer-v1-1-275e070f426f@xxxxxxxxxx/
---
Lorenzo Stoakes (ARM) (3):
Revert "mm/khugepaged: remove definition of struct khugepaged_mm_slot"
mm/khugepaged: scan mm's more efficiently under mmap_lock contention
mm/khugepaged: release contended mmap read lock after scanning enough VMAs
mm/khugepaged.c | 120 +++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 84 insertions(+), 36 deletions(-)
---
base-commit: 34f88a114888c9dba49e45f2b0c2a3e83c4c871c
change-id: 20261029-khugepaged-latency-fix-40495d66ea50
Cheers,
--
Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>