1
votes

I am trying to develop a kernel module which has to execute a thread.

I am facing an error while compiling the module.

This is the module:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/kthread.h>  // for threads

static struct task_struct *thread1;

int thread_fn(void) {   
    while(1) {
        printk("Thread_fuction is running ...\n");
        msleep(2000);
    }
    return 0;
}

int thread_init(void) {
    char our_thread[8]="thread1";
    printk(KERN_INFO "in init");
    thread1 = kthread_create(thread_fn,NULL,our_thread);
    if((thread1)) {
        printk(KERN_INFO "in if");
        wake_up_process(thread1);
    }
    return 0;
}

void thread_cleanup(void) {
    int ret;
    ret = kthread_stop(thread1);
    if(!ret)
         printk(KERN_INFO "Thread stopped");
}

MODULE_LICENSE("GPL");   
module_init(thread_init);
module_exit(thread_cleanup);

And this is the error :

thread.c:25:26: error: passing argument 1 of ‘kthread_create_on_node’ from incompatible pointer type [-Werror=incompatible-pointer-types] thread1 = kthread_create(thread_fn,NULL,our_thread);

Could anyone help me to solve the problem?

1
Please fix your code indentation. - KamilCuk
Does this answer your question? How to use a function pointer with kthread_run? - Tsyvarev

1 Answers

3
votes

In kernel source code you can see that kthread_create_on_node() function has first argument type: int (*threadfn)(void *data). But your "threadfn" int thread_fn (void) roughly speaking is of type int (*threadfn)(void).

So change (void) to (void *data).