[PATCH v8 4/7] mm/memcontrol: check PF_MEMALLOC after trying the limits
From: Joshua Hahn
Date: Wed Nov 04 2026 - 20:03:00 EST
PF_MEMALLOC charges are currently forced right after consume_stock(),
before trying the limits, so that they skip memory.high and the
high_work worker can't queue itself from its own reclaim.
A later patch in this series moves the memory stock into the page
counter, so that the stock is consumed inside page_counter_try_charge().
That leaves no place between consuming the stock and trying the limits
for the PF_MEMALLOC check.
Prepare for this by checking PF_MEMALLOC on both outcomes of the page
counter charge instead: skip memory.high on success, and force the
charge on failure. These charges still do not batch, so the resulting
usage is the same. The differences are that a PF_MEMALLOC charge that
hits a limit is counted in the cgroup-v1 failcnt again, and its
MEMCG_MAX event goes to the cgroup whose counter failed, as for any
other charge.
Suggested-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
Signed-off-by: Joshua Hahn <joshua.hahnjy@xxxxxxxxx>
---
mm/memcontrol.c | 37 ++++++++++++++++++-------------------
1 file changed, 18 insertions(+), 19 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 311bc2fa644a3..75b915febb06b 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2791,23 +2791,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
if (consume_stock(memcg, nr_pages))
return ret;
- /*
- * Reclaim may need memory to free memory. Let it go over the
- * limits for a while rather than fail or OOM kill. Doing this
- * before trying the limits also skips memory.high, so the
- * high_work worker can't queue itself from its own reclaim.
- */
- if (unlikely(current->flags & PF_MEMALLOC)) {
- /* Find the cgroup this charge puts over its limit, if any. */
- for (mem_over_limit = memcg; mem_over_limit;
- mem_over_limit = parent_mem_cgroup(mem_over_limit))
- if (mem_cgroup_margin(mem_over_limit) < nr_pages)
- break;
- goto force;
- }
-
- if (!allow_spinning)
- /* Avoid the refill and flush of the older stock */
+ /* Charges from reclaim or non-spinning contexts don't batch. */
+ if (!allow_spinning || (current->flags & PF_MEMALLOC))
batch = nr_pages;
reclaim_options = MEMCG_RECLAIM_MAY_SWAP;
@@ -2832,6 +2817,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
goto retry;
}
+ /*
+ * Reclaim may need memory to free memory. Let it go over the
+ * limits for a while rather than fail or OOM kill.
+ */
+ if (unlikely(current->flags & PF_MEMALLOC))
+ goto force;
+
if (unlikely(task_in_memcg_oom(current)))
goto nomem;
@@ -2927,9 +2919,9 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
out:
/*
* Don't forget to raise a MEMCG_MAX event for forced or rejected
- * requests. mem_over_limit is NULL if a PF_MEMALLOC charge fits.
+ * requests.
*/
- if (!raised_max_event && mem_over_limit)
+ if (!raised_max_event)
__memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning);
return ret;
@@ -2938,6 +2930,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
if (batch > nr_pages)
refill_stock(memcg, batch - nr_pages);
+ /*
+ * Skip memory.high for charges from reclaim, so the high_work
+ * worker can't queue itself from its own reclaim.
+ */
+ if (unlikely(current->flags & PF_MEMALLOC))
+ return ret;
+
memcg_enforce_high(memcg, batch, gfp_mask);
return ret;
}
--
2.53.0-Meta