Re: [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim
From: Harry Yoo
Date: Wed Dec 02 2026 - 14:22:25 EST
On Wed, Dec 02, 2026 at 10:54:00AM +0100, Michal Hocko wrote:
> 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>
> ---
I was wondering why we have to call rcu_barrier() for every retry
instead of just once before OOM. Retrying memory won't free page
tables anyway.
Apparently sashiko wondered it too.
> 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
--
Cheers,
Harry / Hyeonggon