Skip to contentSkip to conversation

mm/damon/dbgfs: fix memory leak when using

PATCH Applied SeongJae Park 2 participants 2 messages Sep 2, 2022 – Sep 3, 2022

Subsystems (3)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

PATCH v2 1/1 mm/damon/dbgfs: fix memory leak when using

From: Greg Kroah-Hartman <[email protected]>

debugfs_lookup()
Date: Fri, 2 Sep 2022 14:56:31 +0200 [thread overview]
Message-ID: <[email protected]> (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 <[email protected]> # 5.15.x
Cc SeongJae Park <[email protected]>
Cc Andrew Morton <[email protected]>

---
Changes from v1
(https://lore.kernel.org/damon/[email protected]/)
- Call dput() for failure-return case (Andrew Morton)

 mm/damon/dbgfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
mm/damon/dbgfs.c +14 -5
index 3b55a1b219b5..652a94deafe3 100644
@@ -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,
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 19 lines · SeongJae Park

From: Greg Kroah-Hartman <[email protected]>

debugfs_lookup()
Date: Fri, 2 Sep 2022 14:56:31 +0200 [thread overview]
Message-ID: <[email protected]> (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 <[email protected]> # 5.15.x
Cc SeongJae Park <[email protected]>
Cc Andrew Morton <[email protected]>
Quoted 4 lines

---
Changes from v1
(https://lore.kernel.org/damon/[email protected]/)
- 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