Re: [PATCH v3 01/22] timer: Allow to check when the timer callback has not finished yet

From: Petr Mladek
Date: Thu Nov 19 2015 - 07:43:58 EST

On Wed 2015-11-18 23:32:28, Thomas Gleixner wrote:
> On Wed, 18 Nov 2015, Petr Mladek wrote:
> > timer_pending() checks whether the list of callbacks is empty.
> > Each callback is removed from the list before it is called,
> > see call_timer_fn() in __run_timers().
> >
> > Sometimes we need to make sure that the callback has finished.
> > For example, if we want to free some resources that are accessed
> > by the callback.
> >
> > For this purpose, this patch adds timer_active(). It checks both
> > the list of callbacks and the running_timer. It takes the base_lock
> > to see a consistent state.
> >
> > I plan to use it to implement delayed works in kthread worker.
> > But I guess that it will have wider use. In fact, I wonder if
> > timer_pending() is misused in some situations.
> Well. That's nice and good. But how will that new function solve
> anything? After you drop the lock the state is not longer valid.

If we prevent anyone from setting up the timer and timer_pending()
returns false, we are sure that the timer will stay as is.

For example, I use it in the function try_to_cancel_kthread_work().
Any manipulation with the timer is protected by worker->lock.
If the timer is not pending but still active, I have to drop
the lock and busy wait for the timer callback. See

Also I wonder if the following usage in
drivers/infiniband/hw/nes/nes_cm.c is safe:

static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);

if (!cm_core)
return -EINVAL;


if (timer_pending(&cm_core->tcp_timer))


We destroy the workqueue but the timer callback might still
be in progress and queue new work.

There are many more locations where I see the pattern:

if (timer_pending())

IMHO, we should use:

if (timer_active())
/* really safe to free stuff */

or just


I wonder if timer_pending() is used in more racy scenarios. Or maybe,
I just miss something that makes it all safe.

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at