From nobody Mon Feb 9 00:06:30 2026 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 3464424113D for ; Mon, 15 Dec 2025 19:46:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765828005; cv=none; b=UXrm/8wLwsLFjEPG/dvrZSkeImNAdclEBjxjIIiTcl4GNJu5bMqGrjLEB/fx//kL5YFP5Ed0ua925Z8gEwD9FgZ6WrijswvTUeszjgOF6R8X4PobAT4JuQI3Wi8eHAPcI84b7yr6c9CjDLDD6koX/g9o5xCI3rTdtEwInMO9q/E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765828005; c=relaxed/simple; bh=ZTRMA/6HyvEN7/49sTqzEuA2mi2ruyvi0yU5SPDVqTk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=VJoqGzy/5Slf8P6xoKNf4r+2+6Ey7vpghl49M4IjdjW+F3w1yydIXBdzX5hfKjBKgvIUcKBhZECQpzCd1lgq3qc2xpuyGSwA2OUhk/A0k4V/UA80wOfTZ2QZKkWu8AI706JLIbtsu8a4Co2/8sdgXsuORGXYxPabuf32qO/3qVA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pocketlaw.net; spf=pass smtp.mailfrom=pocketlaw.net; dkim=pass (2048-bit key) header.d=pocketlaw-net.20230601.gappssmtp.com header.i=@pocketlaw-net.20230601.gappssmtp.com header.b=j4bXLxeS; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pocketlaw.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pocketlaw.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pocketlaw-net.20230601.gappssmtp.com header.i=@pocketlaw-net.20230601.gappssmtp.com header.b="j4bXLxeS" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4779ce2a624so42858525e9.2 for ; Mon, 15 Dec 2025 11:46:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pocketlaw-net.20230601.gappssmtp.com; s=20230601; t=1765828001; x=1766432801; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=C+0qyyeo10fw4903fJ2t+NGqkrGNZD1/pT/+i7D3xmc=; b=j4bXLxeSwWx7lVIgy1BlOIjaSU0CatE0nhx3eRJD1t4rV+jRMQhl8Y44KkoAhfqEzC ri0a51oFNMIlYE6Af7kkJvf+5lAiSi9q7oVnDuDIHXR/NOZKuM7BFhyEDiLSGLFLu2ZU wm5vaNl7+QbbmtPYNxnp40aWGHDl9BwKVg2Jw8g5rXrkjYJv8J6e/hHVPeqeolZRhuoh P668hayj51fn6fEeFoJ0F9Ru7EPrAaAw168R2j/q8WHPjxssPVxzC5uvRV4CIXRGHiFe 55tksss3lL5FI6X0OFdNCF39bYPBqtnjOXCAZO3hMe3mPQQP8YmwBF3wgwDs5vV8l6mk Ys9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765828001; x=1766432801; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=C+0qyyeo10fw4903fJ2t+NGqkrGNZD1/pT/+i7D3xmc=; b=uMEkbHMtmgys9k7R63GAmmsIIMnoUWgaA/00+slt3pcSEvosP2N8mv43AXFyQvBSr6 jDlBHF7z1zrLQYX9Q8q1bzrDdtM+Y28jL8otTvYsCsMRbrWhLMBbURIbXLuTAvb+7DBx ZwanQPX8nqSbw6nzMjadm8GG5jZZFuhh3Hoz0ptc1w0BreJhR7CpJ4M49fUB9AY3APrs qP12JqV46xRLNXEF5cJzfXYnCckQVf2QE8NBvQ2pZvN7YNhCKbsUYZVJ56fYw3y6GKh/ yMkN6S5Sz1cdmScF43fV7o0BqmA6u5zyNhuNGKJOKJGINe1FMWVzTbl9Cf9v43/ESKl/ auOw== X-Gm-Message-State: AOJu0YwN0zlHobRjkF5ZXg/34O7uDvdkQFbFaTcJbno+0hk8BAjaGw47 ZEZk9qm2TS6qCun8FKyDATi8E8v7cYA588jFjIwrxGELZ5negg+J5168HG/9JLmBqOyb X-Gm-Gg: AY/fxX6Zw1mFB8xkNuM5o3q249pKH2CwyHHy80slP4g38e8SmJ0Glm/fZ2rfskHtdOR 7qcqHPxtMq/15onQgcoRnZ7M4yfOq1g7ByRHkIgShyX3lmO7Sa6h3/2oRNJSIfjZyzYCwKSmYq6 AsnFOTntx6oLAhjGNopK3RsoKHhQw3sH3gDKAZN8vqRTzmeBIQQcucRW5Ct6DulTYc1kUsWQtDO nC4XmNtdy5LfARTv3MHFMLfPnukvlLbNnH1cOMTrR+lUczGtYVDp4JAu4scfqFC3O7xqbLNdjWI j2WVLISBqlpbZq1qTVdrrWUerQnZLngC6dx3MoBGyj4ndkLyAyqLzc9Gpz5mdG5/sUdMS3Lfjrl Oan7TMIge7/nxmDXeyWYeY8N73/jyreC01ldhofVhaVYdOl+s1FVAecgycnI+s6qy+AwWfC5hT5 CuEgzjzny2dA61Fv5J45DCKF7Sfn8SsY4RfM8im3kNZwFGB2zPnn7Z/oUIFPyZwlcbdpCkPic+l mZXIWSQrgVRpO12Dw== X-Google-Smtp-Source: AGHT+IFO+6mGbqIa1Pvp6YYyxwTploq+F/vpHdx8OPBv0hSGOM2Qiu5csRzQAPlH054Vgu2LyeCzNA== X-Received: by 2002:a05:600c:3e8d:b0:475:e067:f23d with SMTP id 5b1f17b1804b1-47a8f90e531mr119755345e9.25.1765828001204; Mon, 15 Dec 2025 11:46:41 -0800 (PST) Received: from schoeman-VMware-Virtual-Platform ([102.68.25.54]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-430f42a3290sm15793706f8f.17.2025.12.15.11.46.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Dec 2025 11:46:40 -0800 (PST) From: Pieter-Louis Schoeman To: aliceryhl@google.com, arve@android.com, brauner@kernel.org, cmllamas@google.com, gregkh@linuxfoundation.org, tkjos@android.com Cc: linux-kernel@vger.kernel.org, ojeda@kernel.org, paul@paul-moore.com, pl.schoeman@pocketlaw.net, tamird@gmail.com, vitaly.wool@konsulko.se, wedsonaf@gmail.com, yury.norov@gmail.com Subject: [PATCH] binder: rust: pin zero-to-one ref increments to initiating thread Date: Mon, 15 Dec 2025 21:45:41 +0200 Message-ID: <20251215194541.19425-1-pl.schoeman@pocketlaw.net> X-Mailer: git-send-email 2.51.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The refcount update path must use the zero-to-one aware API on the first increment to preserve correct delivery semantics. Pass the initiating thread through Process::update_ref() and NodeRef so zero->one increments are pinned when available, while keeping decrement behavior unchanged. Also harden update_refcount_locked() to reject increments. Signed-off-by: Pieter-Louis Schoeman --- drivers/android/binder/allocation.rs | 4 +- drivers/android/binder/node.rs | 200 ++++++++++++++++++--------- drivers/android/binder/process.rs | 49 ++++++- drivers/android/binder/thread.rs | 44 +++--- 4 files changed, 202 insertions(+), 95 deletions(-) diff --git a/drivers/android/binder/allocation.rs b/drivers/android/binder/= allocation.rs index 7f65a9c3a0e5..8e8465163069 100644 --- a/drivers/android/binder/allocation.rs +++ b/drivers/android/binder/allocation.rs @@ -402,7 +402,7 @@ pub(crate) fn transfer_binder_object( .alloc .process .as_arc_borrow() - .update_ref(handle, false, strong); + .update_ref(handle, false, strong, None); return Err(EINVAL); } } @@ -433,7 +433,7 @@ fn cleanup_object(&self, index_offset: usize) -> Result= { self.alloc .process .as_arc_borrow() - .update_ref(handle, false, strong) + .update_ref(handle, false, strong, None) } _ =3D> Ok(()), } diff --git a/drivers/android/binder/node.rs b/drivers/android/binder/node.rs index c26d113ede96..0f52e3ade821 100644 --- a/drivers/android/binder/node.rs +++ b/drivers/android/binder/node.rs @@ -21,6 +21,7 @@ }; =20 use core::mem; +use core::num::NonZeroUsize; =20 mod wrapper; pub(crate) use self::wrapper::CritIncrWrapper; @@ -204,6 +205,73 @@ impl ListItem<0> for DTRWrap { } =20 impl Node { + pub(crate) fn dec_refcount_locked( + self: &DArc, + strong: bool, + count: NonZeroUsize, + owner_inner: &mut ProcessInner, + ) -> Option> { + let is_dead =3D owner_inner.is_dead; + let inner =3D self.inner.access_mut(owner_inner); + + let state =3D if strong { + &mut inner.strong + } else { + &mut inner.weak + }; + + let dec =3D count.get(); + if state.count < dec { + pr_err!("Failure: refcount underflow!"); + return None; + } + + state.count -=3D dec; + let need_push =3D !is_dead && state.count =3D=3D 0 && state.has_co= unt; + + if need_push && inner.delivery_state.should_normal_push() { + let list_arc =3D ListArc::try_from_arc(self.clone()).ok().unwr= ap(); + inner.delivery_state.did_normal_push(); + Some(list_arc) + } else { + None + } + } + + pub(crate) fn add_refcount_locked( + self: &DArc, + strong: bool, + delta: usize, + owner_inner: &mut ProcessInner, + ) { + if delta =3D=3D 0 { + return; + } + + let inner =3D self.inner.access_mut(owner_inner); + let state =3D if strong { + &mut inner.strong + } else { + &mut inner.weak + }; + + match state.count.checked_add(delta) { + Some(new_count) =3D> state.count =3D new_count, + None =3D> pr_err!("Failure: refcount overflow!"), + } + } + + pub(crate) fn update_refcount_with_thread( + self: &DArc, + inc: bool, + count: usize, + strong: bool, + thread: Option<&Thread>, + ) { + let mut owner_inner =3D self.owner.inner.lock(); + owner_inner.update_node_refcount(self, inc, strong, count, thread); + } + pub(crate) fn new( ptr: u64, cookie: u64, @@ -374,37 +442,21 @@ pub(crate) fn update_refcount_locked( count: usize, owner_inner: &mut ProcessInner, ) -> Option> { - let is_dead =3D owner_inner.is_dead; - let inner =3D self.inner.access_mut(owner_inner); - - // Get a reference to the state we'll update. - let state =3D if strong { - &mut inner.strong - } else { - &mut inner.weak - }; - - // Update the count and determine whether we need to push work. - let need_push =3D if inc { - state.count +=3D count; - // TODO: This method shouldn't be used for zero-to-one increme= nts. - !is_dead && !state.has_count - } else { - if state.count < count { - pr_err!("Failure: refcount underflow!"); - return None; - } - state.count -=3D count; - !is_dead && state.count =3D=3D 0 && state.has_count - }; - - if need_push && inner.delivery_state.should_normal_push() { - let list_arc =3D ListArc::try_from_arc(self.clone()).ok().unwr= ap(); - inner.delivery_state.did_normal_push(); - Some(list_arc) - } else { - None + debug_assert!( + !inc, + "update_refcount_locked: increments forbidden; use incr_refcou= nt_allow_zero2one*" + ); + if inc { + pr_err!( + "update_refcount_locked called for increment; use incr_ref= count_allow_zero2one*" + ); + return None; } + let nz =3D match NonZeroUsize::new(count) { + Some(nz) =3D> nz, + None =3D> return None, + }; + self.dec_refcount_locked(strong, nz, owner_inner) } =20 pub(crate) fn incr_refcount_allow_zero2one( @@ -773,6 +825,55 @@ pub(crate) fn new(node: DArc, strong_count: usiz= e, weak_count: usize) -> S } } =20 + pub(crate) fn update_with_thread( + &mut self, + inc: bool, + strong: bool, + thread: Option<&Thread>, + ) -> bool { + if strong && self.strong_count =3D=3D 0 { + return false; + } + let (count, node_count, other_count) =3D if strong { + ( + &mut self.strong_count, + &mut self.strong_node_count, + self.weak_count, + ) + } else { + ( + &mut self.weak_count, + &mut self.weak_node_count, + self.strong_count, + ) + }; + if inc { + if *count =3D=3D 0 { + *node_count =3D 1; + // Critical: zero --> one must be pinned to the initiating= thread when available. + self.node + .update_refcount_with_thread(true, 1, strong, thread); + } + *count +=3D 1; + } else { + if *count =3D=3D 0 { + pr_warn!( + "pid {} performed invalid decrement on ref\n", + kernel::current!().pid() + ); + return false; + } + *count -=3D 1; + if *count =3D=3D 0 { + // Decrements are not "critical"; preserve existing semant= ics (no thread pinning). + self.node.update_refcount(false, *node_count, strong); + *node_count =3D 0; + return other_count =3D=3D 0; + } + } + false + } + pub(crate) fn absorb(&mut self, mut other: Self) { assert!( Arc::ptr_eq(&self.node, &other.node), @@ -826,44 +927,7 @@ pub(crate) fn clone(&self, strong: bool) -> Result { /// /// Returns whether `self` should be removed (when both counts are zer= o). pub(crate) fn update(&mut self, inc: bool, strong: bool) -> bool { - if strong && self.strong_count =3D=3D 0 { - return false; - } - let (count, node_count, other_count) =3D if strong { - ( - &mut self.strong_count, - &mut self.strong_node_count, - self.weak_count, - ) - } else { - ( - &mut self.weak_count, - &mut self.weak_node_count, - self.strong_count, - ) - }; - if inc { - if *count =3D=3D 0 { - *node_count =3D 1; - self.node.update_refcount(true, 1, strong); - } - *count +=3D 1; - } else { - if *count =3D=3D 0 { - pr_warn!( - "pid {} performed invalid decrement on ref\n", - kernel::current!().pid() - ); - return false; - } - *count -=3D 1; - if *count =3D=3D 0 { - self.node.update_refcount(false, *node_count, strong); - *node_count =3D 0; - return other_count =3D=3D 0; - } - } - false + self.update_with_thread(inc, strong, None) } } =20 diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/pro= cess.rs index 132055b4790f..1205320770a0 100644 --- a/drivers/android/binder/process.rs +++ b/drivers/android/binder/process.rs @@ -222,15 +222,53 @@ pub(crate) fn update_node_refcount( count: usize, othread: Option<&Thread>, ) { - let push =3D node.update_refcount_locked(inc, strong, count, self); + let push: Option> =3D if inc { + if count =3D=3D 0 { + None + } else { + // First increment must use the zero2one-aware API so deli= very_state is correct. + let first_push =3D match node.incr_refcount_allow_zero2one= (strong, self) { + Ok(Some(work)) =3D> Some(work as _), + Ok(None) =3D> None, + Err(CouldNotDeliverCriticalIncrement) =3D> { + // Strong-only: we need the wrapper to avoid misde= livery. + match CritIncrWrapper::new() { + Ok(wrapper) =3D> node + .incr_refcount_allow_zero2one_with_wrapper= (strong, wrapper, self), + Err(_) =3D> { + // OOM fallback: keep the refcount, + // but rely on the already + // scheduled node to eventually deliver (d= egrades + // "critical" guarantee). + node.add_refcount_locked(strong, 1, self); + None + } + } + } + }; + + if count > 1 { + node.add_refcount_locked(strong, count - 1, self); + } + + first_push + } + } else { + match core::num::NonZeroUsize::new(count) { + Some(nz) =3D> node + .dec_refcount_locked(strong, nz, self) + .map(|work| work as _), + None =3D> None, + } + }; =20 // If we decided that we need to push work, push either to the pro= cess or to a thread if // one is specified. - if let Some(node) =3D push { + if let Some(work) =3D push { if let Some(thread) =3D othread { - thread.push_work_deferred(node); + thread.push_work_deferred(work); } else { - let _ =3D self.push_work(node); + let _ =3D self.push_work(work); // Nothing to do: `push_work` may fail if the process is d= ead, but that's ok as in // that case, it doesn't care about the notification. } @@ -922,6 +960,7 @@ pub(crate) fn update_ref( handle: u32, inc: bool, strong: bool, + thread: Option<&Thread>, ) -> Result { if inc && handle =3D=3D 0 { if let Ok(node_ref) =3D self.ctx.get_manager_node(strong) { @@ -937,7 +976,7 @@ pub(crate) fn update_ref( // increment references on itself. let mut refs =3D self.node_refs.lock(); if let Some(info) =3D refs.by_handle.get_mut(&handle) { - if info.node_ref().update(inc, strong) { + if info.node_ref().update_with_thread(inc, strong, thread) { // Clean up death if there is one attached to this node re= ference. if let Some(death) =3D info.death().take() { death.set_cleared(true); diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thre= ad.rs index 1a8e6fdc0dc4..ea6de38ceef8 100644 --- a/drivers/android/binder/thread.rs +++ b/drivers/android/binder/thread.rs @@ -1330,26 +1330,30 @@ fn write(self: &Arc, req: &mut BinderWriteRea= d) -> Result { drop(buffer); } } - BC_INCREFS =3D> { - self.process - .as_arc_borrow() - .update_ref(reader.read()?, true, false)? - } - BC_ACQUIRE =3D> { - self.process - .as_arc_borrow() - .update_ref(reader.read()?, true, true)? - } - BC_RELEASE =3D> { - self.process - .as_arc_borrow() - .update_ref(reader.read()?, false, true)? - } - BC_DECREFS =3D> { - self.process - .as_arc_borrow() - .update_ref(reader.read()?, false, false)? - } + BC_INCREFS =3D> self.process.as_arc_borrow().update_ref( + reader.read()?, + true, + false, + Some(self), + )?, + BC_ACQUIRE =3D> self.process.as_arc_borrow().update_ref( + reader.read()?, + true, + true, + Some(self), + )?, + BC_RELEASE =3D> self.process.as_arc_borrow().update_ref( + reader.read()?, + false, + true, + Some(self), + )?, + BC_DECREFS =3D> self.process.as_arc_borrow().update_ref( + reader.read()?, + false, + false, + Some(self), + )?, BC_INCREFS_DONE =3D> self.process.inc_ref_done(&mut reader= , false)?, BC_ACQUIRE_DONE =3D> self.process.inc_ref_done(&mut reader= , true)?, BC_REQUEST_DEATH_NOTIFICATION =3D> self.process.request_de= ath(&mut reader, self)?, --=20 2.51.0