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: 21239 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-vb0-f69.google.com (mail-vb0-f69.google.com [209.85.212.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id C87E020F5A for ; Thu, 24 Oct 2013 07:09:16 +0000 (UTC) Received: by mail-vb0-f69.google.com with SMTP id e12sf4573714vbg.8 for ; Thu, 24 Oct 2013 00:09:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=mime-version:x-gm-message-state:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=FLe15xFUEmGsS9xljMGdlz96oh22I9bxxzmf3fJV2Rk=; b=aMWB2sB32JajRECUQXF5JXLh4q7HpHp6LNuToHgEzP94wuOzVXhjk/X+AXDY6jUyDF QVOvOshQ/8D4AFpZUOpMnPHtYAOTk6ObRWyrvg8LoNkMi+gL1RMIFQGejKTMtqGgR8XJ pTEENva9FK1OqnPBsFCXF/evNuJauBj3/54tLR5u6pYAOuho/ahezaqC7bCFCOj9B1TK L8RLNnN9Sx34olRF1R80J0WKw5XcMZsQt7wHHiyHaTyN3KlKKRBmKT8Z5PmM4VHXn/b/ 7mXOAF0vtNIzvgXb33dnHjg532Ct4gTvnZ96ko6tZQyurlla0rsS8Z+nqf+DvshnV3f0 gJlA== X-Received: by 10.58.133.197 with SMTP id pe5mr474354veb.6.1382598556378; Thu, 24 Oct 2013 00:09:16 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.74.35 with SMTP id q3ls782856qev.50.gmail; Thu, 24 Oct 2013 00:09:16 -0700 (PDT) X-Received: by 10.58.66.233 with SMTP id i9mr609407vet.29.1382598556277; Thu, 24 Oct 2013 00:09:16 -0700 (PDT) Received: from mail-ve0-f170.google.com (mail-ve0-f170.google.com [209.85.128.170]) by mx.google.com with ESMTPS id gq10si33139vdc.95.2013.10.24.00.09.16 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Oct 2013 00:09:16 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.170 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.170; Received: by mail-ve0-f170.google.com with SMTP id db12so1305645veb.1 for ; Thu, 24 Oct 2013 00:09:16 -0700 (PDT) X-Gm-Message-State: ALoCoQmZ42EidwZGsBGy7vFaKW8akRt1UUuKUjWK5b9EdWiVRU1wqYBMrEVsjVKPmqKbfaKjbAY7 X-Received: by 10.58.233.98 with SMTP id tv2mr617349vec.11.1382598556159; Thu, 24 Oct 2013 00:09:16 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp368358vcz; Thu, 24 Oct 2013 00:09:15 -0700 (PDT) X-Received: by 10.68.194.230 with SMTP id hz6mr1343778pbc.55.1382598555248; Thu, 24 Oct 2013 00:09:15 -0700 (PDT) Received: from mail-pa0-f49.google.com (mail-pa0-f49.google.com [209.85.220.49]) by mx.google.com with ESMTPS id hb3si1072440pac.152.2013.10.24.00.09.14 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Oct 2013 00:09:15 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.49 is neither permitted nor denied by best guess record for domain of takahiro.akashi@linaro.org) client-ip=209.85.220.49; Received: by mail-pa0-f49.google.com with SMTP id lj1so2059586pab.22 for ; Thu, 24 Oct 2013 00:09:14 -0700 (PDT) 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 Cc: linaro-kernel@lists.linaro.org, patches@linaro.org, linux-arm-kernel@lists.infradead.org, gkulkarni@caviumnetworks.com, AKASHI Takahiro 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-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: takahiro.akashi@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.170 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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 */