Skip to content

Commit 7b15c24

Browse files
masalkhiYu Kuai
authored andcommitted
md/raid1,raid10: fix deadlock in read error recovery path
raid1d and raid10d may resubmit a split md cloned bio while handling a read error. In this case, resubmitting the bio can lead to a deadlock if the array is suspended before md_handle_request() acquires an active_io reference via percpu_ref_tryget_live(). Since the cloned bio already holds an active_io reference, trying to acquire another reference via percpu_ref_tryget_live() can lead to a deadlock while the array is suspended. Fix this by using percpu_ref_get() for md cloned bios. Fixes: bb2a9ac ("md/raid1: switch to use md_account_bio() for io accounting") Fixes: 8204552 ("md/raid10: switch to use md_account_bio() for io accounting") Signed-off-by: Abd-Alrhman Masalkhi <abd.masalkhi@gmail.com> Reviewed-by: Xiao Ni <xiao@kernel.org> Reviewed-by: Yu Kuai <yukuai@fygo.io> Link: https://patch.msgid.link/20260501114652.590037-2-abd.masalkhi@gmail.com Signed-off-by: Yu Kuai <yukuai@fygo.io>
1 parent 6b8a26a commit 7b15c24

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

drivers/md/md.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,24 @@ static bool is_suspended(struct mddev *mddev, struct bio *bio)
395395
bool md_handle_request(struct mddev *mddev, struct bio *bio)
396396
{
397397
check_suspended:
398-
if (is_suspended(mddev, bio)) {
399-
/* Bail out if REQ_NOWAIT is set for the bio */
400-
if (bio->bi_opf & REQ_NOWAIT) {
401-
bio_wouldblock_error(bio);
402-
return true;
398+
if (unlikely(md_cloned_bio(mddev, bio))) {
399+
/*
400+
* This bio is an MD cloned bio and already holds an
401+
* active_io reference, so percpu_ref_get() is safe here.
402+
*/
403+
percpu_ref_get(&mddev->active_io);
404+
} else {
405+
if (is_suspended(mddev, bio)) {
406+
/* Bail out if REQ_NOWAIT is set for the bio */
407+
if (bio->bi_opf & REQ_NOWAIT) {
408+
bio_wouldblock_error(bio);
409+
return true;
410+
}
411+
wait_event(mddev->sb_wait, !is_suspended(mddev, bio));
403412
}
404-
wait_event(mddev->sb_wait, !is_suspended(mddev, bio));
413+
if (!percpu_ref_tryget_live(&mddev->active_io))
414+
goto check_suspended;
405415
}
406-
if (!percpu_ref_tryget_live(&mddev->active_io))
407-
goto check_suspended;
408-
409416
if (!mddev->pers->make_request(mddev, bio)) {
410417
percpu_ref_put(&mddev->active_io);
411418
if (mddev_is_dm(mddev) && mddev->pers->prepare_suspend)

drivers/md/md.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,11 @@ void mddev_update_io_opt(struct mddev *mddev, unsigned int nr_stripes);
10441044

10451045
extern const struct block_device_operations md_fops;
10461046

1047+
static inline bool md_cloned_bio(struct mddev *mddev, struct bio *bio)
1048+
{
1049+
return bio->bi_pool == &mddev->io_clone_set;
1050+
}
1051+
10471052
/*
10481053
* MD devices can be used undeneath by DM, in which case ->gendisk is NULL.
10491054
*/

0 commit comments

Comments
 (0)