Skip to content

Commit c3b6550

Browse files
Mikulas Patockajnettlet
authored andcommitted
locking/rt-mutex: fix deadlock in device mapper / block-IO
When some block device driver creates a bio and submits it to another block device driver, the bio is added to current->bio_list (in order to avoid unbounded recursion). However, this queuing of bios can cause deadlocks, in order to avoid them, device mapper registers a function flush_current_bio_list. This function is called when device mapper driver blocks. It redirects bios queued on current->bio_list to helper workqueues, so that these bios can proceed even if the driver is blocked. The problem with CONFIG_PREEMPT_RT_FULL is that when the device mapper driver blocks, it won't call flush_current_bio_list (because tsk_is_pi_blocked returns true in sched_submit_work), so deadlocks in block device stack can happen. Note that we can't call blk_schedule_flush_plug if tsk_is_pi_blocked returns true - that would cause BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on)) in task_blocks_on_rt_mutex when flush_current_bio_list attempts to take a spinlock. So the proper fix is to call blk_schedule_flush_plug in rt_mutex_fastlock, when fast acquire failed and when the task is about to block. CC: [email protected] [bigeasy: The deadlock is not device-mapper specific, it can also occur in plain EXT4] Signed-off-by: Mikulas Patocka <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 81af57b commit c3b6550

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

kernel/locking/rtmutex.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/sched/deadline.h>
2323
#include <linux/timer.h>
2424
#include <linux/ww_mutex.h>
25+
#include <linux/blkdev.h>
2526

2627
#include "rtmutex_common.h"
2728

@@ -1968,6 +1969,15 @@ rt_mutex_fastlock(struct rt_mutex *lock, int state,
19681969
if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
19691970
return 0;
19701971

1972+
/*
1973+
* If rt_mutex blocks, the function sched_submit_work will not call
1974+
* blk_schedule_flush_plug (because tsk_is_pi_blocked would be true).
1975+
* We must call blk_schedule_flush_plug here, if we don't call it,
1976+
* a deadlock in device mapper may happen.
1977+
*/
1978+
if (unlikely(blk_needs_flush_plug(current)))
1979+
blk_schedule_flush_plug(current);
1980+
19711981
return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK, ww_ctx);
19721982
}
19731983

@@ -1985,6 +1995,9 @@ rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
19851995
likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
19861996
return 0;
19871997

1998+
if (unlikely(blk_needs_flush_plug(current)))
1999+
blk_schedule_flush_plug(current);
2000+
19882001
return slowfn(lock, state, timeout, chwalk, ww_ctx);
19892002
}
19902003

0 commit comments

Comments
 (0)