Skip to contentSkip to conversation

sched: flush plug in schedule_preempt_disabled() to prevent deadlock

PATCH In review Ming Lei 5 participants 12 messages May 12 – May 19

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 sched: flush plug in schedule_preempt_disabled() to prevent deadlock

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

Both reported deadlocks involve mutex/rwsem slowpaths, which are the primary callers of schedule_preempt_disabled() with non-running task state.

Fix by flushing the plug in schedule_preempt_disabled() while preemption is still disabled. This ensures the plug is empty before the preemption window opens.

Fixes 73c101011926 ("block: initial patch for on-stack per-task plugging")
---
 kernel/sched/core.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/sched/core.c +2 -0
index b8871449d3c6..c1efe110c54d 100644
@@ -7336,6 +7336,8 @@ asmlinkage __visible void __sched schedule_user(void)
  */
 void __sched schedule_preempt_disabled(void)
 {
+	if (!task_is_running(current))
+		blk_flush_plug(current->plug, true);
 	sched_preempt_enable_no_resched();
 	schedule();
 	preempt_disable();
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 23 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

Quoted 7 lines

Both reported deadlocks involve mutex/rwsem slowpaths, which are the primary callers of schedule_preempt_disabled() with non-running task state.

Fix by flushing the plug in schedule_preempt_disabled() while preemption is still disabled. This ensures the plug is empty before the preemption window opens.

How is this different from any path calling schedule()? That would be subject to exactly the same issue.

The patch cannot be correct.

maintainer
#2 lore raw
Quoted 28 lines · Peter Zijlstra
Quoted 27 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

Both reported deadlocks involve mutex/rwsem slowpaths, which are the primary callers of schedule_preempt_disabled() with non-running task state.

Quoted 8 lines
Quoted 3 lines

Fix by flushing the plug in schedule_preempt_disabled() while preemption is still disabled. This ensures the plug is empty before the preemption window opens.

How is this different from any path calling schedule()? That would be subject to exactly the same issue.

The patch cannot be correct.

Also, is there a reason io_schedule_prepare() has a blk_flush_plug() call?

io_schedule()
token = io_schedule_prepare()

    blk_flush_plug(current->plug, true);
  schedule()
    if (!task_is_running(tsk))
      sched_submit_work()
        blk_flush_plug(tsk->plug, true);

Why isn't the one in sched_submit_work() sufficient? This thing either needs a comment justifying its existence, or get removed.

author
#3 lore raw
Quoted 33 lines · Peter Zijlstra
Quoted 32 lines · Peter Zijlstra
Quoted 30 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

Both reported deadlocks involve mutex/rwsem slowpaths, which are the primary callers of schedule_preempt_disabled() with non-running task state.

Fix by flushing the plug in schedule_preempt_disabled() while preemption is still disabled. This ensures the plug is empty before the preemption window opens.

Quoted 7 lines
Quoted 4 lines

How is this different from any path calling schedule()? That would be subject to exactly the same issue.

The patch cannot be correct.

Also, is there a reason io_schedule_prepare() has a blk_flush_plug() call?

It is added in Tejun's "[PATCHSET RFC] sched, jbd2: mark sleeps on journal->j_checkpoint_mutex as iowait":

https://lore.kernel.org/all/[email protected]/#t

which fixes iowait accounting for ext4, meantime adds the model "io_schedule_prepare() + schedule() + io_schedule_finish()", which actually can avoid this kind issue easily because io_schedule_prepare() is called in task running state.

For this f2fs issue, maybe it can be addressed by adding rwsem io variant just like mutex_lock_io(), meantime iowait accounting is covered too.

Quoted 10 lines

io_schedule()
token = io_schedule_prepare()

    blk_flush_plug(current->plug, true);
  schedule()
    if (!task_is_running(tsk))
      sched_submit_work()
        blk_flush_plug(tsk->plug, true);

Why isn't the one in sched_submit_work() sufficient? This thing either needs a comment justifying its existence, or get removed.

This plug is originally added in 73c101011926 "block: initial patch for on-stack per-task plugging") and commit a237c1c5bc5d "block: let io_schedule() flush the plug inline" by Jens, when there isn't such preempt issue.

But it is moved out to sched_submit_work() latter in commit 9c40cef2b799 "sched: Move blk_schedule_flush_plug() out of __schedule()") by Thomas Gleixner, when this issue starts to become likely.

If io_schedule_prepare() can be called in every iowait context, looks blk_flush_plug() from sched_submit_work() may be removed.

Thanks,
Ming

maintainer
#4 lore raw
Quoted 8 lines · Ming Lei
Quoted 8 lines

io_schedule()
token = io_schedule_prepare()

    blk_flush_plug(current->plug, true);
  schedule()
    if (!task_is_running(tsk))
      sched_submit_work()
        blk_flush_plug(tsk->plug, true);
Quoted 5 lines
Quoted 2 lines

Why isn't the one in sched_submit_work() sufficient? This thing either needs a comment justifying its existence, or get removed.

If io_schedule_prepare() can be called in every iowait context, looks blk_flush_plug() from sched_submit_work() may be removed.

No, the other way around. I don't see the point of having the one in io_schedule_prepare(), since we'll hit the one in sched_submit_work().

maintainer
#5 lore raw
In reply to Ming Lei
Quoted 46 lines · Ming Lei
Quoted 33 lines · Peter Zijlstra
Quoted 32 lines · Peter Zijlstra
Quoted 30 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

Both reported deadlocks involve mutex/rwsem slowpaths, which are the primary callers of schedule_preempt_disabled() with non-running task state.

Fix by flushing the plug in schedule_preempt_disabled() while preemption is still disabled. This ensures the plug is empty before the preemption window opens.

Quoted 7 lines
Quoted 4 lines

How is this different from any path calling schedule()? That would be subject to exactly the same issue.

The patch cannot be correct.

Also, is there a reason io_schedule_prepare() has a blk_flush_plug() call?

It is added in Tejun's "[PATCHSET RFC] sched, jbd2: mark sleeps on journal->j_checkpoint_mutex as iowait":

https://lore.kernel.org/all/[email protected]/#t

Quoted 7 lines

which fixes iowait accounting for ext4, meantime adds the model "io_schedule_prepare() + schedule() + io_schedule_finish()", which actually can avoid this kind issue easily because io_schedule_prepare() is called in task running state.

For this f2fs issue, maybe it can be addressed by adding rwsem io variant just like mutex_lock_io(), meantime iowait accounting is covered too.

So personally I detest all of iowait, its an abomination. And I don't see how having an iowait specific version avoids any problem.

You can get preempted at any point before between getting the io started and blocking.

#6 lore raw
In reply to Ming Lei

Hello, Ming.

Quoted 18 lines · Ming Lei
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

My memory is hazy around io_schedule but the above reads really weird to me. A task, regardless of its current state stays on the runqueue when preempted, so the condition is temporary. As soon as the preempted task can get CPU, it should unwind the situation. That's not a deadlock. Is the problem that there can be preemption-induced delay in flushing the plugs?

Thanks.

Signature · 1 line
tejun
author
#7 lore raw
Quoted 29 lines · Tejun Heo

Hello, Ming.

Quoted 18 lines · Ming Lei
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

Quoted 5 lines

My memory is hazy around io_schedule but the above reads really weird to me. A task, regardless of its current state stays on the runqueue when preempted, so the condition is temporary. As soon as the preempted task can get CPU, it should unwind the situation. That's not a deadlock. Is the problem that there can be preemption-induced delay in flushing the plugs?

IMO, preempting a !TASK_RUNNING task can be thought as effective sleep, however block layer plug list isn't flushed before this 'sleep', which is absolutely fragile to cause deadlock.

Thanks,
Ming

maintainer
#8 lore raw
Quoted 30 lines · Ming Lei
Quoted 29 lines · Tejun Heo

Hello, Ming.

Quoted 25 lines · Ming Lei
Quoted 24 lines · Peter Zijlstra
Quoted 23 lines · Peter Zijlstra
Quoted 22 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

Quoted 7 lines
Quoted 5 lines

My memory is hazy around io_schedule but the above reads really weird to me. A task, regardless of its current state stays on the runqueue when preempted, so the condition is temporary. As soon as the preempted task can get CPU, it should unwind the situation. That's not a deadlock. Is the problem that there can be preemption-induced delay in flushing the plugs?

IMO, preempting a !TASK_RUNNING task can be thought as effective sleep,

No it cannot be. Preemption ignores task state.

author
#9 lore raw
Quoted 37 lines · Peter Zijlstra
Quoted 36 lines · Ming Lei
Quoted 34 lines · Tejun Heo

Hello, Ming.

Quoted 18 lines · Ming Lei
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

My memory is hazy around io_schedule but the above reads really weird to me. A task, regardless of its current state stays on the runqueue when preempted, so the condition is temporary. As soon as the preempted task can get CPU, it should unwind the situation. That's not a deadlock. Is the problem that there can be preemption-induced delay in flushing the plugs?

Quoted 3 lines
Quoted 1 line

IMO, preempting a !TASK_RUNNING task can be thought as effective sleep,

No it cannot be. Preemption ignores task state.

Yeah, I get similar conclusion too with AI's assistance.

But both two reports show that the preempted task aren't switched back for long enough time, can you share any idea for Michael & Xiaosen to investigate further from scheduler side?

https://lore.kernel.org/linux-block/[email protected]/

https://lore.kernel.org/linux-block/[email protected]/

Thanks,
Ming

#10 lore raw

https://lore.kernel.org/lkml/[email protected]/
The above change can resolve the deadlock I reported before by setting task's state to TASK_RUNNING before switching context.

There is the likely alternative fix below.
https://lore.kernel.org/lkml/[email protected]/

Regards,
Xiaosen

Quoted 55 lines · Ming Lei
Quoted 37 lines · Peter Zijlstra
Quoted 36 lines · Ming Lei
Quoted 34 lines · Tejun Heo

Hello, Ming.

Quoted 18 lines · Ming Lei
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

My memory is hazy around io_schedule but the above reads really weird to me. A task, regardless of its current state stays on the runqueue when preempted, so the condition is temporary. As soon as the preempted task can get CPU, it should unwind the situation. That's not a deadlock. Is the problem that there can be preemption-induced delay in flushing the plugs?

Quoted 3 lines
Quoted 1 line

IMO, preempting a !TASK_RUNNING task can be thought as effective sleep,

No it cannot be. Preemption ignores task state.

Yeah, I get similar conclusion too with AI's assistance.

But both two reports show that the preempted task aren't switched back for long enough time, can you share any idea for Michael & Xiaosen to investigate further from scheduler side?

https://lore.kernel.org/linux-block/[email protected]/

https://lore.kernel.org/linux-block/[email protected]/

Thanks,
Ming

#11 lore raw

The patch you mentioned also solves my problem; I've verified and tested it on my platform.

Quoted 66 lines · Xiaosen

https://lore.kernel.org/lkml/[email protected]/
The above change can resolve the deadlock I reported before by setting task's state to TASK_RUNNING before switching context.

There is the likely alternative fix below.
https://lore.kernel.org/lkml/[email protected]/

Regards,
Xiaosen

Quoted 55 lines · Ming Lei
Quoted 37 lines · Peter Zijlstra
Quoted 36 lines · Ming Lei
Quoted 34 lines · Tejun Heo

Hello, Ming.

Quoted 18 lines · Ming Lei
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Ming Lei

On preemptible kernels, a deadlock can occur when a task with plugged IO calls schedule_preempt_disabled():

schedule_preempt_disabled()
sched_preempt_enable_no_resched() // preemption now enabled
schedule() // <-- preemption can happen here
sched_submit_work()
blk_flush_plug()

After sched_preempt_enable_no_resched() re-enables preemption, the task can be preempted (e.g., by a higher-priority RT task) before reaching blk_flush_plug() in sched_submit_work(). Since the task's state is already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), requests in current->plug remain unflushed for an unbounded time.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

If another task depends on those plugged requests to make progress (e.g., to release a lock the sleeping task needs), a deadlock results:

- Task A (writeback worker): holds plugged IO, preempted before flushing, stuck on run queue behind higher-priority work
- Task B: waiting for IO completion from Task A's plug, holds a lock that Task A needs to be woken up

My memory is hazy around io_schedule but the above reads really weird to me. A task, regardless of its current state stays on the runqueue when preempted, so the condition is temporary. As soon as the preempted task can get CPU, it should unwind the situation. That's not a deadlock. Is the problem that there can be preemption-induced delay in flushing the plugs?

Quoted 3 lines
Quoted 1 line

IMO, preempting a !TASK_RUNNING task can be thought as effective sleep,

No it cannot be. Preemption ignores task state.

Yeah, I get similar conclusion too with AI's assistance.

But both two reports show that the preempted task aren't switched back for long enough time, can you share any idea for Michael & Xiaosen to investigate further from scheduler side?

https://lore.kernel.org/linux-block/[email protected]/

https://lore.kernel.org/linux-block/[email protected]/

Thanks,
Ming

Signature · 2 lines
Regards,
Michael Wu