Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 06-18-2009, 12:24 AM   #1
LQ Newbie
Registered: Jun 2009
Posts: 4

Rep: Reputation: 0
what Kernel do if user call malloc() function in user space?

Dear all:

I am just wondering what Kernel actually does if a user calls malloc() function in user space. What I am understanding that Kernel has its own memory allocators like buddy and slab. But they are handling contiguous physical memories.

My question is that if an application calls malloc function, then what happens in Kernel? How Kernel allocates memories for the application.

It seems to me that malloc function uses mmap system calls, but still don't understand how they are connected to Kernel's task_struct.

Is there anyone who can answer to my questions?

Thank you.

P.S. I actually posted this question to Linux-Newbie Forum, but I don't think that forum is the right forum, so, I just copied the thread here.
Old 06-18-2009, 12:44 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

You basically need to look at things from dramatically different perspectives, depending on whether you're talking about user-space (i.e. different "malloc()" implementations), or kernel space. Each has surprisingly little knowledge of the other - they're pretty much independent of each other.

Here's a fairly good overview of Linux memory architecture (from a kernel perspective):

"Malloc()" is a library function, there can be many different implementations. In general, however, "malloc()" == "heap memory". Here are a couple of good links:

'Hope that helps .. PSM
Old 06-18-2009, 01:59 AM   #3
LQ Newbie
Registered: Jun 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hi paulsm4,

Thank you for your information. It is useful. But still it is not clear to me. My question was how task struct in Kernel manages heaps if user application calls malloc function in a user space.

I will describe my point step by step:

1. An application calls malloc() function to allocate certain amount of memory.
2. malloc() function in glibc calls dlmalloc()
3. dlmalloc() calls mmap() or brk()

I don't know what happen in Kernel if mmap() or brk() is called by user space and what affects the "task" struct of the application in Kernel space. What is actually changed in the task struct?

When mmap or brk is called, Kernel uses any specific memory allocator for them like buddy or slab?

Do you have any ideas to my questions?

Thank you.
Old 03-26-2014, 01:19 AM   #4
LQ Newbie
Registered: Mar 2014
Posts: 2

Rep: Reputation: Disabled

@Susobhan: Internally kernel has Zone division. (Like HIGHMEM,MORMAL,ZONE_DMA). Mainly user space application allocates memory from ZONE_HIGHMEM.
So internally kernel call the alloc_pages with ZONE_HIGHMEM flag enable.After that create a permanent mapping in the virtual field of page structure.
Old 03-26-2014, 02:18 AM   #5
LQ Newbie
Registered: Mar 2014
Posts: 2

Rep: Reputation: Disabled

rsyoung Your question was what happened after mmap() syscall && how kernel handle it.

So Here is the answer --> when you are creating a process a task_struct allocates for it && inside the structure there is a pointer of process address space called mm_struct *.

When you forking the process then everything gets allocated by slab allocator(task_struct,mm_struct). Inside the mm_struct there is another structure named vm_area_struct.

So when you do a calloc from userspace then mmap() syscall invoke do_mmap(). The do_mmap is a kernel function it does the following.

The do_mmap()function is used by the kernel to create a new linear address interval. Saying
that this function creates a new VMA is not technically correct, because if the created
address interval is adjacent to an existing address interval, and if they share the same permissions,
the two intervals are merged into one.

So another address interval gets added in vm_area_struct list.

Old 03-26-2014, 06:09 PM   #6
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,455
Blog Entries: 4

Rep: Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918
User-space functions such as "malloc()" do all of their work in user-space. As you allocate and free memory, these functions maintain their own lists of what's free and what's not. The kernel is not involved in this.

When these functions determine that they have no more memory to dole out, they issue a system-call to request that another fairly-large chunk of memory be assigned to the process ... which, as previously described, the kernel basically does by fiddling with the virtual-memory tables. The kernel does this, and the user-space memory allocation functions add this chunk to their available space, which they once again begin to dole out. Furthermore, the space once requested is probably never returned. (It's all virtual storage, anyway . . .)

So, most of the requests for memory do not involve the kernel at all.


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
what Kernel do if user call malloc() function in user space? rsyoung Linux - Newbie 1 06-20-2009 11:40 AM
making sys call ex:insmod,rmmod from function in user module shashishankar_h Linux - General 1 09-01-2005 12:36 AM
can interrupt handler call user space function? pot_po Programming 0 11-07-2002 09:32 PM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 02:59 AM.

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