Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1953180pxp; Thu, 17 Mar 2022 22:43:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgBacQs/FSY84Lg8WmjY3CsEF66whpjffcRzYboQlMG5EeR0RCV2HxZBNDH7tDvKIhE//V X-Received: by 2002:a50:bf0f:0:b0:410:c512:cb6f with SMTP id f15-20020a50bf0f000000b00410c512cb6fmr7957392edk.262.1647582202843; Thu, 17 Mar 2022 22:43:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647582202; cv=none; d=google.com; s=arc-20160816; b=n8FXb5jL5NRgNEonMgd7MdTPMfoBBPhnbsfgOkoslauzi5eQ745s5vBKhNYXJc412y O8o/89glQxJqD8qO0Zcb7JFyHTVLSDtV35P7SVvcSs8Xj2hOXMB6dajS1Y64eHtq6Qr2 luEX7Tf2RjlR75kup2LQdtoqcMbRajnvBTbi0nEqPH57XKdEhaB2aL7D5GyZD0Gwcy2x JzzKrg/AZqiCkj2cXal1tQqasNmGChB/VG0B+TW7XoSXNiMCSVZ3mTf86CdftPjOMNCe eYGDz6p9zuUcZN3itX48sQVaqu/plRzgJMY3JLSd/6Xvopy12/5ti+zhSrMj091Vnpxu x9xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=DF1gqtR2+UeRcyVzymysqYRdF/y7jVPHRuydZRbDjeo=; b=jZI7+wuAKfgZvfcTviBdd7IJX3+IR/uhlX902HUHoLHs4Ss3cu/owAOx+m3ffdEEMN WqORwMbtVJBSyJRAJCQoFNr/cz0x02UXSa9zB3TQtLV5S5/jsmjUO9I93aWoAOZndubh svN0V6UczmTPzqFz9pmlwfgwUJEcCjyCaeyizUTI5VTNi19IR3lzhnNFgSR59m3l3X3l ZrTCAthwVdgwk42RO6K5m6xrbXpP/215U2QU9XpeQBqlmxkEx2AYCVZbonQcOkd7lFl7 X25BoM+GHAm5uZXKZRM8381iPgQ5tJS5iKLhdujHldXY9IJLebubHNK0u/HYqlLR1mj3 lNzw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w6-20020a1709060a0600b006df76385d74si686669ejf.532.2022.03.17.22.42.36; Thu, 17 Mar 2022 22:43:22 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230478AbiCRANh (ORCPT + 99 others); Thu, 17 Mar 2022 20:13:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230470AbiCRANd (ORCPT ); Thu, 17 Mar 2022 20:13:33 -0400 Received: from gate.crashing.org (gate.crashing.org [63.228.1.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 86E87141D9E; Thu, 17 Mar 2022 17:12:13 -0700 (PDT) Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 22I05gcE000704; Thu, 17 Mar 2022 19:05:42 -0500 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 22I05eqi000699; Thu, 17 Mar 2022 19:05:40 -0500 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Thu, 17 Mar 2022 19:05:40 -0500 From: Segher Boessenkool To: Linus Torvalds Cc: Bill Wendling , Nick Desaulniers , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Nathan Chancellor , Juergen Gross , Peter Zijlstra , Andy Lutomirski , llvm@lists.linux.dev, LKML , linux-toolchains Subject: Re: [PATCH v5] x86: use builtins to read eflags Message-ID: <20220318000540.GO614@gate.crashing.org> References: <20220317231959.GN614@gate.crashing.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Thu, Mar 17, 2022 at 04:31:19PM -0700, Linus Torvalds wrote: > On Thu, Mar 17, 2022 at 4:25 PM Segher Boessenkool > wrote: > > > > > I still think that from a sanity standpoint, it would be good to > > > actually strengthen the semantics of "asm volatile" to literally act > > > as - and be ordered with - volatile memory accesses. > > > > > > But I guess that's water under the bridge. > > > > That is what it has actually done since forever. See C 5.1.2.3. For > > GCC, "asm volatile" has a side effect like in /2 there as well, as does > > unspec_volatile (an internal GCC thing used to implement certain > > builtins, among other things). > > Oh, so two "asm volatile" statements _are_ in fact defined to be > ordered wrt each other? Colloquially you could say that. But that statement can be understood to mean more than the actual guarantee (and it is also much less than it, oin the positive side). > Because the gcc docs certainly don't say that ;( Older GCC docs said You will also want to add the volatile keyword if the memory affected is not listed in the inputs or outputs of the asm, as the `memory' clobber does not count as a side-effect of the asm. and The volatile keyword indicates that the instruction has important side-effects. GCC will not delete a volatile asm if it is reachable. (The instruction can still be deleted if GCC can prove that control-flow will never reach the location of the instruction.) Note that even a volatile asm instruction can be moved relative to other code, including across jump instructions. and Similarly, you can't expect a sequence of volatile asm instructions to remain perfectly consecutive. If you want consecutive output, use a single asm. Also, GCC will perform some optimizations across a volatile asm instruction; GCC does not “forget everything” when it encounters a volatile asm instruction the way some other compilers do. The internal assembler docs were rewritten later, because they were considered too terse, too hard for users to understand. But they may have lost some clarity in the process. > Yeah, yeah, dead code can be removed, whether volatile or not. That's > true of "*(volatile int *)x" too. It's not the dead code that is the > interesting case, though.. Internally to GCC this is the same in most ways, too; it's no accident this things correspond so well :-) > Is this also well-defined ordering-wise: > > asm volatile("do_something"); > WRITE_ONCE(x, 1); > > (where "WRITE_ONCE()" is that kernel macro that just uses a volatile > pointer assignment to force the access)? The macro in asm-generic is #define __WRITE_ONCE(x, val) \ do { \ *(volatile typeof(x) *)&(x) = (val); \ } while (0) which will work fine given the undocumented C extension I mentioned in https://gcc.gnu.org/PR33053 (this will be part of C2x as well btw). I don't know if all arch-specific versions are fine. > And could we get that documented? Could you open a GCC PR for it? I can do it, but you know better what you want, and I might forget, etc. Segher