rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: "Alice Ryhl" <aliceryhl@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] rust: add global lock support
Date: Fri, 13 Sep 2024 18:10:03 +0200	[thread overview]
Message-ID: <CAH5fLgiaPYphXE-Azfb0MU1EzsNpAMFntDeAt8YAMj5KAxS_xA@mail.gmail.com> (raw)
In-Reply-To: <ZuRh9niSrX6E5CWq@phenom.ffwll.local>

On Fri, Sep 13, 2024 at 6:02 PM Simona Vetter <simona.vetter@ffwll.ch> wrote:
>
> On Tue, Sep 10, 2024 at 02:23:34PM +0000, Alice Ryhl wrote:
> > 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 bindgen refuses to
> > expose __ARCH_SPIN_LOCK_UNLOCKED to Rust as a compile-time constant. It
> > just generates an `extern "C"` global reference instead. In the future,
> > we could expose the value of __ARCH_SPIN_LOCK_UNLOCKED to Rust in a way
> > that Rust can understand. This would remove the need for a separate
> > initialization step.
>
> Yeah it's just a raw C struct initializer, I wouldn't even know how to
> move that to rust.
>
> An absolute horrible idea, and I didn't try whether it's even possible:
> - put all the global locks of a type into a special linker section (we
>   need a macro anyway for them).
> - patch them up with a horrible linker script objtool patching with an
>   example lock initialized by the C macro.
>
> Even worse idea, on most architectures/config it's all zeros. Iirc the one
> I've found that might matter a bit is CONFIG_SMP=n with some lock
> debugging enabled. We could make rust support conditional on those, and
> then maybe a build-time check that it's actually all set to 0 ...
>
> Requiring the unsafe init just feels a bit disappointing to me, when the C
> side (including lockdep annotations) tries really hard to make global
> locks a one-liner.

I actually have a prototype lying around that gets rid of the
initialization step. The idea is to define some new macros:

 #define __ARCH_SPIN_LOCK_UNLOCKED      { 0 }
+#define __ARCH_SPIN_LOCK_UNLOCKED_TYP  unsigned int
+#define __ARCH_SPIN_LOCK_UNLOCKED_INT  0

Rust then uses the two new #defines to initialize the raw spin lock to
the right bytes. As long as __ARCH_SPIN_LOCK_UNLOCKED and
__ARCH_SPIN_LOCK_UNLOCKED_INT are represented by the same sequence of
bytes, it should work.

Alice

  reply	other threads:[~2024-09-13 16:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-10 14:23 [PATCH v3] rust: add global lock support Alice Ryhl
2024-09-11 14:10 ` kernel test robot
2024-09-13 16:01 ` Simona Vetter
2024-09-13 16:10   ` Alice Ryhl [this message]
2024-09-13 16:52     ` Benno Lossin
2024-09-16 15:09       ` Simona Vetter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAH5fLgiaPYphXE-Azfb0MU1EzsNpAMFntDeAt8YAMj5KAxS_xA@mail.gmail.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@samsung.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).