Skip to content

Commit 2b35a4e

Browse files
pmladekSasha Levin
authored andcommitted
kthread: prevent deadlock when kthread_mod_delayed_work() races with kthread_cancel_delayed_work_sync()
commit 5fa5434 upstream. The system might hang with the following backtrace: schedule+0x80/0x100 schedule_timeout+0x48/0x138 wait_for_common+0xa4/0x134 wait_for_completion+0x1c/0x2c kthread_flush_work+0x114/0x1cc kthread_cancel_work_sync.llvm.16514401384283632983+0xe8/0x144 kthread_cancel_delayed_work_sync+0x18/0x2c xxxx_pm_notify+0xb0/0xd8 blocking_notifier_call_chain_robust+0x80/0x194 pm_notifier_call_chain_robust+0x28/0x4c suspend_prepare+0x40/0x260 enter_state+0x80/0x3f4 pm_suspend+0x60/0xdc state_store+0x108/0x144 kobj_attr_store+0x38/0x88 sysfs_kf_write+0x64/0xc0 kernfs_fop_write_iter+0x108/0x1d0 vfs_write+0x2f4/0x368 ksys_write+0x7c/0xec It is caused by the following race between kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync(): CPU0 CPU1 Context: Thread A Context: Thread B kthread_mod_delayed_work() spin_lock() __kthread_cancel_work() spin_unlock() del_timer_sync() kthread_cancel_delayed_work_sync() spin_lock() __kthread_cancel_work() spin_unlock() del_timer_sync() spin_lock() work->canceling++ spin_unlock spin_lock() queue_delayed_work() // dwork is put into the worker->delayed_work_list spin_unlock() kthread_flush_work() // flush_work is put at the tail of the dwork wait_for_completion() Context: IRQ kthread_delayed_work_timer_fn() spin_lock() list_del_init(&work->node); spin_unlock() BANG: flush_work is not longer linked and will never get proceed. The problem is that kthread_mod_delayed_work() checks work->canceling flag before canceling the timer. A simple solution is to (re)check work->canceling after __kthread_cancel_work(). But then it is not clear what should be returned when __kthread_cancel_work() removed the work from the queue (list) and it can't queue it again with the new @delay. The return value might be used for reference counting. The caller has to know whether a new work has been queued or an existing one was replaced. The proper solution is that kthread_mod_delayed_work() will remove the work from the queue (list) _only_ when work->canceling is not set. The flag must be checked after the timer is stopped and the remaining operations can be done under worker->lock. Note that kthread_mod_delayed_work() could remove the timer and then bail out. It is fine. The other canceling caller needs to cancel the timer as well. The important thing is that the queue (list) manipulation is done atomically under worker->lock. Link: https://lkml.kernel.org/r/20210610133051.15337-3-pmladek@suse.com Fixes: 9a6b06c ("kthread: allow to modify delayed kthread work") Signed-off-by: Petr Mladek <pmladek@suse.com> Reported-by: Martin Liu <liumartin@google.com> Cc: <jenhaochen@google.com> Cc: Minchan Kim <minchan@google.com> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Nick Desaulniers <ndesaulniers@google.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Tejun Heo <tj@kernel.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent bfe28af commit 2b35a4e

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

‎kernel/kthread.c‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,22 +1071,20 @@ static void kthread_cancel_delayed_work_timer(struct kthread_work *work,
10711071
}
10721072

10731073
/*
1074-
* This function removes the work from the worker queue. Also it makes sure
1075-
* that it won't get queued later via the delayed work's timer.
1074+
* This function removes the work from the worker queue.
1075+
*
1076+
* It is called under worker->lock. The caller must make sure that
1077+
* the timer used by delayed work is not running, e.g. by calling
1078+
* kthread_cancel_delayed_work_timer().
10761079
*
10771080
* The work might still be in use when this function finishes. See the
10781081
* current_work proceed by the worker.
10791082
*
10801083
* Return: %true if @work was pending and successfully canceled,
10811084
* %false if @work was not pending
10821085
*/
1083-
static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork,
1084-
unsigned long *flags)
1086+
static bool __kthread_cancel_work(struct kthread_work *work)
10851087
{
1086-
/* Try to cancel the timer if exists. */
1087-
if (is_dwork)
1088-
kthread_cancel_delayed_work_timer(work, flags);
1089-
10901088
/*
10911089
* Try to remove the work from a worker list. It might either
10921090
* be from worker->work_list or from worker->delayed_work_list.
@@ -1139,11 +1137,23 @@ bool kthread_mod_delayed_work(struct kthread_worker *worker,
11391137
/* Work must not be used with >1 worker, see kthread_queue_work() */
11401138
WARN_ON_ONCE(work->worker != worker);
11411139

1142-
/* Do not fight with another command that is canceling this work. */
1140+
/*
1141+
* Temporary cancel the work but do not fight with another command
1142+
* that is canceling the work as well.
1143+
*
1144+
* It is a bit tricky because of possible races with another
1145+
* mod_delayed_work() and cancel_delayed_work() callers.
1146+
*
1147+
* The timer must be canceled first because worker->lock is released
1148+
* when doing so. But the work can be removed from the queue (list)
1149+
* only when it can be queued again so that the return value can
1150+
* be used for reference counting.
1151+
*/
1152+
kthread_cancel_delayed_work_timer(work, &flags);
11431153
if (work->canceling)
11441154
goto out;
1155+
ret = __kthread_cancel_work(work);
11451156

1146-
ret = __kthread_cancel_work(work, true, &flags);
11471157
fast_queue:
11481158
__kthread_queue_delayed_work(worker, dwork, delay);
11491159
out:
@@ -1165,7 +1175,10 @@ static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork)
11651175
/* Work must not be used with >1 worker, see kthread_queue_work(). */
11661176
WARN_ON_ONCE(work->worker != worker);
11671177

1168-
ret = __kthread_cancel_work(work, is_dwork, &flags);
1178+
if (is_dwork)
1179+
kthread_cancel_delayed_work_timer(work, &flags);
1180+
1181+
ret = __kthread_cancel_work(work);
11691182

11701183
if (worker->current_work != work)
11711184
goto out_fast;

0 commit comments

Comments
 (0)