Skip to contentSkip to conversation

bpf: Add syscall common attrs support for xdp link_create

PATCH Applied Leon Hwang 5 participants 15 messages Oct 26 – Oct 30

Subsystems (9)
lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

v5 cover bpf: Add syscall common attrs support for xdp link_create

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

This is the first series for the plan, that adds syscall common attrs support for xdp LINK_CREATE.

Thereafter, retire the tracepoint bpf_xdp_link_attach_failed, because the underlying driver's error can be back-propagated to user space via the syscall common attrs.

Changes:
v4 -> v5:
* Pass extack instead of errmsg pointer to bpf_xdp_link_attach(). (Alexei)
* Move cmd as first argument in __sys_bpf_fd() helper. (bot+bpf-ci)
* v4: https://lore.kernel.org/bpf/[email protected]/

v3 -> v4:
* Pass errmsg pointer to bpf_xdp_link_attach(). (Jakub)
* Place log_opts at the slot of iter_info in libbpf. (Andrii)
* v3: https://lore.kernel.org/bpf/[email protected]/

v2 -> v3:
* Drop bpf_log_attr_report_size() helper. (Alexei)
* Drop 2nd write for log_true_size and the put_prog label. (Alexei)
* v2: https://lore.kernel.org/bpf/[email protected]/

v1 -> v2:
* Add log for invalid prog type in link_create().
* Reject log_opts for fallback bpf_raw_tracepoint_open() in libbpf.
* Report log_true_size as 0 before creating concrete link, and report the actual log_true_size when fail to create the concrete link. (Sashiko)
* Add stub bpf_log() when CONFIG_BPF_SYSCALL is not set. (bots)
* Check 'extack._msg' before adding 'extack._msg' to log buffer. (Sashiko, bot+bpf-ci)
* Change __bpf_sys_fd to __sys_bpf_fd in the subject and body of patch 4. (bot+bpf-ci)
* Move OPTS_VALID(opts, bpf_link_create_opts) to __sys_bpf_fd(). (Sashiko)
* Move bpf_link_create_log to the LIBBPF_1.9.0 section. (Sashiko)
* Update patch msg with a concrete case in patch 5. (bot+bpf-ci)
* Factor out helpers to simplify the tests code. (bot+bpf-ci)
* v1: https://lore.kernel.org/bpf/[email protected]/

Leon Hwang (6):
bpf: Add syscall common attrs support for link_create
bpf: Add syscall common attrs support for xdp link_create
bpf: Retire tracepoint bpf_xdp_link_attach_failed
libbpf: Factor out __sys_bpf_fd helper
libbpf: Add log_opts support for bpf_link_create selftests/bpf: Add tests to verify link_create logs

 include/linux/netdevice.h                     |   3 +-
 include/trace/events/xdp.h                    |  17 ---
 kernel/bpf/syscall.c                          |  41 +++++--
 net/core/dev.c                                |   8 +-
 tools/lib/bpf/bpf.c                           |  61 +++++++----
 tools/lib/bpf/bpf.h                           |   5 +-
 .../selftests/bpf/prog_tests/xdp_attach.c     |  65 -----------
 .../selftests/bpf/prog_tests/xdp_link.c       | 102 ++++++++++++++++++
 .../bpf/progs/test_xdp_attach_fail.c          |  54 ----------
 9 files changed, 187 insertions(+), 169 deletions(-)
 delete mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v5 1/6 bpf: Add syscall common attrs support for link_create

Similar to the syscall common attrs support for map_create [1], add the syscall common attrs support for link_create.

Create and finalize the internal verifier log instance.

Then, report the error messages for invalid prog fd, invalid attach type, and invalid prog type.

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

---
 kernel/bpf/syscall.c | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)
kernel/bpf/syscall.c +28 -7
index 90bc7230afa8..db870e795d04 100644
@@ -5862,8 +5862,11 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
 }
 
 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.path_fd
-static int link_create(union bpf_attr *attr, bpfptr_t uattr)
+static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_attr *attr_common,
+		       bpfptr_t uattr_common, u32 size_common)
 {
+	struct bpf_verifier_log *log;
+	struct bpf_log_attr attr_log;
 	struct bpf_prog *prog;
 	int ret;
 
@@ -5873,14 +5876,24 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 	if (attr->link_create.attach_type == BPF_STRUCT_OPS)
 		return bpf_struct_ops_link_create(attr);
 
+	log = bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, size_common);
+	if (IS_ERR(log))
+		return PTR_ERR(log);
+
 	prog = bpf_prog_get(attr->link_create.prog_fd);
-	if (IS_ERR(prog))
-		return PTR_ERR(prog);
+	if (IS_ERR(prog)) {
+		bpf_log(log, "Invalid prog_fd.\n");
+		ret = PTR_ERR(prog);
+		prog = NULL;
+		goto out;
+	}
 
 	ret = bpf_prog_attach_check_attach_type(prog,
 						attr->link_create.attach_type);
-	if (ret)
+	if (ret) {
+		bpf_log(log, "Invalid attach_type.\n");
 		goto out;
+	}
 
 	switch (prog->type) {
 	case BPF_PROG_TYPE_CGROUP_SKB:
@@ -5959,12 +5972,20 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 			ret = bpf_uprobe_multi_link_attach(attr, prog);
 		break;
 	default:
+		bpf_log(log, "Invalid prog type.\n");
 		ret = -EINVAL;
 	}
 
 out:
-	if (ret < 0)
-		bpf_prog_put(prog);
+	if (ret < 0) {
+		int err;
+
+		err = bpf_log_attr_finalize(&attr_log, log);
+		ret = err ? err : ret;
+		if (prog)
+			bpf_prog_put(prog);
+	}
+	kfree(log);
 	return ret;
 }
 
@@ -6536,7 +6557,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
 		err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
 		break;
 	case BPF_LINK_CREATE:
-		err = link_create(&attr, uattr);
+		err = link_create(&attr, uattr, &attr_common, uattr_common, size_common);
 		break;
 	case BPF_LINK_UPDATE:
 		err = link_update(&attr);
Signature · 1 line
2.55.0
author
#4 lore raw

PATCH v5 2/6 bpf: Add syscall common attrs support for xdp link_create

In reply to Leon Hwang

With the syscall common attrs support for link_create, add syscall common attrs support for xdp link_create in order to report the invalid target ifindex error and the underlying driver's error.

Instead of passing bpf log to bpf_xdp_link_attach(), pass an extack to receive the underlying driver's error message. If pass bpf log to bpf_xdp_link_attach(), it will introduce bpf_verifier.h dependency for net/core/dev.c, which is not acceptable for maintainability.

---
 include/linux/netdevice.h | 3 ++-
 kernel/bpf/syscall.c      | 6 +++++-
 net/core/dev.c            | 9 +++++----
 3 files changed, 12 insertions(+), 6 deletions(-)
include/linux/netdevice.h +2 -1
index c0ae442c4344..6f7fec24ca5f 100644
@@ -4440,7 +4440,8 @@ struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *d
 struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 				    struct netdev_queue *txq, int *ret);
 
-int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
+int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+			struct netlink_ext_ack *extack);
 u8 dev_xdp_prog_count(struct net_device *dev);
 int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf);
 int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf);
kernel/bpf/syscall.c +5 -1
index db870e795d04..ca72b35cdcc9 100644
@@ -5865,6 +5865,7 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
 static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_attr *attr_common,
 		       bpfptr_t uattr_common, u32 size_common)
 {
+	struct netlink_ext_ack extack = {};
 	struct bpf_verifier_log *log;
 	struct bpf_log_attr attr_log;
 	struct bpf_prog *prog;
@@ -5944,7 +5945,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
 		break;
 #ifdef CONFIG_NET
 	case BPF_PROG_TYPE_XDP:
-		ret = bpf_xdp_link_attach(attr, prog);
+		ret = bpf_xdp_link_attach(attr, prog, &extack);
 		break;
 	case BPF_PROG_TYPE_SCHED_CLS:
 		if (attr->link_create.attach_type == BPF_TCX_INGRESS ||
@@ -5980,6 +5981,9 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
 	if (ret < 0) {
 		int err;
 
+		if (extack._msg)
+			bpf_log(log, "%s\n", extack._msg);
+
 		err = bpf_log_attr_finalize(&attr_log, log);
 		ret = err ? err : ret;
 		if (prog)
net/core/dev.c +5 -4
index 1a09e7869118..4aa55259d5eb 100644
@@ -10882,11 +10882,11 @@ static const struct bpf_link_ops bpf_xdp_link_lops = {
 	.update_prog = bpf_xdp_link_update,
 };
 
-int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+			struct netlink_ext_ack *extack)
 {
 	struct net *net = current->nsproxy->net_ns;
 	struct bpf_link_primer link_primer;
-	struct netlink_ext_ack extack = {};
 	struct bpf_xdp_link *link;
 	struct net_device *dev;
 	int err, fd;
@@ -10895,6 +10895,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	dev = dev_get_by_index(net, attr->link_create.target_ifindex);
 	if (!dev) {
 		rtnl_unlock();
+		NL_SET_ERR_MSG(extack, "Invalid target_ifindex.");
 		return -EINVAL;
 	}
 
@@ -10916,14 +10917,14 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	}
 
 	netdev_lock_ops(dev);
-	err = dev_xdp_attach_link(dev, &extack, link);
+	err = dev_xdp_attach_link(dev, extack, link);
 	netdev_unlock_ops(dev);
 	rtnl_unlock();
 
 	if (err) {
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
-		trace_bpf_xdp_link_attach_failed(extack._msg);
+		trace_bpf_xdp_link_attach_failed(extack->_msg);
 		goto out_put_dev;
 	}
 
Signature · 1 line
2.55.0
author
#6 lore raw

PATCH v5 3/6 bpf: Retire tracepoint bpf_xdp_link_attach_failed

In reply to Leon Hwang

In the previous patch, it has a proper way to back-propagate the underlying driver error message to user space via the BPF syscall common attributes.

The tracepoint bpf_xdp_link_attach_failed seems redundant to report the error message. And, the tracepoint doesn't report the ifindex alongside the error message [1].

So, retire the tracepoint bpf_xdp_link_attach_failed and its selftest.

According to the tracepoint change discussion [2], please revert this patch if users complain about the disappeared tracepoint bpf_xdp_link_attach_failed in the future.

Links:
[1] https://lore.kernel.org/bpf/CAFk-A4mE9Jweo2hfX7y_85xbPyt0FqpMT1EvqX1OcYZ=LTLgRA@mail.gmail.com/
[2] https://lore.kernel.org/linux-mm/CY8PR11MB7134346A3E4BB28ECA28D6E989132@CY8PR11MB7134.namprd11.prod.outlook.com/

---
 include/trace/events/xdp.h                    | 17 -----
 net/core/dev.c                                |  1 -
 .../selftests/bpf/prog_tests/xdp_attach.c     | 65 -------------------
 .../bpf/progs/test_xdp_attach_fail.c          | 54 ---------------
 4 files changed, 137 deletions(-)
 delete mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
include/trace/events/xdp.h +0 -17
index 18c0ac514fcb..f1d4d0702884 100644
@@ -362,23 +362,6 @@ TRACE_EVENT(mem_connect,
 	)
 );
 
-TRACE_EVENT(bpf_xdp_link_attach_failed,
-
-	TP_PROTO(const char *msg),
-
-	TP_ARGS(msg),
-
-	TP_STRUCT__entry(
-		__string(msg, msg)
-	),
-
-	TP_fast_assign(
-		__assign_str(msg);
-	),
-
-	TP_printk("errmsg=%s", __get_str(msg))
-);
-
 #endif /* _TRACE_XDP_H */
 
 #include <trace/define_trace.h>
net/core/dev.c +0 -1
index 4aa55259d5eb..d259953fceaa 100644
@@ -10924,7 +10924,6 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
 	if (err) {
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
-		trace_bpf_xdp_link_attach_failed(extack->_msg);
 		goto out_put_dev;
 	}
 
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +0 -65

80 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c deleted +0 -54

54 lines not on this page · raw message

Signature · 1 line
2.55.0
author
#7 lore raw

PATCH v5 4/6 libbpf: Factor out __sys_bpf_fd helper

In reply to Leon Hwang

In the next patch, it also needs to use the extended BPF syscall to receive the reported log.

So, factor out __sys_bpf_fd helper.

No functional changes intended.

---
 tools/lib/bpf/bpf.c | 41 +++++++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 16 deletions(-)
tools/lib/bpf/bpf.c +25 -16
index b49822d212ae..aea534764dfe 100644
@@ -203,6 +203,30 @@ int bump_rlimit_memlock(void)
 	return 0;
 }
 
+static int __sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr, const size_t attr_sz,
+			struct bpf_log_opts *log_opts)
+{
+	const size_t attr_common_sz = sizeof(struct bpf_common_attr);
+	struct bpf_common_attr attr_common;
+	int fd;
+
+	if (!OPTS_VALID(log_opts, bpf_log_opts))
+		return libbpf_err(-EINVAL);
+
+	if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) {
+		memset(&attr_common, 0, attr_common_sz);
+		attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL));
+		attr_common.log_size = OPTS_GET(log_opts, size, 0);
+		attr_common.log_level = OPTS_GET(log_opts, level, 0);
+		fd = sys_bpf_ext_fd(cmd, attr, attr_sz, &attr_common, attr_common_sz);
+		OPTS_SET(log_opts, true_size, attr_common.log_true_size);
+	} else {
+		fd = sys_bpf_fd(cmd, attr, attr_sz);
+		OPTS_SET(log_opts, true_size, 0);
+	}
+	return fd;
+}
+
 int bpf_map_create(enum bpf_map_type map_type,
 		   const char *map_name,
 		   __u32 key_size,
@@ -211,8 +235,6 @@ int bpf_map_create(enum bpf_map_type map_type,
 		   const struct bpf_map_create_opts *opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, excl_prog_hash_size);
-	const size_t attr_common_sz = sizeof(struct bpf_common_attr);
-	struct bpf_common_attr attr_common;
 	struct bpf_log_opts *log_opts;
 	union bpf_attr attr;
 	int fd;
@@ -248,20 +270,7 @@ int bpf_map_create(enum bpf_map_type map_type,
 	attr.excl_prog_hash_size = OPTS_GET(opts, excl_prog_hash_size, 0);
 
 	log_opts = OPTS_GET(opts, log_opts, NULL);
-	if (!OPTS_VALID(log_opts, bpf_log_opts))
-		return libbpf_err(-EINVAL);
-
-	if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) {
-		memset(&attr_common, 0, attr_common_sz);
-		attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL));
-		attr_common.log_size = OPTS_GET(log_opts, size, 0);
-		attr_common.log_level = OPTS_GET(log_opts, level, 0);
-		fd = sys_bpf_ext_fd(BPF_MAP_CREATE, &attr, attr_sz, &attr_common, attr_common_sz);
-		OPTS_SET(log_opts, true_size, attr_common.log_true_size);
-	} else {
-		fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
-		OPTS_SET(log_opts, true_size, 0);
-	}
+	fd = __sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz, log_opts);
 	return libbpf_err_errno(fd);
 }
 
Signature · 1 line
2.55.0
author
#8 lore raw

PATCH v5 5/6 libbpf: Add log_opts support for bpf_link_create

In reply to Leon Hwang

Since the iter_info and iter_info_len in struct bpf_link_create_opts are legacy, reuse the slot of iter_info to place log_opts.

When adding log_opts support for iter link_create in the future, we will move the iter_info and iter_info_len to the last union, see below. And, keep the backward compatibility meanwhile, that uses iter_info if iter_info_len is set.

struct bpf_link_create_opts {
	/* ... */
	union {
		/* ... */
		strut {
			struct bpf_iter_link_info *info;
			__u32 len;
		} iter;
	};
};
---
 tools/lib/bpf/bpf.c | 20 +++++++++++++++++---
 tools/lib/bpf/bpf.h |  5 ++++-
 2 files changed, 21 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf.c +17 -3
index aea534764dfe..38dcb31e8c0e 100644
@@ -794,6 +794,8 @@ int bpf_link_create(int prog_fd, int target_fd,
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, link_create);
 	__u32 target_btf_id, iter_info_len, relative_id;
+	union bpf_iter_link_info *iter_info = NULL;
+	struct bpf_log_opts *log_opts = NULL;
 	int fd, err, relative_fd;
 	union bpf_attr attr;
 
@@ -803,6 +805,14 @@ int bpf_link_create(int prog_fd, int target_fd,
 	iter_info_len = OPTS_GET(opts, iter_info_len, 0);
 	target_btf_id = OPTS_GET(opts, target_btf_id, 0);
 
+	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)
@@ -824,7 +834,7 @@ int bpf_link_create(int prog_fd, int target_fd,
 
 	switch (attach_type) {
 	case BPF_TRACE_ITER:
-		attr.link_create.iter_info = ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
+		attr.link_create.iter_info = ptr_to_u64(iter_info);
 		attr.link_create.iter_info_len = iter_info_len;
 		break;
 	case BPF_PERF_EVENT:
@@ -960,12 +970,12 @@ int bpf_link_create(int prog_fd, int target_fd,
 			return libbpf_err(-EINVAL);
 		break;
 	default:
-		if (!OPTS_ZEROED(opts, flags))
+		if (!OPTS_ZEROED(opts, iter_info_len))
 			return libbpf_err(-EINVAL);
 		break;
 	}
 proceed:
-	fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
+	fd = __sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz, log_opts);
 	if (fd >= 0)
 		return fd;
 	/* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
@@ -975,6 +985,10 @@ int bpf_link_create(int prog_fd, int target_fd,
 	if (err != -EINVAL)
 		return libbpf_err(err);
 
+	/* bpf_raw_tracepoint_open() does not support log_opts */
+	if (log_opts)
+		return libbpf_err(err);
+
 	/* if user used features not supported by
 	 * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
 	 */
tools/lib/bpf/bpf.h +4 -1
index 826d9cc9ab65..fe3b7fd8512b 100644
@@ -422,7 +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;
-	union bpf_iter_link_info *iter_info;
+	union {
+		union bpf_iter_link_info *iter_info;
+		struct bpf_log_opts *log_opts;
+	};
 	__u32 iter_info_len;
 	__u32 target_btf_id;
 	union {
Signature · 1 line
2.55.0
author
#13 lore raw

PATCH v5 6/6 selftests/bpf: Add tests to verify link_create logs

In reply to Leon Hwang

Add tests to verify the new logs in link_create and xdp link_create, including a log reported via extack when fail to create an xdp link.

---
 .../selftests/bpf/prog_tests/xdp_link.c       | 102 ++++++++++++++++++
 1 file changed, 102 insertions(+)
tools/testing/selftests/bpf/prog_tests/xdp_link.c +102 -0
index e7e9f3c22edf..d1bca773ca2f 100644
@@ -150,3 +150,105 @@ void serial_test_xdp_link(void)
 	test_xdp_link__destroy(skel1);
 	test_xdp_link__destroy(skel2);
 }
+
+static void test_link_create(int prog_fd, int ifindex, enum bpf_attach_type attach_type,
+			     __u32 flags, int exp_err, const char *msg)
+{
+	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, opts,
+		    .flags = flags,
+		    .log_opts = &log_opts,
+	);
+	int fd;
+
+	fd = bpf_link_create(prog_fd, ifindex, attach_type, &opts);
+	if (!ASSERT_LT(fd, 0, "bpf_link_create")) {
+		close(fd);
+		return;
+	}
+
+	ASSERT_EQ(fd, exp_err, "fd");
+	ASSERT_STREQ(log_buf, msg, "log_buf");
+	ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+}
+
+static void test_link_create_invalid_prog_fd(void)
+{
+	const char *msg = "Invalid prog_fd.\n";
+
+	test_link_create(-1, IFINDEX_LO, BPF_XDP, 0, -EBADF, msg);
+}
+
+static void test_link_create_invalid_prog_type(void)
+{
+	const char *msg = "Invalid prog type.\n";
+	const struct bpf_insn insns[] = {
+		BPF_MOV64_IMM(BPF_REG_0, 0),
+		BPF_EXIT_INSN(),
+	};
+	int prog_fd;
+
+	prog_fd = bpf_prog_load(BPF_PROG_TYPE_LIRC_MODE2, NULL, "GPL", insns,
+				ARRAY_SIZE(insns), NULL);
+	if (!ASSERT_GE(prog_fd, 0, "bpf_prog_load"))
+		return;
+
+	test_link_create(prog_fd, 0, BPF_LIRC_MODE2, 0, -EINVAL, msg);
+
+	close(prog_fd);
+}
+
+static void test_link_create_attach(int ifindex, enum bpf_attach_type attach_type, __u32 flags,
+				    const char *msg)
+{
+	struct test_xdp_link *skel;
+
+	skel = test_xdp_link__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_xdp_link__open_and_load"))
+		return;
+
+	test_link_create(bpf_program__fd(skel->progs.xdp_handler), ifindex, attach_type, flags,
+			 -EINVAL, msg);
+
+	test_xdp_link__destroy(skel);
+}
+
+static void test_link_create_invalid_attach_type(void)
+{
+	const char *msg = "Invalid attach_type.\n";
+
+	test_link_create_attach(IFINDEX_LO, BPF_CGROUP_INET_INGRESS, 0, msg);
+}
+
+static void test_link_create_xdp_invalid_target_ifindex(void)
+{
+	const char *msg = "Invalid target_ifindex.\n";
+
+	test_link_create_attach(0, BPF_XDP, 0, msg);
+}
+
+static void test_link_create_xdp_extack(void)
+{
+	const char *msg = "Invalid XDP flags for BPF link attachment\n";
+
+	test_link_create_attach(IFINDEX_LO, BPF_XDP, XDP_FLAGS_REPLACE, msg);
+}
+
+void test_link_create_log(void)
+{
+	if (test__start_subtest("invalid_prog_fd"))
+		test_link_create_invalid_prog_fd();
+	if (test__start_subtest("invalid_prog_type"))
+		test_link_create_invalid_prog_type();
+	if (test__start_subtest("invalid_attach_type"))
+		test_link_create_invalid_attach_type();
+	if (test__start_subtest("xdp/invalid_target_ifindex"))
+		test_link_create_xdp_invalid_target_ifindex();
+	if (test__start_subtest("xdp/extack"))
+		test_link_create_xdp_extack();
+}
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH bpf-next v5 5/6] libbpf: Add log_opts support for bpf_link_create

In reply to Leon Hwang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] libbpf: type confusion between iter_info and log_opts in bpf_link_create() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] libbpf: type confusion between iter_info and log_opts in bpf_link_create() --

commit b4af2771f4c0edb6656b6ddcd881019158c55a55
Author: Leon Hwang <[email protected]>

libbpf: Add log_opts support for bpf_link_create

This commit adds log_opts support to bpf_link_create() by placing log_opts in an anonymous union with iter_info in struct bpf_link_create_opts and passing it to __sys_bpf_fd().

Quoted 14 lines
tools/lib/bpf/bpf.c +2 -0
index aea534764dfe2..38dcb31e8c0ed 100644
@@ -794,6 +794,8 @@ int bpf_link_create(int prog_fd, int target_fd,
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, link_create);
 	__u32 target_btf_id, iter_info_len, relative_id;
+	union bpf_iter_link_info *iter_info = NULL;
+	struct bpf_log_opts *log_opts = NULL;
 	int fd, err, relative_fd;
 	union bpf_attr attr;
 

tools/lib/bpf/bpf.c int bpf_link_create(int prog_fd, int target_fd, · line 814

Quoted 10 lines
tools/lib/bpf/bpf.c +7 -0
@@ -803,6 +805,14 @@ int bpf_link_create(int prog_fd, int target_fd,
 	iter_info_len = OPTS_GET(opts, iter_info_len, 0);
 	target_btf_id = OPTS_GET(opts, target_btf_id, 0);
 
+	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);

[Severity: Medium]
Can overlaying iter_info and log_opts in struct bpf_link_create_opts and selecting between them using only iter_info_len cause type confusion when a caller passes a non-NULL iter_info with iter_info_len == 0?

For example, a caller of bpf_link_create() or bpf_program__attach_iter() in tools/lib/bpf/libbpf.c:

tools/lib/bpf/libbpf.c:bpf_program__attach_iter() {
	...
	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);
	...
	return bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER, &link_create_opts);
}

might pass a non-NULL union bpf_iter_link_info pointer while leaving iter_info_len (or link_info_len) set to 0.

Previously, bpf_link_create() forwarded both the non-NULL iter_info pointer and iter_info_len == 0 to the kernel, where bpf_iter_link_attach() rejected the mismatched arguments with -EINVAL:

kernel/bpf/bpf_iter.c:bpf_iter_link_attach() {
	...
	if (bpfptr_is_null(ulinfo) ^ !linfo_len)
		return -EINVAL;
	...
}

With this change, when iter_info_len == 0, bpf_link_create() leaves iter_info as NULL (so attr.link_create.iter_info is 0 and the kernel check is bypassed) and instead assigns the union bpf_iter_link_info pointer to log_opts.

When bpf_link_create() then calls __sys_bpf_fd(), log_opts is dereferenced as struct bpf_log_opts:

tools/lib/bpf/bpf.c:__sys_bpf_fd() {
	...
	if (!OPTS_VALID(log_opts, bpf_log_opts))
		return libbpf_err(-EINVAL);

	if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) {
		memset(&attr_common, 0, attr_common_sz);
		attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL));
		attr_common.log_size = OPTS_GET(log_opts, size, 0);
		attr_common.log_level = OPTS_GET(log_opts, level, 0);
		fd = sys_bpf_ext_fd(cmd, attr, attr_sz, &attr_common, attr_common_sz);
		OPTS_SET(log_opts, true_size, attr_common.log_true_size);
	} else {
		fd = sys_bpf_fd(cmd, attr, attr_sz);
		OPTS_SET(log_opts, true_size, 0);
	}
	...
}

Since union bpf_iter_link_info is 16 bytes while struct bpf_log_opts extends to offsetofend(struct bpf_log_opts, true_size) (28 bytes on 64-bit, 20 bytes on 32-bit), interpreting the first sizeof(size_t) bytes of iter_info (such as map.map_fd, task.tid, task.pid, or cgroup.cgroup_fd) as log_opts->sz can cause out-of-bounds reads in OPTS_VALID() and OPTS_GET(), as well as a 4-byte out-of-bounds write past union bpf_iter_link_info via OPTS_SET(log_opts, true_size, ...).

In addition, aliasing iter_info and log_opts prevents BPF_TRACE_ITER links from using log_opts whenever iter_info is specified. Would it be safer to place log_opts in its own field at the end of struct bpf_link_create_opts instead of sharing a union with iter_info?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#2 lore raw

Re: [PATCH bpf-next v5 1/6] bpf: Add syscall common attrs support for link_create

In reply to Leon Hwang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] bpf: inaccurate attach_type and prog_type error logging in link_create() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] bpf: inaccurate attach_type and prog_type error logging in link_create() --

commit bc490436683f8010bb5c27c3e3a1582ddc24a909
Author: Leon Hwang <[email protected]>

bpf: Add syscall common attrs support for link_create

This commit adds syscall common attributes support to link_create() in kernel/bpf/syscall.c, initializing and finalizing a bpf_verifier_log instance to report errors for invalid prog_fd, attach_type, and prog_type.

Quoted 46 lines
kernel/bpf/syscall.c +17 -4
index 90bc7230afa8a..db870e795d044 100644
@@ -5862,8 +5862,11 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
 }
 
 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.path_fd
-static int link_create(union bpf_attr *attr, bpfptr_t uattr)
+static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_attr *attr_common,
+		       bpfptr_t uattr_common, u32 size_common)
 {
+	struct bpf_verifier_log *log;
+	struct bpf_log_attr attr_log;
 	struct bpf_prog *prog;
 	int ret;
 
@@ -5873,14 +5876,24 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 	if (attr->link_create.attach_type == BPF_STRUCT_OPS)
 		return bpf_struct_ops_link_create(attr);
 
+	log = bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, size_common);
+	if (IS_ERR(log))
+		return PTR_ERR(log);
+
 	prog = bpf_prog_get(attr->link_create.prog_fd);
-	if (IS_ERR(prog))
-		return PTR_ERR(prog);
+	if (IS_ERR(prog)) {
+		bpf_log(log, "Invalid prog_fd.\n");
+		ret = PTR_ERR(prog);
+		prog = NULL;
+		goto out;
+	}
 
 	ret = bpf_prog_attach_check_attach_type(prog,
 						attr->link_create.attach_type);
-	if (ret)
+	if (ret) {
+		bpf_log(log, "Invalid attach_type.\n");
 		goto out;
+	}
 
 	switch (prog->type) {
 	case BPF_PROG_TYPE_CGROUP_SKB:

kernel/bpf/syscall.c static int link_create(union bpf_attr *attr, bpfptr_t uattr) · line 5977

Quoted 6 lines
kernel/bpf/syscall.c +1 -0
@@ -5959,12 +5972,20 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 			ret = bpf_uprobe_multi_link_attach(attr, prog);
 		break;
 	default:
+		bpf_log(log, "Invalid prog type.\n");
 		ret = -EINVAL;
 	}

[Severity: Medium]
Does calling bpf_prog_attach_check_attach_type() before switch (prog->type) in kernel/bpf/syscall.c:link_create() cause misleading or missing log messages in a few cases?

First, for almost all program types that are unsupported by link_create() (such as BPF_PROG_TYPE_SOCKET_FILTER, BPF_PROG_TYPE_SCHED_ACT, BPF_PROG_TYPE_RAW_TRACEPOINT, or BPF_PROG_TYPE_SYSCALL), link_create() -> bpf_prog_attach_check_attach_type() hits the default branch:

kernel/bpf/syscall.c:bpf_prog_attach_check_attach_type() {
	...
	default:
		ptype = attach_type_to_prog_type(attach_type);
		if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type)
			return -EINVAL;
		return 0;
}

Because attach_type_to_prog_type() never returns those program types for any attach_type, bpf_prog_attach_check_attach_type() returns -EINVAL and link_create() logs "Invalid attach_type.\n" instead of reaching the default branch of switch (prog->type) to log "Invalid prog type.\n".

Second, for BPF_PROG_TYPE_CGROUP_SKB, bpf_prog_attach_check_attach_type() also performs a capability check:

	case BPF_PROG_TYPE_CGROUP_SKB:
		if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
			return -EPERM;

When this check fails with -EPERM, link_create() still logs "Invalid attach_type.\n" even when attach_type is valid.

Third, for BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_TRACING, bpf_prog_attach_check_attach_type() only checks that attach_type maps to the general program type. The stricter expected_attach_type check happens inside switch (prog->type) in link_create():

	case BPF_PROG_TYPE_LSM:
	case BPF_PROG_TYPE_TRACING:
		if (attr->link_create.attach_type != prog->expected_attach_type) {
			ret = -EINVAL;
			goto out;
		}

If a caller passes an attach_type of the same program type that does not match prog->expected_attach_type, link_create() jumps to out without logging "Invalid attach_type.\n".

Should the program type validation and attach_type logging be ordered so that unsupported program types, -EPERM failures, and expected_attach_type mismatches report accurate messages?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v5 2/6
#5 lore raw
In reply to Leon Hwang
Quoted 10 lines · On Mon, 26 Oct 2026 23:49:43 +0800 Leon Hwang wrote:

With the syscall common attrs support for link_create, add syscall common attrs support for xdp link_create in order to report the invalid target ifindex error and the underlying driver's error.

Instead of passing bpf log to bpf_xdp_link_attach(), pass an extack to receive the underlying driver's error message. If pass bpf log to bpf_xdp_link_attach(), it will introduce bpf_verifier.h dependency for net/core/dev.c, which is not acceptable for maintainability.

Thanks!

author
#3 lore raw

Re: [PATCH bpf-next v5 1/6] bpf: Add syscall common attrs support for link_create

In reply to [email protected]

On 27/10/26 00:06, [email protected] wrote:

[...]

Quoted 31 lines

kernel/bpf/syscall.c static int link_create(union bpf_attr *attr, bpfptr_t uattr) · line 5977

Quoted 10 lines
kernel/bpf/syscall.c +1 -0
 	switch (prog->type) {
 	case BPF_PROG_TYPE_CGROUP_SKB:
@@ -5959,12 +5972,20 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 			ret = bpf_uprobe_multi_link_attach(attr, prog);
 		break;
 	default:
+		bpf_log(log, "Invalid prog type.\n");
 		ret = -EINVAL;
 	}

[Severity: Medium]
Does calling bpf_prog_attach_check_attach_type() before switch (prog->type) in kernel/bpf/syscall.c:link_create() cause misleading or missing log messages in a few cases?

First, for almost all program types that are unsupported by link_create() (such as BPF_PROG_TYPE_SOCKET_FILTER, BPF_PROG_TYPE_SCHED_ACT, BPF_PROG_TYPE_RAW_TRACEPOINT, or BPF_PROG_TYPE_SYSCALL), link_create() -> bpf_prog_attach_check_attach_type() hits the default branch:

kernel/bpf/syscall.c:bpf_prog_attach_check_attach_type() {
	...
	default:
		ptype = attach_type_to_prog_type(attach_type);
		if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type)
			return -EINVAL;
		return 0;
}
Quoted 4 lines

Because attach_type_to_prog_type() never returns those program types for any attach_type, bpf_prog_attach_check_attach_type() returns -EINVAL and link_create() logs "Invalid attach_type.\n" instead of reaching the default branch of switch (prog->type) to log "Invalid prog type.\n".

Either "Invalid attach_type.\n" or "Invalid prog type.\n" is not accurate for this case.

The log here is not required to accurately describe the reason. Instead, the log tells users something wrong at the position.

Quoted 10 lines

Second, for BPF_PROG_TYPE_CGROUP_SKB, bpf_prog_attach_check_attach_type() also performs a capability check:

	case BPF_PROG_TYPE_CGROUP_SKB:
		if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
			return -EPERM;

When this check fails with -EPERM, link_create() still logs "Invalid attach_type.\n" even when attach_type is valid.

Same above.

Quoted 13 lines

Third, for BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_TRACING, bpf_prog_attach_check_attach_type() only checks that attach_type maps to the general program type. The stricter expected_attach_type check happens inside switch (prog->type) in link_create():

	case BPF_PROG_TYPE_LSM:
	case BPF_PROG_TYPE_TRACING:
		if (attr->link_create.attach_type != prog->expected_attach_type) {
			ret = -EINVAL;
			goto out;
		}
Quoted 3 lines

If a caller passes an attach_type of the same program type that does not match prog->expected_attach_type, link_create() jumps to out without logging "Invalid attach_type.\n".

Will add a log here when adding log support for tracing link_create in the future.

Thanks,
Leon

Quoted 5 lines

Should the program type validation and attach_type logging be ordered so that unsupported program types, -EPERM failures, and expected_attach_type mismatches report accurate messages?

author
#10 lore raw

Re: [PATCH bpf-next v5 5/6] libbpf: Add log_opts support for bpf_link_create

In reply to [email protected]
Quoted 30 lines · On 27/10/26 00:05, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] libbpf: type confusion between iter_info and log_opts in bpf_link_create() --

commit b4af2771f4c0edb6656b6ddcd881019158c55a55
Author: Leon Hwang <[email protected]>

libbpf: Add log_opts support for bpf_link_create

This commit adds log_opts support to bpf_link_create() by placing log_opts in an anonymous union with iter_info in struct bpf_link_create_opts and passing it to __sys_bpf_fd().

Quoted 17 lines
tools/lib/bpf/bpf.c +2 -0
index aea534764dfe2..38dcb31e8c0ed 100644
@@ -794,6 +794,8 @@ int bpf_link_create(int prog_fd, int target_fd,
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, link_create);
 	__u32 target_btf_id, iter_info_len, relative_id;
+	union bpf_iter_link_info *iter_info = NULL;
+	struct bpf_log_opts *log_opts = NULL;
 	int fd, err, relative_fd;
 	union bpf_attr attr;
 
@@ -803,6 +805,14 @@ int bpf_link_create(int prog_fd, int target_fd,
 	iter_info_len = OPTS_GET(opts, iter_info_len, 0);
 	target_btf_id = OPTS_GET(opts, target_btf_id, 0);
 
Quoted 12 lines

tools/lib/bpf/bpf.c int bpf_link_create(int prog_fd, int target_fd, · line 814

Quoted 7 lines
tools/lib/bpf/bpf.c +7 -0
+	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);

[Severity: Medium]
Can overlaying iter_info and log_opts in struct bpf_link_create_opts and selecting between them using only iter_info_len cause type confusion when a caller passes a non-NULL iter_info with iter_info_len == 0?

Users must not do like this. By convention, iter_info_len==0 means no iter_info.

Quoted 60 lines

For example, a caller of bpf_link_create() or bpf_program__attach_iter() in tools/lib/bpf/libbpf.c:

tools/lib/bpf/libbpf.c:bpf_program__attach_iter() {
	...
	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);
	...
	return bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER, &link_create_opts);
}

might pass a non-NULL union bpf_iter_link_info pointer while leaving iter_info_len (or link_info_len) set to 0.

Previously, bpf_link_create() forwarded both the non-NULL iter_info pointer and iter_info_len == 0 to the kernel, where bpf_iter_link_attach() rejected the mismatched arguments with -EINVAL:

kernel/bpf/bpf_iter.c:bpf_iter_link_attach() {
	...
	if (bpfptr_is_null(ulinfo) ^ !linfo_len)
		return -EINVAL;
	...
}

With this change, when iter_info_len == 0, bpf_link_create() leaves iter_info as NULL (so attr.link_create.iter_info is 0 and the kernel check is bypassed) and instead assigns the union bpf_iter_link_info pointer to log_opts.

When bpf_link_create() then calls __sys_bpf_fd(), log_opts is dereferenced as struct bpf_log_opts:

tools/lib/bpf/bpf.c:__sys_bpf_fd() {
	...
	if (!OPTS_VALID(log_opts, bpf_log_opts))
		return libbpf_err(-EINVAL);

	if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) {
		memset(&attr_common, 0, attr_common_sz);
		attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL));
		attr_common.log_size = OPTS_GET(log_opts, size, 0);
		attr_common.log_level = OPTS_GET(log_opts, level, 0);
		fd = sys_bpf_ext_fd(cmd, attr, attr_sz, &attr_common, attr_common_sz);
		OPTS_SET(log_opts, true_size, attr_common.log_true_size);
	} else {
		fd = sys_bpf_fd(cmd, attr, attr_sz);
		OPTS_SET(log_opts, true_size, 0);
	}
	...
}

Since union bpf_iter_link_info is 16 bytes while struct bpf_log_opts extends to offsetofend(struct bpf_log_opts, true_size) (28 bytes on 64-bit, 20 bytes on 32-bit), interpreting the first sizeof(size_t) bytes of iter_info (such as map.map_fd, task.tid, task.pid, or cgroup.cgroup_fd) as log_opts->sz can cause out-of-bounds reads in OPTS_VALID() and OPTS_GET(), as well as a 4-byte out-of-bounds write past union bpf_iter_link_info via OPTS_SET(log_opts, true_size, ...).

Quoted 5 lines

In addition, aliasing iter_info and log_opts prevents BPF_TRACE_ITER links from using log_opts whenever iter_info is specified. Would it be safer to place log_opts in its own field at the end of struct bpf_link_create_opts instead of sharing a union with iter_info?

Not at the end of struct bpf_link_create_opts.

When adding log_opts support for iter link in the future, both iter_info and iter_info_len will be moved to the last union.

tools/lib/bpf/bpf.h +4 -0
index fe3b7fd8512b..8bfada1f3ea3 100644
@@ -478,6 +478,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;
 };

Afterwards, log_opts will be valid at the iter_info position for iter link.

Thanks,
Leon

#11 lore raw

Re: [PATCH bpf-next v5 5/6] libbpf: Add log_opts support for bpf_link_create

In reply to Leon Hwang
Quoted 64 lines · Leon Hwang

Since the iter_info and iter_info_len in struct bpf_link_create_opts are legacy, reuse the slot of iter_info to place log_opts.

When adding log_opts support for iter link_create in the future, we will move the iter_info and iter_info_len to the last union, see below. And, keep the backward compatibility meanwhile, that uses iter_info if iter_info_len is set.

struct bpf_link_create_opts {
        /* ... */
        union {
                /* ... */
                strut {
                        struct bpf_iter_link_info *info;
                        __u32 len;
                } iter;
        };
};
---
 tools/lib/bpf/bpf.c | 20 +++++++++++++++++---
 tools/lib/bpf/bpf.h |  5 ++++-
 2 files changed, 21 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf.c +11 -1
index aea534764dfe..38dcb31e8c0e 100644
@@ -794,6 +794,8 @@ int bpf_link_create(int prog_fd, int target_fd,
 {
        const size_t attr_sz = offsetofend(union bpf_attr, link_create);
        __u32 target_btf_id, iter_info_len, relative_id;
+       union bpf_iter_link_info *iter_info = NULL;
+       struct bpf_log_opts *log_opts = NULL;
        int fd, err, relative_fd;
        union bpf_attr attr;
 
@@ -803,6 +805,14 @@ int bpf_link_create(int prog_fd, int target_fd,
        iter_info_len = OPTS_GET(opts, iter_info_len, 0);
        target_btf_id = OPTS_GET(opts, target_btf_id, 0);
 
+       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)
@@ -824,7 +834,7 @@ int bpf_link_create(int prog_fd, int target_fd,
 
        switch (attach_type) {
        case BPF_TRACE_ITER:
-               attr.link_create.iter_info = ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
+               attr.link_create.iter_info = ptr_to_u64(iter_info);
                attr.link_create.iter_info_len = iter_info_len;
                break;
        case BPF_PERF_EVENT:

tools/lib/bpf/bpf.c int bpf_link_create(int prog_fd, int target_fd, · line 973

Quoted 5 lines
tools/lib/bpf/bpf.c +1 -1
@@ -960,12 +970,12 @@ int bpf_link_create(int prog_fd, int target_fd,
                        return libbpf_err(-EINVAL);
                break;
        default:
-               if (!OPTS_ZEROED(opts, flags))
+               if (!OPTS_ZEROED(opts, iter_info_len))

semantically cleaner would be to use log_opts here instead of iter_info_len, send in a follow up?

Quoted 25 lines
tools/lib/bpf/bpf.c +5 -1
                        return libbpf_err(-EINVAL);
                break;
        }
 proceed:
-       fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
+       fd = __sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz, log_opts);
        if (fd >= 0)
                return fd;
        /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
@@ -975,6 +985,10 @@ int bpf_link_create(int prog_fd, int target_fd,
        if (err != -EINVAL)
                return libbpf_err(err);
 
+       /* bpf_raw_tracepoint_open() does not support log_opts */
+       if (log_opts)
+               return libbpf_err(err);
+
        /* if user used features not supported by
         * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
         */
tools/lib/bpf/bpf.h +0 -0
index 826d9cc9ab65..fe3b7fd8512b 100644

tools/lib/bpf/bpf.h union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */ · line 429

Quoted 9 lines
tools/lib/bpf/bpf.h +4 -1
@@ -422,7 +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;
-       union bpf_iter_link_info *iter_info;
+       union {
+               union bpf_iter_link_info *iter_info;
+               struct bpf_log_opts *log_opts;
+       };
        __u32 iter_info_len;

in a follow up, let's add comment next to iter_info above and iter_info_len here that these are deprecated and that iter.info and iter.info_len should be used instead

applied series to bpf-next, thank you!

Quoted 5 lines
tools/lib/bpf/bpf.h +0 -1
        __u32 target_btf_id;
        union {
--

2.55.0

#14 lore raw
In reply to Leon Hwang
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 10 lines · On Mon, 26 Oct 2026 23:49:41 +0800 you wrote:

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

This is the first series for the plan, that adds syscall common attrs support for xdp LINK_CREATE.

Thereafter, retire the tracepoint bpf_xdp_link_attach_failed, because the underlying driver's error can be back-propagated to user space via the syscall common attrs.

[...]

Here is the summary with links:
- [bpf-next,v5,1/6] bpf: Add syscall common attrs support for link_create

    https://git.kernel.org/bpf/bpf-next/c/b891bce3fe4d
  - [bpf-next,v5,2/6] bpf: Add syscall common attrs support for xdp link_create
    https://git.kernel.org/bpf/bpf-next/c/cbb143cc97b6
  - [bpf-next,v5,3/6] bpf: Retire tracepoint bpf_xdp_link_attach_failed
    https://git.kernel.org/bpf/bpf-next/c/d33d3ec8f730
  - [bpf-next,v5,4/6] libbpf: Factor out __sys_bpf_fd helper
    https://git.kernel.org/bpf/bpf-next/c/d6ef68efcece
  - [bpf-next,v5,5/6] libbpf: Add log_opts support for bpf_link_create
    https://git.kernel.org/bpf/bpf-next/c/0be729f9f030
  - [bpf-next,v5,6/6] selftests/bpf: Add tests to verify link_create logs
    https://git.kernel.org/bpf/bpf-next/c/a1a714b7e947

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
author
#12 lore raw

Re: [PATCH bpf-next v5 5/6] libbpf: Add log_opts support for bpf_link_create

In reply to Andrii Nakryiko
Quoted 1 line · Andrii Nakryiko

On Mon, Oct 26, 2026 at 8:51 AM Leon Hwang <[email protected]> wrote:

[...]

Quoted 9 lines

tools/lib/bpf/bpf.c int bpf_link_create(int prog_fd, int target_fd, · line 973

Quoted 6 lines
tools/lib/bpf/bpf.c +1 -1
@@ -960,12 +970,12 @@ int bpf_link_create(int prog_fd, int target_fd,
                        return libbpf_err(-EINVAL);
                break;
        default:
-               if (!OPTS_ZEROED(opts, flags))
+               if (!OPTS_ZEROED(opts, iter_info_len))

semantically cleaner would be to use log_opts here instead of iter_info_len, send in a follow up?

Will do.

Quoted 31 lines
Quoted 31 lines
tools/lib/bpf/bpf.c +5 -1
                        return libbpf_err(-EINVAL);
                break;
        }
 proceed:
-       fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
+       fd = __sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz, log_opts);
        if (fd >= 0)
                return fd;
        /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
@@ -975,6 +985,10 @@ int bpf_link_create(int prog_fd, int target_fd,
        if (err != -EINVAL)
                return libbpf_err(err);
 
+       /* bpf_raw_tracepoint_open() does not support log_opts */
+       if (log_opts)
+               return libbpf_err(err);
+
        /* if user used features not supported by
         * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
         */
tools/lib/bpf/bpf.h +1 -1
index 826d9cc9ab65..fe3b7fd8512b 100644
@@ -422,7 +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;
-       union bpf_iter_link_info *iter_info;
+       union {
Quoted 9 lines

tools/lib/bpf/bpf.h union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */ · line 429

Quoted 4 lines
tools/lib/bpf/bpf.h +3 -0
+               union bpf_iter_link_info *iter_info;
+               struct bpf_log_opts *log_opts;
+       };
        __u32 iter_info_len;

in a follow up, let's add comment next to iter_info above and iter_info_len here that these are deprecated and that iter.info and iter.info_len should be used instead

Will add comment when adding log_opts support for BPF_TRACE_ITER in libbpf in near future.

Thanks,
Leon