rust: add global lock support
Subsystems (3)
Alice Ryhl
maintainerPATCH v6 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 v6:
- Completely rewrote the implementation to move almost everything
outside the macro.
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Adjust syntax as discussed in the last meeting.
- Fix various warnings.
- 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
@@ -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
@@ -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]>
Quoted 8 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_UNLOCKEDfrom Rust. Doing so will require changes to the C side. That change will happen as a follow-up to this patch.
In addition, I've also queued it in my lockdep-for-tip branch:
https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/log/?h=lockdep-for-tip
as I want to help route Rust lock-related patches into tip, so this is a practice round for me ;-)
Miguel, feel free to take it via rust-next with my Reviewed-by, I will drop it from my branch once if I see it shows up in v6.13-rc1.
Regards,
Boqun
Quoted 4 lines
---
This patch is based on top of rust-next as it depends on:
https://lore.kernel.org/r/BL0PR02MB4914579914884B5D7473B3D6E96A2@BL0PR02MB4914.namprd02.prod.outlook.com
---
[...]
Alice Ryhl
maintainerQuoted 15 lines · Boqun Feng
Quoted 13 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.
The separate initialization step is required because it is tricky to access the value of
__ARCH_SPIN_LOCK_UNLOCKEDfrom Rust. Doing so will require changes to the C side. That change will happen as a follow-up to this patch.
Quoted 4 lines
Quoted 2 lines
Reviewed-by Boqun Feng <[email protected]>
Thanks!
Alice
Quoted 5 lines · Boqun Feng
Reviewed-by Boqun Feng <[email protected]>In addition, I've also queued it in my lockdep-for-tip branch:
Quoted 7 lines
https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/log/?h=lockdep-for-tip
as I want to help route Rust lock-related patches into tip, so this is a practice round for me ;-)
Miguel, feel free to take it via rust-next with my Reviewed-by, I will drop it from my branch once if I see it shows up in v6.13-rc1.
No, no, it is great if you can take them :)
By "if I see it shows up in v6.13-rc1", do you mean your branch is not meant for v6.13?
Couple of things I noticed that I would normally double-check/fix in
place: the "// SAFETY: called exactly once" comment could be formatted, and I think the "Link:" tag should be before your SoB (well, at least b4 does that, which makes sense since you added it, but I have seen commits done differently too).
Thanks!
Cheers,
Miguel
Quoted 8 lines · Miguel Ojeda
Quoted 7 lines · Boqun Feng
Reviewed-by Boqun Feng <[email protected]>In addition, I've also queued it in my lockdep-for-tip branch:
https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/log/?h=lockdep-for-tip
Quoted 8 lines
Quoted 5 lines
as I want to help route Rust lock-related patches into tip, so this is a practice round for me ;-)
Miguel, feel free to take it via rust-next with my Reviewed-by, I will drop it from my branch once if I see it shows up in v6.13-rc1.
No, no, it is great if you can take them :)
Thanks!
Quoted 3 lines
By "if I see it shows up in v6.13-rc1", do you mean your branch is not meant for v6.13?
Right, so I'm acting as a sub-subsystem maintainer, and I submit pull requests to the tip tree, and then tip will send pull requests to Linus. So usually (yeah, I'm calling sometimes I've done only twice as "usually"), I submit my PR at rc2 to rc4, and tip will carry that into the next merge window. For example, since we are at v6.12-rc5, my next PR will be at v6.13-rc{2, 3 or 4}. So if this patch is going via me, it has to be in v6.14.
I think this patch has no problem to go into v6.13, so again, feel free to do it ;-)
Quoted 2 lines
Couple of things I noticed that I would normally double-check/fix in
place: the "// SAFETY: called exactly once" comment could be
Got it. This particular one needs to be "// SAFETY: Called exactly once", right?
Note that since lockdep-for-tip is not watched by linux-next, I have some flexibilities between queuing a patch and preparing it for the PR (I will need to rebase anyway). This could give contributers an early notice and we both would have less things to watch (contributers can just wait for the patches to show up eventually in Linus' tree instead of checking the list for a reply, and I only need to focus on my branch for improvement) for normal cases.
Quoted 4 lines
formatted, and I think the "Link:" tag should be before your SoB (well, at least b4 does that, which makes sense since you added it, but I have seen commits done differently too).
For this I'm following the precedents in tip tree: always put the patch links at the last line. See the "Commit notifications" in Documentation/process/maintainer-tip.rst. (And yeah, I have to manually modify this after b4 applies the patches if you have to ask ;-))
Regards,
Boqun
Quoted 4 lines
Thanks!
Cheers,
Miguel
Quoted 2 lines · Boqun Feng
"usually"), I submit my PR at rc2 to rc4, and tip will carry that into
Ah, so you only submit up to -rc4, got it. I didn't know what the cutoff was.
Quoted 2 lines
Got it. This particular one needs to be "// SAFETY: Called exactly once", right?
Yeah, with an ending period (pedantic nit, but it is the style we try to follow).
Quoted 4 lines
For this I'm following the precedents in tip tree: always put the patch links at the last line. See the "Commit notifications" in
Documentation/process/maintainer-tip.rst. (And yeah, I have to manually modify this after b4 applies the patches if you have to ask ;-))
That explains it -- thanks! Sounds good (though perhaps the bot should be amended to agree with b4... :)
Cheers,
Miguel
Miguel Ojeda
AppliedQuoted 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_UNLOCKEDfrom Rust. Doing so will require changes to the C side. That change will happen as a follow-up to this patch.
Applied to rust-next -- thanks everyone!
[ Simplified a few intra-doc links. Formatted a few comments. Reworded
title. - Miguel ]
Cheers,
Miguel