What are the different issues involved in kernel memory allocation
vs user space memory allocation?
Some thoughts that come to mind are:
* Kernel time is highly contended, so time of allocation is a real
issue (slab cache helps in this?)
For user space (malloc of libc), how is allocation done? Does this use the buddy allocation method (or is it for the kernel). Or just the UNIX style free list and in-use list is maintained?
Yes, but you should not care too much about it unless you are trying to write your own kmalloc()/malloc() clone function because kmalloc is for kernel space and malloc is for user land.
Keep in mind that each function use its own header file, one inside the kernel source tree and the other one inside the /usr/include.
Why don't you take a look at both functions?
There are two major differences:
kmalloc returns physically contiguous memory, malloc does not guarantee anything about the physical memory mapping.
The other main difference is that kmalloc'ed memory is reserved and locked, it cannot be swapped. malloc does not actually allocate physical memory. Physical memory gets mapped later, during use.
Memory is subject to fragmentation. You may have many megabytes of free memory left, but a 8K kmalloc (on a 4k page machine) can still fail. Things like the buddy allocator help but can't always guarantee large unfragmented blocks.
If you don't need contiguous mapping in kernel space, you can use vmalloc to avoid the fragmentation problem, at the cost of some MMU overhead.
Userspace memory (via malloc) is virtual - immediately after the call, the same non-writable zero page is mapped into every 4K block of your memory region. Only when you write to a page, a physical memory page is mapped for you at that location.
If swap is enabled, that page may be 'borrowed' from some other process, after the contents are deposited in the swap file.
Have a Unix Problem
Return to : - Unix System Administration Hints and Tips
(c) www.gotothings.com All material on this site is