Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2322580pxj; Sat, 5 Jun 2021 21:48:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbnfnB02RnYwyxBUQ9i/HeSEvZvV5p/eNatwo2rZfilZ+o8y7LbIICWFqrMG8nsmWa9go/ X-Received: by 2002:a05:6402:1d38:: with SMTP id dh24mr13679019edb.18.1622954886792; Sat, 05 Jun 2021 21:48:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622954886; cv=none; d=google.com; s=arc-20160816; b=THnWAZIXPsjBrjERH03KAoInEq1Gm7XuhbDE1oOSYRkjWJuH4him3QgLt/hImYBSqN ldTexL1zo2f51Lqo6HbBTbkHatKauGJFfYtZu3Oo8IdhElqStHGkhSwrkdPb5Jv4+rc9 r14xDwb97PuqAjG1UyBq0sfoG8btMfVQRxIDwMf8VqYqu4rmJXUYXNtvr7quKFtWlJix AU/Kumi0eaFi/u02PppskAvU5JCeqCfk3AWxl5jwTM8qx0/tTjj/kwKmemA9e6ByMee8 El5G8pHNK9ZNIEwOmi/Rdu50jYMdxHjYcEjewtR+Vc2y0ixCo0BQL/ZLFG+J6gEb3x3t r5KA== 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=/dGh59HvD6z7MeAYmITHqBspXwzNqMqGuBIVQ0wU1KM=; b=QAMW8NJlUAZ0sA2K+NKeey4hiZ+qLSLBM/pmdkN5Qzln9cBJYwr/p8M8cxhc3tA9C8 O6HD5t5It/YENPAw9A6DV+A3ElWwRxUyolE5ov2uVNiPtJt6WgXQKN1FhHq8Fzp5t5hX 18xq3IakBqavBxwIhLhMJzfTkU5hjSDhxg6TFtvz1AnTyEfAxDLLBPai84oMu2YOzBmx Z9brs4mrvtOeqI6I2RN46vL4Dr+yTnNR5Tvt/V+jR2KnR/yVUwuJtptr55nPXoSdZ1u1 Yz6aI9fjNBYHlOrhbb9Lc1uiKhGnb0xPOLnzxeyRhojAkJm8sfwUyL+8q9g/pQ0PgJzG JXYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="ZLQMe/4L"; 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 r15si8543067edo.442.2021.06.05.21.47.44; Sat, 05 Jun 2021 21:48:06 -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="ZLQMe/4L"; 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 S230213AbhFFEpY (ORCPT + 99 others); Sun, 6 Jun 2021 00:45:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:45326 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbhFFEpW (ORCPT ); Sun, 6 Jun 2021 00:45:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D353B613F3; Sun, 6 Jun 2021 04:43:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1622954613; bh=ma6WWTmP1DHkeB4vQDJtzJFakqdzhrp/V2kE3Y0HYM8=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=ZLQMe/4LxNwMpONfqG4j5pKWU1XLrYNM244TvDOmmE2BM4HjQ4rv9JC1uwhlkAt9l pmWgOZY4BpCktFjikopqwqNx8FXbBMy/nbM0NsGGWveWiHWX235pv1cqJEqwp6tgQf a4c2Eej9ZF2lowGDpCXQbnHtOBMIsORWyNwR7FmaIPH06FC0OiY3X8mS0wKmH3GMGz yBPbMQGN5fxGzmr78YOGaLsd9duZZ6Va0NiDGfGQ0NTRy+wusgVcdkjcWLm00dT0dO Ymma2bxNpO3/UTdKagtpfY/88GBn3fZBy8IY67BXygr478EyDqtBLCBBfKCM865dos HtS7zOPAnHUOw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id A64605C0991; Sat, 5 Jun 2021 21:43:33 -0700 (PDT) Date: Sat, 5 Jun 2021 21:43:33 -0700 From: "Paul E. McKenney" To: Linus Torvalds Cc: Alan Stern , Segher Boessenkool , 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: <20210606044333.GI4397@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20210604205600.GB4397@paulmck-ThinkPad-P17-Gen-1> <20210604214010.GD4397@paulmck-ThinkPad-P17-Gen-1> <20210605145739.GB1712909@rowland.harvard.edu> <20210606001418.GH4397@paulmck-ThinkPad-P17-Gen-1> <20210606012903.GA1723421@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jun 05, 2021 at 08:41:00PM -0700, Linus Torvalds wrote: > On Sat, Jun 5, 2021 at 6:29 PM Alan Stern wrote: > > > > Interesting. And changing one of the branches from barrier() to __asm__ > > __volatile__("nop": : :"memory") also causes a branch to be emitted. So > > even though the compiler doesn't "look inside" assembly code, it does > > compare two pieces at least textually and apparently assumes if they are > > identical then they do the same thing. > > That's actually a feature in some cases, ie the ability to do CSE on > asm statements (ie the "always has the same output" optimization that > the docs talk about). Agreed, albeit reluctantly. ;-) > So gcc has always looked at the asm string for that reason, afaik. > > I think it's something of a bug when it comes to "asm volatile", but > the documentation isn't exactly super-specific. > > There is a statement of "Under certain circumstances, GCC may > duplicate (or remove duplicates of) your assembly code when > optimizing" and a suggestion of using "%=" to generate a unique > instance of an asm. So gcc might some day note a do-nothing asm and duplicate it for the sole purpose of collapsing the "then" and "else" clauses. I guess I need to keep my paranoia for the time being, then. :-/ > Which might actually be a good idea for "barrier()", just in case. > However, the problem with that is that I don't think we are guaranteed > to have a universal comment character for asm statements. > > IOW, it might be a good idea to do something like > > #define barrier() \ > __asm__ __volatile__("# barrier %=": : :"memory") > > but I'm not 100% convinced that '#' is always a comment in asm code, > so the above might not actually build everywhere. > > However, *testing* the above (in my config, where '#' does work as a > comment character) shows that gcc doesn't actually consider them to be > distinct EVEN THEN, and will still merge two barrier statements. > > That's distressing. If I keep the old definition of barrier() and make a barrier1() as you defined above: #define barrier1() __asm__ __volatile__("# barrier %=": : :"memory") Then putting barrier() in the "then" clause and barrier1() in the "else" clause works, though clang 12 for whatever reason generates an extra jump in that case. https://godbolt.org/z/YhbcsxsxG Increasing the optimization level gets rid of the extra jump. Of course, there is no guarantee that gcc won't learn about assembler constants. :-/ > So the gcc docs are actively wrong, and %= does nothing - it will > still compare as the exact same inline asm, because the string > equality testing is apparently done before any expansion. > > Something like this *does* seem to work: > > #define ____barrier(id) __asm__ __volatile__("#" #id: : :"memory") > #define __barrier(id) ____barrier(id) > #define barrier() __barrier(__COUNTER__) > > which is "interesting" or "disgusting" depending on how you happen to feel. > > And again - the above works only as long as "#" is a valid comment > character in the assembler. And I have this very dim memory of us > having comments in inline asm, and it breaking certain configurations > (for when the assembler that the compiler uses is a special > human-unfriendly one that only accepts compiler output). > > You could make even more disgusting hacks, and have it generate something like > > .pushsection .discard.barrier > .long #id > .popsection > > instead of a comment. We already expect that to work and have generic > inline asm cases that generate code like that. And that does the trick as well, at least with recent gcc and clang. https://godbolt.org/z/P8zPv9f9o Thanx, Paul