From: Jose Fernandez <josef@netflix.com>
To: Jiri Olsa <olsajiri@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org,
Tycho Andersen <tycho@tycho.pizza>
Subject: Re: [PATCH V2 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup
Date: Mon, 18 Mar 2024 18:30:54 -0600 [thread overview]
Message-ID: <20240319003054.tegzffeydaec45ql@SlimPro9i> (raw)
In-Reply-To: <ZfgQXlU_wKpXScwh@krava>
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
> >
next prev parent reply other threads:[~2024-03-19 0:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-16 16:22 [PATCH V2 bpf-next 1/2] bpf: add bpf_task_get_cgroup kfunc Jose Fernandez
2024-03-16 16:22 ` [PATCH V2 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup Jose Fernandez
2024-03-18 3:10 ` Ratheesh Kannoth
2024-03-18 3:15 ` Alexei Starovoitov
2024-03-18 9:58 ` Jiri Olsa
2024-03-19 0:30 ` Jose Fernandez [this message]
2024-03-18 15:54 ` [PATCH V2 bpf-next 1/2] bpf: add bpf_task_get_cgroup kfunc Yonghong Song
2024-03-18 16:07 ` Stanislav Fomichev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240319003054.tegzffeydaec45ql@SlimPro9i \
--to=josef@netflix.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=olsajiri@gmail.com \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=tycho@tycho.pizza \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox