Max size of Kmalloc

1,636 views
Skip to first unread message

Amrit Pal Singh

unread,
Jul 26, 2014, 6:23:59 PM7/26/14
to inside...@googlegroups.com
Hi,
The normally addressable kernel memory region (< 896MB) is called low memory. The kernel memory allocator,
kmalloc(), returns memory from this region. Memory beyond 896MB (called high memory) can be accessed
only using special mappings.

1. Why only 896 MB and why not full 1 GB i.e 1024 MB?
2. I read that out of 4 GB addressable space only 1 GB is available to kernel and 3 GB to user.
What if the main memory i.e RAM that we have is only 2 GB.
How would the memory be divided between user space and kernel Space?
2. kmalloc returns contiguous memory in physical space. i.e minimum of one page.
How do we decide the maximum number of pages that can be allocated in one call to  kmalloc. What if the pages are free but are not contagious?? 4 MB in multiple of 128 KB page size is what I read. Please clarify.

Thanks & Regards,
Amrit Pal Singh.

Thanks,

charan tejareddy

unread,
Jul 27, 2014, 12:42:44 AM7/27/14
to inside...@googlegroups.com
On Sunday, 27 July 2014 3:54 AM, Amrit Pal Singh <aps...@gmail.com> wrote:


Hi,
The normally addressable kernel memory region (< 896MB) is called low memory. The kernel memory allocator,
kmalloc(), returns memory from this region.
[Xman] Yes we call (<896MiB ) is low mmemory. But the kmalloc() can allocate the memory not only from the low memory but from high memory as well. kmalloc() is built on the slab layer. This slab layer will inturn built on top of the buddy system. But it only ensures that the memory is always contiguous no matter from where the memory is allocated.

Check the details for the buddy system/slab layer from net.


 Memory beyond 896MB (called high memory) can be accessed
only using special mappings.

[xman] Yes.  From 896MiB space to rest of the memory we will access it through the special mappings . And these mappings are done either by vmalloc() or  kmap() and some times fixmaps.
We will give a gap of 8MiB space to the after 896MiB. And from then we have vmalloc() space from VMALLOC_START to VMALLOC_END and then a gap of 2 pages is left. And then from PKMAP_BASE to FIXMAP_START is for kmap(). And the from FIXMAP_START to the end , it is used for kmap_atomic() and also for very fast access mappings.

1. Why only 896 MB and why not full 1 GB i.e 1024 MB?

[xman] The reason why we allocated only 896MiB is ..If the system is up and running for a long times,there is a chance for the fragmentation of memory. And so there is possibility that memory is fragmented and we may not get the contiguous memory. So after 896MiB of kernel space, some memory is used for mappings using which we will see the discontiguous physical pages as contiguous in the kernel virtual space.
     The best example for this is when we try to load the modules, the kernel should allocate the memory.  But as the modules can be load at any time and we may not get the contiguous kernel space because of the memory fragmentation. And so we use vmalloc() for this situation.

e2. I read that out of 4 GB addressable space only 1 GB is available to kernel and 3 GB to user.
What if the main memory i.e RAM that we have is only 2 GB.

[xman] The division for kernel and user space is determined by the PAGE_OFFSET macro. So we can use this to control the division. So, In 2GiB case, we may divide as 1GiB of kernel memory and 1GiB of user memory (or) 2GiB of  kernel only memory(I mean only for low memory)
How would the memory be divided between user space and kernel Space?

2. kmalloc returns contiguous memory in physical space. i.e minimum of one page.
How do we decide the maximum number of pages that can be allocated in one call to  kmalloc.
[xman] As i said the kmalloc() is built on top of the slab layer. (Must read the slab layer implementation to understand the kmalloc. I recommend  rober love book  for this.)
 What if the pages are free but are not contagious?? 4 MB in multiple of 128 KB .page size is what I read. Please clarify.
[xman] kmalloc() always ensures that the memory is physically coniguous. If not, it will return NULL

PS: MB = 10 power 6 and MiB (pronounced as  Mi-bee) = 1 * 1024 * 1024. In computers, we have MiB not MB.

Thanks & Regards,
Amrit Pal Singh.

Regards
xman
--
You received this message because you are subscribed to the Google Groups "SysPlay's Inside Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inside_linux...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Amrit Pal Singh

unread,
Jul 27, 2014, 1:53:10 PM7/27/14
to inside...@googlegroups.com

thanks  a lot...
Two doubts...
1. What if in an embedded system has only 1 GB of RAM or say we keep on changing the RAM of the PC.
I mean do we have to change the PAGE_OFFSET macro??
clearly that is not the case as I have changed the RAM of my system several times from 1 GB to max 3 GB.
Do we have memory division like 75% for user space and 25% for kernel space?

2. Vmalloc should also return the address that can be used in kernel space, but the upper 3GB space is reserved for user..
So how does Vmalloc allocates from high memory meant for user. As Vmalloc returned addresses are to be used in kernel space and
user space addresses cannot be used directly in kernel space. Please excuse me if the question is a little elementary.

Thanks & Regards,
Amrit Pal Singh.

charan tejareddy

unread,
Jul 28, 2014, 2:43:09 PM7/28/14
to inside...@googlegroups.com

Comments are inline.

~xman

On Sunday, 27 July 2014 11:23 PM, Amrit Pal Singh <aps...@gmail.com> wrote:



thanks  a lot...
Two doubts...
1. What if in an embedded system has only 1 GB of RAM or say we keep on changing the RAM of the PC.
I mean do we have to change the PAGE_OFFSET macro??
clearly that is not the case as I have changed the RAM of my system several times from 1 GB to max 3 GB.
Do we have memory division like 75% for user space and 25% for kernel space?
[xman] Please do remember that PAGE_OFFSET macro is used to divide the virtual address and **not** physical address space. Generally when talking about physical memory we have the terminology of low(normal)memory and high memory. The memory till 1GiB is called normal memory( as the kernel at max can detect only this much. And ofcourse there are some techniques are there to detect more than that but discarding it for simplicity) and the > 1GiB is called High memory. If you say, the RAM has only 1GiB size then you can confidently say it has only low memory.Obviously in this case you don't need PKMAP_BASE to FIXADDR_START in the virtual space used to map the high memory and hence it willl be absent.
     Now coming to the answer , even it has 1GiB of RAM, you can still have 4GiB **virtual** address space. And so we can still divide the memory using the PAGE_OFFSET(And again its virtual). And there is no hard rule that 25% to kernel and 75% to user. 
    Then what will happen if I use entitire address as kernel space? As the memory for the kernel address space is directly mapped, and so the every process can look into the other process memory. Security problem. ANd what will happen if I use the memory as user address space? Here , as this requires pagetable traversing to get the physical memory and since the kernel can work  slowely and hence the entire system.



2. Vmalloc should also return the address that can be used in kernel space, but the upper 3GB space is reserved for user..
So how does Vmalloc allocates from high memory meant for user. As Vmalloc returned addresses are to be used in kernel space user space addresses cannot be used directly in kernel space.
 [xman]You are correct. vmalloc can also return the memory (exactly it is page)either from the lower/higher memory. If you dig the vmalloc() function inside the kernel, some where we are passing flag as GFP_KERNEL | __GFP_HIGHMEM What does this means is that when we are getting a memory either from  the highmemory (checked first) and if not get it from the low memory(as low memory is valuable resource and hence checked next). But in either case vmalloc() space has its own pagetables to represent the physically discontiguous memory as virtually contiguous memory and hence by creating the pagetable entries for highmemory in vmalloc pagetables, we are accessing it from the kernel space.
Reply all
Reply to author
Forward
0 new messages