From nobody Sat Sep 13 17:00:10 2025 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 E7352C636CD for ; Wed, 1 Feb 2023 15:57:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232848AbjBAP5q (ORCPT ); Wed, 1 Feb 2023 10:57:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232921AbjBAP5e (ORCPT ); Wed, 1 Feb 2023 10:57:34 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4189D751A7; Wed, 1 Feb 2023 07:57:10 -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 4F703B821C6; Wed, 1 Feb 2023 15:57:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A098DC433EF; Wed, 1 Feb 2023 15:57:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675267028; bh=9bLHgL0AFcGkOhvnTj5xqhQoThIfAS/ZkfD2NUxphxA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uMfCFAH2W98Vgu4JEy5itZdIuFnOqQSsqAcv+NR3+JrjFpxZ3ClK0kRxfoveDLrBP Cqi1zxW/RqV4t8GhdPSzzbnOSPTXRvX6HGcbAZhNUo+uzR564UHpeFfAbHMze7B4OS OhF/5lXp40145sm1orhtM9S4vEMvFe7JMBU5kbfRqxJsA4MVmeTubbfoMmjJZJU3gc zYpOrotThbq3wuDpuETITW1WIOmZGPNG+0tvFv/5Ui5FABhgVvZFmLXPSccfJ5B6R0 1xRm+k5CVTXJdcNSy98yx+4ohvj1dx7wfAi29wl3fXNx5xsDH1mhgNULBVPDl9wDIZ vPauptBB5JoGg== From: "Masami Hiramatsu (Google)" To: linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Steven Rostedt , mhiramat@kernel.org, Florent Revest , Mark Rutland , Will Deacon Subject: [PATCH v3 08/10] fprobe: Pass return address to the handlers Date: Thu, 2 Feb 2023 00:57:04 +0900 Message-Id: <167526702449.433354.12543406972254133329.stgit@mhiramat.roam.corp.google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog In-Reply-To: <167526695292.433354.8949652607331707144.stgit@mhiramat.roam.corp.google.com> References: <167526695292.433354.8949652607331707144.stgit@mhiramat.roam.corp.google.com> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu (Google) Pass return address as 'ret_ip' to the fprobe entry and return handlers. Signed-off-by: Masami Hiramatsu (Google) --- include/linux/fprobe.h | 6 ++++-- include/linux/rethook.h | 2 +- kernel/kprobes.c | 1 + kernel/trace/bpf_trace.c | 6 ++++-- kernel/trace/fprobe.c | 6 +++--- kernel/trace/rethook.c | 3 ++- lib/test_fprobe.c | 10 +++++++--- samples/fprobe/fprobe_example.c | 6 ++++-- 8 files changed, 26 insertions(+), 14 deletions(-) diff --git a/include/linux/fprobe.h b/include/linux/fprobe.h index 47fefc7f363b..134f0f59ffa8 100644 --- a/include/linux/fprobe.h +++ b/include/linux/fprobe.h @@ -35,9 +35,11 @@ struct fprobe { int nr_maxactive; =20 int (*entry_handler)(struct fprobe *fp, unsigned long entry_ip, - struct pt_regs *regs, void *entry_data); + unsigned long ret_ip, struct pt_regs *regs, + void *entry_data); void (*exit_handler)(struct fprobe *fp, unsigned long entry_ip, - struct pt_regs *regs, void *entry_data); + unsigned long ret_ip, struct pt_regs *regs, + void *entry_data); }; =20 /* This fprobe is soft-disabled. */ diff --git a/include/linux/rethook.h b/include/linux/rethook.h index c8ac1e5afcd1..fdf26cd0e742 100644 --- a/include/linux/rethook.h +++ b/include/linux/rethook.h @@ -14,7 +14,7 @@ =20 struct rethook_node; =20 -typedef void (*rethook_handler_t) (struct rethook_node *, void *, struct p= t_regs *); +typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned= long, struct pt_regs *); =20 /** * struct rethook - The rethook management data structure. diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 1c18ecf9f98b..7613ed0128dc 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2130,6 +2130,7 @@ static int pre_handler_kretprobe(struct kprobe *p, st= ruct pt_regs *regs) NOKPROBE_SYMBOL(pre_handler_kretprobe); =20 static void kretprobe_rethook_handler(struct rethook_node *rh, void *data, + unsigned long ret_addr, struct pt_regs *regs) { struct kretprobe *rp =3D (struct kretprobe *)data; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index b2567670ca42..baee1c0226d5 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2649,7 +2649,8 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_li= nk *link, =20 static int kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, - struct pt_regs *regs, void *data) + unsigned long ret_ip, struct pt_regs *regs, + void *data) { struct bpf_kprobe_multi_link *link; =20 @@ -2660,7 +2661,8 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned= long fentry_ip, =20 static void kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip, - struct pt_regs *regs, void *data) + unsigned long ret_ip, struct pt_regs *regs, + void *data) { struct bpf_kprobe_multi_link *link; =20 diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 9abb3905bc8e..973bc664fcc1 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -52,7 +52,7 @@ static void fprobe_handler(unsigned long ip, unsigned lon= g parent_ip, } =20 if (fp->entry_handler) - ret =3D fp->entry_handler(fp, ip, ftrace_get_regs(fregs), entry_data); + ret =3D fp->entry_handler(fp, ip, parent_ip, ftrace_get_regs(fregs), ent= ry_data); =20 /* If entry_handler returns !0, nmissed is not counted. */ if (rh) { @@ -81,7 +81,7 @@ static void fprobe_kprobe_handler(unsigned long ip, unsig= ned long parent_ip, } =20 static void fprobe_exit_handler(struct rethook_node *rh, void *data, - struct pt_regs *regs) + unsigned long ret_ip, struct pt_regs *regs) { struct fprobe *fp =3D (struct fprobe *)data; struct fprobe_rethook_node *fpr; @@ -91,7 +91,7 @@ static void fprobe_exit_handler(struct rethook_node *rh, = void *data, =20 fpr =3D container_of(rh, struct fprobe_rethook_node, node); =20 - fp->exit_handler(fp, fpr->entry_ip, regs, + fp->exit_handler(fp, fpr->entry_ip, ret_ip, regs, fp->entry_data_size ? (void *)fpr->data : NULL); } NOKPROBE_SYMBOL(fprobe_exit_handler); diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c index 32c3dfdb4d6a..fc196e186737 100644 --- a/kernel/trace/rethook.c +++ b/kernel/trace/rethook.c @@ -301,7 +301,8 @@ unsigned long rethook_trampoline_handler(struct pt_regs= *regs, break; handler =3D READ_ONCE(rhn->rethook->handler); if (handler) - handler(rhn, rhn->rethook->data, regs); + handler(rhn, rhn->rethook->data, + correct_ret_addr, regs); =20 if (first =3D=3D node) break; diff --git a/lib/test_fprobe.c b/lib/test_fprobe.c index 0fe5273e960b..ade7e3d93dac 100644 --- a/lib/test_fprobe.c +++ b/lib/test_fprobe.c @@ -39,7 +39,8 @@ static noinline u32 fprobe_selftest_nest_target(u32 value= , u32 (*nest)(u32)) } =20 static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip, - struct pt_regs *regs, void *data) + unsigned long ret_ip, + struct pt_regs *regs, void *data) { KUNIT_EXPECT_FALSE(current_test, preemptible()); /* This can be called on the fprobe_selftest_target and the fprobe_selfte= st_target2 */ @@ -57,6 +58,7 @@ static notrace int fp_entry_handler(struct fprobe *fp, un= signed long ip, } =20 static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip, + unsigned long ret_ip, struct pt_regs *regs, void *data) { unsigned long ret =3D regs_return_value(regs); @@ -78,14 +80,16 @@ static notrace void fp_exit_handler(struct fprobe *fp, = unsigned long ip, } =20 static notrace int nest_entry_handler(struct fprobe *fp, unsigned long ip, - struct pt_regs *regs, void *data) + unsigned long ret_ip, + struct pt_regs *regs, void *data) { KUNIT_EXPECT_FALSE(current_test, preemptible()); return 0; } =20 static notrace void nest_exit_handler(struct fprobe *fp, unsigned long ip, - struct pt_regs *regs, void *data) + unsigned long ret_ip, + struct pt_regs *regs, void *data) { KUNIT_EXPECT_FALSE(current_test, preemptible()); KUNIT_EXPECT_EQ(current_test, ip, target_nest_ip); diff --git a/samples/fprobe/fprobe_example.c b/samples/fprobe/fprobe_exampl= e.c index 4efc8feb6277..64e715e7ed11 100644 --- a/samples/fprobe/fprobe_example.c +++ b/samples/fprobe/fprobe_example.c @@ -49,6 +49,7 @@ static void show_backtrace(void) } =20 static int sample_entry_handler(struct fprobe *fp, unsigned long ip, + unsigned long ret_ip, struct pt_regs *regs, void *data) { if (use_trace) @@ -65,10 +66,11 @@ static int sample_entry_handler(struct fprobe *fp, unsi= gned long ip, return 0; } =20 -static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struc= t pt_regs *regs, +static void sample_exit_handler(struct fprobe *fp, unsigned long ip, + unsigned long ret_ip, struct pt_regs *regs, void *data) { - unsigned long rip =3D instruction_pointer(regs); + unsigned long rip =3D ret_ip; =20 if (use_trace) /*