public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cgroup: Fix uninitialized variable warning
@ 2015-12-23 21:30 Ross Zwisler
  2015-12-23 21:35 ` Tejun Heo
  0 siblings, 1 reply; 6+ messages in thread
From: Ross Zwisler @ 2015-12-23 21:30 UTC (permalink / raw)
  To: Linus Torvalds, Tejun Heo
  Cc: Ross Zwisler, Li Zefan, Daniel Wagner, Johannes Weiner, cgroups,
	linux-kernel, Dave Jones, kernel-team, Aleksa Sarai, Michal Hocko,
	Ingo Molnar, Peter Zijlstra, Neil Horman

Commit 1f7dd3e5a6e4 ("cgroup: fix handling of multi-destination migration
from subtree_control enabling") introduced the following compiler warning:

mm/memcontrol.c: In function ‘mem_cgroup_can_attach’:
mm/memcontrol.c:4790:9: warning: ‘memcg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   mc.to = memcg;
         ^
Fix this by initializing 'memcg' to NULL and then verifying that it is set
to a value before dereferencing it.

Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
---

This issue is present in v4.4-rc5 and later.

---
 mm/memcontrol.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index e234c21..69a28b3 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4782,7 +4782,7 @@ static void mem_cgroup_clear_mc(void)
 static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
 {
 	struct cgroup_subsys_state *css;
-	struct mem_cgroup *memcg;
+	struct mem_cgroup *memcg = NULL;
 	struct mem_cgroup *from;
 	struct task_struct *leader, *p;
 	struct mm_struct *mm;
@@ -4805,7 +4805,7 @@ static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
 		p = leader;
 		memcg = mem_cgroup_from_css(css);
 	}
-	if (!p)
+	if (!p || !memcg)
 		return 0;
 
 	/*
-- 
2.6.3


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

* Re: [PATCH] cgroup: Fix uninitialized variable warning
  2015-12-23 21:30 [PATCH] cgroup: Fix uninitialized variable warning Ross Zwisler
@ 2015-12-23 21:35 ` Tejun Heo
  2015-12-23 21:38   ` Ross Zwisler
  0 siblings, 1 reply; 6+ messages in thread
From: Tejun Heo @ 2015-12-23 21:35 UTC (permalink / raw)
  To: Ross Zwisler
  Cc: Linus Torvalds, Li Zefan, Daniel Wagner, Johannes Weiner, cgroups,
	linux-kernel, Dave Jones, kernel-team, Aleksa Sarai, Michal Hocko,
	Ingo Molnar, Peter Zijlstra, Neil Horman

Hello, Ross.

On Wed, Dec 23, 2015 at 02:30:40PM -0700, Ross Zwisler wrote:
>  static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
>  {
>  	struct cgroup_subsys_state *css;
> -	struct mem_cgroup *memcg;
> +	struct mem_cgroup *memcg = NULL;

It's one thing to add spurious init to shut up gcc

> @@ -4805,7 +4805,7 @@ static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
>  		p = leader;
>  		memcg = mem_cgroup_from_css(css);
>  	}
> -	if (!p)
> +	if (!p || !memcg)

and to another to add an additional processing on it.

Please note which gcc version you were using in the commit description
and mark the init with a comment.

Thanks.

-- 
tejun

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

* Re: [PATCH] cgroup: Fix uninitialized variable warning
  2015-12-23 21:35 ` Tejun Heo
@ 2015-12-23 21:38   ` Ross Zwisler
  2015-12-23 21:41     ` Tejun Heo
  0 siblings, 1 reply; 6+ messages in thread
From: Ross Zwisler @ 2015-12-23 21:38 UTC (permalink / raw)
  To: Tejun Heo
  Cc: Ross Zwisler, Linus Torvalds, Li Zefan, Daniel Wagner,
	Johannes Weiner, cgroups, linux-kernel, Dave Jones, kernel-team,
	Aleksa Sarai, Michal Hocko, Ingo Molnar, Peter Zijlstra,
	Neil Horman

On Wed, Dec 23, 2015 at 04:35:19PM -0500, Tejun Heo wrote:
> Hello, Ross.
> 
> On Wed, Dec 23, 2015 at 02:30:40PM -0700, Ross Zwisler wrote:
> >  static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
> >  {
> >  	struct cgroup_subsys_state *css;
> > -	struct mem_cgroup *memcg;
> > +	struct mem_cgroup *memcg = NULL;
> 
> It's one thing to add spurious init to shut up gcc
> 
> > @@ -4805,7 +4805,7 @@ static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
> >  		p = leader;
> >  		memcg = mem_cgroup_from_css(css);
> >  	}
> > -	if (!p)
> > +	if (!p || !memcg)
> 
> and to another to add an additional processing on it.

Do you believe that the additional processing is incorrect?  If somehow we
*do* get through the above loop without setting memcg, the next deref will
OOPs the kernel...

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

* Re: [PATCH] cgroup: Fix uninitialized variable warning
  2015-12-23 21:38   ` Ross Zwisler
@ 2015-12-23 21:41     ` Tejun Heo
  0 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2015-12-23 21:41 UTC (permalink / raw)
  To: Ross Zwisler
  Cc: Linus Torvalds, Li Zefan, Daniel Wagner, Johannes Weiner, cgroups,
	linux-kernel, Dave Jones, kernel-team, Aleksa Sarai, Michal Hocko,
	Ingo Molnar, Peter Zijlstra, Neil Horman

On Wed, Dec 23, 2015 at 02:38:13PM -0700, Ross Zwisler wrote:
> On Wed, Dec 23, 2015 at 04:35:19PM -0500, Tejun Heo wrote:
> > Hello, Ross.
> > 
> > On Wed, Dec 23, 2015 at 02:30:40PM -0700, Ross Zwisler wrote:
> > >  static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
> > >  {
> > >  	struct cgroup_subsys_state *css;
> > > -	struct mem_cgroup *memcg;
> > > +	struct mem_cgroup *memcg = NULL;
> > 
> > It's one thing to add spurious init to shut up gcc
> > 
> > > @@ -4805,7 +4805,7 @@ static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
> > >  		p = leader;
> > >  		memcg = mem_cgroup_from_css(css);
> > >  	}
> > > -	if (!p)
> > > +	if (!p || !memcg)
> > 
> > and to another to add an additional processing on it.
> 
> Do you believe that the additional processing is incorrect?  If somehow we
> *do* get through the above loop without setting memcg, the next deref will
> OOPs the kernel...

That'd be a a plain kernel bug and oopsing is fine.  If such
conditions are particular (more likely, more difficult to debut,
whatever), we sometimes add WARNs for them but we don't generally go
around and add spurious checks.  It actually is deterimental to
readibility as people reading the code constantly have to go "when can
p && !memcg can happen? why is this explicitly checked?".

Thanks.

-- 
tejun

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

* [PATCH] cgroup: Fix uninitialized variable warning
@ 2015-12-23 21:53 Ross Zwisler
  2015-12-28 15:45 ` Tejun Heo
  0 siblings, 1 reply; 6+ messages in thread
From: Ross Zwisler @ 2015-12-23 21:53 UTC (permalink / raw)
  To: Linus Torvalds, Tejun Heo
  Cc: Ross Zwisler, Li Zefan, Daniel Wagner, Johannes Weiner, cgroups,
	linux-kernel, Dave Jones, kernel-team, Aleksa Sarai, Michal Hocko,
	Ingo Molnar, Peter Zijlstra, Neil Horman

Commit 1f7dd3e5a6e4 ("cgroup: fix handling of multi-destination migration
from subtree_control enabling") introduced the following compiler warning:

mm/memcontrol.c: In function ‘mem_cgroup_can_attach’:
mm/memcontrol.c:4790:9: warning: ‘memcg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   mc.to = memcg;
         ^

Fix this by initializing 'memcg' to NULL.

This was found using gcc (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6).

Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
---
 mm/memcontrol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 48b22c3..ffd9750 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4735,7 +4735,7 @@ static void mem_cgroup_clear_mc(void)
 static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
 {
 	struct cgroup_subsys_state *css;
-	struct mem_cgroup *memcg;
+	struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */
 	struct mem_cgroup *from;
 	struct task_struct *leader, *p;
 	struct mm_struct *mm;
-- 
2.6.3


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

* Re: [PATCH] cgroup: Fix uninitialized variable warning
  2015-12-23 21:53 Ross Zwisler
@ 2015-12-28 15:45 ` Tejun Heo
  0 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2015-12-28 15:45 UTC (permalink / raw)
  To: Ross Zwisler
  Cc: Linus Torvalds, Li Zefan, Daniel Wagner, Johannes Weiner, cgroups,
	linux-kernel, Dave Jones, kernel-team, Aleksa Sarai, Michal Hocko,
	Ingo Molnar, Peter Zijlstra, Neil Horman

On Wed, Dec 23, 2015 at 02:53:27PM -0700, Ross Zwisler wrote:
> Commit 1f7dd3e5a6e4 ("cgroup: fix handling of multi-destination migration
> from subtree_control enabling") introduced the following compiler warning:
> 
> mm/memcontrol.c: In function ‘mem_cgroup_can_attach’:
> mm/memcontrol.c:4790:9: warning: ‘memcg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
>    mc.to = memcg;
>          ^
> 
> Fix this by initializing 'memcg' to NULL.
> 
> This was found using gcc (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6).
> 
> Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>

Applied to cgroup/for-4.5.

Thanks.

-- 
tejun

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

end of thread, other threads:[~2015-12-28 15:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-23 21:30 [PATCH] cgroup: Fix uninitialized variable warning Ross Zwisler
2015-12-23 21:35 ` Tejun Heo
2015-12-23 21:38   ` Ross Zwisler
2015-12-23 21:41     ` Tejun Heo
  -- strict thread matches above, loose matches on Subject: below --
2015-12-23 21:53 Ross Zwisler
2015-12-28 15:45 ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox