]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
bpf: make bpf_program_cgroup_attach() static
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 18 Feb 2021 15:21:04 +0000 (16:21 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 18 Feb 2021 15:21:04 +0000 (16:21 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/cgroups/cgroup2_devices.c
src/lxc/cgroups/cgroup2_devices.h

index 37fd4c688ee828507f6971325b43a02274df7b66..99d67f692847df580f2be7fbb8ab6b9c374b4c15 100644 (file)
@@ -355,23 +355,22 @@ static int bpf_program_load_kernel(struct bpf_program *prog)
        return 0;
 }
 
-int bpf_program_cgroup_attach(struct bpf_program *prog, int type, int fd_cgroup,
-                             int replace_bpf_fd, __u32 flags)
+static int bpf_program_cgroup_attach(struct bpf_program *prog, int type,
+                                    int fd_cgroup, __u32 flags)
 {
        __do_close int fd_attach = -EBADF;
        int ret;
        union bpf_attr *attr;
 
+       if (prog->fd_cgroup >= 0 || prog->kernel_fd >= 0)
+               return ret_errno(EBUSY);
+
        if (fd_cgroup < 0)
                return ret_errno(EBADF);
 
        if (flags & ~(BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE))
                return syserrno_set(-EINVAL, "Invalid flags for bpf program");
 
-       if (((flags & BPF_F_REPLACE) && replace_bpf_fd < 0) ||
-           (replace_bpf_fd >= 0 && !(flags & BPF_F_REPLACE)))
-               return syserrno_set(-EINVAL, "Requested to replace bpf program with invalid parameters");
-
        /*
         * Don't allow the bpf program to be overwritten for now. If we ever
         * allow this we need to verify that the attach_flags of the current
@@ -380,18 +379,6 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type, int fd_cgroup,
        if (flags & BPF_F_ALLOW_OVERRIDE)
                INFO("Allowing to override bpf program");
 
-       if (prog->fd_cgroup >= 0) {
-               if (prog->attached_type != type)
-                       return syserrno_set(-EBUSY, "Wrong type for bpf program");
-
-               /*
-                * For BPF_F_ALLOW_OVERRIDE the flags of the new and old
-                * program must match.
-                */
-               if ((flags & BPF_F_ALLOW_OVERRIDE) && (prog->attached_flags != flags))
-                       return syserrno_set(-EBUSY, "Wrong flags for bpf program");
-       }
-
        /* Leave the caller's fd alone. */
        fd_attach = dup_cloexec(fd_cgroup);
        if (fd_attach < 0)
@@ -408,19 +395,15 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type, int fd_cgroup,
                .attach_flags   = flags,
        };
 
-       if (flags & BPF_F_REPLACE)
-               attr->replace_bpf_fd = replace_bpf_fd;
-
        ret = bpf(BPF_PROG_ATTACH, attr, sizeof(*attr));
        if (ret < 0)
                return syserrno(-errno, "Failed to attach bpf program");
 
-       swap(prog->fd_cgroup, fd_attach);
-       prog->attached_type = type;
-       prog->attached_flags = flags;
+       prog->fd_cgroup         = move_fd(fd_attach);
+       prog->attached_type     = type;
+       prog->attached_flags    = flags;
 
-       TRACE("Attached bpf program to cgroup %d%s", prog->fd_cgroup,
-             (flags & BPF_F_REPLACE) ? " and replaced old bpf program" : "");
+       TRACE("Attached bpf program to cgroup %d", prog->fd_cgroup);
        return 0;
 }
 
@@ -614,7 +597,7 @@ bool bpf_cgroup_devices_attach(struct cgroup_ops *ops, struct lxc_list *devices)
                return syserrno(false, "Failed to create bpf program");
 
        ret = bpf_program_cgroup_attach(prog, BPF_CGROUP_DEVICE,
-                                       ops->unified->cgfd_limit, -EBADF,
+                                       ops->unified->cgfd_limit,
                                        BPF_F_ALLOW_MULTI);
        if (ret)
                return syserrno(false, "Failed to attach bpf program");
index 8c7231a72f55103619f7fad8cc08e0e81965181a..89dc0260c21bbc038344f12872ed7486fc4a775d 100644 (file)
@@ -87,10 +87,6 @@ __hidden extern struct bpf_program *bpf_program_new(__u32 prog_type);
 __hidden extern int bpf_program_init(struct bpf_program *prog);
 __hidden extern int bpf_program_append_device(struct bpf_program *prog, struct device_item *device);
 __hidden extern int bpf_program_finalize(struct bpf_program *prog);
-__hidden extern int bpf_program_cgroup_attach(struct bpf_program *prog,
-                                             int type, int fd_cgroup,
-                                             __owns int replace_bpf_fd,
-                                             __u32 flags);
 __hidden extern int bpf_program_cgroup_detach(struct bpf_program *prog);
 __hidden extern void bpf_program_free(struct bpf_program *prog);
 __hidden extern void bpf_device_program_free(struct cgroup_ops *ops);