[PATCH] rust_binder: decrement requested_thread_count on BR_SPAWN_LOOPER write failure

From: Adhyansh Verma

Date: Sat Nov 28 2026 - 10:37:37 EST


In Thread::read(), when more threads are needed for the binder thread pool,
self.process.needs_thread() is called to request a new looper thread.
needs_thread() checks that requested_thread_count is 0, and if so,
increments it to 1.

Subsequently, writer.write(&BR_SPAWN_LOOPER) writes the command to the
userspace read buffer. If this write fails (for example, if userspace
provided an invalid or faulting address), the error is propagated using
the '?' operator. However, requested_thread_count is never decremented on
failure.

Because userspace never received the BR_SPAWN_LOOPER command, it will
never register a new thread via BC_REGISTER_LOOPER (which is the only
place where requested_thread_count is decremented). Consequently,
requested_thread_count remains permanently set to 1. Any subsequent calls
to needs_thread() will see requested_thread_count != 0 and return false,
permanently starving the process binder thread pool from spawning looper
threads.

This mirrors the C Binder driver (drivers/android/binder.c), which
explicitly decrements proc->requested_threads if put_user(BR_SPAWN_LOOPER)
fails.

Introduce undo_needs_thread() on Process to decrement
requested_thread_count if the BR_SPAWN_LOOPER write fails.

Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Signed-off-by: Adhyansh Verma <adhyanshverma2010@xxxxxxxxx>
---
drivers/android/binder/process.rs | 7 +++++++
drivers/android/binder/thread.rs | 5 ++++-
2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/process.rs
index 1f4cb43f1de..a6d49e7dd84 100644
--- a/drivers/android/binder/process.rs
+++ b/drivers/android/binder/process.rs
@@ -1269,6 +1269,13 @@ pub(crate) fn needs_thread(&self) -> bool {
ret
}

+ pub(crate) fn undo_needs_thread(&self) {
+ let mut inner = self.inner.lock();
+ if inner.requested_thread_count > 0 {
+ inner.requested_thread_count -= 1;
+ }
+ }
+
pub(crate) fn request_death(
self: &Arc<Self>,
reader: &mut UserSliceReader,
diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thread.rs
index 4ce6fc53320..d36ecd12dfe 100644
--- a/drivers/android/binder/thread.rs
+++ b/drivers/android/binder/thread.rs
@@ -1658,7 +1658,10 @@ fn read(self: &Arc<Self>, req: &mut BinderWriteRead, wait: bool) -> Result {
req.read_size as usize,
)
.writer();
- writer.write(&BR_SPAWN_LOOPER)?;
+ if let Err(err) = writer.write(&BR_SPAWN_LOOPER) {
+ self.process.undo_needs_thread();
+ return Err(err);
+ }
}
Ok(())
}
--
2.56.1