linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosryahmed@google.com>
To: Axel Rasmussen <axelrasmussen@google.com>
Cc: Geunsik Lim <geunsik.lim@gmail.com>,
	Vlastimil Babka <vbabka@suse.cz>, Ingo Molnar <mingo@elte.hu>,
	 Andrew Morton <akpm@linux-foundation.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	 Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Hugh Dickins <hughd@google.com>,
	 "H. Peter Anvin" <hpa@zytor.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	 linux-kernel <linux-kernel@vger.kernel.org>,
	linux-mm <linux-mm@kvack.org>
Subject: Re: [PATCH] fix: Prevent memory leak by checking for NULL buffer before calling css_put()
Date: Mon, 3 Jun 2024 10:42:07 -0700	[thread overview]
Message-ID: <CAJD7tkaV8a-fVv4D_6_5LgmOHnMFXJWy-DwqvRDCTjEqyJkdKA@mail.gmail.com> (raw)
In-Reply-To: <CAJHvVchjzxLVfg844SNjK9EWmC+yhVneGaf1vVscmjomH_aaow@mail.gmail.com>

On Mon, Jun 3, 2024 at 10:32 AM Axel Rasmussen <axelrasmussen@google.com> wrote:
>
> On Mon, Jun 3, 2024 at 5:33 AM Geunsik Lim <geunsik.lim@gmail.com> wrote:
> >
> > This commit addresses a potential memory leak in the
> > `get_mm_memcg_path()` function
> > by explicitly checking if the allocated buffer (`buf`) is NULL before
> > calling the
> > `css_put()` function. The prefix 'css' means abbreviation of cgroup_subsys_state
> >
> > Previously, the code would directly call `css_put()` without checking
> > the value of
> > `buf`, which could lead to a memory leak if the buffer allocation failed.
> > This commit introduces a conditional check to ensure that `css_put()`
> > is only called
> > if `buf` is not NULL.
> >
> > This change enhances the code's robustness and prevents memory leaks, improving
> > overall system stability.
> >
> > **Specific Changes:**
> >
> > * In the `out_put` label, an `if` statement is added to check
> >   if `buf` is not NULL before calling `css_put()`.
> >
> > **Benefits:**
> >
> > * Prevents potential memory leaks
> > * Enhances code robustness
> > * Improves system stability
> >
> > Signed-off-by: Geunsik Lim <leemgs@gmail.com>
> > Signed-off-by: Geunsik Lim <geunsik.lim@samsung.com>
> > ---
> >  mm/mmap_lock.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c
> > index 1854850b4b89..7314045b0e3b 100644
> > --- a/mm/mmap_lock.c
> > +++ b/mm/mmap_lock.c
> > @@ -213,7 +213,8 @@ static const char *get_mm_memcg_path(struct mm_struct *mm)
> >         cgroup_path(memcg->css.cgroup, buf, MEMCG_PATH_BUF_SIZE);
> >
> >  out_put:
> > -       css_put(&memcg->css);
> > +        if (buf != NULL)
> > +                css_put(&memcg->css);
> >  out:
> >         return buf;
> >  }
>
> I think the existing code is correct, and this change actually
> introduces a memory leak where there was none before.
>
> In the case where get_memcg_path_buf() returns NULL, we *still* need
> to css_put() what we got from get_mem_cgroup_from_mm() before.
>
> NAK, unless I'm missing something.

+1

We already skip css_put() if get_mem_cgroup_from_mm() returns NULL.
Whether or not get_memcg_path_buf() succeeds is irrelevant here.


      reply	other threads:[~2024-06-03 17:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-03 12:33 Geunsik Lim
2024-06-03 17:31 ` Axel Rasmussen
2024-06-03 17:42   ` Yosry Ahmed [this message]

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=CAJD7tkaV8a-fVv4D_6_5LgmOHnMFXJWy-DwqvRDCTjEqyJkdKA@mail.gmail.com \
    --to=yosryahmed@google.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@linux-foundation.org \
    --cc=axelrasmussen@google.com \
    --cc=geunsik.lim@gmail.com \
    --cc=hpa@zytor.com \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mingo@elte.hu \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=vbabka@suse.cz \
    /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