Re: [PATCH v4] rust: add global lock support

From: Alice Ryhl
Date: Thu Oct 10 2024 - 10:01:55 EST


On Thu, Oct 10, 2024 at 3:57 PM Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:
>
> Hi Alice,
>
> Alice Ryhl <aliceryhl@xxxxxxxxxx> writes:
>
> > Add support for creating global variables that are wrapped in a mutex or
> > spinlock. Optionally, the macro can generate a special LockedBy type
> > that does not require a runtime check.
> >
> > 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.
>
> Why is this a challenge? It seems to work with locks that are not
> global.

Because normal locks are not initialized in a const expression.

> > diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global.rs
> > new file mode 100644
> > index 000000000000..fc02fac864f6
> > --- /dev/null
> > +++ b/rust/kernel/sync/lock/global.rs
> > @@ -0,0 +1,260 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +// Copyright (C) 2024 Google LLC.
> > +
> > +//! Support for defining statics containing locks.
> > +
> > +/// Defines a global lock.
> > +///
> > +/// Supports the following options:
> > +///
> > +/// * `value` specifies the initial value in the global lock.
> > +/// * `wrapper` specifies the name of the wrapper struct.
>
> Could you add an example to demonstrate when using `wrapper` option
> would be useful?

Probably only guard and locked_by are useful, but I think you need to
give the wrapper a name to reasonably use guard/locked_by.

> > + /// Lock this global lock.
>
> "Try to lock..." ?

Thanks. Good point.

Alice