Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp3521367ybl; Mon, 19 Aug 2019 20:57:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqz3VtZkjux3tkt25bjQJLlikM5YOfC5JRBrB5cdh3uTVTNKUpb1A6pJcrh32zRguEsfcck9 X-Received: by 2002:a17:902:d70e:: with SMTP id w14mr25096641ply.339.1566273440839; Mon, 19 Aug 2019 20:57:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1566273440; cv=pass; d=google.com; s=arc-20160816; b=ypcGEyyRuOD370wHAoDcztURMSU38qpaos/JZnLrNgifQzJAfltr/RJgsinpXH59e+ P5WDEc3SUfiuxemBgWd3hdTFc7INPRfLrEt7CxMSbw/3Lj2Esfw9waOL/SAxVzM2DZ6a 6muRukmXM8E8M2nsmf1FV/OT1O79iZ7fWGOV13v6PJ6CiQ74cw24YKqcpiaFInC2LxKB WOSpwhW+3V6KNLwrLl5VgOvukHS/kRMeMEhNKwv35kvSTNa7KFJi/4/WFECB7MmY+CEU KvlS/XoRpPgzRq/NlMYDVvIyM1eanBD4MUHFPPQ1V/RoUDWzHltVPk4uyXWbW33GbdVr 2NtQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:references :message-id:date:thread-index:thread-topic:subject:cc:to:from :dkim-signature; bh=ErXupsvfk+tkZsOGpLtm1FI/DiDjRBvF0nj30oUOZh0=; b=BdC/6dk2jCyCq2xLnSNtOF6s+72nOKbRNrTFOpkONEYBouIOs1gLRTxjci6n3OYgpQ TyHIaQSLm2ml4yc0FQ/DkP5tCDufLPMlr5jn7vOWqSst3aRO0KlDk9xL9X9ADG6cLJar v9nn7QzM87zOH3sVv5XjrO2dcbcsHtfjYjrQTb3geU3uwcV5twynKZjwHifAC9M6TU5E UXXs0Las7JcLPkF+D8hFpZv8LL1+QGbVj8mhzxRIBrRqAgtCo3u4a7kq/00EtnsKQVGE iabb8p3ycexDzN2c/TwnMmYyxQ6dkdN2hj2Rm+ZmZRaGyBaIFjwccoX47j68t3NEQu1g NIyQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Synaptics.onmicrosoft.com header.s=selector2-Synaptics-onmicrosoft-com header.b=Wm7aDHMd; arc=pass (i=1 spf=pass spfdomain=synaptics.com dkim=pass dkdomain=synaptics.com dmarc=pass fromdomain=synaptics.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 e3si11142732pgv.153.2019.08.19.20.57.05; Mon, 19 Aug 2019 20:57:20 -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; dkim=pass header.i=@Synaptics.onmicrosoft.com header.s=selector2-Synaptics-onmicrosoft-com header.b=Wm7aDHMd; arc=pass (i=1 spf=pass spfdomain=synaptics.com dkim=pass dkdomain=synaptics.com dmarc=pass fromdomain=synaptics.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 S1729137AbfHTDyX (ORCPT + 99 others); Mon, 19 Aug 2019 23:54:23 -0400 Received: from mail-eopbgr710044.outbound.protection.outlook.com ([40.107.71.44]:2304 "EHLO NAM05-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728777AbfHTDyW (ORCPT ); Mon, 19 Aug 2019 23:54:22 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B1XdVSOvaXIQubruidpCcfxKTSG4Ubrm0475d8ZVb0PV/NmedUJ8IJvZeBpICbx2cgHJfRkA8ein7F+t6E2HMY6zW+rRqC01NRKyUm6FiZdNF5GwJXG6blNssXWq+4J/MfS6JeFOVEeGZ3cHkKeciVDOp5MU1Hm9vM7miYpgHXkotqvatecsNlBxyoYmnSs44SKrTgVGFAKbDvmt4uZkKJRfrTuDIJjkvkxua446EJSLhrBxlQmdW6PKIXzEiaVCFLggayFho6s7+cWv6uIgo6Zvy96bFKBPnuDkWHKP6MYULFVvi5UfMaWiyiIkZuJ2xOBQz4dDVeYMvr1P1riCrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ErXupsvfk+tkZsOGpLtm1FI/DiDjRBvF0nj30oUOZh0=; b=A0QJFpLUhs6zJvu7JVcG78kl2d1SDtC4G+y26CNJufUVLN8C3Euweg34lXTMB9uhPpl0um9kyB7X3T9+DsjkPxgyXAQGcf98jBvCKGduqbG+mTmJ4+hXFQ9YXxmuO3MClamW/tKkLgruCXjZKW2gyoTTsCyC6AhemVo2IpQr/usDTZMtE+Y6F1rIhj4Rr/OZF8UX+VdTrw62LVLhh3bksoE0/Zc6+GCvsTJ60J5XeQF5EQufpJase8zUTvuaQZZ349iZzKlGBuJAb5CRBLIHbzjZ8vX2zPfYZ7B3u+1tvTvF1uq/3nDNbGke3w5lqAJa+n4h6yyy/+juR4Hm+pu74Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=synaptics.com; dmarc=pass action=none header.from=synaptics.com; dkim=pass header.d=synaptics.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Synaptics.onmicrosoft.com; s=selector2-Synaptics-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ErXupsvfk+tkZsOGpLtm1FI/DiDjRBvF0nj30oUOZh0=; b=Wm7aDHMd1+sNydYOYpMVF+PWWvzZ9lyDguNk5ABmA8m6RSHLsLWgbpmuu5iMbA0IHSWsQWnhI/rdgMuyXyg5gbDqhooLv/Tr9A0B+uffZLYVqnmuyf2FZIkeaNjParVO8D/Q5PVERVD/R/JJJe+JYFjS6IQ7oKrQvTyC97/S5mg= Received: from BYAPR03MB4773.namprd03.prod.outlook.com (20.179.92.152) by BYAPR03MB4870.namprd03.prod.outlook.com (20.179.93.215) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2157.16; Tue, 20 Aug 2019 03:54:20 +0000 Received: from BYAPR03MB4773.namprd03.prod.outlook.com ([fe80::a517:3578:67bf:6c88]) by BYAPR03MB4773.namprd03.prod.outlook.com ([fe80::a517:3578:67bf:6c88%7]) with mapi id 15.20.2157.022; Tue, 20 Aug 2019 03:54:20 +0000 From: Jisheng Zhang To: Catalin Marinas , Jonathan Corbet , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , "x86@kernel.org" , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , Masami Hiramatsu CC: "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" Subject: [PATCH v2 3/3] arm64: implement KPROBES_ON_FTRACE Thread-Topic: [PATCH v2 3/3] arm64: implement KPROBES_ON_FTRACE Thread-Index: AQHVVwryjSNW9eV9u0mDAvz7yXMGOw== Date: Tue, 20 Aug 2019 03:54:20 +0000 Message-ID: <20190820114314.685a3239@xhacker.debian> References: <20190820113928.1971900c@xhacker.debian> In-Reply-To: <20190820113928.1971900c@xhacker.debian> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [124.74.246.114] x-clientproxiedby: TY2PR06CA0027.apcprd06.prod.outlook.com (2603:1096:404:2e::15) To BYAPR03MB4773.namprd03.prod.outlook.com (2603:10b6:a03:134::24) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Jisheng.Zhang@synaptics.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 4cf07ffa-c85f-46fb-5508-08d72522145e x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328)(7193020);SRVR:BYAPR03MB4870; x-ms-traffictypediagnostic: BYAPR03MB4870: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:7219; x-forefront-prvs: 013568035E x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(136003)(346002)(39860400002)(376002)(366004)(396003)(189003)(199004)(6116002)(6486002)(3846002)(53936002)(76176011)(2906002)(6436002)(86362001)(1076003)(64756008)(71190400001)(6512007)(110136005)(9686003)(6506007)(71200400001)(54906003)(66066001)(5660300002)(4326008)(14454004)(316002)(52116002)(66556008)(476003)(7736002)(99286004)(446003)(2501003)(486006)(386003)(8676002)(14444005)(81166006)(66946007)(256004)(478600001)(50226002)(102836004)(8936002)(26005)(305945005)(66446008)(66476007)(7416002)(186003)(11346002)(25786009)(81156014)(921003)(39210200001)(1121003);DIR:OUT;SFP:1101;SCL:1;SRVR:BYAPR03MB4870;H:BYAPR03MB4773.namprd03.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:0; received-spf: None (protection.outlook.com: synaptics.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: aCiZWPwy8Jwdi/+8CSI+xJM9idFvTBdYY4HlzBQTf8HwUnS/c4nJpic5UAvYidiJ6dtg4XAdC2K7Gd0LNqQFfQ4jgQS+31sJxp23Qva49gPylD2dTWoSPr2gg5qUlQcMR97ECGzM22nQnXyUrc+aDYK+DHrkaOl03hY29WpfiwtLc1F83yU30RzioHt75JvaXN5ADv9p/N+iNCT5VOAi23KHLuszvh8qaJdq+ALd/Z7zs37vSRzqTYUPP8H9eHB8rtAN+sJORY/TRKvdkmUlSIyjwp/qdjgMUvtHrmEweSNP3qR7PJJPsWidlwPWScegHfwDUYqZyMkB5QSWMN/muCpZCpBnmzS5RUJkqOJgrymiivETL8HyKWkjXvOTvGxZJwMiMG5HP2FkYNnkljePAj0JMgLz9pydgcVDsKhYAto= x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="us-ascii" Content-ID: <758C392CCF526742B344A76B55289EB3@namprd03.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: synaptics.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4cf07ffa-c85f-46fb-5508-08d72522145e X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Aug 2019 03:54:20.0344 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 335d1fbc-2124-4173-9863-17e7051a2a0e X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: CAcL2sqKhRE5cmCP02pDSD1yfO3F+kGxqmameVtbPHlnLTImORFfR/k7vWwSSIv5o1KHH5PMynr1R/LnJg29iA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR03MB4870 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. This patch implements KPROBES_ON_FTRACE for arm64. 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 --- .../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 +++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kernel/probes/ftrace.c diff --git a/Documentation/features/debug/kprobes-on-ftrace/arch-support.tx= t 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/M= akefile index 8e4be92e25b1..4020cfc66564 100644 --- a/arch/arm64/kernel/probes/Makefile +++ b/arch/arm64/kernel/probes/Makefile @@ -4,3 +4,4 @@ obj-$(CONFIG_KPROBES) +=3D kprobes.o decode-insn.o \ simulate-insn.o obj-$(CONFIG_UPROBES) +=3D uprobes.o decode-insn.o \ simulate-insn.o +obj-$(CONFIG_KPROBES_ON_FTRACE) +=3D ftrace.o diff --git a/arch/arm64/kernel/probes/ftrace.c b/arch/arm64/kernel/probes/f= trace.c new file mode 100644 index 000000000000..52901ffff570 --- /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 =3D get_kprobe((kprobe_opcode_t *)ip); + if (unlikely(!p) || kprobe_disabled(p)) + return; + + kcb =3D get_kprobe_ctlblk(); + if (kprobe_running()) { + kprobes_inc_nmissed_count(p); + } else { + unsigned long orig_ip =3D instruction_pointer(regs); + /* Kprobe handler expects regs->pc =3D pc + 1 as breakpoint hit */ + instruction_pointer_set(regs, ip + sizeof(kprobe_opcode_t)); + + __this_cpu_write(current_kprobe, p); + kcb->kprobe_status =3D 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 =3D 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 =3D NULL; + return 0; +} --=20 2.23.0.rc1