Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp265183rdh; Thu, 26 Oct 2023 01:14:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGKIpoby8fx6DtWOrYNwuxYlX6QLY/Qf1kvXIIgU6QTsuijLWNMf8UcFNIjyx8n0deVrE9f X-Received: by 2002:a1f:23d1:0:b0:49a:6dc0:5a89 with SMTP id j200-20020a1f23d1000000b0049a6dc05a89mr15535214vkj.5.1698308087777; Thu, 26 Oct 2023 01:14:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698308087; cv=none; d=google.com; s=arc-20160816; b=Xj4CXiWNLUeYMQvHaWHarZwQRIyiKiTzfiRYy3ahuVf+3cXsrYzU4ITnaifXSgwU5k dY8vCglcYhozSge35hRcwbHCmRq3XLXEGAyVGF9SnqxoSrSrp/7y//0U66RATAkR/N/V ytXmwxX+ctUlTQ1NInpNKXyPdQahDrCrJYKEYEaE3p55ORfY3MFNDtZJ3gKQoviVBxIx s8GQvfdjJzz0W3OMjdRvECEKuPW9/Mbwlm5RA8JV5bskYc6bS846gcLh4FU54EOQB6OA ZHhFqaSOQOHK/NNCgU6oEhcUhLMPgB5oIZguKCVPjukcFEqKV5IntpFqJUh0+VYReuin gDDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=7Ac2lsTLX6oaCFTK+hAgvsoBjk6RrYwjBJWA86xwxA8=; fh=LUk99b6QZarychXZ9UB4roYuA4AW/K5nmvrfAqb+kYM=; b=XOqj65JH2z4YaCtqd8mUn0t6tIeechYzYg40srUxnSofrFwbENJs2GpijCfDqtV/R1 j3wInGFCYOa78RfvY8Iqyo84VkJhtzUgf4VInS8tKOPjWcrMCSUaSgr58jFuto4cfn8Z QMtL3n5h9O5L3Xq7M+GlzfH5Z0R2u2J9w/U99wZ7EyWL8QrwQ/v0yopBQ9am/p/eHGa8 uP7fxgDEksgEhpNcASJNyipRQ6wz0YmWowkaDEqnkWt1H9mpeBHPDCSQQ2UyyF9WSsmM SIqE4y+fu/aqXPHlUohj0ZmOkF9A590uED2JajYLD3wl1G/Fk7EYO5G9YKYRPDUxJzPm i16A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b="PcOA/EqA"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id n1-20020a0dfd01000000b0059e8a6bd2f1si13265954ywf.533.2023.10.26.01.14.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 01:14:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b="PcOA/EqA"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id D0D6380DB72F; Thu, 26 Oct 2023 01:14:44 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344527AbjJZIOi (ORCPT + 99 others); Thu, 26 Oct 2023 04:14:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344444AbjJZIOg (ORCPT ); Thu, 26 Oct 2023 04:14:36 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48F20B8; Thu, 26 Oct 2023 01:14:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=7Ac2lsTLX6oaCFTK+hAgvsoBjk6RrYwjBJWA86xwxA8=; b=PcOA/EqANSQyrrzaA6ZNbON/qw 2GuUgXTosP7z60vDMEAt/QzqafvyRBZ/yV9b/kaPi23W8blMVIEjNfUphG9iltyG6Qi1mitrUR8Kj MRzl05qUDnGaLri4lil6C0/yAGD9swRKnKBjLPu2/B+H1Xg6nwz+Kt/rqUrmkCbKvZG8IffR5pSiK FTdf2fs6HmjryxBtpNoHkuMpvY92HNKF6pewQECWNib6WX6dAWjPaEZg3gCHdp+DGRQaVe0VOovTZ EA0d8v/nB2omlsg0EDU7ypHQD5oJJG8vnhbC8N/wyXiENErE4Rt/5sD9TXl2vpVIlABQszutn7vPl FptwUvjw==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qvvUw-00H96Y-0x; Thu, 26 Oct 2023 08:13:46 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id EB2D8300473; Thu, 26 Oct 2023 10:13:45 +0200 (CEST) Date: Thu, 26 Oct 2023 10:13:45 +0200 From: Peter Zijlstra 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 , =?iso-8859-1?Q?Bj=F6rn?= 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 , Tom Rix , Alexander Viro , Christian Brauner , kent.overstreet@gmail.com, Greg Kroah-Hartman , elver@google.com, Matthew Wilcox , Dave Chinner , linux-fsdevel@vger.kernel.org, Linus Torvalds Subject: Re: [RFC] rust: types: Add read_once and write_once Message-ID: <20231026081345.GJ31411@noisy.programming.kicks-ass.net> References: <20231025195339.1431894-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231025195339.1431894-1-boqun.feng@gmail.com> X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 26 Oct 2023 01:14:45 -0700 (PDT) On Wed, Oct 25, 2023 at 12:53:39PM -0700, Boqun Feng wrote: > In theory, `read_volatile` and `write_volatile` in Rust can have UB in > case of the data races [1]. However, kernel uses volatiles to implement > READ_ONCE() and WRITE_ONCE(), and expects races on these marked accesses > don't cause UB. And they are proven to have a lot of usages in kernel. > > To close this gap, `read_once` and `write_once` are introduced, they > have the same semantics as `READ_ONCE` and `WRITE_ONCE` especially > regarding data races under the assumption that `read_volatile` and > `write_volatile` have the same behavior as a volatile pointer in C from > a compiler point of view. > > Longer term solution is to work with Rust language side for a better way > to implement `read_once` and `write_once`. But so far, it should be good > enough. So the whole READ_ONCE()/WRITE_ONCE() thing does two things we care about (AFAIR): - single-copy-atomicy; this can also be achieved using the C11 __atomic_load_n(.memorder=__ATOMIC_RELAXED) / __atomic_store_n(.memorder=__ATOMIC_RELAXED) thingies. - the ONCE thing; that is inhibits re-materialization, and here I'm not sure C11 atomics help, they might since re-reading an atomic is definitely dodgy -- after all it could've changed. Now, traditionally we've relied on the whole volatile thing simply because there was no C11, or our oldest compiler didn't do C11. But these days we actually *could*. Now, obviously C11 has issues vs LKMM, but perhaps the load/store semantics are near enough to be useful. (IIRC this also came up in the *very* long x86/percpu thread) So is there any distinction between the volatile load/store and the C11 atomic load/store that we care about and could not Rust use the atomic load/store to avoid their UB ?