From: James Houghton <jthoughton@google.com>
To: Sean Christopherson <seanjc@google.com>, kvm@vger.kernel.org
Cc: Maxim Levitsky <mlevitsk@redhat.com>,
Axel Rasmussen <axelrasmussen@google.com>,
Tejun Heo <tj@kernel.org>, Johannes Weiner <hannes@cmpxchg.org>,
mkoutny@suse.com, Yosry Ahmed <yosry.ahmed@linux.dev>,
Yu Zhao <yuzhao@google.com>, David Matlack <dmatlack@google.com>,
James Houghton <jthoughton@google.com>,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v4 5/7] cgroup: selftests: Add API to find root of specific controller
Date: Thu, 8 May 2025 18:46:46 +0000 [thread overview]
Message-ID: <20250508184649.2576210-6-jthoughton@google.com> (raw)
In-Reply-To: <20250508184649.2576210-1-jthoughton@google.com>
From: Sean Christopherson <seanjc@google.com>
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: Sean Christopherson <seanjc@google.com>
Signed-off-by: James Houghton <jthoughton@google.com>
---
.../selftests/cgroup/lib/cgroup_util.c | 34 +++++++++++++++----
.../cgroup/lib/include/cgroup_util.h | 1 +
2 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/tools/testing/selftests/cgroup/lib/cgroup_util.c b/tools/testing/selftests/cgroup/lib/cgroup_util.c
index 4b975637351b2..8832f3d1cb614 100644
--- a/tools/testing/selftests/cgroup/lib/cgroup_util.c
+++ b/tools/testing/selftests/cgroup/lib/cgroup_util.c
@@ -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);
diff --git a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
index b7006dc761aba..adb2bc1931839 100644
--- a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
+++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
@@ -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);
--
2.49.0.1015.ga840276032-goog
next prev parent reply other threads:[~2025-05-08 18:46 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-08 18:46 [PATCH v4 0/7] KVM: selftests: access_tracking_perf_test fixes for NUMA balancing and MGLRU James Houghton
2025-05-08 18:46 ` [PATCH v4 1/7] KVM: selftests: Extract guts of THP accessor to standalone sysfs helpers James Houghton
2025-05-08 18:46 ` [PATCH v4 2/7] KVM: selftests: access_tracking_perf_test: Add option to skip the sanity check James Houghton
2025-05-08 18:46 ` [PATCH v4 3/7] cgroup: selftests: Move memcontrol specific helpers out of common cgroup_util.c James Houghton
2025-05-08 18:46 ` [PATCH v4 4/7] cgroup: selftests: Move cgroup_util into its own library James Houghton
2025-05-08 18:46 ` James Houghton [this message]
2025-05-08 18:46 ` [PATCH v4 6/7] KVM: selftests: Build and link selftests/cgroup/lib into KVM selftests James Houghton
2025-05-08 18:46 ` [PATCH v4 7/7] KVM: selftests: access_tracking_perf_test: Use MGLRU for access tracking James Houghton
2025-05-16 18:47 ` Sean Christopherson
2025-05-20 16:48 ` [PATCH v4 0/7] KVM: selftests: access_tracking_perf_test fixes for NUMA balancing and MGLRU Sean Christopherson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250508184649.2576210-6-jthoughton@google.com \
--to=jthoughton@google.com \
--cc=axelrasmussen@google.com \
--cc=cgroups@vger.kernel.org \
--cc=dmatlack@google.com \
--cc=hannes@cmpxchg.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mkoutny@suse.com \
--cc=mlevitsk@redhat.com \
--cc=seanjc@google.com \
--cc=tj@kernel.org \
--cc=yosry.ahmed@linux.dev \
--cc=yuzhao@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox