[PATCH OLK-6.6 0/2] fix raid10 resync panic
fix raid10 resync panic Christoph Hellwig (1): block: add a bio_reuse helper Zhou Minqiang (1): md/raid10: fix stale bi_offset in sync_request_write() block/bio.c | 34 ++++++++++++++++++++++++++++++++++ drivers/md/raid10.c | 3 +-- include/linux/bio.h | 1 + 3 files changed, 36 insertions(+), 2 deletions(-) -- 2.52.0
From: Christoph Hellwig <hch@lst.de> mainline inclusion from mainline-v7.0-rc1 commit 7ca44303f9f6160a2f87ae3d5d2326d9127cd61c category: bugfix bugzilla: https://atomgit.com/openeuler/kernel/issues/10464 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?i... -------------------------------- Add a helper to allow an existing bio to be resubmitted without having to re-add the payload. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Jens Axboe <axboe@kernel.dk> Reviewed-by: Hans Holmberg <hans.holmberg@wdc.com> Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com> Reviewed-by: Damien Le Moal <dlemoal@kernel.org> Reviewed-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Carlos Maiolino <cem@kernel.org> --- block/bio.c | 34 ++++++++++++++++++++++++++++++++++ include/linux/bio.h | 1 + 2 files changed, 35 insertions(+) diff --git a/block/bio.c b/block/bio.c index b5e01b077703..09fe4cf24826 100644 --- a/block/bio.c +++ b/block/bio.c @@ -331,6 +331,40 @@ void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf) } EXPORT_SYMBOL(bio_reset); +/** + * bio_reuse - reuse a bio with the payload left intact + * @bio: bio to reuse + * @opf: operation and flags for the next I/O + * + * Allow reusing an existing bio for another operation with all set up + * fields including the payload, device and end_io handler left intact. + * + * Typically used when @bio is first used to read data which is then written + * to another location without modification. @bio must not be in-flight and + * owned by the caller. Can't be used for cloned bios. + * + * Note: Can't be used when @bio has integrity or blk-crypto contexts for now. + * Feel free to add that support when you need it, though. + */ +void bio_reuse(struct bio *bio, blk_opf_t opf) +{ + unsigned short vcnt = bio->bi_vcnt, i; + bio_end_io_t *end_io = bio->bi_end_io; + void *private = bio->bi_private; + + WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); + WARN_ON_ONCE(bio_integrity(bio)); + WARN_ON_ONCE(bio_has_crypt_ctx(bio)); + + bio_reset(bio, bio->bi_bdev, opf); + for (i = 0; i < vcnt; i++) + bio->bi_iter.bi_size += bio->bi_io_vec[i].bv_len; + bio->bi_vcnt = vcnt; + bio->bi_private = private; + bio->bi_end_io = end_io; +} +EXPORT_SYMBOL_GPL(bio_reuse); + static struct bio *__bio_chain_endio(struct bio *bio) { struct bio *parent = bio->bi_private; diff --git a/include/linux/bio.h b/include/linux/bio.h index 0e0242f5b339..fe1986dd3f63 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -479,6 +479,7 @@ void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table, unsigned short max_vecs, blk_opf_t opf); extern void bio_uninit(struct bio *); void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf); +void bio_reuse(struct bio *bio, blk_opf_t opf); void bio_chain(struct bio *, struct bio *); int __must_check bio_add_page(struct bio *bio, struct page *page, unsigned len, -- 2.52.0
maillist inclusion category: bugfix bugzilla: https://atomgit.com/openeuler/kernel/issues/10464 Reference: https://lore.kernel.org/all/20261126080632.1598653-1-zhouminqiang2@huawei.co... -------------------------------- 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. Reuse fbio with bio_reuse() so the iterator always starts at the beginning of the first bvec, and no other field is left stale. 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 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c index d267a02af348..550ce9fdc1b2 100644 --- a/drivers/md/raid10.c +++ b/drivers/md/raid10.c @@ -2328,8 +2328,7 @@ static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) first = i; fbio = r10_bio->devs[i].bio; - fbio->bi_iter.bi_size = r10_bio->sectors << 9; - fbio->bi_iter.bi_idx = 0; + bio_reuse(fbio, fbio->bi_opf); fpages = get_resync_pages(fbio)->pages; vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9); -- 2.52.0
反馈: 您发送到kernel@openeuler.org的补丁/补丁集,已成功转换为PR! PR链接地址: https://atomgit.com/openeuler/kernel/merge_requests/31686 邮件列表地址:https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/NIK... FeedBack: The patch(es) which you have sent to kernel@openeuler.org mailing list has been converted to a pull request successfully! Pull request link: https://atomgit.com/openeuler/kernel/merge_requests/31686 Mailing list address: https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/NIK...
participants (2)
-
patchwork bot -
Zhou Minqiang