Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1242297imm; Wed, 10 Oct 2018 11:18:15 -0700 (PDT) X-Google-Smtp-Source: ACcGV61Apd1ZszoFzdKC7MWoYQOGyXoYVWS8s7PWcljcJj/8b9ArUM47jDfTEtqAfwvFGuRoJsPq X-Received: by 2002:a63:6ec4:: with SMTP id j187-v6mr31122644pgc.3.1539195495578; Wed, 10 Oct 2018 11:18:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539195495; cv=none; d=google.com; s=arc-20160816; b=bmzZG5SG7jL3ElS8SI6AyW+0m4QjuJIMDnSkn7Ze21kzdJjkMh8ckKH22owk/zsycn N5KAf5coMNhk/7fo951L2YvfQ5JyIMHDFj7ahHLeI+zypRuNqYWWnCMqmZ3iARGdFeU/ m/9xALKLUSYaEGqiwtnUJidinuiyfHwpYI3yfrxXv0hpfQVj/WLlVKC7Z5ldQTznPetD aIB/kEzxTVe47OIPdPq/SyIKjMnzJa8hbPIA2Pb7vN9bUW5CxiNjlLd5NNxnNUh3a71E 1HdyILwzJHZaqxQzmaLMI+5qDuPUCrjrewyd8ji4EnlZxHIMnEobDq0WQebCmbm/eUdv AaBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=zkX1hf12YnY3S7j9Mw3mhQckEVzmKBoTu+bIhKXCjMU=; b=DzlzMx73Gui3hxlwEWKt6wmfWGrBF4Jsoxh7KsXP0cjwwrIOGrnACne4ldDnu6Z9x3 dCDERFAA5VwOBttLsiGG/dfFMc/Y6WrmUsEY3k/VGIA0+J3uuSdittYt+cnjTUKcPcNK +ezNvWkUep7LwLfeCHLBdz4Qn5LY13OHj+OwVnPy1p4H1sRYfgWiNbzjg90bLYvdazNA tflJsV01T3jMHBebryRKYwq3E86FSPHdOIESs7ODGGXUXQK8Qxxg3BzhwhWBN8yxe2jJ yOHZbg/u/GIy769Oq2GGTEx3jTXAjppE1b5yVOljwiJHSyAbzLs2tUa0GOe7EQxcXhyH r8CA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j5-v6si9011190pfb.211.2018.10.10.11.18.00; Wed, 10 Oct 2018 11:18:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726715AbeJKBje (ORCPT + 99 others); Wed, 10 Oct 2018 21:39:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40280 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726537AbeJKBje (ORCPT ); Wed, 10 Oct 2018 21:39:34 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A226536A5E6; Wed, 10 Oct 2018 18:16:15 +0000 (UTC) Received: from treble (ovpn-124-83.rdu2.redhat.com [10.10.124.83]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D260680D8C; Wed, 10 Oct 2018 18:16:12 +0000 (UTC) Date: Wed, 10 Oct 2018 13:16:05 -0500 From: Josh Poimboeuf To: Andy Lutomirski Cc: Steven Rostedt , Peter Zijlstra , LKML , Linus Torvalds , Ingo Molnar , Andrew Morton , Thomas Gleixner , Masami Hiramatsu , Mathieu Desnoyers , Matthew Helsley , "Rafael J. Wysocki" , David Woodhouse , Paolo Bonzini , Jason Baron , Jiri Kosina , Ard Biesheuvel , Andrew Lutomirski Subject: Re: [POC][RFC][PATCH 1/2] jump_function: Addition of new feature "jump_function" Message-ID: <20181010181605.arsyjxwdztztrjih@treble> References: <20181006121211.GA5663@hirez.programming.kicks-ass.net> <20181006093905.46276505@vmware.local.home> <20181008072134.GB5663@hirez.programming.kicks-ass.net> <20181008155757.GC5663@hirez.programming.kicks-ass.net> <20181009021710.qwt5hpntyeps44h3@treble> <20181008235750.59da83ae@gandalf.local.home> <20181010175237.e7m3sldcu2maoqcq@treble> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 10 Oct 2018 18:16:15 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 10, 2018 at 11:03:43AM -0700, Andy Lutomirski wrote: > > +#define DECLARE_STATIC_CALL(tramp, func) \ > > + extern typeof(func) tramp; \ > > + static void __used __section(.discard.static_call_tramps) \ > > + *__static_call_tramp_##tramp = tramp > > + > > Confused. What's the __static_call_tramp_##tramp variable for? And > why is a DECLARE_ macro defining a variable? This is the magic needed for objtool to find all the call sites. The variable itself isn't needed, but the .discard.static_call_tramps entry is. Objtool reads that section to find out which function call sites are targeted to a static call trampoline. > > +#define DEFINE_STATIC_CALL(tramp, func) \ > > + DECLARE_STATIC_CALL(tramp, func); \ > > + asm(".pushsection .text, \"ax\" \n" \ > > + ".align 4 \n" \ > > + ".globl " #tramp " \n" \ > > + ".type " #tramp ", @function \n" \ > > + #tramp ": \n" \ > > + "jmp " #func " \n" \ > > I think this would be nicer as an indirect call that gets patched to a > direct call so that the update mechanism works even before it's > initialized. (Currently static_branch blows up horribly if you try to > update one too early, and that's rather annoying IMO.) Yeah, that would be better. It would also allow trampoline function pointers to work, which I think you mentioned elsewhere. And then I shouldn't trample this code in __static_call_update() -- that was already kind of nasty anyway. > Also, I think you're looking for "jmp.d32", which is available in > newer toolchains. For older toolchains, you could use .byte 0xe9 or > you could use a different section (I think) to force a real 32-bit > jump. Good idea. > > +void __init static_call_init(void) > > +{ > > + struct static_call_entry *entry; > > + unsigned long insn, tramp, func; > > + unsigned char insn_opcode, tramp_opcode; > > + s32 call_dest; > > + > > + for (entry = __start_static_call_table; > > + entry < __stop_static_call_table; entry++) { > > + > > + insn = (long)entry->insn + (unsigned long)&entry->insn; > > + tramp = (long)entry->tramp + (unsigned long)&entry->tramp; > > + > > + insn_opcode = *(unsigned char *)insn; > > + if (insn_opcode != 0xe8 && insn_opcode != 0xe9) { > > + WARN_ONCE(1, "unexpected static call insn opcode %x at %pS", > > + insn_opcode, (void *)insn); > > + continue; > > + } > > + > > + tramp_opcode = *(unsigned char *)tramp; > > + if (tramp_opcode != 0xeb && tramp_opcode != 0xe9) { > > + WARN_ONCE(1, "unexpected trampoline jump opcode %x at %ps", > > + tramp_opcode, (void *)tramp); > > + continue; > > + } > > + > > + if (tramp_opcode == 0xeb) > > + func = *(s8 *)(tramp + 1) + (tramp + 2); > > I realize you expect some instances of 0xeb due to the assembler > messing you up (see above), but this seems a bit too permissive, since > a 0xeb without the appropriate set of NOPs is going to explode. And: Yep. > > + else > > + func = *(s32 *)(tramp + 1) + (tramp + 5); > > + > > + call_dest = (long)(func) - (long)(insn + 5); > > + > > + printk("static_call_init: poking %lx at %lx\n", (unsigned long)call_dest, (insn+1)); > > + > > + text_poke_early((void *)(insn + 1), &call_dest, 4); > > If you really are going to rewrite an 8-bit jump to a 32-bit jump, I > think you need to actually patch the opcode byte :) Oops :-) -- Josh