Skip to contentSkip to conversation

bpf: Add log support for tracing link_create

PATCH In review Leon Hwang 5 participants 19 messages Nov 23 – Nov 24

Subsystems (12)
lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

v3 cover bpf: Add log support for tracing link_create

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

This is the second series for the plan, that adds syscall common attrs support for LINK_CREATE of tracing prog and cgroup prog.

BTW, address Andrii's review [1] in patch 5:
1. Change if (!OPTS_ZEROED(opts, iter_info_len)) to if (!OPTS_ZEROED(opts, log_opts)).
2. Add comment to iter_info and iter_info_len that they are deprecated, and use iter.info and iter.len instead.

Links:
[1] https://lore.kernel.org/bpf/CAEf4BzbW4zwL-9j8e2B6KNXVNwsfWyMSAo_RsGCqv-nu_yGhTg@mail.gmail.com/

Changes:
v2 -> v3:
* Add log support for bpf_check_attach_btf_id_multi().
* Add more selftests to cover more logs for link creation failures.
* Update some log messages in patch 1. (Alexei)
* Add log for dst attach type check in bpf_check_attach_target(). (Alexei)
* Update some log messages in patch 2. (Alexei)
* Pass 'log' to btf_attach_func_proto() and btf_distill_func_proto() in bpf_check_attach_btf_id_multi(). (Alexei)
* Add a log msg in check_dup_ids() when dup ids. (Alexei)
* Add a log msg in bpf_tracing_multi_attach() when it returns -EOPNOTSUPP. (Alexei)
* Swap function names between iter_info and iter_info_len selftests. (Sashiko)
* v2: https://lore.kernel.org/bpf/[email protected]/

v1 -> v2:
* Add log_opts support for bpf_program__attach_iter() API in libbpf.
* Add test to verify the log for freplace max_ctx_offset case.
* Update a subtest to verify the log_opts in bpf_program__attach_iter() API.
* Add log for freplace max_ctx_offset case. (bot+bpf-ci)
* Drop unnecessary bpf_verifier.h include. (bot+bpf-ci)
* Change log to "Invalid tracing attach_type.\n" when the link attach_type is not prog->expected_attach_type. (bot+bpf-ci)
* Change BPF_XDP to BPF_TRACE_FEXIT in tracing link_create test. (bot+bpf-ci)
* Drop dead iter.len check in libbpf. (bot+bpf-ci, Sashiko)
* Add OPTS_ZEROED(opts, iter) check. (bot+bpf-ci, Sashiko)
* Swap log positions of iter_info and iter_info_len. (Sashiko)
* Update bpf_program__attach_iter() to use iter.info and iter.len. (Sashiko)
* v1: https://lore.kernel.org/bpf/[email protected]/

Leon Hwang (9):
bpf: Add log support for tracing link_create
bpf: Add log support for tracing_multi link_create
bpf: Add log support for iter link_create
bpf: Add log support for cgroup prog link_create
libbpf: Add log_opts support for BPF_TRACE_ITER selftests/bpf: Test tracing link_create logs selftests/bpf: Test tracing_multi link_create logs selftests/bpf: Verify iter link_create logs selftests/bpf: Verify cgroup prog link_create logs

 include/linux/bpf-cgroup.h                    |   6 +-
 include/linux/bpf.h                           |   9 +-
 include/linux/bpf_verifier.h                  |   3 +-
 include/linux/trace_events.h                  |   7 +-
 kernel/bpf/bpf_iter.c                         |  27 +-
 kernel/bpf/cgroup.c                           |  76 +-
 kernel/bpf/syscall.c                          |  30 +-
 kernel/bpf/trampoline.c                       |   5 +-
 kernel/bpf/verifier.c                         |  51 +-
 kernel/trace/bpf_trace.c                      |  27 +-
 tools/lib/bpf/bpf.c                           |  23 +-
 tools/lib/bpf/bpf.h                           |   8 +
 tools/lib/bpf/libbpf.c                        |   5 +-
 tools/lib/bpf/libbpf.h                        |   3 +-
 .../selftests/bpf/prog_tests/cgroup_link.c    | 324 ++++++++
 .../selftests/bpf/prog_tests/iter_link.c      | 156 ++++
 .../selftests/bpf/prog_tests/tracing_link.c   | 741 ++++++++++++++++++
 .../selftests/bpf/progs/tracing_link_ctx.c    |  17 +
 .../selftests/bpf/progs/tracing_link_multi.c  |  36 +
 .../selftests/bpf/test_kmods/bpf_testmod.c    |   5 +
 20 files changed, 1484 insertions(+), 75 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/iter_link.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tracing_link.c
 create mode 100644 tools/testing/selftests/bpf/progs/tracing_link_ctx.c
 create mode 100644 tools/testing/selftests/bpf/progs/tracing_link_multi.c
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v3 1/9 bpf: Add log support for tracing link_create

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.

---
 kernel/bpf/syscall.c  | 22 +++++++++++++++++-----
 kernel/bpf/verifier.c |  6 ++++--
 2 files changed, 21 insertions(+), 7 deletions(-)
kernel/bpf/syscall.c +17 -5
index ec8e78792911..80504011403f 100644
@@ -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:
kernel/bpf/verifier.c +4 -2
index dea18409f480..58d3588ffc84 100644
@@ -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) {
 			/*
Signature · 1 line
2.55.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] bpf: non-UAPI field name tgt_fd logged in bpf_tracing_prog_attach() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] bpf: non-UAPI field name tgt_fd logged in bpf_tracing_prog_attach()
--

commit 476f541041ef9d87251f0598129c4def280d1320
Author: Leon Hwang <[email protected]>

bpf: Add log support for tracing link_create

This commit adds verifier log reporting to tracing BPF_LINK_CREATE for attach parameter validation, reattachment checks, and target compatibility checks in bpf_check_attach_target().

Quoted 24 lines
kernel/bpf/syscall.c +3 -1
index ec8e787929112..80504011403f6 100644
@@ -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.

kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3738

Quoted 10 lines
kernel/bpf/syscall.c +2 -0
 		 */
 		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");

[Severity: Low]
As reported by Alexei Starovoitov on v2 [1], should this log "Invalid target_fd.\n" to match the link_create.target_fd UAPI field name used by the other log messages in bpf_tracing_prog_attach()?

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Acked-by
#3 lore raw
Quoted 46 lines · On Mon, Nov 23, 2026 at 03:38:47PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] bpf: non-UAPI field name tgt_fd logged in bpf_tracing_prog_attach()
--

commit 476f541041ef9d87251f0598129c4def280d1320
Author: Leon Hwang <[email protected]>

bpf: Add log support for tracing link_create

This commit adds verifier log reporting to tracing BPF_LINK_CREATE for attach parameter validation, reattachment checks, and target compatibility checks in bpf_check_attach_target().

Quoted 31 lines
kernel/bpf/syscall.c +4 -1
index ec8e787929112..80504011403f6 100644
@@ -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;
 		}
 
Quoted 8 lines

kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3738

Quoted 3 lines
kernel/bpf/syscall.c +1 -0
 		tgt_prog = bpf_prog_get(tgt_prog_fd);
 		if (IS_ERR(tgt_prog)) {
+			bpf_log(log, "Invalid tgt_fd.\n");

[Severity: Low]
As reported by Alexei Starovoitov on v2 [1], should this log "Invalid target_fd.\n" to match the link_create.target_fd UAPI field name used by the other log messages in bpf_tracing_prog_attach()?

with this fixed, the tracing_multi changes lgtm

thanks,
jirka

author
#4 lore raw
Quoted 1 line · Jiri Olsa

On Mon, Nov 23, 2026 at 03:38:47PM +0000, [email protected] wrote:

[...]

Quoted 8 lines
Quoted 8 lines

kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3738

Quoted 3 lines
kernel/bpf/syscall.c +1 -0
 		tgt_prog = bpf_prog_get(tgt_prog_fd);
 		if (IS_ERR(tgt_prog)) {
+			bpf_log(log, "Invalid tgt_fd.\n");

[Severity: Low]
As reported by Alexei Starovoitov on v2 [1], should this log "Invalid target_fd.\n" to match the link_create.target_fd UAPI field name used by the other log messages in bpf_tracing_prog_attach()?

Will update it to "target_fd.\n".

Quoted 4 lines

with this fixed, the tracing_multi changes lgtm

Thanks for your review.

Leon

author
#5 lore raw

PATCH v3 2/9 bpf: Add log support for tracing_multi link_create

In reply to Leon Hwang

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.

---
 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(-)
include/linux/bpf.h +4 -2
index 4e9a4d15af0e..a1ba3f54ba6a 100644
@@ -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;
 }
include/linux/bpf_verifier.h +2 -1
index 151b5781606d..f86b95da1b1e 100644
@@ -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() */
include/linux/trace_events.h +5 -2
index 321f2f3a6c8e..bb825ea5d09c 100644
@@ -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;
 }
kernel/bpf/syscall.c +1 -1
index 80504011403f..d3ab9c695129 100644
@@ -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,
kernel/bpf/trampoline.c +3 -2
index 5f2b792de8f9..96454443441b 100644
@@ -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;
 
kernel/bpf/verifier.c +35 -10

92 lines not on this page · raw message

kernel/trace/bpf_trace.c +20 -7
index 4f1be6cd2e13..e5dd5c4dbf44 100644
@@ -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;
 }
 
Signature · 1 line
2.55.0
author
#6 lore raw

PATCH v3 3/9 bpf: Add log support for iter link_create

In reply to Leon Hwang

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.

---
 include/linux/bpf.h   |  3 ++-
 kernel/bpf/bpf_iter.c | 27 +++++++++++++++++++++------
 kernel/bpf/syscall.c  |  2 +-
 3 files changed, 24 insertions(+), 8 deletions(-)
include/linux/bpf.h +2 -1
index a1ba3f54ba6a..e6d6a34a2818 100644
@@ -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);
kernel/bpf/bpf_iter.c +21 -6
index f0b29a170935..4a9e08fb8e75 100644
@@ -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)
kernel/bpf/syscall.c +1 -1
index d3ab9c695129..f485e68a14ee 100644
@@ -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))
Signature · 1 line
2.55.0
author
#7 lore raw

PATCH v3 4/9 bpf: Add log support for cgroup prog link_create

In reply to Leon Hwang

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.

---
 include/linux/bpf-cgroup.h |  6 ++-
 kernel/bpf/cgroup.c        | 76 +++++++++++++++++++++++++++-----------
 kernel/bpf/syscall.c       |  4 +-
 3 files changed, 60 insertions(+), 26 deletions(-)
include/linux/bpf-cgroup.h +4 -2
index dfa7a75507c9..0b63c2cc1399 100644
@@ -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;
 }
kernel/bpf/cgroup.c +54 -22

201 lines not on this page · raw message

kernel/bpf/syscall.c +2 -2
index f485e68a14ee..56b3f42fded0 100644
@@ -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
Signature · 1 line
2.55.0
#8 lore raw

Re: [PATCH bpf-next v3 4/9] 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…
Quoted 1 line

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.

Quoted 3 lines
kernel/bpf/cgroup.c +0 -0

[ ... ]

Quoted 10 lines
kernel/bpf/cgroup.c +4 -1
@@ -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;
+	}

[ ... ]

kernel/bpf/cgroup.c static int cgroup_bpf_attach(struct cgroup *cgrp, · line 1172

Quoted 7 lines
kernel/bpf/cgroup.c +1 -1
@@ -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

author
#9 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Add log support for cgroup prog link_create

Quoted 55 lines · On 24/11/26 00:04, [email protected] wrote:
Quoted 1 line

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.

Quoted 3 lines
kernel/bpf/cgroup.c +0 -0

[ ... ]

Quoted 10 lines
kernel/bpf/cgroup.c +4 -1
@@ -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;
+	}

[ ... ]

kernel/bpf/cgroup.c static int cgroup_bpf_attach(struct cgroup *cgrp, · line 1172

Quoted 7 lines
kernel/bpf/cgroup.c +1 -1
@@ -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.

Quoted 8 lines

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()?

I think it's okay to call bpf_log() inside those locks' critical section, even though copy_to_user() could unlikely trigger page fault.

I didn't have good idea to address such concern. Probably, in cgroup_bpf_attach(), prepare a local buffer before cgroup_bpf_lock_hierarchy(), pass the buffer to __cgroup_bpf_attach() in order to store the log messages, then call bpf_log() after cgroup_bpf_unlock_hierarchy() if __cgroup_bpf_attach() returns error.

Thanks,
Leon

author
#10 lore raw

PATCH v3 5/9 libbpf: Add log_opts support for BPF_TRACE_ITER

In reply to Leon Hwang

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.

---
 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(-)
tools/lib/bpf/bpf.c +17 -6
index 38dcb31e8c0e..ad6b356cc48c 100644
@@ -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;
 	}
tools/lib/bpf/bpf.h +8 -0
index fe3b7fd8512b..2ac143fd1102 100644
@@ -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;
 };
tools/lib/bpf/libbpf.c +3 -2
index 1efc0dd37b2e..612eba0ff4f0 100644
@@ -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) {
tools/lib/bpf/libbpf.h +2 -1
index 49f1120f4021..56300be81016 100644
@@ -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,
Signature · 1 line
2.55.0
author
#11 lore raw

PATCH v3 6/9 selftests/bpf: Test tracing link_create logs

In reply to Leon Hwang

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.

---
 .../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
tools/testing/selftests/bpf/prog_tests/tracing_link.c new file +392 -0
new file mode 100644
index 000000000000..9e582b2fae54
@@ -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();
+}
tools/testing/selftests/bpf/progs/tracing_link_ctx.c new file +17 -0
new file mode 100644
index 000000000000..940a7d74b7e3
@@ -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";
Signature · 1 line
2.55.0
author
#12 lore raw

PATCH v3 7/9 selftests/bpf: Test tracing_multi link_create logs

In reply to Leon Hwang

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.

---
 .../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
tools/testing/selftests/bpf/prog_tests/tracing_link.c +349 -0
index 9e582b2fae54..f958f084c6f0 100644
@@ -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();
+}
tools/testing/selftests/bpf/progs/tracing_link_multi.c new file +36 -0
new file mode 100644
index 000000000000..11380c004da0
@@ -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;
+}
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +5 -0
index 1fbc0061cc24..4b011330a5a1 100644
@@ -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;
Signature · 1 line
2.55.0
#13 lore raw

Re: [PATCH bpf-next v3 7/9] 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.
Quoted 1 line

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.

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tracing_link.c +0 -0

[ ... ]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tracing_link.c +5 -0
+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;

tools/testing/selftests/bpf/prog_tests/tracing_link.c void test_tracing_link_log(void) · line 583

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tracing_link.c +10 -0
+
+	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?

tools/testing/selftests/bpf/prog_tests/tracing_link.c void test_tracing_link_log(void) · line 502

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tracing_link.c +3 -0
+
+	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

#14 lore raw

Re: [PATCH bpf-next v3 7/9] selftests/bpf: Test tracing_multi link_create logs

Quoted 51 lines · On Mon, Nov 23, 2026 at 04:04:41PM +0000, [email protected] wrote:
Quoted 1 line

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.

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tracing_link.c +0 -0

[ ... ]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tracing_link.c +5 -0
+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;

tools/testing/selftests/bpf/prog_tests/tracing_link.c void test_tracing_link_log(void) · line 583

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tracing_link.c +10 -0
+
+	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.

Quoted 7 lines

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

author
#15 lore raw

PATCH v3 8/9 selftests/bpf: Verify iter link_create logs

In reply to Leon Hwang

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.

---
 .../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
tools/testing/selftests/bpf/prog_tests/iter_link.c new file +156 -0
new file mode 100644
index 000000000000..d7904b4aa2b1
@@ -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();
+}
Signature · 1 line
2.55.0
author
#16 lore raw

PATCH v3 9/9 selftests/bpf: Verify cgroup prog link_create logs

In reply to Leon Hwang

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.

---
 .../selftests/bpf/prog_tests/cgroup_link.c    | 324 ++++++++++++++++++
 1 file changed, 324 insertions(+)
tools/testing/selftests/bpf/prog_tests/cgroup_link.c +324 -0
index 15093a69510e..5a27d3a6a26c 100644
@@ -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();
+}
Signature · 1 line
2.55.0
#17 lore raw
In reply to Leon Hwang
Quoted 6 lines · Leon Hwang
 .../selftests/bpf/prog_tests/cgroup_link.c    | 324 ++++++++
 .../selftests/bpf/prog_tests/iter_link.c      | 156 ++++
 .../selftests/bpf/prog_tests/tracing_link.c   | 741 ++++++++++++++++++
 .../selftests/bpf/progs/tracing_link_ctx.c    |  17 +
 .../selftests/bpf/progs/tracing_link_multi.c  |  36 +
 .../selftests/bpf/test_kmods/bpf_testmod.c    |   5 +

1279 lines of selftests for log messages ?
This is way too much.

pw-bot: cr

One row per attach path is enough. It checks that the log gets there. The base kernel leaves the log empty, so all four rows fail on it. Something like this:

tools/testing/selftests/bpf/prog_tests/link_log.c new file +72 -0
new file mode 100644
index 0000000..4f7e71a
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+
+/* One row per attach path. It checks that the log gets there. */
+static const struct {
+	const char *name;
+	enum bpf_prog_type type;
+	enum bpf_attach_type attach_type;
+	const char *func;
+	struct bpf_link_create_opts opts;
+	int err;
+	const char *msg;
+} tests[] = {
+	{ "tracing", BPF_PROG_TYPE_TRACING, BPF_TRACE_FENTRY, "bpf_fentry_test1",
+	  { .target_btf_id = 1 }, -EINVAL, "target_btf_id" },
+#ifdef __x86_64__
+	{ "tracing_multi", BPF_PROG_TYPE_TRACING, BPF_TRACE_FENTRY_MULTI, NULL,
+	  { .tracing_multi = { .ids = (const __u32[]){ ~0U }, .cnt = 1 } },
+	  -EINVAL, "4294967295" },
+#endif
+	{ "iter", BPF_PROG_TYPE_TRACING, BPF_TRACE_ITER, "bpf_map",
+	  { .iter.len = 4 }, -EINVAL, "iter_info_len" },
+	{ "cgroup", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_EGRESS, NULL,
+	  { .cgroup.expected_revision = 2 }, -ESTALE, "revision" },
+};
+
+static void run_test(int i, int cg)
+{
+	const struct bpf_insn insns[] = {
+		BPF_MOV64_IMM(BPF_REG_0, 0),
+		BPF_EXIT_INSN(),
+	};
+	struct bpf_link_create_opts opts = tests[i].opts;
+	char buf[128] = {};
+	LIBBPF_OPTS(bpf_log_opts, log_opts, .buf = buf, .size = sizeof(buf), .level = 1);
+	LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
+		    .expected_attach_type = tests[i].attach_type);
+	int prog_fd, fd;
+
+	if (tests[i].func)
+		load_opts.attach_btf_id =
+			libbpf_find_vmlinux_btf_id(tests[i].func, tests[i].attach_type);
+	prog_fd = bpf_prog_load(tests[i].type, NULL, "GPL", insns, ARRAY_SIZE(insns),
+				&load_opts);
+	if (!ASSERT_OK_FD(prog_fd, "bpf_prog_load"))
+		return;
+
+	opts.sz = sizeof(opts);
+	opts.log_opts = &log_opts;
+	fd = bpf_link_create(prog_fd, tests[i].type == BPF_PROG_TYPE_CGROUP_SKB ? cg : 0,
+			     tests[i].attach_type, &opts);
+	if (fd >= 0)
+		close(fd);
+	ASSERT_EQ(fd, tests[i].err, "err");
+	ASSERT_HAS_SUBSTR(buf, tests[i].msg, "log");
+	ASSERT_EQ(log_opts.true_size, strlen(buf) + 1, "true_size");
+	close(prog_fd);
+}
+
+void test_link_log(void)
+{
+	int i, cg;
+
+	cg = test__join_cgroup("/link_log");
+	if (!ASSERT_OK_FD(cg, "join_cgroup"))
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(tests); i++)
+		if (test__start_subtest(tests[i].name))
+			run_test(i, cg);
+	close(cg);
+}
author
#18 lore raw
Quoted 8 lines · Alexei Starovoitov
Quoted 6 lines · Leon Hwang
 .../selftests/bpf/prog_tests/cgroup_link.c    | 324 ++++++++
 .../selftests/bpf/prog_tests/iter_link.c      | 156 ++++
 .../selftests/bpf/prog_tests/tracing_link.c   | 741 ++++++++++++++++++
 .../selftests/bpf/progs/tracing_link_ctx.c    |  17 +
 .../selftests/bpf/progs/tracing_link_multi.c  |  36 +
 .../selftests/bpf/test_kmods/bpf_testmod.c    |   5 +
Quoted 8 lines

1279 lines of selftests for log messages ?
This is way too much.

pw-bot: cr

One row per attach path is enough. It checks that the log gets there. The base kernel leaves the log empty, so all four rows fail on it. Something like this:

Okay, will update the selftests like this.

Thanks,
Leon

Quoted 1 line

[...]