All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 2/4] bpf, fbt: implement a fallback in case bpf_get_func_ret() is not supported
@ 2026-11-09 20:09 Kris Van Hees
  0 siblings, 0 replies; only message in thread
From: Kris Van Hees @ 2026-11-09 20:09 UTC (permalink / raw)
  To: dtrace, dtrace-devel

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-09 20:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-09 20:09 [PATCH 2/4] bpf, fbt: implement a fallback in case bpf_get_func_ret() is not supported Kris Van Hees

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.