From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A2373C27C53 for ; Sun, 16 Jun 2024 09:47:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Subject:Cc:From: To:Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=znzItjjdQt5Gx2XVgBVIK96GA/gE2ZHpRn5IIrL3wOI=; b=Llxqrlmr9GMlvmS6AjEPediTXa GjW0gjnhIBhuPPEsG0ngCV83xfS4UF7TU5zr5mmtC70bpwUAQO9EicCRnV7K4ygVhnxBlYAVwHVFX nIw85SjfWgdGd8bcvcRT3bPH87SCgevwy+jOCdcnlg8VqbDMf2fpbZ9ps40v7BlXDjwzwpqrJNf3a ta4RbtTgtjWFU78zZlXaaGjDWDsGGySyDI7+5srZfb3bO+N98L6LAVhPivJno8WA6Jy1MttF/RjBO cC6XKV9O8xs3v9S0e1R6y5ImADAKtHSEThRgb3R8NsIuhCiOxuiPNkecWY2ykUMBwVmZStTJiu0B9 8W9bsyZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sImTU-00000007GOP-39eC; Sun, 16 Jun 2024 09:47:00 +0000 Received: from mail-4322.protonmail.ch ([185.70.43.22]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sImTR-00000007GNa-1pPw for linux-arm-kernel@lists.infradead.org; Sun, 16 Jun 2024 09:46:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1718531210; x=1718790410; bh=znzItjjdQt5Gx2XVgBVIK96GA/gE2ZHpRn5IIrL3wOI=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=gdcka24V73hbb95kA47a+wMdMUp4FbYBulK5vF0HVp5VuuKVIHcdEWJBB3Pr9bu6R NhlXLdn5BBT81CawX9HbibONV/+vAxt3WSCairMGdKuSRg2n6YWI8G/wJ2DbrPKpf9 HS2i9kWdmrq8qeVqsz85MnkkYJGU468NSjOAVPo/T9VGGCYLfS4Uf4L55f1dZy2R26 9IX33orpEJWw52nEKBg45ayRvmmriRTDobFE4Yw8fZNKBX/SOqToQAtOsvc54DuE62 FvHp1UnFcS7dQGK8DhM/BJHitJbhGgI0o9RkWHNf+XL3ZY7PwFhxxHMnhkctS5siBg uWVcH/2yjrgLQ== Date: Sun, 16 Jun 2024 09:46:45 +0000 To: Boqun Feng From: Benno Lossin Cc: Miguel Ojeda , Gary Guo , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Nathan Chancellor , Nick Desaulniers , kent.overstreet@gmail.com, Greg Kroah-Hartman , elver@google.com, Mark Rutland , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Catalin Marinas , torvalds@linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, Trevor Gross , dakr@redhat.com Subject: Re: [RFC 2/2] rust: sync: Add atomic support Message-ID: In-Reply-To: References: <20240612223025.1158537-3-boqun.feng@gmail.com> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: aa019b834b17958b26c96544baf90fc8c32aaadd MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240616_024657_943466_457A1C41 X-CRM114-Status: GOOD ( 35.23 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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=E2=80=AFPM Boqun Feng wrote: >>>>>>> >>>>>>> Does this make sense? >>>>>> >>>>>> Implementation-wise, if you think it is simpler or more clear/elegan= t >>>>>> 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` 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 ha= ve >>>>> to think about size, alignment, etc, and I think that's important for >>>>> atomic users and people who review their code, because before one use= s >>>>> 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` and `Atomic`? Just >>> >>> The difference is that with Atomic{I32,I64} APIs, one has to choose (an= d >>> 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: >>> >>> =09struct Refcount { >>> =09 refcount: AtomicI32, >>> =09 data: UnsafeCell >>> =09} >>> >>> but with atomic generic, people can leave that option open and do: >>> >>> =09struct Refcount { >>> =09 refcount: Atomic, >>> =09 data: UnsafeCell >>> =09} >>> >>> while it provides configurable options for experienced users, but it >>> also provides opportunities for sub-optimal types, e.g. Refcount= : >>> on ll/sc architectures, because `data` and `refcount` can be in the sam= e >>> machine-word, the accesses of `refcount` are affected by the accesses o= f >>> `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` 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 { >> /* ... */ >> } >> >> And then in the other module, you can't do this (with compiler error): >> >> pub struct Refcount { >> // ^^^^^^^ not found in this scope >> // note: trait `crate::atomic::Integer` exis= ts but is inaccessible >> refcount: Atomic, >> data: UnsafeCell, >> } >> >> 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`, or change the access of `Integer`. >> >=20 > 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 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, for example, Atomic and Atomic > have different sets of API (no inc_unless_negative() for u32). Sure, just like Gary said, you can just do: impl Atomic { 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. > To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic. > What's the downside? A bit specific example would help me understand > the real concern here. I don't like that, why have two ways of doing the same thing? People will be confused whether they should use `AtomicI32` vs `Atomic`... --- Cheers, Benno