valmiki
valmiki

Reputation: 721

add_timer inside kernel timer function doesn't require scheduling?

When we use kernel timers, kernel timers are run in software interrupt, so kernel timer function runs in timer interrupt context.

void timer_func(unsigned long arg)
{
    my_timer.expires = jiffies + HZ;
    add_timer(&my_timer);
}

So add_timer() inside kernel timer function does not need scheduling? Because in interrupt context scheduling is disabled.

Upvotes: 0

Views: 837

Answers (2)

kaiwan
kaiwan

Reputation: 2144

I think the OP means "how come it works in interrupt ctx?". So, add_timer() itself does not invoke schedule() directly; it calls mod_timer():

void add_timer(struct timer_list *timer)
{
    BUG_ON(timer_pending(timer));
    mod_timer(timer, timer->expires);
}
EXPORT_SYMBOL(add_timer);

and as the code comment above this functions clearly says:

"...
 * The kernel will do a ->function(->data) callback from the
 * timer interrupt at the ->expires point in the future. The
 * current time is 'jiffies'.
..."

So it does not directly invoke schedule(); it will invoke your timer function upon expiry. Note that the timer functionality runs in (soft) interrupt context, so don't do anything that will invoke schedule() directly or indirectly.

Upvotes: 0

Tsyvarev
Tsyvarev

Reputation: 65928

Yes, add_timer function can be used in interrup context. Calling it within timer callback function is a standard way for repeatedly doing something.

Upvotes: 1

Related Questions