From: Tejun Heo <tj@kernel.org>
To: Jose Fernandez <josef@netflix.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>,
Jiri Olsa <jolsa@kernel.org>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org,
Tycho Andersen <tycho@tycho.pizza>
Subject: Re: [PATCH V3 bpf-next 1/2] bpf: add bpf_task_get_cgroup kfunc
Date: Wed, 20 Mar 2024 07:41:17 -0700 [thread overview]
Message-ID: <Zfr1jQ6W6yaLsHID@mtj.duckdns.org> (raw)
In-Reply-To: <20240319050302.1085006-1-josef@netflix.com>
On Mon, Mar 18, 2024 at 11:03:01PM -0600, Jose Fernandez wrote:
> 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.
>
> Signed-off-by: Jose Fernandez <josef@netflix.com>
> Reviewed-by: Tycho Andersen <tycho@tycho.pizza>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>
> Acked-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Tejun Heo <tj@kernel.org>
but some questions below
> +__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;
> +}
So, as this is a lot easier in cgroup2, the above can probably be written
directly in BPF (untested and not sure the necessary annotations are in
place, so please take it with a big grain of salt):
bpf_rcu_read_lock();
cgrp = task->cgroups->dfl_cgrp;
cgrp = bpf_cgroup_from_id(cgrp->kn.id);
bpf_rcu_read_unlock();
If all you need is ID, it's even simpler:
bpf_rcu_read_lock();
cgrp_id = task->cgroups->dfl_cgrp->kn.id;
bpf_rcu_read_unlock();
In the first example, it's not great that we go from task pointer to cgroup
pointer to ID and then back to acquired cgroup pointer. I wonder whether
what we really want is to support something like the following:
bpf_rcu_read_lock();
cgrp = bpf_cgroup_tryget(task->cgroups->dfl_cgrp);
bpf_rcu_read_unlock();
What do you think?
Thanks.
--
tejun
next prev parent reply other threads:[~2024-03-20 14:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-19 5:03 [PATCH V3 bpf-next 1/2] bpf: add bpf_task_get_cgroup kfunc Jose Fernandez
2024-03-19 5:03 ` [PATCH V3 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup Jose Fernandez
2024-03-19 13:00 ` Jiri Olsa
2024-03-20 6:03 ` [PATCH V3 bpf-next 1/2] bpf: add bpf_task_get_cgroup kfunc Alexei Starovoitov
2024-03-20 14:41 ` Tejun Heo [this message]
2024-03-20 23:05 ` Alexei Starovoitov
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=Zfr1jQ6W6yaLsHID@mtj.duckdns.org \
--to=tj@kernel.org \
--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=jolsa@kernel.org \
--cc=josef@netflix.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--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