Skip to content

Commit 38e1dd5

Browse files
committed
cgroup: selftests: Add API to find root of specific controller
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 2c754a8 commit 38e1dd5

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

tools/testing/selftests/cgroup/lib/cgroup_util.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ int cg_write_numeric(const char *cgroup, const char *control, long value)
217217
return cg_write(cgroup, control, buf);
218218
}
219219

220-
int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
220+
static int cg_find_root(char *root, size_t len, const char *controller,
221+
bool *nsdelegate)
221222
{
222223
char buf[10 * PAGE_SIZE];
223224
char *fs, *mount, *type, *options;
@@ -236,18 +237,37 @@ int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
236237
options = strtok(NULL, delim);
237238
strtok(NULL, delim);
238239
strtok(NULL, delim);
239-
240-
if (strcmp(type, "cgroup2") == 0) {
241-
strncpy(root, mount, len);
242-
if (nsdelegate)
243-
*nsdelegate = !!strstr(options, "nsdelegate");
244-
return 0;
240+
if (strcmp(type, "cgroup") == 0) {
241+
if (!controller || !strstr(options, controller))
242+
continue;
243+
} else if (strcmp(type, "cgroup2") == 0) {
244+
if (controller &&
245+
cg_read_strstr(mount, "cgroup.controllers", controller))
246+
continue;
247+
} else {
248+
continue;
245249
}
250+
strncpy(root, mount, len);
251+
252+
if (nsdelegate)
253+
*nsdelegate = !!strstr(options, "nsdelegate");
254+
return 0;
255+
246256
}
247257

248258
return -1;
249259
}
250260

261+
int cg_find_controller_root(char *root, size_t len, const char *controller)
262+
{
263+
return cg_find_root(root, len, controller, NULL);
264+
}
265+
266+
int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
267+
{
268+
return cg_find_root(root, len, NULL, nsdelegate);
269+
}
270+
251271
int cg_create(const char *cgroup)
252272
{
253273
return mkdir(cgroup, 0755);

tools/testing/selftests/cgroup/lib/include/cgroup_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static inline int values_close(long a, long b, int err)
2424
extern ssize_t read_text(const char *path, char *buf, size_t max_len);
2525
extern ssize_t write_text(const char *path, char *buf, ssize_t len);
2626

27+
extern int cg_find_controller_root(char *root, size_t len, const char *controller);
2728
extern int cg_find_unified_root(char *root, size_t len, bool *nsdelegate);
2829
extern char *cg_name(const char *root, const char *name);
2930
extern char *cg_name_indexed(const char *root, const char *name, int index);

0 commit comments

Comments
 (0)