Skip to content

Commit

Permalink
cgroup: add cgroup_get_from_file() helper
Browse files Browse the repository at this point in the history
Add a helper cgroup_get_from_file(). The helper will be used in
subsequent patches to retrieve a cgroup while holding a reference to the
struct file it was taken from.

Cc: Tejun Heo <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Li Zefan <[email protected]>
Cc: [email protected]
Acked-by: Michal Koutný <[email protected]>
Signed-off-by: Christian Brauner <[email protected]>
Signed-off-by: Tejun Heo <[email protected]>
  • Loading branch information
Christian Brauner authored and htejun committed Feb 12, 2020
1 parent 6df970e commit 1770309
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions kernel/cgroup/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -5880,6 +5880,24 @@ void cgroup_fork(struct task_struct *child)
INIT_LIST_HEAD(&child->cg_list);
}

static struct cgroup *cgroup_get_from_file(struct file *f)
{
struct cgroup_subsys_state *css;
struct cgroup *cgrp;

css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
if (IS_ERR(css))
return ERR_CAST(css);

cgrp = css->cgroup;
if (!cgroup_on_dfl(cgrp)) {
cgroup_put(cgrp);
return ERR_PTR(-EBADF);
}

return cgrp;
}

/**
* cgroup_can_fork - called on a new task before the process is exposed
* @child: the task in question.
Expand Down Expand Up @@ -6171,25 +6189,15 @@ EXPORT_SYMBOL_GPL(cgroup_get_from_path);
*/
struct cgroup *cgroup_get_from_fd(int fd)
{
struct cgroup_subsys_state *css;
struct cgroup *cgrp;
struct file *f;

f = fget_raw(fd);
if (!f)
return ERR_PTR(-EBADF);

css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
cgrp = cgroup_get_from_file(f);
fput(f);
if (IS_ERR(css))
return ERR_CAST(css);

cgrp = css->cgroup;
if (!cgroup_on_dfl(cgrp)) {
cgroup_put(cgrp);
return ERR_PTR(-EBADF);
}

return cgrp;
}
EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
Expand Down

0 comments on commit 1770309

Please sign in to comment.