cgroup: selftests: Add API to find root of specific controller
authorSean Christopherson <seanjc@google.com>
Thu, 8 May 2025 18:46:46 +0000 (18:46 +0000)
committerSean Christopherson <seanjc@google.com>
Fri, 16 May 2025 18:45:15 +0000 (11:45 -0700)
Add an API in the cgroups library to find the root of a specific
controller.  KVM selftests will use the API to find the memory controller.

Search for the controller on both v1 and v2 mounts, as KVM selftests'
usage will be completely oblivious of v1 versus v2.

Signed-off-by: James Houghton <jthoughton@google.com>
Link: https://lore.kernel.org/r/20250508184649.2576210-6-jthoughton@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/cgroup/lib/cgroup_util.c
tools/testing/selftests/cgroup/lib/include/cgroup_util.h

index 4b975637351b2c4529276fdb879d3dc45832c665..8832f3d1cb6142051dc7655c0796d61bf319124f 100644 (file)
@@ -217,7 +217,8 @@ int cg_write_numeric(const char *cgroup, const char *control, long value)
        return cg_write(cgroup, control, buf);
 }
 
-int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
+static int cg_find_root(char *root, size_t len, const char *controller,
+                       bool *nsdelegate)
 {
        char buf[10 * PAGE_SIZE];
        char *fs, *mount, *type, *options;
@@ -236,18 +237,37 @@ int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
                options = strtok(NULL, delim);
                strtok(NULL, delim);
                strtok(NULL, delim);
-
-               if (strcmp(type, "cgroup2") == 0) {
-                       strncpy(root, mount, len);
-                       if (nsdelegate)
-                               *nsdelegate = !!strstr(options, "nsdelegate");
-                       return 0;
+               if (strcmp(type, "cgroup") == 0) {
+                       if (!controller || !strstr(options, controller))
+                               continue;
+               } else if (strcmp(type, "cgroup2") == 0) {
+                       if (controller &&
+                                       cg_read_strstr(mount, "cgroup.controllers", controller))
+                               continue;
+               } else {
+                       continue;
                }
+               strncpy(root, mount, len);
+
+               if (nsdelegate)
+                       *nsdelegate = !!strstr(options, "nsdelegate");
+               return 0;
+
        }
 
        return -1;
 }
 
+int cg_find_controller_root(char *root, size_t len, const char *controller)
+{
+       return cg_find_root(root, len, controller, NULL);
+}
+
+int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
+{
+       return cg_find_root(root, len, NULL, nsdelegate);
+}
+
 int cg_create(const char *cgroup)
 {
        return mkdir(cgroup, 0755);
index b7006dc761abaa17ae514eb8ba5fb0ba69112263..adb2bc19318393b242e5944f25c3ba3c5884de6d 100644 (file)
@@ -24,6 +24,7 @@ static inline int values_close(long a, long b, int err)
 extern ssize_t read_text(const char *path, char *buf, size_t max_len);
 extern ssize_t write_text(const char *path, char *buf, ssize_t len);
 
+extern int cg_find_controller_root(char *root, size_t len, const char *controller);
 extern int cg_find_unified_root(char *root, size_t len, bool *nsdelegate);
 extern char *cg_name(const char *root, const char *name);
 extern char *cg_name_indexed(const char *root, const char *name, int index);