Skip to content

[kernel] E: Batch page allocation in ELF loader#16

Draft
esaurez wants to merge 1 commit into
devfrom
enhancement-elf-batch-alloc
Draft

[kernel] E: Batch page allocation in ELF loader#16
esaurez wants to merge 1 commit into
devfrom
enhancement-elf-batch-alloc

Conversation

@esaurez

@esaurez esaurez commented Apr 22, 2026

Copy link
Copy Markdown
Owner

Replica of nanvix#2042.

Summary

Batch page allocation in the ELF segment loader and chunk-based processing in alloc_upages to reduce the cost of loading large ELF binaries.

Problem

The ELF loader allocates every page individually. For a ~14 MB binary like Python 3.12 (~3,584 pages), this results in ~3,584 separate frame-allocator + page-table operations.

Changes

ELF segment allocation (elf.rs)

Phase 1: Collect contiguous runs of unmapped pages, allocate each run with a single alloc_upages(nframes) call.
Phase 2: Copy segment data into allocated pages.

Chunked alloc_upages (manager.rs)

Process frames in fixed-size chunks (MAX_CHUNK = 32). Improved error handling with rollback_mapped_pages and free_remaining_frames.

Benchmark (Python 3.12 hello world, WHP standalone)

Metric Before After Change
guest_exec 2,757,370 us 2,439,118 us -11.5%

Restructure the ELF segment loader to batch-allocate contiguous page
runs instead of allocating one page at a time via alloc_upages(1).
Pages within each segment are grouped by their clear requirement (data
pages that will be overwritten vs BSS pages that must be zeroed) and
each contiguous run is allocated with a single alloc_upages(nframes)
call.

This reduces the number of frame-allocator and page-table operations
from O(pages) to O(runs), typically 2-3 per segment (one for the data
region and one for the BSS region).

To support large batch sizes without exhausting the 128 KB kernel slab,
alloc_upages is refactored to process frames in fixed-size chunks
(MAX_CHUNK = 32). Each chunk allocates a small Vec that is dropped
before the next chunk begins.  A rollback helper and a
free_remaining_frames helper ensure that both mapped pages and
unprocessed frames are properly cleaned up on error.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

1 participant