Skip to content

Support fallible malloc attempts without OOM handling#43

Open
hcsch wants to merge 2 commits intoSFBdragon:masterfrom
hcsch:try-without-oom-handler
Open

Support fallible malloc attempts without OOM handling#43
hcsch wants to merge 2 commits intoSFBdragon:masterfrom
hcsch:try-without-oom-handler

Conversation

@hcsch
Copy link
Copy Markdown

@hcsch hcsch commented Aug 27, 2025

When allocating with Talc for a balloon device allocations should be
multiples of 4KiB in size and must be aligned to 4KiB.
To reduce per-allocation overhead we want to group such allocations as
much as possible into large chunks.
Allocation of such almost as large as possible chunks below a maximum size
can be accomplished by attempting allocations in decreasing powers of
two below the maximum size.
These allocation attempts would however trigger the OOM handler which is
undesirable. The balloon driver is also responsible for OOM handling in
my case and since we are already within balloon driver code, making
these allocation attempts, the driver is already locked. The OOM handler
therefore cannot (and should not) deflate the balloon, because the
driver state is locked.
Thus we want to be able to make allocation attempts without triggering
the OOM handler.

This includes a drive-by fix for the derived Hash impl conflicting with the explicit PartialEq implementation of Span. They differed on equality for empty Spans AFAICT.

Hash all empty spans to the same value to align with the `PartialEq`
implementation.

The derived `Hash` was causing clippy to throw an error.
See https://rust-lang.github.io/rust-clippy/master/index.html#derived_hash_with_manual_eq
@SFBdragon
Copy link
Copy Markdown
Owner

SFBdragon commented Aug 28, 2025 via email

@hcsch
Copy link
Copy Markdown
Author

hcsch commented Aug 30, 2025

Hey, thanks for the positive response :D

Being able to query Talc for the largest still possible contiguous allocation would be great as well, and IMO even feels a bit cleaner.

I wrote this change to Talc in the process of my bachelor's thesis so I was a bit limited in time for delving into how talc does things, which is why I picked the current approach.

When allocating with Talc for a balloon device allocations should be
multiples of 4KiB in size and must be aligned to 4KiB.
To reduce per-allocation overhead we want to group such allocations as
much as possible into large chunks.
Allocation of such almost as large as possible chunks below a maximum size
can be accomplished by attempting allocations in decreasing powers of
two below the maximum size.
These allocation attempts would however trigger the OOM handler which is
undesirable. The balloon driver is also responsible for OOM handling in
my case and since we are already within balloon driver code, making
these allocation attempts, the driver is already locked. The OOM handler
therefore cannot (and should not) deflate the balloon, because the
driver state is locked.
Thus we want to be able to make allocation attempts without triggering
the OOM handler.
@hcsch hcsch force-pushed the try-without-oom-handler branch from df18c6c to be81838 Compare October 18, 2025 19:56
@hcsch
Copy link
Copy Markdown
Author

hcsch commented Oct 18, 2025

Just noticed that I still had the old try_malloc name in a doc comment. I fixed that to use the malloc_without_oom_handler name in the force push just now.

hcsch added a commit to hcsch/hermit-kernel that referenced this pull request Oct 20, 2025
Talc previously did not support allocations without attempting to use
the OOM handler to recover. For the balloon device to allocate pages for
the balloon however we want to just probe for and then allocate a chunk
of pages. If the allocation fails we don't want to recover old memory,
we just want to allocate a smaller chunk (since recovery with the
balloon will be shrinking the balloon, causing us to uselessly shuffle
around pages, possibly even failing).

See SFBdragon/talc#43
which is the PR for merging this modification upstream. Another API
function may be added for querying the current largest possible new
allocation in the future. In that case the balloon driver code should be
changed to use that instead of probing allocatable size in powers of
two.
@SFBdragon
Copy link
Copy Markdown
Owner

try_allocate is now available in 5.0.0-beta.1.

I haven't implemented a function to get one/iterate available chunk sizes from the top down though yet. Should be easy to implement, and I'm happy to do it if it's still desirable.

Leaving this open for now.

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.

2 participants