public inbox for linux-mm@kvack.org
 help / color / mirror / Atom feed
From: Rakie Kim <rakie.kim@sk.com>
To: Joshua Hahn <joshua.hahnjy@gmail.com>
Cc: gourry@gourry.net, akpm@linux-foundation.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org,
	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 4/4] mm/mempolicy: Fix duplicate node addition in sysfs for weighted interleave
Date: Thu, 13 Mar 2025 15:34:37 +0900	[thread overview]
Message-ID: <20250313063511.714-1-rakie.kim@sk.com> (raw)
In-Reply-To: <20250312150440.2301373-1-joshua.hahnjy@gmail.com>

On Wed, 12 Mar 2025 08:04:39 -0700 Joshua Hahn <joshua.hahnjy@gmail.com> wrote:

Hi Joshua
Thank you for your response regarding this patch.

> Hi Rakie, thank your new revision!
> 
> I think this new ordering of the series makes more sense, since the bug exists
> even before your patch is applied! IMHO, it might also make sense to take
> patch 1 out of this series, and send it separately (and make patches 2-4
> their own series). 
> 
> I have a nit and a few thoughts about this patch and the way we order locking
> and allocating:
> 
> >  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);
> 
> I am unsure if kzallocing with the mutex_lock held is best practice. Even though
> two threads won't reach this place simultaneously since *most* calls to this
> function are sequential, we should try to keep the code safe since future
> patches might overlook this, and later make non-sequential calls : -)
> 
> It also doesn't seem wise to directly assign the result of an allocation
> without checking for its success (as I explain below).
> 
> IMHO it is best to allocate first, then acquire the lock and check for
> existence, then assign with the lock still held. We can also reduce this code
> section into a single if statement for clarity (but if you think it looks
> cleaner with the if-else, please keep it!)
> 
> struct iw_node_attr *node_attr;
> 
> ...
> 
> node_attr = kzalloc(sizeof(*node_attr), GFP_KERNEL);
> if (!node_attr) {
> 	ret = -ENOMEM;
> 	goto out;
> }
> 
> mutex_lock(&ngrp->kobj_lock);
> if (ngrp->nattrs[nid]) {
> 	mutex_unlock(&ngrp->kobj_lock);
> 	kfree(node_attr);
> 	pr_info("Node [%d] already exists\n");
> 	goto out;
> }
> ngrp->attrs[nid] = node_attr;
> mutex_unlock(&ngrp->kobj_lock):
> 

Your suggestion makes sense, and I will update this part accordingly
to reflect your feedback.

> 
> > +	} else {
> > +		mutex_unlock(&ngrp->kobj_lock);
> > +		pr_info("Node [%d] is already existed\n", nid);
> 
> NIT: To keep consistency with other parts of the kernel, maybe this can be
> rephrased to "Node [%d] already exists\n"

I also agree that modifying the wording would improve clarity.
I will make the necessary adjustments in the next version.

> 
> > +		goto out;
> > +	}
> > +	mutex_unlock(&ngrp->kobj_lock);
> > +
> > +	if (!ngrp->nattrs[nid]) {
> > +		ret = -ENOMEM;
> > +		goto out;
> > +	}
> 
> If we make the changes above, we don't have to check for allocation success
> *after* already having locked & unlocked and making the unnecessary assignment.
> 
> >  
> >  	name = kasprintf(GFP_KERNEL, "node%d", nid);
> >  	if (!name) {
> > -		kfree(node_attr);
> > -		return -ENOMEM;
> > +		kfree(ngrp->nattrs[nid]);
> > +		ret = -ENOMEM;
> > +		goto out;
> >  	}
> 
> For the same reasons above, I think it makes sense to make this allocation
> together with the allocation of node_attr above, and free / return -ENOMEM
> as early as possible if we can.
> 
> [...snip...]
> 
> Thank you again for this patch! Please let me know what you think : -)
> Have a great day!
> Joshua

Thank you for your detailed and thoughtful review. I will incorporate
your suggestions and update the next version accordingly.

> 
> Sent using hkml (https://github.com/sjp38/hackermail)
> 


  reply	other threads:[~2025-03-13  6:35 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 ` [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 [this message]
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=20250313063511.714-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