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