]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mm, swap: fix memory leak in setup_clusters() error path
authorYoungjun Park <youngjun.park@lge.com>
Fri, 31 Oct 2025 06:50:07 +0000 (15:50 +0900)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 24 Nov 2025 23:08:56 +0000 (15:08 -0800)
Patch series "mm: swap: small fixes and comment cleanups", v2.

This series provides a few small fixes and cleanups for the swap code.

The first patch fixes a memory leak in an error path that was recently
introduced. The subsequent patches include minor logic adjustments and
the removal of redundant comments.

This patch (of 5):

setup_clusters() could leak 'cluster_info' memory if an error occurred on
a path that did not jump to the 'err_free' label.

This patch simplifies the error handling by removing the goto label and
instead calling free_cluster_info() on all error exit paths.

The new logic is safe, as free_cluster_info() already handles NULL pointer
inputs.

Link: https://lkml.kernel.org/r/20251031065011.40863-1-youngjun.park@lge.com
Link: https://lkml.kernel.org/r/20251031065011.40863-2-youngjun.park@lge.com
Fixes: 07adc4cf1ecd ("mm, swap: implement dynamic allocation of swap table")
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
Reviewed-by: Kairui Song <kasong@tencent.com>
Reviewed-by: Baoquan He <bhe@redhat.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Chris Li <chrisl@kernel.org>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>
Cc: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/swapfile.c

index cf780fefaf7d7974764116d59ee935223254df33..0a822e0d9bf9220e3986d7d264b1763c5ec0237b 100644 (file)
@@ -3330,7 +3330,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
                si->global_cluster = kmalloc(sizeof(*si->global_cluster),
                                     GFP_KERNEL);
                if (!si->global_cluster)
-                       goto err_free;
+                       goto err;
                for (i = 0; i < SWAP_NR_ORDERS; i++)
                        si->global_cluster->next[i] = SWAP_ENTRY_INVALID;
                spin_lock_init(&si->global_cluster_lock);
@@ -3383,9 +3383,8 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
        }
 
        return cluster_info;
-err_free:
-       free_cluster_info(cluster_info, maxpages);
 err:
+       free_cluster_info(cluster_info, maxpages);
        return ERR_PTR(err);
 }