[RFC PATCH 5/7] mm/khugepaged: handle memory.high overage in MADV_COLLAPSE

From: Shakeel Butt

Date: Sat Nov 21 2026 - 21:48:26 EST


MADV_COLLAPSE doesn't limit empty PTEs, so it can turn a PMD range with
one resident page into a 2MiB THP. Over a large sparse range, one
madvise() call can charge far more than the range used before. Today
the charge path throttles it for memory.high.

A later patch stops enforcing memory.high in the charge path, where
the task may hold locks other tasks need. Handle the overage right
after each collapse that dropped mmap_lock instead:
collapse_single_pmd() drops it to allocate and charge the THP. This
also covers the last PMD of each call, so madvise() over many small
VMAs, or a process_madvise() vector of small ranges, is throttled too.

process_madvise() on another process charges that process's cgroup.
If the caller is in that cgroup or below it, the caller handles the
overage here. Otherwise high_work reclaims it and no task is
throttled, as before.

Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
mm/khugepaged.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 2dc4245814df..1ede7d30853d 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -3306,6 +3306,14 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,

result = collapse_single_pmd(addr, vma, &mmap_unlocked, cc);

+ /*
+ * collapse_single_pmd() drops mmap_lock to allocate and charge
+ * a THP. Handle memory.high overage before taking it again,
+ * including after the last PMD.
+ */
+ if (mmap_unlocked)
+ mem_cgroup_over_high_throttle(GFP_KERNEL);
+
switch (result) {
case SCAN_SUCCEED:
case SCAN_PMD_MAPPED:
--
2.53.1-Meta