cgroups/cgfsng: rework legacy cpuset handling

parent 3fe8773d
...@@ -271,12 +271,12 @@ static uint32_t *lxc_cpumask(char *buf, size_t nbits) ...@@ -271,12 +271,12 @@ static uint32_t *lxc_cpumask(char *buf, size_t nbits)
{ {
char *token; char *token;
size_t arrlen; size_t arrlen;
uint32_t *bitarr; __do_free uint32_t *bitarr = NULL;
arrlen = BITS_TO_LONGS(nbits); arrlen = BITS_TO_LONGS(nbits);
bitarr = calloc(arrlen, sizeof(uint32_t)); bitarr = calloc(arrlen, sizeof(uint32_t));
if (!bitarr) if (!bitarr)
return NULL; return ret_set_errno(NULL, ENOMEM);
lxc_iterate_parts(token, buf, ",") { lxc_iterate_parts(token, buf, ",") {
errno = 0; errno = 0;
...@@ -289,21 +289,17 @@ static uint32_t *lxc_cpumask(char *buf, size_t nbits) ...@@ -289,21 +289,17 @@ static uint32_t *lxc_cpumask(char *buf, size_t nbits)
if (range) if (range)
end = strtoul(range + 1, NULL, 0); end = strtoul(range + 1, NULL, 0);
if (!(start <= end)) { if (!(start <= end))
free(bitarr); return ret_set_errno(NULL, EINVAL);
return NULL;
}
if (end >= nbits) { if (end >= nbits)
free(bitarr); return ret_set_errno(NULL, EINVAL);
return NULL;
}
while (start <= end) while (start <= end)
set_bit(start++, bitarr); set_bit(start++, bitarr);
} }
return bitarr; return move_ptr(bitarr);
} }
/* Turn cpumask into simple, comma-separated cpulist. */ /* Turn cpumask into simple, comma-separated cpulist. */
...@@ -328,12 +324,12 @@ static char *lxc_cpumask_to_cpulist(uint32_t *bitarr, size_t nbits) ...@@ -328,12 +324,12 @@ static char *lxc_cpumask_to_cpulist(uint32_t *bitarr, size_t nbits)
ret = lxc_append_string(&cpulist, numstr); ret = lxc_append_string(&cpulist, numstr);
if (ret < 0) { if (ret < 0) {
lxc_free_array((void **)cpulist, free); lxc_free_array((void **)cpulist, free);
return NULL; return ret_set_errno(NULL, ENOMEM);
} }
} }
if (!cpulist) if (!cpulist)
return NULL; return ret_set_errno(NULL, ENOMEM);
tmp = lxc_string_join(",", (const char **)cpulist, false); tmp = lxc_string_join(",", (const char **)cpulist, false);
lxc_free_array((void **)cpulist, free); lxc_free_array((void **)cpulist, free);
...@@ -374,7 +370,8 @@ static ssize_t get_max_cpus(char *cpulist) ...@@ -374,7 +370,8 @@ static ssize_t get_max_cpus(char *cpulist)
#define __ISOL_CPUS "/sys/devices/system/cpu/isolated" #define __ISOL_CPUS "/sys/devices/system/cpu/isolated"
#define __OFFLINE_CPUS "/sys/devices/system/cpu/offline" #define __OFFLINE_CPUS "/sys/devices/system/cpu/offline"
static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized) static bool cg_legacy_filter_and_set_cpus(const char *parent_cgroup,
char *child_cgroup, bool am_initialized)
{ {
__do_free char *cpulist = NULL, *fpath = NULL, *isolcpus = NULL, __do_free char *cpulist = NULL, *fpath = NULL, *isolcpus = NULL,
*offlinecpus = NULL, *posscpus = NULL; *offlinecpus = NULL, *posscpus = NULL;
...@@ -382,23 +379,14 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized) ...@@ -382,23 +379,14 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized)
*possmask = NULL; *possmask = NULL;
int ret; int ret;
ssize_t i; ssize_t i;
char *lastslash;
ssize_t maxisol = 0, maxoffline = 0, maxposs = 0; ssize_t maxisol = 0, maxoffline = 0, maxposs = 0;
bool bret = false, flipped_bit = false; bool flipped_bit = false;
lastslash = strrchr(path, '/'); SYSERROR("AAAA: %s | %s", parent_cgroup, child_cgroup);
if (!lastslash) { fpath = must_make_path(parent_cgroup, "cpuset.cpus", NULL);
ERROR("Failed to detect \"/\" in \"%s\"", path);
return bret;
}
*lastslash = '\0';
fpath = must_make_path(path, "cpuset.cpus", NULL);
*lastslash = '/';
posscpus = read_file(fpath); posscpus = read_file(fpath);
if (!posscpus) { if (!posscpus)
SYSERROR("Failed to read file \"%s\"", fpath); return log_error_errno(false, errno, "Failed to read file \"%s\"", fpath);
return false;
}
/* Get maximum number of cpus found in possible cpuset. */ /* Get maximum number of cpus found in possible cpuset. */
maxposs = get_max_cpus(posscpus); maxposs = get_max_cpus(posscpus);
...@@ -407,10 +395,8 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized) ...@@ -407,10 +395,8 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized)
if (file_exists(__ISOL_CPUS)) { if (file_exists(__ISOL_CPUS)) {
isolcpus = read_file(__ISOL_CPUS); isolcpus = read_file(__ISOL_CPUS);
if (!isolcpus) { if (!isolcpus)
SYSERROR("Failed to read file \"%s\"", __ISOL_CPUS); return log_error_errno(false, errno, "Failed to read file \"%s\"", __ISOL_CPUS);
return false;
}
if (isdigit(isolcpus[0])) { if (isdigit(isolcpus[0])) {
/* Get maximum number of cpus found in isolated cpuset. */ /* Get maximum number of cpus found in isolated cpuset. */
...@@ -428,10 +414,8 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized) ...@@ -428,10 +414,8 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized)
if (file_exists(__OFFLINE_CPUS)) { if (file_exists(__OFFLINE_CPUS)) {
offlinecpus = read_file(__OFFLINE_CPUS); offlinecpus = read_file(__OFFLINE_CPUS);
if (!offlinecpus) { if (!offlinecpus)
SYSERROR("Failed to read file \"%s\"", __OFFLINE_CPUS); return log_error_errno(false, errno, "Failed to read file \"%s\"", __OFFLINE_CPUS);
return false;
}
if (isdigit(offlinecpus[0])) { if (isdigit(offlinecpus[0])) {
/* Get maximum number of cpus found in offline cpuset. */ /* Get maximum number of cpus found in offline cpuset. */
...@@ -453,25 +437,19 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized) ...@@ -453,25 +437,19 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized)
} }
possmask = lxc_cpumask(posscpus, maxposs); possmask = lxc_cpumask(posscpus, maxposs);
if (!possmask) { if (!possmask)
ERROR("Failed to create cpumask for possible cpus"); return log_error_errno(false, errno, "Failed to create cpumask for possible cpus");
return false;
}
if (maxisol > 0) { if (maxisol > 0) {
isolmask = lxc_cpumask(isolcpus, maxposs); isolmask = lxc_cpumask(isolcpus, maxposs);
if (!isolmask) { if (!isolmask)
ERROR("Failed to create cpumask for isolated cpus"); return log_error_errno(false, errno, "Failed to create cpumask for isolated cpus");
return false;
}
} }
if (maxoffline > 0) { if (maxoffline > 0) {
offlinemask = lxc_cpumask(offlinecpus, maxposs); offlinemask = lxc_cpumask(offlinecpus, maxposs);
if (!offlinemask) { if (!offlinemask)
ERROR("Failed to create cpumask for offline cpus"); return log_error_errno(false, errno, "Failed to create cpumask for offline cpus");
return false;
}
} }
for (i = 0; i <= maxposs; i++) { for (i = 0; i <= maxposs; i++) {
...@@ -491,18 +469,16 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized) ...@@ -491,18 +469,16 @@ static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized)
cpulist = move_ptr(posscpus); cpulist = move_ptr(posscpus);
TRACE("Removed isolated or offline cpus from cpuset"); TRACE("Removed isolated or offline cpus from cpuset");
} }
if (!cpulist) { if (!cpulist)
ERROR("Failed to create cpu list"); return log_error_errno(false, errno, "Failed to create cpu list");
return false;
}
copy_parent: copy_parent:
if (!am_initialized) { if (!am_initialized) {
ret = lxc_write_openat(path, "cpuset.cpus", cpulist, strlen(cpulist)); ret = lxc_write_openat(child_cgroup, "cpuset.cpus", cpulist, strlen(cpulist));
if (ret < 0) if (ret < 0)
return log_error_errno(false, return log_error_errno(false,
errno, "Failed to write cpu list to \"%s/cpuset.cpus\"", errno, "Failed to write cpu list to \"%s/cpuset.cpus\"",
path); child_cgroup);
TRACE("Copied cpu settings of parent cgroup"); TRACE("Copied cpu settings of parent cgroup");
} }
...@@ -511,40 +487,32 @@ copy_parent: ...@@ -511,40 +487,32 @@ copy_parent:
} }
/* Copy contents of parent(@path)/@file to @path/@file */ /* Copy contents of parent(@path)/@file to @path/@file */
static bool copy_parent_file(char *path, char *file) static bool copy_parent_file(const char *parent_cgroup,
const char *child_cgroup, const char *file)
{ {
__do_free char *parent_path = NULL, *value = NULL; __do_free char *parent_file = NULL, *value = NULL;
int len = 0; int len = 0;
char *lastslash = NULL;
int ret; int ret;
lastslash = strrchr(path, '/'); parent_file = must_make_path(parent_cgroup, file, NULL);
if (!lastslash) len = lxc_read_from_file(parent_file, NULL, 0);
return log_error_errno(false, ENOENT,
"Failed to detect \"/\" in \"%s\"", path);
*lastslash = '\0';
parent_path = must_make_path(path, file, NULL);
*lastslash = '/';
len = lxc_read_from_file(parent_path, NULL, 0);
if (len <= 0) if (len <= 0)
return log_error_errno(false, errno, return log_error_errno(false, errno,
"Failed to determine buffer size"); "Failed to determine buffer size");
value = must_realloc(NULL, len + 1); value = must_realloc(NULL, len + 1);
value[len] = '\0'; value[len] = '\0';
ret = lxc_read_from_file(parent_path, value, len); ret = lxc_read_from_file(parent_file, value, len);
if (ret != len) if (ret != len)
return log_error_errno(false, errno, return log_error_errno(false, errno,
"Failed to read from parent file \"%s\"", "Failed to read from parent file \"%s\"",
parent_path); parent_file);
ret = lxc_write_openat(path, file, value, len); ret = lxc_write_openat(child_cgroup, file, value, len);
if (ret < 0 && errno != EACCES) if (ret < 0 && errno != EACCES)
return log_error_errno(false, return log_error_errno(false,
errno, "Failed to write \"%s\" to file \"%s/%s\"", errno, "Failed to write \"%s\" to file \"%s/%s\"",
value, path, file); value, child_cgroup, file);
return true; return true;
} }
...@@ -565,10 +533,9 @@ static bool is_unified_hierarchy(const struct hierarchy *h) ...@@ -565,10 +533,9 @@ static bool is_unified_hierarchy(const struct hierarchy *h)
static int cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h, static int cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h,
const char *cgroup_leaf) const char *cgroup_leaf)
{ {
int fret = -1; __do_free char *parent_cgroup = NULL, *child_cgroup = NULL, *dup = NULL;
__do_free char *parent_or_self = NULL, *dup = NULL;
__do_close_prot_errno int cgroup_fd = -EBADF; __do_close_prot_errno int cgroup_fd = -EBADF;
size_t len; int fret = -1;
int ret; int ret;
char v; char v;
char *leaf, *slash; char *leaf, *slash;
...@@ -586,33 +553,36 @@ static int cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h, ...@@ -586,33 +553,36 @@ static int cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h,
if (!dup) if (!dup)
return ret_set_errno(-1, ENOMEM); return ret_set_errno(-1, ENOMEM);
parent_cgroup = must_make_path(h->mountpoint, h->container_base_path, NULL);
leaf = dup;
leaf += strspn(leaf, "/"); leaf += strspn(leaf, "/");
slash = strchr(leaf, '/'); slash = strchr(leaf, '/');
if (slash) if (slash)
*slash = '\0'; *slash = '\0';
parent_or_self = must_make_path(h->mountpoint, h->container_base_path, leaf, NULL); child_cgroup = must_make_path(parent_cgroup, leaf, NULL);
if (slash) if (slash)
*slash = '/'; *slash = '/';
fret = 1; fret = 1;
ret = mkdir(parent_or_self, 0755); ret = mkdir(child_cgroup, 0755);
if (ret < 0) { if (ret < 0) {
if (errno != EEXIST) if (errno != EEXIST)
return log_error_errno(-1, errno, "Failed to create directory \"%s\"", parent_or_self); return log_error_errno(-1, errno, "Failed to create directory \"%s\"", child_cgroup);
fret = 0; fret = 0;
} }
cgroup_fd = lxc_open_dirfd(parent_or_self); cgroup_fd = lxc_open_dirfd(child_cgroup);
if (cgroup_fd < 0) if (cgroup_fd < 0)
return -1; return -1;
ret = lxc_readat(cgroup_fd, "cgroup.clone_children", &v, 1); ret = lxc_readat(cgroup_fd, "cgroup.clone_children", &v, 1);
if (ret < 0) if (ret < 0)
return log_error_errno(-1, errno, "Failed to read file \"%s/cgroup.clone_children\"", parent_or_self); return log_error_errno(-1, errno, "Failed to read file \"%s/cgroup.clone_children\"", child_cgroup);
/* Make sure any isolated cpus are removed from cpuset.cpus. */ /* Make sure any isolated cpus are removed from cpuset.cpus. */
if (!cg_legacy_filter_and_set_cpus(parent_or_self, v == '1')) if (!cg_legacy_filter_and_set_cpus(parent_cgroup, child_cgroup, v == '1'))
return log_error_errno(-1, errno, "Failed to remove isolated cpus"); return log_error_errno(-1, errno, "Failed to remove isolated cpus");
/* Already set for us by someone else. */ /* Already set for us by someone else. */
...@@ -620,13 +590,13 @@ static int cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h, ...@@ -620,13 +590,13 @@ static int cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h,
TRACE("\"cgroup.clone_children\" was already set to \"1\""); TRACE("\"cgroup.clone_children\" was already set to \"1\"");
/* copy parent's settings */ /* copy parent's settings */
if (!copy_parent_file(parent_or_self, "cpuset.mems")) if (!copy_parent_file(parent_cgroup, child_cgroup, "cpuset.mems"))
return log_error_errno(-1, errno, "Failed to copy \"cpuset.mems\" settings"); return log_error_errno(-1, errno, "Failed to copy \"cpuset.mems\" settings");
/* Set clone_children so children inherit our settings */ /* Set clone_children so children inherit our settings */
ret = lxc_writeat(cgroup_fd, "cgroup.clone_children", "1", 1); ret = lxc_writeat(cgroup_fd, "cgroup.clone_children", "1", 1);
if (ret < 0) if (ret < 0)
return log_error_errno(-1, errno, "Failed to write 1 to \"%s/cgroup.clone_children\"", parent_or_self); return log_error_errno(-1, errno, "Failed to write 1 to \"%s/cgroup.clone_children\"", child_cgroup);
return fret; return fret;
} }
......
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