linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Jesper Juhl <jj@chaosbits.net>
To: Minchan Kim <minchan.kim@gmail.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [updated PATCH] Close mem leak in error path in mm/hugetlb.c::nr_hugepages_store_common()
Date: Mon, 20 Dec 2010 23:05:25 +0100 (CET)	[thread overview]
Message-ID: <alpine.LNX.2.00.1012202302460.23785@swampdragon.chaosbits.net> (raw)
In-Reply-To: <AANLkTikNx5SG9Z=tUu6tyFRqnR2sLe5NxAjLCJr1UKmq@mail.gmail.com>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 2116 bytes --]

On Mon, 20 Dec 2010, Minchan Kim wrote:

> On Mon, Dec 20, 2010 at 7:10 AM, Jesper Juhl <jj@chaosbits.net> wrote:
> > Hi,
> >
> > The NODEMASK_ALLOC macro dynamically allocates memory for its second
> > argument ('nodes_allowed' in this context).
> > In nr_hugepages_store_common() we may abort early if strict_strtoul()
> > fails, but in that case we do not free the memory already allocated to
> > 'nodes_allowed', causing a memory leak.
> > This patch closes the leak by freeing the memory in the error path.
> >
> >
> > Signed-off-by: Jesper Juhl <jj@chaosbits.net>
> > ---
> >  hugetlb.c |    4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> >  compile tested only
> >
> > diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> > index 8585524..9fdcc35 100644
> > --- a/mm/hugetlb.c
> > +++ b/mm/hugetlb.c
> > @@ -1439,8 +1439,10 @@ static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
> >        NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
> >
> >        err = strict_strtoul(buf, 10, &count);
> > -       if (err)
> > +       if (err) {
> > +               kfree(nodes_allowed);
> 
> Nice catch. But use NODEMASK_FREE. It might be not kmalloced object.
> 
Right. I just checked the macro and it used kmalloc(), so I just wrote 
kfree. But you are right, NODEMASK_FREE is the right thing to use here. 
Updated patch below.


Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 hugetlb.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 8585524..71e7886 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1439,8 +1439,10 @@ static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
 	NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
 
 	err = strict_strtoul(buf, 10, &count);
-	if (err)
+	if (err) {
+		NODEMASK_FREE(nodes_allowed);
 		return 0;
+	}
 
 	h = kobj_to_hstate(kobj, &nid);
 	if (nid == NUMA_NO_NODE) {



-- 
Jesper Juhl <jj@chaosbits.net>            http://www.chaosbits.net/
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please.

  reply	other threads:[~2010-12-20 22:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-19 22:10 [PATCH] " Jesper Juhl
2010-12-20  1:00 ` Minchan Kim
2010-12-20 22:05   ` Jesper Juhl [this message]
2010-12-20 23:38     ` [updated PATCH] " Minchan Kim
2010-12-20 23:34       ` Jesper Juhl

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=alpine.LNX.2.00.1012202302460.23785@swampdragon.chaosbits.net \
    --to=jj@chaosbits.net \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=minchan.kim@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