From patchwork Mon Mar 2 14:25:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 5914191 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id CD1CDBF440 for ; Mon, 2 Mar 2015 14:57:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CF59D20218 for ; Mon, 2 Mar 2015 14:57:17 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 013D5201FA for ; Mon, 2 Mar 2015 14:57:16 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1YSRjN-0004dP-2G; Mon, 02 Mar 2015 14:54:01 +0000 Received: from casper.infradead.org ([2001:770:15f::2]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YSRbw-0005rj-8y for linux-arm-kernel@bombadil.infradead.org; Mon, 02 Mar 2015 14:46:20 +0000 Received: from szxga02-in.huawei.com ([119.145.14.65]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YSRJG-0008Am-Ng for linux-arm-kernel@lists.infradead.org; Mon, 02 Mar 2015 14:27:04 +0000 Received: from 172.24.2.119 (EHLO lggeml422-hub.china.huawei.com) ([172.24.2.119]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CHW85032; Mon, 02 Mar 2015 22:26:02 +0800 (CST) Received: from kernel-host.huawei (10.107.197.247) by lggeml422-hub.china.huawei.com (10.72.61.32) with Microsoft SMTP Server id 14.3.158.1; Mon, 2 Mar 2015 22:25:52 +0800 From: Wang Nan To: , , , , Subject: [RFC PATCH v4 27/34] early kprobes on ftrace: kprobe_on_ftrace_get_old_insn() Date: Mon, 2 Mar 2015 22:25:05 +0800 Message-ID: <1425306312-3437-28-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.4 In-Reply-To: <1425306312-3437-1-git-send-email-wangnan0@huawei.com> References: <1425306312-3437-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.197.247] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150302_142703_548579_EE5F0167 X-CRM114-Status: GOOD ( 10.56 ) X-Spam-Score: -4.2 (----) Cc: x86@kernel.org, lizefan@huawei.com, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Newly introduced function kprobe_on_ftrace_get_old_insn() will be called by ftrace when ftrace generating call instruction. It is for retriving probed instructions which original nops are replaced by kprobe. FTRACE_FL_EARLY_KPROBES bit in rec->flags is cleared, so after calling kprobe_on_ftrace_get_old_insn() an ftrace record will not be treated as early kprobed. Signed-off-by: Wang Nan --- include/linux/kprobes.h | 9 +++++++++ kernel/kprobes.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h index 5a5290f..2d78bbb 100644 --- a/include/linux/kprobes.h +++ b/include/linux/kprobes.h @@ -289,6 +289,8 @@ extern const unsigned char *arch_kprobe_on_ftrace_get_old_insn(struct kprobe *kp extern void init_kprobes_on_ftrace(void); extern bool kprobe_fix_ftrace_make_nop(struct dyn_ftrace *rec); +extern const unsigned char *kprobe_on_ftrace_get_old_insn(struct dyn_ftrace *rec, + const unsigned char *ftrace_nop, unsigned char *dest, size_t insn_size); #else static inline void init_kprobes_on_ftrace(void) { @@ -299,6 +301,13 @@ static inline bool kprobe_fix_ftrace_make_nop(struct dyn_ftrace *_unused) return false; } + +static inline const unsigned char * +kprobe_on_ftrace_get_old_insn(struct dyn_ftrace *_unused, + const unsigned char *ftrace_nop, unsigned char *_unused2, size_t _unused3) +{ + return ftrace_nop; +} #endif // CONFIG_EARLY_KPROBES && CONFIG_KPROBES_ON_FTRACE #ifdef CONFIG_EARLY_KPROBES diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 20b6ab8..c504c1c 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2623,6 +2623,40 @@ bool kprobe_fix_ftrace_make_nop(struct dyn_ftrace *rec) return true; } +/* NOTE: caller must ensure holding kprobe_mutex */ +const unsigned char * +kprobe_on_ftrace_get_old_insn(struct dyn_ftrace *rec, + const unsigned char *ftrace_nop, + unsigned char *dest, size_t insn_size) +{ + const unsigned char *ret; + struct kprobe *kp; + void *addr; + + if (!(rec->flags & FTRACE_FL_EARLY_KPROBES)) + return ftrace_nop; + + addr = (void *)rec->ip; + + /* + * Note that get_kprobe always get the kprobe on table, for it + * KPROBE_FLAG_OPTIMIZED is reliable. + */ + kp = get_kprobe(addr); + + if (!kp || !(kp->flags & KPROBE_FLAG_FTRACE_EARLY)) { + mutex_unlock(&kprobe_mutex); + return ftrace_nop; + } + + ret = arch_kprobe_on_ftrace_get_old_insn(kp, ftrace_nop, + dest, insn_size); + + /* Only give one chance for kprobe to retrive old insn. */ + rec->flags &= ~FTRACE_FL_EARLY_KPROBES; + return ret; +} + void init_kprobes_on_ftrace(void) { kprobes_on_ftrace_initialized = true;