Skip to content

Commit fc12d8f

Browse files
pmladekgregkh
authored andcommitted
kthread_worker: fix return value when kthread_mod_delayed_work() races with kthread_cancel_delayed_work_sync()
[ Upstream commit d71ba16 ] kthread_mod_delayed_work() might race with kthread_cancel_delayed_work_sync() or another kthread_mod_delayed_work() call. The function lets the other operation win when it sees work->canceling counter set. And it returns @false. But it should return @true as it is done by the related workqueue API, see mod_delayed_work_on(). The reason is that the return value might be used for reference counting. It has to distinguish the case when the number of queued works has changed or stayed the same. The change is safe. kthread_mod_delayed_work() return value is not checked anywhere at the moment. Link: https://lore.kernel.org/r/20210521163526.GA17916@redhat.com Link: https://lkml.kernel.org/r/20210610133051.15337-4-pmladek@suse.com Signed-off-by: Petr Mladek <pmladek@suse.com> Reported-by: Oleg Nesterov <oleg@redhat.com> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Nick Desaulniers <ndesaulniers@google.com> Cc: Tejun Heo <tj@kernel.org> Cc: Minchan Kim <minchan@google.com> Cc: <jenhaochen@google.com> Cc: Martin Liu <liumartin@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 1208f10 commit fc12d8f

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

‎kernel/kthread.c‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,14 +1107,14 @@ static bool __kthread_cancel_work(struct kthread_work *work)
11071107
* modify @dwork's timer so that it expires after @delay. If @delay is zero,
11081108
* @work is guaranteed to be queued immediately.
11091109
*
1110-
* Return: %true if @dwork was pending and its timer was modified,
1111-
* %false otherwise.
1110+
* Return: %false if @dwork was idle and queued, %true otherwise.
11121111
*
11131112
* A special case is when the work is being canceled in parallel.
11141113
* It might be caused either by the real kthread_cancel_delayed_work_sync()
11151114
* or yet another kthread_mod_delayed_work() call. We let the other command
1116-
* win and return %false here. The caller is supposed to synchronize these
1117-
* operations a reasonable way.
1115+
* win and return %true here. The return value can be used for reference
1116+
* counting and the number of queued works stays the same. Anyway, the caller
1117+
* is supposed to synchronize these operations a reasonable way.
11181118
*
11191119
* This function is safe to call from any context including IRQ handler.
11201120
* See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
@@ -1126,13 +1126,15 @@ bool kthread_mod_delayed_work(struct kthread_worker *worker,
11261126
{
11271127
struct kthread_work *work = &dwork->work;
11281128
unsigned long flags;
1129-
int ret = false;
1129+
int ret;
11301130

11311131
raw_spin_lock_irqsave(&worker->lock, flags);
11321132

11331133
/* Do not bother with canceling when never queued. */
1134-
if (!work->worker)
1134+
if (!work->worker) {
1135+
ret = false;
11351136
goto fast_queue;
1137+
}
11361138

11371139
/* Work must not be used with >1 worker, see kthread_queue_work() */
11381140
WARN_ON_ONCE(work->worker != worker);
@@ -1150,8 +1152,11 @@ bool kthread_mod_delayed_work(struct kthread_worker *worker,
11501152
* be used for reference counting.
11511153
*/
11521154
kthread_cancel_delayed_work_timer(work, &flags);
1153-
if (work->canceling)
1155+
if (work->canceling) {
1156+
/* The number of works in the queue does not change. */
1157+
ret = true;
11541158
goto out;
1159+
}
11551160
ret = __kthread_cancel_work(work);
11561161

11571162
fast_queue:

0 commit comments

Comments
 (0)