Skip to content

mmap/unix.rs: Add with_minimum_alignment to MmapRegionBuilder#375

Open
jaredwhitedev wants to merge 2 commits intorust-vmm:mainfrom
jaredwhitedev:alignment-option
Open

mmap/unix.rs: Add with_minimum_alignment to MmapRegionBuilder#375
jaredwhitedev wants to merge 2 commits intorust-vmm:mainfrom
jaredwhitedev:alignment-option

Conversation

@jaredwhitedev
Copy link
Copy Markdown

Add an option to specify a minimal alignment for the mmap'd region. This is useful to ensure VM memory backings get huge EPT entries.

Summary of the PR

Please summarize here why the changes in this PR are needed.
To get huge EPT entries callers must be able to align their memory to larger page sizes, such as 2 MiB or 1 GiB.
Using huge EPT entries increases the memory performance by decreasing the TLB pressure.

Expose a with_minimum_alignment option to MmapRegionBuilder to allow callers to do so.

To support this, we first allocate size+alignment_size of virtual address space, then use MAP_FIXED to place the actual mapping at an aligned address within the reserved VA space. We then free the excess

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@jaredwhitedev
Copy link
Copy Markdown
Author

Seems that we can't allocate with more than a 512 MiB alignment when using Miri. Would you rather we:
a) Try best effort for Miri (Let the allocation failure happen whenever caller uses more than 512 MiB alignment). Current behavior.
b) Don't support Miri when using with_alignment (explicit error)
c) Ignore it when using Miri (default 8 byte alignment)

src/mmap/unix.rs Outdated

/// Create the `MmapRegion` object with the specified minimum alignment.
/// `min_align` must be a power of two and at least the system page size.
pub fn with_minimum_alignment(mut self, min_align: usize) -> Self {
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.

Non-conformant values are rejected in the build function, but I think it will be better to reject them here. This function can return a Result<Self>. In the error case you return InvalidAlignment.

This is a cleaner approach. Users of the alignment value don't need to validate it again.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated

Add an option to specify a minimal alignment for the mmap'd region.
This is useful to ensure VM memory backings get huge EPT entries.

Signed-off-by: Jared White <git@jaredwhite.dev>
@jaredwhitedev jaredwhitedev force-pushed the alignment-option branch 2 times, most recently from 19deda4 to a6229cb Compare April 7, 2026 05:33
Signed-off-by: Jared White <git@jaredwhite.dev>
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