linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] kernel/kthread.c: kthread_worker: add work status check in timer_fn
@ 2020-09-25  5:07 qiang.zhang
  2020-09-25  9:30 ` Petr Mladek
       [not found] ` <20200925123821.18556-1-hdanton@sina.com>
  0 siblings, 2 replies; 3+ messages in thread
From: qiang.zhang @ 2020-09-25  5:07 UTC (permalink / raw)
  To: tj, pmladek; +Cc: akpm, linux-kernel

From: Zqiang <qiang.zhang@windriver.com>

When queue delayed work to worker, at some point after that the timer_fn
will be call, add work to worker's work_list, at this time, the work may
be cancel, so add "work->canceling" check current work status.

Signed-off-by: Zqiang <qiang.zhang@windriver.com>
---
 v1->v2:
 Change description information.

 kernel/kthread.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/kthread.c b/kernel/kthread.c
index 3edaa380dc7b..85a2c9b32049 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -897,7 +897,8 @@ void kthread_delayed_work_timer_fn(struct timer_list *t)
 	/* Move the work from worker->delayed_work_list. */
 	WARN_ON_ONCE(list_empty(&work->node));
 	list_del_init(&work->node);
-	kthread_insert_work(worker, work, &worker->work_list);
+	if (!work->canceling)
+		kthread_insert_work(worker, work, &worker->work_list);
 
 	raw_spin_unlock_irqrestore(&worker->lock, flags);
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] kernel/kthread.c: kthread_worker: add work status check in timer_fn
  2020-09-25  5:07 [PATCH v2] kernel/kthread.c: kthread_worker: add work status check in timer_fn qiang.zhang
@ 2020-09-25  9:30 ` Petr Mladek
       [not found] ` <20200925123821.18556-1-hdanton@sina.com>
  1 sibling, 0 replies; 3+ messages in thread
From: Petr Mladek @ 2020-09-25  9:30 UTC (permalink / raw)
  To: qiang.zhang; +Cc: tj, akpm, linux-kernel

On Fri 2020-09-25 13:07:59, qiang.zhang@windriver.com wrote:
> From: Zqiang <qiang.zhang@windriver.com>
> 
> When queue delayed work to worker, at some point after that the timer_fn
> will be call, add work to worker's work_list, at this time, the work may
> be cancel, so add "work->canceling" check current work status.

Great catch!

I was able to understand the problem from the description. Though I
would still try to improve it a bit. I suggest:

<new_text>
Subject: kthread_worker: Prevent queuing delayed work from timer_fn when it is being canceled

There is a small race window when a delayed work is being canceled and
the work still might be queued from the timer_fn:

CPU0					CPU1

kthread_cancel_delayed_work_sync()
  __kthread_cancel_work_sync()
    __kthread_cancel_work()
	work->canceling++;

					kthread_delayed_work_timer_fn()
					  kthread_insert_work();

BUG: kthread_insert_work() should not get called when work->canceling
is set.
</new_text>

> Signed-off-by: Zqiang <qiang.zhang@windriver.com>

With the above subject and commit message:

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] kernel/kthread.c: kthread_worker: add work status check in timer_fn
       [not found] ` <20200925123821.18556-1-hdanton@sina.com>
@ 2020-09-25 13:16   ` Petr Mladek
  0 siblings, 0 replies; 3+ messages in thread
From: Petr Mladek @ 2020-09-25 13:16 UTC (permalink / raw)
  To: Hillf Danton
  Cc: qiang.zhang, tj, akpm, Peter Zijlstra, Thomas Gleixner, linux-kernel

On Fri 2020-09-25 20:38:21, Hillf Danton wrote:
> 
> On Fri, 25 Sep 2020 11:30:46 +0200 Petr Mladek wrote:
> > 
> > On Fri 2020-09-25 13:07:59, qiang.zhang@windriver.com wrote:
> > > From: Zqiang <qiang.zhang@windriver.com>
> > > 
> > > When queue delayed work to worker, at some point after that the timer_fn
> > > will be call, add work to worker's work_list, at this time, the work may
> > > be cancel, so add "work->canceling" check current work status.
> > 
> > Great catch!
> > 
> > I was able to understand the problem from the description. Though I
> > would still try to improve it a bit. I suggest:
> > 
> > <new_text>
> > Subject: kthread_worker: Prevent queuing delayed work from timer_fn when it is being canceled
> > 
> > There is a small race window when a delayed work is being canceled and
> > the work still might be queued from the timer_fn:
> > 
> > CPU0					CPU1
> > 
> > kthread_cancel_delayed_work_sync()
> >   __kthread_cancel_work_sync()
> >     __kthread_cancel_work()
> > 	work->canceling++;
> > 
> > 					kthread_delayed_work_timer_fn()
> > 					  kthread_insert_work();
> > 
> > BUG: kthread_insert_work() should not get called when work->canceling
> > is set.
> 
> Seems like the diagram above can't cover the case that the timer fired
> and started acquiring the lock half a tick before here came the cancel
> that took the lock then set the canceling mark.
> Nor is it a bug given that cancel is always flushing the current work.

This is the same as:

CPU0					CPU1

					kthread_delayed_work_timer_fn()
					  kthread_insert_work();

kthread_cancel_delayed_work_sync()


It just shows that kthread_cancel_delayed_work_sync() can't stop work
that is already being proceed. In this case, it has to wait until it
finishes.

By other words, this patch can't fix any real problem with timing.
kthread_cancel_delayed_work_sync() only guarantees that the work
in neither queued nor running when it finishes.


But I still think that the patch makes sense. work->lock synchronizes
manipulation of the work state. And it is wrong to queue the work
when canceling flag is set.

By other words. The bug was harmless. But it still was a bug.

Best Regards,
Petr

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-25 13:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-25  5:07 [PATCH v2] kernel/kthread.c: kthread_worker: add work status check in timer_fn qiang.zhang
2020-09-25  9:30 ` Petr Mladek
     [not found] ` <20200925123821.18556-1-hdanton@sina.com>
2020-09-25 13:16   ` Petr Mladek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).