* [PATCH] mm/damon/dbgfs: fix memory leak when using debugfs_lookup()
@ 2022-09-02 12:56 Greg Kroah-Hartman
2022-09-02 16:17 ` Andrew Morton
0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2022-09-02 12:56 UTC (permalink / raw)
To: SeongJae Park
Cc: linux-kernel, Greg Kroah-Hartman, Andrew Morton, damon, linux-mm, stable
When calling debugfs_lookup() the result must have dput() called on it,
otherwise the memory will leak over time. Fix this up by properly
calling dput().
Cc: SeongJae Park <sj@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: damon@lists.linux.dev
Cc: linux-mm@kvack.org
Cc: stable <stable@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
mm/damon/dbgfs.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c
index cb8a7e9926a4..dfb6ef8c83e0 100644
--- a/mm/damon/dbgfs.c
+++ b/mm/damon/dbgfs.c
@@ -915,6 +915,7 @@ static int dbgfs_rm_context(char *name)
new_ctxs[j++] = dbgfs_ctxs[i];
}
+ dput(dir);
kfree(dbgfs_dirs);
kfree(dbgfs_ctxs);
--
2.37.3
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/damon/dbgfs: fix memory leak when using debugfs_lookup()
2022-09-02 12:56 [PATCH] mm/damon/dbgfs: fix memory leak when using debugfs_lookup() Greg Kroah-Hartman
@ 2022-09-02 16:17 ` Andrew Morton
2022-09-02 18:12 ` SeongJae Park
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2022-09-02 16:17 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: SeongJae Park, linux-kernel, damon, linux-mm, stable
On Fri, 2 Sep 2022 14:56:31 +0200 Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
> When calling debugfs_lookup() the result must have dput() called on it,
> otherwise the memory will leak over time. Fix this up by properly
> calling dput().
>
> ...
>
Fixes: 75c1c2b53c78b, I assume.
> --- a/mm/damon/dbgfs.c
> +++ b/mm/damon/dbgfs.c
> @@ -915,6 +915,7 @@ static int dbgfs_rm_context(char *name)
> new_ctxs[j++] = dbgfs_ctxs[i];
> }
>
> + dput(dir);
> kfree(dbgfs_dirs);
> kfree(dbgfs_ctxs);
>
dput() is also needed if either of the kmalloc_array() calls fail?
Maybe something like
--- a/mm/damon/dbgfs.c~a
+++ a/mm/damon/dbgfs.c
@@ -884,6 +884,7 @@ static int dbgfs_rm_context(char *name)
struct dentry *root, *dir, **new_dirs;
struct damon_ctx **new_ctxs;
int i, j;
+ int ret = 0;
if (damon_nr_running_ctxs())
return -EBUSY;
@@ -899,14 +900,12 @@ static int dbgfs_rm_context(char *name)
new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
GFP_KERNEL);
if (!new_dirs)
- return -ENOMEM;
+ goto out_dput;
new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
GFP_KERNEL);
- if (!new_ctxs) {
- kfree(new_dirs);
- return -ENOMEM;
- }
+ if (!new_ctxs)
+ goto out_new_dirs;
for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
if (dbgfs_dirs[i] == dir) {
@@ -925,7 +924,13 @@ static int dbgfs_rm_context(char *name)
dbgfs_ctxs = new_ctxs;
dbgfs_nr_ctxs--;
- return 0;
+ goto out_dput;
+
+out_new_dirs:
+ kfree(new_dirs);
+out_dput:
+ dput(dir);
+ return ret;
}
static ssize_t dbgfs_rm_context_write(struct file *file,
_
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/damon/dbgfs: fix memory leak when using debugfs_lookup()
2022-09-02 16:17 ` Andrew Morton
@ 2022-09-02 18:12 ` SeongJae Park
0 siblings, 0 replies; 3+ messages in thread
From: SeongJae Park @ 2022-09-02 18:12 UTC (permalink / raw)
To: Andrew Morton
Cc: Greg Kroah-Hartman, SeongJae Park, linux-kernel, damon, linux-mm,
stable, stable
Hi Andrew and Greg!
On Fri, 2 Sep 2022 09:17:03 -0700 Andrew Morton <akpm@linux-foundation.org> wrote:
> On Fri, 2 Sep 2022 14:56:31 +0200 Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
>
> > When calling debugfs_lookup() the result must have dput() called on it,
> > otherwise the memory will leak over time. Fix this up by properly
> > calling dput().
Thank you for finding this bug and posting this patch, Greg!
> >
> > ...
> >
>
> Fixes: 75c1c2b53c78b, I assume.
Correct, Andrew.
Fixes: 75c1c2b53c78b ("mm/damon/dbgfs: support multiple contexts")
Cc: <stable@vger.kernel.org> # 5.15.x
>
> > --- a/mm/damon/dbgfs.c
> > +++ b/mm/damon/dbgfs.c
> > @@ -915,6 +915,7 @@ static int dbgfs_rm_context(char *name)
> > new_ctxs[j++] = dbgfs_ctxs[i];
> > }
> >
> > + dput(dir);
> > kfree(dbgfs_dirs);
> > kfree(dbgfs_ctxs);
> >
>
> dput() is also needed if either of the kmalloc_array() calls fail?
> Maybe something like
Good catch.
>
> --- a/mm/damon/dbgfs.c~a
> +++ a/mm/damon/dbgfs.c
> @@ -884,6 +884,7 @@ static int dbgfs_rm_context(char *name)
> struct dentry *root, *dir, **new_dirs;
> struct damon_ctx **new_ctxs;
> int i, j;
> + int ret = 0;
>
> if (damon_nr_running_ctxs())
> return -EBUSY;
> @@ -899,14 +900,12 @@ static int dbgfs_rm_context(char *name)
> new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
> GFP_KERNEL);
> if (!new_dirs)
> - return -ENOMEM;
> + goto out_dput;
Shouldn't we also do 'ret = -ENOMEM;' before 'godo out_dput'?
>
> new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
> GFP_KERNEL);
> - if (!new_ctxs) {
> - kfree(new_dirs);
> - return -ENOMEM;
> - }
> + if (!new_ctxs)
> + goto out_new_dirs;
ditto.
>
> for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
> if (dbgfs_dirs[i] == dir) {
> @@ -925,7 +924,13 @@ static int dbgfs_rm_context(char *name)
> dbgfs_ctxs = new_ctxs;
> dbgfs_nr_ctxs--;
>
> - return 0;
> + goto out_dput;
> +
> +out_new_dirs:
> + kfree(new_dirs);
> +out_dput:
> + dput(dir);
> + return ret;
> }
>
> static ssize_t dbgfs_rm_context_write(struct file *file,
> _
Thanks,
SJ
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-09-02 18:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-02 12:56 [PATCH] mm/damon/dbgfs: fix memory leak when using debugfs_lookup() Greg Kroah-Hartman
2022-09-02 16:17 ` Andrew Morton
2022-09-02 18:12 ` SeongJae Park
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox