From dcce65bde49dd86046ffd87091cfd17b2af81be5 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Fri, 6 Dec 2024 10:38:17 +0530 Subject: [PATCH] cgconfig: Add support for multiple controllers on the same mount path 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 Acked-by: Tom Hromatka Signed-off-by: Kamalesh Babulal --- src/config.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/config.c b/src/config.c index 5095a501..b489b668 100644 --- a/src/config.c +++ b/src/config.c @@ -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) { -- 2.47.3