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 31955C27C6E for ; Mon, 17 Jun 2024 05:42:45 +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:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=djPwZuKbvrmoSptmQGftbxnFBiXhu4eAY3uKLEqMxtQ=; b=3tDLpytCJ0GOHaS1F4vTDePLRR rXl/AtM8apLEzlMywXF/IDT3uBcpghO37FneIlkZkIPyFVLUpEnZ+Bq16/pMogq+1m1k2Yv2cLbGV LXZc80ztAjN/Z40/hsjUP9V+ax3afYp5O/96//cgrB/X2Uj+QRpZFRSUwI9K26joLzOa0VE/7R3rD 293Vj1RjviYrJ4SH0YmGzubvhsz/4ts6SLyfgPiEBrdK5MIUt64uUO4VwsUMaPHajxxG3XjDQ0JJn AwkkbcFazvBDBAmVNo/Bolttuhz5ypBvQrOyIFCMdXqTsDXTp93bjNMl2/nnGEAJGBAo3MrxBXGtl tSzm3qjw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJ58R-00000009FHt-3yep; Mon, 17 Jun 2024 05:42:31 +0000 Received: from mail-qt1-x832.google.com ([2607:f8b0:4864:20::832]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJ58O-00000009FGw-1dr7 for linux-arm-kernel@lists.infradead.org; Mon, 17 Jun 2024 05:42:30 +0000 Received: by mail-qt1-x832.google.com with SMTP id d75a77b69052e-44056295c8cso20185151cf.3 for ; Sun, 16 Jun 2024 22:42:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718602946; x=1719207746; darn=lists.infradead.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=djPwZuKbvrmoSptmQGftbxnFBiXhu4eAY3uKLEqMxtQ=; b=SuHNZlWkOSit+aXdiJBAjU9pqDZSz8W2k9lNCH5ObgPXSu2H5BWatQg/ru0Vq4o7Xd DHhh9p7+0BA6CgwUIWu9ruUo6ymf+KyioNz3IzvIsAe0sHHTmrA7THgbJLG90rCO1d/C S4LMVULsszraWt0qFV716+F00CSkJolmIGHjfIOUnKgm6ESC8KdMdvSSmgixqsyhXAOB t0IUF9XztWZtfh1gUwZ5F/MaTdxrvJFT0EUAv6aWA03thR4RlKgG82JuWnp1p0L7FLuO oC0IdOaxWmPw/qYKamalycUGWxRqTyVAeq+KAYnRBUvgPpMOwOluN7lV19Pdsq+7zG14 X39Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718602946; x=1719207746; h=in-reply-to: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=djPwZuKbvrmoSptmQGftbxnFBiXhu4eAY3uKLEqMxtQ=; b=JksrSDo1DDnlR2CtWUG07Pig5tXPyvvCkS0kspjP5if0splBbattimY2rzM+ttHWxr ma/YUlHU34+enmbQfFO/8IODF7LDIUi4f6Wqyr+Ty7ezokSxo7QsjUQLwaIFLU0AlVuE OjF9NYGvWmnatdzhE6t9rdSNGUv3LAdmEwIkoRHpsTPHBe7IyiDMGA3DcoIvenjcvW2m F/PKa9GSg9YAe/nCgsCbN5PjYiuqrby/0M1pZ43q8UW+QjdlPct9eaqKilDbBqalhKsQ SexDv+95SWDtHtnA8e1M8pKPwAdjUQmhf0HJ3ZbBr/dqccm66Y9yDD7vCWOQ9o59Z09D pxtQ== X-Forwarded-Encrypted: i=1; AJvYcCWwYpn+hD9+mblv7EmDKx8iIAeXzQBbInddgk0PaSW1VPPIDEqFtOgzk+NYlUISh97fzr4VewYNzu7RlShiO65vHTw8w/RTOXKAQtV+3v0/l7gXP3A= X-Gm-Message-State: AOJu0YwGalQbT4wLqn0EGvz6VPKaHSHLW5b8v2ovmCyV+QyyssDqw1yj cyKBAphj9e6Co+9DD380y5eAtvS43eLsr3a+XgqqI9zvXnGxdu+z X-Google-Smtp-Source: AGHT+IFs1jHgkUIF6bJM9amNqYLJX4wu5fNNYuW2fgfw3dZuTeC2J1Hh8+avNa0MuwtF0uJIpye+nw== X-Received: by 2002:a05:622a:5b0a:b0:444:8e78:6d25 with SMTP id d75a77b69052e-4448e786de1mr26277721cf.65.1718602946046; Sun, 16 Jun 2024 22:42:26 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-441f30f1d2esm43347471cf.81.2024.06.16.22.42.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 22:42:25 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfauth.nyi.internal (Postfix) with ESMTP id A58321200043; Mon, 17 Jun 2024 01:42:24 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 17 Jun 2024 01:42:24 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedvgedgkedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpedtteeuudejhfekffeiudeuvddvheduteejudeiieffhedvhffhffevtefg tdevtdenucffohhmrghinheptghrrghtvghsrdhiohdpghhithhhuhgsrdgtohhmnecuve hluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdo mhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejke ehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgr mhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 17 Jun 2024 01:42:23 -0400 (EDT) Date: Sun, 16 Jun 2024 22:42:19 -0700 From: Boqun Feng To: Gary Guo Cc: John Hubbard , Miguel Ojeda , 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 , Benno Lossin , 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: <79239550-dd6e-4738-acea-e7df50176487@nvidia.com> <20240616155145.54371240.gary@garyguo.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240616_224228_483545_2DD5FE9E X-CRM114-Status: GOOD ( 35.32 ) 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 Sun, Jun 16, 2024 at 10:36:13PM -0700, Boqun Feng wrote: > On Sun, Jun 16, 2024 at 08:06:05AM -0700, Boqun Feng wrote: > [...] > > > > > > Note that crossbeam's AtomicCell is also generic, and crossbeam is used > > > by tons of crates. As Miguel mentioned, I think it's very likely that in > > > the future we want be able to do atomics on new types (e.g. for > > > seqlocks perhaps). We probably don't need the non-lock-free fallback of > > > > Good, another design bit, thank you! > > > > What's our overall idea on sub-word types, like Atomic and > > Atomic, do we plan to say no to them, or they could have a limited > > APIs? IIUC, some operations on them are relatively sub-optimal on some > > architectures, supporting the same set of API as i32 and i64 is probably > > a bad idea. > > > > Another thing in my mind is making `Atomic` > > > > pub struct Atomic { ... } > > > > so that `Atomic` will always be `Sync`, because quite frankly, an > > atomic type that cannot `Sync` is pointless. > > > > Also, how do we avoid this issue [1] in kernel? > > `atomic_load()` in C is implemented as READ_ONCE() and it's, at most > time, a volatile read, so the eventual code is: > > let a: (u8, u16) = (1, 2); > let b = unsafe { core::ptr::read_volatile::(&a as *const _ as *const i32) }; > ^^^^ this line should really be: let b: (u8, u16) = unsafe { transmute_copy(&read_volatile::(&a as *const _ as *const i32)) }; but you get the idea. Regards, Boqun > I know we probably ignore data race here and treat `read_volatile` as a > dependency read per LKMM [2]. But this is an using of uninitialized > data, so it's a bit different. > > We can do what https://crates.io/crates/atomic does: > > pub struct Atomic { ... } > > , where `NoUinit` means no internal padding bytes, but it loses the > ability to put a > > #[repr(u32)] > pub enum Foo { .. } > > into `Atomic`, right? Which is probably a case you want to support? > > Regards, > Boqun > > [1]: https://github.com/crossbeam-rs/crossbeam/issues/748#issuecomment-1133926617 > [2]: tools/memory-model/Documentation/access-marking.txt > > > Regards, > > Boqun > > > > > crossbeam's AtomicCell, but the lock-free subset with newtype support > > > is desirable. > > > > > > People in general don't use the `atomic` crate because it provides no > > > additional feature compared to the standard library. But it doesn't > > > really mean that the standard library's atomic design is good. > > > > > > People decided to use AtomicT and NonZeroT instead of Atomic or > > > NonZero long time ago, but many now thinks the decision was bad. > > > Introduction of NonZero is a good example of it. NonZeroT are now > > > all type aliases of NonZero. > > > > > > I also don't see any downside in using generics. We can provide type > > > aliases so people can use `AtomicI32` and `AtomicI64` when they want > > > their code to be compatible with userspace Rust can still do so. > > > > > > `Atomic` is also just aesthetically better than `AtomicI32` IMO. > > > When all other types look like `NonZero`, `Wrapping`, I don't > > > think we should have `AtomicI32` just because "it's done this way in > > > Rust std". Our alloc already deviates a lot from Rust std. > > > > > > Best, > > > Gary