From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (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 DA513134CD0 for ; Tue, 16 Apr 2024 18:12:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713291152; cv=none; b=c3eJCs/led+azkojJ8/VqUyapavodvKfJCasa9dTvcqzfTqj3/yOfDtOaIoDlrAxDJ5NYlBYmEuadnYWoVklGI6RI1oLZxb3a7HzcSJ56v1f/Y5z4it1nYlLG4nWUer2GXp82BGBUyhhId2MbAK3ZggUiC1fXT/4z/4AGbfeN/c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713291152; c=relaxed/simple; bh=o/Pz18+RKaep+hg8mtEP9kW2B/vTbdfUmQG+bVJKW20=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=u7XnZITyRgd9HE+0iw4m6Su+ZTMrojhQ31VNW4+U6yTMw11ZkPwjq4R1kYIxgAjICBrhwhBiH9nAKR3krDjHOktkjAyBtXsm58R01ELYaANOW5Yt43ZdqFdI2+R6gOZHW2ax2anBwGAqhTh/v5dy5SCdlVMScKUAAgkJ8yFTMWY= 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=CdmzNivi; arc=none smtp.client-ip=209.85.210.46 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="CdmzNivi" Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-6ea2ac4607aso2575267a34.3 for ; Tue, 16 Apr 2024 11:12:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713291149; x=1713895949; darn=lists.linux.dev; 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=iKA01nHZWWeCUMwezGVKQPv6dB8WRSswCOMzscXKYBI=; b=CdmzNiviLLN000QM5P0A0PpUfiscnBOM6yx13vQU41hmt1fBM4sEvkHF3MpvCByzbB Cid4XgKdzUmsVvNxyHpJLPW7gdW9uE+IHprkfN9Mi2feKoZAnlhdJmTd7rOTuiSDbcJI +IfyZ4EjBboh/d8g+ZR8Gc0BPMEP5Re7em58aEgQnmT6BPhx4segw/bJXKWbITZqw5+9 7S4BPoLQNpNyMBoIBNli9iaeavUWMOhW7h30z7Fo8AFm6n9JQhqL1d1VPvOfVgl2Vegh ONLbJjkdzrhf6N8N32OMNb4JMCushzAWHZQP2hNW5ODQW+dhf0v7oK13/3cfahEc4Q7D Xh9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713291149; x=1713895949; 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=iKA01nHZWWeCUMwezGVKQPv6dB8WRSswCOMzscXKYBI=; b=Wk8KhHb4V3Cj3LpQXDbEYTcSfjMgAacJ0QDA10/7FJ3257s4w3g6HSxUT1J1NlyhDs vz3SlcMJ7+VMIK+n5O+WDH1x6KtYNQZcOCw0pNFyMW6ogshQ1Qq03NnBX1Rs42FoI7ty uRzqGigxdkcngeQuwSXn+HcTu0OE0zuQIWn4Hiinhuwqq+q0ysVeQlM5IpD4xssouCos myHhpCaxa4fiyEiim25rRhHmBn4hjGlgfDwxnlNHvFzwbogajhRLD9C5c2u1fzPipBrr 6wGP1Cf3Y2/17VYEBVguKoluaGSGxjqBP1E3VpM2L9DPok1cQTFfsq43hJ7d1fHUBmJQ EtcQ== X-Forwarded-Encrypted: i=1; AJvYcCVGtjJCxD2+6GhJa+fHp53ipGEpBdYuR9LbDBVBz5Hjsaj4YBaJnQs6EE8F5FdNS3SBq5L5FAVtbKFH/uRddRMOufvarQ== X-Gm-Message-State: AOJu0YwLTrNoWWk6Ruhra+dGm4ooXEmL+RSzHstc8XZ5TIgRcUccgulH +cyaSdtIylf8+vUUdgaSTokOC9joIoTVnft3hApKlyfyaKDn3VUE X-Google-Smtp-Source: AGHT+IEsvpJ9PHMdwG/Acyze2YUbjWscWF8ldU9oec4TpSg6OiR2NDXpJYNLstl4DLb3bNX7ulcKpA== X-Received: by 2002:a9d:4d95:0:b0:6eb:7bc3:12a5 with SMTP id u21-20020a9d4d95000000b006eb7bc312a5mr7305200otk.32.1713291148792; Tue, 16 Apr 2024 11:12:28 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id c25-20020a05620a135900b0078edf6393edsm3676965qkl.73.2024.04.16.11.12.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 11:12:28 -0700 (PDT) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfauth.nyi.internal (Postfix) with ESMTP id 028C71200077; Tue, 16 Apr 2024 14:12:27 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Tue, 16 Apr 2024 14:12:27 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudejiedgiedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeethfejhfekjeffueegieejudegjeetveeuhfelhfevvdfgfeekkeevkeel veekgfenucffohhmrghinhepghhithhhuhgsrdgtohhmpdhgnhhurdhorhhgnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgv shhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehhe ehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 16 Apr 2024 14:12:26 -0400 (EDT) Date: Tue, 16 Apr 2024 11:12:07 -0700 From: Boqun Feng To: Peter Zijlstra Cc: Mark Rutland , 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 , "Bj\"orn Roy Baron" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Alan Stern , Andrea Parri , Will Deacon , 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 Subject: Re: [WIP 0/3] Memory model and atomic API in Rust Message-ID: References: <20240322233838.868874-1-boqun.feng@gmail.com> <20240409105015.GC21779@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240409105015.GC21779@noisy.programming.kicks-ass.net> On Tue, Apr 09, 2024 at 12:50:15PM +0200, Peter Zijlstra wrote: > On Mon, Mar 25, 2024 at 01:59:55PM -0700, Boqun Feng wrote: > > On Mon, Mar 25, 2024 at 10:44:45AM +0000, Mark Rutland wrote: > > [...] > > > > > > > > * I choose to re-implement atomics in Rust `asm` because we are still > > > > figuring out how we can make it easy and maintainable for Rust to call > > > > a C function _inlinely_ (Gary makes some progress [2]). Otherwise, > > > > atomic primitives would be function calls, and that can be performance > > > > bottleneck in a few cases. > > > > > > I don't think we want to maintain two copies of each architecture's atomics. > > > This gets painful very quickly (e.g. as arm64's atomics get patched between > > > LL/SC and LSE forms). > > > > > > > No argument here ;-) > > Didn't we talk about bindgen being able to convert inline C functions > into equivalent inline Rust functions? ISTR that getting stuck on Rust Yes, we did. > not having a useful inline asm. > Mostly two features were missing: 1) asm goto and 2) memory operands, #1 gets implemented[1] by Gary, and should be available in Rust 1.78 (plan to release at May 2, 2024); For #2, my understanding is that arch-specific effort is needed (since different architectures may have different contraints on memory operands), I haven't yet found anyone is working on this. (background explanation for broader audience: in GCC's inline asm, you can specify an memory location, other than a register location, as an input or output of an asm block's operand[2], but current Rust inline asm doesn't provide this functionality, by default, without option "pure", "nomem", etc, every asm block in Rust can be thought as a C asm block with "memory" clobber) That being said, if you look at the link I shared or this gist from Gary: https://gist.github.com/nbdd0121/d4bf7dd7f9b6d6b50fa18b1092f45a3c there is another way (yeah, we probably also have discussed this previously), basically what it does is compiling the functions in a C file as LLVM IR, so that Rust can call these functions at LLVM IR level. This in theory is doing some local LTO, and I've tested that it works for asm blocks. We still need to tweak our build system to make this work, but should it work, it would mean that Rust can call a C function in a pretty efficient way. > But fixing all that in a hurry seems like the much saner path forward. So a sane plan to me is wiring our atomics into Rust functions via what Mark has (i.e. starting off as FFI calls), then we can switch to the "local LTO" approach when it's ready. In case that "local LTO" needs more work and we do have performance need, we can always either 1) manually implement some primitives in Rust asm, or 2) look into how bindgen or other tools can translate simple C functions (asm blocks) into Rust. Regards, Boqun [1]: https://github.com/rust-lang/rust/pull/119365 [2]: https://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html#index-m-in-constraint