Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp1745521lqs; Sun, 16 Jun 2024 02:47:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVgF2KQ1jYBRy9Hepg3oFLCtNbk9quMVte0jVjGLy+0Np7LHdoB8aa+3AaPAhkebR3IRmcx9NVCHZDOCtPgNenaDRRsZpL/GIbOjcHV8Q== X-Google-Smtp-Source: AGHT+IGypQGb0YzV3OMD/LpEM2VWTIq8pP70LpmBIXCOvM0lvQGX8i8UtT/TFimoRoMkC8hs0BXG X-Received: by 2002:a2e:9dd4:0:b0:2ec:453:e46e with SMTP id 38308e7fff4ca-2ec0e5a0442mr47449581fa.42.1718531237554; Sun, 16 Jun 2024 02:47:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718531237; cv=pass; d=google.com; s=arc-20160816; b=yssPSIJi8lYEbafKERVy94hex4/JLcmq5pFz/expkBDdfDdnTZE0+KENpzTjJvd9Em CwiWWBknmyq39Kh3hiDr6YewiAjf+6KK5t5ulsFxO5VqnIRfhlnXU4+fvENvCEjxmQLd CNrLL8cR7mDyytWtS+hScezIfyGp19khhgvFzX3ryG7dXzzUlEfP7zXjX9g0W/1y90jq b6HbdkMy1tphzo0GdGFUL3Plq58JwYG2/J3iSkNE3yWwOZcob4r+H2/dYSl7cJ8WAeEm sN23Crb38XxZWx7EpnmnSzjpjMLzJ88+fRdsW9/CKcFmFdceX/C5soxoWhRgEKBbXLWP TCVA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=znzItjjdQt5Gx2XVgBVIK96GA/gE2ZHpRn5IIrL3wOI=; fh=1kT5UWgMMIJZNSDll1bn+M0pgFI5y8CP8IBdNb9h1dM=; b=sl+3G/7MsHR5+5/zlcfau0RyLht0hqLJXv1T9zZ8NuyFCYMErohyhGwTwnIaB7FD9l H5urFygCQtmxRbhHdWtU4oEeJTz756exlILxCGz3nMb31WTPKUwJdyrG4NPKjMxVetHJ KHQ35h7AcCVYMSChpGOVK65X3KPxztXTICR97as9JKp5ynqGF/kx3bfuxBAP4Xucm67w RDiBFhNaKOU2WsniDX+gIZCawkiF3QfMxP51xGsNdeXgm7DMSp+GOhUgXrRw1LiqIzck LH4RHufrP1Tc+e1btfLEf5h2d+GV+jDEGE5GTuUxy3nfpZZJmKX471GRLnTIWs3gX6Yk BE9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=gdcka24V; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-216219-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-216219-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57cb742c421si3636057a12.293.2024.06.16.02.47.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 02:47:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-216219-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=gdcka24V; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-216219-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-216219-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 19A2B1F214E1 for ; Sun, 16 Jun 2024 09:47:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 88345186281; Sun, 16 Jun 2024 09:47:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="gdcka24V" Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) (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 C249216D9AA; Sun, 16 Jun 2024 09:47:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.22 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718531223; cv=none; b=M0Hi38iZ18WmZR61b0CDsv/F1B2MRvJ8HS5OY2FjEbMpo4ZmO7fR5oOSPSZUSbbSMax6e9/yG2DbVIEFbx4CGvqTbL3aeBH7lWbEOlzzIMhq++XZ3Jl6mL6anzCQGDmWt02Wm+D0L0QRn381Aju1saBXhiUkFNMPDLg/v5qPUC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718531223; c=relaxed/simple; bh=q26N2GgeVglH9gCbjku86QbUgLOk6W++2mgK2GguLKA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Psp8iExEV0tyzndRMKlcj/5Vl1ad3MlWDXpI4woI/qU7Bn7qtJRZGgdGeb5dgHoFes7qdR1mX2Hub2AFHGcp4EptAC2QZfkVhGg2jI08zYBN+MAwKDdwu3ad7j0BALg7S3awqdthhGC5uWrtxTobr7UzA3xv67dZHa9sE3pTSd8= 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=gdcka24V; arc=none smtp.client-ip=185.70.43.22 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 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 Precedence: bulk X-Mailing-List: linux-kernel@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 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