]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
selftests/bpf: enable all available cgroup v2 controllers
authorRoman Gushchin <guro@fb.com>
Sat, 25 May 2019 16:37:41 +0000 (09:37 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 28 May 2019 16:30:02 +0000 (09:30 -0700)
Enable all available cgroup v2 controllers when setting up
the environment for the bpf kselftests. It's required to properly test
the bpf prog auto-detach feature. Also it will generally increase
the code coverage.

Signed-off-by: Roman Gushchin <guro@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/cgroup_helpers.c

index 6692a40a6979eac0b48665e77566e6beef066d17..0d89f0396be4c5690cb1ccbaf1564e3179ec2a8b 100644 (file)
        snprintf(buf, sizeof(buf), "%s%s%s", CGROUP_MOUNT_PATH, \
                 CGROUP_WORK_DIR, path)
 
+/**
+ * enable_all_controllers() - Enable all available cgroup v2 controllers
+ *
+ * Enable all available cgroup v2 controllers in order to increase
+ * the code coverage.
+ *
+ * If successful, 0 is returned.
+ */
+int enable_all_controllers(char *cgroup_path)
+{
+       char path[PATH_MAX + 1];
+       char buf[PATH_MAX];
+       char *c, *c2;
+       int fd, cfd;
+       size_t len;
+
+       snprintf(path, sizeof(path), "%s/cgroup.controllers", cgroup_path);
+       fd = open(path, O_RDONLY);
+       if (fd < 0) {
+               log_err("Opening cgroup.controllers: %s", path);
+               return 1;
+       }
+
+       len = read(fd, buf, sizeof(buf) - 1);
+       if (len < 0) {
+               close(fd);
+               log_err("Reading cgroup.controllers: %s", path);
+               return 1;
+       }
+       buf[len] = 0;
+       close(fd);
+
+       /* No controllers available? We're probably on cgroup v1. */
+       if (len == 0)
+               return 0;
+
+       snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
+       cfd = open(path, O_RDWR);
+       if (cfd < 0) {
+               log_err("Opening cgroup.subtree_control: %s", path);
+               return 1;
+       }
+
+       for (c = strtok_r(buf, " ", &c2); c; c = strtok_r(NULL, " ", &c2)) {
+               if (dprintf(cfd, "+%s\n", c) <= 0) {
+                       log_err("Enabling controller %s: %s", c, path);
+                       close(cfd);
+                       return 1;
+               }
+       }
+       close(cfd);
+       return 0;
+}
+
 /**
  * setup_cgroup_environment() - Setup the cgroup environment
  *
@@ -71,6 +125,9 @@ int setup_cgroup_environment(void)
                return 1;
        }
 
+       if (enable_all_controllers(cgroup_workdir))
+               return 1;
+
        return 0;
 }