From patchwork Thu Feb 17 11:36:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Naveen N. Rao" X-Patchwork-Id: 12749865 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F3ADC433FE for ; Thu, 17 Feb 2022 11:37:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239859AbiBQLhl (ORCPT ); Thu, 17 Feb 2022 06:37:41 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:44940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239911AbiBQLhk (ORCPT ); Thu, 17 Feb 2022 06:37:40 -0500 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E861424BF9; Thu, 17 Feb 2022 03:37:25 -0800 (PST) Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21HB9Hvm002737; Thu, 17 Feb 2022 11:36:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=kJD/XeCV9sYc2VxxV22CxFZX2bIMIheeokMIOZ2MLgw=; b=SfuxF3V5bgCDnPPnYgDZdqK03O1psJC2QgPiHC1/HQ8UOZqIEvXKGb0PNx4xscRJTINr +DoUtPI83pSXsT+o5v49qzJAlP2Zmr/EvmR1MZ2x473iASAzHOpvr9WbRsPM9Yuyixli 6LIfWYy/QVTsGWbkhqYUXKsjsEXNEFwx7pGnNLfs9u0AEJJHiV5VyQ8s1dwgaPX+pf8o aIU8ZKqSJPAMZ9zmRT/chNScDmRYqKTv2l6sO50Ov7YG/JVqTulRcL+h+yWxDOsIiJV/ lFkSJpWuJGK82v9FTxrW1Zvk/NwbKz16GvQEsC94bqV4/0bE4ivxCaGOnnKYzaHaR5XT uw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3e9hu2cmrp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:51 +0000 Received: from m0098410.ppops.net (m0098410.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 21HBSk6J004072; Thu, 17 Feb 2022 11:36:51 GMT Received: from ppma03fra.de.ibm.com (6b.4a.5195.ip4.static.sl-reverse.com [149.81.74.107]) by mx0a-001b2d01.pphosted.com with ESMTP id 3e9hu2cmqp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:51 +0000 Received: from pps.filterd (ppma03fra.de.ibm.com [127.0.0.1]) by ppma03fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 21HBaEJW008693; Thu, 17 Feb 2022 11:36:49 GMT Received: from b06avi18878370.portsmouth.uk.ibm.com (b06avi18878370.portsmouth.uk.ibm.com [9.149.26.194]) by ppma03fra.de.ibm.com with ESMTP id 3e64haf7ua-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:48 +0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06avi18878370.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 21HBak5138273368 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 17 Feb 2022 11:36:46 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5873D52050; Thu, 17 Feb 2022 11:36:46 +0000 (GMT) Received: from li-NotSettable.ibm.com.com (unknown [9.43.115.39]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id EFDEC5206D; Thu, 17 Feb 2022 11:36:43 +0000 (GMT) From: "Naveen N. Rao" To: Steven Rostedt , Daniel Borkmann , Alexei Starovoitov , Michael Ellerman , Masami Hiramatsu Cc: Nicholas Piggin , , , Subject: [PATCH 1/3] powerpc/ftrace: Reserve instructions from function entry for ftrace Date: Thu, 17 Feb 2022 17:06:23 +0530 Message-Id: <8843d65ac0878232433573d10ebee30457748624.1645096227.git.naveen.n.rao@linux.vnet.ibm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: Ru5iKSd-xiMgaB_hKuoS9FVCmjWSGT4o X-Proofpoint-ORIG-GUID: 4kh5FzR50R7g40nHwBWh7_1ulXDWEBPe X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-02-17_04,2022-02-17_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 impostorscore=0 bulkscore=0 priorityscore=1501 lowpriorityscore=0 phishscore=0 suspectscore=0 mlxlogscore=811 malwarescore=0 spamscore=0 clxscore=1015 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202170051 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On some architectures, enabling function tracing results in multiple instructions being emitted at function entry. As an example, on powerpc64 with -mprofile-kernel, two instructions are emitted at function entry: mflr r0 bl _mcount It is desirable to nop out both these instructions when ftrace is not active. For that purpose, it is essential to mark both these instructions as belonging to ftrace so that other kernel subsystems (such as kprobes) do not modify these instructions. Add support for this by allowing architectures to override ftrace_cmp_recs() and to match against address ranges over and above a single MCOUNT_INSN_SIZE. For powerpc32, we mark the two instructions preceding the call to _mcount() as belonging to ftrace. For powerpc64, an additional aspect to consider is that functions can have a global entry point for setting up the TOC when invoked from other modules. If present, global entry point always involves two instructions (addis/lis and addi). To handle this, we provide a custom ftrace_init_nop() for powerpc64 where we identify functions having a global entry point and record this information in the LSB of dyn_ftrace->arch.mod. This information is used in ftrace_cmp_recs() to reserve instructions from the global entry point. Suggested-by: Steven Rostedt Signed-off-by: Naveen N. Rao Reported-by: kernel test robot --- arch/powerpc/include/asm/ftrace.h | 15 ++++ arch/powerpc/kernel/trace/ftrace.c | 110 ++++++++++++++++++++++++++--- kernel/trace/ftrace.c | 2 + 3 files changed, 117 insertions(+), 10 deletions(-) diff --git a/arch/powerpc/include/asm/ftrace.h b/arch/powerpc/include/asm/ftrace.h index debe8c4f706260..8eb3235831633d 100644 --- a/arch/powerpc/include/asm/ftrace.h +++ b/arch/powerpc/include/asm/ftrace.h @@ -59,6 +59,21 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr) struct dyn_arch_ftrace { struct module *mod; }; + +struct dyn_ftrace; +struct module *ftrace_mod_addr_get(struct dyn_ftrace *rec); +void ftrace_mod_addr_set(struct dyn_ftrace *rec, struct module *mod); + +#ifdef CONFIG_MPROFILE_KERNEL +int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec); +#define ftrace_init_nop ftrace_init_nop +#endif + +#if defined(CONFIG_MPROFILE_KERNEL) || defined(CONFIG_PPC32) +int ftrace_cmp_recs(const void *a, const void *b); +#define ftrace_cmp_recs ftrace_cmp_recs +#endif + #endif /* __ASSEMBLY__ */ #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS diff --git a/arch/powerpc/kernel/trace/ftrace.c b/arch/powerpc/kernel/trace/ftrace.c index 80b6285769f27c..11ce9296ce3cf2 100644 --- a/arch/powerpc/kernel/trace/ftrace.c +++ b/arch/powerpc/kernel/trace/ftrace.c @@ -428,21 +428,21 @@ int ftrace_make_nop(struct module *mod, * We should either already have a pointer to the module * or it has been passed in. */ - if (!rec->arch.mod) { + if (!ftrace_mod_addr_get(rec)) { if (!mod) { pr_err("No module loaded addr=%lx\n", addr); return -EFAULT; } - rec->arch.mod = mod; + ftrace_mod_addr_set(rec, mod); } else if (mod) { - if (mod != rec->arch.mod) { + if (mod != ftrace_mod_addr_get(rec)) { pr_err("Record mod %p not equal to passed in mod %p\n", - rec->arch.mod, mod); + ftrace_mod_addr_get(rec), mod); return -EINVAL; } /* nothing to do if mod == rec->arch.mod */ } else - mod = rec->arch.mod; + mod = ftrace_mod_addr_get(rec); return __ftrace_make_nop(mod, rec, addr); #else @@ -451,6 +451,96 @@ int ftrace_make_nop(struct module *mod, #endif /* CONFIG_MODULES */ } +#define FUNC_MCOUNT_OFFSET_PPC32 8 +#define FUNC_MCOUNT_OFFSET_PPC64_LEP 4 +#define FUNC_MCOUNT_OFFSET_PPC64_GEP 12 + +#ifdef CONFIG_MPROFILE_KERNEL +struct module *ftrace_mod_addr_get(struct dyn_ftrace *rec) +{ + return (struct module *)((unsigned long)rec->arch.mod & ~0x1); +} + +void ftrace_mod_addr_set(struct dyn_ftrace *rec, struct module *mod) +{ + rec->arch.mod = (struct module *)(((unsigned long)rec->arch.mod & 0x1) | (unsigned long)mod); +} + +int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec) +{ + unsigned long offset, ip = rec->ip; + ppc_inst_t op1, op2; + int ret; + + if (!kallsyms_lookup_size_offset(rec->ip, NULL, &offset) || + (offset != FUNC_MCOUNT_OFFSET_PPC64_GEP && offset != FUNC_MCOUNT_OFFSET_PPC64_LEP)) { + ip -= FUNC_MCOUNT_OFFSET_PPC64_GEP; + ret = copy_inst_from_kernel_nofault(&op1, (void *)ip); + ret |= copy_inst_from_kernel_nofault(&op2, (void *)(ip + MCOUNT_INSN_SIZE)); + if (!ret && + ((ppc_inst_val(op1) & 0xffff0000) == PPC_RAW_LIS(_R2, 0) || + (ppc_inst_val(op1) & 0xffff0000) == PPC_RAW_ADDIS(_R2, _R12, 0)) && + (ppc_inst_val(op2) & 0xffff0000) == PPC_RAW_ADDI(_R2, _R2, 0)) + ftrace_mod_addr_set(rec, (struct module *)1); + } else if (offset == FUNC_MCOUNT_OFFSET_PPC64_GEP) { + ftrace_mod_addr_set(rec, (struct module *)1); + } + + return ftrace_make_nop(mod, rec, MCOUNT_ADDR); +} +#else +struct module *ftrace_mod_addr_get(struct dyn_ftrace *rec) +{ + return rec->arch.mod; +} + +void ftrace_mod_addr_set(struct dyn_ftrace *rec, struct module *mod) +{ + rec->arch.mod = mod; +} +#endif /* CONFIG_MPROFILE_KERNEL */ + +#if defined(CONFIG_MPROFILE_KERNEL) || defined(CONFIG_PPC32) +int ftrace_location_get_offset(const struct dyn_ftrace *rec) +{ + if (IS_ENABLED(CONFIG_MPROFILE_KERNEL)) + /* + * On ppc64le with -mprofile-kernel, function entry can have: + * addis r2, r12, M + * addi r2, r2, N + * mflr r0 + * bl _mcount + * + * The first two instructions are for TOC setup and represent the global entry + * point for cross-module calls, and may be missing if the function is never called + * from other modules. + */ + return ((unsigned long)rec->arch.mod & 0x1) ? FUNC_MCOUNT_OFFSET_PPC64_GEP : + FUNC_MCOUNT_OFFSET_PPC64_LEP; + else + /* + * On ppc32, function entry always has: + * mflr r0 + * stw r0, 4(r1) + * bl _mcount + */ + return FUNC_MCOUNT_OFFSET_PPC32; +} + +int ftrace_cmp_recs(const void *a, const void *b) +{ + const struct dyn_ftrace *key = a; + const struct dyn_ftrace *rec = b; + int offset = ftrace_location_get_offset(rec); + + if (key->flags < rec->ip - offset) + return -1; + if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) + return 1; + return 0; +} +#endif + #ifdef CONFIG_MODULES #ifdef CONFIG_PPC64 /* @@ -494,7 +584,7 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) ppc_inst_t instr; void *ip = (void *)rec->ip; unsigned long entry, ptr, tramp; - struct module *mod = rec->arch.mod; + struct module *mod = ftrace_mod_addr_get(rec); /* read where this goes */ if (copy_inst_from_kernel_nofault(op, ip)) @@ -561,7 +651,7 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) int err; ppc_inst_t op; u32 *ip = (u32 *)rec->ip; - struct module *mod = rec->arch.mod; + struct module *mod = ftrace_mod_addr_get(rec); unsigned long tramp; /* read where this goes */ @@ -678,7 +768,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) * Being that we are converting from nop, it had better * already have a module defined. */ - if (!rec->arch.mod) { + if (!ftrace_mod_addr_get(rec)) { pr_err("No module loaded\n"); return -EINVAL; } @@ -699,7 +789,7 @@ __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, ppc_inst_t op; unsigned long ip = rec->ip; unsigned long entry, ptr, tramp; - struct module *mod = rec->arch.mod; + struct module *mod = ftrace_mod_addr_get(rec); /* If we never set up ftrace trampolines, then bail */ if (!mod->arch.tramp || !mod->arch.tramp_regs) { @@ -814,7 +904,7 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, /* * Out of range jumps are called from modules. */ - if (!rec->arch.mod) { + if (!ftrace_mod_addr_get(rec)) { pr_err("No module loaded\n"); return -EINVAL; } diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index f9feb197b2daaf..68f20cf34b0c47 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -1510,6 +1510,7 @@ ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) } +#ifndef ftrace_cmp_recs static int ftrace_cmp_recs(const void *a, const void *b) { const struct dyn_ftrace *key = a; @@ -1521,6 +1522,7 @@ static int ftrace_cmp_recs(const void *a, const void *b) return 1; return 0; } +#endif static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end) { From patchwork Thu Feb 17 11:36:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Naveen N. Rao" X-Patchwork-Id: 12749866 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DCF35C433F5 for ; Thu, 17 Feb 2022 11:37:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239932AbiBQLhn (ORCPT ); Thu, 17 Feb 2022 06:37:43 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:45704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235427AbiBQLhm (ORCPT ); Thu, 17 Feb 2022 06:37:42 -0500 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8C2924BF9; Thu, 17 Feb 2022 03:37:27 -0800 (PST) Received: from pps.filterd (m0187473.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21HBBctA009493; Thu, 17 Feb 2022 11:36:56 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=lE0gbf8t2g/1fUkvXko9q/tsZdNjgF2I76WzkaK/Q54=; b=Wf+uoQeuOOAhK8frvNMvE4hEJfEQmGum1feKzx6djrFb+Vp+pdyJCxgknpcSrtIahmjT KEEQ0zKhRJo0PgLSAipV9bQUsjSQxrzgBtkB26oESavEq39dhfcX/TJHbbckB5tGesAh 9W3LhxXVuGa0ZEN0da2FOIkqTdkw0c8CALsh/8V3XQWZD4NfLAewYL2jPG3JFjsHUGlW OANjpMeanDflHCRMajg01eHn3M0pWYCnVEhX5dR5wKp2cQ/H3FdDNT4e9Eppd7wln7cg zOzTSaqlfC8LUe6HZNC3vcAjOIRi6MtiPUvPCTcGYRXU1o5r/hAqRwGATPYELNywDLM3 pw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3e9n988q15-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:55 +0000 Received: from m0187473.ppops.net (m0187473.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 21HBD9ve011941; Thu, 17 Feb 2022 11:36:55 GMT Received: from ppma03fra.de.ibm.com (6b.4a.5195.ip4.static.sl-reverse.com [149.81.74.107]) by mx0a-001b2d01.pphosted.com with ESMTP id 3e9n988pyx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:55 +0000 Received: from pps.filterd (ppma03fra.de.ibm.com [127.0.0.1]) by ppma03fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 21HBaFXH008709; Thu, 17 Feb 2022 11:36:52 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma03fra.de.ibm.com with ESMTP id 3e64haf7va-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:52 +0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 21HBanfZ37683588 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 17 Feb 2022 11:36:50 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3B1E15205F; Thu, 17 Feb 2022 11:36:49 +0000 (GMT) Received: from li-NotSettable.ibm.com.com (unknown [9.43.115.39]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id B892952054; Thu, 17 Feb 2022 11:36:46 +0000 (GMT) From: "Naveen N. Rao" To: Steven Rostedt , Daniel Borkmann , Alexei Starovoitov , Michael Ellerman , Masami Hiramatsu Cc: Nicholas Piggin , , , Subject: [PATCH 2/3] bpf/trampoline: Allow ftrace location to differ from trampoline attach address Date: Thu, 17 Feb 2022 17:06:24 +0530 Message-Id: <2a32e4723f7e56c675ffd6aa0762789e56dce2e1.1645096227.git.naveen.n.rao@linux.vnet.ibm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: ghGzwae4IVWidNvEDMylB-Ua5zhta93P X-Proofpoint-GUID: Htspvnek8-mkYMYkwZWwrrjnRyiAJE0M X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-02-17_04,2022-02-17_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 impostorscore=0 adultscore=0 spamscore=0 clxscore=1015 priorityscore=1501 mlxlogscore=770 bulkscore=0 phishscore=0 suspectscore=0 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202170051 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net On some architectures, ftrace location can include multiple instructions, and does not necessarily match the function entry address returned by kallsyms_lookup(). Drop the check in is_ftrace_location() to accommodate the same. Signed-off-by: Naveen N. Rao --- kernel/bpf/trampoline.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 4b6974a195c138..c47c80874bee3f 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -124,8 +124,6 @@ static int is_ftrace_location(void *ip) addr = ftrace_location((long)ip); if (!addr) return 0; - if (WARN_ON_ONCE(addr != (long)ip)) - return -EFAULT; return 1; } From patchwork Thu Feb 17 11:36:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Naveen N. Rao" X-Patchwork-Id: 12749867 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2630C433FE for ; Thu, 17 Feb 2022 11:37:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239941AbiBQLho (ORCPT ); Thu, 17 Feb 2022 06:37:44 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:45748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239926AbiBQLhm (ORCPT ); Thu, 17 Feb 2022 06:37:42 -0500 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07B756E29E; Thu, 17 Feb 2022 03:37:27 -0800 (PST) Received: from pps.filterd (m0187473.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21HBBe4U009513; Thu, 17 Feb 2022 11:36:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=6mlvZRSwlsDh5IrGJe/XwEIms3DwVqrOxjdrt0QYtbY=; b=i4NaFTMWFL2qMTIXxqLfmvM73ABxxvOzqmc5qPnuve8pK4HFL3XIXycerMN+P8FVB1kf 4Cp8f27hrh4YEn/ZnVRAAPCDWH8QuGPVpnCEO5/8spOFXU4GkMkiolPwaxxQ0GCnciCv 6UdOO4Ua5aqeY3sH+n0fcHnIAEQyqLiROieSRGSM65G/MGeFxN8cgxmKB7vyhvLJ8S96 tCtHwTs4BMy009LBRTHrr1ym/XhDrNcBfvoYFsh1isv6evyTAPVnpaPukBXFLO8EWBlK 3BWT9WCrzhQjly3fG4L1tTZawyE04YdTtqWGKLN0rtyX/vcLoxx5Rb+21WBW29j2O2i4 7A== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3e9n988q1w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:57 +0000 Received: from m0187473.ppops.net (m0187473.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 21HBCkbI011474; Thu, 17 Feb 2022 11:36:57 GMT Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 3e9n988q10-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:57 +0000 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 21HBaqxS017593; Thu, 17 Feb 2022 11:36:54 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma04ams.nl.ibm.com with ESMTP id 3e64hahbce-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 17 Feb 2022 11:36:54 +0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 21HBaqn947186420 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 17 Feb 2022 11:36:52 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0D31B52074; Thu, 17 Feb 2022 11:36:52 +0000 (GMT) Received: from li-NotSettable.ibm.com.com (unknown [9.43.115.39]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id 9AF085204E; Thu, 17 Feb 2022 11:36:49 +0000 (GMT) From: "Naveen N. Rao" To: Steven Rostedt , Daniel Borkmann , Alexei Starovoitov , Michael Ellerman , Masami Hiramatsu Cc: Nicholas Piggin , , , Subject: [PATCH 3/3] kprobes: Allow probing on any address belonging to ftrace Date: Thu, 17 Feb 2022 17:06:25 +0530 Message-Id: <78480d05821d45e09fb234f61f9037e26d42f02d.1645096227.git.naveen.n.rao@linux.vnet.ibm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: s2pY3YpUXKKedvIx171jzWymSrmlo6RM X-Proofpoint-GUID: 05TTG-txgITAc8F4zkEq4gAnsVDojiOQ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-02-17_04,2022-02-17_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 impostorscore=0 adultscore=0 spamscore=0 clxscore=1015 priorityscore=1501 mlxlogscore=999 bulkscore=0 phishscore=0 suspectscore=0 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202170051 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On certain architectures, ftrace can reserve multiple instructions at function entry. Rather than rejecting kprobe on addresses other than the exact ftrace call instruction, use the address returned by ftrace to probe at the correct address when CONFIG_KPROBES_ON_FTRACE is enabled. Signed-off-by: Naveen N. Rao --- kernel/kprobes.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 94cab8c9ce56cc..0a797ede3fdf37 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1497,6 +1497,10 @@ bool within_kprobe_blacklist(unsigned long addr) static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr, const char *symbol_name, unsigned int offset) { +#ifdef CONFIG_KPROBES_ON_FTRACE + unsigned long ftrace_addr = 0; +#endif + if ((symbol_name && addr) || (!symbol_name && !addr)) goto invalid; @@ -1507,6 +1511,14 @@ static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr, } addr = (kprobe_opcode_t *)(((char *)addr) + offset); + +#ifdef CONFIG_KPROBES_ON_FTRACE + if (addr) + ftrace_addr = ftrace_location((unsigned long)addr); + if (ftrace_addr) + return (kprobe_opcode_t *)ftrace_addr; +#endif + if (addr) return addr;