kthread_stop always returning -EINTR?
Hi everyone! I'm doing a bit of testing on the Linux kthread functions, and have the following module: #include <linux/module.h> #include <linux/kthread.h> #include <linux/sched.h> #include <linux/smp.h> #include <linux/cpumask.h> #include <linux/err.h> MODULE_LICENSE("GPL"); int function(void *data) { printk(KERN_DEBUG "CPU: %u\n", smp_processor_id()); do_exit(0); /* Not sure if this is ok */ } /* Function executed upon loading driver */ int __init init_module(void) { unsigned int cpu; printk(KERN_DEBUG "init\n"); for_each_present_cpu(cpu) { struct task_struct *thread = kthread_create(function, NULL, "Thread %u", cpu); if (!IS_ERR(thread)) { int result; /* Bind the thread to the current core */ kthread_bind(thread, cpu); /* Start the thread */ wake_up_process(thread); /* Wait for the thread function to complete */ result = kthread_stop(thread); printk(KERN_DEBUG "Thread %u stopped with result %d\n", cpu, result); } else { printk(KERN_ALERT "Could not create a thread bound to core number %u\n", cpu); break; } } return 0; } /* Function executed when unloading module */ void cleanup_module(void) { printk(KERN_DEBUG "exit\n"); }
From what I understood, I should be able to see all the "CPU: ..." prints before init_module ends. However, kthread_stop is always returning -EINTR, as if wake_up_process wasn't actually being called. What am I doing wrong?
(Please CC me directly when you answer this)
On Tue, 18 Jul 2017 13:00:11 -0300, Martin Galvan said:
Hi everyone! I'm doing a bit of testing on the Linux kthread functions, and h
int function(void *data) { printk(KERN_DEBUG "CPU: %u\n", smp_processor_id());
do_exit(0); /* Not sure if this is ok */ }
Note that this will go bye-bye pretty much immediately.
/* Start the thread */ wake_up_process(thread);
Aaannnd... it's possibly gone already, before you get to the next line of code..
/* Wait for the thread function to complete */ result = kthread_stop(thread);
So it's going to be hard to stop a thread that's not there anymore. Note this comment from kernel/kthreads.c: /** * kthread_stop - stop a thread created by kthread_create(). * @k: thread created by kthread_create(). * * Sets kthread_should_stop() for @k to return true, wakes it, and * waits for it to exit. This can also be called after kthread_create() * instead of calling wake_up_process(): the thread will exit without * calling threadfn(). * * If threadfn() may call do_exit() itself, the caller must ensure * task_struct can't go away. Your function calls do_exit() itself. It's surprising that your kernel didn't explode when the tasx_struct went away. Looking at the code of kthread_stop, there's phenomenally little error checking (as most heavily-called kernel functions tend to be). It just assumes that 'thread' points at a valid thread structure and runs with it, with the result that you get back possibly random trash as 'result'. Two important kernel concepts: Locking and reference counting. Use them wisely.
participants (2)
-
Martin Galvan -
valdis.kletnieks@vt.edu