[PATCH v2 0/3] rust: alloc: add Vec shrinking methods

Shivam Kalra posted 3 patches 6 days, 15 hours ago
drivers/android/binder/context.rs |  10 ++
rust/kernel/alloc/kvec.rs         | 176 ++++++++++++++++++++++++++++++
2 files changed, 186 insertions(+)
[PATCH v2 0/3] rust: alloc: add Vec shrinking methods
Posted by Shivam Kalra 6 days, 15 hours ago
This is a resend of v1 [1] to fix threading issues - patches 2-3 were
sent without In-Reply-To headers due to an email quota issue.

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: Adds shrink_to(min_capacity, flags) and shrink_to_fit(flags)
  methods that reallocate to a smaller buffer when beneficial.
- Patch 2: Adds KUnit tests for the new methods.
- Patch 3: 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.

This series depends on:
  https://lore.kernel.org/lkml/20260130182842.217821-1-shivamklr@cock.li/

Testing:
- KUnit tests pass (rust_kvec test suite).
- Kernel boots successfully in QEMU with CONFIG_ANDROID_BINDER_IPC_RUST=y.

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/

Shivam Kalra (3):
  rust: alloc: Add shrink_to and shrink_to_fit methods to Vec
  rust: alloc: add KUnit tests for Vec shrinking
  binder: context: shrink all_procs vector to reclaim memory

 drivers/android/binder/context.rs |  10 ++
 rust/kernel/alloc/kvec.rs         | 176 ++++++++++++++++++++++++++++++
 2 files changed, 186 insertions(+)

-- 
2.43.0