1

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?

Flexo
  • 87,323
  • 22
  • 191
  • 272
docas
  • 11
  • 4

2 Answers2

3

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.

Mark Jones
  • 402
  • 2
  • 5
  • Hello thank you for replying,i want to know how can I check if q is pointed to a valid memory? q is a struct defined as follows: struct aggregate_sched_data *q = qdisc_priv(sch); – docas Sep 28 '11 at 08:56
  • 1
    There really is no way of checking q (or any pointer) to know if it's pointed to valid memory. Typically what I do is initialize my pointers to 0 when I declare them. Later when I check them, if they're still 0, some memory needs to be allocated for them before I use them. Then later when I free them, set them back to 0 afterward. – Mark Jones Sep 28 '11 at 13:11
  • Sir I really tried to check my pointers if are valid but i seemed not to get it right. Can you please check the sample of my code and help me to do it the right way static int aggregate_enqueue(struct sk_buff *skb, struct Qdisc *sch) { if(q->agg_queue_hdr == NULL) //Nothing in the list! q->agg_queue_hdr = kmalloc(sizeof(struct agg_queue),GFP_ATOMIC); – docas Oct 05 '11 at 12:53
  • and then the struct of q is as as follows struct aggregate_sched_data *q = qdisc_priv(sch); // this struct is defined as follows struct aggregate_sched_data { struct qdisc_watchdog watchdog; unsigned int agg_min_size; unsigned int agg_max_size; unsigned int agg_max_timeout; struct agg_queue *agg_queue_hdr; }; – docas Oct 05 '11 at 13:10
-1

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.