Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1354555pxu; Thu, 17 Dec 2020 08:07:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJyH1khzvttdobfUb4D+pTX/TWvEcdCAJWnajgtDpfnU7Mrlqtx9iV5J4+jSnLT5AWoE6njt X-Received: by 2002:a17:906:3704:: with SMTP id d4mr16274938ejc.338.1608221256613; Thu, 17 Dec 2020 08:07:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608221256; cv=none; d=google.com; s=arc-20160816; b=FbSNcFDMZC/Q7/FMDy4chU7yt2s7oCSL4ZtPVfSlWkrhN6s/VDmUgNiKjpDfpEwmiY +t+0upapQfmO4vpYRRRPcNP0KlJS6dIq0zBAO3IZcZFtAgy1HvuFoVzU4VHVOxxxjWRt dYfWx021OMs8VjghcO22WOLdAxN0LKWJ3B0N8QLte80LzlOuEesZUnfOtKnufl9kWA/6 LYX6zYGCdrYlHqTbLD8JtAGpGXg2pYpEVHXQCaFacJP7Zl4/1Dl1ZG1jaYkDkQy3syUV zoxy8gjjZtZPw63+hK0/MD+W7I5woTJeh6QSh7g6SusnryFQT51/r0PK1oTu8ICyBH2T m/kQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ijMI4dergAEadXM3EGYjdht56x88PljIXd9aI3OZjIw=; b=eOmAtz2NlL+iUkwaNgSb1MAnZKvpZdIQUttJ+rz0YXsVa+cAx9llqZB+jAe++FfScX RjVN5hnmYeIer1BkTwg9rdCZyVXBk5iO59YNK3w5iJSbU5/KmmgNKnHdcD4oKT3YsPTV LiaiMcYFK11GFgcq3AHoYOoDqtmXifA+lfdfotDlIJIL9SE5kxzEnWTKsE/4rx/ccZnm y0rX/7d13pdkBzcGXqlxQFV0yAl9QpC1e8EC4fR2BWVpHY2mXNZtpLTB8x10bSdytai5 3DbqTNxWPlv2oX6ordPdSIlOmBNNwbjkXO23BJ3Y6rRx7HumxL01btJePUuNK5BfYxFO Vz4Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h19si2756372ejy.455.2020.12.17.08.07.06; Thu, 17 Dec 2020 08:07:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729605AbgLQQFa (ORCPT + 99 others); Thu, 17 Dec 2020 11:05:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:53648 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729539AbgLQQF3 (ORCPT ); Thu, 17 Dec 2020 11:05:29 -0500 From: guoren@kernel.org Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) To: palmerdabbelt@google.com, paul.walmsley@sifive.com, mhiramat@kernel.org, alankao@andestech.com, rostedt@goodmis.org, bjorn.topel@intel.com, pdp7pdp7@gmail.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, anup@brainfault.org, linux-csky@vger.kernel.org, greentime.hu@sifive.com, zong.li@sifive.com, guoren@kernel.org, me@packi.ch, Guo Ren , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Pekka Enberg Subject: [PATCH v5 7/9] riscv: Add KPROBES_ON_FTRACE supported Date: Thu, 17 Dec 2020 16:01:43 +0000 Message-Id: <1608220905-1962-8-git-send-email-guoren@kernel.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1608220905-1962-1-git-send-email-guoren@kernel.org> References: <1608220905-1962-1-git-send-email-guoren@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guo Ren This patch adds support for kprobes on ftrace call sites to avoids much of the overhead with regular kprobes. Try it with simple steps: echo 'p:myprobe sys_clone a0=%a0 a1=%a1 stack_val=+4($stack)' > /sys/kernel/de bug/tracing/kprobe_events echo 1 > /sys/kernel/debug/tracing/events/kprobes/enable cat /sys/kernel/debug/tracing/trace tracer: nop entries-in-buffer/entries-written: 1/1 #P:1 _-----=> irqs-off / _----=> need-resched | / _---=> hardirq/softirq || / _--=> preempt-depth ||| / delay TASK-PID CPU# |||| TIMESTAMP FUNCTION | | | |||| | | sh-92 [000] .... 369.899962: myprobe: (sys_clone+0x0/0x28) a0=0x1200011 a1=0x0 stack_val=0x201c20ffffffe0 cat /sys/kernel/debug/kprobes/list ffffffe00020b584 k sys_clone+0x0 [FTRACE] ^^^^^^ Signed-off-by: Guo Ren Reviewed-by: Masami Hiramatsu Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Björn Töpel Cc: Zong Li Cc: Pekka Enberg --- arch/riscv/Kconfig | 1 + arch/riscv/kernel/probes/Makefile | 1 + arch/riscv/kernel/probes/ftrace.c | 53 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 arch/riscv/kernel/probes/ftrace.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 9a920ed..f58b234 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -69,6 +69,7 @@ config RISCV select HAVE_GCC_PLUGINS select HAVE_GENERIC_VDSO if MMU && 64BIT select HAVE_KPROBES + select HAVE_KPROBES_ON_FTRACE select HAVE_KRETPROBES select HAVE_PCI select HAVE_PERF_EVENTS diff --git a/arch/riscv/kernel/probes/Makefile b/arch/riscv/kernel/probes/Makefile index 8a39507..abbd131 100644 --- a/arch/riscv/kernel/probes/Makefile +++ b/arch/riscv/kernel/probes/Makefile @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_KPROBES) += kprobes.o decode-insn.o simulate-insn.o obj-$(CONFIG_KPROBES) += kprobes_trampoline.o +obj-$(CONFIG_KPROBES_ON_FTRACE) += ftrace.o CFLAGS_REMOVE_simulate-insn.o = $(CC_FLAGS_FTRACE) diff --git a/arch/riscv/kernel/probes/ftrace.c b/arch/riscv/kernel/probes/ftrace.c new file mode 100644 index 00000000..c7ccfff --- /dev/null +++ b/arch/riscv/kernel/probes/ftrace.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +/* Ftrace callback handler for kprobes -- called under preepmt disabed */ +void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, + struct ftrace_ops *ops, struct pt_regs *regs) +{ + struct kprobe *p; + struct kprobe_ctlblk *kcb; + + p = get_kprobe((kprobe_opcode_t *)ip); + if (unlikely(!p) || kprobe_disabled(p)) + return; + + kcb = get_kprobe_ctlblk(); + if (kprobe_running()) { + kprobes_inc_nmissed_count(p); + } else { + unsigned long orig_ip = instruction_pointer(regs); + + instruction_pointer_set(regs, ip); + + __this_cpu_write(current_kprobe, p); + kcb->kprobe_status = KPROBE_HIT_ACTIVE; + if (!p->pre_handler || !p->pre_handler(p, regs)) { + /* + * Emulate singlestep (and also recover regs->pc) + * as if there is a nop + */ + instruction_pointer_set(regs, + (unsigned long)p->addr + MCOUNT_INSN_SIZE); + if (unlikely(p->post_handler)) { + kcb->kprobe_status = KPROBE_HIT_SSDONE; + p->post_handler(p, regs, 0); + } + instruction_pointer_set(regs, orig_ip); + } + + /* + * If pre_handler returns !0, it changes regs->pc. We have to + * skip emulating post_handler. + */ + __this_cpu_write(current_kprobe, NULL); + } +} +NOKPROBE_SYMBOL(kprobe_ftrace_handler); + +int arch_prepare_kprobe_ftrace(struct kprobe *p) +{ + p->ainsn.api.insn = NULL; + return 0; +} -- 2.7.4