From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796225889; b=Aae+vjb2UtuhBPmuBC2Ugjt7X3ZjCrowg6Pci34h7fjsaAfOY/ooo9Rjt2ywQcfYYggo 9lUJbMnsh+QMxt4qj2pXyttCKFJ9tNNLIxyxTBdLs6/Cb9OkRSfP3/hSI2TaolQsqki5m EehcN8TVKb257dMl2CSivn4gwPScLNI1fO5eiKsqHVMKkM9maeyEsdKA54BaVSJAggK4t M/F9w67bXLpj/YdF5NzljhBLQP7w2K06WMylNZcM5C217+SoApaEVgTkOgMPrFFyy3648 vQ4uf3EjtdBKHzofZpKb71OeckVsJX/qpdStjI+FcctMPguIjT3D5nwMumBFqGGYhbg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796225889; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=9fTDnrfQyD48qKkBTmO7ekrIED2XOYA9deT0FMGsBdE=; b=ZD4xuE6+C0XZX9LAu8IqoFPikfH8NqaMdPpDI6FHYzYozt3lcVFstd+wLWEz55XjX2FZ Xlu4wUSpSeuuFqm6rLrdlZdSfGf+kv41OJ1vXPHD0D4IkBtyrTMXildLjUOSAF9hZzd/H QPCpeEqWT9uqajrs6YiKx8Pof7YMvOuj/axGmJeNaOcStcTMzuBYKoRaM5ByEqQbHPeNg sIHN7xN8b8RGyj9AuDZ6ASvvVQkIAfr50BPsJMD189TyV9OnZXoPqX9aWzIIQfiEyLC4N wCKjT0/Ts7mwPIOvZmtk6d52hdRyDCgfK1ubRnNrHC9CKq+vD2lblwBpW4ZGUMHK7DA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=T0zXgzk2; arc=none smtp.remote-ip=91.218.175.169 Received: from mta0.migadu.com (out-169.mta0.migadu.com [91.218.175.169]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7EC23606A32A for ; Wed, 2 Dec 2026 15:38:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7EC23606A32A 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, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=T0zXgzk2 X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=pm9aW1U91sz1fzwnDY93fXO4noAJ3PLebvlknkHqQhQ=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796225886; v=1; x=1796830686; b=T0zXgzk2JaOrbXqruY41AaBvRQUi+1QAaECmVh825A09deeoIReVfaZPO4YuLvNIGwv7i20h pK2uh/dPtCtyeJ/PnFdi4WMz7Deydl5Rgab6yjKdEGIP0J+zQ6zS6oM3Y5iW5tv6EhjNCgtrEo3 ohMTc7BUZbmr8YNWZA+voNVU= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id ab40133c8dea30e6; Wed, 02 Dec 2026 15:38:06 +0000 X-Mizu-Trace-ID: ab40133c8dea30e6 X-Migadu-Flow: FLOW_OUT From: Leon Hwang To: bpf@vger.kernel.org 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@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 Message-ID: <20261202153739.57959-2-leon.hwang@linux.dev> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261202153739.57959-1-leon.hwang@linux.dev> References: <20261202153739.57959-1-leon.hwang@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 #include #include +#include #include #include #include @@ -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