From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) (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 C7AFA167DB7; Fri, 11 Jul 2025 14:39:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752244762; cv=none; b=jyRsigsSxmVKuAkOir6jvM9To4I4MA0tZuzMRP5OsS/hOMmdYkGJ5Ki9v7DnZr6uG27yoPo/WMtSo03+rmxukYJCAWDJhc/zi+Ez8V5nzwqSoW3MpFZ5pull2fUjlhTj47Xlu3kmWj55k/Na98Spe/AewZvo90mouH7sYlYf9hk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752244762; c=relaxed/simple; bh=M7+e+e1rmKuD/oko475gmT7iuj8HXmG3rZODkspkF08=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QV4soe47FAb6eda7GGSvx/BOLPoyqj7XAXAPVzo3TGjN6FuINRqrBVoKPdwtNqsYn/qwEr3hMiDlpTRkHRFox6/UU5M79aHvER2SKT1mviGTyVQ6OcGCrk5PRgtlnLO3uD8VcZxzynsk/QMa+CGn08SiYRvzDpkbfzK9E/tR4Ew= 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=Z2u4UAgP; arc=none smtp.client-ip=209.85.160.174 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="Z2u4UAgP" Received: by mail-qt1-f174.google.com with SMTP id d75a77b69052e-4a9bf46adedso22186791cf.2; Fri, 11 Jul 2025 07:39:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752244760; x=1752849560; 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=FafGssQJy1j7DrCNrsdYFCExWgvC1bwETw0T/9WZrW4=; b=Z2u4UAgP/uDzJawE/yMHCQnN+2fGVV5DXnLkrXWPBCKFKjkuXyoPuhe/T/6nX5MM65 MBYK2C9hcDbnxwu1LN717KBqHhzYkfIUs/Z9ZEzxlaSGrnRHSiJKlgLV0tT8bCSEvGZE o0aJBa3J9zlZiq8g9v1Wza4f0raAyYnSY5GzBNHQTO/h+1Ou09/F040juWYgeKG8N6Il eHyQuhw67S1IDlzC+DtzPH15QjWVP5dfTf1Zuqjp1DXpZl/5PW4AMs78jvJVZxIEzYgy 7C1H2nTApGazc0cBnf88t6/hWk6Oiq4jH92Q5eE+6K1X9R6iYI3eOAdaFCcUj+UA2RMc 1pdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752244760; x=1752849560; 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=FafGssQJy1j7DrCNrsdYFCExWgvC1bwETw0T/9WZrW4=; b=c0gmgBbD/0l6SGrf89jGH6ahOU8a6Rt4rtVzu69Wz58O1TMnIkvZi0jbloSYs8r87V biu5Kt0RCQzIEwm9C96VCE+Lb5yccaEbO3Csx4wHMOq6jT+EIXhCUuun88vS1s7DzfQl 3TXPDL+0imBmgqPgwk4rXJwJRDigkLlIdyi3+6RSWCneEPao7ujJdqYBTGiHJog6HQRz Y8Riy1qlTYiDKQsMZm4g17rOdVFfUHzEH2soVdV6cl9JAE3xj1PYo97TObWpeEPtE640 LXS2Ovbu7wvDiYsXLhUxrHomipIOUezMO/YzhwvWbh9uKVUFj9ym1dWBqQbRrdNb3Ygu CMuw== X-Forwarded-Encrypted: i=1; AJvYcCUjiMKr/ksvn846ALNp7D3rtO5aYULxx+SPYWe8elkjm2W0bG3BNv4zKwfhEocTbjzFHm0i3+rmkflA3mURtOE=@vger.kernel.org, AJvYcCVSVnJeYJfmHWKHa3x3ibiOrVhGDa5YK0I+Z7H3YUajiwwCRrT0g5u3GSfzZqtdjc9I7Dz/cMqlFFCp@vger.kernel.org X-Gm-Message-State: AOJu0YxVIWOlPguokHti1lYg0uzsCrVGnvgKyZhPjOuGxEH0X8aa6bT4 xwHGtQm/ZUeSokj8/XyXrE2vqwA4GelUryHSRbvAMi6FN3N+7ipSZs+D X-Gm-Gg: ASbGncsvBsQi63xeio8xqLxBp96M3uch1r719sfNtWHK+XLmO30FVbf1SNiYO1krbav QpkG3sz+Fs2ub43vE9nR12n38GHzZxjan8SGTlAjPbue1QIiTn8IiZYZ2XU5C1Ur2MKs5OccB4N 5zSwjT5DGmPv5XNovmmggi/cjnFLfd38t2IG3Em/KPJlgn/9XVd1KbpRGRW6sML0p8yeeMQofYQ 7TbAv8bROaKIkQYjHN2/JynD/9y3V7dCxD66+yqpEQKdF6WlNCKfdWBxix9I62p9nWE3UEuEm2E pY/iWIA/xvF7lSpHOc4cLWtPHD34vPDYQJCC538rRyzPISXACjdO2mcjm+ukvm9gnHUzBXPyBU6 WB01qANPiQW7XCaxltq7yVqyK61nx6q97XODTffMaKFAvxpTkyhTACpGtpX+XBHRAOKMN1UzrFN EZDOYfOfn+KyrA X-Google-Smtp-Source: AGHT+IG7i69rdabJU4rCgv47216T/BQiZX74rLkmqajWAIdu3sM+sbPVU9v9PkHjIiFax5nwv660QQ== X-Received: by 2002:ac8:7d0a:0:b0:4a7:14c3:7405 with SMTP id d75a77b69052e-4aa35e677c6mr57233671cf.27.1752244759405; Fri, 11 Jul 2025 07:39:19 -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 d75a77b69052e-4a9ede76d65sm22250241cf.41.2025.07.11.07.39.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Jul 2025 07:39:18 -0700 (PDT) Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfauth.phl.internal (Postfix) with ESMTP id DD947F4006A; Fri, 11 Jul 2025 10:39:17 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-06.internal (MEProxy); Fri, 11 Jul 2025 10:39:17 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdegfeehkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnhephedugfduffffteeutddvheeuveelvdfhleelieevtdeguefhgeeuveeiudffiedv necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqh hunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddu jeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvg drnhgrmhgvpdhnsggprhgtphhtthhopedvjedpmhhouggvpehsmhhtphhouhhtpdhrtghp thhtoheplhhoshhsihhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqd hkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehruhhsthdq fhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlkh hmmheslhhishhtshdrlhhinhhugidruggvvhdprhgtphhtthhopehlihhnuhigqdgrrhgt hhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehojhgvuggrsehkvghrnh gvlhdrohhrghdprhgtphhtthhopegrlhgvgidrghgrhihnohhrsehgmhgrihhlrdgtohhm pdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpdhrtghpthhtohepsghjoh hrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 11 Jul 2025 10:39:17 -0400 (EDT) Date: Fri, 11 Jul 2025 07:39:15 -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 v6 6/9] rust: sync: atomic: Add the framework of arithmetic operations Message-ID: References: <20250710060052.11955-1-boqun.feng@gmail.com> <20250710060052.11955-7-boqun.feng@gmail.com> 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 Fri, Jul 11, 2025 at 10:53:45AM +0200, Benno Lossin wrote: > On Thu Jul 10, 2025 at 8:00 AM CEST, Boqun Feng wrote: > > One important set of atomic operations is the arithmetic operations, > > i.e. add(), sub(), fetch_add(), add_return(), etc. However it may not > > make senses for all the types that `AllowAtomic` to have arithmetic > > operations, for example a `Foo(u32)` may not have a reasonable add() or > > sub(), plus subword types (`u8` and `u16`) currently don't have > > atomic arithmetic operations even on C side and might not have them in > > the future in Rust (because they are usually suboptimal on a few > > architecures). Therefore add a subtrait of `AllowAtomic` describing > > which types have and can do atomic arithemtic operations. > > > > Trait `AllowAtomicArithmetic` has an associate type `Delta` instead of > > using `AllowAllowAtomic::Repr` because, a `Bar(u32)` (whose `Repr` is > > `i32`) may not wants an `add(&self, i32)`, but an `add(&self, u32)`. > > > > Only add() and fetch_add() are added. The rest will be added in the > > future. > > > > Reviewed-by: Alice Ryhl > > Signed-off-by: Boqun Feng > > --- > > rust/kernel/sync/atomic.rs | 18 +++++ > > rust/kernel/sync/atomic/generic.rs | 108 +++++++++++++++++++++++++++++ > > 2 files changed, 126 insertions(+) > > I think it's better to name this trait `AtomicAdd` and make it generic: > > pub unsafe trait AtomicAdd: AllowAtomic { > fn rhs_into_repr(rhs: Rhs) -> Self::Repr; > } > > `sub` and `fetch_sub` can be added using a similar trait. > Seems a good idea, I will see what I can do. Thanks! > The generic allows you to implement it multiple times with different > meanings, for example: > > pub struct Nanos(u64); > pub struct Micros(u64); > pub struct Millis(u64); > > impl AllowAtomic for Nanos { > type Repr = i64; > } > > impl AtomicAdd for Nanos { > fn rhs_into_repr(rhs: Millis) -> i64 { > transmute(rhs.0 * 1000_000) We probably want to use `as` in real code? > } > } > > impl AtomicAdd for Nanos { > fn rhs_into_repr(rhs: Micros) -> i64 { > transmute(rhs.0 * 1000) > } > } > > impl AtomicAdd for Nanos { > fn rhs_into_repr(rhs: Nanos) -> i64 { > transmute(rhs.0) > } > } > > For the safety requirement on the `AtomicAdd` trait, we might just > require bi-directional transmutability... Or can you imagine a case > where that is not guaranteed, but a weaker form is? I have a case that I don't think it's that useful, but it's similar to the `Micros` and `Millis` above, an `Even` where `Even` is a `i32` but it's always an even number ;-) So transmute>() is not always sound. Maybe we could add a "TODO" in the safety section of `AtomicAdd`, and revisit this later? Like: /// (in # Safety) /// TODO: The safety requirement may be tightened to bi-directional /// transmutability. And maybe also add the `Even` example there? Thoughts? Regards, Boqun > > --- > Cheers, > Benno