From patchwork Thu Oct 24 07:08:04 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 3090341 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B1F5C9F372 for ; Thu, 24 Oct 2013 07:10:03 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 74E6D20398 for ; Thu, 24 Oct 2013 07:10:02 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0795120397 for ; Thu, 24 Oct 2013 07:10:01 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VZF3H-0000vB-Q1; Thu, 24 Oct 2013 07:09:51 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VZF3B-0007BJ-Mn; Thu, 24 Oct 2013 07:09:45 +0000 Received: from mail-pa0-f48.google.com ([209.85.220.48]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VZF32-00079B-8w for linux-arm-kernel@lists.infradead.org; Thu, 24 Oct 2013 07:09:44 +0000 Received: by mail-pa0-f48.google.com with SMTP id bj1so2578304pad.35 for ; Thu, 24 Oct 2013 00:09:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FLe15xFUEmGsS9xljMGdlz96oh22I9bxxzmf3fJV2Rk=; b=iGzopyoBltFqysH9p7ggaOkrZ4KGo8zgmVs11m6CCD4bLYDSiGPVxUclo5dxEGvMDr LtLjFTczzl92BmOipADcDgiXt+cYpQ7m6u1eRhZW0tPFOF0cbycya53khibMSERgsKER IjIkBlg0IFQDVgkhKt+lqRQz8P9Q/eySKgWn5IRLW6qon7uTslZfV/r4/gHdjF2IXa4w TNg3xsBzQDVSkNHVPaV2uGwFgKQ284uOvA0qCzQ7asqzeba8SFBJvNBph6Xgo3VTRQDQ TvC2LmZFT5pOgBehS+rrnxDRBeihLeu3bwnt4/votaW79iJaZebfhSc9s/nOq0OIjfrn 7HfA== X-Gm-Message-State: ALoCoQmzkt+Pshqd+WP51u+/8NvNkYiCaSDDWbrr8KnxF4qovtB3zJyEmYIX5/5hvox38U53n2cn X-Received: by 10.68.170.163 with SMTP id an3mr1300944pbc.91.1382598554823; Thu, 24 Oct 2013 00:09:14 -0700 (PDT) Received: from localhost.localdomain (KD182249089040.au-net.ne.jp. [182.249.89.40]) by mx.google.com with ESMTPSA id go4sm374799pbb.15.2013.10.24.00.09.10 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Oct 2013 00:09:13 -0700 (PDT) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com Subject: [PATCH v2 2/6] arm64: ftrace: Add dynamic ftrace support Date: Thu, 24 Oct 2013 16:08:04 +0900 Message-Id: <1382598488-13511-3-git-send-email-takahiro.akashi@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1382598488-13511-1-git-send-email-takahiro.akashi@linaro.org> References: <1382598488-13511-1-git-send-email-takahiro.akashi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20131024_030942_907416_1D80AC9E X-CRM114-Status: GOOD ( 17.49 ) X-Spam-Score: -2.6 (--) Cc: gkulkarni@caviumnetworks.com, AKASHI Takahiro , linaro-kernel@lists.linaro.org, linux-arm-kernel@lists.infradead.org, patches@linaro.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This enables DYNAMIC_FTRACE configuration. ftrace_caller() is the entry function which is a replacement of _mcount(). If tracing is enabled on a function, the branch instruction to _mcount() is replaced to that of ftace_caller() and if disabled, replaced to nop. See Documentation/trace/ftrace-design.txt Signed-off-by: AKASHI Takahiro --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/ftrace.h | 17 +++++++ arch/arm64/kernel/entry-ftrace.S | 39 +++++++++++++++ arch/arm64/kernel/ftrace.c | 103 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 3776319..c150430 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -23,6 +23,7 @@ config ARM64 select HAVE_DEBUG_KMEMLEAK select HAVE_DMA_API_DEBUG select HAVE_DMA_ATTRS + select HAVE_DYNAMIC_FTRACE select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER select HAVE_GENERIC_DMA_COHERENT diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrace.h index 0d5dfdb..37a379d 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -17,6 +17,23 @@ #ifndef __ASSEMBLY__ extern void _mcount(unsigned long); + +#ifdef CONFIG_DYNAMIC_FTRACE +struct dyn_arch_ftrace { + /* No extra data needed for arm64 */ +}; + +extern unsigned long ftrace_graph_call; + +static inline unsigned long ftrace_call_adjust(unsigned long addr) +{ + /* + * addr is the address of the mcount call instruction. + * recordmcount does the necessary offset calculation. + */ + return addr; +} +#endif /* CONFIG_DYNAMIC_FTRACE */ #endif /* __ASSEMBLY__ */ #endif /* CONFIG_FUNCTION_TRACER */ diff --git a/arch/arm64/kernel/entry-ftrace.S b/arch/arm64/kernel/entry-ftrace.S index ae14ece..c35c650 100644 --- a/arch/arm64/kernel/entry-ftrace.S +++ b/arch/arm64/kernel/entry-ftrace.S @@ -88,6 +88,7 @@ add \reg, \reg, #8 .endm +#ifndef CONFIG_DYNAMIC_FTRACE /* * void _mcount(unsigned long return_address) * @return_address: return address to instrumented function (callsite) @@ -132,6 +133,44 @@ skip_ftrace_call: #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ ENDPROC(_mcount) +#else /* CONFIG_DYNAMIC_FTRACE */ +/* + * This code is executed only at boot time. + * Once after initialized, "bl _mcount" generated by gcc -pg option will be + * replaced to "nop" if tracing is disabled or "b ftrace_caller" if enabled. + */ +ENTRY(_mcount) + ret +ENDPROC(_mcount) + +/* + * void ftrace_caller(unsigned long return_address) + * @return_address: return address to instrumented function (callsite) + * + * Call any kind of tracer function and/or function graph tracer. + */ +ENTRY(ftrace_caller) + mcount_enter + + mcount_get_pc0 x0 // pc in callsite + mcount_get_lr x1 // callsite's lr (adjusted) + + .global ftrace_call +ftrace_call: // tracer(pc, lr); + nop // This will be replaced with "bl xxx" + // where xxx can be any kind of tracer. + +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + .global ftrace_graph_call +ftrace_graph_call: // ftrace_graph_caller(); + nop // If enabled, this will be replaced + // "b ftrace_graph_caller" +#endif + + mcount_exit +ENDPROC(ftrace_caller) +#endif /* CONFIG_DYNAMIC_FTRACE */ + ENTRY(ftrace_stub) ret ENDPROC(ftrace_stub) diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c index e779e16..da481c1 100644 --- a/arch/arm64/kernel/ftrace.c +++ b/arch/arm64/kernel/ftrace.c @@ -17,6 +17,76 @@ #include #include +#ifdef CONFIG_DYNAMIC_FTRACE +/* FIXME: can be replaced by aarch64_insn_patch_text_nosync() */ +static int ftrace_modify_code(unsigned long pc, unsigned int old, + unsigned int new, bool validate) +{ + unsigned int replaced; + +#ifdef __AARCH64EB__ + old = swab32(old); + new = swab32(new); +#endif + + if (validate) { + if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE)) + return -EFAULT; + + if (replaced != old) + return -EINVAL; + } + + if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE)) + return -EPERM; + + flush_icache_range(pc, pc + MCOUNT_INSN_SIZE); + + return 0; +} + +int ftrace_update_ftrace_func(ftrace_func_t func) +{ + unsigned long pc; + unsigned int new; + + pc = (unsigned long)&ftrace_call; + new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func, true); + + return ftrace_modify_code(pc, 0, new, false); +} + +int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) +{ + unsigned long pc = rec->ip; + unsigned int old, new; + + old = aarch64_insn_gen_nop(); + new = aarch64_insn_gen_branch_imm(pc, addr, true); + + return ftrace_modify_code(pc, old, new, true); +} + +int ftrace_make_nop(struct module *mod, + struct dyn_ftrace *rec, unsigned long addr) +{ + unsigned long pc = rec->ip; + unsigned int old, new; + + old = aarch64_insn_gen_branch_imm(pc, addr, true); + new = aarch64_insn_gen_nop(); + + return ftrace_modify_code(pc, old, new, true); +} + +int __init ftrace_dyn_arch_init(void *data) +{ + *(unsigned long *)data = 0; + + return 0; +} +#endif /* CONFIG_DYNAMIC_FTRACE */ + #ifdef CONFIG_FUNCTION_GRAPH_TRACER void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, unsigned long frame_pointer) @@ -80,4 +150,37 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, return; } } + +#ifdef CONFIG_DYNAMIC_FTRACE +static int __ftrace_modify_caller(unsigned long *callsite, + void (*func) (void), bool enable) +{ + unsigned long pc = (unsigned long)callsite; + unsigned int branch, nop, old, new; + + branch = aarch64_insn_gen_branch_imm(pc, (unsigned long)func, false); + nop = aarch64_insn_gen_nop(); + old = enable ? nop : branch; + new = enable ? branch : nop; + + return ftrace_modify_code(pc, old, new, true); +} + +static int ftrace_modify_graph_caller(bool enable) +{ + return __ftrace_modify_caller(&ftrace_graph_call, + ftrace_graph_caller, + enable); +} + +int ftrace_enable_ftrace_graph_caller(void) +{ + return ftrace_modify_graph_caller(true); +} + +int ftrace_disable_ftrace_graph_caller(void) +{ + return ftrace_modify_graph_caller(false); +} +#endif /* CONFIG_DYNAMIC_FTRACE */ #endif /* CONFIG_FUNCTION_GRAPH_TRACER */