]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgconfig: Add support for multiple controllers on the same mount path
authorlipengyu <lipengyu@kylinos.cn>
Fri, 6 Dec 2024 05:08:17 +0000 (10:38 +0530)
committerKamalesh Babulal <kamalesh.babulal@oracle.com>
Fri, 6 Dec 2024 05:38:19 +0000 (11:08 +0530)
Add support to cgconfigparser for mounting multiple controllers to the
same mount point.

Given the following config:

mount {
cpu = /cg_sys;
cpuacct = /cg_sys;
}

cgconfigparser should mount both the cpu and cpuacct controllers to
/cg_sys. The mount should look as follows:

cgroup on /cg_sys type cgroup (rw,relatime,seclabel,cpu,cpuacct)

Without this change, the mount->name gets overwritten and the mount
fails, which can be categorized into two cases:

1.if cpu and cpuacct are at the same hierarchy before being mounted:

Error: cannot mount cpu to /cg_sys: Device or resource busy

2.if cpu and cpuacct are not at the same hierarchy before being mounted:

cgroup on /cg_sys type cgroup (rw,relatime,seclabel,cpu)

[TJH suggested improvements to commit message]
[Kamalesh fixed a few checkpatch warnings]
Signed-off-by: lipengyu <lipengyu@kylinos.cn>
Acked-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/config.c

index 5095a501bb5ffe32d82b70f6fb37c71e199732d8..b489b668c282bf147c54aad4bbcf05ddb0e31959 100644 (file)
@@ -629,6 +629,22 @@ void cgroup_config_cleanup_namespace_table(void)
        memset(&config_namespace_table, 0, sizeof(struct cg_mount_table_s) * CG_CONTROLLER_MAX);
 }
 
+static int is_valid_controller(char *ctrl)
+{
+       int i;
+
+       static const char * const ctrl_list[] = { "blkio", "cpu", "cpuacct", "cpuset", "devices",
+                                                 "freezer", "hugetlb", "memory", "misc", "net_cls",
+                                                 "net_prio", "perf_event", "pids", "rdma", NULL };
+
+       for (i = 0; ctrl_list[i]; i++) {
+               if (strncmp(ctrl, ctrl_list[i], strlen(ctrl_list[i])) == 0)
+                       return 1;
+       }
+
+       return 0;
+}
+
 /**
  * Add necessary options for mount. Currently only 'none' option is added
  * for mounts with only 'name=xxx' and without real controller.
@@ -657,6 +673,9 @@ static int cgroup_config_ajdust_mount_options(struct cg_mount_table_s *mount, un
 
                                strncpy(mount->name, controller, sizeof(mount->name));
                                mount->name[sizeof(mount->name)-1] = '\0';
+                               free(controller);
+                               token = strtok_r(NULL, ",", &save);
+                               continue;
                        }
 
                        if (strncmp(token, "nodev", strlen("nodev")) == 0)
@@ -668,6 +687,18 @@ static int cgroup_config_ajdust_mount_options(struct cg_mount_table_s *mount, un
                        if (strncmp(token, "nosuid", strlen("nosuid")) == 0)
                                *flags |= MS_NOSUID;
 
+                       if (is_valid_controller(token)) {
+                               controller = strdup(token);
+                               if (controller == NULL)
+                                       break;
+
+                               strncat(mount->name, ",", FILENAME_MAX - strlen(mount->name)-1);
+                               strncat(mount->name, controller,
+                                               FILENAME_MAX - strlen(mount->name) - 1);
+
+                               free(controller);
+               }
+
                } else if (!name_only) {
                        /*
                         * We have controller + name=, do the right thing,
@@ -679,7 +710,6 @@ static int cgroup_config_ajdust_mount_options(struct cg_mount_table_s *mount, un
                token = strtok_r(NULL, ",", &save);
        }
 
-       free(controller);
        free(opts);
 
        if (name_only) {