c480ed
From c5ff4dbe89d9a57da51bb92389ebd9e5af4dd8dc Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <c5ff4dbe89d9a57da51bb92389ebd9e5af4dd8dc@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:06:31 +0200
c480ed
Subject: [PATCH] vircgroup: extract virCgroupV1DetectControllers
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit d7f77dd6d5bcf91c3422a15842c2b7c2714510af)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <a066563f5d5183b6e09ae73cbfb3defb478bb693.1561993100.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroup.c        | 66 +------------------------------------
c480ed
 src/util/vircgroupbackend.h |  5 +++
c480ed
 src/util/vircgroupv1.c      | 65 ++++++++++++++++++++++++++++++++++++
c480ed
 3 files changed, 71 insertions(+), 65 deletions(-)
c480ed
c480ed
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c480ed
index 0e64ad67f4..2e74cbbff4 100644
c480ed
--- a/src/util/vircgroup.c
c480ed
+++ b/src/util/vircgroup.c
c480ed
@@ -349,70 +349,6 @@ virCgroupDetectPlacement(virCgroupPtr group,
c480ed
 }
c480ed
 
c480ed
 
c480ed
-static int
c480ed
-virCgroupDetectControllers(virCgroupPtr group,
c480ed
-                           int controllers)
c480ed
-{
c480ed
-    size_t i;
c480ed
-    size_t j;
c480ed
-
c480ed
-    if (controllers >= 0) {
c480ed
-        VIR_DEBUG("Filtering controllers %d", controllers);
c480ed
-        /* First mark requested but non-existing controllers to be ignored */
c480ed
-        for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c480ed
-            if (((1 << i) & controllers)) {
c480ed
-                /* Remove non-existent controllers  */
c480ed
-                if (!group->controllers[i].mountPoint) {
c480ed
-                    VIR_DEBUG("Requested controller '%s' not mounted, ignoring",
c480ed
-                              virCgroupControllerTypeToString(i));
c480ed
-                    controllers &= ~(1 << i);
c480ed
-                }
c480ed
-            }
c480ed
-        }
c480ed
-        for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c480ed
-            VIR_DEBUG("Controller '%s' wanted=%s, mount='%s'",
c480ed
-                      virCgroupControllerTypeToString(i),
c480ed
-                      (1 << i) & controllers ? "yes" : "no",
c480ed
-                      NULLSTR(group->controllers[i].mountPoint));
c480ed
-            if (!((1 << i) & controllers) &&
c480ed
-                group->controllers[i].mountPoint) {
c480ed
-                /* Check whether a request to disable a controller
c480ed
-                 * clashes with co-mounting of controllers */
c480ed
-                for (j = 0; j < VIR_CGROUP_CONTROLLER_LAST; j++) {
c480ed
-                    if (j == i)
c480ed
-                        continue;
c480ed
-                    if (!((1 << j) & controllers))
c480ed
-                        continue;
c480ed
-
c480ed
-                    if (STREQ_NULLABLE(group->controllers[i].mountPoint,
c480ed
-                                       group->controllers[j].mountPoint)) {
c480ed
-                        virReportSystemError(EINVAL,
c480ed
-                                             _("Controller '%s' is not wanted, but '%s' is co-mounted"),
c480ed
-                                             virCgroupControllerTypeToString(i),
c480ed
-                                             virCgroupControllerTypeToString(j));
c480ed
-                        return -1;
c480ed
-                    }
c480ed
-                }
c480ed
-                VIR_FREE(group->controllers[i].mountPoint);
c480ed
-            }
c480ed
-        }
c480ed
-    } else {
c480ed
-        VIR_DEBUG("Auto-detecting controllers");
c480ed
-        controllers = 0;
c480ed
-        for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c480ed
-            VIR_DEBUG("Controller '%s' present=%s",
c480ed
-                      virCgroupControllerTypeToString(i),
c480ed
-                      group->controllers[i].mountPoint ? "yes" : "no");
c480ed
-            if (group->controllers[i].mountPoint == NULL)
c480ed
-                continue;
c480ed
-            controllers |= (1 << i);
c480ed
-        }
c480ed
-    }
c480ed
-
c480ed
-    return controllers;
c480ed
-}
c480ed
-
c480ed
-
c480ed
 static int
c480ed
 virCgroupDetect(virCgroupPtr group,
c480ed
                 pid_t pid,
c480ed
@@ -451,7 +387,7 @@ virCgroupDetect(virCgroupPtr group,
c480ed
             return -1;
c480ed
     }
c480ed
 
c480ed
-    rc = virCgroupDetectControllers(group, controllers);
c480ed
+    rc = group->backend->detectControllers(group, controllers);
c480ed
     if (rc < 0)
c480ed
         return -1;
c480ed
 
c480ed
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
c480ed
index 9c0bd89793..011d1b00da 100644
c480ed
--- a/src/util/vircgroupbackend.h
c480ed
+++ b/src/util/vircgroupbackend.h
c480ed
@@ -69,6 +69,10 @@ typedef int
c480ed
 typedef char *
c480ed
 (*virCgroupStealPlacementCB)(virCgroupPtr group);
c480ed
 
c480ed
+typedef int
c480ed
+(*virCgroupDetectControllersCB)(virCgroupPtr group,
c480ed
+                                int controllers);
c480ed
+
c480ed
 struct _virCgroupBackend {
c480ed
     virCgroupBackendType type;
c480ed
 
c480ed
@@ -81,6 +85,7 @@ struct _virCgroupBackend {
c480ed
     virCgroupDetectPlacementCB detectPlacement;
c480ed
     virCgroupValidatePlacementCB validatePlacement;
c480ed
     virCgroupStealPlacementCB stealPlacement;
c480ed
+    virCgroupDetectControllersCB detectControllers;
c480ed
 };
c480ed
 typedef struct _virCgroupBackend virCgroupBackend;
c480ed
 typedef virCgroupBackend *virCgroupBackendPtr;
c480ed
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
c480ed
index 446546fd58..11a86c061a 100644
c480ed
--- a/src/util/vircgroupv1.c
c480ed
+++ b/src/util/vircgroupv1.c
c480ed
@@ -414,6 +414,70 @@ virCgroupV1StealPlacement(virCgroupPtr group)
c480ed
 }
c480ed
 
c480ed
 
c480ed
+static int
c480ed
+virCgroupV1DetectControllers(virCgroupPtr group,
c480ed
+                             int controllers)
c480ed
+{
c480ed
+    size_t i;
c480ed
+    size_t j;
c480ed
+
c480ed
+    if (controllers >= 0) {
c480ed
+        VIR_DEBUG("Filtering controllers %d", controllers);
c480ed
+        /* First mark requested but non-existing controllers to be ignored */
c480ed
+        for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c480ed
+            if (((1 << i) & controllers)) {
c480ed
+                /* Remove non-existent controllers  */
c480ed
+                if (!group->controllers[i].mountPoint) {
c480ed
+                    VIR_DEBUG("Requested controller '%s' not mounted, ignoring",
c480ed
+                              virCgroupV1ControllerTypeToString(i));
c480ed
+                    controllers &= ~(1 << i);
c480ed
+                }
c480ed
+            }
c480ed
+        }
c480ed
+        for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c480ed
+            VIR_DEBUG("Controller '%s' wanted=%s, mount='%s'",
c480ed
+                      virCgroupV1ControllerTypeToString(i),
c480ed
+                      (1 << i) & controllers ? "yes" : "no",
c480ed
+                      NULLSTR(group->controllers[i].mountPoint));
c480ed
+            if (!((1 << i) & controllers) &&
c480ed
+                group->controllers[i].mountPoint) {
c480ed
+                /* Check whether a request to disable a controller
c480ed
+                 * clashes with co-mounting of controllers */
c480ed
+                for (j = 0; j < VIR_CGROUP_CONTROLLER_LAST; j++) {
c480ed
+                    if (j == i)
c480ed
+                        continue;
c480ed
+                    if (!((1 << j) & controllers))
c480ed
+                        continue;
c480ed
+
c480ed
+                    if (STREQ_NULLABLE(group->controllers[i].mountPoint,
c480ed
+                                       group->controllers[j].mountPoint)) {
c480ed
+                        virReportSystemError(EINVAL,
c480ed
+                                             _("V1 controller '%s' is not wanted, but '%s' is co-mounted"),
c480ed
+                                             virCgroupV1ControllerTypeToString(i),
c480ed
+                                             virCgroupV1ControllerTypeToString(j));
c480ed
+                        return -1;
c480ed
+                    }
c480ed
+                }
c480ed
+                VIR_FREE(group->controllers[i].mountPoint);
c480ed
+            }
c480ed
+        }
c480ed
+    } else {
c480ed
+        VIR_DEBUG("Auto-detecting controllers");
c480ed
+        controllers = 0;
c480ed
+        for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c480ed
+            VIR_DEBUG("Controller '%s' present=%s",
c480ed
+                      virCgroupV1ControllerTypeToString(i),
c480ed
+                      group->controllers[i].mountPoint ? "yes" : "no");
c480ed
+            if (group->controllers[i].mountPoint == NULL)
c480ed
+                continue;
c480ed
+            controllers |= (1 << i);
c480ed
+        }
c480ed
+    }
c480ed
+
c480ed
+    return controllers;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 virCgroupBackend virCgroupV1Backend = {
c480ed
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
c480ed
 
c480ed
@@ -425,6 +489,7 @@ virCgroupBackend virCgroupV1Backend = {
c480ed
     .detectPlacement = virCgroupV1DetectPlacement,
c480ed
     .validatePlacement = virCgroupV1ValidatePlacement,
c480ed
     .stealPlacement = virCgroupV1StealPlacement,
c480ed
+    .detectControllers = virCgroupV1DetectControllers,
c480ed
 };
c480ed
 
c480ed
 
c480ed
-- 
c480ed
2.22.0
c480ed