From: Yu Kuai <[email protected]> DM allocates normal NOWAIT target clones with GFP_NOWAIT. Targets that set needs_bio_set_dev can therefore make alloc_tio() associate blkcg state from a non-blocking allocation path, which may sleep while creating a missing blkg after blkg lookup is protected by q->blkcg_mutex.
Set the default bdev without blkcg association first, then associate blkcg with nowait=true for non-blocking allocations. If a blkg would need creating, fail the NOWAIT allocation with BLK_STS_AGAIN. Targets that advertise DM_TARGET_NOWAIT may also remap bios in their map functions. Those remaps update only the bdev for NOWAIT bios, then DM submission clones the original bio's blkg association with nowait=true before lower submission. If that would need to sleep, complete the clone with BLK_STS_AGAIN. Signed-off-by: Yu Kuai <[email protected]> --- drivers/md/dm-linear.c | 2 +- drivers/md/dm-stripe.c | 6 +++--- drivers/md/dm-switch.c | 2 +- drivers/md/dm-unstripe.c | 2 +- drivers/md/dm.c | 28 +++++++++++++++++++++++++--- include/linux/device-mapper.h | 8 ++++++++ 6 files changed, 39 insertions(+), 9 deletions(-) diff --git a/drivers/md/dm-linear.c b/drivers/md/dm-linear.c index 38c17846deb0..f75a372acd20 100644 --- a/drivers/md/dm-linear.c +++ b/drivers/md/dm-linear.c @@ -90,7 +90,7 @@ int linear_map(struct dm_target *ti, struct bio *bio) { struct linear_c *lc = ti->private; - bio_set_dev(bio, lc->dev->bdev); + dm_bio_set_dev(bio, lc->dev->bdev); bio->bi_iter.bi_sector = linear_map_sector(ti, bio->bi_iter.bi_sector); return DM_MAPIO_REMAPPED; diff --git a/drivers/md/dm-stripe.c b/drivers/md/dm-stripe.c index 750865fd3ae7..73f9483a3e8a 100644 --- a/drivers/md/dm-stripe.c +++ b/drivers/md/dm-stripe.c @@ -257,7 +257,7 @@ static int stripe_map_range(struct stripe_c *sc, struct bio *bio, stripe_map_range_sector(sc, bio_end_sector(bio), target_stripe, &end); if (begin < end) { - bio_set_dev(bio, sc->stripe[target_stripe].dev->bdev); + dm_bio_set_dev(bio, sc->stripe[target_stripe].dev->bdev); bio->bi_iter.bi_sector = begin + sc->stripe[target_stripe].physical_start; bio->bi_iter.bi_size = to_bytes(end - begin); @@ -278,7 +278,7 @@ int stripe_map(struct dm_target *ti, struct bio *bio) if (bio->bi_opf & REQ_PREFLUSH) { target_bio_nr = dm_bio_get_target_bio_nr(bio); BUG_ON(target_bio_nr >= sc->stripes); - bio_set_dev(bio, sc->stripe[target_bio_nr].dev->bdev); + dm_bio_set_dev(bio, sc->stripe[target_bio_nr].dev->bdev); return DM_MAPIO_REMAPPED; } if (unlikely(bio_op(bio) == REQ_OP_DISCARD) || @@ -293,7 +293,7 @@ int stripe_map(struct dm_target *ti, struct bio *bio) &stripe, &bio->bi_iter.bi_sector); bio->bi_iter.bi_sector += sc->stripe[stripe].physical_start; - bio_set_dev(bio, sc->stripe[stripe].dev->bdev); + dm_bio_set_dev(bio, sc->stripe[stripe].dev->bdev); return DM_MAPIO_REMAPPED; } diff --git a/drivers/md/dm-switch.c b/drivers/md/dm-switch.c index 5952f02de1e6..9eea6c263eed 100644 --- a/drivers/md/dm-switch.c +++ b/drivers/md/dm-switch.c @@ -323,7 +323,7 @@ static int switch_map(struct dm_target *ti, struct bio *bio) sector_t offset = dm_target_offset(ti, bio->bi_iter.bi_sector); unsigned int path_nr = switch_get_path_nr(sctx, offset); - bio_set_dev(bio, sctx->path_list[path_nr].dmdev->bdev); + dm_bio_set_dev(bio, sctx->path_list[path_nr].dmdev->bdev); bio->bi_iter.bi_sector = sctx->path_list[path_nr].start + offset; return DM_MAPIO_REMAPPED; diff --git a/drivers/md/dm-unstripe.c b/drivers/md/dm-unstripe.c index bfcbe6bfa71a..900b1ac88bc8 100644 --- a/drivers/md/dm-unstripe.c +++ b/drivers/md/dm-unstripe.c @@ -136,7 +136,7 @@ static int unstripe_map(struct dm_target *ti, struct bio *bio) { struct unstripe_c *uc = ti->private; - bio_set_dev(bio, uc->dev->bdev); + dm_bio_set_dev(bio, uc->dev->bdev); bio->bi_iter.bi_sector = map_to_core(ti, bio) + uc->physical_start; return DM_MAPIO_REMAPPED; diff --git a/drivers/md/dm.c b/drivers/md/dm.c index c54636235ffe..6dde3c699122 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -610,6 +610,8 @@ static void free_io(struct dm_io *io) bio_put(&io->tio.clone); } +static void free_tio(struct bio *clone); + static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti, unsigned int target_bio_nr, unsigned int *len, gfp_t gfp_mask) { @@ -644,8 +646,12 @@ static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti, /* Set default bdev, but target must bio_set_dev() before issuing IO */ clone->bi_bdev = md->disk->part0; - if (likely(ti != NULL) && unlikely(ti->needs_bio_set_dev)) - bio_set_dev(clone, md->disk->part0); + if (likely(ti != NULL) && unlikely(ti->needs_bio_set_dev)) { + bio_set_dev_no_blkg(clone, md->disk->part0); + if (!bio_associate_blkg(clone, + !gfpflags_allow_blocking(gfp_mask))) + goto fail; + } if (len) { clone->bi_iter.bi_size = to_bytes(*len); @@ -654,6 +660,14 @@ static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti, } return clone; + +fail: + if (dm_tio_flagged(clone_to_tio(clone), DM_TIO_INSIDE_DM_IO)) { + clone->bi_bdev = NULL; + clone_to_tio(clone)->io = NULL; + } + free_tio(clone); + return NULL; } static void free_tio(struct bio *clone) @@ -1364,7 +1378,15 @@ void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone) if (!tgt_clone) tgt_clone = clone; - bio_clone_blkg_association(tgt_clone, io->orig_bio, false); + if (tgt_clone->bi_opf & REQ_NOWAIT) { + if (!bio_clone_blkg_association(tgt_clone, io->orig_bio, true)) { + tgt_clone->bi_status = BLK_STS_AGAIN; + tgt_clone->bi_end_io(tgt_clone); + return; + } + } else { + bio_clone_blkg_association(tgt_clone, io->orig_bio, false); + } /* * Account io->origin_bio to DM dev on behalf of target diff --git a/include/linux/device-mapper.h b/include/linux/device-mapper.h index cd4faaf5d427..ca1e1cfee74f 100644 --- a/include/linux/device-mapper.h +++ b/include/linux/device-mapper.h @@ -713,6 +713,14 @@ module_exit(dm_##name##_exit) #define DM_MAPIO_DELAY_REQUEUE DM_ENDIO_DELAY_REQUEUE #define DM_MAPIO_KILL 4 +static inline void dm_bio_set_dev(struct bio *bio, struct block_device *bdev) +{ + if (bio->bi_opf & REQ_NOWAIT) + bio_set_dev_no_blkg(bio, bdev); + else + bio_set_dev(bio, bdev); +} + #define dm_sector_div64(x, y)( \ { \ u64 _res; \ -- 2.51.0
