Re: [PATCH 03/13] rust: lock: introduce `Mutex`

From: Boqun Feng
Date: Mon Apr 03 2023 - 10:05:02 EST


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@xxxxxxxxxxxxx>
> > > >
> > > > 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