Received: by 10.223.148.5 with SMTP id 5csp7697178wrq; Thu, 18 Jan 2018 08:28:49 -0800 (PST) X-Google-Smtp-Source: ACJfBosVzir2/kzYRny0cVjCSfB7MAfNxqxfkEi7BU9wZtaHG1UYp+Xb4vNgcrPJnZ2XdZIYdpUi X-Received: by 10.101.86.201 with SMTP id w9mr4442120pgs.434.1516292929070; Thu, 18 Jan 2018 08:28:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516292929; cv=none; d=google.com; s=arc-20160816; b=OpVYC2JT832iYXGGV7vpkFsDnwzrujSeRs8DUoIuIRC8TmOkeRO09qrWt2bgw/vJZz nqL4EMSqBu7ren3BVIlpZrzi92oK/uUFcNxfnAeGr/PGPVkNoUNL2mAni2ieda4aZEsO tHxUTnDZHwWsfYqq6tDGzOEMxnJXGc2uxJrvrSyve2/PuniEWB3qhxNjH5rs0/f1NkRk iH0EgWp9li5SQehsfGgNDryNZtKDTGbAkdRJgYwhh89k7r+eLpTtIduLB/OwGGIqM5D8 nooiu9r8frrDRxRKo2xAAy4t+BjicvkVs9opNyYU1W+QnhrhPsXS8enSQTRtGBvlZxEu ptwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=ksGzLWU1MXPP/8UuLpYwHIOcNukZzvd41NXa6a/NEY4=; b=OL76uI6N6qJmlBHTBf1o4dV7EAkHuZ1vopxgjcLoVSh2ufllqIopqXVmT0RaWALEQu sQJmoSLvIKuB7EeGUww+SDBuPQNN+igGNM1ZUye+m5l3z4S8DiuoDGPHeTWCXFfpyjKQ ZqXeX5mahkO6qWaRjRKwt6UCa2LWn0EPT6vhInpVUyiUkiq3VSL2QMnmLVrB3rfk1sae 9yMdFMC3XDfHlDchlaxUf9M2UpuVwYefH6BRtZNF/9SUQXcmZnjGNiDtx8HoQmizgD6B AUMiqKQj31zN1avec5zzcyZ6Ypvyc8cap3UxQvoiGL1eONnI7CzYCsAYYX3izM9TVi3A 4TLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=sFi9K3re; 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=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t3si2290001pgp.498.2018.01.18.08.28.35; Thu, 18 Jan 2018 08:28:49 -0800 (PST) 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=@gmail.com header.s=20161025 header.b=sFi9K3re; 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=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933037AbeARPzC (ORCPT + 99 others); Thu, 18 Jan 2018 10:55:02 -0500 Received: from mail-pg0-f67.google.com ([74.125.83.67]:37280 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932862AbeARPyT (ORCPT ); Thu, 18 Jan 2018 10:54:19 -0500 Received: by mail-pg0-f67.google.com with SMTP id z17so14560290pgc.4 for ; Thu, 18 Jan 2018 07:54:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ksGzLWU1MXPP/8UuLpYwHIOcNukZzvd41NXa6a/NEY4=; b=sFi9K3rew8Km1TeVsrW4+ZEkiUj0P5qZQtxWesrpXVZzBMB81c50Njje+FRyfW1WZ8 spCLbk8ABlUuSnCj8yLD2jGF0HI3+FXPDH5Zfm6q4LG9Gb7m0MDhohggS67zJ3XwC3sJ Qoe6mvnrNpExZX8D4xwakP8ExPptoPAGBhRfMQAW+xOYdQG5kvsp3DYSCqKKDwtfeIac MQ6+nYaUkMw1IA0JxQtjEIAHDR27dG4sIUTGItUDu+R5aeXl/3r4ya5futO/TZNu1GcR kPMOKS1kdF9cMJb2YxBj6owkBvA+4VowK8YX780ZR9ZI7bKp19fbhdr613+jYOY6nUOu puvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ksGzLWU1MXPP/8UuLpYwHIOcNukZzvd41NXa6a/NEY4=; b=DMF67ylYa3K5LyiyGawMVkfX4lg6QSxjxNhOUfp1AjT6vMyF82yAI951gmdGxU5f+W 0PZJa6amUC3iypQfWqYwD14Jd0BEboF3WPHMfroSTzVXiLkXWc3hllxMhL6pjigp8Qc+ xnk1QVbtXc0LszhRrcH0XDUO43LlP8a8V/oqsl7SyDHbksO/ib4hJdWYCEGY2hC0ht5n Ghc2Ei7kZ8CUJqGYlElYZpI5uBjIWuav4+AMOAF6GNN+qfnlzbtOBZFpuWGU7jkbX17I oVLEJO6UgJrv2ZIPuIsZ7uEDvXBl15yv3DG4B65LJ2PdryTgrPdurHPmShB/PeVvU58V yV3w== X-Gm-Message-State: AKwxytdEhpeduSTMhWsGBq9MlUPpeZdyKdIugLvljy8lGKoejIsfkDr2 NCT6JqGOQuKYRvnbK6oCkxA= X-Received: by 10.99.120.199 with SMTP id t190mr10223106pgc.180.1516290373236; Thu, 18 Jan 2018 07:46:13 -0800 (PST) Received: from localhost.localdomain (114-37-171-238.dynamic-ip.hinet.net. [114.37.171.238]) by smtp.gmail.com with ESMTPSA id f188sm14571465pfc.22.2018.01.18.07.46.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 07:46:12 -0800 (PST) From: Alan Kao X-Google-Original-From: Alan Kao To: Palmer Dabbelt , Albert Ou , Steven Rostedt , Ingo Molnar , Masahiro Yamada , Kamil Rytarowski , Andrew Morton , patches@groups.riscv.org, linux-kernel@vger.kernel.org, Stefan O'Rear Cc: Alan Kao , Greentime Hu Subject: [PATCH v3 3/6] riscv/ftrace: Add dynamic function graph tracer support Date: Thu, 18 Jan 2018 23:45:42 +0800 Message-Id: <20180118154545.17987-4-alankao@andestech.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180118154545.17987-1-alankao@andestech.com> References: <20180118154545.17987-1-alankao@andestech.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Once the function_graph tracer is enabled, a filtered function has the following call sequence: * ftracer_caller ==> on/off by ftrace_make_call/ftrace_make_nop * ftrace_graph_caller * ftrace_graph_call ==> on/off by ftrace_en/disable_ftrace_graph_caller * prepare_ftrace_return Considering the following DYNAMIC_FTRACE_WITH_REGS feature, it would be more extendable to have a ftrace_graph_caller function, instead of calling prepare_ftrace_return directly in ftrace_caller. Cc: Greentime Hu Signed-off-by: Alan Kao --- arch/riscv/kernel/ftrace.c | 25 ++++++++++++++++- arch/riscv/kernel/mcount-dyn.S | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c index 311c287433c9..dce1286af9b0 100644 --- a/arch/riscv/kernel/ftrace.c +++ b/arch/riscv/kernel/ftrace.c @@ -51,7 +51,7 @@ static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target, unsigned int nops[2] = {NOP4, NOP4}; int ret = 0; - /* when ftrace_make_nop is called */ + /* for ftrace_make_nop and ftrace_disable_ftrace_graph_caller */ if (!enable) ret = ftrace_check_current_call(hook_pos, calls); @@ -105,6 +105,7 @@ int __init ftrace_dyn_arch_init(void) } #endif +#ifdef CONFIG_FUNCTION_GRAPH_TRACER /* * Most of this function is copied from arm64. */ @@ -137,3 +138,25 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, return; *parent = return_hooker; } + +#ifdef CONFIG_DYNAMIC_FTRACE +extern void ftrace_graph_call(void); +int ftrace_enable_ftrace_graph_caller(void) +{ + int ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call, + NULL); + + if (ret) + return ret; + + return __ftrace_modify_call((unsigned long)&ftrace_graph_call, + (unsigned long)&prepare_ftrace_return, true); +} + +int ftrace_disable_ftrace_graph_caller(void) +{ + return __ftrace_modify_call((unsigned long)&ftrace_graph_call, + (unsigned long)&prepare_ftrace_return, false); +} +#endif /* CONFIG_DYNAMIC_FTRACE */ +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ diff --git a/arch/riscv/kernel/mcount-dyn.S b/arch/riscv/kernel/mcount-dyn.S index 5731d9f56142..1ddf8b1a9345 100644 --- a/arch/riscv/kernel/mcount-dyn.S +++ b/arch/riscv/kernel/mcount-dyn.S @@ -14,18 +14,62 @@ .text .macro SAVE_ABI_STATE +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + addi sp, sp, -48 + sd s0, 32(sp) + sd ra, 40(sp) + addi s0, sp, 48 + sd t0, 24(sp) + sd t1, 16(sp) +#ifdef HAVE_FUNCTION_GRAPH_FP_TEST + sd t2, 8(sp) +#endif +#else addi sp, sp, -16 sd s0, 0(sp) sd ra, 8(sp) addi s0, sp, 16 +#endif .endm .macro RESTORE_ABI_STATE +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + ld s0, 32(sp) + ld ra, 40(sp) + addi sp, sp, 48 +#else ld ra, 8(sp) ld s0, 0(sp) addi sp, sp, 16 +#endif .endm + .macro RESTORE_GRAPH_ARGS + ld a0, 24(sp) + ld a1, 16(sp) +#ifdef HAVE_FUNCTION_GRAPH_FP_TEST + ld a2, 8(sp) +#endif + .endm + +ENTRY(ftrace_graph_caller) + addi sp, sp, -16 + sd s0, 0(sp) + sd ra, 8(sp) + addi s0, sp, 16 +ftrace_graph_call: + .global ftrace_graph_call + /* + * Calling ftrace_enable/disable_ftrace_graph_caller would overwrite the + * call below. Check ftrace_modify_all_code for details. + */ + call ftrace_stub + ld ra, 8(sp) + ld s0, 0(sp) + addi sp, sp, 16 + ret +ENDPROC(ftrace_graph_caller) + ENTRY(ftrace_caller) /* * a0: the address in the caller when calling ftrace_caller @@ -33,6 +77,20 @@ ENTRY(ftrace_caller) */ ld a1, -8(s0) addi a0, ra, -MCOUNT_INSN_SIZE + +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + /* + * the graph tracer (specifically, prepare_ftrace_return) needs these + * arguments but for now the function tracer occupies the regs, so we + * save them in temporary regs to recover later. + */ + addi t0, s0, -8 + mv t1, a0 +#ifdef HAVE_FUNCTION_GRAPH_FP_TEST + ld t2, -16(s0) +#endif +#endif + SAVE_ABI_STATE ftrace_call: .global ftrace_call @@ -45,6 +103,12 @@ ftrace_call: * Check ftrace_modify_all_code for details. */ call ftrace_stub + +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + RESTORE_GRAPH_ARGS + call ftrace_graph_caller +#endif + RESTORE_ABI_STATE ret ENDPROC(ftrace_caller) -- 2.15.1