1 // SPDX-License-Identifier: GPL-2.0-only
2 /*\
3 *
4 * [Description]
5 *
6 * Conversion of the first kself test in cgroup/test_memcontrol.c.
7 * This test creates two nested cgroups with and without enabling the
8 * memory controller.
9 *
10 * The LTP API automatically adds controllers to subtree_control when
11 * a child cgroup is added. So unlike the kselftest we remove the
12 * controller after it being added automatically.
13 */
14 #define _GNU_SOURCE
15
16 #include <stdio.h>
17
18 #include "tst_test.h"
19
20 static struct tst_cg_group *parent, *child;
21 static struct tst_cg_group *parent2, *child2;
22
test_memcg_subtree_control(void)23 static void test_memcg_subtree_control(void)
24 {
25 parent = tst_cg_group_mk(tst_cg, "memcg_test_0");
26 child = tst_cg_group_mk(parent, "memcg_test_1");
27 parent2 = tst_cg_group_mk(tst_cg, "memcg_test_2");
28 child2 = tst_cg_group_mk(parent2, "memcg_test_3");
29
30 SAFE_CG_PRINT(parent2, "cgroup.subtree_control", "-memory");
31
32 TST_EXP_POSITIVE(
33 SAFE_CG_OCCURSIN(child, "cgroup.controllers", "memory"),
34 "child should have memory controller");
35 TST_EXP_POSITIVE(
36 !SAFE_CG_OCCURSIN(child2, "cgroup.controllers", "memory"),
37 "child2 should not have memory controller");
38
39 child2 = tst_cg_group_rm(child2);
40 parent2 = tst_cg_group_rm(parent2);
41 child = tst_cg_group_rm(child);
42 parent = tst_cg_group_rm(parent);
43 }
44
cleanup(void)45 static void cleanup(void)
46 {
47 if (child2)
48 child2 = tst_cg_group_rm(child2);
49 if (parent2)
50 parent2 = tst_cg_group_rm(parent2);
51 if (child)
52 child = tst_cg_group_rm(child);
53 if (parent)
54 parent = tst_cg_group_rm(parent);
55 }
56
57 static struct tst_test test = {
58 .cleanup = cleanup,
59 .test_all = test_memcg_subtree_control,
60 .needs_cgroup_ver = TST_CG_V2,
61 .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
62 };
63