Vikas Raturi
Vikas Raturi

Reputation: 926

Dynamically creating variable in Kernel Module

I am planning to use kthread_run API in my kernel module.
As kthread_run, returns a struct task_struct *, which i want to store in a variable global across my module.
However, i want one thread each for a cpu and i obtain no of cpus using num_online_cpus.
However, when i write following code:

    //outside init_module function
    int cpus = num_online_cpus();
    static struct task_struct *my_tasks[cpus];  

    static int __init init_module(){
           for(int j = 0; j < cpus; ++j){
                  my_tasks[j] = kthread_run(...);  
           }  
    }

However, i get following error on this:
error: variably modified ‘tasks’ at file scope
How can i achieve this ???

Upvotes: 1

Views: 345

Answers (1)

John
John

Reputation: 3520

If your variables are actually one per cpu, you might want to use the per_cpu macros. The gist is, you declare such a variable with:

DEFINE_PER_CPU(struct task_struct, my_tasks);

and then access the variable using

get_cpu_var(my_tasks).foo = bar;

You can get more info at See http://www.makelinux.net/ldd3/chp-8-sect-5 (or percpu.h) for more details.

Upvotes: 1

Related Questions