Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2826447pxj; Sun, 6 Jun 2021 15:47:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxHwjloPyJfqez4o1+2JH9XgDaUOZ3pAwcfW60on/Q83o0vL2JblhZOQBH+lbaayxZqyWSh X-Received: by 2002:a17:906:4fcb:: with SMTP id i11mr5088868ejw.366.1623019659363; Sun, 06 Jun 2021 15:47:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623019659; cv=none; d=google.com; s=arc-20160816; b=ZsQ9JqWlNzmP9I5q7YlGGaoqQjtLhxSQIOj35ke5B6I+MuHKyXehx3MF6cpSCsUHOM zOrS7kvWaFKVi4AVogAL96pRxAa6ekTfX3QS0ViFTwiP4xM9FmR397UaTA+GWhP1etqm dXHb5MEkOIRhymZm85qCJq+cIQU95sTjNyR3eIWuDsm/OHDi45cTwJ6wGwAhfsxZA6pk +w/sTXazUUVQW5N/pfw5ENsdy8vJbOyvd0XONwKk/GrYxEG4DL3ZDzTI49bagwsYUV7k hJwJ4P1pa+gEzRTcJtb5zGJF4+msBBtvNyQknG5SXTElL7IbrbGwge5jWP0rBSytixdQ hLsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=gtTe+bW9CJrIIoUl0y1d11JaP4RsQBfOevBdp08DRug=; b=AgWpgr4FTq91a5cIsM0znnyB8Z7vxo/eawQPuUHMpXWAZ3+n930DH2ehfdZi9V3W+0 eqYnWcMn29TXN+Dy7YKZ72gI4dZNbn4ESqnC6cna7xjXaa+APvhn2RtEjuzAtajoLj1X IzXvM+ZH3PLyAvJcKudNOezp8vgKKhySZY4t/nIXfzTQYTJ2aJkmHeUy4jcOikGXuV2+ F1e5ZA1vO5QVMYgYf4Om1wjC6dRuXWAPM/K9DCUCkM2XEAamg0qRdzdRV3tY9UjKgs7w eiNKsypwYh9MPl/FGkQVYpsX4TfSqO0YLuULiF9k8lDAlZt0AE3aj6GvO69+mdpy0UfN Lo0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=OxInyRJV; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o2si10707267edw.92.2021.06.06.15.47.17; Sun, 06 Jun 2021 15:47:39 -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=@linux-foundation.org header.s=google header.b=OxInyRJV; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231172AbhFFWsF (ORCPT + 99 others); Sun, 6 Jun 2021 18:48:05 -0400 Received: from mail-ej1-f47.google.com ([209.85.218.47]:41592 "EHLO mail-ej1-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230519AbhFFWsD (ORCPT ); Sun, 6 Jun 2021 18:48:03 -0400 Received: by mail-ej1-f47.google.com with SMTP id ho18so12469058ejc.8 for ; Sun, 06 Jun 2021 15:46:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=gtTe+bW9CJrIIoUl0y1d11JaP4RsQBfOevBdp08DRug=; b=OxInyRJVkqQCmKRmT6KGFE8qM7XAfQzY8nbQmo5qsLIenPiuQZPigbSL/hrNiuh00j Fiyq6WXUnbySLjjZhVoY4ZVWT2wgI8gyhocnD0Acawt0/u24ZMB8KiorrZ5u8WVzW5Mp bJ3imPB0o2E2dsaZWR8zv/W4ByDxPBlNuhDHo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=gtTe+bW9CJrIIoUl0y1d11JaP4RsQBfOevBdp08DRug=; b=GdsghPiKnTmtEsVp/6NbfgS1iFf9XRqlZs+rYVobGPQZLNGFFdfWrukOp1yz3viLTx JUhY0Y4LZxDML5YOMxQLLGteUzlEu62O/mQ6CWP+YtgA/P6DQbrhmFTmJTuX7tnD6+ar I2fTSE19xeNQytiYtEH+GMxnhQ1vgwemBtsVPxlBmobQrKU8Eszbst66PfmAq+I1uNZ0 u7dAyZZB21+D9s4qzZqC8pMGyPcoGME2P/QkC49+dtpyxqHAlW51ZOSS3GmkJAe7WEmE TR+A2F1TIxAJc+V5Q5OYUOal0ZS/A6OaZ2vsDSkmjyjICFYtkPkQOrzgYlJ7mxgWNs8j P3dw== X-Gm-Message-State: AOAM530ucytnsJ03SY045TyvUgIZIif5S1XMqtzzDM9sOJZe1q3C+mxL ncOvf3gGevem+gQAVE4wSbzqbUJgO58Ls2e7KLE= X-Received: by 2002:a17:906:3845:: with SMTP id w5mr15545197ejc.518.1623019500719; Sun, 06 Jun 2021 15:45:00 -0700 (PDT) Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com. [209.85.221.54]) by smtp.gmail.com with ESMTPSA id v23sm6989711eds.25.2021.06.06.15.45.00 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 06 Jun 2021 15:45:00 -0700 (PDT) Received: by mail-wr1-f54.google.com with SMTP id a11so13466013wrt.13 for ; Sun, 06 Jun 2021 15:45:00 -0700 (PDT) X-Received: by 2002:a05:6512:3f82:: with SMTP id x2mr9598539lfa.421.1623019102844; Sun, 06 Jun 2021 15:38:22 -0700 (PDT) MIME-Version: 1.0 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> <20210606185922.GF7746@tucnak> In-Reply-To: From: Linus Torvalds Date: Sun, 6 Jun 2021 15:38:06 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC] LKMM: Add volatile_if() To: Alexander Monakov Cc: Jakub Jelinek , Alan Stern , Segher Boessenkool , "Paul E. McKenney" , 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jun 6, 2021 at 2:19 PM Alexander Monakov wrote: > > > So yeah, that seems like a nice solution to the issue, and should make > > the barriers all unique to the compiler. > > It also plants a nice LTO time-bomb (__COUNTER__ values will be unique > only within each LTO input unit, not across all of them). That could be an issue in other circumstances, but for at least volatile_if() that doesn't much matter. The decision there is purely local, and it's literally about the two sides of the conditional not being merged. Now, an optimizing linker or assembler can of course do anything at all in theory: and if that ends up being an issue we'd have to have some way to actually propagate the barrier from being just a compiler thing. Right now gcc doesn't even output the barrier in the assembly code, so it's invisible to any optimizing assembler/linker thing. But I don't think that's an issue with what _currently_ goes on in an assembler or linker - not even a smart one like LTO. And such things really are independent of "volatile_if()". We use barriers for other things where we need to force some kind of operation ordering, and right now the only thing that re-orders accesses etc is the compiler. Btw, since we have compiler people on line, the suggested 'barrier()' isn't actually perfect for this particular use: #define barrier() __asm__ __volatile__("" : : "i" (__COUNTER__) : "memory") in the general barrier case, we very much want to have that "memory" clobber, because the whole point of the general barrier case is that we want to make sure that the compiler doesn't cache memory state across it (ie the traditional use was basically what we now use "cpu_relax()" for, and you would use it for busy-looping on some condition). In the case of "volatile_if()", we actually would like to have not a memory clobber, but a "memory read". IOW, it would be a barrier for any writes taking place, but reads can move around it. I don't know of any way to express that to the compiler. We've used hacks for it before (in gcc, BLKmode reads turn into that kind of barrier in practice, so you can do something like make the memory input to the asm be a big array). But that turned out to be fairly unreliable, so now we use memory clobbers even if we just mean "reads random memory". Example: variable_test_bit(), which generates a "bt" instruction, does : "m" (*(unsigned long *)addr), "Ir" (nr) : "memory"); and the memory clobber is obviously wrong: 'bt' only *reads* memory, but since the whole reason we use it is that it's not just that word at address 'addr', in order to make sure that any previous writes are actually stable in memory, we use that "memory" clobber. It would be much nicer to have a "memory read" marker instead, to let the compiler know "I need to have done all pending writes to memory, but I can still cache read values over this op because it doesn't _change_ memory". Anybody have ideas or suggestions for something like that? Linus