Skip to content

Commit cb65082

Browse files
youngjuniakpm00
authored andcommitted
mm, swap: fix memory leak in setup_clusters() error path
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: 07adc4c ("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>
1 parent c230719 commit cb65082

1 file changed

Lines changed: 2 additions & 3 deletions

File tree

mm/swapfile.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3330,7 +3330,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
33303330
si->global_cluster = kmalloc(sizeof(*si->global_cluster),
33313331
GFP_KERNEL);
33323332
if (!si->global_cluster)
3333-
goto err_free;
3333+
goto err;
33343334
for (i = 0; i < SWAP_NR_ORDERS; i++)
33353335
si->global_cluster->next[i] = SWAP_ENTRY_INVALID;
33363336
spin_lock_init(&si->global_cluster_lock);
@@ -3383,9 +3383,8 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
33833383
}
33843384

33853385
return cluster_info;
3386-
err_free:
3387-
free_cluster_info(cluster_info, maxpages);
33883386
err:
3387+
free_cluster_info(cluster_info, maxpages);
33893388
return ERR_PTR(err);
33903389
}
33913390

0 commit comments

Comments
 (0)