From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f48.google.com (mail-qv1-f48.google.com [209.85.219.48]) (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 8CEEE1DF247; Fri, 4 Jul 2025 23:21:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751671295; cv=none; b=S/voCeWPX/p6j8ld5Docxqj+3X1k8VR2nW6XJOeei5CWlxlmDNE0rFSMiwOaz9LS0ZwiZSBII/S74cQtEtbR2WhRs0gq2KYPe4wEshYaNvQSrPXXLTseJEQu1btwgqt00T7DiooYqcbA0HiKtTe+afyBDDC3UlmlMfh+i7M+T/c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751671295; c=relaxed/simple; bh=jE1b/T8vu4ANmTDJ4ST4tad0xZbO91vThM2oPCRjy+Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MGHlFKkuxXuGx+11R9qSAnKhHeGD68epP+uqRmifcDjLYWL9rya8I3DiC+otBmtTQxqSoxfz86v9elSZuwUMk/jW5HX3yJsJJOWrTSbvqVQis0Bur5Nizg6ziy5yJnxNa4mMRE4a3RkN8K01EwozPO2b4dcmlM+quNNfyNJVFoY= 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=hDd75BUe; arc=none smtp.client-ip=209.85.219.48 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="hDd75BUe" Received: by mail-qv1-f48.google.com with SMTP id 6a1803df08f44-6ff16e97d1eso34560066d6.1; Fri, 04 Jul 2025 16:21:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1751671292; x=1752276092; 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=xZOJHZdr8mH7+1fX6/tNc+dr4/GCSPI71PoHd4DmHIg=; b=hDd75BUeP+LRCluXbd6YtB2eUHgGXI8QrbLq3m6vUQMQRu47UfNs5OClx52Koi28CK epIt6Y5r8FC51z19f/gSci5tkdcI4BB0Xm4bnMthBi696YMCN/y5D+Z2NdcZI/2Kk+52 wH+rCukt+lfuEB5IwBC+lA7VPXE3fqqfy7SR0xPbGyfo8nw7eER0OVwW/pSViHuENOH4 LisBoD2n8UENINnEccPDWKkaDzovUmBL6t1FLs9+BBc2ckDsezUE8W2EDOCW8UWNQfZ5 v4+X0XvL+nDOX0dszUT3olxeTZCXuAkpB3gLFtjsx5eBdLNHxtG3gg4S+37VcBz8uZBO UkrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751671292; x=1752276092; 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=xZOJHZdr8mH7+1fX6/tNc+dr4/GCSPI71PoHd4DmHIg=; b=nOdw5y5JB4A8iQStpg7B99FfVqmFUTAiPVW7cZ/rpDiFU/UQjpKZyPtB4JderLGv8o qX5OyPKUdQjvo5OelcDo5AJM0STnRW422ZnV36qHDf/Jy6p6i6QXDGJ2bGNoTki4yqCT rNSaj3jmfBFz87TY0PDtNJWeR2bi5bbO/cUvteOwW3mtIDBbR3td8aDyiyRkfBsNujQi fohvSqrckIgMuVyHawPpxtiGBnOK3BELmBaFxfELnmUxkSebEcvWSyyFtLnXCZey4I4T RNKxvXMhm3rwPhIKcj4A2+Yf+NIERkHalmGjoKuifiJ6emeLB8dGSV4uTLxZZY77ruyh XxVw== X-Forwarded-Encrypted: i=1; AJvYcCUkG55Z2+IEghH7s4LfeJAt4LMciLlrqqmCBcLyEDGDS043pUD9z4rTQiEXYLWnD1ylmmFxDWmPvly4qN2ga+o=@vger.kernel.org, AJvYcCXkXc9zbbB7CSrA8KsZH/kH6QRQXMxO6MLRjbJsik3+GwBBHV3IcVa8ULZbucTgqG25SD0SQ5GCa8IX@vger.kernel.org, AJvYcCXoQLH0JqJNkh2zL6ZFMoJAjGjskkZPuf5a4Q1ZnjRuImcshIRkGENtlZKapES+okuftpdOuMz3dh1rbfBR@vger.kernel.org X-Gm-Message-State: AOJu0Yw7eheTdqahZWn78+DmMoPdXnLq5Q4IwPX6Eo05edJapDjMtHE3 BCMv4qhD5LWqjfZSx47dCGPTJYALaXXmcWwT8cq3eYEG0dRUFEf9SoS2 X-Gm-Gg: ASbGncshOMpLAKgemQp8Eu+SP4nUA0m2/PxW8lyoDtJpRZRCByBfl1JAgvWCq5r0vSu o1rooSQkzyJrsoCZ8GFKPdg4uLPbrL+G/HK0fDYF2T7sxbp8LacT4lUvtwfZE9leCeMwLdc+dxG Pvet//YPlrtRMUTTrf/nru9XG0lVHYTvLQkoiZOyGKUen5IopUiHWnHyCPIIiExlo6aA6UDbXUB k6l+6ltja/Oo1gtn3ksPr/7zB6PAM4JtlcbqshTNSoxFHPsrPXdo8WvZrtyymhEVjapyrw7RhKE fC/8GmHBAYAgxUfqFdLvX29fGmwb35vHRzuULdievhuQmhlUZJuwUEJXOOa6f/dY021od477Z05 2481KmvzkGNeNAKnmj4H8vqWTghawPYv4EGBII5tzlwM0Xv3JQD1n X-Google-Smtp-Source: AGHT+IHQUE1vxUwY6a29kM6aQ4jL6Q5bbMamdAxATn/vcvaCIRvkmQPDq+TxxT8m0tk8xiw0vQII+Q== X-Received: by 2002:a05:6214:5d05:b0:702:bb67:d4f0 with SMTP id 6a1803df08f44-702c574a5a8mr70500076d6.10.1751671292362; Fri, 04 Jul 2025 16:21:32 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-702c4d5ab9csm20191366d6.87.2025.07.04.16.21.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Jul 2025 16:21:31 -0700 (PDT) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id 41BAEF40066; Fri, 4 Jul 2025 19:21:31 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Fri, 04 Jul 2025 19:21:31 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgddvgeegiecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtugfgjgesthekredttddtudenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpedtgeehleevffdujeffgedvlefghffhleekieeifeegveetjedvgeevueffieeh hfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepvdeipdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehlohhsshhinheskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepghgrrhihse hgrghrhihguhhordhnvghtpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgv rhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprhhushhtqdhfohhrqdhlihhnuhigse hvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhkmhhmsehlihhsthhsrdhl ihhnuhigrdguvghvpdhrtghpthhtoheplhhinhhugidqrghrtghhsehvghgvrhdrkhgvrh hnvghlrdhorhhgpdhrtghpthhtohepohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghp thhtoheprghlvgigrdhgrgihnhhorhesghhmrghilhdrtghomhdprhgtphhtthhopegsjh horhhnfegpghhhsehprhhothhonhhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 4 Jul 2025 19:21:30 -0400 (EDT) Date: Fri, 4 Jul 2025 16:21:29 -0700 From: Boqun Feng To: Benno Lossin Cc: Gary Guo , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, lkmm@lists.linux.dev, linux-arch@vger.kernel.org, Miguel Ojeda , Alex Gaynor , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Will Deacon , Peter Zijlstra , Mark Rutland , Wedson Almeida Filho , Viresh Kumar , Lyude Paul , Ingo Molnar , Mitchell Levy , "Paul E. McKenney" , Greg Kroah-Hartman , Linus Torvalds , Thomas Gleixner Subject: Re: [PATCH v5 04/10] rust: sync: atomic: Add generic atomics Message-ID: References: <20250623193019.6c425467.gary@garyguo.net> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Sat, Jul 05, 2025 at 12:49:09AM +0200, Benno Lossin wrote: > On Sat Jul 5, 2025 at 12:30 AM CEST, Boqun Feng wrote: > > On Sat, Jul 05, 2025 at 12:05:48AM +0200, Benno Lossin wrote: > > [..] > >> >> > >> >> I don't think there is a big difference between `Opaque` and > >> >> `Opaque` if we have the transmute equivalence between the two. > >> >> From a safety perspective, you don't gain or lose anything by using the > >> >> first over the second one. They both require the invariant that they are > >> >> valid (as `Opaque` removes that... we should really be using > >> >> `UnsafeCell` here instead... why aren't we doing that?). > >> >> > >> > > >> > I need the `UnsafePinned`-like behavior of `Atomic<*mut T>` to support > >> > Rcu, and I will replace it with `UnsafePinned`, once that's is > >> > available. > >> > >> Can you expand on this? What do you mean by "`UnsafePinned`-like > >> behavior"? And what does `Rcu` have to do with atomics? > >> > > > > `Rcu` is an RCU protected (atomic) pointer, the its definition is > > > > pub struct Rcu(Atomic<*mut T>); > > > > I need Pin<&mut Rcu> and &Rcu able to co-exist: an updater will > > have the access to Pin<&mut Rcu>, and all the readers will have the > > access to &Rcu, for that I need `Atomic<*mut T>` to be > > `UnsafePinned`, because `Pin<&mut Rcu>` cannot imply noalias. > > Then `Rcu` should be > > pub struct Rcu(UnsafePinned>); > > And `Atomic` shouldn't wrap `UnsafePinned`. Because that prevents > `&mut Atomic` to be tagged with `noalias` and that should be fine. > You should only pay for what you need :) > Fair enough. Changing it to UnsafeCell then. > >> > Maybe that also means `UnsafePinned` make more sense? Because if `T` > >> > is a pointer, it's easy to prove the provenance is there. (Note a > >> > `&Atomic<*mut T>` may come from a `*mut *mut T`, may be a field in C > >> > struct) > >> > >> Also don't understand this. > >> > > > > One of the usage of the atomic is being able to communicate with C side, > > for example, if we have a struct foo: > > > > struct foo { > > struct bar *b; > > } > > > > and writer can do this at C side: > > > > struct foo *f = ...; > > struct bar *b = kcalloc(*b, ...); > > > > // init b; > > > > smp_store_release(&f->b, b); > > > > and a reader at Rust side can do: > > > > #[repr(transparent)] > > struct Bar(binding::bar); > > struct Foo(Opaque); > > > > fn get_bar(foo: &Foo) { > > let foo_ptr = foo.0.get(); > > > > let b: *mut *mut Bar = unsafe { &raw mut (*foo_ptr).b }.cast(); > > // SAFETY: C side accessing this pointer with atomics. > > let b = unsafe { Atomic::<*mut Bar>::from_ptr(b) }; > > > > // Acquire pairs with the Release from C side; > > let bar_ptr = b.load(Acquire); > > > > // accessing bar. > > } > > This is a nice example, might be a good idea to put this on > `Atomic::from_ptr`. > I have something similar in the doc comment of `Atomic::from_ptr()`, just not an `Atomic<*mut T>`. > > This is the case we must support if we want to write any non-trivial > > synchronization code communicate with C side. > > > > And in this case, it's generally easier to reason why we can convert a > > *mut *mut Bar to &UnsafePinned<*mut Bar>. > > What does that have to do with `UnsafePinned`? `UnsafeCell` should > suffice. > I was talking about things like UnsafeCell<*mut T> vs UnsafeCell not comparing between UnsafePinned and UnsafeCell. Regards, Boqun > Also where does the provenance interact with `UnsafePinned`? > > --- > Cheers, > Benno