From: Leon Hwang <leon.hwang@linux.dev>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net,
menglong8.dong@gmail.com, Leon Hwang <leon.hwang@linux.dev>
Subject: [RFC PATCH bpf-next 4/5] libbpf: Capture error message on freplace attach failure
Date: Mon, 28 Jul 2025 22:23:45 +0800 [thread overview]
Message-ID: <20250728142346.95681-5-leon.hwang@linux.dev> (raw)
In-Reply-To: <20250728142346.95681-1-leon.hwang@linux.dev>
Extend 'bpf_link_create()' to support capturing log output from the kernel
when creating a freplace link.
Additionally, introduce a new API, 'bpf_program__attach_freplace_log()',
to allow users to retrieve detailed error message when a freplace program
fails to attach.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
tools/lib/bpf/bpf.c | 14 ++++++++++++--
tools/lib/bpf/bpf.h | 2 ++
tools/lib/bpf/libbpf.c | 18 ++++++++++++++----
tools/lib/bpf/libbpf.h | 4 ++++
tools/lib/bpf/libbpf.map | 1 +
5 files changed, 33 insertions(+), 6 deletions(-)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index de0c5c6c8ae6..25e88219d140 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -772,6 +772,7 @@ int bpf_link_create(int prog_fd, int target_fd,
{
const size_t attr_sz = offsetofend(union bpf_attr, link_create);
__u32 target_btf_id, iter_info_len, relative_id;
+ struct bpf_common_attr common_attrs;
int fd, err, relative_fd;
union bpf_attr attr;
@@ -785,7 +786,9 @@ int bpf_link_create(int prog_fd, int target_fd,
if (iter_info_len || target_btf_id) {
if (iter_info_len && target_btf_id)
return libbpf_err(-EINVAL);
- if (!OPTS_ZEROED(opts, target_btf_id))
+ if (iter_info_len && !OPTS_ZEROED(opts, target_btf_id))
+ return libbpf_err(-EINVAL);
+ if (target_btf_id && !OPTS_ZEROED(opts, tracing.log_size))
return libbpf_err(-EINVAL);
}
@@ -795,7 +798,12 @@ int bpf_link_create(int prog_fd, int target_fd,
attr.link_create.attach_type = attach_type;
attr.link_create.flags = OPTS_GET(opts, flags, 0);
+ memset(&common_attrs, 0, sizeof(common_attrs));
if (target_btf_id) {
+ common_attrs.log_buf = (__u64) OPTS_GET(opts, tracing.log_buf, NULL);
+ common_attrs.log_size = OPTS_GET(opts, tracing.log_size, 0);
+ if (common_attrs.log_buf && !feat_supported(NULL, FEAT_EXTENDED_SYSCALL))
+ return libbpf_err(-EOPNOTSUPP);
attr.link_create.target_btf_id = target_btf_id;
goto proceed;
}
@@ -931,7 +939,9 @@ int bpf_link_create(int prog_fd, int target_fd,
break;
}
proceed:
- fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
+ fd = !common_attrs.log_buf ? sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz)
+ : sys_bpf_fd_extended(BPF_LINK_CREATE, &attr, attr_sz,
+ &common_attrs, sizeof(common_attrs));
if (fd >= 0)
return fd;
/* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 38819071ecbe..5720d3e3df76 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -422,6 +422,8 @@ struct bpf_link_create_opts {
} uprobe_multi;
struct {
__u64 cookie;
+ const char *log_buf;
+ unsigned int log_size;
} tracing;
struct {
__u32 pf;
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index e067cb5776bd..9748466691f3 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -12949,9 +12949,11 @@ bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts);
}
-struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
- int target_fd,
- const char *attach_func_name)
+struct bpf_link *bpf_program__attach_freplace_log(const struct bpf_program *prog,
+ int target_fd,
+ const char *attach_func_name,
+ const char *log_buf,
+ unsigned int log_size)
{
int btf_id;
@@ -12975,7 +12977,8 @@ struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
return libbpf_err_ptr(btf_id);
target_opts.target_btf_id = btf_id;
-
+ target_opts.tracing.log_buf = log_buf;
+ target_opts.tracing.log_size = log_size;
return bpf_program_attach_fd(prog, target_fd, "freplace",
&target_opts);
} else {
@@ -12986,6 +12989,13 @@ struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
}
}
+struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
+ int target_fd,
+ const char *attach_func_name)
+{
+ return bpf_program__attach_freplace_log(prog, target_fd, attach_func_name, NULL, 0);
+}
+
struct bpf_link *
bpf_program__attach_iter(const struct bpf_program *prog,
const struct bpf_iter_attach_opts *opts)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index d1cf813a057b..01a0bf76adf5 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -829,6 +829,10 @@ bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);
LIBBPF_API struct bpf_link *
bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
LIBBPF_API struct bpf_link *
+bpf_program__attach_freplace_log(const struct bpf_program *prog,
+ int target_fd, const char *attach_func_name,
+ const char *log_buf, unsigned int log_size);
+LIBBPF_API struct bpf_link *
bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd, const char *attach_func_name);
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 83d3d1af5ed1..25cd2d54b7d9 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -449,5 +449,6 @@ LIBBPF_1.6.0 {
LIBBPF_1.7.0 {
global:
+ bpf_program__attach_freplace_log;
probe_sys_bpf_extended;
} LIBBPF_1.6.0;
--
2.50.1
next prev parent reply other threads:[~2025-07-28 14:24 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-28 14:23 [RFC PATCH bpf-next 0/5] bpf: Extend bpf syscall with common attributes support Leon Hwang
2025-07-28 14:23 ` [RFC PATCH bpf-next 1/5] " Leon Hwang
2025-07-29 3:43 ` kernel test robot
2025-07-29 3:55 ` Leon Hwang
2025-07-28 14:23 ` [RFC PATCH bpf-next 2/5] libbpf: Add support for extended bpf syscall Leon Hwang
2025-07-28 14:23 ` [RFC PATCH bpf-next 3/5] bpf: Report freplace attach failure reason via extended syscall Leon Hwang
2025-07-31 16:32 ` Alexei Starovoitov
2025-08-01 13:45 ` Leon Hwang
2025-07-28 14:23 ` Leon Hwang [this message]
2025-07-28 14:23 ` [RFC PATCH bpf-next 5/5] selftests/bpf: Add case to test freplace attach failure log Leon Hwang
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=20250728142346.95681-5-leon.hwang@linux.dev \
--to=leon.hwang@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=menglong8.dong@gmail.com \
/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 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.