Linux Trace Kernel
 help / color / mirror / Atom feed
From: Leon Hwang <leon.hwang@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	KP Singh <kpsingh@kernel.org>,
	Matt Bobrowski <matt@bobrowski.net>,
	Shuah Khan <shuah@kernel.org>, Leon Hwang <leon.hwang@linux.dev>,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	linux-perf-users@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [PATCH bpf-next 1/4] bpf: Add log support for perf link_create
Date: Wed,  2 Dec 2026 23:37:36 +0800	[thread overview]
Message-ID: <20261202153739.57959-2-leon.hwang@linux.dev> (raw)
In-Reply-To: <20261202153739.57959-1-leon.hwang@linux.dev>

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


  reply	other threads:[~2026-12-02 15:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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 ` [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

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=20261202153739.57959-2-leon.hwang@linux.dev \
    --to=leon.hwang@linux.dev \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=martin.lau@linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=matt@bobrowski.net \
    --cc=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox