Sarev of Aona Sarev of Aona - 6 months ago 57
Linux Question

Do you need to call init_timer() again after a del_timer()

I have a

Linux
module which creates timers, some of which may add themselves again during their handler function.

In some other cases, the timer is removed (perhaps before it's gone off) using
del_timer_sync()
.

In that case, do I need to do the
init_timer()
call again on the struct before I next
add_timer(
) or is that just a waste of (precious) interrupt latency?

Answer Source

To answer my own question, I believe I do need to init_timer() my struct after any del_timer() or del_timer_sync() if I ever intend to access the struct again - for example, when doing a timer_pending() or something during module cleanup.

I think in the case of writing a kernel module which potentially re-uses a timer, the best thing to do is:

static struct timer_list my_timer;

...

static void remove_my_timer(void)
{
  if (timer_pending(&my_timer))
  {
    del_timer_sync(&my_timer);
    init_timer(&my_timer);
  }
}

static void arm_my_timer(...)
{
  remove_my_timer();
  my_timer.expires  = ...;
  my_timer.data     = ...;
  my_timer.function = ...;
  add_timer(&my_timer);
}

...

int __init init_my_device(void)
{
  ...
  init_timer(&my_timer);
  ...
}

void __exit cleanup_my_device(void)
{
  ...
  remove_my_timer();
  ...
}

Hope that helps someone else in future.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download