Memory Regions in virtual address Space
Posted: Mon Dec 07, 2009 9:36 am
Hi there,
currently I'm still trying to get my memory management on some level where it's usable.
I implemented a Physical Memory Allocator using a Bitmap which doesn't do anything more than marking any frame as used or free.
On top of that there is my Paging-Stuff which has a high level API for mapping virtual pages to physical frames.
I have a small heap which I will trash as soon as I finished my SLAB Allocator since I won't need it anymore.
This is my current memory layout for the kernel:
Currently I've got 4 Regions: The Stack, Slabs, Heap and code/static data.
The code and the stack aren't much of a problem. They both don't grow or shrink on runtime. So I can just allocate the amount of space and leave them alone.
The heap is not a big problem. Currently it can expand dynamically, if it is too small, until a given limit which is 256MB here. It can't shrink itself because it uses a linked list for management and as I said it's going to be trashed anyway.
My big problem is the slab allocator. This thing grows and shrinks all the time. At least when physical memory runs short.
Let's assume I create two new slabs which occupy 4 pages and then another slab which takes 5 pages. Then the second of the three slabs gets destroyed.
How can I track that theses 4 pages at this virtual location are free?
The physical location doesn't matter. I've got my paging between the bus and my address space which handles this issue just fine.
But if I create another new 5-pages slab I need to know that I need 5 free frames and map them behind the other 5-page slab. Vice versa, if I create a new slab which takes 4 or less pages I could use the free space behind the first 4-pages slab.
Any ideas how I can track this? And where? Inside my paging stuff? It don't really fits there I think because this virtual-memory-layout-tracking is one level above it.
Does somebody know how linux or the several BSDs handle this? They must have had the same problem.
Unfortunately most of the memory stuff in the linux kernel is total black voodoo magic for me. It's optimized to level where it is almost unreadable. At least for me
currently I'm still trying to get my memory management on some level where it's usable.
I implemented a Physical Memory Allocator using a Bitmap which doesn't do anything more than marking any frame as used or free.
On top of that there is my Paging-Stuff which has a high level API for mapping virtual pages to physical frames.
I have a small heap which I will trash as soon as I finished my SLAB Allocator since I won't need it anymore.
This is my current memory layout for the kernel:
Code: Select all
Kernel:
4 GiB +-------------------------+ 0xFFFFFFFF
| Page Table Mapping | Identity paging = 4MB
|_________________________| 0xFFC00000
| |
| Free |
| |
|_________________________|
| | K_STACK_END = 0xE0403FFF
| Kernel Stack | = 16KB
| |
|_________________________| K_STACK_START = 0xE0400000
| | K_SLAB_END = 0xE03FFFFF
| |
| Slab Caches | = 256MB
| |
|_________________________| K_SLAB_START = 0xD0400000
| | K_HEAP_END = 0xD03FFFFF
| |
| Kernel heap | = 256MB
| |
|_________________________| K_HEAP_START = 0xC0400000
| | K_SPACE_END = 0xC03FFFFF
| Kernel itself | = 4MB
| and placement addr.stuff|
3 GiB |_________________________| K_BEGINNING = 0xC0000000
| |
| Unused |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|_________________________|
| Unmapped 4K page |
0 GiB +_________________________+ 0x00000000
The code and the stack aren't much of a problem. They both don't grow or shrink on runtime. So I can just allocate the amount of space and leave them alone.
The heap is not a big problem. Currently it can expand dynamically, if it is too small, until a given limit which is 256MB here. It can't shrink itself because it uses a linked list for management and as I said it's going to be trashed anyway.
My big problem is the slab allocator. This thing grows and shrinks all the time. At least when physical memory runs short.
Let's assume I create two new slabs which occupy 4 pages and then another slab which takes 5 pages. Then the second of the three slabs gets destroyed.
How can I track that theses 4 pages at this virtual location are free?
The physical location doesn't matter. I've got my paging between the bus and my address space which handles this issue just fine.
But if I create another new 5-pages slab I need to know that I need 5 free frames and map them behind the other 5-page slab. Vice versa, if I create a new slab which takes 4 or less pages I could use the free space behind the first 4-pages slab.
Any ideas how I can track this? And where? Inside my paging stuff? It don't really fits there I think because this virtual-memory-layout-tracking is one level above it.
Does somebody know how linux or the several BSDs handle this? They must have had the same problem.
Unfortunately most of the memory stuff in the linux kernel is total black voodoo magic for me. It's optimized to level where it is almost unreadable. At least for me