All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: jens.korinth@tuta.io
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@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@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Rust For Linux" <rust-for-linux@vger.kernel.org>,
	"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
	"Dirk Behme" <dirk.behme@gmail.com>
Subject: Re: [PATCH v3 1/3] rust: print: Add do_once_lite macro
Date: Sun, 10 Nov 2024 11:23:15 -0800	[thread overview]
Message-ID: <ZzEII0JQWACvyYmF@tardis.local> (raw)
In-Reply-To: <OBK1JLs--B-9@tuta.io>

On Sun, Nov 10, 2024 at 08:45:49AM +0100, jens.korinth@tuta.io wrote:
> 
> 
> > With this `Once` type, we can do other things like waiting for some
> > initialization to finish. We can put the waiting as a future work, since
> > `pr_*_once!()` don't need it.
> >
> > Thoughts?
> >
> A `Once` implementation as the general approach of doing things once in
> the kernel would make a lot of sense to me and I think it would be the"rusty" way to go. I'd prefer that to `do_once_lite`, for sure.
> 
> However, I'm not sure if it can replace `do_once_lite`. Doesn't `Once` use
> a closure? I may be mistaken, but wouldn't that have at least the size of afunction pointer?
> 

No, because `Once` doesn't store the closure, it's just a atomic flag to
ensure that Once::call_once() can only execute once, as a result if you
have two threads calling the `call_once()` function in the same time,
only one will win, and the other won't get executed.

It's something like:

	https://godbolt.org/z/6PaWxnWxd

> My second concern would be with the methods of `Once` itself: We would
> have to rely on link-time optimization to inline the methods, which may bedeactivated. If they are not inlined, it could mean a far-jump on a potential

I don't think LTO is the one who inlines the methods, normal compiler
optimization could do the work, hint: `call_once()` is a generic
function over the closure.

> hot-path that could be triggered many times per second?
> 

and normally kernel will be compiled with -Copt-level=2, and the
compiler would "inline" the closures properly.

> > Another meta comment is the usage of `AtomicBool`, please see the LKMM
> > atomic patchset [2]. In short, we won't use core::sync atomics in
> > kernel, instead we can only use the atomics implemented by ourselves.
> > And we currently don't support `Atomic<u8>`, there requires some work to
> > get that done.
> >
> Ouch, I didn't know that. Sounds like an interesting topic! I would like to
> understand in more detail how the Rust part will imitate the inline assembly
> approaches of the arch-specific code in C. So far my mental model of that is
> "we'll wrap it with bindgen". ;)
> 

Feel free to review that patchset! ;-)

> > The other thing of using `AtomicBool` is that it's not the most
> > space-efficient way for `Once` (if xchg() is used) on all the archs that
> > support Rust in kernel. RISCV doesn't has a byte-wise swap, so the
> > implementation has to simulate with a 32-bit or 64-bit swap + mask,
> > that'll be totally 5 instructions, and each instruction take 4 bytes. As
> > a result, if the `swap()` is inlined (which most atomic operations would
> > be for performance reasons), you save 3 bytes by using `AtomicBool`
> > other than `AtomicI32` at data section, but you pay 4*4 extra bytes at
> > the instruction section compared to using `AtomicI32`.
> >
> Ok, wow, good point and explanation! But it does sound like an
> (arch-specific) implementation detail. I would expect the `AtomicBool`
> implementation to do the "smart" thing on every arch, no? I think on thesemantic level we want an `AtomicBool` here, regardless of how it is
> implemented on RISCV, or another arch?

The thing is `AtomicBool` is not the right layer for this, because it
has to be the same represention as a normal bool [1]. `Once` seems to be
a good layer to do it.

[1]: https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html

Regards,
Boqun

Regards,
Boqun
> Jens

  reply	other threads:[~2024-11-10 19:23 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-09 20:30 [PATCH v3 0/3] rust: Add pr_*_once macros Jens Korinth
2024-11-09 20:30 ` Jens Korinth via B4 Relay
2024-11-09 20:30 ` [PATCH v3 1/3] rust: print: Add do_once_lite macro Jens Korinth
2024-11-09 20:30   ` Jens Korinth via B4 Relay
2024-11-09 20:41   ` Miguel Ojeda
2024-11-09 21:33     ` jens.korinth
2024-11-09 21:44       ` Miguel Ojeda
2024-11-09 22:56   ` Boqun Feng
2024-11-10  7:45     ` jens.korinth
2024-11-10 19:23       ` Boqun Feng [this message]
2024-11-11  9:17     ` Alice Ryhl
2024-11-11 13:34       ` jens.korinth
2024-11-11 13:53         ` Alice Ryhl
2024-11-11 17:46           ` Boqun Feng
2024-11-09 20:30 ` [PATCH v3 2/3] rust: print: Add pr_*_once macros Jens Korinth
2024-11-09 20:30   ` Jens Korinth via B4 Relay
2024-11-09 20:30 ` [PATCH v3 3/3] rust: error: Replace pr_warn by pr_warn_once Jens Korinth
2024-11-09 20:30   ` Jens Korinth via B4 Relay
2024-11-11  7:05 ` [PATCH v3 0/3] rust: Add pr_*_once macros Dirk Behme

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=ZzEII0JQWACvyYmF@tardis.local \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=dirk.behme@gmail.com \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=jens.korinth@tuta.io \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.