提交 0bfb4aa6 编写于 作者: T Tejun Heo

cgroup: fix subsystem file accesses on the root cgroup

105347ba ("cgroup: make cgroup_file_open() rcu_read_lock() around
cgroup_css() and add cfent->css") added cfent->css to cache the
associted cgroup_subsys_state across file operations.

A cfent is associated with single css throughout its lifetime and the
origimal commit initialized the cache pointer during cgroup_add_file()
and verified that it matches the actual one in cgroup_file_open().
While this works fine for !root cgroups, it's broken for root cgroups
as files in a root cgroup are created before the css's are associated
with the cgroup and thus cgroup_css() call in cgroup_add_file()
returns NULL associating all cfents in the root cgroup with NULL css.
This makes cgroup_file_open() trigger WARN and fail with -ENODEV for
all !core subsystem files in the root cgroups.

There's no reason to initialize cfent->css separately from
cgroup_add_file().  As the association never changes,
cgroup_file_open() can set it unconditionally every time and
containing the logic in cgroup_file_open() makes more sense anyway as
the only reason it's necessary is file->private_data being already
occupied.

Fix it by setting cfent->css unconditionally from cgroup_file_open().
Signed-off-by: NTejun Heo <tj@kernel.org>
Acked-by: NLi Zefan <lizefan@huawei.com>
上级 1cb650b9
...@@ -2490,10 +2490,18 @@ static int cgroup_file_open(struct inode *inode, struct file *file) ...@@ -2490,10 +2490,18 @@ static int cgroup_file_open(struct inode *inode, struct file *file)
} }
rcu_read_unlock(); rcu_read_unlock();
/* css should match @cfe->css, see cgroup_add_file() for details */ if (!css)
if (!css || WARN_ON_ONCE(css != cfe->css))
return -ENODEV; return -ENODEV;
/*
* @cfe->css is used by read/write/close to determine the
* associated css. @file->private_data would be a better place but
* that's already used by seqfile. Multiple accessors may use it
* simultaneously which is okay as the association never changes.
*/
WARN_ON_ONCE(cfe->css && cfe->css != css);
cfe->css = css;
if (cft->read_map || cft->read_seq_string) { if (cft->read_map || cft->read_seq_string) {
file->f_op = &cgroup_seqfile_operations; file->f_op = &cgroup_seqfile_operations;
err = single_open(file, cgroup_seqfile_show, cfe); err = single_open(file, cgroup_seqfile_show, cfe);
...@@ -2772,18 +2780,6 @@ static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) ...@@ -2772,18 +2780,6 @@ static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
dentry->d_fsdata = cfe; dentry->d_fsdata = cfe;
simple_xattrs_init(&cfe->xattrs); simple_xattrs_init(&cfe->xattrs);
/*
* cfe->css is used by read/write/close to determine the associated
* css. file->private_data would be a better place but that's
* already used by seqfile. Note that open will use the usual
* cgroup_css() and css_tryget() to acquire the css and this
* caching doesn't affect css lifetime management.
*/
if (cft->ss)
cfe->css = cgroup_css(cgrp, cft->ss->subsys_id);
else
cfe->css = &cgrp->dummy_css;
mode = cgroup_file_mode(cft); mode = cgroup_file_mode(cft);
error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
if (!error) { if (!error) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册