[libvirt] [PATCH 0/3] cgroups v2 fixes

Pavel Hrdina (3): vircgroup: fix cgroups v2 controllers detection vircgroupv2: store enabled controllers vircgroupv2: remove ATTRIBUTE_UNUSED for used attribute src/util/vircgroup.c | 2 +- src/util/vircgroupbackend.h | 3 ++- src/util/vircgroupv1.c | 3 ++- src/util/vircgroupv2.c | 36 ++++++++++++++++++++++++------------ 4 files changed, 29 insertions(+), 15 deletions(-) -- 2.21.0

When creating new group for cgroups v2 the we cannot check cgroups.controllers for that cgroup because the directory is created later. In that case we should check cgroups.subtree_control of parent group to get list of controllers enabled for child cgroups. In order to achieve that we will prefer the parent group if it exists, the current group will be used only for root group. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 2 +- src/util/vircgroupbackend.h | 3 ++- src/util/vircgroupv1.c | 3 ++- src/util/vircgroupv2.c | 23 ++++++++++++++++------- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index f7afc2964d..cc21758323 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -407,7 +407,7 @@ virCgroupDetect(virCgroupPtr group, for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) { if (group->backends[i]) { - int rc = group->backends[i]->detectControllers(group, controllers); + int rc = group->backends[i]->detectControllers(group, controllers, parent); if (rc < 0) return -1; controllersAvailable |= rc; diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h index a91719f89d..77451b0cb6 100644 --- a/src/util/vircgroupbackend.h +++ b/src/util/vircgroupbackend.h @@ -98,7 +98,8 @@ typedef char * typedef int (*virCgroupDetectControllersCB)(virCgroupPtr group, - int controllers); + int controllers, + virCgroupPtr parent); typedef bool (*virCgroupHasControllerCB)(virCgroupPtr cgroup, diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c index 7968ab3cf0..4231d8d6fa 100644 --- a/src/util/vircgroupv1.c +++ b/src/util/vircgroupv1.c @@ -420,7 +420,8 @@ virCgroupV1StealPlacement(virCgroupPtr group) static int virCgroupV1DetectControllers(virCgroupPtr group, - int controllers) + int controllers, + virCgroupPtr parent ATTRIBUTE_UNUSED) { size_t i; size_t j; diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c index 9d8a38925a..9c44b3473e 100644 --- a/src/util/vircgroupv2.c +++ b/src/util/vircgroupv2.c @@ -242,7 +242,8 @@ virCgroupV2StealPlacement(virCgroupPtr group) static int -virCgroupV2ParseControllersFile(virCgroupPtr group) +virCgroupV2ParseControllersFile(virCgroupPtr group, + virCgroupPtr parent) { int rc; VIR_AUTOFREE(char *) contStr = NULL; @@ -250,10 +251,17 @@ virCgroupV2ParseControllersFile(virCgroupPtr group) char **contList = NULL; char **tmp; - if (virAsprintf(&contFile, "%s%s/cgroup.controllers", - group->unified.mountPoint, - NULLSTR_EMPTY(group->unified.placement)) < 0) - return -1; + if (parent) { + if (virAsprintf(&contFile, "%s%s/cgroup.subtree_control", + parent->unified.mountPoint, + NULLSTR_EMPTY(parent->unified.placement)) < 0) + return -1; + } else { + if (virAsprintf(&contFile, "%s%s/cgroup.controllers", + group->unified.mountPoint, + NULLSTR_EMPTY(group->unified.placement)) < 0) + return -1; + } rc = virFileReadAll(contFile, 1024 * 1024, &contStr); if (rc < 0) { @@ -286,11 +294,12 @@ virCgroupV2ParseControllersFile(virCgroupPtr group) static int virCgroupV2DetectControllers(virCgroupPtr group, - int controllers) + int controllers, + virCgroupPtr parent) { size_t i; - if (virCgroupV2ParseControllersFile(group) < 0) + if (virCgroupV2ParseControllersFile(group, parent) < 0) return -1; /* In cgroup v2 there is no cpuacct controller, the cpu.stat file always -- 2.21.0

In cgroups v2 when a new group is created by default no controller is enabled so the detection code will not detect any controllers. When enabling the controllers we should also store them for the group. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroupv2.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c index 9c44b3473e..af3192c99c 100644 --- a/src/util/vircgroupv2.c +++ b/src/util/vircgroupv2.c @@ -365,7 +365,8 @@ virCgroupV2PathOfController(virCgroupPtr group, * 0 on success */ static int -virCgroupV2EnableController(virCgroupPtr parent, +virCgroupV2EnableController(virCgroupPtr group, + virCgroupPtr parent, int controller, bool report) { @@ -391,6 +392,8 @@ virCgroupV2EnableController(virCgroupPtr parent, return -2; } + group->unified.controllers |= 1 << controller; + return 0; } @@ -432,14 +435,14 @@ virCgroupV2MakeGroup(virCgroupPtr parent ATTRIBUTE_UNUSED, } if (virCgroupV2HasController(parent, VIR_CGROUP_CONTROLLER_CPU) && - virCgroupV2EnableController(parent, + virCgroupV2EnableController(group, parent, VIR_CGROUP_CONTROLLER_CPU, true) < 0) { return -1; } if (virCgroupV2HasController(parent, VIR_CGROUP_CONTROLLER_CPUSET) && - virCgroupV2EnableController(parent, + virCgroupV2EnableController(group, parent, VIR_CGROUP_CONTROLLER_CPUSET, true) < 0) { return -1; @@ -456,7 +459,7 @@ virCgroupV2MakeGroup(virCgroupPtr parent ATTRIBUTE_UNUSED, if (i == VIR_CGROUP_CONTROLLER_CPUACCT) continue; - rc = virCgroupV2EnableController(parent, i, false); + rc = virCgroupV2EnableController(group, parent, i, false); if (rc < 0) { if (rc == -2) { virResetLastError(); -- 2.21.0

Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroupv2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c index af3192c99c..e36c36685b 100644 --- a/src/util/vircgroupv2.c +++ b/src/util/vircgroupv2.c @@ -399,7 +399,7 @@ virCgroupV2EnableController(virCgroupPtr group, static int -virCgroupV2MakeGroup(virCgroupPtr parent ATTRIBUTE_UNUSED, +virCgroupV2MakeGroup(virCgroupPtr parent, virCgroupPtr group, bool create, unsigned int flags) -- 2.21.0
participants (2)
-
Pavel Hrdina
-
Peter Krempa