deleting timer that re-registers itself

Hyeonggon Yoo 42.hyeyoo at gmail.com
Tue May 4 10:59:12 EDT 2021


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?

how should I handle it?

Below is my simple kernel timer example.


timer.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/time.h>

#define MODULE_NAME "TIMER"
#define DELAY (1 * HZ)
struct timer_data {
        int value;
        struct timer_list timer;
};

struct timer_data data = {
        .value = 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);
        mod_timer(timer, jiffies + DELAY);
}

int __init timer_init(void) {
        printk("[%s] creating timer...\n", __func__);
        timer_setup(&data.timer, timer_callback, 0);
        mod_timer(&data.timer, jiffies + DELAY);
        return 0;
}

void __exit timer_exit(void) {
        int ret = del_timer(&data.timer);
        printk("[%s] deleting timer..., ret = %d\n", __func__, ret);
}

module_init(timer_init);
module_exit(timer_exit);

MODULE_LICENSE("GPL");



More information about the Kernelnewbies mailing list