Skip to content

userspace: introduce coconut-alloc as allocator#927

Draft
n-ramacciotti wants to merge 17 commits into
coconut-svsm:mainfrom
n-ramacciotti:feature/userspace_allocator
Draft

userspace: introduce coconut-alloc as allocator#927
n-ramacciotti wants to merge 17 commits into
coconut-svsm:mainfrom
n-ramacciotti:feature/userspace_allocator

Conversation

@n-ramacciotti

@n-ramacciotti n-ramacciotti commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

This PR introduces heap allocation support in userspace.

The heap is a single 64KiB static block initialized by userlib at startup before calling the main function. Atomic state operations ensure that only a single initialization can occur.
The PR implements the GlobalAlloc trait by exploiting AllocBlock from coconut-alloc, which supports lockless allocations up to 32KiB using atomic operations.

This PR is marked as draft as it depends on the changes from:

  • #4 from coconut-alloc: this removes an unstable feature not available in the current toolchain (1.86.0) and adds a new utility function required in this PR
  • #937: this updates rust toolchain to 1.87.0 as required by coconut-alloc

These changes were originally included in #913, which also introduced test in userspace svsm, but as suggested, I split them into multiple PRs.

All feedback and suggestions are welcome

@stefano-garzarella stefano-garzarella added the needs-rebase The PR needs to be rebased to the latest upstream branch label Dec 19, 2025
@n-ramacciotti n-ramacciotti force-pushed the feature/userspace_allocator branch from 69d697d to 4a8ed09 Compare December 19, 2025 18:28
@n-ramacciotti

Copy link
Copy Markdown
Contributor Author

v2:

  • rebased on latest main

@peterfang peterfang added TSC review pending This PR requires discussion among the TSC before it can be finalized and removed needs-rebase The PR needs to be rebased to the latest upstream branch labels Dec 22, 2025
@peterfang peterfang removed the TSC review pending This PR requires discussion among the TSC before it can be finalized label Jan 6, 2026
@n-ramacciotti n-ramacciotti force-pushed the feature/userspace_allocator branch from 4a8ed09 to 2a36887 Compare January 9, 2026 18:13
@n-ramacciotti

Copy link
Copy Markdown
Contributor Author

v3:

@n-ramacciotti n-ramacciotti force-pushed the feature/userspace_allocator branch from 2a36887 to 622b9b2 Compare January 12, 2026 14:53
@n-ramacciotti

Copy link
Copy Markdown
Contributor Author

v4:

  • Rebased on latest main, which includes rust 1.87.0

@joergroedel joergroedel self-assigned this Jan 12, 2026

@joergroedel joergroedel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution, this looks very promising and I am happy to see progress on the user-mode allocator implementation. Left some comments to consider.

Comment thread user/lib/src/alloc.rs Outdated
Comment thread user/lib/src/alloc.rs Outdated
Comment thread user/lib/src/alloc.rs Outdated
Comment thread user/lib/src/alloc.rs
@joergroedel joergroedel added the in-review PR is under active review and not yet approved label Jan 12, 2026
joergroedel and others added 16 commits January 22, 2026 17:06
Add the wrappers for system calls which change the memory layout of
the user-mode task.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Add conversion support for the user-mode representation of memory
mapping flags to the kernel representation.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Implement methods to retrieve a reference to the FileHandle or
DirectoryHandle the object points to.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Implement the system call to allow user-mode applications to change
their memory layout.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Implement the system call to unmap memory mappings from the virtual
address space.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
These two methods can be implemented for trait VirtualMapping to
support resizing of an existing mapping.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Convert the map_vmm() and unma_vmm() methods to the more generic
map/unmap_range() methods which can handle sub-ranges of VMMs as well.
Re-implement map/unmap_vmm() to use the more generic methods.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Move the TLB flush logic to a helper function to re-use it at other
places.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Add a method to struct VMR to resize an existing mapping.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Support the resizing of VMalloc mappings.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
This is a workaround to use mmap for anonymous mapping.
Currently, mmap looks for obj identifier to identify if
the mapping is anonymous or not. Anonymous mappings for
the moment is based on the fact that id 0 is free, but
it is utilized by the console.

This commit fix this by changing the identifier to 1
and it will be removed when mmap will be updated. It
has been introduced to test the rest of the allocator
logic based on mmap.

Signed-off-by: Nicola Ramacciotti <niko.ramak@gmail.com>
Add dependency for coconut-alloc crate in the workspace

Signed-off-by: Nicola Ramacciotti <niko.ramak@gmail.com>
Introduce a 64 KiB static heap using `AllocBlock` to support dynamic
memory allocation in userspace.

Initialize the global heap in the `init()` function from `userlib` before
calling the `main` function of the crate. Ensure single initialization of
the heap by using `OnceRef`.

Signed-off-by: Nicola Ramacciotti <niko.ramak@gmail.com>
Implement the `GlobalAlloc` trait by wrapping the lockless `alloc`
and `free` operations of `AllocBlock`, which use atomic operations
internally. Since those operation return offset from the start of
the block, convert them to pointers relative to the heap base.

Signed-off-by: Nicola Ramacciotti <niko.ramak@gmail.com>
Implement `layout_from_size()` and `layout_from_ptr()` utilities to
construct `Layout.

Signed-off-by: Nicola Ramacciotti <niko.ramak@gmail.com>
Disable custom global allocator during normal test execution
since the default allocator is sufficient for unit testing

To avoid unused warnings in test builds, move `SvsmUserAllocator`
implementation behind a configuration flag

Add unit tests for layout calculation.

Signed-off-by: Nicola Ramacciotti <niko.ramak@gmail.com>
Allocate a Vec to show that the verify that the userspace
memory allocator works correctly

Signed-off-by: Nicola Ramacciotti <niko.ramak@gmail.com>
@n-ramacciotti n-ramacciotti force-pushed the feature/userspace_allocator branch from 622b9b2 to 0b4a6eb Compare January 22, 2026 17:55
@n-ramacciotti

Copy link
Copy Markdown
Contributor Author

v5:

  • Rebased on latest main, which includes rust 2024 version.
  • Included mmap PR to use mmap syscall.
  • Added commit 047429e as a temporary workaround for the problem of anonymous mapping. More details are in the commit description.
  • Addressed @joergroedel's comments:
    • use mmap for the heap area
    • panic when trying to access a non initialized HEAP
    • remove unnecessary println
    • use OnceRef for single initalization of the heap
  • With these new changes the test function for alloc and dealloc had to be removed since now it is required an online usage.

TODO:

  • implement get_global_heap and set_global_heap on a new AllocBlock available when testing. So it is possible to test alloc and dealloc of the GlobalAlloc trait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in-review PR is under active review and not yet approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants