Skip to content

Commit 41425f9

Browse files
YuKuai-huaweiliu-song-6
authored andcommitted
dm-raid456, md/raid456: fix a deadlock for dm-raid456 while io concurrent with reshape
For raid456, if reshape is still in progress, then IO across reshape position will wait for reshape to make progress. However, for dm-raid, in following cases reshape will never make progress hence IO will hang: 1) the array is read-only; 2) MD_RECOVERY_WAIT is set; 3) MD_RECOVERY_FROZEN is set; After commit c467e97 ("md/raid6: use valid sector values to determine if an I/O should wait on the reshape") fix the problem that IO across reshape position doesn't wait for reshape, the dm-raid test shell/lvconvert-raid-reshape.sh start to hang: [root@fedora ~]# cat /proc/979/stack [<0>] wait_woken+0x7d/0x90 [<0>] raid5_make_request+0x929/0x1d70 [raid456] [<0>] md_handle_request+0xc2/0x3b0 [md_mod] [<0>] raid_map+0x2c/0x50 [dm_raid] [<0>] __map_bio+0x251/0x380 [dm_mod] [<0>] dm_submit_bio+0x1f0/0x760 [dm_mod] [<0>] __submit_bio+0xc2/0x1c0 [<0>] submit_bio_noacct_nocheck+0x17f/0x450 [<0>] submit_bio_noacct+0x2bc/0x780 [<0>] submit_bio+0x70/0xc0 [<0>] mpage_readahead+0x169/0x1f0 [<0>] blkdev_readahead+0x18/0x30 [<0>] read_pages+0x7c/0x3b0 [<0>] page_cache_ra_unbounded+0x1ab/0x280 [<0>] force_page_cache_ra+0x9e/0x130 [<0>] page_cache_sync_ra+0x3b/0x110 [<0>] filemap_get_pages+0x143/0xa30 [<0>] filemap_read+0xdc/0x4b0 [<0>] blkdev_read_iter+0x75/0x200 [<0>] vfs_read+0x272/0x460 [<0>] ksys_read+0x7a/0x170 [<0>] __x64_sys_read+0x1c/0x30 [<0>] do_syscall_64+0xc6/0x230 [<0>] entry_SYSCALL_64_after_hwframe+0x6c/0x74 This is because reshape can't make progress. For md/raid, the problem doesn't exist because register new sync_thread doesn't rely on the IO to be done any more: 1) If array is read-only, it can switch to read-write by ioctl/sysfs; 2) md/raid never set MD_RECOVERY_WAIT; 3) If MD_RECOVERY_FROZEN is set, mddev_suspend() doesn't hold 'reconfig_mutex', hence it can be cleared and reshape can continue by sysfs api 'sync_action'. However, I'm not sure yet how to avoid the problem in dm-raid yet. This patch on the one hand make sure raid_message() can't change sync_thread() through raid_message() after presuspend(), on the other hand detect the above 3 cases before wait for IO do be done in dm_suspend(), and let dm-raid requeue those IO. Cc: [email protected] # v6.7+ Signed-off-by: Yu Kuai <[email protected]> Signed-off-by: Xiao Ni <[email protected]> Acked-by: Mike Snitzer <[email protected]> Signed-off-by: Song Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 5625ff8 commit 41425f9

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

drivers/md/dm-raid.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ struct raid_dev {
213213
#define RT_FLAG_RS_IN_SYNC 6
214214
#define RT_FLAG_RS_RESYNCING 7
215215
#define RT_FLAG_RS_GROW 8
216+
#define RT_FLAG_RS_FROZEN 9
216217

217218
/* Array elements of 64 bit needed for rebuild/failed disk bits */
218219
#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
@@ -3340,7 +3341,8 @@ static int raid_map(struct dm_target *ti, struct bio *bio)
33403341
if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
33413342
return DM_MAPIO_REQUEUE;
33423343

3343-
md_handle_request(mddev, bio);
3344+
if (unlikely(!md_handle_request(mddev, bio)))
3345+
return DM_MAPIO_REQUEUE;
33443346

33453347
return DM_MAPIO_SUBMITTED;
33463348
}
@@ -3724,7 +3726,8 @@ static int raid_message(struct dm_target *ti, unsigned int argc, char **argv,
37243726
if (!mddev->pers || !mddev->pers->sync_request)
37253727
return -EINVAL;
37263728

3727-
if (test_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags))
3729+
if (test_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags) ||
3730+
test_bit(RT_FLAG_RS_FROZEN, &rs->runtime_flags))
37283731
return -EBUSY;
37293732

37303733
if (!strcasecmp(argv[0], "frozen")) {
@@ -3808,6 +3811,12 @@ static void raid_presuspend(struct dm_target *ti)
38083811
struct raid_set *rs = ti->private;
38093812
struct mddev *mddev = &rs->md;
38103813

3814+
/*
3815+
* From now on, disallow raid_message() to change sync_thread until
3816+
* resume, raid_postsuspend() is too late.
3817+
*/
3818+
set_bit(RT_FLAG_RS_FROZEN, &rs->runtime_flags);
3819+
38113820
if (!reshape_interrupted(mddev))
38123821
return;
38133822

@@ -3820,6 +3829,13 @@ static void raid_presuspend(struct dm_target *ti)
38203829
mddev->pers->prepare_suspend(mddev);
38213830
}
38223831

3832+
static void raid_presuspend_undo(struct dm_target *ti)
3833+
{
3834+
struct raid_set *rs = ti->private;
3835+
3836+
clear_bit(RT_FLAG_RS_FROZEN, &rs->runtime_flags);
3837+
}
3838+
38233839
static void raid_postsuspend(struct dm_target *ti)
38243840
{
38253841
struct raid_set *rs = ti->private;
@@ -4085,6 +4101,7 @@ static void raid_resume(struct dm_target *ti)
40854101

40864102
WARN_ON_ONCE(!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery));
40874103
WARN_ON_ONCE(test_bit(MD_RECOVERY_RUNNING, &mddev->recovery));
4104+
clear_bit(RT_FLAG_RS_FROZEN, &rs->runtime_flags);
40884105
mddev_lock_nointr(mddev);
40894106
mddev->ro = 0;
40904107
mddev->in_sync = 0;
@@ -4105,6 +4122,7 @@ static struct target_type raid_target = {
41054122
.iterate_devices = raid_iterate_devices,
41064123
.io_hints = raid_io_hints,
41074124
.presuspend = raid_presuspend,
4125+
.presuspend_undo = raid_presuspend_undo,
41084126
.postsuspend = raid_postsuspend,
41094127
.preresume = raid_preresume,
41104128
.resume = raid_resume,

drivers/md/md.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,15 @@ static bool is_suspended(struct mddev *mddev, struct bio *bio)
366366
return true;
367367
}
368368

369-
void md_handle_request(struct mddev *mddev, struct bio *bio)
369+
bool md_handle_request(struct mddev *mddev, struct bio *bio)
370370
{
371371
check_suspended:
372372
if (is_suspended(mddev, bio)) {
373373
DEFINE_WAIT(__wait);
374374
/* Bail out if REQ_NOWAIT is set for the bio */
375375
if (bio->bi_opf & REQ_NOWAIT) {
376376
bio_wouldblock_error(bio);
377-
return;
377+
return true;
378378
}
379379
for (;;) {
380380
prepare_to_wait(&mddev->sb_wait, &__wait,
@@ -390,10 +390,13 @@ void md_handle_request(struct mddev *mddev, struct bio *bio)
390390

391391
if (!mddev->pers->make_request(mddev, bio)) {
392392
percpu_ref_put(&mddev->active_io);
393+
if (!mddev->gendisk && mddev->pers->prepare_suspend)
394+
return false;
393395
goto check_suspended;
394396
}
395397

396398
percpu_ref_put(&mddev->active_io);
399+
return true;
397400
}
398401
EXPORT_SYMBOL(md_handle_request);
399402

@@ -8733,6 +8736,23 @@ void md_account_bio(struct mddev *mddev, struct bio **bio)
87338736
}
87348737
EXPORT_SYMBOL_GPL(md_account_bio);
87358738

8739+
void md_free_cloned_bio(struct bio *bio)
8740+
{
8741+
struct md_io_clone *md_io_clone = bio->bi_private;
8742+
struct bio *orig_bio = md_io_clone->orig_bio;
8743+
struct mddev *mddev = md_io_clone->mddev;
8744+
8745+
if (bio->bi_status && !orig_bio->bi_status)
8746+
orig_bio->bi_status = bio->bi_status;
8747+
8748+
if (md_io_clone->start_time)
8749+
bio_end_io_acct(orig_bio, md_io_clone->start_time);
8750+
8751+
bio_put(bio);
8752+
percpu_ref_put(&mddev->active_io);
8753+
}
8754+
EXPORT_SYMBOL_GPL(md_free_cloned_bio);
8755+
87368756
/* md_allow_write(mddev)
87378757
* Calling this ensures that the array is marked 'active' so that writes
87388758
* may proceed without blocking. It is important to call this before

drivers/md/md.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ extern void md_finish_reshape(struct mddev *mddev);
793793
void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
794794
struct bio *bio, sector_t start, sector_t size);
795795
void md_account_bio(struct mddev *mddev, struct bio **bio);
796+
void md_free_cloned_bio(struct bio *bio);
796797

797798
extern bool __must_check md_flush_request(struct mddev *mddev, struct bio *bio);
798799
extern void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
@@ -821,7 +822,7 @@ extern void md_stop_writes(struct mddev *mddev);
821822
extern int md_rdev_init(struct md_rdev *rdev);
822823
extern void md_rdev_clear(struct md_rdev *rdev);
823824

824-
extern void md_handle_request(struct mddev *mddev, struct bio *bio);
825+
extern bool md_handle_request(struct mddev *mddev, struct bio *bio);
825826
extern int mddev_suspend(struct mddev *mddev, bool interruptible);
826827
extern void mddev_resume(struct mddev *mddev);
827828
extern void md_idle_sync_thread(struct mddev *mddev);

drivers/md/raid5.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ enum stripe_result {
760760
STRIPE_RETRY,
761761
STRIPE_SCHEDULE_AND_RETRY,
762762
STRIPE_FAIL,
763+
STRIPE_WAIT_RESHAPE,
763764
};
764765

765766
struct stripe_request_ctx {
@@ -5937,7 +5938,8 @@ static enum stripe_result make_stripe_request(struct mddev *mddev,
59375938
if (ahead_of_reshape(mddev, logical_sector,
59385939
conf->reshape_safe)) {
59395940
spin_unlock_irq(&conf->device_lock);
5940-
return STRIPE_SCHEDULE_AND_RETRY;
5941+
ret = STRIPE_SCHEDULE_AND_RETRY;
5942+
goto out;
59415943
}
59425944
}
59435945
spin_unlock_irq(&conf->device_lock);
@@ -6016,6 +6018,12 @@ static enum stripe_result make_stripe_request(struct mddev *mddev,
60166018

60176019
out_release:
60186020
raid5_release_stripe(sh);
6021+
out:
6022+
if (ret == STRIPE_SCHEDULE_AND_RETRY && reshape_interrupted(mddev)) {
6023+
bi->bi_status = BLK_STS_RESOURCE;
6024+
ret = STRIPE_WAIT_RESHAPE;
6025+
pr_err_ratelimited("dm-raid456: io across reshape position while reshape can't make progress");
6026+
}
60196027
return ret;
60206028
}
60216029

@@ -6137,7 +6145,7 @@ static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
61376145
while (1) {
61386146
res = make_stripe_request(mddev, conf, &ctx, logical_sector,
61396147
bi);
6140-
if (res == STRIPE_FAIL)
6148+
if (res == STRIPE_FAIL || res == STRIPE_WAIT_RESHAPE)
61416149
break;
61426150

61436151
if (res == STRIPE_RETRY)
@@ -6175,6 +6183,11 @@ static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
61756183

61766184
if (rw == WRITE)
61776185
md_write_end(mddev);
6186+
if (res == STRIPE_WAIT_RESHAPE) {
6187+
md_free_cloned_bio(bi);
6188+
return false;
6189+
}
6190+
61786191
bio_endio(bi);
61796192
return true;
61806193
}
@@ -8925,6 +8938,18 @@ static int raid5_start(struct mddev *mddev)
89258938
return r5l_start(conf->log);
89268939
}
89278940

8941+
/*
8942+
* This is only used for dm-raid456, caller already frozen sync_thread, hence
8943+
* if rehsape is still in progress, io that is waiting for reshape can never be
8944+
* done now, hence wake up and handle those IO.
8945+
*/
8946+
static void raid5_prepare_suspend(struct mddev *mddev)
8947+
{
8948+
struct r5conf *conf = mddev->private;
8949+
8950+
wake_up(&conf->wait_for_overlap);
8951+
}
8952+
89288953
static struct md_personality raid6_personality =
89298954
{
89308955
.name = "raid6",
@@ -8948,6 +8973,7 @@ static struct md_personality raid6_personality =
89488973
.quiesce = raid5_quiesce,
89498974
.takeover = raid6_takeover,
89508975
.change_consistency_policy = raid5_change_consistency_policy,
8976+
.prepare_suspend = raid5_prepare_suspend,
89518977
};
89528978
static struct md_personality raid5_personality =
89538979
{
@@ -8972,6 +8998,7 @@ static struct md_personality raid5_personality =
89728998
.quiesce = raid5_quiesce,
89738999
.takeover = raid5_takeover,
89749000
.change_consistency_policy = raid5_change_consistency_policy,
9001+
.prepare_suspend = raid5_prepare_suspend,
89759002
};
89769003

89779004
static struct md_personality raid4_personality =
@@ -8997,6 +9024,7 @@ static struct md_personality raid4_personality =
89979024
.quiesce = raid5_quiesce,
89989025
.takeover = raid4_takeover,
89999026
.change_consistency_policy = raid5_change_consistency_policy,
9027+
.prepare_suspend = raid5_prepare_suspend,
90009028
};
90019029

90029030
static int __init raid5_init(void)

0 commit comments

Comments
 (0)