Skip to contentSkip to conversation

bpf: Add log support for perf link_create

PATCH In review Leon Hwang 4 participants 9 messages Dec 2 – Dec 3

Subsystems (8)
Kselftest BPF perf events Tracing +4 more subsystems
lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

cover bpf: Add log support for perf link_create

I'm planning to add syscall common attrs support for LINK_CREATE cmd.

This is the third series for the plan, that adds syscall common attrs support for BPF_LINK_CREATE of perf_event, tracepoint, and kprobe progs.

Leon Hwang (4):
bpf: Add log support for perf link_create
bpf: Add log support for kprobe_multi link_create
bpf: Add log support for uprobe_multi link_create selftests/bpf: Test perf event and multi-probe link creation logs

 include/linux/bpf_verifier.h                  |   7 +
 include/linux/trace_events.h                  |  21 ++-
 kernel/bpf/syscall.c                          |  24 ++-
 kernel/events/core.c                          |  79 ++++++---
 kernel/trace/bpf_trace.c                      | 158 ++++++++++++++----
 .../selftests/bpf/prog_tests/link_log.c       |  46 ++++-
 6 files changed, 249 insertions(+), 86 deletions(-)

--
2.55.0

author
#1 lore raw

PATCH 1/4 bpf: Add log support for perf link_create

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.

---
 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(-)
include/linux/bpf_verifier.h +7 -0
index 50b3f8ab9720..a27f66a25618 100644
@@ -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);
include/linux/trace_events.h +6 -3
index dff8a209875c..0fe900813581 100644
@@ -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);
kernel/bpf/syscall.c +13 -7
index 801175b809e7..2cd1846e5c76 100644
@@ -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);
kernel/events/core.c +53 -26

180 lines not on this page · raw message

kernel/trace/bpf_trace.c +12 -3
index 2031c3727320..44ea62054f81 100644
@@ -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;
 
Signature · 1 line
2.55.0
#2 lore raw
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…
Quoted 1 line

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.

Quoted 3 lines
kernel/trace/bpf_trace.c +0 -0

[ ... ]

Quoted 11 lines
kernel/trace/bpf_trace.c +3 -1
 	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) {

kernel/trace/bpf_trace.c int perf_event_attach_bpf_prog(struct perf_event *event, · line 2010

Quoted 10 lines
kernel/trace/bpf_trace.c +3 -0
+		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

#3 lore raw
In reply to Leon Hwang
Quoted 44 lines · Leon Hwang

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.

---
 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(-)
include/linux/bpf_verifier.h +7 -0
index 50b3f8ab9720..a27f66a25618 100644
@@ -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);
include/linux/trace_events.h +0 -0
index dff8a209875c..0fe900813581 100644

include/linux/trace_events.h trace_trigger_soft_disabled(struct trace_event_file *file) · line 785

Quoted 6 lines
include/linux/trace_events.h +2 -1
@@ -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);

This is not landable.
Don't you remember how netdev folks objected to such thing? Same story here.

pw-bot: cr

author
#4 lore raw
Quoted 1 line · Alexei Starovoitov

On Wed Dec 2, 2026 at 3:37 PM UTC, Leon Hwang wrote:

[...]

Quoted 7 lines
Quoted 7 lines
include/linux/trace_events.h +0 -0
index dff8a209875c..0fe900813581 100644
@@ -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);
Quoted 9 lines

include/linux/trace_events.h trace_trigger_soft_disabled(struct trace_event_file *file) · line 785

Quoted 4 lines
include/linux/trace_events.h +2 -1
 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);

This is not landable.
Don't you remember how netdev folks objected to such thing? Same story here.

Got it.

Perf has no equivalent of netlink extack. I'd like to pass a 'const char **errmsg' to the perf attach helper, so it can return a static error msg. On failure, BPF_LINK_CREATE can write the msg to the log buffer if errmsg was set.

Thanks,
Leon

author
#5 lore raw

PATCH 2/4 bpf: Add log support for kprobe_multi link_create

In reply to Leon Hwang

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.

---
 include/linux/trace_events.h |  6 ++--
 kernel/bpf/syscall.c         |  2 +-
 kernel/trace/bpf_trace.c     | 66 ++++++++++++++++++++++++++----------
 3 files changed, 53 insertions(+), 21 deletions(-)
include/linux/trace_events.h +4 -2
index 0fe900813581..e855ca8a10f1 100644
@@ -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;
 }
kernel/bpf/syscall.c +1 -1
index 2cd1846e5c76..25f4a0c9c9c1 100644
@@ -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);
kernel/trace/bpf_trace.c +48 -18

155 lines not on this page · raw message

Signature · 1 line
2.55.0
author
#6 lore raw

PATCH 3/4 bpf: Add log support for uprobe_multi link_create

In reply to Leon Hwang

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.

---
 include/linux/trace_events.h |  6 ++-
 kernel/bpf/syscall.c         |  2 +-
 kernel/trace/bpf_trace.c     | 77 ++++++++++++++++++++++++++++--------
 3 files changed, 66 insertions(+), 19 deletions(-)
include/linux/trace_events.h +4 -2
index e855ca8a10f1..6e46a2a75e4c 100644
@@ -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;
 }
kernel/bpf/syscall.c +1 -1
index 25f4a0c9c9c1..152044e5fb62 100644
@@ -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");
kernel/trace/bpf_trace.c +61 -16

177 lines not on this page · raw message

Signature · 1 line
2.55.0
author
#7 lore raw

PATCH 4/4 selftests/bpf: Test perf event and multi-probe link creation logs

In reply to Leon Hwang

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.

---
 .../selftests/bpf/prog_tests/link_log.c       | 46 ++++++++++++++++---
 1 file changed, 39 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/link_log.c +39 -7
index 8a05d6c48d9b..73a5e876ea2b 100644
@@ -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);
 }
Signature · 1 line
2.55.0