From nobody Tue May 13 06:16:40 2025
Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 82AA61EBFE4;
	Tue,  1 Apr 2025 09:10:36 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=166.125.252.92
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743498642; cv=none;
 b=a7bCjM8wEZLNS2L0NsVkgNcbulpNNIlZtpvxnqciMP71KafX0eKdOIL7lFQslhmWdImIygcuIfPQn7TWnX0D7tA0DoiFvLD9UdDIBVVakozJsg3Y1QFXyTof6iHcnU5mzLSbFSamtisu4LdzH11jNcTy9rop2l4b8Fw1+JHYzRA=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743498642; c=relaxed/simple;
	bh=ev88QDfI70wGQFqzTDVwN6Lpb4hXA5Cj9+UKl3RwI9M=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=iwiYQnW9p9VkhxOb86afCU1mkhfOuVVz2cKixfOEKujMBNayZd83+n56wlLb0LJ6Pj6IMzYaAgK82t9Qsfuavc4Utz5iZ5i4OINyrdl1dROJEdaaErrcQiUiN6nUUKDAnj9l6vCqwe2s2CGBY/xgoVsNFqFMytfi33GNGrUJS1I=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=none (p=none dis=none) header.from=sk.com;
 spf=pass smtp.mailfrom=sk.com; arc=none smtp.client-ip=166.125.252.92
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=none (p=none dis=none) header.from=sk.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=sk.com
X-AuditID: a67dfc5b-669ff7000002311f-33-67ebad88ce4b
From: Rakie Kim <rakie.kim@sk.com>
To: gourry@gourry.net
Cc: akpm@linux-foundation.org,
	linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,
	linux-cxl@vger.kernel.org,
	joshua.hahnjy@gmail.com,
	dan.j.williams@intel.com,
	ying.huang@linux.alibaba.com,
	david@redhat.com,
	Jonathan.Cameron@huawei.com,
	kernel_team@skhynix.com,
	honggyu.kim@sk.com,
	yunjeong.mun@sk.com,
	rakie.kim@sk.com
Subject: [PATCH v4 1/3] mm/mempolicy: Fix memory leaks in weighted interleave
 sysfs
Date: Tue,  1 Apr 2025 18:08:57 +0900
Message-ID: <20250401090901.1050-2-rakie.kim@sk.com>
X-Mailer: git-send-email 2.48.1.windows.1
In-Reply-To: <20250401090901.1050-1-rakie.kim@sk.com>
References: <20250401090901.1050-1-rakie.kim@sk.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
X-Brightmail-Tracker: 
 H4sIAAAAAAAAA+NgFnrILMWRmVeSWpSXmKPExsXC9ZZnkW7n2tfpBquPMlnMWb+GzWL61AuM
	Fl/X/2K2+Hn3OLvFqoXX2CyOb53HbnF+1ikWi8u75rBZ3Fvzn9Vi9ZoMBy6PnbPusnt0t11m
	92g58pbVY/Gel0wemz5NYvc4MeM3i8fOh5Ye7/ddZfP4vEkugDOKyyYlNSezLLVI3y6BK2Pe
	kkdMBWdkKk5NOMbYwDhDvIuRk0NCwERi1fZjbDD2+0vXWLsYOTjYBJQkju2NAQmLCIhKzDs6
	m6WLkYuDWeAak8SBzzdZQBLCAsESx261sYLYLAKqEp9+TGEGsXmB5vS87WKBmKkp0XDpHhOI
	zSlgKrH/6TFGEFsIqObR7TMsEPWCEidnPgGzmQXkJZq3zmYGWSYh8JpN4si008wQgyQlDq64
	wTKBkX8Wkp5ZSHoWMDKtYhTKzCvLTczMMdHLqMzLrNBLzs/dxAgM+GW1f6J3MH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zl++/1F1679ymVzifzZtfSt/Qljz9UzT4XuDFjbvDMtI1X3+98DnqdZ/28RCLlokHr5hX21
	EktxRqKhFnNRcSIAxk+6MW4CAAA=
X-CFilter-Loop: Reflected
Content-Type: text/plain; charset="utf-8"

Memory leaks occurred when removing sysfs attributes for weighted
interleave. Improper kobject deallocation led to unreleased memory
when initialization failed or when nodes were removed.

This patch resolves the issue by replacing unnecessary `kfree()`
calls with `kobject_put()`, ensuring proper cleanup and preventing
memory leaks.

By correctly using `kobject_put()`, the release function now
properly deallocates memory without causing resource leaks,
thereby improving system stability.

Fixes: dce41f5ae253 ("mm/mempolicy: implement the sysfs-based weighted_inte=
rleave interface")
Signed-off-by: Rakie Kim <rakie.kim@sk.com>
Signed-off-by: Honggyu Kim <honggyu.kim@sk.com>
Signed-off-by: Yunjeong Mun <yunjeong.mun@sk.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
---
 mm/mempolicy.c | 61 +++++++++++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 30 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index bbaadbeeb291..5950d5d5b85e 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -3448,7 +3448,9 @@ static void sysfs_wi_release(struct kobject *wi_kobj)
=20
 	for (i =3D 0; i < nr_node_ids; i++)
 		sysfs_wi_node_release(node_attrs[i], wi_kobj);
-	kobject_put(wi_kobj);
+
+	kfree(node_attrs);
+	kfree(wi_kobj);
 }
=20
 static const struct kobj_type wi_ktype =3D {
@@ -3494,15 +3496,22 @@ static int add_weighted_interleave_group(struct kob=
ject *root_kobj)
 	struct kobject *wi_kobj;
 	int nid, err;
=20
-	wi_kobj =3D kzalloc(sizeof(struct kobject), GFP_KERNEL);
-	if (!wi_kobj)
+	node_attrs =3D kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
+			     GFP_KERNEL);
+	if (!node_attrs)
 		return -ENOMEM;
=20
+	wi_kobj =3D kzalloc(sizeof(struct kobject), GFP_KERNEL);
+	if (!wi_kobj) {
+		err =3D -ENOMEM;
+		goto node_out;
+	}
+
 	err =3D kobject_init_and_add(wi_kobj, &wi_ktype, root_kobj,
 				   "weighted_interleave");
 	if (err) {
-		kfree(wi_kobj);
-		return err;
+		kobject_put(wi_kobj);
+		goto err_out;
 	}
=20
 	for_each_node_state(nid, N_POSSIBLE) {
@@ -3512,9 +3521,17 @@ static int add_weighted_interleave_group(struct kobj=
ect *root_kobj)
 			break;
 		}
 	}
-	if (err)
+	if (err) {
 		kobject_put(wi_kobj);
+		goto err_out;
+	}
+
 	return 0;
+
+node_out:
+	kfree(node_attrs);
+err_out:
+	return err;
 }
=20
 static void mempolicy_kobj_release(struct kobject *kobj)
@@ -3528,7 +3545,6 @@ static void mempolicy_kobj_release(struct kobject *ko=
bj)
 	mutex_unlock(&iw_table_lock);
 	synchronize_rcu();
 	kfree(old);
-	kfree(node_attrs);
 	kfree(kobj);
 }
=20
@@ -3542,37 +3558,22 @@ static int __init mempolicy_sysfs_init(void)
 	static struct kobject *mempolicy_kobj;
=20
 	mempolicy_kobj =3D kzalloc(sizeof(*mempolicy_kobj), GFP_KERNEL);
-	if (!mempolicy_kobj) {
-		err =3D -ENOMEM;
-		goto err_out;
-	}
-
-	node_attrs =3D kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
-			     GFP_KERNEL);
-	if (!node_attrs) {
-		err =3D -ENOMEM;
-		goto mempol_out;
-	}
+	if (!mempolicy_kobj)
+		return -ENOMEM;
=20
 	err =3D kobject_init_and_add(mempolicy_kobj, &mempolicy_ktype, mm_kobj,
 				   "mempolicy");
 	if (err)
-		goto node_out;
+		goto err_out;
=20
 	err =3D add_weighted_interleave_group(mempolicy_kobj);
-	if (err) {
-		pr_err("mempolicy sysfs structure failed to initialize\n");
-		kobject_put(mempolicy_kobj);
-		return err;
-	}
+	if (err)
+		goto err_out;
+
+	return 0;
=20
-	return err;
-node_out:
-	kfree(node_attrs);
-mempol_out:
-	kfree(mempolicy_kobj);
 err_out:
-	pr_err("failed to add mempolicy kobject to the system\n");
+	kobject_put(mempolicy_kobj);
 	return err;
 }
=20
--=20
2.34.1
From nobody Tue May 13 06:16:40 2025
Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 619FD1EDA01;
	Tue,  1 Apr 2025 09:10:42 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=166.125.252.92
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743498645; cv=none;
 b=TccKvN7fD/Jlg2+VBy8yK7coRGcNu0DvkDAtIDCNz6W5Zgmmv5CA8CPo1NUUiEGBr8AYtC1assibiBOK/oXJTmjd0hHc6x51dEQOghnizQiyz3wq5KXf4ngXM6DlrBEcfexeZHVyz/E/aqD9CIOEuyNw+1ZOvg3QoXFTFBIwo14=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743498645; c=relaxed/simple;
	bh=uimdlVjavfDZcsgVyO339gHPURzcTPcAOVuyKRuDtpc=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=D8fUevsiS7FDjtOVPLgBfrwAljQH9n2C30w2h1Futam/64kRG3p8n0ccld93NrCTI9aCl9er36XBdO+TaUGVHn6kb/NvTCf+7Q08PxycwOB46Sw8l5q0MJaTv52vUHM01M96WNZGqJZdODefNZNSqs3qqWgC+6lOiM+BOOOAcLA=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=none (p=none dis=none) header.from=sk.com;
 spf=pass smtp.mailfrom=sk.com; arc=none smtp.client-ip=166.125.252.92
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=none (p=none dis=none) header.from=sk.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=sk.com
X-AuditID: a67dfc5b-669ff7000002311f-3a-67ebad8bf2ef
From: Rakie Kim <rakie.kim@sk.com>
To: gourry@gourry.net
Cc: akpm@linux-foundation.org,
	linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,
	linux-cxl@vger.kernel.org,
	joshua.hahnjy@gmail.com,
	dan.j.williams@intel.com,
	ying.huang@linux.alibaba.com,
	david@redhat.com,
	Jonathan.Cameron@huawei.com,
	kernel_team@skhynix.com,
	honggyu.kim@sk.com,
	yunjeong.mun@sk.com,
	rakie.kim@sk.com
Subject: [PATCH v4 2/3] mm/mempolicy: Support dynamic sysfs updates for
 weighted interleave
Date: Tue,  1 Apr 2025 18:08:58 +0900
Message-ID: <20250401090901.1050-3-rakie.kim@sk.com>
X-Mailer: git-send-email 2.48.1.windows.1
In-Reply-To: <20250401090901.1050-1-rakie.kim@sk.com>
References: <20250401090901.1050-1-rakie.kim@sk.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
X-Brightmail-Tracker: 
 H4sIAAAAAAAAA+NgFnrILMWRmVeSWpSXmKPExsXC9ZZnkW732tfpBqfei1jMWb+GzWL61AuM
	Fl/X/2K2+Hn3OLvFqoXX2CyOb53HbnF+1ikWi8u75rBZ3Fvzn9Vi9ZoMBy6PnbPusnt0t11m
	92g58pbVY/Gel0wemz5NYvc4MeM3i8fOh5Ye7/ddZfP4vEkugDOKyyYlNSezLLVI3y6BK2PV
	wj9MBWeUKjZsucfewDhHpouRk0NCwETi9Nc7TDD21tYpzF2MHBxsAkoSx/bGgIRFBEQl5h2d
	zdLFyMXBLHCNSeLA55ssIAlhgWiJqUv7wXpZBFQlJp+YzA5i8wLNOfT6HQvETE2Jhkv3wGo4
	BUwl9j89xghiCwHVPLp9hgWiXlDi5MwnYDazgLxE89bZzCDLJARes0m8mbeLGWKQp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zr4ZKpLROYK6O07YK4aYfA0NCct7MkfO4Fdd3+5zjqSebLG9Vx2zruUJV+++Nq0dus6bL+4
	6b4oV8tsl20q9zyOrOB/5Rsv4WckOUvFRWrTf3cvB8lJmtLMprMSav23G/44EcdQ0X+7eK8S
	S3FGoqEWc1FxIgBDQjL9bgIAAA==
X-CFilter-Loop: Reflected
Content-Type: text/plain; charset="utf-8"

Previously, the weighted interleave sysfs structure was statically
managed, preventing dynamic updates when nodes were added or removed.

This patch restructures the weighted interleave sysfs to support
dynamic insertion and deletion. The sysfs that was part of
the 'weighted_interleave_group' is now globally accessible,
allowing external access to that sysfs.

With this change, sysfs management for weighted interleave is
more flexible, supporting hotplug events and runtime updates
more effectively.

Signed-off-by: Rakie Kim <rakie.kim@sk.com>
Signed-off-by: Honggyu Kim <honggyu.kim@sk.com>
Signed-off-by: Yunjeong Mun <yunjeong.mun@sk.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
---
 mm/mempolicy.c | 71 ++++++++++++++++++++++----------------------------
 1 file changed, 31 insertions(+), 40 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 5950d5d5b85e..3092a792bd28 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -3388,6 +3388,13 @@ struct iw_node_attr {
 	int nid;
 };
=20
+struct sysfs_wi_group {
+	struct kobject wi_kobj;
+	struct iw_node_attr *nattrs[];
+};
+
+static struct sysfs_wi_group *wi_group;
+
 static ssize_t node_show(struct kobject *kobj, struct kobj_attribute *attr,
 			 char *buf)
 {
@@ -3430,27 +3437,24 @@ static ssize_t node_store(struct kobject *kobj, str=
uct kobj_attribute *attr,
 	return count;
 }
=20
-static struct iw_node_attr **node_attrs;
-
-static void sysfs_wi_node_release(struct iw_node_attr *node_attr,
-				  struct kobject *parent)
+static void sysfs_wi_node_release(int nid)
 {
-	if (!node_attr)
+	if (!wi_group->nattrs[nid])
 		return;
-	sysfs_remove_file(parent, &node_attr->kobj_attr.attr);
-	kfree(node_attr->kobj_attr.attr.name);
-	kfree(node_attr);
+
+	sysfs_remove_file(&wi_group->wi_kobj,
+			  &wi_group->nattrs[nid]->kobj_attr.attr);
+	kfree(wi_group->nattrs[nid]->kobj_attr.attr.name);
+	kfree(wi_group->nattrs[nid]);
 }
=20
 static void sysfs_wi_release(struct kobject *wi_kobj)
 {
-	int i;
-
-	for (i =3D 0; i < nr_node_ids; i++)
-		sysfs_wi_node_release(node_attrs[i], wi_kobj);
+	int nid;
=20
-	kfree(node_attrs);
-	kfree(wi_kobj);
+	for (nid =3D 0; nid < nr_node_ids; nid++)
+		sysfs_wi_node_release(nid);
+	kfree(wi_group);
 }
=20
 static const struct kobj_type wi_ktype =3D {
@@ -3458,7 +3462,7 @@ static const struct kobj_type wi_ktype =3D {
 	.release =3D sysfs_wi_release,
 };
=20
-static int add_weight_node(int nid, struct kobject *wi_kobj)
+static int sysfs_wi_node_add(int nid)
 {
 	struct iw_node_attr *node_attr;
 	char *name;
@@ -3480,57 +3484,44 @@ static int add_weight_node(int nid, struct kobject =
*wi_kobj)
 	node_attr->kobj_attr.store =3D node_store;
 	node_attr->nid =3D nid;
=20
-	if (sysfs_create_file(wi_kobj, &node_attr->kobj_attr.attr)) {
+	if (sysfs_create_file(&wi_group->wi_kobj, &node_attr->kobj_attr.attr)) {
 		kfree(node_attr->kobj_attr.attr.name);
 		kfree(node_attr);
 		pr_err("failed to add attribute to weighted_interleave\n");
 		return -ENOMEM;
 	}
=20
-	node_attrs[nid] =3D node_attr;
+	wi_group->nattrs[nid] =3D node_attr;
 	return 0;
 }
=20
-static int add_weighted_interleave_group(struct kobject *root_kobj)
+static int add_weighted_interleave_group(struct kobject *mempolicy_kobj)
 {
-	struct kobject *wi_kobj;
 	int nid, err;
=20
-	node_attrs =3D kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
-			     GFP_KERNEL);
-	if (!node_attrs)
+	wi_group =3D kzalloc(sizeof(struct sysfs_wi_group) + 		\
+		       nr_node_ids * sizeof(struct iw_node_attr *),	\
+		       GFP_KERNEL);
+	if (!wi_group)
 		return -ENOMEM;
=20
-	wi_kobj =3D kzalloc(sizeof(struct kobject), GFP_KERNEL);
-	if (!wi_kobj) {
-		err =3D -ENOMEM;
-		goto node_out;
-	}
-
-	err =3D kobject_init_and_add(wi_kobj, &wi_ktype, root_kobj,
+	err =3D kobject_init_and_add(&wi_group->wi_kobj, &wi_ktype, mempolicy_kob=
j,
 				   "weighted_interleave");
-	if (err) {
-		kobject_put(wi_kobj);
+	if (err)
 		goto err_out;
-	}
=20
 	for_each_node_state(nid, N_POSSIBLE) {
-		err =3D add_weight_node(nid, wi_kobj);
+		err =3D sysfs_wi_node_add(nid);
 		if (err) {
 			pr_err("failed to add sysfs [node%d]\n", nid);
-			break;
+			goto err_out;
 		}
 	}
-	if (err) {
-		kobject_put(wi_kobj);
-		goto err_out;
-	}
=20
 	return 0;
=20
-node_out:
-	kfree(node_attrs);
 err_out:
+	kobject_put(&wi_group->wi_kobj);
 	return err;
 }
=20
--=20
2.34.1
From nobody Tue May 13 06:16:40 2025
Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 619AB1EDA00;
	Tue,  1 Apr 2025 09:10:42 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=166.125.252.92
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743498645; cv=none;
 b=qrX2WONxniH5P+GIsSMssNut84+nOhkzeZPN7X1S5mq0LkN8BBN0q1Lupg7jc+iM+mU2l1XotiUXIyYSFIQAROJ6D4nPFzMyzJmcoAfh0jo6hjUt64NhIjgLLncioODVYJ8idtwXBNpBYssJKnQKw3KYmU4CRYhwG29GaQzpKtc=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743498645; c=relaxed/simple;
	bh=u7y5RQ3yhvPve+ym+NdXHsq0oUcG0iSL6jH8XakV10w=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=R2dJNvXXzuin77TYdz0xjby9o/BQmmd0vxwN8ziVywCaU+6phapgUczyTReclTmTlJlp/NOn+8uzFHvJah4DuAuCB0J7XMkumWPd+1t8vpiyEfyf3R02aSSeVcbiC6crOC17xxYZDcFSUnju9NCgKiYUqnCdkdwu9Dcfx62PgWY=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=none (p=none dis=none) header.from=sk.com;
 spf=pass smtp.mailfrom=sk.com; arc=none smtp.client-ip=166.125.252.92
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=none (p=none dis=none) header.from=sk.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=sk.com
X-AuditID: a67dfc5b-669ff7000002311f-44-67ebad8d6b2a
From: Rakie Kim <rakie.kim@sk.com>
To: gourry@gourry.net
Cc: akpm@linux-foundation.org,
	linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,
	linux-cxl@vger.kernel.org,
	joshua.hahnjy@gmail.com,
	dan.j.williams@intel.com,
	ying.huang@linux.alibaba.com,
	david@redhat.com,
	Jonathan.Cameron@huawei.com,
	kernel_team@skhynix.com,
	honggyu.kim@sk.com,
	yunjeong.mun@sk.com,
	rakie.kim@sk.com
Subject: [PATCH v4 3/3] mm/mempolicy: Support memory hotplug in weighted
 interleave
Date: Tue,  1 Apr 2025 18:08:59 +0900
Message-ID: <20250401090901.1050-4-rakie.kim@sk.com>
X-Mailer: git-send-email 2.48.1.windows.1
In-Reply-To: <20250401090901.1050-1-rakie.kim@sk.com>
References: <20250401090901.1050-1-rakie.kim@sk.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer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/WTx2PrT0eL/vKpvH
	t9seHotffGDy+LxJLoA7issmJTUnsyy1SN8ugSujadN5poKtxhXPTt9kbWDcrtXFyMkhIWAi
	MWPuSfYuRg4ONgEliWN7Y0DCIgKiEvOOzmbpYuTiYBa4xiRx4PNNFpCEsECwxJOFWxlBbBYB
	VYlvfctYQWxeoDmv3l5kg5ipKdFw6R4TiM0pYCqx/+kxsHohoJpHt8+wQNQLSpyc+QTMZhaQ
	l2jeOpt5AiPPLCSpWUhSCxiZVjGKZOaV5SZm5pjqFWdnVOZlVugl5+duYgQG+bLaPxN3MH65
	7H6IUYCDUYmHd0Ppq3Qh1sSy4srcQ4wSHMxKIrwRX1+mC/GmJFZWpRblxxeV5qQWH2KU5mBR
	Euf1Ck9NEBJITyxJzU5NLUgtgskycXBKNTCu3yq6YY6s8RX3CM7POaY5/VV256dapTfw3Lb6
	qt955rhr04Nim3KVtJ+xj4JMXxwMY5vzpK9csOhnosjfxii+xMwDimFOm0xmNHKaztwiERTf
	HOleciG9+HaC7Y6anwkfih65zQhU1gziixM2n1s2b9eeiNgHVnrnHxZduMEpyZWtwzW5WYml
	OCPRUIu5qDgRAKOluJBuAgAA
X-CFilter-Loop: Reflected
Content-Type: text/plain; charset="utf-8"

The weighted interleave policy distributes page allocations across multiple
NUMA nodes based on their performance weight, thereby improving memory
bandwidth utilization. The weight values for each node are configured
through sysfs.

Previously, sysfs entries for configuring weighted interleave were created
for all possible nodes (N_POSSIBLE) at initialization, including nodes that
might not have memory. However, not all nodes in N_POSSIBLE are usable at
runtime, as some may remain memoryless or offline.
This led to sysfs entries being created for unusable nodes, causing
potential misconfiguration issues.

To address this issue, this patch modifies the sysfs creation logic to:
1) Limit sysfs entries to nodes that are online and have memory, avoiding
   the creation of sysfs entries for nodes that cannot be used.
2) Support memory hotplug by dynamically adding and removing sysfs entries
   based on whether a node transitions into or out of the N_MEMORY state.

Additionally, the patch ensures that sysfs attributes are properly managed
when nodes go offline, preventing stale or redundant entries from persisting
in the system.

By making these changes, the weighted interleave policy now manages its
sysfs entries more efficiently, ensuring that only relevant nodes are
considered for interleaving, and dynamically adapting to memory hotplug
events.

Signed-off-by: Rakie Kim <rakie.kim@sk.com>
Signed-off-by: Honggyu Kim <honggyu.kim@sk.com>
Signed-off-by: Yunjeong Mun <yunjeong.mun@sk.com>
---
 mm/mempolicy.c | 113 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 90 insertions(+), 23 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 3092a792bd28..fa755d20780c 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -113,6 +113,7 @@
 #include <asm/tlbflush.h>
 #include <asm/tlb.h>
 #include <linux/uaccess.h>
+#include <linux/memory.h>
=20
 #include "internal.h"
=20
@@ -3390,6 +3391,7 @@ struct iw_node_attr {
=20
 struct sysfs_wi_group {
 	struct kobject wi_kobj;
+	struct mutex kobj_lock;
 	struct iw_node_attr *nattrs[];
 };
=20
@@ -3439,13 +3441,24 @@ static ssize_t node_store(struct kobject *kobj, str=
uct kobj_attribute *attr,
=20
 static void sysfs_wi_node_release(int nid)
 {
-	if (!wi_group->nattrs[nid])
+	struct iw_node_attr *attr;
+
+	if (nid < 0 || nid >=3D nr_node_ids)
+		return;
+
+	mutex_lock(&wi_group->kobj_lock);
+	attr =3D wi_group->nattrs[nid];
+	if (!attr) {
+		mutex_unlock(&wi_group->kobj_lock);
 		return;
+	}
=20
-	sysfs_remove_file(&wi_group->wi_kobj,
-			  &wi_group->nattrs[nid]->kobj_attr.attr);
-	kfree(wi_group->nattrs[nid]->kobj_attr.attr.name);
-	kfree(wi_group->nattrs[nid]);
+	wi_group->nattrs[nid] =3D NULL;
+	mutex_unlock(&wi_group->kobj_lock);
+
+	sysfs_remove_file(&wi_group->wi_kobj, &attr->kobj_attr.attr);
+	kfree(attr->kobj_attr.attr.name);
+	kfree(attr);
 }
=20
 static void sysfs_wi_release(struct kobject *wi_kobj)
@@ -3464,35 +3477,84 @@ static const struct kobj_type wi_ktype =3D {
=20
 static int sysfs_wi_node_add(int nid)
 {
-	struct iw_node_attr *node_attr;
+	int ret =3D 0;
 	char *name;
+	struct iw_node_attr *new_attr =3D NULL;
=20
-	node_attr =3D kzalloc(sizeof(*node_attr), GFP_KERNEL);
-	if (!node_attr)
+	if (nid < 0 || nid >=3D nr_node_ids) {
+		pr_err("Invalid node id: %d\n", nid);
+		return -EINVAL;
+	}
+
+	new_attr =3D kzalloc(sizeof(struct iw_node_attr), GFP_KERNEL);
+	if (!new_attr)
 		return -ENOMEM;
=20
 	name =3D kasprintf(GFP_KERNEL, "node%d", nid);
 	if (!name) {
-		kfree(node_attr);
+		kfree(new_attr);
 		return -ENOMEM;
 	}
=20
-	sysfs_attr_init(&node_attr->kobj_attr.attr);
-	node_attr->kobj_attr.attr.name =3D name;
-	node_attr->kobj_attr.attr.mode =3D 0644;
-	node_attr->kobj_attr.show =3D node_show;
-	node_attr->kobj_attr.store =3D node_store;
-	node_attr->nid =3D nid;
+	mutex_lock(&wi_group->kobj_lock);
+	if (wi_group->nattrs[nid]) {
+		mutex_unlock(&wi_group->kobj_lock);
+		pr_info("Node [%d] already exists\n", nid);
+		kfree(new_attr);
+		kfree(name);
+		return 0;
+	}
=20
-	if (sysfs_create_file(&wi_group->wi_kobj, &node_attr->kobj_attr.attr)) {
-		kfree(node_attr->kobj_attr.attr.name);
-		kfree(node_attr);
-		pr_err("failed to add attribute to weighted_interleave\n");
-		return -ENOMEM;
+	wi_group->nattrs[nid] =3D new_attr;
+	mutex_unlock(&wi_group->kobj_lock);
+
+	sysfs_attr_init(&wi_group->nattrs[nid]->kobj_attr.attr);
+	wi_group->nattrs[nid]->kobj_attr.attr.name =3D name;
+	wi_group->nattrs[nid]->kobj_attr.attr.mode =3D 0644;
+	wi_group->nattrs[nid]->kobj_attr.show =3D node_show;
+	wi_group->nattrs[nid]->kobj_attr.store =3D node_store;
+	wi_group->nattrs[nid]->nid =3D nid;
+
+	ret =3D sysfs_create_file(&wi_group->wi_kobj,
+				&wi_group->nattrs[nid]->kobj_attr.attr);
+	if (ret) {
+		kfree(wi_group->nattrs[nid]->kobj_attr.attr.name);
+		kfree(wi_group->nattrs[nid]);
+		wi_group->nattrs[nid] =3D NULL;
+		pr_err("Failed to add attribute to weighted_interleave: %d\n", ret);
 	}
=20
-	wi_group->nattrs[nid] =3D node_attr;
-	return 0;
+	return ret;
+}
+
+static int wi_node_notifier(struct notifier_block *nb,
+			       unsigned long action, void *data)
+{
+	int err;
+	struct memory_notify *arg =3D data;
+	int nid =3D arg->status_change_nid;
+
+	if (nid < 0)
+		goto notifier_end;
+
+	switch(action) {
+	case MEM_ONLINE:
+		if (node_state(nid, N_MEMORY)) {
+			err =3D sysfs_wi_node_add(nid);
+			if (err) {
+				pr_err("failed to add sysfs [node%d]\n", nid);
+				return NOTIFY_BAD;
+			}
+		}
+		break;
+	case MEM_OFFLINE:
+		if (!node_state(nid, N_MEMORY))
+			sysfs_wi_node_release(nid);
+		break;
+	}
+
+notifier_end:
+	return NOTIFY_OK;
 }
=20
 static int add_weighted_interleave_group(struct kobject *mempolicy_kobj)
@@ -3504,13 +3566,17 @@ static int add_weighted_interleave_group(struct kob=
ject *mempolicy_kobj)
 		       GFP_KERNEL);
 	if (!wi_group)
 		return -ENOMEM;
+	mutex_init(&wi_group->kobj_lock);
=20
 	err =3D kobject_init_and_add(&wi_group->wi_kobj, &wi_ktype, mempolicy_kob=
j,
 				   "weighted_interleave");
 	if (err)
 		goto err_out;
=20
-	for_each_node_state(nid, N_POSSIBLE) {
+	for_each_online_node(nid) {
+		if (!node_state(nid, N_MEMORY))
+			continue;
+
 		err =3D sysfs_wi_node_add(nid);
 		if (err) {
 			pr_err("failed to add sysfs [node%d]\n", nid);
@@ -3518,6 +3584,7 @@ static int add_weighted_interleave_group(struct kobje=
ct *mempolicy_kobj)
 		}
 	}
=20
+	hotplug_memory_notifier(wi_node_notifier, DEFAULT_CALLBACK_PRI);
 	return 0;
=20
 err_out:
--=20
2.34.1