Skip to content

Commit a6d1ce5

Browse files
yosrym93htejun
authored andcommitted
cgroup: add cgroup_v1v2_get_from_[fd/file]()
Add cgroup_v1v2_get_from_fd() and cgroup_v1v2_get_from_file() that support both cgroup1 and cgroup2. Signed-off-by: Yosry Ahmed <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 03db771 commit a6d1ce5

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

include/linux/cgroup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
106106

107107
struct cgroup *cgroup_get_from_path(const char *path);
108108
struct cgroup *cgroup_get_from_fd(int fd);
109+
struct cgroup *cgroup_v1v2_get_from_fd(int fd);
109110

110111
int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
111112
int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);

kernel/cgroup/cgroup.c

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6224,16 +6224,36 @@ void cgroup_fork(struct task_struct *child)
62246224
INIT_LIST_HEAD(&child->cg_list);
62256225
}
62266226

6227-
static struct cgroup *cgroup_get_from_file(struct file *f)
6227+
/**
6228+
* cgroup_v1v2_get_from_file - get a cgroup pointer from a file pointer
6229+
* @f: file corresponding to cgroup_dir
6230+
*
6231+
* Find the cgroup from a file pointer associated with a cgroup directory.
6232+
* Returns a pointer to the cgroup on success. ERR_PTR is returned if the
6233+
* cgroup cannot be found.
6234+
*/
6235+
static struct cgroup *cgroup_v1v2_get_from_file(struct file *f)
62286236
{
62296237
struct cgroup_subsys_state *css;
6230-
struct cgroup *cgrp;
62316238

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

6236-
cgrp = css->cgroup;
6243+
return css->cgroup;
6244+
}
6245+
6246+
/**
6247+
* cgroup_get_from_file - same as cgroup_v1v2_get_from_file, but only supports
6248+
* cgroup2.
6249+
*/
6250+
static struct cgroup *cgroup_get_from_file(struct file *f)
6251+
{
6252+
struct cgroup *cgrp = cgroup_v1v2_get_from_file(f);
6253+
6254+
if (IS_ERR(cgrp))
6255+
return ERR_CAST(cgrp);
6256+
62376257
if (!cgroup_on_dfl(cgrp)) {
62386258
cgroup_put(cgrp);
62396259
return ERR_PTR(-EBADF);
@@ -6734,14 +6754,14 @@ EXPORT_SYMBOL_GPL(cgroup_get_from_path);
67346754

67356755
/**
67366756
* cgroup_get_from_fd - get a cgroup pointer from a fd
6737-
* @fd: fd obtained by open(cgroup2_dir)
6757+
* @fd: fd obtained by open(cgroup_dir)
67386758
*
67396759
* Find the cgroup from a fd which should be obtained
67406760
* by opening a cgroup directory. Returns a pointer to the
67416761
* cgroup on success. ERR_PTR is returned if the cgroup
67426762
* cannot be found.
67436763
*/
6744-
struct cgroup *cgroup_get_from_fd(int fd)
6764+
struct cgroup *cgroup_v1v2_get_from_fd(int fd)
67456765
{
67466766
struct cgroup *cgrp;
67476767
struct file *f;
@@ -6750,10 +6770,28 @@ struct cgroup *cgroup_get_from_fd(int fd)
67506770
if (!f)
67516771
return ERR_PTR(-EBADF);
67526772

6753-
cgrp = cgroup_get_from_file(f);
6773+
cgrp = cgroup_v1v2_get_from_file(f);
67546774
fput(f);
67556775
return cgrp;
67566776
}
6777+
6778+
/**
6779+
* cgroup_get_from_fd - same as cgroup_v1v2_get_from_fd, but only supports
6780+
* cgroup2.
6781+
*/
6782+
struct cgroup *cgroup_get_from_fd(int fd)
6783+
{
6784+
struct cgroup *cgrp = cgroup_v1v2_get_from_fd(fd);
6785+
6786+
if (IS_ERR(cgrp))
6787+
return ERR_CAST(cgrp);
6788+
6789+
if (!cgroup_on_dfl(cgrp)) {
6790+
cgroup_put(cgrp);
6791+
return ERR_PTR(-EBADF);
6792+
}
6793+
return cgrp;
6794+
}
67576795
EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
67586796

67596797
static u64 power_of_ten(int power)

0 commit comments

Comments
 (0)