From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.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 93BB9246778 for ; Mon, 19 Jan 2026 10:11:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768817502; cv=none; b=VEkw2OHW/Cl65yPU9uTWBKvrtC3ZZ4olFZmZ2b4ETjB+/MGc2rYR6iThXSAYpmEQuxfFW2sNDig7EO+asndwFPBs+RjMx95IbvmBnFLTvLYYnlHFfsE3veZuhdhk7Yju8euxByVYIVjVeYzDu674yp3qPKQ1KVG0j9+dluuywOE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768817502; c=relaxed/simple; bh=Ssx9cOeeaeOAkrAUdOFWwQkudSdrQ32uSi9dHfkU6K4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RZw5Q9Qfw5tMi3krBAUbPeD3xrbisq8U5l8ve+6udnEUX6gVrmX8lHQJTtgh1KHvyaUJ1tJtvB3zoboSKFVPVBlWfpt8YY5e7yHE7KjjzrfAsL3s/vhFuQZUPftLsyoIg+U0mq4qpN4uHcBvKL7JbxbGtsG+QcmKxo5lnEtVTtc= 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=pKZpNbm4; arc=none smtp.client-ip=209.85.128.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="pKZpNbm4" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4803a72daaeso1745635e9.0 for ; Mon, 19 Jan 2026 02:11:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768817499; x=1769422299; 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=myeEaOqT7ZtmafUikt8QRfhp72T8cvpHGYSTXcwxmj8=; b=pKZpNbm41xsltk89kAt6nc4LPDzVfcGdz7Jis0yCBBcSYuB4M0f6p0b7QzW4kZgGFz rdkX7C+Y45QqOJfmGLzUra6rSHw4rydi+Skgvi7PnHLMtP73vVWvlWn0T6CC+8H1Du1T AiFoqeWt/RMI6UfDxnq4egNV3AzPud3uwjyN+4a74/yr7ddavXjaT/sFKHq3AprEgI9Q Ge1qEh6kgwPMURGY0ScQY7dTsHqh1sMIdadiXHp8d36DYXR1pH1G0NGQPY1SACcF7+gH bwTduw7nm12oOeN8xfCCvXlahYM5SAIQ9GvyDIANBjXtGhqNZarRQ8k+Bd4E3DyhQWlx HACw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768817499; x=1769422299; 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=myeEaOqT7ZtmafUikt8QRfhp72T8cvpHGYSTXcwxmj8=; b=DczpHc1t8NyktsFqUe4AFWxS1vRdnBRsIkZWPK8r32W5xG7I7Wuu1frpwp6dZ71JPM 1f5enHRgwvOKIKKRHWhsAkDX6GDnQ27cMSPba7NuLCwxGFkNHACN3FhWu1JIYwVdN7cC 0RW6o1NwLOk1yJhnKkzonnGs2xKrMkRfaf3aq6SDnxKvSEmEcypcmil7VZUZR0Wq5kkd J4lCy/64sEpzlTCnHziPSDZBspLDJpwLiuHWKoaCuOov+dRkzE28cUgx0IDR4AsMdn6r /F3Ba4hdw5LT2mA6htHpVZF9QBRDFEmePCjZI7pY++k6s707JcL5MqxAve8oOb0LbX94 beXw== X-Forwarded-Encrypted: i=1; AJvYcCVfHtTC9q1Ec4GsDf6n/qeOkeV/hYiE2p1lvPnT0qOzI1lJRil2y8fPz20f9vB4xB2dmAkvXdfyWxWdN8Y=@vger.kernel.org X-Gm-Message-State: AOJu0YwLLsTz/WQ0mv6V12S1LzerOnWcmsHRfDTrS1yBRgYnEV2Zha6/ y6SmQ7sIyhX5erwjZ7/+YRrhnjmj6s5oE10U13g5i4flrQ5pM36gj4BI6/CZ2IT9lu/IUMBtMXc xZsYmjm9GWPBcJWHDLg== X-Received: from wmbjv16.prod.google.com ([2002:a05:600c:5710:b0:480:39fd:8dae]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3484:b0:46e:59bd:f7e2 with SMTP id 5b1f17b1804b1-4801e670bf9mr140160455e9.11.1768817499043; Mon, 19 Jan 2026 02:11:39 -0800 (PST) Date: Mon, 19 Jan 2026 10:11:37 +0000 In-Reply-To: <20260118142623.1188141-1-jason.kei.hall@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260118142623.1188141-1-jason.kei.hall@gmail.com> Message-ID: Subject: Re: [PATCH v2] rust_binder: refactor context management to use KVVec From: Alice Ryhl To: Jason Hall Cc: Greg Kroah-Hartman , "Arve =?utf-8?B?SGrDuG5uZXbDpWc=?=" , Todd Kjos , Carlos Llamas , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Sun, Jan 18, 2026 at 07:26:23AM -0700, Jason Hall wrote: > Replace the linked list management in context.rs with KVVec. > This simplifies the ownership model by using standard > Arc-based tracking and moves away from manual unsafe list removals. > > The refactor improves memory safety by leveraging Rust's contiguous > collection types while maintaining proper error propagation for > allocation failures during process registration. > > Suggested-by: Alice Ryhl > Link: https://github.com/rust-for-linux/linux/issues/1215 > Signed-off-by: Jason Hall > - pub(crate) fn register_process(self: &Arc, proc: ListArc) { > + pub(crate) fn register_process(self: &Arc, proc: Arc) -> Result { > if !Arc::ptr_eq(self, &proc.ctx) { > pr_err!("Context::register_process called on the wrong context."); > - return; > + return Err(EINVAL); > } > - self.manager.lock().all_procs.push_back(proc); > + self.manager > + .lock() > + .all_procs > + .push(proc, GFP_KERNEL) > + .map_err(Error::from) This can be simplified to: self.manager .lock() .all_procs .push(proc, GFP_KERNEL)?; Ok(()) > } > > pub(crate) fn deregister_process(self: &Arc, proc: &Process) { > @@ -108,8 +99,12 @@ pub(crate) fn deregister_process(self: &Arc, proc: &Process) { > pr_err!("Context::deregister_process called on the wrong context."); > return; > } > - // SAFETY: We just checked that this is the right list. > - unsafe { self.manager.lock().all_procs.remove(proc) }; > + // Safe removal using retain > + self.manager.lock().all_procs.retain(|p| { > + let p1 = Arc::as_ptr(p); > + let p2 = proc as *const Process; > + p1 != p2 > + }); Please use Arc::ptr_eq() for this comparison (in both places). You may change deregister_process to take an &Arc as argument to make this work. Also, I think we should shrink the `all_procs` vector if it becomes too much larger than the number of procs. Let's say ... after removing a process, if the capacity() is less than half of the length and less than 128, then we shrink the capacity. > } > > pub(crate) fn set_manager_node(&self, node_ref: NodeRef) -> Result { > @@ -158,13 +153,11 @@ pub(crate) fn for_each_proc(&self, mut func: F) > } > } > > - pub(crate) fn get_all_procs(&self) -> Result>> { > + pub(crate) fn get_all_procs(&self) -> Result>> { > let lock = self.manager.lock(); > - let count = lock.all_procs.iter().count(); > - > - let mut procs = KVec::with_capacity(count, GFP_KERNEL)?; > - for proc in &lock.all_procs { > - procs.push(Arc::from(proc), GFP_KERNEL)?; > + let mut procs = KVVec::with_capacity(lock.all_procs.len(), GFP_KERNEL)?; > + for proc in lock.all_procs.iter() { > + procs.push(Arc::clone(proc), GFP_KERNEL)?; > } Let's change get_procs_with_pid() too. Both to use KVVec, and also let's have it iterate all_procs() directly. > diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/process.rs > index 132055b4790f..c3676fc7785d 100644 > --- a/drivers/android/binder/process.rs > +++ b/drivers/android/binder/process.rs > @@ -513,7 +513,9 @@ fn new(ctx: Arc, cred: ARef) -> Result> { > )?; > > let process = list_process.clone_arc(); > - process.ctx.register_process(list_process); > + process > + .ctx > + .register_process(Arc::from(list_process.as_arc_borrow()))?; You do not need to create a ListArc in the first place. impl Process { fn new(ctx: Arc, cred: ARef) -> Result> { let current = kernel::current!(); - let list_process = ListArc::pin_init::( + let process = Arc::pin_init::( try_pin_init!(Process { ctx, cred, inner <- kernel::new_spinlock!(ProcessInner::new(), "Process::inner"), pages <- ShrinkablePageRange::new(&super::BINDER_SHRINKER), node_refs <- kernel::new_mutex!(ProcessNodeRefs::new(), "Process::node_refs"), freeze_wait <- kernel::new_condvar!("Process::freeze_wait"), task: current.group_leader().into(), defer_work <- kernel::new_work!("Process::defer_work"), links <- ListLinks::new(), stats: BinderStats::new(), }), GFP_KERNEL, )?; - let process = list_process.clone_arc(); - process - .ctx - .register_process(Arc::from(list_process.as_arc_borrow()))?; + process.ctx.register_process(process.clone())?; Ok(process) } Alice