From nobody Sun Feb 8 21:26:34 2026 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 124B33A8F7 for ; Wed, 30 Apr 2025 16:47:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746031631; cv=none; b=lbZ/boYPVcqoFfqmXWunVgG1mMjqT6aa0hysQ19undJ5IM3c3fLEVu4uFpfavplWZdeKE1xD6GC5kttg++k1ZhuNS/Yz+X3JpFKSdqOafRCujU2BLllDAQWB5A7ZB7N2JAB/paADyN3fHZYsvhsh4+K8++lL5zsrAc222Alu9sg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746031631; c=relaxed/simple; bh=cZBrAbeX5IT6YCCfasFvUYVkaCKs3khXkqTan3lrTb0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T/Jj+wozHBx/Qsj8v9Z1avGG9xM1j/fPjAf0NwfltIDVO+WUNxOYt+tC+LfSggiUgF5DleXwuieroGzD715ehhIFrrjaD3jaTRPr8iOCbr/Yfrf6L3z8wV7LStHNGPFaBKthVrqT6OsSqw+Sqa1gGPT4Ice9m9DPbCjcfNNE9XI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=ZbMJvq7M; arc=none smtp.client-ip=91.218.175.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ZbMJvq7M" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1746031626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uvDxFe99tlP67W9rJgqWgbLX4neeNVtGZon90RdKFIc=; b=ZbMJvq7MrNufKBtpXGIdWfjZD9z/b619WpdYi0gmr081ARrX13VtRJVZcFkXmcvrnZi8Z1 DPS6P4rqq+ARW/PmoBHXTbLKRsdzy/93pVxv3M7h70An1Cr5PUkmsKtbD/EQdMDcO8IHlo 82jSnavdI8DofvoG+qIk7rIJvnIbD7I= From: Tao Chen To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, alan.maguire@oracle.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Chen Subject: [RFC PATCH bpf-next 1/2] libbpf: Try get fentry func addr from available_filter_functions_addr Date: Thu, 1 May 2025 00:46:07 +0800 Message-Id: <20250430164608.3790552-2-chen.dylane@linux.dev> In-Reply-To: <20250430164608.3790552-1-chen.dylane@linux.dev> References: <20250430164608.3790552-1-chen.dylane@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Tao Chen --- 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(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 07ee73cdf9..7016e47a70 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -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 diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index a9c3e33d0f..639a6b54e8 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -312,6 +312,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, =20 attr.fd_array =3D ptr_to_u64(OPTS_GET(opts, fd_array, NULL)); attr.fd_array_cnt =3D OPTS_GET(opts, fd_array_cnt, 0); + attr.fentry_func =3D OPTS_GET(opts, fentry_func, 0); =20 if (log_level) { attr.log_buf =3D ptr_to_u64(log_buf); diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 777627d33d..b3340bcf7b 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -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; =20 const int *fd_array; =20 diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c index 113ae4abd3..d90874b746 100644 --- a/tools/lib/bpf/gen_loader.c +++ b/tools/lib/bpf/gen_loader.c @@ -1023,6 +1023,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen, attr.kern_version =3D 0; attr.insn_cnt =3D tgt_endian((__u32)insn_cnt); attr.prog_flags =3D tgt_endian(load_attr->prog_flags); + attr.fentry_func =3D tgt_endian(load_attr->fentry_func); =20 attr.func_info_rec_size =3D tgt_endian(load_attr->func_info_rec_size); attr.func_info_cnt =3D tgt_endian(load_attr->func_info_cnt); diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 37d563e140..624e75cf2f 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7456,6 +7456,50 @@ static int libbpf_prepare_prog_load(struct bpf_progr= am *prog, } =20 static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t= buf_sz); +static const char *tracefs_available_filter_functions_addrs(void); + +static void try_get_fentry_func_addr(const char *func_name, struct bpf_pro= g_load_opts *load_attr) +{ + const char *available_functions_file =3D tracefs_available_filter_functio= ns_addrs(); + char sym_name[500]; + FILE *f; + char *suffix; + int ret; + unsigned long long sym_addr; + + f =3D fopen(available_functions_file, "re"); + if (!f) { + pr_warn("failed to open %s: %s\n", available_functions_file, errstr(errn= o)); + return; + } + + while (true) { + ret =3D fscanf(f, "%llx %499s%*[^\n]\n", &sym_addr, sym_name); + if (ret =3D=3D EOF && feof(f)) + break; + + if (ret !=3D 2) { + pr_warn("failed to parse available_filter_functions_addrs entry: %d\n", + ret); + goto cleanup; + } + + if (strcmp(func_name, sym_name) =3D=3D 0) { + load_attr->fentry_func =3D sym_addr; + break; + } + /* find [func_name] optimized by compiler like [func_name].isra.0 */ + suffix =3D strstr(sym_name, "."); + if (suffix && strncmp(sym_name, func_name, + strlen(sym_name) - strlen(suffix)) =3D=3D 0) { + load_attr->fentry_func =3D sym_addr; + break; + } + } + +cleanup: + fclose(f); +} =20 static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program= *prog, struct bpf_insn *insns, int insns_cnt, @@ -7504,6 +7548,15 @@ static int bpf_object_load_prog(struct bpf_object *o= bj, struct bpf_program *prog load_attr.prog_ifindex =3D prog->prog_ifindex; load_attr.expected_attach_type =3D prog->expected_attach_type; =20 + if (load_attr.expected_attach_type =3D=3D BPF_TRACE_FENTRY || + load_attr.expected_attach_type =3D=3D BPF_TRACE_FEXIT) { + const char *func_name; + + func_name =3D strchr(prog->sec_name, '/'); + if (func_name) + try_get_fentry_func_addr(++func_name, &load_attr); + } + /* specify func_info/line_info only if kernel supports them */ if (obj->btf && btf__fd(obj->btf) >=3D 0 && kernel_supports(obj, FEAT_BTF= _FUNC)) { load_attr.prog_btf_fd =3D btf__fd(obj->btf); --=20 2.43.0 From nobody Sun Feb 8 21:26:34 2026 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 789F7264A90 for ; Wed, 30 Apr 2025 16:47:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746031635; cv=none; b=dop/I4zhZ2hljVoenA0WMPsoCpY+JE0jRvTCRuR1wNjwZ8PpWY7q7DZsW4/AII8tFL8oldZtOznxUibKOelyULX3T3R6M6VBrAX+HKQIeA5rHgsQiIzHtBd9DUFVryETah64IfkXRIvJgWN7AfEPx6hQdaHZcQFKE8qCMbuc57k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746031635; c=relaxed/simple; bh=q9ytZ2bqnS4AW5iOsrYAArh8ag5ZF0zJhleYllk09Tw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=P4FFQ1BZie3zDrxDZ1R8LO/bjN5OwkTECDgDnUGTaPnplVdRK9xL9l9NkCd0fEdn4Br3uBIDppLomcdLHQjGdNue2NiIuhkgy/fNyB/mNTzbOIMCbWw+GVam4uZX08r0NAatuLH3TrbksGMRxio5V4H06ERDAtjDtluCFkSPceA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=otrJNo1A; arc=none smtp.client-ip=91.218.175.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="otrJNo1A" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1746031631; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DZoAG17QbBV/4PAv7+P9YXWHmX6KfllOZqrH5JYkMPo=; b=otrJNo1AcYDrI0/AhSwcB4K20up9Ma9jsPFVr8QgJDLYZPBHJMsocjpyJu5pYC7+5iAh2l MowTfTwI3XJeWRSC5LtPu6KtW/kU2TffsYPra/INRbRQ7eplhEc6R82A9VqF02uWb1u0Bq KKgT4Yctyk6VaYze8jIb1R8QjDhsSco= From: Tao Chen To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, alan.maguire@oracle.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Chen Subject: [RFC PATCH bpf-next 2/2] bpf: Get fentry func addr from user when BTF info invalid Date: Thu, 1 May 2025 00:46:08 +0800 Message-Id: <20250430164608.3790552-3-chen.dylane@linux.dev> In-Reply-To: <20250430164608.3790552-1-chen.dylane@linux.dev> References: <20250430164608.3790552-1-chen.dylane@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Tao Chen --- 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(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 3f0cc89c06..fd53d1817f 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1626,6 +1626,7 @@ struct bpf_prog_aux { struct work_struct work; struct rcu_head rcu; }; + u64 fentry_func; }; =20 struct bpf_prog { diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 07ee73cdf9..7016e47a70 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -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 diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 9794446bc8..6c1e3572cc 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2892,6 +2892,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr= _t uattr, u32 uattr_size) prog->sleepable =3D !!(attr->prog_flags & BPF_F_SLEEPABLE); prog->aux->attach_btf =3D attach_btf; prog->aux->attach_btf_id =3D attr->attach_btf_id; + prog->aux->fentry_func =3D attr->fentry_func; prog->aux->dst_prog =3D dst_prog; prog->aux->dev_bound =3D !!attr->prog_ifindex; prog->aux->xdp_has_frags =3D attr->prog_flags & BPF_F_XDP_HAS_FRAGS; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 54c6953a8b..507c281ddc 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -23304,6 +23304,15 @@ int bpf_check_attach_target(struct bpf_verifier_lo= g *log, } else { addr =3D kallsyms_lookup_name(tname); } + + if (!addr && (prog->expected_attach_type =3D=3D BPF_TRACE_FENTRY || + prog->expected_attach_type =3D=3D BPF_TRACE_FEXIT)) { + fname =3D kallsyms_lookup((unsigned long)prog->aux->fentry_func, + NULL, NULL, NULL, trace_symbol); + if (fname) + addr =3D (long)prog->aux->fentry_func; + } + if (!addr) { module_put(mod); bpf_log(log, --=20 2.43.0