* [PATCH bpf-next v3 1/9] bpf: Add log support for tracing link_create
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 2/9] bpf: Add log support for tracing_multi link_create Leon Hwang
` (7 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
Tracing BPF_LINK_CREATE returns errors for invalid attach parameters
without explaining which check failed.
Report parameter and reattachment errors through the link creation log.
Pass the log to bpf_check_attach_target() so freplace attachment to a new
target also reports target-check failures.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
kernel/bpf/syscall.c | 22 +++++++++++++++++-----
kernel/bpf/verifier.c | 6 ++++--
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ec8e78792911..80504011403f 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3677,7 +3677,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
int tgt_prog_fd,
u32 btf_id,
u64 bpf_cookie,
- enum bpf_attach_type attach_type)
+ enum bpf_attach_type attach_type,
+ struct bpf_verifier_log *log)
{
struct bpf_link_primer link_primer;
struct bpf_prog *tgt_prog = NULL;
@@ -3714,6 +3715,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
}
if (!!tgt_prog_fd != !!btf_id) {
+ bpf_log(log, "target_fd and target_btf_id must be both set or both unset.\n");
err = -EINVAL;
goto out_put_prog;
}
@@ -3726,12 +3728,14 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
* attach_tracing_prog flag is set.
*/
if (prog->type != BPF_PROG_TYPE_EXT) {
+ bpf_log(log, "target_fd is only allowed for freplace prog.\n");
err = -EINVAL;
goto out_put_prog;
}
tgt_prog = bpf_prog_get(tgt_prog_fd);
if (IS_ERR(tgt_prog)) {
+ bpf_log(log, "Invalid tgt_fd.\n");
err = PTR_ERR(tgt_prog);
tgt_prog = NULL;
goto out_put_prog;
@@ -3786,11 +3790,14 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
*/
if (prog->type != BPF_PROG_TYPE_TRACING &&
prog->type != BPF_PROG_TYPE_LSM) {
+ bpf_log(log,
+ "Re-attach of freplace prog needs target_fd and target_btf_id.\n");
err = -EINVAL;
goto out_unlock;
}
/* We can allow re-attach only if we have valid attach_btf. */
if (!prog->aux->attach_btf) {
+ bpf_log(log, "Cannot re-attach a prog that was attached to a bpf prog.\n");
err = -EINVAL;
goto out_unlock;
}
@@ -3806,7 +3813,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
*/
struct bpf_attach_target_info tgt_info = {};
- err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
+ err = bpf_check_attach_target(log, prog, tgt_prog, btf_id,
&tgt_info);
if (err)
goto out_unlock;
@@ -3846,6 +3853,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
*/
if (prog->type == BPF_PROG_TYPE_EXT &&
prog->aux->kprobe_write_ctx != tgt_prog->aux->kprobe_write_ctx) {
+ bpf_log(log, "Only one of freplace prog and its target writes to pt_regs.\n");
err = -EINVAL;
goto out_unlock;
}
@@ -3859,6 +3867,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
tgt_prog->type == BPF_PROG_TYPE_TRACEPOINT ||
tgt_prog->type == BPF_PROG_TYPE_SYSCALL) &&
prog->aux->max_ctx_offset > tgt_prog->aux->max_ctx_offset) {
+ bpf_log(log, "Freplace prog accesses more ctx than its target.\n");
err = -EINVAL;
goto out_unlock;
}
@@ -4383,7 +4392,7 @@ static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
tp_name = prog->aux->attach_func_name;
break;
}
- return bpf_tracing_prog_attach(prog, 0, 0, 0, attach_type);
+ return bpf_tracing_prog_attach(prog, 0, 0, 0, attach_type, NULL);
case BPF_PROG_TYPE_RAW_TRACEPOINT:
case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
@@ -5927,11 +5936,13 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
attr->link_create.target_fd,
attr->link_create.target_btf_id,
attr->link_create.tracing.cookie,
- attr->link_create.attach_type);
+ attr->link_create.attach_type,
+ log);
break;
case BPF_PROG_TYPE_LSM:
case BPF_PROG_TYPE_TRACING:
if (attr->link_create.attach_type != prog->expected_attach_type) {
+ bpf_log(log, "attach_type is not expected_attach_type of the prog.\n");
ret = -EINVAL;
goto out;
}
@@ -5949,7 +5960,8 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
attr->link_create.target_fd,
attr->link_create.target_btf_id,
attr->link_create.tracing.cookie,
- attr->link_create.attach_type);
+ attr->link_create.attach_type,
+ log);
break;
case BPF_PROG_TYPE_FLOW_DISSECTOR:
case BPF_PROG_TYPE_SK_LOOKUP:
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index dea18409f480..58d3588ffc84 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -24345,7 +24345,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
if (bpf_prog_is_dev_bound(prog->aux) &&
!bpf_prog_dev_bound_match(prog, tgt_prog)) {
- bpf_log(log, "Target program bound device mismatch");
+ bpf_log(log, "Target program bound device mismatch\n");
return -EINVAL;
}
@@ -24605,8 +24605,10 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) &&
(!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type ||
- prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type))
+ prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) {
+ bpf_log(log, "Target differs in type from the target at load time\n");
return -EINVAL;
+ }
if (tgt_prog && conservative) {
/*
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH bpf-next v3 2/9] bpf: Add log support for tracing_multi link_create
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 1/9] " Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 3/9] bpf: Add log support for iter link_create Leon Hwang
` (6 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
tracing_multi BPF_LINK_CREATE returns errors without identifying the
failed parameter or target check.
Report excessive counts, invalid flags and target FDs, and duplicate IDs
through the link creation log. Also report BTF target validation errors,
including unsupported prototypes, missing ftrace locations and disallowed
sleepable attachment.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
include/linux/bpf.h | 6 +++--
include/linux/bpf_verifier.h | 3 ++-
include/linux/trace_events.h | 7 ++++--
kernel/bpf/syscall.c | 2 +-
kernel/bpf/trampoline.c | 5 ++--
kernel/bpf/verifier.c | 45 ++++++++++++++++++++++++++++--------
kernel/trace/bpf_trace.c | 27 ++++++++++++++++------
7 files changed, 70 insertions(+), 25 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4e9a4d15af0e..a1ba3f54ba6a 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1640,7 +1640,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr);
int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,
- struct bpf_tracing_multi_link *link);
+ struct bpf_tracing_multi_link *link,
+ struct bpf_verifier_log *log);
void bpf_trampoline_multi_detach(struct bpf_prog *prog,
struct bpf_tracing_multi_link *link);
void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags);
@@ -1758,7 +1759,8 @@ static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
return false;
}
static inline int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,
- struct bpf_tracing_multi_link *link)
+ struct bpf_tracing_multi_link *link,
+ struct bpf_verifier_log *log)
{
return -ENOTSUPP;
}
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 151b5781606d..f86b95da1b1e 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -2492,7 +2492,8 @@ int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
struct bpf_insn *insn_buf, int insn_idx, int *cnt);
/* Functions exported from verifier.c, used by trampoline.c */
-int bpf_check_attach_btf_id_multi(struct btf *btf, struct bpf_prog *prog, u32 btf_id,
+int bpf_check_attach_btf_id_multi(struct bpf_verifier_log *log, struct btf *btf,
+ struct bpf_prog *prog, u32 btf_id,
struct bpf_attach_target_info *tgt_info);
/* Functions in fixups.c, called from bpf_check() */
diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
index 321f2f3a6c8e..bb825ea5d09c 100644
--- a/include/linux/trace_events.h
+++ b/include/linux/trace_events.h
@@ -16,6 +16,7 @@ struct tracer;
struct dentry;
struct bpf_prog;
union bpf_attr;
+struct bpf_verifier_log;
/* Used for event string fields when they are NULL */
#define EVENT_NULL_STR "(null)"
@@ -796,7 +797,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);
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);
+int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
+ struct bpf_verifier_log *log);
#else
static inline unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
{
@@ -855,7 +857,8 @@ bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
return -EOPNOTSUPP;
}
static inline int
-bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr)
+bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
+ struct bpf_verifier_log *log)
{
return -EOPNOTSUPP;
}
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 80504011403f..d3ab9c695129 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -5954,7 +5954,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
else if (prog->expected_attach_type == BPF_LSM_CGROUP)
ret = cgroup_bpf_link_attach(attr, prog);
else if (is_tracing_multi(prog->expected_attach_type))
- ret = bpf_tracing_multi_attach(prog, attr);
+ ret = bpf_tracing_multi_attach(prog, attr, log);
else
ret = bpf_tracing_prog_attach(prog,
attr->link_create.target_fd,
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 5f2b792de8f9..96454443441b 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -1723,7 +1723,8 @@ static void bpf_trampoline_multi_attach_rollback(struct bpf_trampoline *tr)
for_each_mnode_cnt(mnode, link, link->nodes_cnt)
int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,
- struct bpf_tracing_multi_link *link)
+ struct bpf_tracing_multi_link *link,
+ struct bpf_verifier_log *log)
{
struct bpf_tracing_multi_data *data = &link->data;
struct bpf_attach_target_info tgt_info = {};
@@ -1736,7 +1737,7 @@ int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,
for_each_mnode(mnode, link) {
rollback_cnt = i;
- err = bpf_check_attach_btf_id_multi(btf, prog, ids[i], &tgt_info);
+ err = bpf_check_attach_btf_id_multi(log, btf, prog, ids[i], &tgt_info);
if (err)
goto rollback_put;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 58d3588ffc84..4927d3674021 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -24150,8 +24150,12 @@ btf_attach_func_proto(struct bpf_verifier_log *log, struct btf *btf, u32 func_id
int implicit;
func = btf_type_by_id(btf, func_id);
- if (!func || !btf_type_is_func(func))
+ if (!func)
+ return NULL;
+ if (!btf_type_is_func(func)) {
+ bpf_log(log, "Type of id %u is not function.\n", func_id);
return NULL;
+ }
name = btf_name_by_offset(btf, func->name_off);
/*
@@ -24906,7 +24910,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
return 0;
}
-int bpf_check_attach_btf_id_multi(struct btf *btf, struct bpf_prog *prog, u32 btf_id,
+int bpf_check_attach_btf_id_multi(struct bpf_verifier_log *log, struct btf *btf,
+ struct bpf_prog *prog, u32 btf_id,
struct bpf_attach_target_info *tgt_info)
{
const struct btf_type *t;
@@ -24920,28 +24925,40 @@ int bpf_check_attach_btf_id_multi(struct btf *btf, struct bpf_prog *prog, u32 bt
/* Check noreturn attachment. */
if ((prog->expected_attach_type == BPF_TRACE_FEXIT_MULTI ||
prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI) &&
- btf_id_set_contains(&noreturn_deny, btf_id))
+ btf_id_set_contains(&noreturn_deny, btf_id)) {
+ bpf_log(log,
+ "Cannot attach fexit/fsession_multi prog to noreturn btf id %u.\n",
+ btf_id);
return -EINVAL;
+ }
/* Check denied attachment. */
- if (btf_id_set_contains(&btf_id_deny, btf_id))
+ if (btf_id_set_contains(&btf_id_deny, btf_id)) {
+ bpf_log(log, "Cannot attach to denylist btf id %u.\n", btf_id);
return -EINVAL;
+ }
/* Check and get function target data. */
t = btf_type_by_id(btf, btf_id);
- if (!t)
+ if (!t) {
+ bpf_log(log, "Cannot resolve type of btf id %u.\n", btf_id);
return -EINVAL;
+ }
tname = btf_name_by_offset(btf, t->name_off);
if (!tname)
return -EINVAL;
- t = btf_attach_func_proto(NULL, btf, btf_id);
+ t = btf_attach_func_proto(log, btf, btf_id);
if (!t || !btf_type_is_func_proto(t))
return -EINVAL;
- err = btf_distill_func_proto(NULL, btf, t, tname, &tgt_info->fmodel);
+ err = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel);
if (err < 0)
return err;
if (tgt_info->fmodel.ret_size > 8 &&
- attach_uses_trampoline_retval(prog->expected_attach_type))
+ attach_uses_trampoline_retval(prog->expected_attach_type)) {
+ bpf_log(log,
+ "Attach to target %u:%s with a >8 byte return value is not supported for this attach type.\n",
+ btf_id, tname);
return -EOPNOTSUPP;
+ }
if (btf_is_module(btf)) {
/* The bpf program already holds reference to module. */
if (WARN_ON_ONCE(!prog->aux->mod))
@@ -24950,14 +24967,22 @@ int bpf_check_attach_btf_id_multi(struct btf *btf, struct bpf_prog *prog, u32 bt
} else {
addr = kallsyms_lookup_name(tname);
}
- if (!addr || !ftrace_location(addr))
+ if (!addr) {
+ bpf_log(log, "Cannot resolve address of target %u:%s.\n", btf_id, tname);
+ return -ENOENT;
+ }
+ if (!ftrace_location(addr)) {
+ bpf_log(log, "Target %u:%s is not ftrace attachable.\n", btf_id, tname);
return -ENOENT;
+ }
/* Check sleepable program attachment. */
if (prog->sleepable) {
err = btf_id_allow_sleepable(btf_id, addr, prog, btf);
- if (err)
+ if (err) {
+ bpf_log(log, "Target %u:%s is not sleepable.\n", btf_id, tname);
return err;
+ }
}
tgt_info->tgt_addr = addr;
return 0;
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 4f1be6cd2e13..e5dd5c4dbf44 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -3898,7 +3898,7 @@ static void ids_swap_r(void *a, void *b, int size __maybe_unused,
swap(*id_a, *id_b);
}
-static int check_dup_ids(u32 *ids, u64 *cookies, u32 cnt)
+static int check_dup_ids(u32 *ids, u64 *cookies, u32 cnt, struct bpf_verifier_log *log)
{
void *data[2] = { ids, cookies };
int err = 0;
@@ -3912,6 +3912,7 @@ static int check_dup_ids(u32 *ids, u64 *cookies, u32 cnt)
for (int i = 1; i < cnt; i++) {
if (ids[i] == ids[i - 1]) {
+ bpf_log(log, "id %u is in tracing_multi.ids more than once.\n", ids[i]);
err = -EINVAL;
break;
}
@@ -3919,7 +3920,8 @@ static int check_dup_ids(u32 *ids, u64 *cookies, u32 cnt)
return err;
}
-int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr)
+int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
+ struct bpf_verifier_log *log)
{
struct bpf_tracing_multi_link *link = NULL;
struct bpf_tramp_node *fexits = NULL;
@@ -3935,10 +3937,19 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr)
if (!cnt || !uids)
return -EINVAL;
- if (cnt > MAX_TRACING_MULTI_CNT)
+ if (cnt > MAX_TRACING_MULTI_CNT) {
+ bpf_log(log, "tracing_multi.cnt %u is above the limit %u.\n",
+ cnt, MAX_TRACING_MULTI_CNT);
return -E2BIG;
- if (attr->link_create.flags || attr->link_create.target_fd)
+ }
+ if (attr->link_create.flags) {
+ bpf_log(log, "flags must be zero.\n");
+ return -EINVAL;
+ }
+ if (attr->link_create.target_fd) {
+ bpf_log(log, "target_fd must be zero.\n");
return -EINVAL;
+ }
ids = kvmalloc_objs(*ids, cnt);
if (!ids)
@@ -3962,7 +3973,7 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr)
}
}
- err = check_dup_ids(ids, cookies, cnt);
+ err = check_dup_ids(ids, cookies, cnt, log);
if (err)
goto error;
@@ -3991,7 +4002,7 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr)
link->cookies = cookies;
link->fexits = fexits;
- err = bpf_trampoline_multi_attach(prog, ids, link);
+ err = bpf_trampoline_multi_attach(prog, ids, link, log);
kvfree(ids);
if (err) {
bpf_link_cleanup(&link_primer);
@@ -4009,8 +4020,10 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr)
#else
-int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr)
+int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
+ struct bpf_verifier_log *log)
{
+ bpf_log(log, "This kernel does not support tracing_multi.\n");
return -EOPNOTSUPP;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH bpf-next v3 3/9] bpf: Add log support for iter link_create
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 1/9] " Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 2/9] bpf: Add log support for tracing_multi link_create Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 4/9] bpf: Add log support for cgroup prog link_create Leon Hwang
` (5 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
iter BPF_LINK_CREATE returns an error without explaining invalid target
FDs, flags, or iterator information, or an unsupported program.
Pass the BPF_LINK_CREATE log to the iter attach path and report these
failures.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
include/linux/bpf.h | 3 ++-
kernel/bpf/bpf_iter.c | 27 +++++++++++++++++++++------
kernel/bpf/syscall.c | 2 +-
3 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index a1ba3f54ba6a..e6d6a34a2818 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3272,7 +3272,8 @@ void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
int bpf_iter_prog_supported(struct bpf_prog *prog);
const struct bpf_func_proto *
bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
-int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
+int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log);
int bpf_iter_new_fd(struct bpf_link *link);
bool bpf_link_is_iter(struct bpf_link *link);
struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
diff --git a/kernel/bpf/bpf_iter.c b/kernel/bpf/bpf_iter.c
index f0b29a170935..4a9e08fb8e75 100644
--- a/kernel/bpf/bpf_iter.c
+++ b/kernel/bpf/bpf_iter.c
@@ -5,6 +5,7 @@
#include <linux/anon_inodes.h>
#include <linux/filter.h>
#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
#include <linux/rcupdate_trace.h>
struct bpf_iter_target_info {
@@ -528,7 +529,7 @@ bool bpf_link_is_iter(struct bpf_link *link)
}
int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
- struct bpf_prog *prog)
+ struct bpf_prog *prog, struct bpf_verifier_log *log)
{
struct bpf_iter_target_info *tinfo = NULL, *iter;
struct bpf_link_primer link_primer;
@@ -538,24 +539,36 @@ int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
bpfptr_t ulinfo;
int err;
- if (attr->link_create.target_fd || attr->link_create.flags)
+ if (attr->link_create.target_fd) {
+ bpf_log(log, "Invalid target_fd.\n");
+ return -EINVAL;
+ }
+ if (attr->link_create.flags) {
+ bpf_log(log, "Invalid flags.\n");
return -EINVAL;
+ }
memset(&linfo, 0, sizeof(union bpf_iter_link_info));
ulinfo = make_bpfptr(attr->link_create.iter_info, uattr.is_kernel);
linfo_len = attr->link_create.iter_info_len;
- if (bpfptr_is_null(ulinfo) ^ !linfo_len)
+ if (bpfptr_is_null(ulinfo) ^ !linfo_len) {
+ bpf_log(log, "iter_info and iter_info_len must be both set or both unset.\n");
return -EINVAL;
+ }
if (!bpfptr_is_null(ulinfo)) {
err = bpf_check_uarg_tail_zero(ulinfo, sizeof(linfo),
linfo_len);
- if (err)
+ if (err) {
+ bpf_log(log, "Invalid iter_info_len.\n");
return err;
+ }
linfo_len = min_t(u32, linfo_len, sizeof(linfo));
- if (copy_from_bpfptr(&linfo, ulinfo, linfo_len))
+ if (copy_from_bpfptr(&linfo, ulinfo, linfo_len)) {
+ bpf_log(log, "Invalid iter_info.\n");
return -EFAULT;
+ }
}
prog_btf_id = prog->aux->attach_btf_id;
@@ -570,8 +583,10 @@ int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
if (!tinfo)
return -ENOENT;
- if (!bpf_iter_target_support_prog(tinfo, prog))
+ if (!bpf_iter_target_support_prog(tinfo, prog)) {
+ bpf_log(log, "The iter target does not support the bpf prog.\n");
return -EINVAL;
+ }
link = kzalloc_obj(*link, GFP_USER | __GFP_NOWARN);
if (!link)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index d3ab9c695129..f485e68a14ee 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -5950,7 +5950,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie,
attr->link_create.attach_type);
else if (prog->expected_attach_type == BPF_TRACE_ITER)
- ret = bpf_iter_link_attach(attr, uattr, prog);
+ ret = bpf_iter_link_attach(attr, uattr, prog, log);
else if (prog->expected_attach_type == BPF_LSM_CGROUP)
ret = cgroup_bpf_link_attach(attr, prog);
else if (is_tracing_multi(prog->expected_attach_type))
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH bpf-next v3 4/9] bpf: Add log support for cgroup prog link_create
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
` (2 preceding siblings ...)
2026-11-23 15:27 ` [PATCH bpf-next v3 3/9] bpf: Add log support for iter link_create Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
2026-11-23 16:04 ` bot+bpf-ci
2026-11-23 15:27 ` [PATCH bpf-next v3 5/9] libbpf: Add log_opts support for BPF_TRACE_ITER Leon Hwang
` (4 subsequent siblings)
8 siblings, 1 reply; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
Cgroup prog BPF_LINK_CREATE returns errors without explaining invalid
flags, target FDs, or relative attachment parameters. Revision mismatches,
cgroup hierarchy restrictions, conflicting attachment flags, and the
per-cgroup program limit can also cause unexplained failures.
Pass the BPF_LINK_CREATE log through the cgroup attachment helpers and
report these failures at the checks that reject the attachment. Diagnose
relative link attachments that omit BPF_F_LINK.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
include/linux/bpf-cgroup.h | 6 ++-
kernel/bpf/cgroup.c | 76 +++++++++++++++++++++++++++-----------
kernel/bpf/syscall.c | 4 +-
3 files changed, 60 insertions(+), 26 deletions(-)
diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
index dfa7a75507c9..0b63c2cc1399 100644
--- a/include/linux/bpf-cgroup.h
+++ b/include/linux/bpf-cgroup.h
@@ -428,7 +428,8 @@ int cgroup_bpf_prog_attach(const union bpf_attr *attr,
enum bpf_prog_type ptype, struct bpf_prog *prog);
int cgroup_bpf_prog_detach(const union bpf_attr *attr,
enum bpf_prog_type ptype);
-int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
+int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log);
int cgroup_bpf_prog_query(const union bpf_attr *attr,
union bpf_attr __user *uattr, u32 uattr_size);
@@ -455,7 +456,8 @@ static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
}
static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
- struct bpf_prog *prog)
+ struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
return -EINVAL;
}
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index f195d5fcae6c..4dff4a178af3 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -901,7 +901,8 @@ static struct bpf_prog *bpf_get_anchor_prog(u32 flags, u32 id_or_fd)
}
static struct bpf_prog_list *get_prog_list(struct hlist_head *progs, struct bpf_prog *prog,
- struct bpf_cgroup_link *link, u32 flags, u32 id_or_fd)
+ struct bpf_cgroup_link *link, u32 flags, u32 id_or_fd,
+ struct bpf_verifier_log *log)
{
bool is_link = flags & BPF_F_LINK, is_id = flags & BPF_F_ID;
struct bpf_prog_list *pltmp, *pl = ERR_PTR(-EINVAL);
@@ -914,20 +915,30 @@ static struct bpf_prog_list *get_prog_list(struct hlist_head *progs, struct bpf_
is_after = flags & BPF_F_AFTER;
if (is_link || is_id || id_or_fd) {
/* flags must have either BPF_F_BEFORE or BPF_F_AFTER */
- if (is_before == is_after)
+ if (is_before == is_after) {
+ bpf_log(log, "Specify exactly one of BPF_F_BEFORE or BPF_F_AFTER.\n");
return ERR_PTR(-EINVAL);
- if ((is_link && !link) || (!is_link && !prog))
+ }
+ if (is_link && !link)
+ return ERR_PTR(-EINVAL);
+ if (!is_link && !prog) {
+ bpf_log(log, "BPF_F_LINK is required for relative link attachment.\n");
return ERR_PTR(-EINVAL);
+ }
} else if (!hlist_empty(progs)) {
/* flags cannot have both BPF_F_BEFORE and BPF_F_AFTER */
- if (is_before && is_after)
+ if (is_before && is_after) {
+ bpf_log(log, "BPF_F_BEFORE and BPF_F_AFTER are mutually exclusive.\n");
return ERR_PTR(-EINVAL);
+ }
}
if (is_link) {
anchor_link = bpf_get_anchor_link(flags, id_or_fd);
- if (IS_ERR(anchor_link))
+ if (IS_ERR(anchor_link)) {
+ bpf_log(log, "Invalid relative link %s.\n", is_id ? "ID" : "FD");
return ERR_CAST(anchor_link);
+ }
} else if (is_id || id_or_fd) {
anchor_prog = bpf_get_anchor_prog(flags, id_or_fd);
if (IS_ERR(anchor_prog))
@@ -952,13 +963,16 @@ static struct bpf_prog_list *get_prog_list(struct hlist_head *progs, struct bpf_
hlist_for_each_entry(pltmp, progs, node) {
if ((anchor_prog && anchor_prog == pltmp->prog) ||
(anchor_link && anchor_link == &pltmp->link->link)) {
- if (!!(pltmp->flags & BPF_F_PREORDER) != preorder)
+ if (!!(pltmp->flags & BPF_F_PREORDER) != preorder) {
+ bpf_log(log, "Relative attachment BPF_F_PREORDER mismatch.\n");
goto out;
+ }
pl = pltmp;
goto out;
}
}
+ bpf_log(log, "Relative attachment not found in target cgroup.\n");
pl = ERR_PTR(-ENOENT);
out:
if (anchor_link)
@@ -970,11 +984,11 @@ static struct bpf_prog_list *get_prog_list(struct hlist_head *progs, struct bpf_
static int insert_pl_to_hlist(struct bpf_prog_list *pl, struct hlist_head *progs,
struct bpf_prog *prog, struct bpf_cgroup_link *link,
- u32 flags, u32 id_or_fd)
+ u32 flags, u32 id_or_fd, struct bpf_verifier_log *log)
{
struct bpf_prog_list *pltmp;
- pltmp = get_prog_list(progs, prog, link, flags, id_or_fd);
+ pltmp = get_prog_list(progs, prog, link, flags, id_or_fd, log);
if (IS_ERR(pltmp))
return PTR_ERR(pltmp);
@@ -999,6 +1013,7 @@ static int insert_pl_to_hlist(struct bpf_prog_list *pl, struct hlist_head *progs
* @flags: Option flags
* @id_or_fd: Relative prog id or fd
* @revision: bpf_prog_list revision
+ * @log: Optional log for attachment errors
*
* Exactly one of @prog or @link can be non-null.
* Must be called with cgroup_mutex and cgroup_bpf_mutex held.
@@ -1007,7 +1022,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,
struct bpf_prog *prog, struct bpf_prog *replace_prog,
struct bpf_cgroup_link *link,
enum bpf_attach_type type, u32 flags, u32 id_or_fd,
- u64 revision)
+ u64 revision, struct bpf_verifier_log *log)
{
u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI));
struct bpf_prog *old_prog = NULL;
@@ -1045,23 +1060,33 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,
atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id);
if (atype < 0)
return -EINVAL;
- if (revision && revision != cgrp->bpf.revisions[atype])
+ if (revision && revision != cgrp->bpf.revisions[atype]) {
+ bpf_log(log, "Cgroup revision mismatch: expected %llu, actual %llu.\n",
+ revision, cgrp->bpf.revisions[atype]);
return -ESTALE;
+ }
progs = &cgrp->bpf.progs[atype];
- if (!hierarchy_allows_attach(cgrp, atype))
+ if (!hierarchy_allows_attach(cgrp, atype)) {
+ bpf_log(log, "Cgroup hierarchy does not allow attachment.\n");
return -EPERM;
+ }
- if (!hlist_empty(progs) && cgrp->bpf.flags[atype] != saved_flags)
+ if (!hlist_empty(progs) && cgrp->bpf.flags[atype] != saved_flags) {
/* Disallow attaching non-overridable on top
* of existing overridable in this cgroup.
* Disallow attaching multi-prog if overridable or none
*/
+ bpf_log(log, "Attachment flags conflict with existing flags.\n");
return -EPERM;
+ }
- if (prog_list_length(progs, NULL) >= BPF_CGROUP_MAX_PROGS)
+ if (prog_list_length(progs, NULL) >= BPF_CGROUP_MAX_PROGS) {
+ bpf_log(log, "Too many programs attached to cgroup (limit %u).\n",
+ BPF_CGROUP_MAX_PROGS);
return -E2BIG;
+ }
pl = find_attach_entry(progs, prog, link, replace_prog,
flags & BPF_F_ALLOW_MULTI);
@@ -1083,7 +1108,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,
return -ENOMEM;
}
- err = insert_pl_to_hlist(pl, progs, prog, link, flags, id_or_fd);
+ err = insert_pl_to_hlist(pl, progs, prog, link, flags, id_or_fd, log);
if (err) {
kfree(pl);
bpf_cgroup_storages_free(new_storage);
@@ -1143,13 +1168,14 @@ static int cgroup_bpf_attach(struct cgroup *cgrp,
struct bpf_prog *prog, struct bpf_prog *replace_prog,
struct bpf_cgroup_link *link,
enum bpf_attach_type type,
- u32 flags, u32 id_or_fd, u64 revision)
+ u32 flags, u32 id_or_fd, u64 revision,
+ struct bpf_verifier_log *log)
{
int ret;
cgroup_bpf_lock_hierarchy();
ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, link, type, flags,
- id_or_fd, revision);
+ id_or_fd, revision, log);
cgroup_bpf_unlock_hierarchy();
return ret;
}
@@ -1635,7 +1661,7 @@ int cgroup_bpf_prog_attach(const union bpf_attr *attr,
ret = cgroup_bpf_attach(cgrp, prog, replace_prog, NULL,
attr->attach_type, attr->attach_flags,
- attr->relative_fd, attr->expected_revision);
+ attr->relative_fd, attr->expected_revision, NULL);
if (replace_prog)
bpf_prog_put(replace_prog);
@@ -1765,19 +1791,24 @@ static const struct bpf_link_ops bpf_cgroup_link_lops = {
BPF_F_PREORDER | \
BPF_F_LINK)
-int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+ struct bpf_verifier_log *log)
{
struct bpf_link_primer link_primer;
struct bpf_cgroup_link *link;
struct cgroup *cgrp;
int err;
- if (attr->link_create.flags & (~BPF_F_LINK_ATTACH_MASK))
+ if (attr->link_create.flags & (~BPF_F_LINK_ATTACH_MASK)) {
+ bpf_log(log, "Invalid flags.\n");
return -EINVAL;
+ }
cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
- if (IS_ERR(cgrp))
+ if (IS_ERR(cgrp)) {
+ bpf_log(log, "Invalid target_fd.\n");
return PTR_ERR(cgrp);
+ }
link = kzalloc_obj(*link, GFP_USER);
if (!link) {
@@ -1797,7 +1828,7 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
err = cgroup_bpf_attach(cgrp, NULL, NULL, link,
link->link.attach_type, BPF_F_ALLOW_MULTI | attr->link_create.flags,
attr->link_create.cgroup.relative_fd,
- attr->link_create.cgroup.expected_revision);
+ attr->link_create.cgroup.expected_revision, log);
if (err) {
bpf_link_cleanup(&link_primer);
goto out_put_cgroup;
@@ -3253,7 +3284,8 @@ int cgroup_bpf_struct_ops_attach(struct bpf_map *map, const union bpf_attr *attr
cgrp->bpf.flags[atype] = BPF_F_ALLOW_MULTI;
err = insert_pl_to_hlist(pl, progs, NULL, link,
- flags | BPF_F_ALLOW_MULTI, attr->link_create.cgroup.relative_fd);
+ flags | BPF_F_ALLOW_MULTI,
+ attr->link_create.cgroup.relative_fd, NULL);
if (err)
goto unlock;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index f485e68a14ee..56b3f42fded0 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -5929,7 +5929,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
case BPF_PROG_TYPE_CGROUP_DEVICE:
case BPF_PROG_TYPE_CGROUP_SYSCTL:
case BPF_PROG_TYPE_CGROUP_SOCKOPT:
- ret = cgroup_bpf_link_attach(attr, prog);
+ ret = cgroup_bpf_link_attach(attr, prog, log);
break;
case BPF_PROG_TYPE_EXT:
ret = bpf_tracing_prog_attach(prog,
@@ -5952,7 +5952,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
else if (prog->expected_attach_type == BPF_TRACE_ITER)
ret = bpf_iter_link_attach(attr, uattr, prog, log);
else if (prog->expected_attach_type == BPF_LSM_CGROUP)
- ret = cgroup_bpf_link_attach(attr, prog);
+ ret = cgroup_bpf_link_attach(attr, prog, log);
else if (is_tracing_multi(prog->expected_attach_type))
ret = bpf_tracing_multi_attach(prog, attr, log);
else
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH bpf-next v3 4/9] bpf: Add log support for cgroup prog link_create
2026-11-23 15:27 ` [PATCH bpf-next v3 4/9] bpf: Add log support for cgroup prog link_create Leon Hwang
@ 2026-11-23 16:04 ` bot+bpf-ci
0 siblings, 0 replies; 13+ messages in thread
From: bot+bpf-ci @ 2026-11-23 16:04 UTC (permalink / raw)
To: leon.hwang, bpf
Cc: ast, daniel, john.fastabend, andrii, eddyz87, memxor, martin.lau,
song, yonghong.song, jolsa, emil, ihor.solodrai, rostedt,
mhiramat, mathieu.desnoyers, kpsingh, matt, shuah, leon.hwang,
linux-kernel, linux-trace-kernel, linux-kselftest, netdev, ast,
andrii, daniel, martin.lau, eddyz87, yonghong.song, mason,
ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2689 bytes --]
> bpf: Add log support for cgroup prog link_create
TL;DR: The new bpf_log() calls in cgroup attach run under cgroup_mutex and
cgroup_bpf_mutex, so a copy_to_user() fault on the user log buffer may stall
other cgroup operations. Consider logging after unlock.
> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
> --- a/kernel/bpf/cgroup.c
> +++ b/kernel/bpf/cgroup.c
[ ... ]
> @@ -1045,23 +1060,33 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,
> atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id);
> if (atype < 0)
> return -EINVAL;
> - if (revision && revision != cgrp->bpf.revisions[atype])
> + if (revision && revision != cgrp->bpf.revisions[atype]) {
> + bpf_log(log, "Cgroup revision mismatch: expected %llu, actual %llu.\n",
> + revision, cgrp->bpf.revisions[atype]);
> return -ESTALE;
> + }
[ ... ]
> @@ -1143,13 +1168,14 @@ static int cgroup_bpf_attach(struct cgroup *cgrp,
> cgroup_bpf_lock_hierarchy();
> ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, link, type, flags,
> - id_or_fd, revision);
> + id_or_fd, revision, log);
> cgroup_bpf_unlock_hierarchy();
> return ret;
Can these bpf_log() calls block other cgroup users while the locks are
held?
cgroup_bpf_attach() takes cgroup_mutex and cgroup_bpf_mutex through
cgroup_bpf_lock_hierarchy() before calling __cgroup_bpf_attach(), and
the new messages are emitted from inside it (and from get_prog_list()
via insert_pl_to_hlist()):
cgroup_bpf_attach()
cgroup_bpf_lock_hierarchy()
__cgroup_bpf_attach()
bpf_log()
bpf_verifier_vlog()
copy_to_user(log->ubuf + ..., log->kbuf, ...)
log->ubuf is the user supplied log_buf, so if that page is not resident
(swapped out, or backed by FUSE or userfaultfd), copy_to_user() can sleep
in the fault for as long as the caller chooses. While it sleeps,
cgroup_mutex and cgroup_bpf_mutex remain held, so cgroup mkdir/rmdir,
migration, BPF attach/detach, BPF_PROG_QUERY and the cgroup bpf release
work would all wait.
Commit 40aec6bab02e ("bpf, cgroup: Protect cgroup_bpf state with its own
mutex") split these paths out from cgroup_mutex to avoid hung tasks, and
before this patch the attach path did not touch user memory under these
locks.
This isn't a lock ordering problem, since cgroup_mutex -> mmap_lock
already exists, but would it be better to record the message and emit it
from cgroup_bpf_link_attach() after cgroup_bpf_unlock_hierarchy()?
---
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/45209424655
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH bpf-next v3 5/9] libbpf: Add log_opts support for BPF_TRACE_ITER
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
` (3 preceding siblings ...)
2026-11-23 15:27 ` [PATCH bpf-next v3 4/9] bpf: Add log support for cgroup prog link_create Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 6/9] selftests/bpf: Test tracing link_create logs Leon Hwang
` (3 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
Add iter.info and iter.len to pass iterator information separately from
log_opts. Keep the existing iter_info and iter_info_len fields working
for callers that do not request logs.
Thereafter, log_opts can work for iter link_create.
Meanwhile, add log_opts support for bpf_program__attach_iter() API.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
tools/lib/bpf/bpf.c | 23 +++++++++++++++++------
tools/lib/bpf/bpf.h | 8 ++++++++
tools/lib/bpf/libbpf.c | 5 +++--
tools/lib/bpf/libbpf.h | 3 ++-
4 files changed, 30 insertions(+), 9 deletions(-)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 38dcb31e8c0e..ad6b356cc48c 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -808,11 +808,6 @@ int bpf_link_create(int prog_fd, int target_fd,
if (iter_info_len && attach_type != BPF_TRACE_ITER)
return libbpf_err(-EINVAL);
- if (iter_info_len)
- iter_info = OPTS_GET(opts, iter_info, (void *)0);
- else
- log_opts = OPTS_GET(opts, log_opts, (void *)0);
-
/* validate we don't have unexpected combinations of non-zero fields */
if (iter_info_len || target_btf_id) {
if (iter_info_len && target_btf_id)
@@ -821,6 +816,22 @@ int bpf_link_create(int prog_fd, int target_fd,
return libbpf_err(-EINVAL);
}
+ if (attach_type == BPF_TRACE_ITER) {
+ if (iter_info_len) {
+ iter_info = OPTS_GET(opts, iter_info, (void *)0);
+ } else {
+ log_opts = OPTS_GET(opts, log_opts, (void *)0);
+ iter_info = OPTS_GET(opts, iter.info, (void *)0);
+ iter_info_len = OPTS_GET(opts, iter.len, 0);
+ if (iter_info_len && target_btf_id)
+ return libbpf_err(-EINVAL);
+ if (!OPTS_ZEROED(opts, iter))
+ return libbpf_err(-EINVAL);
+ }
+ } else {
+ log_opts = OPTS_GET(opts, log_opts, (void *)0);
+ }
+
memset(&attr, 0, attr_sz);
attr.link_create.prog_fd = prog_fd;
attr.link_create.target_fd = target_fd;
@@ -970,7 +981,7 @@ int bpf_link_create(int prog_fd, int target_fd,
return libbpf_err(-EINVAL);
break;
default:
- if (!OPTS_ZEROED(opts, iter_info_len))
+ if (!OPTS_ZEROED(opts, log_opts))
return libbpf_err(-EINVAL);
break;
}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index fe3b7fd8512b..2ac143fd1102 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -422,6 +422,10 @@ union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
struct bpf_link_create_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
__u32 flags;
+ /*
+ * iter_info and iter_info_len are deprecated, use iter.info and
+ * iter.len instead.
+ */
union {
union bpf_iter_link_info *iter_info;
struct bpf_log_opts *log_opts;
@@ -478,6 +482,10 @@ struct bpf_link_create_opts {
const __u64 *cookies;
__u32 cnt;
} tracing_multi;
+ struct {
+ union bpf_iter_link_info *info;
+ __u32 len;
+ } iter;
};
size_t :0;
};
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1efc0dd37b2e..612eba0ff4f0 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -14813,8 +14813,9 @@ bpf_program__attach_iter(const struct bpf_program *prog,
if (!OPTS_VALID(opts, bpf_iter_attach_opts))
return libbpf_err_ptr(-EINVAL);
- link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
- link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);
+ link_create_opts.log_opts = OPTS_GET(opts, log_opts, (void *)0);
+ link_create_opts.iter.info = OPTS_GET(opts, link_info, (void *)0);
+ link_create_opts.iter.len = OPTS_GET(opts, link_info_len, 0);
prog_fd = bpf_program__fd(prog);
if (prog_fd < 0) {
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 49f1120f4021..56300be81016 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -1000,8 +1000,9 @@ struct bpf_iter_attach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
union bpf_iter_link_info *link_info;
__u32 link_info_len;
+ struct bpf_log_opts *log_opts;
};
-#define bpf_iter_attach_opts__last_field link_info_len
+#define bpf_iter_attach_opts__last_field log_opts
LIBBPF_API struct bpf_link *
bpf_program__attach_iter(const struct bpf_program *prog,
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH bpf-next v3 6/9] selftests/bpf: Test tracing link_create logs
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
` (4 preceding siblings ...)
2026-11-23 15:27 ` [PATCH bpf-next v3 5/9] libbpf: Add log_opts support for BPF_TRACE_ITER Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 7/9] selftests/bpf: Test tracing_multi " Leon Hwang
` (2 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
Check the errno, log message and reported log size for invalid tracing
and freplace link creation parameters.
Cover reattachment, invalid target BTF IDs, mismatched freplace target
attach types, and incompatible context writes and access sizes.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
.../selftests/bpf/prog_tests/tracing_link.c | 392 ++++++++++++++++++
.../selftests/bpf/progs/tracing_link_ctx.c | 17 +
2 files changed, 409 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/tracing_link.c
create mode 100644 tools/testing/selftests/bpf/progs/tracing_link_ctx.c
diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_link.c b/tools/testing/selftests/bpf/prog_tests/tracing_link.c
new file mode 100644
index 000000000000..9e582b2fae54
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/tracing_link.c
@@ -0,0 +1,392 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include "fentry_recursive.skel.h"
+#include "priv_freplace_prog.skel.h"
+#include "xdp_dummy.skel.h"
+#include "kprobe_write_ctx.skel.h"
+#include "tracing_link_ctx.skel.h"
+
+static void test_fentry_link_create_log(int target_fd, __u32 target_btf_id, int cookie,
+ enum bpf_attach_type attach_type,
+ bool reattach, const char *msg)
+{
+ struct fentry_recursive *skel = NULL;
+ struct xdp_dummy *target;
+ char log_buf[128] = {};
+ int err, fd, prog_fd;
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .log_opts = &log_opts,
+ .target_btf_id = target_btf_id,
+ .tracing.cookie = cookie,
+ );
+
+ target = xdp_dummy__open_and_load();
+ if (!ASSERT_OK_PTR(target, "xdp_dummy__open_and_load"))
+ return;
+
+ skel = fentry_recursive__open();
+ if (!ASSERT_OK_PTR(skel, "fentry_recursive__open"))
+ goto out;
+
+ err = bpf_program__set_attach_target(skel->progs.recursive_attach,
+ bpf_program__fd(target->progs.xdp_dummy_prog),
+ "xdp_dummy_prog");
+ if (!ASSERT_OK(err, "bpf_program__set_attach_target"))
+ goto out;
+
+ err = fentry_recursive__load(skel);
+ if (!ASSERT_OK(err, "fentry_recursive__load"))
+ goto out;
+
+ prog_fd = bpf_program__fd(skel->progs.recursive_attach);
+ if (reattach) {
+ /* Consume the initial target saved when the program was loaded. */
+ fd = bpf_link_create(prog_fd, 0, BPF_TRACE_FENTRY, &opts);
+ if (!ASSERT_GE(fd, 0, "bpf_link_create first"))
+ goto out;
+
+ close(fd);
+ memset(log_buf, 0, sizeof(log_buf));
+ log_opts.true_size = 0;
+ }
+
+ fd = bpf_link_create(prog_fd, target_fd, attach_type, &opts);
+ if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+ close(fd);
+ goto out;
+ }
+
+ ASSERT_EQ(fd, -EINVAL, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+out:
+ fentry_recursive__destroy(skel);
+ xdp_dummy__destroy(target);
+}
+
+static void test_log_invalid_attach_type(void)
+{
+ const char *msg = "attach_type is not expected_attach_type of the prog.\n";
+
+ test_fentry_link_create_log(0, 0, 0, BPF_TRACE_FEXIT, false, msg);
+}
+
+static void test_log_tgt_prog_fd_only(void)
+{
+ const char *msg = "target_fd and target_btf_id must be both set or both unset.\n";
+
+ test_fentry_link_create_log(-1, 0, 1, BPF_TRACE_FENTRY, false, msg);
+}
+
+static void test_log_tgt_prog_fd_disallowed(void)
+{
+ const char *msg = "target_fd is only allowed for freplace prog.\n";
+
+ test_fentry_link_create_log(-1, 1, 0, BPF_TRACE_FENTRY, false, msg);
+}
+
+static void test_log_invalid_attach_btf(void)
+{
+ const char *msg = "Cannot re-attach a prog that was attached to a bpf prog.\n";
+
+ test_fentry_link_create_log(0, 0, 1, BPF_TRACE_FENTRY, true, msg);
+}
+
+static void test_freplace_link_create_log(int target_fd, __u32 target_btf_id, int err_exp,
+ bool reattach, const char *msg)
+{
+ struct priv_freplace_prog *skel = NULL;
+ char log_buf[256] = {};
+ struct xdp_dummy *xdp;
+ int err, fd, prog_fd;
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .log_opts = &log_opts,
+ .target_btf_id = target_btf_id,
+ );
+
+ xdp = xdp_dummy__open_and_load();
+ if (!ASSERT_OK_PTR(xdp, "xdp_dummy__open_and_load"))
+ return;
+
+ skel = priv_freplace_prog__open();
+ if (!ASSERT_OK_PTR(skel, "priv_freplace_prog__open"))
+ goto out;
+
+ err = bpf_program__set_attach_target(skel->progs.new_xdp_prog2,
+ bpf_program__fd(xdp->progs.xdp_dummy_prog),
+ "xdp_dummy_prog");
+ if (!ASSERT_OK(err, "bpf_program__set_attach_target"))
+ goto out;
+
+ err = priv_freplace_prog__load(skel);
+ if (!ASSERT_OK(err, "priv_freplace_prog__load"))
+ goto out;
+
+ prog_fd = bpf_program__fd(skel->progs.new_xdp_prog2);
+ if (reattach) {
+ /* Consume the initial target saved when the program was loaded. */
+ fd = bpf_link_create(prog_fd, 0, 0, &opts);
+ if (!ASSERT_GE(fd, 0, "bpf_link_create first"))
+ goto out;
+
+ close(fd);
+ memset(log_buf, 0, sizeof(log_buf));
+ log_opts.true_size = 0;
+ }
+
+ if (!target_fd && target_btf_id)
+ target_fd = bpf_program__fd(xdp->progs.xdp_dummy_prog);
+
+ fd = bpf_link_create(prog_fd, target_fd, 0, &opts);
+ if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+ close(fd);
+ goto out;
+ }
+
+ ASSERT_EQ(fd, err_exp, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+out:
+ priv_freplace_prog__destroy(skel);
+ xdp_dummy__destroy(xdp);
+}
+
+static void test_log_invalid_target_fd(void)
+{
+ const char *msg = "Invalid tgt_fd.\n";
+
+ test_freplace_link_create_log(-1, 1, -EBADF, false, msg);
+}
+
+static void test_log_reattach_without_target(void)
+{
+ const char *msg = "Re-attach of freplace prog needs target_fd and target_btf_id.\n";
+
+ test_freplace_link_create_log(0, 0, -EINVAL, true, msg);
+}
+
+static void test_log_invalid_target_btf_id(void)
+{
+ const char *msg = "attach_btf_id 4294967295 is invalid\n";
+
+ test_freplace_link_create_log(0, ~0U, -EINVAL, false, msg);
+}
+
+static void test_log_freplace_target_type_mismatch(void)
+{
+ const char *msg = "Target differs in type from the target at load time\n";
+ struct priv_freplace_prog *skel = NULL;
+ char log_buf[128] = {};
+ struct xdp_dummy *xdp;
+ int err, fd, btf_id;
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .log_opts = &log_opts,
+ );
+
+ xdp = xdp_dummy__open();
+ if (!ASSERT_OK_PTR(xdp, "xdp_dummy__open"))
+ return;
+
+ /* Keep the two target prototypes identical and vary the attach type. */
+ err = bpf_program__set_expected_attach_type(xdp->progs.__x64_sys_nop, BPF_XDP_DEVMAP);
+ if (!ASSERT_OK(err, "set_expected_attach_type"))
+ goto out;
+ err = xdp_dummy__load(xdp);
+ if (!ASSERT_OK(err, "xdp_dummy__load"))
+ goto out;
+
+ skel = priv_freplace_prog__open();
+ if (!ASSERT_OK_PTR(skel, "priv_freplace_prog__open"))
+ goto out;
+
+ err = bpf_program__set_attach_target(skel->progs.new_xdp_prog2,
+ bpf_program__fd(xdp->progs.xdp_dummy_prog),
+ "xdp_dummy_prog");
+ if (!ASSERT_OK(err, "bpf_program__set_attach_target"))
+ goto out;
+ err = priv_freplace_prog__load(skel);
+ if (!ASSERT_OK(err, "priv_freplace_prog__load"))
+ goto out;
+
+ btf_id = btf__find_by_name_kind(bpf_object__btf(xdp->obj), "__x64_sys_nop", BTF_KIND_FUNC);
+ if (!ASSERT_GT(btf_id, 0, "target_btf_id"))
+ goto out;
+
+ opts.target_btf_id = btf_id;
+ fd = bpf_link_create(bpf_program__fd(skel->progs.new_xdp_prog2),
+ bpf_program__fd(xdp->progs.__x64_sys_nop), 0, &opts);
+ if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+ close(fd);
+ goto out;
+ }
+
+ ASSERT_EQ(fd, -EINVAL, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+out:
+ priv_freplace_prog__destroy(skel);
+ xdp_dummy__destroy(xdp);
+}
+
+#ifdef __x86_64__
+static void test_log_invalid_kprobe_write_ctx(void)
+{
+ const char *msg = "Only one of freplace prog and its target writes to pt_regs.\n";
+ struct kprobe_write_ctx *skel_kprobe, *skel_freplace = NULL;
+ struct bpf_program *prog_kprobe, *prog_freplace;
+ int err, prog_fd, btf_id, fd;
+ char log_buf[128] = {};
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+ LIBBPF_OPTS(bpf_link_create_opts, link_opts,
+ .log_opts = &log_opts,
+ );
+
+ skel_kprobe = kprobe_write_ctx__open();
+ if (!ASSERT_OK_PTR(skel_kprobe, "kprobe_write_ctx__open kprobe"))
+ return;
+
+ prog_kprobe = skel_kprobe->progs.kprobe_dummy;
+ bpf_program__set_autoload(prog_kprobe, true);
+
+ err = kprobe_write_ctx__load(skel_kprobe);
+ if (!ASSERT_OK(err, "kprobe_write_ctx__load kprobe"))
+ goto out;
+
+ skel_freplace = kprobe_write_ctx__open();
+ if (!ASSERT_OK_PTR(skel_freplace, "kprobe_write_ctx__open freplace"))
+ goto out;
+
+ prog_freplace = skel_freplace->progs.freplace_kprobe;
+ bpf_program__set_autoload(prog_freplace, true);
+
+ prog_fd = bpf_program__fd(skel_kprobe->progs.kprobe_write_ctx);
+ bpf_program__set_attach_target(prog_freplace, prog_fd, "kprobe_write_ctx");
+
+ err = kprobe_write_ctx__load(skel_freplace);
+ if (!ASSERT_OK(err, "kprobe_write_ctx__load freplace"))
+ goto out;
+
+ prog_fd = bpf_program__fd(prog_kprobe);
+ btf_id = btf__find_by_name_kind(bpf_object__btf(skel_kprobe->obj), "kprobe_dummy",
+ BTF_KIND_FUNC);
+ if (!ASSERT_GT(btf_id, 0, "btf__find_by_name_kind"))
+ goto out;
+
+ link_opts.target_btf_id = btf_id;
+ fd = bpf_link_create(bpf_program__fd(prog_freplace), prog_fd, 0, &link_opts);
+ if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+ close(fd);
+ goto out;
+ }
+
+ ASSERT_EQ(fd, -EINVAL, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+out:
+ kprobe_write_ctx__destroy(skel_freplace);
+ kprobe_write_ctx__destroy(skel_kprobe);
+}
+#else
+static void test_log_invalid_kprobe_write_ctx(void)
+{
+ test__skip();
+}
+#endif
+
+static void test_log_freplace_ctx_size(void)
+{
+ const char *msg = "Freplace prog accesses more ctx than its target.\n";
+ struct tracing_link_ctx *target, *skel = NULL;
+ char log_buf[128] = {};
+ int err, fd;
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .log_opts = &log_opts,
+ );
+
+ target = tracing_link_ctx__open_and_load();
+ if (!ASSERT_OK_PTR(target, "tracing_link_ctx__open_and_load"))
+ return;
+
+ skel = tracing_link_ctx__open();
+ if (!ASSERT_OK_PTR(skel, "tracing_link_ctx__open"))
+ goto out;
+
+ bpf_program__set_autoload(skel->progs.target, false);
+ bpf_program__set_autoload(skel->progs.replacement, true);
+
+ err = bpf_program__set_attach_target(skel->progs.replacement,
+ bpf_program__fd(target->progs.target), "target");
+ if (!ASSERT_OK(err, "bpf_program__set_attach_target"))
+ goto out;
+
+ err = tracing_link_ctx__load(skel);
+ if (!ASSERT_OK(err, "tracing_link_ctx__load"))
+ goto out;
+
+ fd = bpf_link_create(bpf_program__fd(skel->progs.replacement), 0, 0, &opts);
+ if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+ close(fd);
+ goto out;
+ }
+
+ ASSERT_EQ(fd, -EINVAL, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+out:
+ tracing_link_ctx__destroy(skel);
+ tracing_link_ctx__destroy(target);
+}
+
+void test_tracing_link_log(void)
+{
+ if (test__start_subtest("invalid_attach_type"))
+ test_log_invalid_attach_type();
+ if (test__start_subtest("tgt_prog_fd_only"))
+ test_log_tgt_prog_fd_only();
+ if (test__start_subtest("tgt_prog_fd_disallowed"))
+ test_log_tgt_prog_fd_disallowed();
+ if (test__start_subtest("invalid_attach_btf"))
+ test_log_invalid_attach_btf();
+ if (test__start_subtest("invalid_target_fd"))
+ test_log_invalid_target_fd();
+ if (test__start_subtest("reattach_without_target"))
+ test_log_reattach_without_target();
+ if (test__start_subtest("invalid_target_btf_id"))
+ test_log_invalid_target_btf_id();
+ if (test__start_subtest("freplace_target_type_mismatch"))
+ test_log_freplace_target_type_mismatch();
+ if (test__start_subtest("invalid_kprobe_write_ctx"))
+ test_log_invalid_kprobe_write_ctx();
+ if (test__start_subtest("freplace_ctx_size"))
+ test_log_freplace_ctx_size();
+}
diff --git a/tools/testing/selftests/bpf/progs/tracing_link_ctx.c b/tools/testing/selftests/bpf/progs/tracing_link_ctx.c
new file mode 100644
index 000000000000..940a7d74b7e3
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tracing_link_ctx.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("tracepoint")
+int target(struct trace_event_raw_sys_enter *ctx __arg_ctx)
+{
+ return 0;
+}
+
+SEC("?freplace")
+int replacement(struct trace_event_raw_sys_enter *ctx __arg_ctx)
+{
+ return ctx->id;
+}
+
+char LICENSE[] SEC("license") = "GPL";
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH bpf-next v3 7/9] selftests/bpf: Test tracing_multi link_create logs
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
` (5 preceding siblings ...)
2026-11-23 15:27 ` [PATCH bpf-next v3 6/9] selftests/bpf: Test tracing link_create logs Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
2026-11-23 16:04 ` bot+bpf-ci
2026-11-23 15:27 ` [PATCH bpf-next v3 8/9] selftests/bpf: Verify iter " Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 9/9] selftests/bpf: Verify cgroup prog " Leon Hwang
8 siblings, 1 reply; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
Check the errno, log message and reported log size when tracing_multi
link creation rejects invalid parameters, duplicate IDs or a target.
Cover invalid BTF IDs and types, unsupported prototypes, noreturn
FEXIT_MULTI targets, targets whose addresses cannot be resolved, targets
without an ftrace location, and sleepable attachment restrictions. Check
the diagnostic when tracing_multi is unavailable.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
.../selftests/bpf/prog_tests/tracing_link.c | 349 ++++++++++++++++++
.../selftests/bpf/progs/tracing_link_multi.c | 36 ++
.../selftests/bpf/test_kmods/bpf_testmod.c | 5 +
3 files changed, 390 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/tracing_link_multi.c
diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_link.c b/tools/testing/selftests/bpf/prog_tests/tracing_link.c
index 9e582b2fae54..f958f084c6f0 100644
--- a/tools/testing/selftests/bpf/prog_tests/tracing_link.c
+++ b/tools/testing/selftests/bpf/prog_tests/tracing_link.c
@@ -6,6 +6,7 @@
#include "xdp_dummy.skel.h"
#include "kprobe_write_ctx.skel.h"
#include "tracing_link_ctx.skel.h"
+#include "tracing_link_multi.skel.h"
static void test_fentry_link_create_log(int target_fd, __u32 target_btf_id, int cookie,
enum bpf_attach_type attach_type,
@@ -390,3 +391,351 @@ void test_tracing_link_log(void)
if (test__start_subtest("freplace_ctx_size"))
test_log_freplace_ctx_size();
}
+
+static void test_tracing_multi_link_create_log(const char *prog_name, int target_fd,
+ struct bpf_link_create_opts *opts,
+ int err_exp, const char *msg)
+{
+ struct tracing_link_multi *skel;
+ struct bpf_program *prog;
+ char log_buf[128] = {};
+ int err, fd;
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+
+ skel = tracing_link_multi__open();
+ if (!ASSERT_OK_PTR(skel, "tracing_link_multi__open"))
+ return;
+
+ prog = bpf_object__find_program_by_name(skel->obj, prog_name);
+ if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
+ goto out;
+
+ bpf_program__set_autoload(prog, true);
+ err = tracing_link_multi__load(skel);
+ if (!ASSERT_OK(err, "tracing_link_multi__load"))
+ goto out;
+
+ opts->log_opts = &log_opts;
+ fd = bpf_link_create(bpf_program__fd(prog), target_fd,
+ bpf_program__expected_attach_type(prog), opts);
+ opts->log_opts = NULL;
+ if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+ close(fd);
+ goto out;
+ }
+
+ ASSERT_EQ(fd, err_exp, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, msg[0] ? strlen(msg) + 1 : 0, "log_size");
+
+out:
+ tracing_link_multi__destroy(skel);
+}
+
+static void test_multi_log_excessive_count(void)
+{
+ const char *msg = "tracing_multi.cnt 2147483647 is above the limit 1048576.\n";
+ __u32 id = 0;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .tracing_multi.ids = &id,
+ .tracing_multi.cnt = INT_MAX,
+ );
+
+ test_tracing_multi_link_create_log("fentry", 0, &opts, -E2BIG, msg);
+}
+
+static void test_multi_log_invalid_flags(void)
+{
+ const char *msg = "flags must be zero.\n";
+ __u32 id = 0;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = 1,
+ .tracing_multi.ids = &id,
+ .tracing_multi.cnt = 1,
+ );
+
+ test_tracing_multi_link_create_log("fentry", 0, &opts, -EINVAL, msg);
+}
+
+static void test_multi_log_invalid_target_fd(void)
+{
+ const char *msg = "target_fd must be zero.\n";
+ __u32 id = 0;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .tracing_multi.ids = &id,
+ .tracing_multi.cnt = 1,
+ );
+
+ test_tracing_multi_link_create_log("fentry", -1, &opts, -EINVAL, msg);
+}
+
+static void test_multi_log_duplicate_ids(void)
+{
+ struct btf *btf;
+ char msg[128];
+ __u32 ids[2];
+ int id;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .tracing_multi.ids = ids,
+ .tracing_multi.cnt = ARRAY_SIZE(ids),
+ );
+
+ btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
+ return;
+
+ id = btf__find_by_name_kind(btf, "bpf_fentry_test1", BTF_KIND_FUNC);
+ if (!ASSERT_GT(id, 0, "target_btf_id"))
+ goto out;
+
+ ids[0] = ids[1] = id;
+
+ snprintf(msg, sizeof(msg), "id %u is in tracing_multi.ids more than once.\n", id);
+ test_tracing_multi_link_create_log("fentry", 0, &opts, -EINVAL, msg);
+
+out:
+ btf__free(btf);
+}
+
+static void test_multi_log_target(struct btf *btf, const char *prog_name, const char *target,
+ int err_exp, const char *format)
+{
+ char msg[128];
+ __u32 id;
+ int ret;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .tracing_multi.ids = &id,
+ .tracing_multi.cnt = 1,
+ );
+
+ ret = btf__find_by_name_kind(btf, target, BTF_KIND_FUNC);
+ if (!ASSERT_GT(ret, 0, "target_btf_id"))
+ return;
+
+ id = ret;
+ snprintf(msg, sizeof(msg), format, id, target);
+ test_tracing_multi_link_create_log(prog_name, 0, &opts, err_exp, msg);
+}
+
+static void test_multi_log_vmlinux_target(const char *prog_name, const char *target,
+ int err_exp, const char *format)
+{
+ struct btf *btf;
+
+ btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
+ return;
+
+ test_multi_log_target(btf, prog_name, target, err_exp, format);
+ btf__free(btf);
+}
+
+static void test_multi_log_module_target(const char *prog_name, const char *target,
+ int err_exp, const char *format)
+{
+ struct btf *vmlinux, *btf;
+
+ if (!env.has_testmod) {
+ test__skip();
+ return;
+ }
+
+ vmlinux = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(vmlinux, "btf__load_vmlinux_btf"))
+ return;
+
+ btf = btf__load_module_btf("bpf_testmod", vmlinux);
+ if (!ASSERT_OK_PTR(btf, "btf__load_module_btf"))
+ goto out;
+
+ test_multi_log_target(btf, prog_name, target, err_exp, format);
+ btf__free(btf);
+
+out:
+ btf__free(vmlinux);
+}
+
+static void test_multi_log_noreturn_fexit(void)
+{
+ const char *msg = "Cannot attach fexit/fsession_multi prog to noreturn btf id %u.\n";
+
+ test_multi_log_vmlinux_target("fexit", "do_exit", -EINVAL, msg);
+}
+
+static void test_multi_log_denylist(void)
+{
+ const char *msg = "Cannot attach to denylist btf id %u.\n";
+ const char *target = "migrate_disable";
+ struct btf *btf;
+
+ btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
+ return;
+
+ /* migrate_disable is only in the denylist on SMP kernels. */
+ if (btf__find_by_name_kind(btf, target, BTF_KIND_FUNC) == -ENOENT)
+ test__skip();
+ else
+ test_multi_log_target(btf, "fentry", target, -EINVAL, msg);
+
+ btf__free(btf);
+}
+
+static void test_multi_log_invalid_btf_id(void)
+{
+ const char *msg = "Cannot resolve type of btf id 4294967295.\n";
+ __u32 id = ~0U;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .tracing_multi.ids = &id,
+ .tracing_multi.cnt = 1,
+ );
+
+ test_tracing_multi_link_create_log("fentry", 0, &opts, -EINVAL, msg);
+}
+
+static void test_multi_log_non_function(void)
+{
+ const char *target = "bpf_fentry_test_t";
+ struct btf *btf;
+ char msg[128];
+ __u32 id;
+ int ret;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .tracing_multi.ids = &id,
+ .tracing_multi.cnt = 1,
+ );
+
+ btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
+ return;
+
+ ret = btf__find_by_name_kind(btf, target, BTF_KIND_STRUCT);
+ if (!ASSERT_GT(ret, 0, "target_btf_id"))
+ goto out;
+
+ id = ret;
+ snprintf(msg, sizeof(msg), "Type of id %u is not function.\n", id);
+ test_tracing_multi_link_create_log("fentry", 0, &opts, -EINVAL, msg);
+
+out:
+ btf__free(btf);
+}
+
+static void test_multi_log_too_many_args(void)
+{
+ const char *msg = "The function bpf_trace_run12 has 13 arguments. Too many.\n";
+
+ test_multi_log_vmlinux_target("fentry", "bpf_trace_run12", -EINVAL, msg);
+}
+
+static void test_multi_log_varargs(void)
+{
+ const char *msg = "The function _printk with variable args is unsupported.\n";
+
+ test_multi_log_vmlinux_target("fentry", "_printk", -EINVAL, msg);
+}
+
+static void test_multi_log_large_return(void)
+{
+ const char *msg = "The function bpf_kfunc_call_test_ret_big return type STRUCT is unsupported.\n";
+
+ test_multi_log_module_target("module_fentry", "bpf_kfunc_call_test_ret_big", -EINVAL, msg);
+}
+
+static void test_multi_log_large_arg(void)
+{
+ const char *msg = "The function bpf_testmod_large_arg arg0 type STRUCT is unsupported.\n";
+
+ test_multi_log_module_target("module_fentry", "bpf_testmod_large_arg", -EINVAL, msg);
+}
+
+static void test_multi_log_large_return_fexit(void)
+{
+ const char *msg = "Attach to target %u:%s with a >8 byte return value is not supported for this attach type.\n";
+
+ test_multi_log_module_target("module_fexit", "bpf_kfunc_call_test_ret_pair",
+ -EOPNOTSUPP, msg);
+}
+
+static void test_multi_log_missing_address(void)
+{
+ const char *msg = "Cannot resolve address of target %u:%s.\n";
+
+ /* The type is visible through base BTF, but the symbol is in vmlinux. */
+ test_multi_log_module_target("module_fentry", "bpf_fentry_test1", -ENOENT, msg);
+}
+
+static void test_multi_log_notrace(void)
+{
+ const char *msg = "Target %u:%s is not ftrace attachable.\n";
+
+ test_multi_log_vmlinux_target("fentry", "__bpf_tramp_enter", -ENOENT, msg);
+}
+
+static void test_multi_log_not_sleepable(void)
+{
+ const char *msg = "Target %u:%s is not sleepable.\n";
+
+ test_multi_log_vmlinux_target("fentry_sleepable", "bpf_fentry_test2", -EINVAL, msg);
+}
+
+#ifndef __x86_64__
+static void test_multi_log_kernel_unsupported(void)
+{
+ const char *msg = "This kernel does not support tracing_multi.\n";
+ __u32 id = 0;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = 1,
+ .tracing_multi.ids = &id,
+ .tracing_multi.cnt = 1,
+ );
+
+ test_tracing_multi_link_create_log("fentry", 0, &opts, -EOPNOTSUPP, msg);
+}
+#endif
+
+void test_tracing_multi_link_log(void)
+{
+#ifndef __x86_64__
+ if (test__start_subtest("kernel_unsupported"))
+ test_multi_log_kernel_unsupported();
+ return;
+#endif
+
+ if (test__start_subtest("excessive_count"))
+ test_multi_log_excessive_count();
+ if (test__start_subtest("invalid_flags"))
+ test_multi_log_invalid_flags();
+ if (test__start_subtest("invalid_target_fd"))
+ test_multi_log_invalid_target_fd();
+ if (test__start_subtest("duplicate_ids"))
+ test_multi_log_duplicate_ids();
+ if (test__start_subtest("noreturn_fexit"))
+ test_multi_log_noreturn_fexit();
+ if (test__start_subtest("denylist"))
+ test_multi_log_denylist();
+ if (test__start_subtest("invalid_btf_id"))
+ test_multi_log_invalid_btf_id();
+ if (test__start_subtest("non_function"))
+ test_multi_log_non_function();
+ if (test__start_subtest("too_many_args"))
+ test_multi_log_too_many_args();
+ if (test__start_subtest("varargs"))
+ test_multi_log_varargs();
+ if (test__start_subtest("large_return"))
+ test_multi_log_large_return();
+ if (test__start_subtest("large_arg"))
+ test_multi_log_large_arg();
+ if (test__start_subtest("large_return_fexit"))
+ test_multi_log_large_return_fexit();
+ if (test__start_subtest("missing_address"))
+ test_multi_log_missing_address();
+ if (test__start_subtest("notrace"))
+ test_multi_log_notrace();
+ if (test__start_subtest("not_sleepable"))
+ test_multi_log_not_sleepable();
+}
diff --git a/tools/testing/selftests/bpf/progs/tracing_link_multi.c b/tools/testing/selftests/bpf/progs/tracing_link_multi.c
new file mode 100644
index 000000000000..11380c004da0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tracing_link_multi.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("?fentry.multi")
+int BPF_PROG(fentry)
+{
+ return 0;
+}
+
+SEC("?fexit.multi")
+int BPF_PROG(fexit)
+{
+ return 0;
+}
+
+SEC("?fentry.multi.s")
+int BPF_PROG(fentry_sleepable)
+{
+ return 0;
+}
+
+SEC("?fentry.multi/bpf_testmod:*")
+int BPF_PROG(module_fentry)
+{
+ return 0;
+}
+
+SEC("?fexit.multi/bpf_testmod:*")
+int BPF_PROG(module_fexit)
+{
+ return 0;
+}
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 1fbc0061cc24..4b011330a5a1 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -88,6 +88,11 @@ struct bpf_testmod_oob {
__bpf_hook_start();
+noinline int bpf_testmod_large_arg(struct prog_test_ret_big a)
+{
+ return a.a + a.b + a.c;
+}
+
noinline int
bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a, int b, int c) {
bpf_testmod_test_struct_arg_result = a.a + a.b + b + c;
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH bpf-next v3 7/9] selftests/bpf: Test tracing_multi link_create logs
2026-11-23 15:27 ` [PATCH bpf-next v3 7/9] selftests/bpf: Test tracing_multi " Leon Hwang
@ 2026-11-23 16:04 ` bot+bpf-ci
2026-11-23 22:04 ` Jiri Olsa
0 siblings, 1 reply; 13+ messages in thread
From: bot+bpf-ci @ 2026-11-23 16:04 UTC (permalink / raw)
To: leon.hwang, bpf
Cc: ast, daniel, john.fastabend, andrii, eddyz87, memxor, martin.lau,
song, yonghong.song, jolsa, emil, ihor.solodrai, rostedt,
mhiramat, mathieu.desnoyers, kpsingh, matt, shuah, leon.hwang,
linux-kernel, linux-trace-kernel, linux-kselftest, netdev, ast,
andrii, daniel, martin.lau, eddyz87, yonghong.song, mason,
ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2281 bytes --]
> selftests/bpf: Test tracing_multi link_create logs
TL;DR: Test issue: the migrate_disable denylist subtest's skip may not trigger
on !CONFIG_SMP kernels, since the function still exists in BTF, so it could
fail instead of skipping.
> diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_link.c b/tools/testing/selftests/bpf/prog_tests/tracing_link.c
> --- a/tools/testing/selftests/bpf/prog_tests/tracing_link.c
> +++ b/tools/testing/selftests/bpf/prog_tests/tracing_link.c
[ ... ]
> +static void test_multi_log_denylist(void)
> +{
> + const char *msg = "Cannot attach to denylist btf id %u.\n";
> + const char *target = "migrate_disable";
> + struct btf *btf;
> +
> + btf = btf__load_vmlinux_btf();
> + if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
> + return;
> +
> + /* migrate_disable is only in the denylist on SMP kernels. */
> + if (btf__find_by_name_kind(btf, target, BTF_KIND_FUNC) == -ENOENT)
> + test__skip();
> + else
> + test_multi_log_target(btf, "fentry", target, -EINVAL, msg);
Is this skip reachable on a !CONFIG_SMP kernel?
kernel/sched/core.c always builds an out-of-line, exported
migrate_disable() with no CONFIG_SMP guard:
void migrate_disable(void)
{
__migrate_disable();
}
EXPORT_SYMBOL_GPL(migrate_disable);
while the denylist in kernel/bpf/verifier.c adds it only under
CONFIG_SMP:
BTF_SET_START(btf_id_deny)
BTF_ID_UNUSED
#ifdef CONFIG_SMP
BTF_ID(func, ___migrate_enable)
BTF_ID(func, migrate_disable)
BTF_ID(func, migrate_enable)
#endif
So btf__find_by_name_kind() should find migrate_disable even when
CONFIG_SMP=n, and test__skip() would never run.
On such an x86_64 kernel, bpf_check_attach_btf_id_multi() would not deny
the id, the link would be created, and the subtest would fail at
ASSERT_LT(fd, 0, "bpf_link_create") instead of skipping.
Should the skip check the kernel's SMP configuration directly, for
example through a __kconfig extern, or should the comment and skip be
dropped?
> +
> + btf__free(btf);
> +}
[ ... ]
---
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/45209424655
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH bpf-next v3 7/9] selftests/bpf: Test tracing_multi link_create logs
2026-11-23 16:04 ` bot+bpf-ci
@ 2026-11-23 22:04 ` Jiri Olsa
0 siblings, 0 replies; 13+ messages in thread
From: Jiri Olsa @ 2026-11-23 22:04 UTC (permalink / raw)
To: bot+bpf-ci
Cc: leon.hwang, bpf, ast, daniel, john.fastabend, andrii, eddyz87,
memxor, martin.lau, song, yonghong.song, emil, ihor.solodrai,
rostedt, mhiramat, mathieu.desnoyers, kpsingh, matt, shuah,
linux-kernel, linux-trace-kernel, linux-kselftest, netdev,
martin.lau, mason
On Mon, Nov 23, 2026 at 04:04:41PM +0000, bot+bpf-ci@kernel.org wrote:
> > selftests/bpf: Test tracing_multi link_create logs
>
> TL;DR: Test issue: the migrate_disable denylist subtest's skip may not trigger
> on !CONFIG_SMP kernels, since the function still exists in BTF, so it could
> fail instead of skipping.
>
> > diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_link.c b/tools/testing/selftests/bpf/prog_tests/tracing_link.c
> > --- a/tools/testing/selftests/bpf/prog_tests/tracing_link.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/tracing_link.c
> [ ... ]
> > +static void test_multi_log_denylist(void)
> > +{
> > + const char *msg = "Cannot attach to denylist btf id %u.\n";
> > + const char *target = "migrate_disable";
> > + struct btf *btf;
> > +
> > + btf = btf__load_vmlinux_btf();
> > + if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
> > + return;
> > +
> > + /* migrate_disable is only in the denylist on SMP kernels. */
> > + if (btf__find_by_name_kind(btf, target, BTF_KIND_FUNC) == -ENOENT)
> > + test__skip();
> > + else
> > + test_multi_log_target(btf, "fentry", target, -EINVAL, msg);
>
> Is this skip reachable on a !CONFIG_SMP kernel?
>
> kernel/sched/core.c always builds an out-of-line, exported
> migrate_disable() with no CONFIG_SMP guard:
>
> void migrate_disable(void)
> {
> __migrate_disable();
> }
> EXPORT_SYMBOL_GPL(migrate_disable);
>
> while the denylist in kernel/bpf/verifier.c adds it only under
> CONFIG_SMP:
>
> BTF_SET_START(btf_id_deny)
> BTF_ID_UNUSED
> #ifdef CONFIG_SMP
> BTF_ID(func, ___migrate_enable)
> BTF_ID(func, migrate_disable)
> BTF_ID(func, migrate_enable)
> #endif
>
> So btf__find_by_name_kind() should find migrate_disable even when
> CONFIG_SMP=n, and test__skip() would never run.
>
> On such an x86_64 kernel, bpf_check_attach_btf_id_multi() would not deny
> the id, the link would be created, and the subtest would fail at
> ASSERT_LT(fd, 0, "bpf_link_create") instead of skipping.
>
> Should the skip check the kernel's SMP configuration directly, for
> example through a __kconfig extern, or should the comment and skip be
> dropped?
I don't think we care, but if we do perhaps we could use __rcu_read_lock instead
jirka
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH bpf-next v3 8/9] selftests/bpf: Verify iter link_create logs
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
` (6 preceding siblings ...)
2026-11-23 15:27 ` [PATCH bpf-next v3 7/9] selftests/bpf: Test tracing_multi " Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
2026-11-23 15:27 ` [PATCH bpf-next v3 9/9] selftests/bpf: Verify cgroup prog " Leon Hwang
8 siblings, 0 replies; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
Check the error code, log message, and reported log size for invalid target
FDs, flags, and iterator information. Also cover attaching a sleepable
program to an iterator that does not support it.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
.../selftests/bpf/prog_tests/iter_link.c | 156 ++++++++++++++++++
1 file changed, 156 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/iter_link.c
diff --git a/tools/testing/selftests/bpf/prog_tests/iter_link.c b/tools/testing/selftests/bpf/prog_tests/iter_link.c
new file mode 100644
index 000000000000..d7904b4aa2b1
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/iter_link.c
@@ -0,0 +1,156 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "bpf_iter_bpf_map.skel.h"
+
+static void test_iter_link_create_log(int target_fd, struct bpf_link_create_opts *opts,
+ __u64 flags, int err_exp, const char *msg)
+{
+ struct bpf_iter_bpf_map *skel;
+ char log_buf[128] = {};
+ int err, fd, prog_fd;
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+
+ skel = bpf_iter_bpf_map__open();
+ if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_map__open"))
+ return;
+
+ err = bpf_program__set_flags(skel->progs.dump_bpf_map, flags);
+ if (!ASSERT_OK(err, "bpf_program__set_flags"))
+ goto out;
+
+ err = bpf_iter_bpf_map__load(skel);
+ if (!ASSERT_OK(err, "bpf_iter_bpf_map__load"))
+ goto out;
+
+ opts->log_opts = &log_opts;
+ prog_fd = bpf_program__fd(skel->progs.dump_bpf_map);
+ fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER, opts);
+ if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+ close(fd);
+ goto out;
+ }
+
+ ASSERT_EQ(fd, err_exp, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+out:
+ bpf_iter_bpf_map__destroy(skel);
+}
+
+static void test_log_invalid_target_fd(void)
+{
+ const char *msg = "Invalid target_fd.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts);
+
+ test_iter_link_create_log(-1, &opts, 0, -EINVAL, msg);
+}
+
+static void test_log_invalid_flags(void)
+{
+ const char *msg = "Invalid flags.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = 1,
+ );
+
+ test_iter_link_create_log(0, &opts, 0, -EINVAL, msg);
+}
+
+static void test_log_iter_info_only(void)
+{
+ const char *msg = "iter_info and iter_info_len must be both set or both unset.\n";
+ union bpf_iter_link_info info = {};
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .iter.info = &info,
+ );
+
+ test_iter_link_create_log(0, &opts, 0, -EINVAL, msg);
+}
+
+static void test_log_iter_info_len_only(void)
+{
+ const char *msg = "iter_info and iter_info_len must be both set or both unset.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .iter.len = sizeof(union bpf_iter_link_info),
+ );
+
+ test_iter_link_create_log(0, &opts, 0, -EINVAL, msg);
+}
+
+static void test_log_invalid_iter_info_len(void)
+{
+ unsigned char info[sizeof(union bpf_iter_link_info) + 1] = {};
+ const char *msg = "Invalid iter_info_len.\n";
+ struct bpf_iter_bpf_map *skel;
+ struct bpf_link *link;
+ char log_buf[128] = {};
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+ LIBBPF_OPTS(bpf_iter_attach_opts, opts,
+ .link_info = (union bpf_iter_link_info *)info,
+ .link_info_len = sizeof(info),
+ .log_opts = &log_opts,
+ );
+
+ skel = bpf_iter_bpf_map__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_map__open_and_load"))
+ return;
+
+ info[sizeof(info) - 1] = 1;
+ link = bpf_program__attach_iter(skel->progs.dump_bpf_map, &opts);
+ if (!ASSERT_ERR_PTR(link, "bpf_program__attach_iter")) {
+ bpf_link__destroy(link);
+ goto out;
+ }
+
+ ASSERT_EQ(libbpf_get_error(link), -E2BIG, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+out:
+ bpf_iter_bpf_map__destroy(skel);
+}
+
+static void test_log_invalid_iter_info(void)
+{
+ const char *msg = "Invalid iter_info.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .iter.info = (union bpf_iter_link_info *)1,
+ .iter.len = sizeof(union bpf_iter_link_info),
+ );
+
+ test_iter_link_create_log(0, &opts, 0, -EFAULT, msg);
+}
+
+static void test_log_invalid_prog(void)
+{
+ const char *msg = "The iter target does not support the bpf prog.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts);
+
+ test_iter_link_create_log(0, &opts, BPF_F_SLEEPABLE, -EINVAL, msg);
+}
+
+void test_iter_link_log(void)
+{
+ if (test__start_subtest("invalid_target_fd"))
+ test_log_invalid_target_fd();
+ if (test__start_subtest("invalid_flags"))
+ test_log_invalid_flags();
+ if (test__start_subtest("iter_info_only"))
+ test_log_iter_info_only();
+ if (test__start_subtest("iter_info_len_only"))
+ test_log_iter_info_len_only();
+ if (test__start_subtest("invalid_iter_info"))
+ test_log_invalid_iter_info();
+ if (test__start_subtest("invalid_iter_info_len"))
+ test_log_invalid_iter_info_len();
+ if (test__start_subtest("invalid_prog"))
+ test_log_invalid_prog();
+}
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH bpf-next v3 9/9] selftests/bpf: Verify cgroup prog link_create logs
2026-11-23 15:27 [PATCH bpf-next v3 0/9] bpf: Add log support for tracing link_create Leon Hwang
` (7 preceding siblings ...)
2026-11-23 15:27 ` [PATCH bpf-next v3 8/9] selftests/bpf: Verify iter " Leon Hwang
@ 2026-11-23 15:27 ` Leon Hwang
8 siblings, 0 replies; 13+ messages in thread
From: Leon Hwang @ 2026-11-23 15:27 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
Mathieu Desnoyers, KP Singh, Matt Bobrowski, Shuah Khan,
Leon Hwang, linux-kernel, linux-trace-kernel, linux-kselftest,
netdev
Check the error code, log message, and reported log size for unsupported
flags, invalid target FDs, and relative attachment errors. Cover missing
or conflicting ordering flags, a missing BPF_F_LINK flag, invalid link FDs
and IDs, preorder mismatches, and anchors outside the target cgroup.
Also cover revision mismatches, conflicting attachment flags, cgroup
hierarchy restrictions, and the per-cgroup program limit.
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
.../selftests/bpf/prog_tests/cgroup_link.c | 324 ++++++++++++++++++
1 file changed, 324 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_link.c b/tools/testing/selftests/bpf/prog_tests/cgroup_link.c
index 15093a69510e..5a27d3a6a26c 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_link.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_link.c
@@ -253,3 +253,327 @@ void serial_test_cgroup_link(void)
}
cleanup_cgroup_environment();
}
+
+static void check_cgroup_link_create_log(int prog_fd, int target_fd,
+ struct bpf_link_create_opts *opts,
+ int err_exp, const char *msg)
+{
+ char log_buf[128] = {};
+ LIBBPF_OPTS(bpf_log_opts, log_opts,
+ .buf = log_buf,
+ .size = sizeof(log_buf),
+ .level = 1,
+ );
+ int fd;
+
+ opts->log_opts = &log_opts;
+ fd = bpf_link_create(prog_fd, target_fd, BPF_CGROUP_INET_EGRESS, opts);
+ if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+ close(fd);
+ return;
+ }
+
+ ASSERT_EQ(fd, err_exp, "err");
+ ASSERT_STREQ(log_buf, msg, "log_buf");
+ ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+}
+
+static void test_cgroup_link_create_log(int target_fd, __u32 flags, int err_exp, const char *msg)
+{
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = flags,
+ );
+ struct test_cgroup_link *skel;
+
+ skel = test_cgroup_link__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "test_cgroup_link__open_and_load"))
+ return;
+
+ check_cgroup_link_create_log(bpf_program__fd(skel->progs.egress), target_fd, &opts,
+ err_exp, msg);
+
+ test_cgroup_link__destroy(skel);
+}
+
+static void test_log_invalid_flags(void)
+{
+ const char *msg = "Invalid flags.\n";
+
+ test_cgroup_link_create_log(-1, BPF_F_ALLOW_OVERRIDE, -EINVAL, msg);
+}
+
+static void test_log_invalid_target_fd(void)
+{
+ const char *msg = "Invalid target_fd.\n";
+
+ test_cgroup_link_create_log(-1, 0, -EBADF, msg);
+}
+
+static void test_log_cgroup_attach(struct bpf_link_create_opts *opts,
+ int err_exp, const char *msg)
+{
+ struct test_cgroup_link *skel;
+ int cg;
+
+ cg = test__join_cgroup("/cgroup_link_log_attach");
+ if (!ASSERT_GE(cg, 0, "join_cgroup"))
+ return;
+
+ skel = test_cgroup_link__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "test_cgroup_link__open_and_load"))
+ goto out;
+
+ check_cgroup_link_create_log(bpf_program__fd(skel->progs.egress), cg, opts, err_exp, msg);
+ test_cgroup_link__destroy(skel);
+
+out:
+ close(cg);
+}
+
+static void test_log_relative_without_order(void)
+{
+ const char *msg = "Specify exactly one of BPF_F_BEFORE or BPF_F_AFTER.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = BPF_F_LINK,
+ );
+
+ test_log_cgroup_attach(&opts, -EINVAL, msg);
+}
+
+static void test_log_relative_both_orders(void)
+{
+ const char *msg = "Specify exactly one of BPF_F_BEFORE or BPF_F_AFTER.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = BPF_F_LINK | BPF_F_BEFORE | BPF_F_AFTER,
+ );
+
+ test_log_cgroup_attach(&opts, -EINVAL, msg);
+}
+
+static void test_log_relative_type_mismatch(void)
+{
+ const char *msg = "BPF_F_LINK is required for relative link attachment.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = BPF_F_BEFORE,
+ .cgroup.relative_fd = -1,
+ );
+
+ test_log_cgroup_attach(&opts, -EINVAL, msg);
+}
+
+static void test_log_invalid_relative_link_fd(void)
+{
+ const char *msg = "Invalid relative link FD.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = BPF_F_LINK | BPF_F_BEFORE,
+ .cgroup.relative_fd = -1,
+ );
+
+ test_log_cgroup_attach(&opts, -EBADF, msg);
+}
+
+static void test_log_invalid_relative_link_id(void)
+{
+ const char *msg = "Invalid relative link ID.\n";
+ /* Link ID zero cannot identify a link. */
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = BPF_F_LINK | BPF_F_ID | BPF_F_BEFORE,
+ );
+
+ test_log_cgroup_attach(&opts, -ENOENT, msg);
+}
+
+static void test_log_relative_anchor(__u32 flags, bool other_cgroup, int err_exp, const char *msg)
+{
+ int cg, other_cg = -1, anchor_fd = -1, prog_fd;
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .flags = flags,
+ );
+ struct test_cgroup_link *skel;
+
+ cg = test__join_cgroup("/cgroup_link_log_relative");
+ if (!ASSERT_GE(cg, 0, "join_cgroup"))
+ return;
+
+ if (other_cgroup) {
+ other_cg = test__join_cgroup("/cgroup_link_log_other");
+ if (!ASSERT_GE(other_cg, 0, "join_cgroup other"))
+ goto cleanup;
+ }
+
+ skel = test_cgroup_link__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "test_cgroup_link__open_and_load"))
+ goto cleanup;
+
+ prog_fd = bpf_program__fd(skel->progs.egress);
+ anchor_fd = bpf_link_create(prog_fd, cg, BPF_CGROUP_INET_EGRESS, NULL);
+ if (!ASSERT_GE(anchor_fd, 0, "bpf_link_create"))
+ goto destroy;
+
+ if (flags & BPF_F_LINK)
+ opts.cgroup.relative_fd = anchor_fd;
+ check_cgroup_link_create_log(prog_fd, other_cgroup ? other_cg : cg, &opts, err_exp, msg);
+
+ close(anchor_fd);
+
+destroy:
+ test_cgroup_link__destroy(skel);
+cleanup:
+ if (other_cg >= 0)
+ close(other_cg);
+ close(cg);
+}
+
+static void test_log_both_orders(void)
+{
+ const char *msg = "BPF_F_BEFORE and BPF_F_AFTER are mutually exclusive.\n";
+
+ test_log_relative_anchor(BPF_F_BEFORE | BPF_F_AFTER, false, -EINVAL, msg);
+}
+
+static void test_log_relative_preorder_mismatch(void)
+{
+ const char *msg = "Relative attachment BPF_F_PREORDER mismatch.\n";
+
+ test_log_relative_anchor(BPF_F_LINK | BPF_F_BEFORE | BPF_F_PREORDER, false, -EINVAL, msg);
+}
+
+static void test_log_relative_not_in_cgroup(void)
+{
+ const char *msg = "Relative attachment not found in target cgroup.\n";
+
+ test_log_relative_anchor(BPF_F_LINK | BPF_F_BEFORE, true, -ENOENT, msg);
+}
+
+static void test_log_revision_mismatch(void)
+{
+ const char *msg = "Cgroup revision mismatch: expected 2, actual 1.\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts,
+ .cgroup.expected_revision = 2,
+ );
+
+ test_log_cgroup_attach(&opts, -ESTALE, msg);
+}
+
+static void test_log_exclusive_attach(bool child, const char *msg)
+{
+ LIBBPF_OPTS(bpf_link_create_opts, opts);
+ int cg, child_cg = -1, prog_fd, err;
+ struct test_cgroup_link *skel;
+
+ cg = test__join_cgroup("/cgroup_link_log_exclusive");
+ if (!ASSERT_GE(cg, 0, "join_cgroup"))
+ return;
+
+ if (child) {
+ child_cg = test__join_cgroup("/cgroup_link_log_exclusive/child");
+ if (!ASSERT_GE(child_cg, 0, "join_child_cgroup"))
+ goto cleanup;
+ }
+
+ skel = test_cgroup_link__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "test_cgroup_link__open_and_load"))
+ goto cleanup;
+
+ prog_fd = bpf_program__fd(skel->progs.egress);
+
+ err = bpf_prog_attach(prog_fd, cg, BPF_CGROUP_INET_EGRESS, 0);
+ if (!ASSERT_OK(err, "bpf_prog_attach"))
+ goto destroy;
+
+ check_cgroup_link_create_log(prog_fd, child ? child_cg : cg, &opts, -EPERM, msg);
+
+ err = bpf_prog_detach2(prog_fd, cg, BPF_CGROUP_INET_EGRESS);
+ ASSERT_OK(err, "bpf_prog_detach2");
+
+destroy:
+ test_cgroup_link__destroy(skel);
+cleanup:
+ if (child_cg >= 0)
+ close(child_cg);
+ close(cg);
+}
+
+static void test_log_attachment_flags_conflict(void)
+{
+ const char *msg = "Attachment flags conflict with existing flags.\n";
+
+ test_log_exclusive_attach(false, msg);
+}
+
+static void test_log_hierarchy_disallows_attach(void)
+{
+ const char *msg = "Cgroup hierarchy does not allow attachment.\n";
+
+ test_log_exclusive_attach(true, msg);
+}
+
+#define BPF_CGROUP_MAX_PROGS 64
+
+static void test_log_too_many_programs(void)
+{
+ const char *msg = "Too many programs attached to cgroup (limit 64).\n";
+ LIBBPF_OPTS(bpf_link_create_opts, opts);
+ int cg, prog_fd, err, i, nr_links = 0;
+ int link_fds[BPF_CGROUP_MAX_PROGS];
+ struct test_cgroup_link *skel;
+
+ cg = test__join_cgroup("/cgroup_link_log_limit");
+ if (!ASSERT_GE(cg, 0, "join_cgroup"))
+ return;
+
+ skel = test_cgroup_link__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "test_cgroup_link__open_and_load"))
+ goto cleanup;
+
+ prog_fd = bpf_program__fd(skel->progs.egress);
+
+ for (i = 0; i < ARRAY_SIZE(link_fds); i++) {
+ err = bpf_link_create(prog_fd, cg, BPF_CGROUP_INET_EGRESS, NULL);
+ if (!ASSERT_GE(err, 0, "bpf_link_create"))
+ goto destroy;
+
+ link_fds[nr_links++] = err;
+ }
+
+ check_cgroup_link_create_log(prog_fd, cg, &opts, -E2BIG, msg);
+
+destroy:
+ for (i = 0; i < nr_links; i++)
+ close(link_fds[i]);
+ test_cgroup_link__destroy(skel);
+cleanup:
+ close(cg);
+}
+
+void test_cgroup_link_log(void)
+{
+ if (test__start_subtest("invalid_flags"))
+ test_log_invalid_flags();
+ if (test__start_subtest("invalid_target_fd"))
+ test_log_invalid_target_fd();
+ if (test__start_subtest("relative_without_order"))
+ test_log_relative_without_order();
+ if (test__start_subtest("relative_both_orders"))
+ test_log_relative_both_orders();
+ if (test__start_subtest("relative_type_mismatch"))
+ test_log_relative_type_mismatch();
+ if (test__start_subtest("invalid_relative_link_fd"))
+ test_log_invalid_relative_link_fd();
+ if (test__start_subtest("invalid_relative_link_id"))
+ test_log_invalid_relative_link_id();
+ if (test__start_subtest("both_orders"))
+ test_log_both_orders();
+ if (test__start_subtest("relative_preorder_mismatch"))
+ test_log_relative_preorder_mismatch();
+ if (test__start_subtest("relative_not_in_cgroup"))
+ test_log_relative_not_in_cgroup();
+ if (test__start_subtest("revision_mismatch"))
+ test_log_revision_mismatch();
+ if (test__start_subtest("attachment_flags_conflict"))
+ test_log_attachment_flags_conflict();
+ if (test__start_subtest("hierarchy_disallows_attach"))
+ test_log_hierarchy_disallows_attach();
+ if (test__start_subtest("too_many_programs"))
+ test_log_too_many_programs();
+}
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread