[PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim
From: Michal Hocko
Date: Wed Dec 02 2026 - 04:55:27 EST
From: Michal Hocko <mhocko@xxxxxxxx>
In a memory cgroup with a small limit, the OOM killer can kill a task
while most of the limit is taken up by memory that is already queued to
be freed after an RCU grace period.
Memory that was passed to call_rcu() stays charged until its callback
frees it. The task_struct and the credentials of an exited child are
freed that way. With MMU_GATHER_RCU_TABLE_FREE, so are its page tables.
Reclaim cannot free such memory, and call_rcu() does not throttle its
caller. So tasks that fork short-lived children can fill the limit with
memory that only waits to be freed. When a charge then fails at the
limit, try_charge_memcg() reclaims and drains the per-CPU stocks. It
then calls mem_cgroup_oom() without waiting for a grace period.
Call rcu_barrier before retrying the charge after unsuccessful reclaim
(slow path) to reduce this potential issue.
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: Michal Hocko <mhocko@xxxxxxxx>
---
mm/memcontrol.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 84f4a8fd4d16..a8dd933273c8 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2800,8 +2800,11 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
goto retry;
- if (nr_retries--)
+ if (nr_retries--) {
+ /* Wait for any potential call_rcu memory freers */
+ rcu_barrier();
goto retry;
+ }
if (gfp_mask & __GFP_RETRY_MAYFAIL)
goto nomem;
--
2.56.1