* [PATCH bpf-next 1/4] bpf: Add log support for perf link_create
2026-12-02 15:37 [PATCH bpf-next 0/4] bpf: Add log support for perf link_create Leon Hwang
@ 2026-12-02 15:37 ` Leon Hwang
2026-12-02 16:26 ` bot+bpf-ci
2026-12-02 15:37 ` [PATCH bpf-next 2/4] bpf: Add log support for kprobe_multi link_create Leon Hwang
` (2 subsequent siblings)
3 siblings, 1 reply; 6+ messages in thread
From: Leon Hwang @ 2026-12-02 15:37 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Ian Rogers, Adrian Hunter, James Clark,
KP Singh, Matt Bobrowski, Shuah Khan, Leon Hwang, linux-kernel,
linux-trace-kernel, linux-perf-users, linux-kselftest
BPF_LINK_CREATE returns -EINVAL for several perf event attach failures.
It also returns -EEXIST when the perf event already has a program or
when the same program is attached to the trace event through another
perf event.
Pass the link log through the perf event attach path so callers can
tell which check failed. Keep PERF_EVENT_IOC_SET_BPF on the same path
without a log. Provide a bpf_log() stub for builds without
CONFIG_BPF_SYSCALL.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
include/linux/bpf_verifier.h | 7 ++++
include/linux/trace_events.h | 9 ++--
kernel/bpf/syscall.c | 20 +++++----
kernel/events/core.c | 79 ++++++++++++++++++++++++------------
kernel/trace/bpf_trace.c | 15 +++++--
5 files changed, 91 insertions(+), 39 deletions(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 50b3f8ab9720..a27f66a25618 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1607,8 +1607,15 @@ __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,
const char *fmt, va_list args);
__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
const char *fmt, ...);
+#ifdef CONFIG_BPF_SYSCALL
__printf(2, 3) void bpf_log(struct bpf_verifier_log *log,
const char *fmt, ...);
+#else
+static inline __printf(2, 3) void bpf_log(struct bpf_verifier_log *log,
+ const char *fmt, ...)
+{
+}
+#endif
int bpf_vlog_init(struct bpf_verifier_log *log, u32 log_level,
char __user *log_buf, u32 log_size);
void bpf_vlog_reset(struct bpf_verifier_log *log, u64 new_pos);
diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
index dff8a209875c..0fe900813581 100644
--- a/include/linux/trace_events.h
+++ b/include/linux/trace_events.h
@@ -781,7 +781,8 @@ trace_trigger_soft_disabled(struct trace_event_file *file)
#ifdef CONFIG_BPF_EVENTS
unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx);
unsigned int trace_call_bpf_faultable(struct trace_event_call *call, void *ctx);
-int perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
+int perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie,
+ struct bpf_verifier_log *log);
void perf_event_detach_bpf_prog(struct perf_event *event);
int perf_event_query_prog_array(struct perf_event *event, void __user *info);
@@ -811,7 +812,8 @@ static inline unsigned int trace_call_bpf_faultable(struct trace_event_call *cal
}
static inline int
-perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie)
+perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie,
+ struct bpf_verifier_log *log)
{
return -EOPNOTSUPP;
}
@@ -924,7 +926,8 @@ extern void ftrace_profile_free_filter(struct perf_event *event);
void perf_trace_buf_update(void *record, u16 type);
void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp);
-int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
+int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie,
+ struct bpf_verifier_log *log);
void perf_event_free_bpf_prog(struct perf_event *event);
void bpf_trace_run1(struct bpf_raw_tp_link *link, u64 arg1);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 801175b809e7..2cd1846e5c76 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -4355,7 +4355,8 @@ static const struct bpf_link_ops bpf_perf_link_lops = {
.show_fdinfo = bpf_perf_link_show_fdinfo,
};
-static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
struct bpf_link_primer link_primer;
struct bpf_perf_link *link;
@@ -4363,12 +4364,16 @@ static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro
struct file *perf_file;
int err;
- if (attr->link_create.flags)
+ if (attr->link_create.flags) {
+ bpf_log(log, "flags must be zero.\n");
return -EINVAL;
+ }
perf_file = perf_event_get(attr->link_create.target_fd);
- if (IS_ERR(perf_file))
+ if (IS_ERR(perf_file)) {
+ bpf_log(log, "Invalid target_fd.\n");
return PTR_ERR(perf_file);
+ }
link = kzalloc_obj(*link, GFP_USER);
if (!link) {
@@ -4386,7 +4391,7 @@ static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro
}
event = perf_file->private_data;
- err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
+ err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie, log);
if (err) {
bpf_link_cleanup(&link_primer);
goto out_put_file;
@@ -4401,7 +4406,8 @@ static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro
return err;
}
#else
-static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
return -EOPNOTSUPP;
}
@@ -6029,11 +6035,11 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
#endif
case BPF_PROG_TYPE_PERF_EVENT:
case BPF_PROG_TYPE_TRACEPOINT:
- ret = bpf_perf_link_attach(attr, prog);
+ ret = bpf_perf_link_attach(attr, prog, log);
break;
case BPF_PROG_TYPE_KPROBE:
if (attr->link_create.attach_type == BPF_PERF_EVENT)
- ret = bpf_perf_link_attach(attr, prog);
+ ret = bpf_perf_link_attach(attr, prog, log);
else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI ||
attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION)
ret = bpf_kprobe_multi_link_attach(attr, prog);
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 614f9f0e6500..292945ff20c2 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -43,6 +43,7 @@
#include <linux/mman.h>
#include <linux/compat.h>
#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
#include <linux/filter.h>
#include <linux/namei.h>
#include <linux/parser.h>
@@ -6633,7 +6634,8 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
struct perf_event_attr *attr);
static int __perf_event_set_bpf_prog(struct perf_event *event,
struct bpf_prog *prog,
- u64 bpf_cookie);
+ u64 bpf_cookie,
+ struct bpf_verifier_log *log);
static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
{
@@ -6699,7 +6701,7 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
if (IS_ERR(prog))
return PTR_ERR(prog);
- err = __perf_event_set_bpf_prog(event, prog, 0);
+ err = __perf_event_set_bpf_prog(event, prog, 0, NULL);
if (err) {
bpf_prog_put(prog);
return err;
@@ -10830,17 +10832,22 @@ static int bpf_overflow_handler(struct perf_event *event,
static inline int perf_event_set_bpf_handler(struct perf_event *event,
struct bpf_prog *prog,
- u64 bpf_cookie)
+ u64 bpf_cookie,
+ struct bpf_verifier_log *log)
{
if (event->overflow_handler_context)
/* hw breakpoint or kernel counter */
return -EINVAL;
- if (event->prog)
+ if (event->prog) {
+ bpf_log(log, "Target perf event already has a program attached.\n");
return -EEXIST;
+ }
- if (prog->type != BPF_PROG_TYPE_PERF_EVENT)
+ if (prog->type != BPF_PROG_TYPE_PERF_EVENT) {
+ bpf_log(log, "Prog type must be BPF_PROG_TYPE_PERF_EVENT.\n");
return -EINVAL;
+ }
if (event->attr.precise_ip &&
prog->call_get_stack &&
@@ -10884,7 +10891,8 @@ static inline int bpf_overflow_handler(struct perf_event *event,
static inline int perf_event_set_bpf_handler(struct perf_event *event,
struct bpf_prog *prog,
- u64 bpf_cookie)
+ u64 bpf_cookie,
+ struct bpf_verifier_log *log)
{
return -EOPNOTSUPP;
}
@@ -11837,7 +11845,8 @@ static inline bool perf_event_is_tracing(struct perf_event *event)
static int __perf_event_set_bpf_prog(struct perf_event *event,
struct bpf_prog *prog,
- u64 bpf_cookie)
+ u64 bpf_cookie,
+ struct bpf_verifier_log *log)
{
bool is_kprobe, is_uprobe, is_tracepoint, is_syscall_tp;
@@ -11845,25 +11854,33 @@ static int __perf_event_set_bpf_prog(struct perf_event *event,
return -ENODEV;
if (!perf_event_is_tracing(event))
- return perf_event_set_bpf_handler(event, prog, bpf_cookie);
+ return perf_event_set_bpf_handler(event, prog, bpf_cookie, log);
is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE;
is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE;
is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
is_syscall_tp = is_syscall_trace_event(event->tp_event);
- if (!is_kprobe && !is_uprobe && !is_tracepoint && !is_syscall_tp)
- /* bpf programs can only be attached to u/kprobe or tracepoint */
+ if (!is_kprobe && !is_uprobe && !is_tracepoint && !is_syscall_tp) {
+ bpf_log(log, "Programs can only be attached to uprobe, kprobe or tracepoint.\n");
+ return -EINVAL;
+ }
+
+ if ((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) {
+ bpf_log(log, "Only kprobe program can attach to uprobe or kprobe.\n");
return -EINVAL;
+ }
- if (((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) ||
- (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
- (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT))
+ if ((is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
+ (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
+ bpf_log(log, "Only tracepoint program can attach to tracepoint.\n");
return -EINVAL;
+ }
if (prog->type == BPF_PROG_TYPE_KPROBE) {
- /* only uprobe programs are allowed to be sleepable */
- if (prog->sleepable && !is_uprobe)
+ if (prog->sleepable && !is_uprobe) {
+ bpf_log(log, "Only uprobe programs are allowed to be sleepable.\n");
return -EINVAL;
+ }
/* only standard kprobe/uprobe programs are allowed to attach */
if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ||
@@ -11878,37 +11895,45 @@ static int __perf_event_set_bpf_prog(struct perf_event *event,
* Sleepable tracepoint programs can only attach to faultable
* tracepoints. Currently only syscall tracepoints are faultable.
*/
- if (!is_syscall_tp)
+ if (!is_syscall_tp) {
+ bpf_log(log,
+ "Sleepable tracepoint programs require a faultable syscall tracepoint.\n");
return -EINVAL;
+ }
}
- /* Kprobe override only works for kprobes, not uprobes. */
- if (prog->kprobe_override && !is_kprobe)
+ if (prog->kprobe_override && !is_kprobe) {
+ bpf_log(log, "Kprobe override only works for kprobes, not uprobes.\n");
return -EINVAL;
+ }
- /* Writing to context allowed only for uprobes. */
- if (prog->aux->kprobe_write_ctx && !is_uprobe)
+ if (prog->aux->kprobe_write_ctx && !is_uprobe) {
+ bpf_log(log, "Writing to context is only allowed for uprobes.\n");
return -EINVAL;
+ }
if (is_tracepoint || is_syscall_tp) {
int off = trace_event_get_offsets(event->tp_event);
- if (prog->aux->max_ctx_offset > off)
+ if (prog->aux->max_ctx_offset > off) {
+ bpf_log(log, "Cannot access more ctx than the target tracepoint.\n");
return -EACCES;
+ }
}
- return perf_event_attach_bpf_prog(event, prog, bpf_cookie);
+ return perf_event_attach_bpf_prog(event, prog, bpf_cookie, log);
}
int perf_event_set_bpf_prog(struct perf_event *event,
struct bpf_prog *prog,
- u64 bpf_cookie)
+ u64 bpf_cookie,
+ struct bpf_verifier_log *log)
{
struct perf_event_context *ctx;
int ret;
ctx = perf_event_ctx_lock(event);
- ret = __perf_event_set_bpf_prog(event, prog, bpf_cookie);
+ ret = __perf_event_set_bpf_prog(event, prog, bpf_cookie, log);
perf_event_ctx_unlock(event, ctx);
return ret;
@@ -11938,14 +11963,16 @@ static void perf_event_free_filter(struct perf_event *event)
static int __perf_event_set_bpf_prog(struct perf_event *event,
struct bpf_prog *prog,
- u64 bpf_cookie)
+ u64 bpf_cookie,
+ struct bpf_verifier_log *log)
{
return -ENOENT;
}
int perf_event_set_bpf_prog(struct perf_event *event,
struct bpf_prog *prog,
- u64 bpf_cookie)
+ u64 bpf_cookie,
+ struct bpf_verifier_log *log)
{
return -ENOENT;
}
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 2031c3727320..44ea62054f81 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1969,7 +1969,8 @@ static DEFINE_MUTEX(bpf_event_mutex);
int perf_event_attach_bpf_prog(struct perf_event *event,
struct bpf_prog *prog,
- u64 bpf_cookie)
+ u64 bpf_cookie,
+ struct bpf_verifier_log *log)
{
struct bpf_prog_array *old_array;
struct bpf_prog_array *new_array;
@@ -1981,22 +1982,30 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
*/
if (prog->kprobe_override &&
(!trace_kprobe_on_func_entry(event->tp_event) ||
- !trace_kprobe_error_injectable(event->tp_event)))
+ !trace_kprobe_error_injectable(event->tp_event))) {
+ bpf_log(log,
+ "Kprobe override requires a function entry on the error injection list.\n");
return -EINVAL;
+ }
mutex_lock(&bpf_event_mutex);
- if (event->prog)
+ if (event->prog) {
+ bpf_log(log, "Target perf event already has a program attached.\n");
goto unlock;
+ }
old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
if (old_array &&
bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
+ bpf_log(log, "Too many programs (limit %d).\n", BPF_TRACE_MAX_PROGS);
ret = -E2BIG;
goto unlock;
}
ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
+ if (ret == -EEXIST)
+ bpf_log(log, "Program already attached to this trace event.\n");
if (ret < 0)
goto unlock;
--
2.55.0
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH bpf-next 1/4] bpf: Add log support for perf link_create
2026-12-02 15:37 ` [PATCH bpf-next 1/4] " Leon Hwang
@ 2026-12-02 16:26 ` bot+bpf-ci
0 siblings, 0 replies; 6+ messages in thread
From: bot+bpf-ci @ 2026-12-02 16:26 UTC (permalink / raw)
To: leon.hwang, bpf
Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai, john.fastabend,
rostedt, mhiramat, mathieu.desnoyers, peterz, mingo, acme,
namhyung, mark.rutland, alexander.shishkin, irogers,
adrian.hunter, james.clark, kpsingh, matt, shuah, leon.hwang,
linux-kernel, linux-trace-kernel, linux-perf-users,
linux-kselftest, ast, andrii, daniel, martin.lau, eddyz87,
yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3248 bytes --]
> bpf: Add log support for perf link_create
TL;DR: bpf_log() under bpf_event_mutex in perf_event_attach_bpf_prog() may
fault on the user log buffer, possibly causing an ABBA deadlock with mmap_lock
or stalling all perf BPF attaches; log after unlocking.
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
[ ... ]
> mutex_lock(&bpf_event_mutex);
>
> - if (event->prog)
> + if (event->prog) {
> + bpf_log(log, "Target perf event already has a program attached.\n");
> goto unlock;
> + }
>
> old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
> if (old_array &&
> bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
> + bpf_log(log, "Too many programs (limit %d).\n", BPF_TRACE_MAX_PROGS);
> ret = -E2BIG;
> goto unlock;
> }
>
> ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
> + if (ret == -EEXIST)
> + bpf_log(log, "Program already attached to this trace event.\n");
> if (ret < 0)
> goto unlock;
Can these bpf_log() calls run safely under bpf_event_mutex?
On the BPF_LINK_CREATE path the log is a user log, so bpf_log() ends up in
bpf_verifier_vlog(), which calls copy_to_user() on log->ubuf. That can
fault, and might_fault() tells lockdep about current->mm->mmap_lock. This
records a bpf_event_mutex -> mmap_lock dependency.
The opposite order already exists:
perf_mmap_close() [mmap_lock held via remove_vma()]
put_event() [last reference]
_free_event()
perf_event_free_bpf_prog()
perf_event_detach_bpf_prog()
mutex_lock(&bpf_event_mutex);
Can this produce an ABBA deadlock between mmap_lock and bpf_event_mutex
when the log page has to be faulted in? It looks like the same inversion
that commit 3a38bb98d9ab ("bpf/tracing: fix a deadlock in
perf_event_detach_bpf_prog") fixed by moving copy_to_user() out of
bpf_event_mutex in perf_event_query_prog_array(), which still copies to
user only after mutex_unlock(&bpf_event_mutex).
The failing checks are easy to reach from userspace. Calling
BPF_LINK_CREATE twice on the same tracepoint, kprobe or uprobe perf fd hits
the event->prog branch, and attaching the same program through a second
perf event on the same trace event returns -EEXIST from
bpf_prog_array_copy().
Also, since bpf_event_mutex is global, could a slow or never-completing
fault on the log buffer (for example a FUSE-backed mapping) block every
perf-based BPF attach, detach and PERF_EVENT_IOC_QUERY_BPF on the system?
The bpf_log() calls made under ctx->mutex in __perf_event_set_bpf_prog()
and perf_event_set_bpf_handler(), and the kprobe_override message before
mutex_lock(), look fine.
Would it work to record the failure reason inside the critical section (for
example in a const char *msg) and call bpf_log() after
mutex_unlock(&bpf_event_mutex)? No later commit in the series changes
perf_event_attach_bpf_prog().
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46921726766
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next 2/4] bpf: Add log support for kprobe_multi link_create
2026-12-02 15:37 [PATCH bpf-next 0/4] bpf: Add log support for perf link_create Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 1/4] " Leon Hwang
@ 2026-12-02 15:37 ` Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 3/4] bpf: Add log support for uprobe_multi link_create Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 4/4] selftests/bpf: Test perf event and multi-probe link creation logs Leon Hwang
3 siblings, 0 replies; 6+ messages in thread
From: Leon Hwang @ 2026-12-02 15:37 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Ian Rogers, Adrian Hunter, James Clark,
KP Singh, Matt Bobrowski, Shuah Khan, Leon Hwang, linux-kernel,
linux-trace-kernel, linux-perf-users, linux-kselftest
BPF_LINK_CREATE returns -EFAULT for unreadable kprobe_multi target
or cookie arrays. It also returns -E2BIG for both too many probes
and an overlong symbol name.
Pass the link log through kprobe multi and session attachment.
Identify the failed check or unreadable array, and include the index
when reading a symbol pointer or name fails or the name is too long.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
include/linux/trace_events.h | 6 ++--
kernel/bpf/syscall.c | 2 +-
kernel/trace/bpf_trace.c | 66 ++++++++++++++++++++++++++----------
3 files changed, 53 insertions(+), 21 deletions(-)
diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
index 0fe900813581..e855ca8a10f1 100644
--- a/include/linux/trace_events.h
+++ b/include/linux/trace_events.h
@@ -796,7 +796,8 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
u32 *fd_type, const char **buf,
u64 *probe_offset, u64 *probe_addr,
unsigned long *missed);
-int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
+int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log);
int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
struct bpf_verifier_log *log);
@@ -849,7 +850,8 @@ static inline int bpf_get_perf_event_info(const struct perf_event *event,
return -EOPNOTSUPP;
}
static inline int
-bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
return -EOPNOTSUPP;
}
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 2cd1846e5c76..25f4a0c9c9c1 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -6042,7 +6042,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
ret = bpf_perf_link_attach(attr, prog, log);
else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI ||
attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION)
- ret = bpf_kprobe_multi_link_attach(attr, prog);
+ ret = bpf_kprobe_multi_link_attach(attr, prog, log);
else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI ||
attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION)
ret = bpf_uprobe_multi_link_attach(attr, prog);
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 44ea62054f81..9ab7ceaed014 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2404,7 +2404,8 @@ static unsigned long ftrace_get_entry_ip(unsigned long fentry_ip)
return ip ? : fentry_ip;
}
-static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
+static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt,
+ struct bpf_verifier_log *log)
{
unsigned long __user usymbol;
const char **syms = NULL;
@@ -2412,8 +2413,10 @@ static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32
int err = -ENOMEM;
unsigned int i;
- if (!access_ok(usyms, cnt * sizeof(*usyms)))
+ if (!access_ok(usyms, cnt * sizeof(*usyms))) {
+ bpf_log(log, "Symbols access check failed.\n");
return -EFAULT;
+ }
syms = kvmalloc_objs(*syms, cnt);
if (!syms)
@@ -2425,14 +2428,20 @@ static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32
for (p = buf, i = 0; i < cnt; i++) {
if (get_user(usymbol, usyms + i)) {
+ bpf_log(log, "Read syms[%u] pointer failed.\n", i);
err = -EFAULT;
goto error;
}
err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
- if (err == KSYM_NAME_LEN)
+ if (err == KSYM_NAME_LEN) {
+ bpf_log(log, "Syms[%u] is too long.\n", i);
err = -E2BIG;
- if (err < 0)
goto error;
+ }
+ if (err < 0) {
+ bpf_log(log, "Copy syms[%u] name failed.\n", i);
+ goto error;
+ }
syms[i] = p;
p += err + 1;
}
@@ -2813,7 +2822,8 @@ static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)
return 0;
}
-int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
struct bpf_kprobe_multi_link *link = NULL;
struct bpf_link_primer link_primer;
@@ -2825,38 +2835,53 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
void __user *usyms;
int err;
- /* no support for 32bit archs yet */
- if (sizeof(u64) != sizeof(void *))
+ if (sizeof(u64) != sizeof(void *)) {
+ bpf_log(log, "No support for 32bit archs yet.\n");
return -EOPNOTSUPP;
+ }
- if (attr->link_create.flags)
+ if (attr->link_create.flags) {
+ bpf_log(log, "flags must be zero.\n");
return -EINVAL;
+ }
- if (!is_kprobe_multi(prog))
+ if (!is_kprobe_multi(prog)) {
+ bpf_log(log, "expected_attach_type must be kprobe_multi or kprobe_session.\n");
return -EINVAL;
+ }
- /* kprobe_multi is not allowed to be sleepable. */
- if (prog->sleepable)
+ if (prog->sleepable) {
+ bpf_log(log, "Sleepable prog is not allowed.\n");
return -EINVAL;
+ }
- /* Writing to context is not allowed for kprobes. */
- if (prog->aux->kprobe_write_ctx)
+ if (prog->aux->kprobe_write_ctx) {
+ bpf_log(log, "Writing to context is not allowed.\n");
return -EINVAL;
+ }
flags = attr->link_create.kprobe_multi.flags;
- if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
+ if (flags & ~BPF_F_KPROBE_MULTI_RETURN) {
+ bpf_log(log, "Invalid kprobe_multi.flags.\n");
return -EINVAL;
+ }
uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
- if (!!uaddrs == !!usyms)
+ if (!!uaddrs == !!usyms) {
+ bpf_log(log,
+ "Exactly one of kprobe_multi.addrs and kprobe_multi.syms must be set.\n");
return -EINVAL;
+ }
cnt = attr->link_create.kprobe_multi.cnt;
if (!cnt)
return -EINVAL;
- if (cnt > MAX_KPROBE_MULTI_CNT)
+ if (cnt > MAX_KPROBE_MULTI_CNT) {
+ bpf_log(log, "kprobe_multi.cnt %u is larger than limit %u.\n",
+ cnt, MAX_KPROBE_MULTI_CNT);
return -E2BIG;
+ }
size = cnt * sizeof(*addrs);
addrs = kvmalloc_objs(*addrs, cnt);
@@ -2871,6 +2896,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
goto error;
}
if (copy_from_user(cookies, ucookies, size)) {
+ bpf_log(log, "Read cookies failed.\n");
err = -EFAULT;
goto error;
}
@@ -2878,6 +2904,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
if (uaddrs) {
if (copy_from_user(addrs, uaddrs, size)) {
+ bpf_log(log, "Read addrs failed.\n");
err = -EFAULT;
goto error;
}
@@ -2887,7 +2914,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
};
struct user_syms us;
- err = copy_user_syms(&us, usyms, cnt);
+ err = copy_user_syms(&us, usyms, cnt, log);
if (err)
goto error;
@@ -2904,6 +2931,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
}
if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) {
+ bpf_log(log, "All kprobe override targets must be on the error injection list.\n");
err = -EINVAL;
goto error;
}
@@ -2969,8 +2997,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
return err;
}
#else /* !CONFIG_FPROBE */
-int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
+ bpf_log(log, "This kernel does not support fprobe.\n");
return -EOPNOTSUPP;
}
static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
--
2.55.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH bpf-next 3/4] bpf: Add log support for uprobe_multi link_create
2026-12-02 15:37 [PATCH bpf-next 0/4] bpf: Add log support for perf link_create Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 1/4] " Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 2/4] bpf: Add log support for kprobe_multi link_create Leon Hwang
@ 2026-12-02 15:37 ` Leon Hwang
2026-12-02 15:37 ` [PATCH bpf-next 4/4] selftests/bpf: Test perf event and multi-probe link creation logs Leon Hwang
3 siblings, 0 replies; 6+ messages in thread
From: Leon Hwang @ 2026-12-02 15:37 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Ian Rogers, Adrian Hunter, James Clark,
KP Singh, Matt Bobrowski, Shuah Khan, Leon Hwang, linux-kernel,
linux-trace-kernel, linux-perf-users, linux-kselftest
BPF_LINK_CREATE returns -EFAULT when it cannot read an offset, reference
counter offset, or cookie. Userspace cannot tell which array or entry
caused the failure from errno alone.
Pass the link_create log through the uprobe multi and session attach
path. Report invalid target path arguments and identify the failing
entry when reading probe attributes or registering a uprobe.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
include/linux/trace_events.h | 6 ++-
kernel/bpf/syscall.c | 2 +-
kernel/trace/bpf_trace.c | 77 ++++++++++++++++++++++++++++--------
3 files changed, 66 insertions(+), 19 deletions(-)
diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
index e855ca8a10f1..6e46a2a75e4c 100644
--- a/include/linux/trace_events.h
+++ b/include/linux/trace_events.h
@@ -798,7 +798,8 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
unsigned long *missed);
int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
struct bpf_verifier_log *log);
-int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
+int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log);
int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
struct bpf_verifier_log *log);
#else
@@ -856,7 +857,8 @@ bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
return -EOPNOTSUPP;
}
static inline int
-bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
return -EOPNOTSUPP;
}
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 25f4a0c9c9c1..152044e5fb62 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -6045,7 +6045,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
ret = bpf_kprobe_multi_link_attach(attr, prog, log);
else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI ||
attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION)
- ret = bpf_uprobe_multi_link_attach(attr, prog);
+ ret = bpf_uprobe_multi_link_attach(attr, prog, log);
break;
default:
bpf_log(log, "Invalid prog type.\n");
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 9ab7ceaed014..79e85c940f55 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -3282,23 +3282,30 @@ static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
return run_ctx->uprobe->cookie;
}
-static int bpf_uprobe_multi_get_path(const union bpf_attr *attr, struct path *path)
+static int bpf_uprobe_multi_get_path(const union bpf_attr *attr, struct path *path,
+ struct bpf_verifier_log *log)
{
void __user *upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path);
u32 path_fd = attr->link_create.uprobe_multi.path_fd;
u32 flags = attr->link_create.uprobe_multi.flags;
+ int ret;
if (flags & BPF_F_UPROBE_MULTI_PATH_FD) {
/*
* When BPF_F_UPROBE_MULTI_PATH_FD is set, the executable is
* identified by path_fd, upath must be NULL.
*/
- if (upath)
+ if (upath) {
+ bpf_log(log,
+ "uprobe_multi.path must not be set when BPF_F_UPROBE_MULTI_PATH_FD is set.\n");
return -EINVAL;
+ }
CLASS(fd, f)(path_fd);
- if (fd_empty(f))
+ if (fd_empty(f)) {
+ bpf_log(log, "Invalid uprobe_multi.path_fd.\n");
return -EBADF;
+ }
*path = fd_file(f)->f_path;
path_get(path);
return 0;
@@ -3308,13 +3315,23 @@ static int bpf_uprobe_multi_get_path(const union bpf_attr *attr, struct path *pa
* When BPF_F_UPROBE_MULTI_PATH_FD is not set, the path is resolved
* relative to the cwd (AT_FDCWD) or absolute using the upath string.
*/
- if (!upath || path_fd)
+ if (!upath) {
+ bpf_log(log, "uprobe_multi.path must be set.\n");
return -EINVAL;
+ }
+ if (path_fd) {
+ bpf_log(log, "uprobe_multi.path_fd must not be set.\n");
+ return -EINVAL;
+ }
- return user_path_at(AT_FDCWD, upath, LOOKUP_FOLLOW, path);
+ ret = user_path_at(AT_FDCWD, upath, LOOKUP_FOLLOW, path);
+ if (ret)
+ bpf_log(log, "Lookup path failed.\n");
+ return ret;
}
-int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
struct bpf_uprobe_multi_link *link = NULL;
unsigned long __user *uref_ctr_offsets;
@@ -3329,19 +3346,26 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
pid_t pid;
int err;
- /* no support for 32bit archs yet */
- if (sizeof(u64) != sizeof(void *))
+ if (sizeof(u64) != sizeof(void *)) {
+ bpf_log(log, "No support for 32bit archs yet.\n");
return -EOPNOTSUPP;
+ }
- if (attr->link_create.flags)
+ if (attr->link_create.flags) {
+ bpf_log(log, "flags must be zero.\n");
return -EINVAL;
+ }
- if (!is_uprobe_multi(prog))
+ if (!is_uprobe_multi(prog)) {
+ bpf_log(log, "expected_attach_type must be uprobe_multi or uprobe_session.\n");
return -EINVAL;
+ }
flags = attr->link_create.uprobe_multi.flags;
- if (flags & ~(BPF_F_UPROBE_MULTI_RETURN | BPF_F_UPROBE_MULTI_PATH_FD))
+ if (flags & ~(BPF_F_UPROBE_MULTI_RETURN | BPF_F_UPROBE_MULTI_PATH_FD)) {
+ bpf_log(log, "Invalid uprobe_multi.flags.\n");
return -EINVAL;
+ }
/*
* offsets and cnt are mandatory,
@@ -3353,8 +3377,11 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
if (!uoffsets || !cnt || pid < 0)
return -EINVAL;
- if (cnt > MAX_UPROBE_MULTI_CNT)
+ if (cnt > MAX_UPROBE_MULTI_CNT) {
+ bpf_log(log, "uprobe_multi.cnt %u is larger than limit %u.\n",
+ cnt, MAX_UPROBE_MULTI_CNT);
return -E2BIG;
+ }
uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets);
ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies);
@@ -3365,15 +3392,25 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
* fails before anything is looked up or allocated.
*/
size = sizeof(*uoffsets) * cnt;
- if (!access_ok(uoffsets, size) || !access_ok(uref_ctr_offsets, size) ||
- !access_ok(ucookies, size))
+ if (!access_ok(uoffsets, size)) {
+ bpf_log(log, "offsets access check failed.\n");
return -EFAULT;
+ }
+ if (!access_ok(uref_ctr_offsets, size)) {
+ bpf_log(log, "ref_ctr_offsets access check failed.\n");
+ return -EFAULT;
+ }
+ if (!access_ok(ucookies, size)) {
+ bpf_log(log, "cookies access check failed.\n");
+ return -EFAULT;
+ }
- err = bpf_uprobe_multi_get_path(attr, &path);
+ err = bpf_uprobe_multi_get_path(attr, &path, log);
if (err)
return err;
if (!d_is_reg(path.dentry)) {
+ bpf_log(log, "Invalid path.\n");
err = -EBADF;
goto error_path_put;
}
@@ -3383,6 +3420,7 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
task = get_pid_task(find_vpid(pid), PIDTYPE_TGID);
rcu_read_unlock();
if (!task) {
+ bpf_log(log, "Invalid uprobe_multi.pid.\n");
err = -ESRCH;
goto error_path_put;
}
@@ -3398,18 +3436,22 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
for (i = 0; i < cnt; i++) {
if (get_user(uprobes[i].offset, uoffsets + i)) {
+ bpf_log(log, "Read offsets[%u] failed.\n", i);
err = -EFAULT;
goto error_free;
}
if (uprobes[i].offset < 0) {
+ bpf_log(log, "offsets[%u] must not be negative.\n", i);
err = -EINVAL;
goto error_free;
}
if (uref_ctr_offsets && get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) {
+ bpf_log(log, "Read ref_ctr_offsets[%u] failed.\n", i);
err = -EFAULT;
goto error_free;
}
if (ucookies && get_user(uprobes[i].cookie, ucookies + i)) {
+ bpf_log(log, "Read cookies[%u] failed.\n", i);
err = -EFAULT;
goto error_free;
}
@@ -3441,6 +3483,7 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
uprobes[i].ref_ctr_offset,
&uprobes[i].consumer);
if (IS_ERR(uprobes[i].uprobe)) {
+ bpf_log(log, "Register uprobes[%u] failed.\n", i);
err = PTR_ERR(uprobes[i].uprobe);
link->cnt = i;
goto error_unregister;
@@ -3466,8 +3509,10 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
return err;
}
#else /* !CONFIG_UPROBES */
-int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
+ bpf_log(log, "This kernel does not support uprobes.\n");
return -EOPNOTSUPP;
}
static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
--
2.55.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH bpf-next 4/4] selftests/bpf: Test perf event and multi-probe link creation logs
2026-12-02 15:37 [PATCH bpf-next 0/4] bpf: Add log support for perf link_create Leon Hwang
` (2 preceding siblings ...)
2026-12-02 15:37 ` [PATCH bpf-next 3/4] bpf: Add log support for uprobe_multi link_create Leon Hwang
@ 2026-12-02 15:37 ` Leon Hwang
3 siblings, 0 replies; 6+ messages in thread
From: Leon Hwang @ 2026-12-02 15:37 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, John Fastabend, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Ian Rogers, Adrian Hunter, James Clark,
KP Singh, Matt Bobrowski, Shuah Khan, Leon Hwang, linux-kernel,
linux-trace-kernel, linux-perf-users, linux-kselftest
Check the link creation log for a perf event program with an invalid
target fd and a tracepoint program targeting a software perf event.
Exercise kprobe multi attachment without symbols or addresses and
uprobe multi attachment without a path. Check the errno, diagnostic
text and reported log size for each failure.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
.../selftests/bpf/prog_tests/link_log.c | 46 ++++++++++++++++---
1 file changed, 39 insertions(+), 7 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/link_log.c b/tools/testing/selftests/bpf/prog_tests/link_log.c
index 8a05d6c48d9b..73a5e876ea2b 100644
--- a/tools/testing/selftests/bpf/prog_tests/link_log.c
+++ b/tools/testing/selftests/bpf/prog_tests/link_log.c
@@ -21,6 +21,15 @@ static const struct link_log_case {
{ .iter.len = sizeof(union bpf_iter_link_info) }, -EINVAL, "iter_info_len" },
{ "cgroup", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_EGRESS, NULL,
{ .cgroup.expected_revision = 2 }, -ESTALE, "revision" },
+ { "perf_event", BPF_PROG_TYPE_PERF_EVENT, BPF_PERF_EVENT, NULL,
+ {}, -EBADF, "Invalid target_fd" },
+ { "tracepoint", BPF_PROG_TYPE_TRACEPOINT, BPF_PERF_EVENT, NULL,
+ {}, -EINVAL, "Prog type must be BPF_PROG_TYPE_PERF_EVENT" },
+ { "kprobe_multi", BPF_PROG_TYPE_KPROBE, BPF_TRACE_KPROBE_MULTI, NULL,
+ { .kprobe_multi.cnt = 1 }, -EINVAL, "Exactly one of kprobe_multi.addrs" },
+ { "uprobe_multi", BPF_PROG_TYPE_KPROBE, BPF_TRACE_UPROBE_MULTI, NULL,
+ { .uprobe_multi = { .cnt = 1, .offsets = (const unsigned long[]){ 0 } } },
+ -EINVAL, "uprobe_multi.path must be set" },
};
static void run_test(const struct link_log_case *test)
@@ -30,7 +39,8 @@ static void run_test(const struct link_log_case *test)
BPF_EXIT_INSN(),
};
struct bpf_link_create_opts opts = test->opts;
- int prog_fd, fd, btf_id, cg = -1;
+ int prog_fd, fd, btf_id, target_fd;
+ bool close_target_fd = false;
LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
.expected_attach_type = test->attach_type,
);
@@ -53,15 +63,37 @@ static void run_test(const struct link_log_case *test)
if (!ASSERT_OK_FD(prog_fd, "bpf_prog_load"))
return;
- if (test->type == BPF_PROG_TYPE_CGROUP_SKB) {
- cg = test__join_cgroup("/link_log");
- if (!ASSERT_OK_FD(cg, "join_cgroup"))
+ switch (test->type) {
+ case BPF_PROG_TYPE_CGROUP_SKB:
+ target_fd = test__join_cgroup("/link_log");
+ if (!ASSERT_OK_FD(target_fd, "join_cgroup"))
goto out;
+ close_target_fd = true;
+ break;
+ case BPF_PROG_TYPE_TRACEPOINT: {
+ struct perf_event_attr attr = {
+ .type = PERF_TYPE_SOFTWARE,
+ .config = PERF_COUNT_SW_DUMMY,
+ .size = sizeof(attr),
+ };
+
+ target_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC);
+ if (!ASSERT_OK_FD(target_fd, "perf_event_open"))
+ goto out;
+ close_target_fd = true;
+ break;
+ }
+ case BPF_PROG_TYPE_PERF_EVENT:
+ target_fd = -1;
+ break;
+ default:
+ target_fd = 0;
+ break;
}
opts.sz = sizeof(opts);
opts.log_opts = &log_opts;
- fd = bpf_link_create(prog_fd, cg < 0 ? 0 : cg, test->attach_type, &opts);
+ fd = bpf_link_create(prog_fd, target_fd, test->attach_type, &opts);
if (fd >= 0)
close(fd);
@@ -69,8 +101,8 @@ static void run_test(const struct link_log_case *test)
ASSERT_HAS_SUBSTR(buf, test->msg, "log");
ASSERT_EQ(log_opts.true_size, strlen(buf) + 1, "true_size");
- if (cg >= 0)
- close(cg);
+ if (close_target_fd)
+ close(target_fd);
out:
close(prog_fd);
}
--
2.55.0
^ permalink raw reply [flat|nested] 6+ messages in thread