cgroups: introduce cgroup hierarchy type

parent e821c3b7
...@@ -269,7 +269,7 @@ static ssize_t get_max_cpus(char *cpulist) ...@@ -269,7 +269,7 @@ static ssize_t get_max_cpus(char *cpulist)
static inline bool is_unified_hierarchy(const struct hierarchy *h) static inline bool is_unified_hierarchy(const struct hierarchy *h)
{ {
return h->version == CGROUP2_SUPER_MAGIC; return h->fs_type == UNIFIED_HIERARCHY;
} }
/* Return true if the controller @entry is found in the null-terminated list of /* Return true if the controller @entry is found in the null-terminated list of
...@@ -389,7 +389,7 @@ static bool skip_hierarchy(const struct cgroup_ops *ops, char **controllers) ...@@ -389,7 +389,7 @@ static bool skip_hierarchy(const struct cgroup_ops *ops, char **controllers)
static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt, static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt,
int dfd_base, char *base_cgroup, int dfd_base, char *base_cgroup,
char **controllers, int type) char **controllers, cgroupfs_type_magic_t fs_type)
{ {
__do_free struct hierarchy *new = NULL; __do_free struct hierarchy *new = NULL;
int idx; int idx;
...@@ -405,7 +405,7 @@ static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt, ...@@ -405,7 +405,7 @@ static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt,
new->cgfd_limit = -EBADF; new->cgfd_limit = -EBADF;
new->cgfd_mon = -EBADF; new->cgfd_mon = -EBADF;
new->version = type; new->fs_type = fs_type;
new->controllers = controllers; new->controllers = controllers;
new->mountpoint = mnt; new->mountpoint = mnt;
new->container_base_path = base_cgroup; new->container_base_path = base_cgroup;
...@@ -422,7 +422,7 @@ static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt, ...@@ -422,7 +422,7 @@ static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt,
if (idx < 0) if (idx < 0)
return ret_errno(idx); return ret_errno(idx);
if (type == CGROUP2_SUPER_MAGIC) if (fs_type == UNIFIED_HIERARCHY)
ops->unified = new; ops->unified = new;
(ops->hierarchies)[idx] = move_ptr(new); (ops->hierarchies)[idx] = move_ptr(new);
...@@ -1318,12 +1318,12 @@ static int chown_cgroup_wrapper(void *data) ...@@ -1318,12 +1318,12 @@ static int chown_cgroup_wrapper(void *data)
* files (which systemd in wily insists on doing). * files (which systemd in wily insists on doing).
*/ */
if (arg->hierarchies[i]->version == CGROUP_SUPER_MAGIC) if (arg->hierarchies[i]->fs_type == LEGACY_HIERARCHY)
(void)fchowmodat(dirfd, "tasks", destuid, nsgid, 0664); (void)fchowmodat(dirfd, "tasks", destuid, nsgid, 0664);
(void)fchowmodat(dirfd, "cgroup.procs", destuid, nsgid, 0664); (void)fchowmodat(dirfd, "cgroup.procs", destuid, nsgid, 0664);
if (arg->hierarchies[i]->version != CGROUP2_SUPER_MAGIC) if (arg->hierarchies[i]->fs_type != UNIFIED_HIERARCHY)
continue; continue;
for (char **p = arg->hierarchies[i]->cgroup2_chown; p && *p; p++) for (char **p = arg->hierarchies[i]->cgroup2_chown; p && *p; p++)
...@@ -2323,7 +2323,7 @@ __cgfsng_ops static bool cgfsng_attach(struct cgroup_ops *ops, ...@@ -2323,7 +2323,7 @@ __cgfsng_ops static bool cgfsng_attach(struct cgroup_ops *ops,
__do_free char *fullpath = NULL, *path = NULL; __do_free char *fullpath = NULL, *path = NULL;
struct hierarchy *h = ops->hierarchies[i]; struct hierarchy *h = ops->hierarchies[i];
if (h->version == CGROUP2_SUPER_MAGIC) { if (h->fs_type == UNIFIED_HIERARCHY) {
ret = __cg_unified_attach(h, conf, name, lxcpath, pid, ret = __cg_unified_attach(h, conf, name, lxcpath, pid,
h->controllers[0]); h->controllers[0]);
if (ret < 0) if (ret < 0)
...@@ -3078,6 +3078,8 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative, ...@@ -3078,6 +3078,8 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative,
if (unified_cgroup(line)) { if (unified_cgroup(line)) {
char *unified_mnt; char *unified_mnt;
type = UNIFIED_HIERARCHY;
current_cgroup = current_unified_cgroup(relative, line); current_cgroup = current_unified_cgroup(relative, line);
if (IS_ERR(current_cgroup)) if (IS_ERR(current_cgroup))
return PTR_ERR(current_cgroup); return PTR_ERR(current_cgroup);
...@@ -3121,13 +3123,14 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative, ...@@ -3121,13 +3123,14 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative,
return syserrno(-ENOMEM, "Failed to create empty controller list"); return syserrno(-ENOMEM, "Failed to create empty controller list");
} }
type = CGROUP2_SUPER_MAGIC;
controllers = strdup(unified_mnt); controllers = strdup(unified_mnt);
if (!controllers) if (!controllers)
return ret_errno(ENOMEM); return ret_errno(ENOMEM);
} else { } else {
char *__controllers, *__current_cgroup; char *__controllers, *__current_cgroup;
type = LEGACY_HIERARCHY;
__controllers = strchr(line, ':'); __controllers = strchr(line, ':');
if (!__controllers) if (!__controllers)
return ret_errno(EINVAL); return ret_errno(EINVAL);
...@@ -3195,7 +3198,6 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative, ...@@ -3195,7 +3198,6 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative,
if (skip_hierarchy(ops, controller_list)) if (skip_hierarchy(ops, controller_list))
continue; continue;
type = CGROUP_SUPER_MAGIC;
ops->cgroup_layout = CGROUP_LAYOUT_LEGACY; ops->cgroup_layout = CGROUP_LAYOUT_LEGACY;
} }
...@@ -3210,7 +3212,7 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative, ...@@ -3210,7 +3212,7 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative,
move_ptr(current_cgroup); move_ptr(current_cgroup);
move_ptr(controllers); move_ptr(controllers);
move_ptr(controller_list); move_ptr(controller_list);
if (type == CGROUP2_SUPER_MAGIC) if (type == UNIFIED_HIERARCHY)
ops->unified->cgroup2_chown = move_ptr(delegate); ops->unified->cgroup2_chown = move_ptr(delegate);
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
#include <linux/magic.h>
#include "compiler.h" #include "compiler.h"
#include "macro.h" #include "macro.h"
...@@ -32,6 +33,11 @@ typedef enum { ...@@ -32,6 +33,11 @@ typedef enum {
CGROUP_LAYOUT_UNIFIED = 2, CGROUP_LAYOUT_UNIFIED = 2,
} cgroup_layout_t; } cgroup_layout_t;
typedef enum {
LEGACY_HIERARCHY = CGROUP_SUPER_MAGIC,
UNIFIED_HIERARCHY = CGROUP2_SUPER_MAGIC,
} cgroupfs_type_magic_t;
/* A descriptor for a mounted hierarchy /* A descriptor for a mounted hierarchy
* *
* @controllers * @controllers
...@@ -81,7 +87,7 @@ struct hierarchy { ...@@ -81,7 +87,7 @@ struct hierarchy {
char *container_base_path; char *container_base_path;
char *container_full_path; char *container_full_path;
char *container_limit_path; char *container_limit_path;
int version; cgroupfs_type_magic_t fs_type;
/* cgroup2 only */ /* cgroup2 only */
unsigned int bpf_device_controller:1; unsigned int bpf_device_controller:1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment