Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2193528pxm; Thu, 24 Feb 2022 19:02:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJx882CyWFnmFqmj2jLaaL0/dGEkDZ/Qz1299kcnUXGS1Nh8mBZu/cpSJHsklGOyCpJeTdeM X-Received: by 2002:a17:906:e244:b0:6cd:24e3:ab8b with SMTP id gq4-20020a170906e24400b006cd24e3ab8bmr4471508ejb.633.1645758158332; Thu, 24 Feb 2022 19:02:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645758158; cv=none; d=google.com; s=arc-20160816; b=PTXDK/FFqbsmKf/ZueekM8b4laEIsiX8E730sOuLtA6SG0AqTQ9lLRFlhFt1QPX/wA 7NbakRpwE5omTtIymRo8oDTPCx0LZCJZfSk8dU8SkMUzPwuArDQznRVyp5V3hvDFp4TC HsPqzU62vb8IbfjMH6Z/b2cMdmDFwvF1Ipv1LnkUOI7e6/vHFwPxhs08pRBKk2tICkGB RIxb7hzgGiRD0pnZr3to/0kYx4+ydEPnef3P+Ug6u9vCc4Bi7h9sB2hS39GVwTSRX2cp lyQye025YES87vGYvoM5GVys6WNa0Nz4mblhpgoCQb7QJ/3KmmskJYlQgR91Qo0ieS5S 8LmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=PbVjNn5DCj9paZXBpLVoYaDK4585Zfs9LjzwSoDJ2bs=; b=d2tpaH9t4smo0MFbdrSJJ/NinwP6/nxMpYhpsURXx2n8EdZbSHahaa3IpPntVzXXPq WRJ667vEpjUx3CQEylUfpI8OX9unEaexlMTn7/+wHcxxeqpAfjz590Zzu6ESnHMD1OI8 Qx4u5Tyz/2VPKbmLD1J1urHriEq+qiNEGJOSnImtJvAomF3Bqd3w1hr3oj9Fb+IELX05 jI3n72ksgLXg1OV6PDYS760drdWnPq34T4VEEg9k4jOmuROp+ypRbQ+Z0o5d1utRomMu dksiKlZca5EKizzgSMZ4DdoN/p2xeGAqMyExmBIUVuloIhBSDp8TUOI5RwC+216lIDHt dLBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NtfrDuV7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l6-20020a170906a40600b006cf9cd3fb7asi684132ejz.451.2022.02.24.19.02.15; Thu, 24 Feb 2022 19:02:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NtfrDuV7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236423AbiBYBc5 (ORCPT + 99 others); Thu, 24 Feb 2022 20:32:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234984AbiBYBc4 (ORCPT ); Thu, 24 Feb 2022 20:32:56 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1B58186216 for ; Thu, 24 Feb 2022 17:32:23 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 37AE4B82A85 for ; Fri, 25 Feb 2022 01:32:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A727C340E9; Fri, 25 Feb 2022 01:32:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1645752740; bh=WM2n7P5HcptYIrh/8Oiu/xF67/AfvdZbOpDJ2NsVTG0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=NtfrDuV7INO5SSDNC/bwDgj1rZN0s1VgQAtJpxtUkr1iCnvPdtcYD5iJ4QYvKilFb FfAG37xLNz1PwpOE8baMBsqJfLZaizdGJcK8uxWFZoZNoRs6X9PHTcnrBxnZvDBN3r aPiJgjZrow2gkUWpfBKlQZruveUxh/o+/eZK3tgo5CpAqrcrXeE+xB5qj6tJcFig3B W/x1eHkv6GMxQy+jhJQrZymMXD/iWNNzcckMj5fkZbbch228zklzfqVi8MWvBVKnyy j4epE+f4tRyOt4mLYo64+4hTi8xDAqWv9N1kwt3hGls84V+Lyt+nTIqn770PReOFLQ 6713mU+pS483w== Date: Fri, 25 Feb 2022 10:32:15 +0900 From: Masami Hiramatsu To: Peter Zijlstra Cc: x86@kernel.org, joao@overdrivepizza.com, hjl.tools@gmail.com, jpoimboe@redhat.com, andrew.cooper3@citrix.com, linux-kernel@vger.kernel.org, ndesaulniers@google.com, keescook@chromium.org, samitolvanen@google.com, mark.rutland@arm.com, alyssa.milburn@intel.com, mbenes@suse.cz, rostedt@goodmis.org, mhiramat@kernel.org, alexei.starovoitov@gmail.com Subject: Re: [PATCH v2 15/39] x86/ibt,kprobes: Fix more +0 assumptions Message-Id: <20220225103215.77080de0b3edd0fa2839b8fa@kernel.org> In-Reply-To: <20220224151322.892372059@infradead.org> References: <20220224145138.952963315@infradead.org> <20220224151322.892372059@infradead.org> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Peter, On Thu, 24 Feb 2022 15:51:53 +0100 Peter Zijlstra wrote: > With IBT on, sym+0 is no longer the __fentry__ site. > > NOTE: the architecture has a special case and *does* allow placing an > INT3 breakpoint over ENDBR in which case #BP has precedence over #CP > and as such we don't need to disallow probing these instructions. Does this mean we can still putting a probe on sym+0?? If so, NAK this patch, since the KPROBES_ON_FTRACE is not meaning to accelerate the function entry probe, but just allows user to put a probe on 'call _mcount' (which can be modified by ftrace). func: endbr <- sym+0 : INT3 is used. (kp->addr = func+0) nop5 <- sym+4? : ftrace is used. (kp->addr = func+4?) ... And anyway, in some case (e.g. perf probe) symbol will be a basement symbol like '_text' and @offset will be the function addr - _text addr so that we can put a probe on local-scope function. If you think we should not probe on the endbr, we should treat the pair of endbr and nop5 (or call _mcount) instructions as a virtual single instruction. This means kp->addr should point sym+0, but use ftrace to probe. func: endbr <- sym+0 : ftrace is used. (kp->addr = func+0) nop5 <- sym+4? : This is not able to be probed. ... Thank you, > > NOTE: irrespective of the above; there is a complication in that > direct branches to functions are rewritten to not execute ENDBR, so > any breakpoint thereon might miss lots of actual function executions. > > Signed-off-by: Peter Zijlstra (Intel) > --- > arch/x86/kernel/kprobes/core.c | 11 +++++++++++ > kernel/kprobes.c | 15 ++++++++++++--- > 2 files changed, 23 insertions(+), 3 deletions(-) > > --- a/arch/x86/kernel/kprobes/core.c > +++ b/arch/x86/kernel/kprobes/core.c > @@ -1156,3 +1162,8 @@ int arch_trampoline_kprobe(struct kprobe > { > return 0; > } > + > +bool arch_kprobe_on_func_entry(unsigned long offset) > +{ > + return offset <= 4*HAS_KERNEL_IBT; > +} > --- a/kernel/kprobes.c > +++ b/kernel/kprobes.c > @@ -67,10 +67,19 @@ static bool kprobes_all_disarmed; > static DEFINE_MUTEX(kprobe_mutex); > static DEFINE_PER_CPU(struct kprobe *, kprobe_instance); > > -kprobe_opcode_t * __weak kprobe_lookup_name(const char *name, > - unsigned int __unused) > +kprobe_opcode_t * __weak kprobe_lookup_name(const char *name, unsigned int offset) > { > - return ((kprobe_opcode_t *)(kallsyms_lookup_name(name))); > + kprobe_opcode_t *addr = NULL; > + > + addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name))); > +#ifdef CONFIG_KPROBES_ON_FTRACE > + if (addr && !offset) { > + unsigned long faddr = ftrace_location((unsigned long)addr); > + if (faddr) > + addr = (kprobe_opcode_t *)faddr; > + } > +#endif > + return addr; > } > > /* > > -- Masami Hiramatsu