vircgroup: introduce virCgroupV2MakeGroup

When creating cgroup hierarchy we need to enable controllers in the
parent cgroup in order to be usable.  That means writing "+{controller}"
into cgroup.subtree_control file.  We can enable only controllers that
are enabled for parent cgroup, that means we need to do that for the
whole cgroup tree.

Cgroups for threads needs to be handled differently in cgroup v2.  There
are two types of controllers:

    - domain controllers: these cannot be enabled for threads
    - threaded controllers: these can be enabled for threads

In addition there are multiple types of cgroups:

    - domain: normal cgroup
    - domain threaded: a domain cgroup that serves as root for threaded
                       cgroups
    - domain invalid: invalid cgroup, can be changed into threaded, this
                      is the default state if you create subgroup inside
                      domain threaded group or threaded group
    - threaded: threaded cgroup which can have domain threaded or
                threaded as parent group

In order to create threaded cgroup it's sufficient to write "threaded"
into cgroup.type file, it will automatically make parent cgroup
"domain threaded" if it was only "domain".  In case the parent cgroup
is already "domain threaded" or "threaded" it will modify only the type
of current cgroup.  After that we can enable threaded controllers.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Pavel Hrdina 2018-09-18 17:49:19 +02:00
parent 0542640a9c
commit 89f52abd07
3 changed files with 78 additions and 1 deletions

View File

@ -938,7 +938,7 @@ virCgroupNewThread(virCgroupPtr domain,
if (virCgroupNew(-1, name, domain, controllers, group) < 0)
return -1;
if (virCgroupMakeGroup(domain, *group, create, VIR_CGROUP_NONE) < 0) {
if (virCgroupMakeGroup(domain, *group, create, VIR_CGROUP_THREAD) < 0) {
virCgroupFree(group);
return -1;
}

View File

@ -33,6 +33,7 @@ typedef enum {
* before creating subcgroups and
* attaching tasks
*/
VIR_CGROUP_THREAD = 1 << 1, /* cgroup v2 handles threads differently */
} virCgroupBackendFlags;
typedef enum {

View File

@ -337,6 +337,81 @@ virCgroupV2PathOfController(virCgroupPtr group,
}
static int
virCgroupV2EnableController(virCgroupPtr parent,
int controller)
{
VIR_AUTOFREE(char *) val = NULL;
if (virAsprintf(&val, "+%s",
virCgroupV2ControllerTypeToString(controller)) < 0) {
return -1;
}
if (virCgroupSetValueStr(parent, controller,
"cgroup.subtree_control", val) < 0) {
return -1;
}
return 0;
}
static int
virCgroupV2MakeGroup(virCgroupPtr parent ATTRIBUTE_UNUSED,
virCgroupPtr group,
bool create,
unsigned int flags)
{
VIR_AUTOFREE(char *) path = NULL;
int controller;
VIR_DEBUG("Make group %s", group->path);
controller = virCgroupV2GetAnyController(group);
if (virCgroupV2PathOfController(group, controller, "", &path) < 0)
return -1;
VIR_DEBUG("Make controller %s", path);
if (!virFileExists(path) &&
(!create || (mkdir(path, 0755) < 0 && errno != EEXIST))) {
virReportSystemError(errno, _("Failed to create v2 cgroup '%s'"),
group->path);
return -1;
}
if (create) {
if (flags & VIR_CGROUP_THREAD) {
if (virCgroupSetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
"cgroup.type", "threaded") < 0) {
return -1;
}
if (virCgroupV2EnableController(parent,
VIR_CGROUP_CONTROLLER_CPU) < 0) {
return -1;
}
} else {
size_t i;
for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
if (!virCgroupV2HasController(parent, i))
continue;
/* Controllers that are implicitly enabled if available. */
if (i == VIR_CGROUP_CONTROLLER_CPUACCT)
continue;
if (virCgroupV2EnableController(parent, i) < 0)
return -1;
}
}
}
return 0;
}
virCgroupBackend virCgroupV2Backend = {
.type = VIR_CGROUP_BACKEND_TYPE_V2,
@ -352,6 +427,7 @@ virCgroupBackend virCgroupV2Backend = {
.hasController = virCgroupV2HasController,
.getAnyController = virCgroupV2GetAnyController,
.pathOfController = virCgroupV2PathOfController,
.makeGroup = virCgroupV2MakeGroup,
};