From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DF47175AB; Sat, 15 Jun 2024 07:09:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718435388; cv=none; b=uZMO8KeSb9AO1MA8GmARL5oZ7pj/EisJkuiX17qHihUkcJj12UUzInVgNOQBXq2UlEnjYm++KtRa1OcPDDDn7EJ3JdDJUJxCiqZbmJW05/DUE1fVsuMQ4EFEiDQx41sICFuMe1OTna7Hary2vc+Km+lA+yb+61k5Xc1BrMQ+7u4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718435388; c=relaxed/simple; bh=InMc+8IWJ3kDnz0Ha0kfcXNeCtlY2AtkImdW3XBWYi0=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YJ6vnTdR2GJ9cTygzdjDeK8Fm8ZQ4K99+TVf06tNxesAUR7ZAdez0Qap/o6MtfxneyV2sS3EvA/GVDAbvA++EzvUlnLDkTtBD7bn+OJIOfrR/a1P0KVcjupKVerhQw9k7BPvUk4oCHh0cpxwweZkQdXzjjweZNI76dYWiLTLRz4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=al09To6l; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="al09To6l" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1718435377; x=1718694577; bh=VZAtgC22c+KKhYQxrDN1MaUsBpanAgCvr8LCqwklF+A=; 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=al09To6lFGef0pLt+lAgVXExOeTE7RV2cHxyTFfnDiKPM6HH0JIcwLretKPKvEDkB Ix5L9JkKTfEIK/ZiCrif3IzTEpvAcFVkwOusL1PGEWCsRjs5OgZw5uwSSV8AHjS2gS 8BelcQduzkOZafPBwRjgQpWTmpW1VAeHoLwsail/AbQQ88Dig1tIsWm54EGO/Y2RBb EIcJR5Ko7JSuwBCJdqXbfz+OV2AqT4C/OypMsZWUPvIy++By/GMQj0b2jKMiaBCwwz wmwF3X0uK2onzKhp8BdJZZlkZGlY0gIM2nsS1K0i7NkxaIQWOpwPxeqrvrJWbpXgPA IZu3dEYfErRTg== Date: Sat, 15 Jun 2024 07:09:30 +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-1-boqun.feng@gmail.com> <20240612223025.1158537-3-boqun.feng@gmail.com> <20240613144432.77711a3a@eugeo> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: 0fe548fbc11fa01208724021192655e59f7fc5d2 Precedence: bulk X-Mailing-List: linux-arch@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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/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` 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` and `Atomic`? Just >=20 > 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: >=20 > =09struct Refcount { > =09 refcount: AtomicI32, > =09 data: UnsafeCell > =09} >=20 > but with atomic generic, people can leave that option open and do: >=20 > =09struct Refcount { > =09 refcount: Atomic, > =09 data: UnsafeCell > =09} >=20 > 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 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` 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` exists = 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`. --- Cheers, Benno > The point I'm trying to make here is: when you are using atomics, you > care about performance a lot (otherwise, why don't you use a lock?), and > because of that, you should care about the size of the atomics, because > it may affect the performance significantly.