Repository navigation
rust: alloc: short-circuit zero-size realloc of null pointers - #4321
RyanTheTide merged 1 commit into
Conversation
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
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>
b85b6c3 to
f9590a9
Compare
|
Rebased onto current
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
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.
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 anddEQP-VK.synchronization.smoke.*12/12 passed.Risk: two-way · all Macs (generic Rust kernel allocator zero-size fastpath).
Summary
KVec::drop()callsA::free(self.ptr, self.layout)unconditionally, even whenself.layout.size() == 0(every emptyKVec::new()orKVec::with_capacity(0)).ReallocFunc::call()normalizesold_layout.size() == 0toptr = NULLand still invokes C FFIkrealloc_node_align/vrealloc_node_align/kvrealloc_node_alignwith(NULL, 0), which immediately callskfree(NULL)/vfree(NULL)and returns a pointer thatReallocFunc::call()discards before returningdangling_from_layout(layout).Zero-size allocations from
Nonetake the same unnecessary C FFI trip.Return
dangling_from_layout(layout)directly inReallocFunc::call()whensize == 0 && ptr.is_null(), eliminating the C FFI call andkfree(NULL)on every emptyKVecdrop and zero-size allocation, and addtest_zero_sizeKUnit coverage inrust/kernel/alloc/allocator.rs.Verification
b85b6c3e7188on base56a577e83350(origin/mainat build time), identical kernel config, default kernel command line.Base1 -> C1 -> D1 -> D2 -> C2 -> Base2, comparingBase1/Base2vsC1/C2).FAILED_UNITS=0, cleandmesg(0 GPU faults, 0 WARN/Oops), 0 capacity refusals across all 16 sites,dEQP-VK.api.smoke.*8/8passed,dEQP-VK.synchronization.smoke.*12/12passed.perfoncompute.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%)*= 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_us12.93 -> 12.85 us(-0.6%*)rt.scene[light] d=2:submit_cpu_us33.50 -> 32.35 us(-3.4%*), fps8497 -> 8579(+1.0%)render.heavy[fill, 1080p] d=8:submit_cpu_us25.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/8andgeom d=1/4/8flat (0.0%to+0.4%).rust_allocatorKUnit).