How do I wakeup a kthread that's sleeping with msleep_interruptible() so I can stop it?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
static int mainTask(void* data)
{
bool allDone;
allow_signal(SIGKILL);
set_current_state(TASK_INTERRUPTIBLE);
while (!kthread_should_stop())
{
set_current_state(TASK_RUNNING);
allDone = mainLoop();
set_current_state(TASK_INTERRUPTIBLE);
if (allDone)
msleep_interruptible(poll_interval*1000);
else
usleep_range(TICK_TIME, TICK_TIME+100);
}
return 0;
}
The problem is that when I call rmmod the thread doesn't wake up from the sleep, so I'm stuck there until the timeout happens. Depending on settings, that poll_interval timeout could be as long as 5 minutes. I thought kthread_stop() was supposed to wakeup the associated thread, and I thought msleep_interruptible was supposed to allow that wakeup to happen. What am I missing?
Also, in all such situations (kernel-side or not ...) I advise that you should use signals only to mean, "you should not be asleep right now."
To put it differently: the capability to "wait on a signal" enables the thread to avoid "busy waiting." But the thread may well "spin" a few "extra" times, every now and then, where it discovers upon waking-up that it has nothing to do.
The thread should be the proper master of its own affairs from start to finish: never hold a pistol to its head as a way of telling it to do something, because your pistol-shot, irrevocable though it will be, is not synchronized with whatever the thread was doing at the time. Instead, use a simple boolean flag which is regularly tested in the thread's main loop, to cause the thread to break out of the loop and to subsequently terminate itself, graciously.
This termination will take an indeterminate amount of time. Avoid race-conditions.
Last edited by sundialsvcs; 09-19-2016 at 07:06 AM.
sundialsvcs, thanks a lot. I ended up changing things around and I've got something that works now, but I'm still frustrated that I just don't understand what's going on here. For example, I just for the life of me don't understand what the difference between msleep() and msleep_interruptible() is. Digging through the code it does appear that the interruptible one will wake up on a signal, but then it just checks the timeout and goes back to sleep. So how is that any good to anyone? (I'm 99% sure I'm missing something here, but I can't figure it out). ssleep() just seems like an irresponsible function if you ever want your LKM to ever exit. Hit ssleep with a value of 300 and your module could take 5 minutes to exit and there's not a damn thing you can do about it.
Ultimately it seems that the only solution here is to bypass the whole kthread_stop() and kthread_should_stop() functions and just set a boolean that I'll check every few (hundred) milliseconds and then go back to sleep. (basically what I inferred from your suggestion).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.