bpf: add bpf_task_get_cgroup kfunc
Subsystems (3)
PATCH v2 1/2 bpf: add bpf_task_get_cgroup kfunc
This patch enhances the BPF helpers by adding a kfunc to retrieve the cgroup v2 of a task, addressing a previous limitation where only bpf_task_get_cgroup1 was available for cgroup v1. The new kfunc is particularly useful for scenarios where obtaining the cgroup ID of a task other than the "current" one is necessary, which the existing bpf_get_current_cgroup_id helper cannot accommodate. A specific use case at Netflix involved the sched_switch tracepoint, where we had to get the cgroup IDs of both the prev and next tasks.
The bpf_task_get_cgroup kfunc acquires and returns a reference to a task's default cgroup, ensuring thread-safe access by correctly implementing RCU read locking and unlocking. It leverages the existing cgroup.h helper, and cgroup_tryget to safely acquire a reference to it.
---
V1 -> V2: Return a pointer to the cgroup instead of the cgroup ID
kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
kernel/bpf/helpers.c +26 -0
@@ -2266,6 +2266,31 @@ bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id)return NULL ; return cgrp; } + +/** +* bpf_task_get_cgroup - Acquire a reference to the default cgroup of a task. +* @task: The target task +* +* This function returns the task's default cgroup, primarily +* designed for use with cgroup v2. In cgroup v1, the concept of default +* cgroup varies by subsystem, and while this function will work with +* cgroup v1, it's recommended to use bpf_task_get_cgroup1 instead. +* A cgroup returned by this kfunc which is not subsequently stored in a +* map, must be released by calling bpf_cgroup_release(). +* +* Return: On success, the cgroup is returned. On failure, NULL is returned. +*/ +__bpf_kfunc struct cgroup *bpf_task_get_cgroup(struct task_struct *task) +{ +struct cgroup *cgrp; + +rcu_read_lock (); + cgrp =task_dfl_cgroup (task); +if (!cgroup_tryget (cgrp)) + cgrp =NULL ; +rcu_read_unlock (); +return cgrp; +}#endif /* CONFIG_CGROUPS */ /**
@@ -2573,6 +2598,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU) BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_task_get_cgroup, KF_ACQUIRE | KF_RCU | KF_RET_NULL)#endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)BTF_ID_FLAGS (func, bpf_throw)
base-commit: 4c8644f86c854c214aaabbcc24a27fa4c7e6a951
Signature · 1 line
2.40.1
PATCH v2 2/2 selftests/bpf: add selftest for bpf_task_get_cgroup
This patch adds a selftest for the bpf_task_get_cgroup 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 during a sched_switch tracepoint, and validating that the obtained cgroup ID for the previous task matches the expected cgroup ID.
---
V1 -> V2: Refactor test to work with a cgroup pointer instead of the ID
.../bpf/prog_tests/task_get_cgroup.c | 58 +++++++++++++++++++ .../bpf/progs/test_task_get_cgroup.c | 37 ++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +58 -0
@@ -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.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; +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__open (); +if (!ASSERT_OK_PTR (skel,"test_task_get_cgroup__open" )) + goto cleanup; + + err =test_task_get_cgroup__load (skel); +if (!ASSERT_OK (err,"test_task_get_cgroup__load" )) + goto cleanup; + + err =test_task_get_cgroup__attach (skel); +if (!ASSERT_OK (err,"test_task_get_cgroup__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__destroy (skel); +close (fd); +}
tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file +37 -0
@@ -0,0 +1,37 @@ +// 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; + +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) +{ +struct cgroup *cgrp; +u64 cgroup_id; +u32 pid; + + cgrp =bpf_task_get_cgroup (prev); +if (cgrp ==NULL ) +return 0 ; + cgroup_id = cgrp->kn->id; + pid = prev->pid; +bpf_map_update_elem (&pid_to_cgid_map, &pid, &cgroup_id,BPF_ANY ); + +bpf_cgroup_release (cgrp); +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.40.1
Re: [PATCH V2 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup
Quoted 36 lines · Jose Fernandez
This patch adds a selftest for the
bpf_task_get_cgroupkfunc. The test focuses on the use case of obtaining the cgroup ID of the previous task in asched_switchtracepoint.The selftest involves creating a test cgroup, attaching a BPF program that utilizes the
bpf_task_get_cgroupduring asched_switchtracepoint, and validating that the obtained cgroup ID for the previous task matches the expected cgroup ID.Signed-off-by Jose Fernandez <[email protected]>Reviewed-by Tycho Andersen <[email protected]>---
V1 -> V2: Refactor test to work with a cgroup pointer instead of the ID.../bpf/prog_tests/task_get_cgroup.c | 58 +++++++++++++++++++ .../bpf/progs/test_task_get_cgroup.c | 37 ++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_get_cgroup.ctools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +10
-0@@ -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.skel.h" +#include <unistd.h> + +#define TEST_CGROUP "/test-task-get-cgroup/" +
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c line 20
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +10
-0+void test_task_get_cgroup (void ) +{ +struct test_task_get_cgroup *skel; +int err, fd; +pid_t pid; +__u64 cgroup_id, expected_cgroup_id; +const struct timespec req = { + .tv_sec =1 , + .tv_nsec =0 , + };
Reverse Xmas tree.
Quoted 84 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +38
-0+ + fd =test__join_cgroup (TEST_CGROUP ); +if (!ASSERT_OK (fd <0 ,"test_join_cgroup_TEST_CGROUP" )) +return ; + + skel =test_task_get_cgroup__open (); +if (!ASSERT_OK_PTR (skel,"test_task_get_cgroup__open" )) + goto cleanup; + + err =test_task_get_cgroup__load (skel); +if (!ASSERT_OK (err,"test_task_get_cgroup__load" )) + goto cleanup; + + err =test_task_get_cgroup__attach (skel); +if (!ASSERT_OK (err,"test_task_get_cgroup__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__destroy (skel); +close (fd); +}tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file +37
-1@@ -0,0 +1,37 @@ +// 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; + +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) +{ +struct cgroup *cgrp; +u64 cgroup_id; +u32 pid; + + cgrp =bpf_task_get_cgroup (prev); +if (cgrp ==NULL ) +return 0 ; + cgroup_id = cgrp->kn->id; + pid = prev->pid; +bpf_map_update_elem (&pid_to_cgid_map, &pid, &cgroup_id,BPF_ANY ); + +bpf_cgroup_release (cgrp); +return 0 ; +} + +char _license[] SEC("license") = "GPL"; --2.40.1
Re: [PATCH V2 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup
Quoted 41 lines · Ratheesh Kannoth
Quoted 39 lines · Jose Fernandez
This patch adds a selftest for the
bpf_task_get_cgroupkfunc. The test focuses on the use case of obtaining the cgroup ID of the previous task in asched_switchtracepoint.The selftest involves creating a test cgroup, attaching a BPF program that utilizes the
bpf_task_get_cgroupduring asched_switchtracepoint, and validating that the obtained cgroup ID for the previous task matches the expected cgroup ID.Signed-off-by Jose Fernandez <[email protected]>Reviewed-by Tycho Andersen <[email protected]>---
V1 -> V2: Refactor test to work with a cgroup pointer instead of the ID.../bpf/prog_tests/task_get_cgroup.c | 58 +++++++++++++++++++ .../bpf/progs/test_task_get_cgroup.c | 37 ++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_get_cgroup.ctools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +13
-0@@ -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.skel.h" +#include <unistd.h> + +#define TEST_CGROUP "/test-task-get-cgroup/" + +void test_task_get_cgroup (void ) +{ +struct test_task_get_cgroup *skel;
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c line 20
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +7
-0+int err, fd; +pid_t pid; +__u64 cgroup_id, expected_cgroup_id; +const struct timespec req = { + .tv_sec =1 , + .tv_nsec =0 , + };Reverse Xmas tree.
NO. We don't do it in bpf trees.
Re: [PATCH V2 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup
In reply to Jose FernandezOn Sat, Mar 16, 2024 at 10:22:41AM -0600, Jose Fernandez wrote:
SNIP
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +12
-0+void test_task_get_cgroup (void ) +{ +struct test_task_get_cgroup *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 );
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c line 32
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +10
-0+if (!ASSERT_OK (fd <0 ,"test_join_cgroup_TEST_CGROUP" )) +return ; + + skel =test_task_get_cgroup__open (); +if (!ASSERT_OK_PTR (skel,"test_task_get_cgroup__open" )) + goto cleanup; + + err =test_task_get_cgroup__load (skel); +if (!ASSERT_OK (err,"test_task_get_cgroup__load" )) + goto cleanup;
nit, you could call test_task_get_cgroup__open_and_load
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c line 45
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +13
-0+ + err =test_task_get_cgroup__attach (skel); +if (!ASSERT_OK (err,"test_task_get_cgroup__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 );
would smaller sleep do? also we have our own usleep (in test_progs.c) that calls nanosleep
Quoted 34 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +13
-0+ + 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__destroy (skel); +close (fd); +}tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file +15
-0@@ -0,0 +1,37 @@ +// 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; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 4096); + __type(key, __u32); + __type(value, __u64);
tools/testing/selftests/bpf/progs/test_task_get_cgroup.c line 25
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file +10
-0+} 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) +{ +struct cgroup *cgrp; +u64 cgroup_id; +u32 pid; +
could you filter for your pid in here like we do in other places, (eg in progs/kprobe_multi.c)
in which case you won't need hash map, but just a single value to store the cgroup id to
jirka
Quoted 15 lines
tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file +12
-0+ cgrp =bpf_task_get_cgroup (prev); +if (cgrp ==NULL ) +return 0 ; + cgroup_id = cgrp->kn->id; + pid = prev->pid; +bpf_map_update_elem (&pid_to_cgid_map, &pid, &cgroup_id,BPF_ANY ); + +bpf_cgroup_release (cgrp); +return 0 ; +} + +char _license[] SEC("license") = "GPL";Signature · 1 line
2.40.1
Re: [PATCH V2 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup
Quoted 20 lines · Jiri Olsa
On Sat, Mar 16, 2024 at 10:22:41AM -0600, Jose Fernandez wrote:
SNIP
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +16
-0+void test_task_get_cgroup (void ) +{ +struct test_task_get_cgroup *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__open ();
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c line 32
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +6
-0+if (!ASSERT_OK_PTR (skel,"test_task_get_cgroup__open" )) + goto cleanup; + + err =test_task_get_cgroup__load (skel); +if (!ASSERT_OK (err,"test_task_get_cgroup__load" )) + goto cleanup;nit, you could call
test_task_get_cgroup__open_and_load
I'll rename.
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +8
-0+ + err =test_task_get_cgroup__attach (skel); +if (!ASSERT_OK (err,"test_task_get_cgroup__attach" )) + goto cleanup; + + pid =getpid (); + expected_cgroup_id =get_cgroup_id (TEST_CGROUP ); +if (!ASSERT_GT (expected_cgroup_id,0 ,"get_cgroup_id" ))
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c line 45
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +5
-0+ goto cleanup; + +/* Trigger nanosleep to enter the sched_switch tracepoint */ +/* The previous task should be this process */ +syscall (__NR_nanosleep, &req,NULL );would smaller sleep do? also we have our own usleep (in
test_progs.c) that calls nanosleep
Yes a smaller sleep should be fine.
I'll reduce the sleep and use the usleep helper.
Quoted 48 lines
Quoted 34 lines
tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file +13
-0+ + 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__destroy (skel); +close (fd); +}tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file +15
-0@@ -0,0 +1,37 @@ +// 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; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 4096); + __type(key, __u32); + __type(value, __u64);tools/testing/selftests/bpf/progs/test_task_get_cgroup.c line 25
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file +10
-0+} 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) +{ +struct cgroup *cgrp; +u64 cgroup_id; +u32 pid; +could you filter for your pid in here like we do in other places, (eg in
progs/kprobe_multi.c)
Quoted 4 lines
in which case you won't need hash map, but just a single value to store the cgroup id to
jirka
I'll apply this suggestion as well and include it in V3. Thanks for the feedback.
Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file +12
-0+ cgrp =bpf_task_get_cgroup (prev); +if (cgrp ==NULL ) +return 0 ; + cgroup_id = cgrp->kn->id; + pid = prev->pid; +bpf_map_update_elem (&pid_to_cgid_map, &pid, &cgroup_id,BPF_ANY ); + +bpf_cgroup_release (cgrp); +return 0 ; +} + +char _license[] SEC("license") = "GPL";Signature · 1 line
2.40.1
Quoted 9 lines · Jose Fernandez
This patch enhances the BPF helpers by adding a kfunc to retrieve the cgroup v2 of a task, addressing a previous limitation where only
bpf_task_get_cgroup1was available for cgroup v1. The new kfunc is particularly useful for scenarios where obtaining the cgroup ID of a task other than the "current" one is necessary, which the existingbpf_get_current_cgroup_idhelper cannot accommodate. A specific use case at Netflix involved thesched_switchtracepoint, where we had to get the cgroup IDs of both the prev and next tasks.
Quoted 7 lines
The
bpf_task_get_cgroupkfunc acquires and returns a reference to a task's default cgroup, ensuring thread-safe access by correctly implementing RCU read locking and unlocking. It leverages the existingcgroup.hhelper, andcgroup_trygetto safely acquire a reference to it.Signed-off-by Jose Fernandez <[email protected]>Reviewed-by Tycho Andersen <[email protected]>
Quoted 9 lines · Jose Fernandez
This patch enhances the BPF helpers by adding a kfunc to retrieve the cgroup v2 of a task, addressing a previous limitation where only
bpf_task_get_cgroup1was available for cgroup v1. The new kfunc is particularly useful for scenarios where obtaining the cgroup ID of a task other than the "current" one is necessary, which the existingbpf_get_current_cgroup_idhelper cannot accommodate. A specific use case at Netflix involved thesched_switchtracepoint, where we had to get the cgroup IDs of both the prev and next tasks.
Quoted 7 lines
The
bpf_task_get_cgroupkfunc acquires and returns a reference to a task's default cgroup, ensuring thread-safe access by correctly implementing RCU read locking and unlocking. It leverages the existingcgroup.hhelper, andcgroup_trygetto safely acquire a reference to it.Signed-off-by Jose Fernandez <[email protected]>Reviewed-by Tycho Andersen <[email protected]>