From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) (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 B55E61E0E14; Wed, 16 Jul 2025 17:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752687498; cv=none; b=rn/vWNQZA0I1ejV78i1Ssbzgit9JjZYOYahxH8Gdcy9V1aKdXhqpyNpApOI3yCNRnlU4fTx/ibNRqWoiS/TKDVRirbRUvY3avPtBUsjnxXUZtNLL1kcZk5zm95dttxxIC0CQJmt+cXF9a1Fhaz6SFqptFb2UaWFFqq3U9xlhSXU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752687498; c=relaxed/simple; bh=hwFt5JgyJnM6fRddKezbFqDWwSiXWnG7mQ3pfMQpna0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=HzJiwWFPHw+pKdXVQ1cL/tKvG565XxjFknQ2aR3vRV0b94VedXTGfphZUn1Bfw2s7GZt/iRahfVknRq86KWfZT8JM7fQ9kPGJMgx982O9gl5HneH9hnuU0x52cSymWQGgBRSUnN5kYi27IAwnm9JtQVdtHGdXp5H/rGDEpFUtwc= 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=a1e29Vue; arc=none smtp.client-ip=209.85.219.43 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="a1e29Vue" Received: by mail-qv1-f43.google.com with SMTP id 6a1803df08f44-6facacf521eso1774756d6.3; Wed, 16 Jul 2025 10:38:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752687496; x=1753292296; darn=vger.kernel.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=R0w70VSXxhIR5wvQCxbTzFCPPCvDaebArjr1LK5bYl4=; b=a1e29VueSMUdS3RDpX8ew8Y64mTN/l0V55HqQt7+5J8tPrmnDcWUtVZyNOKibJ/tf3 TlZadntFo11gkhB6FLVJndPukHZcGmdQCu9yvCOZ3ZrA0Qbq6wnHuGsXmpBDFI4Nr7FD oj6CSos0rR2eeOYsAr7zOFHheL/5B+MAHpVaOlfOGaw2tjt5IWUBDaflFRA2NynaHjWy 61e5M47yS2T2mCrDiQzP4l4wV42BA76CKEDoB1M2EGODHYsxoDf/W7thvAxBKSMMt29r iQdMRmuE4+CyjjszLRtrTrBRMkfct2QkhBkc6Als7q4Dv/AaUFI64dBcuZoDDZI9xYOz RHNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752687496; x=1753292296; 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=R0w70VSXxhIR5wvQCxbTzFCPPCvDaebArjr1LK5bYl4=; b=cJyrTlnWnP8EemcXz6gcS03H3HePH2iJWVrtFS/SyE8oAMjSNMgr7ZXPyVnsYaa7VO Vv1+DsAXiCnoar4sW4udxFfuICS5fg/8PnhxUokD0kig1KOCYCoBCdLlUtcDb5tmJUPS P9sK+p08t86UBWFvPKWJpWOgoZsfoe6tg7snqkMkeclDdtXSNCNkXrl6k3E/wiMBI940 x20n40tSxKWHCJ7i737FI7KjXA7vyxNKepB2JuQOMsXz+U5rD2TITBxH1766pOUc1zeA 0vZhhznoSoy01GS1ENQJxGPB2xu1moyIZZsLZ76XHT+84qaeOFEZBMCZ++/Ys3+KNPwA n2Zw== X-Forwarded-Encrypted: i=1; AJvYcCVixAAky9fgK6vGr38SmjoPXk8Bzjgv/PvS9WpxRU4vlZHIXwLoBTw4lPTBBaktS2n7+m+DbeIffGGu@vger.kernel.org, AJvYcCXvFy0FmTHUcmQQhx7qu7JGbFbXAmfAgkRoYsrtp0QZTLHVqdUqvwcaUeFub5n9ZJZnpjatT5MijWtNm91xIyI=@vger.kernel.org X-Gm-Message-State: AOJu0YxDTk/czvaoiBeeTqUOmP4TBYNB4E5o+A7G2qZ7pfA9DiVa3R1k 0tneShDlPwBNpX8S+hULlzHtIQ3LS6iFqr/S5STpY5riNflWKYwczcEJ X-Gm-Gg: ASbGncsxlKXzz+TB/4eiHRopwuKzQLaxA9xFgaiNIheRPbT4LhYn5BZRtlK905hToOX 9R+XGVVB14LBA7a4chBo+d4ikyIMJwosVUthvPs+1GmUT/Qltx7G3EFb2JZufJU3J4WphtCBsSl gNzz9IwZtsMzd93bv2ekc4FSnlFFZGPX02QZhh+bf7bPlFTu5NucgXzzOircMIPcARaZFfakjNH 7aIqwcDGKtoacCcxqXrdcOniOVT81YgcvMs/XRaNBWkxpZST3MKuZXZwH9uAVo8IccS22FzXE6K 0ckMGR6lpuQUi1fdaYge8xOri3+6ohy+BQ1Dm/zAEeX3sr2e2a5guLoA/qVryR1JBvwDkduo6y1 Bc/YfGbT4UJZ4JYsAwwkPlBtew9fhjUS7y/DjUKiZQZXL5ADGXJuZK7iBrPAF6Q1wfv45Gp1sit XNeWNbZDa+T50zdEweaMIQ4mw= X-Google-Smtp-Source: AGHT+IHXXkjwOxGahH8fFj1RMMHTFy2y+TzXu8ch0VEcl1er1f6Qb+4zYOHN9wmf6UbbwIebNk4j+A== X-Received: by 2002:a05:6214:440f:b0:702:c4d8:ec02 with SMTP id 6a1803df08f44-704f4aed57amr60900036d6.40.1752687495480; Wed, 16 Jul 2025 10:38:15 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-70497d94538sm72665236d6.99.2025.07.16.10.38.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Jul 2025 10:38:15 -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 4AC26F40068; Wed, 16 Jul 2025 13:38:14 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Wed, 16 Jul 2025 13:38:14 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdehkeefvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtuggjsehttdortddttddvnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnhepjeffgeeijedvtdfgkeekhfejgeejveeuudfgheeftdekffejtdelieeuhfdvfeeg necuffhomhgrihhnpehkvghrnhgvlhdrohhrghenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghr shhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvg hngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohep vdejpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehlohhsshhinheskhgvrhhnvg hlrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhn vghlrdhorhhgpdhrtghpthhtoheprhhushhtqdhfohhrqdhlihhnuhigsehvghgvrhdrkh gvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhkmhhmsehlihhsthhsrdhlihhnuhigrdgu vghvpdhrtghpthhtoheplhhinhhugidqrghrtghhsehvghgvrhdrkhgvrhhnvghlrdhorh hgpdhrtghpthhtohepohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprghl vgigrdhgrgihnhhorhesghhmrghilhdrtghomhdprhgtphhtthhopehgrghrhiesghgrrh ihghhuohdrnhgvthdprhgtphhtthhopegsjhhorhhnfegpghhhsehprhhothhonhhmrghi lhdrtghomh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 16 Jul 2025 13:38:13 -0400 (EDT) Date: Wed, 16 Jul 2025 10:38:12 -0700 From: Boqun Feng To: Benno Lossin Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, lkmm@lists.linux.dev, linux-arch@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Gary Guo , =?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 , Alan Stern Subject: Re: [PATCH v7 6/9] rust: sync: atomic: Add the framework of arithmetic operations Message-ID: References: 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=us-ascii Content-Disposition: inline In-Reply-To: On Wed, Jul 16, 2025 at 07:16:02PM +0200, Benno Lossin wrote: > On Wed Jul 16, 2025 at 5:48 PM CEST, Boqun Feng wrote: > > On Wed, Jul 16, 2025 at 05:36:05PM +0200, Benno Lossin wrote: > > [..] > >> > > >> > I have a better solution: > >> > > >> > in ops.rs > >> > > >> > pub struct AtomicRepr(UnsafeCell) > >> > > >> > impl AtomicArithmeticOps for i32 { > >> > // a *safe* function > >> > fn atomic_add(a: &AtomicRepr, v: i32) { > >> > ... > >> > } > >> > } > >> > > >> > in generic.rs > >> > > >> > pub struct Atomic(AtoimcRepr); > >> > > >> > impl Atomic { > >> > fn add(&self, v: .., ...) { > >> > T::Repr::atomic_add(&self.0, ...); > >> > } > >> > } > >> > > >> > see: > >> > > >> > https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/log/?h=rust-atomic-impl > >> > >> Hmm what does the additional indirection give you? > >> > > > > What additional indirection you mean? You cannot make atomic_add() safe > > with only `UnsafeCell`. > > What is the advantage of making it safe? It just moves the safety Well, first we in general are in favor of safe functions when we can make it safe, right? Second, at Atomic level, the major unsafe stuff comes from the T <-> T::Repr transmutable and making `Atomic` a valid `T`, the safety of i{32, 64}::atomic_add() would be a bit distraction when implementing Atomic::add(). With i{32, 64}::atomic_add() being safe, I can implementation Atomic::add() as: impl Atomic { #[inline(always)] pub fn add(&self, v: Rhs, _: ordering::Relaxed) where T: AtomicAdd, { let v = T::rhs_into_delta(v); // INVARIANT: `self.0` is a valid `T` due to safety requirement of `AtomicAdd`. T::Repr::atomic_add(&self.0, v); } } then all the safety related comments will be focused on why `self.0` is still a valid `T` after the operation (if you want to be extra detailed about it, it's fine, and can be done easily) > comments into `ops.rs` which makes it harder to read due to the macros. Does it? Add `i32` and `i64` level, you only need the pointer to be a valid `* mut i{32, 64}`. So the following is pretty clear to me. /// Atomic arithmetic operations pub trait AtomicArithmeticOps { /// Atomic add (wrapping). /// /// Atomically updates `*a` to `(*a).wrapping_add(v)`. fn add[](a: &AtomicRepr, v: Self::Delta) { // SAFETY: `a.as_ptr()` is valid and properly aligned. bindings::#call(v, a.as_ptr().cast()) } } it is at least better than: $( $(#[$($p_attr)*])* $pvis unsafe fn $p_field( self, slot: *mut $p_type, init: impl $crate::PinInit<$p_type, E>, ) -> ::core::result::Result<(), E> { // SAFETY: TODO. unsafe { $crate::PinInit::__pinned_init(init, slot) } } )* ;-) Regards, Boqun > > --- > Cheers, > Benno