From: Yafang Shao Subject: [RFC PATCH bpf-next 1/5] cgroup: Enable task_under_cgroup_hierarchy() on cgroup1 Date: Sun, 3 Sep 2023 14:27:56 +0000 Message-ID: <20230903142800.3870-2-laoar.shao@gmail.com> References: <20230903142800.3870-1-laoar.shao@gmail.com> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693751289; x=1694356089; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cMXLcIWio9WW57rG1glRWMEPDKGAafnI6Sr1JX6PcGQ=; b=LzVEieRrkSFlJ8mJ+2vRTvgW+b8tuZ1vl3MVX0HWrPwLSZpeV2sJGWL3iZ6LtUg3We aIoe90saY0SE/4qVQTwIq496PdeKhSpaZHQFnmuEeS6BpoRNvz9SnXjtBK8qUPbQYy1C XOPZc6WMep7QcvL9nMB/jlhT2+1/6D5BfllT5fNX15djA+4qvqFP365NbRAu+CGjLTqF bkaheS4ccN4TYmzML38Dh6IhVwMD3mrCjyj4g17UZJRERfG0q3X9rgOrjtDC4OTCiSY5 /YxFUU7KgA5zRBQvYBVIDRPy4LQp1MpZFPEHwwzX+bGceV4mqwEdKRniJOA27wCRfk6D e1Ng== In-Reply-To: <20230903142800.3870-1-laoar.shao-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> List-ID: Content-Type: text/plain; charset="us-ascii" To: ast-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, daniel-FeC+5ew28dpmcu3hnIyYJQ@public.gmane.org, john.fastabend-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org, andrii-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, martin.lau-fxUVXftIFDnyG1zEObXtfA@public.gmane.org, song-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, yonghong.song-fxUVXftIFDnyG1zEObXtfA@public.gmane.org, kpsingh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, sdf-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, haoluo-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, jolsa-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, lizefan.x-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org, hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org, yosryahmed-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org Cc: cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, bpf-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, Yafang Shao 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 --- 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