* [PATCH] fix: Prevent memory leak by checking for NULL buffer before calling css_put()
@ 2024-06-03 12:33 Geunsik Lim
2024-06-03 17:31 ` Axel Rasmussen
0 siblings, 1 reply; 3+ messages in thread
From: Geunsik Lim @ 2024-06-03 12:33 UTC (permalink / raw)
To: Axel Rasmussen, Vlastimil Babka, Ingo Molnar, Andrew Morton,
Thomas Gleixner, Peter Zijlstra, Hugh Dickins
Cc: H. Peter Anvin, Steven Rostedt, linux-kernel, linux-mm
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;
}
--
2.34.1
----
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] fix: Prevent memory leak by checking for NULL buffer before calling css_put()
2024-06-03 12:33 [PATCH] fix: Prevent memory leak by checking for NULL buffer before calling css_put() Geunsik Lim
@ 2024-06-03 17:31 ` Axel Rasmussen
2024-06-03 17:42 ` Yosry Ahmed
0 siblings, 1 reply; 3+ messages in thread
From: Axel Rasmussen @ 2024-06-03 17:31 UTC (permalink / raw)
To: Geunsik Lim
Cc: Vlastimil Babka, Ingo Molnar, Andrew Morton, Thomas Gleixner,
Peter Zijlstra, Hugh Dickins, H. Peter Anvin, Steven Rostedt,
linux-kernel, linux-mm
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.
> --
> 2.34.1
> ----
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] fix: Prevent memory leak by checking for NULL buffer before calling css_put()
2024-06-03 17:31 ` Axel Rasmussen
@ 2024-06-03 17:42 ` Yosry Ahmed
0 siblings, 0 replies; 3+ messages in thread
From: Yosry Ahmed @ 2024-06-03 17:42 UTC (permalink / raw)
To: Axel Rasmussen
Cc: Geunsik Lim, Vlastimil Babka, Ingo Molnar, Andrew Morton,
Thomas Gleixner, Peter Zijlstra, Hugh Dickins, H. Peter Anvin,
Steven Rostedt, linux-kernel, linux-mm
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-06-03 17:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-03 12:33 [PATCH] fix: Prevent memory leak by checking for NULL buffer before calling css_put() Geunsik Lim
2024-06-03 17:31 ` Axel Rasmussen
2024-06-03 17:42 ` Yosry Ahmed
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox