[PATCH 2/7] md/raid5: make sure max_sectors is not less than io_opt

From: Yu Kuai

Date: Fri Nov 21 2025 - 00:14:14 EST


Otherwise, even if user issue IO by io_opt, such IO will be split
by max_sectors before they are submitted to raid5. For consequence,
full stripe IO is impossible.

BTW, dm-raid5 is not affected and still have such problem.

Signed-off-by: Yu Kuai <yukuai@xxxxxxxxx>
---
drivers/md/raid5.c | 35 ++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
index 0ccb5907cd20..dc7bdbdb04b7 100644
--- a/drivers/md/raid5.c
+++ b/drivers/md/raid5.c
@@ -773,14 +773,14 @@ struct stripe_request_ctx {
/* last sector in the request */
sector_t last_sector;

+ /* the request had REQ_PREFLUSH, cleared after the first stripe_head */
+ bool do_flush;
+
/*
* bitmap to track stripe sectors that have been added to stripes
* add one to account for unaligned requests
*/
- DECLARE_BITMAP(sectors_to_do, RAID5_MAX_REQ_STRIPES + 1);
-
- /* the request had REQ_PREFLUSH, cleared after the first stripe_head */
- bool do_flush;
+ unsigned long sectors_to_do[];
};

/*
@@ -7732,6 +7732,24 @@ static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded
return 0;
}

+static int raid5_create_ctx_pool(struct r5conf *conf)
+{
+ struct stripe_request_ctx *ctx;
+ int size;
+
+ if (mddev_is_dm(conf->mddev))
+ size = BITS_TO_LONGS(RAID5_MAX_REQ_STRIPES);
+ else
+ size = BITS_TO_LONGS(
+ queue_max_hw_sectors(conf->mddev->gendisk->queue) >>
+ RAID5_STRIPE_SHIFT(conf));
+
+ conf->ctx_pool = mempool_create_kmalloc_pool(NR_RAID_BIOS,
+ struct_size(ctx, sectors_to_do, size));
+
+ return conf->ctx_pool ? 0 : -ENOMEM;
+}
+
static int raid5_set_limits(struct mddev *mddev)
{
struct r5conf *conf = mddev->private;
@@ -7788,6 +7806,8 @@ static int raid5_set_limits(struct mddev *mddev)
* Limit the max sectors based on this.
*/
lim.max_hw_sectors = RAID5_MAX_REQ_STRIPES << RAID5_STRIPE_SHIFT(conf);
+ if ((lim.max_hw_sectors << 9) < lim.io_opt)
+ lim.max_hw_sectors = lim.io_opt >> 9;

/* No restrictions on the number of segments in the request */
lim.max_segments = USHRT_MAX;
@@ -8060,12 +8080,9 @@ static int raid5_run(struct mddev *mddev)
goto abort;
}

- conf->ctx_pool = mempool_create_kmalloc_pool(NR_RAID_BIOS,
- sizeof(struct stripe_request_ctx));
- if (!conf->ctx_pool) {
- ret = -ENOMEM;
+ ret = raid5_create_ctx_pool(conf);
+ if (ret)
goto abort;
- }

if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
goto abort;
--
2.51.0