BPF List
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com,
	andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
	yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com,
	haoluo@google.com, jolsa@kernel.org, tj@kernel.org,
	lizefan.x@bytedance.com, hannes@cmpxchg.org,
	yosryahmed@google.com
Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org,
	Yafang Shao <laoar.shao@gmail.com>
Subject: [RFC PATCH bpf-next 1/5] cgroup: Enable task_under_cgroup_hierarchy() on cgroup1
Date: Sun,  3 Sep 2023 14:27:56 +0000	[thread overview]
Message-ID: <20230903142800.3870-2-laoar.shao@gmail.com> (raw)
In-Reply-To: <20230903142800.3870-1-laoar.shao@gmail.com>

Currently, the function task_under_cgroup_hierarchy() allows us to
determine if a task resides exclusively within a cgroup2 hierarchy.
Nevertheless, given the continued prevalence of cgroup1, it's useful that
we make a minor adjustment to extend its functionality to cgroup1 as well.
Once this modification is implemented, we will have the ability to
effortlessly verify a task's cgroup membership within BPF programs. For
instance, we can easily check if a task belongs to a cgroup1 directory,
such as /sys/fs/cgroup/cpu,cpuacct/kubepods/burstable/ or
/sys/fs/cgroup/cpu,cpuacct/kubepods/besteffort/.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 include/linux/cgroup.h | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index b307013..5414a2c 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -543,15 +543,33 @@ static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
  * @ancestor: possible ancestor of @task's cgroup
  *
  * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
- * It follows all the same rules as cgroup_is_descendant, and only applies
- * to the default hierarchy.
+ * It follows all the same rules as cgroup_is_descendant.
  */
 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
 					       struct cgroup *ancestor)
 {
 	struct css_set *cset = task_css_set(task);
+	struct cgroup *cgrp;
+	bool ret = false;
+	int ssid;
+
+	if (ancestor->root == &cgrp_dfl_root)
+		return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
+
+	for (ssid = 0; ssid < CGROUP_SUBSYS_COUNT; ssid++) {
+		if (!ancestor->subsys[ssid])
+			continue;
 
-	return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
+		cgrp = task_css(task, ssid)->cgroup;
+		if (!cgrp)
+			continue;
+
+		if (!cgroup_is_descendant(cgrp, ancestor))
+			return false;
+		if (!ret)
+			ret = true;
+	}
+	return ret;
 }
 
 /* no synchronization, the result can only be used as a hint */
-- 
1.8.3.1


  reply	other threads:[~2023-09-03 14:28 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-03 14:27 [RFC PATCH bpf-next 0/5] bpf, cgroup: Enable cgroup_array map on cgroup1 Yafang Shao
2023-09-03 14:27 ` Yafang Shao [this message]
2023-09-06 19:53   ` [RFC PATCH bpf-next 1/5] cgroup: Enable task_under_cgroup_hierarchy() " Alexei Starovoitov
2023-09-06 20:13   ` Tejun Heo
2023-09-07  3:05     ` Yafang Shao
2023-09-11 20:27       ` Tejun Heo
2023-09-18 14:45   ` Michal Koutný
2023-09-19  5:42     ` Yafang Shao
2023-09-03 14:27 ` [RFC PATCH bpf-next 2/5] bpf: Enable cgroup_array map " Yafang Shao
2023-09-06 19:54   ` Alexei Starovoitov
2023-09-03 14:27 ` [RFC PATCH bpf-next 3/5] selftests/bpf: Fix issues in setup_classid_environment() Yafang Shao
2023-09-03 14:27 ` [RFC PATCH bpf-next 4/5] selftests/bpf: Add new cgroup helper open_classid() Yafang Shao
2023-09-03 14:28 ` [RFC PATCH bpf-next 5/5] selftests/bpf: Add selftests for current_under_cgroupv1v2 Yafang Shao
2023-09-07 14:41 ` [RFC PATCH bpf-next 0/5] bpf, cgroup: Enable cgroup_array map on cgroup1 Michal Koutný
2023-09-08  2:53   ` Yafang Shao
2023-09-08 18:09     ` Alexei Starovoitov
2023-09-10  3:17       ` Yafang Shao
2023-09-11 19:53         ` Alexei Starovoitov
2023-09-11 20:24         ` Tejun Heo
2023-09-12  3:30           ` Yafang Shao
2023-09-15 17:01             ` Michal Koutný
2023-09-15 17:31               ` Tejun Heo
2023-09-17  7:28                 ` Yafang Shao
2023-09-17  7:19               ` Yafang Shao
2023-09-18 14:44                 ` Michal Koutný
2023-09-15 18:57             ` Hao Luo
2023-09-17  7:30               ` Yafang Shao

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=20230903142800.3870-2-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=hannes@cmpxchg.org \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=lizefan.x@bytedance.com \
    --cc=martin.lau@linux.dev \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=yonghong.song@linux.dev \
    --cc=yosryahmed@google.com \
    /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