From: Rakie Kim <rakie.kim@sk.com>
To: Gregory Price <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 <rakie.kim@sk.com>
Subject: Re: [PATCH v2 1/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init()
Date: Thu, 13 Mar 2025 15:31:38 +0900 [thread overview]
Message-ID: <20250313063247.681-1-rakie.kim@sk.com> (raw)
In-Reply-To: <Z9Gs8i1FhJJ0eaiA@gourry-fedora-PF4VCD3F>
On Wed, 12 Mar 2025 11:49:06 -0400 Gregory Price <gourry@gourry.net> wrote:
Hi Gregory
Thank you for your response regarding this patch.
> On Wed, Mar 12, 2025 at 04:56:24PM +0900, Rakie Kim wrote:
> > 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.
> >
>
> This patch does multiple things, please split these changes into
> multiple patches.
This patch should remain as a single patch since all changes address
kobject-related memory issues in mempolicy_sysfs_init(). If you still
believe it should be split, I would appreciate your suggestion on
which parts should be separated.
>
> > Fixes: dce41f5ae253 ("mm/mempolicy: implement the sysfs-based weighted_interleave interface")
> > Signed-off-by: Rakie Kim <rakie.kim@sk.com>
> > ---
> > mm/mempolicy.c | 29 +++++++++++++++--------------
> > 1 file changed, 15 insertions(+), 14 deletions(-)
> >
> > diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> > index bbaadbeeb291..1691748badb2 100644
> > --- a/mm/mempolicy.c
> > +++ b/mm/mempolicy.c
> > @@ -3541,39 +3541,40 @@ 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) {
> > + node_attrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
> > + GFP_KERNEL);
> > + if (!node_attrs) {
> > err = -ENOMEM;
> > goto err_out;
> > }
> >
> > - node_attrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
> > - GFP_KERNEL);
> > - if (!node_attrs) {
> > + mempolicy_kobj = kzalloc(sizeof(*mempolicy_kobj), GFP_KERNEL);
> > + if (!mempolicy_kobj) {
> > err = -ENOMEM;
> > - goto mempol_out;
> > + goto node_out;
> > }
>
> It's not clear to me why you re-ordered these allocations, it seems
> superfluous.
>
> >
> > err = kobject_init_and_add(mempolicy_kobj, &mempolicy_ktype, mm_kobj,
> > "mempolicy");
> > - if (err)
> > - goto node_out;
> > + if (err) {
> > + kobject_put(mempolicy_kobj);
>
> Is this correct? If kobject_init_and_add fails, from other examples we
> need only free the mempolicy_kobj - because it failed to initialize and
> therefore should not have any references. I think this causes an
> underflow.
Regarding the reordering of mempolicy_kobj allocation:
1) In kobject_init_and_add(), kobject_init() is always called, which
increments the kobject's refcount. Therefore, even if
kobject_init_and_add() fails, kobject_put() must be called to ensure
proper memory cleanup.
int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
struct kobject *parent, const char *fmt, ...)
{
...
kobject_init(kobj, ktype);
retval = kobject_add_varg(kobj, parent, fmt, args);
...
return retval;
}
2) The release function for mempolicy_kobj is responsible for freeing
associated memory:
static void mempolicy_kobj_release(struct kobject *kobj)
{
...
kfree(ngrp->nattrs);
kfree(ngrp);
kfree(kobj);
}
Once mempolicy_kobj is passed to kobject_init_and_add(), the memory
for ngrp->attrs and ngrp should be released via mempolicy_kobj_release().
The allocation order was changed to ensure that kobject_put() properly
invokes mempolicy_kobj_release() when required.
>
> > + 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;
> > + return 0;
> > +
>
> Please keep functional changes and refactors separate. There's more
> churn in this patch than is needed to accomplish what the changelog
> states is the intent.
As mentioned earlier, I believe this patch does not need to be split.
However, if you have further concerns or suggestions, I would
appreciate your input.
>
> > node_out:
> > kfree(node_attrs);
> > -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;
> > +
> > }
> >
> > late_initcall(mempolicy_sysfs_init);
> >
> > base-commit: 80e54e84911a923c40d7bee33a34c1b4be148d7a
> > --
> > 2.34.1
> >
next prev parent reply other threads:[~2025-03-13 6:33 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-12 7:56 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 ` [PATCH v2 4/4] mm/mempolicy: Fix duplicate node addition in sysfs for " Rakie Kim
2025-03-12 15:04 ` 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 [this message]
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=20250313063247.681-1-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