diff options
| author | Mikulas Patocka <mpatocka@redhat.com> | 2017-02-15 11:26:10 -0500 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2017-03-18 07:14:34 -0400 |
| commit | 21582cd0b64ca464084cb872b3d348065b0b54af (patch) | |
| tree | f1791e88619f84747517bf383c6504a8b60b2b0a /drivers/md | |
| parent | d29e6215e5ab9a6b64b5cee57598ae8ec05666a1 (diff) | |
dm: flush queued bios when process blocks to avoid deadlock
commit d67a5f4b5947aba4bfe9a80a2b86079c215ca755 upstream.
Commit df2cb6daa4 ("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: df2cb6daa4 ("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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/md')
| -rw-r--r-- | drivers/md/dm.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/drivers/md/dm.c b/drivers/md/dm.c index ef7bf1dd6900..628ba001bb3c 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c | |||
| @@ -972,10 +972,61 @@ void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) | |||
| 972 | } | 972 | } |
| 973 | EXPORT_SYMBOL_GPL(dm_accept_partial_bio); | 973 | EXPORT_SYMBOL_GPL(dm_accept_partial_bio); |
| 974 | 974 | ||
| 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, ¤t->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 | |||
| 975 | static void __map_bio(struct dm_target_io *tio) | 1025 | static void __map_bio(struct dm_target_io *tio) |
| 976 | { | 1026 | { |
| 977 | int r; | 1027 | int r; |
| 978 | sector_t sector; | 1028 | sector_t sector; |
| 1029 | struct dm_offload o; | ||
| 979 | struct bio *clone = &tio->clone; | 1030 | struct bio *clone = &tio->clone; |
| 980 | struct dm_target *ti = tio->ti; | 1031 | struct dm_target *ti = tio->ti; |
| 981 | 1032 | ||
| @@ -988,7 +1039,11 @@ static void __map_bio(struct dm_target_io *tio) | |||
| 988 | */ | 1039 | */ |
| 989 | atomic_inc(&tio->io->io_count); | 1040 | atomic_inc(&tio->io->io_count); |
| 990 | sector = clone->bi_iter.bi_sector; | 1041 | sector = clone->bi_iter.bi_sector; |
| 1042 | |||
| 1043 | dm_offload_start(&o); | ||
| 991 | r = ti->type->map(ti, clone); | 1044 | r = ti->type->map(ti, clone); |
| 1045 | dm_offload_end(&o); | ||
| 1046 | |||
| 992 | if (r == DM_MAPIO_REMAPPED) { | 1047 | if (r == DM_MAPIO_REMAPPED) { |
| 993 | /* the bio has been remapped so dispatch it */ | 1048 | /* the bio has been remapped so dispatch it */ |
| 994 | 1049 | ||
