From: Benno Lossin <benno.lossin@proton.me>
To: Boqun Feng <boqun.feng@gmail.com>
Cc: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-arch@vger.kernel.org, llvm@lists.linux.dev,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Alan Stern" <stern@rowland.harvard.edu>,
"Andrea Parri" <parri.andrea@gmail.com>,
"Will Deacon" <will@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Nicholas Piggin" <npiggin@gmail.com>,
"David Howells" <dhowells@redhat.com>,
"Jade Alglave" <j.alglave@ucl.ac.uk>,
"Luc Maranget" <luc.maranget@inria.fr>,
"Paul E. McKenney" <paulmck@kernel.org>,
"Akira Yokosawa" <akiyks@gmail.com>,
"Daniel Lustig" <dlustig@nvidia.com>,
"Joel Fernandes" <joel@joelfernandes.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nick Desaulniers" <ndesaulniers@google.com>,
kent.overstreet@gmail.com,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
elver@google.com, "Mark Rutland" <mark.rutland@arm.com>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Ingo Molnar" <mingo@redhat.com>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
"Catalin Marinas" <catalin.marinas@arm.com>,
torvalds@linux-foundation.org,
linux-arm-kernel@lists.infradead.org,
linux-fsdevel@vger.kernel.org, "Trevor Gross" <tmgross@umich.edu>,
dakr@redhat.com
Subject: Re: [RFC 2/2] rust: sync: Add atomic support
Date: Wed, 19 Jun 2024 09:09:46 +0000 [thread overview]
Message-ID: <d87c75d3-9557-4a9f-8fc2-a297a945ef2e@proton.me> (raw)
In-Reply-To: <Zm8TPRK-h2mDUX0b@Boquns-Mac-mini.home>
On 16.06.24 18:30, Boqun Feng wrote:
> On Sun, Jun 16, 2024 at 03:55:12PM +0000, Benno Lossin wrote:
> [...]
>>>>
>>>> I don't think that the idea was to "do the design later". I don't even
>>>> know how you would do that, since you need the design to submit a patch.
>>>>
>>>
>>> Then I might mis-understand Gary? He said:
>>>
>>> "Can we avoid two types and use a generic `Atomic<T>` and then implement
>>> on `Atomic<i32>` and `Atomic<i64>` instead?"
>>>
>>> , which means just replace `impl AtomicI32` with `impl Atomic<i32>` to
>>> me.
>>
>> This is a fair interpretation, but what prevents you from merging the
>> impls of functions that can be? I assumed that you would do that
>> automatically.
>>
>
> I think you missed the point, Gary's suggestion at that time sounds
> like: let's impl Atomic<i32> and Atomic<i64> first, and leave rest of
> the work for later, that is a "do the design later" to me.
Hmm, but wouldn't that just be less work for you?
>>>> I can't offer you a complete API description, since that would require
>>>> me writing it up myself. But I would recommend trying to get it to work
>>>> with generics. I got a few other comments:
>>>
>>> We should work on things that are concrete, right? It's fine that the
>>> design is not complete, and it's fine if you just recommend. But without
>>> a somewhat concrete design (doesn't have to be complete), I cannot be
>>> sure about whether we have the same vision of the future of generic
>>> atomics (see my question to Gary), that's a bit hard for me to try to
>>
>> Sorry, which question?
>
> https://lore.kernel.org/rust-for-linux/Zm7_XWe6ciy1yN-h@Boquns-Mac-mini.home/
>
>> Also to be aligned on the vision, I think we should rather talk about
>> the vision and not the design, since the design that we want to have now
>> can be different from the vision. On that note, what do you envision the
>> future of the atomic API?
>>
>
> Mine is simple, just providing AtomicI32 and AtomicI64 first, since
> there are immediate users right now, and should we learn more needs from
> the users, we get more idea about what a good API is, and we evolve from
> there.
That is fine, but since we want to get generics in the future anyways, I
think it would be good to already implement them now to also gather
feedback on them.
>>> work something out (plus I personally don't think it's a good idea, it's
>>> OK to me, but not good). Anyway, I wasn't trying to refuse to do this
>>> just based on personal reasons, but I do need to understand what you are
>>> all proposing, because I don't have one myself.
>>
>> I went back through the thread and here is what I think your argument
>> against generics is: people should think about size and alignment when
>> using atomics, which is problematic when allowing users to leave the
>> atomic generic.
>> But as I argued before, this is not an issue. Have I overlooked another
>
> You mean you said it's a non-issue but gave me two counteract? If it's
> really a non-issue, you won't need a couneraction, right? In other words
> non-generic atomics do provide some value.
The second counteractions would provide exactly the same API surface as
your non-generic version, so I don't see how going non-generic provides
any value over going generic.
The first approach was intended for a future in which we are not scared
of people using generic atomics in their structs. I don't know if we are
going to be in that future, so I think we should go with the second
approach for the time being.
>> argument? Because I don't see anything else.
>>
>>>> - I don't think that we should resort to a script to generate the Rust
>>>> code since it prevents adding good documentation & examples to the
>>>> various methods. AFAIU you want to generate the functions from
>>>> `scripts/atomic/atomics.tbl` to keep it in sync with the C side. I
>>>> looked at the git log of that file and it hasn't been changed
>>>> significantly since its inception. I don't think that there is any
>>>> benefit to generating the functions from that file.
>>>
>>> I'll leave this to other atomic maintainers.
>>>
>>>> - most of the documented functions say "See `c_function`", I don't like
>>>> this, can we either copy the C documentation (I imagine it not
>>>> changing that often, or is that assumption wrong?) or write our own?
>>>
>>> You're not wrong. AN in C side, we do have some documentation template
>>> to generate the comments (see scripts/atomic/kerneldoc). But first the
>>> format is for doxygen(I guess?), and second as you just bring up, the
>>> templates are tied with the bash script.
>>
>> I see a bash script similarly to how Wedson sees proc macros ;)
>> We should try *hard* to avoid them and only use them when there is no
>> other way.
>>
>
> I will just start with the existing mechanism and try to evolve, whether
> it's a script or proc macro, I don't mind, I want to get the work done
> and most relevant people can understand in the way the they prefer and
> step-by-step, move it to the place I think is the best for the project.
I don't think that we need a script or a proc macro. A few declarative
macros probably suffice if we go the way of generics.
>>>> - we should try to use either const generic or normal parameters for the
>>>> access ordering instead of putting it in the function name.
>>>
>>> Why is it important? Keeping it in the current way brings the value that
>>> it's not too much different than their C counterparts. Could you explain
>>> a bit the pros and cons on suffix vs const generic approach?
>>
>> Reduce code duplication, instead of 3 different variants, we can have
>> one. It allows people to build ergonomic APIs that allows the user to
>> decide which synchronization they use under the hood.
>>
>
> I already mentioned why I think it's good in the current way, I will
> defer it to others on their inputs.
>
>>>> - why do we need both non-return and return variants?
>>>>
>>>
>>> Historical reason I guess. Plus maybe some architectures have a better
>>> implementation on non-return atomics IIRC.
>>
>> Could we get some more concrete arguments for why we would need these in
>> rust? If the reason is purely historical, then we shouldn't expose the
>
> Sure. Look like my memory is correct, at least on ARM64 they are
> different instructions (see arch/arm64/include/asm/atomic_lse.h)
>
> non-return atomics on ARM64:
>
> #define ATOMIC_OP(op, asm_op) \
> static __always_inline void \
> __lse_atomic_##op(int i, atomic_t *v) \
> { \
> asm volatile( \
> __LSE_PREAMBLE \
> " " #asm_op " %w[i], %[v]\n" \
> : [v] "+Q" (v->counter) \
> : [i] "r" (i)); \
> }
>
> value-return atomics on ARM64:
>
> #define ATOMIC_FETCH_OP(name, mb, op, asm_op, cl...) \
> static __always_inline int \
> __lse_atomic_fetch_##op##name(int i, atomic_t *v) \
> { \
> int old; \
> \
> asm volatile( \
> __LSE_PREAMBLE \
> " " #asm_op #mb " %w[i], %w[old], %[v]" \
> : [v] "+Q" (v->counter), \
> [old] "=r" (old) \
> : [i] "r" (i) \
> : cl); \
> \
> return old; \
> }
>
> It may not be easy to see the different instructions from the pasted
> code, but you can find them in the header file, also you could notice
> that the number of operands is different?
This is not my expertise, so I believe you :)
>> non-return variant IMO. If it is because of performance, then we can
>> only expose them in the respective arches.
>>
>
> Hmm.. so we ask user to write arch-specific code like:
>
> pub fn increase_counter(&self) {
> #[cfg(CONFIG_ARM64)]
> self.counter.inc();
>
> #[cfg(CONFIG_X86_64)]
> let _ = self.counter.inc_return();
> }
>
> are you sure it's a good idea?
No that looks horrible.
Maybe there is something that we can do with generics, but I don't know
if it is worth it. I guess we can leave that open for the time being.
---
Cheers,
Benno
next prev parent reply other threads:[~2024-06-19 9:09 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-12 22:30 [RFC 0/2] Initial LKMM atomics support in Rust Boqun Feng
2024-06-12 22:30 ` [RFC 1/2] rust: Introduce atomic API helpers Boqun Feng
2024-06-13 5:38 ` Greg Kroah-Hartman
2024-06-13 9:17 ` Peter Zijlstra
2024-06-13 10:03 ` Greg Kroah-Hartman
2024-06-13 10:36 ` Mark Rutland
2024-06-14 10:31 ` Mark Rutland
2024-06-14 20:13 ` Boqun Feng
2024-06-12 22:30 ` [RFC 2/2] rust: sync: Add atomic support Boqun Feng
2024-06-13 5:40 ` Greg Kroah-Hartman
2024-06-13 13:44 ` Gary Guo
2024-06-13 16:30 ` Boqun Feng
2024-06-13 17:19 ` Gary Guo
2024-06-13 17:22 ` Miguel Ojeda
2024-06-13 19:05 ` Boqun Feng
2024-06-14 9:59 ` Miguel Ojeda
2024-06-14 14:33 ` Boqun Feng
2024-06-14 21:22 ` Benno Lossin
2024-06-15 1:33 ` Boqun Feng
2024-06-15 7:09 ` Benno Lossin
2024-06-15 22:12 ` Boqun Feng
2024-06-16 9:46 ` Benno Lossin
2024-06-16 14:08 ` Boqun Feng
2024-06-16 15:06 ` Benno Lossin
2024-06-16 15:34 ` Boqun Feng
2024-06-16 15:55 ` Benno Lossin
2024-06-16 16:30 ` Boqun Feng
2024-06-19 9:09 ` Benno Lossin [this message]
2024-06-19 15:00 ` Boqun Feng
2024-06-16 17:05 ` Boqun Feng
2024-06-16 9:51 ` Kent Overstreet
2024-06-16 14:16 ` Boqun Feng
2024-06-16 14:35 ` Boqun Feng
2024-06-16 15:14 ` Miguel Ojeda
2024-06-16 15:32 ` Kent Overstreet
2024-06-16 15:54 ` Boqun Feng
2024-06-16 17:30 ` Boqun Feng
2024-06-16 17:59 ` Kent Overstreet
2024-06-16 15:50 ` Boqun Feng
2024-06-16 15:23 ` Kent Overstreet
2024-06-15 1:03 ` John Hubbard
2024-06-15 1:24 ` Boqun Feng
2024-06-15 1:28 ` John Hubbard
2024-06-15 2:39 ` Boqun Feng
2024-06-15 2:51 ` John Hubbard
2024-06-16 14:51 ` Gary Guo
2024-06-16 15:06 ` Boqun Feng
2024-06-17 5:36 ` Boqun Feng
2024-06-17 5:42 ` Boqun Feng
2024-06-19 9:30 ` Benno Lossin
2024-06-16 0:51 ` Andrew Lunn
2024-06-14 9:51 ` Peter Zijlstra
2024-06-14 14:18 ` Boqun Feng
2024-06-13 20:25 ` Boqun Feng
2024-06-14 10:40 ` Mark Rutland
2024-06-14 20:20 ` Boqun Feng
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=d87c75d3-9557-4a9f-8fc2-a297a945ef2e@proton.me \
--to=benno.lossin@proton.me \
--cc=a.hindborg@samsung.com \
--cc=akiyks@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=bp@alien8.de \
--cc=catalin.marinas@arm.com \
--cc=dakr@redhat.com \
--cc=dave.hansen@linux.intel.com \
--cc=dhowells@redhat.com \
--cc=dlustig@nvidia.com \
--cc=elver@google.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=hpa@zytor.com \
--cc=j.alglave@ucl.ac.uk \
--cc=joel@joelfernandes.org \
--cc=kent.overstreet@gmail.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=luc.maranget@inria.fr \
--cc=mark.rutland@arm.com \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=mingo@redhat.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=npiggin@gmail.com \
--cc=ojeda@kernel.org \
--cc=parri.andrea@gmail.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
--cc=tglx@linutronix.de \
--cc=tmgross@umich.edu \
--cc=torvalds@linux-foundation.org \
--cc=wedsonaf@gmail.com \
--cc=will@kernel.org \
--cc=x86@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).