add_timer crashes the kernel,Why?

Dexter Haslem dexter.haslem at gmail.com
Mon Dec 20 09:15:57 EST 2010


On 12/20/2010 2:08 AM, Mulyadi Santosa wrote:
> Hi :)
>
> On Mon, Dec 20, 2010 at 15:27, lijin liu<llj098 at gmail.com>  wrote:
>> Hello everyone!
>>
>> I want to use the timer in my kernel module. I read the book  _Linux
>> kernel development 3rd version_  and LDD 3rd version.
>>
>> Both the two books tell me write the code like this:
>>
>> struct timer_list my_timer;
>> init_timer(&my_timer);
>>
>> my_timer.expires = jiffies + wait;
>> my_timer.data = 0;
>> my_timer.function=my_function;
>>
>> //active the timer:
>> add_timer(&timer);
>>
>>
>> When I insmod the module, the kernel crashed, and when I change my
>> code to *mod_timer* rather than add_timer.
>
> Hmmm, from http://lxr.linux.no/linux+v2.6.36/kernel/timer.c#L864, it reads:
> void add_timer(struct timer_list *timer)
> {
>          BUG_ON(timer_pending(timer));
>          mod_timer(timer, timer->expires);
> }
>
> thus, IMHO the error happened because there is other timer pending...
> somehow it is forbidden to add timer when other are still in queue in
> latest kernel
>

I believe add_timer just prevents creating duplicate timers from the 
same struct. I was actually thinking I had the same problem with timer 
code (following essential linux device drivers). But the issue was 
removing a module with an active timer will cause a crash.

Here is what I ended up with, a silly sample timer that chains itself 3 
times. It might be useful to OP: http://pastebin.com/v4rr3t50

-- 
-Dexter Haslem



More information about the Kernelnewbies mailing list