Skip to content

Commit cd8ad4d

Browse files
Mikulas Patockagregkh
authored andcommitted
dm: flush queued bios when process blocks to avoid deadlock
commit d67a5f4 upstream. 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 <[email protected]> Signed-off-by: Mike Snitzer <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 66dd58f commit cd8ad4d

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

drivers/md/dm.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,11 +1467,62 @@ void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
14671467
}
14681468
EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
14691469

1470+
/*
1471+
* Flush current->bio_list when the target map method blocks.
1472+
* This fixes deadlocks in snapshot and possibly in other targets.
1473+
*/
1474+
struct dm_offload {
1475+
struct blk_plug plug;
1476+
struct blk_plug_cb cb;
1477+
};
1478+
1479+
static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
1480+
{
1481+
struct dm_offload *o = container_of(cb, struct dm_offload, cb);
1482+
struct bio_list list;
1483+
struct bio *bio;
1484+
1485+
INIT_LIST_HEAD(&o->cb.list);
1486+
1487+
if (unlikely(!current->bio_list))
1488+
return;
1489+
1490+
list = *current->bio_list;
1491+
bio_list_init(current->bio_list);
1492+
1493+
while ((bio = bio_list_pop(&list))) {
1494+
struct bio_set *bs = bio->bi_pool;
1495+
if (unlikely(!bs) || bs == fs_bio_set) {
1496+
bio_list_add(current->bio_list, bio);
1497+
continue;
1498+
}
1499+
1500+
spin_lock(&bs->rescue_lock);
1501+
bio_list_add(&bs->rescue_list, bio);
1502+
queue_work(bs->rescue_workqueue, &bs->rescue_work);
1503+
spin_unlock(&bs->rescue_lock);
1504+
}
1505+
}
1506+
1507+
static void dm_offload_start(struct dm_offload *o)
1508+
{
1509+
blk_start_plug(&o->plug);
1510+
o->cb.callback = flush_current_bio_list;
1511+
list_add(&o->cb.list, &current->plug->cb_list);
1512+
}
1513+
1514+
static void dm_offload_end(struct dm_offload *o)
1515+
{
1516+
list_del(&o->cb.list);
1517+
blk_finish_plug(&o->plug);
1518+
}
1519+
14701520
static void __map_bio(struct dm_target_io *tio)
14711521
{
14721522
int r;
14731523
sector_t sector;
14741524
struct mapped_device *md;
1525+
struct dm_offload o;
14751526
struct bio *clone = &tio->clone;
14761527
struct dm_target *ti = tio->ti;
14771528

@@ -1484,7 +1535,11 @@ static void __map_bio(struct dm_target_io *tio)
14841535
*/
14851536
atomic_inc(&tio->io->io_count);
14861537
sector = clone->bi_iter.bi_sector;
1538+
1539+
dm_offload_start(&o);
14871540
r = ti->type->map(ti, clone);
1541+
dm_offload_end(&o);
1542+
14881543
if (r == DM_MAPIO_REMAPPED) {
14891544
/* the bio has been remapped so dispatch it */
14901545

0 commit comments

Comments
 (0)