This series adds shrink_to() and shrink_to_fit() methods to Vec<T, A>
(and by extension KVVec, KVec, VVec) to allow explicit capacity
reduction for memory reclamation.
Problem:
When elements are removed from a KVVec, the allocated capacity is not
reduced. The underlying C allocators (krealloc/kvrealloc) don't shrink
memory in-place. This can cause significant memory waste in scenarios
with variable workloads.
Solution:
- Patch 1: Introduces the `Shrinkable` marker trait to identify allocators
that can meaningfully reclaim memory when shrinking (e.g., Vmalloc but
not Kmalloc, since Kmalloc uses fixed-size buckets).
- Patch 2: Implements shrink_to(min_capacity, flags) and
shrink_to_fit(flags) methods. Shrinking only occurs when
the allocator is shrinkable AND the operation would free at least
one page of memory.
- Patch 3: Adds KUnit tests for the new methods across different allocator
backends (Vmalloc and KVmalloc).
- Patch 4: Uses shrink_to() in the Rust binder driver to reclaim memory
when processes are deregistered. Shrinking is triggered only when the
vector capacity exceeds 128 elements and less than half is used.
Testing:
- KUnit tests pass (rust_kvec test suite).
- Kernel boots successfully in QEMU with CONFIG_ANDROID_BINDER_IPC_RUST=y.
Changes since v2:
- Introduced new `Shrinkable` marker trait to distinguish allocators that
benefit from shrinking (Vmalloc) from those that don't (Kmalloc).
- Shrinking now only occurs for vmalloc-backed buffers when at least one
page can be freed, avoiding unnecessary work for kmalloc buffers.
(Suggested by Danilo Krummrich)
- Split into 4 patches: Shrinkable trait introduction is now a
separate patch to improve reviewability.
- Uses explicit alloc+copy+free since vrealloc doesn't yet support
in-place shrinking; TODO added for future optimization when vrealloc
gains that capability. (Discussed with Danilo Krummrich)
- Fixed minor KVVec/KVec terminology (binder uses KVVec not KVec).
- Expanded KUnit tests to cover different allocator backends and
page-boundary shrinking behavior.
Changes since v1:
- Resend with correct threading (no code changes).
- Removed base-commit.
- Added explicit lore link to dependency in cover letter.
[1] https://lore.kernel.org/lkml/20260130205424.261700-1-shivamklr@cock.li/
[2] https://lore.kernel.org/lkml/20260131154016.270385-1-shivamklr@cock.li/
Signed-off-by: Shivam Kalra <shivamklr@cock.li>
---
Shivam Kalra (4):
rust: alloc: introduce Shrinkable trait
rust: kvec: implement shrink_to and shrink_to_fit for Vec
rust: alloc: add KUnit tests for Vec shrink operations
rust: binder: shrink all_procs when deregistering processes
drivers/android/binder/context.rs | 10 ++
rust/kernel/alloc/allocator.rs | 48 +++++++
rust/kernel/alloc/kvec.rs | 296 +++++++++++++++++++++++++++++++++++++-
3 files changed, 352 insertions(+), 2 deletions(-)
---
base-commit: 3c4ae63073d84abee5d81ce46d86a94e9dae9c89
change-id: 20260207-binder-shrink-vec-v3-54045d77b0e7
Best regards,
--
Shivam Kalra <shivamklr@cock.li>