Skip to content

Commit 1f3fe7e

Browse files
iamkafaidavem330
authored andcommitted
cgroup: Add cgroup_get_from_fd
Add a helper function to get a cgroup2 from a fd. It will be stored in a bpf array (BPF_MAP_TYPE_CGROUP_ARRAY) which will be introduced in the later patch. Signed-off-by: Martin KaFai Lau <[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 6bd3847 commit 1f3fe7e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

include/linux/cgroup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
8787
struct cgroup_subsys *ss);
8888

8989
struct cgroup *cgroup_get_from_path(const char *path);
90+
struct cgroup *cgroup_get_from_fd(int fd);
9091

9192
int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
9293
int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);

kernel/cgroup.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include <linux/proc_ns.h>
6363
#include <linux/nsproxy.h>
6464
#include <linux/proc_ns.h>
65+
#include <linux/file.h>
6566
#include <net/sock.h>
6667

6768
/*
@@ -6209,6 +6210,40 @@ struct cgroup *cgroup_get_from_path(const char *path)
62096210
}
62106211
EXPORT_SYMBOL_GPL(cgroup_get_from_path);
62116212

6213+
/**
6214+
* cgroup_get_from_fd - get a cgroup pointer from a fd
6215+
* @fd: fd obtained by open(cgroup2_dir)
6216+
*
6217+
* Find the cgroup from a fd which should be obtained
6218+
* by opening a cgroup directory. Returns a pointer to the
6219+
* cgroup on success. ERR_PTR is returned if the cgroup
6220+
* cannot be found.
6221+
*/
6222+
struct cgroup *cgroup_get_from_fd(int fd)
6223+
{
6224+
struct cgroup_subsys_state *css;
6225+
struct cgroup *cgrp;
6226+
struct file *f;
6227+
6228+
f = fget_raw(fd);
6229+
if (!f)
6230+
return ERR_PTR(-EBADF);
6231+
6232+
css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
6233+
fput(f);
6234+
if (IS_ERR(css))
6235+
return ERR_CAST(css);
6236+
6237+
cgrp = css->cgroup;
6238+
if (!cgroup_on_dfl(cgrp)) {
6239+
cgroup_put(cgrp);
6240+
return ERR_PTR(-EBADF);
6241+
}
6242+
6243+
return cgrp;
6244+
}
6245+
EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
6246+
62126247
/*
62136248
* sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
62146249
* definition in cgroup-defs.h.

0 commit comments

Comments
 (0)