mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust_binder: Reuse the thread lock in get_work_local()
@ 2026-11-24 15:04 Peder Bergan via B4 Relay
  2026-11-24 18:53 ` Alice Ryhl
  0 siblings, 1 reply; 2+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-24 15:04 UTC (permalink / raw)
  To: Alice Ryhl, Carlos Llamas, Greg Kroah-Hartman
  Cc: Arve Hjønnevåg, Todd Kjos, Christian Brauner,
	rust-for-linux, linux-kernel

From: Peder Bergan <peder.bergan@gmail.com>

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@gmail.com>
---
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@gmail.com>



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] rust_binder: Reuse the thread lock in get_work_local()
  2026-11-24 15:04 [PATCH] rust_binder: Reuse the thread lock in get_work_local() Peder Bergan via B4 Relay
@ 2026-11-24 18:53 ` Alice Ryhl
  0 siblings, 0 replies; 2+ messages in thread
From: Alice Ryhl @ 2026-11-24 18:53 UTC (permalink / raw)
  To: Peder Bergan
  Cc: Alice Ryhl, Carlos Llamas, Greg Kroah-Hartman,
	Arve Hjønnevåg, Todd Kjos, Christian Brauner,
	rust-for-linux, linux-kernel

On Tue, Nov 24, 2026 at 04:04:27PM +0100, Peder Bergan wrote:
> 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@gmail.com>

Looks good to me! Unlike get_work(), get_work_local() never needs to
acquire the process lock, so dropping self.inner between checking
looper_need_return and calling pop_work() was unnecessary.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

Alice

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 18:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 15:04 [PATCH] rust_binder: Reuse the thread lock in get_work_local() Peder Bergan via B4 Relay
2026-11-24 18:53 ` Alice Ryhl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®