[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20260131080614.264242-2-shivamklr@cock.li>
Date: Sat, 31 Jan 2026 13:36:14 +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 3/3] binder: context: shrink all_procs vector to reclaim memory
After removing a process from the all_procs list, shrink the vector
if it has significant unused capacity. Specifically, if the capacity
exceeds 128 and the length is less than half the capacity, shrink
the vector to its current length.
This prevents unbounded memory retention in long-running systems where
the number of binder processes fluctuates significantly over time.
Link: https://lore.kernel.org/rust-for-linux/aW4DWebqCg1c8Fgb@google.com/
Suggested-by: Alice Ryhl <aliceryhl@...gle.com>
Signed-off-by: Shivam Kalra <shivamklr@...k.li>
---
drivers/android/binder/context.rs | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/android/binder/context.rs b/drivers/android/binder/context.rs
index 9cf437c025a20..f2505fbf17403 100644
--- a/drivers/android/binder/context.rs
+++ b/drivers/android/binder/context.rs
@@ -94,6 +94,16 @@ pub(crate) fn deregister_process(self: &Arc<Self>, proc: &Arc<Process>) {
}
let mut manager = self.manager.lock();
manager.all_procs.retain(|p| !Arc::ptr_eq(p, proc));
+
+ // Shrink the vector if it has significant unused capacity.
+ // Only shrink if capacity > 128 to avoid repeated reallocations for small vectors.
+ let len = manager.all_procs.len();
+ let cap = manager.all_procs.capacity();
+ if cap > 128 && len < cap / 2 {
+ // Shrink to current length. Ignore allocation failures since this is just an
+ // optimization; the vector remains valid even if shrinking fails.
+ let _ = manager.all_procs.shrink_to(len, GFP_KERNEL);
+ }
}
pub(crate) fn set_manager_node(&self, node_ref: NodeRef) -> Result {
--
2.43.0
Powered by blists - more mailing lists