linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj()
@ 2020-09-10  2:24 Roman Gushchin
  2020-09-10 22:43 ` Roman Gushchin
  0 siblings, 1 reply; 6+ messages in thread
From: Roman Gushchin @ 2020-09-10  2:24 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: =Shakeel Butt, Johannes Weiner, Michal Hocko, kernel-team,
	linux-kernel, Roman Gushchin, Vlastimil Babka

mem_cgroup_from_obj() checks the lowest bit of the page->mem_cgroup
pointer to determine if the page has an attached obj_cgroup vector
instead of a regular memcg pointer. If it's not set, it simple returns
the page->mem_cgroup value as a struct mem_cgroup pointer.

The commit 10befea91b61 ("mm: memcg/slab: use a single set of
kmem_caches for all allocations") changed the moment when this bit
is set: if previously it was set on the allocation of the slab page,
now it can be set well after, when the first accounted object is
allocated on this page.

It opened a race: if page->mem_cgroup is set concurrently after the
first page_has_obj_cgroups(page) check, a pointer to the obj_cgroups
array can be returned as a memory cgroup pointer.

A simple check for page->mem_cgroup pointer for NULL before the
page_has_obj_cgroups() check fixes the race. Indeed, if the pointer
is not NULL, it's either a simple mem_cgroup pointer or a pointer
to obj_cgroup vector. The pointer can be asynchronously changed
from NULL to (obj_cgroup_vec | 0x1UL), but can't be changed
from a valid memcg pointer to objcg vector or back.

If the object passed to mem_cgroup_from_obj() is a slab object
and page->mem_cgroup is NULL, it means that the object is not
accounted, so the function must return NULL.

I've discovered the race looking at the code, so far I haven't seen it
in the wild.

Fixes: 10befea91b61 ("mm: memcg/slab: use a single set of kmem_caches for all allocations")
Signed-off-by: Roman Gushchin <guro@fb.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Shakeel Butt <shakeelb@google.com>
---
 mm/memcontrol.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 75cd1a1e66c8..093526fec4bf 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2923,6 +2923,17 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p)
 
 	page = virt_to_head_page(p);
 
+	/*
+	 * If page->mem_cgroup is set, it's either a simple mem_cgroup pointer
+	 * or a pointer to obj_cgroup vector. In the latter case the lowest
+	 * bit of the pointer is set.
+	 * The page->mem_cgroup pointer can be asynchronously changed
+	 * from NULL to (obj_cgroup_vec | 0x1UL), but can't be changed
+	 * from a valid memcg pointer to objcg vector or back.
+	 */
+	if (!page->mem_cgroup)
+		return NULL;
+
 	/*
 	 * Slab objects are accounted individually, not per-page.
 	 * Memcg membership data for each individual object is saved in
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj()
  2020-09-10  2:24 [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj() Roman Gushchin
@ 2020-09-10 22:43 ` Roman Gushchin
  2020-09-11 16:21   ` Shakeel Butt
  2020-09-11 22:41   ` Shakeel Butt
  0 siblings, 2 replies; 6+ messages in thread
From: Roman Gushchin @ 2020-09-10 22:43 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: Shakeel Butt, Johannes Weiner, Michal Hocko, kernel-team,
	linux-kernel, Vlastimil Babka, stable

Forgot to cc stable@, an updated version is below.

Thanks!

--

From fe61af45ae570b143ca783ba4d013a0a2b923a15 Mon Sep 17 00:00:00 2001
From: Roman Gushchin <guro@fb.com>
Date: Wed, 9 Sep 2020 12:19:37 -0700
Subject: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in
 mem_cgroup_from_obj()

mem_cgroup_from_obj() checks the lowest bit of the page->mem_cgroup
pointer to determine if the page has an attached obj_cgroup vector
instead of a regular memcg pointer. If it's not set, it simple returns
the page->mem_cgroup value as a struct mem_cgroup pointer.

The commit 10befea91b61 ("mm: memcg/slab: use a single set of
kmem_caches for all allocations") changed the moment when this bit
is set: if previously it was set on the allocation of the slab page,
now it can be set well after, when the first accounted object is
allocated on this page.

It opened a race: if page->mem_cgroup is set concurrently after the
first page_has_obj_cgroups(page) check, a pointer to the obj_cgroups
array can be returned as a memory cgroup pointer.

A simple check for page->mem_cgroup pointer for NULL before the
page_has_obj_cgroups() check fixes the race. Indeed, if the pointer
is not NULL, it's either a simple mem_cgroup pointer or a pointer
to obj_cgroup vector. The pointer can be asynchronously changed
from NULL to (obj_cgroup_vec | 0x1UL), but can't be changed
from a valid memcg pointer to objcg vector or back.

If the object passed to mem_cgroup_from_obj() is a slab object
and page->mem_cgroup is NULL, it means that the object is not
accounted, so the function must return NULL.

I've discovered the race looking at the code, so far I haven't seen it
in the wild.

Fixes: 10befea91b61 ("mm: memcg/slab: use a single set of kmem_caches for all allocations")
Signed-off-by: Roman Gushchin <guro@fb.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: stable@vger.kernel.org
---
 mm/memcontrol.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 75cd1a1e66c8..093526fec4bf 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2923,6 +2923,17 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p)
 
 	page = virt_to_head_page(p);
 
+	/*
+	 * If page->mem_cgroup is set, it's either a simple mem_cgroup pointer
+	 * or a pointer to obj_cgroup vector. In the latter case the lowest
+	 * bit of the pointer is set.
+	 * The page->mem_cgroup pointer can be asynchronously changed
+	 * from NULL to (obj_cgroup_vec | 0x1UL), but can't be changed
+	 * from a valid memcg pointer to objcg vector or back.
+	 */
+	if (!page->mem_cgroup)
+		return NULL;
+
 	/*
 	 * Slab objects are accounted individually, not per-page.
 	 * Memcg membership data for each individual object is saved in
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj()
  2020-09-10 22:43 ` Roman Gushchin
@ 2020-09-11 16:21   ` Shakeel Butt
  2020-09-11 21:34     ` Roman Gushchin
  2020-09-11 22:41   ` Shakeel Butt
  1 sibling, 1 reply; 6+ messages in thread
From: Shakeel Butt @ 2020-09-11 16:21 UTC (permalink / raw)
  To: Roman Gushchin
  Cc: Andrew Morton, Linux MM, Johannes Weiner, Michal Hocko,
	Kernel Team, LKML, Vlastimil Babka, stable

On Thu, Sep 10, 2020 at 3:43 PM Roman Gushchin <guro@fb.com> wrote:
>
> Forgot to cc stable@, an updated version is below.
>
> Thanks!
>
> --
>
> From fe61af45ae570b143ca783ba4d013a0a2b923a15 Mon Sep 17 00:00:00 2001
> From: Roman Gushchin <guro@fb.com>
> Date: Wed, 9 Sep 2020 12:19:37 -0700
> Subject: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in
>  mem_cgroup_from_obj()
>
> mem_cgroup_from_obj() checks the lowest bit of the page->mem_cgroup
> pointer to determine if the page has an attached obj_cgroup vector
> instead of a regular memcg pointer. If it's not set, it simple returns
> the page->mem_cgroup value as a struct mem_cgroup pointer.
>
> The commit 10befea91b61 ("mm: memcg/slab: use a single set of
> kmem_caches for all allocations") changed the moment when this bit
> is set: if previously it was set on the allocation of the slab page,
> now it can be set well after, when the first accounted object is
> allocated on this page.
>
> It opened a race: if page->mem_cgroup is set concurrently after the
> first page_has_obj_cgroups(page) check, a pointer to the obj_cgroups
> array can be returned as a memory cgroup pointer.
>
> A simple check for page->mem_cgroup pointer for NULL before the
> page_has_obj_cgroups() check fixes the race. Indeed, if the pointer
> is not NULL, it's either a simple mem_cgroup pointer or a pointer
> to obj_cgroup vector. The pointer can be asynchronously changed
> from NULL to (obj_cgroup_vec | 0x1UL), but can't be changed
> from a valid memcg pointer to objcg vector or back.
>
> If the object passed to mem_cgroup_from_obj() is a slab object
> and page->mem_cgroup is NULL, it means that the object is not
> accounted, so the function must return NULL.
>
> I've discovered the race looking at the code, so far I haven't seen it
> in the wild.
>
> Fixes: 10befea91b61 ("mm: memcg/slab: use a single set of kmem_caches for all allocations")
> Signed-off-by: Roman Gushchin <guro@fb.com>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Vlastimil Babka <vbabka@suse.cz>
> Cc: Shakeel Butt <shakeelb@google.com>
> Cc: stable@vger.kernel.org
> ---

Is the caller list_lru_from_kmem() the concern or is this more about
making mem_cgroup_from_obj() more future proof?

Also have you taken a look at [1]? I am still trying to figure out how
that is possible.

[1] https://lore.kernel.org/lkml/20200901075321.GL4299@shao2-debian/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj()
  2020-09-11 16:21   ` Shakeel Butt
@ 2020-09-11 21:34     ` Roman Gushchin
  2020-09-11 22:40       ` Shakeel Butt
  0 siblings, 1 reply; 6+ messages in thread
From: Roman Gushchin @ 2020-09-11 21:34 UTC (permalink / raw)
  To: Shakeel Butt
  Cc: Andrew Morton, Linux MM, Johannes Weiner, Michal Hocko,
	Kernel Team, LKML, Vlastimil Babka, stable

On Fri, Sep 11, 2020 at 09:21:49AM -0700, Shakeel Butt wrote:
> On Thu, Sep 10, 2020 at 3:43 PM Roman Gushchin <guro@fb.com> wrote:
> >
> > Forgot to cc stable@, an updated version is below.
> >
> > Thanks!
> >
> > --
> >
> > From fe61af45ae570b143ca783ba4d013a0a2b923a15 Mon Sep 17 00:00:00 2001
> > From: Roman Gushchin <guro@fb.com>
> > Date: Wed, 9 Sep 2020 12:19:37 -0700
> > Subject: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in
> >  mem_cgroup_from_obj()
> >
> > mem_cgroup_from_obj() checks the lowest bit of the page->mem_cgroup
> > pointer to determine if the page has an attached obj_cgroup vector
> > instead of a regular memcg pointer. If it's not set, it simple returns
> > the page->mem_cgroup value as a struct mem_cgroup pointer.
> >
> > The commit 10befea91b61 ("mm: memcg/slab: use a single set of
> > kmem_caches for all allocations") changed the moment when this bit
> > is set: if previously it was set on the allocation of the slab page,
> > now it can be set well after, when the first accounted object is
> > allocated on this page.
> >
> > It opened a race: if page->mem_cgroup is set concurrently after the
> > first page_has_obj_cgroups(page) check, a pointer to the obj_cgroups
> > array can be returned as a memory cgroup pointer.
> >
> > A simple check for page->mem_cgroup pointer for NULL before the
> > page_has_obj_cgroups() check fixes the race. Indeed, if the pointer
> > is not NULL, it's either a simple mem_cgroup pointer or a pointer
> > to obj_cgroup vector. The pointer can be asynchronously changed
> > from NULL to (obj_cgroup_vec | 0x1UL), but can't be changed
> > from a valid memcg pointer to objcg vector or back.
> >
> > If the object passed to mem_cgroup_from_obj() is a slab object
> > and page->mem_cgroup is NULL, it means that the object is not
> > accounted, so the function must return NULL.
> >
> > I've discovered the race looking at the code, so far I haven't seen it
> > in the wild.
> >
> > Fixes: 10befea91b61 ("mm: memcg/slab: use a single set of kmem_caches for all allocations")
> > Signed-off-by: Roman Gushchin <guro@fb.com>
> > Cc: Johannes Weiner <hannes@cmpxchg.org>
> > Cc: Vlastimil Babka <vbabka@suse.cz>
> > Cc: Shakeel Butt <shakeelb@google.com>
> > Cc: stable@vger.kernel.org
> > ---
> 
> Is the caller list_lru_from_kmem() the concern or is this more about
> making mem_cgroup_from_obj() more future proof?

I was doing some refactorings around (see
https://lore.kernel.org/linux-mm/20200910202659.1378404-1-guro@fb.com/T/#t ),
and just noticed it from looking at the code. I'm not aware of any real life
consequences at the moment.

> 
> Also have you taken a look at [1]? I am still trying to figure out how
> that is possible.
> 
> [1] https://lore.kernel.org/lkml/20200901075321.GL4299@shao2-debian/

Hm, yeah, it's complicated. At the very first glance it looks like that the
obj_cgroups vector is placed onto the very same page it describes, or at least
it shares the kmem_cache with it, with some bad consequences. Could be something
SLAB-specific, newer saw anything like that with SLUB.
Or maybe it's completely unrelated and has been attributed to this commit
by mistake.

I've spent several hours running the provided test in a loop, but wasn't
lucky enough to trigger it. Did you try?

Thanks!

Roman

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj()
  2020-09-11 21:34     ` Roman Gushchin
@ 2020-09-11 22:40       ` Shakeel Butt
  0 siblings, 0 replies; 6+ messages in thread
From: Shakeel Butt @ 2020-09-11 22:40 UTC (permalink / raw)
  To: Roman Gushchin
  Cc: Andrew Morton, Linux MM, Johannes Weiner, Michal Hocko,
	Kernel Team, LKML, Vlastimil Babka, stable

On Fri, Sep 11, 2020 at 2:34 PM Roman Gushchin <guro@fb.com> wrote:
>
[snip]
> >
> > Also have you taken a look at [1]? I am still trying to figure out how
> > that is possible.
> >
> > [1] https://lore.kernel.org/lkml/20200901075321.GL4299@shao2-debian/
>
> Hm, yeah, it's complicated. At the very first glance it looks like that the
> obj_cgroups vector is placed onto the very same page it describes, or at least
> it shares the kmem_cache with it, with some bad consequences. Could be something
> SLAB-specific, newer saw anything like that with SLUB.
> Or maybe it's completely unrelated and has been attributed to this commit
> by mistake.
>
> I've spent several hours running the provided test in a loop, but wasn't
> lucky enough to trigger it. Did you try?
>

Yeah same, no success in reproducing it.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj()
  2020-09-10 22:43 ` Roman Gushchin
  2020-09-11 16:21   ` Shakeel Butt
@ 2020-09-11 22:41   ` Shakeel Butt
  1 sibling, 0 replies; 6+ messages in thread
From: Shakeel Butt @ 2020-09-11 22:41 UTC (permalink / raw)
  To: Roman Gushchin
  Cc: Andrew Morton, Linux MM, Johannes Weiner, Michal Hocko,
	Kernel Team, LKML, Vlastimil Babka, stable

On Thu, Sep 10, 2020 at 3:43 PM Roman Gushchin <guro@fb.com> wrote:
>
> Forgot to cc stable@, an updated version is below.
>
> Thanks!
>
> --
>
> From fe61af45ae570b143ca783ba4d013a0a2b923a15 Mon Sep 17 00:00:00 2001
> From: Roman Gushchin <guro@fb.com>
> Date: Wed, 9 Sep 2020 12:19:37 -0700
> Subject: [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in
>  mem_cgroup_from_obj()
>
> mem_cgroup_from_obj() checks the lowest bit of the page->mem_cgroup
> pointer to determine if the page has an attached obj_cgroup vector
> instead of a regular memcg pointer. If it's not set, it simple returns
> the page->mem_cgroup value as a struct mem_cgroup pointer.
>
> The commit 10befea91b61 ("mm: memcg/slab: use a single set of
> kmem_caches for all allocations") changed the moment when this bit
> is set: if previously it was set on the allocation of the slab page,
> now it can be set well after, when the first accounted object is
> allocated on this page.
>
> It opened a race: if page->mem_cgroup is set concurrently after the
> first page_has_obj_cgroups(page) check, a pointer to the obj_cgroups
> array can be returned as a memory cgroup pointer.
>
> A simple check for page->mem_cgroup pointer for NULL before the
> page_has_obj_cgroups() check fixes the race. Indeed, if the pointer
> is not NULL, it's either a simple mem_cgroup pointer or a pointer
> to obj_cgroup vector. The pointer can be asynchronously changed
> from NULL to (obj_cgroup_vec | 0x1UL), but can't be changed
> from a valid memcg pointer to objcg vector or back.
>
> If the object passed to mem_cgroup_from_obj() is a slab object
> and page->mem_cgroup is NULL, it means that the object is not
> accounted, so the function must return NULL.
>
> I've discovered the race looking at the code, so far I haven't seen it
> in the wild.
>
> Fixes: 10befea91b61 ("mm: memcg/slab: use a single set of kmem_caches for all allocations")
> Signed-off-by: Roman Gushchin <guro@fb.com>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Vlastimil Babka <vbabka@suse.cz>
> Cc: Shakeel Butt <shakeelb@google.com>
> Cc: stable@vger.kernel.org

I think this patch is good to have as it will make
mem_cgroup_from_obj() more future proof.

Reviewed-by: Shakeel Butt <shakeelb@google.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-09-11 22:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-10  2:24 [PATCH] mm: memcg/slab: fix racy access to page->mem_cgroup in mem_cgroup_from_obj() Roman Gushchin
2020-09-10 22:43 ` Roman Gushchin
2020-09-11 16:21   ` Shakeel Butt
2020-09-11 21:34     ` Roman Gushchin
2020-09-11 22:40       ` Shakeel Butt
2020-09-11 22:41   ` Shakeel Butt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).