[PATCH v2 1/3] md/raid5: set pool_size before extra_page allocation to fix leak on error path
From: ghuicao
Date: Thu Aug 27 2026 - 02:07:27 EST
From: Cao Guanghui <caoguanghui@xxxxxxxxxx>
In setup_conf(), conf->disks is allocated with max_disks slots and
extra_page is allocated for each slot. However, pool_size remains 0
(uninitialized from kzalloc) until grow_stripes() sets it later. If
any allocation or initialization between the extra_page loop and
grow_stripes() fails and jumps to abort, free_conf() iterates
pool_size (= 0) times and skips the extra_page freeing loop entirely,
leaking max_disks pages.
Set pool_size to max_disks right after the disks array allocation
succeeds, so that free_conf() correctly frees all allocated
extra_page entries on any error path. This is safe because:
- If kzalloc_objs(disks) fails, pool_size stays 0 and free_conf
skips the loop (kfree(NULL) is safe).
- grow_stripes() later sets pool_size = devs, which equals max_disks,
so the early assignment does not change the final value.
- resize_stripes() only updates pool_size on success and reallocates
the disks array in lockstep.
Fixes: d7bd398e97f2 ("md/r5cache: handle alloc_page failure")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Cao Guanghui <caoguanghui@xxxxxxxxxx>
---
drivers/md/raid5.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
--- a/drivers/md/raid5.c
+++ b/drivers/md/raid5.c
@@ -7732,8 +7732,9 @@ static struct r5conf *setup_conf(struct mddev *mddev)
conf->disks = kzalloc_objs(struct disk_info, max_disks);
if (!conf->disks)
goto abort;
+ conf->pool_size = max_disks;
for (i = 0; i < max_disks; i++) {
conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
if (!conf->disks[i].extra_page)
--
2.34.1