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: Sun, 16 Jun 2024 15:06:36 +0000 [thread overview]
Message-ID: <f29cb2fd-651b-4bc5-8055-e3a412192e29@proton.me> (raw)
In-Reply-To: <Zm7xySzPJcddF-I_@Boquns-Mac-mini.home>
On 16.06.24 16:08, Boqun Feng wrote:
> On Sun, Jun 16, 2024 at 09:46:45AM +0000, Benno Lossin wrote:
>> On 16.06.24 00:12, Boqun Feng wrote:
>>> On Sat, Jun 15, 2024 at 07:09:30AM +0000, Benno Lossin wrote:
>>>> On 15.06.24 03:33, Boqun Feng wrote:
>>>>> On Fri, Jun 14, 2024 at 09:22:24PM +0000, Benno Lossin wrote:
>>>>>> On 14.06.24 16:33, Boqun Feng wrote:
>>>>>>> On Fri, Jun 14, 2024 at 11:59:58AM +0200, Miguel Ojeda wrote:
>>>>>>>> On Thu, Jun 13, 2024 at 9:05 PM Boqun Feng <boqun.feng@gmail.com> wrote:
>>>>>>>>>
>>>>>>>>> Does this make sense?
>>>>>>>>
>>>>>>>> Implementation-wise, if you think it is simpler or more clear/elegant
>>>>>>>> to have the extra lower level layer, then that sounds fine.
>>>>>>>>
>>>>>>>> However, I was mainly talking about what we would eventually expose to
>>>>>>>> users, i.e. do we want to provide `Atomic<T>` to begin with? If yes,
>>>>>>>
>>>>>>> The truth is I don't know ;-) I don't have much data on which one is
>>>>>>> better. Personally, I think AtomicI32 and AtomicI64 make the users have
>>>>>>> to think about size, alignment, etc, and I think that's important for
>>>>>>> atomic users and people who review their code, because before one uses
>>>>>>> atomics, one should ask themselves: why don't I use a lock? Atomics
>>>>>>> provide the ablities to do low level stuffs and when doing low level
>>>>>>> stuffs, you want to be more explicit than ergonomic.
>>>>>>
>>>>>> How would this be different with `Atomic<i32>` and `Atomic<i64>`? Just
>>>>>
>>>>> The difference is that with Atomic{I32,I64} APIs, one has to choose (and
>>>>> think about) the size when using atomics, and cannot leave that option
>>>>> open. It's somewhere unconvenient, but as I said, atomics variables are
>>>>> different. For example, if someone is going to implement a reference
>>>>> counter struct, they can define as follow:
>>>>>
>>>>> struct Refcount<T> {
>>>>> refcount: AtomicI32,
>>>>> data: UnsafeCell<T>
>>>>> }
>>>>>
>>>>> but with atomic generic, people can leave that option open and do:
>>>>>
>>>>> struct Refcount<R, T> {
>>>>> refcount: Atomic<R>,
>>>>> data: UnsafeCell<T>
>>>>> }
>>>>>
>>>>> while it provides configurable options for experienced users, but it
>>>>> also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
>>>>> on ll/sc architectures, because `data` and `refcount` can be in the same
>>>>> machine-word, the accesses of `refcount` are affected by the accesses of
>>>>> `data`.
>>>>
>>>> I think this is a non-issue. We have two options of counteracting this:
>>>> 1. We can just point this out in reviews and force people to use
>>>> `Atomic<T>` with a concrete type. In cases where there really is the
>>>> need to be generic, we can have it.
>>>> 2. We can add a private trait in the bounds for the generic, nobody
>>>> outside of the module can access it and thus they need to use a
>>>> concrete type:
>>>>
>>>> // needs a better name
>>>> trait Integer {}
>>>> impl Integer for i32 {}
>>>> impl Integer for i64 {}
>>>>
>>>> pub struct Atomic<T: Integer> {
>>>> /* ... */
>>>> }
>>>>
>>>> And then in the other module, you can't do this (with compiler error):
>>>>
>>>> pub struct Refcount<R: Integer, T> {
>>>> // ^^^^^^^ not found in this scope
>>>> // note: trait `crate::atomic::Integer` exists but is inaccessible
>>>> refcount: Atomic<R>,
>>>> data: UnsafeCell<T>,
>>>> }
>>>>
>>>> I think that we can start with approach 2 and if we find a use-case
>>>> where generics are really unavoidable, we can either put it in the same
>>>> module as `Atomic<T>`, or change the access of `Integer`.
>>>>
>>>
>>> What's the issue of having AtomicI32 and AtomicI64 first then? We don't
>>> need to do 1 or 2 until the real users show up.
>>
>> Generics allow you to avoid code duplication (I don't think that you
>> want to create the `Atomic{I32,I64}` types via macros...). We would have
>> to do a lot of refactoring, when we want to introduce it. I don't see
>
> You can simply do
>
> type AtomicI32=Atomic<i32>;
Eh, I would think that we could just do a text replacement in this case.
Or if that doesn't work, Coccinelle should be able to do this...
> Plus, we always do refactoring in kernel, because it's impossible to get
> everything right at the first time. TBH, it's too confident to think one
> can.
I don't think that we're at the "let's just put it in" stage. This is an
RFC version, so it should be fine to completely change the approach.
I agree, that we can't get it 100% right the first time, but we should
at least strive to get a good version.
>> the harm of introducing generics from the get-go.
>>
>>> And I'd like also to point out that there are a few more trait bound
>>> designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32>
>>> have different sets of API (no inc_unless_negative() for u32).
>>
>> Sure, just like Gary said, you can just do:
>>
>> impl Atomic<i32> {
>> pub fn inc_unless_negative(&self, ordering: Ordering) -> bool;
>> }
>>
>> Or add a `HasNegative` trait.
>>
>>> Don't make me wrong, I have no doubt we can handle this in the type
>>> system, but given the design work need, won't it make sense that we take
>>> baby steps on this? We can first introduce AtomicI32 and AtomicI64 which
>>> already have real users, and then if there are some values of generic
>>> atomics, we introduce them and have proper discussion on design.
>>
>> I don't understand this point, why can't we put in the effort for a good
>> design? AFAIK we normally spend considerable time to get the API right
>> and I think in this case it would include making it generic.
>>
>
> What's the design you propose here? Well, the conversation between us is
> only the design bit I saw, elsewhere it's all handwaving that "generics
> are overall really good". I'm happy to get the API right, and it's easy
> and simple to do on concrete types. But IIUC, Gary's suggestion is to
> only have Atomic<i32> and Atomic<i64> first, and do the design later,
> which I really don't like. It may not be a complete design, but I need
> to see the design now to understand whether we need to go to that
> direction. I cannot just introduce a TBD generic.
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.
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:
- 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.
- 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?
- we should try to use either const generic or normal parameters for the
access ordering instead of putting it in the function name.
- why do we need both non-return and return variants?
I think it is probably a good idea to discuss this in our meeting.
---
Cheers,
Benno
next prev parent reply other threads:[~2024-06-16 15:06 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 [this message]
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
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=f29cb2fd-651b-4bc5-8055-e3a412192e29@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).