All of lore.kernel.org
 help / color / mirror / Atom feed
From: Roman Gushchin <guro@fb.com>
To: Muchun Song <songmuchun@bytedance.com>
Cc: <hannes@cmpxchg.org>, <mhocko@kernel.org>,
	<vdavydov.dev@gmail.com>, <akpm@linux-foundation.org>,
	<shakeelb@google.com>, <iamjoonsoo.kim@lge.com>,
	<laoar.shao@gmail.com>, <chris@chrisdown.name>,
	<christian.brauner@ubuntu.com>, <peterz@infradead.org>,
	<mingo@kernel.org>, <keescook@chromium.org>, <tglx@linutronix.de>,
	<esyr@redhat.com>, <surenb@google.com>, <areber@redhat.com>,
	<elver@google.com>, <linux-kernel@vger.kernel.org>,
	<cgroups@vger.kernel.org>, <linux-mm@kvack.org>
Subject: Re: [PATCH 1/5] mm: memcg/slab: Fix return child memcg objcg for root memcg
Date: Tue, 27 Oct 2020 10:42:36 -0700	[thread overview]
Message-ID: <20201027174236.GB725724@carbon.dhcp.thefacebook.com> (raw)
In-Reply-To: <20201027080256.76497-2-songmuchun@bytedance.com>

On Tue, Oct 27, 2020 at 04:02:52PM +0800, Muchun Song wrote:
> Consider the following memcg hierarchy.
> 
>                     root
>                    /    \
>                   A      B
> 
> If we get the objcg of memcg A failed, the get_obj_cgroup_from_current
> can return the wrong objcg for the root memcg.
> 
> Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API")
> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> ---
>  mm/memcontrol.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)

Hi Muchun,

thank you for the patchset!

A generic note: it's usually not a good idea to group fixes with non-fixes
in one patchset if fixes are planned to be backported to stable.

> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 1337775b04f3..fcbd79c5023e 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2945,7 +2945,7 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p)
>  
>  __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
>  {
> -	struct obj_cgroup *objcg = NULL;
> +	struct obj_cgroup *objcg;
>  	struct mem_cgroup *memcg;
>  
>  	if (memcg_kmem_bypass())
> @@ -2964,6 +2964,9 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
>  	}
>  	rcu_read_unlock();
>  
> +	if (memcg == root_mem_cgroup)
> +		return NULL;
> +
>  	return objcg;
>  }

I agree, it's a bug. Good catch, thanks!

I would prefer a slightly different fix though. Because we're going to change how
the root memory cgroup is handled (an rfc version posted here:
https://lkml.org/lkml/2020/10/21/612), it's better to not use a comparison
with the root_mem_cgroup and handle the obj_cgroup_tryget() return code instead.
Something like this:

@@ -2973,6 +2973,7 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
 		objcg = rcu_dereference(memcg->objcg);
 		if (objcg && obj_cgroup_tryget(objcg))
 			break;
+		objcg = NULL;
 	}
 	rcu_read_unlock();

Or a more explicit:
   if (obj_cgroup_tryget(objcg)) {
     ...
   } else {
     ...
   }

Also, please, add:
Cc: stable@vger.kernel.org

Thanks!

WARNING: multiple messages have this Message-ID (diff)
From: Roman Gushchin <guro@fb.com>
To: Muchun Song <songmuchun@bytedance.com>
Cc: hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com,
	akpm@linux-foundation.org, shakeelb@google.com,
	iamjoonsoo.kim@lge.com, laoar.shao@gmail.com,
	chris@chrisdown.name, christian.brauner@ubuntu.com,
	peterz@infradead.org, mingo@kernel.org, keescook@chromium.org,
	tglx@linutronix.de, esyr@redhat.com, surenb@google.com,
	areber@redhat.com, elver@google.com,
	linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
	linux-mm@kvack.org
Subject: Re: [PATCH 1/5] mm: memcg/slab: Fix return child memcg objcg for root memcg
Date: Tue, 27 Oct 2020 10:42:36 -0700	[thread overview]
Message-ID: <20201027174236.GB725724@carbon.dhcp.thefacebook.com> (raw)
In-Reply-To: <20201027080256.76497-2-songmuchun@bytedance.com>

On Tue, Oct 27, 2020 at 04:02:52PM +0800, Muchun Song wrote:
> Consider the following memcg hierarchy.
> 
>                     root
>                    /    \
>                   A      B
> 
> If we get the objcg of memcg A failed, the get_obj_cgroup_from_current
> can return the wrong objcg for the root memcg.
> 
> Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API")
> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> ---
>  mm/memcontrol.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)

Hi Muchun,

thank you for the patchset!

A generic note: it's usually not a good idea to group fixes with non-fixes
in one patchset if fixes are planned to be backported to stable.

> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 1337775b04f3..fcbd79c5023e 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2945,7 +2945,7 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p)
>  
>  __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
>  {
> -	struct obj_cgroup *objcg = NULL;
> +	struct obj_cgroup *objcg;
>  	struct mem_cgroup *memcg;
>  
>  	if (memcg_kmem_bypass())
> @@ -2964,6 +2964,9 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
>  	}
>  	rcu_read_unlock();
>  
> +	if (memcg == root_mem_cgroup)
> +		return NULL;
> +
>  	return objcg;
>  }

I agree, it's a bug. Good catch, thanks!

I would prefer a slightly different fix though. Because we're going to change how
the root memory cgroup is handled (an rfc version posted here:
https://lkml.org/lkml/2020/10/21/612), it's better to not use a comparison
with the root_mem_cgroup and handle the obj_cgroup_tryget() return code instead.
Something like this:

@@ -2973,6 +2973,7 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
 		objcg = rcu_dereference(memcg->objcg);
 		if (objcg && obj_cgroup_tryget(objcg))
 			break;
+		objcg = NULL;
 	}
 	rcu_read_unlock();

Or a more explicit:
   if (obj_cgroup_tryget(objcg)) {
     ...
   } else {
     ...
   }

Also, please, add:
Cc: stable@vger.kernel.org

Thanks!

  reply	other threads:[~2020-10-27 17:43 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-27  8:02 [PATCH 0/5] Fix some bugs in memcg/slab Muchun Song
2020-10-27  8:02 ` Muchun Song
2020-10-27  8:02 ` [PATCH 1/5] mm: memcg/slab: Fix return child memcg objcg for root memcg Muchun Song
2020-10-27 17:42   ` Roman Gushchin [this message]
2020-10-27 17:42     ` Roman Gushchin
2020-10-27 19:05     ` Roman Gushchin
2020-10-27 19:05       ` Roman Gushchin
2020-10-27  8:02 ` [PATCH 2/5] mm: memcg/slab: Fix use after free in obj_cgroup_charge Muchun Song
2020-10-27 18:31   ` Roman Gushchin
2020-10-27 18:31     ` Roman Gushchin
2020-10-27  8:02 ` [PATCH 3/5] mm: memcg/slab: Rename *_lruvec_slab_state to *_lruvec_kmem_state Muchun Song
2020-10-27  8:02   ` Muchun Song
2020-10-27 18:37   ` Roman Gushchin
2020-10-27 18:37     ` Roman Gushchin
2020-10-27  8:02 ` [PATCH 4/5] mm: memcg/slab: Fix root memcg vmstats Muchun Song
2020-10-27  8:02   ` Muchun Song
2020-10-27 18:48   ` Roman Gushchin
2020-10-27 18:48     ` Roman Gushchin
2020-10-28  2:56     ` [External] " Muchun Song
2020-10-28  2:56       ` Muchun Song
2020-10-28  2:56       ` Muchun Song
2020-10-28  3:47       ` Muchun Song
2020-10-28  3:47         ` Muchun Song
2020-10-29  0:14       ` Roman Gushchin
2020-10-29  0:14         ` Roman Gushchin
2020-10-29  6:15         ` Muchun Song
2020-10-29  6:15           ` Muchun Song
2020-11-10  1:32           ` Roman Gushchin
2020-11-10  1:32             ` Roman Gushchin
2020-11-10  2:57             ` Muchun Song
2020-11-10  2:57               ` Muchun Song
2020-11-10  2:57               ` Muchun Song
2020-10-27  8:02 ` [PATCH 5/5] mm: memcontrol: Simplify the mem_cgroup_page_lruvec Muchun Song
2020-10-27  8:02   ` Muchun Song
2020-10-27 13:36   ` Michal Hocko
2020-10-27 13:36     ` Michal Hocko
2020-10-27 14:15     ` [External] " Muchun Song
2020-10-27 14:15       ` Muchun Song
2020-10-27 14:15       ` Muchun Song
2020-10-27 14:31       ` Michal Hocko
2020-10-27 14:31         ` 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=20201027174236.GB725724@carbon.dhcp.thefacebook.com \
    --to=guro@fb.com \
    --cc=akpm@linux-foundation.org \
    --cc=areber@redhat.com \
    --cc=cgroups@vger.kernel.org \
    --cc=chris@chrisdown.name \
    --cc=christian.brauner@ubuntu.com \
    --cc=elver@google.com \
    --cc=esyr@redhat.com \
    --cc=hannes@cmpxchg.org \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=keescook@chromium.org \
    --cc=laoar.shao@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=shakeelb@google.com \
    --cc=songmuchun@bytedance.com \
    --cc=surenb@google.com \
    --cc=tglx@linutronix.de \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.