Should kthread_stop be used if we return from the kernel thread?

If I have the following kernel thread function:

int thread_fn() { printk(KERN_INFO "In thread1"); return 0; } 

Should I use the kthread_stop() function here?

Will return in a thread function cause the kernel thread to stop and exit?

+5
source share
1 answer

If you look at how kthread() implemented , on line 209 it calls threadfn(data) and stores the exit code in ret ; then it calls do_exit(ret) .

Thus, a simple return from threadfn .

If you look at the kthread_stop documentation, it says that it:

  • sets kthread_should_stop to return true;
  • wakes up a stream;
  • Waits for a stream to exit.

This means that kthread_stop() should only be called from a thread to stop the thread. Since it is waiting for the thread to complete, you should not call this inside the thread, or you might be stuck!

In addition, the documentation says that it only informs the thread that it should exit, and that the thread should call kthread_should_stop to find out about it. Therefore, a durable threadfn can do this:

 int thread_fn() { printk(KERN_INFO "In thread1"); while (!kthread_should_stop()) { get_some_work_to_do_or_block(); if (have_work_to_do()) do_work(); } return 0; } 

But if your function is not durable, a call to kthread_should_stop not required.

+1
source

Source: https://habr.com/ru/post/1246163/


All Articles