clinux-kernelkernelkmalloc

Unable to access struct data


In the header file:

typedef struct {
char* a;         
int allowed;

struct suit {
        struct t {
                char* option;
                int count;      
        } t;

        struct inner {
                char* option; 
                int count;      
        } inner;        
} suit;
} contain;

typedef struct {
       contain info;
} query_arg_t;

In the kernel module,

// initialize

static const contain _vector = { 
.a = "John",
.allowed = 1,
.suit = {
    .t = {
        .option = "ON",
        .count = 7
    },
    .inner = {
        .option = "OFF (*)",
        .count = 7
    }           
}
};

However, as we try:

query_arg_t q;

    q.info = kmalloc(sizeof(_vector), GFP_KERNEL);

We will get this error: error: incompatible types when assigning to type ‘contain’ from type ‘void *’

The above error is solved by @SunEric and @Sakthi Kumar.

         q.info = kmalloc(sizeof(_vector), GFP_KERNEL);
        memcpy(&(q.info), &(_vector), sizeof(_vector));

It seems ok now. It builds but when it runs to that part, it states that the kernel stack is corrupted. after trying to execute:

     printf("option: %s \n", q.info->suit.t.option); 
     printf("option: %s \n", q.info->suit.t.option);

[Updated: Solved]

@Sakthi Kumar solved it by:

   //removing kmalloc
   // commenting out: q.info = &_vector;
   memcpy(&(q.info), &(_vector), sizeof(_vector));

printf("option: %s \n", q.info.suit.t.option);
printf("option: %s \n", q.info.suit.inner.option);

Solution

  • Your structure to be of the form

    typedef struct {
           contain *info;
    } query_arg_t;
    

    You are trying to assign a pointer (kmalloc returns void *) to a struct variable (contain).