Skip to content

Commit aed704b

Browse files
sargundavem330
authored andcommitted
cgroup: Add task_under_cgroup_hierarchy cgroup inline function to headers
This commit adds an inline function to cgroup.h to check whether a given task is under a given cgroup hierarchy. This is to avoid having to put ifdefs in .c files to gate access to cgroups. When cgroups are disabled this always returns true. Signed-off-by: Sargun Dhillon <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Tejun Heo <[email protected]> Acked-by: Tejun Heo <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f9f9ab1 commit aed704b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

include/linux/cgroup.h

+23
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,23 @@ static inline bool cgroup_is_descendant(struct cgroup *cgrp,
497497
return cgrp->ancestor_ids[ancestor->level] == ancestor->id;
498498
}
499499

500+
/**
501+
* task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
502+
* @task: the task to be tested
503+
* @ancestor: possible ancestor of @task's cgroup
504+
*
505+
* Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
506+
* It follows all the same rules as cgroup_is_descendant, and only applies
507+
* to the default hierarchy.
508+
*/
509+
static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
510+
struct cgroup *ancestor)
511+
{
512+
struct css_set *cset = task_css_set(task);
513+
514+
return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
515+
}
516+
500517
/* no synchronization, the result can only be used as a hint */
501518
static inline bool cgroup_is_populated(struct cgroup *cgrp)
502519
{
@@ -557,6 +574,7 @@ static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
557574
#else /* !CONFIG_CGROUPS */
558575

559576
struct cgroup_subsys_state;
577+
struct cgroup;
560578

561579
static inline void css_put(struct cgroup_subsys_state *css) {}
562580
static inline int cgroup_attach_task_all(struct task_struct *from,
@@ -574,6 +592,11 @@ static inline void cgroup_free(struct task_struct *p) {}
574592
static inline int cgroup_init_early(void) { return 0; }
575593
static inline int cgroup_init(void) { return 0; }
576594

595+
static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
596+
struct cgroup *ancestor)
597+
{
598+
return true;
599+
}
577600
#endif /* !CONFIG_CGROUPS */
578601

579602
/*

0 commit comments

Comments
 (0)