[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260130205424.261700-1-shivamklr@cock.li>
Date: Sat, 31 Jan 2026 02:24:20 +0530
From: Shivam Kalra <shivamklr@...k.li>
To: dakr@...nel.org,
cmllamas@...gle.com,
gregkh@...uxfoundation.org
Cc: aliceryhl@...gle.com,
rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
Shivam Kalra <shivamklr@...k.li>
Subject: [PATCH v1 0/3] rust: alloc: add Vec shrinking methods
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 - e.g., a driver managing processes that spikes
to thousands and drops back down.
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, to
avoid repeated reallocations for small process lists.
Testing:
- KUnit tests pass (rust_kvec test suite).
- Kernel boots successfully in QEMU with CONFIG_ANDROID_BINDER_IPC_RUST=y.
- The binder driver loads and logs "rust_binder: Loaded Rust Binder."
This series depends on commit a2477dc74d871 ("rust_binder: fix needless
borrow in context.rs") which is currently pending on char-misc-next.
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(+)
base-commit: a2477dc74d871ed4e5c20d9160586dc475182674
--
2.43.0
Powered by blists - more mailing lists