From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C284E54D; Sun, 16 Jun 2024 14:08:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718546896; cv=none; b=ZygeXQ43JmK9Vv8YtFMCkQq3UW2F7PQLjeRaxgJKzFGF2S1YrQ6z0PV8Vur70zDK9JpiDozSFxXOdAH32nc2sHvNpPyZGvG98vaD1cB13yu+yQJ3qN0HYa9fVPATx5CAoNm95QR7qcBdW9rr50Ox9owivCZrpJMBIZwauImviz4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718546896; c=relaxed/simple; bh=tNS9Etxsl6KrAzfyJHQLvYkh19XSh1b/dhR1ppMDTy8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=EXaF0uHX1VpR1MmA2a4f+TqyYRZ4X64kS6NqK1tP4wzOIGjd/qvG7DdqINJ6A2LA/tCGVkTDTAqcA042a7H3kakzjamZkKFU7DXJc+FyIMFqV4ZuySqGTwAa+zG7bsvMeiWYnHwTux0wOkOwTjH7UpJggwHMoLCg85FDnrTmG78= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=A1RRO9DD; arc=none smtp.client-ip=209.85.160.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="A1RRO9DD" Received: by mail-qt1-f172.google.com with SMTP id d75a77b69052e-43fecdecd32so18265701cf.1; Sun, 16 Jun 2024 07:08:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718546893; x=1719151693; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=zWCtR0NK0A/iolZufFAg4WyNKPe6pDrlaIgYrNiP6lA=; b=A1RRO9DDW/DLW0FoMYp59eNqLduQHYMFrU9FUHaXi568vVaqJYUHf6q0HpQMkCJ+La TAivrhuHnVMwbeM7xkQkFh+DB6GzieGW4y7/k9CgpU6JsnabsP8ReffIXaxBdbMNemab oQDh0fQOEMRKmfcmBqf1mUeSyEf5F4Kh1PEs43InJ3YKCDeEPV3Ch1CyAOPAk7ixK/ch FqT8zKka2joXj45PF4W7AeQvLUNnrKzkxPD4CRap5zFJsqWoYZc3viaSnUIAAwbceAHA Wt1xRzjKVDVSabR/uAtag7rHuVUO8DroZB2ubunltZ7cvuB2PCW+0u6q5Lz1smqK9hBF Nf5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718546893; x=1719151693; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zWCtR0NK0A/iolZufFAg4WyNKPe6pDrlaIgYrNiP6lA=; b=TV2pJrEiCOF/ZZ0/tqf8EM/7GQTD0m0nWQz+n9knn7SM8cF9CP1WCup0L4KzmMIcOd bhJQ3fGOSS3GBxJXDwd8HNj0hzywluEqF6Q56u4NHvMoFDxeB2AQZCn5HuljShcO8k5H 1s8MAxO0fOIT1Jx9dcYqXZV3n9Moz8JMmpdy4/0Z37i4boEJIqhjT/xcXXunCTPLhFAm 9HwtHvQMnmrgvHcKJ/cqxiYjgbnDWlGd7QKh2DOrmQ3IhlZnmBybCYe5NLAyb11gt1IF QhSOgtKWVKf1Lj+VOv1yfK+Phv6yiW8sW7MFUaHg9Smg19W7iFVKRK+T8/PeBTWeHxIu fE3w== X-Forwarded-Encrypted: i=1; AJvYcCVYmT8eKvp2zbX+pkYlpKqJ6rFfRUR3AqxLOqfktoK/VJ2S7XxZkCpuxc+3l66pLWXYRjHkLFMu3+VA+pTu4IQlqxGxGnHaF797cAntFyQFTBWK+Rhzq+mYljRG+BLf1ok/EyhB0R9uWaLgE5/r9RDskyIZFVNER8ZJUESSLKfQ1swJ+cA1hL/oBi7cKNgCoiBAm55DSmx+NukHfsYgOEAR5gBmVI21iA== X-Gm-Message-State: AOJu0YwDv3eaCo7ApHPEzaQVapHf3bAllFbx0xDK8VtjWySse+b3oVp3 vyoAaL2CMB6MIA5IpE4WwOxZYQtSmE+7gAVttxTfUKV3hkG3VLbg X-Google-Smtp-Source: AGHT+IGlsF1MCeeD/KzOzQlwQyzWV7lOdWKNgh9/d8ctGowzFmw3RRNpVqJUcbfJaqo3xJiTmTpRUQ== X-Received: by 2002:a05:622a:15d4:b0:441:2995:fa08 with SMTP id d75a77b69052e-4421687be0emr81833641cf.19.1718546893299; Sun, 16 Jun 2024 07:08:13 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-441f310cdfasm36804731cf.96.2024.06.16.07.08.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 07:08:12 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfauth.nyi.internal (Postfix) with ESMTP id 8033F1200043; Sun, 16 Jun 2024 10:08:11 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Sun, 16 Jun 2024 10:08:11 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedvfedgjeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggugfgjsehtkeertddttdejnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepvefghfeuveekudetgfevudeuudejfeeltdfhgfehgeekkeeigfdukefh gfegleefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 16 Jun 2024 10:08:10 -0400 (EDT) Date: Sun, 16 Jun 2024 07:08:09 -0700 From: Boqun Feng To: 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 , =?iso-8859-1?Q?Bj=F6rn?= 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: References: <20240612223025.1158537-3-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: 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 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 > >>> > >>> 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 { > >>> refcount: AtomicI32, > >>> data: UnsafeCell > >>> } > >>> > >>> but with atomic generic, people can leave that option open and do: > >>> > >>> struct Refcount { > >>> refcount: Atomic, > >>> data: UnsafeCell > >>> } > >>> > >>> 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`. > >> > > > > 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; 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. > 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. > 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 and Atomic 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. Regards, Boqun > > 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 >