Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1625142pxf; Fri, 9 Apr 2021 13:11:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw4r/JyQwvtfjAkYooU1ZfNPCfwXflzKDsdlZWOCWzi7BB5yokEC6VeuZ9B3FM84wibGyRY X-Received: by 2002:a05:6402:1013:: with SMTP id c19mr19112533edu.213.1617999112563; Fri, 09 Apr 2021 13:11:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617999112; cv=none; d=google.com; s=arc-20160816; b=ZA0Rezi/I7SQg/WkqrPWmEGEKConrpBU0+wkSUhggIiX/6NZNxfmYFjUGqdnGsI+TU Vl2qLGnGYAlovA92bmvqKQbqDY7Gc8R10s4/I62INcdKQjESqBOhpAZqE4GIrChqH3gr PRRGh4LX1Gir1ml1xxuBzsYeUbZfisGiiDyhIzAPrsNj25rLJHVjGv4m71sH/XfHJwt1 /8GtPKUfCbKBK6xospgTL1pKw6FaUaHDTKKTvDL4czuCNXJYqR00xpywTfxAQ67oRL/T DxqfGorhe69aJdOy2j4g70bB3yH2/pQ36g7EAjYEKPL2mMUamziQg9uGurdXNofuxTkc gCjg== 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:message-id:subject:cc:to:from:date:dkim-signature; bh=RTRfESDMgp5WTWcYaOiBYP6gw6aHG5qvZbrxxN3CTDs=; b=dP/FiLTjC2LgGEzm+KysluubwIbxZ5qLGUN8Z24ywX3l5SuK0y3+nXz5V0rE0FsPvK 39vv7NLrjUPRFUUF524ckUmUrx+QeJ5wU8w0IOgUNdoPbaWBBj8ze0IuEbTg0FA53YQD uymm/v4rqBNZgF5CQaGktsR3h+rcEy3wMbSkv0h3wnMjx6O9FbU/Kx7bhc8O833ifZjo 08QcxlMeNHgWoICIMARDOYrP0DptHr4VexC+DH8cebGq83FusP1WlVv5J4YLQ04JF9JN 8UXvtUk5pU32gtc84ML5Zu16wS8nhATcI56aOfCKW2m/dZK1IFHBlv6/d/x/cDgKEQSP hvFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=t5U1qHTI; 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 c20si2808467eds.379.2021.04.09.13.11.29; Fri, 09 Apr 2021 13:11:52 -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=@infradead.org header.s=casper.20170209 header.b=t5U1qHTI; 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 S233993AbhDIUKq (ORCPT + 99 others); Fri, 9 Apr 2021 16:10:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232796AbhDIUKp (ORCPT ); Fri, 9 Apr 2021 16:10:45 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49EE5C061762; Fri, 9 Apr 2021 13:10:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=RTRfESDMgp5WTWcYaOiBYP6gw6aHG5qvZbrxxN3CTDs=; b=t5U1qHTIa8JHz3diU+neb+7sk3 E/T3zqkQdnaVCIrD7NeTNSEGUiq9rLD3FXlB0/K2BHFvGEbQHTBXOyScmzpGMaMgH7uFFnbpIozrq 2JGRHy52WlsNCoXc1zbvfNomPmlX9ey+VzVHz5tYATnfk6y6zoV8a5v0Fb9kBxQiiGhPnbBcGfvJ1 5yBiFlqe8IHPWdWg9eVui0zY2jzPefxdWHq4QRz3L1ewNrAEb2VplLLBpM+I9i3lr4OvkBHnOtjQM vFzYHMQTDgFBRNGsviKw9Oj/rSWhg3yvnhCQWKe5Vv55/au5VhHHAFF48YGX0lvZPAGCT1zul244T uR6FxTyA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lUxRY-000sNh-2n; Fri, 09 Apr 2021 20:09:32 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id E77A5300033; Fri, 9 Apr 2021 22:09:26 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id CB94B2C6D7C6F; Fri, 9 Apr 2021 22:09:26 +0200 (CEST) Date: Fri, 9 Apr 2021 22:09:26 +0200 From: Peter Zijlstra To: David Malcolm Cc: Ard Biesheuvel , linux-toolchains@vger.kernel.org, Linux Kernel Mailing List , Josh Poimboeuf , Jason Baron , "Steven Rostedt (VMware)" Subject: Re: static_branch/jump_label vs branch merging Message-ID: References: <5f78b7e2f9ae937271ef52ee9e999a91c2719da9.camel@redhat.com> <3c062f70ffef2dcd48a661f7c8162fb8fbaf6869.camel@redhat.com> <0a9da587b0330bafdf612c3d51285e144b0b9e46.camel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0a9da587b0330bafdf612c3d51285e144b0b9e46.camel@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 09, 2021 at 03:21:49PM -0400, David Malcolm wrote: > [Caveat: I'm a gcc developer, not a kernel expert] > > But it's not *quite* a global constant, or presumably you would be > simply using a global constant, right? As the optimizer gets smarter, > you don't want to have it one day decide that actually it really is > constant, and optimize away everything at compile-time (e.g. when LTO > is turned on, or whatnot). Right; as I said, the result is not a constant, but any invocation ever, will return the same result. Small but subtle difference :-) > I get the impression that you're resorting to assembler because you're > pushing beyond what the C language can express. Of course :-) I tend to always push waaaaay past what C considers sane. Lets say I'm firmly in the C-as-Optimizing-Assembler camp :-) > Taking things to a slightly higher level, am I right in thinking that > what you're trying to achieve is a control flow construct that almost > always takes one of the given branches, but which can (very rarely) be > switched to permanently take one of the other branches, and that you > want the lowest possible overhead for the common case where the > control flow hasn't been touched yet? Correct, that's what it is. We do runtime code patching to flip the branch if/when needed. We've been doing this for many many years now. The issue of today is all this clever stuff defeating some simple optimizations. > (and presumably little overhead for when it > has been?)... and that you want to be able to merge repeated such > conditionals. This.. So the 'static' branches have been upstream and in use ever since GCC added asm-goto, it was in fact the driving force to get asm-goto implemented. This was 2010 according to git history. So we emit, using asm goto, either a "NOP5" or "JMP.d32" (x86 speaking), and a special section entry into which we encode the key address and the instruction address and the jump target. GCC, not knowing what the asm does, only sees the 2 edges and all is well. Then, at runtime, when we decide we want the other edge for a given key, we iterate our section and rewrite the code to either nop5 or jmp.d32 with the correct jump target. > It's kind of the opposite of "volatile" - something that the user is > happy for the compiler to treat as not changing much, as opposed to > something the user is warning the compiler about changing from under > it. A "const-ish" value? Just so. Encoded in text, not data. > Sorry if I'm being incoherent; I'm kind of thinking aloud here. No problem, we're way outside of what is generally considered normal, and I did somewhat assume people were familiar with our 'dodgy' construct (some on this list are more than others). I hope it's all a little clearer now.