Skip to contentSkip to conversation

rust: add global lock support

PATCH Superseded Alice Ryhl 1 participant 2 messages Oct 23, 2024

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

PATCH v5 1/1 rust: add global lock support

Add support for creating global variables that are wrapped in a mutex or spinlock.

The implementation here is intended to replace the global mutex workaround found in the Rust Binder RFC [1]. In both cases, the global lock must be initialized before first use. The macro is unsafe to use for the same reason.

The separate initialization step is required because it is tricky to access the value of __ARCH_SPIN_LOCK_UNLOCKED from Rust. Doing so will require changes to the C side. That change will happen as a follow-up to this patch.

---
This patch is based on top of rust-next as it depends on:
https://lore.kernel.org/r/BL0PR02MB4914579914884B5D7473B3D6E96A2@BL0PR02MB4914.namprd02.prod.outlook.com
---
Changes in v5:
- Completely rewrote the implementation to move almost everything
outside the macro.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Evaluate $value in the surrounding scope.
- Make types pub(crate) to avoid "private type in public interface"
errors when using the macro.
- Add trylock method.
- using https://lore.kernel.org/r/BL0PR02MB4914579914884B5D7473B3D6E96A2@BL0PR02MB4914.namprd02.prod.outlook.com
- Add Send/Sync implementations of LockedBy.
- Fix examples so they compile.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rewrite to provide a macro instead.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Require self: Pin<&Self> and recommend Pin::static_ref.
- Other doc improvements including new example.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/sync.rs             |   1 +
 rust/kernel/sync/lock.rs        |   3 +
 rust/kernel/sync/lock/global.rs | 303 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 307 insertions(+)
rust/kernel/sync.rs +1 -0
index 0ab20975a3b5..2bdd1cffcdab 100644
@@ -14,6 +14,7 @@
 
 pub use arc::{Arc, ArcBorrow, UniqueArc};
 pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
+pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
 pub use lock::mutex::{new_mutex, Mutex};
 pub use lock::spinlock::{new_spinlock, SpinLock};
 pub use locked_by::LockedBy;
rust/kernel/sync/lock.rs +3 -0
index 90cc5416529b..a5d89cebf106 100644
@@ -13,6 +13,9 @@
 pub mod mutex;
 pub mod spinlock;
 
+pub(super) mod global;
+pub use global::{GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
+
 /// The "backend" of a lock.
 ///
 /// It is the actual implementation of the lock, without the need to repeat patterns used in all
rust/kernel/sync/lock/global.rs new file +303 -0

303 lines not on this page · raw message

---
base-commit: 6ce162a002657910104c7a07fb50017681bc476c
change-id: 20240826-static-mutex-a4b228e0e6aa

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
author maintainer
#1 lore raw
Quoted 9 lines · Alice Ryhl

Add support for creating global variables that are wrapped in a mutex or spinlock.

The implementation here is intended to replace the global mutex workaround found in the Rust Binder RFC [1]. In both cases, the global lock must be initialized before first use. The macro is unsafe to use for the same reason.

Quoted 7 lines

The separate initialization step is required because it is tricky to access the value of __ARCH_SPIN_LOCK_UNLOCKED from Rust. Doing so will require changes to the C side. That change will happen as a follow-up to this patch.

Something has gone terribly wrong with the versioning. This should be v6. Resending ...

Sorry!

Alice