deleting timer that re-registers itself

Greg KH greg at kroah.com
Tue May 4 12:28:48 EDT 2021


On Tue, May 04, 2021 at 12:17:56PM -0400, Valdis Klētnieks wrote:
> On Tue, 04 May 2021 23:59:12 +0900, Hyeonggon Yoo said:
> > Does del_timer work well for timers that re-registers itself?
> > what if the timer is currently running, and del_timer is called,
> > and the running timer re-registers itself?
> 
> Minor nit: while the timer is running, there's no problem.
> When the timer has *expired* and timer_callback() is running
> is when you have a race condition.
> 
> So who's going to call del_timer()? The only thing that does that is
> timer_exit. Soo...  Apply some silly locking:
> 
> static int exiting = 0;
> 
> void timer_callback(struct timer_list *timer) {
>         struct timer_data *data = from_timer(data, timer, timer);
>         data->value++;
>         printk(KERN_INFO "[%s] value is = %d\n", __func__, data->value);
>         if (!exiting)
>                 mod_timer(timer, jiffies + DELAY);
> }
> 
> void __exit timer_exit(void) {
>         exiting = 1;
>         int ret = del_timer(&data.timer);
>         printk("[%s] deleting timer..., ret = %d\n", __func__, ret);
> }

That's mean, don't write buggy code as an example because then I'm going
to have to reject it when it gets submitted as a "real" driver :)


Hint, a "global" variable like this does not work like you expect it to
at all, sorry.

greg k-h



More information about the Kernelnewbies mailing list