From: Harry Yoo <harry@kernel.org>
To: Takuo Koguchi <takuo.koguchi@gmail.com>
Cc: cip-dev@lists.cip-project.org,
Steven Rostedt <rostedt@goodmis.org>,
Takuo Koguchi <takuo.koguchi.sw@hitachi.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Muchun Song <muchun.song@linux.dev>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Clark Williams <clrkwllms@kernel.org>,
linux-rt-devel@lists.linux.dev
Subject: Re: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
Date: Wed, 25 Nov 2026 15:13:05 +0000 [thread overview]
Message-ID: <awb6bJ1-_8zeyTYQ@thinkstation> (raw)
In-Reply-To: <20261125043900.3618611-1-takuo.koguchi.sw@hitachi.com>
[+Cc PREEMPT_RT and MEMCG folks properly ]
This doesn't look like a patch for the latest mainline.
Which kernel version is it based onto?
On Wed, Nov 25, 2026 at 01:39:00PM +0900, Takuo Koguchi wrote:
> On PREEMPT_RT, drain_local_stock() accesses the per-cpu memcg_stock
> via this_cpu_ptr() without disabling preemption.
>
> Accesses to memcg_stock from consume_stock() and refill_stock() are
> already serialized using get_cpu_var()/put_cpu_var(). However,
> drain_local_stock() still accesses the same per-cpu state without
> equivalent protection.
>
> As a result, drain_local_stock() may be preempted while operating on
> memcg_stock, allowing another task running on the same CPU to observe
> or modify the same per-cpu stock concurrently. This may result in
> inconsistent updates of cached charges and stock counts.
>
> Failures were observed as divergence between memcg accounting and
> actual memory consumption. The divergence occurred in both positive
> and negative directions, indicating corruption of accounting state
> rather than a simple charge leak.
>
> Under stress testing, the accounting divergence was reproducible and
> was frequently accompanied by oops storms. Protecting
> drain_local_stock() with get_cpu_var()/put_cpu_var() eliminated the
> accounting divergence and the associated stability issues.
>
> Use get_cpu_var()/put_cpu_var() in drain_local_stock() to serialize
> accesses to the per-cpu memcg stock and make its protection consistent
> with the other memcg stock users.
>
> Signed-off-by: Takuo Koguchi <takuo.koguchi.sw@hitachi.com>
> ---
> mm/memcontrol.c | 13 ++++++++++---
> 1 file changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index f9d80952723c..9b298ce10a7a 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1933,14 +1933,21 @@ static void drain_stock(struct memcg_stock_pcp *stock)
> }
>
> /*
> - * This must be called under preempt disabled or must be called by
> - * a thread which is pinned to local cpu.
> + * Drain this cpu's stock. Called directly by drain_all_stock() for the
> + * local cpu and as a work item for the other cpus. Preemption is
> + * disabled here because neither caller guarantees it on PREEMPT_RT:
> + * drain_all_stock() only holds get_cpu_light(), which is a plain
> + * barrier() without CONFIG_SMP, and the work item runs in a preemptible
> + * kworker. consume_stock() and refill_stock() run with preemption
> + * disabled, so this keeps every user of the per-cpu stock serialized.
> */
> static void drain_local_stock(struct work_struct *dummy)
> {
> - struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
> + struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
> +
> drain_stock(stock);
> clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
> + put_cpu_var(memcg_stock);
> }
>
> /*
> --
> 2.34.1
--
Cheers,
Harry / Hyeonggon
next parent reply other threads:[~2026-11-25 15:13 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261125043900.3618611-1-takuo.koguchi.sw@hitachi.com>
2026-11-25 15:13 ` Harry Yoo [this message]
2026-11-25 15:19 ` [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var() Sebastian Andrzej Siewior
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=awb6bJ1-_8zeyTYQ@thinkstation \
--to=harry@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=cgroups@vger.kernel.org \
--cc=cip-dev@lists.cip-project.org \
--cc=clrkwllms@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=shakeel.butt@linux.dev \
--cc=takuo.koguchi.sw@hitachi.com \
--cc=takuo.koguchi@gmail.com \
/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