Skip to content

Commit d67a5f4

Browse files
Mikulas Patockasnitm
authored andcommitted
dm: flush queued bios when process blocks to avoid deadlock
Commit df2cb6d ("block: Avoid deadlocks with bio allocation by stacking drivers") created a workqueue for every bio set and code in bio_alloc_bioset() that tries to resolve some low-memory deadlocks by redirecting bios queued on current->bio_list to the workqueue if the system is low on memory. However other deadlocks (see below **) may happen, without any low memory condition, because generic_make_request is queuing bios to current->bio_list (rather than submitting them). ** the related dm-snapshot deadlock is detailed here: https://www.redhat.com/archives/dm-devel/2016-July/msg00065.html Fix this deadlock by redirecting any bios on current->bio_list to the bio_set's rescue workqueue on every schedule() call. Consequently, when the process blocks on a mutex, the bios queued on current->bio_list are dispatched to independent workqueus and they can complete without waiting for the mutex to be available. The structure blk_plug contains an entry cb_list and this list can contain arbitrary callback functions that are called when the process blocks. To implement this fix DM (ab)uses the onstack plug's cb_list interface to get its flush_current_bio_list() called at schedule() time. This fixes the snapshot deadlock - if the map method blocks, flush_current_bio_list() will be called and it redirects bios waiting on current->bio_list to appropriate workqueues. Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1267650 Depends-on: df2cb6d ("block: Avoid deadlocks with bio allocation by stacking drivers") Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Mike Snitzer <snitzer@redhat.com>
1 parent 37a098e commit d67a5f4

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

‎drivers/md/dm.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,61 @@ void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
972972
}
973973
EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
974974

975+
/*
976+
* Flush current->bio_list when the target map method blocks.
977+
* This fixes deadlocks in snapshot and possibly in other targets.
978+
*/
979+
struct dm_offload {
980+
struct blk_plug plug;
981+
struct blk_plug_cb cb;
982+
};
983+
984+
static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
985+
{
986+
struct dm_offload *o = container_of(cb, struct dm_offload, cb);
987+
struct bio_list list;
988+
struct bio *bio;
989+
990+
INIT_LIST_HEAD(&o->cb.list);
991+
992+
if (unlikely(!current->bio_list))
993+
return;
994+
995+
list = *current->bio_list;
996+
bio_list_init(current->bio_list);
997+
998+
while ((bio = bio_list_pop(&list))) {
999+
struct bio_set *bs = bio->bi_pool;
1000+
if (unlikely(!bs) || bs == fs_bio_set) {
1001+
bio_list_add(current->bio_list, bio);
1002+
continue;
1003+
}
1004+
1005+
spin_lock(&bs->rescue_lock);
1006+
bio_list_add(&bs->rescue_list, bio);
1007+
queue_work(bs->rescue_workqueue, &bs->rescue_work);
1008+
spin_unlock(&bs->rescue_lock);
1009+
}
1010+
}
1011+
1012+
static void dm_offload_start(struct dm_offload *o)
1013+
{
1014+
blk_start_plug(&o->plug);
1015+
o->cb.callback = flush_current_bio_list;
1016+
list_add(&o->cb.list, &current->plug->cb_list);
1017+
}
1018+
1019+
static void dm_offload_end(struct dm_offload *o)
1020+
{
1021+
list_del(&o->cb.list);
1022+
blk_finish_plug(&o->plug);
1023+
}
1024+
9751025
static void __map_bio(struct dm_target_io *tio)
9761026
{
9771027
int r;
9781028
sector_t sector;
1029+
struct dm_offload o;
9791030
struct bio *clone = &tio->clone;
9801031
struct dm_target *ti = tio->ti;
9811032

@@ -988,7 +1039,11 @@ static void __map_bio(struct dm_target_io *tio)
9881039
*/
9891040
atomic_inc(&tio->io->io_count);
9901041
sector = clone->bi_iter.bi_sector;
1042+
1043+
dm_offload_start(&o);
9911044
r = ti->type->map(ti, clone);
1045+
dm_offload_end(&o);
1046+
9921047
if (r == DM_MAPIO_REMAPPED) {
9931048
/* the bio has been remapped so dispatch it */
9941049

0 commit comments

Comments
 (0)