[PATCH] rust_binder: Reuse the thread lock in get_work_local()
From: Peder Bergan via B4 Relay
Date: Tue Nov 24 2026 - 10:04:54 EST
From: Peder Bergan <peder.bergan@xxxxxxxxx>
get_work_local() takes the thread lock to check looper_need_return.
When the flag is clear, it drops and retakes the lock before trying to
pop work for either a blocking or a nonblocking read.
Keep the guard across the flag check and the first pop. A blocking
read that finds no work still registers its wait before the condition
variable releases the guard, so a producer can wake the reader.
Signed-off-by: Peder Bergan <peder.bergan@xxxxxxxxx>
---
Reusing the guard eliminated 4,013 lock acquisitions and 4,013 releases
on each of two x86-64 KVM hosts. Each trace covered setup and 1,000
synchronous Binder echo calls. Both kernels passed checks for empty
nonblocking reads, signal interruption and waking a blocked reader by
closing a duplicate Binder file descriptor.
OpenAI Codex assisted with coding and testing.
---
drivers/android/binder/thread.rs | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thread.rs
index 54a208caebe12..8efda6e9ded1e 100644
--- a/drivers/android/binder/thread.rs
+++ b/drivers/android/binder/thread.rs
@@ -549,20 +549,17 @@ pub(crate) fn has_current_transaction(&self) -> bool {
// mangled symbol names.
#[export_name = "rust_binder_waitlcl"]
fn get_work_local(self: &Arc<Self>, wait: bool) -> Result<Option<DLArc<dyn DeliverToRead>>> {
- {
- let mut inner = self.inner.lock();
- if inner.looper_need_return {
- return Ok(inner.pop_work());
- }
+ let mut inner = self.inner.lock();
+ if inner.looper_need_return {
+ return Ok(inner.pop_work());
}
// Try once if the caller does not want to wait.
if !wait {
- return self.inner.lock().pop_work().ok_or(EAGAIN).map(Some);
+ return inner.pop_work().ok_or(EAGAIN).map(Some);
}
// Loop waiting only on the local queue (i.e., not registering with the process queue).
- let mut inner = self.inner.lock();
loop {
if let Some(work) = inner.pop_work() {
return Ok(Some(work));
---
base-commit: 917fcbf033154a6c8d83cfa6c88f012b71eb37b2
change-id: 20261124-perf-binder-local-work-lock-9ce5d1f365bc
Best regards,
--
Peder Bergan <peder.bergan@xxxxxxxxx>