From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 681D4C27C6E for ; Fri, 14 Jun 2024 20:13:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=2bHm/aaPtqr+seGxV2S60l3XmS90OeMKM4QcbWBal74=; b=WGQ2klEVBGJJtyehIDpmnK/ZeL hqnXrA48NuemjvIHz1QJL0NyLI7GT6sFtwNZAKGbNhpqx+IIe2wkw2wr9PkYY16flWpnFvWJrZvvR eVxS2zTNTGDbmv5ZPxdpKdL5QLWj+tWWzDfLFP8aGl0IQqLtZA4QqdJjzEaPlL1IkKE84SPO07TIS 6KUF4awyogHPnMLEybBhad9KdeaHS5a/VHp5t5jmwDRfXmUG6p1q2KaXAEuWAqnZpzaSkQsfC7UKm 6XGH1W0JMNY8uBJtZbVf7NBFIoIeLtw0AIT6qZ8eBO+MjG2waMW+hEE2fbjcrS9sqDcABT+v/GToZ KZ1Ta7TA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sIDIT-00000003xhs-2FQO; Fri, 14 Jun 2024 20:13:17 +0000 Received: from mail-oi1-x229.google.com ([2607:f8b0:4864:20::229]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sIDIQ-00000003xgB-12Mi for linux-arm-kernel@lists.infradead.org; Fri, 14 Jun 2024 20:13:16 +0000 Received: by mail-oi1-x229.google.com with SMTP id 5614622812f47-3c9b94951cfso1450137b6e.3 for ; Fri, 14 Jun 2024 13:13:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718395993; x=1719000793; darn=lists.infradead.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=2bHm/aaPtqr+seGxV2S60l3XmS90OeMKM4QcbWBal74=; b=WvsimA7YiJiEGY9adoqquTzyK8nBGubuNm8Jz+vNI8Sywf2/mbD/r8G2ZPKI9jODj+ jXNGiZzsvxzbyf57SEEBAwgYrql6w31NE1cyvROGd5PtfRJf8hSs6FOPHLaUXrhcI8bY sAW2Ob6jsgmcvDEZD8T4E5E3AKBwdiycWpPIxUEhQ5ZJE/X/a4KVLHQsZuA+T2IUjGna cRzhjDoQyw1wCaV/q5TifNYde+ILW2Q4xv/VnklPVI++0zKss3fHiizC3V8b7VOuHbVO tbS5fEi/tfz19mLbV6+MpMyCwAgQAqgmfxedWJJGo+wNlDWPDiNCKrGWFPFO+zwCpK8W Yoig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718395993; x=1719000793; 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=2bHm/aaPtqr+seGxV2S60l3XmS90OeMKM4QcbWBal74=; b=Y6JxRIS71XoQP1AxzjfGpFwv9hixabUHx8748U+FNIs1rGa1qBqqFwL4R11gfQ6GVS xkxKgSy2FLpNifFaXLvN6nACdfFeyJqO8VyPEcIqu+N8cRZu1bVNywfezPnaMvsyqbvf zizVkvxrZnKJ7lGrwDKRJZDBrcYDoUDbnfUneay7h6MpOHiP5JFXYbzVwzNGd24X8yxs wbbe9y8MuPsCErav5GZOEXx3QOHbxDG3uEFq3+YVgxevio9iXLR2Xdp0LKymoUaapl7d NM6Sfgshl257bf/e5A9i1b/4eAEQ2ZDxpYTBmzJYLQbvIHm26c/5IV8u4OqRqM4dR1oZ P/Uw== X-Forwarded-Encrypted: i=1; AJvYcCVYyFtsZSIf7T5VS9NpO67JSYz43urf3nteg4Zpt/yh1VNiUAwe6ZyEiwFRbx48OQA8WwzdjDVMvNZuOl6LvRHANIl6bUkDVVAa2elK+lsiiLg0vyQ= X-Gm-Message-State: AOJu0YzJuApoIhfKsey71fOpm5totNhYr2XmpnsiSXrVOuUoLdqvOuBv iTzQHr3eBSBYnsTe6j0B3ZsBvg0wsZuymjXev9XL9Yj4PJWT6lCn X-Google-Smtp-Source: AGHT+IGQ/7jt+p7Vy+rl8KY42hsPlfY6qspnV6dIbABmXy17cfCC4w/P5jUh7xtu725fkfqJwGMFrQ== X-Received: by 2002:a05:6808:e8e:b0:3d2:1ce7:43da with SMTP id 5614622812f47-3d24e9bc08fmr3473362b6e.49.1718395993043; Fri, 14 Jun 2024 13:13:13 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id af79cd13be357-798aacb1e6dsm176551585a.13.2024.06.14.13.13.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 13:13:12 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfauth.nyi.internal (Postfix) with ESMTP id 5DA3B120006C; Fri, 14 Jun 2024 16:13:11 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 14 Jun 2024 16:13:11 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfeduledgudegiecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepffdtiefhieegtddvueeuffeiteevtdegjeeuhffhgfdugfefgefgfedt ieeghedvnecuffhomhgrihhnpehgihhthhhusgdrtghomhenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhht hhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquh hnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 14 Jun 2024 16:13:09 -0400 (EDT) Date: Fri, 14 Jun 2024 13:13:06 -0700 From: Boqun Feng To: Mark Rutland Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Nathan Chancellor , Nick Desaulniers , kent.overstreet@gmail.com, Greg Kroah-Hartman , elver@google.com, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Catalin Marinas , torvalds@linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, Trevor Gross , dakr@redhat.com Subject: Re: [RFC 1/2] rust: Introduce atomic API helpers Message-ID: References: <20240612223025.1158537-1-boqun.feng@gmail.com> <20240612223025.1158537-2-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240614_131314_536778_54169003 X-CRM114-Status: GOOD ( 27.25 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Jun 14, 2024 at 11:31:33AM +0100, Mark Rutland wrote: > On Wed, Jun 12, 2024 at 03:30:24PM -0700, Boqun Feng wrote: > > In order to support LKMM atomics in Rust, add rust_helper_* for atomic > > APIs. These helpers ensure the implementation of LKMM atomics in Rust is > > the same as in C. This could save the maintenance burden of having two > > similar atomic implementations in asm. > > > > Originally-by: Mark Rutland > > Signed-off-by: Boqun Feng > > FWIW, I'm happy with the concept; I have a couple of minor comments ;-) > below. > > > --- > > rust/atomic_helpers.h | 1035 +++++++++++++++++++++ > > rust/helpers.c | 2 + > > scripts/atomic/gen-atomics.sh | 1 + > > scripts/atomic/gen-rust-atomic-helpers.sh | 64 ++ > > 4 files changed, 1102 insertions(+) > > create mode 100644 rust/atomic_helpers.h > > create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh > > [...] > > > +#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, raw, arg...) > > +gen_proto_order_variant() > > +{ > > + local meta="$1"; shift > > + local pfx="$1"; shift > > + local name="$1"; shift > > + local sfx="$1"; shift > > + local order="$1"; shift > > + local atomic="$1"; shift > > + local int="$1"; shift > > + local raw="$1"; shift > > + local attrs="${raw:+noinstr }" > > You removed the 'raw_' atomic generation below, so you can drop the > 'raw' parameter and the 'attrs' variable (both here and in the > template)... > > > + local atomicname="${raw}${atomic}_${pfx}${name}${sfx}${order}" > > + > > + local ret="$(gen_ret_type "${meta}" "${int}")" > > + local params="$(gen_params "${int}" "${atomic}" "$@")" > > + local args="$(gen_args "$@")" > > + local retstmt="$(gen_ret_stmt "${meta}")" > > + > > +cat < > +__rust_helper ${attrs}${ret} > > ... e.g. you can remove '${attrs}' here. > > [...] > > > +grep '^[a-z]' "$1" | while read name meta args; do > > + gen_proto "${meta}" "${name}" "atomic" "int" "" ${args} > > +done > > + > > +grep '^[a-z]' "$1" | while read name meta args; do > > + gen_proto "${meta}" "${name}" "atomic64" "s64" "" ${args} > > +done > > With the 'raw' parameter removed above, the '""' argument can be > dropped. > Fix all above locally. > Any reason to not have the atomic_long_*() API? It seems like an odd > ommision. > See my reply to Peter, but there's also a more technical reason: right now, we use core::ffi::c_long for bindgen to translate C's long. But instead of `isize` (Rust's version of pointer-sized integer) core::ffi::c_long is `i64` on 64bit and `i32` on 32bit. So right now, atomic_long_add_return()'s helper signature would be (on 64bit): extern "C" { #[link_name="rust_helper_atomic_long_add_return"] pub fn atomic_long_add_return( i: i64, v: *mut atomic_long_t, ) -> i64; } and I would need to cast the types in order to put it in an `AtomicIsize` method: impl AtomicIsize { pub fn add_return(&self, i: isize) -> isize { unsafe { return atomic_long_add_return(i as i64, self.0.get()) as isize } } } see these two `as`s. I want to avoid handling them in a bash script ;-) A better solution would be what Gary has: https://github.com/nbdd0121/linux/commit/b604a43db56f149a90084fa8aed7988a8066894b , which defines kernel's own ffi types and teach bindgen to pick the right type for c_long. If we prefer script code generating to Rust macro code generating, I will work with Gary on getting that done first and then add atomic_long_t support unless we feel atomic_long_t support is urgent. Regards, Boqun > Mark.