Skip to content

Memory management overhaul#50

Merged
jastintime merged 61 commits intotrunkfrom
remexre/mm
Jan 12, 2026
Merged

Memory management overhaul#50
jastintime merged 61 commits intotrunkfrom
remexre/mm

Conversation

@remexre
Copy link
Member

@remexre remexre commented Sep 21, 2025

On top of #59; merge that one first, then rebase this.


  • Revamps the memory map to have a single large region of identity-mapped physical memory, and a single large region of RAM. All the previous special regions (beyond the kernel itself) are just part of one of these two.
  • Adds a new Devicetree parser that heap-allocates the parsed structure, so the physical memory contained in it can safely be reused.
  • Adds a new and much simpler physical memory allocator.
  • Adds a virtual memory allocator.
  • Adds a heap allocator based on the design from Mimalloc: Free List Sharding in Action.
  • Adds some documentation for the above.
    • The mimalloc paper is the "real" documentation for the heap allocator; this code pretty directly follows its design, and divergences are commented on.
    • I'm open to adding more documentation, if there's something that users of the allocators need to know.

There are a couple remaining bugs I'm not fixing now:

  • Race conditions after freeing from a different hart than allocated memory. The way the delayed free list is described in the paper seems to inherently leave a gap for a race, unless there's extra synchronization not described in the paper.
    • The entire remote free path is TODO()ed out anyway; we've got a bunch of places where we're going to have to make fixes to support multiple harts, so this is just one more for the list.
  • Performance improvements; the way randomness is used is slow, and Add a notion of debug builds and debug assertions. #62's debug assertions should be applied to everything in the hot paths of alloc and free.
  • A circularity between vma_alloc and alloc. This can probably get resolved by having the kernel virtual allocator pool some memory to use in this case, but I'm going to wait until we have a reproducer before doing this.
  • vma_alloc_aligned can spuriously fail when low of virtual address space. Again, I'll wait for a reproducer; this is tricky to trigger in the kernel anyway. Fixing this without a performance regression could be tricky, too; we don't want virtual address space allocation to go from O(log2 n) to O(n) when we have lots of fragmentation, that's a DoS issue.

I'll file these as issues once this is passing review, if none of them seem like blockers to a merge.


Resolves #12.
Resolves #27.

@remexre remexre force-pushed the remexre/mm branch 7 times, most recently from db85c88 to 369d8a2 Compare September 28, 2025 10:14
@remexre remexre force-pushed the remexre/mm branch 3 times, most recently from 4a22a12 to ad4db7e Compare October 1, 2025 23:27
@remexre remexre force-pushed the remexre/mm branch 2 times, most recently from 8211b11 to 1595383 Compare October 7, 2025 09:08
@remexre remexre force-pushed the remexre/mm branch 5 times, most recently from d2d8a31 to 6444554 Compare October 25, 2025 15:42
@remexre
Copy link
Member Author

remexre commented Oct 25, 2025

Undrafting this; after 1363024, the allocator itself works, and the rest of the changes are definitely reviewable.

This might make sense to split up; if a potential reviewer would like me to do so, I can.

@remexre remexre marked this pull request as ready for review October 25, 2025 17:08
@remexre
Copy link
Member Author

remexre commented Oct 25, 2025

Okay, I think this is fully ready for review and merging.

@jastintime
Copy link
Contributor

started review, devicetree stuff skipped over for now, I need to read up on it prior to reviewing it.

Copy link
Contributor

@jastintime jastintime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finished review, src/kernel/devicetree.c and src/kernel/include/devicetree.h aren't reviewed since I don't know enough about devicetree to do so.

@remexre remexre force-pushed the remexre/mm branch 3 times, most recently from 90b1f0e to 46d18f8 Compare January 8, 2026 20:39
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
At this point, allocation works but freeing is a no-op.

Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
- page_collect shouldn't assume that the page has an empty free list,
  but neither should it do O(n) work when it does not.
- After page_free, alloc_generic should not refer to the page anymore.

Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
…data structures.

Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
@jastintime jastintime added this pull request to the merge queue Jan 12, 2026
Merged via the queue into trunk with commit ecd7f5e Jan 12, 2026
13 checks passed
@jastintime jastintime deleted the remexre/mm branch January 12, 2026 23:18
@remexre remexre mentioned this pull request Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid /reserved-memory DeviceTree nodes. Finish the allocator.

2 participants