• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
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 #include "tst_cgroup.h"
20 
21 static const struct tst_cgroup_group *cg_test;
22 static struct tst_cgroup_group *parent, *child;
23 static struct tst_cgroup_group *parent2, *child2;
24 
test_memcg_subtree_control(void)25 static void test_memcg_subtree_control(void)
26 {
27 	parent = tst_cgroup_group_mk(cg_test, "memcg_test_0");
28 	child = tst_cgroup_group_mk(parent, "memcg_test_1");
29 	parent2 = tst_cgroup_group_mk(cg_test, "memcg_test_2");
30 	child2 = tst_cgroup_group_mk(parent2, "memcg_test_3");
31 
32 	SAFE_CGROUP_PRINT(parent2, "cgroup.subtree_control", "-memory");
33 
34 	TST_EXP_POSITIVE(
35 		SAFE_CGROUP_OCCURSIN(child, "cgroup.controllers", "memory"),
36 		"child should have memory controller");
37 	TST_EXP_POSITIVE(
38 		!SAFE_CGROUP_OCCURSIN(child2, "cgroup.controllers", "memory"),
39 		"child2 should not have memory controller");
40 
41 	child2 = tst_cgroup_group_rm(child2);
42 	parent2 = tst_cgroup_group_rm(parent2);
43 	child = tst_cgroup_group_rm(child);
44 	parent = tst_cgroup_group_rm(parent);
45 }
46 
setup(void)47 static void setup(void)
48 {
49 	tst_cgroup_require("memory", NULL);
50 	cg_test = tst_cgroup_get_test_group();
51 
52 	if (TST_CGROUP_VER_IS_V1(cg_test, "memory"))
53 		tst_brk(TCONF, "V1 controllers do not have subtree control");
54 }
55 
cleanup(void)56 static void cleanup(void)
57 {
58 	if (child2)
59 		child2 = tst_cgroup_group_rm(child2);
60 	if (parent2)
61 		parent2 = tst_cgroup_group_rm(parent2);
62 	if (child)
63 		child = tst_cgroup_group_rm(child);
64 	if (parent)
65 		parent = tst_cgroup_group_rm(parent);
66 
67 	tst_cgroup_cleanup();
68 }
69 
70 static struct tst_test test = {
71 	.setup = setup,
72 	.cleanup = cleanup,
73 	.test_all = test_memcg_subtree_control,
74 };
75