Skip to content

mm: memcg: settle memory.high debt for non-blocking THP charges

Qinyun Tan cgroups linux-mm lkml stable 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 4 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2mm: memcg: settle memory.high debt after THP faults with non-blocking gfpmm/huge_memory.c mm/memory.c+10 -04 replies
2/2mm: memcg: settle memory.high debt after large folio swapinmm/memory.c+8 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/2] mm: memcg: settle memory.high debt for non-blocking THP charges

memory.high is enforced at two points after a charge succeeds: on return to userspace, and synchronously in try_charge_memcg() for large overcharges, the latter gated on gfpflags_allow_blocking().

THP charge paths pass the physical allocation gfp from vma_thp_gfp_mask() to the memcg charge. With the default defrag=madvise (and no MADV_HUGEPAGE), as well as with defrag=defer, that gfp does not allow blocking. This is the right allocation policy -- a THP is not worth direct compaction, fall back to smaller orders instead -- but the charge code also reads it as "this context cannot sleep" and skips the synchronous enforcement, even though fault context sleeps just fine. Inside a single-syscall populate loop (mlock(), MADV_POPULATE_*, any GUP-driven population) the return-to-userspace hook is not reached between faults either, so nothing throttles at all: a memcg's usage runs from memory.high straight up to memory.max with zero reclaim and zero penalty sleep, consuming the reaction window that userspace OOM handlers (oomd, Kubernetes) depend on.

Patch 1 fixes the anonymous THP/mTHP fault path. The pre-existing selftest test_memcg_high_sync readily reproduces the problem: with transparent_hugepage/enabled=always it fails without the patch and passes with it.

Patch 2 fixes the same problem for large folio swapin on SWP_SYNCHRONOUS_IO devices (zram) with mTHP swapin enabled. Its changelog carries measured before/after numbers for both the reclaim-keeps-up regime and the penalty-sleep regime.

In both patches the charge gfp deliberately stays coupled to the allocation gfp, so the fail-fast fallback at memory.max is fully preserved; the over-high debt is instead settled from the fault path, which knows its context can sleep.

A note for stable backports: mem_cgroup_handle_over_high() only gained its gfp_mask argument in v6.6, from commit 9ea9cb00a82b ("mm:
memcontrol: fix GFP_NOFS recursion in memory.high enforcement"); older kernels take no argument. On kernels predating the swap table rework the swapin charge sits in alloc_swap_folio() rather than __swap_cache_alloc(), but patch 2's fix location at the end of do_swap_page() applies unchanged.

Qinyun Tan (2):
mm: memcg: settle memory.high debt after THP faults with
non-blocking gfp
mm: memcg: settle memory.high debt after large folio swapin

 mm/huge_memory.c |  8 ++++++++
 mm/memory.c      | 10 ++++++++++
 2 files changed, 18 insertions(+)

--
2.55.0