Skip to content

Commit 20313b1

Browse files
lsgunthaxboe
authored andcommitted
md/raid5: Ensure batch_last is released before sleeping for quiesce
A race condition exists where if raid5_quiesce() is called in the middle of a request that has set batch_last, it will deadlock. batch_last will hold a reference to a stripe when raid5_quiesce() is called. This will cause the next raid5_get_active_stripe() call to sleep waiting for the quiesce to finish, but the raid5_quiesce() thread will wait for active_stripes to go to zero which will never happen because request thread is waiting for the quiesce to stop. Fix this by creating a special __raid5_get_active_stripe() function which takes the request context and clears the last_batch before sleeping. While we're at it, change the arguments of raid5_get_active_stripe() to bools. Fixes: 3312e6c ("md/raid5: Keep a reference to last stripe_head for batch") Reported-by: David Sloan <[email protected]> Signed-off-by: Logan Gunthorpe <[email protected]> Signed-off-by: Song Liu <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent df6b0e2 commit 20313b1

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

drivers/md/raid5.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,9 @@ static bool is_inactive_blocked(struct r5conf *conf, int hash)
800800
return active < (conf->max_nr_stripes * 3 / 4);
801801
}
802802

803-
struct stripe_head *
804-
raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
805-
int previous, int noblock, int noquiesce)
803+
static struct stripe_head *__raid5_get_active_stripe(struct r5conf *conf,
804+
struct stripe_request_ctx *ctx, sector_t sector,
805+
bool previous, bool noblock, bool noquiesce)
806806
{
807807
struct stripe_head *sh;
808808
int hash = stripe_hash_locks_hash(conf, sector);
@@ -812,9 +812,22 @@ raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
812812
spin_lock_irq(conf->hash_locks + hash);
813813

814814
retry:
815-
wait_event_lock_irq(conf->wait_for_quiescent,
816-
conf->quiesce == 0 || noquiesce,
817-
*(conf->hash_locks + hash));
815+
if (!noquiesce && conf->quiesce) {
816+
/*
817+
* Must release the reference to batch_last before waiting,
818+
* on quiesce, otherwise the batch_last will hold a reference
819+
* to a stripe and raid5_quiesce() will deadlock waiting for
820+
* active_stripes to go to zero.
821+
*/
822+
if (ctx && ctx->batch_last) {
823+
raid5_release_stripe(ctx->batch_last);
824+
ctx->batch_last = NULL;
825+
}
826+
827+
wait_event_lock_irq(conf->wait_for_quiescent, !conf->quiesce,
828+
*(conf->hash_locks + hash));
829+
}
830+
818831
sh = find_get_stripe(conf, sector, conf->generation - previous, hash);
819832
if (sh)
820833
goto out;
@@ -850,6 +863,13 @@ raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
850863
return sh;
851864
}
852865

866+
struct stripe_head *raid5_get_active_stripe(struct r5conf *conf,
867+
sector_t sector, bool previous, bool noblock, bool noquiesce)
868+
{
869+
return __raid5_get_active_stripe(conf, NULL, sector, previous, noblock,
870+
noquiesce);
871+
}
872+
853873
static bool is_full_stripe_write(struct stripe_head *sh)
854874
{
855875
BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
@@ -5992,8 +6012,8 @@ static enum stripe_result make_stripe_request(struct mddev *mddev,
59926012
pr_debug("raid456: %s, sector %llu logical %llu\n", __func__,
59936013
new_sector, logical_sector);
59946014

5995-
sh = raid5_get_active_stripe(conf, new_sector, previous,
5996-
(bi->bi_opf & REQ_RAHEAD), 0);
6015+
sh = __raid5_get_active_stripe(conf, ctx, new_sector, previous,
6016+
(bi->bi_opf & REQ_RAHEAD), 0);
59976017
if (unlikely(!sh)) {
59986018
/* cannot get stripe, just give-up */
59996019
bi->bi_status = BLK_STS_IOERR;

drivers/md/raid5.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ extern sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
812812
struct stripe_head *sh);
813813
extern struct stripe_head *
814814
raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
815-
int previous, int noblock, int noquiesce);
815+
bool previous, bool noblock, bool noquiesce);
816816
extern int raid5_calc_degraded(struct r5conf *conf);
817817
extern int r5c_journal_mode_set(struct mddev *mddev, int journal_mode);
818818
#endif

0 commit comments

Comments
 (0)