Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6340546ybl; Mon, 23 Dec 2019 04:24:46 -0800 (PST) X-Google-Smtp-Source: APXvYqz98Tr1et8F4j+njDy2LOINSXyW6W/gBcMl+iqqP6zUlF1wu93+jrIL1zk202qBHwYE50ep X-Received: by 2002:a9d:66c1:: with SMTP id t1mr29381486otm.73.1577103886800; Mon, 23 Dec 2019 04:24:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577103886; cv=none; d=google.com; s=arc-20160816; b=C17x/QNI+C58VUUDm5rh+M3NG+WKgHSlTwO3+78gEY26pVs+Doe9EjFYXEkUpd6P35 pWyLOPU/sJNcKWjMtuWi84BPE6IdfPzxPsqzxFDvIOC2XqWL6uX2yyccKCHdjWoKHITU vbMQ/qKFSkxb5CaxJTBL570cZHuMl567zHDFrRJAJMrGFEM3wGGD/BbyLYp4H1YLYjqC 3b66NkQNtv6+0aGV04c103CwG2lMan9ILiDzTTw16xOh7t0x/VjiNqsteQmFQWboAdo9 IyBiW3Z0qRsy3sk6KbqUMx3sWFmIVCOmlMRivTe2aUmcuf1PCVmjrjSzyd7fRs/dG19q YKYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=JklhHGMHvysFUw9tXIfxFb5iIENxG7HRnjvxaTILu3c=; b=0c9ZbONtcLK+VELU7gd25ZfwjJivSeFzGh74APlsm9x735K7xDdNxbrMxuTsgviqFh KdKlZ7Deyd0II+ZPp0q65thi8JptlbBxRe1FeQKG00YLFYd88jAc8mHCr63b0/NI4rUb JcTw5gESnf1R2Y1DeZOvLhYPQ6oKY0SRFnhqbIVECgR9pyNe0RQ6gZF3aMQhMrupzGt7 ReFxJ9QwPY5X3OBntv7iRGBNr6kVF8QGLOEfwsMLocA2HLpFRuAVuZOMbN/QzKp9hdvk 6HC91vpXmpq6hMGrob6gl482hxMZtrNK0vxXfJ9Z/2HbIT8vAIsf3/Mo5Uh5ewNpWc90 5D+g== ARC-Authentication-Results: i=1; mx.google.com; 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 i20si4846471oie.119.2019.12.23.04.24.33; Mon, 23 Dec 2019 04:24:46 -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; 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 S1726763AbfLWMXm (ORCPT + 99 others); Mon, 23 Dec 2019 07:23:42 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:50154 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726257AbfLWMXm (ORCPT ); Mon, 23 Dec 2019 07:23:42 -0500 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id AAB932A3EF2B827604A9; Mon, 23 Dec 2019 20:23:38 +0800 (CST) Received: from huawei.com (10.175.104.193) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.439.0; Mon, 23 Dec 2019 20:23:28 +0800 From: Cheng Jian To: , CC: , , , , , , Subject: [PATCH] arm64/kprobe: add support for KPROBES_ON_FTRACE Date: Mon, 23 Dec 2019 12:38:28 +0000 Message-ID: <20191223123828.39543-1-cj.chengjian@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.104.193] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow KPROBES to use the ftrace infrastructure. This depends on HAVE_DYNAMIC_FTRACE_WITH_REGS which introduce by : commit 3b23e4991fb6 ("arm64: implement ftrace with regs") Based on the x86 code by Masami. With: # cd /sys/kernel/debug/tracing # echo 'p _do_fork+0x4' > kprobe_events before patch: # cat ../kprobes/list sh: write error: Invalid argument after patch: # cat ../kprobes/list ffffa00016aa9d4c k _do_fork+0x4 [FTRACE] Signed-off-by: Cheng Jian --- .../debug/kprobes-on-ftrace/arch-support.txt | 2 +- arch/arm64/Kconfig | 1 + arch/arm64/kernel/probes/Makefile | 2 + arch/arm64/kernel/probes/ftrace.c | 65 +++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kernel/probes/ftrace.c diff --git a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt index 4fae0464ddff..f9dd9dd91e0c 100644 --- a/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt +++ b/Documentation/features/debug/kprobes-on-ftrace/arch-support.txt @@ -9,7 +9,7 @@ | alpha: | TODO | | arc: | TODO | | arm: | TODO | - | arm64: | TODO | + | arm64: | ok | | c6x: | TODO | | csky: | TODO | | h8300: | TODO | diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index b1b4476ddb83..d613be9c2346 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -144,6 +144,7 @@ config ARM64 select HAVE_DYNAMIC_FTRACE select HAVE_DYNAMIC_FTRACE_WITH_REGS \ if $(cc-option,-fpatchable-function-entry=2) + select HAVE_KPROBES_ON_FTRACE if HAVE_DYNAMIC_FTRACE_WITH_REGS select HAVE_EFFICIENT_UNALIGNED_ACCESS select HAVE_FAST_GUP select HAVE_FTRACE_MCOUNT_RECORD diff --git a/arch/arm64/kernel/probes/Makefile b/arch/arm64/kernel/probes/Makefile index 8e4be92e25b1..45a9d916a47d 100644 --- a/arch/arm64/kernel/probes/Makefile +++ b/arch/arm64/kernel/probes/Makefile @@ -4,3 +4,5 @@ obj-$(CONFIG_KPROBES) += kprobes.o decode-insn.o \ simulate-insn.o obj-$(CONFIG_UPROBES) += uprobes.o decode-insn.o \ simulate-insn.o + +obj-$(CONFIG_KPROBES_ON_FTRACE) += ftrace.o diff --git a/arch/arm64/kernel/probes/ftrace.c b/arch/arm64/kernel/probes/ftrace.c new file mode 100644 index 000000000000..f89bfaa3e5f3 --- /dev/null +++ b/arch/arm64/kernel/probes/ftrace.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Dynamic Ftrace based Kprobes Optimization + * + * Copyright (C) Hitachi Ltd., 2012 + * Copyright (C) 2019 Huawei Inc. + * Copyright 2019 Cheng Jian + */ +#include +#include +#include +#include +#include + + +/* Ftrace callback handler for kprobes -- called under preepmt disabed */ +void kprobe_ftrace_handler(unsigned long pc, unsigned long parent_pc, + struct ftrace_ops *ops, struct pt_regs *regs) +{ + struct kprobe *p; + struct kprobe_ctlblk *kcb; + + /* Preempt is disabled by ftrace */ + p = get_kprobe((kprobe_opcode_t *)pc); + if (unlikely(!p) || kprobe_disabled(p)) + return; + + kcb = get_kprobe_ctlblk(); + if (kprobe_running()) { + kprobes_inc_nmissed_count(p); + } else { + /* + * On ARM64, recg->pc is *before* this instruction for the + * pre handler. + */ + regs->pc -= MCOUNT_INSN_SIZE; + + __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->ip) + * as if there is a nop + */ + regs->pc += MCOUNT_INSN_SIZE; + if (unlikely(p->post_handler)) { + kcb->kprobe_status = KPROBE_HIT_SSDONE; + p->post_handler(p, regs, 0); + } + } + /* + * If pre_handler returns !0, it changes regs->ip. 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.17.1