1 /* SPDX-License-Identifier: GPL-2.0 */
3 #include <linux/limits.h>
9 #include "../kselftest.h"
10 #include "cgroup_util.h"
16 * A, B and C's "populated" fields would be 1 while D's 0.
17 * test that after the one process in C is moved to root,
18 * A,B and C's "populated" fields would flip to "0" and file
19 * modified events will be generated on the
20 * "cgroup.events" files of both cgroups.
22 static int test_cgcore_populated(const char *root
)
25 char *cg_test_a
= NULL
, *cg_test_b
= NULL
;
26 char *cg_test_c
= NULL
, *cg_test_d
= NULL
;
28 cg_test_a
= cg_name(root
, "cg_test_a");
29 cg_test_b
= cg_name(root
, "cg_test_a/cg_test_b");
30 cg_test_c
= cg_name(root
, "cg_test_a/cg_test_b/cg_test_c");
31 cg_test_d
= cg_name(root
, "cg_test_a/cg_test_b/cg_test_d");
33 if (!cg_test_a
|| !cg_test_b
|| !cg_test_c
|| !cg_test_d
)
36 if (cg_create(cg_test_a
))
39 if (cg_create(cg_test_b
))
42 if (cg_create(cg_test_c
))
45 if (cg_create(cg_test_d
))
48 if (cg_enter_current(cg_test_c
))
51 if (cg_read_strcmp(cg_test_a
, "cgroup.events", "populated 1\n"))
54 if (cg_read_strcmp(cg_test_b
, "cgroup.events", "populated 1\n"))
57 if (cg_read_strcmp(cg_test_c
, "cgroup.events", "populated 1\n"))
60 if (cg_read_strcmp(cg_test_d
, "cgroup.events", "populated 0\n"))
63 if (cg_enter_current(root
))
66 if (cg_read_strcmp(cg_test_a
, "cgroup.events", "populated 0\n"))
69 if (cg_read_strcmp(cg_test_b
, "cgroup.events", "populated 0\n"))
72 if (cg_read_strcmp(cg_test_c
, "cgroup.events", "populated 0\n"))
75 if (cg_read_strcmp(cg_test_d
, "cgroup.events", "populated 0\n"))
82 cg_destroy(cg_test_d
);
84 cg_destroy(cg_test_c
);
86 cg_destroy(cg_test_b
);
88 cg_destroy(cg_test_a
);
97 * A (domain threaded) - B (threaded) - C (domain)
99 * test that C can't be used until it is turned into a
100 * threaded cgroup. "cgroup.type" file will report "domain (invalid)" in
101 * these cases. Operations which fail due to invalid topology use
102 * EOPNOTSUPP as the errno.
104 static int test_cgcore_invalid_domain(const char *root
)
107 char *grandparent
= NULL
, *parent
= NULL
, *child
= NULL
;
109 grandparent
= cg_name(root
, "cg_test_grandparent");
110 parent
= cg_name(root
, "cg_test_grandparent/cg_test_parent");
111 child
= cg_name(root
, "cg_test_grandparent/cg_test_parent/cg_test_child");
112 if (!parent
|| !child
|| !grandparent
)
115 if (cg_create(grandparent
))
118 if (cg_create(parent
))
121 if (cg_create(child
))
124 if (cg_write(parent
, "cgroup.type", "threaded"))
127 if (cg_read_strcmp(child
, "cgroup.type", "domain invalid\n"))
130 if (!cg_enter_current(child
))
133 if (errno
!= EOPNOTSUPP
)
139 cg_enter_current(root
);
145 cg_destroy(grandparent
);
153 * Test that when a child becomes threaded
154 * the parent type becomes domain threaded.
156 static int test_cgcore_parent_becomes_threaded(const char *root
)
159 char *parent
= NULL
, *child
= NULL
;
161 parent
= cg_name(root
, "cg_test_parent");
162 child
= cg_name(root
, "cg_test_parent/cg_test_child");
163 if (!parent
|| !child
)
166 if (cg_create(parent
))
169 if (cg_create(child
))
172 if (cg_write(child
, "cgroup.type", "threaded"))
175 if (cg_read_strcmp(parent
, "cgroup.type", "domain threaded\n"))
192 * Test that there's no internal process constrain on threaded cgroups.
193 * You can add threads/processes on a parent with a controller enabled.
195 static int test_cgcore_no_internal_process_constraint_on_threads(const char *root
)
198 char *parent
= NULL
, *child
= NULL
;
200 if (cg_read_strstr(root
, "cgroup.controllers", "cpu") ||
201 cg_write(root
, "cgroup.subtree_control", "+cpu")) {
206 parent
= cg_name(root
, "cg_test_parent");
207 child
= cg_name(root
, "cg_test_parent/cg_test_child");
208 if (!parent
|| !child
)
211 if (cg_create(parent
))
214 if (cg_create(child
))
217 if (cg_write(parent
, "cgroup.type", "threaded"))
220 if (cg_write(child
, "cgroup.type", "threaded"))
223 if (cg_write(parent
, "cgroup.subtree_control", "+cpu"))
226 if (cg_enter_current(parent
))
232 cg_enter_current(root
);
233 cg_enter_current(root
);
244 * Test that you can't enable a controller on a child if it's not enabled
247 static int test_cgcore_top_down_constraint_enable(const char *root
)
250 char *parent
= NULL
, *child
= NULL
;
252 parent
= cg_name(root
, "cg_test_parent");
253 child
= cg_name(root
, "cg_test_parent/cg_test_child");
254 if (!parent
|| !child
)
257 if (cg_create(parent
))
260 if (cg_create(child
))
263 if (!cg_write(child
, "cgroup.subtree_control", "+memory"))
279 * Test that you can't disable a controller on a parent
280 * if it's enabled in a child.
282 static int test_cgcore_top_down_constraint_disable(const char *root
)
285 char *parent
= NULL
, *child
= NULL
;
287 parent
= cg_name(root
, "cg_test_parent");
288 child
= cg_name(root
, "cg_test_parent/cg_test_child");
289 if (!parent
|| !child
)
292 if (cg_create(parent
))
295 if (cg_create(child
))
298 if (cg_write(parent
, "cgroup.subtree_control", "+memory"))
301 if (cg_write(child
, "cgroup.subtree_control", "+memory"))
304 if (!cg_write(parent
, "cgroup.subtree_control", "-memory"))
320 * Test internal process constraint.
321 * You can't add a pid to a domain parent if a controller is enabled.
323 static int test_cgcore_internal_process_constraint(const char *root
)
326 char *parent
= NULL
, *child
= NULL
;
328 parent
= cg_name(root
, "cg_test_parent");
329 child
= cg_name(root
, "cg_test_parent/cg_test_child");
330 if (!parent
|| !child
)
333 if (cg_create(parent
))
336 if (cg_create(child
))
339 if (cg_write(parent
, "cgroup.subtree_control", "+memory"))
342 if (!cg_enter_current(parent
))
357 #define T(x) { x, #x }
359 int (*fn
)(const char *root
);
362 T(test_cgcore_internal_process_constraint
),
363 T(test_cgcore_top_down_constraint_enable
),
364 T(test_cgcore_top_down_constraint_disable
),
365 T(test_cgcore_no_internal_process_constraint_on_threads
),
366 T(test_cgcore_parent_becomes_threaded
),
367 T(test_cgcore_invalid_domain
),
368 T(test_cgcore_populated
),
372 int main(int argc
, char *argv
[])
375 int i
, ret
= EXIT_SUCCESS
;
377 if (cg_find_unified_root(root
, sizeof(root
)))
378 ksft_exit_skip("cgroup v2 isn't mounted\n");
380 if (cg_read_strstr(root
, "cgroup.subtree_control", "memory"))
381 if (cg_write(root
, "cgroup.subtree_control", "+memory"))
382 ksft_exit_skip("Failed to set memory controller\n");
384 for (i
= 0; i
< ARRAY_SIZE(tests
); i
++) {
385 switch (tests
[i
].fn(root
)) {
387 ksft_test_result_pass("%s\n", tests
[i
].name
);
390 ksft_test_result_skip("%s\n", tests
[i
].name
);
394 ksft_test_result_fail("%s\n", tests
[i
].name
);