rust_binder: Avoid holding lock when dropping delivered_death
Subsystems (3)
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.
--- drivers/android/binder/process.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/android/binder/process.rs +6 -1
@@ -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]>
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_LOCKINGidentifying an invalid wait context at the drop site.Reported-by David Stevens <[email protected]>Signed-off-by Matthew Maurer <[email protected]>Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")
Thanks
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_LOCKINGidentifying an invalid wait context at the drop site.Reported-by David Stevens <[email protected]>Signed-off-by Matthew Maurer <[email protected]>Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")---
Alice Ryhl
maintainerQuoted 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_LOCKINGidentifying an invalid wait context at the drop site.Reported-by David Stevens <[email protected]>Signed-off-by Matthew Maurer <[email protected]>Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")
I realized this tag should be:
Greg, can you fix this on apply, or do you want a new version?
Alice