From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8102218ACC for ; Fri, 13 Feb 2026 16:19:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770999584; cv=none; b=hJJQBT/DdhC0nDWRwg1w0nBiip6eFDZJkWSAJsrVpa/DkpWegplPfxFFd4rRnoDsw8sYTo35kGkKKWa/lUWWu3pRBmYHfNBLtlKL+6ehr36gjEMUBI1dKCVZ8p4t1Jxb5PorkVfMOZ2b6GHSGlVhP1Xr58TWcLJwJMKKqqgVX1k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770999584; c=relaxed/simple; bh=skxGPYGF0shbk35Z+9byZsuGGwHFRooDQP2l9UO6RI4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LA4p0XJfm8/MnclD7UiFIlWU1zDRPdnEBQKCTyu3znTL6tX4STjZ/8WZmC8CaBVp2YeEAE74PlcwkJQ0v5DyBgxfdFAoX1+tITwPX8lHbXYDKuqgHOeEwBWgSdG8r+ZvJA1JhP1/krSrqBwlLj2KIo+RpBQAXIWdWUTxivt+AmE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DlX80Weh; arc=none smtp.client-ip=209.85.218.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DlX80Weh" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-b8db7f340b3so40750366b.3 for ; Fri, 13 Feb 2026 08:19:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770999581; x=1771604381; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6STMy2Sn3VeXtt0dq37yqQOGqgyUcYOyF5J32Z9FjUE=; b=DlX80Wehx9NR5OMphwMKo5XkK2oN2duQDbl+xzB3YRXNkfkjpitvSQ0t13Cbvzitno YtqfVIiDEd9o65q4oTkNYZ6EwhxF0wd8Te6KE7rGkcpKvLfxz7ni94Rlc7KeKKuvRgWR rqbaH+cq7KZ9zt9IiipHt+e8U7C0Lj0vyAdK1CPAkkJq2zBqX9KLGMGaAJiMkAnUJlGf UeZZAZMpvGpq3YUnSZypkw6KsmxMvibMngJGL3pV81QSH8qPA7Z8tgJhECITtbXPsfe1 CGtcRlTJIFrJVD9EFlWW06W2GYxlUdrxckLPS1zNMgI6huCwpXSHhTmYIU0RievJiVRk RHyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770999581; x=1771604381; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6STMy2Sn3VeXtt0dq37yqQOGqgyUcYOyF5J32Z9FjUE=; b=fdAcyqWDs8oCVODki5cyhner7q+jEBgz1KOPT9WGZLLz4V8RnLjCMWPTFfbZjfzoM4 tgbs41AC8r0xVB5mhP8VmUKNs9OKbbDojyYJqAZH00IjD5s1RTFX9uZNbWDYtT8nRskL KzVhVaA/6XlJ26qzEASEZKzz+6u2A0t+0Tk26YGefg8HZEAVQtq5Znf+iZ9yn0u1YysK W1AY+cTrpipSa8Ylxo4bOCiWfV+PQCT2VH0mkHuMwgOI2fudGRlPks8XsHKIq0COyN7z s2WXMW7aPMI1ISW6+6ZRxIzZ5lBJU8rNge079n9K7vL2BmUK5gNNLsxbGIMrykN3b/3l nSrQ== X-Forwarded-Encrypted: i=1; AJvYcCUhEB9YS0Mvegz7301n0pzMhaExhpiY0pO8M8TqAlvaY/mYjeSDZPAkA4PH9gAkG7ZnhtdNZvPOtKSMgVc=@vger.kernel.org X-Gm-Message-State: AOJu0YypN5Dvbz5OflfMklKUOu7CU8KO80Qeeitcsh+r1M6A8uKps0fU augNyZ2VPqpXfE+S4s6Gv72o962NzxZ/GuY68qyfQR07/Seh8J49oJupwPPHEvNkaBYZwyV8uyZ 6iCK2CVlnhArOb55nbA== X-Received: from ejcjg26.prod.google.com ([2002:a17:907:971a:b0:b8f:99f1:6959]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:6d05:b0:b8f:6f6d:c593 with SMTP id a640c23a62f3a-b8fb44767c4mr142915066b.32.1770999580951; Fri, 13 Feb 2026 08:19:40 -0800 (PST) Date: Fri, 13 Feb 2026 16:19:39 +0000 In-Reply-To: <20260212-binder-shrink-vec-v3-v4-3-bd02f06bf2cd@zohomail.in> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260212-binder-shrink-vec-v3-v4-0-bd02f06bf2cd@zohomail.in> <20260212-binder-shrink-vec-v3-v4-3-bd02f06bf2cd@zohomail.in> Message-ID: Subject: Re: [PATCH v4 3/3] rust_binder: shrink all_procs when deregistering processes From: Alice Ryhl To: Shivam Kalra Cc: Danilo Krummrich , Lorenzo Stoakes , Vlastimil Babka , "Liam R. Howlett" , Uladzislau Rezki , Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , Greg Kroah-Hartman , "Arve =?utf-8?B?SGrDuG5uZXbDpWc=?=" , Todd Kjos , Christian Brauner , Carlos Llamas , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 > Signed-off-by: Shivam Kalra > --- > 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, proc: &Arc) { > } > 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