Skip to content

rust: alloc: short-circuit zero-size realloc of null pointers - #4321

Merged
RyanTheTide merged 1 commit into
aurora-silicon:mainfrom
satchlj:satchlj/rust-alloc-zero-size-fastpath
Dec 4, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:mainfrom
satchlj:satchlj/rust-alloc-zero-size-fastpath

Conversation

@satchlj

@satchlj satchlj commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Tested: J775c (Apple M5 Max, T6050, G17C B1) at b85b6c3: 6-arm ABCCBA (Base1 -> C1 -> D1 -> D2 -> C2 -> Base2, default cmdline), 0 failed units, 0 GPU faults, 0 capacity refusals, dEQP-VK.api.smoke.* 8/8 and dEQP-VK.synchronization.smoke.* 12/12 passed.
Risk: two-way · all Macs (generic Rust kernel allocator zero-size fastpath).

Summary

KVec::drop() calls A::free(self.ptr, self.layout) unconditionally, even when self.layout.size() == 0 (every empty KVec::new() or KVec::with_capacity(0)). ReallocFunc::call() normalizes old_layout.size() == 0 to ptr = NULL and still invokes C FFI krealloc_node_align / vrealloc_node_align / kvrealloc_node_align with (NULL, 0), which immediately calls kfree(NULL) / vfree(NULL) and returns a pointer that ReallocFunc::call() discards before returning dangling_from_layout(layout).

Zero-size allocations from None take the same unnecessary C FFI trip.

Return dangling_from_layout(layout) directly in ReallocFunc::call() when size == 0 && ptr.is_null(), eliminating the C FFI call and kfree(NULL) on every empty KVec drop and zero-size allocation, and add test_zero_size KUnit coverage in rust/kernel/alloc/allocator.rs.

Verification

  • Exact head tested: b85b6c3e7188 on base 56a577e83350 (origin/main at build time), identical kernel config, default kernel command line.
  • Hardware: J775c (Apple M5 Max, T6050, G17C B1), symmetric 6-arm ABCCBA (Base1 -> C1 -> D1 -> D2 -> C2 -> Base2, comparing Base1/Base2 vs C1/C2).
  • Health (both C arms): FAILED_UNITS=0, clean dmesg (0 GPU faults, 0 WARN/Oops), 0 capacity refusals across all 16 sites, dEQP-VK.api.smoke.* 8/8 passed, dEQP-VK.synchronization.smoke.* 12/12 passed.
  • Kernel CPU profile (perf on compute.light d=32, gpubench-vk):
    • kfree: 2.14% (2.30%, 1.98%) -> 0.00% (0.00%, 0.00%)
    • krealloc_node_align_noprof: 0.94% (0.77%, 1.11%) -> 0.00% (0.00%, 0.00%)
  • Benchmark deltas (Base mean vs C mean, * = zero overlap between arm pairs):
    • render.light d=4: 28.35 -> 28.14 us (-0.7%, gpu_gap_us -2.6%)
    • render.light d=8: 23.54 -> 23.24 us (-1.3%, submit_cpu_us -1.3%, gpu_gap_us -1.6%*)
    • render.light d=32: 22.74 -> 22.50 us (-1.1%, submit_cpu_us -1.8%)
    • compute.light d=4: 20.77 -> 20.52 us (-1.2%*)
    • compute.light d=8: submit_cpu_us 12.93 -> 12.85 us (-0.6%*)
    • rt.scene[light] d=2: submit_cpu_us 33.50 -> 32.35 us (-3.4%*), fps 8497 -> 8579 (+1.0%)
    • render.heavy[fill, 1080p] d=8: submit_cpu_us 25.04 -> 23.39 us (-6.6%*), throughput flat (60.07 -> 60.10 us)
    • render.heavy[fill, 1080p] d=1: 193.72 -> 195.16 us (+0.7%, within single-submit idle wake variance); d=4/8 and geom d=1/4/8 flat (0.0% to +0.4%).
  • Untested on hardware: Other Apple Silicon SoCs (change is generic kernel Rust allocator code, covered by rust_allocator KUnit).

@satchlj
satchlj requested review from a team as code owners December 4, 2026 13:57
@github-actions github-actions Bot added area:rust Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Merge readiness at f9590a92: ready to merge

Check Detail
CI at head ok 9 checks passed
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head ok the PR body at b85b6c3e; the changed files are byte-identical at f9590a92
Conflicts ok none
PR text ok Risk: two-way · all Macs (generic Rust kernel allocator zero-size fastpath).

Areas: area:rust
Merge: gh pr merge 4321 --merge (Owners and Core through the bypass: add --admin).

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@satchlj satchlj closed this Dec 4, 2026
@satchlj satchlj reopened this Dec 4, 2026
Every empty KVec::drop() calls A::free(ptr, old_layout) unconditionally.
ReallocFunc::call() turns old_layout.size() == 0 into a NULL pointer and
still invokes C FFI krealloc_node_align/vrealloc_node_align/kvrealloc_node_align
with (NULL, 0), which immediately calls kfree(NULL)/vfree(NULL) and returns
a value that ReallocFunc::call() discards before returning
dangling_from_layout(layout).

Zero-size allocations from None take the same unnecessary FFI trip.

Return dangling_from_layout(layout) directly when size == 0 and the
normalized pointer is NULL, avoiding the FFI call and kfree(NULL) on
every empty KVec drop and zero-size allocation.

Signed-off-by: Satya Benson <satya@benson.earth>
@satchlj
satchlj force-pushed the satchlj/rust-alloc-zero-size-fastpath branch from b85b6c3 to f9590a9 Compare December 4, 2026 15:02
@satchlj

satchlj commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

Rebased onto current main (c3260b7c09c0, post-#4323) so Host tests/host-tests runs against the fixed test-vm-slots.py fixture. Identical diff in rust/kernel/alloc/allocator.rs.

  • Old head: b85b6c3e7188
  • New head: f9590a92dbb8

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Dec 4, 2026

@DjDeveloperr DjDeveloperr 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.

Reviewed f9590a92dbb8. The early return preserves the aligned empty slice for a zero-size request with a normalized null pointer. Shrinking a live allocation to zero still calls the underlying allocator to free it. The new KUnit case covers alignment and empty allocations across Kmalloc, Vmalloc and KVmalloc. The allocator file is byte-identical to the J775c-tested head, and the VM-slot host fixture passed 7/7 locally at this rebased head. The current hosted host and Apple build/KUnit runs cover the new base.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 4, 2026
@RyanTheTide
RyanTheTide merged commit 399152a into aurora-silicon:main Dec 4, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:rust Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants