Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running infinite loop in linux kernel module

I have make a module in which i would like to run an infinite loop till I don't unload the module . Presently if I do rmmod it gives me notice that module is still busy and after some time kernel panics.

while(1)
{
    .......

}

Is there any trick through which I could run infinite loop till I unload the module.

like image 287
karan421 Avatar asked Apr 19 '12 07:04

karan421


2 Answers

In which context does this loop run? This is a very important question.

If init_module runs it, then the insmod process will never end, which is quite bad.
If some system call runs it, then the system call won't return, and it will also be bad.
In both cases, there's no way to kill the process (not even kill -9).
If you're doing it in a softIRQ handler (or, worse, hardIRQ handler), you'll hang the system.

If you do it in a kernel thread, which is dedicated to this task, you have a chance to get it right.
But if you don't want to hog the CPU completely, you need to call the scheduler and let it run other tasks. msleep_interruptible is a nice way to do it.

like image 167
ugoren Avatar answered Nov 06 '22 05:11

ugoren


I'm not sure this would work, but instead of while(1), use while(notStopped), which is set to 1 at first, and set it to 0 in stop_module().

like image 22
jcomeau_ictx Avatar answered Nov 06 '22 07:11

jcomeau_ictx