Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp331108rdh; Thu, 26 Oct 2023 03:37:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFmguj6HM1g+ioX7afRVmZPmbDAHf3xSr/yZvmJMFTLa4LlW8LlmoEMm1pBsgfJfbjRbTou X-Received: by 2002:a0d:db45:0:b0:5a7:ba17:584b with SMTP id d66-20020a0ddb45000000b005a7ba17584bmr19474667ywe.1.1698316640314; Thu, 26 Oct 2023 03:37:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698316640; cv=pass; d=google.com; s=arc-20160816; b=d4nj78feC8dHd2KVMV6PWdJ8WlAET2S0iElKl0Gs2VGMvyS+aYauHU+UB2Ue98S3/J ULPBuft49y3QN3YUgIAVwQHaEF3K1p3QdYnT3Dg8nBWZ6/IRpFB7SOK1fcGqtgTczK/W OPcCxp8f72O6HKBRwgMpBY6fTqaOBueAdNkLY7WHqadGyqyuI4oLlWnbgmztldo6VOGa 0RESQ62KG3oyUa7Rln9oM+oK2O8DX48RnC57kXd91Cafbq8jLaPCnmGgLDcLEc5ij+43 ctoFQQwEBdsCY6wZkR9As7ckO1d1a8tplfxe3MJWrjX8V4DwJuW4eONn+YgvHkGEOdGZ sWkg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=vHfwMIwJc6un10nuK1mblB0eOJMlqa7OBIEYvuwUhdA=; fh=ncQiVz2nJQHZ6NqBYwzmtuGLA5KSDtooticRuntwLbY=; b=bvEpF+ftKggkjQI6j7hdMXUnBRBzFJx2Fc1MV7LBf4Y2O+v+fgeJKOmhJ57SNyrJx4 fTMQuWpg10fIg2Y2wKoOy+zECDKICZRleKg93P4sITRwD3VkEDCKY9SGJ2YAv9BvOBRc uzpPRDvHmCQoolj+oJ+FDlVjl9eBXKi7l9T/c6s0fVYo0UAgHm4+cJeTCd0cLTx8G7Rv RHlPka7v5E1K7ddeFd7jLYyMKxwsilu2DHuAkB1Lfwp94jda0lTbr8iypiVc0wjWnpCg s1lcaZP7SEshbzwV1Y92sKcDQMNYN50Sa8Tuc1kX1f/aB6s+aypLKrh8oX3rNWmByTwl ybyQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@garyguo.net header.s=selector1 header.b=qCamM+jR; arc=pass (i=1 spf=pass spfdomain=garyguo.net dkim=pass dkdomain=garyguo.net dmarc=pass fromdomain=garyguo.net); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id o9-20020a81de49000000b005a204618ad5si15019183ywl.504.2023.10.26.03.37.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 03:37:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@garyguo.net header.s=selector1 header.b=qCamM+jR; arc=pass (i=1 spf=pass spfdomain=garyguo.net dkim=pass dkdomain=garyguo.net dmarc=pass fromdomain=garyguo.net); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 7256481A3361; Thu, 26 Oct 2023 03:36:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231314AbjJZKgX (ORCPT + 99 others); Thu, 26 Oct 2023 06:36:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230405AbjJZKgU (ORCPT ); Thu, 26 Oct 2023 06:36:20 -0400 Received: from GBR01-CWX-obe.outbound.protection.outlook.com (mail-cwxgbr01on2092.outbound.protection.outlook.com [40.107.121.92]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 225FD189; Thu, 26 Oct 2023 03:36:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mG0Fshx/Vhb6O/6MuRCRvPEG9hYyTIL+UHDynBeaNYzXAC0ZLwYZ9ZpGX/FlIn9+K0pHUh46CADlTgMi6GLi2aD+TjNB42zwN6M1aFOAwTZENbnbsrMnZaDijykP8oEt3Dj9CJant9s0E+oNTpLSVLfAsevWnK/pTE1vZZeE133WYNkH2dsOG8qznUZ/IYt3hbZLchT9Sq+p4Y9GjbMphsEbls7/WmBwypEUFLj/lR7u5gBR1gW9D7e8GK0cHgp2eReNgM7oNCF4VkDaCParUktcrI7yiW2VIRA4DEjOnNfYRgsByqSGFXv2bNg/bdqm8MogLQ9B3qg6YLySG1ZZAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vHfwMIwJc6un10nuK1mblB0eOJMlqa7OBIEYvuwUhdA=; b=eX44g7P/QdxFk46yECytgYydkFAMGxyQ9KxVPoGuH0IXIKtDlHZLoEqLYongJjYD48d4Ocr+ZNnrrFmKmzo+Hx+x8CVCJZD3YzAPGsYPegSUkssJ8kKSm7bz+QTc0bhdAMjwYH18g0gJjdZRRs/DdOyWKLBlH72Otq6GbCtlVTSSf2aQO/Qsm1qPyGJBKMr5E88h3KFQkyh0wwv5+oYpZc4/Hd/GM2+4NPPAjqCv1Oc+PPiXtNsif38LYmv8AO/a5S7pjMS2aEi0CYLiUZUSk/kzsEDfVg5CHjhgTFQE8anWmBQN6z2gEzWyxNmt8YZSZwfCaJjzwXACrRIWddOTdQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=garyguo.net; dmarc=pass action=none header.from=garyguo.net; dkim=pass header.d=garyguo.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garyguo.net; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vHfwMIwJc6un10nuK1mblB0eOJMlqa7OBIEYvuwUhdA=; b=qCamM+jR6okZA9mHUY3Y6e2PtWYXNPj2jo7pAAE4Bwr8u7DJFDBxLSIDsREa0YKogOW+ek2uG/eTthlZ2InubEAmBsUF5aji/CabGqvkOSQzwQQbREnCLkiTP4sWwMtLwOoFrUCIyO+tXV5LEa02U3PdeEz4+S3D7SCgrtQ6pfg= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::10) by LO0P265MB5407.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:245::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.33; Thu, 26 Oct 2023 10:36:14 +0000 Received: from LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM ([fe80::6331:81d5:43cc:a9a2]) by LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM ([fe80::6331:81d5:43cc:a9a2%6]) with mapi id 15.20.6933.019; Thu, 26 Oct 2023 10:36:14 +0000 Date: Thu, 26 Oct 2023 11:36:10 +0100 From: Gary Guo To: Peter Zijlstra Cc: Boqun Feng , 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 , =?UTF-8?B?QmrDtnJu?= 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: <20231026113610.1425be1b@eugeo> In-Reply-To: <20231026081345.GJ31411@noisy.programming.kicks-ass.net> References: <20231025195339.1431894-1-boqun.feng@gmail.com> <20231026081345.GJ31411@noisy.programming.kicks-ass.net> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.37; x86_64-pc-linux-gnu) Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-ClientProxiedBy: LO4P123CA0552.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:319::14) To LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LO2P265MB5183:EE_|LO0P265MB5407:EE_ X-MS-Office365-Filtering-Correlation-Id: c03f7c35-f11b-4491-9734-08dbd60f60f6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qZPEXaGrl03SQklEBTw1rWSVoO6BPKV6EAxbL6+pnDd4oM7Cq/D0bBZYc2Zpwdv33usECNuKJVu9qanGuyh99ZG1llh6juGYFUAAcQS9nMondYFCex71BiAea6cwU+vO7R38CMKCNGtpeIK+piv6HFvexv1ww1w+xBNguFDQRBmhno3m+vLTPjkUmulZ2BxFlToe1ry8V6q14JJKo3Fv3QpAcJQWFhN3d1bdx4pzgLd3JDjP09ckLtQW+nU4Grs1MZ/PbFQ33Ahsh9gcifwMRHTsQ5qIo9JsTaS6CUq2nJsr2C7fop1mZQUOHES/druPOcUYcC27eYkWM95ytaaMtMqgayHt8cXxW48tM3sQUXu/krZVEgAv3Yf2yy7tz0dahlcr5/Szc89o8J9/osv63Pz1iTyatMwr6m5H9EiTWxfxVL67CmducE2levCPwZhU67LmkvdiZHWsIbmK09bPdRf3eMaQm8SLAF6Ho2hi9oezaILUuIUStbMQakWxnXhrgouZCSvc0eRwkPQIgtGOKHh/pWAT9CukqcSBpRd3KFkZDph/+QDP6n2Ppidm0tiQ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230031)(7916004)(346002)(39830400003)(136003)(396003)(366004)(376002)(230922051799003)(186009)(1800799009)(64100799003)(451199024)(41300700001)(8936002)(8676002)(6486002)(4326008)(7406005)(86362001)(7416002)(2906002)(478600001)(5660300002)(54906003)(66476007)(66946007)(316002)(66556008)(6916009)(33716001)(6512007)(26005)(1076003)(83380400001)(9686003)(38100700002)(6506007)(6666004);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?1qm6QcojY0Pw4/OXoN7TPsgKSthuxNwSig/Dso/q8GEQIzFKtAf1qTAXrYal?= =?us-ascii?Q?jFATVLiecR6sQ2t3tPXf68lAtoPkp10VZo2u3/3Xd/PAFYJ+7fDsCz/XVQ6J?= =?us-ascii?Q?ztFiLvyTw4F37Ywz/lZ+g0sleFKv08pVhkHNCLZxY2dFOrZID3DRS/csJG5M?= =?us-ascii?Q?iokkC0IVWf5Hvyd6acs8DVxWvvYz7L5Ki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PWBR2A6nTfDlmdFHPXyMTYxUo24qSiGVCjkF8vNo3L77q0k+3U?= =?us-ascii?Q?FHPhCb0sYDCVVRUp8/e4ghapkU2my8A7NszvW4b3KpUxoeWyCVhBvXFLSId9?= =?us-ascii?Q?Jv2rst7LhQvB2UAO4U+sUDi5mdClAKLxxNbDeZxOe7e5oyWm6peJpoqohvpN?= =?us-ascii?Q?IYrZbds4bKtmD552LiBnHZCXVJUp5CbttJzKTflLD8rCsnrLuWMADj+lSkzy?= =?us-ascii?Q?rHkEhR3P2fkDUHrZf3u5b6i7YqhuJyXaCrElXur5Ya+sb+Z/j+CxvZvIfR/Z?= =?us-ascii?Q?pRXntq/bNbMiFXtvttAjC85hVPBfk+uoyC1WMZwDRSzjAF3cL7zzi5jtt3Lq?= =?us-ascii?Q?ac03Yh+QlN3U9rrds61sgtqGh2GIBUUiCnbMBMU56P9U4MmsNYZHRvNQk9rR?= =?us-ascii?Q?q6Sb9R78NtvYOHlnQp6b6PBrIOMWY8xOwZejSP87828GkfzsH+kFK1zwQQfR?= =?us-ascii?Q?TsX4PEjKhmTCs6XISXqFMojIXhd8DSgmbecEhw14?= X-OriginatorOrg: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: c03f7c35-f11b-4491-9734-08dbd60f60f6 X-MS-Exchange-CrossTenant-AuthSource: LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Oct 2023 10:36:14.4023 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bbc898ad-b10f-4e10-8552-d9377b823d45 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: WR6BWMCcaeDvqz2Q09TunQlzzaef9xkLFCeOiOE3dyjVnuqPB8hrcjE4L3QlVNF7W5aQUCndce8sCggSy5eh2Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LO0P265MB5407 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 26 Oct 2023 03:36:29 -0700 (PDT) On Thu, 26 Oct 2023 10:13:45 +0200 Peter Zijlstra wrote: > 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 ? There's two reasons that we are using volatile read/write as opposed to relaxed atomic: * Rust lacks volatile atomics at the moment. Non-volatile atomics are not sufficient because the compiler is allowed (although they currently don't) optimise atomics. If you have two adjacent relaxed loads, they could be merged into one. * Atomics only works for integer types determined by the platform. On some 32-bit platforms you wouldn't be able to use 64-bit atomics at all, and on x86 you get less optimal sequence since volatile load is permitted to tear while atomic load needs to use LOCK CMPXCHG8B. * Atomics doesn't work for complex structs. Although I am not quite sure of the value of supporting it.