Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / cgroup / test_cpuset.c
blob4034d14ba69ac03b119d9718beca6e66332d90cf
1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/limits.h>
4 #include <signal.h>
6 #include "../kselftest.h"
7 #include "cgroup_util.h"
9 static int idle_process_fn(const char *cgroup, void *arg)
11 (void)pause();
12 return 0;
15 static int do_migration_fn(const char *cgroup, void *arg)
17 int object_pid = (int)(size_t)arg;
19 if (setuid(TEST_UID))
20 return EXIT_FAILURE;
22 // XXX checking /proc/$pid/cgroup would be quicker than wait
23 if (cg_enter(cgroup, object_pid) ||
24 cg_wait_for_proc_count(cgroup, 1))
25 return EXIT_FAILURE;
27 return EXIT_SUCCESS;
30 static int do_controller_fn(const char *cgroup, void *arg)
32 const char *child = cgroup;
33 const char *parent = arg;
35 if (setuid(TEST_UID))
36 return EXIT_FAILURE;
38 if (!cg_read_strstr(child, "cgroup.controllers", "cpuset"))
39 return EXIT_FAILURE;
41 if (cg_write(parent, "cgroup.subtree_control", "+cpuset"))
42 return EXIT_FAILURE;
44 if (cg_read_strstr(child, "cgroup.controllers", "cpuset"))
45 return EXIT_FAILURE;
47 if (cg_write(parent, "cgroup.subtree_control", "-cpuset"))
48 return EXIT_FAILURE;
50 if (!cg_read_strstr(child, "cgroup.controllers", "cpuset"))
51 return EXIT_FAILURE;
53 return EXIT_SUCCESS;
57 * Migrate a process between two sibling cgroups.
58 * The success should only depend on the parent cgroup permissions and not the
59 * migrated process itself (cpuset controller is in place because it uses
60 * security_task_setscheduler() in cgroup v1).
62 * Deliberately don't set cpuset.cpus in children to avoid definining migration
63 * permissions between two different cpusets.
65 static int test_cpuset_perms_object(const char *root, bool allow)
67 char *parent = NULL, *child_src = NULL, *child_dst = NULL;
68 char *parent_procs = NULL, *child_src_procs = NULL, *child_dst_procs = NULL;
69 const uid_t test_euid = TEST_UID;
70 int object_pid = 0;
71 int ret = KSFT_FAIL;
73 parent = cg_name(root, "cpuset_test_0");
74 if (!parent)
75 goto cleanup;
76 parent_procs = cg_name(parent, "cgroup.procs");
77 if (!parent_procs)
78 goto cleanup;
79 if (cg_create(parent))
80 goto cleanup;
82 child_src = cg_name(parent, "cpuset_test_1");
83 if (!child_src)
84 goto cleanup;
85 child_src_procs = cg_name(child_src, "cgroup.procs");
86 if (!child_src_procs)
87 goto cleanup;
88 if (cg_create(child_src))
89 goto cleanup;
91 child_dst = cg_name(parent, "cpuset_test_2");
92 if (!child_dst)
93 goto cleanup;
94 child_dst_procs = cg_name(child_dst, "cgroup.procs");
95 if (!child_dst_procs)
96 goto cleanup;
97 if (cg_create(child_dst))
98 goto cleanup;
100 if (cg_write(parent, "cgroup.subtree_control", "+cpuset"))
101 goto cleanup;
103 if (cg_read_strstr(child_src, "cgroup.controllers", "cpuset") ||
104 cg_read_strstr(child_dst, "cgroup.controllers", "cpuset"))
105 goto cleanup;
107 /* Enable permissions along src->dst tree path */
108 if (chown(child_src_procs, test_euid, -1) ||
109 chown(child_dst_procs, test_euid, -1))
110 goto cleanup;
112 if (allow && chown(parent_procs, test_euid, -1))
113 goto cleanup;
115 /* Fork a privileged child as a test object */
116 object_pid = cg_run_nowait(child_src, idle_process_fn, NULL);
117 if (object_pid < 0)
118 goto cleanup;
120 /* Carry out migration in a child process that can drop all privileges
121 * (including capabilities), the main process must remain privileged for
122 * cleanup.
123 * Child process's cgroup is irrelevant but we place it into child_dst
124 * as hacky way to pass information about migration target to the child.
126 if (allow ^ (cg_run(child_dst, do_migration_fn, (void *)(size_t)object_pid) == EXIT_SUCCESS))
127 goto cleanup;
129 ret = KSFT_PASS;
131 cleanup:
132 if (object_pid > 0) {
133 (void)kill(object_pid, SIGTERM);
134 (void)clone_reap(object_pid, WEXITED);
137 cg_destroy(child_dst);
138 free(child_dst_procs);
139 free(child_dst);
141 cg_destroy(child_src);
142 free(child_src_procs);
143 free(child_src);
145 cg_destroy(parent);
146 free(parent_procs);
147 free(parent);
149 return ret;
152 static int test_cpuset_perms_object_allow(const char *root)
154 return test_cpuset_perms_object(root, true);
157 static int test_cpuset_perms_object_deny(const char *root)
159 return test_cpuset_perms_object(root, false);
163 * Migrate a process between parent and child implicitely
164 * Implicit migration happens when a controller is enabled/disabled.
167 static int test_cpuset_perms_subtree(const char *root)
169 char *parent = NULL, *child = NULL;
170 char *parent_procs = NULL, *parent_subctl = NULL, *child_procs = NULL;
171 const uid_t test_euid = TEST_UID;
172 int object_pid = 0;
173 int ret = KSFT_FAIL;
175 parent = cg_name(root, "cpuset_test_0");
176 if (!parent)
177 goto cleanup;
178 parent_procs = cg_name(parent, "cgroup.procs");
179 if (!parent_procs)
180 goto cleanup;
181 parent_subctl = cg_name(parent, "cgroup.subtree_control");
182 if (!parent_subctl)
183 goto cleanup;
184 if (cg_create(parent))
185 goto cleanup;
187 child = cg_name(parent, "cpuset_test_1");
188 if (!child)
189 goto cleanup;
190 child_procs = cg_name(child, "cgroup.procs");
191 if (!child_procs)
192 goto cleanup;
193 if (cg_create(child))
194 goto cleanup;
196 /* Enable permissions as in a delegated subtree */
197 if (chown(parent_procs, test_euid, -1) ||
198 chown(parent_subctl, test_euid, -1) ||
199 chown(child_procs, test_euid, -1))
200 goto cleanup;
202 /* Put a privileged child in the subtree and modify controller state
203 * from an unprivileged process, the main process remains privileged
204 * for cleanup.
205 * The unprivileged child runs in subtree too to avoid parent and
206 * internal-node constraing violation.
208 object_pid = cg_run_nowait(child, idle_process_fn, NULL);
209 if (object_pid < 0)
210 goto cleanup;
212 if (cg_run(child, do_controller_fn, parent) != EXIT_SUCCESS)
213 goto cleanup;
215 ret = KSFT_PASS;
217 cleanup:
218 if (object_pid > 0) {
219 (void)kill(object_pid, SIGTERM);
220 (void)clone_reap(object_pid, WEXITED);
223 cg_destroy(child);
224 free(child_procs);
225 free(child);
227 cg_destroy(parent);
228 free(parent_subctl);
229 free(parent_procs);
230 free(parent);
232 return ret;
236 #define T(x) { x, #x }
237 struct cpuset_test {
238 int (*fn)(const char *root);
239 const char *name;
240 } tests[] = {
241 T(test_cpuset_perms_object_allow),
242 T(test_cpuset_perms_object_deny),
243 T(test_cpuset_perms_subtree),
245 #undef T
247 int main(int argc, char *argv[])
249 char root[PATH_MAX];
250 int i, ret = EXIT_SUCCESS;
252 if (cg_find_unified_root(root, sizeof(root), NULL))
253 ksft_exit_skip("cgroup v2 isn't mounted\n");
255 if (cg_read_strstr(root, "cgroup.subtree_control", "cpuset"))
256 if (cg_write(root, "cgroup.subtree_control", "+cpuset"))
257 ksft_exit_skip("Failed to set cpuset controller\n");
259 for (i = 0; i < ARRAY_SIZE(tests); i++) {
260 switch (tests[i].fn(root)) {
261 case KSFT_PASS:
262 ksft_test_result_pass("%s\n", tests[i].name);
263 break;
264 case KSFT_SKIP:
265 ksft_test_result_skip("%s\n", tests[i].name);
266 break;
267 default:
268 ret = EXIT_FAILURE;
269 ksft_test_result_fail("%s\n", tests[i].name);
270 break;
274 return ret;