Skip to content

Use a lower minimum expansion size for non-BS Data memory types - #1169

Merged
makubacki merged 7 commits into
OpenDevicePartnership:mainfrom
makubacki:reduce_mem_type_min_expansion
Dec 11, 2025
Merged

makubacki merged 7 commits into
OpenDevicePartnership:mainfrom
makubacki:reduce_mem_type_min_expansion

Conversation

@makubacki

@makubacki makubacki commented Dec 6, 2025 •

Copy link
Copy Markdown
Collaborator

Description

Replace the MIN_EXPANSION constant with memory-type-specific expansion sizes to optimize memory usage. Boot Services Data allocators now use HIGH_TRAFFIC_ALLOC_MIN_EXPANSION (1MB), runtime memory types use LOW_TRAFFIC_RUNTIME_ALLOC_MIN_EXPANSION (uses runtime granularity), and other types use LOW_TRAFFIC_ALLOC_MIN_EXPANSION (page size / 4KB).


A few other changes are included to better separate these allocator types:

  1. Do not have UefiAllocator directly depend on SpinLockedFixedSizeBlockAllocator. Instead take the parameter as a generic type to decouple the types.
  2. Add a PageAllocator trait that defines the interface UefiAllocator needs to use an allocator. Have SpinLockedFixedSizeBlockAllocator implement this trait.
  3. Because the SpinLockedFixedSizeBlockAllocator type is no longer fixed (e.g. SpinLockedFixedSizeBlockAllocator<A> vs SpinLockedFixedSizeBlockAllocator<B>), a single static array cannot be used to hold all of the static allocators. This change adds a macro module to hold macros that make iterating across the diverse allocator types more ergonomic.

A follow up change will be made to simplify overall allocator design and management, tracked in #1174.


  • Impacts functionality?
  • Impacts security?
  • Breaking change?
  • Includes tests?
  • Includes documentation?

How This Was Tested

  • cargo make all
  • QEMU platform boot to EFI shell

Integration Instructions

  • N/A

@makubacki
makubacki requested review from joschock and os-d December 6, 2025 00:35
@makubacki makubacki self-assigned this Dec 6, 2025
@github-actions github-actions Bot added the impact:testing Affects testing label Dec 6, 2025
@joschock

joschock commented Dec 6, 2025

Copy link
Copy Markdown
Contributor

You could use a const-generic for this:

pub struct FixedSizeBlockAllocator<const MIN_EXPANSION:usize> {
...
}
...
impl<const MIN_EXPANSION:usize> FixedSizeBlockAllocator<MIN_EXPANSION> {
...
}

#[cfg_attr(target_os = "uefi", global_allocator)]
pub(crate) static EFI_BOOT_SERVICES_DATA_ALLOCATOR: UefiAllocator<SIZE_1MB> = UefiAllocator::new(
    &GCD,
    NonNull::from_ref(GCD.memory_type_info(efi::BOOT_SERVICES_DATA)),
    protocol_db::EFI_BOOT_SERVICES_DATA_ALLOCATOR_HANDLE,
    DEFAULT_PAGE_ALLOCATION_GRANULARITY,
);

etc.

@makubacki
makubacki force-pushed the reduce_mem_type_min_expansion branch from 1a02589 to 1924899 Compare December 6, 2025 02:01
@codecov

codecov Bot commented Dec 6, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.99099% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
patina_dxe_core/src/allocator.rs 85.92% 10 Missing ⚠️

📢 Thoughts on this report? Let us know!

@os-d os-d left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While we are here, is there already a check (I think not based on the issue that led us to here) to make sure that we aren’t blowing buckets? It would be worth a print and debug assert when a bucketized allocator has to expand. Doesn’t need to be done here, but is related to the issue. That could go with a behavior change such that the bucketized allocators just hold onto the entire bucket memory and then service pool and pages directly from it and are never expected to expand.

As I’m typing this, this is a bigger change, so definitely not for here, but @joschock thoughts on this?

@makubacki
makubacki force-pushed the reduce_mem_type_min_expansion branch from 1924899 to 24f2ace Compare December 8, 2025 23:20
@joschock

joschock commented Dec 8, 2025

Copy link
Copy Markdown
Contributor

While we are here, is there already a check (I think not based on the issue that led us to here) to make sure that we aren’t blowing buckets? It would be worth a print and debug assert when a bucketized allocator has to expand. Doesn’t need to be done here, but is related to the issue.

On the platform side we have https://github.com/microsoft/mu_plus/blob/05554be4a2ee7cc35f401c46492e02746c323c9d/MsCorePkg/Library/MemoryTypeInformationChangeLib/MemoryTypeInformationChangeLib.c#L25 to report bucket changes in log and telemetry, and we write tasks for various projects on the regular to tune the buckets to match.

While we definitely need tools to allow us to tune the buckets, these exist. So I'm not sure it is worth making the allocators more complex or adding further constraints on their behavior is worth it.

@makubacki

Copy link
Copy Markdown
Collaborator Author

I'm going to explore simply storing the min expansion size in a field in the allocator to reduce some complexity around the static type management for the const generic in the current PR. As I added the commit for the const generic type parameter onto the original commit in the PR branch, I'll do the same for this to show the delta for the different approaches.

@makubacki

Copy link
Copy Markdown
Collaborator Author

I'm going to explore simply storing the min expansion size in a field in the allocator to reduce some complexity around the static type management for the const generic in the current PR. As I added the commit for the const generic type parameter onto the original commit in the PR branch, I'll do the same for this to show the delta for the different approaches.

Pushed in the latest commit

@makubacki
makubacki force-pushed the reduce_mem_type_min_expansion branch from c04f446 to a6b246a Compare December 10, 2025 00:30
Comment thread patina_dxe_core/src/allocator.rs Resolved
Comment thread patina_dxe_core/src/allocator.rs Resolved
@makubacki
makubacki force-pushed the reduce_mem_type_min_expansion branch 2 times, most recently from 2310302 to 7e9dff4 Compare December 11, 2025 14:26
Replace the `MIN_EXPANSION` constant with memory-type-specific
expansion sizes to optimize memory usage. Boot Services Data
allocators now use `HIGH_TRAFFIC_ALLOC_MIN_EXPANSION` (1MB) while
other memory types use `LOW_TRAFFIC_ALLOC_MIN_EXPANSION` (4KB).

A `min_expansion_for_memory_type()` helper function is used to
centralize the mapping logic making it easier to adjust expansion
sizes in the future if needed.

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
Simplify type management by simply storing the minimum expansion
value directly in the allocator type.
@makubacki
makubacki force-pushed the reduce_mem_type_min_expansion branch from 7e9dff4 to a78d403 Compare December 11, 2025 20:03
@makubacki
makubacki enabled auto-merge (squash) December 11, 2025 20:05
@makubacki
makubacki merged commit 287e42b into OpenDevicePartnership:main Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

impact:testing Affects testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants