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, kernel_team@skhynix.com,
honggyu.kim@sk.com, yunjeong.mun@sk.com, rakie.kim@sk.com
Subject: [PATCH v2 4/4] mm/mempolicy: Fix duplicate node addition in sysfs for weighted interleave
Date: Wed, 12 Mar 2025 16:56:27 +0900 [thread overview]
Message-ID: <20250312075628.648-4-rakie.kim@sk.com> (raw)
In-Reply-To: <20250312075628.648-1-rakie.kim@sk.com>
Sysfs attributes for interleave control were registered both at initialization
and when new nodes were detected via hotplug, leading to potential duplicates.
This patch ensures that each node is registered only once, preventing conflicts
and redundant sysfs entries.
Signed-off-by: Rakie Kim <rakie.kim@sk.com>
---
mm/mempolicy.c | 66 +++++++++++++++++++++++++++++++++++---------------
1 file changed, 46 insertions(+), 20 deletions(-)
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 71aff1276d4d..5f20521036ec 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -3391,6 +3391,7 @@ struct iw_node_attr {
struct iw_node_group {
struct kobject *wi_kobj;
+ struct mutex kobj_lock;
struct iw_node_attr **nattrs;
};
@@ -3440,12 +3441,17 @@ static ssize_t node_store(struct kobject *kobj, struct kobj_attribute *attr,
static void sysfs_wi_node_release(int nid)
{
- if (!ngrp->nattrs[nid])
+ mutex_lock(&ngrp->kobj_lock);
+ if (!ngrp->nattrs[nid]) {
+ mutex_unlock(&ngrp->kobj_lock);
return;
+ }
sysfs_remove_file(ngrp->wi_kobj, &ngrp->nattrs[nid]->kobj_attr.attr);
kfree(ngrp->nattrs[nid]->kobj_attr.attr.name);
kfree(ngrp->nattrs[nid]);
+ ngrp->nattrs[nid] = NULL;
+ mutex_unlock(&ngrp->kobj_lock);
}
static void sysfs_wi_release(struct kobject *wi_kobj)
@@ -3464,35 +3470,54 @@ static const struct kobj_type wi_ktype = {
static int sysfs_wi_node_add(int nid)
{
- struct iw_node_attr *node_attr;
+ int ret = 0;
char *name;
- node_attr = kzalloc(sizeof(*node_attr), GFP_KERNEL);
- if (!node_attr)
- return -ENOMEM;
+ if (nid < 0 || nid >= nr_node_ids) {
+ pr_err("Invalid node id: %d\n", nid);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ mutex_lock(&ngrp->kobj_lock);
+ if (!ngrp->nattrs[nid]) {
+ ngrp->nattrs[nid] = kzalloc(sizeof(struct iw_node_attr), GFP_KERNEL);
+ } else {
+ mutex_unlock(&ngrp->kobj_lock);
+ pr_info("Node [%d] is already existed\n", nid);
+ goto out;
+ }
+ mutex_unlock(&ngrp->kobj_lock);
+
+ if (!ngrp->nattrs[nid]) {
+ ret = -ENOMEM;
+ goto out;
+ }
name = kasprintf(GFP_KERNEL, "node%d", nid);
if (!name) {
- kfree(node_attr);
- return -ENOMEM;
+ kfree(ngrp->nattrs[nid]);
+ ret = -ENOMEM;
+ goto out;
}
- sysfs_attr_init(&node_attr->kobj_attr.attr);
- node_attr->kobj_attr.attr.name = name;
- node_attr->kobj_attr.attr.mode = 0644;
- node_attr->kobj_attr.show = node_show;
- node_attr->kobj_attr.store = node_store;
- node_attr->nid = nid;
+ sysfs_attr_init(&ngrp->nattrs[nid]->kobj_attr.attr);
+ ngrp->nattrs[nid]->kobj_attr.attr.name = name;
+ ngrp->nattrs[nid]->kobj_attr.attr.mode = 0644;
+ ngrp->nattrs[nid]->kobj_attr.show = node_show;
+ ngrp->nattrs[nid]->kobj_attr.store = node_store;
+ ngrp->nattrs[nid]->nid = nid;
- if (sysfs_create_file(ngrp->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;
+ ret = sysfs_create_file(ngrp->wi_kobj, &ngrp->nattrs[nid]->kobj_attr.attr);
+ if (ret) {
+ kfree(ngrp->nattrs[nid]->kobj_attr.attr.name);
+ kfree(ngrp->nattrs[nid]);
+ pr_err("failed to add attribute to weighted_interleave: %d\n", ret);
+ goto out;
}
- ngrp->nattrs[nid] = node_attr;
- return 0;
+out:
+ return ret;
}
static int wi_node_notifier(struct notifier_block *nb,
@@ -3588,6 +3613,7 @@ static int __init mempolicy_sysfs_init(void)
err = -ENOMEM;
goto err_out;
}
+ mutex_init(&ngrp->kobj_lock);
ngrp->nattrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
GFP_KERNEL);
--
2.34.1
next prev parent reply other threads:[~2025-03-12 7:56 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-12 7:56 [PATCH v2 1/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init() Rakie Kim
2025-03-12 7:56 ` [PATCH v2 2/4] mm/mempolicy: Support memory hotplug in weighted interleave Rakie Kim
2025-03-12 16:03 ` Gregory Price
2025-03-13 6:33 ` Rakie Kim
2025-03-13 16:23 ` Gregory Price
2025-03-13 22:36 ` David Hildenbrand
2025-03-14 6:00 ` Rakie Kim
2025-03-14 9:17 ` David Hildenbrand
2025-03-17 8:23 ` Rakie Kim
2025-03-12 7:56 ` [PATCH v2 3/4] mm/mempolicy: Enable sysfs support for " Rakie Kim
2025-03-12 16:14 ` Gregory Price
2025-03-13 6:34 ` Rakie Kim
2025-03-13 16:40 ` Gregory Price
2025-03-14 6:35 ` Rakie Kim
2025-03-12 7:56 ` Rakie Kim [this message]
2025-03-12 15:04 ` [PATCH v2 4/4] mm/mempolicy: Fix duplicate node addition in sysfs for " Joshua Hahn
2025-03-13 6:34 ` Rakie Kim
2025-03-13 16:42 ` Gregory Price
2025-03-14 6:35 ` Rakie Kim
2025-03-12 15:49 ` [PATCH v2 1/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init() Gregory Price
2025-03-13 6:31 ` Rakie Kim
2025-03-13 15:52 ` Gregory Price
2025-03-14 7:44 ` Rakie Kim
2025-03-14 10:55 ` Jonathan Cameron
2025-03-14 13:42 ` Gregory Price
2025-03-17 8:24 ` Rakie Kim
2025-03-17 8:24 ` Rakie Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250312075628.648-4-rakie.kim@sk.com \
--to=rakie.kim@sk.com \
--cc=akpm@linux-foundation.org \
--cc=dan.j.williams@intel.com \
--cc=gourry@gourry.net \
--cc=honggyu.kim@sk.com \
--cc=joshua.hahnjy@gmail.com \
--cc=kernel_team@skhynix.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ying.huang@linux.alibaba.com \
--cc=yunjeong.mun@sk.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox