Skip to content

Commit 5024ae2

Browse files
committed
cgroup: introduce task_css_is_root()
Determining the css of a task usually requires RCU read lock as that's the only thing which keeps the returned css accessible till its reference is acquired; however, testing whether a task belongs to the root can be performed without dereferencing the returned css by comparing the returned pointer against the root one in init_css_set[] which never changes. Implement task_css_is_root() which can be invoked in any context. This will be used by the scheduled cgroup_freezer change. v2: cgroup no longer supports modular controllers. No need to export init_css_set. Pointed out by Li. Signed-off-by: Tejun Heo <[email protected]> Acked-by: Li Zefan <[email protected]>
1 parent 36c38fb commit 5024ae2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

include/linux/cgroup.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ struct cftype {
473473
};
474474

475475
extern struct cgroup_root cgrp_dfl_root;
476+
extern struct css_set init_css_set;
476477

477478
static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
478479
{
@@ -700,6 +701,20 @@ static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
700701
return task_css_check(task, subsys_id, false);
701702
}
702703

704+
/**
705+
* task_css_is_root - test whether a task belongs to the root css
706+
* @task: the target task
707+
* @subsys_id: the target subsystem ID
708+
*
709+
* Test whether @task belongs to the root css on the specified subsystem.
710+
* May be invoked in any context.
711+
*/
712+
static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
713+
{
714+
return task_css_check(task, subsys_id, true) ==
715+
init_css_set.subsys[subsys_id];
716+
}
717+
703718
static inline struct cgroup *task_cgroup(struct task_struct *task,
704719
int subsys_id)
705720
{

kernel/cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ struct cgrp_cset_link {
348348
* reference-counted, to improve performance when child cgroups
349349
* haven't been created.
350350
*/
351-
static struct css_set init_css_set = {
351+
struct css_set init_css_set = {
352352
.refcount = ATOMIC_INIT(1),
353353
.cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
354354
.tasks = LIST_HEAD_INIT(init_css_set.tasks),

0 commit comments

Comments
 (0)