From nobody Sun Feb 8 08:27:45 2026 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) (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 08F643164C3 for ; Sat, 17 Jan 2026 12:22:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652579; cv=none; b=DNnXX9cVsH02kgXarljGnLBAVR87m2mdH4HI99geo8GKM2kIERpT6stgJU7050FeVbHkU1meg8S8drrZDIFpg+BPehOmSIfyarOTY+WiMKGsxbdORmevv2rUkU8kPiTIeyrU7xMmLQrhCsDhfGIibQvg2pfszOQ7iCuew64lAd8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652579; c=relaxed/simple; bh=2YTixGagJGjbYi4msm0geEt/p3KMC7b9ZbPR/aUA/5k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dKHxRPXvxswgUNxkDsO+PA7EV4csT7okOTdDLUUHRuOHMdhju9E4XK4dRZzg+h9b9x1Bd/NE9tP00xyv5BgefwoXkyodJ0fZQCc5rNpqMxVL2BtOCEaYs92kp2SBrvMB5kYgE42i/QXHXqEqnd60Iimg9Fmt3++H/STuNSuCvdE= 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=CTSqMZD8; arc=none smtp.client-ip=209.85.160.182 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="CTSqMZD8" Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-502acd495feso11909341cf.2 for ; Sat, 17 Jan 2026 04:22:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768652577; x=1769257377; 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=1OIo1B4Gq4+lB/pLEgP5IRCzf+1y4S/6CWlM+DSz+/I=; b=CTSqMZD866clmiwcO15+ZD9vuljYVGBw5UK041+vRaHkf579UaPfSzEHxMXiyp5Cwe sE+1c976iOBMuMPwNrqBXCl/wpsiOpvjpG6UzQZo3O3DSs51SX4JZiVvemdFHgQDxuoh jUaQW6jef56OJ2zJNth5sf7Nw82EWMJnlywKHxbdHf5N8DEiPV1YU2XSSlDrKjyrlzt8 0udP0ppQn65ujTKUi4LaXvbyurFYZb9Ret0Jjp7skATUgpyUsHE4gWDwmnUpTGIg7RHh aRnbLClDEpd5rTN2DjSXH31FrECG/gy41ICnboK9hLR/4OyND52z/UuwFkIwHy3upeRP xKzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768652577; x=1769257377; 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=1OIo1B4Gq4+lB/pLEgP5IRCzf+1y4S/6CWlM+DSz+/I=; b=PE68UMstb7SnQ/LEHK4T9FkMbhuKbznsFRabOVQRApgyQO6XuzxOGT6XtRYLNDgH1z Pvpb4eJH5b97vKlAYBCP49axazSY5kIzeoNX1AvH0gH5D77LCTOeD3mjAog933yedBD5 IwSqTc0630A98tPsd5re9Oi6YQ0VjJ14S8jvy+Wi4rU/LSZdtzWw/KDEkDIqQO39dHez WOXdweWRnm1DSkBakDgdeNL8nHJ+LgUJkBZ5dJkWHAnjfTu2dmbEpJu/KYIMrh2HhPOB H6SMhMmlMov2YHjvVLhe/bivFB/PFSmNazpRx6YWLbM+vAYGO+sy4zCz2FSxKsfPzmSs J1YA== X-Forwarded-Encrypted: i=1; AJvYcCVbl8s9Do71o83JAx8XYZU9Eh5FFxCFhJohRySz2n93vtIXf2FeMmQ5EtKQLOEqltQKNXWpboIhkcWNsP0=@vger.kernel.org X-Gm-Message-State: AOJu0YwvqJ3o9uqaNzX7Wh0ysR1gMWVwnytEYpsBpuaqUWrYjzk1gU7j DX5vs1dAqnKkWWNu4GKeDTGVgPtfk+U1jmd/bhg3/ZhVrEAmvAxSIoyN X-Gm-Gg: AY/fxX7abxi2dy5spIKEiaT3PkFvVH/PGYOh/TY1JwYjl4E59kBWNCbqf/dZVw9xma+ iSxv5iDaiGskXGIzjdLjrP7gB4rCyBZ44TmG6t/1uKw71LNlO1Sl/8UCcZQtYpuSORebm4OGUPF 4RIaBVIBQQcgCS3nXnOmZMu+bvlKyT2ijt2Hr585HWD+EWOVjpwLlhgNuJtOg9JkWG3iO8Kb674 JrzAh9alaGaaZJunkp8wsUXZoKXRjzXyYmzucGzldxm4WxVk1nGek2BwWrgtZp3mAten2zPnyJp IZY4lovnVtdIm7hEqvb4gwrdJmY5ew0CqHhDIhu8FgUgxj027MM9rm8pNnSspoDa0h30tDxXp92 Vn0QhpbnVBsIP5tizHc/g/FZCDwtky0L4rQkDLPEqAT02ydZJzxJX8E4rQ80313cYhFHpyi3/6D kuTBTk/5pH5SAyA5so7t1UyCT3RsLNtnVHZ6L+YpvtLa9H32yKgyIymNDQwaWw9RlVS3vxpGntM sT0HNvv0KbVjMsTcSPvEWqT7A== X-Received: by 2002:a05:622a:199c:b0:4ed:dec2:301a with SMTP id d75a77b69052e-502a1e1da21mr95361171cf.16.1768652576843; Sat, 17 Jan 2026 04:22:56 -0800 (PST) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-502a1ed3256sm38240091cf.16.2026.01.17.04.22.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 04:22:56 -0800 (PST) Received: from phl-compute-10.internal (phl-compute-10.internal [10.202.2.50]) by mailfauth.phl.internal (Postfix) with ESMTP id 7F02DF4007E; Sat, 17 Jan 2026 07:22:55 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-10.internal (MEProxy); Sat, 17 Jan 2026 07:22:55 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddufedujeelucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueehgeeg gfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepvdeipdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrgh dprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopehrtghusehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoh epohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepsghoqhhunhdrfhgvnhhg sehgmhgrihhlrdgtohhmpdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpd hrtghpthhtohepsghjohhrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhmpdhrtghp thhtoheplhhoshhsihhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegrrdhhihhnug gsohhrgheskhgvrhhnvghlrdhorhhg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 17 Jan 2026 07:22:54 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Cc: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Will Deacon , Peter Zijlstra , Mark Rutland , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , FUJITA Tomonori Subject: [PATCH 1/5] rust: helpers: Generify the definitions of rust_helper_*_{read,set}* Date: Sat, 17 Jan 2026 20:22:39 +0800 Message-ID: <20260117122243.24404-2-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260117122243.24404-1-boqun.feng@gmail.com> References: <20260117122243.24404-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" To support atomic pointers, more {read,set} helpers will be introduced, hence define macros to generate these helpers to ease the introduction of the future helpers. Signed-off-by: Boqun Feng --- rust/helpers/atomic_ext.c | 53 +++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c index 7d0c2bd340da..f471c1ff123d 100644 --- a/rust/helpers/atomic_ext.c +++ b/rust/helpers/atomic_ext.c @@ -4,45 +4,38 @@ #include #include =20 -__rust_helper s8 rust_helper_atomic_i8_read(s8 *ptr) -{ - return READ_ONCE(*ptr); -} - -__rust_helper s8 rust_helper_atomic_i8_read_acquire(s8 *ptr) -{ - return smp_load_acquire(ptr); -} - -__rust_helper s16 rust_helper_atomic_i16_read(s16 *ptr) -{ - return READ_ONCE(*ptr); +#define GEN_READ_HELPER(tname, type) \ +__rust_helper type rust_helper_atomic_##tname##_read(type *ptr) \ +{ \ + return READ_ONCE(*ptr); \ } =20 -__rust_helper s16 rust_helper_atomic_i16_read_acquire(s16 *ptr) -{ - return smp_load_acquire(ptr); +#define GEN_SET_HELPER(tname, type) \ +__rust_helper void rust_helper_atomic_##tname##_set(type *ptr, type val) \ +{ \ + WRITE_ONCE(*ptr, val); \ } =20 -__rust_helper void rust_helper_atomic_i8_set(s8 *ptr, s8 val) -{ - WRITE_ONCE(*ptr, val); +#define GEN_READ_ACQUIRE_HELPER(tname, type) \ +__rust_helper type rust_helper_atomic_##tname##_read_acquire(type *ptr) \ +{ \ + return smp_load_acquire(ptr); \ } =20 -__rust_helper void rust_helper_atomic_i8_set_release(s8 *ptr, s8 val) -{ - smp_store_release(ptr, val); +#define GEN_SET_RELEASE_HELPER(tname, type) \ +__rust_helper void rust_helper_atomic_##tname##_set_release(type *ptr, typ= e val)\ +{ \ + smp_store_release(ptr, val); \ } =20 -__rust_helper void rust_helper_atomic_i16_set(s16 *ptr, s16 val) -{ - WRITE_ONCE(*ptr, val); -} +#define GEN_READ_SET_HELPERS(tname, type) \ + GEN_READ_HELPER(tname, type) \ + GEN_SET_HELPER(tname, type) \ + GEN_READ_ACQUIRE_HELPER(tname, type) \ + GEN_SET_RELEASE_HELPER(tname, type) \ =20 -__rust_helper void rust_helper_atomic_i16_set_release(s16 *ptr, s16 val) -{ - smp_store_release(ptr, val); -} +GEN_READ_SET_HELPERS(i8, s8) +GEN_READ_SET_HELPERS(i16, s16) =20 /* * xchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the --=20 2.51.0 From nobody Sun Feb 8 08:27:45 2026 Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) (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 5B46030C601 for ; Sat, 17 Jan 2026 12:23:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652585; cv=none; b=LpqddBMWLOl+T9msezMYyZCUTaak78k+CVU8jvgMntogLvXg1hrJTq6wkdj59vna0T0EFKeqaEVSfjD9UEaEjDPAS5uFtSkKoMfHIQ4DIxcaDn747JOwAGZJXDaYE04tw2ZTbG628Yv0TTytPKNnmy5k4sjQmC8q0QVTiJNaSb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652585; c=relaxed/simple; bh=ypW5mDR1pdpYmnFGvy1k5garryQ20NH8PB64bMKaoRM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OLqmZhbJklUS0LwFt6J7oJemcKZTTRHf6gjNDOt0zW/pV7SGRfrBc6ryrXYPGEqT3k6gtkUGYHMBWUCco2o13G03XFTEtleF/48JZBzHSk9PIuivfRg7orSWewtH7hpSeALZfsC84I0YYGbY3YqitNR5dejeOiyxCnj04GrqdYM= 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=mRGoRTfl; arc=none smtp.client-ip=209.85.160.173 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="mRGoRTfl" Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-5014f383df6so21933881cf.1 for ; Sat, 17 Jan 2026 04:23:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768652579; x=1769257379; 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=ezYTTB2Yi2q5O6hjbHNxnHEWCgaRA8NSsABtQ4Dr/Zk=; b=mRGoRTfl4oRa/my4bPocutX0w00DTkp3x4Mf/ICdMOiMuuMGHFsaQEeoWPlF3gEY8W zcBGRm7HJvRA3/KTCYpJ6ey72SHn12YKH0PVXNo6n8hdRjybMKarHu1aHqrUV1p+cSyT f+LRkuisoKB6XHItIUPBfJQAryYW22Oz9ddP1FbTlhV8RIQPB9uki6xsuEpWFLQTy4XL S79TUfV87Qto1sckxwoa7ba1c1tqoCv+cUQ1Fadt9q3RJcsI3teZJk7W6Ct3Bfj6klMC E6jkA4NWs9XYLXVVGHJGUw0LHtD6JfQnasj8m/ZH66U+R8Po8uDYvhHVuOVeKMCEdz8r aC9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768652579; x=1769257379; 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=ezYTTB2Yi2q5O6hjbHNxnHEWCgaRA8NSsABtQ4Dr/Zk=; b=cSOM5XR0SrbomzQzQ/nesK2R4WTng+344hgn8T/RU0ztaNVd/fNg0GbGOc3bemU/4e PAHpJ1NL2kZeG12YbXSkmLFTYVMoUTdkd0+W3YJXPmcfl+eLg8kGsXyTiD3tJVKbg9qA Kd8Tumx90M0/+HeYp4oT1l6+b0lAYMG9UsA4VoyiDah3RbA0tMLT11YY+f6LSONfAgLL 83DjaZvxO+iv3w3QMDUgW5C+ng3sWTAmdxDG5AX/Xdlutaio0mmRWouO2S+Z693kfA5k 2mUIUvN6tvJ6ItJFB0YCMvRIesxrc6cUzVvK/E+dyWb6fHWS26f4aAAtttj7Qpx3X8Nc MEsw== X-Forwarded-Encrypted: i=1; AJvYcCUxciUU3a1D7TZOgFuMQKGDNUM6jfD8pxV5ASSbMrFB1TSshTt5BHUCcG2Huj6Vduk3zzOpchNfLoSGLsA=@vger.kernel.org X-Gm-Message-State: AOJu0YzWFSI5I6PLqq5uOv07uf3aJST508xJjpBIUgqonw+HvoebYF1Q kAiZz5ryNh31FfF9MLjZIwqWnrzBkMJiV7Ecq3GOZbBubfAr68ByfJET X-Gm-Gg: AY/fxX4/33WjSn6oq24YTQeAocWSeDerCo2I1BLLxYb38vuKK1BiwMtb5pPVaFcW5pc qjNucMLZf8t5+A62JwE0MfZWKgClC1SmJks30pMoG8ckv5aUorfnJ3QOA2W3b6+efepxbA21OqQ r9x+7s0OEZD2x1S7x2ZrAIpvd99K1sZzQx+qcKApoixnXh+N2wIgKH2dJ1VlDlPQaI9QhdBA2Em QRiCDLb9HhAWDloNNaURKoKw0J81jMlzQVGmMZmAH781dBAPwQDbADILiwu8Wa4X1YeDwWGUwHN ytARFmycozAPzHiGYU1VX6VgwZD7apC35ExCcTLDG6CrkUhV8MKVGnC77jPE3ncNO7o2GROAoV9 ++v0zSh3/hvk+X8cS6r6c/pEYmNZzrzVjBct8N/njsKPfFbjc8qEVqathMNh8FGk5vr4C4+Ycy4 PyG4b4qCfpyTqliQlk2WgnqBenzpnwzD65+AOxd5HJRfu/MjzFXLFjF2ZiTb7L59xiKN+NBxahG +iOTCtuxpD/b6I= X-Received: by 2002:a05:622a:c9:b0:4f0:131f:66fe with SMTP id d75a77b69052e-502a1779249mr70449511cf.59.1768652579264; Sat, 17 Jan 2026 04:22:59 -0800 (PST) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-502a1d9ee19sm37969421cf.14.2026.01.17.04.22.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 04:22:58 -0800 (PST) Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfauth.phl.internal (Postfix) with ESMTP id 3FCEDF4007E; Sat, 17 Jan 2026 07:22:58 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Sat, 17 Jan 2026 07:22:58 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddufedukedtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueehgeeg gfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepvdeipdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrgh dprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopehrtghusehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoh epohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepsghoqhhunhdrfhgvnhhg sehgmhgrihhlrdgtohhmpdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpd hrtghpthhtohepsghjohhrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhmpdhrtghp thhtoheplhhoshhsihhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegrrdhhihhnug gsohhrgheskhgvrhhnvghlrdhorhhg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 17 Jan 2026 07:22:57 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Cc: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Will Deacon , Peter Zijlstra , Mark Rutland , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , FUJITA Tomonori Subject: [PATCH 2/5] rust: helpers: Generify the definitions of rust_helper_*_xchg* Date: Sat, 17 Jan 2026 20:22:40 +0800 Message-ID: <20260117122243.24404-3-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260117122243.24404-1-boqun.feng@gmail.com> References: <20260117122243.24404-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" To support atomic pointers, more xchg helpers will be introduced, hence define macros to generate these helpers to ease the introduction of the future helpers. Signed-off-by: Boqun Feng --- rust/helpers/atomic_ext.c | 48 ++++++++++----------------------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c index f471c1ff123d..c5f665bbe785 100644 --- a/rust/helpers/atomic_ext.c +++ b/rust/helpers/atomic_ext.c @@ -44,45 +44,21 @@ GEN_READ_SET_HELPERS(i16, s16) * The architectures that currently support Rust (x86_64, armv7, * arm64, riscv, and loongarch) satisfy these requirements. */ -__rust_helper s8 rust_helper_atomic_i8_xchg(s8 *ptr, s8 new) -{ - return xchg(ptr, new); -} - -__rust_helper s16 rust_helper_atomic_i16_xchg(s16 *ptr, s16 new) -{ - return xchg(ptr, new); -} - -__rust_helper s8 rust_helper_atomic_i8_xchg_acquire(s8 *ptr, s8 new) -{ - return xchg_acquire(ptr, new); -} - -__rust_helper s16 rust_helper_atomic_i16_xchg_acquire(s16 *ptr, s16 new) -{ - return xchg_acquire(ptr, new); -} - -__rust_helper s8 rust_helper_atomic_i8_xchg_release(s8 *ptr, s8 new) -{ - return xchg_release(ptr, new); -} - -__rust_helper s16 rust_helper_atomic_i16_xchg_release(s16 *ptr, s16 new) -{ - return xchg_release(ptr, new); +#define GEN_XCHG_HELPER(tname, type, suffix) \ +__rust_helper type \ +rust_helper_atomic_##tname##_xchg##suffix(type *ptr, type new) \ +{ \ + return xchg##suffix(ptr, new); \ } =20 -__rust_helper s8 rust_helper_atomic_i8_xchg_relaxed(s8 *ptr, s8 new) -{ - return xchg_relaxed(ptr, new); -} +#define GEN_XCHG_HELPERS(tname, type) \ + GEN_XCHG_HELPER(tname, type, ) \ + GEN_XCHG_HELPER(tname, type, _acquire) \ + GEN_XCHG_HELPER(tname, type, _release) \ + GEN_XCHG_HELPER(tname, type, _relaxed) \ =20 -__rust_helper s16 rust_helper_atomic_i16_xchg_relaxed(s16 *ptr, s16 new) -{ - return xchg_relaxed(ptr, new); -} +GEN_XCHG_HELPERS(i8, s8) +GEN_XCHG_HELPERS(i16, s16) =20 /* * try_cmpxchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the --=20 2.51.0 From nobody Sun Feb 8 08:27:45 2026 Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) (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 2FE68313E0D for ; Sat, 17 Jan 2026 12:23:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652585; cv=none; b=J8Y6nXJITksFO2BSjNN5Qnh1sEq6WGNG3OgQu+y8Ei6+kbEQhRikXu0t/gvKD1CAULxn8v0ReviYDEIvHBuPXRGJGaF3r7k2sPtuw2HsoeDcKj6b6HDAPZf5t78BzqxRUfPlDaSJYFnB3tAcZ6p8eNdWAUkR/mkiX1p+gOk3vuo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652585; c=relaxed/simple; bh=4NIKpxlbSunL+Z4N22NpquT4aJ22jfPSx7k1kPWckZw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MhIAAYWmFiuNwy8xqAIOhlxvN1o+BtBuu06jrSl9NZfjMs8ZHGyh+Xvm0qxZ/s5juqqTI17QFvwLI7KonW0wDeh1+JD5fdXwrnC1fPW+EtVIjPNhXV+LvB9MHo6jAWelUQGsIIi6Duv1NjuznyOLUe9/nWjf+bSTJRbYyTIGuFw= 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=TODDNEXj; arc=none smtp.client-ip=209.85.222.175 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="TODDNEXj" Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-8c6a508a595so231162985a.2 for ; Sat, 17 Jan 2026 04:23:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768652582; x=1769257382; 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=gkWeTN6oy/OKIhfGiOTDWYHCdlEF4RUeCy+vRBjzbJU=; b=TODDNEXjJHh+4tOP4Xyod0JUm4eSjO2uxblJkZRvubat8/MWLRFQiHSBfaOuwEYMse BoOgdo5a0nbQI9ud88+JNAy2fqXofzJmg+iMpODvxhYrTnTWQXZ4C1ZRzSUCO7thVlGd 1jp3pHB4Mkidmv4fxQyHzTb8gX3cRf+8KyjVDkKqNYQBNNhvIKMeYEt7lSiMB/puOZwy J5T+4b+zh9g27AWlR+GLiaTk7eF1wPiEMFigy999LPoJhO468TfJLx7TCM9PwFyBeg5F 6AAb36c+eJMAFcvxgB/I6OBOPSNqQieyVwZuJUh5d9pW5njVROgtLuvkO/rcG5rdoB43 l2pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768652582; x=1769257382; 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=gkWeTN6oy/OKIhfGiOTDWYHCdlEF4RUeCy+vRBjzbJU=; b=Z1Jd7I+9XyfemDlHuJ7ZbjSLtF5HSYWgzIsHhJsrgqj7BF8RdzhRLA9qHzYO1VbLDt dAe0ALfboYurvCQuPuVB33IYGCASaYmQQnvdaXcGwzs1zbnRWDp8Ku2VmbbHWfYqSMka wv7CGc6Yk565kq+1DOywU3Zlnkr2FD+wyg+TN74lsU+swPuJniK6vNmY27N02oJQZHep rysCivsstx0WLbDQhiFizKbnkq1gfx6sVWLfMiVCjd/DwGwMrfvY30XrMJhVhi5MBfDy nrma0RB8R0l2tfgmpyz0Y9BybTsh2CBQMVQhzS5BWc5IxSmnl+EelI+abqRBm66DORhJ cElA== X-Forwarded-Encrypted: i=1; AJvYcCWGsrfw3qdJm+sIdgTLEG/P98QBRpjdbgzAs79WBh9IZ5W4GBD9/8O4Bl2lHmv3d39QbgpRHg97BjUN1Us=@vger.kernel.org X-Gm-Message-State: AOJu0YxyG9vEKp8/NaSVaypsSHKiFG563Sua0AjH4tOw5rB7TgqUxj5A i0AASqvwIYjfQbmN3mj+40LiN+eRbIk3lolkjNq3IapZseRVP8f8lQho X-Gm-Gg: AY/fxX6xRTykEwVGdPtLtQdBQ/VY5sGfLGdfHWO+2d05EQhMAFRdQshMifuJBuZeY16 gk+LLUxtkmTacrgmt9gZ/lI5nLAWo0eFCgxPxbFqs30GP12dQ/P/zAL3y2oqptt5CyCzWXcMfJ8 MJq9xuZWsQaBBXpeAASZnqiivYaQjmVwI55cSyGaWdgGs+wgTIxyqyOFylx6a11jyYmDZysCTAO YVPy65I68rFZoP9UGlzcUCWVkg1cTWEPHK7lNKogJFkm4qZPMK+ozhvW8+wwO4cmx5aq90NRPuN yjcpOhxkT4enOeGk+49zjwO/bgtCo4kPFmJ20fBmmMYD3dScz3z8ZahiIHGulw2PnDihPqOwIQM 20uaySnaFyDfw0Jv6iRBun/UDy4Au+PqANEe/J1JSBFWwX9qQLujh6fUTKF90SkVOkG55zkoHcl 5neOy4zdPccz8/SjoMKFaaiDEcXMfeNsa9EU10bP0JduJUC0xZjzNFVrlwjkRk82u9gV5JR1zt7 Dfw7i8KpyMd4cy0ob7p738lbA== X-Received: by 2002:a05:620a:7009:b0:8b2:dd0a:8809 with SMTP id af79cd13be357-8c6b1628dabmr318598085a.89.1768652582099; Sat, 17 Jan 2026 04:23:02 -0800 (PST) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8c6a71c0469sm422247385a.13.2026.01.17.04.23.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 04:23:01 -0800 (PST) Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id 02268F4007E; Sat, 17 Jan 2026 07:23:01 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Sat, 17 Jan 2026 07:23:01 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddufedukedtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueehgeeg gfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepvdeipdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrgh dprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopehrtghusehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoh epohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepsghoqhhunhdrfhgvnhhg sehgmhgrihhlrdgtohhmpdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpd hrtghpthhtohepsghjohhrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhmpdhrtghp thhtoheplhhoshhsihhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegrrdhhihhnug gsohhrgheskhgvrhhnvghlrdhorhhg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 17 Jan 2026 07:23:00 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Cc: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Will Deacon , Peter Zijlstra , Mark Rutland , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , FUJITA Tomonori Subject: [PATCH 3/5] rust: helpers: Generify the definitions of rust_helper_*_cmpxchg* Date: Sat, 17 Jan 2026 20:22:41 +0800 Message-ID: <20260117122243.24404-4-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260117122243.24404-1-boqun.feng@gmail.com> References: <20260117122243.24404-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" To support atomic pointers, more cmpxchg helpers will be introduced, hence define macros to generate these helpers to ease the introduction of the future helpers. Signed-off-by: Boqun Feng --- rust/helpers/atomic_ext.c | 48 ++++++++++----------------------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c index c5f665bbe785..240218e2e708 100644 --- a/rust/helpers/atomic_ext.c +++ b/rust/helpers/atomic_ext.c @@ -67,42 +67,18 @@ GEN_XCHG_HELPERS(i16, s16) * The architectures that currently support Rust (x86_64, armv7, * arm64, riscv, and loongarch) satisfy these requirements. */ -__rust_helper bool rust_helper_atomic_i8_try_cmpxchg(s8 *ptr, s8 *old, s8 = new) -{ - return try_cmpxchg(ptr, old, new); -} - -__rust_helper bool rust_helper_atomic_i16_try_cmpxchg(s16 *ptr, s16 *old, = s16 new) -{ - return try_cmpxchg(ptr, old, new); -} - -__rust_helper bool rust_helper_atomic_i8_try_cmpxchg_acquire(s8 *ptr, s8 *= old, s8 new) -{ - return try_cmpxchg_acquire(ptr, old, new); -} - -__rust_helper bool rust_helper_atomic_i16_try_cmpxchg_acquire(s16 *ptr, s1= 6 *old, s16 new) -{ - return try_cmpxchg_acquire(ptr, old, new); -} - -__rust_helper bool rust_helper_atomic_i8_try_cmpxchg_release(s8 *ptr, s8 *= old, s8 new) -{ - return try_cmpxchg_release(ptr, old, new); -} - -__rust_helper bool rust_helper_atomic_i16_try_cmpxchg_release(s16 *ptr, s1= 6 *old, s16 new) -{ - return try_cmpxchg_release(ptr, old, new); +#define GEN_TRY_CMPXCHG_HELPER(tname, type, suffix) \ +__rust_helper bool \ +rust_helper_atomic_##tname##_try_cmpxchg##suffix(type *ptr, type *old, typ= e new)\ +{ \ + return try_cmpxchg##suffix(ptr, old, new); \ } =20 -__rust_helper bool rust_helper_atomic_i8_try_cmpxchg_relaxed(s8 *ptr, s8 *= old, s8 new) -{ - return try_cmpxchg_relaxed(ptr, old, new); -} +#define GEN_TRY_CMPXCHG_HELPERS(tname, type) \ + GEN_TRY_CMPXCHG_HELPER(tname, type, ) \ + GEN_TRY_CMPXCHG_HELPER(tname, type, _acquire) \ + GEN_TRY_CMPXCHG_HELPER(tname, type, _release) \ + GEN_TRY_CMPXCHG_HELPER(tname, type, _relaxed) \ =20 -__rust_helper bool rust_helper_atomic_i16_try_cmpxchg_relaxed(s16 *ptr, s1= 6 *old, s16 new) -{ - return try_cmpxchg_relaxed(ptr, old, new); -} +GEN_TRY_CMPXCHG_HELPERS(i8, s8) +GEN_TRY_CMPXCHG_HELPERS(i16, s16) --=20 2.51.0 From nobody Sun Feb 8 08:27:45 2026 Received: from mail-qk1-f193.google.com (mail-qk1-f193.google.com [209.85.222.193]) (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 108542D780A for ; Sat, 17 Jan 2026 12:23:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652591; cv=none; b=n4eSdjgBUMFEFgUxE34WnlfEZ+Fr4FtvHW0RiqIF44T5ee/S/sUgKSKpNqb/GU8fZ6IRUu+O5cYh0Orp9DOWkEgNFuwsnMDsmPfMrvQT94da0rzJM9ijbTkQE99HOnW+Yd2K2HLDAj3GMeyIhmOyHkwY939Qke2pkHhSuIxF7zE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652591; c=relaxed/simple; bh=Mn9K21+uoF4cJ3TSX9t5OyIIQAr4QjA54LiStkUJdz8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d+Fgn5EOj2CHNS8kSNuGHjLC0a4Kqfzut4WlwDlqp3OWSrOYTY4zQ22XWcX0jplwmtxxdHTdTjn2Np8pZvi5OTzpBdET5dcS8MHSERyIZ9hgfZAJRhu8+f0wnnC5I23vn7/Aujyq4sTSC7oIzTkVGgKpLTOrjB/m4K/Eq0wPbeY= 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=iYLk7iNb; arc=none smtp.client-ip=209.85.222.193 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="iYLk7iNb" Received: by mail-qk1-f193.google.com with SMTP id af79cd13be357-8c52f15c5b3so337891585a.3 for ; Sat, 17 Jan 2026 04:23:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768652586; x=1769257386; 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=JqkORS96nGwm/tYZHUw31tz+3dE4qDqicBtgX0AoLrQ=; b=iYLk7iNblRbM46CW1cZBLTffa+NPDJqDFmr+Z2fqsm0zr5jw0pPMP9OCVwfxzDC7yy BqosSPt3A5lCtVzFsIqKM1nQ16UJRrcsqFJTDuo241s2FQZfqF5IPwQY8uH18qEno+f9 A7D0cdOFsnG+YVtoCjfweZ0pl/jOqi2sQ7U3d53fy7EuPNJUsQSw+JSwL1Wpe/XyKC1W 1MjTi76ZzuuvAmjy4Gu9DF8ZcKN8qk+PtKITj0tObmwzW0dFZ5+LVb/IFhh0oCY2w8rh mMtNNR3XeUQGBhAKIWgstcYkC0DXGSL0bkRexBCZOZmpFIniEqkfcOKK3WmA4btQOUVG rvng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768652586; x=1769257386; 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=JqkORS96nGwm/tYZHUw31tz+3dE4qDqicBtgX0AoLrQ=; b=lIIjYXtvS00F4pkfhKbAuancU/r+LaFT8EP3+4xnsRdEYJPProD4Z5w3HKJqOlqgqv kKdesVwpmB1q5ZnEKb2qK/iR+X+V4Jv7MUNLqR5F5z6lswcGbhv/EqoQuuWZSi5Ko8g1 2PhdxJCsaLdWywwKsXlu4O4Bm2+RzwLuWpl3jgbmMKSqvlMBBRuxV77+bVpHBmrsWenK zNx/z25sWx+Gc7IXEZ+U5VlIN3LqatZUBXJ2V+uJrGhN1mvHYCzli6FwTBXqdruTxrwX fDXcr2APmlojTg1JpzD+xuPtV7xA6HhOKQj0vUGRoK5HKweGi+6XyihOlWrGxCtd6p+6 rY8A== X-Forwarded-Encrypted: i=1; AJvYcCXJyZA6Ief0V27t7MRECGpDUAG0/V0qWI7HDvTMnXwKywosg+FXt8Mn5ywG4p0MmIulr0hjI/nAzt6z8Tc=@vger.kernel.org X-Gm-Message-State: AOJu0YxUGG066rdExFOuJ/hU3wUru7c3Sf5cz3yBVQHu1OR2JnzDtbCt KEZAsrii0onULrRuoU8a7iF3pz7oi/KDMeN0LB6lbnHVfYPxjAWS7QGg X-Gm-Gg: AY/fxX56c0rKBbPuAgnMfvFUfgRmtbuJihNmcHMuQJzHNehygMpkksBqdHiGbjpRBFM gpcsaod/sofYS2koix2swybTDQ4/yLvEs/oPabrn86iTC/a5XNNNCGnhe9c/ePTc60edFFPXsTa UIec+ewjnBBvcL2FolqmzCzC6SNQgzOrmxtf//WQHr99p6hqRc1eCySW1y8T7SreB4bb72rteZt o4M/0V7DHf+kbCeNXlkZf1sPZ0q0o2vRPrSCL4WsR2oAGKUKIfHxdzqmG4cDPZ+8RhLWkRHUMB+ nnQXC+ebhCC96pRYu4lFl+vlEPTUlzZ1sR306gis0M6TdW6J/UALomPRvZn013ROGuGFa/tRATm vEhnPjE3pIWxgTUOgyH6tw38uPC9ZtmQJwyskl95z7RSzL1Te1TqfUcWV2nn1vEkoUf4HU6EmpH vVGgsouAdvV1O4tv6tGtg/ZXiWeY+3iqnqoqhMyyIlLrakcOaiMcmPjADmqI0JYsuuZzrEg6271 mB8wqOaYubte3o= X-Received: by 2002:a05:620a:710c:b0:8b2:d72d:e41b with SMTP id af79cd13be357-8c6a676de84mr823908785a.44.1768652585732; Sat, 17 Jan 2026 04:23:05 -0800 (PST) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8942e5e535dsm46061576d6.4.2026.01.17.04.23.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 04:23:05 -0800 (PST) Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id 86362F4007E; Sat, 17 Jan 2026 07:23:04 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Sat, 17 Jan 2026 07:23:04 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddufedukedtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeegleejiedthedvheeggfejveefjeejkefgveffieeujefhueeigfegueehgeeg gfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepvdeipdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrgh dprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopehrtghusehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoh epohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepsghoqhhunhdrfhgvnhhg sehgmhgrihhlrdgtohhmpdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpd hrtghpthhtohepsghjohhrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhmpdhrtghp thhtoheplhhoshhsihhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegrrdhhihhnug gsohhrgheskhgvrhhnvghlrdhorhhg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 17 Jan 2026 07:23:03 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Cc: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Will Deacon , Peter Zijlstra , Mark Rutland , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , FUJITA Tomonori Subject: [PATCH 4/5] rust: sync: atomic: Add Atomic<*mut T> support Date: Sat, 17 Jan 2026 20:22:42 +0800 Message-ID: <20260117122243.24404-5-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260117122243.24404-1-boqun.feng@gmail.com> References: <20260117122243.24404-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" Atomic pointer support is an important piece of synchronization algorithm, e.g. RCU, hence provide the support for that. Note that instead of relying on atomic_long or the implementation of `Atomic`, a new set of helpers (atomic_ptr_*) is introduced for atomic pointer specifically, this is because ptr2int casting would lose the provenance of a pointer and even though in theory there are a few tricks the provenance can be restored, it'll still be a simpler implementation if C could provide atomic pointers directly. The side effects of this approach are: we don't have the arithmetic and logical operations for pointers yet and the current implementation only works on ARCH_SUPPORTS_ATOMIC_RMW architectures, but these are implementation issues and can be added later. Signed-off-by: Boqun Feng Reviewed-by: FUJITA Tomonori Reviewed-by: Gary Guo --- rust/helpers/atomic_ext.c | 3 +++ rust/kernel/sync/atomic.rs | 12 +++++++++++- rust/kernel/sync/atomic/internal.rs | 21 +++++++++++++++------ rust/kernel/sync/atomic/predefine.rs | 23 +++++++++++++++++++++++ 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c index 240218e2e708..c267d5190529 100644 --- a/rust/helpers/atomic_ext.c +++ b/rust/helpers/atomic_ext.c @@ -36,6 +36,7 @@ __rust_helper void rust_helper_atomic_##tname##_set_relea= se(type *ptr, type val) =20 GEN_READ_SET_HELPERS(i8, s8) GEN_READ_SET_HELPERS(i16, s16) +GEN_READ_SET_HELPERS(ptr, const void *) =20 /* * xchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the @@ -59,6 +60,7 @@ rust_helper_atomic_##tname##_xchg##suffix(type *ptr, type= new) \ =20 GEN_XCHG_HELPERS(i8, s8) GEN_XCHG_HELPERS(i16, s16) +GEN_XCHG_HELPERS(ptr, const void *) =20 /* * try_cmpxchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the @@ -82,3 +84,4 @@ rust_helper_atomic_##tname##_try_cmpxchg##suffix(type *pt= r, type *old, type new) =20 GEN_TRY_CMPXCHG_HELPERS(i8, s8) GEN_TRY_CMPXCHG_HELPERS(i16, s16) +GEN_TRY_CMPXCHG_HELPERS(ptr, const void *) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 4aebeacb961a..4d2a5228c2e4 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -51,6 +51,10 @@ #[repr(transparent)] pub struct Atomic(AtomicRepr); =20 +// SAFETY: `Atomic` is safe to transfer between execution contexts beca= use of the safety +// requirement of `AtomicType`. +unsafe impl Send for Atomic {} + // SAFETY: `Atomic` is safe to share among execution contexts because a= ll accesses are atomic. unsafe impl Sync for Atomic {} =20 @@ -68,6 +72,11 @@ unsafe impl Sync for Atomic {} /// /// - [`Self`] must have the same size and alignment as [`Self::Repr`]. /// - [`Self`] must be [round-trip transmutable] to [`Self::Repr`]. +/// - [`Self`] must be safe to transfer between execution contexts, if it'= s [`Send`], this is +/// automatically satisfied. The exception is pointer types that are eve= n though marked as +/// `!Send` (e.g. raw pointers and [`NonNull`]) but requiring `unsafe= ` to do anything +/// meaningful on them. This is because transferring pointer values betw= een execution contexts is +/// safe as long as the actual `unsafe` dereferencing is justified. /// /// Note that this is more relaxed than requiring the bi-directional trans= mutability (i.e. /// [`transmute()`] is always sound between `U` and `T`) because of the su= pport for atomic @@ -108,7 +117,8 @@ unsafe impl Sync for Atomic {} /// [`transmute()`]: core::mem::transmute /// [round-trip transmutable]: AtomicType#round-trip-transmutability /// [Examples]: AtomicType#examples -pub unsafe trait AtomicType: Sized + Send + Copy { +/// [`NonNull`]: core::ptr::NonNull +pub unsafe trait AtomicType: Sized + Copy { /// The backing atomic implementation type. type Repr: AtomicImpl; } diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/= internal.rs index 0dac58bca2b3..93f5a7846645 100644 --- a/rust/kernel/sync/atomic/internal.rs +++ b/rust/kernel/sync/atomic/internal.rs @@ -7,6 +7,7 @@ use crate::bindings; use crate::macros::paste; use core::cell::UnsafeCell; +use ffi::c_void; =20 mod private { /// Sealed trait marker to disable customized impls on atomic implemen= tation traits. @@ -14,10 +15,11 @@ pub trait Sealed {} } =20 // The C side supports atomic primitives only for `i32` and `i64` (`atomic= _t` and `atomic64_t`), -// while the Rust side also layers provides atomic support for `i8` and `i= 16` -// on top of lower-level C primitives. +// while the Rust side also provides atomic support for `i8`, `i16` and `*= const c_void` on top of +// lower-level C primitives. impl private::Sealed for i8 {} impl private::Sealed for i16 {} +impl private::Sealed for *const c_void {} impl private::Sealed for i32 {} impl private::Sealed for i64 {} =20 @@ -26,10 +28,10 @@ impl private::Sealed for i64 {} /// This trait is sealed, and only types that map directly to the C side a= tomics /// or can be implemented with lower-level C primitives are allowed to imp= lement this: /// -/// - `i8` and `i16` are implemented with lower-level C primitives. +/// - `i8`, `i16` and `*const c_void` are implemented with lower-level C p= rimitives. /// - `i32` map to `atomic_t` /// - `i64` map to `atomic64_t` -pub trait AtomicImpl: Sized + Send + Copy + private::Sealed { +pub trait AtomicImpl: Sized + Copy + private::Sealed { /// The type of the delta in arithmetic or logical operations. /// /// For example, in `atomic_add(ptr, v)`, it's the type of `v`. Usuall= y it's the same type of @@ -51,6 +53,13 @@ impl AtomicImpl for i16 { type Delta =3D Self; } =20 +// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the = atomicity is only +// guaranteed against read-modify-write operations if the architecture sup= ports native atomic RmW. +#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)] +impl AtomicImpl for *const c_void { + type Delta =3D isize; +} + // `atomic_t` implements atomic operations on `i32`. impl AtomicImpl for i32 { type Delta =3D Self; @@ -262,7 +271,7 @@ macro_rules! declare_and_impl_atomic_methods { } =20 declare_and_impl_atomic_methods!( - [ i8 =3D> atomic_i8, i16 =3D> atomic_i16, i32 =3D> atomic, i64 =3D> at= omic64 ] + [ i8 =3D> atomic_i8, i16 =3D> atomic_i16, *const c_void =3D> atomic_pt= r, i32 =3D> atomic, i64 =3D> atomic64 ] /// Basic atomic operations pub trait AtomicBasicOps { /// Atomic read (load). @@ -280,7 +289,7 @@ fn set[release](a: &AtomicRepr, v: Self) { ); =20 declare_and_impl_atomic_methods!( - [ i8 =3D> atomic_i8, i16 =3D> atomic_i16, i32 =3D> atomic, i64 =3D> at= omic64 ] + [ i8 =3D> atomic_i8, i16 =3D> atomic_i16, *const c_void =3D> atomic_pt= r, i32 =3D> atomic, i64 =3D> atomic64 ] /// Exchange and compare-and-exchange atomic operations pub trait AtomicExchangeOps { /// Atomic exchange. diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic= /predefine.rs index 42067c6a266c..1a4670d225b5 100644 --- a/rust/kernel/sync/atomic/predefine.rs +++ b/rust/kernel/sync/atomic/predefine.rs @@ -4,6 +4,7 @@ =20 use crate::static_assert; use core::mem::{align_of, size_of}; +use ffi::c_void; =20 // Ensure size and alignment requirements are checked. static_assert!(size_of::() =3D=3D size_of::()); @@ -28,6 +29,16 @@ unsafe impl super::AtomicType for i16 { type Repr =3D i16; } =20 +// SAFETY: +// +// - `*mut T` has the same size and alignment with `*const c_void`, and is= round-trip +// transmutable to `*const c_void`. +// - `*mut T` is safe to transfer between execution contexts. See the safe= ty requirement of +// [`AtomicType`]. +unsafe impl super::AtomicType for *mut T { + type Repr =3D *const c_void; +} + // SAFETY: `i32` has the same size and alignment with itself, and is round= -trip transmutable to // itself. unsafe impl super::AtomicType for i32 { @@ -215,4 +226,16 @@ fn atomic_bool_tests() { assert_eq!(false, x.load(Relaxed)); assert_eq!(Ok(false), x.cmpxchg(false, true, Full)); } + + #[test] + fn atomic_ptr_tests() { + let mut v =3D 42; + let mut u =3D 43; + let x =3D Atomic::new(&raw mut v); + + assert_eq!(x.load(Acquire), &raw mut v); + assert_eq!(x.cmpxchg(&raw mut u, &raw mut u, Relaxed), Err(&raw mu= t v)); + assert_eq!(x.cmpxchg(&raw mut v, &raw mut u, Relaxed), Ok(&raw mut= v)); + assert_eq!(x.load(Relaxed), &raw mut u); + } } --=20 2.51.0 From nobody Sun Feb 8 08:27:45 2026 Received: from mail-qk1-f194.google.com (mail-qk1-f194.google.com [209.85.222.194]) (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 0EA92313E0D for ; Sat, 17 Jan 2026 12:23:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652597; cv=none; b=KkRoxBO23kRnuANYkRZYpXY5WGcuwpSvsSvcr6bMHo2unfVkyHjsAVlHQgGGQyIOkX9E1FEieih2dfqrTm6REXN+bQ2C61teGcZrho51mfuExqnri4oZKPGniReF5Yayngp0aHG871iDjAofa5VNhmYsW5LoJanpr6nqWGkcc6E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768652597; c=relaxed/simple; bh=V5rA4Ne6UgNwYNUm7jyPdhZ3jQMnmk3tK3I6DZnhgU8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ML0VYdfglS6RPMfkwIw4zODRLZovODI3bCCrZbzKJavqN6YlQ29ZBcs8plv0ZwRubtYVe1JnpvQKBRxU/W9h/zLwjifUJq/i64EDvmtpy+/V+lOKSfogQsDI+8y4M3J29IwH6VXdyAkt06jOIUp3KWJUodH9dmSQsgtn9WPIrUQ= 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=Ykh+AuQY; arc=none smtp.client-ip=209.85.222.194 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="Ykh+AuQY" Received: by mail-qk1-f194.google.com with SMTP id af79cd13be357-8c538d17816so387383985a.0 for ; Sat, 17 Jan 2026 04:23:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768652589; x=1769257389; 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=i1mr2kwBbNejDsJIhqZMH0e65CT0fSdHoMoFoGTkVbU=; b=Ykh+AuQYH/1v8qwXPhfet0kfxhZGjFyQwM7E3mlxYlSLVinBGPB445IrHiQU2hW9VH evWkgDfsQPq4YwwQzBF+8c1hpG6CD8LJ99VXcssN74Fw7PsYxpZSgnWkXnddDzKkKKZL M/PFOKxTKnPzmvI/EqlQS8WRWKsg4una2x+ImRtWXk7b21R9gAg1uLugf92xlZp9NTjs 48wZM1FlwceDIt+oc+NR2EV5J7uyT8Gt6+J0z9bosNqZLRDyTEp3vZHOKioPacmotZp7 F7p5b9eQkd3HpBLKsmBKv9Q9+JWSCRAaPL3z7a4pcTkNgmTX5HZh75pc/TALivSFhtxH k5cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768652589; x=1769257389; 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=i1mr2kwBbNejDsJIhqZMH0e65CT0fSdHoMoFoGTkVbU=; b=ow7tzHQ2Ta7ItKXNY9ZUOfOYVrMQ3qlcFfvdIMNNzZAso+kPVItrF20CSdHoMK24+/ uJZeJY45OwtJbOQfiz3NHqW5Au94lmJRI0gejXyzd63tei221xR6Vd3qlVpnpQrX7lkJ hWqZSbwTVPUAeEVLuIBQBnCyf4EdNzLELAutZomXhZFWHdBMY/3l/uQA6DEHtXNr93jQ bO8Rofb5nMPSgskquone77We6CMTvBkVYFYaeWEpILnKndA9qtLD57etqXTNrfiPoc5m bHLG0eJ37MeNePjyKaXxuqqPrw/RWH71rGjhUt/PZsJgiH0va/I4s23xXF3YgyCuoSZa bm6Q== X-Forwarded-Encrypted: i=1; AJvYcCW5RYrWIYNv0MOu/2Hu1bGtQaMAAihk91h5LoJlUlJlU2MegTj1mWkmWnq77nIdD1lcxKAoRo368rQ1U6A=@vger.kernel.org X-Gm-Message-State: AOJu0YwK/6cDp0dUyc/Wn77e9bOfDha1xFQUqd07C2WuD3Ct/ZE4AaOg Z0B6Cfq63iobrVnzXohI65JlfeJMr/B9RQ8jYX9KgHIDhhgLoW7lor2o X-Gm-Gg: AY/fxX6ijCZ1bHMVUOyHnEJQJFg+YJ0f5G2hz6j74/JOM6JXYleCXeYB3W97Um2aozF f4TlpjGBDnN6r5TaWliSp/YwkekoIiZJBBMnqDS5lnEGR5hsF7Q6gJqm+pDiiLMsvDZaAjIYJvD ADcx4BwBL6EwIX6/GOlNb3DksKC/W8YU3KlQtiA5Ieyin3bWHj2+Mh/aqfTDPcTbAbTfvDtA4E+ TQiMctNW9Ipj0ETjBx0OkjKPOqUj1CUdorLE4zGw5myITiPEXxnktCHOxPVxuQCP6PHuPu/+pNM B9ETFaV++GwCm5p6NxVcIexsJGxh0X1GLmXB3qiHzv03tPTmMX65I2Kvl7mbV3F2dz48bOG82Jf EwOALxetIcOkKRfONsrW5QhdGmzGKJWTQeQvBgigY61gaQ+aVlth7w+ZYHnsN7UNU7qF4kk7KVj u1eoTl5cKhZ/+hx7RftxBmUyrnjiP0RW9lYqs1gduAoHdMiKSkZnUEaXyLcvt/vdBBjW4iAhSLP PNAyzEzwFJlsIM= X-Received: by 2002:a05:620a:1725:b0:8b3:3d62:67f5 with SMTP id af79cd13be357-8c6a68d359bmr808856085a.11.1768652588564; Sat, 17 Jan 2026 04:23:08 -0800 (PST) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8c6a72602fesm429329085a.41.2026.01.17.04.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 04:23:08 -0800 (PST) Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfauth.phl.internal (Postfix) with ESMTP id 70CDEF4007E; Sat, 17 Jan 2026 07:23:07 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Sat, 17 Jan 2026 07:23:07 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddufedukedtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpedugfevheekvdefveevleffheevgedufeettdejtdevheefjeduhefgleelffdv ieenucffohhmrghinheptghrrghtvghsrdhiohdpiihulhhiphgthhgrthdrtghomhdpgh hithhhuhgsrdgtohhmpdhkrghnghhrvghjohhsrdgtohhmpdhruhhsthdqlhgrnhhgrdho rhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsg hoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieeg qddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigi hmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedviedpmhhouggvpehsmhhtphhouhhtpdhr tghpthhtoheprhhushhtqdhfohhrqdhlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorh hgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdho rhhgpdhrtghpthhtoheprhgtuhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtth hopehojhgvuggrsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegsohhquhhnrdhfvghn ghesghhmrghilhdrtghomhdprhgtphhtthhopehgrghrhiesghgrrhihghhuohdrnhgvth dprhgtphhtthhopegsjhhorhhnfegpghhhsehprhhothhonhhmrghilhdrtghomhdprhgt phhtthhopehlohhsshhinheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprgdrhhhinh gusghorhhgsehkvghrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 17 Jan 2026 07:23:06 -0500 (EST) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Cc: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Will Deacon , Peter Zijlstra , Mark Rutland , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , FUJITA Tomonori Subject: [PATCH 5/5] rust: sync: rcu: Add RCU protected pointer Date: Sat, 17 Jan 2026 20:22:43 +0800 Message-ID: <20260117122243.24404-6-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260117122243.24404-1-boqun.feng@gmail.com> References: <20260117122243.24404-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" RCU protected pointers are an atomic pointer that can be loaded and dereferenced by mulitple RCU readers, but only one updater/writer can change the value (following a read-copy-update pattern usually). This is useful in the case where data is read-mostly. The rationale of this patch is to provide a proof of concept on how RCU should be exposed to the Rust world, and it also serves as an example for atomic usage. Similar mechanisms like ArcSwap [1] are already widely used. Provide a `Rcu

` type with an atomic pointer implementation. `P` has to be a `ForeignOwnable`, which means the ownership of a object can be represented by a pointer-size value. `Rcu::dereference()` requires a RCU Guard, which means dereferencing is only valid under RCU read lock protection. `Rcu::copy_update()` is the operation for updaters, it requries a `Pin<&mut Self>` for exclusive accesses, since RCU updaters are normally exclusive with each other. A lot of RCU functionalities including asynchronously free (call_rcu() and kfree_rcu()) are still missing, and will be the future work. Also, we still need language changes like field projection [2] to provide better ergonomic. Acknowledgment: this work is based on a lot of productive discussions and hard work from others, these are the ones I can remember (sorry if I forgot your contribution): * Wedson started the work on RCU field projection and Benno followed it up and had been working on it as a more general language feature. Also, Gary's field-projection repo [3] has been used as an example for related discussions. * During Kangrejos 2023 [4], Gary, Benno and Alice provided a lot of feedbacks on the talk from Paul and me: "If you want to use RCU in Rust for Linux kernel..." * During a recent discussion among Benno, Paul and me, Benno suggested using `Pin<&mut>` to guarantee the exclusive access on updater operations. Link: https://crates.io/crates/arc-swap [1] Link: https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/F= ield.20Projections/near/474648059 [2] Link: https://github.com/nbdd0121/field-projection [3] Link: https://kangrejos.com/2023 [4] Signed-off-by: Boqun Feng --- rust/kernel/sync/rcu.rs | 326 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 325 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/rcu.rs b/rust/kernel/sync/rcu.rs index a32bef6e490b..28bbccaa2e5e 100644 --- a/rust/kernel/sync/rcu.rs +++ b/rust/kernel/sync/rcu.rs @@ -4,7 +4,23 @@ //! //! C header: [`include/linux/rcupdate.h`](srctree/include/linux/rcupdate.= h) =20 -use crate::{bindings, types::NotThreadSafe}; +use crate::bindings; +use crate::{ + sync::atomic::{ + Atomic, + Relaxed, + Release, // + }, + types::{ + ForeignOwnable, + NotThreadSafe, // + }, +}; +use core::{ + marker::PhantomData, + pin::Pin, + ptr::NonNull, // +}; =20 /// Evidence that the RCU read side lock is held on the current thread/CPU. /// @@ -50,3 +66,311 @@ fn drop(&mut self) { pub fn read_lock() -> Guard { Guard::new() } + +use crate::types::Opaque; + +/// A temporary `UnsafePinned` [1] that provides a way to opt-out typical = alias rules for mutable +/// references. +/// +/// # Invariants +/// +/// `self.0` is always properly initialized. +/// +/// [1]: https://doc.rust-lang.org/std/pin/struct.UnsafePinned.html +struct UnsafePinned(Opaque); + +impl UnsafePinned { + const fn new(value: T) -> Self { + // INVARIANTS: `value` is initialized. + Self(Opaque::new(value)) + } + + const fn get(&self) -> *mut T { + self.0.get() + } +} + +// SAFETY: `UnsafePinned` is safe to transfer between execution contexts a= s long as `T` is `Send`. +unsafe impl Send for UnsafePinned {} +// SAFETY: `UnsafePinned` is safe to shared between execution contexts as = long as `T` is `Sync`. +unsafe impl Sync for UnsafePinned {} + +/// An RCU protected pointer, the pointed object is protected by RCU. +/// +/// # Invariants +/// +/// Either the pointer is null, or it points to a return value of +/// [`ForeignOwnable::into_foreign()`] and the atomic variable exclusively= owns the pointer. +pub struct Rcu( + UnsafePinned>, + PhantomData

, +); + +/// A pointer that has been unpublished, but hasn't waited for a grace per= iod yet. +/// +/// The pointed object may still have an existing RCU reader. Therefore a = grace period is needed to +/// free the object. +/// +/// # Invariants +/// +/// The pointer has to be a return value of [`ForeignOwnable::into_foreign= `] and [`Self`] +/// exclusively owns the pointer. +pub struct RcuOld(NonNull, PhantomD= ata

); + +impl Drop for RcuOld

{ + fn drop(&mut self) { + // SAFETY: As long as called in a sleepable context, which should = be checked by klint, + // `synchronize_rcu()` is safe to call. + unsafe { + bindings::synchronize_rcu(); + } + + // SAFETY: `self.0` is a return value of `P::into_foreign()`, so i= t's safe to call + // `from_foreign()` on it. Plus, the above `synchronize_rcu()` gua= rantees no existing + // `ForeignOwnable::borrow()` anymore. + let p: P =3D unsafe { P::from_foreign(self.0.as_ptr()) }; + drop(p); + } +} + +impl Rcu

{ + /// Creates a new RCU pointer. + pub fn new(p: P) -> Self { + // INVARIANTS: The return value of `p.into_foreign()` is directly = stored in the atomic + // variable. + Self( + UnsafePinned::new(Atomic::new(p.into_foreign())), + PhantomData, + ) + } + + fn as_atomic(&self) -> &Atomic<*mut crate::ffi::c_void> { + // SAFETY: Per type invariants of `UnsafePinned`, `self.0.get()` p= oints to an initialized + // `&Atomic`. + unsafe { &*self.0.get() } + } + + fn as_atomic_mut_pinned(self: Pin<&mut Self>) -> &Atomic<*mut crate::f= fi::c_void> { + self.into_ref().get_ref().as_atomic() + } + + /// Dereferences the protected object. + /// + /// Returns `Some(b)`, where `b` is a reference-like borrowed type, if= the pointer is not null, + /// otherwise returns `None`. + /// + /// # Examples + /// + /// ```rust + /// # use kernel::alloc::{flags, KBox}; + /// use kernel::sync::rcu::{self, Rcu}; + /// + /// let x =3D Rcu::new(KBox::new(100i32, flags::GFP_KERNEL)?); + /// + /// let g =3D rcu::read_lock(); + /// // Read in under RCU read lock protection. + /// let v =3D x.dereference(&g); + /// + /// assert_eq!(v, Some(&100i32)); + /// + /// # Ok::<(), Error>(()) + /// ``` + /// + /// Note the borrowed access can outlive the reference of the [`Rcu

= `], this is because as + /// long as the RCU read lock is held, the pointed object should remai= n valid. + /// + /// In the following case, the main thread is responsible for the owne= rship of `shared`, i.e. it + /// will drop it eventually, and a work item can temporarily access th= e `shared` via `cloned`, + /// but the use of the dereferenced object doesn't depend on `cloned`'= s existence. + /// + /// ```rust + /// # use kernel::alloc::{flags, KBox}; + /// # use kernel::workqueue::system; + /// # use kernel::sync::{Arc, atomic::{Atomic, Acquire, Release}}; + /// use kernel::sync::rcu::{self, Rcu}; + /// + /// struct Config { + /// a: i32, + /// b: i32, + /// c: i32, + /// } + /// + /// let config =3D KBox::new(Config { a: 1, b: 2, c: 3 }, flags::GFP_K= ERNEL)?; + /// + /// let shared =3D Arc::new(Rcu::new(config), flags::GFP_KERNEL)?; + /// let cloned =3D shared.clone(); + /// + /// // Use atomic to simulate a special refcounting. + /// static FLAG: Atomic =3D Atomic::new(0); + /// + /// system().try_spawn(flags::GFP_KERNEL, move || { + /// let g =3D rcu::read_lock(); + /// let v =3D cloned.dereference(&g).unwrap(); + /// drop(cloned); // release reference to `shared`. + /// FLAG.store(1, Release); + /// + /// // but still need to access `v`. + /// assert_eq!(v.a, 1); + /// drop(g); + /// }); + /// + /// // Wait until `cloned` dropped. + /// while FLAG.load(Acquire) =3D=3D 0 { + /// // SAFETY: Sleep should be safe. + /// unsafe { kernel::bindings::schedule(); } + /// } + /// + /// drop(shared); + /// + /// # Ok::<(), Error>(()) + /// ``` + pub fn dereference<'rcu>(&self, _rcu_guard: &'rcu Guard) -> Option> { + // Ordering: Address dependency pairs with the `store(Release)` in= copy_update(). + let ptr =3D self.as_atomic().load(Relaxed); + + if !ptr.is_null() { + // SAFETY: + // - Since `ptr` is not null, so it has to be a return value o= f `P::into_foreign()`. + // - The returned `Borrowed<'rcu>` cannot outlive the RCU Guar= , this guarantees the + // return value will only be used under RCU read lock, and t= he RCU read lock prevents + // the pass of a grace period that the drop of `RcuOld` or `= Rcu` is waiting for, + // therefore no `from_foreign()` will be called for `ptr` as= long as `Borrowed` exists. + // + // CPU 0 CPU 1 + // =3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D + // { `x` is a reference to Rcu> } + // let g =3D rcu::read_lock(); + // + // if let Some(b) =3D x.dereference(&g) { + // // drop(g); cannot be done, since `b` is still alive. + // + // if let Some(ol= d) =3D x.replace(...) { + // // `x` is = null now. + // println!("{}", b); + // } + // drop(old): + // synchron= ize_rcu(); + // drop(g); + // // a gra= ce period passed. + // // No `B= orrowed` exists now. + // from_for= eign(...); + // } + Some(unsafe { P::borrow(ptr) }) + } else { + None + } + } + + /// Read, copy and update the pointer with new value. + /// + /// Returns `None` if the pointer's old value is null, otherwise retur= ns `Some(old)`, where old + /// is a [`RcuOld`] which can be used to free the old object eventuall= y. + /// + /// The `Pin<&mut Self>` is needed because this function needs the exc= lusive access to + /// [`Rcu

`], otherwise two `copy_update()`s may get the same old ob= ject and double free. + /// Using `Pin<&mut Self>` provides the exclusive access that C side r= equires with the type + /// system checking. + /// + /// Also this has to be `Pin` because a `&mut Self` may allow users to= `swap()` safely, that + /// will break the atomicity. A [`Rcu

`] should be structurally pinn= ed in the struct that + /// contains it. + /// + /// Note that `Pin<&mut Self>` cannot assume noalias on `self.0` here = because of `self.0` is an + /// [`UnsafePinned`]. + /// + /// [`UnsafePinned`]: https://doc.rust-lang.org/std/pin/struct.UnsafeP= inned.html + pub fn copy_update(self: Pin<&mut Self>, f: F) -> Option> + where + F: FnOnce(Option>) -> Option

, + { + let inner =3D self.as_atomic_mut_pinned(); + + // step 1: COPY, or more generally, initializing `new` based on `o= ld`. + // Ordering: Address dependency pairs with the `store(Release)` in= copy_update(). + let old_ptr =3D NonNull::new(inner.load(Relaxed)); + + let old =3D old_ptr.map(|nonnull| { + // SAFETY: Per type invariants `old_ptr` has to be a value ret= urn by a previous + // `into_foreign()`, and the exclusive reference `self` guaran= tees that `from_foreign()` + // has not been called. + unsafe { P::borrow(nonnull.as_ptr()) } + }); + + let new =3D f(old); + + // step 2: UPDATE. + if let Some(new) =3D new { + let new_ptr =3D new.into_foreign(); + // Ordering: Pairs with the address dependency in `dereference= ()` and + // `copy_update()`. + // INVARIANTS: `new.into_foreign()` is directly store into the= atomic variable. + inner.store(new_ptr, Release); + } else { + // Ordering: Setting to a null pointer doesn't need to be Rele= ase. + // INVARIANTS: The atomic variable is set to be null. + inner.store(core::ptr::null_mut(), Relaxed); + } + + // INVARIANTS: The exclusive reference guarantess that the ownersh= ip of a previous + // `into_foreign()` transferred to the `RcuOld`. + Some(RcuOld(old_ptr?, PhantomData)) + } + + /// Replaces the pointer with new value. + /// + /// Returns `None` if the pointer's old value is null, otherwise retur= ns `Some(old)`, where old + /// is a [`RcuOld`] which can be used to free the old object eventuall= y. + /// + /// # Examples + /// + /// ```rust + /// use core::pin::pin; + /// # use kernel::alloc::{flags, KBox}; + /// use kernel::sync::rcu::{self, Rcu}; + /// + /// let mut x =3D pin!(Rcu::new(KBox::new(100i32, flags::GFP_KERNEL)?)= ); + /// let q =3D KBox::new(101i32, flags::GFP_KERNEL)?; + /// + /// // Read in under RCU read lock protection. + /// let g =3D rcu::read_lock(); + /// let v =3D x.dereference(&g); + /// + /// // Replace with a new object. + /// let old =3D x.as_mut().replace(q); + /// + /// assert!(old.is_some()); + /// + /// // `v` should still read the old value. + /// assert_eq!(v, Some(&100i32)); + /// + /// // New readers should get the new value. + /// assert_eq!(x.dereference(&g), Some(&101i32)); + /// + /// drop(g); + /// + /// // Can free the object outside the read-side critical section. + /// drop(old); + /// # Ok::<(), Error>(()) + /// ``` + pub fn replace(self: Pin<&mut Self>, new: P) -> Option> { + self.copy_update(|_| Some(new)) + } +} + +impl Drop for Rcu

{ + fn drop(&mut self) { + let ptr =3D self.as_atomic().load(Relaxed); + if !ptr.is_null() { + // SAFETY: As long as called in a sleepable context, which sho= uld be checked by klint, + // `synchronize_rcu()` is safe to call. + unsafe { + bindings::synchronize_rcu(); + } + + // SAFETY: `self.0` is a return value of `P::into_foreign()`, = so it's safe to call + // `from_foreign()` on it. Plus, the above `synchronize_rcu()`= guarantees no existing + // `ForeignOwnable::borrow()` anymore. + drop(unsafe { P::from_foreign(ptr) }); + } + } +} --=20 2.51.0