OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Apr 27, 2024 10:23 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Kernel Heap Design Questions
PostPosted: Sat Aug 26, 2023 11:56 pm 
Offline

Joined: Fri Nov 08, 2019 5:35 pm
Posts: 16
Hello, I have some questions about my kernel heap, and related things.
Firstly, what should, and shouldn't I be using my kernel heap allocator for? During the boot process I initialize my kernel heap with a large block of memory, and then use this to allocate memory for all kernel space operations.
I'm writing this kernel for RISC-V, and I don't use a higher-half kernel design. My kernel has a trampoline design like Minix, or XV6 to transfer control between userspace and the kernel. Userspace processes have a stack/heap mapped at arbitrary high addresses, but the kernel stack/heap are identity mapped in kernel space, so that I can easily allocate memory for things like user process page tables, or VirtIO queues where I need a physical address. The initial block of userspace heap memory is allocated when the process is created, and can be grown/shrunk as needed. Are there any obvious flaws with what I'm doing? I'm trying to follow open-source examples, but I'm finding them hard to follow, or ill suited for one reason or another.


Top
 Profile  
 
 Post subject: Re: Kernel Heap Design Questions
PostPosted: Sun Aug 27, 2023 10:29 am 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 403
stillconfused wrote:
Hello, I have some questions about my kernel heap, and related things.
Firstly, what should, and shouldn't I be using my kernel heap allocator for?


You should us the heap for anything that cannot be statically sized at compile time.

In older kernels, tables of data structures like all the processes, were statically sized and compiled into the kernel image at build time, making them very difficult to change and tune (basically, at least a small re-compile and relink was required to change any statically sized table.)

Not only that, but the tables would have to be sized for the worst case, wasting memory in the average case.

Instead, just allocate your process structures (and any other dynamic structure) on your heap.

stillconfused wrote:
During the boot process I initialize my kernel heap with a large block of memory, and then use this to allocate memory for all kernel space operations.
I'm writing this kernel for RISC-V, and I don't use a higher-half kernel design. My kernel has a trampoline design like Minix, or XV6 to transfer control between userspace and the kernel. Userspace processes have a stack/heap mapped at arbitrary high addresses, but the kernel stack/heap are identity mapped in kernel space, so that I can easily allocate memory for things like user process page tables, or VirtIO queues where I need a physical address.


The problem with using 1:1 virtual/physical mapping for heap memory is that you have to size and allocate all your heap memory up front, whether you use it or not.

If you instead create your virtual memory space, without backing the memory with physical memory, you can allocate physical memory to your heap on demand.

Otherwise, you're little better off than with statically sized tables.

The other option is to not have a single contiguous heap, but instead use a slab allocator, with each slab sliced into equal sized memory slots. Then, you can new allocate physical pages when you need a new slab, and your heap need not be contiguous.


Top
 Profile  
 
 Post subject: Re: Kernel Heap Design Questions
PostPosted: Sun Aug 27, 2023 8:24 pm 
Offline

Joined: Fri Nov 08, 2019 5:35 pm
Posts: 16
Thank you for your reply!

thewrongchristian wrote:
In older kernels, tables of data structures like all the processes, were statically sized and compiled into the kernel image at build time...

I started off doing things this way. I figured there might be some benefit in knowing ahead of time what your worst-case memory usage would be, but allocating every single structure (Process Control Blocks, Memory Space mapping structures, etc) ballooned in size very quickly.

thewrongchristian wrote:
The other option is to not have a single contiguous heap, but instead use a slab allocator, with each slab sliced into equal sized memory slots. Then, you can new allocate physical pages when you need a new slab, and your heap need not be contiguous.

I see the benefit of what you're talking about. My current heap implementation allows for non-contiguous zones, but it's not a perfect system.

Something else I'm having trouble with is the large amount of fragmentation caused by page-aligned allocations. My allocator creates an 8-byte allocation header before the allocated address, and if I'm allocating a large number of page-aligned structures on the kernel heap, I end up with a very fragmented memory space. I'm using these page-aligned allocations for structures that need to be mapped into a process' virtual memory space, such as their stack/heap. I've wanted to avoid having alternate allocators for special purposes like Linux has. Should I just use my PMM for allocating these blocks of page-aligned memory?


Top
 Profile  
 
 Post subject: Re: Kernel Heap Design Questions
PostPosted: Mon Aug 28, 2023 4:24 am 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 403
stillconfused wrote:
Something else I'm having trouble with is the large amount of fragmentation caused by page-aligned allocations. My allocator creates an 8-byte allocation header before the allocated address, and if I'm allocating a large number of page-aligned structures on the kernel heap, I end up with a very fragmented memory space. I'm using these page-aligned allocations for structures that need to be mapped into a process' virtual memory space, such as their stack/heap. I've wanted to avoid having alternate allocators for special purposes like Linux has. Should I just use my PMM for allocating these blocks of page-aligned memory?


Personally, I don't think anything large and page aligned should be using the heap, so yes, just use your PMM to get whole pages and map them as appropriate.


Top
 Profile  
 
 Post subject: Re: Kernel Heap Design Questions
PostPosted: Tue Aug 29, 2023 4:39 pm 
Offline

Joined: Fri Nov 08, 2019 5:35 pm
Posts: 16
Thank you very much for your help! I really appreciate it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 34 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group