Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1303777ybl; Wed, 21 Aug 2019 13:16:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqxYMxRMfwQG3Q6Lom9Ehr09eUQrUa+b8K5V3SbwrU0y3l2a4HR9bno2qZ7c3Nv0Qdb9je1Z X-Received: by 2002:a63:6901:: with SMTP id e1mr29717483pgc.390.1566418568110; Wed, 21 Aug 2019 13:16:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566418568; cv=none; d=google.com; s=arc-20160816; b=WoMfWZuVlfOfVqbNOhio8fAT9N9QmdCNJQu9d+KmhiKVAkakdCEal4aRa+amERvVtO jRbAaAxe4Krhuh1U9HLGe080oP30OGcOsG5Oa020eMpuHgbKsB+CBoY+pxn2whKSDDoA AkVtyOO0wIBtFe8e6J7ASze/2xzmqRMfgkwq1V9dQaUMOjyYS5lSeM1Ikd6xuBFzabWM S29IaajmOqkTLNZGcoqWoPAe2TXBMA6fzAKsW22lrEY1N3NZPfBnQp/ZCMrSSjQrXm7G 8ngTIoC+/SPQdcws8+Mfqbo1+WvTenZjTByyxHTTvNX4RWphXB9Mk5G2kLEMJM3F7gcE tQtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:content-transfer-encoding :user-agent:mime-version:in-reply-to:references:cc:to:subject:from :date; bh=ke+r8och8avkqVSzxsQja3u/WKd/1f0+CvdVqMgCOlc=; b=A1yBkFgzSZiuTjYS9QY+f0yD+nJSECgjKvH3bFFgxDbYugpbu776BQ0678hhaeT91n LQi7e9GOoCGtymquWefv6dE9LWOY3vLTUFNeYS5Rgkqj6jYcaRwrlaE1pzI0oQ4nAZC/ o++UA/3YPmJzhXUu0PZbPAKQXDvvfMfbfXB3JdVmPQMuLD09k48oZ/ysW4j+JyqkvTkJ pzRj/Z2ChLHtIp1oZmFz1DwTaCQ7iqJVtkpphPqw+dl/LnNjFlhVfYAj8WMqF6DaUQs7 v+7qSEqiVhK/i2g23ynueDgCO0U4xetgopsXGOpSw14BMdELo6rXlytHH3kx1kS6SLBk QTwA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b2si7020221plz.15.2019.08.21.13.15.51; Wed, 21 Aug 2019 13:16:08 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728788AbfHUUNA convert rfc822-to-8bit (ORCPT + 99 others); Wed, 21 Aug 2019 16:13:00 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:34208 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726330AbfHUUNA (ORCPT ); Wed, 21 Aug 2019 16:13:00 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x7LKClEf007416 for ; Wed, 21 Aug 2019 16:12:59 -0400 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0a-001b2d01.pphosted.com with ESMTP id 2uhc7v97rb-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 21 Aug 2019 16:12:58 -0400 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 21 Aug 2019 21:12:56 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 21 Aug 2019 21:12:51 +0100 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x7LKCol060162128 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 21 Aug 2019 20:12:50 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7B78EAE04D; Wed, 21 Aug 2019 20:12:50 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 15FCBAE045; Wed, 21 Aug 2019 20:12:50 +0000 (GMT) Received: from localhost (unknown [9.85.72.179]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 21 Aug 2019 20:12:49 +0000 (GMT) Date: Thu, 22 Aug 2019 01:42:48 +0530 From: "Naveen N. Rao" Subject: Re: [PATCH v3] arm64: implement KPROBES_ON_FTRACE To: Catalin Marinas , Jonathan Corbet , Jisheng Zhang , Masami Hiramatsu , Peter Zijlstra , Thomas Gleixner , Will Deacon Cc: "linux-arm-kernel@lists.infradead.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Ingo Molnar , Steven Rostedt References: <20190821183501.33588dd8@xhacker.debian> In-Reply-To: <20190821183501.33588dd8@xhacker.debian> MIME-Version: 1.0 User-Agent: astroid/0.15.0 (https://github.com/astroidmail/astroid) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8BIT X-TM-AS-GCONF: 00 x-cbid: 19082120-4275-0000-0000-0000035BA116 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19082120-4276-0000-0000-0000386DC544 Message-Id: <1566418060.32spdm55zk.naveen@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-08-21_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1906280000 definitions=main-1908210196 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Jisheng Zhang wrote: > KPROBES_ON_FTRACE avoids much of the overhead with regular kprobes as it > eliminates the need for a trap, as well as the need to emulate or > single-step instructions. > > Tested on berlin arm64 platform. > > ~ # mount -t debugfs debugfs /sys/kernel/debug/ > ~ # cd /sys/kernel/debug/ > /sys/kernel/debug # echo 'p _do_fork' > tracing/kprobe_events > > before the patch: > > /sys/kernel/debug # cat kprobes/list > ffffff801009fe28 k _do_fork+0x0 [DISABLED] > > after the patch: > > /sys/kernel/debug # cat kprobes/list > ffffff801009ff54 k _do_fork+0x4 [DISABLED][FTRACE] > > Signed-off-by: Jisheng Zhang > --- > KPROBES_ON_FTRACE avoids much of the overhead with regular kprobes as it > eliminates the need for a trap, as well as the need to emulate or > single-step instructions. > > Applied after arm64 FTRACE_WITH_REGS: > http://lists.infradead.org/pipermail/linux-arm-kernel/2019-August/674404.html > > Changes since v2: > - remove patch1, make it a single cleanup patch > - remove "This patch" in the change log > - implement arm64's kprobe_lookup_name() and arch_kprobe_on_func_entry instead > patching the common kprobes code > > Changes since v1: > - make the kprobes/x86: use instruction_pointer and instruction_pointer_set > as patch1 > - add Masami's ACK to patch1 > - add some description about KPROBES_ON_FTRACE and why we need it on > arm64 > - correct the log before the patch > - remove the consolidation patch, make it as TODO > - only adjust kprobe's addr when KPROBE_FLAG_FTRACE is set > - if KPROBES_ON_FTRACE, ftrace_call_adjust() the kprobe's addr before > calling ftrace_location() > - update the kprobes-on-ftrace/arch-support.txt in doc > > > .../debug/kprobes-on-ftrace/arch-support.txt | 2 +- > arch/arm64/Kconfig | 1 + > arch/arm64/kernel/probes/Makefile | 1 + > arch/arm64/kernel/probes/ftrace.c | 60 +++++++++++++++++++ > arch/arm64/kernel/probes/kprobes.c | 23 +++++++ > 5 files changed, 86 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 68f266944d5f..e8358a38981c 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 663392d1eae2..928700f15e23 100644 > --- a/arch/arm64/Kconfig > +++ b/arch/arm64/Kconfig > @@ -167,6 +167,7 @@ config ARM64 > select HAVE_STACKPROTECTOR > select HAVE_SYSCALL_TRACEPOINTS > select HAVE_KPROBES > + select HAVE_KPROBES_ON_FTRACE > select HAVE_KRETPROBES > select HAVE_GENERIC_VDSO > select IOMMU_DMA if IOMMU_SUPPORT > diff --git a/arch/arm64/kernel/probes/Makefile b/arch/arm64/kernel/probes/Makefile > index 8e4be92e25b1..4020cfc66564 100644 > --- a/arch/arm64/kernel/probes/Makefile > +++ b/arch/arm64/kernel/probes/Makefile > @@ -4,3 +4,4 @@ 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..1f0c09d02bb8 > --- /dev/null > +++ b/arch/arm64/kernel/probes/ftrace.c > @@ -0,0 +1,60 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Dynamic Ftrace based Kprobes Optimization > + * > + * Copyright (C) Hitachi Ltd., 2012 > + * Copyright (C) 2019 Jisheng Zhang > + * Synaptics Incorporated > + */ > + > +#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; > + > + /* Preempt is disabled by ftrace */ > + 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); > + /* Kprobe handler expects regs->pc = pc + 4 as breakpoint hit */ > + instruction_pointer_set(regs, ip + sizeof(kprobe_opcode_t)); > + > + __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; > +} > diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c > index c4452827419b..f2bf8c70da79 100644 > --- a/arch/arm64/kernel/probes/kprobes.c > +++ b/arch/arm64/kernel/probes/kprobes.c > @@ -551,6 +551,29 @@ void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs) > return (void *)orig_ret_address; > } > > +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS This should be CONFIG_KPROBES_ON_FTRACE since you only want to choose the ftrace entry in that case. > +kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) > +{ > + unsigned long addr = kallsyms_lookup_name(name); > + unsigned long faddr; > + > + /* > + * with -fpatchable-function-entry=2, the first 4 bytes is the > + * LR saver, then the actual call insn. So ftrace location is > + * always on the first 4 bytes offset. > + */ > + faddr = ftrace_location_range(addr, addr + AARCH64_INSN_SIZE); > + if (faddr) > + return (kprobe_opcode_t *)faddr; You should only return the ftrace location if offset is 0, since the offset is added to the address returned from here (see _kprobe_addr()). - Naveen