LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   what Kernel do if user call malloc() function in user space? (https://www.linuxquestions.org/questions/linux-kernel-70/what-kernel-do-if-user-call-malloc-function-in-user-space-733790/)

rsyoung 06-18-2009 12:24 AM

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.

paulsm4 06-18-2009 12:44 AM

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):

http://www.linuxhq.com/guides/TLK/mm/memory.html

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

http://www.linuxjournal.com/article/6390
http://en.wikipedia.org/wiki/Malloc

'Hope that helps .. PSM

rsyoung 06-18-2009 01:59 AM

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.

susobhan 03-26-2014 01:19 AM

@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.

susobhan 03-26-2014 02:18 AM

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.

-Susobhan

sundialsvcs 03-26-2014 06:09 PM

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.


All times are GMT -5. The time now is 11:29 PM.