linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] mm/damon/dbgfs: fix memory leak when using
@ 2022-09-02 19:11 SeongJae Park
  2022-09-03  5:36 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: SeongJae Park @ 2022-09-02 19:11 UTC (permalink / raw)
  To: akpm; +Cc: sj, gregkh, damon, linux-mm, linux-kernel, stable

From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

debugfs_lookup()
Date: Fri,  2 Sep 2022 14:56:31 +0200	[thread overview]
Message-ID: <20220902125631.128329-1-gregkh@linuxfoundation.org> (raw)

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 ("mm/damon/dbgfs: support multiple contexts")
Cc: <stable@vger.kernel.org> # 5.15.x
Cc: SeongJae Park <sj@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: damon@lists.linux.dev
Cc: linux-mm@kvack.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: SeongJae Park <sj@kernel.org>
---
Changes from v1
(https://lore.kernel.org/damon/20220902125631.128329-1-gregkh@linuxfoundation.org/)
- Call dput() for failure-return case (Andrew Morton)

 mm/damon/dbgfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c
index 3b55a1b219b5..652a94deafe3 100644
--- a/mm/damon/dbgfs.c
+++ b/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;
@@ -898,14 +899,16 @@ 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;
+	if (!new_dirs) {
+		ret = -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;
+		ret = -ENOMEM;
+		goto out_new_dirs;
 	}
 
 	for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
@@ -925,7 +928,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,
-- 
2.25.1



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

* Re: [PATCH v2] mm/damon/dbgfs: fix memory leak when using
  2022-09-02 19:11 [PATCH v2] mm/damon/dbgfs: fix memory leak when using SeongJae Park
@ 2022-09-03  5:36 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2022-09-03  5:36 UTC (permalink / raw)
  To: SeongJae Park; +Cc: akpm, damon, linux-mm, linux-kernel, stable

On Fri, Sep 02, 2022 at 07:11:49PM +0000, SeongJae Park wrote:
> From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> 
> debugfs_lookup()
> Date: Fri,  2 Sep 2022 14:56:31 +0200	[thread overview]
> Message-ID: <20220902125631.128329-1-gregkh@linuxfoundation.org> (raw)
> 
> 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 ("mm/damon/dbgfs: support multiple contexts")
> Cc: <stable@vger.kernel.org> # 5.15.x
> Cc: SeongJae Park <sj@kernel.org>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: damon@lists.linux.dev
> Cc: linux-mm@kvack.org
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: SeongJae Park <sj@kernel.org>
> ---
> Changes from v1
> (https://lore.kernel.org/damon/20220902125631.128329-1-gregkh@linuxfoundation.org/)
> - Call dput() for failure-return case (Andrew Morton)

Thanks for fixing this up, I missed the other return error cases in my
rush to audit the whole tree at once.

This version looks great, and I see Andrew has taken it now into his
tree, thanks!

greg k-h


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

end of thread, other threads:[~2022-09-03  5:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-02 19:11 [PATCH v2] mm/damon/dbgfs: fix memory leak when using SeongJae Park
2022-09-03  5:36 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).