Skip to content

Commit aecd408

Browse files
laoarhtejun
authored andcommitted
cgroup: Add a new helper for cgroup1 hierarchy
A new helper is added for cgroup1 hierarchy: - task_get_cgroup1 Acquires the associated cgroup of a task within a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID. This helper function is added to facilitate the tracing of tasks within a particular container or cgroup dir in BPF programs. It's important to note that this helper is designed specifically for cgroup1 only. tj: Use irsqsave/restore as suggested by Hou Tao <[email protected]>. Suggested-by: Tejun Heo <[email protected]> Signed-off-by: Yafang Shao <[email protected]> Cc: Hou Tao <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 0008454 commit aecd408

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

include/linux/cgroup.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct css_task_iter {
6969
extern struct file_system_type cgroup_fs_type;
7070
extern struct cgroup_root cgrp_dfl_root;
7171
extern struct css_set init_css_set;
72+
extern spinlock_t css_set_lock;
7273

7374
#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
7475
#include <linux/cgroup_subsys.h>
@@ -386,7 +387,6 @@ static inline void cgroup_unlock(void)
386387
* as locks used during the cgroup_subsys::attach() methods.
387388
*/
388389
#ifdef CONFIG_PROVE_RCU
389-
extern spinlock_t css_set_lock;
390390
#define task_css_set_check(task, __c) \
391391
rcu_dereference_check((task)->cgroups, \
392392
rcu_read_lock_sched_held() || \
@@ -853,4 +853,6 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
853853

854854
#endif /* CONFIG_CGROUP_BPF */
855855

856+
struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id);
857+
856858
#endif /* _LINUX_CGROUP_H */

kernel/cgroup/cgroup-internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ struct cgroup_mgctx {
164164
#define DEFINE_CGROUP_MGCTX(name) \
165165
struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name)
166166

167-
extern spinlock_t css_set_lock;
168167
extern struct cgroup_subsys *cgroup_subsys[];
169168
extern struct list_head cgroup_roots;
170169

kernel/cgroup/cgroup-v1.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,40 @@ int cgroup1_get_tree(struct fs_context *fc)
12621262
return ret;
12631263
}
12641264

1265+
/**
1266+
* task_get_cgroup1 - Acquires the associated cgroup of a task within a
1267+
* specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its
1268+
* hierarchy ID.
1269+
* @tsk: The target task
1270+
* @hierarchy_id: The ID of a cgroup1 hierarchy
1271+
*
1272+
* On success, the cgroup is returned. On failure, ERR_PTR is returned.
1273+
* We limit it to cgroup1 only.
1274+
*/
1275+
struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id)
1276+
{
1277+
struct cgroup *cgrp = ERR_PTR(-ENOENT);
1278+
struct cgroup_root *root;
1279+
unsigned long flags;
1280+
1281+
rcu_read_lock();
1282+
for_each_root(root) {
1283+
/* cgroup1 only*/
1284+
if (root == &cgrp_dfl_root)
1285+
continue;
1286+
if (root->hierarchy_id != hierarchy_id)
1287+
continue;
1288+
spin_lock_irqsave(&css_set_lock, flags);
1289+
cgrp = task_cgroup_from_root(tsk, root);
1290+
if (!cgrp || !cgroup_tryget(cgrp))
1291+
cgrp = ERR_PTR(-ENOENT);
1292+
spin_unlock_irqrestore(&css_set_lock, flags);
1293+
break;
1294+
}
1295+
rcu_read_unlock();
1296+
return cgrp;
1297+
}
1298+
12651299
static int __init cgroup1_wq_init(void)
12661300
{
12671301
/*

0 commit comments

Comments
 (0)