Linux-rt-users archive on lore.kernel.org
 help / color / Atom feed
* Question about kthread_mod_delayed_work() allowed context
@ 2020-02-11 10:23 Grygorii Strashko
  2020-02-12 15:41 ` Petr Mladek
  0 siblings, 1 reply; 4+ messages in thread
From: Grygorii Strashko @ 2020-02-11 10:23 UTC (permalink / raw)
  To: linux-kernel, Petr Mladek
  Cc: linux-rt-users, Linux ARM, netdev, Richard Cochran

Hi All,

I'd like to ask question about allowed calling context for kthread_mod_delayed_work().

The comment to kthread_mod_delayed_work() says:

  * This function is safe to call from any context including IRQ handler.
  * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
  * for details.
  */

But it has del_timer_sync() inside which seems can't be called from hard_irq context:
kthread_mod_delayed_work()
   |-__kthread_cancel_work()
      |- del_timer_sync()
	|- WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));

My use case is related to PTP processing using PTP auxiliary worker:
(commit d9535cb7b760 ("ptp: introduce ptp auxiliary worker")):
  - periodic work A is started and res-schedules itself for every dtX
  - on IRQ - the work A need to be scheduled immediately

Any advice on how to proceed?
Can kthread_queue_work() be used even if there is delayed work is
scheduled already (in general, don't care if work A will be executed one
more time after timer expiration)?

-- 
Best regards,
grygorii

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

* Re: Question about kthread_mod_delayed_work() allowed context
  2020-02-11 10:23 Question about kthread_mod_delayed_work() allowed context Grygorii Strashko
@ 2020-02-12 15:41 ` Petr Mladek
  2020-02-12 19:17   ` Grygorii Strashko
  0 siblings, 1 reply; 4+ messages in thread
From: Petr Mladek @ 2020-02-12 15:41 UTC (permalink / raw)
  To: Grygorii Strashko
  Cc: linux-kernel, linux-rt-users, Linux ARM, netdev, Richard Cochran

On Tue 2020-02-11 12:23:59, Grygorii Strashko wrote:
> Hi All,
> 
> I'd like to ask question about allowed calling context for kthread_mod_delayed_work().
> 
> The comment to kthread_mod_delayed_work() says:
> 
>  * This function is safe to call from any context including IRQ handler.
>  * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
>  * for details.
>  */
> 
> But it has del_timer_sync() inside which seems can't be called from hard_irq context:
> kthread_mod_delayed_work()
>   |-__kthread_cancel_work()
>      |- del_timer_sync()
> 	|- WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));

It is safe because kthread_delayed_work_timer_fn() is IRQ safe.
Note that it uses raw_spin_lock_irqsave(). It is the reason why
the timer could have set TIMER_IRQSAFE flag, see
KTHREAD_DELAYED_WORK_INIT().

In more details. The timer is either canceled before the callback
is called. Or it waits for the callback but the callback is safe
because it can't sleep.


> My use case is related to PTP processing using PTP auxiliary worker:
> (commit d9535cb7b760 ("ptp: introduce ptp auxiliary worker")):
>  - periodic work A is started and res-schedules itself for every dtX
>  - on IRQ - the work A need to be scheduled immediately

This is exactly where kthread_mod_delayed_work() need to be used
in the IRQ context with 0 delay.


> Any advice on how to proceed?
> Can kthread_queue_work() be used even if there is delayed work is
> scheduled already (in general, don't care if work A will be executed one
> more time after timer expiration)?

Yes, it can be used this way. It should behave the same way as
the workqueue API.

I am happy that there are more users for this API. I wanted to
convert more kthreads but it was just falling down in my TODO.

I hope that I answered all questions. Feel free to ask more
when in doubts.

Best Regards,
Petr

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

* Re: Question about kthread_mod_delayed_work() allowed context
  2020-02-12 15:41 ` Petr Mladek
@ 2020-02-12 19:17   ` Grygorii Strashko
  2020-02-13  8:33     ` Petr Mladek
  0 siblings, 1 reply; 4+ messages in thread
From: Grygorii Strashko @ 2020-02-12 19:17 UTC (permalink / raw)
  To: Petr Mladek
  Cc: linux-kernel, linux-rt-users, Linux ARM, netdev, Richard Cochran



On 12/02/2020 17:41, Petr Mladek wrote:
> On Tue 2020-02-11 12:23:59, Grygorii Strashko wrote:
>> Hi All,
>>
>> I'd like to ask question about allowed calling context for kthread_mod_delayed_work().
>>
>> The comment to kthread_mod_delayed_work() says:
>>
>>   * This function is safe to call from any context including IRQ handler.
>>   * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
>>   * for details.
>>   */
>>
>> But it has del_timer_sync() inside which seems can't be called from hard_irq context:
>> kthread_mod_delayed_work()
>>    |-__kthread_cancel_work()
>>       |- del_timer_sync()
>> 	|- WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
> 
> It is safe because kthread_delayed_work_timer_fn() is IRQ safe.
> Note that it uses raw_spin_lock_irqsave(). It is the reason why
> the timer could have set TIMER_IRQSAFE flag, see
> KTHREAD_DELAYED_WORK_INIT().
> 
> In more details. The timer is either canceled before the callback
> is called. Or it waits for the callback but the callback is safe
> because it can't sleep.

I think, my issue (warning) could be related to the fact that kthread_init_delayed_work()
is used, which seems doesn't set TIMER_IRQSAFE flag.

> 
> 
>> My use case is related to PTP processing using PTP auxiliary worker:
>> (commit d9535cb7b760 ("ptp: introduce ptp auxiliary worker")):
>>   - periodic work A is started and res-schedules itself for every dtX
>>   - on IRQ - the work A need to be scheduled immediately
> 
> This is exactly where kthread_mod_delayed_work() need to be used
> in the IRQ context with 0 delay.
> 
> 
>> Any advice on how to proceed?
>> Can kthread_queue_work() be used even if there is delayed work is
>> scheduled already (in general, don't care if work A will be executed one
>> more time after timer expiration)?
> 
> Yes, it can be used this way. It should behave the same way as
> the workqueue API.
> 
> I am happy that there are more users for this API. I wanted to
> convert more kthreads but it was just falling down in my TODO.
> 
> I hope that I answered all questions. Feel free to ask more
> when in doubts.
> 
> Best Regards,
> Petr
> 

-- 
Best regards,
grygorii

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

* Re: Question about kthread_mod_delayed_work() allowed context
  2020-02-12 19:17   ` Grygorii Strashko
@ 2020-02-13  8:33     ` Petr Mladek
  0 siblings, 0 replies; 4+ messages in thread
From: Petr Mladek @ 2020-02-13  8:33 UTC (permalink / raw)
  To: Grygorii Strashko
  Cc: linux-kernel, linux-rt-users, Linux ARM, netdev, Richard Cochran

On Wed 2020-02-12 21:17:53, Grygorii Strashko wrote:
> 
> 
> On 12/02/2020 17:41, Petr Mladek wrote:
> > On Tue 2020-02-11 12:23:59, Grygorii Strashko wrote:
> > > Hi All,
> > > 
> > > I'd like to ask question about allowed calling context for kthread_mod_delayed_work().
> > > 
> > > The comment to kthread_mod_delayed_work() says:
> > > 
> > >   * This function is safe to call from any context including IRQ handler.
> > >   * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
> > >   * for details.
> > >   */
> > > 
> > > But it has del_timer_sync() inside which seems can't be called from hard_irq context:
> > > kthread_mod_delayed_work()
> > >    |-__kthread_cancel_work()
> > >       |- del_timer_sync()
> > > 	|- WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
> > 
> > It is safe because kthread_delayed_work_timer_fn() is IRQ safe.
> > Note that it uses raw_spin_lock_irqsave(). It is the reason why
> > the timer could have set TIMER_IRQSAFE flag, see
> > KTHREAD_DELAYED_WORK_INIT().
> > 
> > In more details. The timer is either canceled before the callback
> > is called. Or it waits for the callback but the callback is safe
> > because it can't sleep.
> 
> I think, my issue (warning) could be related to the fact that kthread_init_delayed_work()
> is used, which seems doesn't set TIMER_IRQSAFE flag.

Great catch!

It is a bug. Would you like to send the fix for
kthread_init_delayed_work() or would you prefer me doing so?

Best Regards,
Petr

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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-11 10:23 Question about kthread_mod_delayed_work() allowed context Grygorii Strashko
2020-02-12 15:41 ` Petr Mladek
2020-02-12 19:17   ` Grygorii Strashko
2020-02-13  8:33     ` Petr Mladek

Linux-rt-users archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-rt-users/0 linux-rt-users/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-rt-users linux-rt-users/ https://lore.kernel.org/linux-rt-users \
		linux-rt-users@vger.kernel.org
	public-inbox-index linux-rt-users

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-rt-users


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git