From nobody Sat Feb 7 18:20:43 2026 Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (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 9C363423159 for ; Tue, 20 Jan 2026 11:52:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768909943; cv=none; b=J41q1NADzPQdRYKJqojheg6HT02mfgfVVtaAQAagC0M8uanMF0Ak9HcvspZ75L0bOnxlwFUQyz8/t2ZfZEQWG/jIt17vY9KdlZv5GGBJ7MIXeoebUa5o05y7fCjNUl5rFPQHaH28C1soyu8y3V18qJk6jYeJOXEQ5M5PLGs+4ls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768909943; c=relaxed/simple; bh=IgWAuNJ6h3E/R0Fu5awjtkTf/OgcqvY3PFwI4AxvJf8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hCiezOsUvgOcXMIeBY46CyC6+vjkKFH5gWLpBDfKiZZMziW+Wx24JDd0ei8/9faM9HwHaYBDtPEfpU6ShJUO8j9bQ94v3+KzRsek7oOls++AwenLa89gUg1QjS1ttWl2LEXMGsHVE5D/kUPKumOCOqbXwgURDS0OzUlDiyQs40Q= 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=gzCa3A/l; arc=none smtp.client-ip=209.85.219.49 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="gzCa3A/l" Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-88a288811a4so54561446d6.3 for ; Tue, 20 Jan 2026 03:52:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768909939; x=1769514739; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=noKL7zQXHKYfYinQlJHUIYY5IKQR6tHuuXlN5E414EI=; b=gzCa3A/lBb272Jq1hJmmpYkHojOD7e6a9++PdVPlcn53UAEzy6vZFxAU8i/M91CL2O ALLAJI9gy+fgUoUx2Poj1q5kYGJl3ckcTqlEHiQa0fyl9w5lXG41WQKui9exAujhGr7X Po3Brufq+Bjfi6sQxIJuZkyLSVM3AQ06oxfyjfj6iHo9HlKDigGaQkEKsSfMrXDzDli7 z9tX4AFkAGBXMFLloUfgow/OWYawQqsB/zQgec5X7hIPX9W4Sqa9CIwgDQu9duCqXxWk xVbVm13wGI9GeouH49YzzBIqwaliZL0da/59A7d6p8pzNpLJJ0VizVNWzCEXjdFtkA5W r8VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768909939; x=1769514739; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=noKL7zQXHKYfYinQlJHUIYY5IKQR6tHuuXlN5E414EI=; b=C1Hae8C/UoCaqEUdWzPTH0FqZ4VUdMBS5Wc9vuOc2K1ORf70eEP/XxJTVUg87wwqwb Tujg4Iv4/8uz//sE0IOekUsbsXoJWmFQqAtOpGbp6tHJj3KLAOiU08fqL8/0sZubB6eh jWnkpfjK3KNSUeoODwp37gkq+LDPGkYlfkd+sYgagIYinSsSzdZOgVSedSUK+MSNvZqk a6kq8RGX5iVpx0RgNkAkDzOXV6aYBx4qywmOcr3fecBgU+aNWCM3dDT6Ymxsys4wIYdU VneZPzB173KaiP0tNMH+WxFwGWIVNb8vbk/XZY3VGrfZz/R6H2p3BIebtKX8gHoCFb4x jYmw== X-Gm-Message-State: AOJu0Ywwa5VUpZLxuR6mg2d0cQKp0ED2EZiIa8aGpUii+eAdr5h79twI J4TbvVRixd9m9ozdQL0XlppaUHuqmyO9BcJAemv0cTvBlUkHgxx9gZzl X-Gm-Gg: AZuq6aJ8/oYV960Syraypd62t4brN+ZxDgnCaZ5L724N2b7xzBKx6xlaQWXIltJCSGR nuvT8uCVEeJUnEBqoSizkrC4ak5sz/ak01UTWHI7E6maDQLJ7l3gtDGhoS5qDwu6/60ynuZ3swR IIFqXASDC0bysHMSIzixzPy5avQU4ns/5WUIfeXWNdRphKZHKQN1FZ+dNYvi5EzkWoK5g11LSyZ ZMqwFMlR8fWcDa+Pjwqh2mWsTA0i6M4Sp7mauBdotpapV5HWKmuZJx+cd6M/UepqFWcuSe0E0eK qx+3bVt8omxq2qTTK2IMjDtNP2LdkcPkJSZ0SJfzGpxKFGmoy/WXwDXzGf7lMcjFHrgjBswyDEO 36j1lOlnadnQgTtByjoLIUHpkM75QaRmcJP51BqVzXb7o8UJ54vSwVH9biHjvgwTWxUUELs4BuF 0NTqjjf796zeJUkbkQH7dXmwDb+kIlQjIovc+A93cpAmGDMLHG7YTbBkgVwZuhvQ5kSiHamYV1w uCsb9IzMAOaslFPs2t2VQndEg== X-Received: by 2002:a0c:f6d0:0:b0:894:4793:efb5 with SMTP id 6a1803df08f44-89463837a6cmr13834676d6.4.1768909938912; Tue, 20 Jan 2026 03:52:18 -0800 (PST) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8942e6c9a9dsm107071836d6.43.2026.01.20.03.52.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jan 2026 03:52:18 -0800 (PST) Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfauth.phl.internal (Postfix) with ESMTP id 8EB16F40068; Tue, 20 Jan 2026 06:52:17 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Tue, 20 Jan 2026 06:52:17 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddugedtfeegucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueehgeeg gfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepvddtpdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopehlihhnuhigqdhfshguvghvvghlsehvghgvrhdrkhgvrhhnvghlrd horhhgpdhrtghpthhtohepkhgrshgrnhdquggvvhesghhoohhglhgvghhrohhuphhsrdgt ohhmpdhrtghpthhtohepfihilhhlsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehpvg htvghriiesihhnfhhrrgguvggrugdrohhrghdprhgtphhtthhopegsohhquhhnrdhfvghn ghesghhmrghilhdrtghomhdprhgtphhtthhopehmrghrkhdrrhhuthhlrghnugesrghrmh drtghomhdprhgtphhtthhopehgrghrhiesghgrrhihghhuohdrnhgvth X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 20 Jan 2026 06:52:16 -0500 (EST) From: Boqun Feng To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, kasan-dev@googlegroups.com Cc: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Gary Guo , Miguel Ojeda , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Elle Rhumsaa , "Paul E. McKenney" , Marco Elver , FUJITA Tomonori Subject: [PATCH 1/2] rust: sync: atomic: Remove bound `T: Sync` for `Atomci::from_ptr()` Date: Tue, 20 Jan 2026 19:52:06 +0800 Message-ID: <20260120115207.55318-2-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260120115207.55318-1-boqun.feng@gmail.com> References: <20260120115207.55318-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Originally, `Atomic::from_ptr()` requires `T` being a `Sync` because I thought having the ability to do `from_ptr()` meant multiplle `&Atomic`s shared by different threads, which was identical (or similar) to multiple `&T`s shared by different threads. Hence `T` was required to be `Sync`. However this is not true, since `&Atomic` is not the same at `&T`. Moreover, having this bound makes `Atomic::<*mut T>::from_ptr()` impossible, which is definitely not intended. Therefore remove the `T: Sync` bound. Fixes: 29c32c405e53 ("rust: sync: atomic: Add generic atomics") Signed-off-by: Boqun Feng Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo --- rust/kernel/sync/atomic.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 224bd57da1ab..d49ee45c6eb7 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -215,10 +215,7 @@ pub const fn new(v: T) -> Self { /// // no data race. /// unsafe { Atomic::from_ptr(foo_a_ptr) }.store(2, Release); /// ``` - pub unsafe fn from_ptr<'a>(ptr: *mut T) -> &'a Self - where - T: Sync, - { + pub unsafe fn from_ptr<'a>(ptr: *mut T) -> &'a Self { // CAST: `T` and `Atomic` have the same size, alignment and bit= validity. // SAFETY: Per function safety requirement, `ptr` is a valid point= er and the object will // live long enough. It's safe to return a `&Atomic` because fu= nction safety requirement --=20 2.51.0 From nobody Sat Feb 7 18:20:43 2026 Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) (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 3003F410D04 for ; Tue, 20 Jan 2026 11:52:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768909947; cv=none; b=qg3v/1zJdlADlhrYsxIBl4Dn5+gs0Rm3dN7hFutaKmKxZIcttYNVHIqRrPSTC/KhpX+/0kOTE+iUSztC7oPyr+ZXLvFYWMaEkuq3a/qAHXnNYW8fVCXKHTLzk3+HhYCTKvA29dm0J3SJj9JvDFx+bUpIbQJNRhE0lvJaxVaTTlI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768909947; c=relaxed/simple; bh=bvuVfpg7fANpqrGpBEK9Ta/Hkb+dwC9XZqzfpSHeTjg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jGqnMoTPgEdMUecIHMDckB6NjnheKHLqVrFupq/MRrnaEyvMQh7OLCA/Q/8nnyqam0VlkArlMZ0wWiZvfvwB3IgBWfnimw37c60q0cR1YIOa2LyWc/wjGmd6JpcXgJzPQ7XA6uxfcGtzn+Sotn3VMN9xugAXzojoYfiwJLTOz7w= 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=lZLQHPZa; arc=none smtp.client-ip=209.85.219.47 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="lZLQHPZa" Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-88a37cb5afdso87188606d6.0 for ; Tue, 20 Jan 2026 03:52:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768909942; x=1769514742; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=1M3E4UxKfMJ+dI/l+/VVbPQmnjuaMV2kWf95M7jFT9o=; b=lZLQHPZayZfdeaVhh8SLENMbqPIm+/SU6AdOOfukMavEYQtV4FMd04lvrhjCSYNVKn csbAZyhLzEuyTrHwMWgD2jkOc4ZgveGyR/u9jSAs+51B7Y6PccPucDk7sEILt8hhgpC+ 3QqSDNpnUygvigHDV8l3qm2cizUolEN9lCHIcQpR5vrMTjpSI6OgYRK2NTtEdpMb3NRZ KsSaKWgaxpsiSCn7WX677/reAjv4EXT9Q4RkizfEBSpNGkcM5T5nDERltYG/0xnS3rKh h1irv8vrr6RQ6kgYDSxXNjxnouaFAC2vndfrzosWr41VqJowz5uMaA+c+Y4yypFB0Q7j 0EHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768909942; x=1769514742; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1M3E4UxKfMJ+dI/l+/VVbPQmnjuaMV2kWf95M7jFT9o=; b=IG3oTW+Qwll+Mt1BDEN/rNmYENZ0ltAu/g1MuJzkDim2sQbFOTsbxnLr7maXl7Gz4+ L/tHDJhIHiNSlUfwmvRqAwVCCddHLXtF9f1YUQgJbvwq6xQq2/WfAkJ3Xpfv7SZDmJJS tdK0CuwIzk1eOcs7n7+/Om1uOuSq6Q1JSe9zW+4ZKQYIOAaVbnZeF71AFH44J0ULUiQ/ cHNkWvGreR0PxOLI28iv8xtSd8X+RVGHIGmrky+1zsCIefgC2kAJ4Zd632BSO/kYZxhG TNtmUfkJCOv37m0i6VCJB25CyuxC8Qwy6G0M00o/RZO8pyPuy6hX+XgzO4Ndk+NMf4K1 PA1w== X-Gm-Message-State: AOJu0YyTSXsgHjHErBhU6y60TqT1o8ZuUAuYL2Z7jMdorbqKtVeCYpWa UEeF+pRVGa0hcBhUaqi8pvsRYKfKG9sUur5TvdkEBbLz0PZDU0oBFQsy X-Gm-Gg: AZuq6aLuSnot6lmlGstkkIMVh34hQrF0bDdlkF8csT/H/7KdDOhK53PcuFXjoHewlaS 3RCtfaAAlB3tw7Wqg08n6aBtq0OZtxQLHcgOygUMaoCkD4pcpgUuJf5jR59r7CWLFR1ZXZViECf 2bmxa8TyTatMVZP3I8m2xcwSM6USv58dty/XPJe8KuXX/11Qs5u3XUS6WYPy7GBpyNbriiWg8RE bMUN1a8SwrgRQRxxcL4qNaHnnwoT9Ybxw0CuAUnb5TP4tcqWNyzVOFikV5a9sCtL4Anf3Svu/nC U5raelm5LOd/mmSIJtPD1LOy1HrBOLNz5arLbef5ecpPovkyh9YKAaJ8Q7hGA3SUEulh4CfkwkB O9Q6BDPSSpPyWx/BEdzEV7oIhpiLod13OFuxNIUOXAv31SUhxwyCyhpmvbyqU/PDyAxEXwpg06U 4qV5OoMQBQNo4OegEf7r0SUzJMkxIfqX//yKZPEcohbsrWwOY+LVdDTfoL4rqMSeo3IccfGIytx Mc5XoDi7B/FTiY= X-Received: by 2002:a05:6214:da6:b0:88a:375b:ed7c with SMTP id 6a1803df08f44-89398273c7cmr206650326d6.35.1768909941648; Tue, 20 Jan 2026 03:52:21 -0800 (PST) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8942e6214c5sm100416876d6.25.2026.01.20.03.52.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jan 2026 03:52:21 -0800 (PST) Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfauth.phl.internal (Postfix) with ESMTP id 45E7EF40068; Tue, 20 Jan 2026 06:52:20 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Tue, 20 Jan 2026 06:52:20 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddugedtfeegucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueehgeeg gfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepvddtpdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopehlihhnuhigqdhfshguvghvvghlsehvghgvrhdrkhgvrhhnvghlrd horhhgpdhrtghpthhtohepkhgrshgrnhdquggvvhesghhoohhglhgvghhrohhuphhsrdgt ohhmpdhrtghpthhtohepfihilhhlsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehpvg htvghriiesihhnfhhrrgguvggrugdrohhrghdprhgtphhtthhopegsohhquhhnrdhfvghn ghesghhmrghilhdrtghomhdprhgtphhtthhopehmrghrkhdrrhhuthhlrghnugesrghrmh drtghomhdprhgtphhtthhopehgrghrhiesghgrrhihghhuohdrnhgvth X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 20 Jan 2026 06:52:19 -0500 (EST) From: Boqun Feng To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, kasan-dev@googlegroups.com Cc: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Gary Guo , Miguel Ojeda , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Elle Rhumsaa , "Paul E. McKenney" , Marco Elver , FUJITA Tomonori Subject: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers Date: Tue, 20 Jan 2026 19:52:07 +0800 Message-ID: <20260120115207.55318-3-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260120115207.55318-1-boqun.feng@gmail.com> References: <20260120115207.55318-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an `Atomic::from_ptr()` to provide a `&Atomic`. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically a `Atomic::from_ptr().op()` wrapper. Note: for naming, since `atomic_xchg()` and `atomic_cmpxchg()` has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named as `xchg()` and `cmpxchg()`. For `atomic_load()` and `atomic_store()`, their 32bit C counterparts are `atomic_read()` and `atomic_set()`, so keep the `atomic_` prefix. Signed-off-by: Boqun Feng Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo --- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index d49ee45c6eb7..6c46335bdb8c 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -611,3 +611,107 @@ pub fn cmpxchg( } } } + +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, a= nd can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inte= r-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`. +/// +/// # Safety +/// +/// - `ptr` is a valid pointer to `T` and aligned to `align_of::()`. +/// - If there is a concurrent store from kernel (C or Rust), it has to be= atomic. +#[doc(alias("READ_ONCE", "smp_load_acquire"))] +#[inline(always)] +pub unsafe fn atomic_load( + ptr: *mut T, + o: Ordering, +) -> T +where + T::Repr: AtomicBasicOps, +{ + // SAFETY: Per the function safety requirement, `ptr` is valid and ali= gned to + // `align_of::()`, and all concurrent stores from kernel are atomic= , hence no data race per + // LKMM. + unsafe { Atomic::from_ptr(ptr) }.load(o) +} + +/// Atomic store over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, a= nd can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_store(.., Relaxed)` maps to `WRITE_ONCE()` when using for in= ter-thread communication. +/// - `atomic_load(.., Release)` maps to `smp_store_release()`. +/// +/// # Safety +/// +/// - `ptr` is a valid pointer to `T` and aligned to `align_of::()`. +/// - If there is a concurrent access from kernel (C or Rust), it has to b= e atomic. +#[doc(alias("WRITE_ONCE", "smp_store_release"))] +#[inline(always)] +pub unsafe fn atomic_store( + ptr: *mut T, + v: T, + o: Ordering, +) where + T::Repr: AtomicBasicOps, +{ + // SAFETY: Per the function safety requirement, `ptr` is valid and ali= gned to + // `align_of::()`, and all concurrent accesses from kernel are atom= ic, hence no data race + // per LKMM. + unsafe { Atomic::from_ptr(ptr) }.store(v, o); +} + +/// Atomic exchange over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().xchg(..)`, a= nd can be used to work +/// with C side on synchronizations. +/// +/// # Safety +/// +/// - `ptr` is a valid pointer to `T` and aligned to `align_of::()`. +/// - If there is a concurrent access from kernel (C or Rust), it has to b= e atomic. +#[inline(always)] +pub unsafe fn xchg( + ptr: *mut T, + new: T, + o: Ordering, +) -> T +where + T::Repr: AtomicExchangeOps, +{ + // SAFETY: Per the function safety requirement, `ptr` is valid and ali= gned to + // `align_of::()`, and all concurrent accesses from kernel are atom= ic, hence no data race + // per LKMM. + unsafe { Atomic::from_ptr(ptr) }.xchg(new, o) +} + +/// Atomic compare and exchange over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().cmpxchg(..)`= , and can be used to work +/// with C side on synchronizations. +/// +/// # Safety +/// +/// - `ptr` is a valid pointer to `T` and aligned to `align_of::()`. +/// - If there is a concurrent access from kernel (C or Rust), it has to b= e atomic. +#[doc(alias("try_cmpxchg"))] +#[inline(always)] +pub unsafe fn cmpxchg( + ptr: *mut T, + old: T, + new: T, + o: Ordering, +) -> Result +where + T::Repr: AtomicExchangeOps, +{ + // SAFETY: Per the function safety requirement, `ptr` is valid and ali= gned to + // `align_of::()`, and all concurrent accesses from kernel are atom= ic, hence no data race + // per LKMM. + unsafe { Atomic::from_ptr(ptr) }.cmpxchg(old, new, o) +} diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic= /predefine.rs index 5faa2fe2f4b6..11bc67ab70a3 100644 --- a/rust/kernel/sync/atomic/predefine.rs +++ b/rust/kernel/sync/atomic/predefine.rs @@ -235,6 +235,14 @@ fn atomic_basic_tests() { =20 assert_eq!(v, x.load(Relaxed)); }); + + for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |= v| { + let x =3D Atomic::new(v); + let ptr =3D x.as_ptr(); + + // SAFETY: `ptr` is a valid pointer and no concurrent access. + assert_eq!(v, unsafe { atomic_load(ptr, Relaxed) }); + }); } =20 #[test] @@ -245,6 +253,17 @@ fn atomic_acquire_release_tests() { x.store(v, Release); assert_eq!(v, x.load(Acquire)); }); + + for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |= v| { + let x =3D Atomic::new(0); + let ptr =3D x.as_ptr(); + + // SAFETY: `ptr` is a valid pointer and no concurrent access. + unsafe { atomic_store(ptr, v, Release) }; + + // SAFETY: `ptr` is a valid pointer and no concurrent access. + assert_eq!(v, unsafe { atomic_load(ptr, Acquire) }); + }); } =20 #[test] @@ -258,6 +277,18 @@ fn atomic_xchg_tests() { assert_eq!(old, x.xchg(new, Full)); assert_eq!(new, x.load(Relaxed)); }); + + for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |= v| { + let x =3D Atomic::new(v); + let ptr =3D x.as_ptr(); + + let old =3D v; + let new =3D v + 1; + + // SAFETY: `ptr` is a valid pointer and no concurrent access. + assert_eq!(old, unsafe { xchg(ptr, new, Full) }); + assert_eq!(new, x.load(Relaxed)); + }); } =20 #[test] @@ -273,6 +304,21 @@ fn atomic_cmpxchg_tests() { assert_eq!(Ok(old), x.cmpxchg(old, new, Relaxed)); assert_eq!(new, x.load(Relaxed)); }); + + for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |= v| { + let x =3D Atomic::new(v); + let ptr =3D x.as_ptr(); + + let old =3D v; + let new =3D v + 1; + + // SAFETY: `ptr` is a valid pointer and no concurrent access. + assert_eq!(Err(old), unsafe { cmpxchg(ptr, new, new, Full) }); + assert_eq!(old, x.load(Relaxed)); + // SAFETY: `ptr` is a valid pointer and no concurrent access. + assert_eq!(Ok(old), unsafe { cmpxchg(ptr, old, new, Relaxed) }= ); + assert_eq!(new, x.load(Relaxed)); + }); } =20 #[test] --=20 2.51.0