1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) 2021 SUSE LLC */
3*49cdfc7eSAndroid Build Coastguard Worker
4*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
5*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker static char *only_mount_v1;
10*49cdfc7eSAndroid Build Coastguard Worker static char *no_cleanup;
11*49cdfc7eSAndroid Build Coastguard Worker static struct tst_option opts[] = {
12*49cdfc7eSAndroid Build Coastguard Worker {"v", &only_mount_v1, "-v\tOnly try to mount CGroups V1"},
13*49cdfc7eSAndroid Build Coastguard Worker {"n", &no_cleanup, "-n\tLeave CGroups created by test"},
14*49cdfc7eSAndroid Build Coastguard Worker {NULL, NULL, NULL},
15*49cdfc7eSAndroid Build Coastguard Worker };
16*49cdfc7eSAndroid Build Coastguard Worker static struct tst_cg_opts cgopts;
17*49cdfc7eSAndroid Build Coastguard Worker static struct tst_cg_group *cg_child;
18*49cdfc7eSAndroid Build Coastguard Worker
do_test(void)19*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
20*49cdfc7eSAndroid Build Coastguard Worker {
21*49cdfc7eSAndroid Build Coastguard Worker char buf[BUFSIZ];
22*49cdfc7eSAndroid Build Coastguard Worker size_t mem;
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker if (!TST_CG_VER_IS_V1(tst_cg, "memory"))
25*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINT(tst_cg, "cgroup.subtree_control", "+memory");
26*49cdfc7eSAndroid Build Coastguard Worker if (!TST_CG_VER_IS_V1(tst_cg, "cpuset"))
27*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINT(tst_cg, "cgroup.subtree_control", "+cpuset");
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker cg_child = tst_cg_group_mk(tst_cg, "child");
30*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_FORK()) {
31*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_SCANF(cg_child, "memory.current", "%zu", &mem);
34*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "child/memory.current = %zu", mem);
35*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(cg_child, "memory.max",
36*49cdfc7eSAndroid Build Coastguard Worker "%zu", (1UL << 24) - 1);
37*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(cg_child, "memory.swap.max",
38*49cdfc7eSAndroid Build Coastguard Worker "%zu", 1UL << 31);
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_READ(cg_child, "cpuset.mems", buf, sizeof(buf));
41*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "child/cpuset.mems = %s", buf);
42*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINT(cg_child, "cpuset.mems", buf);
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker exit(0);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg, "memory.max", "%zu", (1UL << 24) - 1);
48*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
49*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_SCANF(tst_cg, "memory.current", "%zu", &mem);
50*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "memory.current = %zu", mem);
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
53*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid());
54*49cdfc7eSAndroid Build Coastguard Worker cg_child = tst_cg_group_rm(cg_child);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
setup(void)57*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker cgopts.needs_ver = !!only_mount_v1 ? TST_CG_V1 : 0;
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker tst_cg_scan();
62*49cdfc7eSAndroid Build Coastguard Worker tst_cg_print_config();
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker tst_cg_require("memory", &cgopts);
65*49cdfc7eSAndroid Build Coastguard Worker tst_cg_require("cpuset", &cgopts);
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker tst_cg_init();
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)70*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker if (cg_child) {
73*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg_drain,
74*49cdfc7eSAndroid Build Coastguard Worker "cgroup.procs", "%d", getpid());
75*49cdfc7eSAndroid Build Coastguard Worker cg_child = tst_cg_group_rm(cg_child);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker if (!no_cleanup)
78*49cdfc7eSAndroid Build Coastguard Worker tst_cg_cleanup();
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
82*49cdfc7eSAndroid Build Coastguard Worker .test_all = do_test,
83*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
84*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
85*49cdfc7eSAndroid Build Coastguard Worker .options = opts,
86*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
87*49cdfc7eSAndroid Build Coastguard Worker };
88