aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Fernandez <josef@netflix.com>2024-03-02 23:17:30 -0700
committerJose Fernandez <josef@netflix.com>2024-03-04 21:35:08 -0700
commit7c92234950fd064f85d8fd02d81caa32574a0e0b (patch)
tree77c3ba79bd3f7a580a659b309d64f6951b0a7f33
parent63db8a022404e41878773a44665a5ad9662410eb (diff)
downloadlinux-bpf_task_get_cgroup_id.tar.gz
selftests/bpf: add selfstest for btf_task_get_cgroup_idbpf_task_get_cgroup_id
This patch adds a selftest for the `bpf_task_get_cgroup_id` kfunc. The test focuses on the use case of obtaining the cgroup ID of the previous task in a `sched_switch` tracepoint. The selftest involves creating a test cgroup, attaching a BPF program that utilizes the `bpf_task_get_cgroup_id` during a `sched_switch` tracepoint, and validating that the obtained cgroup ID for the previous task matches the expected cgroup ID. #315 task_get_cgroup_id:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Jose Fernandez <josef@netflix.com>
-rw-r--r--tools/testing/selftests/bpf/prog_tests/task_get_cgroup_id.c58
-rw-r--r--tools/testing/selftests/bpf/progs/test_task_get_cgroup_id.c30
2 files changed, 88 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/prog_tests/task_get_cgroup_id.c b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup_id.c
new file mode 100644
index 00000000000000..b8c4551195d357
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup_id.c
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright 2024 Netflix, Inc.
+
+#include <test_progs.h>
+#include <cgroup_helpers.h>
+#include "test_task_get_cgroup_id.skel.h"
+#include <unistd.h>
+
+#define TEST_CGROUP "/test-task-get-cgroup-id/"
+
+void test_task_get_cgroup_id(void)
+{
+ struct test_task_get_cgroup_id *skel;
+ int err, fd;
+ pid_t pid;
+ __u64 cgroup_id, expected_cgroup_id;
+ const struct timespec req = {
+ .tv_sec = 1,
+ .tv_nsec = 0,
+ };
+
+ fd = test__join_cgroup(TEST_CGROUP);
+ if (!ASSERT_OK(fd < 0, "test_join_cgroup_TEST_CGROUP"))
+ return;
+
+ skel = test_task_get_cgroup_id__open();
+ if (!ASSERT_OK_PTR(skel, "test_task_get_cgroup_id__open"))
+ goto cleanup;
+
+ err = test_task_get_cgroup_id__load(skel);
+ if (!ASSERT_OK(err, "test_task_get_cgroup_id__load"))
+ goto cleanup;
+
+ err = test_task_get_cgroup_id__attach(skel);
+ if (!ASSERT_OK(err, "test_task_get_cgroup_id__attach"))
+ goto cleanup;
+
+ pid = getpid();
+ expected_cgroup_id = get_cgroup_id(TEST_CGROUP);
+ if (!ASSERT_GT(expected_cgroup_id, 0, "get_cgroup_id"))
+ goto cleanup;
+
+ /* Trigger nanosleep to enter the sched_switch tracepoint */
+ /* The previous task should be this process */
+ syscall(__NR_nanosleep, &req, NULL);
+
+ err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.pid_to_cgid_map), &pid,
+ &cgroup_id);
+
+ if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
+ goto cleanup;
+
+ ASSERT_EQ(cgroup_id, expected_cgroup_id, "cgroup_id");
+
+cleanup:
+ test_task_get_cgroup_id__destroy(skel);
+ close(fd);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_task_get_cgroup_id.c b/tools/testing/selftests/bpf/progs/test_task_get_cgroup_id.c
new file mode 100644
index 00000000000000..7e6bc008970f20
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_task_get_cgroup_id.c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright 2024 Netflix, Inc.
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+u64 bpf_task_get_cgroup_id(struct task_struct *task) __ksym;
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(max_entries, 4096);
+ __type(key, __u32);
+ __type(value, __u64);
+} pid_to_cgid_map SEC(".maps");
+
+SEC("tp_btf/sched_switch")
+int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev,
+ struct task_struct *next)
+{
+ u32 pid = prev->pid;
+ u64 cgroup_id;
+
+ cgroup_id = bpf_task_get_cgroup_id(prev);
+ bpf_map_update_elem(&pid_to_cgid_map, &pid, &cgroup_id, BPF_ANY);
+
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";