Skip to contentSkip to conversation

rust_binder: Avoid holding lock when dropping delivered_death

PATCH In review Matthew Maurer 3 participants 4 messages Apr 3 – Apr 28

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust_binder: Avoid holding lock when dropping delivered_death

In 6c37bebd8c926, we switched to looping over the list and dropping each individual node, ostensibly without the lock held in the loop body.

If the kernel were using Rust Edition 2024, the comment would be accurate, and the lock would not be held across the drop. However, the kernel is currently using 2021, so tail expression lifetime extension results in the lock being held across the drop. Explicitly binding the expression result to a variable makes the lockguard no longer part of a tail expression, causing the lock to be dropped before entering the loop body.

This was detected via CONFIG_PROVE_LOCKING identifying an invalid wait context at the drop site.

Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")
---
 drivers/android/binder/process.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/android/binder/process.rs +6 -1
index f06498129aa9765ecbe7a34af36074f15b7490f1..9812c52dc16ee52044dbaf86e30a0a3861effefb 100644
@@ -1402,7 +1402,12 @@ fn deferred_release(self: Arc<Self>) {
         // Clear delivered_deaths list.
         //
         // Scope ensures that MutexGuard is dropped while executing the body.
-        while let Some(delivered_death) = { self.inner.lock().delivered_deaths.pop_front() } {
+        while let Some(delivered_death) = {
+            // Explicitly bind to avoid tail expression lifetime extension of the lockguard
+            // Can be removed when the kernel moves to edition 2024
+            let maybe_death = self.inner.lock().delivered_deaths.pop_front();
+            maybe_death
+        } {
             drop(delivered_death);
         }
 

---
base-commit: d8a9a4b11a137909e306e50346148fc5c3b63f9d
change-id: 20260403-lockhold-bbc2398794c0

Best regards,

Signature · 1 line
Matthew Maurer <[email protected]>
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Matthew Maurer

In 6c37bebd8c926, we switched to looping over the list and dropping each individual node, ostensibly without the lock held in the loop body.

If the kernel were using Rust Edition 2024, the comment would be accurate, and the lock would not be held across the drop. However, the kernel is currently using 2021, so tail expression lifetime extension results in the lock being held across the drop. Explicitly binding the expression result to a variable makes the lockguard no longer part of a tail expression, causing the lock to be dropped before entering the loop body.

Quoted 7 lines

This was detected via CONFIG_PROVE_LOCKING identifying an invalid wait context at the drop site.

Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")

Thanks

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Matthew Maurer
Quoted 11 lines · Matthew Maurer

In 6c37bebd8c926, we switched to looping over the list and dropping each individual node, ostensibly without the lock held in the loop body.

If the kernel were using Rust Edition 2024, the comment would be accurate, and the lock would not be held across the drop. However, the kernel is currently using 2021, so tail expression lifetime extension results in the lock being held across the drop. Explicitly binding the expression result to a variable makes the lockguard no longer part of a tail expression, causing the lock to be dropped before entering the loop body.

Quoted 8 lines

This was detected via CONFIG_PROVE_LOCKING identifying an invalid wait context at the drop site.

Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")

---

maintainer
#3 lore raw
In reply to Matthew Maurer
Quoted 11 lines · Matthew Maurer

In 6c37bebd8c926, we switched to looping over the list and dropping each individual node, ostensibly without the lock held in the loop body.

If the kernel were using Rust Edition 2024, the comment would be accurate, and the lock would not be held across the drop. However, the kernel is currently using 2021, so tail expression lifetime extension results in the lock being held across the drop. Explicitly binding the expression result to a variable makes the lockguard no longer part of a tail expression, causing the lock to be dropped before entering the loop body.

Quoted 7 lines

This was detected via CONFIG_PROVE_LOCKING identifying an invalid wait context at the drop site.

Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")

I realized this tag should be:

Fixes 6c37bebd8c92 ("rust_binder: avoid mem::take on delivered_deaths")

Greg, can you fix this on apply, or do you want a new version?

Alice