From: Michal Hocko <mhocko@suse.com>
To: Alexander Fedorov <halcien@gmail.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>,
Johannes Weiner <hannes@cmpxchg.org>,
Shakeel Butt <shakeelb@google.com>,
Vladimir Davydov <vdavydov.dev@gmail.com>,
Muchun Song <songmuchun@bytedance.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
cgroups@vger.kernel.org, linux-mm@kvack.org
Subject: Re: Possible race in obj_stock_flush_required() vs drain_obj_stock()
Date: Mon, 3 Oct 2022 15:32:24 +0200 [thread overview]
Message-ID: <YzrkaKZKYqx+c325@dhcp22.suse.cz> (raw)
In-Reply-To: <d3cf9c69-19a1-53f9-cf97-5d40ce5cda44@gmail.com>
On Mon 03-10-22 15:47:10, Alexander Fedorov wrote:
> On 02.10.2022 19:16, Roman Gushchin wrote:
> > On Sat, Oct 01, 2022 at 03:38:43PM +0300, Alexander Fedorov wrote:
> >> Tested READ_ONCE() patch and it works.
> >
> > Thank you!
> >
> >> But are rcu primitives an overkill?
> >> For me they are documenting how actually complex is synchronization here.
> >
> > I agree, however rcu primitives will add unnecessary barriers on hot paths.
> > In this particular case most accesses to stock->cached_objcg are done from
> > a local cpu, so no rcu primitives are needed. So in my opinion using a
> > READ_ONCE() is preferred.
>
> Understood, then here is patch that besides READ_ONCE() also fixes mentioned
> use-after-free that exists in 5.10. In mainline the drain_obj_stock() part
> of the patch should be skipped.
>
> Should probably be also Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
> but I am not sure if I have rights to add that :)
>
>
> mm/memcg: fix race in obj_stock_flush_required() vs drain_obj_stock()
>
> When obj_stock_flush_required() is called from drain_all_stock() it
> reads the `memcg_stock->cached_objcg` field twice for another CPU's
> per-cpu variable, leading to TOCTTOU race: another CPU can
> simultaneously enter drain_obj_stock() and clear its own instance of
> `memcg_stock->cached_objcg`.
>
> Another problem is in drain_obj_stock() which sets `cached_objcg` to
> NULL after freeing which might lead to use-after-free.
>
> To fix it use READ_ONCE() for TOCTTOU problem and also clear the
> `cached_objcg` pointer earlier in drain_obj_stock() for use-after-free
> problem.
>
> Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API")
> Signed-off-by: Alexander Fedorov <halcien@gmail.com>
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index c1152f8747..56bd5ea6d3 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -3197,17 +3197,30 @@ static void drain_obj_stock(struct memcg_stock_pcp *stock)
> stock->nr_bytes = 0;
> }
>
> - obj_cgroup_put(old);
> + /*
> + * Clear pointer before freeing memory so that
> + * drain_all_stock() -> obj_stock_flush_required()
> + * does not see a freed pointer.
> + */
> stock->cached_objcg = NULL;
> + obj_cgroup_put(old);
Do we need barrier() or something else to ensure there is no reordering?
I am not reallyu sure what kind of barriers are implied by the pcp ref
counting.
--
Michal Hocko
SUSE Labs
next prev parent reply other threads:[~2022-10-03 13:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-30 14:06 Alexander Fedorov
2022-09-30 18:26 ` Roman Gushchin
2022-10-01 12:38 ` Alexander Fedorov
2022-10-02 16:16 ` Roman Gushchin
2022-10-03 12:47 ` Alexander Fedorov
2022-10-03 13:32 ` Michal Hocko [this message]
2022-10-03 14:09 ` Alexander Fedorov
2022-10-03 14:27 ` Michal Hocko
2022-10-03 15:01 ` Alexander Fedorov
2022-10-04 16:18 ` Roman Gushchin
2022-10-12 17:23 ` Johannes Weiner
2022-10-12 18:49 ` Roman Gushchin
2022-10-12 19:18 ` Johannes Weiner
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=YzrkaKZKYqx+c325@dhcp22.suse.cz \
--to=mhocko@suse.com \
--cc=bigeasy@linutronix.de \
--cc=cgroups@vger.kernel.org \
--cc=halcien@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=linux-mm@kvack.org \
--cc=roman.gushchin@linux.dev \
--cc=shakeelb@google.com \
--cc=songmuchun@bytedance.com \
--cc=vdavydov.dev@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