Prz3m3k
Prz3m3k

Reputation: 605

linux kernel timer - why timer function is not started immediately after system boots

I use a Kernel timer to schedule a function to run periodically (once the timer is initialised and the function is called, the timer gets rescheduled inside that function). It is happening as part of my device driver kernel module and the wanted behaviour is that the function is triggered and runs straight after system boots (module is loaded). The problem is that the function triggered by the timer is not started immediately after system boots but it is started after approximately 5 minutes from the boot up (although I can confirm that the module is already loaded and the code execution goes through add_timer early enough).

Here is my initialisation of the timer:

// Allocate memory for the timer
struct timer_list* pTimer = (struct timer_list*)vmalloc(sizeof(struct timer_list));
// step 1: Initialising function for the timer
init_timer(pTimer);
// step 2: set timer fields as needed
pTimer->data = (unsigned long)Data;
pTimer->function = ((void(*)(unsigned long))start_routine);
pTimer->expires = -1; // fire immediately
// step 3: register timer
add_timer(pTimer);

, where start_routine is the function to run. Then, inside that function, I reschedule the timer:

/* reschedule the timer */
pTimer->expires = jiffies + 1; // fire every 4msec
add_timer(pTimer);

and the whole thing works fine but not during the initial approx 5 minutes after system boots.

Like I wrote, I can see the code executing through the timer initialisation part straight after the system boot up (and add_timer is called) but function start_routine for some reason is blocked for the initial 5 minutes after boot up. After that, start_routine is started being called as intended. What could be the reason for that approx 5 minutes delay to start calling timer function after system boot ups?

Upvotes: 0

Views: 1220

Answers (1)

CL.
CL.

Reputation: 180060

pTimer->expires = -1; // fire immediately

There is no jiffies value that has a special meaning. This code tells the kernel to run the timer when the jiffies counter reaches the value -1. On your machine, the counter happens to start at -75000, so the value -1 is reached 5 minutes after booting.

pTimer->expires = jiffies + 1; // fire every 4msec

This comment is wrong unless CONFIG_HZ is set to 250.

Upvotes: 2

Related Questions