Skip to content

Commit bfe28af

Browse files
pmladekSasha Levin
authored andcommitted
kthread_worker: split code for canceling the delayed work timer
commit 34b3d53 upstream. Patch series "kthread_worker: Fix race between kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync()". This patchset fixes the race between kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync() including proper return value handling. This patch (of 2): Simple code refactoring as a preparation step for fixing a race between kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync(). It does not modify the existing behavior. Link: https://lkml.kernel.org/r/20210610133051.15337-2-pmladek@suse.com Signed-off-by: Petr Mladek <pmladek@suse.com> Cc: <jenhaochen@google.com> Cc: Martin Liu <liumartin@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 02c303f commit bfe28af

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

‎kernel/kthread.c‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,33 @@ void kthread_flush_work(struct kthread_work *work)
10431043
}
10441044
EXPORT_SYMBOL_GPL(kthread_flush_work);
10451045

1046+
/*
1047+
* Make sure that the timer is neither set nor running and could
1048+
* not manipulate the work list_head any longer.
1049+
*
1050+
* The function is called under worker->lock. The lock is temporary
1051+
* released but the timer can't be set again in the meantime.
1052+
*/
1053+
static void kthread_cancel_delayed_work_timer(struct kthread_work *work,
1054+
unsigned long *flags)
1055+
{
1056+
struct kthread_delayed_work *dwork =
1057+
container_of(work, struct kthread_delayed_work, work);
1058+
struct kthread_worker *worker = work->worker;
1059+
1060+
/*
1061+
* del_timer_sync() must be called to make sure that the timer
1062+
* callback is not running. The lock must be temporary released
1063+
* to avoid a deadlock with the callback. In the meantime,
1064+
* any queuing is blocked by setting the canceling counter.
1065+
*/
1066+
work->canceling++;
1067+
raw_spin_unlock_irqrestore(&worker->lock, *flags);
1068+
del_timer_sync(&dwork->timer);
1069+
raw_spin_lock_irqsave(&worker->lock, *flags);
1070+
work->canceling--;
1071+
}
1072+
10461073
/*
10471074
* This function removes the work from the worker queue. Also it makes sure
10481075
* that it won't get queued later via the delayed work's timer.
@@ -1057,23 +1084,8 @@ static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork,
10571084
unsigned long *flags)
10581085
{
10591086
/* Try to cancel the timer if exists. */
1060-
if (is_dwork) {
1061-
struct kthread_delayed_work *dwork =
1062-
container_of(work, struct kthread_delayed_work, work);
1063-
struct kthread_worker *worker = work->worker;
1064-
1065-
/*
1066-
* del_timer_sync() must be called to make sure that the timer
1067-
* callback is not running. The lock must be temporary released
1068-
* to avoid a deadlock with the callback. In the meantime,
1069-
* any queuing is blocked by setting the canceling counter.
1070-
*/
1071-
work->canceling++;
1072-
raw_spin_unlock_irqrestore(&worker->lock, *flags);
1073-
del_timer_sync(&dwork->timer);
1074-
raw_spin_lock_irqsave(&worker->lock, *flags);
1075-
work->canceling--;
1076-
}
1087+
if (is_dwork)
1088+
kthread_cancel_delayed_work_timer(work, flags);
10771089

10781090
/*
10791091
* Try to remove the work from a worker list. It might either

0 commit comments

Comments
 (0)