Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2211292pxj; Sat, 5 Jun 2021 17:16:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmgfo88VL17jAAyV8d1NoBdDjQU+anidwPP8Vag4N513m0PmwaarIx6PRBP2i+KrbnUgCx X-Received: by 2002:a17:906:33c8:: with SMTP id w8mr11321880eja.46.1622938579025; Sat, 05 Jun 2021 17:16:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622938579; cv=none; d=google.com; s=arc-20160816; b=KMQpl+Qnyw4q66PEwqHDuwoD+xInNKLPSe7kWeQjAZTzmGGfBXU+4Wh+whd7El1qCl d6WdmJju8o8hn+b3qium737Q46fxyLpEz60XBCP9zvNC+/ZmPmgqJ4jUHfoHg6zH01a2 f8lQMKl5R+XUCAUtrSxW9OE6idZ9MExlsO+pXGqp3o4dAMAn8X4ysYVSYm60IJrWl5Pi H0YqCDdNvFSYx1kzXAeaVLSxc5kjSdfcSK+cdAaLTwxq5eFpzDNN1DUgHSAee0REpyhQ n15BpxUd5DuONX/zv3+PAZDHnXG3eIMPAVCP0bHvXIvctUxGjfjBY8cPGmll6SmRMS9C oYYw== 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=HOP67gppNkoPwKcNIIge2LTvx8JVCtMwR3PqEqYoOJQ=; b=iNtGRGWQDravcLT1xi8FhrZnS2zW1rSnCru/5ddwgjKdioDJv/P2XInxBs/bhYWDwF b3QZj4rTjkgUx2mK50RIBJGMnyYToCXLZS6QcioDPv+C4qsqwczCTz3R6mRoyiS4LzXE WzECFcBF/Ca2jodTRE+h0PCL+RdkiCVqaWnKFYvRcxZ9YFswIcmuRh2/nrupBIeSTuaw GxUara6LENrvqNbFWFcyUvXRVlt1U57PJXULgyL1JdVtzrqBgblvCp+cJQVe3vzU8DnI VxXlgGpqoXEh4znD+VXAf3pWx41A7fuFuuhCKhMPxiEZpdn2W+CPg0Hyb6TMUoZGgC9R wh2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PuqKNynF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n2si11071283ejl.693.2021.06.05.17.15.56; Sat, 05 Jun 2021 17:16:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PuqKNynF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230073AbhFFAQH (ORCPT + 99 others); Sat, 5 Jun 2021 20:16:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:51102 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230022AbhFFAQH (ORCPT ); Sat, 5 Jun 2021 20:16:07 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 72070613F3; Sun, 6 Jun 2021 00:14:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1622938458; bh=5zAebfKw3wGjoJ9snXmpScoW0ytD1mv0Hz2fwhZpB/M=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=PuqKNynFwhPXp3oCxNFlJ12YpG0p5s5e6Bjs1K5I9MVmZvc1b4D0ZtKzAFWZvlE2Y 3kPTw/sVtD3JLnb/i47qSvRD4uCoS+CHxSZQ8PdUmXgelA/pdQ11f1DzYQfVmlDTLD 6SOXzg7wPGFa73BZqiGEvxXLcAuPvufifO1IkU2VwNnnsqLbXIzyQI4J08Msk4K7nw bihn6gYFkWlsA1s8prKb8aJ3rKwJRvouQp6OsstqT+NrhsOxmCEJPtOviP4SMHXl8z FNkhyzO+IrUny2mxJ97ZKsH0q3O8UYcvMa6Rjr2cHKJRhhNnY+ofVkdC7CHcHljKjo KwN4y3eD5T2Vw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 2E0575C0991; Sat, 5 Jun 2021 17:14:18 -0700 (PDT) Date: Sat, 5 Jun 2021 17:14:18 -0700 From: "Paul E. McKenney" To: Alan Stern Cc: Linus Torvalds , Peter Zijlstra , Will Deacon , Andrea Parri , Boqun Feng , Nick Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Linux Kernel Mailing List , linux-toolchains@vger.kernel.org, linux-arch Subject: Re: [RFC] LKMM: Add volatile_if() Message-ID: <20210606001418.GH4397@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20210604155154.GG1676809@rowland.harvard.edu> <20210604182708.GB1688170@rowland.harvard.edu> <20210604205600.GB4397@paulmck-ThinkPad-P17-Gen-1> <20210604214010.GD4397@paulmck-ThinkPad-P17-Gen-1> <20210605145739.GB1712909@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210605145739.GB1712909@rowland.harvard.edu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jun 05, 2021 at 10:57:39AM -0400, Alan Stern wrote: > On Fri, Jun 04, 2021 at 03:19:11PM -0700, Linus Torvalds wrote: > > Now, part of this is that I do think that in *general* we should never > > use this very suble load-cond-store pattern to begin with. We should > > strive to use more smp_load_acquire() and smp_store_release() if we > > care about ordering of accesses. They are typically cheap enough, and > > if there's much of an ordering issue, they are the right things to do. > > > > I think the whole "load-to-store ordering" subtle non-ordered case is > > for very very special cases, when you literally don't have a general > > memory ordering, you just have an ordering for *one* very particular > > access. Like some of the very magical code in the rw-semaphore case, > > or that smp_cond_load_acquire(). > > > > IOW, I would expect that we have a handful of uses of this thing. And > > none of them have that "the conditional store is the same on both > > sides" pattern, afaik. > > > > And immediately when the conditional store is different, you end up > > having a dependency on it that orders it. > > > > But I guess I can accept the above made-up example as an "argument", > > even though I feel it is entirely irrelevant to the actual issues and > > uses we have. > > Indeed, the expansion of the currently proposed version of > > volatile_if (A) { > B; > } else { > C; > } > > is basically the same as > > if (A) { > barrier(); > B; > } else { > barrier(); > C; > } > > which is just about as easy to write by hand. (For some reason my > fingers don't like typing "volatile_"; the letters tend to get > scrambled.) > > So given that: > > 1. Reliance on control dependencies is uncommon in the kernel, > and > > 2. The loads in A could just be replaced with load_acquires > at a low penalty (or store-releases could go into B and C), > > it seems that we may not need volatile_if at all! The only real reason > for having it in the first place was to avoid the penalty of > load-acquire on architectures where it has a significant cost, when the > control dependency would provide the necessary ordering for free. Such > architectures are getting less and less common. That does sound good, but... Current compilers beg to differ at -O2: https://godbolt.org/z/5K55Gardn ------------------------------------------------------------------------ #define READ_ONCE(x) (*(volatile typeof(x) *)&(x)) #define WRITE_ONCE(x, val) (READ_ONCE(x) = (val)) #define barrier() __asm__ __volatile__("": : :"memory") int x, y; int main(int argc, char *argv[]) { if (READ_ONCE(x)) { barrier(); WRITE_ONCE(y, 1); } else { barrier(); WRITE_ONCE(y, 1); } return 0; } ------------------------------------------------------------------------ Both gcc and clang generate a load followed by a store, with no branch. ARM gets the same results from both compilers. As Linus suggested, removing one (but not both!) invocations of barrier() does cause a branch to be emitted, so maybe that is a way forward. Assuming it is more than just dumb luck, anyway. :-/ Thanx, Paul