All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Michal Hocko" <mhocko@kernel.org>,
	"David Hildenbrand" <david@kernel.org>
Cc: "David Rientjes" <rientjes@google.com>,
	"Ben Gardon" <bgardon@google.com>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	mhocko@suse.com, "Andrew Morton" <akpm@linux-foundation.org>,
	linux-mm@kvack.org, "Chris Mason" <mason@kernel.org>
Subject: Re: [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim
Date: Wed, 02 Dec 2026 16:31:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-246217@kernel.org> (raw)
In-Reply-To: <20261202095450.296675-8-mhocko@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] memcg: repeated rcu_barrier() on every retry loop in
  try_charge_memcg()
- [Medium] memcg: per-CPU stocks not drained after rcu_barrier() in
  try_charge_memcg()
--

commit cdd0f5947203961fb686dca72c1e12849c882b50
Author: Michal Hocko <mhocko@suse.com>

memcg, oom: wait for rcu_call callbacks before retrying reclaim

This commit calls rcu_barrier() in try_charge_memcg() before retrying the
charge after unsuccessful reclaim, so pending call_rcu() memory frees complete
before invoking the memcg OOM killer.

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index b75a61ddeded5..33d1e18abeda1 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2921,8 +2921,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;

[Severity: High]
Should rcu_barrier() be limited to run at most once per charge rather than on
every nr_retries iteration?

nr_retries starts at MAX_RECLAIM_RETRIES (16) and is reset to 16 after
mem_cgroup_oom(), while each rcu_barrier() serializes on the global
rcu_state.barrier_mutex, sends cross-CPU IPIs, and waits for a full RCU
callback drain.  In addition to running 16 times before each OOM attempt,
costly high-order charges (nr_pages > 1 << PAGE_ALLOC_COSTLY_ORDER), such as
THP faults with GFP_TRANSHUGE on MADV_HUGEPAGE mappings near memory.max, fall
through the nr_reclaimed check above and block for 16 consecutive
rcu_barrier() calls before falling back to order-0 pages.

[Severity: Medium]
Can the charges freed by RCU callbacks during rcu_barrier() remain trapped in
per-CPU stocks without drain_all_stock() running again?

On the first failed reclaim pass, drain_all_stock(mem_over_limit) runs and
sets drained = true before jumping to retry, so rcu_barrier() is only reached
on a later pass after drained is already set:

mm/memcontrol.c:try_charge_memcg() {
	...
	if (!drained) {
		drain_all_stock(mem_over_limit);
		drained = true;
		goto retry;
	}
	...
	if (nr_retries--) {
		/* Wait for any potential call_rcu memory freers */
		rcu_barrier();
		goto retry;
	}
}

When RCU callbacks such as delayed_put_task_struct() or tlb_remove_table_rcu()
free slab objects and page tables during rcu_barrier(), they uncharge via
obj_cgroup_uncharge_pages() -> refill_stock(), which caches up to
MEMCG_STOCK_HIGH (64 pages) in the callback CPU's memcg_stock (plus up to a
page in obj_stock).  Because drained is never reset after rcu_barrier(),
consume_stock() on retry only sees the charging CPU's local stock, leaving
charges freed on other CPUs cached in remote stocks and still triggering
mem_cgroup_oom() in small cgroups.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202095450.296675-1-mhocko@kernel.org?part=7


  reply	other threads:[~2026-12-02 16:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
2026-12-02  9:53 ` [PATCH 01/10] memcg-v1, oom: do not sleep in worker contexts Michal Hocko
2026-12-02  9:53 ` [PATCH 02/10] mm/oom_kill: keep the allocating mm eligible Michal Hocko
2026-12-02  9:53 ` [PATCH 03/10] mm, oom: do not panic on constrained OOMs Michal Hocko
2026-12-02  9:53 ` [PATCH 04/10] mm/oom_kill: ratelimit memcg OOM reports with no victim Michal Hocko
2026-12-02  9:53 ` [PATCH 05/10] mm, oom: thaw tasks sharing the mm with the oom victim Michal Hocko
2026-12-02 16:30   ` sashiko-bot
2026-12-02  9:53 ` [PATCH 06/10] mm, oom: check oom victims prior to numa placement Michal Hocko
2026-12-02  9:54 ` [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim Michal Hocko
2026-12-02 16:31   ` sashiko-bot [this message]
2026-12-02  9:54 ` [PATCH 08/10] mm, " Michal Hocko
2026-12-02 16:31   ` sashiko-bot
2026-12-02  9:54 ` [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer Michal Hocko
2026-12-02 16:31   ` sashiko-bot
2026-12-02  9:54 ` [PATCH 10/10] memcg: wake v1 OOM lock contenders on unlock Michal Hocko
2026-12-02 13:06 ` [PATCH 0/10] assorted oom fixes David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-246217@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bgardon@google.com \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=mhocko@suse.com \
    --cc=rientjes@google.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.