vsm
vsm

Reputation: 9

EXPORT_SYMBOL a dynamic memory location

Is it possible to EXPORT_SYMBOL() a struct that contains a kmalloc array? If yes, what are the things that I need to keep in mind?

This is a psuedo code of what I want to do.

struct test {
   int a;
   ...
   uint64_t* data;
}

struct test foo;
EXPORT_SYMBOL(foo);
...

In module1_kthread1_func() I have:

int module1_kthread1_func(void *foo){
...
foo->data = kmalloc(SIZE, GFP_KERNEL);
...
foo->data[var] = 1243;   
var++;  
...
}

In module2_kthread2_func() I have:

...
extern struct test foo; 
...

int module2_kthread2_func(void* foo){
...
for (i=0; i<SIZE; i++)
    printk(KERN_INFO "Variable value of %d is %llu", i, foo->data[var]); 
...
}

Upvotes: 0

Views: 226

Answers (1)

Marco Bonelli
Marco Bonelli

Reputation: 69367

It's definitely possible, yes.

You need to be careful and make sure that code that uses it knows that some of the fields might not be available before they are allocated. That is, check if they are NULL first instead of directly accessing them.

You might want to declare the structure with an explicit initializer, just so that it is obvious what is going on:

struct test foo = {
    .a    = 123,
    .data = NULL // Initialized by function X when Y
};
EXPORT_SYMBOL(foo);

If such fields are compulsory for the structure to be used, you might want to initialize them early on (see here).

Upvotes: 1

Related Questions