Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1228415imm; Wed, 10 Oct 2018 11:05:14 -0700 (PDT) X-Google-Smtp-Source: ACcGV62l79EpzRvKEsNesSuy0Yt9mCJ6CqQHO9SgOTGfcbzdnuMeEGxBWgiE1Yt6y+QVKeMLOF4S X-Received: by 2002:a65:62d5:: with SMTP id m21-v6mr31199139pgv.243.1539194714486; Wed, 10 Oct 2018 11:05:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539194714; cv=none; d=google.com; s=arc-20160816; b=0o9P6SV6fq2NyZysbzNydXBiSZrm+zbmDHOf+jCz2BxTKqefNcgyd5JaXEbHEDKmAU VJ4tu9wZR6oQRr816AwbeeFo4ws++Lqfl6NOy6oirKMwBIpQXbBfLPvenujW1kb72eHT cWiEvtp+wJ+gNRLEaDmhhMgY/1qrISHvqk00CWkUPYh51KEcSly4YOq8kVkWYbIGxfNL 72S5/Ym+cydM6RZmmSBJh6epAwCkTrm/3t0fFw6vr3BGhgVhR0QEXQSEagBbibp4RndS aIOCKnSr8bOOlC16ujbVOoDfZKSi2Dj0vVcWw54jGqWLbVcO98l271LgKeUUgpM5MPDa 5Pxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=1shNk7n2oOgD9+BO00oSqO5Hb98+635hkuFoxp4xFkI=; b=qYXUwHQlHG/jlAIp5ERUg/uZO1ChFXUok4zrmS6PM8n90J8VvUTJpb6fH084UaivRW aG3wCc7ftu4SrJW2XuUX04bDbxFLNHVCpXqgs/7C+6XnG1a8hzeeArb/owufcgf17iIv 0eNX0ukip6Tz61vpe4z4BRnhPiC5iA9ngP7N1O0AeZro8ODeGxRfWfjg1hZteflh0TnJ 5863NBGroQH1AVHcXJ6Nej/gi8OILeOX8TDvNZb/0FPVA8a54owtnB56k4Qg882yr8Xs Dy2yw7ogQ5zctOtd9NR+ncVye8KEsEep2ur/TjJ8QOPXmlDDN002WCQQci/BLwZPJ5+d VCvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=MBVoqKuq; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k5-v6si22341261pgo.312.2018.10.10.11.04.59; Wed, 10 Oct 2018 11:05:14 -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; dkim=pass header.i=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=MBVoqKuq; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726728AbeJKB1N (ORCPT + 99 others); Wed, 10 Oct 2018 21:27:13 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:40280 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726537AbeJKB1N (ORCPT ); Wed, 10 Oct 2018 21:27:13 -0400 Received: by mail-wr1-f68.google.com with SMTP id d2-v6so6762667wro.7 for ; Wed, 10 Oct 2018 11:03:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amacapital-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1shNk7n2oOgD9+BO00oSqO5Hb98+635hkuFoxp4xFkI=; b=MBVoqKuqCpi/HjZ28fUwBU/eHrKf7RnLF90cKeOgJ7vfBGIhQmWMlZUNg3SvjKeI/v RZH9bEQu10rjQd4P45imZQeEXALihEDVhJlOQpr+NoGcbewcgZam4Lw+2TChUFnYBmBY vDnwpRKCNo757wPMYDolbu9yZABRKnkPyLk171p8+bUOCyR9HdpmCSTNeihQ2mLcmdcp cDqMcYIC0A2LhvIXh582ojR0bGpI/H8DifqxS4zk0GiHc+Gb0qw5O2Pc/BvZgB62tITt iKo9GYVOSsJb1eOYPIH4+Y1hTuzLXPKQoQbRD/JPmR39Lb76dbgc6hDRYkMdQpGIr3Ab 8Ssw== 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=1shNk7n2oOgD9+BO00oSqO5Hb98+635hkuFoxp4xFkI=; b=n4Od97ToDk+WPsfpsc425OqL6ar4ydLwJnjpvXFL0UmrPylFyDP/GBOgcQAHNPPZ6Y /wYfN5po9wVE6mJkJ8ISHhQmTYDLAbFfedNkurDZU/8srXTOAjDwyIwiDWH/KqvN7l1A NA18AKV/2FfWjdLyNEFcFuwWu4131XBa8zKZHVShcOSNyrmIB2QEKlU2tHWvnVv8QWft gWRKtZsAEFI08o57EEm3hTYaJihh+yBrW4uIJN8WcDM3RINjietO8UuUOJAFS3lC/Tsn LFpeN1yHDFsJf8qzDgPFJcPF9uTfW1vVqA+qW+CQ12bin23hhSIsDYlH2mr5byQHlx5a IpYg== X-Gm-Message-State: ABuFfoiNguvRFNs+eEXv2BRj8AdUd4r+iDRq2roF7/OXjEPpCYu4uhw4 NUzXyg0e4OQAoH1H/iAfk2JSiosEWsOV0KcYMVrqUg== X-Received: by 2002:adf:82e3:: with SMTP id 90-v6mr23421227wrc.131.1539194635834; Wed, 10 Oct 2018 11:03:55 -0700 (PDT) MIME-Version: 1.0 References: <20181006015110.653946300@goodmis.org> <20181006015720.634688468@goodmis.org> <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> In-Reply-To: <20181010175237.e7m3sldcu2maoqcq@treble> From: Andy Lutomirski Date: Wed, 10 Oct 2018 11:03:43 -0700 Message-ID: Subject: Re: [POC][RFC][PATCH 1/2] jump_function: Addition of new feature "jump_function" To: Josh Poimboeuf 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 Content-Type: text/plain; charset="UTF-8" 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 10:52 AM Josh Poimboeuf wrote: > > On Mon, Oct 08, 2018 at 11:57:50PM -0400, Steven Rostedt wrote: > > On Mon, 8 Oct 2018 21:17:10 -0500 > > Josh Poimboeuf wrote: > > > > > I'm not really convinced we need objtool for this, maybe I'll try > > > whipping up a POC. > > > > Awesome! > > > > I wasn't thinking of actually having objtool itself perform this task, > > but instead breaking the internals of objtool up into more of a generic > > infrastructure, that recordmcount.c, objtool, and whatever this does > > can use. > > So I had been thinking that we could find the call sites at runtime, by > looking at the relocations. But I managed to forget that vmlinux > relocations are resolved during linking. So yeah, some kind of tooling > magic would be needed. > > I worked up a POC using objtool. It doesn't *have* to be done with > objtool, but since it's already reading/writing all the ELF stuff > anyway, it was pretty easy to add this on. > > This patch has at least a few issues: > > - No module support. > > - For some reason, the sync_cores in text_poke_bp() don't always seem to > be working as expected. Running this patch on my VM, the test code in > cmdline_proc_show() works *most* of the time, but it occasionally > branches off into the weeds. I have no idea what the problem is yet. > > diff --git a/arch/Kconfig b/arch/Kconfig > index 9d329608913e..20ff5624dad7 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -865,6 +865,9 @@ config HAVE_ARCH_PREL32_RELOCATIONS > architectures, and don't require runtime relocation on relocatable > kernels. > > +config HAVE_ARCH_STATIC_CALL > + bool > + > source "kernel/gcov/Kconfig" > > source "scripts/gcc-plugins/Kconfig" > diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig > index 5136a1281870..1a14c8f87876 100644 > --- a/arch/x86/Kconfig > +++ b/arch/x86/Kconfig > @@ -128,6 +128,7 @@ config X86 > select HAVE_ARCH_COMPAT_MMAP_BASES if MMU && COMPAT > select HAVE_ARCH_PREL32_RELOCATIONS > select HAVE_ARCH_SECCOMP_FILTER > + select HAVE_ARCH_STATIC_CALL if X86_64 > select HAVE_ARCH_THREAD_STRUCT_WHITELIST > select HAVE_ARCH_TRACEHOOK > select HAVE_ARCH_TRANSPARENT_HUGEPAGE > diff --git a/arch/x86/include/asm/static_call.h b/arch/x86/include/asm/static_call.h > new file mode 100644 > index 000000000000..40fec631b760 > --- /dev/null > +++ b/arch/x86/include/asm/static_call.h > @@ -0,0 +1,35 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef _ASM_STATIC_CALL_H > +#define _ASM_STATIC_CALL_H > + > +#ifdef CONFIG_X86_64 > + > +#include > + > +void static_call_init(void); > +extern void __static_call_update(void *tramp, void *func); > + > +#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? > +#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.) 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. > +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: > + 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 :)