diff mbox series

[V2,bpf-next,2/2] selftests/bpf: add selftest for bpf_task_get_cgroup

Message ID 20240316162241.628855-2-josef@netflix.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [V2,bpf-next,1/2] bpf: add bpf_task_get_cgroup kfunc | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 954 this patch: 954
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 2 maintainers not CCed: mykolal@fb.com shuah@kernel.org
netdev/build_clang success Errors and warnings before: 957 this patch: 957
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 969 this patch: 969
netdev/checkpatch warning CHECK: Comparison to NULL could be written "!cgrp" WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization

Commit Message

Jose Fernandez March 16, 2024, 4:22 p.m. UTC
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.

Signed-off-by: Jose Fernandez <josef@netflix.com>
Reviewed-by: Tycho Andersen <tycho@tycho.pizza>
---
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

Comments

Ratheesh Kannoth March 18, 2024, 3:10 a.m. UTC | #1
On 2024-03-16 at 21:52:41, Jose Fernandez (josef@netflix.com) wrote:
> 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.
>
> Signed-off-by: Jose Fernandez <josef@netflix.com>
> Reviewed-by: Tycho Andersen <tycho@tycho.pizza>
> ---
> 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
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c
> new file mode 100644
> index 000000000000..67ed65d0c461
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.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.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,
> +	};
Reverse Xmas tree.

> +
> +	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);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
> new file mode 100644
> index 000000000000..580f8f0657d5
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
> @@ -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
>
Alexei Starovoitov March 18, 2024, 3:15 a.m. UTC | #2
On Sun, Mar 17, 2024 at 8:10 PM Ratheesh Kannoth <rkannoth@marvell.com> wrote:
>
> On 2024-03-16 at 21:52:41, Jose Fernandez (josef@netflix.com) wrote:
> > 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.
> >
> > Signed-off-by: Jose Fernandez <josef@netflix.com>
> > Reviewed-by: Tycho Andersen <tycho@tycho.pizza>
> > ---
> > 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
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c
> > new file mode 100644
> > index 000000000000..67ed65d0c461
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.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.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,
> > +     };
> Reverse Xmas tree.

NO. We don't do it in bpf trees.
Jiri Olsa March 18, 2024, 9:58 a.m. UTC | #3
On Sat, Mar 16, 2024 at 10:22:41AM -0600, Jose Fernandez wrote:

SNIP

> +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;

nit, you could call test_task_get_cgroup__open_and_load

> +
> +	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

> +
> +	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);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
> new file mode 100644
> index 000000000000..580f8f0657d5
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
> @@ -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;
> +

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

> +	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
>
Jose Fernandez March 19, 2024, 12:30 a.m. UTC | #4
On 24/03/18 10:58AM, Jiri Olsa wrote:
> On Sat, Mar 16, 2024 at 10:22:41AM -0600, Jose Fernandez wrote:
> 
> SNIP
> 
> > +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;
> 
> nit, you could call test_task_get_cgroup__open_and_load

I'll rename.

> > +
> > +	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

Yes a smaller sleep should be fine.
I'll reduce the sleep and use the usleep helper.

> > +
> > +	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);
> > +}
> > diff --git a/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
> > new file mode 100644
> > index 000000000000..580f8f0657d5
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
> > @@ -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;
> > +
> 
> 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

I'll apply this suggestion as well and include it in V3. 
Thanks for the feedback.

> 
> > +	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
> >
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c
new file mode 100644
index 000000000000..67ed65d0c461
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.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.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);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
new file mode 100644
index 000000000000..580f8f0657d5
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c
@@ -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";