Skip to contentSkip to conversation

bpf: Add cookie to tracing bpf_link_info

PATCH Applied Tao Chen 2 participants 6 messages Jun 6, 2025 – Jun 9, 2025

Subsystems (5)
Kselftest BPF BPF selftests BPF core +1 more subsystems
lore
Share & tools
lore

Tao Chen

author
#0 lore raw

PATCH 1/5 bpf: Add cookie to tracing bpf_link_info

bpf_tramp_link includes cookie info, we can add it in bpf_link_info.

---
 include/uapi/linux/bpf.h       | 2 ++
 kernel/bpf/syscall.c           | 1 +
 tools/include/uapi/linux/bpf.h | 2 ++
 3 files changed, 5 insertions(+)
include/uapi/linux/bpf.h +2 -0
index f1160ebbf52..fd4869d8cd1 100644
@@ -6658,6 +6658,8 @@ struct bpf_link_info {
 			__u32 attach_type;
 			__u32 target_obj_id; /* prog_id for PROG_EXT, otherwise btf object id */
 			__u32 target_btf_id; /* BTF type id inside the object */
+			__u32 :32;
+			__u64 cookie;
 		} tracing;
 		struct {
 			__u64 cgroup_id;
kernel/bpf/syscall.c +1 -0
index 89d027cd7ca..4c0ac083518 100644
@@ -3416,6 +3416,7 @@ static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
 		container_of(link, struct bpf_tracing_link, link.link);
 
 	info->tracing.attach_type = tr_link->attach_type;
+	info->tracing.cookie = tr_link->link.cookie;
 	bpf_trampoline_unpack_key(tr_link->trampoline->key,
 				  &info->tracing.target_obj_id,
 				  &info->tracing.target_btf_id);
tools/include/uapi/linux/bpf.h +2 -0
index f1160ebbf52..fd4869d8cd1 100644
@@ -6658,6 +6658,8 @@ struct bpf_link_info {
 			__u32 attach_type;
 			__u32 target_obj_id; /* prog_id for PROG_EXT, otherwise btf object id */
 			__u32 target_btf_id; /* BTF type id inside the object */
+			__u32 :32;
+			__u64 cookie;
 		} tracing;
 		struct {
 			__u64 cgroup_id;
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 2/5 selftests/bpf: Add cookies check for tracing fill_link_info test

Adding tests for getting cookie with fill_link_info for tracing.

---
 .../selftests/bpf/prog_tests/bpf_cookie.c     | 24 ++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +23 -1
index 0774ae6c1be..4a0670c056b 100644
@@ -489,10 +489,28 @@ static void pe_subtest(struct test_bpf_cookie *skel)
 	bpf_link__destroy(link);
 }
 
+static int verify_tracing_link_info(int fd, u64 cookie)
+{
+	struct bpf_link_info info;
+	int err;
+	u32 len = sizeof(info);
+
+	err = bpf_link_get_info_by_fd(fd, &info, &len);
+	if (!ASSERT_OK(err, "get_link_info"))
+		return -1;
+
+	if (!ASSERT_EQ(info.type, BPF_LINK_TYPE_TRACING, "link_type"))
+		return -1;
+
+	ASSERT_EQ(info.tracing.cookie, cookie, "tracing_cookie");
+
+	return 0;
+}
+
 static void tracing_subtest(struct test_bpf_cookie *skel)
 {
 	__u64 cookie;
-	int prog_fd;
+	int prog_fd, err;
 	int fentry_fd = -1, fexit_fd = -1, fmod_ret_fd = -1;
 	LIBBPF_OPTS(bpf_test_run_opts, opts);
 	LIBBPF_OPTS(bpf_link_create_opts, link_opts);
@@ -507,6 +525,10 @@ static void tracing_subtest(struct test_bpf_cookie *skel)
 	if (!ASSERT_GE(fentry_fd, 0, "fentry.link_create"))
 		goto cleanup;
 
+	err = verify_tracing_link_info(fentry_fd, cookie);
+	if (!ASSERT_OK(err, "verify_tracing_link_info"))
+		goto cleanup;
+
 	cookie = 0x20000000000000L;
 	prog_fd = bpf_program__fd(skel->progs.fexit_test1);
 	link_opts.tracing.cookie = cookie;
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 3/5 bpftool: Display cookie for tracing link probe

In reply to Tao Chen

Display cookie for tracing link probe, in plain mode:

#bpftool link
5: tracing prog 34

	prog_type tracing  attach_type trace_fentry
	target_obj_id 1  target_btf_id 60355
	cookie 4503599627370496
	pids test_progs(176)

And in json mode:

#bpftool link -j | jq

{
    "id": 5,
    "type": "tracing",
    "prog_id": 34,
    "prog_type": "tracing",
    "attach_type": "trace_fentry",
    "target_obj_id": 1,
    "target_btf_id": 60355,
    "cookie": 4503599627370496,
    "pids": [
      {
        "pid": 176,
        "comm": "test_progs"
      }
    ]

}

---
 tools/bpf/bpftool/link.c | 3 +++
 1 file changed, 3 insertions(+)
tools/bpf/bpftool/link.c +3 -0
index cb67ce4eba2..03513ffffb7 100644
@@ -503,6 +503,7 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
 					   json_wtr);
 		jsonw_uint_field(json_wtr, "target_obj_id", info->tracing.target_obj_id);
 		jsonw_uint_field(json_wtr, "target_btf_id", info->tracing.target_btf_id);
+		jsonw_uint_field(json_wtr, "cookie", info->tracing.cookie);
 		break;
 	case BPF_LINK_TYPE_CGROUP:
 		jsonw_lluint_field(json_wtr, "cgroup_id",
@@ -900,6 +901,8 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
 			printf("\n\ttarget_obj_id %u  target_btf_id %u  ",
 			       info->tracing.target_obj_id,
 			       info->tracing.target_btf_id);
+		if (info->tracing.cookie)
+			printf("\n\tcookie %llu  ", info->tracing.cookie);
 		break;
 	case BPF_LINK_TYPE_CGROUP:
 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH 4/5 bpf: Add cookie in fdinfo for tracing

In reply to Tao Chen

Add cookie in fdinfo for tracing, the info as follows:

link_type: tracing
link_id: 6
prog_tag: 9dfdf8ef453843bf
prog_id: 35
attach_type: 25
target_obj_id: 1
target_btf_id: 60355
cookie: 9007199254740992

---
 kernel/bpf/syscall.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/bpf/syscall.c +4 -2
index 4c0ac083518..d6eba1339ad 100644
@@ -3403,10 +3403,12 @@ static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
 	seq_printf(seq,
 		   "attach_type:\t%d\n"
 		   "target_obj_id:\t%u\n"
-		   "target_btf_id:\t%u\n",
+		   "target_btf_id:\t%u\n"
+		   "cookie:\t%llu\n",
 		   tr_link->attach_type,
 		   target_obj_id,
-		   target_btf_id);
+		   target_btf_id,
+		   tr_link->link.cookie);
 }
 
 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 5/5 bpf: Add cookie in fdinfo for raw_tp

In reply to Tao Chen

Add cookie in fdinfo for raw_tp, the info as follows:

link_type: raw_tracepoint
link_id: 31
prog_tag: 9dfdf8ef453843bf
prog_id: 32
tp_name: sys_enter
cookie: 23925373020405760

---
 kernel/bpf/syscall.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/bpf/syscall.c +4 -2
index d6eba1339ad..51ba1a7aa43 100644
@@ -3654,8 +3654,10 @@ static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
 		container_of(link, struct bpf_raw_tp_link, link);
 
 	seq_printf(seq,
-		   "tp_name:\t%s\n",
-		   raw_tp_link->btp->tp->name);
+		   "tp_name:\t%s\n"
+		   "cookie:\t%llu\n",
+		   raw_tp_link->btp->tp->name,
+		   raw_tp_link->cookie);
 }
 
 static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
Signature · 1 line
2.43.0
#5 lore raw
In reply to Tao Chen
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 8 lines · On Sat, 7 Jun 2025 00:58:14 +0800 you wrote:

bpf_tramp_link includes cookie info, we can add it in bpf_link_info.

---
 include/uapi/linux/bpf.h       | 2 ++
 kernel/bpf/syscall.c           | 1 +
 tools/include/uapi/linux/bpf.h | 2 ++
 3 files changed, 5 insertions(+)

Here is the summary with links:
- [bpf-next,1/5] bpf: Add cookie to tracing bpf_link_info

    https://git.kernel.org/bpf/bpf-next/c/c7beb48344d2
  - [bpf-next,2/5] selftests/bpf: Add cookies check for tracing fill_link_info test
    https://git.kernel.org/bpf/bpf-next/c/d77efc0ef5b0
  - [bpf-next,3/5] bpftool: Display cookie for tracing link probe
    https://git.kernel.org/bpf/bpf-next/c/ad954cbe0849
  - [bpf-next,4/5] bpf: Add cookie in fdinfo for tracing
    https://git.kernel.org/bpf/bpf-next/c/380cb6dfa2bf
  - [bpf-next,5/5] bpf: Add cookie in fdinfo for raw_tp
    https://git.kernel.org/bpf/bpf-next/c/2bc0575fec36

You are awesome, thank you!

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