[PATCH 4/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init()

Rakie Kim posted 4 patches 11 months, 1 week ago
[PATCH 4/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init()
Posted by Rakie Kim 11 months, 1 week ago
Improper cleanup of sysfs attributes caused kobject and memory leaks when
initialization failed or nodes were removed.

This patch ensures proper deallocation of kobjects and memory, preventing
resource leaks and improving stability.

Signed-off-by: Rakie Kim <rakie.kim@sk.com>
---
 mm/mempolicy.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 2d19434c61ed..441a0635e81d 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -3604,47 +3604,47 @@ static int __init mempolicy_sysfs_init(void)
 	int err;
 	static struct kobject *mempolicy_kobj;
 
-	mempolicy_kobj = kzalloc(sizeof(*mempolicy_kobj), GFP_KERNEL);
-	if (!mempolicy_kobj) {
-		err = -ENOMEM;
-		goto err_out;
-	}
-
 	ngrp = kzalloc(sizeof(*ngrp), GFP_KERNEL);
 	if (!ngrp) {
 		err = -ENOMEM;
-		goto mempol_out;
+		goto err_out;
 	}
 	mutex_init(&ngrp->kobj_lock);
 
 	ngrp->nattrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
-			     GFP_KERNEL);
+			       GFP_KERNEL);
 	if (!ngrp->nattrs) {
 		err = -ENOMEM;
 		goto ngrp_out;
 	}
 
+	mempolicy_kobj = kzalloc(sizeof(*mempolicy_kobj), GFP_KERNEL);
+	if (!mempolicy_kobj) {
+		err = -ENOMEM;
+		goto nattr_out;
+	}
+
 	err = kobject_init_and_add(mempolicy_kobj, &mempolicy_ktype, mm_kobj,
 				   "mempolicy");
-	if (err)
-		goto node_out;
+	if (err) {
+		kobject_put(mempolicy_kobj);
+		goto err_out;
+	}
 
 	err = add_weighted_interleave_group(mempolicy_kobj);
 	if (err) {
-		pr_err("mempolicy sysfs structure failed to initialize\n");
 		kobject_put(mempolicy_kobj);
-		return err;
+		goto err_out;
 	}
 
-	return err;
-node_out:
+	return 0;
+
+nattr_out:
 	kfree(ngrp->nattrs);
 ngrp_out:
 	kfree(ngrp);
-mempol_out:
-	kfree(mempolicy_kobj);
 err_out:
-	pr_err("failed to add mempolicy kobject to the system\n");
+	pr_err("mempolicy sysfs structure failed to initialize\n");
 	return err;
 }
 
-- 
2.34.1
Re: [PATCH 4/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init()
Posted by Gregory Price 11 months, 1 week ago
On Fri, Mar 07, 2025 at 03:35:33PM +0900, Rakie Kim wrote:
> Improper cleanup of sysfs attributes caused kobject and memory leaks when
> initialization failed or nodes were removed.
>

Is this fixing something in your patch set or fixing something in the
current upstream code?  If in the current patch set, roll this into the
patch that causes it.

If this is fixing something upstream, I recommend submitting this
separately to stable and rebasing on top of it.

~Gregory
Re: [PATCH 4/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init()
Posted by Rakie Kim 11 months ago
On Fri, 7 Mar 2025 10:23:57 -0500 Gregory Price <gourry@gourry.net> wrote:

Hi Gregory

 On Fri, Mar 07, 2025 at 03:35:33PM +0900, Rakie Kim wrote:
> > Improper cleanup of sysfs attributes caused kobject and memory leaks when
> > initialization failed or nodes were removed.
> >
> 
> Is this fixing something in your patch set or fixing something in the
> current upstream code?  If in the current patch set, roll this into the
> patch that causes it.
> 
> If this is fixing something upstream, I recommend submitting this
> separately to stable and rebasing on top of it.

Thank you for your response regarding this patch.
This patch isn't a modification of my hotplug-related patch but rather
a fix addressing issues in the existing implementation of the commit
listed below.
I will proceed to update it as a separate patch based on the mentioned
commit.

mm/mempolicy: implement the sysfs-based weighted_interleave interface
(dce41f5ae2539d1c20ae8de4e039630aec3c3f3c)

Rakie

>
> 
> ~Gregory