Skip to contentSkip to conversation

fentry supports function optimized by complier

RFC PATCH In review Tao Chen 3 participants 7 messages Apr 30, 2025 – May 6, 2025

Subsystems (3)
lore
Share & tools
lore

Tao Chen

author
#0 lore raw

cover fentry supports function optimized by complier

The previous discussion about fentry invalid on function optimeized by complier is as follows:
https://lore.kernel.org/bpf/[email protected]/

This seems to be something that pahole needs to resolve. However, Alan mentioned that there are many situations involved in this, and he proposed that the available_filter_functions_addr can be used to find the address of the real function. If we can get the real address from user, maybe this address can be used when the function obtained from the BTF is invalid.

The specific selftest has not been added yet. I just wrote a simple test program and ran it.

This is the initial RFC patch, feedback is welcome.

Tao Chen (2):
libbpf: Try get fentry func addr from available_filter_functions_addr
bpf: Get fentry func addr from user when BTF info invalid

 include/linux/bpf.h            |  1 +
 include/uapi/linux/bpf.h       |  1 +
 kernel/bpf/syscall.c           |  1 +
 kernel/bpf/verifier.c          |  9 ++++++
 tools/include/uapi/linux/bpf.h |  1 +
 tools/lib/bpf/bpf.c            |  1 +
 tools/lib/bpf/bpf.h            |  1 +
 tools/lib/bpf/gen_loader.c     |  1 +
 tools/lib/bpf/libbpf.c         | 53 ++++++++++++++++++++++++++++++++++
 9 files changed, 69 insertions(+)
Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/2 libbpf: Try get fentry func addr from available_filter_functions_addr

Try to get the real kernel func addr from available_filter_functions_addr, in case of the function name is optimized by the compiler and a suffix such as ".isra.0" is added, which will be used in the next patch.

---
 tools/include/uapi/linux/bpf.h |  1 +
 tools/lib/bpf/bpf.c            |  1 +
 tools/lib/bpf/bpf.h            |  1 +
 tools/lib/bpf/gen_loader.c     |  1 +
 tools/lib/bpf/libbpf.c         | 53 ++++++++++++++++++++++++++++++++++
 5 files changed, 57 insertions(+)
tools/include/uapi/linux/bpf.h +1 -0
index 07ee73cdf9..7016e47a70 100644
@@ -1577,6 +1577,7 @@ union bpf_attr {
 		 * If provided, prog_flags should have BPF_F_TOKEN_FD flag set.
 		 */
 		__s32		prog_token_fd;
+		__aligned_u64	fentry_func;
 		/* The fd_array_cnt can be used to pass the length of the
 		 * fd_array array. In this case all the [map] file descriptors
 		 * passed in this array will be bound to the program, even if
tools/lib/bpf/bpf.c +1 -0
index a9c3e33d0f..639a6b54e8 100644
@@ -312,6 +312,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
 
 	attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
 	attr.fd_array_cnt = OPTS_GET(opts, fd_array_cnt, 0);
+	attr.fentry_func = OPTS_GET(opts, fentry_func, 0);
 
 	if (log_level) {
 		attr.log_buf = ptr_to_u64(log_buf);
tools/lib/bpf/bpf.h +1 -0
index 777627d33d..b3340bcf7b 100644
@@ -83,6 +83,7 @@ struct bpf_prog_load_opts {
 	__u32 attach_btf_id;
 	__u32 attach_prog_fd;
 	__u32 attach_btf_obj_fd;
+	__u64 fentry_func;
 
 	const int *fd_array;
 
tools/lib/bpf/gen_loader.c +1 -0
index 113ae4abd3..d90874b746 100644
@@ -1023,6 +1023,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 	attr.kern_version = 0;
 	attr.insn_cnt = tgt_endian((__u32)insn_cnt);
 	attr.prog_flags = tgt_endian(load_attr->prog_flags);
+	attr.fentry_func = tgt_endian(load_attr->fentry_func);
 
 	attr.func_info_rec_size = tgt_endian(load_attr->func_info_rec_size);
 	attr.func_info_cnt = tgt_endian(load_attr->func_info_cnt);
tools/lib/bpf/libbpf.c +53 -0

65 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#2 lore raw

RFC PATCH 2/2 bpf: Get fentry func addr from user when BTF info invalid

In reply to Tao Chen

If kernel function optimized by the compiler, the function name in kallsyms will have suffix like ".isra.0" or others. And fentry uses function name from BTF to find the function address in kallsyms, in this situation, it will fail due to the inconsistency of function names. If so, try to use the function address passed from the user.

---
 include/linux/bpf.h      | 1 +
 include/uapi/linux/bpf.h | 1 +
 kernel/bpf/syscall.c     | 1 +
 kernel/bpf/verifier.c    | 9 +++++++++
 4 files changed, 12 insertions(+)
include/linux/bpf.h +1 -0
index 3f0cc89c06..fd53d1817f 100644
@@ -1626,6 +1626,7 @@ struct bpf_prog_aux {
 		struct work_struct work;
 		struct rcu_head	rcu;
 	};
+	u64 fentry_func;
 };
 
 struct bpf_prog {
include/uapi/linux/bpf.h +1 -0
index 07ee73cdf9..7016e47a70 100644
@@ -1577,6 +1577,7 @@ union bpf_attr {
 		 * If provided, prog_flags should have BPF_F_TOKEN_FD flag set.
 		 */
 		__s32		prog_token_fd;
+		__aligned_u64	fentry_func;
 		/* The fd_array_cnt can be used to pass the length of the
 		 * fd_array array. In this case all the [map] file descriptors
 		 * passed in this array will be bound to the program, even if
kernel/bpf/syscall.c +1 -0
index 9794446bc8..6c1e3572cc 100644
@@ -2892,6 +2892,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
 	prog->sleepable = !!(attr->prog_flags & BPF_F_SLEEPABLE);
 	prog->aux->attach_btf = attach_btf;
 	prog->aux->attach_btf_id = attr->attach_btf_id;
+	prog->aux->fentry_func = attr->fentry_func;
 	prog->aux->dst_prog = dst_prog;
 	prog->aux->dev_bound = !!attr->prog_ifindex;
 	prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
kernel/bpf/verifier.c +9 -0
index 54c6953a8b..507c281ddc 100644
@@ -23304,6 +23304,15 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
 			} else {
 				addr = kallsyms_lookup_name(tname);
 			}
+
+			if (!addr && (prog->expected_attach_type == BPF_TRACE_FENTRY ||
+					prog->expected_attach_type == BPF_TRACE_FEXIT)) {
+				fname = kallsyms_lookup((unsigned long)prog->aux->fentry_func,
+							NULL, NULL, NULL, trace_symbol);
+				if (fname)
+					addr = (long)prog->aux->fentry_func;
+			}
+
 			if (!addr) {
 				module_put(mod);
 				bpf_log(log,
Signature · 1 line
2.43.0
#3 lore raw

Re: [RFC PATCH bpf-next 2/2] bpf: Get fentry func addr from user when BTF info invalid

Quoted 31 lines · Tao Chen

If kernel function optimized by the compiler, the function name in kallsyms will have suffix like ".isra.0" or others. And fentry uses function name from BTF to find the function address in kallsyms, in this situation, it will fail due to the inconsistency of function names. If so, try to use the function address passed from the user.

---
 include/linux/bpf.h      | 1 +
 include/uapi/linux/bpf.h | 1 +
 kernel/bpf/syscall.c     | 1 +
 kernel/bpf/verifier.c    | 9 +++++++++
 4 files changed, 12 insertions(+)
include/linux/bpf.h +1 -0
index 3f0cc89c06..fd53d1817f 100644
@@ -1626,6 +1626,7 @@ struct bpf_prog_aux {
 		struct work_struct work;
 		struct rcu_head	rcu;
 	};
+	u64 fentry_func;
 };
 
 struct bpf_prog {
include/uapi/linux/bpf.h +0 -0
index 07ee73cdf9..7016e47a70 100644

include/uapi/linux/bpf.h union bpf_attr { · line 1580

Quoted 4 lines
include/uapi/linux/bpf.h +1 -0
@@ -1577,6 +1577,7 @@ union bpf_attr {
 		 * If provided, prog_flags should have BPF_F_TOKEN_FD flag set.
 		 */
 		__s32		prog_token_fd;
+		__aligned_u64	fentry_func;

since it can be used for fentry/fexit (I think from below?) might be clearer to call it attach_func_addr or similar.

Quoted 20 lines
include/uapi/linux/bpf.h +0 -0
 		/* The fd_array_cnt can be used to pass the length of the
 		 * fd_array array. In this case all the [map] file descriptors
 		 * passed in this array will be bound to the program, even if
kernel/bpf/syscall.c +1 -0
index 9794446bc8..6c1e3572cc 100644
@@ -2892,6 +2892,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
 	prog->sleepable = !!(attr->prog_flags & BPF_F_SLEEPABLE);
 	prog->aux->attach_btf = attach_btf;
 	prog->aux->attach_btf_id = attr->attach_btf_id;
+	prog->aux->fentry_func = attr->fentry_func;
 	prog->aux->dst_prog = dst_prog;
 	prog->aux->dev_bound = !!attr->prog_ifindex;
 	prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
kernel/bpf/verifier.c +0 -0
index 54c6953a8b..507c281ddc 100644

kernel/bpf/verifier.c int bpf_check_attach_target(struct bpf_verifier_log *log, · line 23313

Quoted 10 lines
kernel/bpf/verifier.c +7 -0
@@ -23304,6 +23304,15 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
 			} else {
 				addr = kallsyms_lookup_name(tname);
 			}
+
+			if (!addr && (prog->expected_attach_type == BPF_TRACE_FENTRY ||
+					prog->expected_attach_type == BPF_TRACE_FEXIT)) {
+				fname = kallsyms_lookup((unsigned long)prog->aux->fentry_func,
+							NULL, NULL, NULL, trace_symbol);
+				if (fname)
+					addr = (long)prog->aux->fentry_func;

We should do some validation that the fname we get back matches the BTF func name prefix (fname "foo.isra.0" matches "foo") I think?

Quoted 5 lines
kernel/bpf/verifier.c +2 -0
+			}
+
 			if (!addr) {
 				module_put(mod);
 				bpf_log(log,
#4 lore raw

Re: [RFC PATCH bpf-next 2/2] bpf: Get fentry func addr from user when BTF info invalid

Quoted 11 lines · Alan Maguire

kernel/bpf/verifier.c int bpf_check_attach_target(struct bpf_verifier_log *log, · line 23313

Quoted 7 lines
kernel/bpf/verifier.c +7 -0
+
+                     if (!addr && (prog->expected_attach_type == BPF_TRACE_FENTRY ||
+                                     prog->expected_attach_type == BPF_TRACE_FEXIT)) {
+                             fname = kallsyms_lookup((unsigned long)prog->aux->fentry_func,
+                                                     NULL, NULL, NULL, trace_symbol);
+                             if (fname)
+                                     addr = (long)prog->aux->fentry_func;

We should do some validation that the fname we get back matches the BTF func name prefix (fname "foo.isra.0" matches "foo") I think?

I don't think that will be enough.
User space should not be able to pass a random kernel address and convince the kernel that it matches a particular btf_id. As discussed in the other thread matching based on name is breaking apart.
pahole does all the safety check to make sure name/addr/btf_id are consistent.
We shouldn't be adding workarounds like this because pahole/btf/kernel build is not smart enough.

pw-bot: cr

author
#5 lore raw

Re: [RFC PATCH bpf-next 2/2] bpf: Get fentry func addr from user when BTF info invalid

Quoted 13 lines · Alexei Starovoitov
Quoted 11 lines · Alan Maguire

kernel/bpf/verifier.c int bpf_check_attach_target(struct bpf_verifier_log *log, · line 23313

Quoted 7 lines
kernel/bpf/verifier.c +7 -0
+
+                     if (!addr && (prog->expected_attach_type == BPF_TRACE_FENTRY ||
+                                     prog->expected_attach_type == BPF_TRACE_FEXIT)) {
+                             fname = kallsyms_lookup((unsigned long)prog->aux->fentry_func,
+                                                     NULL, NULL, NULL, trace_symbol);
+                             if (fname)
+                                     addr = (long)prog->aux->fentry_func;

We should do some validation that the fname we get back matches the BTF func name prefix (fname "foo.isra.0" matches "foo") I think?

Quoted 10 lines

I don't think that will be enough.
User space should not be able to pass a random kernel address and convince the kernel that it matches a particular btf_id. As discussed in the other thread matching based on name is breaking apart.
pahole does all the safety check to make sure name/addr/btf_id are consistent.
We shouldn't be adding workarounds like this because pahole/btf/kernel build is not smart enough.

Got it thanks for your reply, it is hoped that pahole/btf can have a better way to solve such problems.

Quoted 1 line

pw-bot: cr

Signature · 2 lines
Best Regards
Tao Chen
#6 lore raw
In reply to Tao Chen
Quoted 10 lines · Tao Chen

The previous discussion about fentry invalid on function optimeized by complier is as follows:
https://lore.kernel.org/bpf/[email protected]/

This seems to be something that pahole needs to resolve. However, Alan mentioned that there are many situations involved in this, and he proposed that the available_filter_functions_addr can be used to find the address of the real function. If we can get the real address from user, maybe this address can be used when the function obtained from the BTF is invalid.

Quoted 5 lines

The specific selftest has not been added yet. I just wrote a simple test program and ran it.

This is the initial RFC patch, feedback is welcome.

There's some discussion around this in the context of inlines and optimized functions at [1] that might be of interest. Ultimately for more complicated cases like this I'd really like to see us have a name/address relationship encoded in BTF so that we can be clear about relationships between a function site and its BTF.

The current state of affairs though is that a function will only make it into BTF if its function signature is consistent with the prototype, so unless I'm missing something the approach of passing an address to clarify the relationship seems like a valid one.

It would be great to have a test, can we try adding a function to bpf_testmod that a compiler will likely optimize to .isra.0 and trace it? This won't work in all cases, but we could skip the test if the function isn't optimized as wanted.

[1]
https://lore.kernel.org/dwarves/[email protected]/

Quoted 15 lines

Tao Chen (2):
libbpf: Try get fentry func addr from available_filter_functions_addr
bpf: Get fentry func addr from user when BTF info invalid

 include/linux/bpf.h            |  1 +
 include/uapi/linux/bpf.h       |  1 +
 kernel/bpf/syscall.c           |  1 +
 kernel/bpf/verifier.c          |  9 ++++++
 tools/include/uapi/linux/bpf.h |  1 +
 tools/lib/bpf/bpf.c            |  1 +
 tools/lib/bpf/bpf.h            |  1 +
 tools/lib/bpf/gen_loader.c     |  1 +
 tools/lib/bpf/libbpf.c         | 53 ++++++++++++++++++++++++++++++++++
 9 files changed, 69 insertions(+)