Linux DTrace development list
 help / color / mirror / Atom feed
From: Kris Van Hees <kris.van.hees@oracle.com>
To: dtrace@lists.linux.dev, dtrace-devel@oss.oracle.com
Subject: [PATCH 2/4] bpf, fbt: implement a fallback in case bpf_get_func_ret() is not supported
Date: Mon, 09 Nov 2026 20:09:14 +0000	[thread overview]
Message-ID: <41d2adfe9a6f03ad47bd6994c12dc73e@oracle.com> (raw)

The bpf_get_func_ret() BPF helper was not introduced until Linux 5.17,
and DTrace should still support kernels older than that.

Signed-off-by: Kris Van Hees <kris.van.hees@oracle.com>
---
 libdtrace/dt_bpf.c      | 21 ++++++++++++++++-----
 libdtrace/dt_prov_fbt.c | 29 ++++++++++++++++++++++++-----
 2 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/libdtrace/dt_bpf.c b/libdtrace/dt_bpf.c
index c7cde24f..3d12a596 100644
--- a/libdtrace/dt_bpf.c
+++ b/libdtrace/dt_bpf.c
@@ -414,7 +414,8 @@ dt_bpf_raw_tracepoint_open(const void *tp, int fd)
 }
 
 static int
-have_helper(uint32_t func_id)
+have_helper(uint32_t func_id, enum bpf_prog_type ptype,
+	    enum bpf_attach_type atype, uint32_t btf_id)
 {
 	char		log[DT_BPF_LOG_SIZE_SMALL] = { 0, };
 	char		*ptr = &log[0];
@@ -426,7 +427,7 @@ have_helper(uint32_t func_id)
 	int		fd;
 
 	/* If the program loads, we can use the helper. */
-	fd = dt_bpf_prog_attach(BPF_PROG_TYPE_KPROBE, 0, 0, 0, &dp,
+	fd = dt_bpf_prog_attach(ptype, atype, 0, btf_id, &dp,
 			      1, log, DT_BPF_LOG_SIZE_SMALL);
 	if (fd >= 0) {
 		close(fd);
@@ -463,7 +464,7 @@ dt_bpf_init_helpers(dtrace_hdl_t *dtp)
 
 	/* Verify the existence of some specific helpers. */
 #define BPF_HELPER_MAP(orig, alt)	\
-	if (!have_helper(BPF_FUNC_##orig)) \
+	if (!have_helper(BPF_FUNC_##orig, BPF_PROG_TYPE_KPROBE, 0, 0)) \
 			dtp->dt_bpfhelper[BPF_FUNC_##orig] = BPF_FUNC_##alt;
 
 	BPF_HELPER_MAP(probe_read_user, probe_read);
@@ -473,6 +474,9 @@ dt_bpf_init_helpers(dtrace_hdl_t *dtp)
 	BPF_HELPER_MAP(get_current_task_btf, unspec);
 	BPF_HELPER_MAP(task_pt_regs, unspec);
 #undef BPF_HELPER_MAP
+
+	/* This helper is checked with an fexit program during feature setup. */
+	dtp->dt_bpfhelper[BPF_FUNC_get_func_ret] = BPF_FUNC_unspec;
 }
 
 static int
@@ -519,9 +523,16 @@ dt_bpf_init_features(dtrace_hdl_t *dtp)
 
 	btf_id = dt_btf_lookup_name_kind(dtp, dtp->dt_exec,
 					 "bpf_get_btf_vmlinux", BTF_KIND_FUNC);
-	if (btf_id >= 0 &&
-	    have_attach_type(BPF_PROG_TYPE_TRACING, BPF_TRACE_FENTRY, btf_id))
+	if (btf_id != BTF_TYPE_NONE &&
+	    have_attach_type(BPF_PROG_TYPE_TRACING, BPF_TRACE_FENTRY, btf_id)) {
 		BPF_SET_FEATURE(dtp, BPF_FEAT_FENTRY);
+
+		/* get_func_ret is only available to tracing programs. */
+		if (have_helper(BPF_FUNC_get_func_ret, BPF_PROG_TYPE_TRACING,
+				BPF_TRACE_FEXIT, btf_id))
+			dtp->dt_bpfhelper[BPF_FUNC_get_func_ret] =
+							BPF_FUNC_get_func_ret;
+	}
 }
 
 void
diff --git a/libdtrace/dt_prov_fbt.c b/libdtrace/dt_prov_fbt.c
index 8bdfc89f..a1bb4918 100644
--- a/libdtrace/dt_prov_fbt.c
+++ b/libdtrace/dt_prov_fbt.c
@@ -336,11 +336,30 @@ static int fprobe_trampoline(dt_pcb_t *pcb, uint_t exitlbl)
 		 */
 		dmp = dt_module_lookup_by_name(dtp, prp->desc->mod);
 		if (dmp && prp->argc == 2) {
-			emit(dlp, BPF_MOV_REG(BPF_REG_1, BPF_REG_8));
-			emit(dlp, BPF_MOV_REG(BPF_REG_2, BPF_REG_FP));
-			emit(dlp, BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, DT_TRAMP_SP_SLOT(0)));
-			emit(dlp, BPF_CALL_HELPER(dtp->dt_bpfhelper[BPF_FUNC_get_func_ret]));
-			emit(dlp, BPF_LOAD(BPF_DW, BPF_REG_0, BPF_REG_FP, DT_TRAMP_SP_SLOT(0)));
+			if (dtp->dt_bpfhelper[BPF_FUNC_get_func_ret] !=
+			    BPF_FUNC_unspec) {
+				emit(dlp, BPF_MOV_REG(BPF_REG_1, BPF_REG_8));
+				emit(dlp, BPF_MOV_REG(BPF_REG_2, BPF_REG_FP));
+				emit(dlp, BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, DT_TRAMP_SP_SLOT(0)));
+				emit(dlp, BPF_CALL_HELPER(dtp->dt_bpfhelper[BPF_FUNC_get_func_ret]));
+				emit(dlp, BPF_LOAD(BPF_DW, BPF_REG_0, BPF_REG_FP, DT_TRAMP_SP_SLOT(0)));
+			} else {
+				int	argc;
+
+				argc = dt_btf_func_argc(dmp->dm_btf,
+						       dt_tp_probe_get_id(prp));
+				if (argc < 0)
+					return -1;
+
+				/* As for entry args, avoid typed context loads. */
+				emit(dlp, BPF_MOV_REG(BPF_REG_1, BPF_REG_FP));
+				emit(dlp, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, DT_TRAMP_SP_SLOT(0)));
+				emit(dlp, BPF_MOV_IMM(BPF_REG_2, 8));
+				emit(dlp, BPF_MOV_REG(BPF_REG_3, BPF_REG_8));
+				emit(dlp, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, argc * 8));
+				emit(dlp, BPF_CALL_HELPER(dtp->dt_bpfhelper[BPF_FUNC_probe_read_kernel]));
+				emit(dlp, BPF_LOAD(BPF_DW, BPF_REG_0, BPF_REG_FP, DT_TRAMP_SP_SLOT(0)));
+			}
 			emit(dlp, BPF_STORE(BPF_DW, BPF_REG_7, DMST_ARG(1), BPF_REG_0));
 		}
 	}
-- 
2.52.0


                 reply	other threads:[~2026-11-09 20:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=41d2adfe9a6f03ad47bd6994c12dc73e@oracle.com \
    --to=kris.van.hees@oracle.com \
    --cc=dtrace-devel@oss.oracle.com \
    --cc=dtrace@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox