Commit 56fee144 authored by Liu Jian's avatar Liu Jian
Browse files

cgroup: make cgroup_bpf_prog_attach work when cgroup2 is not mounted

hulk inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I7DNAP


CVE: N/A

----------------------------------------------------

BPF_PROG_TYPE_CGROUP* bpf programs is associated with cgroup2. If cgroup2
is not mounted, the bpf program is associated with cgrp_dfl_root.cgrp
by default.

Then we can use it like below:
bpftool cgroup attach /sys/fs/cgroup/cpu sock_ops pinned /sys/fs/bpf/xxx

Signed-off-by: default avatarLiu Jian <liujian56@huawei.com>
parent 590ef703
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,

struct cgroup *cgroup_get_from_path(const char *path);
struct cgroup *cgroup_get_from_fd(int fd);
struct cgroup *cgroup_get_from_fd_v2(int fd);
struct cgroup *cgroup_v1v2_get_from_fd(int fd);

int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
+4 −4
Original line number Diff line number Diff line
@@ -848,7 +848,7 @@ int cgroup_bpf_prog_attach(const union bpf_attr *attr,
	struct cgroup *cgrp;
	int ret;

	cgrp = cgroup_get_from_fd(attr->target_fd);
	cgrp = cgroup_get_from_fd_v2(attr->target_fd);
	if (IS_ERR(cgrp))
		return PTR_ERR(cgrp);

@@ -876,7 +876,7 @@ int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
	struct cgroup *cgrp;
	int ret;

	cgrp = cgroup_get_from_fd(attr->target_fd);
	cgrp = cgroup_get_from_fd_v2(attr->target_fd);
	if (IS_ERR(cgrp))
		return PTR_ERR(cgrp);

@@ -993,7 +993,7 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
	if (attr->link_create.flags)
		return -EINVAL;

	cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
	cgrp = cgroup_get_from_fd_v2(attr->link_create.target_fd);
	if (IS_ERR(cgrp))
		return PTR_ERR(cgrp);

@@ -1033,7 +1033,7 @@ int cgroup_bpf_prog_query(const union bpf_attr *attr,
	struct cgroup *cgrp;
	int ret;

	cgrp = cgroup_get_from_fd(attr->query.target_fd);
	cgrp = cgroup_get_from_fd_v2(attr->query.target_fd);
	if (IS_ERR(cgrp))
		return PTR_ERR(cgrp);

+22 −0
Original line number Diff line number Diff line
@@ -6727,6 +6727,28 @@ struct cgroup *cgroup_get_from_fd(int fd)
}
EXPORT_SYMBOL_GPL(cgroup_get_from_fd);

/**
 * same with cgroup_get_from_fd, only add cgrp_dfl_visible check
 */
struct cgroup *cgroup_get_from_fd_v2(int fd)
{
	struct cgroup *cgrp = cgroup_v1v2_get_from_fd(fd);

	if (IS_ERR(cgrp))
		return ERR_CAST(cgrp);

	if (!cgroup_on_dfl(cgrp)) {
		cgroup_put(cgrp);
		if (cgrp_dfl_visible)
			return ERR_PTR(-EBADF);

		cgrp = &cgrp_dfl_root.cgrp;
		cgroup_get(cgrp);
	}
	return cgrp;
}
EXPORT_SYMBOL_GPL(cgroup_get_from_fd_v2);

static u64 power_of_ten(int power)
{
	u64 v = 1;