aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/rust/kernel/sync/lock/mutex.rs
diff options
context:
space:
mode:
authorFilipe Xavier <felipe_life@live.com>2024-09-26 17:50:37 -0300
committerMiguel Ojeda <ojeda@kernel.org>2024-10-10 00:33:42 +0200
commitf4c2c90bb7b4ae1812dbaca15d9637eecaac2c9f (patch)
tree6f8c4af627bd127e47ef15a4a980e2a49329e637 /rust/kernel/sync/lock/mutex.rs
parentrust: std_vendor: update dbg macro from Rust upstream (diff)
downloadwireguard-linux-f4c2c90bb7b4ae1812dbaca15d9637eecaac2c9f.tar.xz
wireguard-linux-f4c2c90bb7b4ae1812dbaca15d9637eecaac2c9f.zip
rust: lock: add trylock method support for lock backend
Add a non-blocking trylock method to lock backend interface, mutex and spinlock implementations. It includes a C helper for spin_trylock. Rust Binder will use this method together with the new shrinker abstractions to avoid deadlocks in the memory shrinker. Link: https://lore.kernel.org/all/20240912-shrinker-v1-1-18b7f1253553@google.com Signed-off-by: Filipe Xavier <felipe_life@live.com> Reviewed-by: Fiona Behrens <me@kloenk.dev> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Link: https://lore.kernel.org/r/BL0PR02MB4914579914884B5D7473B3D6E96A2@BL0PR02MB4914.namprd02.prod.outlook.com [ Slightly reworded. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust/kernel/sync/lock/mutex.rs')
-rw-r--r--rust/kernel/sync/lock/mutex.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs
index 30632070ee67..c4f3b6cbfe48 100644
--- a/rust/kernel/sync/lock/mutex.rs
+++ b/rust/kernel/sync/lock/mutex.rs
@@ -115,4 +115,15 @@ unsafe impl super::Backend for MutexBackend {
// caller is the owner of the mutex.
unsafe { bindings::mutex_unlock(ptr) };
}
+
+ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {
+ // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
+ let result = unsafe { bindings::mutex_trylock(ptr) };
+
+ if result != 0 {
+ Some(())
+ } else {
+ None
+ }
+ }
}