LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-21-2004, 02:17 AM   #1
appas
Member
 
Registered: Jul 2004
Posts: 72

Rep: Reputation: 15
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.

Last edited by appas; 09-21-2004 at 02:22 AM.
 
Old 09-21-2004, 02:33 AM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 09-21-2004, 02:55 AM   #3
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
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.

Last edited by appas; 09-21-2004 at 03:16 AM.
 
Old 09-21-2004, 03:09 AM   #4
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
Is it possible to know the available stack size and the system memory.
 
Old 09-21-2004, 07:36 AM   #5
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
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
.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Memory Allocation jvff Programming 1 09-05-2005 06:01 AM
dynamic memory allocation failure guam Programming 4 04-13-2005 10:16 AM
dynamic allocation of memory in kernel space appas Programming 1 08-04-2004 07:32 AM
pointers and dynamic memory allocation deveraux83 Programming 2 01-24-2004 11:35 AM
Dynamic Memory Allocation query dhanakom Programming 2 07-21-2003 03:19 PM


All times are GMT -5. The time now is 02:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration