[03/13] rust: lock: introduce `Mutex`
Commit Message
From: Wedson Almeida Filho <walmeida@microsoft.com>
This is the `struct mutex` lock backend and allows Rust code to use the
kernel mutex idiomatically.
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: Waiman Long <longman@redhat.com>
Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
---
rust/helpers.c | 7 ++
rust/kernel/sync.rs | 1 +
rust/kernel/sync/lock.rs | 2 +
rust/kernel/sync/lock/mutex.rs | 118 +++++++++++++++++++++++++++++++++
4 files changed, 128 insertions(+)
create mode 100644 rust/kernel/sync/lock/mutex.rs
Comments
On Thu, Mar 30, 2023 at 01:39:44AM -0300, Wedson Almeida Filho wrote:
> From: Wedson Almeida Filho <walmeida@microsoft.com>
>
> This is the `struct mutex` lock backend and allows Rust code to use the
> kernel mutex idiomatically.
What, if anything, are the plans to support the various lockdep
annotations? Idem for the spinlock thing in the other patch I suppose.
On Thu, Mar 30, 2023 at 03:01:08PM +0200, Peter Zijlstra wrote:
> On Thu, Mar 30, 2023 at 01:39:44AM -0300, Wedson Almeida Filho wrote:
> > From: Wedson Almeida Filho <walmeida@microsoft.com>
> >
> > This is the `struct mutex` lock backend and allows Rust code to use the
> > kernel mutex idiomatically.
>
> What, if anything, are the plans to support the various lockdep
> annotations? Idem for the spinlock thing in the other patch I suppose.
FWIW:
* At the init stage, SpinLock and Mutex in Rust use initializers
that are aware of the lockdep, so everything (lockdep_map and
lock_class) is all set up.
* At acquire or release time, Rust locks just use ffi to call C
functions that have lockdep annotations in them, so lockdep
should just work.
In fact, I shared some same worry as you, so I already work on adding
lockdep selftests for Rust lock APIs, will send them shortly, although
they are just draft.
Regards,
Boqun
On Thu, Mar 30, 2023 at 11:47:12AM -0700, Boqun Feng wrote:
> On Thu, Mar 30, 2023 at 03:01:08PM +0200, Peter Zijlstra wrote:
> > On Thu, Mar 30, 2023 at 01:39:44AM -0300, Wedson Almeida Filho wrote:
> > > From: Wedson Almeida Filho <walmeida@microsoft.com>
> > >
> > > This is the `struct mutex` lock backend and allows Rust code to use the
> > > kernel mutex idiomatically.
> >
> > What, if anything, are the plans to support the various lockdep
> > annotations? Idem for the spinlock thing in the other patch I suppose.
>
> FWIW:
>
> * At the init stage, SpinLock and Mutex in Rust use initializers
> that are aware of the lockdep, so everything (lockdep_map and
> lock_class) is all set up.
>
> * At acquire or release time, Rust locks just use ffi to call C
> functions that have lockdep annotations in them, so lockdep
> should just work.
>
> In fact, I shared some same worry as you, so I already work on adding
> lockdep selftests for Rust lock APIs, will send them shortly, although
> they are just draft.
>
Needless to say, the test shows that lockdep works for deadlock
detection (although currently they are only simple cases):
[...] locking selftest: Selftests for Rust locking APIs
[...] rust_locking_selftest::SpinLockAATest:
[...]
[...] ============================================
[...] WARNING: possible recursive locking detected
[...] 6.3.0-rc1-00049-gee35790bd43e-dirty #99 Not tainted
[...] --------------------------------------------
[...] swapper/0/0 is trying to acquire lock:
[...] ffffffff8c603e30 (A1){+.+.}-{2:2}, at: _RNvXNtNtNtCs1t6xtuX2C8s_6kernel4sync4lock8spinlockNtB2_15SpinLockBackendNtB4_7Backend4lock+0x6/0x10
[...]
[...] but task is already holding lock:
[...] ffffffff8c603de0 (A1){+.+.}-{2:2}, at: _RNvXNtNtNtCs1t6xtuX2C8s_6kernel4sync4lock8spinlockNtB2_15SpinLockBackendNtB4_7Backend4lock+0x6/0x10
[...]
[...] other info that might help us debug this:
[...] Possible unsafe locking scenario:
[...]
[...] CPU0
[...] ----
[...] lock(A1);
[...] lock(A1);
[...]
[...] *** DEADLOCK ***
[...]
[...] May be due to missing lock nesting notation
[...]
[...] 1 lock held by swapper/0/0:
[...] #0: ffffffff8c603de0 (A1){+.+.}-{2:2}, at: _RNvXNtNtNtCs1t6xtuX2C8s_6kernel4sync4lock8spinlockNtB2_15SpinLockBackendNtB4_7Backend4lock+0x6/0x10
[...]
[...] stack backtrace:
[...] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.3.0-rc1-00049-gee35790bd43e-dirty #99
[...] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.1-1-1 04/01/2014
[...] Call Trace:
[...] <TASK>
[...] dump_stack_lvl+0x6d/0xa0
[...] __lock_acquire+0x825/0x2e20
[...] ? __lock_acquire+0x626/0x2e20
[...] ? prb_read_valid+0x24/0x50
[...] ? printk_get_next_message+0xf6/0x380
[...] ? _RNvXNtNtNtCs1t6xtuX2C8s_6kernel4sync4lock8spinlockNtB2_15SpinLockBackendNtB4_7Backend4lock+0x6/0x10
[...] lock_acquire+0x109/0x2c0
[...] ? _RNvXNtNtNtCs1t6xtuX2C8s_6kernel4sync4lock8spinlockNtB2_15SpinLockBackendNtB4_7Backend4lock+0x6/0x10
[...] _raw_spin_lock+0x2e/0x40
[...] ? _RNvXNtNtNtCs1t6xtuX2C8s_6kernel4sync4lock8spinlockNtB2_15SpinLockBackendNtB4_7Backend4lock+0x6/0x10
[...] _RNvXNtNtNtCs1t6xtuX2C8s_6kernel4sync4lock8spinlockNtB2_15SpinLockBackendNtB4_7Backend4lock+0x6/0x10
[...] _RNvXCsaDWbe1gW6fC_21rust_locking_selftestNtB2_14SpinLockAATestNtB2_8LockTest4test+0xa5/0xe0
[...] ? prb_read_valid+0x24/0x50
[...] dotest+0x5a/0x8d0
[...] rust_locking_test+0xf8/0x210
[...] ? _printk+0x58/0x80
[...] ? local_lock_release+0x60/0x60
[...] locking_selftest+0x328f/0x32b0
[...] start_kernel+0x285/0x420
[...] secondary_startup_64_no_verify+0xe1/0xeb
[...] </TASK>
[...] ok | lockclass mask: 100, debug_locks: 0, expected: 0
Regards,
Boqun
> Regards,
> Boqun
On Thu, Mar 30, 2023 at 11:47:12AM -0700, Boqun Feng wrote:
> On Thu, Mar 30, 2023 at 03:01:08PM +0200, Peter Zijlstra wrote:
> > On Thu, Mar 30, 2023 at 01:39:44AM -0300, Wedson Almeida Filho wrote:
> > > From: Wedson Almeida Filho <walmeida@microsoft.com>
> > >
> > > This is the `struct mutex` lock backend and allows Rust code to use the
> > > kernel mutex idiomatically.
> >
> > What, if anything, are the plans to support the various lockdep
> > annotations? Idem for the spinlock thing in the other patch I suppose.
>
> FWIW:
>
> * At the init stage, SpinLock and Mutex in Rust use initializers
> that are aware of the lockdep, so everything (lockdep_map and
> lock_class) is all set up.
>
> * At acquire or release time, Rust locks just use ffi to call C
> functions that have lockdep annotations in them, so lockdep
> should just work.
>
ffi is what the C++ world calls RAII ?
But yes, I got that far, but I wondered about things like
spin_lock_nested(&foo, SINGLE_DEPTH_NESTING) and other such 'advanced'
annotations.
Surely we're going to be needing them at some point. I suppose you can
do the single depth nesting one with a special guard type (or whatever
you call that in the rust world) ?
On Mon, Apr 03, 2023 at 10:20:52AM +0200, Peter Zijlstra wrote:
> On Thu, Mar 30, 2023 at 11:47:12AM -0700, Boqun Feng wrote:
> > On Thu, Mar 30, 2023 at 03:01:08PM +0200, Peter Zijlstra wrote:
> > > On Thu, Mar 30, 2023 at 01:39:44AM -0300, Wedson Almeida Filho wrote:
> > > > From: Wedson Almeida Filho <walmeida@microsoft.com>
> > > >
> > > > This is the `struct mutex` lock backend and allows Rust code to use the
> > > > kernel mutex idiomatically.
> > >
> > > What, if anything, are the plans to support the various lockdep
> > > annotations? Idem for the spinlock thing in the other patch I suppose.
> >
> > FWIW:
> >
> > * At the init stage, SpinLock and Mutex in Rust use initializers
> > that are aware of the lockdep, so everything (lockdep_map and
> > lock_class) is all set up.
> >
> > * At acquire or release time, Rust locks just use ffi to call C
> > functions that have lockdep annotations in them, so lockdep
> > should just work.
> >
>
> ffi is what the C++ world calls RAII ?
No, ffi is 'foreign function interface', it just means that the caller will use
the same ABI as the callee.
> But yes, I got that far, but I wondered about things like
> spin_lock_nested(&foo, SINGLE_DEPTH_NESTING) and other such 'advanced'
> annotations.
>
> Surely we're going to be needing them at some point. I suppose you can
> do the single depth nesting one with a special guard type (or whatever
> you call that in the rust world) ?
I haven't looked at all the advanced annotations, but something like
spin_lock_nested/mutex_lock_nested can be exposed as a lock_nested() associated
function of the `Lock` type, so one would do:
let guard = my_mutex.lock_nested(SINGLE_DEPTH_NESTING);
// Do something with data protected by my_mutex.
On Mon, Apr 03, 2023 at 10:20:52AM +0200, Peter Zijlstra wrote:
> On Thu, Mar 30, 2023 at 11:47:12AM -0700, Boqun Feng wrote:
> > On Thu, Mar 30, 2023 at 03:01:08PM +0200, Peter Zijlstra wrote:
> > > On Thu, Mar 30, 2023 at 01:39:44AM -0300, Wedson Almeida Filho wrote:
> > > > From: Wedson Almeida Filho <walmeida@microsoft.com>
> > > >
> > > > This is the `struct mutex` lock backend and allows Rust code to use the
> > > > kernel mutex idiomatically.
> > >
> > > What, if anything, are the plans to support the various lockdep
> > > annotations? Idem for the spinlock thing in the other patch I suppose.
> >
> > FWIW:
> >
> > * At the init stage, SpinLock and Mutex in Rust use initializers
> > that are aware of the lockdep, so everything (lockdep_map and
> > lock_class) is all set up.
> >
> > * At acquire or release time, Rust locks just use ffi to call C
> > functions that have lockdep annotations in them, so lockdep
> > should just work.
> >
>
> ffi is what the C++ world calls RAII ?
>
ffi is foreign function interface, it means calling a C function from
Rust. Sorry if I make things confusing ;-)
> But yes, I got that far, but I wondered about things like
> spin_lock_nested(&foo, SINGLE_DEPTH_NESTING) and other such 'advanced'
> annotations.
>
Right, I haven't really thought through them, but I think it's easy to
add them later (famous later words).
> Surely we're going to be needing them at some point. I suppose you can
> do the single depth nesting one with a special guard type (or whatever
> you call that in the rust world) ?
or a different method for Lock:
impl Lock { // implementation block for type `Lock`
// v function is called via a.lock_nested(SINGLE_DEPTH_NESTING), a is a Lock
fn lock_nested(&self, level: i32) -> Guard<..> {
// ^ defines a function ^ returns a guard
..
}
}
since Rust side just uses the same function to unlock as C side, so a
normal Guard type suffices, because we don't treat nested lock
differently at unlock time. But if we were to add some more checking at
compile time, we could have a slight different Guard or something.
Regards,
Boqun
On Mon, 3 Apr 2023 10:50:09 -0300
Wedson Almeida Filho <wedsonaf@gmail.com> wrote:
> On Mon, Apr 03, 2023 at 10:20:52AM +0200, Peter Zijlstra wrote:
> > On Thu, Mar 30, 2023 at 11:47:12AM -0700, Boqun Feng wrote:
> > > On Thu, Mar 30, 2023 at 03:01:08PM +0200, Peter Zijlstra wrote:
> > > > On Thu, Mar 30, 2023 at 01:39:44AM -0300, Wedson Almeida Filho wrote:
> > > > > From: Wedson Almeida Filho <walmeida@microsoft.com>
> > > > >
> > > > > This is the `struct mutex` lock backend and allows Rust code to use the
> > > > > kernel mutex idiomatically.
> > > >
> > > > What, if anything, are the plans to support the various lockdep
> > > > annotations? Idem for the spinlock thing in the other patch I suppose.
> > >
> > > FWIW:
> > >
> > > * At the init stage, SpinLock and Mutex in Rust use initializers
> > > that are aware of the lockdep, so everything (lockdep_map and
> > > lock_class) is all set up.
> > >
> > > * At acquire or release time, Rust locks just use ffi to call C
> > > functions that have lockdep annotations in them, so lockdep
> > > should just work.
> > >
> >
> > ffi is what the C++ world calls RAII ?
>
> No, ffi is 'foreign function interface', it just means that the caller will use
> the same ABI as the callee.
>
> > But yes, I got that far, but I wondered about things like
> > spin_lock_nested(&foo, SINGLE_DEPTH_NESTING) and other such 'advanced'
> > annotations.
> >
> > Surely we're going to be needing them at some point. I suppose you can
> > do the single depth nesting one with a special guard type (or whatever
> > you call that in the rust world) ?
>
> I haven't looked at all the advanced annotations, but something like
> spin_lock_nested/mutex_lock_nested can be exposed as a lock_nested() associated
> function of the `Lock` type, so one would do:
>
> let guard = my_mutex.lock_nested(SINGLE_DEPTH_NESTING);
> // Do something with data protected by my_mutex.
I don't think an additional function would work. It's not okay to
perform both nested locking and non-nested locking on the same lock
because non-nested locking will give you a mutable reference, and
getting another reference from nested lock guard would violate aliasing
rules.
A new lock type would be needed for nested locking, and guard of it can
only hand out immutable reference.
Best,
Gary
On Mon, Apr 03, 2023 at 04:25:29PM +0100, Gary Guo wrote:
> On Mon, 3 Apr 2023 10:50:09 -0300
> Wedson Almeida Filho <wedsonaf@gmail.com> wrote:
>
> > On Mon, Apr 03, 2023 at 10:20:52AM +0200, Peter Zijlstra wrote:
> > > On Thu, Mar 30, 2023 at 11:47:12AM -0700, Boqun Feng wrote:
> > > > On Thu, Mar 30, 2023 at 03:01:08PM +0200, Peter Zijlstra wrote:
> > > > > On Thu, Mar 30, 2023 at 01:39:44AM -0300, Wedson Almeida Filho wrote:
> > > > > > From: Wedson Almeida Filho <walmeida@microsoft.com>
> > > > > >
> > > > > > This is the `struct mutex` lock backend and allows Rust code to use the
> > > > > > kernel mutex idiomatically.
> > > > >
> > > > > What, if anything, are the plans to support the various lockdep
> > > > > annotations? Idem for the spinlock thing in the other patch I suppose.
> > > >
> > > > FWIW:
> > > >
> > > > * At the init stage, SpinLock and Mutex in Rust use initializers
> > > > that are aware of the lockdep, so everything (lockdep_map and
> > > > lock_class) is all set up.
> > > >
> > > > * At acquire or release time, Rust locks just use ffi to call C
> > > > functions that have lockdep annotations in them, so lockdep
> > > > should just work.
> > > >
> > >
> > > ffi is what the C++ world calls RAII ?
> >
> > No, ffi is 'foreign function interface', it just means that the caller will use
> > the same ABI as the callee.
> >
> > > But yes, I got that far, but I wondered about things like
> > > spin_lock_nested(&foo, SINGLE_DEPTH_NESTING) and other such 'advanced'
> > > annotations.
> > >
> > > Surely we're going to be needing them at some point. I suppose you can
> > > do the single depth nesting one with a special guard type (or whatever
> > > you call that in the rust world) ?
> >
> > I haven't looked at all the advanced annotations, but something like
> > spin_lock_nested/mutex_lock_nested can be exposed as a lock_nested() associated
> > function of the `Lock` type, so one would do:
> >
> > let guard = my_mutex.lock_nested(SINGLE_DEPTH_NESTING);
> > // Do something with data protected by my_mutex.
>
> I don't think an additional function would work. It's not okay to
> perform both nested locking and non-nested locking on the same lock
Note that lock_nested() here is simply a lockdep concept, it means
locking nested under the same lock class (key), not lock instance, for
example:
spinlock_t X1;
spinlock_t X2;
// X1 and X2 are of the same lock class X
spin_lock(&X1);
spin_lock(&X2); // lockdep will report a deadlock.
// However, if we know that X1 and X2 has some ordering to lock,
// e.g. X1 is the lock for a directory and X2 is the lock for
// the file in the directory, we can
spin_lock(&X1);
spin_lock_nested(&X2, SINGLE_DEPTH_NESTING);
// and lockdep won't complain.
There is some design space here for Rust, since we may be able to put
the nested information in the type.
Regards,
Boqun
> because non-nested locking will give you a mutable reference, and
> getting another reference from nested lock guard would violate aliasing
> rules.
>
> A new lock type would be needed for nested locking, and guard of it can
> only hand out immutable reference.
>
> Best,
> Gary
@@ -21,6 +21,7 @@
#include <linux/bug.h>
#include <linux/build_bug.h>
#include <linux/refcount.h>
+#include <linux/mutex.h>
__noreturn void rust_helper_BUG(void)
{
@@ -28,6 +29,12 @@ __noreturn void rust_helper_BUG(void)
}
EXPORT_SYMBOL_GPL(rust_helper_BUG);
+void rust_helper_mutex_lock(struct mutex *lock)
+{
+ mutex_lock(lock);
+}
+EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
+
refcount_t rust_helper_REFCOUNT_INIT(int n)
{
return (refcount_t)REFCOUNT_INIT(n);
@@ -11,6 +11,7 @@ mod arc;
pub mod lock;
pub use arc::{Arc, ArcBorrow, UniqueArc};
+pub use lock::mutex::Mutex;
/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
#[repr(transparent)]
@@ -10,6 +10,8 @@ use crate::{bindings, init::PinInit, pin_init, str::CStr, types::Opaque};
use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned};
use macros::pin_data;
+pub mod mutex;
+
/// The "backend" of a lock.
///
/// It is the actual implementation of the lock, without the need to repeat patterns used in all
new file mode 100644
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! A kernel mutex.
+//!
+//! This module allows Rust code to use the kernel's `struct mutex`.
+
+use crate::bindings;
+
+/// Creates a [`Mutex`] initialiser with the given name and a newly-created lock class.
+///
+/// It uses the name if one is given, otherwise it generates one based on the file name and line
+/// number.
+#[macro_export]
+macro_rules! new_mutex {
+ ($inner:expr $(, $name:literal)? $(,)?) => {
+ $crate::sync::Mutex::new(
+ $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
+ };
+}
+
+/// A mutual exclusion primitive.
+///
+/// Exposes the kernel's [`struct mutex`]. When multiple threads attempt to lock the same mutex,
+/// only one at a time is allowed to progress, the others will block (sleep) until the mutex is
+/// unlocked, at which point another thread will be allowed to wake up and make progress.
+///
+/// Since it may block, [`Mutex`] needs to be used with care in atomic contexts.
+///
+/// Instances of [`Mutex`] need a lock class and to be pinned. The recommended way to create such
+/// instances is with the [`pin_init`](crate::pin_init) and [`new_mutex`] macros.
+///
+/// # Examples
+///
+/// The following example shows how to declare, allocate and initialise a struct (`Example`) that
+/// contains an inner struct (`Inner`) that is protected by a mutex.
+///
+/// ```
+/// use kernel::{init::InPlaceInit, init::PinInit, new_mutex, pin_init, sync::Mutex};
+///
+/// struct Inner {
+/// a: u32,
+/// b: u32,
+/// }
+///
+/// #[pin_data]
+/// struct Example {
+/// c: u32,
+/// #[pin]
+/// d: Mutex<Inner>,
+/// }
+///
+/// impl Example {
+/// fn new() -> impl PinInit<Self> {
+/// pin_init!(Self {
+/// c: 10,
+/// d <- new_mutex!(Inner { a: 20, b: 30 }),
+/// })
+/// }
+/// }
+///
+/// // Allocate a boxed `Example`.
+/// let e = Box::pin_init(Example::new())?;
+/// assert_eq!(e.c, 10);
+/// assert_eq!(e.d.lock().a, 20);
+/// assert_eq!(e.d.lock().b, 30);
+/// ```
+///
+/// The following example shows how to use interior mutability to modify the contents of a struct
+/// protected by a mutex despite only having a shared reference:
+///
+/// ```
+/// use kernel::sync::Mutex;
+///
+/// struct Example {
+/// a: u32,
+/// b: u32,
+/// }
+///
+/// fn example(m: &Mutex<Example>) {
+/// let mut guard = m.lock();
+/// guard.a += 10;
+/// guard.b += 20;
+/// }
+/// ```
+///
+/// [`struct mutex`]: ../../../../include/linux/mutex.h
+pub type Mutex<T> = super::Lock<T, MutexBackend>;
+
+/// A kernel `struct mutex` lock backend.
+pub struct MutexBackend;
+
+// SAFETY: The underlying kernel `struct mutex` object ensures mutual exclusion.
+unsafe impl super::Backend for MutexBackend {
+ type State = bindings::mutex;
+ type GuardState = ();
+
+ unsafe fn init(
+ ptr: *mut Self::State,
+ name: *const core::ffi::c_char,
+ key: *mut bindings::lock_class_key,
+ ) {
+ // SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and
+ // `key` are valid for read indefinitely.
+ unsafe { bindings::__mutex_init(ptr, name, key) }
+ }
+
+ unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
+ // SAFETY: The safety requirements of this function ensure that `ptr` points to valid
+ // memory, and that it has been initialised before.
+ unsafe { bindings::mutex_lock(ptr) };
+ }
+
+ unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
+ // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
+ // caller is the owner of the mutex.
+ unsafe { bindings::mutex_unlock(ptr) };
+ }
+}