From: Yosry Ahmed <yosryahmed@google.com>
To: Roman Gushchin <roman.gushchin@linux.dev>
Cc: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Shakeel Butt <shakeelb@google.com>,
Muchun Song <muchun.song@linux.dev>,
linux-kernel@vger.kernel.org, Dmitry Vyukov <dvyukov@google.com>
Subject: Re: [PATCH v2 2/2] mm: memcg: use READ_ONCE()/WRITE_ONCE() to access stock->cached
Date: Tue, 2 May 2023 13:21:30 -0700 [thread overview]
Message-ID: <CAJD7tkbPUNv+tNDwnsmyXdOStCoA91dBCX+bRDQx5pDQx4bmpw@mail.gmail.com> (raw)
In-Reply-To: <20230502160839.361544-2-roman.gushchin@linux.dev>
On Tue, May 2, 2023 at 9:09 AM Roman Gushchin <roman.gushchin@linux.dev> wrote:
>
> A memcg pointer in the percpu stock can be accessed by drain_all_stock()
> from another cpu in a lockless way.
> In theory it might lead to an issue, similar to the one which has been
> discovered with stock->cached_objcg, where the pointer was zeroed
> between the check for being NULL and dereferencing.
> In this case the issue is unlikely a real problem, but to make it
> bulletproof and similar to stock->cached_objcg, let's annotate all
> accesses to stock->cached with READ_ONCE()/WTRITE_ONCE().
Is it time to rename that to cached_memcg? :)
Anyway, same comment as patch 1 about annotating all reads with
READ_ONCE() vs. singling out the racy read.
>
> Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Cc: Yosry Ahmed <yosryahmed@google.com>
> Cc: Shakeel Butt <shakeelb@google.com>
> ---
> mm/memcontrol.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index c823c35c2ed4..1e364ad495a3 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2275,7 +2275,7 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
> local_lock_irqsave(&memcg_stock.stock_lock, flags);
>
> stock = this_cpu_ptr(&memcg_stock);
> - if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
> + if (memcg == READ_ONCE(stock->cached) && stock->nr_pages >= nr_pages) {
> stock->nr_pages -= nr_pages;
> ret = true;
> }
> @@ -2290,7 +2290,7 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
> */
> static void drain_stock(struct memcg_stock_pcp *stock)
> {
> - struct mem_cgroup *old = stock->cached;
> + struct mem_cgroup *old = READ_ONCE(stock->cached);
>
> if (!old)
> return;
> @@ -2303,7 +2303,7 @@ static void drain_stock(struct memcg_stock_pcp *stock)
> }
>
> css_put(&old->css);
> - stock->cached = NULL;
> + WRITE_ONCE(stock->cached, NULL);
Is it me or can we call drain_stock() from memcg_hotplug_cpu_dead()
without holding the lock, unlike all other callers. Is this a problem?
> }
>
> static void drain_local_stock(struct work_struct *dummy)
> @@ -2338,10 +2338,10 @@ static void __refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
> struct memcg_stock_pcp *stock;
>
> stock = this_cpu_ptr(&memcg_stock);
> - if (stock->cached != memcg) { /* reset if necessary */
> + if (READ_ONCE(stock->cached) != memcg) { /* reset if necessary */
> drain_stock(stock);
> css_get(&memcg->css);
> - stock->cached = memcg;
> + WRITE_ONCE(stock->cached, memcg);
> }
> stock->nr_pages += nr_pages;
>
> @@ -2383,7 +2383,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
> bool flush = false;
>
> rcu_read_lock();
> - memcg = stock->cached;
> + memcg = READ_ONCE(stock->cached);
> if (memcg && stock->nr_pages &&
> mem_cgroup_is_descendant(memcg, root_memcg))
> flush = true;
> --
> 2.40.1
>
next prev parent reply other threads:[~2023-05-02 20:22 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-02 16:08 [PATCH v2 1/2] mm: kmem: fix a NULL pointer dereference in obj_stock_flush_required() Roman Gushchin
2023-05-02 16:08 ` [PATCH v2 2/2] mm: memcg: use READ_ONCE()/WRITE_ONCE() to access stock->cached Roman Gushchin
2023-05-02 20:21 ` Yosry Ahmed [this message]
2023-05-03 17:06 ` Shakeel Butt
2023-05-02 20:15 ` [PATCH v2 1/2] mm: kmem: fix a NULL pointer dereference in obj_stock_flush_required() Yosry Ahmed
2023-05-02 21:38 ` Roman Gushchin
2023-05-02 22:10 ` Yosry Ahmed
2023-05-03 17:03 ` Shakeel Butt
2023-05-03 8:05 ` Dmitry Vyukov
2023-05-03 17:04 ` Shakeel Butt
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=CAJD7tkbPUNv+tNDwnsmyXdOStCoA91dBCX+bRDQx5pDQx4bmpw@mail.gmail.com \
--to=yosryahmed@google.com \
--cc=akpm@linux-foundation.org \
--cc=dvyukov@google.com \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=shakeelb@google.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