* [PATCH] md/raid10: reset bi_offset in sync_request_write()
@ 2026-11-24 6:08 Zhou Minqiang
2026-11-25 5:03 ` Christoph Hellwig
0 siblings, 1 reply; 3+ messages in thread
From: Zhou Minqiang @ 2026-11-24 6:08 UTC (permalink / raw)
To: song, yukuai, neilb, artur.paszkiewicz, shli, axboe
Cc: linux-raid, chengzhihao1, yangerkun, yi.zhang
sync_request_write() reuses the bio that completed the resync read (fbio)
as the source bio of bio_copy_data(). Commit cc57858831e3 ("md/raid10:
fix data corruption and crash during resync") restored bi_iter.bi_size and
bi_iter.bi_idx, but missed bi_iter.bi_offset. When bi_offset is non-zero,
bio_copy_data() starts copying from the middle of the first bvec and
walks past the end of the bvec table:
BUG: KASAN: wild-memory-access in bio_copy_data+0x3d4/0x8f0
Read of size 1919 at addr 00050880ffff8881 by task md0_raid10/2674
CPU: 7 UID: 0 PID: 2674 Comm: md0_raid10 Not tainted
Call Trace:
bio_copy_data
raid10d
md_thread
Commit d4aa57a1cac3 ("block: don't bother iter advancing a fully done bio")
added a fast path to bio_advance(): a bio that completes its full length
returns without calling __bio_advance(), which makes a stale bi_offset
possible and lets this bug be triggered.
The residue is produced when the block layer splits the resync bio inside
a merged bvec. The resync bio is built in raid10_sync_request() with a
series of bio_add_page() calls, and bio_add_page() merges physically
contiguous pages into one bvec. blk_mq_submit_bio() then passes the bio
to __bio_split_to_limits(), where bio_may_need_split() checks the merged
bvec against max_fast_segment_size (4096 bytes on loop), and exceeding
it forces bio_split_rw() to cut in the middle of the bvec and resubmit
the remainder. With all 16 resync pages merged into a single bvec:
split 1: orig 65536 offset=0 -> frag 4096 | orig 61440 offset=4096
split 2: orig 61440 offset=4096 -> frag 4096 | orig 57344 offset=8192
...
split 15: orig 8192 offset=57344 -> frag 4096 | orig 4096 offset=61440
no split: orig 4096 offset=61440
After these 15 splits, there is nothing left to cut, so the remaining
4096 bytes are submitted as the original bio itself. When it completes,
blk_update_request() calls bio_advance() with nbytes equal to bi_size,
which takes the fast path and skips __bio_advance():
blk_mq_end_request
blk_update_request
bio_advance(bio, 4096) /* 4096 == bi_size, fast path */
bi_size = 0 /* bi_offset left at 61440 */
sync_request_write() then resets bi_idx to 0 but keeps bi_offset at
61440, so bio_copy_data() starts at offset 61440 of bvec 0 while claiming
to copy the full 64 KiB, and the crash above follows.
Reset bi_offset along with bi_idx so the reused iterator always starts at
the beginning of the first bvec.
Fixes: cc57858831e3 ("md/raid10: fix data corruption and crash during resync")
Fixes: d4aa57a1cac3 ("block: don't bother iter advancing a fully done bio")
Signed-off-by: Zhou Minqiang <zhouminqiang2@huawei.com>
---
drivers/md/raid10.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index 066c43154497..74e834246ca2 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -2330,6 +2330,7 @@ static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
fbio = r10_bio->devs[i].bio;
fbio->bi_iter.bi_size = r10_bio->sectors << 9;
fbio->bi_iter.bi_idx = 0;
+ fbio->bi_iter.bi_offset = 0;
fpages = get_resync_pages(fbio)->pages;
vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
--
2.52.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] md/raid10: reset bi_offset in sync_request_write()
2026-11-24 6:08 [PATCH] md/raid10: reset bi_offset in sync_request_write() Zhou Minqiang
@ 2026-11-25 5:03 ` Christoph Hellwig
2026-11-26 9:00 ` zhouminqiang
0 siblings, 1 reply; 3+ messages in thread
From: Christoph Hellwig @ 2026-11-25 5:03 UTC (permalink / raw)
To: Zhou Minqiang
Cc: song, yukuai, neilb, artur.paszkiewicz, shli, axboe, linux-raid,
chengzhihao1, yangerkun, yi.zhang
On Tue, Nov 24, 2026 at 02:08:56PM +0800, Zhou Minqiang wrote:
> +++ b/drivers/md/raid10.c
> @@ -2330,6 +2330,7 @@ static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
> fbio = r10_bio->devs[i].bio;
> fbio->bi_iter.bi_size = r10_bio->sectors << 9;
> fbio->bi_iter.bi_idx = 0;
> + fbio->bi_iter.bi_offset = 0;
> fpages = get_resync_pages(fbio)->pages;
Directly messing with the iterator is a really bad idea. This needs to
use bio_reuse or bio_reset.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] md/raid10: reset bi_offset in sync_request_write()
2026-11-25 5:03 ` Christoph Hellwig
@ 2026-11-26 9:00 ` zhouminqiang
0 siblings, 0 replies; 3+ messages in thread
From: zhouminqiang @ 2026-11-26 9:00 UTC (permalink / raw)
To: Christoph Hellwig
Cc: song, yukuai, neilb, artur.paszkiewicz, shli, axboe, linux-raid,
chengzhihao1, yangerkun, yi.zhang
Hi,
bio_reuse() is indeed the right helper. v2 has been sent:
https://lore.kernel.org/all/20261126080632.1598653-1-zhouminqiang2@huawei.com/
在 2026/11/25 13:03, Christoph Hellwig 写道:
> On Tue, Nov 24, 2026 at 02:08:56PM +0800, Zhou Minqiang wrote:
>> +++ b/drivers/md/raid10.c
>> @@ -2330,6 +2330,7 @@ static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
>> fbio = r10_bio->devs[i].bio;
>> fbio->bi_iter.bi_size = r10_bio->sectors << 9;
>> fbio->bi_iter.bi_idx = 0;
>> + fbio->bi_iter.bi_offset = 0;
>> fpages = get_resync_pages(fbio)->pages;
>
> Directly messing with the iterator is a really bad idea. This needs to
> use bio_reuse or bio_reset.
Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-26 9:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 6:08 [PATCH] md/raid10: reset bi_offset in sync_request_write() Zhou Minqiang
2026-11-25 5:03 ` Christoph Hellwig
2026-11-26 9:00 ` zhouminqiang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox