new file mode 100644
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright 2024 Netflix, Inc.
+
+#include <test_progs.h>
+#include <cgroup_helpers.h>
+#include "test_task_get_cgroup.skel.h"
+#include <unistd.h>
+
+#define TEST_CGROUP "/test-task-get-cgroup/"
+
+void test_task_get_cgroup(void)
+{
+ struct test_task_get_cgroup *skel;
+ int err, fd;
+ __u64 expected_cgroup_id;
+
+ fd = test__join_cgroup(TEST_CGROUP);
+ if (!ASSERT_OK(fd < 0, "test_join_cgroup_TEST_CGROUP"))
+ return;
+
+ skel = test_task_get_cgroup__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "test_task_get_cgroup__open_and_load"))
+ goto cleanup;
+
+ err = test_task_get_cgroup__attach(skel);
+ if (!ASSERT_OK(err, "test_task_get_cgroup__attach"))
+ goto cleanup;
+
+ skel->bss->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 */
+ usleep(100);
+
+ ASSERT_EQ(skel->bss->cgroup_id, expected_cgroup_id, "cgroup_id");
+
+cleanup:
+ test_task_get_cgroup__destroy(skel);
+ close(fd);
+}
new file mode 100644
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright 2024 Netflix, Inc.
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+struct cgroup *bpf_task_get_cgroup(struct task_struct *task) __ksym;
+void bpf_cgroup_release(struct cgroup *cgrp) __ksym;
+
+int pid = 0;
+u64 cgroup_id = 0;
+
+SEC("tp_btf/sched_switch")
+int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev,
+ struct task_struct *next)
+{
+ struct cgroup *cgrp;
+
+ if (prev->pid != pid)
+ return 0;
+
+ cgrp = bpf_task_get_cgroup(prev);
+ if (cgrp == NULL)
+ return 0;
+ cgroup_id = cgrp->kn->id;
+
+ bpf_cgroup_release(cgrp);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";