docas
docas

Reputation: 11

kernel crash with kmalloc

I am trying to assign memory using kmalloc in kernel code in fact in a queueing discipline. I want to assign memory to q->agg_queue_hdr of which q is a queueing discipline and agg_queue_hdr is a struct, so if assign memory like this:

q->agg_queue_hdr=kmalloc(sizeof(struct agg_queue), GFP_ATOMIC);

the kernel crashes. Based on the examples of kmalloc I saw from searching, I now changed it to:

agg_queue_hdr=kmalloc(sizeof(struct agg_queue), GFP_ATOMIC);

with which the kernel doesn't crash. Now I want to know how can I assign memory to the pointer q->agg_queue_hdr?

Upvotes: 1

Views: 2366

Answers (2)

Austin Kim
Austin Kim

Reputation: 9

Why don't you modify your code with below way, which would avoid kernel panic.

if (q->agg_queue_hdr) {
    q->agg_queue_hdr = kmalloc(sizeof(struct agg_queue), GFP_ATOMIC);
}
else {
    printk("[+] q->agg_queue_hdr invalid \n");

    dump_stack();  // print callstack in the kernel log.

}

When disassembing "q->agg_queue_hdr", "ldr" instruction will works where kernel panic occurs.

Upvotes: -1

Mark Jones
Mark Jones

Reputation: 402

Make sure q is pointed to a valid area of memory. Then you should be able to assign q->agg_queue_hdr like you had it to begin with.

Upvotes: 3

Related Questions