Skip to contentSkip to conversation

bpf, cgroup: Add BPF support for cgroup1 hierarchy

RFC PATCH Superseded Yafang Shao 4 participants 28 messages Oct 7, 2023 – Oct 10, 2023

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover bpf, cgroup: Add BPF support for cgroup1 hierarchy

Currently, BPF is primarily confined to cgroup2, with the exception of cgroup_iter, which supports cgroup1 fds. Unfortunately, this limitation prevents us from harnessing the full potential of BPF within cgroup1 environments.

In our endeavor to seamlessly integrate BPF within our Kubernetes environment, which relies on cgroup1, we have been exploring the possibility of transitioning to cgroup2. While this transition is forward-looking, it poses challenges due to the necessity for numerous applications to adapt.

While we acknowledge that cgroup2 represents the future, we also recognize that such transitions demand time and effort. As a result, we are considering an alternative approach. Instead of migrating to cgroup2, we are contemplating modifications to the BPF kernel code to ensure compatibility with cgroup1. These adjustments appear to be relatively minor, making this option more feasible.

Given the widespread use of cgroup1 in container environments, this change would be beneficial to many users.

As discussed with Tejun[1], it has been determined that tying the interface directly to the cgroup1 hierarchies is acceptable. As a result, this patchset introduces cgroup1-only interfaces that operate with both hierarchy ID and cgroup ID as parameters.

Within this patchset, two new cgroup1-only interfaces have been introduced:

- [bpf_]task_cgroup1_id_within_hierarchy
Retrieves the associated cgroup ID of a task whithin a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID.
- [bpf_]task_ancestor_cgroup1_id_within_hierarchy
Retrieves the associated ancestor cgroup ID of a task whithin a specific cgroup1 hierarchy. he specific ancestor cgroup is determined by the ancestor level within the cgroup1 hierarchy.

These two new kfuncs enable the tracing of tasks within a designated container or its ancestor cgroup directory in BPF programs. Additionally, they are capable of operating on named cgroups, providing valuable utility for hybrid cgroup mode scenarios.

[1]. https://lwn.net/ml/cgroups/[email protected]/

Changes:
- bpf, cgroup: Add bpf support for cgroup controller
https://lwn.net/Articles/945318/
- bpf, cgroup: Enable cgroup_array map on cgroup1
https://lore.kernel.org/bpf/[email protected]/

Yafang Shao (8):
cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root()
cgroup: Add new helpers for cgroup1 hierarchy
bpf: Add kfuncs for cgroup1 hierarchy
selftests/bpf: Fix issues in setup_classid_environment() selftests/bpf: Add parallel support for classid selftests/bpf: Add a new cgroup helper get_classid_cgroup_id() selftests/bpf: Add a new cgroup helper get_cgroup_hierarchy_id() selftests/bpf: Add selftests for cgroup1 hierarchy

 include/linux/cgroup.h                        |   9 +-
 kernel/bpf/helpers.c                          |  26 +++
 kernel/cgroup/cgroup-internal.h               |   2 -
 kernel/cgroup/cgroup-v1.c                     |  67 ++++++++
 kernel/cgroup/cgroup.c                        |   5 +-
 tools/testing/selftests/bpf/cgroup_helpers.c  | 114 +++++++++++--
 tools/testing/selftests/bpf/cgroup_helpers.h  |   4 +-
 .../bpf/prog_tests/cgroup1_hierarchy.c        | 159 ++++++++++++++++++
 .../selftests/bpf/prog_tests/cgroup_v1v2.c    |   2 +-
 .../bpf/progs/test_cgroup1_hierarchy.c        |  62 +++++++
 10 files changed, 426 insertions(+), 24 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c
Signature · 1 line
2.30.1 (Apple Git-130)
#1 lore raw

RFC PATCH 1/8 cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root()

The task cannot modify cgroups if we have already acquired the css_set_lock, thus eliminating the need to hold the cgroup_mutex. Following this change, task_cgroup_from_root() can be employed in non-sleepable contexts.

---
 kernel/cgroup/cgroup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup.c +3 -2
index 1fb7f562289d..bd1692f48be5 100644
@@ -1453,7 +1453,7 @@ static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
 
 /*
  * Return the cgroup for "task" from the given hierarchy. Must be
- * called with cgroup_mutex and css_set_lock held.
+ * called with css_set_lock held.
  */
 struct cgroup *task_cgroup_from_root(struct task_struct *task,
 				     struct cgroup_root *root)
@@ -1462,7 +1462,8 @@ struct cgroup *task_cgroup_from_root(struct task_struct *task,
 	 * No need to lock the task - since we hold css_set_lock the
 	 * task can't change groups.
 	 */
-	return cset_cgroup_from_root(task_css_set(task), root);
+	lockdep_assert_held(&css_set_lock);
+	return __cset_cgroup_from_root(task_css_set(task), root);
 }
 
 /*
Signature · 1 line
2.30.1 (Apple Git-130)
maintainer
#2 lore raw

Re: [RFC PATCH bpf-next 1/8] cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root()

Quoted 5 lines · Yafang Shao

The task cannot modify cgroups if we have already acquired the css_set_lock, thus eliminating the need to hold the cgroup_mutex. Following this change, task_cgroup_from_root() can be employed in non-sleepable contexts.

Maybe just drop lockdep_assert_held(&cgroup_mutex) from cset_cgroup_from_root()?

Thanks.

Signature · 1 line
tejun
#3 lore raw

Re: [RFC PATCH bpf-next 1/8] cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root()

Quoted 10 lines · Tejun Heo
Quoted 5 lines · Yafang Shao

The task cannot modify cgroups if we have already acquired the css_set_lock, thus eliminating the need to hold the cgroup_mutex. Following this change, task_cgroup_from_root() can be employed in non-sleepable contexts.

Maybe just drop lockdep_assert_held(&cgroup_mutex) from cset_cgroup_from_root()?

It seems we can do it.
will do it in the next version.

Signature · 2 lines
Regards
Yafang
maintainer
#4 lore raw

Re: [RFC PATCH bpf-next 1/8] cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root()

In reply to Yafang Shao
Quoted 3 lines · Yafang Shao

The task cannot modify cgroups if we have already acquired the css_set_lock, thus eliminating the need to hold the cgroup_mutex. Following this change, task_cgroup_from_root() can be employed in non-sleepable contexts.

IIRC, cset_cgroup_from_root() needs cgroup_mutex to make sure the root doesn't disappear under hands (synchronization with cgroup_destroy_root().
However, as I look at it now, cgroup_mutex won't synchronize against cgroup_kill_sb(), it may worked by accident(?) nowadays (i.e. callers pinned the root implicitly in another way).

Still, it'd be good to have an annotation that ensures, the root is around when using it. (RCU read lock might be fine but you'd need cgroup_get_live() if passing it out of the RCU read section.)

Basically, the code must be made safe against cgroup v1 unmounts.

Michal

#5 lore raw

Re: [RFC PATCH bpf-next 1/8] cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root()

Quoted 9 lines · Michal Koutný
Quoted 3 lines · Yafang Shao

The task cannot modify cgroups if we have already acquired the css_set_lock, thus eliminating the need to hold the cgroup_mutex. Following this change, task_cgroup_from_root() can be employed in non-sleepable contexts.

IIRC, cset_cgroup_from_root() needs cgroup_mutex to make sure the root doesn't disappear under hands (synchronization with cgroup_destroy_root().

current_cgns_cgroup_from_root() doesn't hold the cgroup_mutext as well. Could this potentially lead to issues, such as triggering the BUG_ON() in __cset_cgroup_from_root(), if the root has already been destroyed?

Quoted 9 lines

However, as I look at it now, cgroup_mutex won't synchronize against cgroup_kill_sb(), it may worked by accident(?) nowadays (i.e. callers pinned the root implicitly in another way).

Still, it'd be good to have an annotation that ensures, the root is around when using it. (RCU read lock might be fine but you'd need cgroup_get_live() if passing it out of the RCU read section.)

Basically, the code must be made safe against cgroup v1 unmounts.

What we aim to protect against is changes to the root_list, which occur exclusively during cgroup setup and destroy paths. Would it be beneficial to introduce a dedicated root_list_lock specifically for this purpose? This approach could potentially reduce the need for the broader cgroup_mutex in other scenarios.

--
Regards
Yafang

maintainer
#6 lore raw

Re: [RFC PATCH bpf-next 1/8] cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root()

Hi Yafang.

Quoted 4 lines · Yafang Shao

current_cgns_cgroup_from_root() doesn't hold the cgroup_mutext as well. Could this potentially lead to issues, such as triggering the BUG_ON() in __cset_cgroup_from_root(), if the root has already been destroyed?

current_cgns_cgroup_from_root() is a tricky one, see also
https://lore.kernel.org/r/[email protected]/

I argued there with RCU read lock but when I look at it now, it may not be sufficient for the cgroup returned from current_cgns_cgroup_from_root().

The 2nd half still applies, umount synchronization is ensured via VFS layer, so the cgroup_root nor its cgroup won't go away in the only caller cgroup_show_path().

Quoted 3 lines

Would it be beneficial to introduce a dedicated root_list_lock specifically for this purpose? This approach could potentially reduce the need for the broader cgroup_mutex in other scenarios.

It may be a convenience lock but v2 (cgrp_dfl_root could make do just fine without it).

I'm keeping this dicussuion to illustrate the difficulties of adding the BPF support for cgroup v1. That is a benefit I see ;-)

Michal

#7 lore raw

Re: [RFC PATCH bpf-next 1/8] cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root()

Quoted 12 lines · Michal Koutný

Hi Yafang.

Quoted 4 lines · Yafang Shao

current_cgns_cgroup_from_root() doesn't hold the cgroup_mutext as well. Could this potentially lead to issues, such as triggering the BUG_ON() in __cset_cgroup_from_root(), if the root has already been destroyed?

current_cgns_cgroup_from_root() is a tricky one, see also
https://lore.kernel.org/r/[email protected]/

Quoted 6 lines

I argued there with RCU read lock but when I look at it now, it may not be sufficient for the cgroup returned from current_cgns_cgroup_from_root().

The 2nd half still applies, umount synchronization is ensured via VFS layer, so the cgroup_root nor its cgroup won't go away in the only caller cgroup_show_path().

Thanks for your explanation.

Quoted 8 lines
Quoted 5 lines

Would it be beneficial to introduce a dedicated root_list_lock specifically for this purpose? This approach could potentially reduce the need for the broader cgroup_mutex in other scenarios.

It may be a convenience lock but v2 (cgrp_dfl_root could make do just fine without it).

Right. cgroup2 doesn't require it.

Quoted 3 lines

I'm keeping this dicussuion to illustrate the difficulties of adding the BPF support for cgroup v1. That is a benefit I see ;-)

A potentially more effective approach might be to employ RCU protection for the cgroup_list. That said, use list_for_each_entry_rcu/list_add_rcu/list_del_rcu instead.

--
Regards

Yafang

#8 lore raw

RFC PATCH 2/8 cgroup: Add new helpers for cgroup1 hierarchy

In reply to Yafang Shao

Two new helpers are added for cgroup1 hierarchy:

- task_cgroup1_id_within_hierarchy
Retrieves the associated cgroup ID of a task within a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID.
- task_ancestor_cgroup1_id_within_hierarchy
Retrieves the associated ancestor cgroup ID of a task whithin a specific cgroup1 hierarchy. The specific ancestor level is determined by its ancestor level.

These helper functions have been added to facilitate the tracing of tasks within a particular container or cgroup in BPF programs. It's important to note that these helpers are designed specifically for cgroup1.

---
 include/linux/cgroup.h          |  9 ++++-
 kernel/cgroup/cgroup-internal.h |  2 -
 kernel/cgroup/cgroup-v1.c       | 67 +++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 3 deletions(-)
include/linux/cgroup.h +8 -1
index b307013b9c6c..65bde6eb41ef 100644
@@ -71,6 +71,8 @@ struct css_task_iter {
 extern struct file_system_type cgroup_fs_type;
 extern struct cgroup_root cgrp_dfl_root;
 extern struct css_set init_css_set;
+extern struct list_head cgroup_roots;
+extern spinlock_t css_set_lock;
 
 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
 #include <linux/cgroup_subsys.h>
@@ -159,6 +161,8 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
 			 struct css_task_iter *it);
 struct task_struct *css_task_iter_next(struct css_task_iter *it);
 void css_task_iter_end(struct css_task_iter *it);
+struct cgroup *task_cgroup_from_root(struct task_struct *task,
+				     struct cgroup_root *root);
 
 /**
  * css_for_each_child - iterate through children of a css
@@ -388,7 +392,6 @@ static inline void cgroup_unlock(void)
  * as locks used during the cgroup_subsys::attach() methods.
  */
 #ifdef CONFIG_PROVE_RCU
-extern spinlock_t css_set_lock;
 #define task_css_set_check(task, __c)					\
 	rcu_dereference_check((task)->cgroups,				\
 		rcu_read_lock_sched_held() ||				\
@@ -855,4 +858,8 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
 
 #endif /* CONFIG_CGROUP_BPF */
 
+u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id);
+u64 task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id,
+					      int ancestor_level);
+
 #endif /* _LINUX_CGROUP_H */
kernel/cgroup/cgroup-internal.h +0 -2
index c56071f150f2..2c32a80c1334 100644
@@ -164,9 +164,7 @@ struct cgroup_mgctx {
 #define DEFINE_CGROUP_MGCTX(name)						\
 	struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name)
 
-extern spinlock_t css_set_lock;
 extern struct cgroup_subsys *cgroup_subsys[];
-extern struct list_head cgroup_roots;
 
 /* iterate across the hierarchies */
 #define for_each_root(root)						\
kernel/cgroup/cgroup-v1.c +67 -0

73 lines not on this page · raw message

Signature · 1 line
2.30.1 (Apple Git-130)
maintainer
#9 lore raw

Re: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy

Hello,

include/linux/cgroup.h struct css_task_iter { · line 75

Quoted 10 lines · Yafang Shao
include/linux/cgroup.h +2 -0
index b307013b9c6c..65bde6eb41ef 100644
@@ -71,6 +71,8 @@ struct css_task_iter {
 extern struct file_system_type cgroup_fs_type;
 extern struct cgroup_root cgrp_dfl_root;
 extern struct css_set init_css_set;
+extern struct list_head cgroup_roots;
+extern spinlock_t css_set_lock;

css_set_lock was already out here but why do we need to move cgrou_roots to this header?

Quoted 10 lines
include/linux/cgroup.h +10 -0
+/**
+ * task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID from
+ * a task within a specific cgroup1 hierarchy.
+ * @task: The task to be tested
+ * @hierarchy_id: The hierarchy ID of a cgroup1
+ *
+ * We limit it to cgroup1 only.
+ */
+u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id)
+{

...

Quoted 14 lines
include/linux/cgroup.h +14 -0
+}
+
+/**
+ * task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated ancestor
+ * cgroup ID from a task within a specific cgroup1 hierarchy.
+ * @task: The task to be tested
+ * @hierarchy_id: The hierarchy ID of a cgroup1
+ * @ancestor_level: level of ancestor to find starting from root
+ *
+ * We limit it to cgroup1 only.
+ */
+u64 task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id,
+					      int ancestor_level)
+{

...

Quoted 1 line

+}

I'd much prefer to have struct group *task_get_cgroup1_within_hierarchy() then the caller can do cgroup_ancestor() itself.

Thanks.

Signature · 1 line
tejun
#10 lore raw

Re: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy

Quoted 4 lines · Tejun Heo

Hello,

On Sat, Oct 07, 2023 at 02:02:58PM +0000, Yafang Shao wrote:

Quoted 13 lines

include/linux/cgroup.h struct css_task_iter { · line 75

Quoted 10 lines
include/linux/cgroup.h +2 -0
index b307013b9c6c..65bde6eb41ef 100644
@@ -71,6 +71,8 @@ struct css_task_iter {
 extern struct file_system_type cgroup_fs_type;
 extern struct cgroup_root cgrp_dfl_root;
 extern struct css_set init_css_set;
+extern struct list_head cgroup_roots;
+extern spinlock_t css_set_lock;

css_set_lock was already out here but why do we need to move cgrou_roots to this header?

Ah, shouldn't export cgrou_roots. Thanks for pointing it out.

Quoted 27 lines
Quoted 11 lines
include/linux/cgroup.h +10 -0
+/**
+ * task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID from
+ * a task within a specific cgroup1 hierarchy.
+ * @task: The task to be tested
+ * @hierarchy_id: The hierarchy ID of a cgroup1
+ *
+ * We limit it to cgroup1 only.
+ */
+u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id)
+{

...

Quoted 14 lines
include/linux/cgroup.h +14 -0
+}
+
+/**
+ * task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated ancestor
+ * cgroup ID from a task within a specific cgroup1 hierarchy.
+ * @task: The task to be tested
+ * @hierarchy_id: The hierarchy ID of a cgroup1
+ * @ancestor_level: level of ancestor to find starting from root
+ *
+ * We limit it to cgroup1 only.
+ */
+u64 task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id,
+                                           int ancestor_level)
+{

...

Quoted 4 lines
Quoted 1 line

+}

I'd much prefer to have struct group *task_get_cgroup1_within_hierarchy() then the caller can do cgroup_ancestor() itself.

Good suggestion. will do it in the next version.

Signature · 2 lines
Regards
Yafang
#11 lore raw

Re: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy

In reply to Yafang Shao
kernel test robot noticed the following build warnings:

Hi Yafang,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/cgroup-Don-t-have-to-hold-cgroup_mutex-in-task_cgroup_from_root/20231007-220510
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20231007140304.4390-3-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231008/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231008/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> kernel/cgroup/cgroup-v1.c:1275: warning: Function parameter or member 'tsk' not described in 'task_cgroup1_id_within_hierarchy'
>> kernel/cgroup/cgroup-v1.c:1275: warning: expecting prototype for task_cgroup_id_within_hierarchy(). Prototype was for task_cgroup1_id_within_hierarchy() instead
>> kernel/cgroup/cgroup-v1.c:1307: warning: Function parameter or member 'tsk' not described in 'task_ancestor_cgroup1_id_within_hierarchy'
>> kernel/cgroup/cgroup-v1.c:1307: warning: expecting prototype for task_ancestor_cgroup_id_within_hierarchy(). Prototype was for task_ancestor_cgroup1_id_within_hierarchy() instead

vim +1275 kernel/cgroup/cgroup-v1.c

1265
1266 /**
1267 * task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID from
1268 * a task within a specific cgroup1 hierarchy.
1269 * @task: The task to be tested
1270 * @hierarchy_id: The hierarchy ID of a cgroup1
1271 *
1272 * We limit it to cgroup1 only.
1273 */
1274 u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id)

> 1275	{
  1276		struct cgroup_root *root;
  1277		struct cgroup *cgrp;
  1278		u64 cgid = 0;
  1279	
  1280		spin_lock_irq(&css_set_lock);
  1281		list_for_each_entry(root, &cgroup_roots, root_list) {
  1282			/* cgroup1 only*/
  1283			if (root == &cgrp_dfl_root)
  1284				continue;
  1285			if (root->hierarchy_id != hierarchy_id)
  1286				continue;
  1287			cgrp = task_cgroup_from_root(tsk, root);
  1288			WARN_ON_ONCE(!cgrp);
  1289			cgid = cgroup_id(cgrp);
  1290			break;
  1291		}
  1292		spin_unlock_irq(&css_set_lock);
  1293		return cgid;
  1294	}
  1295	
  1296	/**
  1297	 * task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated ancestor
  1298	 * cgroup ID from a task within a specific cgroup1 hierarchy.
  1299	 * @task: The task to be tested
  1300	 * @hierarchy_id: The hierarchy ID of a cgroup1
  1301	 * @ancestor_level: level of ancestor to find starting from root
  1302	 *
  1303	 * We limit it to cgroup1 only.
  1304	 */
  1305	u64 task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id,
  1306						      int ancestor_level)
> 1307	{

1308 struct cgroup *cgrp, *ancestor;
1309 struct cgroup_root *root;
1310 u64 cgid = 0;
1311
1312 spin_lock_irq(&css_set_lock);
1313 list_for_each_entry(root, &cgroup_roots, root_list) {
1314 /* cgroup1 only*/
1315 if (root == &cgrp_dfl_root)
1316 continue;
1317 if (root->hierarchy_id != hierarchy_id)
1318 continue;
1319
1320 cgrp = task_cgroup_from_root(tsk, root);
1321 WARN_ON_ONCE(!cgrp);
1322 ancestor = cgroup_ancestor(cgrp, ancestor_level);
1323 if (!ancestor)
1324 break;
1325
1326 cgid = cgroup_id(ancestor);
1327 break;
1328 }
1329 spin_unlock_irq(&css_set_lock);
1330 return cgid;
1331 }
1332

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#12 lore raw

Re: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy

Quoted 97 lines · kernel test robot

Hi Yafang,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/cgroup-Don-t-have-to-hold-cgroup_mutex-in-task_cgroup_from_root/20231007-220510
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20231007140304.4390-3-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231008/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231008/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 4 lines
Quoted 4 lines

kernel/cgroup/cgroup-v1.c:1275: warning: Function parameter or member 'tsk' not described in 'task_cgroup1_id_within_hierarchy' kernel/cgroup/cgroup-v1.c:1275: warning: expecting prototype for task_cgroup_id_within_hierarchy(). Prototype was for task_cgroup1_id_within_hierarchy() instead kernel/cgroup/cgroup-v1.c:1307: warning: Function parameter or member 'tsk' not described in 'task_ancestor_cgroup1_id_within_hierarchy' kernel/cgroup/cgroup-v1.c:1307: warning: expecting prototype for task_ancestor_cgroup_id_within_hierarchy(). Prototype was for task_ancestor_cgroup1_id_within_hierarchy() instead

vim +1275 kernel/cgroup/cgroup-v1.c

1265
1266 /**

  1267   * task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID from
  1268   * a task within a specific cgroup1 hierarchy.
  1269   * @task: The task to be tested
  1270   * @hierarchy_id: The hierarchy ID of a cgroup1
  1271   *
  1272   * We limit it to cgroup1 only.
  1273   */

1274 u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id)

Quoted 1 line

1275 {

  1276          struct cgroup_root *root;
  1277          struct cgroup *cgrp;
  1278          u64 cgid = 0;
  1279
  1280          spin_lock_irq(&css_set_lock);
  1281          list_for_each_entry(root, &cgroup_roots, root_list) {
  1282                  /* cgroup1 only*/
  1283                  if (root == &cgrp_dfl_root)
  1284                          continue;
  1285                  if (root->hierarchy_id != hierarchy_id)
  1286                          continue;
  1287                  cgrp = task_cgroup_from_root(tsk, root);
  1288                  WARN_ON_ONCE(!cgrp);
  1289                  cgid = cgroup_id(cgrp);
  1290                  break;
  1291          }
  1292          spin_unlock_irq(&css_set_lock);
  1293          return cgid;
  1294  }
  1295
  1296  /**
  1297   * task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated ancestor
  1298   * cgroup ID from a task within a specific cgroup1 hierarchy.
  1299   * @task: The task to be tested
  1300   * @hierarchy_id: The hierarchy ID of a cgroup1
  1301   * @ancestor_level: level of ancestor to find starting from root
  1302   *
  1303   * We limit it to cgroup1 only.
  1304   */

1305 u64 task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id,
1306 int ancestor_level)

Quoted 1 line

1307 {

  1308          struct cgroup *cgrp, *ancestor;
  1309          struct cgroup_root *root;
  1310          u64 cgid = 0;
  1311
  1312          spin_lock_irq(&css_set_lock);
  1313          list_for_each_entry(root, &cgroup_roots, root_list) {
  1314                  /* cgroup1 only*/
  1315                  if (root == &cgrp_dfl_root)
  1316                          continue;
  1317                  if (root->hierarchy_id != hierarchy_id)
  1318                          continue;
  1319
  1320                  cgrp = task_cgroup_from_root(tsk, root);
  1321                  WARN_ON_ONCE(!cgrp);
  1322                  ancestor = cgroup_ancestor(cgrp, ancestor_level);
  1323                  if (!ancestor)
  1324                          break;
  1325
  1326                  cgid = cgroup_id(ancestor);
  1327                  break;
  1328          }
  1329          spin_unlock_irq(&css_set_lock);
  1330          return cgid;
Quoted 6 lines

1331 }
1332

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

Thanks for your report. will fix it.

Signature · 2 lines
Regards
Yafang
maintainer
#13 lore raw

Re: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy

In reply to Yafang Shao

Hello.

Quoted 10 lines · Yafang Shao

Two new helpers are added for cgroup1 hierarchy:

- task_cgroup1_id_within_hierarchy
Retrieves the associated cgroup ID of a task within a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID.
- task_ancestor_cgroup1_id_within_hierarchy
Retrieves the associated ancestor cgroup ID of a task whithin a specific cgroup1 hierarchy. The specific ancestor level is determined by its ancestor level.

Quoted 3 lines

These helper functions have been added to facilitate the tracing of tasks within a particular container or cgroup in BPF programs. It's important to note that these helpers are designed specifically for cgroup1.

Are this helpers need for any 3rd party task?

I *think* operating on current would be simpler wrt assumptions needed for object presense.

kernel/cgroup/cgroup-v1.c int cgroup1_get_tree(struct fs_context *fc) · line 1281

Quoted 8 lines
kernel/cgroup/cgroup-v1.c +8 -0
+u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id)
+{
+	struct cgroup_root *root;
+	struct cgroup *cgrp;
+	u64 cgid = 0;
+
+	spin_lock_irq(&css_set_lock);
+	list_for_each_entry(root, &cgroup_roots, root_list) {

This should be for_each_root() macro for better uniform style.

Michal

#14 lore raw

Re: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy

Quoted 14 lines · Michal Koutný

Hello.

Quoted 10 lines · Yafang Shao

Two new helpers are added for cgroup1 hierarchy:

- task_cgroup1_id_within_hierarchy
Retrieves the associated cgroup ID of a task within a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID.
- task_ancestor_cgroup1_id_within_hierarchy
Retrieves the associated ancestor cgroup ID of a task whithin a specific cgroup1 hierarchy. The specific ancestor level is determined by its ancestor level.

Quoted 5 lines
Quoted 3 lines

These helper functions have been added to facilitate the tracing of tasks within a particular container or cgroup in BPF programs. It's important to note that these helpers are designed specifically for cgroup1.

Are this helpers need for any 3rd party task?

Yes, for example, we can check if the *next* task in sched_switch belongs to a specific cgroup.

Hao also pointed out the use case of a 3rd partry task[1].

[1]. https://lore.kernel.org/bpf/CA+khW7hah317_beZ7QDA1R=sWi5q7TanRC+efMhivPKtWzbA4w@mail.gmail.com/

Quoted 5 lines

I *think* operating on current would be simpler wrt assumptions needed for object presense.

Quoted 10 lines

kernel/cgroup/cgroup-v1.c int cgroup1_get_tree(struct fs_context *fc) · line 1281

Quoted 8 lines
kernel/cgroup/cgroup-v1.c +8 -0
+u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id)
+{
+     struct cgroup_root *root;
+     struct cgroup *cgrp;
+     u64 cgid = 0;
+
+     spin_lock_irq(&css_set_lock);
+     list_for_each_entry(root, &cgroup_roots, root_list) {

This should be for_each_root() macro for better uniform style.

will use it in the next version.

Signature · 2 lines
Regards
Yafang
maintainer
#15 lore raw

Re: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy

Quoted 1 line · Yafang Shao

Hao also pointed out the use case of a 3rd partry task[1].

Sigh (I must have expulsed that mail from my mind).

Is the WARN_ON_ONCE(!cgrp); of any use when
__cset_cgroup_from_root() has

        BUG_ON(!res_cgroup);

?

Thanks,
Michal

#16 lore raw

Re: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy

Quoted 10 lines · Michal Koutný
Quoted 1 line · Yafang Shao

Hao also pointed out the use case of a 3rd partry task[1].

Sigh (I must have expulsed that mail from my mind).

Is the WARN_ON_ONCE(!cgrp); of any use when
__cset_cgroup_from_root() has

        BUG_ON(!res_cgroup);

?

It is useless. will drop it. Thank you for pointing it out.

Signature · 2 lines
Regards
Yafang
#17 lore raw

RFC PATCH 3/8 bpf: Add kfuncs for cgroup1 hierarchy

In reply to Yafang Shao

Two new kfuncs are added to retrieve the cgroup1 IDs.

- bpf_task_cgroup1_id_within_hierarchy
Retrieves the associated cgroup ID of a task whithin a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID.
- bpf_task_ancestor_cgroup1_id_within_hierarchy
Retrieves the associated ancestor cgroup ID of a task whithin a specific cgroup1 hierarchy. he specific ancestor cgroup is determined by the ancestor level within the cgroup1 hierarchy.

These two new kfuncs enable the tracing of tasks within a designated container or cgroup directory in BPF programs.

---
 kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/bpf/helpers.c +26 -0
index dd1c69ee3375..39ec6f9f2027 100644
@@ -2214,6 +2214,30 @@ __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
 {
 	return task_under_cgroup_hierarchy(task, ancestor);
 }
+
+/**
+ * bpf_task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID of a
+ * task within a specific cgroup1 hierarchy.
+ * @task: The target task
+ * @hierarchy_id: The ID of a cgroup1 hierarchy
+ */
+__bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id)
+{
+	return task_cgroup1_id_within_hierarchy(task, hierarchy_id);
+}
+
+/**
+ * bpf_task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated
+ * ancestor cgroup ID of a task within a specific cgroup1 hierarchy.
+ * @task: The target task
+ * @hierarchy_id: The ID of a cgroup1 hierarchy
+ * @ancestor_level: The cgroup level of the ancestor in the cgroup1 hierarchy
+ */
+__bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task,
+							      int hierarchy_id, int ancestor_level)
+{
+	return task_ancestor_cgroup1_id_within_hierarchy(task, hierarchy_id, ancestor_level);
+}
 #endif /* CONFIG_CGROUPS */
 
 /**
@@ -2520,6 +2544,8 @@ BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE)
 BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU)
+BTF_ID_FLAGS(func, bpf_task_cgroup1_id_within_hierarchy, KF_RCU)
+BTF_ID_FLAGS(func, bpf_task_ancestor_cgroup1_id_within_hierarchy, KF_RCU)
 #endif
 BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_throw)
Signature · 1 line
2.30.1 (Apple Git-130)
maintainer
#18 lore raw

Re: [RFC PATCH bpf-next 3/8] bpf: Add kfuncs for cgroup1 hierarchy

Quoted 14 lines · Yafang Shao
kernel/bpf/helpers.c +14 -0
+
+/**
+ * bpf_task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID of a
+ * task within a specific cgroup1 hierarchy.
+ * @task: The target task
+ * @hierarchy_id: The ID of a cgroup1 hierarchy
+ */
+__bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id)
+{
+	return task_cgroup1_id_within_hierarchy(task, hierarchy_id);
+}
+
+/**
+ * bpf_task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated

kernel/bpf/helpers.c __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, · line 2240

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+ * ancestor cgroup ID of a task within a specific cgroup1 hierarchy.
+ * @task: The target task
+ * @hierarchy_id: The ID of a cgroup1 hierarchy
+ * @ancestor_level: The cgroup level of the ancestor in the cgroup1 hierarchy
+ */
+__bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task,
+							      int hierarchy_id, int ancestor_level)
+{
+	return task_ancestor_cgroup1_id_within_hierarchy(task, hierarchy_id, ancestor_level);
+}

The same here. Please make one helper that returns a kptr and then let the user call bpf_cgroup_ancestor() if desired.

Thanks.

Signature · 1 line
tejun
#19 lore raw

Re: [RFC PATCH bpf-next 3/8] bpf: Add kfuncs for cgroup1 hierarchy

Quoted 21 lines · Tejun Heo
Quoted 19 lines · Yafang Shao
kernel/bpf/helpers.c +19 -0
+
+/**
+ * bpf_task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID of a
+ * task within a specific cgroup1 hierarchy.
+ * @task: The target task
+ * @hierarchy_id: The ID of a cgroup1 hierarchy
+ */
+__bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id)
+{
+     return task_cgroup1_id_within_hierarchy(task, hierarchy_id);
+}
+
+/**
+ * bpf_task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated
+ * ancestor cgroup ID of a task within a specific cgroup1 hierarchy.
+ * @task: The target task
+ * @hierarchy_id: The ID of a cgroup1 hierarchy
+ * @ancestor_level: The cgroup level of the ancestor in the cgroup1 hierarchy
+ */
Quoted 8 lines

kernel/bpf/helpers.c __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, · line 2240

Quoted 5 lines
kernel/bpf/helpers.c +5 -0
+__bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task,
+                                                           int hierarchy_id, int ancestor_level)
+{
+     return task_ancestor_cgroup1_id_within_hierarchy(task, hierarchy_id, ancestor_level);
+}

The same here. Please make one helper that returns a kptr and then let the user call bpf_cgroup_ancestor() if desired.

Sure, will do it.

Signature · 2 lines
Regards
Yafang
#20 lore raw

Re: [RFC PATCH bpf-next 3/8] bpf: Add kfuncs for cgroup1 hierarchy

In reply to Yafang Shao
kernel test robot noticed the following build warnings:

Hi Yafang,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/cgroup-Don-t-have-to-hold-cgroup_mutex-in-task_cgroup_from_root/20231007-220510
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20231007140304.4390-4-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 3/8] bpf: Add kfuncs for cgroup1 hierarchy
config: x86_64-randconfig-004-20231008 (https://download.01.org/0day-ci/archive/20231008/[email protected]/config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231008/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

kernel/bpf/helpers.c:1892:19: warning: no previous declaration for 'bpf_obj_new_impl' [-Wmissing-declarations]

    __bpf_kfunc void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign)
                      ^~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:1906:19: warning: no previous declaration for 'bpf_percpu_obj_new_impl' [-Wmissing-declarations]
    __bpf_kfunc void *bpf_percpu_obj_new_impl(u64 local_type_id__k, void *meta__ign)
                      ^~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:1934:18: warning: no previous declaration for 'bpf_obj_drop_impl' [-Wmissing-declarations]
    __bpf_kfunc void bpf_obj_drop_impl(void *p__alloc, void *meta__ign)
                     ^~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:1942:18: warning: no previous declaration for 'bpf_percpu_obj_drop_impl' [-Wmissing-declarations]
    __bpf_kfunc void bpf_percpu_obj_drop_impl(void *p__alloc, void *meta__ign)
                     ^~~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:1948:19: warning: no previous declaration for 'bpf_refcount_acquire_impl' [-Wmissing-declarations]
    __bpf_kfunc void *bpf_refcount_acquire_impl(void *p__refcounted_kptr, void *meta__ign)
                      ^~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:1993:17: warning: no previous declaration for 'bpf_list_push_front_impl' [-Wmissing-declarations]
    __bpf_kfunc int bpf_list_push_front_impl(struct bpf_list_head *head,
                    ^~~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2003:17: warning: no previous declaration for 'bpf_list_push_back_impl' [-Wmissing-declarations]
    __bpf_kfunc int bpf_list_push_back_impl(struct bpf_list_head *head,
                    ^~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2036:35: warning: no previous declaration for 'bpf_list_pop_front' [-Wmissing-declarations]
    __bpf_kfunc struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head)
                                      ^~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2041:35: warning: no previous declaration for 'bpf_list_pop_back' [-Wmissing-declarations]
    __bpf_kfunc struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head)
                                      ^~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2046:33: warning: no previous declaration for 'bpf_rbtree_remove' [-Wmissing-declarations]
    __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root,
                                    ^~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2102:17: warning: no previous declaration for 'bpf_rbtree_add_impl' [-Wmissing-declarations]
    __bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node,
                    ^~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2112:33: warning: no previous declaration for 'bpf_rbtree_first' [-Wmissing-declarations]
    __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root)
                                    ^~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2125:33: warning: no previous declaration for 'bpf_task_acquire' [-Wmissing-declarations]
    __bpf_kfunc struct task_struct *bpf_task_acquire(struct task_struct *p)
                                    ^~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2136:18: warning: no previous declaration for 'bpf_task_release' [-Wmissing-declarations]
    __bpf_kfunc void bpf_task_release(struct task_struct *p)
                     ^~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2148:28: warning: no previous declaration for 'bpf_cgroup_acquire' [-Wmissing-declarations]
    __bpf_kfunc struct cgroup *bpf_cgroup_acquire(struct cgroup *cgrp)
                               ^~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2160:18: warning: no previous declaration for 'bpf_cgroup_release' [-Wmissing-declarations]
    __bpf_kfunc void bpf_cgroup_release(struct cgroup *cgrp)
                     ^~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2172:28: warning: no previous declaration for 'bpf_cgroup_ancestor' [-Wmissing-declarations]
    __bpf_kfunc struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level)
                               ^~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2192:28: warning: no previous declaration for 'bpf_cgroup_from_id' [-Wmissing-declarations]
    __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)
                               ^~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2212:18: warning: no previous declaration for 'bpf_task_under_cgroup' [-Wmissing-declarations]
    __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
                     ^~~~~~~~~~~~~~~~~~~~~
>> kernel/bpf/helpers.c:2224:17: warning: no previous declaration for 'bpf_task_cgroup1_id_within_hierarchy' [-Wmissing-declarations]
    __bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id)
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/bpf/helpers.c:2236:17: warning: no previous declaration for 'bpf_task_ancestor_cgroup1_id_within_hierarchy' [-Wmissing-declarations]
    __bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task,
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2249:33: warning: no previous declaration for 'bpf_task_from_pid' [-Wmissing-declarations]
    __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)
                                    ^~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2290:19: warning: no previous declaration for 'bpf_dynptr_slice' [-Wmissing-declarations]
    __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset,
                      ^~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2374:19: warning: no previous declaration for 'bpf_dynptr_slice_rdwr' [-Wmissing-declarations]
    __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,
                      ^~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2405:17: warning: no previous declaration for 'bpf_dynptr_adjust' [-Wmissing-declarations]
    __bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end)
                    ^~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2423:18: warning: no previous declaration for 'bpf_dynptr_is_null' [-Wmissing-declarations]
    __bpf_kfunc bool bpf_dynptr_is_null(struct bpf_dynptr_kern *ptr)
                     ^~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2428:18: warning: no previous declaration for 'bpf_dynptr_is_rdonly' [-Wmissing-declarations]
    __bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
                     ^~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2436:19: warning: no previous declaration for 'bpf_dynptr_size' [-Wmissing-declarations]
    __bpf_kfunc __u32 bpf_dynptr_size(const struct bpf_dynptr_kern *ptr)
                      ^~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2444:17: warning: no previous declaration for 'bpf_dynptr_clone' [-Wmissing-declarations]
    __bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
                    ^~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2457:19: warning: no previous declaration for 'bpf_cast_to_kern_ctx' [-Wmissing-declarations]
    __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
                      ^~~~~~~~~~~~~~~~~~~~
   kernel/bpf/helpers.c:2462:19: warning: no previous declaration for 'bpf_rdonly_cast' [-Wmissing-declarations]
    __bpf_kfunc void *bpf_rdonly_cast(void *obj__ign, u32 btf_id__k)
                      ^~~~~~~~~~~~~~~

kernel/bpf/helpers.c:2467:18: warning: no previous declaration for 'bpf_rcu_read_lock' [-Wmissing-declarations]
__bpf_kfunc void bpf_rcu_read_lock(void)
^~~~~~~~~~~~~~~~~
kernel/bpf/helpers.c:2472:18: warning: no previous declaration for 'bpf_rcu_read_unlock' [-Wmissing-declarations]
__bpf_kfunc void bpf_rcu_read_unlock(void)
^~~~~~~~~~~~~~~~~~~
kernel/bpf/helpers.c:2501:18: warning: no previous declaration for 'bpf_throw' [-Wmissing-declarations]
__bpf_kfunc void bpf_throw(u64 cookie)
^~~~~~~~~

vim +/bpf_task_cgroup1_id_within_hierarchy +2224 kernel/bpf/helpers.c

2217
2218 /**
2219 * bpf_task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID of a
2220 * task within a specific cgroup1 hierarchy.
2221 * @task: The target task
2222 * @hierarchy_id: The ID of a cgroup1 hierarchy
2223 */

> 2224	__bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id)
  2225	{
  2226		return task_cgroup1_id_within_hierarchy(task, hierarchy_id);
  2227	}
  2228	
  2229	/**
  2230	 * bpf_task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated
  2231	 * ancestor cgroup ID of a task within a specific cgroup1 hierarchy.
  2232	 * @task: The target task
  2233	 * @hierarchy_id: The ID of a cgroup1 hierarchy
  2234	 * @ancestor_level: The cgroup level of the ancestor in the cgroup1 hierarchy
  2235	 */
> 2236	__bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task,

2237 int hierarchy_id, int ancestor_level)
2238 {
2239 return task_ancestor_cgroup1_id_within_hierarchy(task, hierarchy_id, ancestor_level);
2240 }
2241 #endif /* CONFIG_CGROUPS */
2242

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#21 lore raw

RFC PATCH 4/8 selftests/bpf: Fix issues in setup_classid_environment()

In reply to Yafang Shao

If the net_cls subsystem is already mounted, attempting to mount it again in setup_classid_environment() will result in a failure with the error code EBUSY. Despite this, tmpfs will have been successfully mounted at /sys/fs/cgroup/net_cls. Consequently, the /sys/fs/cgroup/net_cls directory will be empty, causing subsequent setup operations to fail.

Here's an error log excerpt illustrating the issue when net_cls has already been mounted at /sys/fs/cgroup/net_cls prior to running setup_classid_environment():

- Before that change

  $ tools/testing/selftests/bpf/test_progs --name=cgroup_v1v2
  test_cgroup_v1v2:PASS:server_fd 0 nsec
  test_cgroup_v1v2:PASS:client_fd 0 nsec
  test_cgroup_v1v2:PASS:cgroup_fd 0 nsec
  test_cgroup_v1v2:PASS:server_fd 0 nsec
  run_test:PASS:skel_open 0 nsec
  run_test:PASS:prog_attach 0 nsec
  test_cgroup_v1v2:PASS:cgroup-v2-only 0 nsec
  (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup.procs
  (cgroup_helpers.c:540: errno: No such file or directory) Opening cgroup classid: /sys/fs/cgroup/net_cls/cgroup-test-work-dir/net_cls.classid
  run_test:PASS:skel_open 0 nsec
  run_test:PASS:prog_attach 0 nsec
  (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup-test-work-dir/cgroup.procs
  run_test:FAIL:join_classid unexpected error: 1 (errno 2)
  test_cgroup_v1v2:FAIL:cgroup-v1v2 unexpected error: -1 (errno 2)
  (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup.procs
  #44      cgroup_v1v2:FAIL
  Summary: 0/0 PASSED, 0 SKIPPED, 1 FAILED

- After that change

  $ tools/testing/selftests/bpf/test_progs --name=cgroup_v1v2
  #44      cgroup_v1v2:OK
  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
---
 tools/testing/selftests/bpf/cgroup_helpers.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/cgroup_helpers.c +14 -4
index 24ba56d42f2d..9c36d1db9f94 100644
@@ -518,10 +518,20 @@ int setup_classid_environment(void)
 		return 1;
 	}
 
-	if (mount("net_cls", NETCLS_MOUNT_PATH, "cgroup", 0, "net_cls") &&
-	    errno != EBUSY) {
-		log_err("mount cgroup net_cls");
-		return 1;
+	if (mount("net_cls", NETCLS_MOUNT_PATH, "cgroup", 0, "net_cls")) {
+		if (errno != EBUSY) {
+			log_err("mount cgroup net_cls");
+			return 1;
+		}
+
+		if (rmdir(NETCLS_MOUNT_PATH)) {
+			log_err("rmdir cgroup net_cls");
+			return 1;
+		}
+		if (umount(CGROUP_MOUNT_DFLT)) {
+			log_err("umount cgroup base");
+			return 1;
+		}
 	}
 
 	cleanup_classid_environment();
Signature · 1 line
2.30.1 (Apple Git-130)
#22 lore raw

RFC PATCH 5/8 selftests/bpf: Add parallel support for classid

In reply to Yafang Shao

Include the current pid in the classid cgroup path. This way, different testers relying on classid-based configurations will have distinct classid cgroup directories, enabling them to run concurrently. Additionally, we leverage the current pid as the classid, ensuring unique identification.

---
 tools/testing/selftests/bpf/cgroup_helpers.c  | 19 ++++++++++++-------
 tools/testing/selftests/bpf/cgroup_helpers.h  |  2 +-
 .../selftests/bpf/prog_tests/cgroup_v1v2.c    |  2 +-
 3 files changed, 14 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/cgroup_helpers.c +12 -7
index 9c36d1db9f94..e378fa057757 100644
@@ -45,9 +45,12 @@
 #define format_parent_cgroup_path(buf, path) \
 	format_cgroup_path_pid(buf, path, getppid())
 
-#define format_classid_path(buf)				\
-	snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH,	\
-		 CGROUP_WORK_DIR)
+#define format_classid_path_pid(buf, pid)				\
+	snprintf(buf, sizeof(buf), "%s%s%d", NETCLS_MOUNT_PATH,	\
+		 CGROUP_WORK_DIR, pid)
+
+#define format_classid_path(buf)	\
+	format_classid_path_pid(buf, getpid())
 
 static __thread bool cgroup_workdir_mounted;
 
@@ -546,15 +549,17 @@ int setup_classid_environment(void)
 
 /**
  * set_classid() - Set a cgroupv1 net_cls classid
- * @id: the numeric classid
  *
- * Writes the passed classid into the cgroup work dir's net_cls.classid
+ * Writes the classid into the cgroup work dir's net_cls.classid
  * file in order to later on trigger socket tagging.
  *
+ * To make sure different classid testers have different classids, we use
+ * the current pid as the classid by default.
+ *
  * On success, it returns 0, otherwise on failure it returns 1. If there
  * is a failure, it prints the error to stderr.
  */
-int set_classid(unsigned int id)
+int set_classid(void)
 {
 	char cgroup_workdir[PATH_MAX - 42];
 	char cgroup_classid_path[PATH_MAX + 1];
@@ -570,7 +575,7 @@ int set_classid(unsigned int id)
 		return 1;
 	}
 
-	if (dprintf(fd, "%u\n", id) < 0) {
+	if (dprintf(fd, "%u\n", getpid()) < 0) {
 		log_err("Setting cgroup classid");
 		rc = 1;
 	}
tools/testing/selftests/bpf/cgroup_helpers.h +1 -1
index 5c2cb9c8b546..92fc41daf4a4 100644
@@ -29,7 +29,7 @@ int setup_cgroup_environment(void);
 void cleanup_cgroup_environment(void);
 
 /* cgroupv1 related */
-int set_classid(unsigned int id);
+int set_classid(void);
 int join_classid(void);
 
 int setup_classid_environment(void);
tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +1 -1
index 9026b42914d3..addf720428f7 100644
@@ -71,7 +71,7 @@ void test_cgroup_v1v2(void)
 	}
 	ASSERT_OK(run_test(cgroup_fd, server_fd, false), "cgroup-v2-only");
 	setup_classid_environment();
-	set_classid(42);
+	set_classid();
 	ASSERT_OK(run_test(cgroup_fd, server_fd, true), "cgroup-v1v2");
 	cleanup_classid_environment();
 	close(server_fd);
Signature · 1 line
2.30.1 (Apple Git-130)
#23 lore raw

RFC PATCH 6/8 selftests/bpf: Add a new cgroup helper get_classid_cgroup_id()

In reply to Yafang Shao

Introduce a new helper function to retrieve the cgroup ID from a net_cls cgroup directory.

---
 tools/testing/selftests/bpf/cgroup_helpers.c | 28 +++++++++++++++-----
 tools/testing/selftests/bpf/cgroup_helpers.h |  1 +
 2 files changed, 23 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/cgroup_helpers.c +22 -6
index e378fa057757..7cb2c9597b8f 100644
@@ -417,26 +417,23 @@ int create_and_get_cgroup(const char *relative_path)
 }
 
 /**
- * get_cgroup_id() - Get cgroup id for a particular cgroup path
- * @relative_path: The cgroup path, relative to the workdir, to join
+ * get_cgroup_id_from_path - Get cgroup id for a particular cgroup path
+ * @cgroup_workdir: The absolute cgroup path
  *
  * On success, it returns the cgroup id. On failure it returns 0,
  * which is an invalid cgroup id.
  * If there is a failure, it prints the error to stderr.
  */
-unsigned long long get_cgroup_id(const char *relative_path)
+unsigned long long get_cgroup_id_from_path(const char *cgroup_workdir)
 {
 	int dirfd, err, flags, mount_id, fhsize;
 	union {
 		unsigned long long cgid;
 		unsigned char raw_bytes[8];
 	} id;
-	char cgroup_workdir[PATH_MAX + 1];
 	struct file_handle *fhp, *fhp2;
 	unsigned long long ret = 0;
 
-	format_cgroup_path(cgroup_workdir, relative_path);
-
 	dirfd = AT_FDCWD;
 	flags = 0;
 	fhsize = sizeof(*fhp);
@@ -472,6 +469,14 @@ unsigned long long get_cgroup_id(const char *relative_path)
 	return ret;
 }
 
+unsigned long long get_cgroup_id(const char *relative_path)
+{
+	char cgroup_workdir[PATH_MAX + 1];
+
+	format_cgroup_path(cgroup_workdir, relative_path);
+	return get_cgroup_id_from_path(cgroup_workdir);
+}
+
 int cgroup_setup_and_join(const char *path) {
 	int cg_fd;
 
@@ -617,3 +622,14 @@ void cleanup_classid_environment(void)
 	join_cgroup_from_top(NETCLS_MOUNT_PATH);
 	nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT);
 }
+
+/**
+ * get_classid_cgroup_id - Get the cgroup id of a net_cls cgroup
+ */
+unsigned long long get_classid_cgroup_id(void)
+{
+	char cgroup_workdir[PATH_MAX + 1];
+
+	format_classid_path(cgroup_workdir);
+	return get_cgroup_id_from_path(cgroup_workdir);
+}
tools/testing/selftests/bpf/cgroup_helpers.h +1 -0
index 92fc41daf4a4..e71da4ef031b 100644
@@ -31,6 +31,7 @@ void cleanup_cgroup_environment(void);
 /* cgroupv1 related */
 int set_classid(void);
 int join_classid(void);
+unsigned long long get_classid_cgroup_id(void);
 
 int setup_classid_environment(void);
 void cleanup_classid_environment(void);
Signature · 1 line
2.30.1 (Apple Git-130)
#24 lore raw

RFC PATCH 7/8 selftests/bpf: Add a new cgroup helper get_cgroup_hierarchy_id()

In reply to Yafang Shao

A new cgroup helper function, get_cgroup1_hierarchy_id(), has been introduced to obtain the ID of a cgroup1 hierarchy based on the provided cgroup name. This cgroup name can be obtained from the /proc/self/cgroup file.

---
 tools/testing/selftests/bpf/cgroup_helpers.c | 49 ++++++++++++++++++++
 tools/testing/selftests/bpf/cgroup_helpers.h |  1 +
 2 files changed, 50 insertions(+)
tools/testing/selftests/bpf/cgroup_helpers.c +49 -0
index 7cb2c9597b8f..5cb66fb3d4fe 100644
@@ -633,3 +633,52 @@ unsigned long long get_classid_cgroup_id(void)
 	format_classid_path(cgroup_workdir);
 	return get_cgroup_id_from_path(cgroup_workdir);
 }
+
+/**
+ * get_cgroup1_hierarchy_id - Retrieves the ID of a cgroup1 hierarchy from the cgroup1 name
+ * @cgrp_name: The cgroup1 name, which can be retrieved from /proc/self/cgroup.
+ */
+int get_cgroup1_hierarchy_id(const char *cgrp_name)
+{
+	char *c, *c2, *c3, *c4;
+	bool found = false;
+	char line[1024];
+	FILE *file;
+	int i, id;
+
+	if (!cgrp_name)
+		return -1;
+
+	file = fopen("/proc/self/cgroup", "r");
+	if (!file) {
+		log_err("fopen /proc/self/cgroup");
+		return -1;
+	}
+
+	while (fgets(line, 1024, file)) {
+		i = 0;
+		for (c = strtok_r(line, ":", &c2); c && i < 2; c = strtok_r(NULL, ":", &c2)) {
+			if (i == 0) {
+				id = strtol(c, NULL, 10);
+			} else if (i == 1) {
+				if (!strcmp(c, cgrp_name)) {
+					found = true;
+					break;
+				}
+
+				/* Multiple subsystems may share one single mount point */
+				for (c3 = strtok_r(c, ",", &c4); c3;
+				     c3 = strtok_r(NULL, ",", &c4)) {
+					if (!strcmp(c, cgrp_name)) {
+						found = true;
+						break;
+					}
+				}
+			}
+			i++;
+		}
+		if (found)
+			break;
+	}
+	return found ? id : -1;
+}
tools/testing/selftests/bpf/cgroup_helpers.h +1 -0
index e71da4ef031b..a80c41734a26 100644
@@ -20,6 +20,7 @@ int get_root_cgroup(void);
 int create_and_get_cgroup(const char *relative_path);
 void remove_cgroup(const char *relative_path);
 unsigned long long get_cgroup_id(const char *relative_path);
+int get_cgroup1_hierarchy_id(const char *cgrp_name);
 
 int join_cgroup(const char *relative_path);
 int join_root_cgroup(void);
Signature · 1 line
2.30.1 (Apple Git-130)
#25 lore raw

RFC PATCH 8/8 selftests/bpf: Add selftests for cgroup1 hierarchy

In reply to Yafang Shao

Add selftests for cgroup1 hierarchy.
The result as follows,

  $ tools/testing/selftests/bpf/test_progs --name=cgroup1_hierarchy
  #36/1    cgroup1_hierarchy/test_cgroup1_hierarchy:OK
  #36/2    cgroup1_hierarchy/test_root_cgid:OK
  #36/3    cgroup1_hierarchy/test_invalid_level:OK
  #36/4    cgroup1_hierarchy/test_invalid_cgid:OK
  #36/5    cgroup1_hierarchy/test_invalid_hid:OK
  #36/6    cgroup1_hierarchy/test_invalid_cgrp_name:OK
  #36/7    cgroup1_hierarchy/test_invalid_cgrp_name2:OK
  #36/8    cgroup1_hierarchy/test_sleepable_prog:OK
  #36      cgroup1_hierarchy:OK
  Summary: 1/8 PASSED, 0 SKIPPED, 0 FAILED
---
 .../bpf/prog_tests/cgroup1_hierarchy.c        | 159 ++++++++++++++++++
 .../bpf/progs/test_cgroup1_hierarchy.c        |  62 +++++++
 2 files changed, 221 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c
tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c new file +159 -0
new file mode 100644
index 000000000000..4aafbc921254
@@ -0,0 +1,159 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2023 Yafang Shao <[email protected]> */
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include "cgroup_helpers.h"
+#include "test_cgroup1_hierarchy.skel.h"
+
+static void bpf_cgroup1(struct test_cgroup1_hierarchy *skel)
+{
+	int err;
+
+	/* Attach LSM prog first */
+	skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run);
+	if (!ASSERT_OK_PTR(skel->links.lsm_run, "lsm_attach"))
+		return;
+
+	/* LSM prog will be triggered when attaching fentry */
+	skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run);
+	ASSERT_NULL(skel->links.fentry_run, "fentry_attach_fail");
+
+	err = bpf_link__destroy(skel->links.lsm_run);
+	ASSERT_OK(err, "destroy_lsm");
+	skel->links.lsm_run = NULL;
+}
+
+static void bpf_cgroup1_sleepable(struct test_cgroup1_hierarchy *skel)
+{
+	int err;
+
+	/* Attach LSM prog first */
+	skel->links.lsm_s_run = bpf_program__attach_lsm(skel->progs.lsm_s_run);
+	if (!ASSERT_OK_PTR(skel->links.lsm_s_run, "lsm_attach"))
+		return;
+
+	/* LSM prog will be triggered when attaching fentry */
+	skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run);
+	ASSERT_NULL(skel->links.fentry_run, "fentry_attach_fail");
+
+	err = bpf_link__destroy(skel->links.lsm_s_run);
+	ASSERT_OK(err, "destroy_lsm");
+	skel->links.lsm_s_run = NULL;
+}
+
+static void bpf_cgroup1_invalid_id(struct test_cgroup1_hierarchy *skel)
+{
+	int err;
+
+	/* Attach LSM prog first */
+	skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run);
+	if (!ASSERT_OK_PTR(skel->links.lsm_run, "lsm_attach"))
+		return;
+
+	/* LSM prog will be triggered when attaching fentry */
+	skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run);
+	if (!ASSERT_OK_PTR(skel->links.fentry_run, "fentry_attach_success"))
+		goto cleanup;
+
+	err = bpf_link__destroy(skel->links.lsm_run);
+	ASSERT_OK(err, "destroy_lsm");
+	skel->links.lsm_run = NULL;
+
+cleanup:
+	err = bpf_link__destroy(skel->links.fentry_run);
+	ASSERT_OK(err, "destroy_fentry");
+	skel->links.fentry_run = NULL;
+}
+
+void test_cgroup1_hierarchy(void)
+{
+	struct test_cgroup1_hierarchy *skel;
+	__u64 current_cgid;
+	int hid, err;
+
+	skel = test_cgroup1_hierarchy__open();
+	if (!ASSERT_OK_PTR(skel, "open"))
+		return;
+
+	skel->bss->target_pid = getpid();
+
+	err = bpf_program__set_attach_target(skel->progs.fentry_run, 0, "bpf_fentry_test1");
+	if (!ASSERT_OK(err, "fentry_set_target"))
+		goto destroy;
+
+	err = test_cgroup1_hierarchy__load(skel);
+	if (!ASSERT_OK(err, "load"))
+		goto destroy;
+
+	/* Setup cgroup1 hierarchy */
+	err = setup_classid_environment();
+	if (!ASSERT_OK(err, "setup_classid_environment"))
+		goto destroy;
+
+	err = join_classid();
+	if (!ASSERT_OK(err, "join_cgroup1"))
+		goto cleanup;
+
+	current_cgid = get_classid_cgroup_id();
+	if (!ASSERT_GE(current_cgid, 0, "cgroup1 id"))
+		goto cleanup;
+
+	hid = get_cgroup1_hierarchy_id("net_cls");
+	if (!ASSERT_GE(hid, 0, "cgroup1 id"))
+		goto cleanup;
+	skel->bss->target_hid = hid;
+
+	if (test__start_subtest("test_cgroup1_hierarchy")) {
+		skel->bss->target_ancestor_cgid = current_cgid;
+		bpf_cgroup1(skel);
+	}
+
+	if (test__start_subtest("test_root_cgid")) {
+		skel->bss->target_ancestor_cgid = 1;
+		skel->bss->target_ancestor_level = 0;
+		bpf_cgroup1(skel);
+	}
+
+	if (test__start_subtest("test_invalid_level")) {
+		skel->bss->target_ancestor_cgid = 1;
+		skel->bss->target_ancestor_level = 1;
+		bpf_cgroup1_invalid_id(skel);
+	}
+
+	if (test__start_subtest("test_invalid_cgid")) {
+		skel->bss->target_ancestor_cgid = 0;
+		bpf_cgroup1_invalid_id(skel);
+	}
+
+	if (test__start_subtest("test_invalid_hid")) {
+		skel->bss->target_ancestor_cgid = 1;
+		skel->bss->target_ancestor_level = 0;
+		skel->bss->target_hid = -1;
+		bpf_cgroup1_invalid_id(skel);
+	}
+
+	if (test__start_subtest("test_invalid_cgrp_name")) {
+		skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cl");
+		skel->bss->target_ancestor_cgid = current_cgid;
+		bpf_cgroup1_invalid_id(skel);
+	}
+
+	if (test__start_subtest("test_invalid_cgrp_name2")) {
+		skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cls,");
+		skel->bss->target_ancestor_cgid = current_cgid;
+		bpf_cgroup1_invalid_id(skel);
+	}
+
+	if (test__start_subtest("test_sleepable_prog")) {
+		skel->bss->target_hid = hid;
+		skel->bss->target_ancestor_cgid = current_cgid;
+		bpf_cgroup1_sleepable(skel);
+	}
+
+cleanup:
+	cleanup_classid_environment();
+destroy:
+	test_cgroup1_hierarchy__destroy(skel);
+}
tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.30.1 (Apple Git-130)
maintainer
#26 lore raw
In reply to Yafang Shao

Hi.

Quoted 2 lines · Yafang Shao

Given the widespread use of cgroup1 in container environments, this change would be beneficial to many users.

This is an unverifiable claim (and benefit applies only to subset of those users who would use cgroup1 and BPF). So please don't use it in this form.

Thanks,
Michal

#27 lore raw
Quoted 10 lines · Michal Koutný

Hi.

Quoted 2 lines · Yafang Shao

Given the widespread use of cgroup1 in container environments, this change would be beneficial to many users.

This is an unverifiable claim (and benefit applies only to subset of those users who would use cgroup1 and BPF). So please don't use it in this form.

Sure. will remove it.

Signature · 2 lines
Regards
Yafang