From: YoungJun Park <youngjun.park@lge.com>
To: Kairui Song <ryncsn@gmail.com>
Cc: akpm@linux-foundation.org, linux-mm@kvack.org,
shikemeng@huaweicloud.com, nphamcs@gmail.com, bhe@redhat.com,
baohua@kernel.org, chrisl@kernel.org, gunho.lee@lge.com
Subject: Re: [PATCH 1/5] mm, swap: Fix memory leak in setup_clusters() error path
Date: Thu, 30 Oct 2025 23:32:38 +0900 [thread overview]
Message-ID: <aQN3BhYtgW3VUD1L@yjaykim-PowerEdge-T330> (raw)
In-Reply-To: <CAMgjq7BiQ2X5K+Ri+wk2J+QdcfwfDGKjT7WXC1_6b2myFGrXaA@mail.gmail.com>
On Wed, Oct 29, 2025 at 11:41:27PM +0800, Kairui Song wrote:
> On Wed, Oct 29, 2025 at 8:44 PM Youngjun Park <youngjun.park@lge.com> wrote:
> >
> > Some error path neglects to free the allocated 'cluster_info',
> > causing a memory leak.
> >
> > Change the error jumps to the 'err_free' label to ensure proper
> > cleanup.
> >
> > Fixes: 07adc4cf1ecd ("mm, swap: implement dynamic allocation of swap table")
> > Signed-off-by: Youngjun Park <youngjun.park@lge.com>
> > ---
> > mm/swapfile.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/mm/swapfile.c b/mm/swapfile.c
> > index c35bb8593f50..6dc0e7a738bc 100644
> > --- a/mm/swapfile.c
> > +++ b/mm/swapfile.c
> > @@ -3339,7 +3339,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
> > */
> > err = swap_cluster_setup_bad_slot(cluster_info, 0);
> > if (err)
> > - goto err;
> > + goto err_free;
> > for (i = 0; i < swap_header->info.nr_badpages; i++) {
> > unsigned int page_nr = swap_header->info.badpages[i];
> >
> > @@ -3347,12 +3347,12 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
> > continue;
> > err = swap_cluster_setup_bad_slot(cluster_info, page_nr);
> > if (err)
> > - goto err;
> > + goto err_free;
> > }
> > for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) {
> > err = swap_cluster_setup_bad_slot(cluster_info, i);
> > if (err)
> > - goto err;
> > + goto err_free;
> > }
> >
> > INIT_LIST_HEAD(&si->free_clusters);
> > --
> > 2.34.1
> >
> >
>
> Nice catch.
Hello Kairui.
> Maybe it's better to just move free_cluster_info under "err:"? That
> might help to avoid more issues like this. free_cluster_info checks if
> cluster_info is null already, we just need to initialize *cluster_info
> with "= NULL".
You're right - moving free_cluster_info under 'err:' is cleaner.
I'll update the code accordingly.
Thanks!
Youngjun Park
> Just a nit pick, I'm fine either way.
>
> Reviewed-by: Kairui Song <kasong@tencent.com>
next prev parent reply other threads:[~2025-10-30 14:32 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-29 8:56 [PATCH 0/5] mm: swap: small fixes and comment cleanups Youngjun Park
2025-10-29 8:56 ` [PATCH 1/5] mm, swap: Fix memory leak in setup_clusters() error path Youngjun Park
2025-10-29 15:41 ` Kairui Song
2025-10-30 14:32 ` YoungJun Park [this message]
2025-10-29 8:56 ` [PATCH 2/5] mm, swap: Use SWP_SOLIDSTATE to determine if swap is rotational Youngjun Park
2025-10-29 16:09 ` Kairui Song
2025-10-30 14:35 ` YoungJun Park
2025-10-29 8:56 ` [PATCH 3/5] mm, swap: Remove redundant comment for read_swap_cache_async Youngjun Park
2025-10-29 8:56 ` [PATCH 4/5] mm: swap: change swap_alloc_slow() to void Youngjun Park
2025-10-29 16:13 ` Kairui Song
2025-10-30 14:39 ` YoungJun Park
2025-10-29 8:56 ` [PATCH 5/5] mm: swap: remove scan_swap_map_slots() references from comments Youngjun Park
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=aQN3BhYtgW3VUD1L@yjaykim-PowerEdge-T330 \
--to=youngjun.park@lge.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=bhe@redhat.com \
--cc=chrisl@kernel.org \
--cc=gunho.lee@lge.com \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=ryncsn@gmail.com \
--cc=shikemeng@huaweicloud.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