Linux cgroups development
 help / color / mirror / Atom feed
From: Michal Hocko <mhocko@suse.com>
To: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Muchun Song <muchun.song@linux.dev>, Tejun Heo <tj@kernel.org>,
	Meta kernel team <kernel-team@meta.com>,
	cgroups@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH for-7.5 4/5] memcg: avoid queuing high_work from reclaim
Date: Wed, 14 Oct 2026 18:59:30 +0200	[thread overview]
Message-ID: <as-08j9_1C2F_N8s@tiehlicka> (raw)
In-Reply-To: <as97Nzob4XuzO9Ss@linux.dev>

On Wed 14-10-26 09:06:00, Shakeel Butt wrote:
> On Wed, Oct 14, 2026 at 01:39:06PM +0200, Michal Hocko wrote:
> > On Sat 10-10-26 14:09:56, Shakeel Butt wrote:
> > > Kernel threads and remote chargers use high_work when memory.high is
> > > exceeded. Their allocations during reclaim can also charge memory,
> > > for example when zswap stores a page.
> > > 
> > > Without a check, the high_work worker can queue itself from its own
> > > reclaim. The pending bit is cleared before high_work runs, so each run
> > > can queue another run.
> > 
> > What does that mean really? Does this delays the actual run?
> 
> It will not delay the actual charge request. This mainly means that we keep
> enqueing the high reclaim work even from high reclaim context and can
> potentially keep wasting CPU if high reclaim is not very successful.

Hmm, so is there any practical problem if we do that? What exactly do you
mean by "keep wasting CPU"?
 
> > > Skip memory.high enforcement for charges from task context with
> > > PF_MEMALLOC set when they would use high_work. This covers kernel
> > > threads and remote charges. The memory is still charged. Interrupt
> > > charges keep their current path.
> > > 
> > > In a VM with incompressible zswap data, the worker queued itself 163 times
> > > without this check.
> > > 
> > > Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
> > 
> > The patch itself looks OK. I am wondering whether we can unify the check
> > with the existing one which forces the allocation through in main
> > charging path.
> 
> The force charge (and ENOMEM) paths bypass the memory.high enforcement
> altogether.

Exactly. Isn't that exactl what we want to achieve here as well?

> Only successful charges (done_restock label) goes through
> memory.high enforcement. So, even if we move this check in try_charge_memcg, it
> will still be a separate check before calling high enforcement function. I think
> this check is very high_work specific and better to be in the high enforcement
> function.

I meant something like the following

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 856a7d07586c..832b40ee5b6f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2653,7 +2653,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	bool passed_oom = false;
 	unsigned int reclaim_options;
 	bool drained = false;
-	bool raised_max_event = false;
 	unsigned long pflags;
 	bool allow_spinning = gfpflags_allow_spinning(gfp_mask);
 
@@ -2661,6 +2660,15 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	if (consume_stock(memcg, nr_pages))
 		return 0;
 
+	/*
+	 * Prevent unbounded recursion when reclaim operations need to
+	 * allocate memory. This might exceed the limits temporarily,
+	 * but we prefer facilitating memory reclaim and getting back
+	 * under the limit over triggering OOM kills in these cases.
+	 */
+	if (unlikely(current->flags & PF_MEMALLOC))
+		goto force;
+
 	if (!allow_spinning)
 		/* Avoid the refill and flush of the older stock */
 		batch = nr_pages;
@@ -2683,15 +2691,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto retry;
 	}
 
-	/*
-	 * Prevent unbounded recursion when reclaim operations need to
-	 * allocate memory. This might exceed the limits temporarily,
-	 * but we prefer facilitating memory reclaim and getting back
-	 * under the limit over triggering OOM kills in these cases.
-	 */
-	if (unlikely(current->flags & PF_MEMALLOC))
-		goto force;
-
 	if (unlikely(task_in_memcg_oom(current)))
 		goto nomem;
 
@@ -2712,7 +2711,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto nomem;
 
 	__memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning);
-	raised_max_event = true;
 
 	psi_memstall_enter(&pflags);
 	nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
@@ -2773,13 +2771,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
 		return -ENOMEM;
 force:
-	/*
-	 * If the allocation has to be enforced, don't forget to raise
-	 * a MEMCG_MAX event.
-	 */
-	if (!raised_max_event)
-		__memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning);
-
 	/*
 	 * The allocation either can't fail or will lead to more memory
 	 * being freed very soon.  Allow memory usage go over the limit
@@ -2789,6 +2780,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	if (do_memsw_account())
 		page_counter_charge(&memcg->memsw, nr_pages);
 
+	if (mem_cgroup_margin(memcg))
+		__memcg_memory_event(memcg, MEMCG_MAX, allow_spinning);
 	return 0;
 
 done_restock:

-- 
Michal Hocko
SUSE Labs

  reply	other threads:[~2026-10-14 16:59 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-10 21:09 [PATCH for-7.5 0/5] memcg: clean up memory.high enforcement Shakeel Butt
2026-10-10 21:09 ` [PATCH for-7.5 1/5] memcg: move memory.high enforcement out of try_charge_memcg() Shakeel Butt
2026-10-12 13:22   ` Harry Yoo
2026-10-12 17:13     ` Shakeel Butt
2026-10-14 11:13   ` Michal Hocko
2026-10-10 21:09 ` [PATCH for-7.5 2/5] memcg: use high_work for kernel threads Shakeel Butt
2026-10-14 11:21   ` Michal Hocko
2026-10-10 21:09 ` [PATCH for-7.5 3/5] memcg: use high_work for remote charges Shakeel Butt
2026-10-14 11:29   ` Michal Hocko
2026-10-10 21:09 ` [PATCH for-7.5 4/5] memcg: avoid queuing high_work from reclaim Shakeel Butt
2026-10-14 11:39   ` Michal Hocko
2026-10-14 16:06     ` Shakeel Butt
2026-10-14 16:59       ` Michal Hocko [this message]
2026-10-14 17:55         ` Shakeel Butt
2026-10-14 19:27           ` Michal Hocko
2026-10-14 19:36             ` Shakeel Butt
2026-10-10 21:09 ` [PATCH for-7.5 5/5] memcg: simplify high limit handling after a charge Shakeel Butt
2026-10-14 11:46   ` Michal Hocko
2026-10-14 17:43     ` Shakeel Butt
2026-10-14 19:30       ` Michal Hocko

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=as-08j9_1C2F_N8s@tiehlicka \
    --to=mhocko@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox