When a looper thread replies with TF_DEFER_COMPLETE, its 4-byte BR_TRANSACTION_COMPLETE work item is queued via push_work_deferred() in inner.work_list without setting inner.process_work_list. On the next blocking BINDER_WRITE_READ ioctl, inner.should_use_process_work_queue() returns true and Thread::read() checks only whether the remaining output buffer can fit a single transaction (4-byte command code plus size_of::()). If userspace provides a read buffer with 76 bytes remaining after the initial 4-byte BR_NOOP placeholder (read_size = 80), Thread::read() calls Self::get_work(), sleeps in Process.ready_threads, and receives an incoming synchronous transaction queued behind the deferred BR_TRANSACTION_COMPLETE in inner.work_list. Iteration 1 of the loop writes the 4-byte BR_TRANSACTION_COMPLETE, reducing writer.len() to 72 bytes, and iteration 2 exits the loop without delivering the incoming transaction, leaving it stranded in the thread-local work_list. Add a size(&self) -> usize method to DeliverToRead returning the maximum number of bytes that do_work() may write to the user read buffer, and reserve the initial BR_NOOP placeholder before checking the work queue in Thread::read(). When inner.should_use_process_work_queue() is true and writer.len() lacks room for both the queued deferred items and an incoming transaction, set inner.process_work_list when inner.work_list is non-empty so Thread::read() falls back to Self::get_work_local and drains the thread's deferred work items without waiting for global process work. Suggested-by: Alice Ryhl Fixes: 8eb05e8e47f1 ("rust_binder: add TF_DEFER_COMPLETE flag for avoiding userspace roundtrip") Cc: stable@vger.kernel.org Signed-off-by: Chris Wiseman --- v1 -> v2: - Reserve BR_NOOP before checking the work queue and set inner.process_work_list when writer.len() < 76 + deferred_size so Thread::read() drains deferred items via Self::get_work_local instead of skipping the read loop and returning only BR_NOOP, and avoid the per-iteration work.size() dynamic call inside the read loop when deferred_size is zero (Alice Ryhl, Sashiko, Carlos Llamas). drivers/android/binder/freeze.rs | 4 +++ drivers/android/binder/node.rs | 8 +++++ drivers/android/binder/node/wrapper.rs | 4 +++ drivers/android/binder/rust_binder_main.rs | 8 +++++ drivers/android/binder/thread.rs | 36 ++++++++++++++-------- drivers/android/binder/transaction.rs | 4 +++ 6 files changed, 52 insertions(+), 12 deletions(-) diff --git a/drivers/android/binder/freeze.rs b/drivers/android/binder/freeze.rs index 69e058e6d818..5103bfac781f 100644 --- a/drivers/android/binder/freeze.rs +++ b/drivers/android/binder/freeze.rs @@ -152,6 +152,10 @@ fn should_sync_wakeup(&self) -> bool { false } + fn size(&self) -> usize { + size_of::() + size_of::() + } + #[inline(never)] fn debug_print(&self, m: &SeqFile, prefix: &str, _tprefix: &str) -> Result { seq_print!(m, "{}has frozen binder\n", prefix); diff --git a/drivers/android/binder/node.rs b/drivers/android/binder/node.rs index efdafc202e27..4f4c892cba58 100644 --- a/drivers/android/binder/node.rs +++ b/drivers/android/binder/node.rs @@ -754,6 +754,10 @@ fn should_sync_wakeup(&self) -> bool { false } + fn size(&self) -> usize { + 2 * (size_of::() + 2 * size_of::()) + } + #[inline(never)] fn debug_print(&self, m: &SeqFile, prefix: &str, _tprefix: &str) -> Result { seq_print!( @@ -1149,6 +1153,10 @@ fn should_sync_wakeup(&self) -> bool { false } + fn size(&self) -> usize { + size_of::() + size_of::() + } + #[inline(never)] fn debug_print(&self, m: &SeqFile, prefix: &str, _tprefix: &str) -> Result { let inner = self.inner.lock(); diff --git a/drivers/android/binder/node/wrapper.rs b/drivers/android/binder/node/wrapper.rs index edfbccf6e6da..37826349eafb 100644 --- a/drivers/android/binder/node/wrapper.rs +++ b/drivers/android/binder/node/wrapper.rs @@ -93,6 +93,10 @@ fn should_sync_wakeup(&self) -> bool { false } + fn size(&self) -> usize { + 2 * (size_of::() + 2 * size_of::()) + } + #[inline(never)] fn debug_print(&self, m: &SeqFile, prefix: &str, _tprefix: &str) -> Result { seq_print!( diff --git a/drivers/android/binder/rust_binder_main.rs b/drivers/android/binder/rust_binder_main.rs index 56c786475237..361f3a987235 100644 --- a/drivers/android/binder/rust_binder_main.rs +++ b/drivers/android/binder/rust_binder_main.rs @@ -163,6 +163,10 @@ fn do_work( /// Generally only set to true for non-oneway transactions. fn should_sync_wakeup(&self) -> bool; + /// Returns the maximum size in bytes that [`DeliverToRead::do_work`] may write to the read + /// buffer. + fn size(&self) -> usize; + fn debug_print(&self, m: &SeqFile, prefix: &str, transaction_prefix: &str) -> Result; } @@ -279,6 +283,10 @@ fn should_sync_wakeup(&self) -> bool { false } + fn size(&self) -> usize { + size_of::() + } + fn debug_print(&self, m: &SeqFile, prefix: &str, _tprefix: &str) -> Result { seq_print!(m, "{}", prefix); if self.skip.load(Relaxed) { diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thread.rs index 763f0646a1e9..ded50a1c964e 100644 --- a/drivers/android/binder/thread.rs +++ b/drivers/android/binder/thread.rs @@ -1601,8 +1601,27 @@ fn read(self: &Arc, req: &mut BinderWriteRead, wait: bool) -> Result { UserSlice::new(UserPtr::from_addr(read_start as usize), read_len as usize).writer(), self, ); + // Reserve some room at the beginning of the read buffer so that we can send a + // BR_SPAWN_LOOPER if we need to. + let mut has_noop_placeholder = false; + if req.read_consumed == 0 { + if let Err(err) = writer.write_code(BR_NOOP) { + pr_warn_ratelimited!("Failure when writing BR_NOOP at beginning of buffer.\n"); + return Err(err); + } + has_noop_placeholder = true; + } + let (in_pool, has_transaction, thread_todo, use_proc_queue) = { - let inner = self.inner.lock(); + let mut inner = self.inner.lock(); + if inner.should_use_process_work_queue() { + let deferred_size: usize = inner.work_list.iter().map(|w| w.size()).sum(); + if writer.len() + < size_of::() + 4 + deferred_size + { + inner.process_work_list = !inner.work_list.is_empty(); + } + } ( inner.is_looper(), inner.current_transaction.is_some(), @@ -1619,17 +1638,6 @@ fn read(self: &Arc, req: &mut BinderWriteRead, wait: bool) -> Result { Self::get_work_local }; - // Reserve some room at the beginning of the read buffer so that we can send a - // BR_SPAWN_LOOPER if we need to. - let mut has_noop_placeholder = false; - if req.read_consumed == 0 { - if let Err(err) = writer.write_code(BR_NOOP) { - pr_warn_ratelimited!("Failure when writing BR_NOOP at beginning of buffer.\n"); - return Err(err); - } - has_noop_placeholder = true; - } - // Loop doing work while there is room in the buffer. let initial_len = writer.len(); while writer.len() >= size_of::() + 4 { @@ -1800,6 +1808,10 @@ fn should_sync_wakeup(&self) -> bool { false } + fn size(&self) -> usize { + size_of::() + } + fn debug_print(&self, m: &SeqFile, prefix: &str, _tprefix: &str) -> Result { seq_print!( m, diff --git a/drivers/android/binder/transaction.rs b/drivers/android/binder/transaction.rs index 48ac942bb75a..6909d7a8a219 100644 --- a/drivers/android/binder/transaction.rs +++ b/drivers/android/binder/transaction.rs @@ -762,6 +762,10 @@ fn should_sync_wakeup(&self) -> bool { !self.flags.is_oneway() } + fn size(&self) -> usize { + size_of::() + size_of::() + } + fn debug_print(&self, m: &SeqFile, _prefix: &str, tprefix: &str) -> Result { self.debug_print_inner(m, tprefix); Ok(()) -- 2.47.0