Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3545398pxj; Mon, 7 Jun 2021 13:36:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXxcakWT05PZgn+cI8+HycPB/jAX5yUCyY2fClqGQhVCW7sZyPgH5rJy4ARuaKewkPi93x X-Received: by 2002:a50:a6c2:: with SMTP id f2mr21557039edc.39.1623098199680; Mon, 07 Jun 2021 13:36:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623098199; cv=none; d=google.com; s=arc-20160816; b=aL0CSzR03mrREdVJrURrlIhFjHPdw8e1a/YapSQ/TonFqP6CR3MhxIxS42kw8c5458 NcX5j1Z6/jPL/66qg9aF43QNhIB+sfX98chH1Ogg6goq/zmZjNU+6WNy3a+SIJEUseUq jSDF4GdCEYxxEZ8TvIBf0ZDk2KgDL7w7Jb//xPWWGQZQ0epTywK3pVykIwcuk0WCd9Ab 9wJ1xRlN5jOMBtiwWU0zZX1KNy1c8aFBOtbNnmXWvwFe52Bnkdiv4bVu40+YWWZLqctu JtaGICyTccVsvpAchO27aUXSOuA3Ny1TV2jt6EIuMeFAqq9/imTUUVEi0iW7v42h69LK jGFA== 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=kteMWI8wqvnKbw9QPTVRDBy7GPMe911SvO5GJ7EDIIo=; b=GK3NTWV+7Rb4DHWlkITHkYx4CeFSjdI01uD5vs7ZZqxgPo5TQCZ3RkWZAyaUXogZg/ aPjE3xCFrhvGLQTRH5MCn3aDOqhxKamOJB7Z02YnA3i/DKYBPuyU/JYnKeSpRV2Cbv2a rcbPhwMliYrUcD5iKI0V42PJBJjo4qPGAULH/8M7GQoFxx7LuWHY+ud4Pkro3soji8lZ e2+FtN82iVQgzfMKUeThttTCyCb4mwdGp1lHgPu4TUX+10zZKd3XjGaE/o68R10cnMXG Z9J+Eq4Jp/B2GqJPY8Uf+gtosFpiCOpDZCDPaaIwYq7wdmsqlwpVg7Pwm9aQNfYRUKAW 133Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=DBmsALCC; 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 j7si14648839ejm.280.2021.06.07.13.36.15; Mon, 07 Jun 2021 13:36: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=DBmsALCC; 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 S231492AbhFGUew (ORCPT + 99 others); Mon, 7 Jun 2021 16:34:52 -0400 Received: from mail-lj1-f171.google.com ([209.85.208.171]:45878 "EHLO mail-lj1-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230436AbhFGUew (ORCPT ); Mon, 7 Jun 2021 16:34:52 -0400 Received: by mail-lj1-f171.google.com with SMTP id u18so3806879lju.12 for ; Mon, 07 Jun 2021 13:32:43 -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=kteMWI8wqvnKbw9QPTVRDBy7GPMe911SvO5GJ7EDIIo=; b=DBmsALCCUaGl/AsAhStDaRku1s3wuNNlysYwZV3avf0trIA4fgd3qdx/kJYwf3UO6P RVujQzv/zjbuLQIcZqiN0YqSfz9j8Ek1UF2WTIdcppVu2J0t0BJMi2j8GvoUqTRSapAQ URU9aCCc0d1NcbPLvHkwwQ2JUUTVGUza1ifOc= 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=kteMWI8wqvnKbw9QPTVRDBy7GPMe911SvO5GJ7EDIIo=; b=JWiVxQBwodw2V7re3JvqtxCqdrQ7Xk4T4pHwrmkEM8tkkI1q4gyT3C1UaOJuV6RycO SEizNp575DIUTP4UmIUqoLH9RuRpb33NS4rbx78SkYsa8jwpppipjJhhsg4ol/8CB1S/ TdEHNSiKJBNccOvXE8CXISm5/LNslv0g0+sBST3fPVnOcrHptkA33lSK8gVnX/KvyQgp Ef0GAkNyMlNLyWlCvnnHcFTRloTbrAcM8Wy78wVYPV89k99nkckefCGipyMx+noeRzzQ j+ErjSMwVi/VDapD6ztnSK4NrJpv9nFb3j/2aMQSo1hPXXcDkhxyXdpsXH5zjgIgQ7qk L22A== X-Gm-Message-State: AOAM530RVqgWTWeDohGZCmFoefdFJi17YGPny3nuncwnz13M6jqYS7EV DT+RjYXWgB4wV+vPDAwpTv0B9ZbhL72MXUmueBo= X-Received: by 2002:a2e:9855:: with SMTP id e21mr16683376ljj.295.1623097902521; Mon, 07 Jun 2021 13:31:42 -0700 (PDT) Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com. [209.85.167.54]) by smtp.gmail.com with ESMTPSA id i23sm1379127ljg.38.2021.06.07.13.31.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Jun 2021 13:31:41 -0700 (PDT) Received: by mail-lf1-f54.google.com with SMTP id i10so28449074lfj.2 for ; Mon, 07 Jun 2021 13:31:41 -0700 (PDT) X-Received: by 2002:a05:6512:3f82:: with SMTP id x2mr12594362lfa.421.1623097901047; Mon, 07 Jun 2021 13:31:41 -0700 (PDT) MIME-Version: 1.0 References: <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> <20210607174206.GF18427@gate.crashing.org> In-Reply-To: <20210607174206.GF18427@gate.crashing.org> From: Linus Torvalds Date: Mon, 7 Jun 2021 13:31:24 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC] LKMM: Add volatile_if() To: Segher Boessenkool Cc: Alexander Monakov , Jakub Jelinek , Alan Stern , "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 Mon, Jun 7, 2021 at 10:45 AM Segher Boessenkool wrote: > > On Sun, Jun 06, 2021 at 03:38:06PM -0700, Linus Torvalds wrote: > > > > 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. > > You can split the "I" version from the "r" version, it does not need > the memory clobber. If you know the actual maximum bit offset used you > don't need the clobber for "r" either. Or you could even write > "m"(((unsigned long *)addr)[nr/32]) > That should work for all cases. Note that the bit test thing really was just an example. And some other cases don't actually have an address range at all, because they affect arbitrary ranges, not - like that bit test - just one particular range. To pick a couple of examples of that, think of (a) write memory barrier. On some architectures it's an explicit instruction, on x86 it's just a compiler barrier, since writes are ordered on the CPU anyway, and we only need to make sure that the compiler doesn't re-order writes around the barrier Again, we currently use that same "barrier()" macro for that: #define __smp_wmb() barrier() but as mentioned, the barrier() thing has a "memory" clobber, and that means that this write barrier - which is really really cheap on x86 - also unnecessarily ends up causing pointless reloads from globals. It obviously doesn't actually *change* memory, but it very much requires that writes are not moved around it. (b) things like cache flush and/or invalidate instructions, eg asm volatile("wbinvd": : :"memory"); Again, this one doesn't actually *modify* memory, and honestly, this one is not performance critical so the memory clobber is not actually a problem, but I'm pointing it out as an example of the exact same issue: the notion of an instruction that we don't want _writes_ to move around, but reads can happily be moved and/or cached around it. (c) this whole "volatile_if()" situation: we want to make sure writes can't move around it, but there's no reason to re-load memory values, because it doesn't modify memory, and we only need to make sure that any writes are delayed to after the conditional. We long long ago (over 20 years by now) used to do things like this: struct __dummy { unsigned long a[100]; }; #define ADDR (*(volatile struct __dummy *) addr) __asm__ __volatile__( "btl %2,%1\n\tsbbl %0,%0" :"=r" (oldbit) :"m" (ADDR),"ir" (nr)); for that test-bit thing. Note how the above doesn't need the memory clobber, because for gcc that ADDR thing (access to a big struct) ends up being a "BLKmode" read, and then gcc at least used to treat it as an arbitrary read. I forget just why we had to stop using that trick, I think it caused some reload confusion for some gcc version at some point. Probably exactly because inline asms had issues with some BLKmode thing. That change happened before 2001, we didn't have nice changelogs with detailed commit messages back then, so > > Anybody have ideas or suggestions for something like that? > > Is it useful in general for the kernel to have separate "read" and > "write" clobbers in asm expressions? And for other applications? See above. It's actually not all that uncommon that you have a "this doesn't modify memory, but you can't move writes around it". It's usually very much about cache handling or memory ordering operations, and that bit test example was probably a bad example exactly because it made it look like it's about some controlled range. The "write memory barroer" is likely the best and simplest example, but it's in not the only one. Linus