From: Tejun Heo Date: Mon, 15 Aug 2016 22:13:36 +0000 (-0400) Subject: core: rename cg_unified() to cg_all_unified() X-Git-Tag: v232.2~91 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=682a7fe521cc3d983dc6c690cbc107a3c8a13344 core: rename cg_unified() to cg_all_unified() A following patch will update cgroup handling so that the elogind controller (/sys/fs/cgroup/elogind) can use the unified hierarchy even if the kernel resource controllers are on the legacy hierarchies. This would require distinguishing whether all controllers are on cgroup v2 or only the elogind controller is. In preparation, this patch renames cg_unified() to cg_all_unified(). This patch doesn't cause any functional changes. --- diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 604ce2b31..e3b67e3d8 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -876,7 +876,7 @@ int cg_set_task_access( if (r < 0) return r; - unified = cg_unified(controller); + unified = cg_all_unified(); if (unified < 0) return unified; if (unified) @@ -901,17 +901,18 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) { assert(path); assert(pid >= 0); - if (controller) { - if (!cg_controller_is_valid(controller)) - return -EINVAL; - } else - controller = SYSTEMD_CGROUP_CONTROLLER; - - unified = cg_unified(controller); + unified = cg_all_unified(); if (unified < 0) return unified; - if (unified == 0) + if (unified == 0) { + if (controller) { + if (!cg_controller_is_valid(controller)) + return -EINVAL; + } else + controller = SYSTEMD_CGROUP_CONTROLLER; + cs = strlen(controller); + } fs = procfs_file_alloca(pid, "cgroup"); log_debug_elogind("Searching for PID %u in \"%s\" (controller \"%s\")", @@ -979,7 +980,7 @@ int cg_install_release_agent(const char *controller, const char *agent) { assert(agent); - unified = cg_unified(controller); + unified = cg_all_unified(); if (unified < 0) return unified; if (unified) /* doesn't apply to unified hierarchy */ @@ -1030,7 +1031,7 @@ int cg_uninstall_release_agent(const char *controller) { _cleanup_free_ char *fs = NULL; int r, unified; - unified = cg_unified(controller); + unified = cg_all_unified(); if (unified < 0) return unified; if (unified) /* Doesn't apply to unified hierarchy */ @@ -1086,7 +1087,7 @@ int cg_is_empty_recursive(const char *controller, const char *path) { if (controller && (isempty(path) || path_equal(path, "/"))) return false; - unified = cg_unified(controller); + unified = cg_all_unified(); if (unified < 0) return unified; @@ -2275,10 +2276,9 @@ int cg_kernel_controllers(Set *controllers) { } #endif // 0 -static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN; - -static int cg_update_unified(void) { +static thread_local int unified_cache = -1; +int cg_all_unified(void) { struct statfs fs; /* Checks if we support the unified hierarchy. Returns an @@ -2286,16 +2286,14 @@ static int cg_update_unified(void) { * have any other trouble determining if the unified hierarchy * is supported. */ - if (unified_cache >= CGROUP_UNIFIED_NONE) - return 0; + if (unified_cache >= 0) + return unified_cache; if (statfs("/sys/fs/cgroup/", &fs) < 0) return -errno; #if 0 /// UNNEEDED by elogind if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) - unified_cache = CGROUP_UNIFIED_ALL; - else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) { #else /* elogind can not support the unified hierarchy as a controller, * so always assume a classical hierarchy. @@ -2304,39 +2302,18 @@ static int cg_update_unified(void) { * add such a support. */ if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) { #endif // 0 - if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0) - return -errno; - - unified_cache = F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC) ? - CGROUP_UNIFIED_SYSTEMD : CGROUP_UNIFIED_NONE; - } else - return -ENOMEDIUM; - - return 0; -} - -int cg_unified(const char *controller) { - - int r; - - r = cg_update_unified(); - if (r < 0) - return r; - - if (streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER)) - return unified_cache >= CGROUP_UNIFIED_SYSTEMD; + unified_cache = true; + else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) + unified_cache = false; else - return unified_cache >= CGROUP_UNIFIED_ALL; -} - -int cg_all_unified(void) { + return -ENOMEDIUM; - return cg_unified(NULL); + return unified_cache; } #if 0 /// UNNEEDED by elogind void cg_unified_flush(void) { - unified_cache = CGROUP_UNIFIED_UNKNOWN; + unified_cache = -1; } int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) { diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h index d6e2a8ee1..0255e2850 100644 --- a/src/basic/cgroup-util.h +++ b/src/basic/cgroup-util.h @@ -114,13 +114,6 @@ static inline bool CGROUP_BLKIO_WEIGHT_IS_OK(uint64_t x) { } #endif // 0 -typedef enum CGroupUnified { - CGROUP_UNIFIED_UNKNOWN = -1, - CGROUP_UNIFIED_NONE = 0, /* Both systemd and controllers on legacy */ - CGROUP_UNIFIED_SYSTEMD = 1, /* Only systemd on unified */ - CGROUP_UNIFIED_ALL = 2, /* Both systemd and controllers on unified */ -} CGroupUnified; - /* * General rules: * @@ -246,13 +239,11 @@ bool cg_ns_supported(void); #if 0 /// UNNEEDED by elogind int cg_all_unified(void); -int cg_unified(const char *controller); void cg_unified_flush(void); bool cg_is_unified_wanted(void); #endif // 0 bool cg_is_legacy_wanted(void); -bool cg_is_unified_systemd_controller_wanted(void); const char* cgroup_controller_to_string(CGroupController c) _const_; CGroupController cgroup_controller_from_string(const char *s) _pure_; diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 56f065fdb..a7bb97985 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -1247,7 +1247,7 @@ int unit_watch_cgroup(Unit *u) { return 0; /* Only applies to the unified hierarchy */ - r = cg_unified(SYSTEMD_CGROUP_CONTROLLER); + r = cg_all_unified(); if (r < 0) return log_unit_error_errno(u, r, "Failed detect whether the unified hierarchy is used: %m"); if (r == 0) @@ -1647,7 +1647,7 @@ int unit_watch_all_pids(Unit *u) { if (!u->cgroup_path) return -ENOENT; - if (cg_unified(SYSTEMD_CGROUP_CONTROLLER) > 0) /* On unified we can use proper notifications */ + if (cg_all_unified() > 0) /* On unified we can use proper notifications */ return 0; return unit_watch_pids_in_path(u, u->cgroup_path); @@ -1721,7 +1721,7 @@ static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents, int manager_setup_cgroup(Manager *m) { _cleanup_free_ char *path = NULL; CGroupController c; - int r, all_unified, systemd_unified; + int r, unified; char *e; assert(m); @@ -1762,17 +1762,11 @@ int manager_setup_cgroup(Manager *m) { if (r < 0) return log_error_errno(r, "Cannot find cgroup mount point: %m"); - all_unified = cg_all_unified(); - systemd_unified = cg_unified(SYSTEMD_CGROUP_CONTROLLER); - - if (all_unified < 0 || systemd_unified < 0) - return log_error_errno(all_unified < 0 ? all_unified : systemd_unified, - "Couldn't determine if we are running in the unified hierarchy: %m"); - - if (all_unified > 0) + unified = cg_all_unified(); + if (unified < 0) + return log_error_errno(r, "Couldn't determine if we are running in the unified hierarchy: %m"); + if (unified > 0) log_debug("Unified cgroup hierarchy is located at %s.", path); - else if (systemd_unified > 0) - log_debug("Unified cgroup hierarchy is located at %s. Controllers are on legacy hierarchies.", path); else log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path); @@ -1780,7 +1774,7 @@ int manager_setup_cgroup(Manager *m) { const char *scope_path; /* 3. Install agent */ - if (systemd_unified) { + if (unified) { /* In the unified hierarchy we can get * cgroup empty notifications via inotify. */ @@ -1858,7 +1852,7 @@ int manager_setup_cgroup(Manager *m) { return log_error_errno(errno, "Failed to open pin file: %m"); /* 6. Always enable hierarchical support if it exists... */ - if (!all_unified) + if (!unified) (void) cg_set_attribute("memory", "/", "memory.use_hierarchy", "1"); }