LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   dynamic memory allocation in kernel module (https://www.linuxquestions.org/questions/programming-9/dynamic-memory-allocation-in-kernel-module-233226/)

appas 09-21-2004 01:17 AM

dynamic memory allocation in kernel module
 
Hi ,

I am writing a linux kernel module where i had to allocate large memory quite a few times in the module.
For eg. I was using statement like
Code:

void my_kernel_function
{
 char buf[1024] ;
}

which was giving me stack overflow error and the system hang. After referrring some document i replaced the statement using
Code:

void my_kernel_function
{
char * buf ;
buf = kmalloc(required_length, GFP_ATOMIC) ;
if(!buf)
{
    // though this never happened
      error handling
}
kfree(buf);
}

This fixed my problem.
but I dont find any difference between the dynamic allocation and my previous approach, since my kmalloc never failed and i am freeing the 'buf' only at the end of function.
Since i dont know the internals of kmalloc, if someone could find a reason for the happening please explain.

infamous41md 09-21-2004 01:33 AM

kmalloc() gets memory from the cache allocator, which in turn gets whole pages of memory via getfreepages(). The total amount of memory you can have from successive kmalloc() calls is limited only to the available memory in the system.

allocating stack variables, you are limited by the max depth the stack is allowed to grow to, which is nowhere near the amount of memory you have.

appas 09-21-2004 01:55 AM

Thank you for the reply,


allocating stack variables, you are limited by the max depth the stack is allowed to grow to, which is nowhere near the amount of memory you have.


By 'stack variables' do you mean those variables declared in the module without dynamic allocation. Is it possible to know the stack size of stack available.

appas 09-21-2004 02:09 AM

Is it possible to know the available stack size and the system memory.

appas 09-21-2004 06:36 AM

All my questions were answered after referring a few documents to have clear picture of what has happened.
This thread can be closed at this point. Thank you once again for the support
.


All times are GMT -5. The time now is 07:44 PM.