Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2725725lqp; Mon, 25 Mar 2024 07:38:59 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCX7WLwvaxOyUzUMpWeNeT++jw9eCfKxYA+YFQ3TMBRtP10m/IHQPpu5dKTqQJNCO4uRT9C7xC/NiPexUETu6qqj7SDhFf25em+HdszJCQ== X-Google-Smtp-Source: AGHT+IEGAqe6dvvcCaFv/ejnWut321CoXVDv1NzNg7/TqX/iCdOIvT0MbxKdmtFeymbH8AglIhHO X-Received: by 2002:a50:d786:0:b0:567:2cf:1ecc with SMTP id w6-20020a50d786000000b0056702cf1eccmr4823938edi.30.1711377539525; Mon, 25 Mar 2024 07:38:59 -0700 (PDT) Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id r12-20020a056402234c00b0056bfa6c1ddbsi2308399eda.645.2024.03.25.07.38.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 07:38:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-116879-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-116879-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116879-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 3AFFC1F25DD8 for ; Mon, 25 Mar 2024 14:38:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7DF5C1C41C2; Mon, 25 Mar 2024 11:29:51 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CBC0017D23D; Mon, 25 Mar 2024 10:44:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711363501; cv=none; b=lol9DxDbvPP9K2/71SQ/SYgMCxeAmgN6lujHBpZzzpSdwiXXwNxPREV6yNZqaXLWpaxWjrWy34ee5cJ8cxaqzoSffbrjP873+ruU/OyhBpfYGE01q0NWlP/dC7lq/sy6r5k+41zMhQL+tepVhUwDHztnWOakWV1cA08FQXDPFds= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711363501; c=relaxed/simple; bh=HVlViT7fg37ArzDR5okosyV0TQroP4C7gOqT9V4q2f0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=LJVgG1cja4QnpRC+43Tf3ug6jce0R7PXmitvWr8upEsksCGCbGoAMRLoH4HTE92+Ct0wLJ40FoMz4Nn6rTOG0AHm5FTsg9jCaOyLcPuG67SI9sr7DBzuMXUxYcgh6a3O4OmtYpjBOAGYPMXy19Z8pUNo6PYKz6RzrBhlyeJpqKc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C15E01FB; Mon, 25 Mar 2024 03:45:31 -0700 (PDT) Received: from FVFF77S0Q05N (unknown [10.57.16.150]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AFF8B3F67D; Mon, 25 Mar 2024 03:44:51 -0700 (PDT) Date: Mon, 25 Mar 2024 10:44:45 +0000 From: Mark Rutland To: Boqun Feng 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 , =?us-ascii?Q?Bj=22orn?= 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 Subject: Re: [WIP 0/3] Memory model and atomic API in Rust Message-ID: References: <20240322233838.868874-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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240322233838.868874-1-boqun.feng@gmail.com> On Fri, Mar 22, 2024 at 04:38:35PM -0700, Boqun Feng wrote: > Hi, > > Since I see more and more Rust code is comming in, I feel like this > should be sent sooner rather than later, so here is a WIP to open the > discussion and get feedback. > > One of the most important questions we need to answer is: which > memory (ordering) model we should use when developing Rust in Linux > kernel, given Rust has its own memory ordering model[1]. I had some > discussion with Rust language community to understand their position > on this: > > https://github.com/rust-lang/unsafe-code-guidelines/issues/348#issuecomment-1218407557 > https://github.com/rust-lang/unsafe-code-guidelines/issues/476#issue-2001382992 > > My takeaway from these discussions, along with other offline discussion > is that supporting two memory models is challenging for both correctness > reasoning (some one needs to provide a model) and implementation (one > model needs to be aware of the other model). So that's not wise to do > (at least at the beginning). So the most reasonable option to me is: > > we only use LKMM for Rust code in kernel (i.e. avoid using > Rust's own atomic). > > Because kernel developers are more familiar with LKMM and when Rust code > interacts with C code, it has to use the model that C code uses. I think that makes sense; if nothing else it's consistent with how we handle the C atomics today. > And this patchset is the result of that option. I introduced an atomic > library to wrap and implement LKMM atomics (of course, given it's a WIP, > so it's unfinished). Things to notice: > > * I know I could use Rust macro to generate the whole set of atomics, > but I choose not to in the beginning, as I want to make it easier to > review. > > * Very likely, we will only have AtomicI32, AtomicI64 and AtomicUsize > (i.e no atomic for bool, u8, u16, etc), with limited support for > atomic load and store on 8/16 bits. > > * 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). Can we start off with out-of-line atomics, and see where the bottlenecks are? It's relatively easy to do that today, at least for the atomic*_*() APIs: https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/commit/?h=atomics/outlined&id=e0a77bfa63e7416d610769aa4ab62bc06993ce56 .. which IIUC covers the "AtomicI32, AtomicI64 and AtomicUsize" cases you mention above. Mark.