[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aYnnBffE9zCe20Ny@google.com>
Date: Mon, 9 Feb 2026 13:54:13 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: shivamklr@...k.li
Cc: Danilo Krummrich <dakr@...nel.org>, Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
Vlastimil Babka <vbabka@...e.cz>, "Liam R. Howlett" <Liam.Howlett@...cle.com>,
Uladzislau Rezki <urezki@...il.com>, Miguel Ojeda <ojeda@...nel.org>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
"Björn Roy Baron" <bjorn3_gh@...tonmail.com>, Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Arve Hjønnevåg" <arve@...roid.com>, Todd Kjos <tkjos@...roid.com>,
Christian Brauner <brauner@...nel.org>, Carlos Llamas <cmllamas@...gle.com>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v3 4/4] rust: binder: shrink all_procs when deregistering processes
On Sat, Feb 07, 2026 at 05:02:50PM +0530, Shivam Kalra via B4 Relay wrote:
> [PATCH v3 4/4] rust: binder: shrink all_procs when deregistering
The usual prefix for Rust Binder changes is rust_binder:, not
rust: binder:.
> From: Shivam Kalra <shivamklr@...k.li>
>
> When a process is deregistered from the binder context, the all_procs
> vector may have significant unused capacity. Add logic to shrink the
> vector when capacity exceeds 128 and usage drops below 50%, reducing
> memory overhead for long-running systems.
>
> The shrink operation uses GFP_KERNEL and is allowed to fail gracefully
> since it is purely an optimization. The vector remains valid and
> functional even if shrinking fails.
>
> 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);
Hmm. This way we need to reallocate immediately if one more process is
added. How about:
if len < cap / 4 {
shrink_to(cap / 2);
}
Alice
Powered by blists - more mailing lists