mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shivam Kalra <shivamkalra98@zohomail.in>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
	"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
	"Vlastimil Babka" <vbabka@suse.cz>,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>,
	"Uladzislau Rezki" <urezki@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 3/3] rust_binder: shrink all_procs when deregistering processes
Date: Sat, 14 Feb 2026 13:12:00 +0530	[thread overview]
Message-ID: <75202b71-e5b3-4833-9fa4-e0b279900b49@zohomail.in> (raw)
In-Reply-To: <aY9PG909tCTZgMup@google.com>

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@google.com>
>> Signed-off-by: Shivam Kalra <shivamkalra98@zohomail.in>
>> ---
>>  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.

      reply	other threads:[~2026-02-14  7:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-12  8:17 [PATCH v4 0/3] rust: alloc: add KVVec shrinking method Shivam Kalra via B4 Relay
2026-02-12  8:17 ` [PATCH v4 1/3] rust: kvec: implement shrink_to for KVVec Shivam Kalra via B4 Relay
2026-02-12 10:08   ` Danilo Krummrich
2026-02-13 13:00     ` Shivam Kalra
2026-02-12  8:17 ` [PATCH v4 2/3] rust: alloc: add KUnit tests for KVVec shrink_to Shivam Kalra via B4 Relay
2026-02-12  8:17 ` [PATCH v4 3/3] rust_binder: shrink all_procs when deregistering processes Shivam Kalra via B4 Relay
2026-02-13 16:19   ` Alice Ryhl
2026-02-14  7:42     ` Shivam Kalra [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=75202b71-e5b3-4833-9fa4-e0b279900b49@zohomail.in \
    --to=shivamkalra98@zohomail.in \
    --cc=Liam.Howlett@oracle.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=arve@android.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tkjos@android.com \
    --cc=tmgross@umich.edu \
    --cc=urezki@gmail.com \
    --cc=vbabka@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®