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 1/5] memcg: move memory.high enforcement out of try_charge_memcg()
Date: Wed, 14 Oct 2026 13:13:34 +0200 [thread overview]
Message-ID: <as9j3rH3qRtFO0-7@tiehlicka> (raw)
In-Reply-To: <20261010210957.1350874-2-shakeel.butt@linux.dev>
On Sat 10-10-26 14:09:53, Shakeel Butt wrote:
> try_charge_memcg() does a lot. It charges the page counters, reclaims
> when a limit is hit, may call the OOM killer, and at the end it checks
> and enforces memory.high. Move the memory.high part into a new function,
> memcg_enforce_high(), so that try_charge_memcg() is shorter and the
> memory.high code is easier to read and change.
>
> The code and its comments move as they are. The only difference is that
> the new function gets allow_spinning from gfp_mask itself.
>
> No functional change.
Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!
> ---
> mm/memcontrol.c | 125 ++++++++++++++++++++++++++----------------------
> 1 file changed, 69 insertions(+), 56 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index aad0498a7bd6..20c5dfe1a894 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2696,6 +2696,74 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask)
> css_put(&memcg->css);
> }
>
> +/*
> + * Called after @nr_pages were charged to @memcg. If @memcg or one of its
> + * ancestors is over memory.high or memory.swap.high, start reclaim or slow
> + * down the task.
> + */
> +static void memcg_enforce_high(struct mem_cgroup *memcg, unsigned int nr_pages,
> + gfp_t gfp_mask)
> +{
> + bool allow_spinning = gfpflags_allow_spinning(gfp_mask);
> +
> + /*
> + * If the hierarchy is above the normal consumption range, schedule
> + * reclaim on returning to userland. We can perform reclaim here
> + * if __GFP_RECLAIM but let's always punt for simplicity and so that
> + * GFP_KERNEL can consistently be used during reclaim. @memcg is
> + * not recorded as it most likely matches current's and won't
> + * change in the meantime. As high limit is checked again before
> + * reclaim, the cost of mismatch is negligible.
> + */
> + do {
> + bool mem_high, swap_high;
> +
> + mem_high = page_counter_read(&memcg->memory) >
> + READ_ONCE(memcg->memory.high);
> + swap_high = page_counter_read(&memcg->swap) >
> + READ_ONCE(memcg->swap.high);
> +
> + /* Don't bother a random interrupted task */
> + if (!in_task()) {
> + if (mem_high) {
> + if (allow_spinning)
> + schedule_work(&memcg->high_work);
> + else
> + irq_work_queue(&memcg->high_irq_work);
> + break;
> + }
> + continue;
> + }
> +
> + if (mem_high || swap_high) {
> + /*
> + * The allocating tasks in this cgroup will need to do
> + * reclaim or be throttled to prevent further growth
> + * of the memory or swap footprints.
> + *
> + * Target some best-effort fairness between the tasks,
> + * and distribute reclaim work and delay penalties
> + * based on how much each task is actually allocating.
> + */
> + current->memcg_nr_pages_over_high += nr_pages;
> + set_notify_resume(current);
> + break;
> + }
> + } while ((memcg = parent_mem_cgroup(memcg)));
> +
> + /*
> + * Reclaim is set up above to be called from the userland
> + * return path. But also attempt synchronous reclaim to avoid
> + * excessive overrun while the task is still inside the
> + * kernel. If this is successful, the return path will see it
> + * when it rechecks the overage and simply bail out.
> + */
> + if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH &&
> + !(current->flags & PF_MEMALLOC) &&
> + gfpflags_allow_blocking(gfp_mask))
> + __mem_cgroup_handle_over_high(gfp_mask);
> +}
> +
> static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
> unsigned int nr_pages)
> {
> @@ -2853,62 +2921,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
> if (batch > nr_pages)
> refill_stock(memcg, batch - nr_pages);
>
> - /*
> - * If the hierarchy is above the normal consumption range, schedule
> - * reclaim on returning to userland. We can perform reclaim here
> - * if __GFP_RECLAIM but let's always punt for simplicity and so that
> - * GFP_KERNEL can consistently be used during reclaim. @memcg is
> - * not recorded as it most likely matches current's and won't
> - * change in the meantime. As high limit is checked again before
> - * reclaim, the cost of mismatch is negligible.
> - */
> - do {
> - bool mem_high, swap_high;
> -
> - mem_high = page_counter_read(&memcg->memory) >
> - READ_ONCE(memcg->memory.high);
> - swap_high = page_counter_read(&memcg->swap) >
> - READ_ONCE(memcg->swap.high);
> -
> - /* Don't bother a random interrupted task */
> - if (!in_task()) {
> - if (mem_high) {
> - if (allow_spinning)
> - schedule_work(&memcg->high_work);
> - else
> - irq_work_queue(&memcg->high_irq_work);
> - break;
> - }
> - continue;
> - }
> -
> - if (mem_high || swap_high) {
> - /*
> - * The allocating tasks in this cgroup will need to do
> - * reclaim or be throttled to prevent further growth
> - * of the memory or swap footprints.
> - *
> - * Target some best-effort fairness between the tasks,
> - * and distribute reclaim work and delay penalties
> - * based on how much each task is actually allocating.
> - */
> - current->memcg_nr_pages_over_high += batch;
> - set_notify_resume(current);
> - break;
> - }
> - } while ((memcg = parent_mem_cgroup(memcg)));
> -
> - /*
> - * Reclaim is set up above to be called from the userland
> - * return path. But also attempt synchronous reclaim to avoid
> - * excessive overrun while the task is still inside the
> - * kernel. If this is successful, the return path will see it
> - * when it rechecks the overage and simply bail out.
> - */
> - if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH &&
> - !(current->flags & PF_MEMALLOC) &&
> - gfpflags_allow_blocking(gfp_mask))
> - __mem_cgroup_handle_over_high(gfp_mask);
> + memcg_enforce_high(memcg, batch, gfp_mask);
> return ret;
> }
>
> --
> 2.53.0-Meta
--
Michal Hocko
SUSE Labs
next prev parent reply other threads:[~2026-10-14 11:13 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 [this message]
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
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=as9j3rH3qRtFO0-7@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