Re: [PATCH v4 3/3] rust_binder: shrink all_procs when deregistering processes
From: Shivam Kalra
Date: Sat Feb 14 2026 - 02:43:12 EST
On 13/02/26 21:49, Alice Ryhl wrote:
> On Thu, Feb 12, 2026 at 01:47:09PM +0530, Shivam Kalra wrote:
>> When a process is deregistered from the binder context, the all_procs
>> vector may have significant unused capacity. Add logic to shrink the
>> vector using a conservative strategy that prevents shrink-then-regrow
>> oscillation.
>> The shrinking strategy triggers when length drops below 1/4 of capacity,
>> and shrinks to 1/2 of capacity rather than to the exact length. This
>> provides hysteresis to avoid repeated reallocations when the process
>> count fluctuates.
>> 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.
>>
>> Suggested-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
>> Signed-off-by: Shivam Kalra <shivamkalra98@xxxxxxxxxxx>
>> ---
>> drivers/android/binder/context.rs | 11 +++++++++++
>> 1 file changed, 11 insertions(+)
>>
>> diff --git a/drivers/android/binder/context.rs b/drivers/android/binder/context.rs
>> index 9cf437c025a20..399dab475728b 100644
>> --- a/drivers/android/binder/context.rs
>> +++ b/drivers/android/binder/context.rs
>> @@ -94,6 +94,17 @@ 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 to avoid memory waste,
>> + // but use a conservative strategy to prevent shrink-then-regrow oscillation.
>> + // Only shrink when length drops below 1/4 of capacity, and shrink to 1/2 capacity.
>> + let len = manager.all_procs.len();
>> + let cap = manager.all_procs.capacity();
>> + if len < cap / 4 {
>> + // Shrink to half capacity. Ignore allocation failures since this is just an
>> + // optimization; the vector remains valid even if shrinking fails.
>> + let _ = manager.all_procs.shrink_to(cap / 2, GFP_KERNEL);
>
> Not a big deal, but perhaps we should write len*2 here instead of cap/2?
> That way, if the cap got way far away from the length, it would equalize
> right away.
>
> Alice
Thanks for the feedback. Will add this in v5.