How to Pass Data to Kthread_Run

I'm try to make simple kernel module with multithreading. So I'm using linux/kthread.h, kernel v. 5.2.11

Problem: I can't passing the char array into thread: Segmentation fault.

That's what I'm doing:

typedef struct {
    int num; 
    char origin[MAXSTR]; //part of input for current thread
    struct completion wait_for_thread;      //completion struct
} kthread_arg;

Then:

struct task_struct *task;
static kthread_arg kta_first_thread;
kta_first_thread.num = 1;
strncpy(kta_first_thread.origin, dat1, MAXSTR );

//Here I have normal char array 'origin'
init_completion(&kta_first_thread.wait_for_thread);
task = kthread_run(&thread_function, (void*)&kta_first_thread, "one");

And after that I have the error. Moreover, if you remove the array from struct, then everything works. I'm sure doing something wrong?

5

1 Answer

The args passed to kernel_run must be kmalloced, your args is in stack. I have met the same problem, your code should like this:

struct your_struct* test=NULL;
struct task_struct* t=NULL;
test=(struct your_struct*)kmalloc(sizeof(struct your_struct),GFP_KERNEL);
t=kthread_run(your_function,(void*)test,name);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest