Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1530354imn; Sun, 31 Jul 2022 10:37:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR7j0K8Ymokr8cmoENlrrTR7MSQGb+0EkjXks+ml0UsRGPeL6uvVUeUutXCqO0UBAn2nwUy1 X-Received: by 2002:a17:907:270e:b0:730:7c46:30df with SMTP id w14-20020a170907270e00b007307c4630dfmr1713848ejk.411.1659289069587; Sun, 31 Jul 2022 10:37:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659289069; cv=none; d=google.com; s=arc-20160816; b=v8aSg52uL3FiMRbNCWXPRIguFZAW7RtT7ulsZi5I0C8up3KZdlLBz6sDFu6fFmjT3Y 0wlzh9Ep22cOLlTSRYoArTN3+wUInKhZ9KWlHpkbeWpGXaRpjRrhddy9RMyu3IimjH15 LEjCb0Y2QP7WnpNQkKo8rQfEtRiy5Zzd6Tqg3dhnF8GzymmRj9V85Z4yoJTfbgLhDzrh 367W/DqIXRsAkKf2lSehX9C7vvytI1xlRXAfDWtcHK2WoOlo+Lt9QYA1+wsAiiOfwLD9 iAz3CpDKQOLPVO1agovrvOzqLY6vWiUDLmvoIM3jgmrRUTR7B2jXU1eZjbOTg7NeD30d Otjw== 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:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=kKNcx/lP8fj3uJt6N88B5XDSHfkBOkjR42tXOMzysUk=; b=ccRGk4BDnGikaOiW/9yS2czpO/mDVlLkRFBukQ9KKSPrBiORew7FP37fMhYNLAFr22 ACPWTtUyeYCOrjKd6AwjHY++b2pghm6vxvaFb/3LBckS3ACtJDgunZ4tsRWZ08AqeVAN cuHhL5qVmXd6JFS8Y50Bn/dd7m95wAHnK8MbXskHOGN2P5Qs8OQj+BMVzWHL+AMxBKGK MmNiqXYJ0o2IwlCmUCmpPi4OK3Lj8DRtTt/1qs2HO5UH8wFDksj7bCfgNY/FzFKct6pa C393R60D7wpK/mdVQoMJcyt1kBE2HjkJvi0RBAszRGvf+8oQFyXj/RUBy71fFSYRnie3 wneg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=P8416Z2Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z60-20020a509e42000000b0043a4bc94f49si7177184ede.196.2022.07.31.10.37.25; Sun, 31 Jul 2022 10:37:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=P8416Z2Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237577AbiGaRaR (ORCPT + 99 others); Sun, 31 Jul 2022 13:30:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230074AbiGaRaO (ORCPT ); Sun, 31 Jul 2022 13:30:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8BFD5F8A; Sun, 31 Jul 2022 10:30:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6B3D260F1F; Sun, 31 Jul 2022 17:30:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AB9DDC433C1; Sun, 31 Jul 2022 17:30:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659288611; bh=FH0blCyVvfudwL8ce3BTQJUuYsbrfBaewHrHEz9SgsU=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=P8416Z2Yh37otidwE8Z7NVDq7lcvqpRNoWfUOua9IveI8aEUOJ52Nlt/CNTT7wMF2 2IQ+XlCnziFRSmUT41WYd3b8Bf8aS6D61RTcDsydT8licqRQQVY5psT7Eg3Uc2+PP+ DajKlQ/1UwQyTEquO8QYUNvlJul+JwnzfQ0YVhO/jCDVnYpzNtH1TAFN3k+N+AAvHv IPK1GKzuoTpiwZQuBThZkghdQUhwU6dTh8MWT/qTJy4jrQsVJprRkaEjcyaMVV1W1Y 1t2KGjeiV7vuhZZSLA5mRlcuQsrIMr2qCHYwlV75I97xPzWwNsInYkUMi6yBQnRWbW On4VzjDbmLQbg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 3CBD75C03E0; Sun, 31 Jul 2022 10:30:11 -0700 (PDT) Date: Sun, 31 Jul 2022 10:30:11 -0700 From: "Paul E. McKenney" To: Linus Torvalds Cc: Mikulas Patocka , Will Deacon , Ard Biesheuvel , Alexander Viro , Alan Stern , Andrea Parri , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Linux Kernel Mailing List , linux-arch , linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v2] make buffer_locked provide an acquire semantics Message-ID: <20220731173011.GX2860372@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 On Sun, Jul 31, 2022 at 09:51:47AM -0700, Linus Torvalds wrote: > [ Will and Paul, question for you below ] > > On Sun, Jul 31, 2022 at 8:08 AM Mikulas Patocka wrote: > > > > Also, there is this pattern present several times: > > wait_on_buffer(bh); > > if (!buffer_uptodate(bh)) > > err = -EIO; > > It may be possible that buffer_uptodate is executed before wait_on_buffer > > and it may return spurious error. > > I'm not convinced that's actually valid. > > They are testing the same memory location, and I don't think our > memory ordering model allows for _that_ to be out-of-order. Memory > barriers are for accesses to different memory locations. Yes, aligned same-sized marked accesses to a given location are always executed so as to be consistent with some global order. Please note the "marked accesses". The compiler can rearrange unmarked reads and in some cases can discard unmarked writes. For more information, please see tools/memory-model/Documentation/recipes.txt's "Single CPU or single memory location" section. > Even alpha is specified to be locally ordered wrt *one* memory > location, including for reads (See table 5-1: "Processor issue order", > and also 5.6.2.2: "Litmus test 2"). So if a previous read has seen a > new value, a subsequent read is not allowed to see an older one - even > without a memory barrier. > > Will, Paul? Maybe that's only for overlapping loads/stores, not for > loads/loads. Because maybe alpha for once isn't the weakest possible > ordering. The "bad boy" in this case is Itanium, which can do some VLIW reordering of accesses. Or could, I am not sure that newer Itanium hardware does this. But this is why Itanium compilers made volatile loads use the ld,acq instruction. Which means that aligned same-sized marked accesses to a single location really do execute consistently with some global ordering, even on Itanium. That said, I confess that I am having a hard time finding the buffer_locked() definition. So if buffer_locked() uses normal C-language accesses to sample the BH_Lock bit of the ->b_state field, then yes, there could be a problem. The compiler would then be free to reorder calls to buffer_locked() because it could then assume that no other thread was touching that ->b_state field. > I didn't find this actually in our documentation, so maybe other > architectures allow it? Our docs talk about "overlapping loads and > stores", and maybe that was meant to imply that "load overlaps with > load" case, but it really reads like load-vs-store, not load-vs-load. I placed the relevant text from recipes.txt at the end of this email. > But the patch looks fine, though I agree that the ordering in > __wait_on_buffer should probably be moved into > wait_on_bit/wait_on_bit_io. > > And would we perhaps want the bitops to have the different ordering > versions? Like "set_bit_release()" and "test_bit_acquire()"? That > would seem to be (a) cleaner and (b) possibly generate better code for > architectures where that makes a difference? As always, I defer to the architecture maintainers on this one. Thanx, Paul ------------------------------------------------------------------------ Single CPU or single memory location ------------------------------------ If there is only one CPU on the one hand or only one variable on the other, the code will execute in order. There are (as usual) some things to be careful of: 1. Some aspects of the C language are unordered. For example, in the expression "f(x) + g(y)", the order in which f and g are called is not defined; the object code is allowed to use either order or even to interleave the computations. 2. Compilers are permitted to use the "as-if" rule. That is, a compiler can emit whatever code it likes for normal accesses, as long as the results of a single-threaded execution appear just as if the compiler had followed all the relevant rules. To see this, compile with a high level of optimization and run the debugger on the resulting binary. 3. If there is only one variable but multiple CPUs, that variable must be properly aligned and all accesses to that variable must be full sized. Variables that straddle cachelines or pages void your full-ordering warranty, as do undersized accesses that load from or store to only part of the variable. 4. If there are multiple CPUs, accesses to shared variables should use READ_ONCE() and WRITE_ONCE() or stronger to prevent load/store tearing, load/store fusing, and invented loads and stores. There are exceptions to this rule, including: i. When there is no possibility of a given shared variable being updated by some other CPU, for example, while holding the update-side lock, reads from that variable need not use READ_ONCE(). ii. When there is no possibility of a given shared variable being either read or updated by other CPUs, for example, when running during early boot, reads from that variable need not use READ_ONCE() and writes to that variable need not use WRITE_ONCE().