[libvirt] [PATCH 0/7] cgroup cleanup

Another small set of cgroup cleanup patches as preparation for cgroup v2. Pavel Hrdina (7): vircgroup: Duplicate string before modifying vircgroup: Extract controller detection into function vircgroup: Extract placement validation into function vircgroup: Split virCgroupPathOfController into two functions vircgroup: Call virCgroupRemove inside virCgroupMakeGroup vircgroup: Simplify if conditions in virCgroupMakeGroup vircgroup: Remove obsolete sa_assert src/util/vircgroup.c | 200 +++++++++++++++++++++++++------------------ src/util/vircgroup.h | 2 +- 2 files changed, 118 insertions(+), 84 deletions(-) -- 2.17.1

The 'mntDir' is part of 'struct mntent' as a result of getmntent_r therefor we should not mangle with it. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index df38bb77e0..45b854e864 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -350,18 +350,22 @@ virCgroupCopyMounts(virCgroupPtr group, static int -virCgroupResolveMountLink(char *mntDir, +virCgroupResolveMountLink(const char *mntDir, const char *typeStr, virCgroupControllerPtr controller) { VIR_AUTOFREE(char *) linkSrc = NULL; + VIR_AUTOFREE(char *) tmp = NULL; char *dirName; struct stat sb; - dirName = strrchr(mntDir, '/'); + if (VIR_STRDUP(tmp, mntDir) < 0) + return -1; + + dirName = strrchr(tmp, '/'); if (!dirName) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing '/' separator in cgroup mount '%s'"), mntDir); + _("Missing '/' separator in cgroup mount '%s'"), tmp); return -1; } @@ -369,14 +373,14 @@ virCgroupResolveMountLink(char *mntDir, return 0; *dirName = '\0'; - if (virAsprintf(&linkSrc, "%s/%s", mntDir, typeStr) < 0) + if (virAsprintf(&linkSrc, "%s/%s", tmp, typeStr) < 0) return -1; *dirName = '/'; if (lstat(linkSrc, &sb) < 0) { if (errno == ENOENT) { VIR_WARN("Controller %s co-mounted at %s is missing symlink at %s", - typeStr, mntDir, linkSrc); + typeStr, tmp, linkSrc); } else { virReportSystemError(errno, _("Cannot stat %s"), linkSrc); return -1; -- 2.17.1

On Fri, Aug 24, 2018 at 03:45:51PM +0200, Pavel Hrdina wrote:
The 'mntDir' is part of 'struct mntent' as a result of getmntent_r therefor we should not mangle with it.
therefore
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 48 +++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 45b854e864..6f27c50cbd 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -631,24 +631,11 @@ virCgroupDetectPlacement(virCgroupPtr group, static int -virCgroupDetect(virCgroupPtr group, - pid_t pid, - int controllers, - const char *path, - virCgroupPtr parent) +virCgroupDetectControllers(virCgroupPtr group, + int controllers) { size_t i; size_t j; - VIR_DEBUG("group=%p controllers=%d path=%s parent=%p", - group, controllers, path, parent); - - if (parent) { - if (virCgroupCopyMounts(group, parent) < 0) - return -1; - } else { - if (virCgroupDetectMounts(group) < 0) - return -1; - } if (controllers >= 0) { VIR_DEBUG("Filtering controllers %d", controllers); @@ -703,8 +690,37 @@ virCgroupDetect(virCgroupPtr group, } } + return controllers; +} + + +static int +virCgroupDetect(virCgroupPtr group, + pid_t pid, + int controllers, + const char *path, + virCgroupPtr parent) +{ + size_t i; + int rc; + + VIR_DEBUG("group=%p controllers=%d path=%s parent=%p", + group, controllers, path, parent); + + if (parent) { + if (virCgroupCopyMounts(group, parent) < 0) + return -1; + } else { + if (virCgroupDetectMounts(group) < 0) + return -1; + } + + rc = virCgroupDetectControllers(group, controllers); + if (rc < 0) + return -1; + /* Check that at least 1 controller is available */ - if (!controllers) { + if (rc == 0) { virReportSystemError(ENXIO, "%s", _("At least one cgroup controller is required")); return -1; -- 2.17.1

On Fri, Aug 24, 2018 at 03:45:52PM +0200, Pavel Hrdina wrote:
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 48 +++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 52 +++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 6f27c50cbd..2bc4febf23 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -630,6 +630,36 @@ virCgroupDetectPlacement(virCgroupPtr group, } +static int +virCgroupValidatePlacement(virCgroupPtr group, + pid_t pid) +{ + size_t i; + + for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { + if (!group->controllers[i].mountPoint) + continue; + + if (!group->controllers[i].placement) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not find placement for controller %s at %s"), + virCgroupControllerTypeToString(i), + group->controllers[i].placement); + return -1; + } + + VIR_DEBUG("Detected mount/mapping %zu:%s at %s in %s for pid %lld", + i, + virCgroupControllerTypeToString(i), + group->controllers[i].mountPoint, + group->controllers[i].placement, + (long long) pid); + } + + return 0; +} + + static int virCgroupDetectControllers(virCgroupPtr group, int controllers) @@ -701,7 +731,6 @@ virCgroupDetect(virCgroupPtr group, const char *path, virCgroupPtr parent) { - size_t i; int rc; VIR_DEBUG("group=%p controllers=%d path=%s parent=%p", @@ -738,25 +767,8 @@ virCgroupDetect(virCgroupPtr group, return -1; /* Check that for every mounted controller, we found our placement */ - for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { - if (!group->controllers[i].mountPoint) - continue; - - if (!group->controllers[i].placement) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Could not find placement for controller %s at %s"), - virCgroupControllerTypeToString(i), - group->controllers[i].placement); - return -1; - } - - VIR_DEBUG("Detected mount/mapping %zu:%s at %s in %s for pid %lld", - i, - virCgroupControllerTypeToString(i), - group->controllers[i].mountPoint, - group->controllers[i].placement, - (long long) pid); - } + if (virCgroupValidatePlacement(group, pid) < 0) + return -1; return 0; } -- 2.17.1

On Fri, Aug 24, 2018 at 03:45:53PM +0200, Pavel Hrdina wrote:
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 52 +++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

The case where we need path of any controller is only for internal use so move it out to a different function. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 54 ++++++++++++++++++++++++++------------------ src/util/vircgroup.h | 2 +- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 2bc4febf23..8646a4a479 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -1772,28 +1772,13 @@ virCgroupHasController(virCgroupPtr cgroup, int controller) int virCgroupPathOfController(virCgroupPtr group, - int controller, + unsigned int controller, const char *key, char **path) { - if (controller == -1) { - size_t i; - for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { - /* Reject any controller with a placement - * of '/' to avoid doing bad stuff to the root - * cgroup - */ - if (group->controllers[i].mountPoint && - group->controllers[i].placement && - STRNEQ(group->controllers[i].placement, "/")) { - controller = i; - break; - } - } - } - if (controller == -1) { - virReportSystemError(ENOSYS, "%s", - _("No controllers are mounted")); + if (controller >= VIR_CGROUP_CONTROLLER_LAST) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid controller id '%d'"), controller); return -1; } @@ -3505,6 +3490,31 @@ virCgroupRemove(virCgroupPtr group) } +static int +virCgroupPathOfAnyController(virCgroupPtr group, + const char *name, + char **keypath) +{ + size_t i; + + for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { + /* Reject any controller with a placement + * of '/' to avoid doing bad stuff to the root + * cgroup + */ + if (group->controllers[i].mountPoint && + group->controllers[i].placement && + STRNEQ(group->controllers[i].placement, "/")) { + return virCgroupPathOfController(group, i, name, keypath); + } + } + + virReportSystemError(ENOSYS, "%s", + _("No controllers are mounted")); + return -1; +} + + /* * Returns 1 if some PIDs are killed, 0 if none are killed, or -1 on error */ @@ -3519,7 +3529,7 @@ virCgroupKillInternal(virCgroupPtr group, int signum, virHashTablePtr pids) VIR_DEBUG("group=%p path=%s signum=%d pids=%p", group, group->path, signum, pids); - if (virCgroupPathOfController(group, -1, "tasks", &keypath) < 0) + if (virCgroupPathOfAnyController(group, "tasks", &keypath) < 0) return -1; /* PIDs may be forking as we kill them, so loop @@ -3622,7 +3632,7 @@ virCgroupKillRecursiveInternal(virCgroupPtr group, VIR_DEBUG("group=%p path=%s signum=%d pids=%p", group, group->path, signum, pids); - if (virCgroupPathOfController(group, -1, "", &keypath) < 0) + if (virCgroupPathOfAnyController(group, "", &keypath) < 0) return -1; if ((rc = virCgroupKillInternal(group, signum, pids)) < 0) @@ -4180,7 +4190,7 @@ virCgroupHasController(virCgroupPtr cgroup ATTRIBUTE_UNUSED, int virCgroupPathOfController(virCgroupPtr group ATTRIBUTE_UNUSED, - int controller ATTRIBUTE_UNUSED, + unsigned int controller ATTRIBUTE_UNUSED, const char *key ATTRIBUTE_UNUSED, char **path ATTRIBUTE_UNUSED) { diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h index c7fdaaede4..ee3b7c7222 100644 --- a/src/util/vircgroup.h +++ b/src/util/vircgroup.h @@ -114,7 +114,7 @@ void virCgroupFree(virCgroupPtr *group); bool virCgroupHasController(virCgroupPtr cgroup, int controller); int virCgroupPathOfController(virCgroupPtr group, - int controller, + unsigned int controller, const char *key, char **path); -- 2.17.1

On Fri, Aug 24, 2018 at 03:45:54PM +0200, Pavel Hrdina wrote:
The case where we need path of any controller is only for internal use so move it out to a different function.
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 54 ++++++++++++++++++++++++++------------------ src/util/vircgroup.h | 2 +- 2 files changed, 33 insertions(+), 23 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

This fixes virCgroupEnableMissingControllers where virCgroupRemove was not called in case virCgroupMakeGroup failed. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 8646a4a479..dde9ed21a2 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -1052,7 +1052,7 @@ virCgroupMakeGroup(virCgroupPtr parent, } if (virCgroupPathOfController(group, i, "", &path) < 0) - return -1; + goto error; /* As of Feb 2011, clang can't see that the above function * call did not modify group. */ @@ -1076,7 +1076,7 @@ virCgroupMakeGroup(virCgroupPtr parent, virReportSystemError(errno, _("Failed to create controller %s for group"), virCgroupControllerTypeToString(i)); - return -1; + goto error; } } if (group->controllers[VIR_CGROUP_CONTROLLER_CPUSET].mountPoint != NULL && @@ -1084,7 +1084,7 @@ virCgroupMakeGroup(virCgroupPtr parent, STREQ(group->controllers[i].mountPoint, group->controllers[VIR_CGROUP_CONTROLLER_CPUSET].mountPoint))) { if (virCgroupCpuSetInherit(parent, group) < 0) - return -1; + goto error; } /* * Note that virCgroupSetMemoryUseHierarchy should always be @@ -1096,13 +1096,17 @@ virCgroupMakeGroup(virCgroupPtr parent, STREQ(group->controllers[i].mountPoint, group->controllers[VIR_CGROUP_CONTROLLER_MEMORY].mountPoint))) { if (virCgroupSetMemoryUseHierarchy(group) < 0) - return -1; + goto error; } } } VIR_DEBUG("Done making controllers for group"); return 0; + + error: + virCgroupRemove(group); + return -1; } @@ -1316,10 +1320,8 @@ virCgroupNewPartition(const char *path, if (virCgroupNew(-1, parentPath, NULL, controllers, &parent) < 0) goto cleanup; - if (virCgroupMakeGroup(parent, *group, create, VIR_CGROUP_NONE) < 0) { - virCgroupRemove(*group); + if (virCgroupMakeGroup(parent, *group, create, VIR_CGROUP_NONE) < 0) goto cleanup; - } } ret = 0; @@ -1389,7 +1391,6 @@ virCgroupNewDomainPartition(virCgroupPtr partition, */ if (virCgroupMakeGroup(partition, *group, create, VIR_CGROUP_MEM_HIERACHY) < 0) { - virCgroupRemove(*group); virCgroupFree(group); return -1; } @@ -1446,7 +1447,6 @@ virCgroupNewThread(virCgroupPtr domain, return -1; if (virCgroupMakeGroup(domain, *group, create, VIR_CGROUP_NONE) < 0) { - virCgroupRemove(*group); virCgroupFree(group); return -1; } -- 2.17.1

On Fri, Aug 24, 2018 at 03:45:55PM +0200, Pavel Hrdina wrote:
This fixes virCgroupEnableMissingControllers where virCgroupRemove was not called in case virCgroupMakeGroup failed.
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index dde9ed21a2..02de33f74f 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -1079,24 +1079,20 @@ virCgroupMakeGroup(virCgroupPtr parent, goto error; } } - if (group->controllers[VIR_CGROUP_CONTROLLER_CPUSET].mountPoint != NULL && - (i == VIR_CGROUP_CONTROLLER_CPUSET || - STREQ(group->controllers[i].mountPoint, - group->controllers[VIR_CGROUP_CONTROLLER_CPUSET].mountPoint))) { - if (virCgroupCpuSetInherit(parent, group) < 0) - goto error; + if (i == VIR_CGROUP_CONTROLLER_CPUSET && + group->controllers[i].mountPoint != NULL && + virCgroupCpuSetInherit(parent, group) < 0) { + goto error; } /* * Note that virCgroupSetMemoryUseHierarchy should always be * called prior to creating subcgroups and attaching tasks. */ if ((flags & VIR_CGROUP_MEM_HIERACHY) && - (group->controllers[VIR_CGROUP_CONTROLLER_MEMORY].mountPoint != NULL) && - (i == VIR_CGROUP_CONTROLLER_MEMORY || - STREQ(group->controllers[i].mountPoint, - group->controllers[VIR_CGROUP_CONTROLLER_MEMORY].mountPoint))) { - if (virCgroupSetMemoryUseHierarchy(group) < 0) - goto error; + i == VIR_CGROUP_CONTROLLER_MEMORY && + group->controllers[i].mountPoint != NULL && + virCgroupSetMemoryUseHierarchy(group) < 0) { + goto error; } } } -- 2.17.1

On Fri, Aug 24, 2018 at 03:45:56PM +0200, Pavel Hrdina wrote:
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 02de33f74f..64507bf8aa 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -1054,10 +1054,6 @@ virCgroupMakeGroup(virCgroupPtr parent, if (virCgroupPathOfController(group, i, "", &path) < 0) goto error; - /* As of Feb 2011, clang can't see that the above function - * call did not modify group. */ - sa_assert(group->controllers[i].mountPoint); - VIR_DEBUG("Make controller %s", path); if (!virFileExists(path)) { if (!create || -- 2.17.1

On Fri, Aug 24, 2018 at 03:45:57PM +0200, Pavel Hrdina wrote:
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 4 ---- 1 file changed, 4 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (2)
-
Ján Tomko
-
Pavel Hrdina