kthread_stop always returning -EINTR?

Martin Galvan omgalvan.86 at gmail.com
Tue Jul 18 12:00:11 EDT 2017

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>


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");

        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 */

            /* Wait for the thread function to complete */
            result = kthread_stop(thread);

            printk(KERN_DEBUG "Thread %u stopped with result %d\n", cpu, result);
            printk(KERN_ALERT "Could not create a thread bound to core number %u\n", cpu);

    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)

More information about the Kernelnewbies mailing list