Return-Path: Received: from mail-bl2nam02on0137.outbound.protection.outlook.com ([104.47.38.137]:48134 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753956AbeARVAQ (ORCPT ); Thu, 18 Jan 2018 16:00:16 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Mikulas Patocka , Mike Snitzer , Sasha Levin Subject: [added to the 4.1 stable tree] dm: flush queued bios when process blocks to avoid deadlock Date: Thu, 18 Jan 2018 20:59:40 +0000 Message-ID: <20180118205908.3220-32-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Mikulas Patocka This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit d67a5f4b5947aba4bfe9a80a2b86079c215ca755 ] 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=3D1267650 Depends-on: df2cb6daa4 ("block: Avoid deadlocks with bio allocation by stac= king drivers") Signed-off-by: Mikulas Patocka Signed-off-by: Mike Snitzer Signed-off-by: Sasha Levin --- drivers/md/dm.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++= ++ 1 file changed, 55 insertions(+) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index d192ab2ed17c..c2a13fd9e0ca 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -1435,11 +1435,62 @@ void dm_accept_partial_bio(struct bio *bio, unsigne= d n_sectors) } EXPORT_SYMBOL_GPL(dm_accept_partial_bio); =20 +/* + * Flush current->bio_list when the target map method blocks. + * This fixes deadlocks in snapshot and possibly in other targets. + */ +struct dm_offload { + struct blk_plug plug; + struct blk_plug_cb cb; +}; + +static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_sched= ule) +{ + struct dm_offload *o =3D container_of(cb, struct dm_offload, cb); + struct bio_list list; + struct bio *bio; + + INIT_LIST_HEAD(&o->cb.list); + + if (unlikely(!current->bio_list)) + return; + + list =3D *current->bio_list; + bio_list_init(current->bio_list); + + while ((bio =3D bio_list_pop(&list))) { + struct bio_set *bs =3D bio->bi_pool; + if (unlikely(!bs) || bs =3D=3D fs_bio_set) { + bio_list_add(current->bio_list, bio); + continue; + } + + spin_lock(&bs->rescue_lock); + bio_list_add(&bs->rescue_list, bio); + queue_work(bs->rescue_workqueue, &bs->rescue_work); + spin_unlock(&bs->rescue_lock); + } +} + +static void dm_offload_start(struct dm_offload *o) +{ + blk_start_plug(&o->plug); + o->cb.callback =3D flush_current_bio_list; + list_add(&o->cb.list, ¤t->plug->cb_list); +} + +static void dm_offload_end(struct dm_offload *o) +{ + list_del(&o->cb.list); + blk_finish_plug(&o->plug); +} + static void __map_bio(struct dm_target_io *tio) { int r; sector_t sector; struct mapped_device *md; + struct dm_offload o; struct bio *clone =3D &tio->clone; struct dm_target *ti =3D tio->ti; =20 @@ -1452,7 +1503,11 @@ static void __map_bio(struct dm_target_io *tio) */ atomic_inc(&tio->io->io_count); sector =3D clone->bi_iter.bi_sector; + + dm_offload_start(&o); r =3D ti->type->map(ti, clone); + dm_offload_end(&o); + if (r =3D=3D DM_MAPIO_REMAPPED) { /* the bio has been remapped so dispatch it */ =20 --=20 2.11.0